Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Host scalars

int, float and bool are host scalars: Python numbers and booleans, kept on the host rather than on the device, for counters, epochs, seeds and flags. A number written in the source is an int or a float, true and false are bools, and a dim or axis name used as a value is an int. A type that is only int, float or bool declares one, as in steps: int or -> bool. Internally a host scalar is a value of shape [] with a host dtype, and messages show it as int, float or bool.

  • Arithmetic on host scalars gives a host scalar: int with int gives an int, except that / gives a float, and anything with a float gives a float. Arithmetic with a tensor gives a tensor, with the dtype promotion gives.
  • A comparison of host scalars gives a bool, and and, or and ! take and give bools; they compile to Python’s and, or and not. A bool isn’t a number, so arithmetic on one is E0505, and !, and or or on an int is E0501.
  • A parameter or return type that is a host scalar takes a host scalar or a Python value. An int fits where a float is expected, and nothing else converts: a float where an int is expected is E0502, and a tensor, even one of shape [], is E0805, since reading it would wait for the device.
  • x as int, x as float and x as bool give a Python value a host type, with a run-time check that it is one (Runtime checks); a bool is neither an int nor a float. On a tensor, such an as is E0805. to int is E0504: no conversion makes a host scalar of a tensor.
  • An int or float has a range, as a tensor does (Range inference), so a counter that starts at 0 and is only increased in a loop is in $[0, \infty)$ (Blocks and scope).

A host scalar parameter isn’t checked at run time.