Ericu950 commited on
Commit
7ea8280
·
verified ·
1 Parent(s): 5151257

Fix: RoPE inv buffer could fail to materialize under some transformers meta-device loading paths, corrupting all attention output (see repo history for full diagnosis)

Browse files
Files changed (1) hide show
  1. modeling_char_bert.py +10 -3
modeling_char_bert.py CHANGED
@@ -34,11 +34,18 @@ class RMSNorm(nn.Module):
34
  class RoPE(nn.Module):
35
  def __init__(self, dim, base=10000.0):
36
  super().__init__()
37
- inv = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
38
- self.register_buffer("inv", inv, persistent=False)
39
 
40
  def cos_sin(self, pos):
41
- f = torch.outer(pos.float(), self.inv)
 
 
 
 
 
 
 
42
  emb = torch.cat([f, f], -1)
43
  return emb.cos(), emb.sin()
44
 
 
34
  class RoPE(nn.Module):
35
  def __init__(self, dim, base=10000.0):
36
  super().__init__()
37
+ self.dim = dim
38
+ self.base = base
39
 
40
  def cos_sin(self, pos):
41
+ # Recomputed on every call rather than cached in a registered buffer: a
42
+ # persistent=False buffer is never covered by the checkpoint's state dict,
43
+ # so it depends entirely on __init__-time materialization -- which some
44
+ # transformers versions' meta-device/low_cpu_mem_usage loading path can
45
+ # skip, silently leaving this tensor uninitialized. Recomputing here is
46
+ # immune to that regardless of how the model was constructed/loaded.
47
+ inv = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, device=pos.device).float() / self.dim))
48
+ f = torch.outer(pos.float(), inv)
49
  emb = torch.cat([f, f], -1)
50
  return emb.cos(), emb.sin()
51