Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional arg to specify device for Transformer model. #165

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions models/llama3/reference_impl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def repeat_kv(x: torch.Tensor, n_rep: int) -> torch.Tensor:


class Attention(nn.Module):
def __init__(self, args: ModelArgs):
def __init__(self, args: ModelArgs, device: torch.device = torch.device('cuda')):
super().__init__()
self.n_kv_heads = args.n_heads if args.n_kv_heads is None else args.n_kv_heads
model_parallel_size = fs_init.get_model_parallel_world_size()
Expand Down Expand Up @@ -158,15 +158,15 @@ def __init__(self, args: ModelArgs):
self.n_local_kv_heads,
self.head_dim,
)
).cuda()
).to(device)
self.cache_v = torch.zeros(
(
args.max_batch_size,
args.max_seq_len,
self.n_local_kv_heads,
self.head_dim,
)
).cuda()
).to(device)

def forward(
self,
Expand Down Expand Up @@ -245,12 +245,12 @@ def forward(self, x):


class TransformerBlock(nn.Module):
def __init__(self, layer_id: int, args: ModelArgs):
def __init__(self, layer_id: int, args: ModelArgs, device: torch.device = torch.device('cuda')):
super().__init__()
self.n_heads = args.n_heads
self.dim = args.dim
self.head_dim = args.dim // args.n_heads
self.attention = Attention(args)
self.attention = Attention(args, device)
self.feed_forward = FeedForward(
dim=args.dim,
hidden_dim=4 * args.dim,
Expand All @@ -274,7 +274,7 @@ def forward(


class Transformer(nn.Module):
def __init__(self, params: ModelArgs):
def __init__(self, params: ModelArgs, device: torch.device = torch.device('cuda')):
super().__init__()
self.params = params
self.vocab_size = params.vocab_size
Expand All @@ -286,7 +286,7 @@ def __init__(self, params: ModelArgs):

self.layers = torch.nn.ModuleList()
for layer_id in range(params.n_layers):
self.layers.append(TransformerBlock(layer_id, params))
self.layers.append(TransformerBlock(layer_id, params, device))

self.norm = RMSNorm(params.dim, eps=params.norm_eps)
self.output = ColumnParallelLinear(
Expand Down