import logging
import pytest
import torch
import torch_npu
import ops_multimodal_fusion
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
if not hasattr(torch.ops.ops_multimodal_fusion, "abs"):
pytest.skip(
"ops_multimodal_fusion.abs not registered for current NPU_ARCH; skipping module",
allow_module_level=True,
)
def test_abs_interface_exist():
"""Test that the 'ops_multimodal_fusion.abs' operator is registered in torch.ops."""
logging.info(torch.ops.ops_multimodal_fusion.abs)
assert hasattr(torch.ops.ops_multimodal_fusion, "abs"), \
"The 'abs' operator is not registered in the 'torch.ops.ops_multimodal_fusion' 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),
(100000,),
(1000000,),
(2048, 2048),
(4096, 1024),
(64, 128, 256),
(16, 16, 128, 128),
]
DTYPES = [
torch.float32,
torch.float16,
]
@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_abs_operator(shape, dtype):
"""
Test the abs operator with various shapes and data types.
Parameters:
shape: Tensor shape
dtype: Data type
"""
a = torch.randn(*shape, dtype=dtype)
expected = torch.abs(a)
a_npu = a.npu()
result_npu = torch.ops.ops_multimodal_fusion.abs(a_npu)
result = result_npu.cpu()
assert torch.allclose(result, expected, rtol=1e-4, atol=1e-4), \
f"Abs failed for shape {shape}, dtype {dtype}. " \
f"Max diff: {torch.max(torch.abs(result - expected)):.6f}"
logging.info(f"Test passed: shape={shape}, dtype={dtype}")