Files
TorchNPU/test/distributed/tensor/test_dtensor_ops.py
T
pta-robot 509fa6ae70 modify test case
Co-authored-by: pta-robot<pta_robot@163.com>
Co-authored-by: gcw_Dgfy2aKk<dujunjie2@huawei.com>



# message auto-generated for no-merge-commit merge:
!29184 merge correct_testcase_260107 into master

modify test case

Created-by: gcw_Dgfy2aKk
Commit-by: gcw_Dgfy2aKk;pta-robot
Merged-by: ascend-robot
Description: <!--  Thanks for sending a pull request! 
-->

**What type of PR is this?**
> Uncomment only one ` /kind <>` line, hit enter to put that in a new line, and remove leading whitespaces from that line:
>
> /kind bug
> /kind task
> /kind feature


**What does this PR do / why do we need it**:


**Which issue(s) this PR fixes**:
<!-- 
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

**Special notes for your reviewers**:




See merge request: Ascend/pytorch!29184
2026-01-12 19:18:35 +08:00

31 lines
1.2 KiB
Python

import torch
from torch.distributed._tensor import distribute_tensor, Replicate
from torch_npu.testing.testcase import run_tests
from torch_npu.testing._internal.common_dtensor import NPUDTensorTestBase
from torch_npu.testing.common_distributed import with_comms, skipIfUnsupportMultiNPU
from torch_npu.testing.common_utils import SupportedDevices
class TestDTensorOps(NPUDTensorTestBase):
@skipIfUnsupportMultiNPU(2)
@with_comms
def test_torch_nn_functional_one_hot(self):
"""Test torch.nn.functional.one_hot with DTensor on NPU"""
# Build device mesh for distributed tensor operations on NPU
device_mesh = self.build_device_mesh()
input_tensor = torch.randint(0, 4, (5,), device="npu")
num_classes = 5
x = distribute_tensor(input_tensor, device_mesh, [Replicate()])
# Apply one-hot encoding on distributed tensor
one_hot_tensor = torch.nn.functional.one_hot(x, num_classes)
# Verify the output
self.assertTrue(hasattr(one_hot_tensor, "to_local"))
self.assertEqual(one_hot_tensor.shape, (5, 5))
self.assertEqual(one_hot_tensor.placements, (Replicate(),))
if __name__ == "__main__":
run_tests()