elif_scope

Description

Specifies the current elif_scope code block to be executed if previous if_scope and elif_scope are not true and the current elif_scope is true.

The pseudocode is as follows:

# When cond1 is true, execute the if_scope code block.
if_scope(cond1)

# Execute the elif_scope code block if if_scope is not true while cond2 in this statement is true.
# The first elif_scope statement must be after the if_scope function.
elif_scope(cond2)

# Execute the elif_scope code block when the preceding if_scope and elif_scope statements are not true while cond3 in this statement is true.
elif_scope(cond3)

Prototype

elif_scope(cond)

Parameters

Table 1 Parameter description

Parameter

Input/Output

Description

cond

Input

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

  • Use this conditional statement after the if_scope or elif_scope function.
  • The number of elif_scope and else_scope statements following if_scope must not exceed 400.
  • The Scalar in Expr does not support the following data types:

    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)
# When cond==1 is false and cond==2 is true, execute the elif_scope code block.
with tik_instance.elif_scope(cond == 2):
    tik_instance.data_move(dst_ub, src_gm[8:11,], 0, 1, 1, 0, 0)
# When cond==1 and cond==2 are false while cond==3 is true, execute the elif_scope code block.
with tik_instance.elif_scope(cond == 3):
    tik_instance.data_move(dst_ub, src_gm[12:15,], 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):
3

Output (dst_gm):
[12, 13, 14, 15]