ThingsAI commited on
Commit
cd6716a
·
verified ·
1 Parent(s): 92a7d9d

fix: dtype cast in SDPA + tie_weights() esplicito

Browse files
Files changed (1) hide show
  1. modeling_quark.py +14 -7
modeling_quark.py CHANGED
@@ -46,8 +46,8 @@ class RotaryEmbedding(nn.Module):
46
  T = q.size(2)
47
  if T > self._max:
48
  self._build_cache(T)
49
- cos = self.cos_cache[:, :, :T, :]
50
- sin = self.sin_cache[:, :, :T, :]
51
  return q * cos + self._rotate_half(q) * sin, k * cos + self._rotate_half(k) * sin
52
 
53
 
@@ -75,6 +75,10 @@ class GroupedQueryAttention(nn.Module):
75
  if self.n_groups > 1:
76
  k = k.repeat_interleave(self.n_groups, dim=1)
77
  v = v.repeat_interleave(self.n_groups, dim=1)
 
 
 
 
78
  out = F.scaled_dot_product_attention(
79
  q, k, v, attn_mask=None,
80
  dropout_p=self.drop if self.training else 0.0,
@@ -110,7 +114,7 @@ class TransformerBlock(nn.Module):
110
 
111
 
112
  class QuarkPreTrainedModel(PreTrainedModel):
113
- config_class = QuarkConfig
114
  base_model_prefix = "model"
115
  supports_gradient_checkpointing = False
116
 
@@ -153,6 +157,11 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
153
  def set_output_embeddings(self, value):
154
  self.lm_head = value
155
 
 
 
 
 
 
156
  def forward(
157
  self,
158
  input_ids = None,
@@ -160,9 +169,9 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
160
  labels = None,
161
  **kwargs,
162
  ):
163
- x = self.embed_tokens(input_ids)
164
  for layer in self.layers:
165
- x = layer(x, attention_mask=attention_mask)
166
  x = self.norm(x)
167
  logits = self.lm_head(x)
168
 
@@ -179,7 +188,6 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
179
  @torch.no_grad()
180
  def generate_text(self, input_ids, max_new_tokens=200, temperature=0.7,
181
  top_p=0.9, eos_token_id=None):
182
- """Generazione semplice senza dipendere da .generate() HF."""
183
  ctx = input_ids.clone()
184
  for _ in range(max_new_tokens):
185
  out = self(ctx[:, -self.config.max_seq_len:])
@@ -187,7 +195,6 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
187
  if temperature > 0:
188
  logits /= temperature
189
  probs = F.softmax(logits, dim=-1)
190
- # top-p
191
  sorted_p, sorted_i = torch.sort(probs, descending=True)
192
  cum_p = torch.cumsum(sorted_p, dim=-1)
193
  remove = cum_p - sorted_p > top_p
 
46
  T = q.size(2)
47
  if T > self._max:
48
  self._build_cache(T)
49
+ cos = self.cos_cache[:, :, :T, :].to(dtype=q.dtype)
50
+ sin = self.sin_cache[:, :, :T, :].to(dtype=q.dtype)
51
  return q * cos + self._rotate_half(q) * sin, k * cos + self._rotate_half(k) * sin
52
 
53
 
 
75
  if self.n_groups > 1:
76
  k = k.repeat_interleave(self.n_groups, dim=1)
77
  v = v.repeat_interleave(self.n_groups, dim=1)
78
+ # Forza dtype uniforme prima di SDPA
79
+ dtype = q.dtype
80
+ k = k.to(dtype)
81
+ v = v.to(dtype)
82
  out = F.scaled_dot_product_attention(
83
  q, k, v, attn_mask=None,
84
  dropout_p=self.drop if self.training else 0.0,
 
114
 
115
 
116
  class QuarkPreTrainedModel(PreTrainedModel):
117
+ config_class = QuarkConfig
118
  base_model_prefix = "model"
119
  supports_gradient_checkpointing = False
120
 
 
157
  def set_output_embeddings(self, value):
158
  self.lm_head = value
159
 
160
+ def tie_weights(self):
161
+ """HF chiama questo metodo dopo il caricamento dei pesi."""
162
+ if self.config.tie_word_embeddings:
163
+ self.lm_head.weight = self.embed_tokens.weight
164
+
165
  def forward(
166
  self,
167
  input_ids = None,
 
169
  labels = None,
170
  **kwargs,
171
  ):
172
+ x = self.embed_tokens(input_ids)
173
  for layer in self.layers:
174
+ x = layer(x, attention_mask=attention_mask)
175
  x = self.norm(x)
176
  logits = self.lm_head(x)
177
 
 
188
  @torch.no_grad()
189
  def generate_text(self, input_ids, max_new_tokens=200, temperature=0.7,
190
  top_p=0.9, eos_token_id=None):
 
191
  ctx = input_ids.clone()
192
  for _ in range(max_new_tokens):
193
  out = self(ctx[:, -self.config.max_seq_len:])
 
195
  if temperature > 0:
196
  logits /= temperature
197
  probs = F.softmax(logits, dim=-1)
 
198
  sorted_p, sorted_i = torch.sort(probs, descending=True)
199
  cum_p = torch.cumsum(sorted_p, dim=-1)
200
  remove = cum_p - sorted_p > top_p