"""
This test case verifies that the swimlane diagram generation for the costmodel works correctly
regardless of whether the CANN is installed in the environment or not.
"""
import numpy as np
from numpy.testing import assert_allclose
import torch
import pypto
@pypto.frontend.jit(
runtime_options={"run_mode": 1}
)
def add_kernel(
x: pypto.Tensor([...], pypto.DT_FP32),
y: pypto.Tensor([...], pypto.DT_FP32),
out: pypto.Tensor([...], pypto.DT_FP32),
):
"""
Add implementation.
Parameters
----------
x : pypto.Tensor
First input tensor
y : pypto.Tensor
Second input tensor
out : pypto.Tensor
Output tensor
"""
pypto.set_vec_tile_shapes(1, 4, 1, 64)
out[:] = x + y
def test_add():
"""
Test add implementation against PyTorch reference.
"""
shape = (1, 4, 1, 64)
input_data0 = torch.rand(shape, dtype=torch.float32)
input_data1 = torch.rand(shape, dtype=torch.float32)
output_data = torch.empty(shape, dtype=torch.float32)
add_kernel(input_data0, input_data1, output_data)
golden = torch.add(input_data0, input_data1)
assert_allclose(np.array(output_data.cpu()), np.array(golden.cpu()), rtol=3e-3, atol=3e-3)
if __name__ == "__main__":
test_add()