Skip to content

HCQ Compatible Runtime¤

Overview¤

The main aspect of HCQ-compatible runtimes is how they interact with devices. In HCQ, all interactions with devices occur in a hardware-friendly manner using command queues. This approach allows commands to be issued directly to devices, bypassing runtime overhead such as HIP or CUDA. Additionally, by using the HCQ API, these runtimes can benefit from various optimizations and features, including HCQGraph and built-in profiling capabilities.

Command Queues¤

To interact with devices, there are 2 types of queues: HWComputeQueue and HWCopyQueue. Commands which are defined in a base HWCommandQueue class should be supported by both queues. These methods are timestamp and synchronization methods like signal and wait.

For example, the following Python code enqueues a wait, execute, and signal command on the HCQ-compatible device:

HWComputeQueue().wait(signal_to_wait, value_to_wait) \
                .exec(program, args_state, global_dims, local_dims) \
                .signal(signal_to_fire, value_to_fire) \
                .submit(your_device)

Each runtime should implement the required functions that are defined in the HWCommandQueue, HWComputeQueue, and HWCopyQueue classes.

HWCommandQueue ¤

HWCommandQueue()

A base class for hardware command queues in the HCQ (Hardware Command Queue) API. Both compute and copy queues should have the following commands implemented.

signal ¤

signal(signal: HCQSignal, value: int)

Enqueues a signal command which sets the signal to the given value, ensuring all previous operations are completed.

Parameters:

  • signal (HCQSignal) –

    The signal to set

  • value (int) –

    The value to set the signal to

wait ¤

wait(signal: HCQSignal, value: int)

Enqueues a wait command which halts execution until the signal is greater than or equal to a specific value.

Parameters:

  • signal (HCQSignal) –

    The signal to wait on

  • value (int) –

    The value to wait for

timestamp ¤

timestamp(signal: HCQSignal)

Enqueues a timestamp command which records the current time in a signal after all previously enqueued commands are completed.

Parameters:

  • signal (HCQSignal) –

    The signal to store the timestamp

update_signal ¤

update_signal(
    cmd_idx: int,
    signal: Optional[Any] = None,
    value: Optional[int] = None,
)

Updates a previously queued signal command.

Parameters:

  • cmd_idx (int) –

    Index of the signal command to update

  • signal (Optional[Any], default: None ) –

    New signal to set (if None, keeps the original)

  • value (Optional[int], default: None ) –

    New value to set (if None, keeps the original)

update_wait ¤

update_wait(
    cmd_idx: int,
    signal: Optional[Any] = None,
    value: Optional[int] = None,
)

Updates a previously queued wait command.

Parameters:

  • cmd_idx (int) –

    Index of the wait command to update

  • signal (Optional[Any], default: None ) –

    New signal to wait on (if None, keeps the original)

  • value (Optional[int], default: None ) –

    New value to wait for (if None, keeps the original)

bind ¤

bind(device: HCQCompiled)

Associates the queue with a specific device for optimized execution.

This optional method allows backend implementations to tailor the queue for efficient use on the given device. When implemented, it can eliminate the need to copy queues into the device, thereby enhancing performance.

Parameters:

  • device (HCQCompiled) –

    The target device for queue optimization.

Note

Implementing this method is optional but recommended for performance gains.

submit ¤

submit(device: HCQCompiled)

Submits the command queue to a specific device for execution.

Parameters:

  • device (HCQCompiled) –

    The device to submit the queue to

HWComputeQueue ¤

HWComputeQueue()

Bases: HWCommandQueue

memory_barrier ¤

memory_barrier()

Enqueues a memory barrier command to ensure memory coherence between agents.

exec ¤

exec(
    prg: HCQProgram,
    args_state: HCQArgsState,
    global_size: Tuple[int, int, int],
    local_size: Tuple[int, int, int],
)

Enqueues an execution command for a kernel program.

Parameters:

update_exec ¤

update_exec(
    cmd_idx: int,
    global_size: Optional[Tuple[int, int, int]] = None,
    local_size: Optional[Tuple[int, int, int]] = None,
)

Updates a previously queued execution command.

