Numbers¶
The reference machine holds every number as a
tensor. A NumberDomain names the real dtype and the device
of a run; machine_value converts a host value to that form
where it enters the machine, and host_value converts a
machine value back to plain Python data where it leaves.
numbers
¶
The numbers of the reference machine, as tensors.
Every value of a numeric type that the machine computes is a
torch.Tensor: a Real or a LogWeight is a zero-dimensional
floating tensor of the run's real dtype, an Int a zero-dimensional
int64 tensor, a Bool a zero-dimensional bool tensor, and a
Tensor of one of those element types a tensor of its shape. A value of
any other type keeps its host form: a string, a constructor, a product as a
tuple, a tensor of structured elements as nested tuples.
So the machine keeps the gradient of everything it computes, and a number leaves the tensor representation in exactly three places, each of which reads a discrete value and none of which needs a derivative:
- a decision, the condition of a branch (
decide), which selects the code that runs; - a host index, an
Intthat selects an entry of a tuple of structured values or keys an item (host_index,discrete_key), and a number a report states (host_real); - the host boundary, where values enter from or return to Python
(
machine_value,host_value).
Over many positions at once (quivers._positions.map_positions) a value
may depend on the position. A decision or a host index read from such a
value has one answer per position, which no single evaluation can follow;
reading one raises PositionDependentControl, and the caller evaluates the
positions one at a time instead.
NumberKind
¶
What kind of number a numeric type holds.
RealDtype
¶
The representations a run may carry its real numbers in: a floating
dtype, or "rational" for exact rational numbers
(quivers.core.exact.RationalTensor).
PositionDependentControl
¶
Bases: RuntimeError
A discrete value read for control depends on the position.
Raised over many positions at once when a branch condition, a host index, or an item key differs between positions: one evaluation cannot take every branch, so the positions must be evaluated one at a time.
| PARAMETER | DESCRIPTION |
|---|---|
what
|
What read the value.
TYPE:
|
Source code in src/quivers/qiec/numbers.py
NumberDomain
didactic-model
¶
NumberDomain(*, real: RealDtype = 'float64', device: str = 'cpu')
Bases: QiecModel
The dtype and device a run carries its numbers in.
An exact domain (real="rational") carries Real and LogWeight
values as exact rational tensors on the CPU, so a run either computes
them exactly or refuses the first operation the rationals are not
closed under (quivers.core.exact).
| PARAMETER | DESCRIPTION |
|---|---|
real
|
The floating dtype of
TYPE:
|
device
|
The device of every number; an exact domain is on the CPU.
TYPE:
|
is_exact
property
¶
Whether reals are exact rationals.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
real_dtype
property
¶
real_dtype: dtype
The floating dtype.
| RETURNS | DESCRIPTION |
|---|---|
dtype
|
The dtype |
torch_device
property
¶
torch_device: device
The device.
| RETURNS | DESCRIPTION |
|---|---|
device
|
The device |
exact
classmethod
¶
exact() -> NumberDomain
The exact domain of rational numbers.
| RETURNS | DESCRIPTION |
|---|---|
NumberDomain
|
The domain with |
of
classmethod
¶
of(tensor: Tensor) -> NumberDomain
The domain of a floating tensor.
| PARAMETER | DESCRIPTION |
|---|---|
tensor
|
A tensor whose dtype and device the run takes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NumberDomain
|
The domain; a tensor that is not floating gives the default floating dtype. |
Source code in src/quivers/qiec/numbers.py
default
classmethod
¶
default() -> NumberDomain
The domain of torch's default floating dtype on the CPU.
| RETURNS | DESCRIPTION |
|---|---|
NumberDomain
|
The domain. |
dtype
¶
dtype(kind: NumberKind) -> dtype
The dtype of one kind of number.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The kind.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dtype
|
The real dtype, |
Source code in src/quivers/qiec/numbers.py
number
¶
number(kind: NumberKind, value: bool | int | float) -> Tensor
A host scalar as a number of the machine.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The kind of number.
TYPE:
|
value
|
The scalar.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A zero-dimensional tensor of the kind's dtype; an exact domain's real is a rational tensor, a float read as its shortest decimal. |
Source code in src/quivers/qiec/numbers.py
zeros
¶
zeros(shape: tuple[int, ...], kind: NumberKind) -> Tensor
A tensor of zeros of one kind of number.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The shape.
TYPE:
|
kind
|
The kind of number.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The zeros in the kind's dtype, rational for an exact domain's reals. |
Source code in src/quivers/qiec/numbers.py
real_of
¶
Integers, truth values, or reals as this domain's reals.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A tensor of any numeric dtype.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The same numbers as reals: exactly, as a rational tensor, for an exact domain; in the real dtype otherwise. |
Source code in src/quivers/qiec/numbers.py
number_kind
¶
number_kind(type_: TypeExpr) -> NumberKind | None
The kind of number a type's values are, if they are numbers.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
A type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NumberKind | None
|
The kind for a numeric scalar type or a tensor of one, |
Source code in src/quivers/qiec/numbers.py
static_shape
¶
static_shape(type_: TypeExpr) -> tuple[int, ...] | None
The literal shape of a tensor type.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
A type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, ...] | None
|
The dimensions when every one is a literal; |
Source code in src/quivers/qiec/numbers.py
machine_value
¶
machine_value(type_: TypeExpr, value: object, domain: NumberDomain) -> object
A host value as the machine holds it, by its checked type.
The value is converted where it enters the machine: a run's argument, an attachment, the environment of a host evaluation. A tensor keeps its gradient; only its dtype and device are made the run's.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
The value's checked type.
TYPE:
|
value
|
The host value: a number, nested tuples or lists of numbers, a tensor, or any value of a non-numeric type.
TYPE:
|
domain
|
The run's numbers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
A tensor for a value of a numeric type whose host form is one; components converted for a product; the value itself otherwise, so a value of the wrong form reaches the type's validator, which names it. |
Source code in src/quivers/qiec/numbers.py
host_value
¶
A machine value as plain Python data, for the host to print or store.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A machine value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
A number for a zero-dimensional tensor, nested tuples of numbers for a tensor of higher rank, the components of a tuple converted, and any other value itself. |
Source code in src/quivers/qiec/numbers.py
decide
¶
Read a truth value that selects the code a run takes.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The machine's truth value.
TYPE:
|
what
|
What decides, for the diagnostics.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
The truth value. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the value is not a scalar truth value. |
PositionDependentControl
|
If, over many positions, it depends on the position. |
Source code in src/quivers/qiec/numbers.py
host_index
¶
Read an integer that selects among host values.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The machine's integer.
TYPE:
|
what
|
What reads it, for the diagnostics.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The integer. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the value is not a scalar integer. |
PositionDependentControl
|
If, over many positions, it depends on the position. |
Source code in src/quivers/qiec/numbers.py
host_real
¶
host_real(value: Tensor, what: str) -> float
Read a real number a report or a diagnostic states.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A scalar real of the machine.
TYPE:
|
what
|
What reads it, for the diagnostics.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The number; the reading keeps no gradient, since a report is not part of the computation. An exact rational is rounded to the nearest float, since a report states it, not the run. |
| RAISES | DESCRIPTION |
|---|---|
PositionDependentControl
|
If, over many positions, it depends on the position. |
Source code in src/quivers/qiec/numbers.py
discrete_key
¶
discrete_key(value: Tensor) -> tuple[str, tuple[int, ...], tuple[object, ...]]
The value of a tensor of integers or truth values, as a hashable key.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A tensor of a dtype that is not floating.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, tuple[int, ...], tuple[object, ...]]
|
Its dtype, its shape and its entries in row-major order. |
| RAISES | DESCRIPTION |
|---|---|
PositionDependentControl
|
If, over many positions, it depends on the position. |