Download code/models/tt_transformers/tests/test_model_config_utils.py from DeAIafdeling/qwen38-flash-next: direct link, hf CLI and curl.
- Browser
- Download file 6.55 kB
-
https://huggingface.co/DeAIafdeling/qwen38-flash-next/resolve/main/code/models/tt_transformers/tests/test_model_config_utils.py
- Command line
-
hf download hf://DeAIafdeling/qwen38-flash-next/code/models/tt_transformers/tests/test_model_config_utils.py
-
curl -L -o test_model_config_utils.py https://huggingface.co/DeAIafdeling/qwen38-flash-next/resolve/main/code/models/tt_transformers/tests/test_model_config_utils.py
6.55 kB
| # SPDX-FileCopyrightText: © 2026 Tenstorrent USA, Inc. | |
| # | |
| # SPDX-License-Identifier: Apache-2.0 | |
| from types import SimpleNamespace | |
| import pytest | |
| 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_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 | |