33
44from __future__ import annotations
55
6+ from collections .abc import Mapping
7+
68import numpy as np
79import onnx_ir as ir
810import pytest
@@ -28,6 +30,28 @@ def _tiny_overrides() -> dict:
2830 )
2931
3032
33+ def _linear_attention_state (
34+ states : torch .Tensor | Mapping [int , torch .Tensor | None ], state_name : str
35+ ) -> torch .Tensor :
36+ if isinstance (states , torch .Tensor ):
37+ return states
38+ elif isinstance (states , Mapping ):
39+ assert set (states ) == {0 }, (
40+ f"Falcon-H1 expected one { state_name } tensor at state index 0, "
41+ f"got indices { sorted (states )} "
42+ )
43+ state = states [0 ]
44+ assert state is not None , (
45+ f"Falcon-H1 expected { state_name } at state index 0 to be a tensor, got None"
46+ )
47+ return state
48+ else :
49+ raise AssertionError ( # noqa: TRY004 - unexpected test data is an assertion failure
50+ f"Falcon-H1 received unexpected { state_name } container type: "
51+ f"{ type (states ).__name__ } "
52+ )
53+
54+
3155def test_falcon_h1_rejects_invalid_attention_and_ssm_geometry () -> None :
3256 base = _tiny_overrides ()
3357 with pytest .raises (ValueError , match = "hidden_size must equal" ):
@@ -188,6 +212,10 @@ def test_falcon_h1_prefill_decode_logits_and_four_states_match_transformers() ->
188212 atol = 1e-3 ,
189213 )
190214 for layer , hf_state in enumerate (hf_prefill .past_key_values .layers ):
215+ hf_conv_state = _linear_attention_state (hf_state .conv_states , "convolution state" )
216+ hf_recurrent_state = _linear_attention_state (
217+ hf_state .recurrent_states , "recurrent state"
218+ )
191219 np .testing .assert_allclose (
192220 ort_prefill [f"present.{ layer } .key" ],
193221 hf_state .keys .numpy (),
@@ -202,13 +230,13 @@ def test_falcon_h1_prefill_decode_logits_and_four_states_match_transformers() ->
202230 )
203231 np .testing .assert_allclose (
204232 ort_prefill [f"present.{ layer } .conv_state" ],
205- hf_state . conv_states [:, :, - (config .mamba_d_conv - 1 ) :].numpy (),
233+ hf_conv_state [:, :, - (config .mamba_d_conv - 1 ) :].numpy (),
206234 rtol = 1e-4 ,
207235 atol = 1e-4 ,
208236 )
209237 np .testing .assert_allclose (
210238 ort_prefill [f"present.{ layer } .ssm_state" ],
211- hf_state . recurrent_states .transpose (2 , 3 ).numpy (),
239+ hf_recurrent_state .transpose (2 , 3 ).numpy (),
212240 rtol = 1e-3 ,
213241 atol = 1e-3 ,
214242 )
0 commit comments