This document is relevant for: Trn2, Trn3

nki.isa.NkiInstruction#

class nki.isa.NkiInstruction(handle)[source]#

Handle or named reference to a NKI instruction.

Returned by nki.isa operations and nisa.instruction(). Use .depends_on(*producers, kind=...) to add runtime or scheduling-only dependencies.

Methods

depends_on

Run this instruction after each producer.

schedule_after

Move the self instruction immediately after the target instruction in program order.

schedule_before

Move the self instruction immediately before the target instruction in program order.

depends_on(*producers: NkiInstruction, kind: Optional[dependency] = None) → NkiInstruction[source]#

Run this instruction after each producer.

depends_on() creates a post-allocation scheduling dependency. Each producer must appear before this instruction in program order. Use schedule_after() or schedule_before() to create a pre-allocation scheduling dependency.

kind=dependency.runtime adds a hardware semaphore wait. The producer must be in the same control-flow block as this instruction, or in an enclosing block.

kind=dependency.schedule adds a compiler scheduling constraint without hardware synchronization.

Omitting kind currently adds a hardware semaphore wait. This form is deprecated.

Returns this instruction.

schedule_after(target: NkiInstruction, *, add_dependency: Optional[dependency] = None, carry_cone: bool = False) → NkiInstruction[source]#

Move the self instruction immediately after the target instruction in program order.

Unlike depends_on(), which creates a post-allocation scheduling dependency, schedule_after() creates a pre-allocation scheduling dependency. It changes program order before tensor allocation and can affect tensor live ranges and allocation. The target instruction stays in place.

Example:

# Before: op_a, op_b
op_a.schedule_after(op_b)
# After: op_b, op_a

By default, no additional post-allocation scheduling dependency is added. Set add_dependency to also make self depend on target. Set carry_cone=True to also move every instruction between self and target that self transitively depends on (when moving earlier) or that transitively depends on self (when moving later). Carried producers land immediately before self and carried consumers immediately after it, in their current relative order.

Both instructions must be in the same block. For example, an instruction inside a compiled loop or branch cannot move outside that loop or branch. An instruction also cannot move across a compiled loop or branch. The compiler checks the final instruction order after applying all pre-allocation scheduling changes. Compilation fails if the final order breaks a data dependency. Add schedule_before() or schedule_after() calls for any other instructions that must move.

Returns this instruction.

schedule_before(target: NkiInstruction, *, add_dependency: Optional[dependency] = None, carry_cone: bool = False) → NkiInstruction[source]#

Move the self instruction immediately before the target instruction in program order.

Unlike depends_on(), which creates a post-allocation scheduling dependency, schedule_before() creates a pre-allocation scheduling dependency. It changes program order before tensor allocation and can affect tensor live ranges and allocation. The target instruction stays in place.

Example:

# Before: op_a, op_b
op_b.schedule_before(op_a)
# After: op_b, op_a

By default, no additional post-allocation scheduling dependency is added. Set add_dependency to also make target depend on self. Set carry_cone=True to also move every instruction between self and target that self transitively depends on (when moving earlier) or that transitively depends on self (when moving later). Carried producers land immediately before self and carried consumers immediately after it, in their current relative order.

Both instructions must be in the same block. For example, an instruction inside a compiled loop or branch cannot move outside that loop or branch. An instruction also cannot move across a compiled loop or branch. The compiler checks the final instruction order after applying all pre-allocation scheduling changes. Compilation fails if the final order breaks a data dependency. Add schedule_before() or schedule_after() calls for any other instructions that must move.

Returns this instruction.

This document is relevant for: Trn2, Trn3