Parameters:

  • cmd_idx (int) –

    Index of the execution command to update

  • global_size (Optional[Tuple[int, int, int]], default: None ) –

    New global work size (if None, keeps the original)

  • local_size (Optional[Tuple[int, int, int]], default: None ) –

    New local work size (if None, keeps the original)

HWCopyQueue ¤

HWCopyQueue()

Bases: HWCommandQueue

copy ¤

copy(dest: HCQBuffer, src: HCQBuffer, copy_size: int)

Enqueues a copy command to transfer data.

Parameters:

  • dest (HCQBuffer) –

    The destination of the copy

  • src (HCQBuffer) –

    The source of the copy

  • copy_size (int) –

    The size of data to copy

update_copy ¤

update_copy(
    cmd_idx: int,
    dest: Optional[HCQBuffer] = None,
    src: Optional[HCQBuffer] = None,
)

Updates a previously queued copy command.

Parameters:

  • cmd_idx (int) –

    Index of the copy command to update

  • dest (Optional[HCQBuffer], default: None ) –

    New destination of the copy (if None, keeps the original)

  • src (Optional[HCQBuffer], default: None ) –

    New source of the copy (if None, keeps the original)

Implementing custom commands¤

To implement custom commands in the queue, use the @hcq_command decorator for your command implementations.

hcq_command ¤

hcq_command(func)

Decorator for HWCommandQueue commands. Enables command indexing and stores metadata for command updates.

For example
  @hcq_command
  def command_method(self, ...): ...

HCQ Compatible Device¤

The HCQCompiled class defines the API for HCQ-compatible devices. This class serves as an abstract base class that device-specific implementations should inherit from and implement.

HCQCompiled ¤

HCQCompiled(
    device: str,
    allocator: Allocator,
    renderer: Renderer,
    compiler: Compiler,
    runtime,
    signal_t: Type[HCQSignal],
    comp_queue_t: Type[HWComputeQueue],
    copy_queue_t: Optional[Type[HWCopyQueue]],
    timeline_signals: Tuple[HCQSignal, HCQSignal],
)

Bases: Compiled

A base class for devices compatible with the HCQ (Hardware Command Queue) API.

Signals¤

Signals are device-dependent structures used for synchronization and timing in HCQ-compatible devices. They should be designed to record both a value and a timestamp within the same signal. HCQ-compatible backend implementations should use HCQSignal as a base class.

HCQSignal ¤

HCQSignal(value: int = 0)

value property writable ¤

value: int

timestamp property ¤

timestamp: Decimal

Get the timestamp field of the signal.

This property provides read-only access to the signal's timestamp.

Returns:

  • Decimal

    The timestamp in microseconds.

wait ¤

wait(value: int, timeout: int = 10000)

Waits the signal is greater than or equal to a specific value.

Parameters:

  • value (int) –

    The value to wait for.

  • timeout (int, default: 10000 ) –

    Maximum time to wait in milliseconds. Defaults to 10s.

The following Python code demonstrates the usage of signals:

signal = your_device.signal_t()

HWComputeQueue().timestamp(signal) \
                .signal(signal, value_to_fire) \
                .submit(your_device)

signal.wait(value_to_fire)
signaled_value = signal.value # should be the same as `value_to_fire`
timestamp = signal.timestamp
Synchronization signals¤

Each HCQ-compatible device must allocate two signals for global synchronization purposes. These signals are passed to the HCQCompiled base class during initialization: an active timeline signal self.timeline_signal and a shadow timeline signal self._shadow_timeline_signal which helps to handle signal value overflow issues. You can find more about synchronization in the synchronization section

HCQ Compatible Allocator¤

The HCQAllocator base class simplifies allocator logic by leveraging command queues abstractions. This class efficiently handles copy and transfer operations, leaving only the alloc and free functions to be implemented by individual backends.

HCQAllocator ¤

HCQAllocator(
    device: HCQCompiled,
    batch_size: int = 2 << 20,
    batch_cnt: int = 32,
)

Bases: LRUAllocator

A base allocator class compatible with the HCQ (Hardware Command Queue) API.

This class implements basic copy operations following the HCQ API, utilizing both HWComputeQueue and HWCopyQueue.

