|
36 | 36 | AutoConfig, |
37 | 37 | AutoModelForCausalLM, |
38 | 38 | AutoTokenizer, |
| 39 | + DynamicCache, |
39 | 40 | GPTBigCodeForCausalLM, |
40 | 41 | GPTNeoXForCausalLM, |
41 | 42 | MptConfig, |
@@ -903,6 +904,74 @@ def _run_promoted_ort( |
903 | 904 | return dict(zip(output_names, session.run(output_names, feeds), strict=True)) |
904 | 905 |
|
905 | 906 |
|
| 907 | +def _assert_starcoder2_long_context_sliding_window( |
| 908 | + session: ort.InferenceSession, |
| 909 | + reference: torch.nn.Module, |
| 910 | + token_id: int, |
| 911 | + *, |
| 912 | + atol: float, |
| 913 | +) -> None: |
| 914 | + """Prove the exact route applies StarCoder2's window past position 4096.""" |
| 915 | + window = int(reference.config.sliding_window) |
| 916 | + assert window == 4096 |
| 917 | + past_length = window |
| 918 | + num_layers = int(reference.config.num_hidden_layers) |
| 919 | + num_kv_heads = int(reference.config.num_key_value_heads) |
| 920 | + head_dim = int(reference.config.hidden_size // reference.config.num_attention_heads) |
| 921 | + |
| 922 | + state: dict[str, np.ndarray] = {} |
| 923 | + cache_data: list[tuple[torch.Tensor, torch.Tensor]] = [] |
| 924 | + for layer_idx in range(num_layers): |
| 925 | + key = np.zeros((1, num_kv_heads, past_length, head_dim), dtype=np.float32) |
| 926 | + value = np.zeros_like(key) |
| 927 | + # Only the oldest value is nonzero. Sliding attention must exclude it at |
| 928 | + # position 4096; full causal attention still observes it. |
| 929 | + value[:, :, 0, :] = 1024.0 |
| 930 | + state[f"past_key_values.{layer_idx}.key"] = key |
| 931 | + state[f"past_key_values.{layer_idx}.value"] = value |
| 932 | + cache_data.append((torch.from_numpy(key.copy()), torch.from_numpy(value.copy()))) |
| 933 | + |
| 934 | + input_ids = np.asarray([[token_id]], dtype=np.int64) |
| 935 | + ort_logits = _run_promoted_ort(session, input_ids, state, past_length)["logits"] |
| 936 | + attention_mask = torch.ones((1, past_length + 1), dtype=torch.int64) |
| 937 | + position_ids = torch.asarray([[past_length]], dtype=torch.int64) |
| 938 | + |
| 939 | + def reference_logits(sliding_window: int | None) -> np.ndarray: |
| 940 | + reference.config.sliding_window = sliding_window |
| 941 | + cache = DynamicCache( |
| 942 | + ((key.clone(), value.clone()) for key, value in cache_data), |
| 943 | + config=reference.config, |
| 944 | + ) |
| 945 | + with torch.no_grad(): |
| 946 | + return ( |
| 947 | + reference( |
| 948 | + torch.from_numpy(input_ids), |
| 949 | + attention_mask=attention_mask, |
| 950 | + position_ids=position_ids, |
| 951 | + past_key_values=cache, |
| 952 | + use_cache=False, |
| 953 | + ) |
| 954 | + .logits.numpy() |
| 955 | + .copy() |
| 956 | + ) |
| 957 | + |
| 958 | + try: |
| 959 | + expected_logits = reference_logits(window) |
| 960 | + np.testing.assert_allclose(ort_logits, expected_logits, rtol=1e-4, atol=atol) |
| 961 | + |
| 962 | + full_causal_logits = reference_logits(None) |
| 963 | + assert np.max(np.abs(ort_logits - full_causal_logits)) > 1e-3 |
| 964 | + with pytest.raises(AssertionError): |
| 965 | + np.testing.assert_allclose( |
| 966 | + ort_logits, |
| 967 | + full_causal_logits, |
| 968 | + rtol=1e-4, |
| 969 | + atol=atol, |
| 970 | + ) |
| 971 | + finally: |
| 972 | + reference.config.sliding_window = window |
| 973 | + |
| 974 | + |
906 | 975 | def _assert_replay_rollback_and_reorder( |
907 | 976 | session: ort.InferenceSession, |
908 | 977 | prompt_ids: np.ndarray, |
@@ -1680,6 +1749,14 @@ def capture_save(package: ModelPackage, *args: object, **kwargs: object) -> None |
1680 | 1749 | ) |
1681 | 1750 | del mutated, mutated_logits |
1682 | 1751 |
|
| 1752 | + if evidence.architecture == "starcoder2": |
| 1753 | + _assert_starcoder2_long_context_sliding_window( |
| 1754 | + session, |
| 1755 | + reference, |
| 1756 | + int(prompt_ids[0, -1]), |
| 1757 | + atol=case.atol, |
| 1758 | + ) |
| 1759 | + |
1683 | 1760 | state = _next_cache(ort_output) |
1684 | 1761 | generated: list[int] = [] |
1685 | 1762 | reference_generated: list[int] = [] |
|
0 commit comments