Scalar

Description

Defines a Scalar variable.

Prototype

Scalar(dtype="int64", name="reg_buf", init_value=None)

Parameters

Table 1 Parameter description

Parameter

Input/Output

Description

dtype

Input

Data type of the Scalar object, such as:

int8, uint8, int16, uint16, float16, int32, uint32, float32, int64, uint64

Defaults to int64.

name

Input

A string specifying the name of the Scalar object. Only digits, letters, and underscores (_) are allowed.

Defaults to reg_buf$(COUNT), with COUNT starting at 0.

init_value

Input

Initial value. Supported data types:

  • An immediate of type int or float
  • A Scalar variable
  • A Tensor value
  • An Expr consisting of a Scalar variable and an immediate
NOTICE:

Exprs containing float immediates are not supported.

Applicability

Atlas 200/300/500 Inference Product

Atlas Training Series Product

Restrictions

When the initial value is an Expr or a list or tuple of Exprs, the constituent immediates must be of type int, instead of float. For example:

from tbe import tik
tik_instance = tik.Tik()
index_reg = tik_instance.Scalar(dtype="float32")
# Immediate: float
index_reg.set_as(10.2)

# Assign an initial value to the scalar using init_value.
index_reg1 = tik_instance.Scalar(dtype="float32", init_value=10.2)
 
index_reg2 = tik_instance.Scalar(dtype="float32")
# Expr. The immediate is a float . A CCEC compile error is reported because the hardware does not support this data type.
#index_reg2.set_as(index_reg + 2.2)

Returns

A Scalar instance.

Example

from tbe import tik
tik_instance = tik.Tik()
# Immediate: int
index_reg = tik_instance.Scalar(dtype = "int32")
index_reg.set_as(10)   

# Immediate: float
index_reg2 = tik_instance.Scalar(dtype = "float16")
index_reg2.set_as(10.2) 

# A Scalar variable
index_reg3 = tik_instance.Scalar(dtype = "float16")
index_reg3.set_as(index_reg2)  

# A Tensor value
data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_ubuf)
index_reg3.set_as(data_A[0])// tensor value

#An Expr
index_reg4 = tik_instance.Scalar(dtype = "int32")
index_reg4.set_as(index_reg + 20)