asc2.inline_vf
- asc2.inline_vf(code: str, shape: Tuple[int, ...], dtype: DataType, inputs: Iterable[LocalTensor] | None = None) LocalTensor
Embed Ascend C VF (vector function) code within a kernel.
This is an escape hatch for advanced users who need to express vector-fusion operations (e.g., Ascend C MicroAPI calls) that are not covered by the built-in API. The provided code string is injected verbatim as the body of a
__VEC_SCOPE__block in the generated Ascend C source.Tensors are referenced by positional placeholders:
$0is always the output tensor, and$1,$2, … refer to the input tensors in the order they appear ininputs. Zero or more input tensors are allowed. Each placeholder will be replaced with aLocalTensorallocated for a corresponding tensor.All input tensors must reside in UB memory. The output tensor is always allocated in UB.
- Parameters:
code – The raw Ascend C code string to embed (treated as a
__VEC_SCOPE__body). Use$0for the output tensor and$1,$2, … for input tensors.shape – The shape of the output tensor.
dtype – The data type of the output tensor.
inputs – An optional iterable of zero or more input tensors referenced as
$1,$2, … in the code.
- Returns:
A new UB tensor (
$0) containing the result produced by the inline vector function.- Return type:
- Raises:
TypeError – If code is not a str, dtype is not a DataType, or any input is not a LocalTensor.
RuntimeError – If any input tensor is not located in UB memory or shape is invalid.
Examples
Embed an inline vector multiply-add (
x * y + z) using Ascend C MicroAPI:out = asc2.inline_vf( ''' auto* out_ptr = reinterpret_cast<__ubuf__ float*>($0.GetPhyAddr()); auto* x_ptr = reinterpret_cast<__ubuf__ float*>($1.GetPhyAddr()); auto* y_ptr = reinterpret_cast<__ubuf__ float*>($2.GetPhyAddr()); auto* z_ptr = reinterpret_cast<__ubuf__ float*>($3.GetPhyAddr()); AscendC::MicroAPI::RegTensor<float> result_reg; . . . AscendC::MicroAPI::MaskReg mask_reg = AscendC::MicroAPI::UpdateMask<float>(mask); AscendC::MicroAPI::DataCopy(x_reg, x_ptr); AscendC::MicroAPI::DataCopy(y_reg, y_ptr); AscendC::MicroAPI::Mul(xy_reg, x_reg, y_reg, mask_reg); . . . ''', x.shape, x.dtype, [x, y, z])
In the example above,
$0placeholder refers to aLocalTensorcorresponding toouttensor;$1,$2, and$3refers tox,y, andzrespectively.