Download code/models/tt_dit/tests/unit/test_linear.py from sunandbean/diffrhythm-1.2-p100a: direct link, hf CLI and curl.
- Browser
- Download file 9.47 kB
-
https://huggingface.co/sunandbean/diffrhythm-1.2-p100a/resolve/main/code/models/tt_dit/tests/unit/test_linear.py
- Command line
-
hf download hf://sunandbean/diffrhythm-1.2-p100a/code/models/tt_dit/tests/unit/test_linear.py
-
curl -L -o test_linear.py https://huggingface.co/sunandbean/diffrhythm-1.2-p100a/resolve/main/code/models/tt_dit/tests/unit/test_linear.py
9.47 kB
| # SPDX-FileCopyrightText: © 2025 Tenstorrent USA, Inc. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| import pytest | |
| import torch | |
| import ttnn | |
| from ...layers.linear import ColParallelLinear, Linear, RowParallelLinear | |
| from ...parallel.manager import CCLManager | |
| from ...utils.check import assert_quality | |
| from ...utils.tensor import bf16_tensor | |
| from ...utils.test import mesh_device_config_to_string | |
| def test_linear( | |
| mesh_device: ttnn.MeshDevice, | |
| B: int, | |
| M: int, | |
| K: int, | |
| N: int, | |
| bias: bool, | |
| ) -> None: | |
| torch_dtype = torch.bfloat16 | |
| torch_model = torch.nn.Linear(K, N, bias=bias).to(dtype=torch_dtype) | |
| torch_model.eval() | |
| tt_model = Linear(K, N, bias=bias, mesh_device=mesh_device) | |
| tt_model.load_torch_state_dict(torch_model.state_dict()) | |
| torch_input_tensor = torch.randn((1, B, M, K), dtype=torch_dtype) | |
| tt_input_tensor = bf16_tensor(torch_input_tensor, device=mesh_device) | |
| with torch.no_grad(): | |
| torch_output = torch_model(torch_input_tensor) | |
| tt_output = tt_model(tt_input_tensor) | |
| for t in ttnn.get_device_tensors(tt_output): | |
| t = ttnn.to_torch(t) | |
| assert_quality(torch_output, t, pcc=0.999_500) | |
| def test_col_parallel_linear( | |
| mesh_device: ttnn.MeshDevice, | |
| B: int, | |
| M: int, | |
| K: int, | |
| N: int, | |
| bias: bool, | |
| tp_mesh_axis: int, | |
| is_fsdp: bool, | |
| ) -> None: | |
| torch_dtype = torch.bfloat16 | |
| torch_model = torch.nn.Linear(K, N, bias=bias).to(dtype=torch_dtype) | |
| torch_model.eval() | |
| if is_fsdp: | |
| fsdp_mesh_axis = 1 - tp_mesh_axis | |
| ccl_manager = CCLManager(mesh_device, topology=ttnn.Topology.Linear) | |
| tt_model = ColParallelLinear( | |
| K, | |
| N, | |
| bias=bias, | |
| mesh_device=mesh_device, | |
| mesh_axis=tp_mesh_axis, | |
| fsdp_mesh_axis=fsdp_mesh_axis, | |
| ccl_manager=ccl_manager, | |
| ) | |
| else: | |
| tt_model = ColParallelLinear(K, N, bias=bias, mesh_device=mesh_device, mesh_axis=tp_mesh_axis) | |
| tt_model.load_torch_state_dict(torch_model.state_dict()) | |
| torch_input_tensor = torch.randn((1, B, M, K), dtype=torch_dtype) | |
| tt_input_tensor = bf16_tensor(torch_input_tensor, device=mesh_device) | |
| with torch.no_grad(): | |
| torch_output = torch_model(torch_input_tensor) | |
| tt_output = tt_model(tt_input_tensor) | |
| shard_dims = [None, None] | |
| shard_dims[tp_mesh_axis] = -1 | |
| shard_dims[1 - tp_mesh_axis] = 0 | |
| tt_output = ttnn.to_torch( | |
| tt_output, | |
| mesh_composer=ttnn.ConcatMesh2dToTensor(mesh_device, dims=shard_dims, mesh_shape=tuple(mesh_device.shape)), | |
| ) | |
| for i in range(tt_output.shape[0]): | |
| assert_quality(torch_output.squeeze(), tt_output[i].squeeze(), pcc=0.999_500) | |
| def test_row_parallel_linear( | |
| mesh_device: ttnn.MeshDevice, | |
| B: int, | |
| M: int, | |
| K: int, | |
| N: int, | |
| bias: bool, | |
| tp_mesh_axis: int, | |
| is_fsdp: bool, | |
| ) -> None: | |
| torch_dtype = torch.bfloat16 | |
| torch_model = torch.nn.Linear(K, N, bias=bias).to(dtype=torch_dtype) | |
| torch_model.eval() | |
| fsdp_mesh_axis = 1 - tp_mesh_axis if is_fsdp else None | |
| ccl_manager = CCLManager(mesh_device, topology=ttnn.Topology.Linear) | |
| tt_model = RowParallelLinear( | |
| K, | |
| N, | |
| bias=bias, | |
| mesh_device=mesh_device, | |
| mesh_axis=tp_mesh_axis, | |
| fsdp_mesh_axis=fsdp_mesh_axis, | |
| ccl_manager=ccl_manager, | |
| ) | |
| tt_model.load_torch_state_dict(torch_model.state_dict()) | |
| torch_input_tensor = torch.randn((1, B, M, K), dtype=torch_dtype) | |
| tt_input_tensor = bf16_tensor(torch_input_tensor, device=mesh_device, mesh_axis=tp_mesh_axis, shard_dim=-1) | |
| with torch.no_grad(): | |
| torch_output = torch_model(torch_input_tensor) | |
| tt_output = tt_model(tt_input_tensor) | |
| shard_dims = [None, None] | |
| shard_dims[tp_mesh_axis] = -1 | |
| shard_dims[1 - tp_mesh_axis] = 0 | |
| tt_output = ttnn.to_torch( | |
| tt_output, | |
| mesh_composer=ttnn.ConcatMesh2dToTensor(mesh_device, dims=shard_dims, mesh_shape=tuple(mesh_device.shape)), | |
| ) | |
| for i in range(tt_output.shape[0]): | |
| assert_quality(torch_output.squeeze(), tt_output[i].squeeze(), pcc=0.999_500) | |