scalar_conv

Description

Converts the data type of a Scalar.

The following conversion modes are supported:

  • int32 to float32
  • float32 to int32
  • float32 to float16
  • float16 to float32

Prototype

scalar_conv(round_mode, dst, src)

Parameters

Table 1 Parameter description

Parameter

Input/Output

Description

dst

Output

A Scalar of type float32/float16/int32, for the destination operand.

round_mode

Input

Rounding mode

  • '' or 'none': no rounding
  • 'round': rounds x.5 to the nearest even number and applies banker's rounding for other values.
  • 'floor': rounds down.
  • 'ceil' or 'ceiling': rounds up.
  • 'away-zero':

    Atlas training product: rounds away from 0. For a positive number, x.x is rounded to (x + 1). For a negative number, –x.x is rounded to –(x + 1).

    Atlas A2 training product/Atlas A2 inference product: rounds away from 0. For a positive number, x.x is rounded to (x + 1). For a negative number, –x.x is rounded to –(x + 1).

    Atlas 200I/500 A2 inference product: rounds away from 0. For a positive number, x.x is rounded to (x + 1). For a negative number, –x.x is rounded to –(x + 1).

    Atlas inference product AI Core: rounds to nearest whole number.

    Atlas inference product Vector Core: rounds to nearest whole number.

  • 'to-zero': rounds towards 0.
  • 'odd': rounds to the nearest odd number.

Table 2 describes the precision conversion and the corresponding round_mode.

src

Input

Source operand.

A Scalar of type float32/float16/int32.

Table 2 round_mode

Source Operand

Destination Operand

Conversion Mode

float32

int32

'round', 'away-zero', 'to-zero', 'floor', 'ceil', 'ceiling'

int32

float32

'', 'none'

float16

float32

'', 'none'

float32

float16

'', 'none', 'odd'

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

Note the accuracy loss risks.

Table 3 Atlas training product round_mode examples

Value

round

floor

ceil/ceiling

away-zero

to-zero

odd

1.8

2

1

2

2

1

2

1.5

2

1

2

2

1

1

1.2

1

1

2

2

1

1

0.8

1

0

1

1

0

1

0.5

0

0

1

1

0

1

0.2

0

0

1

1

0

0

-0.2

0

-1

0

-1

0

0

-0.5

0

-1

0

-1

0

-1

-0.8

-1

-1

0

-1

0

-1

-1.2

-1

-2

-1

-2

-1

-1

-1.5

-2

-2

-1

-2

-1

-1

-1.8

-2

-2

-1

-2

-1

2

Table 4 Atlas A2 training product/Atlas A2 inference product round_mode examples

Value

round

floor

ceil/ceiling

away-zero

to-zero

odd

1.8

2

1

2

2

1

2

1.5

2

1

2

2

1

1

1.2

1

1

2

2

1

1

0.8

1

0

1

1

0

1

0.5

0

0

1

1

0

1

0.2

0

0

1

1

0

0

-0.2

0

-1

0

-1

0

0

-0.5

0

-1

0

-1

0

-1

-0.8

-1

-1

0

-1

0

-1

-1.2

-1

-2

-1

-2

-1

-1

-1.5

-2

-2

-1

-2

-1

-1

-1.8

-2

-2

-1

-2

-1

2

Table 5 Atlas 200I/500 A2 inference product round_mode examples

Value

round

floor

ceil/ceiling

away-zero

to-zero

odd

1.8

2

1

2

2

1

2

1.5

2

1

2

2

1

1

1.2

1

1

2

2

1

1

0.8

1

0

1

1

0

1

0.5

0

0

1

1

0

1

0.2

0

0

1

1

0

0

-0.2

0

-1

0

-1

0

0

-0.5

0

-1

0

-1

0

-1

-0.8

-1

-1

0

-1

0

-1

-1.2

-1

-2

-1

-2

-1

-1

-1.5

-2

-2

-1

-2

-1

-1

-1.8

-2

-2

-1

-2

-1

2

Table 6 Atlas inference product AI Core round_mode examples

Value

round

floor

ceil/ceiling

away-zero

to-zero

odd

1.8

2

1

2

2

1

2

1.5

2

1

2

2

1

1

1.2

1

1

2

1

1

1

0.8

1

0

1

1

0

1

0.5

0

0

1

1

0

1

0.2

0

0

1

0

0

0

-0.2

0

-1

0

0

0

0

-0.5

0

-1

0

-1

0

-1

-0.8

-1

-1

0

-1

0

-1

-1.2

-1

-2

-1

-1

-1

-1

-1.5

-2

-2

-1

-2

-1

-1

-1.8

-2

-2

-1

-2

-1

2

Table 7 Atlas inference product Vector Core round_mode examples

Value

round

floor

ceil/ceiling

away-zero

to-zero

odd

1.8

2

1

2

2

1

2

1.5

2

1

2

2

1

1

1.2

1

1

2

1

1

1

0.8

1

0

1

1

0

1

0.5

0

0

1

1

0

1

0.2

0

0

1

0

0

0

-0.2

0

-1

0

0

0

0

-0.5

0

-1

0

-1

0

-1

-0.8

-1

-1

0

-1

0

-1

-1.2

-1

-2

-1

-1

-1

-1

-1.5

-2

-2

-1

-2

-1

-1

-1.8

-2

-2

-1

-2

-1

2

Returns

None

Example

from tbe import tik
tik_instance = tik.Tik()
src_scalar = tik_instance.Scalar(dtype="float32")
src_scalar.set_as(1.4)
dst_scalar = tik_instance.Scalar(dtype="int32")
round_mode = "ceiling"
tik_instance.scalar_conv(round_mode, dst_scalar, src_scalar)
"""
  round_mode=round dst_scalar=1
  round_mode=away-zero dst_scalar=2
  round_mode=to-zero dst_scalar=1
  round_mode=floor dst_scalar=1
  round_mode=ceil dst_scalar=2
  round_mode=ceiling dst_scalar=2
"""
tik_instance.BuildCCE(kernel_name="run_scalar_conv", inputs=[], outputs=[])