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: list[QInterval]

Quantization intervals of the output elements.

out_shifts: list[int]

Alias for field number 3

predict(data: NDArray | Sequence[NDArray], n_threads: int = 0, debug=False, dump=False, ignore_lookup_oob: bool = False) NDArray[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.

  • ignore_lookup_oob (bool) – If True, the function will ignore out-of-bounds accesses in lookup operations and return random values instead on those entries.

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() bytes

Return the raw bytecode consumed by the C++ ALIR interpreter.

static upgrade_dict(dump: dict) dict

Return a v4 ALIR JSON dictionary converted from an older dictionary.

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

Bases: NamedTuple

Internal state of the DA algorithm.

expr: list[NDArray[int8]]

Alias for field number 1

freq_stat: dict[Pair, int]

Alias for field number 3

kernel: NDArray[float32]

Alias for field number 4

ops: list[Op]

Alias for field number 2

shifts: tuple[NDArray[int8], NDArray[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(addr: tuple[int, ...], opcode: int, data: tuple[int, ...], qint: QInterval, latency: float, cost: float)

Bases: NamedTuple

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

Parameters:
  • addr (tuple[int, ...]) – Buffer dependencies used by this operation.

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

  • data (tuple[int, ...]) – Opcode-specific integer payload. For opcode -1 this stores the input index, because inputs are not buffer dependencies.

  • 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.

addr: tuple[int, ...]

Alias for field number 0

cost: float

Alias for field number 5

data: tuple[int, ...]

Alias for field number 2

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

Alias for field number 4

opcode: int

Alias for field number 1

qint: QInterval

Alias for field number 3

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.Precision(signed: 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

property qint: QInterval
signed: 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.

property kif
max: float

Alias for field number 1

min: float

Alias for field number 0

step: float

Alias for field number 2

Module contents