A straightforward method for training your LLM, from downloading data to generating text.
fix: ensure entire model is on same device
Tianyi (Alex) Qiua4e2f608a881
diff --git a/scripts/train_transformer.py b/scripts/train_transformer.pyindex 6c6351d..c22b8d3 100644--- a/scripts/train_transformer.py+++ b/scripts/train_transformer.py@@ -19,6 +19,7 @@ model = Transformer(emb_dim=config.N_EMBED,num_heads=config.N_HEAD,num_blocks=config.N_BLOCKS,+ device=device,).to(device)criterion = torch.nn.CrossEntropyLoss()diff --git a/src/models/transformer.py b/src/models/transformer.pyindex 1250faf..49d1140 100644--- a/src/models/transformer.py+++ b/src/models/transformer.py@@ -14,13 +14,16 @@ class Transformer(nn.Module):blocks: nn.ModuleListlm_head: nn.Linear- def __init__(self, text_dim: int, ctx_len: int, emb_dim: int, num_heads: int, num_blocks: int):+ model_device: torch.device++ def __init__(self, text_dim: int, ctx_len: int, emb_dim: int, num_heads: int, num_blocks: int, device: torch.device = None):super().__init__()self.text_dim = text_dimself.ctx_len = ctx_lenself.emb_dim = emb_dimself.num_heads = num_headsself.num_blocks = num_blocks+ self.model_device = deviceself.text_emb = nn.Embedding(text_dim, emb_dim)self.pos_emb = nn.Embedding(ctx_len, emb_dim)@@ -35,7 +38,7 @@ class Transformer(nn.Module):"""Transformer forward pass. Input 2-D tensor contains raw tokens, NOT one-hot encoded."""assert len(x.shape) == 2 and x.shape[-1] <= self.ctx_len- x = self.text_emb(x) + self.pos_emb(torch.arange(x.shape[-1]))+ x = self.text_emb(x) + self.pos_emb(torch.arange(x.shape[-1], device=self.model_device))assert len(x.shape) == 3 and x.shape[2] == self.emb_dimfor block in self.blocks: