fhelium.backend.lowering
Lower registered operations to exec functions.
An implementation's lower receives its Tensor operands as layout-carrying placeholder Tensors and writes ordinary PyTorch and native operator calls on them. ExecBuilder records each call as an operation of the MLIR exec dialect: operators that read placeholders become exec.call_op, view operators become static exec.view operations, allocation operators become exec.alloc/exec.zero, and operators whose Tensor inputs are all link-time constants run once while lowering and become exec.constant buffers. Kernel launches, Python calls, collectives and mutable resource state are recorded through the builder's methods.
Lowering runs once per operation and operand layout: link lowers the operations of a compiled module, and Eager caches one Fragment per operation, attributes and operand layouts. A fragment prints as an exec function (Fragment.to_mlir); fhelium.backend.schedule translates exec functions into Runner schedules.
ExecBuilder
class View source
ExecBuilder(operations: list[Operation] = field(default_factory=list), constants: list[torch.Tensor] = field(default_factory=list), fake_mode: FakeTensorMode = field(default_factory=lambda: FakeTensorMode(allow_non_fake_inputs=False)), launches: list[tuple[Any, tuple[object, ...]]] = field(default_factory=list), destinations: bool = False)Record exec operations for operations lowered at one insertion point.
constants holds the link-time constant Tensors the recorded operations read, and launches the recorded kernels with their warm signatures: dtypes for Tensor arguments, values otherwise.
Attributes
| Name | Type | Default/value |
|---|---|---|
operations | list[Operation] | field(default_factory=list) |
constants | list[torch.Tensor] | field(default_factory=list) |
fake_mode | FakeTensorMode | field(default_factory=lambda: FakeTensorMode(allow_non_fake_inputs=False)) |
launches | list[tuple[Any, tuple[object, ...]]] | field(default_factory=list) |
destinations | bool | False |
placeholder
method
def placeholder(value: Value, layout: torch.Tensor) -> torch.Tensor: ...Bind one SSA value to a placeholder with the static layout of layout.
fake
method
def fake(tensor: torch.Tensor) -> torch.Tensor: ...Return a placeholder Tensor with the layout of tensor.
like
method
def like(tensor: torch.Tensor, *, device: torch.device | None=None) -> torch.Tensor: ...Return an unbound placeholder with the layout of tensor.
host_scalar
method
def host_scalar(value: Value, sample: float) -> HostScalar: ...Bind one host scalar SSA value for use as an operator argument.
host_integer
method
def host_integer(value: Value, sample: int) -> HostInt: ...Bind one host integer SSA value for use as an operator argument.
value
method
def value(tensor: torch.Tensor | HostScalar | HostInt) -> Value: ...Return the SSA value of a placeholder, host scalar or constant.
is_placeholder
method
def is_placeholder(tensor: object) -> bool: ...tracing
method
def tracing(*, host: bool=True) -> Iterator[None]: ...Record PyTorch operator calls and kernel launches made inside this context.
host keeps host scalars passed to operators as runtime inputs (host_arguments); a lowering whose operands and attributes hold no host scalar leaves it off.
host_arguments
method
def host_arguments() -> Iterator[None]: ...Keep HostScalar and HostInt operator arguments made inside this context as runtime inputs of the recorded calls.
suspended
method
def suspended() -> Iterator[None]: ...Run link-time preparation on placeholders without recording it.
memo
method
def memo(key: Hashable, build: Callable[[], Any]) -> Any: ...Return the value built once for key while lowering this schedule.
alloc
method
def alloc(sizes: Sequence[int], *, dtype: torch.dtype, device: torch.device | str, strides: Sequence[int] | None=None, zero: bool=False) -> torch.Tensor: ...Allocate one schedule-owned buffer.
copy
method
def copy(target: torch.Tensor, source: torch.Tensor) -> None: ...Copy source into the storage of target.
state
method
def state(resource: object, field_name: str) -> torch.Tensor: ...Bind one mutable Tensor field of a live resource.
group_query
method
def group_query(kind: str, resource: object, sample: int) -> HostInt: ...Read the rank or size of a bound process group when the schedule runs; sample is its value at link time.
collective
method
def collective(kind: str, resource: object, inputs: Sequence[torch.Tensor], outputs: Sequence[torch.Tensor], options: Sequence[int]=()) -> None: ...Run a collective of a bound process group that reads inputs and writes outputs.
launch
method
def launch(kernel: object, arguments: Sequence[object], *, device: torch.device, grid: Sequence[int]=()) -> None: ...Launch a prepared kernel whose Tensor arguments are buffers.
python_call
method
def python_call(function: Callable[..., Sequence[torch.Tensor]], inputs: Sequence[object], *, results: Sequence[torch.Tensor] | None=None) -> tuple[torch.Tensor, ...]: ...Call a Python function with the operands as positional arguments from the schedule.
results gives placeholders with the layouts of the returned Tensors; when omitted, the function runs once on the placeholders to determine them.
record_call
method
def record_call(func, args, kwargs, output: object) -> object: ...Record a call of func whose output was computed outside tracing and return its placeholders.
Placeholder arguments read their buffers and other Tensors become link-time constants. Every Tensor of output becomes a fresh result of the recorded call, in flattening order; the returned structure mirrors output with placeholders in their place.
view_of
method
def view_of(source: torch.Tensor, layout: torch.Tensor) -> torch.Tensor: ...Record a view of placeholder source with the shape, strides and storage offset of layout.
Fragment
class View source
Fragment(arguments: tuple[Value, ...], operations: tuple[Operation, ...], outputs: tuple[Value, ...], results: tuple[torch.Tensor, ...], builder: ExecBuilder)Lowered exec operations of one operation call.
arguments are the function arguments in sample order, results the placeholders of the returned values and builder the recording builder, whose launches list the fragment's kernels. function runs the fragment on a Runner, or calls its single operator directly.
Attributes
| Name | Type | Default/value |
|---|---|---|
arguments | tuple[Value, ...] | |
operations | tuple[Operation, ...] | |
outputs | tuple[Value, ...] | |
results | tuple[torch.Tensor, ...] | |
builder | ExecBuilder |
to_mlir
method
def to_mlir(table: ObjectTable, name: str, *, private: bool=True) -> str: ...Print the fragment as the exec function name; its objects become entries of table.
function
property
function: Callable[..., tuple[object, ...]]HostInt
class View source
HostInt()Bases: int
Integer supplied when a lowered fragment runs; see HostScalar.
Attributes
| Name | Type | Default/value |
|---|---|---|
index | int |
HostScalar
class View source
HostScalar()Bases: float
Real scalar supplied when a lowered fragment runs.
Lowering passes it to operators like a Python float; the recorded call reads the fragment input at index.
Attributes
| Name | Type | Default/value |
|---|---|---|
index | int |
Layout
class View source
Layout(sizes: tuple[int, ...], strides: tuple[int, ...], dtype: torch.dtype, device: torch.device)Static Tensor layout: sizes, element strides, dtype and device.
Attributes
| Name | Type | Default/value |
|---|---|---|
sizes | tuple[int, ...] | |
strides | tuple[int, ...] | |
dtype | torch.dtype | |
device | torch.device |
of
method
def of(tensor: torch.Tensor) -> Layout: ...LoweredCall
class View source
LoweredCall(implementation, invocation)Call one implementation through fragments lowered per payload layout and resources.
Generated Backend Python and prepared combine callables use it for operation calls; the first call with a payload layout and resource selection lowers and compiles its fragment.
LoweringError
class View source
LoweringError()Bases: RuntimeError
A lowered operation reads Tensor data or writes a link-time constant.
Operation
class View source
Operation(name: str, operands: Sequence[Value], result_types: Sequence[str], attributes: Mapping[str, object], pure: bool)One recorded exec operation.
attributes maps names to MLIR attribute text, or to an ObjectAttribute resolved against the object table when the operation prints. pure operations are removed when their results are unused.
Value
class View source
Value(value_type: str, owner: Operation | None=None)SSA value of a recorded exec operation or function argument with its MLIR type.
active_builder
function View source
def active_builder() -> ExecBuilder | None: ...Return the builder recording the current lowering, if any.
buffer_type
function View source
def buffer_type(storage: str, tensor: torch.Tensor) -> str: ...Return the !exec.buffer type of a buffer with the static layout of tensor.
compile_fragment
function View source
def compile_fragment(lower: Callable[[ExecBuilder, tuple[object, ...]], Sequence[torch.Tensor]], samples: Sequence[object], *, destinations: bool=False) -> Fragment: ...Lower one call into a fragment.
samples lists the fragment inputs in order: Tensors fix the layouts of runtime Tensors, Python floats and integers become HostScalar and HostInt inputs. lower receives the builder and the corresponding placeholders and returns the results; a HostInt result is returned as a 0-dim int64 CPU Tensor. With destinations, operators that have a destination overload write their results, and their workspaces, into allocated buffers that a schedule's arena can place (ExecBuilder.destinations).
layout_key
function View source
def layout_key(tensor: torch.Tensor) -> tuple[object, ...]: ...Return the hashable layout of one runtime Tensor.
mlir_float
function View source
def mlir_float(value: float) -> str: ...Return value as an f64 MLIR attribute with its IEEE-754 bits.
mlir_string
function View source
def mlir_string(value: str) -> str: ...Return value as an MLIR string literal.
print_function
function View source
def print_function(name: str, arguments: Sequence[Value], operations: Sequence[Operation], outputs: Sequence[Value], table: ObjectTable, *, private: bool=False, read_in_place: Sequence[bool]=()) -> str: ...Print recorded operations as the MLIR func.func @name in generic operation form.
remove_dead_operations
function View source
def remove_dead_operations(operations: list[Operation], outputs: Sequence[Value]) -> list[Operation]: ...Return operations without the pure operations whose results neither another operation nor outputs use.
resolve_operator
function View source
def resolve_operator(schema: str): ...Return the dispatcher operator named namespace::name.overload.
torch_constant_value
function View source
def torch_constant_value(kind: str, value: str) -> object: ...Decode a PyTorch constant operator argument.