alkaid package

Subpackages

Submodules

alkaid.types module

class alkaid.types.CombLogic(shape: tuple[int, int], inp_shifts: list[int], out_idxs: list[int], out_shifts: list[int], out_negs: list[bool], ops: list[Op], carry_size: int, adder_size: int, lookup_tables: tuple[LookupTable, ...] | None = None)

Bases: NamedTuple

ALIR combinational program.

ops is an SSA-style operation list. Executing each operation populates one buffer element; outputs are selected with out_idxs, scaled by out_shifts, and negated according to out_negs. lookup_tables stores the tables referenced by opcode 8 operations when present.

adder_size: int

Alias for field number 7

carry_size: int

Alias for field number 6

property cost

Total cost of the solution.

exec_op(op: Op, buf: ndarray, inp: ndarray)
classmethod from_dict(dump: dict, raw=False)

Load ALIR from a serialized dictionary.

property inp_kifs

KIFs of all input elements of the solution.

property inp_latency

Latencies of all input elements of the solution.

property inp_qint

Quantization intervals of the input elements.

inp_shifts: list[int]

Alias for field number 1

property kernel

the kernel represented by the solution, when applicable.

property latency

Minimum and maximum latency of the solution.

classmethod load(path: str | Path)

Load from a JSON file; accepts gzip (detected by magic bytes).

lookup_tables: tuple[LookupTable, ...] | None

Alias for field number 8

ops: list[Op]

Alias for field number 5

out_idxs: list[int]

Alias for field number 2

property out_kifs

KIFs of all output elements of the solution.

property out_latency

Latencies of all output elements of the solution.

out_negs: list[bool]

Alias for field number 4

property out_qint

Quantization intervals of the output elements.

out_shifts: list[int]

Alias for field number 3

predict(data: ndarray[tuple[Any, ...], dtype[_ScalarT]] | Sequence[ndarray[tuple[Any, ...], dtype[_ScalarT]]], n_threads: int = 0, debug=False, dump=False) ndarray[tuple[Any, ...], dtype[float64]]

Predict a batch with the C++ ALIR interpreter.

Cannot be used if the binary interpreter is not installed.

Parameters:
  • data (NDArray|Sequence[NDArray]) – Input data to the model. The shape is ignored, and the number of samples is determined by the size of the data.

  • n_threads (int) – Number of threads to use for prediction. Negative or zero values will use maximum available threads, or the value of the DA_DEFAULT_THREADS environment variable if set. Default is 0. If OpenMP is not supported, this parameter is ignored.

  • debug (bool) – If True, the function will print debug information about the operations being performed.

Returns:

Output of the model in shape (n_samples, output_size).

Return type:

NDArray[np.float64]

property ref_count: ndarray

The number of references to the output elements in the solution.

save(path: str | Path, compresslevel: int = 6)

Save to a JSON file; gzip-compresses if path ends with .gz.

shape: tuple[int, int]

Alias for field number 0

to_bytecode(version: int = 0) ndarray[tuple[Any, ...], dtype[int32]]

Return the int32 bytecode array consumed by the C++ ALIR interpreter.

class alkaid.types.DAState(shifts: tuple[ndarray[tuple[Any, ...], dtype[int8]], ndarray[tuple[Any, ...], dtype[int8]]], expr: list[ndarray[tuple[Any, ...], dtype[int8]]], ops: list[Op], freq_stat: dict[Pair, int], kernel: ndarray[tuple[Any, ...], dtype[float32]])

Bases: NamedTuple

Internal state of the DA algorithm.

expr: list[ndarray[tuple[Any, ...], dtype[int8]]]

Alias for field number 1

freq_stat: dict[Pair, int]

Alias for field number 3

kernel: ndarray[tuple[Any, ...], dtype[float32]]

Alias for field number 4

ops: list[Op]

Alias for field number 2

shifts: tuple[ndarray[tuple[Any, ...], dtype[int8]], ndarray[tuple[Any, ...], dtype[int8]]]

Alias for field number 0

class alkaid.types.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: JSONEncoder

default(o)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
class alkaid.types.Op(id0: int, id1: int, opcode: int, data: int, qint: QInterval, latency: float, cost: float)

Bases: NamedTuple

One ALIR operation that writes a single data-buffer element.

Parameters:
  • id0 (int) – Index of the first operand, or the input index for opcode -1.

  • id1 (int) – Index of the second operand, or -1 when unused.

  • opcode (int) – Operation code. See docs/alir.md for the opcode table.

  • data (int) – Opcode-specific integer payload.

  • qint (QInterval) – Quantization interval of the produced buffer element.

  • latency (float) – Estimated availability time of the produced value.

  • cost (float) – Estimated cost of the operation.

cost: float

Alias for field number 6

data: int

Alias for field number 3

id0: int

Alias for field number 0

id1: int

Alias for field number 1

property input_ids: tuple[int, ...]
latency: float

Alias for field number 5

opcode: int

Alias for field number 2

qint: QInterval

Alias for field number 4

class alkaid.types.Pair(id0: int, id1: int, sub: bool, shift: int)

Bases: NamedTuple

An operation representing data[id0] +/- data[id1] * 2**shift.

id0: int

Alias for field number 0

id1: int

Alias for field number 1

shift: int

Alias for field number 3

sub: bool

Alias for field number 2

class alkaid.types.Pipeline(solutions: tuple[CombLogic, ...])

Bases: NamedTuple

Initiation-interval-one pipeline represented as cascaded CombLogic stages.

property cost
property inp_latency
property inp_qint
property inp_shifts
property kernel
property latency
property out_latency
property out_negs
property out_qint
property out_shifts
property reg_bits

The number of bits used for the register in the solution.

property shape
solutions: tuple[CombLogic, ...]

Alias for field number 0

class alkaid.types.Precision(keep_negative: bool, integers: int, fractional: int)

Bases: NamedTuple

Fixed-point precision in KIF form: sign flag, integer bits, fractional bits.

fractional: int

Alias for field number 2

integers: int

Alias for field number 1

keep_negative: bool

Alias for field number 0

class alkaid.types.QInterval(min: float, max: float, step: float)

Bases: NamedTuple

Quantized interval described by inclusive bounds and a power-of-two step.

max: float

Alias for field number 1

min: float

Alias for field number 0

step: float

Alias for field number 2

alkaid.types.minimal_kif(qi: QInterval) Precision

Minimal (keep_negative, integers, fractionals) precision for qi.

Module contents