MCPcopy Create free account
hub / github.com/csuhan/OneLLM / MetaModel

Class MetaModel

model/meta.py:12–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12class MetaModel(nn.Module):
13
14 def __init__(self, llama_type, llama_config, llama_ckpt_dir=None, tokenizer_path=None):
15 super().__init__()
16
17 self.criterion = torch.nn.CrossEntropyLoss(ignore_index=0)
18
19 ModelArgs = LLM.__dict__[llama_type].ModelArgs
20 Transformer = LLM.__dict__[llama_type].Transformer
21
22 with open(llama_config, "r") as f:
23 params = json.loads(f.read())
24 model_args: ModelArgs = ModelArgs(
25 max_seq_len=2048, max_batch_size=32, **params
26 )
27 self.tokenizer = Tokenizer(model_path=tokenizer_path)
28 model_args.vocab_size = self.tokenizer.n_words
29
30 model = Transformer(model_args)
31 mp_rank = fs_init.get_model_parallel_rank()
32 if llama_ckpt_dir is not None:
33 ckpt_path = os.path.join(llama_ckpt_dir, f"consolidated.{mp_rank:02d}.pth")
34 if os.path.exists(ckpt_path):
35 checkpoint = torch.load(ckpt_path, map_location="cpu")
36 msg = model.load_state_dict(checkpoint, strict=False)
37 print(msg)
38 else:
39 print(f'Checkpoint not found at {ckpt_path}')
40 self.llma = model
41 for name, param in self.named_parameters():
42 if param.requires_grad:
43 print(f"Trainable param: {name}, {param.shape}, {param.dtype}")
44 count = sum(p.numel() for p in self.parameters() if p.requires_grad)
45 print(f"Parameter count : {count}")
46
47 def forward(self, examples, labels, image=None, modal='image'):
48 output = self.llma(examples, image=image, modal=modal)
49 output = output[:, :-1, :]
50 labels = labels[:, 1:]
51
52 if labels.sum() == 0:
53 c_loss = output.mean() * 0
54 else:
55 c_loss = self.criterion(output.reshape(-1, 32000), labels.flatten())
56
57 return c_loss
58
59 def generate(
60 self,
61 prompts: List[str],
62 images,
63 max_gen_len: int,
64 temperature: float = 0.8,
65 top_p: float = 0.95,
66 modal = ['image'],
67 ) -> List[str]:
68 bsz = len(prompts)
69 params = self.llma.params

Callers 11

mainFunction · 0.90
mainFunction · 0.90
fmri_cap_nsd.pyFile · 0.90
video_qa_msvd.pyFile · 0.90
imu_cap_ego4d.pyFile · 0.90
model_workerFunction · 0.90
model_workerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected