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

Table 1 Parameter description

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,
  • Arguments of the Scalar and InputScalar types are automatically converted into Exprs. For example, if the argument name is a, it is converted into a!=0.
  • Arguments of the Expr type are automatically converted into Exprs with !=0. For example, if the argument is scalar // 2, it is converted to scalar // 2!=0.
NOTICE:

The Expr supports the following operators:

  • Comparison operators: >, <, !=, ==, >=, <=
  • Arithmetic operators: +, , *, /, //, %
  • Bitwise 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:

  • The following is not supported:

    1 < n < 4

  • The recommended expression is:

    tik.all((1 < n) , (n < 4))

Applicability

Atlas training product

Atlas inference product AI Core

Atlas inference product Vector Core

Atlas A2 training product/Atlas A2 inference product

Atlas 200I/500 A2 inference product

Restrictions

For the Atlas training product, the Expr must not contain Scalars of type float16.

For the Atlas inference product AI Core, the Expr must not contain Scalars of type float16.

For the Atlas inference product Vector Core, the Expr must not contain Scalars of type float16.

For the Atlas A2 training product/Atlas A2 inference product, the Expr must not contain Scalars of type float16.

For the Atlas 200I/500 A2 inference product, the Expr must not contain Scalars of type float16.

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]