ThingsAI commited on
Commit
33147aa
·
verified ·
1 Parent(s): efc6baa

Upload modeling_quark.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_quark.py +12 -2
modeling_quark.py CHANGED
@@ -74,13 +74,22 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
74
  self.layers=nn.ModuleList([QuarkBlock(config) for _ in range(config.n_layers)])
75
  self.norm=QuarkRMSNorm(config.d_model,config.rms_eps)
76
  self.lm_head=nn.Linear(config.d_model,config.vocab_size,bias=False)
77
- self.lm_head.weight=self.embed_tokens.weight
78
  self.post_init()
79
- self.lm_head.weight = self.model.embed_tokens.weight
 
 
 
 
 
 
 
 
80
  def get_input_embeddings(self): return self.embed_tokens
81
  def set_input_embeddings(self, v): self.embed_tokens=v
82
  def get_output_embeddings(self): return self.lm_head
83
  def set_output_embeddings(self, v): self.lm_head=v
 
84
  def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
85
  h=self.embed_tokens(input_ids)
86
  for layer in self.layers: h=layer(h)
@@ -90,4 +99,5 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
90
  loss=F.cross_entropy(logits[...,:-1,:].contiguous().view(-1,self.config.vocab_size),
91
  labels[...,1:].contiguous().view(-1),ignore_index=-100)
92
  return CausalLMOutputWithPast(loss=loss, logits=logits)
 
93
  def prepare_inputs_for_generation(self, input_ids, **kwargs): return {"input_ids": input_ids}
 
74
  self.layers=nn.ModuleList([QuarkBlock(config) for _ in range(config.n_layers)])
75
  self.norm=QuarkRMSNorm(config.d_model,config.rms_eps)
76
  self.lm_head=nn.Linear(config.d_model,config.vocab_size,bias=False)
77
+ self.lm_head.weight=self.embed_tokens.weight # weight tying
78
  self.post_init()
79
+
80
+ def _load_from_state_dict(self, state_dict, prefix, *args, **kwargs):
81
+ """Se lm_head.weight manca, copia da embed_tokens.weight (weight tying)"""
82
+ lm_key = f"{prefix}lm_head.weight"
83
+ emb_key = f"{prefix}embed_tokens.weight"
84
+ if lm_key not in state_dict and emb_key in state_dict:
85
+ state_dict[lm_key] = state_dict[emb_key].clone()
86
+ super()._load_from_state_dict(state_dict, prefix, *args, **kwargs)
87
+
88
  def get_input_embeddings(self): return self.embed_tokens
89
  def set_input_embeddings(self, v): self.embed_tokens=v
90
  def get_output_embeddings(self): return self.lm_head
91
  def set_output_embeddings(self, v): self.lm_head=v
92
+
93
  def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
94
  h=self.embed_tokens(input_ids)
95
  for layer in self.layers: h=layer(h)
 
99
  loss=F.cross_entropy(logits[...,:-1,:].contiguous().view(-1,self.config.vocab_size),
100
  labels[...,1:].contiguous().view(-1),ignore_index=-100)
101
  return CausalLMOutputWithPast(loss=loss, logits=logits)
102
+
103
  def prepare_inputs_for_generation(self, input_ids, **kwargs): return {"input_ids": input_ids}