Download code/models/tt_dit/tests/unit/test_conv2d.py from sunandbean/diffrhythm-1.2-p100a: direct link, hf CLI and curl.
- Browser
- Download file 5.03 kB
-
https://huggingface.co/sunandbean/diffrhythm-1.2-p100a/resolve/main/code/models/tt_dit/tests/unit/test_conv2d.py
- Command line
-
hf download hf://sunandbean/diffrhythm-1.2-p100a/code/models/tt_dit/tests/unit/test_conv2d.py
-
curl -L -o test_conv2d.py https://huggingface.co/sunandbean/diffrhythm-1.2-p100a/resolve/main/code/models/tt_dit/tests/unit/test_conv2d.py
5.03 kB
| # SPDX-FileCopyrightText: © 2025 Tenstorrent USA, Inc. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| import pytest | |
| import torch | |
| import ttnn | |
| from ...layers.conv2d import Conv2d | |
| from ...parallel.config import vae_all_gather | |
| from ...parallel.manager import CCLManager | |
| from ...utils.check import assert_quality | |
| def test_conv2d( | |
| *, | |
| mesh_device: ttnn.Device, | |
| batch: int, | |
| height: int, | |
| width: int, | |
| in_channels: int, | |
| out_channels: int, | |
| kernel: int, | |
| padding: int, | |
| stride: int, | |
| shard_input: bool, | |
| mesh_axis: int, | |
| ) -> None: | |
| ccl_manager = CCLManager(mesh_device, topology=ttnn.Topology.Linear) | |
| # models | |
| torch_model = torch.nn.Conv2d( | |
| in_channels=in_channels, out_channels=out_channels, kernel_size=kernel, padding=padding, stride=stride | |
| ) | |
| torch.nn.init.normal_(torch_model.weight) | |
| torch.nn.init.normal_(torch_model.bias) | |
| torch_model.eval() | |
| tt_model = Conv2d.from_torch( | |
| torch_ref=torch_model, mesh_device=mesh_device, out_mesh_axis=mesh_axis, ccl_manager=ccl_manager | |
| ) | |
| torch_input = torch.randn(batch, in_channels, height, width) | |
| tt_input_tensor = ttnn.from_torch( | |
| torch_input.permute(0, 2, 3, 1), | |
| dtype=ttnn.bfloat16, | |
| device=mesh_device, | |
| mesh_mapper=ttnn.ShardTensorToMesh(mesh_device, dim=-1) if shard_input else None, | |
| layout=ttnn.TILE_LAYOUT, | |
| ) | |
| with torch.no_grad(): | |
| torch_output = torch_model(torch_input) | |
| tt_out = tt_model(tt_input_tensor) | |
| if mesh_axis is not None: | |
| tt_out = vae_all_gather(ccl_manager, tt_out) | |
| tt_final_out_torch = ttnn.to_torch(ttnn.get_device_tensors(tt_out)[0]).permute(0, 3, 1, 2) | |
| assert_quality(torch_output, tt_final_out_torch, pcc=0.999_500) | |
| def test_conv2d_input_parallel( | |
| *, | |
| mesh_device: ttnn.Device, | |
| batch: int, | |
| height: int, | |
| width: int, | |
| in_channels: int, | |
| out_channels: int, | |
| kernel: int, | |
| padding: int, | |
| stride: int, | |
| mesh_axis: int, | |
| ) -> None: | |
| ccl_manager = CCLManager(mesh_device, topology=ttnn.Topology.Linear) | |
| # models | |
| torch_model = torch.nn.Conv2d( | |
| in_channels=in_channels, out_channels=out_channels, kernel_size=kernel, padding=padding, stride=stride | |
| ) | |
| torch.nn.init.normal_(torch_model.weight) | |
| torch.nn.init.normal_(torch_model.bias) | |
| torch_model.eval() | |
| tt_model = Conv2d.from_torch( | |
| torch_ref=torch_model, mesh_device=mesh_device, in_mesh_axis=mesh_axis, ccl_manager=ccl_manager | |
| ) | |
| torch_input = torch.randn(batch, in_channels, height, width) | |
| tt_input_tensor = ttnn.from_torch( | |
| torch_input.permute(0, 2, 3, 1), | |
| dtype=ttnn.bfloat16, | |
| device=mesh_device, | |
| mesh_mapper=ttnn.ShardTensorToMesh(mesh_device, dim=-1), | |
| layout=ttnn.TILE_LAYOUT, | |
| ) | |
| with torch.no_grad(): | |
| torch_output = torch_model(torch_input) | |
| tt_out = tt_model(tt_input_tensor) | |
| tt_out = vae_all_gather(ccl_manager, tt_out) | |
| tt_final_out_torch = ttnn.to_torch(ttnn.get_device_tensors(tt_out)[0]).permute(0, 3, 1, 2) | |
| assert_quality(torch_output, tt_final_out_torch, pcc=0.999_500) | |