Tracing and native execution¶
NumPy-style tracing and explicit native execution for virtio-accel.
- class virtio_accel.Array¶
Bases:
objectTyped tensor value. Eager values use NumPy; symbolic values belong to one trace.
Use
to_numpy()for an explicit, independent host copy. No device allocation or acceleration is implied by the reference implementation.- property T¶
Transpose with the dimension order reversed.
- __init__(_shape, _dtype=dtype('float32'), _data=None, _graph=None, _id=-1)¶
- Parameters:
_shape (tuple)
_dtype (object)
_data (object)
_graph (object)
_id (int)
- Return type:
None
- astype(dtype)¶
Explicitly convert between supported dtype pairs.
- property dtype¶
NumPy dtype describing the tensor storage format.
- mean(axis=None, dtype=None, out=None, keepdims=False)¶
Reduce by averaging; see
virtio_accel.numpy.mean().
- property ndim¶
Number of dimensions.
- reshape(*shape)¶
Return a reshaped value; accepts a shape tuple or individual dimensions.
- property shape¶
Static dimensions as a tuple.
- property size¶
Total number of elements.
- sum(axis=None, dtype=None, out=None, keepdims=False)¶
Reduce by summation; see
virtio_accel.numpy.sum().
- to_numpy()¶
Return an independent NumPy copy of an eager value.
Raises TypeError for symbolic values; tracing never reads tensor values.
- class virtio_accel.CompiledFunction¶
Bases:
objectTrace a pure function, specializing on input shapes and dtypes.
Select device=”cpu”, “npu”, “gpu”, or a discovered Device for native execution, or backend=”reference” for the CPU oracle. Programs and buffers are cached with each specialization.
- __call__(*args, **kwargs)¶
Run using the explicitly selected device or CPU reference backend.
- __init__(function, *, backend=None, device=None, cache_size=16, resident_cache_size=None, timeout=30.0, strict=False)¶
- clear_cache()¶
Retrace on next use, including any changed captured constants.
- close()¶
Release every resident program and permanently close this function.
- lower(*args, **kwargs)¶
Trace or reuse a graph specialized to the input shapes and dtypes.
The Python function runs only when a specialization is first traced. Captured values are snapshotted; clear the cache to capture changes.
- class virtio_accel.Lowered¶
Bases:
objectA frozen graph. Export validates it with the Rust TOSA implementation.
- __init__(graph, signature, specs, multiple)¶
- compile(device, *, timeout=30.0, strict=False)¶
Compile for a native device; use the returned executable as a context manager.
- reference(*args, **kwargs)¶
Evaluate with NumPy on the CPU. This is an oracle, not acceleration.
- to_dict()¶
Return an independent diagnostic graph, not a stable serialized ABI.
- to_tosa()¶
Return validated static TOSA 1.0 bytes; requires the native extension.
- virtio_accel.jit(function=None, *, backend=None, device=None, cache_size=16, resident_cache_size=None, timeout=30.0, strict=False)¶
Decorate a pure function for static tracing and explicit execution.
- Parameters:
function – Function to wrap, or None when used as a decorator factory.
backend – Set to
"reference"for the NumPy CPU oracle. Mutually exclusive withdevice. There is no automatic CPU fallback.device – A discovered Device or selector such as
"gpu","npu","cpu"or"hybrid". Bare kinds select index zero.cache_size – Maximum cached shape/dtype specializations (default 16).
resident_cache_size – Maximum resident executables. By default, one for virtio endpoints and
cache_sizefor native endpoints. Eviction releases programs before compilation; traced graphs stay cached.timeout – Native-operation timeout in seconds, greater than zero and no more than 86400. A timeout does not cancel native work.
strict – Reject accelerator endpoints that permit CPU participation.
- Returns:
A CompiledFunction, or a decorator when function is omitted.
With neither device nor backend, only lowering/export is enabled. Functions must be pure and return one Array or a nonempty tuple of Arrays. Python control flow may depend on static shapes, but never on tensor values.
- class virtio_accel.Device¶
Bases:
objectImmutable endpoint discovered by
virtio_accel.devices().IDs are local to a discovery snapshot. Multiple providers can expose the same hardware. CPU participation is a placement allowance, not a claim about which processors executed a particular graph.
- __init__(id, kind, provider, name, cpu_participation, _identity=None, _discovery_provider=None)¶
- Parameters:
id (str)
kind (str)
provider (str)
name (str)
cpu_participation (bool)
_identity (tuple | None)
_discovery_provider (str | None)
- Return type:
None
- id: str¶
Snapshot-local selector, for example
gpu:0.
- kind: str¶
cpu,gpu,npuorhybrid.
- provider: str¶
coreml,vulkan,openvino,hexagon,xdnaorvirtio.
- name: str¶
Human-readable endpoint name.
- cpu_participation: bool¶
Whether CPU participation is permitted or cannot be excluded by the transport.
- class virtio_accel.Executable¶
Bases:
objectResident program and reusable native buffers; results are independent host arrays.
Close explicitly or use a context manager to observe release failures. A wait timeout closes this executable without cancelling or retrying native work.
- __call__(*args, **kwargs)¶
Execute with the compiled shapes and dtypes, returning independent host arrays.
Calls are serialized per executable. Results are an Array or a tuple matching the traced return structure. Use
to_numpy()for host copies.
- __init__(lowered, device, *, timeout=30.0, strict=False)¶
- close()¶
Release native programs and buffers; repeated calls have no effect.
Use a context manager or call explicitly to observe release errors.
- virtio_accel.devices(kind=None, *, provider=None)¶
Discover endpoints. Indices belong to this provider-filtered snapshot.
Filter by kind or provider, and pass a returned Device to jit/compile to select that endpoint. Multiple providers may expose the same physical accelerator. Virtio metadata is cached until its node changes; execution revalidates identity.
- virtio_accel.device_diagnostics()¶
Explain missing runtimes, failed probes and excluded native endpoints.