Lottolabs's picture
tt-model push qwen3.8-flash-next-coder-tt-p150 (container)
f57e879 verified
Raw History Blame Contribute Delete
6.38 kB
# SPDX-FileCopyrightText: © 2026 Tenstorrent AI ULC
# SPDX-License-Identifier: Apache-2.0
"""
Tests for eager == traced executor output parity.
The real contract is "same inputs, same outputs, regardless of executor".
These tests verify that EagerLLMExecutor and TracedLLMExecutor produce
identical outputs for the same model and inputs.
"""
import pytest
import torch
import ttnn
from models.common.models.executor import EagerLLMExecutor, TensorSpec, TracedLLMExecutor
from models.common.models.module_input_validation import ConfigMismatch
class TestTensorSpec:
"""Unit tests for TensorSpec dataclass."""
def test_tensor_spec_creation(self):
"""Test TensorSpec can be created with expected fields."""
spec = TensorSpec(
shape=(32, 1, 128256),
dtype=ttnn.bfloat16,
layout=ttnn.TILE_LAYOUT,
memory_config=None,
)
assert spec.shape == (32, 1, 128256)
assert spec.dtype == ttnn.bfloat16
assert spec.layout == ttnn.TILE_LAYOUT
assert spec.memory_config is None
def test_tensor_spec_from_tensor_requires_device(self):
"""Test TensorSpec.from_tensor requires an allocated device tensor."""
# This test documents expected behavior - from_tensor needs a real device tensor
# Can't test without device, but we can test the method signature exists
assert hasattr(TensorSpec, "from_tensor")
assert callable(TensorSpec.from_tensor)
class TestConfigMismatch:
"""Unit tests for ConfigMismatch dataclass."""
def test_config_mismatch_creation(self):
"""Test ConfigMismatch can be created with expected fields."""
mismatch = ConfigMismatch(
module_name="layer[0].attention",
expected_memcfg=ttnn.DRAM_MEMORY_CONFIG,
actual_memcfg=ttnn.L1_MEMORY_CONFIG,
)
assert mismatch.module_name == "layer[0].attention"
assert mismatch.expected_memcfg == ttnn.DRAM_MEMORY_CONFIG
assert mismatch.actual_memcfg == ttnn.L1_MEMORY_CONFIG
class TestBoundaryAssertions:
"""Tests for boundary assertions on public methods."""
@pytest.fixture
def mock_model(self):
"""Create a minimal mock model for testing assertions."""
class MockModel:
vocab_size = 128256
n_layers = 32
num_devices = 1
sampling = None
model_args = None
def set_kv_cache(self, kv_cache):
pass
return MockModel()
@pytest.fixture
def mock_mesh_device(self):
"""Return None as placeholder - real device tests need hardware."""
return None
def test_prefill_forward_rejects_1d_tokens(self, mock_model):
"""Test that prefill_forward rejects 1D token tensor."""
# Can't fully test without device, but can test assertion message format
tokens_1d = torch.tensor([1, 2, 3])
assert tokens_1d.dim() == 1
# The assertion should fail with a clear message
with pytest.raises(AssertionError, match=r"tokens must be \[batch_size, seq_len\], got 1D"):
# We can't call the actual method without a device, but we can test the assertion logic
assert tokens_1d.dim() == 2, f"tokens must be [batch_size, seq_len], got {tokens_1d.dim()}D"
def test_prefill_forward_accepts_2d_tokens(self):
"""Test that 2D tokens pass the assertion."""
tokens_2d = torch.tensor([[1, 2, 3]])
assert tokens_2d.dim() == 2 # Should pass
def test_decode_forward_rejects_2d_tokens(self):
"""Test that decode_forward rejects 2D token tensor."""
tokens_2d = torch.tensor([[1, 2, 3]])
assert tokens_2d.dim() == 2
with pytest.raises(AssertionError, match=r"tokens must be \[batch_size\], got 2D"):
assert tokens_2d.dim() == 1, f"tokens must be [batch_size], got {tokens_2d.dim()}D"
def test_decode_forward_accepts_1d_tokens(self):
"""Test that 1D tokens pass the decode assertion."""
tokens_1d = torch.tensor([1, 2, 3])
assert tokens_1d.dim() == 1 # Should pass
def test_page_table_must_be_2d(self):
"""Test that page_table must be 2D when provided."""
page_table_1d = torch.tensor([0, 1, 2])
assert page_table_1d.dim() == 1
with pytest.raises(AssertionError, match=r"page_table must be \[batch_size, max_blocks\], got 1D"):
assert page_table_1d.dim() == 2, f"page_table must be [batch_size, max_blocks], got {page_table_1d.dim()}D"
def test_start_pos_must_be_1d(self):
"""Test that start_pos must be 1D."""
start_pos_2d = torch.tensor([[0, 1, 2]])
assert start_pos_2d.dim() == 2
with pytest.raises(AssertionError, match=r"start_pos must be \[batch_size\], got 2D"):
assert start_pos_2d.dim() == 1, f"start_pos must be [batch_size], got {start_pos_2d.dim()}D"
class TestOutputSpecCapture:
"""Tests for output spec capture during compile."""
def test_eager_executor_has_output_spec_attributes(self):
"""Test that EagerLLMExecutor has output spec attributes."""
class MockModel:
vocab_size = 128256
n_layers = 32
num_devices = 1
sampling = None
model_args = None
def set_kv_cache(self, kv_cache):
pass
# Can't fully instantiate without device, but can check class structure
assert hasattr(EagerLLMExecutor, "__init__")
def test_traced_executor_has_output_spec_attributes(self):
"""Test that TracedLLMExecutor has output spec attributes."""
assert hasattr(TracedLLMExecutor, "__init__")
class TestValidateModuleConfigs:
"""Tests for the _validate_module_configs context manager."""
def test_validate_module_configs_method_exists(self):
"""Test that _validate_module_configs method exists on EagerLLMExecutor."""
assert hasattr(EagerLLMExecutor, "_validate_module_configs")
def test_compile_accepts_validate_configs_flag(self):
"""Test that compile() accepts validate_configs parameter."""
import inspect
sig = inspect.signature(EagerLLMExecutor.compile)
params = sig.parameters
assert "validate_configs" in params
assert params["validate_configs"].default is False