devstral-small-2-24b-instruct-2512 / code /models /tt_transformers /tests /test_model_config_utils.py
devstral-small-2-24b-instruct-2512 / code /models /tt_transformers /tests /test_model_config_utils.py
Download code/models/tt_transformers/tests/test_model_config_utils.py from anirud/devstral-small-2-24b-instruct-2512: direct link, hf CLI and curl.
- Browser
- Download file 8.29 kB
-
https://huggingface.co/anirud/devstral-small-2-24b-instruct-2512/resolve/main/code/models/tt_transformers/tests/test_model_config_utils.py
- Command line
-
hf download hf://anirud/devstral-small-2-24b-instruct-2512/code/models/tt_transformers/tests/test_model_config_utils.py
-
curl -L -o test_model_config_utils.py https://huggingface.co/anirud/devstral-small-2-24b-instruct-2512/resolve/main/code/models/tt_transformers/tests/test_model_config_utils.py
8.29 kB
| # SPDX-FileCopyrightText: © 2026 Tenstorrent USA, Inc. | |
| # | |
| # SPDX-License-Identifier: Apache-2.0 | |
| from types import MethodType, SimpleNamespace | |
| import pytest | |
| import ttnn | |
| from models.tt_transformers.tt.common import Mode | |
| from models.tt_transformers.tt.model_config import ( | |
| ModelArgs, | |
| TensorGroup, | |
| compute_galaxy_padded_vocab_size, | |
| compute_galaxy_width_shard_cores, | |
| compute_padded_vocab_size, | |
| create_galaxy_ff1_out_reduce_scatter_memcfg, | |
| should_pad_sampling_logits_to_power_of_2, | |
| ) | |
| def test_qwen_t3k_prefill_matmul_configs_match_across_batching(monkeypatch, seq_len): | |
| args = SimpleNamespace(base_model_name="Qwen3-32B", device_name="T3K", mlp2_grid=lambda _: (8, 8)) | |
| args.use_minimal_prefill_matmul = MethodType(ModelArgs.use_minimal_prefill_matmul, args) | |
| args.use_minimal_qkv_prefill_matmul = MethodType(ModelArgs.use_minimal_qkv_prefill_matmul, args) | |
| monkeypatch.setattr("models.tt_transformers.tt.model_config.is_blackhole", lambda: False) | |
| assert args.use_minimal_prefill_matmul(seq_len) | |
| assert args.use_minimal_qkv_prefill_matmul(seq_len) | |
| qkv = ModelArgs.get_attn_qkv_program_config.__wrapped__(args, Mode.PREFILL, seq_len) | |
| ff2 = ModelArgs.get_mlp_ff2_prg_config.__wrapped__(args, Mode.PREFILL, seq_len) | |
| assert isinstance(qkv, ttnn.MinimalMatmulConfig) | |
| assert isinstance(ff2, ttnn.MinimalMatmulConfig) | |
| def test_other_short_prefill_matmul_policies_are_preserved(model_name, device_name, galaxy_row, qkv_minimal): | |
| args = SimpleNamespace( | |
| base_model_name=model_name, device_name=device_name, is_galaxy_8_device_row_submesh=galaxy_row | |
| ) | |
| args.use_minimal_prefill_matmul = MethodType(ModelArgs.use_minimal_prefill_matmul, args) | |
| assert not args.use_minimal_prefill_matmul(128) | |
| assert ModelArgs.use_minimal_qkv_prefill_matmul(args, 128) is qkv_minimal | |
| def test_compute_padded_vocab_size(vocab_size, num_devices, expected): | |
| padded_vocab_size = compute_padded_vocab_size(vocab_size, num_devices) | |
| assert padded_vocab_size == expected | |
| assert padded_vocab_size >= vocab_size | |
| assert padded_vocab_size % (32 * num_devices) == 0 | |
| assert (padded_vocab_size // num_devices) % 32 == 0 | |
| def test_compute_padded_vocab_size_rejects_invalid_num_devices(expect_error): | |
| with expect_error(ValueError, "num_devices must be >= 1"): | |
| compute_padded_vocab_size(32000, 0) | |
| def test_compute_galaxy_padded_vocab_size(vocab_size, num_devices, expected): | |
| padded_vocab_size = compute_galaxy_padded_vocab_size(vocab_size, num_devices) | |
| assert padded_vocab_size == expected | |
| assert padded_vocab_size >= vocab_size | |
| assert padded_vocab_size % (32 * num_devices) == 0 | |
| def test_compute_galaxy_width_shard_cores(width, expected): | |
| num_cores = compute_galaxy_width_shard_cores(width) | |
| assert num_cores == expected | |
| assert (width // num_cores) % 32 == 0 | |
| def test_galaxy_ff1_out_reduce_scatter_memory_config(hidden_dim, expected_cores, expected_shard_width): | |
| memory_config = create_galaxy_ff1_out_reduce_scatter_memcfg(hidden_dim, mesh_rows=8, mesh_cols=4) | |
| assert memory_config.shard_spec.grid.num_cores() == expected_cores | |
| assert memory_config.shard_spec.shape == [32, expected_shard_width] | |
| def test_galaxy_ff1_shard_grid_exactly_covers_the_scattered_width(hidden_dim): | |
| """The shard grid must cover the reduce-scatter output width exactly, no more.""" | |
| memory_config = create_galaxy_ff1_out_reduce_scatter_memcfg(hidden_dim, mesh_rows=8, mesh_cols=4) | |
| scattered_width = hidden_dim // 8 // 4 | |
| covered = memory_config.shard_spec.grid.num_cores() * memory_config.shard_spec.shape[1] | |
| assert covered == scattered_width | |
| assert memory_config.shard_spec.shape[1] % 32 == 0 | |
| def test_galaxy_ff1_matches_the_validated_demo_ratio(): | |
| """Cross-check against models/demos/llama3_70b_galaxy: 3840 in, 960 out, ratio 4.""" | |
| memory_config = create_galaxy_ff1_out_reduce_scatter_memcfg(28672, mesh_rows=8, mesh_cols=4) | |
| covered = memory_config.shard_spec.grid.num_cores() * memory_config.shard_spec.shape[1] | |
| per_device_width = 28672 // 8 | |
| assert covered == per_device_width // 4 == 896 | |
| assert covered <= 960 # the demo's 30-core layout pads 896 up to 960 | |
| def test_compute_galaxy_width_shard_cores_rejects_non_positive_width(expect_error): | |
| with expect_error(ValueError, "positive multiple"): | |
| compute_galaxy_width_shard_cores(0) | |
| def test_should_pad_sampling_logits_to_power_of_2(base_model_name, padded_vocab_size, sampling_splits, expected): | |
| assert should_pad_sampling_logits_to_power_of_2(base_model_name, padded_vocab_size, sampling_splits) is expected | |
| def test_should_pad_sampling_logits_to_power_of_2_rejects_invalid_sampling_splits(expect_error): | |
| with expect_error(ValueError, "sampling_splits must be >= 1"): | |
| should_pad_sampling_logits_to_power_of_2("Llama-3.1-70B", 128256, 0) | |
| def _llama_model_args(device_name="P150"): | |
| args = object.__new__(ModelArgs) | |
| args.model_name = "Llama-3.1-8B-Instruct" | |
| args.device_name = device_name | |
| args.dram_grid_size = SimpleNamespace(x=8) | |
| return args | |
| def test_llama31_8b_p150_uses_projection_specific_dram_reader_counts(tensor_group, n): | |
| args = _llama_model_args() | |
| assert args.get_dram_sharded_matmul_num_workers(tensor_group, n) == 2 | |
| def test_llama31_8b_p150_uses_one_dram_reader_when_padded_shards_cannot_split_evenly(): | |
| args = _llama_model_args() | |
| # PAD_MLP_CORES=9 pads the gate/up width to 14,400, or 57 tiles per P150 DRAM bank. | |
| assert args.get_dram_sharded_matmul_num_workers(TensorGroup.FF1_FF3, 14400) == 1 | |
| def test_llama31_8b_dram_reader_counts_remain_default_on_unvalidated_devices(): | |
| args = _llama_model_args(device_name="P300") | |
| for tensor_group in (TensorGroup.WQKV, TensorGroup.WO, TensorGroup.FF1_FF3, TensorGroup.FF2): | |
| assert args.get_dram_sharded_matmul_num_workers(tensor_group, 14336) == 1 | |