"""AddExamplePypto kernel — the PyPTO counterpart of examples/add_example.
Elementwise add/sub over a 2D FP16 tensor, written in the PyPTO DSL instead of AscendC C++.
The build picks this file up because op_host/CMakeLists.txt calls enable_pypto_kernel(add_example_pypto):
cmake runs scripts/util/pypto_codegen.py over it at configure time, which emits
* AddExamplePyptoTiling_tiling.h -- the tiling struct the host tiling fills in
* AddExamplePyptoTilingKey_tilingkey.h -- the ASCENDC_TPL tiling-key declarations
* add_example_pypto_pypto_infer.cpp -- the self-contained source used for .i infer
The first two are force-included into add_example_pypto_tiling.cpp, so the host side shares this file's
tiling layout and tiling-key bit assignment without a hand-written copy of either.
"""
from dataclasses import dataclass
import pypto_pro.language as pl
from pypto_pro.runtime.tilingkey import TilingKeyField
@dataclass
class AddExamplePyptoTiling:
rows: int
columns: int
class AddExamplePyptoTilingKey:
Operation = TilingKeyField(bits=1, values=[0, 1])
TILE_ROWS = 16
TILE_COLUMNS = 16
TILE_BYTES = TILE_ROWS * TILE_COLUMNS * 2
@pl.jit(auto_mutex=True, tiling_key=AddExamplePyptoTilingKey)
def add_example_pypto(
x1: pl.Ptr[pl.DT_FP16],
x2: pl.Ptr[pl.DT_FP16],
y: pl.Ptr[pl.DT_FP16],
workspace: pl.Ptr[pl.DT_UINT8],
tiling: AddExamplePyptoTiling,
):
x1_tensor = pl.make_tensor(
x1, [tiling.rows, tiling.columns], [tiling.columns, 1], dtype=pl.DT_FP16
)
x2_tensor = pl.make_tensor(
x2, [tiling.rows, tiling.columns], [tiling.columns, 1], dtype=pl.DT_FP16
)
y_tensor = pl.make_tensor(
y, [tiling.rows, tiling.columns], [tiling.columns, 1], dtype=pl.DT_FP16
)
tile_type = pl.TileType(
shape=[TILE_ROWS, TILE_COLUMNS],
dtype=pl.DT_FP16,
target_memory=pl.MemorySpace.Vec,
)
x1_group = pl.make_tile_group(
type=tile_type, addrs=[0x0000, 0x0200], mutex_ids=[0, 1]
)
x2_group = pl.make_tile_group(
type=tile_type, addrs=[0x0400, 0x0600], mutex_ids=[2, 3]
)
y_group = pl.make_tile_group(
type=tile_type, addrs=[0x0800, 0x0A00], mutex_ids=[4, 5]
)
with pl.section_vector():
for row in pl.range(0, tiling.rows, TILE_ROWS):
for column in pl.range(0, tiling.columns, TILE_COLUMNS):
tile_x1 = x1_group.next()
tile_x2 = x2_group.next()
tile_y = y_group.next()
pl.load(tile_x1, x1_tensor, [row, column])
pl.load(tile_x2, x2_tensor, [row, column])
if Operation == 0:
pl.add(tile_y, tile_x1, tile_x2)
else:
pl.sub(tile_y, tile_x1, tile_x2)
pl.store(y_tensor, tile_y, [row, column])