import torch
import torch_npu
import ascend_ops
import pytest
def test_sqrt_interface_exist():
"""
Test that the 'ascend_ops.sqrt' operator is present in torch.ops.
"""
print(torch.ops.ascend_ops.sqrt)
assert hasattr(torch.ops.ascend_ops, "sqrt"), "The 'sqrt' operator is not registered in the 'torch.ops.ascend_ops' namespace."
SHAPES = [
(1,),
(3,),
(10,),
(100,),
(1024,),
(10000,),
(10, 10),
(32, 32),
(100, 100),
(10, 100),
(100, 10),
(256, 512),
(5, 10, 15),
(16, 32, 64),
(32, 64, 128),
(1, 3, 32, 32),
(4, 3, 64, 64),
(8, 3, 128, 128),
(1000, 1000),
]
DTYPES = [
torch.float32,
]
@pytest.mark.skipif(not torch.npu.is_available(), reason="NPU device not found")
@pytest.mark.parametrize("shape", SHAPES)
@pytest.mark.parametrize("dtype", DTYPES)
def test_sqrt_operator(shape, dtype):
"""
Test the functionality of the sqrt operator.
Parameters:
shape: Tensor shape
dtype: Data type
"""
a = torch.randn(*shape, dtype=dtype)
a = torch.abs(a)
expected = torch.sqrt(a)
a_npu = a.npu()
result_npu = torch.ops.ascend_ops.sqrt(a_npu)
result = result_npu.cpu()
assert torch.allclose(result, expected, rtol=1e-4, atol=1e-4), \
f"Sqrt failed for shape {shape}, dtype {dtype}. " \
f"Max diff: {torch.max(torch.abs(result - expected)):.6f}"
print(f"Test passed: shape={shape}, dtype={dtype}")