veloxquant_mlx.dsa package¶
Hand-implemented data structures backing VeloxQuant-MLX’s quantizer internals.
Groups the manually written (no heapq/stdlib shortcuts) structures used by
the codebook, outlier-tracking, and pipeline-validation code: AVLTree /
VoronoiTree for nearest-centroid search, BitPackBuffer for sub-byte
index packing, QuantizationGraph for DAG validation and topological
ordering of handler chains, MaxHeap / SortedChannelIndex for streaming
top-k channel tracking, and RingBuffer for fixed-capacity FIFO windows.
- class veloxquant_mlx.dsa.AVLNode(key, value)[source]
Bases:
objectSingle node of an AVL tree.
- Variables:
- Parameters:
- key: float
- value: int
- height: int
- class veloxquant_mlx.dsa.AVLTree[source]
Bases:
objectSelf-balancing AVL binary search tree.
Keys are float centroid values; values are integer centroid indices. All rotations are implemented manually to satisfy the balance invariant: |balance_factor(node)| <= 1 for every node.
- Parameters:
empty. (None. Tree starts)
- insert(key, value)[source]
Insert a (key, value) pair into the tree.
- search_nearest(query)[source]
Find the (key, value) pair with key closest to query.
- Parameters:
query (
float) – Target value.- Return type:
- Returns:
Tuple (nearest_key, nearest_value).
- Raises:
RuntimeError – If the tree is empty.
- class veloxquant_mlx.dsa.VoronoiTree[source]
Bases:
objectNearest-centroid search structure wrapping AVLTree.
Falls back to linear scan (
numpy.argmin) for small codebooks (n_centroids <= LINEAR_THRESHOLD).- Parameters:
build (None. Call)
- LINEAR_THRESHOLD: int = 16
- build(centroids)[source]
Populate the search structure from a 1-D centroid array.
- nearest(value)[source]
Return the index of the centroid nearest to value.
- Parameters:
value (
float) – Query float.- Return type:
- Returns:
Index of the nearest centroid.
- Raises:
RuntimeError – If build() has not been called.
- class veloxquant_mlx.dsa.BitPackBuffer(b)[source]
Bases:
objectCompact buffer that packs b-bit unsigned integers into uint8 bytes.
Supports b ∈ {1, 2, 3, 4}. All bit manipulation is implemented manually.
For non-power-of-2 bit widths (b=3), 8 values fit exactly in 3 bytes: each group of 8 values is packed into 3 bytes (24 bits / 3 bits = 8 values).
- Parameters:
b (
int) – Bits per element. Must be one of {1, 2, 3, 4}.- Raises:
ValueError – If b is not in {1, 2, 3, 4}.
- pack(indices)[source]
Pack an array of b-bit unsigned integers into bytes.
- Parameters:
indices (
ndarray) – Array of uint8 values in range [0, 2^b). Shape (n,).- Return type:
- Returns:
Packed uint8 array. Length is ceil(n * b / 8).
- Raises:
ValueError – If any value exceeds 2^b - 1.
- class veloxquant_mlx.dsa.QuantizationGraph[source]
Bases:
objectDirected Acyclic Graph of QuantizationHandler nodes.
Used to validate user-assembled pipelines (detect cycles) and to determine a correct execution order via topological sort (Kahn’s algorithm).
Nodes are QuantizationHandler instances identified by their
id(). Edges represent data-flow dependencies (src must execute before dst).- Parameters:
empty. (None. Graph starts)
- add_node(handler)[source]
Register a handler node.
- add_edge(src, dst)[source]
Add a directed edge src → dst.
Both nodes must have been added with add_node() first.
- has_cycle()[source]
Return True if the graph contains at least one directed cycle.
Uses Kahn’s algorithm: if not all nodes can be processed, a cycle exists.
- Return type:
- Returns:
True if a cycle is present, False if the graph is a DAG.
- topological_sort()[source]
Return nodes in topological order using Kahn’s algorithm.
- Kahn’s algorithm:
- Return type:
- Returns:
List of handler objects in valid execution order.
- Raises:
CyclicPipelineError – If the graph contains a cycle.
- critical_path()[source]
Return the longest path through the DAG (by number of nodes).
Uses the topological order computed by Kahn’s algorithm and a dynamic programming pass: dist[v] = max(dist[u] + 1) for all u→v.
- Return type:
- Returns:
List of handler objects forming the critical (longest) path.
- Raises:
CyclicPipelineError – If the graph contains a cycle.
- class veloxquant_mlx.dsa.MaxHeap[source]
Bases:
Generic[T]Binary max-heap backed by an internal array.
Nodes are (priority, value) pairs. The heap property is
priority[parent] >= priority[child]for all nodes.All heap operations are implemented manually without the
heapqmodule.valueis typically an int (a channel/token index), but SortedChannelIndex uses(channel_idx, version)tuples, hence the generic parameter rather than a fixedint.- Parameters:
empty. (None. The heap starts)
- push(priority, value)[source]
Insert a (priority, value) pair.
- pop()[source]
Remove and return the maximum (priority, value) pair.
- Return type:
- Returns:
Tuple of (priority, value).
- Raises:
IndexError – If the heap is empty.
- peek()[source]
Return but do not remove the maximum pair.
- Raises:
IndexError – If the heap is empty.
- Return type:
- class veloxquant_mlx.dsa.SortedChannelIndex[source]
Bases:
objectMax-heap–based structure for tracking top-k high-magnitude channels.
Used by OutlierDetector to maintain a running top-k across streaming key vectors during the prefill phase.
Each entry maps a channel index to a magnitude estimate. The structure supports O(log n) insert, O(k log n) top-k query, and O(log n) update.
- Parameters:
empty. (None. The index starts)
- insert(channel_idx, magnitude)[source]
Insert or update a channel’s magnitude.
If the channel already exists, the magnitude is updated (old entry is lazily invalidated). Re-inserting the same magnitude for a channel that already holds it is a no-op — this keeps the heap bounded at O(number of distinct channels observed) instead of growing by one entry per call.
- update(channel_idx, new_magnitude)[source]
Update the magnitude of an existing channel.
Internally uses lazy deletion — the old entry remains in the heap but is skipped when popped because the stored magnitude no longer matches
_valid.
- class veloxquant_mlx.dsa.RingBuffer(capacity)[source]
Bases:
Generic[T]Fixed-capacity circular buffer with O(1) append and index access.
When full, the oldest element is silently overwritten (FIFO eviction). Supports negative indexing (
buf[-1]is the most recently appended item).- Internal layout:
_data: list of length_capacity, pre-filled withNone._head: index in_dataof the oldest element._size: number of currently live elements.
- Parameters:
capacity (
int) – Maximum number of elements the buffer can hold. Must be >= 1.- Raises:
ValueError – If capacity < 1.
- append(item)[source]
Append an item to the buffer.
If the buffer is full the oldest item is evicted and returned.
- to_list()[source]
Return all elements as a plain list (oldest first).
- to_stacked()[source]
Stack all stored MLX arrays along axis 0.
Only valid when
T = mx.array. Usesmx.stackfor efficiency.- Return type:
array- Returns:
Single MLX array of shape (size, *element_shape).
- Raises:
RuntimeError – If the buffer is empty.