Skip to content

Commit 9008d6d

Browse files
justinchubyCopilot
andcommitted
Make OnnxGenerator KV-cache init dtype-aware (f16/bf16)
OnnxGenerator allocated all past_key_values / recurrent-state buffers as float32, so feeding them into an f16/bf16 ONNX export raised an ORT input dtype mismatch — breaking the half-precision CUDA path advertised by the Hy-MT2 example. Resolve each cache input's dtype from the session (get_input_dtype, which maps bf16 via ml_dtypes) and allocate the zero buffers accordingly, falling back to float32 when unknown. Validated: examples/hy_mt2.py --dtype f16 now runs and matches HuggingFace token-for-token; the f32 path is unchanged. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent b754f4d commit 9008d6d

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎src/mobius/_testing/generation.py‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ def generate(
6161
head_dim = self.config.head_dim
6262
layer_types = self.config.layer_types or []
6363

64-
# Initialize empty past KV / recurrent state per layer
64+
# Initialize empty past KV / recurrent state per layer.
65+
# Resolve each input's dtype from the session so f16/bf16 exports get
66+
# matching cache buffers (the graph type-checks even zero-length KV).
67+
def _kv_dtype(name: str) -> np.dtype:
68+
return self.session.get_input_dtype(name) or np.dtype(np.float32)
69+
6570
past_kv: dict[str, np.ndarray] = {}
6671
for i in range(num_layers):
6772
ltype = layer_types[i] if i < len(layer_types) else "full_attention"
@@ -75,29 +80,31 @@ def generate(
7580
name = f"past_key_values.{i}.{suffix}"
7681
shape = self.session.get_input_shape(name) or []
7782
static = [d if isinstance(d, int) and d > 0 else batch_size for d in shape]
78-
past_kv[name] = np.zeros(static, dtype=np.float32)
83+
past_kv[name] = np.zeros(static, dtype=_kv_dtype(name))
7984
elif ltype == "lightning_attention":
8085
# Single recurrent state only (no conv_state)
8186
name = f"past_key_values.{i}.recurrent_state"
8287
shape = self.session.get_input_shape(name) or []
8388
static = [d if isinstance(d, int) and d > 0 else batch_size for d in shape]
84-
past_kv[name] = np.zeros(static, dtype=np.float32)
89+
past_kv[name] = np.zeros(static, dtype=_kv_dtype(name))
8590
elif ltype == "conv":
8691
# ShortConv conv_state only (no SSM state)
8792
name = f"past_key_values.{i}.conv_state"
8893
shape = self.session.get_input_shape(name) or []
8994
static = [d if isinstance(d, int) and d > 0 else batch_size for d in shape]
90-
past_kv[name] = np.zeros(static, dtype=np.float32)
95+
past_kv[name] = np.zeros(static, dtype=_kv_dtype(name))
9196
elif ltype in ("mlp", "moe"):
9297
# Pure feed-forward layers (e.g. NemotronH hybrid) carry no
9398
# attention KV and no recurrent state — nothing to initialize.
9499
continue
95100
else:
96-
past_kv[f"past_key_values.{i}.key"] = np.zeros(
97-
(batch_size, num_kv_heads, 0, head_dim), dtype=np.float32
101+
key_name = f"past_key_values.{i}.key"
102+
value_name = f"past_key_values.{i}.value"
103+
past_kv[key_name] = np.zeros(
104+
(batch_size, num_kv_heads, 0, head_dim), dtype=_kv_dtype(key_name)
98105
)
99-
past_kv[f"past_key_values.{i}.value"] = np.zeros(
100-
(batch_size, num_kv_heads, 0, head_dim), dtype=np.float32
106+
past_kv[value_name] = np.zeros(
107+
(batch_size, num_kv_heads, 0, head_dim), dtype=_kv_dtype(value_name)
101108
)
102109

103110
all_ids = input_ids.copy()

0 commit comments

Comments
 (0)