IREE Python Compiler API - MLIR-based end-to-end compiler for Machine Learning models
—
High-level compilation functions that provide the primary interface for compiling MLIR code to IREE VM modules. These functions support various input formats, target backends, and compilation options for ML model deployment workflows.
Compiles MLIR assembly provided as a string or bytes object directly to VM bytecode.
def compile_str(input_str: Union[str, bytes], **kwargs) -> Optional[bytes]:
"""
Invokes the IREE compiler with an input string.
Parameters:
- input_str: MLIR assembly to parse/compile (str or bytes)
- **kwargs: Keyword arguments corresponding to CompilerOptions
Returns:
Optional[bytes]: Compiled binary content, or None if output_file was specified
"""Compiles MLIR assembly from a file to VM bytecode.
def compile_file(input_file: str, **kwargs) -> Optional[bytes]:
"""
Invokes the IREE compiler on an input file.
Parameters:
- input_file: File containing MLIR assembly to compile
- **kwargs: Keyword arguments corresponding to CompilerOptions
Returns:
Optional[bytes]: Compiled binary content, or None if output_file was specified
"""Queries available target backends for hardware-specific compilation.
def query_available_targets() -> List[str]:
"""
Returns a collection of target names that are registered.
Returns:
List[str]: Available target backend names
"""Configuration object for controlling compilation behavior and output format.
class CompilerOptions:
"""
Options to the compiler backend.
Attributes:
- output_file: Optional[str], save compiled binary to file instead of returning
- target_backends: Sequence[str], list of target backends to compile for
- input_type: Union[InputType, str], input legalization type (default: AUTO)
- output_format: Union[OutputFormat, str], output format (default: FLATBUFFER_BINARY)
- extra_args: Sequence[str], additional compiler arguments
- optimize: bool, apply default optimizations (default: True)
- output_mlir_debuginfo: bool, include debuginfo in MLIR (default: True)
- output_generic_mlir: bool, use generic MLIR formatting (default: False)
- extended_diagnostics: bool, output verbose diagnostics (default: False)
- strip_debug_ops: bool, strip debug operations (default: False)
- strip_source_map: bool, strip source map information (default: False)
- crash_reproducer_path: Optional[str], crash dump output path
- enable_tflite_bindings: bool, support TFLite runtime API (default: False)
- enable_benchmark: bool, generate instrumented binaries (default: False)
"""
def __init__(self, **kwargs): ...Enumeration of supported input format types for model compilation.
class InputType(Enum):
"""
Enumeration of allowable input types to the compiler.
"""
NONE = "none"
AUTO = "auto"
STABLEHLO = "stablehlo"
STABLEHLO_XLA = "stablehlo_xla"
TOSA = "tosa"
TM_TENSOR = "tm_tensor"
TORCH = "torch"
ONNX = "onnx"
@staticmethod
def parse(spec: Union[str, InputType]) -> InputType: ...Enumeration of supported output formats for compiled modules.
class OutputFormat(Enum):
"""
The output format of the compiler.
"""
FLATBUFFER_BINARY = "flatbuffer-binary"
FLATBUFFER_TEXT = "flatbuffer-text"
MLIR_TEXT = "mlir-text"
@staticmethod
def parse(spec: Union[str, OutputFormat]) -> OutputFormat: ...Exception raised when compiler tools encounter errors during execution.
class CompilerToolError(Exception):
"""
Exception raised when a compiler tool fails.
"""File management utility for preserving intermediate compilation artifacts during debugging.
class TempFileSaver:
"""
Utility for managing temporary files during compilation with optional retention.
"""
@staticmethod
def implicit() -> TempFileSaver: ...
def alloc_optional(self, name: str, export_as: Optional[str] = None) -> Optional[str]: ...Default backend and driver constants for testing and development workflows.
DEFAULT_TESTING_BACKENDS: List[str] = ["llvm-cpu"]
DEFAULT_TESTING_DRIVER: str = "local-task"Utilities for locating and invoking compiler tool binaries.
def find_tool(exe_name: str) -> str:
"""
Finds a tool by its executable name.
Parameters:
- exe_name: Name of the executable (extension-less)
Returns:
str: Absolute path to the tool
Raises:
ValueError: If the tool is not known or not found
"""
def get_tool_path() -> List[str]:
"""
Returns list of paths to search for tools.
Returns:
List[str]: List of tool search paths
"""
def invoke_immediate(command_line: List[str], *, input_file: Optional[bytes] = None, immediate_input=None):
"""
Invokes an immediate command.
Parameters:
- command_line: Command line arguments as list
- input_file: Optional input file path
- immediate_input: Optional immediate input data
Returns:
bytes: Command output
Raises:
CompilerToolError: If command fails
"""
def build_compile_command_line(input_file: str, tfs: TempFileSaver, options: CompilerOptions) -> List[str]:
"""
Builds a command line for invoking the compiler.
Parameters:
- input_file: Input file name
- tfs: TempFileSaver instance
- options: Compiler options
Returns:
List[str]: Command line arguments
"""import iree.compiler.tools
# Compile MLIR string for CPU target
mlir_code = """
func.func @add(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> {
%0 = arith.addf %arg0, %arg1 : tensor<4xf32>
return %0 : tensor<4xf32>
}
"""
binary = iree.compiler.tools.compile_str(
mlir_code,
target_backends=["llvm-cpu"],
input_type="auto"
)from iree.compiler.tools import CompilerOptions, compile_file
# Configure detailed compilation options
options = CompilerOptions(
target_backends=["llvm-cpu", "cuda"],
output_file="model.vmfb",
optimize=True,
output_mlir_debuginfo=True,
extra_args=["--iree-flow-enable-fusing", "--mlir-print-ir-after-all"]
)
# Compile from file
compile_file("input_model.mlir", **options.__dict__)# Query available hardware targets
available_targets = iree.compiler.tools.query_available_targets()
print("Available targets:", available_targets)
# Output: ['llvm-cpu', 'cuda', 'vulkan-spirv', 'rocm', ...]Install with Tessl CLI
npx tessl i tessl/pypi-iree-base-compiler