ThingsAI commited on
Commit
38feb38
·
verified ·
1 Parent(s): 4e1e026

fix: inv_freq calcolato runtime, non buffer (evita corruzione meta-device)

Browse files
Files changed (1) hide show
  1. modeling_quark.py +12 -9
modeling_quark.py CHANGED
@@ -26,18 +26,21 @@ class RMSNorm(nn.Module):
26
  class RotaryEmbedding(nn.Module):
27
  def __init__(self, head_dim, max_seq_len, theta=10_000.0):
28
  super().__init__()
29
- inv_freq = 1.0 / (theta ** (torch.arange(0, head_dim, 2).float() / head_dim))
30
- self.register_buffer("inv_freq", inv_freq, persistent=False)
 
 
31
  self.max_seq_len = max_seq_len
32
- self._max = 0 # forza build al primo forward, mai nell'__init__
33
- self.cos_cache = None
34
- self.sin_cache = None
35
 
36
  def _build_cache(self, seq_len, device, dtype):
37
- # Ricalcola sempre da inv_freq corrente (mai cache stantia da meta-device)
38
- t = torch.arange(seq_len, device=device, dtype=torch.float32)
39
- freqs = torch.outer(t, self.inv_freq.to(device=device, dtype=torch.float32))
40
- emb = torch.cat([freqs, freqs], dim=-1)
 
41
  self.cos_cache = emb.cos()[None, None].to(dtype)
42
  self.sin_cache = emb.sin()[None, None].to(dtype)
43
  self._max = seq_len
 
26
  class RotaryEmbedding(nn.Module):
27
  def __init__(self, head_dim, max_seq_len, theta=10_000.0):
28
  super().__init__()
29
+ # head_dim/theta come Python float, NON tensori gestiti da HF —
30
+ # evita corruzione da meta-device init durante from_pretrained()
31
+ self.head_dim = head_dim
32
+ self.theta = theta
33
  self.max_seq_len = max_seq_len
34
+ self._max = 0
35
+ self.cos_cache = None
36
+ self.sin_cache = None
37
 
38
  def _build_cache(self, seq_len, device, dtype):
39
+ # Ricalcola inv_freq da zero ogni volta niente stato persistito
40
+ inv_freq = 1.0 / (self.theta ** (torch.arange(0, self.head_dim, 2, device=device).float() / self.head_dim))
41
+ t = torch.arange(seq_len, device=device, dtype=torch.float32)
42
+ freqs = torch.outer(t, inv_freq)
43
+ emb = torch.cat([freqs, freqs], dim=-1)
44
  self.cos_cache = emb.cos()[None, None].to(dtype)
45
  self.sin_cache = emb.sin()[None, None].to(dtype)
46
  self._max = seq_len