else_scope

Description

Specifies the code block in the else_scope statement to be executed if previous if_scope and elif_scope statements are not 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)

# else_scope (if any) must come after if_scope/elif_scope.
else_scope()

Prototype

else_scope()

Parameters

None

Applicability

Atlas 200/300/500 Inference Product

Atlas Training Series Product

Restrictions

  • Must come after the if_scope or elif_scope function.
  • The number of elif_scope and else_scope statements following if_scope must not exceed 400.

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 met, 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 not met and cond==2 is met, 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 neither cond==1 nor cond==2 is met, execute the else_scope code block.
with tik_instance.else_scope():
    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]