_alloc ¤

_alloc(size: int, options: BufferOptions) -> HCQBuffer

HCQ Allocator Result Protocol¤

Backends must adhere to the HCQBuffer protocol when returning allocation results.

HCQBuffer ¤

Bases: Protocol

size instance-attribute ¤

size: int

va_addr instance-attribute ¤

va_addr: int

HCQ Compatible Program¤

HCQProgram is a base class for defining programs compatible with HCQ-enabled devices. It provides a flexible framework for handling different argument layouts (see HCQArgsState).

HCQProgram ¤

HCQProgram(
    args_state_t: Type[HCQArgsState],
    device: HCQCompiled,
    name: str,
    kernargs_alloc_size: int,
)

__call__ ¤

__call__(
    *bufs: HCQBuffer,
    global_size: Tuple[int, int, int] = (1, 1, 1),
    local_size: Tuple[int, int, int] = (1, 1, 1),
    vals: Tuple[int, ...] = (),
    wait: bool = False
) -> Optional[float]

Enqueues the program for execution with the given arguments and dimensions.

Parameters:

  • bufs (HCQBuffer, default: () ) –

    Buffer arguments to execute the kernel with.

  • global_size (Tuple[int, int, int], default: (1, 1, 1) ) –

    Specifies the global work size for kernel execution (equivalent to CUDA's grid size).

  • local_size (Tuple[int, int, int], default: (1, 1, 1) ) –

    Specifies the local work size for kernel execution (equivalent to CUDA's block size).

  • vals (Tuple[int, ...], default: () ) –

    Value arguments to execute the kernel with.

  • wait (bool, default: False ) –

    If True, waits for the kernel to complete execution.

Returns:

  • Optional[float]

    Execution time of the kernel if 'wait' is True, otherwise None.

fill_kernargs ¤

fill_kernargs(
    bufs: Tuple[HCQBuffer, ...],
    vals: Tuple[int, ...] = (),
    kernargs_ptr: Optional[int] = None,
) -> HCQArgsState

Fills arguments for the kernel, optionally allocating space from the device if kernargs_ptr is not provided. Args: bufs: Buffers to be written to kernel arguments. vals: Values to be written to kernel arguments. kernargs_ptr: Optional pointer to pre-allocated kernel arguments memory. Returns: Arguments state with the given buffers and values set for the program.

Arguments State¤

HCQArgsState is a base class for managing the argument state for HCQ programs. Backend implementations should create a subclass of HCQArgsState to manage arguments for the given program.

HCQArgsState ¤

HCQArgsState(
    ptr: int,
    prg: HCQProgram,
    bufs: Tuple[HCQBuffer, ...],
    vals: Tuple[int, ...] = (),
)

update_buffer ¤

update_buffer(index: int, buf: HCQBuffer)

update_var ¤

update_var(index: int, val: int)

Lifetime: The HCQArgsState is passed to HWComputeQueue.exec and is guaranteed not to be freed until HWComputeQueue.submit for the same queue is called.

Synchronization¤

HCQ-compatible devices use a global timeline signal for synchronizing all operations. This mechanism ensures proper ordering and completion of tasks across the device. By convention, self.timeline_value points to the next value to signal. So, to wait for all previous operations on the device to complete, wait for self.timeline_value - 1 value. The following Python code demonstrates the typical usage of signals to synchronize execution to other operations on the device:

HWComputeQueue().wait(your_device.timeline_signal, your_device.timeline_value - 1) \
                .exec(...)
                .signal(your_device.timeline_signal, your_device.timeline_value) \
                .submit(your_device)
your_device.timeline_value += 1

# Optionally wait for execution
your_device.timeline_signal.wait(your_device.timeline_value - 1)

HCQGraph¤

HCQGraph is a core feature that implements GraphRunner for HCQ-compatible devices. HCQGraph builds a static HWComputeQueue and HWCopyQueue for all operations per device. To optimize enqueue time, only the necessary parts of the queues are updated for each run using the update APIs of the queues, avoiding a complete rebuild. Optionally, queues can implement a bind API, which allows further optimization by eliminating the need to copy the queues into the device ring.