"""
Add consistency validation cases for torch.Tensor.map_.
This file adds self-contained, focused validation for torch.Tensor.map_ as requested by the
Ascend for PyTorch API consistency task (#2720). Extendable.
PyTorch community test: test/test_torch.py::test_broadcast includes "map" in its
parametrized fn list, but the test is explicitly skipped on CUDA devices
("map and map2 are not implemented on CUDA tensors"). NPU follows the same
behavior -- map_ is CPU-only, so functional validation is done on CPU tensors.
NPU tensors are verified to raise the expected error.
"""
import torch
from torch.testing._internal.common_utils import run_tests, TestCase
class TestTensorMap(TestCase):
def test_map_applies_callable(self):
dst = torch.zeros(3)
src = torch.tensor([1.0, 2.0, 3.0])
dst.map_(src, lambda d, s: s * 2)
self.assertEqual(dst, torch.tensor([2.0, 4.0, 6.0]))
def test_map_uses_destination_values(self):
dst = torch.tensor([10.0, 20.0])
src = torch.tensor([1.0, 2.0])
dst.map_(src, lambda d, s: d + s)
self.assertEqual(dst, torch.tensor([11.0, 22.0]))
def test_map_raises_on_npu_tensor(self):
dst = torch.zeros(3).npu()
src = torch.tensor([1.0, 2.0, 3.0]).npu()
with self.assertRaises(TypeError) as ctx:
dst.map_(src, lambda d, s: s * 2)
self.assertIn("map_ is only implemented on CPU", str(ctx.exception))
if __name__ == "__main__":
run_tests()