ereniko commited on
Commit
3dc0499
·
verified ·
1 Parent(s): 959b481

Upload Ivme-Coder-v1 (Otter 1): safetensors + custom modeling code

Browse files
Files changed (2) hide show
  1. model/model.py +8 -2
  2. modeling_ivme_coder.py +8 -2
model/model.py CHANGED
@@ -64,8 +64,14 @@ def precompute_rope(dim, max_len, theta=10000.0, device="cpu"):
64
 
65
  def apply_rope(x, cos, sin):
66
  T = x.size(2)
67
- cos = cos[:T].unsqueeze(0).unsqueeze(0)
68
- sin = sin[:T].unsqueeze(0).unsqueeze(0)
 
 
 
 
 
 
69
  x1, x2 = x[..., 0::2], x[..., 1::2]
70
  rot1 = x1 * cos - x2 * sin
71
  rot2 = x1 * sin + x2 * cos
 
64
 
65
  def apply_rope(x, cos, sin):
66
  T = x.size(2)
67
+ # Cast cos/sin to match x's dtype. precompute_rope always builds these tables in
68
+ # fp32 for precision, but if q/k arrive in bf16 (as they do when the model is
69
+ # loaded with dtype=torch.bfloat16), multiplying against fp32 cos/sin silently
70
+ # promotes the result back to fp32 - which then doesn't match `v` (which never
71
+ # passes through this function and stays in bf16), and scaled_dot_product_attention
72
+ # requires q, k, v to share one dtype.
73
+ cos = cos[:T].unsqueeze(0).unsqueeze(0).to(x.dtype)
74
+ sin = sin[:T].unsqueeze(0).unsqueeze(0).to(x.dtype)
75
  x1, x2 = x[..., 0::2], x[..., 1::2]
76
  rot1 = x1 * cos - x2 * sin
77
  rot2 = x1 * sin + x2 * cos
modeling_ivme_coder.py CHANGED
@@ -64,8 +64,14 @@ def precompute_rope(dim, max_len, theta=10000.0, device="cpu"):
64
 
65
  def apply_rope(x, cos, sin):
66
  T = x.size(2)
67
- cos = cos[:T].unsqueeze(0).unsqueeze(0)
68
- sin = sin[:T].unsqueeze(0).unsqueeze(0)
 
 
 
 
 
 
69
  x1, x2 = x[..., 0::2], x[..., 1::2]
70
  rot1 = x1 * cos - x2 * sin
71
  rot2 = x1 * sin + x2 * cos
 
64
 
65
  def apply_rope(x, cos, sin):
66
  T = x.size(2)
67
+ # Cast cos/sin to match x's dtype. precompute_rope always builds these tables in
68
+ # fp32 for precision, but if q/k arrive in bf16 (as they do when the model is
69
+ # loaded with dtype=torch.bfloat16), multiplying against fp32 cos/sin silently
70
+ # promotes the result back to fp32 - which then doesn't match `v` (which never
71
+ # passes through this function and stays in bf16), and scaled_dot_product_attention
72
+ # requires q, k, v to share one dtype.
73
+ cos = cos[:T].unsqueeze(0).unsqueeze(0).to(x.dtype)
74
+ sin = sin[:T].unsqueeze(0).unsqueeze(0).to(x.dtype)
75
  x1, x2 = x[..., 0::2], x[..., 1::2]
76
  rot1 = x1 * cos - x2 * sin
77
  rot2 = x1 * sin + x2 * cos