Source code for atomiq.point_schema
"""Point schema entries, tiers and the mapping of argument values to kernel types.
The point schema lists every attribute of a scan point together with its tier and kernel type. It is frozen once per
run and drives the generated point class, the chunk-fetch RPC and the generated kernel code.
"""
from dataclasses import dataclass
from enum import Enum
import numpy as np
[docs]
class Tier(Enum):
"""Classification of a point attribute by when its value can change.
``CONSTANT`` attributes are fixed for the whole run. They are declared as kernel invariants, so the compiler can
hoist their loads out of loops. They hold arguments that are neither scanned nor mutable, and the run id.
``SCANNED`` attributes vary per point. They hold scan axes with more than one value and the step counter. The kernel
writes them for every point from the columns of the fetched chunk.
``CHUNK`` attributes belong to mutable arguments that are not an active scan axis. The value is read once per
chunk. It is constant within a chunk and may differ between chunks.
``COMPUTED`` attributes are set inside the kernel and never sent by the host. The point identifier is one.
"""
CONSTANT = "constant"
SCANNED = "scanned"
CHUNK = "chunk"
COMPUTED = "computed"
[docs]
@dataclass(frozen=True)
class PointSchemaEntry:
"""One attribute of the frozen point schema.
Args:
name: Attribute name, read as ``point.<name>`` in user code.
tier: Tier of the attribute.
type: Kernel type of the attribute, for example ``float`` or ``str``.
options: Allowed values of an enumeration. The wire value is the index into this tuple. ``None`` for all other
entries.
"""
name: str
tier: Tier
type: type
options: tuple[str, ...] | None = None
[docs]
def kernel_type_of(value: object) -> type:
"""Return the kernel type for a host value.
The mapping is ``bool`` to ``bool``, integers to ``numpy.int64``, floats to ``float`` and strings to ``str``. The
bool check comes first because ``bool`` is a subclass of ``int``.
Args:
value: Host value of an argument.
Returns:
The kernel type of the value.
Raises:
TypeError: If the value has no kernel type.
"""
if isinstance(value, bool | np.bool_):
return bool
if isinstance(value, int | np.integer):
return np.int64
if isinstance(value, float | np.floating):
return float
if isinstance(value, str):
return str
msg = f"no kernel type for value of type {type(value).__name__}"
raise TypeError(msg)
[docs]
def to_wire(value: object, entry: PointSchemaEntry) -> object:
"""Cast a host value to the scalar that is sent to the kernel for a schema entry.
This only casts. Callers validate the value first, because the casts truncate silently: ``numpy.int64(2.7)`` is 2
and ``bool("False")`` is ``True``. ARTIQ checks RPC return values strictly. A ``numpy.bool_`` is rejected for a bool and an int is rejected for a
float, so every value is cast to the exact type of its entry. Enumeration values are sent as a ``numpy.int32``
index into the options of the entry.
Args:
value: Host value of the argument.
entry: Schema entry of the argument.
Returns:
The value as the kernel type of the entry, or the option index for an enumeration.
Raises:
ValueError: If the entry has options and the value is not one of them.
OverflowError: If an integer value does not fit into ``numpy.int64``.
"""
if entry.options is None:
return entry.type(value)
if value not in entry.options:
msg = f"{value!r} is not an option of {entry.name}: {entry.options}"
raise ValueError(msg)
return np.int32(entry.options.index(value))