if_scope
Description
Specifies the if_scope code block to be executed if a specified condition of TIK is true.
The pseudocode is as follows:
# When cond1 is true, execute the if_scope code block. if_scope(cond1)
Prototype
if_scope(cond)
Parameters
Parameter |
Input/Output |
Description |
|---|---|---|
cond |
Input |
Indicates a condition. It supports the InputScalar, Scalar, immediate (int and float), Expr, and bool data types, and any, all, and negate functions. Specifically,
NOTICE:
The Expr supports the following operators:
You can use supported operators to form a complex expression. However, the comparison operators cannot be used together. For example, if you want to express n > 1 and n < 4: |
Applicability
Restrictions
For the
For the
Returns
A TikWithScope object.
It is inherited from WithScope of TVM.
Example
tik_instance = tik.Tik()
cond = tik_instance.InputScalar(dtype="int16", name="cond")
src_gm = tik_instance.Tensor(dtype="int64", shape=(16, ),
scope=tik.scope_gm, name="src_gm")
dst_ub = tik_instance.Tensor(dtype="int64", shape=(4, ),
scope=tik.scope_ubuf, name="dst_ub")
dst_gm = tik_instance.Tensor(dtype="int64", shape=(4, ),
scope=tik.scope_gm, name="dst_gm")
# Initialize dst to src_gm[0:3].
tik_instance.data_move(dst_ub, src_gm[0:3,], 0, 1, 1, 0, 0)
# Move slices of src to dst based on cond's value.
# When cond==1 is true, execute the if_scope code block.
with tik_instance.if_scope(cond == 1):
tik_instance.data_move(dst_ub, src_gm[4:7,], 0, 1, 1, 0, 0)
# Move dst to the Global Memory for output.
tik_instance.data_move(dst_gm, dst_ub, 0, 1, 1, 0, 0)
tik_instance.BuildCCE(kernel_name="if_scope", inputs=[src_gm, cond], outputs=[dst_gm])
Result example
Input (src_gm): [0, 1, 2,..., 15] Input (cond): 1 Output (dst_gm): [4, 5, 6, 7]