Merged
docs(en): Add English docs and clean up figures #7294
lyz0328Add on 29 days ago
docs(en): Add English docs and clean up figures #7294
Merged
Merged fromlyz0328/pypto9.2.0intocann/pypto9.2.0
Total: 805 files changed+58808-5
| @@ -0,0 +1,9 @@ | |||
| 1 | +# PyPTO API Reference | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=c757335981cdfedb9fcaea5bed6c066a8dc94f1f translatedAt=2026-10-26T12:41:34.973Z pushedAt=2026-10-28T11:57:28.583Z nl=2 --> | ||
| 4 | + | ||
| 5 | +```{toctree} | ||
| 6 | +:maxdepth: 2 | ||
| 7 | + | ||
| 8 | +tensor_api/index | ||
| 9 | +pro_api/index | ||
| @@ -0,0 +1,562 @@ | |||
| 1 | +# PyPTO API Reference | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=6dc68739e530164e836f473e2668f6a3df7ad7fd translatedAt=2026-10-26T12:42:21.910Z pushedAt=2026-10-28T11:57:28.599Z nl=2 --> | ||
| 4 | + | ||
| 5 | +- [PyPTO Tensor API<a npu_parse_enabled="true"></a>](tensor_api/index.md) | ||
| 6 | + - [Tensors](tensor_api/tensor/index.md) | ||
| 7 | + - [Introduction to pypto.Tensor](tensor_api/tensor/pypto-Tensor_introduction.md) | ||
| 8 | + - [pypto.Tensor Constructor](tensor_api/tensor/pypto-Tensor_constructor.md) | ||
| 9 | + - [pypto.Tensor.add](tensor_api/tensor/pypto-Tensor-add.md) | ||
| 10 | + - [pypto.Tensor.amax](tensor_api/tensor/pypto-Tensor-amax.md) | ||
| 11 | + - [pypto.Tensor.amin](tensor_api/tensor/pypto-Tensor-amin.md) | ||
| 12 | + - [pypto.Tensor.assemble](tensor_api/tensor/pypto-Tensor-assemble.md) | ||
| 13 | + - [pypto.Tensor.clone](tensor_api/tensor/pypto-Tensor-clone.md) | ||
| 14 | + - [pypto.Tensor.concat](tensor_api/tensor/pypto-Tensor-concat.md) | ||
| 15 | + - [pypto.Tensor.cos](tensor_api/tensor/pypto-Tensor-cos.md) | ||
| 16 | + - [pypto.Tensor.cumprod](tensor_api/tensor/pypto-Tensor-cumprod.md) | ||
| 17 | + - [pypto.Tensor.cumsum](tensor_api/tensor/pypto-Tensor-cumsum.md) | ||
| 18 | + - [pypto.Tensor.dim](tensor_api/tensor/pypto-Tensor-dim.md) | ||
| 19 | + - [pypto.Tensor.div](tensor_api/tensor/pypto-Tensor-div.md) | ||
| 20 | + - [pypto.Tensor.dtype](tensor_api/tensor/pypto-Tensor-dtype.md) | ||
| 21 | + - [pypto.Tensor.exp](tensor_api/tensor/pypto-Tensor-exp.md) | ||
| 22 | + - [pypto.Tensor.exp2](tensor_api/tensor/pypto-Tensor-exp2.md) | ||
| 23 | + - [pypto.Tensor.expand\_clone](tensor_api/tensor/pypto-Tensor-expand_clone.md) | ||
| 24 | + - [pypto.Tensor.expm1](tensor_api/tensor/pypto-Tensor-expm1.md) | ||
| 25 | + - [pypto.Tensor.fmod](tensor_api/tensor/pypto-Tensor-fmod.md) | ||
| 26 | + - [pypto.Tensor.id](tensor_api/tensor/pypto-Tensor-id.md) | ||
| 27 | + - [pypto.Tensor.index\_add](tensor_api/tensor/pypto-Tensor-index_add.md) | ||
| 28 | + - [pypto.Tensor.index\_add\_](tensor_api/tensor/pypto-Tensor-index_add_.md) | ||
| 29 | + - [pypto.Tensor.format](tensor_api/tensor/pypto-Tensor-format.md) | ||
| 30 | + - [pypto.Tensor.gather](tensor_api/tensor/pypto-Tensor-gather.md) | ||
| 31 | + - [pypto.Tensor.gcd](tensor_api/tensor/pypto-Tensor-gcd.md) | ||
| 32 | + - [pypto.Tensor.get\_cache\_policy](tensor_api/tensor/pypto-Tensor-get_cache_policy.md) | ||
| 33 | + - [pypto.Tensor.log](tensor_api/tensor/pypto-Tensor-log.md) | ||
| 34 | + - [pypto.Tensor.log10](tensor_api/tensor/pypto-Tensor-log10.md) | ||
| 35 | + - [pypto.Tensor.log2](tensor_api/tensor/pypto-Tensor-log2.md) | ||
| 36 | + - [pypto.Tensor.logical\_not](tensor_api/tensor/pypto-Tensor-logical_not.md) | ||
| 37 | + - [pypto.Tensor.matmul](tensor_api/tensor/pypto-Tensor-matmul.md) | ||
| 38 | + - [pypto.Tensor.maximum](tensor_api/tensor/pypto-Tensor-maximum.md) | ||
| 39 | + - [pypto.Tensor.move](tensor_api/tensor/pypto-Tensor-move.md) | ||
| 40 | + - [pypto.Tensor.mul](tensor_api/tensor/pypto-Tensor-mul.md) | ||
| 41 | + - [pypto.Tensor.name](tensor_api/tensor/pypto-Tensor-name.md) | ||
| 42 | + - [pypto.Tensor.permute](tensor_api/tensor/pypto-Tensor-permute.md) | ||
| 43 | + - [pypto.Tensor.reciprocal](tensor_api/tensor/pypto-Tensor-reciprocal.md) | ||
| 44 | + - [pypto.Tensor.remainder](tensor_api/tensor/pypto-Tensor-remainder.md) | ||
| 45 | + - [pypto.Tensor.reshape](tensor_api/tensor/pypto-Tensor-reshape.md) | ||
| 46 | + - [pypto.Tensor.round](tensor_api/tensor/pypto-Tensor-round.md) | ||
| 47 | + - [pypto.Tensor.set\_cache\_policy](tensor_api/tensor/pypto-Tensor-set_cache_policy.md) | ||
| 48 | + - [pypto.Tensor.scatter](tensor_api/tensor/pypto-Tensor-scatter.md) | ||
| 49 | + - [pypto.Tensor.scatter\_](tensor_api/tensor/pypto-Tensor-scatter_.md) | ||
| 50 | + - [pypto.Tensor.scatter\_update](tensor_api/tensor/pypto-Tensor-scatter_update.md) | ||
| 51 | + - [pypto.Tensor.shape](tensor_api/tensor/pypto-Tensor-shape.md) | ||
| 52 | + - [pypto.Tensor.rsqrt](tensor_api/tensor/pypto-Tensor-rsqrt.md) | ||
| 53 | + - [pypto.Tensor.sigmoid](tensor_api/tensor/pypto-Tensor-sigmoid.md) | ||
| 54 | + - [pypto.Tensor.sin](tensor_api/tensor/pypto-Tensor-sin.md) | ||
| 55 | + - [pypto.Tensor.softmax](tensor_api/tensor/pypto-Tensor-softmax.md) | ||
| 56 | + - [pypto.Tensor.sqrt](tensor_api/tensor/pypto-Tensor-sqrt.md) | ||
| 57 | + - [pypto.Tensor.ceil](tensor_api/tensor/pypto-Tensor-ceil.md) | ||
| 58 | + - [pypto.Tensor.floor](tensor_api/tensor/pypto-Tensor-floor.md) | ||
| 59 | + - [pypto.Tensor.trunc](tensor_api/tensor/pypto-Tensor-trunc.md) | ||
| 60 | + - [pypto.Tensor.sub](tensor_api/tensor/pypto-Tensor-sub.md) | ||
| 61 | + - [pypto.Tensor.sum](tensor_api/tensor/pypto-Tensor-sum.md) | ||
| 62 | + - [pypto.Tensor.topk](tensor_api/tensor/pypto-Tensor-topk.md) | ||
| 63 | + - [pypto.Tensor.transpose](tensor_api/tensor/pypto-Tensor-transpose.md) | ||
| 64 | + - [pypto.Tensor.tril](tensor_api/tensor/pypto-Tensor-tril.md) | ||
| 65 | + - [pypto.Tensor.tril_](tensor_api/tensor/pypto-Tensor-tril_.md) | ||
| 66 | + - [pypto.Tensor.triu](tensor_api/tensor/pypto-Tensor-triu.md) | ||
| 67 | + - [pypto.Tensor.triu_](tensor_api/tensor/pypto-Tensor-triu_.md) | ||
| 68 | + - [pypto.Tensor.unsqueeze](tensor_api/tensor/pypto-Tensor-unsqueeze.md) | ||
| 69 | + - [pypto.Tensor.view](tensor_api/tensor/pypto-Tensor-view.md) | ||
| 70 | + - [pypto.Tensor.where](tensor_api/tensor/pypto-Tensor-where.md) | ||
| 71 | + - [pypto.Tensor Indexing Feature Description](tensor_api/tensor/pypto-Tensor-getitem.md) | ||
| 72 | + - [Scalar](tensor_api/element/index.md) | ||
| 73 | + - [Introduction to pypto.Element](tensor_api/element/pypto-Element_introduction.md) | ||
| 74 | + - [pypto.Element Constructor](tensor_api/element/pypto-Element_constructor.md) | ||
| 75 | + - [pypto.Element.dtype](tensor_api/element/pypto-Element-dtype.md) | ||
| 76 | + - [pypto.Element.value](tensor_api/element/pypto-Element-value.md) | ||
| 77 | + - [Operations](tensor_api/operation/index.md) | ||
| 78 | + - [pypto.abs](tensor_api/operation/pypto-abs.md) | ||
| 79 | + - [pypto.acos](tensor_api/operation/pypto-acos.md) | ||
| 80 | + - [pypto.acosh](tensor_api/operation/pypto-acosh.md) | ||
| 81 | + - [pypto.add](tensor_api/operation/pypto-add.md) | ||
| 82 | + - [pypto.amax](tensor_api/operation/pypto-amax.md) | ||
| 83 | + - [pypto.amin](tensor_api/operation/pypto-amin.md) | ||
| 84 | + - [pypto.arange](tensor_api/operation/pypto-arange.md) | ||
| 85 | + - [pypto.argmax](tensor_api/operation/pypto-argmax.md) | ||
| 86 | + - [pypto.argmin](tensor_api/operation/pypto-argmin.md) | ||
| 87 | + - [pypto.argsort](tensor_api/operation/pypto-argsort.md) | ||
| 88 | + - [pypto.asin](tensor_api/operation/pypto-asin.md) | ||
| 89 | + - [pypto.asinh](tensor_api/operation/pypto-asinh.md) | ||
| 90 | + - [pypto.assemble](tensor_api/operation/pypto-assemble.md) | ||
| 91 | + - [pypto.atan](tensor_api/operation/pypto-atan.md) | ||
| 92 | + - [pypto.atan2](tensor_api/operation/pypto-atan2.md) | ||
| 93 | + - [pypto.atanh](tensor_api/operation/pypto-atanh.md) | ||
| 94 | + - [pypto.atomic\_add](tensor_api/operation/pypto-atomic_add.md) | ||
| 95 | + - [pypto.axpy_](tensor_api/operation/pypto-axpy_.md) | ||
| 96 | + - [pypto.bitwise\_and](tensor_api/operation/pypto-bitwise_and.md) | ||
| 97 | + - [pypto.bitwise_left_shift](tensor_api/operation/pypto-bitwise_left_shift.md) | ||
| 98 | + - [pypto.bitwise\_not](tensor_api/operation/pypto-bitwise_not.md) | ||
| 99 | + - [pypto.bitwise\_or](tensor_api/operation/pypto-bitwise_or.md) | ||
| 100 | + - [pypto.bitwise_right_shift](tensor_api/operation/pypto-bitwise_right_shift.md) | ||
| @@ -0,0 +1,158 @@ | |||
| 1 | +# vf.de_interleave | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=7d0ab31d84c294a0a8327f597d2bdee012acb22d translatedAt=2026-10-26T13:05:20.796Z pushedAt=2026-10-28T11:57:28.729Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +Given source operands src0 and src1, de-interleave the elements in src0 and src1 and store them into destination operands dst0 and dst1. The de-interleaving layout is shown in the following figure, where each square represents an element: | ||
| 20 | + | ||
| 21 | +$$dstReg0_i = srcReg_{2i}, \quad dstReg1_i = srcReg_{2i+1}$$ | ||
| 22 | + | ||
| 23 | +**Figure 1** de_interleave de-interleaving layout | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +## Prototype | ||
| 28 | + | ||
| 29 | +```python | ||
| 30 | +de_interleave(src0, src1, dtype: Optional[DType] = None) -> (dst0, dst1) | ||
| 31 | +``` | ||
| 32 | + | ||
| 33 | +## Parameters | ||
| 34 | + | ||
| 35 | +| Parameter | Input/Output | Description | | ||
| 36 | +|---|---|---| | ||
| 37 | +| src0 | Input | Source operand, of the [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type.<br>- **reg_tensor input**: The data types of source operands src0 and src1 and destination operand dst must be consistent. Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_FP16, DT_BF16, DT_INT32, DT_UINT32, DT_FP32.<br>- **mask_reg input**: Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_FP16, DT_BF16, DT_INT32, DT_UINT32, DT_FP32. | | ||
| 38 | +| src1 | Input | Source operand, of the [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type. The supported data types are the same as those of src0. | | ||
| 39 | +| dtype | Input | Optional. Data type corresponding to the template parameter. It takes effect only when the source operand is mask_reg, determining the bit width for bitwise de-interleaving. | | ||
| 40 | + | ||
| 41 | +## Constraints | ||
| 42 | + | ||
| 43 | +- src0 and src1 can be the same reg_tensor; dst0 and dst1 cannot be the same reg_tensor. Configuring them as the same reg_tensor causes functional exceptions. | ||
| 44 | + | ||
| 45 | +- The source operand and destination operand may be the same reg_tensor. | ||
| 46 | + | ||
| 47 | +## Return Value | ||
| 48 | + | ||
| 49 | +Returns a tuple (dst0, dst1). | ||
| 50 | + | ||
| 51 | +- dst0: Destination operand, of type [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md). The supported data types are the same as those described for src0. | ||
| 52 | +- dst1: Destination operand, of type [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md). The supported data types are the same as those described for src0. | ||
| 53 | + | ||
| 54 | +## Examples | ||
| 55 | + | ||
| 56 | +### reg_tensor Examples | ||
| 57 | + | ||
| 58 | +```python | ||
| 59 | +import os | ||
| 60 | +import pypto_pro.language as pl | ||
| 61 | +import torch | ||
| 62 | +import torch_npu | ||
| 63 | + | ||
| 64 | +@pl.vector_function | ||
| 65 | +def example_vf(src_a, src_b, dst_tile): | ||
| 66 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP16) | ||
| 67 | + src0 = vf.load_align(src_a, 0) | ||
| 68 | + src1 = vf.load_align(src_b, 0) | ||
| 69 | + dst0, dst1 = vf.de_interleave(src0, src1) | ||
| 70 | + vf.store_align(dst_tile, dst0, preg) | ||
| 71 | + | ||
| 72 | +@pl.jit() | ||
| 73 | +def example_kernel( | ||
| 74 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 75 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 76 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 77 | +): | ||
| 78 | + tf = pl.TileType(shape=[1, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | ||
| 79 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 80 | + in_a = in_a_grp.current() | ||
| 81 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 82 | + in_b = in_b_grp.current() | ||
| 83 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 84 | + t_out = t_out_grp.current() | ||
| 85 | + with pl.section_vector(): | ||
| 86 | + pl.load(in_a, a, [0, 0]) | ||
| 87 | + pl.load(in_b, b, [0, 0]) | ||
| 88 | + example_vf(in_a, in_b, t_out) | ||
| 89 | + pl.store(out, t_out, [0, 0]) | ||
| 90 | + | ||
| 91 | +def test_example(): | ||
| 92 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 93 | + device = f"npu:{device_id}" | ||
| 94 | + core_nums = 1 | ||
| 95 | + torch.npu.set_device(device) | ||
| 96 | + a = torch.randn([1, 128], device=device, dtype=torch.float16) | ||
| 97 | + b = torch.randn([1, 128], device=device, dtype=torch.float16) | ||
| 98 | + out = torch.empty([1, 128], device=device, dtype=torch.float16) | ||
| 99 | + example_kernel[None, core_nums](a, b, out) | ||
| 100 | + torch.npu.synchronize() | ||
| 101 | + assert out.shape == torch.Size([1, 128]) | ||
| 102 | + | ||
| 103 | +if __name__ == "__main__": | ||
| 104 | + test_example() | ||
| 105 | + print("PASSED") | ||
| 106 | +``` | ||
| 107 | + | ||
| 108 | +### mask_reg Examples | ||
| 109 | + | ||
| 110 | +When the source operands are mask_reg, vf.de_interleave de-interleaves the two mask_reg bit by bit. The de-interleaving bit width is determined by the data type of mask_reg. | ||
| 111 | + | ||
| 112 | +```python | ||
| 113 | +import os | ||
| 114 | +import pypto_pro.language as pl | ||
| 115 | +import torch | ||
| 116 | +import torch_npu | ||
| 117 | + | ||
| 118 | +@pl.vector_function | ||
| 119 | +def example_vf(src_tile, dst_tile): | ||
| 120 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 121 | + mask_full = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 122 | + mask_m3 = vf.create_mask(pattern=pl.MaskPattern.M3, dtype=pl.DT_FP32) | ||
| 123 | + reg = vf.load_align(src_tile, 0) | ||
| 124 | + new_mask0, new_mask1 = vf.interleave(mask_full, mask_m3) | ||
| 125 | + new_mask0, new_mask1 = vf.de_interleave(new_mask0, new_mask1) | ||
| 126 | + reg_dst = vf.abs(reg, new_mask0) | ||
| 127 | + vf.store_align(dst_tile, reg_dst, preg) | ||
| 128 | + | ||
| 129 | +@pl.jit() | ||
| 130 | +def example_kernel( | ||
| 131 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 132 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 133 | +): | ||
| 134 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 135 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 136 | + in_a = in_a_grp.current() | ||
| 137 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 138 | + t_out = t_out_grp.current() | ||
| 139 | + with pl.section_vector(): | ||
| 140 | + pl.load(in_a, a, [0, 0]) | ||
| 141 | + example_vf(in_a, t_out) | ||
| 142 | + pl.store(out, t_out, [0, 0]) | ||
| 143 | + | ||
| 144 | +def test_example(): | ||
| 145 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 146 | + device = f"npu:{device_id}" | ||
| 147 | + core_nums = 1 | ||
| 148 | + torch.npu.set_device(device) | ||
| 149 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 150 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 151 | + example_kernel[None, core_nums](a, out) | ||
| 152 | + torch.npu.synchronize() | ||
| 153 | + torch.testing.assert_close(out, torch.abs(a), rtol=1e-5, atol=1e-5) | ||
| 154 | + | ||
| 155 | +if __name__ == "__main__": | ||
| 156 | + test_example() | ||
| 157 | + print("PASSED") | ||
| 158 | +``` | ||
| @@ -0,0 +1,14 @@ | |||
| 1 | +# Advanced Computation | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:05:11.354Z pushedAt=2026-10-28T11:57:28.726Z nl=2 --> | ||
| 4 | + | ||
| 5 | +```{toctree} | ||
| 6 | +:maxdepth: 1 | ||
| 7 | +:titlesonly: | ||
| 8 | + | ||
| 9 | +de_interleave | ||
| 10 | +interleave | ||
| 11 | +pack | ||
| 12 | +unpack | ||
| 13 | +unsqueeze | ||
| 14 | +``` | ||
| @@ -0,0 +1,158 @@ | |||
| 1 | +# vf.interleave | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=7d0ab31d84c294a0a8327f597d2bdee012acb22d translatedAt=2026-10-26T13:06:27.129Z pushedAt=2026-10-28T11:57:28.733Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +Given source operands src0 and src1, interleave the elements of src0 and src1 into result operands dst0 and dst1. The interleaving layout is shown in the following figure, where each cell represents one element: | ||
| 20 | + | ||
| 21 | +$$dstReg0_{2i} = srcReg0_i, \quad dstReg1_{2i+1} = srcReg1_i$$ | ||
| 22 | + | ||
| 23 | +**Figure 1** Interleave data layout | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +## Prototype | ||
| 28 | + | ||
| 29 | +```python | ||
| 30 | +interleave(src0, src1, dtype: Optional[DType] = None) -> (dst0, dst1) | ||
| 31 | +``` | ||
| 32 | + | ||
| 33 | +## Parameters | ||
| 34 | + | ||
| 35 | +| Parameter | Input/Output | Description | | ||
| 36 | +|---|---|---| | ||
| 37 | +| src0 | Input | Source operand, of the [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type.<br>- **reg_tensor input**: The data types of source operands src0 and src1 and the destination operand dst must be consistent. Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_FP16, DT_BF16, DT_INT32, DT_UINT32, DT_FP32.<br>- **mask_reg input**: Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_FP16, DT_BF16, DT_INT32, DT_UINT32, DT_FP32. | | ||
| 38 | +| src1 | Input | Source operand, of the [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type. The supported data types are the same as those described for src0. | | ||
| 39 | +| dtype | Input | Optional. Data type corresponding to the template parameter. It takes effect only when the source operand is mask_reg, and determines the bit width for bitwise interleaving. | | ||
| 40 | + | ||
| 41 | +## Constraints | ||
| 42 | + | ||
| 43 | +- src0 and src1 can be the same reg_tensor; dst0 and dst1 cannot be the same reg_tensor. Configuring them as the same reg_tensor causes functional exceptions. | ||
| 44 | + | ||
| 45 | +- The source operand and destination operand may be the same reg_tensor. | ||
| 46 | + | ||
| 47 | +## Return Value | ||
| 48 | + | ||
| 49 | +Returns a 2-tuple (dst0, dst1). | ||
| 50 | + | ||
| 51 | +- dst0: Destination operand, of type [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md). The supported data types are the same as those described for src0. | ||
| 52 | +- dst1: Destination operand, of type [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md). The supported data types are the same as those described for src0. | ||
| 53 | + | ||
| 54 | +## Examples | ||
| 55 | + | ||
| 56 | +### reg_tensor Examples | ||
| 57 | + | ||
| 58 | +```python | ||
| 59 | +import os | ||
| 60 | +import pypto_pro.language as pl | ||
| 61 | +import torch | ||
| 62 | +import torch_npu | ||
| 63 | + | ||
| 64 | +@pl.vector_function | ||
| 65 | +def example_vf(src_a, src_b, dst_tile): | ||
| 66 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP16) | ||
| 67 | + src0 = vf.load_align(src_a, 0) | ||
| 68 | + src1 = vf.load_align(src_b, 0) | ||
| 69 | + dst0, dst1 = vf.interleave(src0, src1) | ||
| 70 | + vf.store_align(dst_tile, dst0, preg) | ||
| 71 | + | ||
| 72 | +@pl.jit() | ||
| 73 | +def example_kernel( | ||
| 74 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 75 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 76 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 77 | +): | ||
| 78 | + tf = pl.TileType(shape=[1, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | ||
| 79 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 80 | + in_a = in_a_grp.current() | ||
| 81 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 82 | + in_b = in_b_grp.current() | ||
| 83 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 84 | + t_out = t_out_grp.current() | ||
| 85 | + with pl.section_vector(): | ||
| 86 | + pl.load(in_a, a, [0, 0]) | ||
| 87 | + pl.load(in_b, b, [0, 0]) | ||
| 88 | + example_vf(in_a, in_b, t_out) | ||
| 89 | + pl.store(out, t_out, [0, 0]) | ||
| 90 | + | ||
| 91 | +def test_example(): | ||
| 92 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 93 | + device = f"npu:{device_id}" | ||
| 94 | + core_nums = 1 | ||
| 95 | + torch.npu.set_device(device) | ||
| 96 | + a = torch.randn([1, 128], device=device, dtype=torch.float16) | ||
| 97 | + b = torch.randn([1, 128], device=device, dtype=torch.float16) | ||
| 98 | + out = torch.empty([1, 128], device=device, dtype=torch.float16) | ||
| 99 | + example_kernel[None, core_nums](a, b, out) | ||
| 100 | + torch.npu.synchronize() | ||
| 101 | + assert out.shape == torch.Size([1, 128]) | ||
| 102 | + | ||
| 103 | +if __name__ == "__main__": | ||
| 104 | + test_example() | ||
| 105 | + print("PASSED") | ||
| 106 | +``` | ||
| 107 | + | ||
| 108 | +### mask_reg Examples | ||
| 109 | + | ||
| 110 | +When the source operands are mask_reg, vf.interleave interleaves the two mask_reg values bit by bit. The interleaving bit width is determined by the data type of mask_reg. | ||
| 111 | + | ||
| 112 | +```python | ||
| 113 | +import os | ||
| 114 | +import pypto_pro.language as pl | ||
| 115 | +import torch | ||
| 116 | +import torch_npu | ||
| 117 | + | ||
| 118 | +@pl.vector_function | ||
| 119 | +def example_vf(src_tile, dst_tile): | ||
| 120 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 121 | + mask_full = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 122 | + mask_m3 = vf.create_mask(pattern=pl.MaskPattern.M3, dtype=pl.DT_FP32) | ||
| 123 | + reg = vf.load_align(src_tile, 0) | ||
| 124 | + new_mask0, new_mask1 = vf.interleave(mask_full, mask_m3) | ||
| 125 | + new_mask0, new_mask1 = vf.de_interleave(new_mask0, new_mask1) | ||
| 126 | + reg_dst = vf.abs(reg, new_mask0) | ||
| 127 | + vf.store_align(dst_tile, reg_dst, preg) | ||
| 128 | + | ||
| 129 | +@pl.jit() | ||
| 130 | +def example_kernel( | ||
| 131 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 132 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 133 | +): | ||
| 134 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 135 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 136 | + in_a = in_a_grp.current() | ||
| 137 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 138 | + t_out = t_out_grp.current() | ||
| 139 | + with pl.section_vector(): | ||
| 140 | + pl.load(in_a, a, [0, 0]) | ||
| 141 | + example_vf(in_a, t_out) | ||
| 142 | + pl.store(out, t_out, [0, 0]) | ||
| 143 | + | ||
| 144 | +def test_example(): | ||
| 145 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 146 | + device = f"npu:{device_id}" | ||
| 147 | + core_nums = 1 | ||
| 148 | + torch.npu.set_device(device) | ||
| 149 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 150 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 151 | + example_kernel[None, core_nums](a, out) | ||
| 152 | + torch.npu.synchronize() | ||
| 153 | + torch.testing.assert_close(out, torch.abs(a), rtol=1e-5, atol=1e-5) | ||
| 154 | + | ||
| 155 | +if __name__ == "__main__": | ||
| 156 | + test_example() | ||
| 157 | + print("PASSED") | ||
| 158 | +``` | ||
| @@ -0,0 +1,169 @@ | |||
| 1 | +# vf.pack | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=7d0ab31d84c294a0a8327f597d2bdee012acb22d translatedAt=2026-10-26T13:06:41.600Z pushedAt=2026-10-28T11:57:28.736Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +The API automatically supports two modes based on the type of the src parameter: | ||
| 20 | + | ||
| 21 | +**reg_tensor input**: Selects the lower 8 bits (for 16-bit-wide types such as DT_INT16, DT_UINT16, DT_FP16, and DT_BF16), the lower 16 bits (for 32-bit-wide types such as DT_INT32, DT_UINT32, and DT_FP32), or the lower 32 bits (for 64-bit-wide types such as DT_INT64 and DT_UINT64) from the elements in the source operand src, and writes them to the lower or upper half of dst based on the mode selected by part. It is used to compress wide-type data into narrow-type data. The following figure shows the schematic diagram: | ||
| 22 | + | ||
| 23 | +**Figure 1** Pack with reg_tensor input | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +**mask_reg input**: Extracts the even bits from the source operand src into the lower or upper half of dst based on the mode selected by part. The following figure shows the schematic diagram: | ||
| 28 | + | ||
| 29 | +**Figure 2** Pack with mask_reg input | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +## Prototype | ||
| 34 | + | ||
| 35 | +```python | ||
| 36 | +pack(src, part: Optional[PackPart] = None, dtype: Optional[DType] = None) -> dst | ||
| 37 | +``` | ||
| 38 | + | ||
| 39 | +## Parameters | ||
| 40 | + | ||
| 41 | +| Parameter | Input/Output | Description | | ||
| 42 | +|---|---|---| | ||
| 43 | +| src | Input | Source operand, of [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type. For reg_tensor, the data type is the wide type before compression; for mask_reg, the data type remains unchanged. | | ||
| 44 | +| part | Input | Optional. Controls whether to write to the lower or upper half of dst, corresponding to the [PackPart](../basic_data_structures/PackPart.md) type.<br>- pypto_pro.language.PackPart.LOWER: Low mode, writes to the lower half of dst.<br>- pypto_pro.language.PackPart.UPPER: High mode, writes to the upper half of dst.<br>Defaults to pypto_pro.language.PackPart.LOWER. The dual-register mode supports only the pypto_pro.language.PackPart.LOWER mode. | | ||
| 45 | +| dtype | Input | Optional. Data type.<br>- Required in reg_tensor mode. Specifies the data type of the target reg_tensor (for example, pypto_pro.language.DT_UINT8, pypto_pro.language.DT_UINT16). It is required because compressing a wide type into a narrow type (for example, DT_UINT16 to DT_UINT8) makes the target reg_tensor data type different from the source reg_tensor data type, which cannot be inferred from the source operand. Therefore, the target data type must be explicitly specified through the dtype parameter.<br>- In mask_reg mode, the register type is retained, so this parameter can be omitted. | | ||
| 46 | + | ||
| 47 | +## Constraints | ||
| 48 | + | ||
| 49 | +- Data type constraints: | ||
| 50 | + | ||
| 51 | + - **reg_tensor input** | ||
| 52 | + | ||
| 53 | + | dst | src | | ||
| 54 | + |---|---| | ||
| 55 | + | DT_UINT8 | DT_INT16 | | ||
| 56 | + | DT_UINT8 | DT_UINT16 | | ||
| 57 | + | DT_UINT16 | DT_INT32 | | ||
| 58 | + | DT_UINT16 | DT_UINT32 | | ||
| 59 | + | DT_UINT32 | DT_INT64 | | ||
| 60 | + | DT_UINT32 | DT_UINT64 | | ||
| 61 | + | ||
| 62 | + - **mask_reg input** | ||
| 63 | + | ||
| 64 | + None | ||
| 65 | + | ||
| 66 | +## Return Value | ||
| 67 | + | ||
| 68 | +Returns the dst destination operand, of [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type. For reg_tensor, the data type is the narrow type after compression; for mask_reg, the data type remains unchanged. For supported data types, see [Constraints](#constraints). | ||
| 69 | + | ||
| 70 | +## Examples | ||
| 71 | + | ||
| 72 | +### reg_tensor Call Example | ||
| 73 | + | ||
| 74 | +```python | ||
| 75 | +import os | ||
| 76 | +import pypto_pro.language as pl | ||
| 77 | +import torch | ||
| 78 | +import torch_npu | ||
| 79 | + | ||
| 80 | +@pl.vector_function | ||
| 81 | +def example_vf(src_tile, dst_tile): | ||
| 82 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT8) | ||
| 83 | + src = vf.load_align(src_tile, 0) | ||
| 84 | + dst = vf.pack(src, part=pl.PackPart.LOWER, dtype=pl.DT_UINT8) | ||
| 85 | + vf.store_align(dst_tile, dst, preg) | ||
| 86 | + | ||
| 87 | +@pl.jit() | ||
| 88 | +def example_kernel( | ||
| 89 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT16], | ||
| 90 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT8], | ||
| 91 | +): | ||
| 92 | + tf_src = pl.TileType(shape=[1, 128], dtype=pl.DT_UINT16, target_memory=pl.MemorySpace.Vec) | ||
| 93 | + tf_dst = pl.TileType(shape=[1, 256], dtype=pl.DT_UINT8, target_memory=pl.MemorySpace.Vec) | ||
| 94 | + in_a_grp = pl.make_tile_group(type=tf_src, addrs=0x0, mutex_ids=[0]) | ||
| 95 | + in_a = in_a_grp.current() | ||
| 96 | + t_out_grp = pl.make_tile_group(type=tf_dst, addrs=0x100, mutex_ids=[1]) | ||
| 97 | + t_out = t_out_grp.current() | ||
| 98 | + with pl.section_vector(): | ||
| 99 | + pl.load(in_a, a, [0, 0]) | ||
| 100 | + example_vf(in_a, t_out) | ||
| 101 | + pl.store(out, t_out, [0, 0]) | ||
| 102 | + | ||
| 103 | +def test_example(): | ||
| 104 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 105 | + device = f"npu:{device_id}" | ||
| 106 | + core_nums = 1 | ||
| 107 | + torch.npu.set_device(device) | ||
| 108 | + a = torch.randint(0, 256, [1, 128], device=device, dtype=torch.int16) | ||
| 109 | + out = torch.empty([1, 256], device=device, dtype=torch.uint8) | ||
| 110 | + example_kernel[None, core_nums](a, out) | ||
| 111 | + torch.npu.synchronize() | ||
| 112 | + assert out.dtype == torch.uint8 | ||
| 113 | + | ||
| 114 | +if __name__ == "__main__": | ||
| 115 | + test_example() | ||
| 116 | + print("PASSED") | ||
| 117 | +``` | ||
| 118 | + | ||
| 119 | +### mask_reg Examples | ||
| 120 | + | ||
| 121 | +When the source operand is mask_reg, vf.pack extracts the even bits of the mask into the lower half or the upper half. The mask_reg variant shares the part= parameter with the reg_tensor variant to specify the mode. | ||
| 122 | + | ||
| 123 | +```python | ||
| 124 | +import os | ||
| 125 | +import pypto_pro.language as pl | ||
| 126 | +import torch | ||
| 127 | +import torch_npu | ||
| 128 | + | ||
| 129 | +@pl.vector_function | ||
| 130 | +def example_vf(src_tile, dst_tile): | ||
| 131 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 132 | + reg = vf.load_align(src_tile, 0) | ||
| 133 | + mask_a = vf.ge(reg, 0.0, preg) | ||
| 134 | + preg_packed = vf.pack(mask_a, part=pl.PackPart.LOWER) | ||
| 135 | + preg_unpacked = vf.unpack(preg_packed, part=pl.PackPart.LOWER) | ||
| 136 | + reg_dst = vf.abs(reg, preg_unpacked) | ||
| 137 | + vf.store_align(dst_tile, reg_dst, preg) | ||
| 138 | + | ||
| 139 | +@pl.jit() | ||
| 140 | +def example_kernel( | ||
| 141 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 142 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 143 | +): | ||
| 144 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 145 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 146 | + in_a = in_a_grp.current() | ||
| 147 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 148 | + t_out = t_out_grp.current() | ||
| 149 | + with pl.section_vector(): | ||
| 150 | + pl.load(in_a, a, [0, 0]) | ||
| 151 | + example_vf(in_a, t_out) | ||
| 152 | + pl.store(out, t_out, [0, 0]) | ||
| 153 | + | ||
| 154 | +def test_example(): | ||
| 155 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 156 | + device = f"npu:{device_id}" | ||
| 157 | + core_nums = 1 | ||
| 158 | + torch.npu.set_device(device) | ||
| 159 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 160 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 161 | + example_kernel[None, core_nums](a, out) | ||
| 162 | + torch.npu.synchronize() | ||
| 163 | + expected = torch.where(a >= 0, a, torch.zeros_like(a)) | ||
| 164 | + torch.testing.assert_close(out, expected, rtol=1e-5, atol=1e-5) | ||
| 165 | + | ||
| 166 | +if __name__ == "__main__": | ||
| 167 | + test_example() | ||
| 168 | + print("PASSED") | ||
| 169 | +``` | ||
| @@ -0,0 +1,171 @@ | |||
| 1 | +# vf.unpack | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=7d0ab31d84c294a0a8327f597d2bdee012acb22d translatedAt=2026-10-26T13:08:08.661Z pushedAt=2026-10-28T11:57:28.739Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +The API automatically supports two modes on the backend based on the type of the src parameter: | ||
| 20 | + | ||
| 21 | +**reg_tensor input**: For unsigned integer types, the elements in the lower half or upper half of the source operand src are written to the destination operand dst with the bit width expanded by padding the high bits with 0. For signed integer types, the elements in the lower half or upper half of the source operand src are written to dst with the bit width expanded while preserving the sign bit. This is used to expand narrow-type data into wide-type data. The schematic diagram is as follows: | ||
| 22 | + | ||
| 23 | +**Figure 1** Unpack with reg_tensor input | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +**mask_reg input**: Based on the mode selected by part, the lower half or upper half of the source operand src is expanded into the destination operand dst. The schematic diagram is as follows: | ||
| 28 | + | ||
| 29 | +**Figure 2** Unpack with mask_reg input | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +## Prototype | ||
| 34 | + | ||
| 35 | +```python | ||
| 36 | +unpack(src, part: Optional[PackPart] = None, dtype: Optional[DType] = None) -> dst | ||
| 37 | +``` | ||
| 38 | + | ||
| 39 | +## Parameters | ||
| 40 | + | ||
| 41 | +| Parameter | Input/Output | Description | | ||
| 42 | +|---|---|---| | ||
| 43 | +| src | Input | Source operand, of [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type. For supported data types, see [Constraints](#constraints). For reg_tensor, the data type is the narrow type before unpacking; for mask_reg, the data type remains unchanged. | | ||
| 44 | +| part | Input | Optional. Controls whether to read the lower or upper half of src, corresponding to the [PackPart](../basic_data_structures/PackPart.md) type.<br>- pypto_pro.language.PackPart.LOWER: Low mode, reads the lower half of src.<br>- pypto_pro.language.PackPart.UPPER: High mode, reads the upper half of src.<br>Defaults to pypto_pro.language.PackPart.LOWER. Dual-register mode supports only pypto_pro.language.PackPart.LOWER. | | ||
| 45 | +| dtype | Input | Optional. Data type.<br>- Required in reg_tensor mode. Specifies the data type of the destination reg_tensor (for example, pypto_pro.language.DT_UINT16, pypto_pro.language.DT_UINT32). It is required because unpacking a narrow type into a wide type (for example, DT_UINT8→DT_UINT16) changes the data type of the destination reg_tensor relative to the source reg_tensor, which cannot be inferred from the source operand. Therefore, the destination data type must be explicitly specified through the dtype parameter.<br>- In mask_reg mode, the register type is retained, so this parameter can be omitted. | | ||
| 46 | + | ||
| 47 | +## Constraints | ||
| 48 | + | ||
| 49 | +- Data type constraints: | ||
| 50 | + | ||
| 51 | + - **reg_tensor input** | ||
| 52 | + | ||
| 53 | + Data type mapping between source and destination operands | ||
| 54 | + | ||
| 55 | + | dst | src | | ||
| 56 | + |---|---| | ||
| 57 | + | DT_INT16 | DT_INT8 | | ||
| 58 | + | DT_UINT16 | DT_UINT8 | | ||
| 59 | + | DT_INT32 | DT_INT16 | | ||
| 60 | + | DT_UINT32 | DT_UINT16 | | ||
| 61 | + | DT_INT64 | DT_INT32 | | ||
| 62 | + | DT_UINT64 | DT_UINT32 | | ||
| 63 | + | ||
| 64 | + - **mask_reg input** | ||
| 65 | + | ||
| 66 | + None | ||
| 67 | + | ||
| 68 | +## Return Value | ||
| 69 | + | ||
| 70 | +Returns the destination operand dst, of [reg_tensor](../basic_data_structures/reg_tensor.md) or [mask_reg](../basic_data_structures/mask_reg.md) type. For reg_tensor, the data type is the wide type after unpacking; for mask_reg, the data type remains unchanged. For supported data types, see [Constraints](#constraints). | ||
| 71 | + | ||
| 72 | +## Examples | ||
| 73 | + | ||
| 74 | +### reg_tensor Examples | ||
| 75 | + | ||
| 76 | +```python | ||
| 77 | +import os | ||
| 78 | +import pypto_pro.language as pl | ||
| 79 | +import torch | ||
| 80 | +import torch_npu | ||
| 81 | + | ||
| 82 | +@pl.vector_function | ||
| 83 | +def example_vf(src_tile, dst_tile): | ||
| 84 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT16) | ||
| 85 | + src = vf.load_align(src_tile, 0) | ||
| 86 | + dst = vf.unpack(src, part=pl.PackPart.LOWER, dtype=pl.DT_UINT16) | ||
| 87 | + vf.store_align(dst_tile, dst, preg) | ||
| 88 | + | ||
| 89 | +@pl.jit() | ||
| 90 | +def example_kernel( | ||
| 91 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT8], | ||
| 92 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT16], | ||
| 93 | +): | ||
| 94 | + tf_src = pl.TileType(shape=[1, 256], dtype=pl.DT_UINT8, target_memory=pl.MemorySpace.Vec) | ||
| 95 | + tf_dst = pl.TileType(shape=[1, 128], dtype=pl.DT_UINT16, target_memory=pl.MemorySpace.Vec) | ||
| 96 | + in_a_grp = pl.make_tile_group(type=tf_src, addrs=0x0, mutex_ids=[0]) | ||
| 97 | + in_a = in_a_grp.current() | ||
| 98 | + t_out_grp = pl.make_tile_group(type=tf_dst, addrs=0x100, mutex_ids=[1]) | ||
| 99 | + t_out = t_out_grp.current() | ||
| 100 | + with pl.section_vector(): | ||
| 101 | + pl.load(in_a, a, [0, 0]) | ||
| 102 | + example_vf(in_a, t_out) | ||
| 103 | + pl.store(out, t_out, [0, 0]) | ||
| 104 | + | ||
| 105 | +def test_example(): | ||
| 106 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 107 | + device = f"npu:{device_id}" | ||
| 108 | + core_nums = 1 | ||
| 109 | + torch.npu.set_device(device) | ||
| 110 | + a = torch.randint(0, 256, [1, 256], device=device, dtype=torch.uint8) | ||
| 111 | + out = torch.empty([1, 128], device=device, dtype=torch.int16) | ||
| 112 | + example_kernel[None, core_nums](a, out) | ||
| 113 | + torch.npu.synchronize() | ||
| 114 | + assert out.dtype == torch.int16 | ||
| 115 | + | ||
| 116 | +if __name__ == "__main__": | ||
| 117 | + test_example() | ||
| 118 | + print("PASSED") | ||
| 119 | +``` | ||
| 120 | + | ||
| 121 | +### mask_reg Examples | ||
| 122 | + | ||
| 123 | +When the source operand is mask_reg, vf.unpack expands the lower half or upper half of the mask (each bit is expanded to 2 bits, with the high bits set to zero). The mask_reg variant shares the part= parameter with the reg_tensor variant to specify the mode. | ||
| 124 | + | ||
| 125 | +```python | ||
| 126 | +import os | ||
| 127 | +import pypto_pro.language as pl | ||
| 128 | +import torch | ||
| 129 | +import torch_npu | ||
| 130 | + | ||
| 131 | +@pl.vector_function | ||
| 132 | +def example_vf(src_tile, dst_tile): | ||
| 133 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 134 | + reg = vf.load_align(src_tile, 0) | ||
| 135 | + mask_a = vf.ge(reg, 0.0, preg) | ||
| 136 | + preg_packed = vf.pack(mask_a, part=pl.PackPart.LOWER) | ||
| 137 | + preg_unpacked = vf.unpack(preg_packed, part=pl.PackPart.LOWER) | ||
| 138 | + reg_dst = vf.abs(reg, preg_unpacked) | ||
| 139 | + vf.store_align(dst_tile, reg_dst, preg) | ||
| 140 | + | ||
| 141 | +@pl.jit() | ||
| 142 | +def example_kernel( | ||
| 143 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 144 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 145 | +): | ||
| 146 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 147 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 148 | + in_a = in_a_grp.current() | ||
| 149 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 150 | + t_out = t_out_grp.current() | ||
| 151 | + with pl.section_vector(): | ||
| 152 | + pl.load(in_a, a, [0, 0]) | ||
| 153 | + example_vf(in_a, t_out) | ||
| 154 | + pl.store(out, t_out, [0, 0]) | ||
| 155 | + | ||
| 156 | +def test_example(): | ||
| 157 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 158 | + device = f"npu:{device_id}" | ||
| 159 | + core_nums = 1 | ||
| 160 | + torch.npu.set_device(device) | ||
| 161 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 162 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 163 | + example_kernel[None, core_nums](a, out) | ||
| 164 | + torch.npu.synchronize() | ||
| 165 | + expected = torch.where(a >= 0, a, torch.zeros_like(a)) | ||
| 166 | + torch.testing.assert_close(out, expected, rtol=1e-5, atol=1e-5) | ||
| 167 | + | ||
| 168 | +if __name__ == "__main__": | ||
| 169 | + test_example() | ||
| 170 | + print("PASSED") | ||
| 171 | +``` | ||
| @@ -0,0 +1,95 @@ | |||
| 1 | +# vf.unsqueeze | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=7d0ab31d84c294a0a8327f597d2bdee012acb22d translatedAt=2026-10-26T13:08:33.624Z pushedAt=2026-10-28T11:57:28.746Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +Decompresses dst according to preg. | ||
| 20 | + | ||
| 21 | +The algorithm is shown in the following figure. The first bit of dst is 0. When the corresponding mask value of mask[i] is 1, the value of dst[i] is dst[i-1] + 1; when the corresponding mask value of mask[i] is 0, the value of dst[i] is dst[i-1]. The most significant bit of mask is ignored and does not participate in the statistics. | ||
| 22 | + | ||
| 23 | +$$dstReg_i = \begin{cases} 1 & \text{if } mask_i = 1 \\ 0 & \text{if } mask_i = 0 \end{cases}$$ | ||
| 24 | + | ||
| 25 | +**Figure 1** Unsqueeze diagram | ||
| 26 | + | ||
| 27 | + | ||
| 28 | + | ||
| 29 | +mask_reg is generated by vf.create_mask or vf.update_mask and is passed directly to the vector computation API as a parameter of the mask_reg type to control which elements participate in the computation. | ||
| 30 | + | ||
| 31 | +## Prototype | ||
| 32 | + | ||
| 33 | +```python | ||
| 34 | +unsqueeze(preg) -> dst | ||
| 35 | +``` | ||
| 36 | + | ||
| 37 | +## Parameters | ||
| 38 | + | ||
| 39 | +| Parameter | Input/Output | Description | | ||
| 40 | +|---|---|---| | ||
| 41 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md) (generated by vf.create_mask or vf.update_mask). | | ||
| 42 | + | ||
| 43 | +## Constraints | ||
| 44 | + | ||
| 45 | +None | ||
| 46 | + | ||
| 47 | +## Return Value | ||
| 48 | + | ||
| 49 | +Returns the dst destination operand, [reg_tensor](../basic_data_structures/reg_tensor.md), which stores the expansion result. Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_INT32, DT_UINT32. | ||
| 50 | + | ||
| 51 | +## Examples | ||
| 52 | + | ||
| 53 | +```python | ||
| 54 | +import os | ||
| 55 | +import pypto_pro.language as pl | ||
| 56 | +import torch | ||
| 57 | +import torch_npu | ||
| 58 | + | ||
| 59 | +@pl.vector_function | ||
| 60 | +def example_vf(src_tile, dst_tile): | ||
| 61 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) | ||
| 62 | + src = vf.load_align(src_tile, 0) | ||
| 63 | + dst = vf.unsqueeze(preg) | ||
| 64 | + vf.store_align(dst_tile, dst, preg) | ||
| 65 | + | ||
| 66 | +@pl.jit() | ||
| 67 | +def example_kernel( | ||
| 68 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 69 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 70 | +): | ||
| 71 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_UINT32, target_memory=pl.MemorySpace.Vec) | ||
| 72 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 73 | + in_a = in_a_grp.current() | ||
| 74 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 75 | + t_out = t_out_grp.current() | ||
| 76 | + with pl.section_vector(): | ||
| 77 | + pl.load(in_a, a, [0, 0]) | ||
| 78 | + example_vf(in_a, t_out) | ||
| 79 | + pl.store(out, t_out, [0, 0]) | ||
| 80 | + | ||
| 81 | +def test_example(): | ||
| 82 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 83 | + device = f"npu:{device_id}" | ||
| 84 | + core_nums = 1 | ||
| 85 | + torch.npu.set_device(device) | ||
| 86 | + a = torch.randint(0, 100, [1, 64], device=device, dtype=torch.int32) | ||
| 87 | + out = torch.empty([1, 64], device=device, dtype=torch.int32) | ||
| 88 | + example_kernel[None, core_nums](a, out) | ||
| 89 | + torch.npu.synchronize() | ||
| 90 | + assert out.shape == torch.Size([1, 64]) | ||
| 91 | + | ||
| 92 | +if __name__ == "__main__": | ||
| 93 | + test_example() | ||
| 94 | + print("PASSED") | ||
| 95 | +``` | ||
| @@ -0,0 +1,101 @@ | |||
| 1 | +# vf.abs | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:08:14.042Z pushedAt=2026-10-28T11:57:28.743Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +- For real number types (DT_INT8, DT_INT16, DT_INT32, DT_FP16, DT_FP32) | ||
| 20 | + | ||
| 21 | + Computes the absolute value of each valid element in src and writes the result to the corresponding position in dst. The formula is as follows: | ||
| 22 | + | ||
| 23 | + $$dst_i = |src_i|$$ | ||
| 24 | + | ||
| 25 | +- For complex number types (DT_FP16 dual register mode, DT_FP32 dual register mode) | ||
| 26 | + | ||
| 27 | + Complex number types are implemented through the dual register mode: the DT_FP16 dual register mode corresponds to a complex number composed of two DT_FP16 values (the real part and the imaginary part each occupy 16 bits, totaling 32 bits), and the DT_FP32 dual register mode corresponds to a complex number composed of two DT_FP32 values (the real part and the imaginary part each occupy 32 bits, totaling 64 bits). reg[0] stores the real part, and reg[1] stores the imaginary part. | ||
| 28 | + | ||
| 29 | + Computes the modulus of each valid element in src and writes the result to the corresponding position in dst. The formula is as follows: | ||
| 30 | + | ||
| 31 | + $$dst_i = |src_i| = (\alpha^2 + \beta^2)^{1/2}$$ | ||
| 32 | + | ||
| 33 | + where $src_i = \alpha + \beta i$, α is the real part of the complex number, and β is the imaginary part. | ||
| 34 | + | ||
| 35 | +## Prototype | ||
| 36 | + | ||
| 37 | +```python | ||
| 38 | +abs(src, preg, mode: Optional[MergeMode] = None) -> dst | ||
| 39 | +``` | ||
| 40 | + | ||
| 41 | +## Parameters | ||
| 42 | + | ||
| 43 | +| Parameter | Input/Output | Description | | ||
| 44 | +|---|---|---| | ||
| 45 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md). The source operand src and the destination operand dst must have the same data type. Supported data types: DT_INT8, DT_INT16, DT_INT32, DT_FP16, and DT_FP32. DT_FP16 and DT_FP32 support dual register mode for complex number modulus computation. | | ||
| 46 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 47 | +| mode | Input | Optional, of the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not selected by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported. | | ||
| 48 | + | ||
| 49 | +## Constraints | ||
| 50 | + | ||
| 51 | +None | ||
| 52 | + | ||
| 53 | +## Return Value | ||
| 54 | + | ||
| 55 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), with the same supported data types as described for src. If the computation result of integer data exceeds the representable range of the data type, non-saturating truncation is applied. For example, for DT_INT8, if src is -128, its absolute value 128 is truncated to -128. | ||
| 56 | + | ||
| 57 | +## Examples | ||
| 58 | + | ||
| 59 | +```python | ||
| 60 | +import os | ||
| 61 | +import pypto_pro.language as pl | ||
| 62 | +import torch | ||
| 63 | +import torch_npu | ||
| 64 | + | ||
| 65 | +@pl.vector_function | ||
| 66 | +def example_vf(src_tile, dst_tile): | ||
| 67 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 68 | + reg_a = vf.load_align(src_tile, 0) | ||
| 69 | + reg_out = vf.abs(reg_a, preg) | ||
| 70 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 71 | + | ||
| 72 | +@pl.jit() | ||
| 73 | +def example_kernel( | ||
| 74 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 75 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 76 | +): | ||
| 77 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 78 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 79 | + in_a = in_a_grp.current() | ||
| 80 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 81 | + t_out = t_out_grp.current() | ||
| 82 | + with pl.section_vector(): | ||
| 83 | + pl.load(in_a, a, [0, 0]) | ||
| 84 | + example_vf(in_a, t_out) | ||
| 85 | + pl.store(out, t_out, [0, 0]) | ||
| 86 | + | ||
| 87 | +def test_example(): | ||
| 88 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 89 | + device = f"npu:{device_id}" | ||
| 90 | + core_nums = 1 | ||
| 91 | + torch.npu.set_device(device) | ||
| 92 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 93 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 94 | + example_kernel[None, core_nums](a, out) | ||
| 95 | + torch.npu.synchronize() | ||
| 96 | + torch.testing.assert_close(out, torch.abs(a), rtol=1e-5, atol=1e-5) | ||
| 97 | + | ||
| 98 | +if __name__ == "__main__": | ||
| 99 | + test_example() | ||
| 100 | + print("PASSED") | ||
| 101 | +``` | ||
| @@ -0,0 +1,104 @@ | |||
| 1 | +# vf.add | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=7d0ab31d84c294a0a8327f597d2bdee012acb22d translatedAt=2026-10-26T13:09:00.769Z pushedAt=2026-10-28T11:57:28.750Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +Performs element-wise addition of two reg_tensor operands. | ||
| 20 | + | ||
| 21 | +Based on the mask, performs element-wise addition on the source operands src0 and src1, and writes the result to the destination operand dst. The computation formula is as follows: | ||
| 22 | + | ||
| 23 | +$$dst_i = src0_i + src1_i$$ | ||
| 24 | + | ||
| 25 | +**Figure 1** vf.add function description | ||
| 26 | + | ||
| 27 | + | ||
| 28 | + | ||
| 29 | +To obtain the addition carry output, see [vf.addc](addc.md#description). | ||
| 30 | + | ||
| 31 | +## Prototype | ||
| 32 | + | ||
| 33 | +```python | ||
| 34 | +add(src0, src1, preg, mode: Optional[MergeMode] = None) -> dst | ||
| 35 | +``` | ||
| 36 | + | ||
| 37 | +## Parameters | ||
| 38 | + | ||
| 39 | +| Parameter | Input/Output | Description | | ||
| 40 | +|---|---|---| | ||
| 41 | +| src0 | Input | Source Operand 0, [reg_tensor](../basic_data_structures/reg_tensor.md). The data types of source operands src0 and src1 and destination operand dst must be consistent. Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_INT32, DT_UINT32, DT_FP16, DT_BF16, DT_FP32. | | ||
| 42 | +| src1 | Input | Source Operand 1, [reg_tensor](../basic_data_structures/reg_tensor.md). The supported data types are the same as those described for src0. | | ||
| 43 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 44 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported. | | ||
| 45 | + | ||
| 46 | +## Constraints | ||
| 47 | + | ||
| 48 | +None | ||
| 49 | + | ||
| 50 | +## Return Value | ||
| 51 | + | ||
| 52 | +Returns dst, the destination operand, [reg_tensor](../basic_data_structures/reg_tensor.md). The supported data types are the same as those described for src0. | ||
| 53 | + | ||
| 54 | +## Examples | ||
| 55 | + | ||
| 56 | +```python | ||
| 57 | +import os | ||
| 58 | +import pypto_pro.language as pl | ||
| 59 | +import torch | ||
| 60 | +import torch_npu | ||
| 61 | + | ||
| 62 | +@pl.vector_function | ||
| 63 | +def example_vf(src_a, src_b, dst_tile): | ||
| 64 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 65 | + reg_a = vf.load_align(src_a, 0) | ||
| 66 | + reg_b = vf.load_align(src_b, 0) | ||
| 67 | + reg_out = vf.add(reg_a, reg_b, preg) | ||
| 68 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 69 | + | ||
| 70 | +@pl.jit() | ||
| 71 | +def example_kernel( | ||
| 72 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 73 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 74 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 75 | +): | ||
| 76 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 77 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 78 | + in_a = in_a_grp.current() | ||
| 79 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 80 | + in_b = in_b_grp.current() | ||
| 81 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 82 | + t_out = t_out_grp.current() | ||
| 83 | + with pl.section_vector(): | ||
| 84 | + pl.load(in_a, a, [0, 0]) | ||
| 85 | + pl.load(in_b, b, [0, 0]) | ||
| 86 | + example_vf(in_a, in_b, t_out) | ||
| 87 | + pl.store(out, t_out, [0, 0]) | ||
| 88 | + | ||
| 89 | +def test_example(): | ||
| 90 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 91 | + device = f"npu:{device_id}" | ||
| 92 | + core_nums = 1 | ||
| 93 | + torch.npu.set_device(device) | ||
| 94 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 95 | + b = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 96 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 97 | + example_kernel[None, core_nums](a, b, out) | ||
| 98 | + torch.npu.synchronize() | ||
| 99 | + torch.testing.assert_close(out, a + b, rtol=1e-5, atol=1e-5) | ||
| 100 | + | ||
| 101 | +if __name__ == "__main__": | ||
| 102 | + test_example() | ||
| 103 | + print("PASSED") | ||
| 104 | +``` | ||
| @@ -0,0 +1,110 @@ | |||
| 1 | +# vf.addc | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=7d0ab31d84c294a0a8327f597d2bdee012acb22d translatedAt=2026-10-26T13:11:32.640Z pushedAt=2026-10-28T11:57:28.759Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API performs element-wise addition on source operands src0 and src1 and the input carry carry_src based on mask, writes the result to destination operand dst, and writes the carry result of each element to carry. The computation formula is as follows: | ||
| 20 | + | ||
| 21 | +$$\{carry_i, dst_i\} = src0_i + src1_i + carry\_src_i$$ | ||
| 22 | + | ||
| 23 | +Carry flag indicates an addition carry. If the highest bit of the bitwise sum of src0, src1, and carry_src produces a carry, the corresponding position in carry (the mask_reg that stores the carry) is set to 1 for every 4 bits; otherwise, 0 is written. | ||
| 24 | + | ||
| 25 | +The following figure shows an applicable scenario of the vf.addc API, using the int64_t computation -1 + 8 = 7 as an example: | ||
| 26 | + | ||
| 27 | +**Figure 1** Applicable scenario of vf.addc | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +## Prototype | ||
| 32 | + | ||
| 33 | +```python | ||
| 34 | +addc(src0, src1, carry_src, preg) -> (carry, dst) | ||
| 35 | +``` | ||
| 36 | + | ||
| 37 | +## Parameters | ||
| 38 | + | ||
| 39 | +| Parameter | Input/Output | Description | | ||
| 40 | +|---|---|---| | ||
| 41 | +| src0 | Input | Source operand 0, [reg_tensor](../basic_data_structures/reg_tensor.md). The data types of source operands src0 and src1 and destination operand dst must be consistent. Supported data types: DT_INT32 and DT_UINT32. | | ||
| 42 | +| src1 | Input | Source operand 1, [reg_tensor](../basic_data_structures/reg_tensor.md). The supported data types are the same as those described for src0. | | ||
| 43 | +| carry_src | Input | Input carry value, [mask_reg](../basic_data_structures/mask_reg.md). It must be created before use: create it through [vf.create_mask](../mask_operations/create_mask.md), or provide it from the carry output of the previous vf.addc. | | ||
| 44 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 45 | + | ||
| 46 | +## Constraints | ||
| 47 | + | ||
| 48 | +None | ||
| 49 | + | ||
| 50 | +## Return Value | ||
| 51 | + | ||
| 52 | +Returns a two-tuple (carry, dst). | ||
| 53 | + | ||
| 54 | +- carry: Output carry value, [mask_reg](../basic_data_structures/mask_reg.md), which must be created before use. Create it through [vf.create_mask](../mask_operations/create_mask.md), use it as the tuple unpacking target, and it can serve as the carry_src input for the next vf.addc call. | ||
| 55 | +- dst: Destination operand, [reg_tensor](../basic_data_structures/reg_tensor.md). The supported data types are the same as those described for src0. | ||
| 56 | + | ||
| 57 | +## Examples | ||
| 58 | + | ||
| 59 | +```python | ||
| 60 | +import os | ||
| 61 | +import pypto_pro.language as pl | ||
| 62 | +import torch | ||
| 63 | +import torch_npu | ||
| 64 | + | ||
| 65 | +@pl.vector_function | ||
| 66 | +def example_vf(src_a, src_b, dst_tile): | ||
| 67 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) | ||
| 68 | + carry_src = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) | ||
| 69 | + carry = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) | ||
| 70 | + reg_a = vf.load_align(src_a, 0, dtype=pl.DT_UINT32) | ||
| 71 | + reg_b = vf.load_align(src_b, 0, dtype=pl.DT_UINT32) | ||
| 72 | + carry, reg_out = vf.addc(reg_a, reg_b, carry_src, preg) | ||
| 73 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 74 | + | ||
| 75 | +@pl.jit() | ||
| 76 | +def example_kernel( | ||
| 77 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 78 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 79 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 80 | +): | ||
| 81 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_UINT32, target_memory=pl.MemorySpace.Vec) | ||
| 82 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 83 | + in_a = in_a_grp.current() | ||
| 84 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 85 | + in_b = in_b_grp.current() | ||
| 86 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 87 | + t_out = t_out_grp.current() | ||
| 88 | + with pl.section_vector(): | ||
| 89 | + pl.load(in_a, a, [0, 0]) | ||
| 90 | + pl.load(in_b, b, [0, 0]) | ||
| 91 | + example_vf(in_a, in_b, t_out) | ||
| 92 | + pl.store(out, t_out, [0, 0]) | ||
| 93 | + | ||
| 94 | +def test_example(): | ||
| 95 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 96 | + device = f"npu:{device_id}" | ||
| 97 | + core_nums = 1 | ||
| 98 | + torch.npu.set_device(device) | ||
| 99 | + a = torch.randint(0, 50, [1, 64], device=device, dtype=torch.int32) | ||
| 100 | + b = torch.randint(0, 50, [1, 64], device=device, dtype=torch.int32) | ||
| 101 | + out = torch.empty([1, 64], device=device, dtype=torch.int32) | ||
| 102 | + example_kernel[None, core_nums](a, b, out) | ||
| 103 | + torch.npu.synchronize() | ||
| 104 | + expected = (a.to(torch.int64) + b.to(torch.int64) + 1).to(torch.int32) | ||
| 105 | + assert torch.equal(out, expected) | ||
| 106 | + | ||
| 107 | +if __name__ == "__main__": | ||
| 108 | + test_example() | ||
| 109 | + print("PASSED") | ||
| 110 | +``` | ||
| @@ -0,0 +1,90 @@ | |||
| 1 | +# vf.adds | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:10:46.327Z pushedAt=2026-10-28T11:57:28.753Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API performs element-wise addition of the source operand src and the scalar scalar based on mask, and writes the result to the destination operand dst. The formula is as follows: | ||
| 20 | + | ||
| 21 | +$$dst_i = src_i + scalar$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +adds(src, scalar, preg, mode: Optional[MergeMode] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md). The source operand src and the destination operand dst must have the same data type. Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_FP16, DT_BF16, DT_INT32, DT_UINT32, DT_FP32. | | ||
| 34 | +| scalar | Input | Scalar source operand. | | ||
| 35 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 36 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements of preg that are not selected are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported currently. | | ||
| 37 | + | ||
| 38 | +## Constraints | ||
| 39 | + | ||
| 40 | +None | ||
| 41 | + | ||
| 42 | +## Return Value | ||
| 43 | + | ||
| 44 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), with the same supported data types as described for src. | ||
| 45 | + | ||
| 46 | +## Examples | ||
| 47 | + | ||
| 48 | +```python | ||
| 49 | +import os | ||
| 50 | +import pypto_pro.language as pl | ||
| 51 | +import torch | ||
| 52 | +import torch_npu | ||
| 53 | + | ||
| 54 | +@pl.vector_function | ||
| 55 | +def example_vf(src_tile, dst_tile): | ||
| 56 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 57 | + reg_a = vf.load_align(src_tile, 0) | ||
| 58 | + reg_out = vf.adds(reg_a, 1.0, preg) | ||
| 59 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 60 | + | ||
| 61 | +@pl.jit() | ||
| 62 | +def example_kernel( | ||
| 63 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 64 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 65 | +): | ||
| 66 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 67 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 68 | + in_a = in_a_grp.current() | ||
| 69 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 70 | + t_out = t_out_grp.current() | ||
| 71 | + with pl.section_vector(): | ||
| 72 | + pl.load(in_a, a, [0, 0]) | ||
| 73 | + example_vf(in_a, t_out) | ||
| 74 | + pl.store(out, t_out, [0, 0]) | ||
| 75 | + | ||
| 76 | +def test_example(): | ||
| 77 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 78 | + device = f"npu:{device_id}" | ||
| 79 | + core_nums = 1 | ||
| 80 | + torch.npu.set_device(device) | ||
| 81 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 82 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 83 | + example_kernel[None, core_nums](a, out) | ||
| 84 | + torch.npu.synchronize() | ||
| 85 | + torch.testing.assert_close(out, a + 1.0, rtol=1e-5, atol=1e-5) | ||
| 86 | + | ||
| 87 | +if __name__ == "__main__": | ||
| 88 | + test_example() | ||
| 89 | + print("PASSED") | ||
| 90 | +``` | ||
| @@ -0,0 +1,203 @@ | |||
| 1 | +# vf.div | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:12:40.918Z pushedAt=2026-10-28T11:57:28.767Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +Element-wise division of two reg_tensors. | ||
| 20 | + | ||
| 21 | +$$dstReg_i = srcReg0_i \div srcReg1_i$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +div(src0, src1, preg, mode: Optional[MergeMode] = None, precision: Optional[bool] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src0 | Input | Source operand 0, [reg_tensor](../basic_data_structures/reg_tensor.md). The data types of source operands src0 and src1 and destination operand dst must be consistent. Supported data types: DT_INT16, DT_UINT16, DT_INT32, DT_UINT32, DT_FP16, DT_FP32. | | ||
| 34 | +| src1 | Input | Source operand 1, [reg_tensor](../basic_data_structures/reg_tensor.md). Supported data types are the same as those described for src0. | | ||
| 35 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 36 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is currently not supported. | | ||
| 37 | +| precision | Input | Optional, high-precision mode switch. True enables high-precision mode; False (default) is standard mode. Supported data types in high-precision mode: DT_FP16, DT_FP32. | | ||
| 38 | + | ||
| 39 | +## Constraints | ||
| 40 | + | ||
| 41 | +None | ||
| 42 | + | ||
| 43 | +## Return Value | ||
| 44 | + | ||
| 45 | +Returns dst destination operand, [reg_tensor](../basic_data_structures/reg_tensor.md). Supported data types are the same as those described for src0. | ||
| 46 | + | ||
| 47 | +## Examples | ||
| 48 | + | ||
| 49 | +### Standard Mode Examples | ||
| 50 | + | ||
| 51 | +```python | ||
| 52 | +import os | ||
| 53 | +import pypto_pro.language as pl | ||
| 54 | +import torch | ||
| 55 | +import torch_npu | ||
| 56 | + | ||
| 57 | +@pl.vector_function | ||
| 58 | +def example_vf(src_a, src_b, dst_tile): | ||
| 59 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 60 | + reg_a = vf.load_align(src_a, 0) | ||
| 61 | + reg_b = vf.load_align(src_b, 0) | ||
| 62 | + reg_out = vf.div(reg_a, reg_b, preg) | ||
| 63 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 64 | + | ||
| 65 | +@pl.jit() | ||
| 66 | +def example_kernel( | ||
| 67 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 68 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 69 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 70 | +): | ||
| 71 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 72 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 73 | + in_a = in_a_grp.current() | ||
| 74 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 75 | + in_b = in_b_grp.current() | ||
| 76 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 77 | + t_out = t_out_grp.current() | ||
| 78 | + with pl.section_vector(): | ||
| 79 | + pl.load(in_a, a, [0, 0]) | ||
| 80 | + pl.load(in_b, b, [0, 0]) | ||
| 81 | + example_vf(in_a, in_b, t_out) | ||
| 82 | + pl.store(out, t_out, [0, 0]) | ||
| 83 | + | ||
| 84 | +def test_example(): | ||
| 85 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 86 | + device = f"npu:{device_id}" | ||
| 87 | + core_nums = 1 | ||
| 88 | + torch.npu.set_device(device) | ||
| 89 | + a = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 90 | + b = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 91 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 92 | + example_kernel[None, core_nums](a, b, out) | ||
| 93 | + torch.npu.synchronize() | ||
| 94 | + torch.testing.assert_close(out, a / b, rtol=1e-5, atol=1e-5) | ||
| 95 | + | ||
| 96 | +if __name__ == "__main__": | ||
| 97 | + test_example() | ||
| 98 | + print("PASSED") | ||
| 99 | +``` | ||
| 100 | + | ||
| 101 | +### High-Precision Mode Example | ||
| 102 | + | ||
| 103 | +```python | ||
| 104 | +import os | ||
| 105 | +import pypto_pro.language as pl | ||
| 106 | +import torch | ||
| 107 | +import torch_npu | ||
| 108 | + | ||
| 109 | +@pl.vector_function | ||
| 110 | +def example_vf(src_a, src_b, dst_tile): | ||
| 111 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 112 | + reg_a = vf.load_align(src_a, 0) | ||
| 113 | + reg_b = vf.load_align(src_b, 0) | ||
| 114 | + reg_out = vf.div(reg_a, reg_b, preg, precision=True) | ||
| 115 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 116 | + | ||
| 117 | +@pl.jit() | ||
| 118 | +def example_kernel( | ||
| 119 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 120 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 121 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 122 | +): | ||
| 123 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 124 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 125 | + in_a = in_a_grp.current() | ||
| 126 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 127 | + in_b = in_b_grp.current() | ||
| 128 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 129 | + t_out = t_out_grp.current() | ||
| 130 | + with pl.section_vector(): | ||
| 131 | + pl.load(in_a, a, [0, 0]) | ||
| 132 | + pl.load(in_b, b, [0, 0]) | ||
| 133 | + example_vf(in_a, in_b, t_out) | ||
| 134 | + pl.store(out, t_out, [0, 0]) | ||
| 135 | + | ||
| 136 | +def test_example(): | ||
| 137 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 138 | + device = f"npu:{device_id}" | ||
| 139 | + core_nums = 1 | ||
| 140 | + torch.npu.set_device(device) | ||
| 141 | + a = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 142 | + b = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 143 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 144 | + example_kernel[None, core_nums](a, b, out) | ||
| 145 | + torch.npu.synchronize() | ||
| 146 | + ref = (a.cpu().double() / b.cpu().double()).float().to(device) | ||
| 147 | + torch.testing.assert_close(out, ref, rtol=0, atol=0) | ||
| 148 | + | ||
| 149 | +if __name__ == "__main__": | ||
| 150 | + test_example() | ||
| 151 | + print("PASSED") | ||
| 152 | +### FP16 Data Type High-Precision Example | ||
| 153 | + | ||
| 154 | +```python | ||
| 155 | +import os | ||
| 156 | +import pypto_pro.language as pl | ||
| 157 | +import torch | ||
| 158 | +import torch_npu | ||
| 159 | + | ||
| 160 | +@pl.vector_function | ||
| 161 | +def example_vf_fp16(src_a, src_b, dst_tile): | ||
| 162 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP16) | ||
| 163 | + reg_a = vf.load_align(src_a, 0) | ||
| 164 | + reg_b = vf.load_align(src_b, 0) | ||
| 165 | + reg_out = vf.div(reg_a, reg_b, preg, precision=True) | ||
| 166 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 167 | + | ||
| 168 | +@pl.jit() | ||
| 169 | +def example_kernel_fp16( | ||
| 170 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 171 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 172 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | ||
| 173 | +): | ||
| 174 | + tf = pl.TileType(shape=[1, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | ||
| 175 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 176 | + in_a = in_a_grp.current() | ||
| 177 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 178 | + in_b = in_b_grp.current() | ||
| 179 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 180 | + t_out = t_out_grp.current() | ||
| 181 | + with pl.section_vector(): | ||
| 182 | + pl.load(in_a, a, [0, 0]) | ||
| 183 | + pl.load(in_b, b, [0, 0]) | ||
| 184 | + example_vf_fp16(in_a, in_b, t_out) | ||
| 185 | + pl.store(out, t_out, [0, 0]) | ||
| 186 | + | ||
| 187 | +def test_example_fp16(): | ||
| 188 | + device_id = int(os.environ.get('TILE_FWK_DEVICE_ID', 0)) | ||
| 189 | + device = f'npu:{device_id}' | ||
| 190 | + core_nums = 1 | ||
| 191 | + torch.npu.set_device(device) | ||
| 192 | + a = torch.rand([1, 128], device=device, dtype=torch.float16) + 0.5 | ||
| 193 | + b = torch.rand([1, 128], device=device, dtype=torch.float16) + 0.5 | ||
| 194 | + out = torch.empty([1, 128], device=device, dtype=torch.float16) | ||
| 195 | + example_kernel_fp16[None, core_nums](a, b, out) | ||
| 196 | + torch.npu.synchronize() | ||
| 197 | + ref = (a.cpu().double() / b.cpu().double()).half().to(device) | ||
| 198 | + torch.testing.assert_close(out, ref, rtol=0, atol=0) | ||
| 199 | + | ||
| 200 | +if __name__ == '__main__': | ||
| 201 | + test_example_fp16() | ||
| 202 | + print('PASSED') | ||
| 203 | +``` | ||
| @@ -0,0 +1,139 @@ | |||
| 1 | +# vf.exp | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:12:39.813Z pushedAt=2026-10-28T11:57:28.756Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API performs an element-wise exponential operation on the source operand src based on mask, and writes the result to the destination operand dst. The computation formula is as follows: | ||
| 20 | + | ||
| 21 | +$$dst_i = e^{src_i}$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +exp(src, preg, mode: Optional[MergeMode] = None, precision: Optional[bool] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md). The source operand src and the destination operand dst must have the same data type. Supported data types: DT_FP16 and DT_FP32. | | ||
| 34 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 35 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported. | | ||
| 36 | +| precision | Input | Optional, high-precision mode switch. True enables high-precision mode; False (default) is standard mode. | | ||
| 37 | + | ||
| 38 | +## Constraints | ||
| 39 | + | ||
| 40 | +None | ||
| 41 | + | ||
| 42 | +## Return Value | ||
| 43 | + | ||
| 44 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md). The supported data types are the same as those described for src. | ||
| 45 | + | ||
| 46 | +## Examples | ||
| 47 | + | ||
| 48 | +### Standard Mode Example | ||
| 49 | + | ||
| 50 | +```python | ||
| 51 | +import os | ||
| 52 | +import pypto_pro.language as pl | ||
| 53 | +import torch | ||
| 54 | +import torch_npu | ||
| 55 | + | ||
| 56 | +@pl.vector_function | ||
| 57 | +def example_vf(src_tile, dst_tile): | ||
| 58 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 59 | + reg_a = vf.load_align(src_tile, 0) | ||
| 60 | + reg_out = vf.exp(reg_a, preg) | ||
| 61 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 62 | + | ||
| 63 | +@pl.jit() | ||
| 64 | +def example_kernel( | ||
| 65 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 66 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 67 | +): | ||
| 68 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 69 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 70 | + in_a = in_a_grp.current() | ||
| 71 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 72 | + t_out = t_out_grp.current() | ||
| 73 | + with pl.section_vector(): | ||
| 74 | + pl.load(in_a, a, [0, 0]) | ||
| 75 | + example_vf(in_a, t_out) | ||
| 76 | + pl.store(out, t_out, [0, 0]) | ||
| 77 | + | ||
| 78 | +def test_example(): | ||
| 79 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 80 | + device = f"npu:{device_id}" | ||
| 81 | + core_nums = 1 | ||
| 82 | + torch.npu.set_device(device) | ||
| 83 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 84 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 85 | + example_kernel[None, core_nums](a, out) | ||
| 86 | + torch.npu.synchronize() | ||
| 87 | + torch.testing.assert_close(out, torch.exp(a), rtol=1e-5, atol=1e-5) | ||
| 88 | + | ||
| 89 | +if __name__ == "__main__": | ||
| 90 | + test_example() | ||
| 91 | + print("PASSED") | ||
| 92 | +``` | ||
| 93 | + | ||
| 94 | +### High-Precision Mode Examples | ||
| 95 | + | ||
| 96 | +```python | ||
| 97 | +import os | ||
| 98 | +import pypto_pro.language as pl | ||
| 99 | +import torch | ||
| 100 | +import torch_npu | ||
| 101 | + | ||
| 102 | +@pl.vector_function | ||
| 103 | +def example_vf(src_tile, dst_tile): | ||
| 104 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 105 | + reg_a = vf.load_align(src_tile, 0) | ||
| 106 | + reg_out = vf.exp(reg_a, preg, precision=True) | ||
| 107 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 108 | + | ||
| 109 | +@pl.jit() | ||
| 110 | +def example_kernel( | ||
| 111 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 112 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 113 | +): | ||
| 114 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 115 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 116 | + in_a = in_a_grp.current() | ||
| 117 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 118 | + t_out = t_out_grp.current() | ||
| 119 | + with pl.section_vector(): | ||
| 120 | + pl.load(in_a, a, [0, 0]) | ||
| 121 | + example_vf(in_a, t_out) | ||
| 122 | + pl.store(out, t_out, [0, 0]) | ||
| 123 | + | ||
| 124 | +def test_example(): | ||
| 125 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 126 | + device = f"npu:{device_id}" | ||
| 127 | + core_nums = 1 | ||
| 128 | + torch.npu.set_device(device) | ||
| 129 | + a = torch.linspace(-90, -87.5, 64, device=device, dtype=torch.float32).reshape(1, 64) | ||
| 130 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 131 | + example_kernel[None, core_nums](a, out) | ||
| 132 | + torch.npu.synchronize() | ||
| 133 | + ref = torch.exp(a.cpu().double()).float().to(device) | ||
| 134 | + torch.testing.assert_close(out, ref, rtol=1e-5, atol=0) | ||
| 135 | + | ||
| 136 | +if __name__ == "__main__": | ||
| 137 | + test_example() | ||
| 138 | + print("PASSED") | ||
| 139 | +``` | ||
| @@ -0,0 +1,33 @@ | |||
| 1 | +# Basic Arithmetic | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:12:57.023Z pushedAt=2026-10-28T11:57:28.763Z nl=2 --> | ||
| 4 | + | ||
| 5 | +```{toctree} | ||
| 6 | +:maxdepth: 1 | ||
| 7 | +:titlesonly: | ||
| 8 | + | ||
| 9 | +abs | ||
| 10 | +add | ||
| 11 | +addc | ||
| 12 | +adds | ||
| 13 | +div | ||
| 14 | +exp | ||
| 15 | +leaky_relu | ||
| 16 | +ln | ||
| 17 | +log | ||
| 18 | +log10 | ||
| 19 | +log2 | ||
| 20 | +max | ||
| 21 | +maxs | ||
| 22 | +min | ||
| 23 | +mins | ||
| 24 | +mul | ||
| 25 | +mull | ||
| 26 | +muls | ||
| 27 | +neg | ||
| 28 | +prelu | ||
| 29 | +relu | ||
| 30 | +sqrt | ||
| 31 | +sub | ||
| 32 | +subc | ||
| 33 | +``` | ||
| @@ -0,0 +1,91 @@ | |||
| 1 | +# vf.leaky_relu | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:13:56.389Z pushedAt=2026-10-28T11:57:28.771Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API performs the Leaky ReLU (Leaky Rectified Linear Unit) operation element-wise: if an element in the source operand is greater than or equal to 0, the element is written to the destination operand directly; otherwise, the element is multiplied by the scalar value scalar and then written to the destination operand. The computation formula is as follows: | ||
| 20 | + | ||
| 21 | +$$dst_i = \begin{cases} src_i & src_i \geq 0 \\ src_i \times scalar & src_i < 0 \end{cases}$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +leaky_relu(src, scalar, preg, mode: Optional[MergeMode] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md). The source operand src and the destination operand dst must have the same data type. Supported data types: DT_FP16, DT_FP32. | | ||
| 34 | +| scalar | Input | Scalar source operand, scalar. | | ||
| 35 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 36 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported currently. | | ||
| 37 | + | ||
| 38 | +## Constraints | ||
| 39 | + | ||
| 40 | +None | ||
| 41 | + | ||
| 42 | +## Return Value | ||
| 43 | + | ||
| 44 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), with the same supported data types as described for src. | ||
| 45 | + | ||
| 46 | +## Examples | ||
| 47 | + | ||
| 48 | +```python | ||
| 49 | +import os | ||
| 50 | +import pypto_pro.language as pl | ||
| 51 | +import torch | ||
| 52 | +import torch_npu | ||
| 53 | + | ||
| 54 | +@pl.vector_function | ||
| 55 | +def example_vf(src_tile, dst_tile): | ||
| 56 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 57 | + reg_a = vf.load_align(src_tile, 0) | ||
| 58 | + reg_out = vf.leaky_relu(reg_a, 0.01, preg) | ||
| 59 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 60 | + | ||
| 61 | +@pl.jit() | ||
| 62 | +def example_kernel( | ||
| 63 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 64 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 65 | +): | ||
| 66 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 67 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 68 | + in_a = in_a_grp.current() | ||
| 69 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 70 | + t_out = t_out_grp.current() | ||
| 71 | + with pl.section_vector(): | ||
| 72 | + pl.load(in_a, a, [0, 0]) | ||
| 73 | + example_vf(in_a, t_out) | ||
| 74 | + pl.store(out, t_out, [0, 0]) | ||
| 75 | + | ||
| 76 | +def test_example(): | ||
| 77 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 78 | + device = f"npu:{device_id}" | ||
| 79 | + core_nums = 1 | ||
| 80 | + torch.npu.set_device(device) | ||
| 81 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 82 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 83 | + example_kernel[None, core_nums](a, out) | ||
| 84 | + torch.npu.synchronize() | ||
| 85 | + expected = torch.nn.functional.leaky_relu(a, 0.01) | ||
| 86 | + torch.testing.assert_close(out, expected, rtol=1e-5, atol=1e-5) | ||
| 87 | + | ||
| 88 | +if __name__ == "__main__": | ||
| 89 | + test_example() | ||
| 90 | + print("PASSED") | ||
| 91 | +``` | ||
| @@ -0,0 +1,139 @@ | |||
| 1 | +# vf.ln | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:14:01.129Z pushedAt=2026-10-28T11:57:28.774Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API computes the natural logarithm of the source operand element by element according to the mask and writes the result to the destination operand. The computation formula is as follows: | ||
| 20 | + | ||
| 21 | +$$dst_i = \ln(src_i)$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +ln(src, preg, mode: Optional[MergeMode] = None, precision: Optional[bool] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md), which must be a positive number. The source operand src and the destination operand dst must have the same data type. Supported data types: DT_FP16 and DT_FP32. | | ||
| 34 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 35 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported currently. | | ||
| 36 | +| precision | Input | Optional, high-precision mode switch. True enables high-precision mode; False (default) is standard mode. | | ||
| 37 | + | ||
| 38 | +## Constraints | ||
| 39 | + | ||
| 40 | +None | ||
| 41 | + | ||
| 42 | +## Return Value | ||
| 43 | + | ||
| 44 | +Returns the dst destination operand, [reg_tensor](../basic_data_structures/reg_tensor.md), with the supported data types consistent with those described for src. | ||
| 45 | + | ||
| 46 | +## Examples | ||
| 47 | + | ||
| 48 | +### Standard Mode Examples | ||
| 49 | + | ||
| 50 | +```python | ||
| 51 | +import os | ||
| 52 | +import pypto_pro.language as pl | ||
| 53 | +import torch | ||
| 54 | +import torch_npu | ||
| 55 | + | ||
| 56 | +@pl.vector_function | ||
| 57 | +def example_vf(src_tile, dst_tile): | ||
| 58 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 59 | + reg_a = vf.load_align(src_tile, 0) | ||
| 60 | + reg_out = vf.ln(reg_a, preg) | ||
| 61 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 62 | + | ||
| 63 | +@pl.jit() | ||
| 64 | +def example_kernel( | ||
| 65 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 66 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 67 | +): | ||
| 68 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 69 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 70 | + in_a = in_a_grp.current() | ||
| 71 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 72 | + t_out = t_out_grp.current() | ||
| 73 | + with pl.section_vector(): | ||
| 74 | + pl.load(in_a, a, [0, 0]) | ||
| 75 | + example_vf(in_a, t_out) | ||
| 76 | + pl.store(out, t_out, [0, 0]) | ||
| 77 | + | ||
| 78 | +def test_example(): | ||
| 79 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 80 | + device = f"npu:{device_id}" | ||
| 81 | + core_nums = 1 | ||
| 82 | + torch.npu.set_device(device) | ||
| 83 | + a = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 84 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 85 | + example_kernel[None, core_nums](a, out) | ||
| 86 | + torch.npu.synchronize() | ||
| 87 | + torch.testing.assert_close(out, torch.log(a), rtol=1e-5, atol=1e-5) | ||
| 88 | + | ||
| 89 | +if __name__ == "__main__": | ||
| 90 | + test_example() | ||
| 91 | + print("PASSED") | ||
| 92 | +``` | ||
| 93 | + | ||
| 94 | +### High-Precision Mode Examples | ||
| 95 | + | ||
| 96 | +```python | ||
| 97 | +import os | ||
| 98 | +import pypto_pro.language as pl | ||
| 99 | +import torch | ||
| 100 | +import torch_npu | ||
| 101 | + | ||
| 102 | +@pl.vector_function | ||
| 103 | +def example_vf(src_tile, dst_tile): | ||
| 104 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 105 | + reg_a = vf.load_align(src_tile, 0) | ||
| 106 | + reg_out = vf.ln(reg_a, preg, precision=True) | ||
| 107 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 108 | + | ||
| 109 | +@pl.jit() | ||
| 110 | +def example_kernel( | ||
| 111 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 112 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 113 | +): | ||
| 114 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 115 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 116 | + in_a = in_a_grp.current() | ||
| 117 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 118 | + t_out = t_out_grp.current() | ||
| 119 | + with pl.section_vector(): | ||
| 120 | + pl.load(in_a, a, [0, 0]) | ||
| 121 | + example_vf(in_a, t_out) | ||
| 122 | + pl.store(out, t_out, [0, 0]) | ||
| 123 | + | ||
| 124 | +def test_example(): | ||
| 125 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 126 | + device = f"npu:{device_id}" | ||
| 127 | + core_nums = 1 | ||
| 128 | + torch.npu.set_device(device) | ||
| 129 | + a = torch.linspace(1e-39, 1e-38, 64, device=device, dtype=torch.float32).reshape(1, 64) | ||
| 130 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 131 | + example_kernel[None, core_nums](a, out) | ||
| 132 | + torch.npu.synchronize() | ||
| 133 | + ref = torch.log(a.cpu().double()).float().to(device) | ||
| 134 | + torch.testing.assert_close(out, ref, rtol=1e-5, atol=0) | ||
| 135 | + | ||
| 136 | +if __name__ == "__main__": | ||
| 137 | + test_example() | ||
| 138 | + print("PASSED") | ||
| 139 | +``` | ||
| @@ -0,0 +1,139 @@ | |||
| 1 | +# vf.log | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:14:20.106Z pushedAt=2026-10-28T11:57:28.778Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API computes the natural logarithm of the source operand element by element based on mask and writes the result to the destination operand. vf.log is an alias of vf.ln, and the two behave identically. The computation formula is as follows: | ||
| 20 | + | ||
| 21 | +$$dst_i = \log(src_i)$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +log(src, preg, mode: Optional[MergeMode] = None, precision: Optional[bool] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md), which must be positive. The source operand src and the destination operand dst must have the same data type. Supported data types: DT_FP16 and DT_FP32. | | ||
| 34 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 35 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not selected by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported. | | ||
| 36 | +| precision | Input | Optional, high-precision mode switch. True enables high-precision mode; False (default) is standard mode. | | ||
| 37 | + | ||
| 38 | +## Constraints | ||
| 39 | + | ||
| 40 | +None | ||
| 41 | + | ||
| 42 | +## Return Value | ||
| 43 | + | ||
| 44 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), with the same supported data types as described for src. | ||
| 45 | + | ||
| 46 | +## Examples | ||
| 47 | + | ||
| 48 | +### Standard Mode Examples | ||
| 49 | + | ||
| 50 | +```python | ||
| 51 | +import os | ||
| 52 | +import pypto_pro.language as pl | ||
| 53 | +import torch | ||
| 54 | +import torch_npu | ||
| 55 | + | ||
| 56 | +@pl.vector_function | ||
| 57 | +def example_vf(src_tile, dst_tile): | ||
| 58 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 59 | + reg_a = vf.load_align(src_tile, 0) | ||
| 60 | + reg_out = vf.log(reg_a, preg) | ||
| 61 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 62 | + | ||
| 63 | +@pl.jit() | ||
| 64 | +def example_kernel( | ||
| 65 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 66 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 67 | +): | ||
| 68 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 69 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 70 | + in_a = in_a_grp.current() | ||
| 71 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 72 | + t_out = t_out_grp.current() | ||
| 73 | + with pl.section_vector(): | ||
| 74 | + pl.load(in_a, a, [0, 0]) | ||
| 75 | + example_vf(in_a, t_out) | ||
| 76 | + pl.store(out, t_out, [0, 0]) | ||
| 77 | + | ||
| 78 | +def test_example(): | ||
| 79 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 80 | + device = f"npu:{device_id}" | ||
| 81 | + core_nums = 1 | ||
| 82 | + torch.npu.set_device(device) | ||
| 83 | + a = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 84 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 85 | + example_kernel[None, core_nums](a, out) | ||
| 86 | + torch.npu.synchronize() | ||
| 87 | + torch.testing.assert_close(out, torch.log(a), rtol=1e-5, atol=1e-5) | ||
| 88 | + | ||
| 89 | +if __name__ == "__main__": | ||
| 90 | + test_example() | ||
| 91 | + print("PASSED") | ||
| 92 | +``` | ||
| 93 | + | ||
| 94 | +### High-Precision Mode Examples | ||
| 95 | + | ||
| 96 | +```python | ||
| 97 | +import os | ||
| 98 | +import pypto_pro.language as pl | ||
| 99 | +import torch | ||
| 100 | +import torch_npu | ||
| 101 | + | ||
| 102 | +@pl.vector_function | ||
| 103 | +def example_vf(src_tile, dst_tile): | ||
| 104 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 105 | + reg_a = vf.load_align(src_tile, 0) | ||
| 106 | + reg_out = vf.log(reg_a, preg, precision=True) | ||
| 107 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 108 | + | ||
| 109 | +@pl.jit() | ||
| 110 | +def example_kernel( | ||
| 111 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 112 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 113 | +): | ||
| 114 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 115 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 116 | + in_a = in_a_grp.current() | ||
| 117 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 118 | + t_out = t_out_grp.current() | ||
| 119 | + with pl.section_vector(): | ||
| 120 | + pl.load(in_a, a, [0, 0]) | ||
| 121 | + example_vf(in_a, t_out) | ||
| 122 | + pl.store(out, t_out, [0, 0]) | ||
| 123 | + | ||
| 124 | +def test_example(): | ||
| 125 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 126 | + device = f"npu:{device_id}" | ||
| 127 | + core_nums = 1 | ||
| 128 | + torch.npu.set_device(device) | ||
| 129 | + a = torch.linspace(1e-39, 1e-38, 64, device=device, dtype=torch.float32).reshape(1, 64) | ||
| 130 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 131 | + example_kernel[None, core_nums](a, out) | ||
| 132 | + torch.npu.synchronize() | ||
| 133 | + ref = torch.log(a.cpu().double()).float().to(device) | ||
| 134 | + torch.testing.assert_close(out, ref, rtol=1e-5, atol=0) | ||
| 135 | + | ||
| 136 | +if __name__ == "__main__": | ||
| 137 | + test_example() | ||
| 138 | + print("PASSED") | ||
| 139 | +``` | ||
| @@ -0,0 +1,141 @@ | |||
| 1 | +# vf.log10 | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:14:55.579Z pushedAt=2026-10-28T11:57:28.781Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API computes the base-10 logarithm of the source operand src element by element according to mask, and writes the result to the destination operand dst. It is implemented by multiplying the natural logarithm by 1/ln(10). | ||
| 20 | + | ||
| 21 | +The computation formula is as follows: | ||
| 22 | + | ||
| 23 | +$$dst_i = \log_{10}(src_i)$$ | ||
| 24 | + | ||
| 25 | +## Prototype | ||
| 26 | + | ||
| 27 | +```python | ||
| 28 | +log10(src, preg, mode: Optional[MergeMode] = None, precision: Optional[bool] = None) -> dst | ||
| 29 | +``` | ||
| 30 | + | ||
| 31 | +## Parameters | ||
| 32 | + | ||
| 33 | +| Parameter | Input/Output | Description | | ||
| 34 | +|---|---|---| | ||
| 35 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md), which must be positive. The source operand src and the destination operand dst must have the same data type. Supported data types: DT_FP16 and DT_FP32. | | ||
| 36 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 37 | +| mode | Input | Optional, of the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported. | | ||
| 38 | +| precision | Input | Optional, high-precision mode switch. True enables high-precision mode; False (default) is standard mode. | | ||
| 39 | + | ||
| 40 | +## Constraints | ||
| 41 | + | ||
| 42 | +None | ||
| 43 | + | ||
| 44 | +## Return Value | ||
| 45 | + | ||
| 46 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), with the same supported data types as described for src. | ||
| 47 | + | ||
| 48 | +## Examples | ||
| 49 | + | ||
| 50 | +### Standard Mode Call Example | ||
| 51 | + | ||
| 52 | +```python | ||
| 53 | +import os | ||
| 54 | +import pypto_pro.language as pl | ||
| 55 | +import torch | ||
| 56 | +import torch_npu | ||
| 57 | + | ||
| 58 | +@pl.vector_function | ||
| 59 | +def example_vf(src_tile, dst_tile): | ||
| 60 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 61 | + reg_a = vf.load_align(src_tile, 0) | ||
| 62 | + reg_out = vf.log10(reg_a, preg) | ||
| 63 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 64 | + | ||
| 65 | +@pl.jit() | ||
| 66 | +def example_kernel( | ||
| 67 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 68 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 69 | +): | ||
| 70 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 71 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 72 | + in_a = in_a_grp.current() | ||
| 73 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 74 | + t_out = t_out_grp.current() | ||
| 75 | + with pl.section_vector(): | ||
| 76 | + pl.load(in_a, a, [0, 0]) | ||
| 77 | + example_vf(in_a, t_out) | ||
| 78 | + pl.store(out, t_out, [0, 0]) | ||
| 79 | + | ||
| 80 | +def test_example(): | ||
| 81 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 82 | + device = f"npu:{device_id}" | ||
| 83 | + core_nums = 1 | ||
| 84 | + torch.npu.set_device(device) | ||
| 85 | + a = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 86 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 87 | + example_kernel[None, core_nums](a, out) | ||
| 88 | + torch.npu.synchronize() | ||
| 89 | + torch.testing.assert_close(out, torch.log10(a), rtol=1e-5, atol=1e-5) | ||
| 90 | + | ||
| 91 | +if __name__ == "__main__": | ||
| 92 | + test_example() | ||
| 93 | + print("PASSED") | ||
| 94 | +``` | ||
| 95 | + | ||
| 96 | +### High-Precision Mode Examples | ||
| 97 | + | ||
| 98 | +```python | ||
| 99 | +import os | ||
| 100 | +import pypto_pro.language as pl | ||
| 101 | +import torch | ||
| 102 | +import torch_npu | ||
| 103 | + | ||
| 104 | +@pl.vector_function | ||
| 105 | +def example_vf(src_tile, dst_tile): | ||
| 106 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 107 | + reg_a = vf.load_align(src_tile, 0) | ||
| 108 | + reg_out = vf.log10(reg_a, preg, precision=True) | ||
| 109 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 110 | + | ||
| 111 | +@pl.jit() | ||
| 112 | +def example_kernel( | ||
| 113 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 114 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 115 | +): | ||
| 116 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 117 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 118 | + in_a = in_a_grp.current() | ||
| 119 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 120 | + t_out = t_out_grp.current() | ||
| 121 | + with pl.section_vector(): | ||
| 122 | + pl.load(in_a, a, [0, 0]) | ||
| 123 | + example_vf(in_a, t_out) | ||
| 124 | + pl.store(out, t_out, [0, 0]) | ||
| 125 | + | ||
| 126 | +def test_example(): | ||
| 127 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 128 | + device = f"npu:{device_id}" | ||
| 129 | + core_nums = 1 | ||
| 130 | + torch.npu.set_device(device) | ||
| 131 | + a = torch.linspace(1e-39, 1e-38, 64, device=device, dtype=torch.float32).reshape(1, 64) | ||
| 132 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 133 | + example_kernel[None, core_nums](a, out) | ||
| 134 | + torch.npu.synchronize() | ||
| 135 | + ref = torch.log10(a.cpu().double()).float().to(device) | ||
| 136 | + torch.testing.assert_close(out, ref, rtol=1e-5, atol=0) | ||
| 137 | + | ||
| 138 | +if __name__ == "__main__": | ||
| 139 | + test_example() | ||
| 140 | + print("PASSED") | ||
| 141 | +``` | ||
| @@ -0,0 +1,141 @@ | |||
| 1 | +# vf.log2 | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:15:02.885Z pushedAt=2026-10-28T11:57:28.785Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API computes the base-2 logarithm of the input data src according to mask and writes the result to dst. It is implemented by multiplying the natural logarithm by 1/ln(2). | ||
| 20 | + | ||
| 21 | +The computation formula is as follows: | ||
| 22 | + | ||
| 23 | +$$dst_i = \log_2(src_i)$$ | ||
| 24 | + | ||
| 25 | +## Prototype | ||
| 26 | + | ||
| 27 | +```python | ||
| 28 | +log2(src, preg, mode: Optional[MergeMode] = None, precision: Optional[bool] = None) -> dst | ||
| 29 | +``` | ||
| 30 | + | ||
| 31 | +## Parameters | ||
| 32 | + | ||
| 33 | +| Parameter | Input/Output | Description | | ||
| 34 | +|---|---|---| | ||
| 35 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md), which must be a positive number. The source operand src and the destination operand dst must have the same data type. Supported data types: DT_FP16 and DT_FP32. | | ||
| 36 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 37 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported. | | ||
| 38 | +| precision | Input | Optional, high-precision mode switch. True enables high-precision mode; False (default) is standard mode. | | ||
| 39 | + | ||
| 40 | +## Constraints | ||
| 41 | + | ||
| 42 | +None | ||
| 43 | + | ||
| 44 | +## Return Value | ||
| 45 | + | ||
| 46 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), whose supported data types are the same as those described for src. | ||
| 47 | + | ||
| 48 | +## Examples | ||
| 49 | + | ||
| 50 | +### Standard Mode Examples | ||
| 51 | + | ||
| 52 | +```python | ||
| 53 | +import os | ||
| 54 | +import pypto_pro.language as pl | ||
| 55 | +import torch | ||
| 56 | +import torch_npu | ||
| 57 | + | ||
| 58 | +@pl.vector_function | ||
| 59 | +def example_vf(src_tile, dst_tile): | ||
| 60 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 61 | + reg_a = vf.load_align(src_tile, 0) | ||
| 62 | + reg_out = vf.log2(reg_a, preg) | ||
| 63 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 64 | + | ||
| 65 | +@pl.jit() | ||
| 66 | +def example_kernel( | ||
| 67 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 68 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 69 | +): | ||
| 70 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 71 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 72 | + in_a = in_a_grp.current() | ||
| 73 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 74 | + t_out = t_out_grp.current() | ||
| 75 | + with pl.section_vector(): | ||
| 76 | + pl.load(in_a, a, [0, 0]) | ||
| 77 | + example_vf(in_a, t_out) | ||
| 78 | + pl.store(out, t_out, [0, 0]) | ||
| 79 | + | ||
| 80 | +def test_example(): | ||
| 81 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 82 | + device = f"npu:{device_id}" | ||
| 83 | + core_nums = 1 | ||
| 84 | + torch.npu.set_device(device) | ||
| 85 | + a = torch.abs(torch.randn([1, 64], device=device, dtype=torch.float32)) + 0.1 | ||
| 86 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 87 | + example_kernel[None, core_nums](a, out) | ||
| 88 | + torch.npu.synchronize() | ||
| 89 | + torch.testing.assert_close(out, torch.log2(a), rtol=1e-5, atol=1e-5) | ||
| 90 | + | ||
| 91 | +if __name__ == "__main__": | ||
| 92 | + test_example() | ||
| 93 | + print("PASSED") | ||
| 94 | +``` | ||
| 95 | + | ||
| 96 | +### High-Precision Mode Example | ||
| 97 | + | ||
| 98 | +```python | ||
| 99 | +import os | ||
| 100 | +import pypto_pro.language as pl | ||
| 101 | +import torch | ||
| 102 | +import torch_npu | ||
| 103 | + | ||
| 104 | +@pl.vector_function | ||
| 105 | +def example_vf(src_tile, dst_tile): | ||
| 106 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 107 | + reg_a = vf.load_align(src_tile, 0) | ||
| 108 | + reg_out = vf.log2(reg_a, preg, precision=True) | ||
| 109 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 110 | + | ||
| 111 | +@pl.jit() | ||
| 112 | +def example_kernel( | ||
| 113 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 114 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 115 | +): | ||
| 116 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 117 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 118 | + in_a = in_a_grp.current() | ||
| 119 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 120 | + t_out = t_out_grp.current() | ||
| 121 | + with pl.section_vector(): | ||
| 122 | + pl.load(in_a, a, [0, 0]) | ||
| 123 | + example_vf(in_a, t_out) | ||
| 124 | + pl.store(out, t_out, [0, 0]) | ||
| 125 | + | ||
| 126 | +def test_example(): | ||
| 127 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 128 | + device = f"npu:{device_id}" | ||
| 129 | + core_nums = 1 | ||
| 130 | + torch.npu.set_device(device) | ||
| 131 | + a = torch.linspace(1e-39, 1e-38, 64, device=device, dtype=torch.float32).reshape(1, 64) | ||
| 132 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 133 | + example_kernel[None, core_nums](a, out) | ||
| 134 | + torch.npu.synchronize() | ||
| 135 | + ref = torch.log2(a.cpu().double()).float().to(device) | ||
| 136 | + torch.testing.assert_close(out, ref, rtol=1e-5, atol=0) | ||
| 137 | + | ||
| 138 | +if __name__ == "__main__": | ||
| 139 | + test_example() | ||
| 140 | + print("PASSED") | ||
| 141 | +``` | ||
| @@ -0,0 +1,96 @@ | |||
| 1 | +# vf.max | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:15:39.576Z pushedAt=2026-10-28T11:57:28.789Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API performs an element-wise maximum operation on the source operands src0 and src1 based on mask, and writes the result to the destination operand dst. The computation formula is as follows: | ||
| 20 | + | ||
| 21 | +$$dst_i = \max(src0_i, src1_i)$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +max(src0, src1, preg, mode: Optional[MergeMode] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src0 | Input | Source operand 0, [reg_tensor](../basic_data_structures/reg_tensor.md). The data type of source operand src0 must be consistent with that of destination operand dst. Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_FP16, DT_BF16, DT_INT32, DT_UINT32, DT_FP32. | | ||
| 34 | +| src1 | Input | Source operand 1, [reg_tensor](../basic_data_structures/reg_tensor.md), with the same data type as src0. | | ||
| 35 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 36 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): Elements not selected by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is not supported. | | ||
| 37 | + | ||
| 38 | +## Constraints | ||
| 39 | + | ||
| 40 | +- When src0 is -0 and src1 is +0, the output dst is +0. | ||
| 41 | + | ||
| 42 | +## Return Value | ||
| 43 | + | ||
| 44 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), with the same supported data types as described for src0. | ||
| 45 | + | ||
| 46 | +## Examples | ||
| 47 | + | ||
| 48 | +```python | ||
| 49 | +import os | ||
| 50 | +import pypto_pro.language as pl | ||
| 51 | +import torch | ||
| 52 | +import torch_npu | ||
| 53 | + | ||
| 54 | +@pl.vector_function | ||
| 55 | +def example_vf(src_a, src_b, dst_tile): | ||
| 56 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 57 | + reg_a = vf.load_align(src_a, 0) | ||
| 58 | + reg_b = vf.load_align(src_b, 0) | ||
| 59 | + reg_out = vf.max(reg_a, reg_b, preg) | ||
| 60 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 61 | + | ||
| 62 | +@pl.jit() | ||
| 63 | +def example_kernel( | ||
| 64 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 65 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 66 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 67 | +): | ||
| 68 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 69 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 70 | + in_a = in_a_grp.current() | ||
| 71 | + in_b_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 72 | + in_b = in_b_grp.current() | ||
| 73 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x200, mutex_ids=[2]) | ||
| 74 | + t_out = t_out_grp.current() | ||
| 75 | + with pl.section_vector(): | ||
| 76 | + pl.load(in_a, a, [0, 0]) | ||
| 77 | + pl.load(in_b, b, [0, 0]) | ||
| 78 | + example_vf(in_a, in_b, t_out) | ||
| 79 | + pl.store(out, t_out, [0, 0]) | ||
| 80 | + | ||
| 81 | +def test_example(): | ||
| 82 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 83 | + device = f"npu:{device_id}" | ||
| 84 | + core_nums = 1 | ||
| 85 | + torch.npu.set_device(device) | ||
| 86 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 87 | + b = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 88 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 89 | + example_kernel[None, core_nums](a, b, out) | ||
| 90 | + torch.npu.synchronize() | ||
| 91 | + torch.testing.assert_close(out, torch.maximum(a, b), rtol=1e-5, atol=1e-5) | ||
| 92 | + | ||
| 93 | +if __name__ == "__main__": | ||
| 94 | + test_example() | ||
| 95 | + print("PASSED") | ||
| 96 | +``` | ||
| @@ -0,0 +1,91 @@ | |||
| 1 | +# vf.maxs | ||
| 2 | + | ||
| 3 | +<!-- md-trans-meta sourceCommit=b055f6477ecc13f6fe4ce2ec802663fd5100ad23 translatedAt=2026-10-26T13:16:50.675Z pushedAt=2026-10-28T11:57:28.796Z nl=2 --> | ||
| 4 | + | ||
| 5 | +## Applicable Products | ||
| 6 | + | ||
| 7 | +<!-- npu="950" id1 --> | ||
| 8 | +- Ascend 950PR&950DT products: Supported | ||
| 9 | +<!-- end id1 --> | ||
| 10 | +<!-- npu="A3" id2 --> | ||
| 11 | +- Atlas A3 products: Not supported | ||
| 12 | +<!-- end id2 --> | ||
| 13 | +<!-- npu="910b" id3 --> | ||
| 14 | +- Atlas A2 products: Not supported | ||
| 15 | +<!-- end id3 --> | ||
| 16 | + | ||
| 17 | +## Description | ||
| 18 | + | ||
| 19 | +This API compares the source operand src with the scalar scalar element-wise based on mask. If the source operand value is greater than the scalar, the source operand value is taken; if it is smaller than the scalar, the scalar value is taken. The result is written to the destination operand dst. The computation formula is as follows: | ||
| 20 | + | ||
| 21 | +$$dst_i = \max(src_i, scalar)$$ | ||
| 22 | + | ||
| 23 | +## Prototype | ||
| 24 | + | ||
| 25 | +```python | ||
| 26 | +maxs(src, scalar, preg, mode: Optional[MergeMode] = None) -> dst | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Parameters | ||
| 30 | + | ||
| 31 | +| Parameter | Input/Output | Description | | ||
| 32 | +|---|---|---| | ||
| 33 | +| src | Input | Source operand, [reg_tensor](../basic_data_structures/reg_tensor.md). The source operand src and the destination operand dst have the same data type. Supported data types: DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_FP16, DT_BF16, DT_INT32, DT_UINT32, DT_FP32. | | ||
| 34 | +| scalar | Input | Scalar source operand. | | ||
| 35 | +| preg | Input | [mask_reg](../basic_data_structures/mask_reg.md). | | ||
| 36 | +| mode | Input | Optional, corresponding to the [MergeMode](../basic_data_structures/MergeMode.md) type.<br>- pypto_pro.language.MergeMode.ZEROING (default): elements not filtered by preg are set to 0 in dst.<br>- pypto_pro.language.MergeMode.MERGING is currently not supported. | | ||
| 37 | + | ||
| 38 | +## Constraints | ||
| 39 | + | ||
| 40 | +- When src is -0 and scalar is +0, dst is +0. | ||
| 41 | + | ||
| 42 | +## Return Value | ||
| 43 | + | ||
| 44 | +Returns the destination operand dst, [reg_tensor](../basic_data_structures/reg_tensor.md), with supported data types consistent with those described for src. | ||
| 45 | + | ||
| 46 | +## Examples | ||
| 47 | + | ||
| 48 | +```python | ||
| 49 | +import os | ||
| 50 | +import pypto_pro.language as pl | ||
| 51 | +import torch | ||
| 52 | +import torch_npu | ||
| 53 | + | ||
| 54 | +@pl.vector_function | ||
| 55 | +def example_vf(src_tile, dst_tile): | ||
| 56 | + preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 57 | + reg_a = vf.load_align(src_tile, 0) | ||
| 58 | + reg_out = vf.maxs(reg_a, 0.0, preg) | ||
| 59 | + vf.store_align(dst_tile, reg_out, preg) | ||
| 60 | + | ||
| 61 | +@pl.jit() | ||
| 62 | +def example_kernel( | ||
| 63 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 64 | + out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 65 | +): | ||
| 66 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 67 | + in_a_grp = pl.make_tile_group(type=tf, addrs=0x0, mutex_ids=[0]) | ||
| 68 | + in_a = in_a_grp.current() | ||
| 69 | + t_out_grp = pl.make_tile_group(type=tf, addrs=0x100, mutex_ids=[1]) | ||
| 70 | + t_out = t_out_grp.current() | ||
| 71 | + with pl.section_vector(): | ||
| 72 | + pl.load(in_a, a, [0, 0]) | ||
| 73 | + example_vf(in_a, t_out) | ||
| 74 | + pl.store(out, t_out, [0, 0]) | ||
| 75 | + | ||
| 76 | +def test_example(): | ||
| 77 | + device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0)) | ||
| 78 | + device = f"npu:{device_id}" | ||
| 79 | + core_nums = 1 | ||
| 80 | + torch.npu.set_device(device) | ||
| 81 | + a = torch.randn([1, 64], device=device, dtype=torch.float32) | ||
| 82 | + out = torch.empty([1, 64], device=device, dtype=torch.float32) | ||
| 83 | + example_kernel[None, core_nums](a, out) | ||
| 84 | + torch.npu.synchronize() | ||
| 85 | + expected = torch.clamp(a, min=0.0) | ||
| 86 | + torch.testing.assert_close(out, expected, rtol=1e-5, atol=1e-5) | ||
| 87 | + | ||
| 88 | +if __name__ == "__main__": | ||
| 89 | + test_example() | ||
| 90 | + print("PASSED") | ||
| 91 | +``` | ||