IR API

lattice_contract is now MLIR-first. Its public surface contains the annotation-backed lattice dialect schema plus a generated textual MLIR builder. It does not import MLX, Torch, native kernels, or sparse runtime objects.

For the conceptual model, read Sparse model IR.

Annotated dialect schema

The dialect schema is executable metadata: decorators register types, attributes, operations, operands, results, and operation attributes once. The builder consumes this schema directly, so exporter code can call the generated operation surface instead of carrying a parallel op-name registry.

The current dialect surface is intentionally small but semantic: sparse ABI construction/decomposition, symbolic weight binding, the sparse convolution family (forward, submanifold, target, transpose, and generative transpose), local/global sparse pooling, point/voxel conversion, dense feature projection, dense feature activation, dense feature normalization, sparse feature replacement, and coordinate-aligned sparse binary algebra. Backend execution routes such as TensorOps, CSR views, point-to-voxel cache maps, or rulebook layouts are not encoded in the dialect.

class lattice_contract.schema.TypeParameter(name, kind)[source]

Bases: object

One MLIR dialect type parameter.

Parameters:
name: str
kind: str
class lattice_contract.schema.TypeDef(name, mnemonic, parameters=(), summary='')[source]

Bases: object

Annotated MLIR type declaration.

Parameters:
name: str
mnemonic: str
parameters: tuple[TypeParameter, ...]
summary: str
class lattice_contract.schema.AttrParameter(name, kind)[source]

Bases: object

One MLIR dialect attribute parameter.

Parameters:
name: str
kind: str
class lattice_contract.schema.AttrDef(name, mnemonic, parameters=(), values=(), summary='')[source]

Bases: object

Annotated MLIR attribute declaration.

Parameters:
name: str
mnemonic: str
parameters: tuple[AttrParameter, ...]
values: tuple[str, ...]
summary: str
class lattice_contract.schema.OperandDef(name, type, kind='ssa', optional=False)[source]

Bases: object

One operation operand declaration.

Parameters:
name: str
type: str
kind: Literal['ssa', 'symbol']
optional: bool
class lattice_contract.schema.ResultDef(name, type)[source]

Bases: object

One operation result declaration.

Parameters:
name: str
type: str
class lattice_contract.schema.OpAttributeDef(name, kind, required=True)[source]

Bases: object

One operation attribute declaration.

Parameters:
  • name (str)

  • kind (Literal['coord', 'feature_layout', 'weight_layout', 'packing', 'activation', 'gelu_approx', 'join', 'binary_op', 'pool_mode', 'voxel_reduction', 'point_interpolation', 'i64_triple', 'f64_triple', 'i64', 'f32', 'str'])

  • required (bool)

name: str
kind: Literal['coord', 'feature_layout', 'weight_layout', 'packing', 'activation', 'gelu_approx', 'join', 'binary_op', 'pool_mode', 'voxel_reduction', 'point_interpolation', 'i64_triple', 'f64_triple', 'i64', 'f32', 'str']
required: bool
class lattice_contract.schema.OpDef(name, python_name, operands=(), results=(), attributes=(), assembly='functional', summary='')[source]

Bases: object

Annotated MLIR operation declaration.

Parameters:
name: str
python_name: str
operands: tuple[OperandDef, ...]
results: tuple[ResultDef, ...]
attributes: tuple[OpAttributeDef, ...]
assembly: str
summary: str
class lattice_contract.schema.DialectSchema(namespace, types=<factory>, attrs=<factory>, ops=<factory>, _ops_by_python_name=<factory>)[source]

Bases: object

Annotation-backed dialect schema.

The schema is intentionally executable: decorators register MLIR declarations once, and downstream builders/importers consume the registry instead of maintaining hand-written op-name maps.

Parameters:
namespace: str
types: dict[str, TypeDef]
attrs: dict[str, AttrDef]
ops: dict[str, OpDef]
type(name, mnemonic, *, parameters=(), summary='')[source]

Register a dialect type declaration.

Return type:

Callable[[TypeVar(DeclarationT, bound= Callable | type)], TypeVar(DeclarationT, bound= Callable | type)]

Parameters:
attr(name, mnemonic, *, parameters=(), values=(), summary='')[source]

Register a dialect attribute declaration.

Return type:

Callable[[TypeVar(DeclarationT, bound= Callable | type)], TypeVar(DeclarationT, bound= Callable | type)]

Parameters:
op(name, *, python_name=None, operands=(), results=(), attributes=(), assembly='functional', summary='')[source]

Register a dialect operation declaration.

Return type:

Callable[[TypeVar(DeclarationT, bound= Callable | type)], TypeVar(DeclarationT, bound= Callable | type)]

Parameters:
add_type(definition)[source]
Return type:

Self

Parameters:

definition (TypeDef)

add_attr(definition)[source]
Return type:

Self

Parameters:

definition (AttrDef)

add_op(definition)[source]
Return type:

Self

Parameters:

definition (OpDef)

op_by_python_name(name)[source]
Return type:

OpDef

Parameters:

name (str)

qualified_op_name(definition)[source]

Return the fully-qualified MLIR operation name.

Return type:

str

Parameters:

definition (str | OpDef)

resolve_qualified_op(name)[source]

Resolve a fully-qualified MLIR operation name.

Return type:

OpDef

Parameters:

name (str)

resolve_op(value)[source]

Resolve an op handle to its schema definition.

Decorated functions/classes carry __lattice_op__ metadata. This is the preferred path because it lets framework code bind to the API object instead of repeating dialect strings.

Return type:

OpDef

Parameters:

value (str | OpDef | Callable[[...], Any])

iter_ops()[source]
Return type:

Iterator[OpDef]

lattice_contract.schema.type_param(name, kind)[source]

Declare one dialect type parameter.

Return type:

TypeParameter

Parameters:
lattice_contract.schema.attr_param(name, kind)[source]

Declare one dialect attribute parameter.

Return type:

AttrParameter

Parameters:
lattice_contract.schema.operand(name, type, *, kind='ssa', optional=False)[source]

Declare one operation operand.

Return type:

OperandDef

Parameters:
lattice_contract.schema.result(name, type)[source]

Declare one operation result.

Return type:

ResultDef

Parameters:
lattice_contract.schema.op_attr(name, kind, *, required=True)[source]

Declare one operation attribute.

Return type:

OpAttributeDef

Parameters:
  • name (str)

  • kind (Literal['coord', 'feature_layout', 'weight_layout', 'packing', 'activation', 'gelu_approx', 'join', 'binary_op', 'pool_mode', 'voxel_reduction', 'point_interpolation', 'i64_triple', 'f64_triple', 'i64', 'f32', 'str'])

  • required (bool)

lattice_contract.schema.schema_digest(schema)[source]

Return a compact stable digest useful for drift tests.

Return type:

Mapping[str, tuple[str, ...]]

Parameters:

schema (DialectSchema)

lattice_contract.schema.canonical_schema(schema)[source]

Return the canonical public schema used for artifact compatibility.

Return type:

Mapping[str, Any]

Parameters:

schema (DialectSchema)

lattice_contract.schema.schema_fingerprint(schema)[source]

Return a deterministic SHA-256 fingerprint for a dialect schema.

Return type:

str

Parameters:

schema (DialectSchema)

class lattice_contract.dialect.SparseTensorType[source]

Bases: object

Annotated sparse tensor type declaration.

class lattice_contract.dialect.WeightType[source]

Bases: object

Annotated symbolic weight type declaration.

class lattice_contract.dialect.CoordAttr[source]

Bases: object

Annotated coordinate convention attribute.

class lattice_contract.dialect.FeatureLayoutAttr[source]

Bases: object

Annotated feature layout attribute.

class lattice_contract.dialect.WeightLayoutAttr[source]

Bases: object

Annotated weight layout attribute.

class lattice_contract.dialect.PackingAttr[source]

Bases: object

Annotated weight packing attribute.

class lattice_contract.dialect.ActivationAttr[source]

Bases: object

Annotated dense feature activation attribute.

class lattice_contract.dialect.GeluApproxAttr[source]

Bases: object

Annotated GELU approximation attribute.

class lattice_contract.dialect.JoinAttr[source]

Bases: object

Annotated sparse algebra join attribute.

class lattice_contract.dialect.BinaryOpAttr[source]

Bases: object

Annotated sparse binary operation attribute.

class lattice_contract.dialect.PoolModeAttr[source]

Bases: object

Annotated sparse pooling reduction mode attribute.

class lattice_contract.dialect.VoxelReductionAttr[source]

Bases: object

Annotated point-to-voxel feature aggregation reduction.

class lattice_contract.dialect.PointInterpolationAttr[source]

Bases: object

Annotated voxel-to-point interpolation mode.

lattice_contract.dialect.weight()[source]

Register lattice.weight.

Return type:

None

lattice_contract.dialect.sparse_make()[source]

Register lattice.sparse.make.

Return type:

None

lattice_contract.dialect.sparse_decompose()[source]

Register lattice.sparse.decompose.

Return type:

None

lattice_contract.dialect.sparse_with_features()[source]

Register lattice.sparse.with_features.

Return type:

None

lattice_contract.dialect.sparse_reindex()[source]

Register lattice.sparse.reindex.

Return type:

None

lattice_contract.dialect.conv3d()[source]

Register lattice.conv3d.

Return type:

None

lattice_contract.dialect.subm_conv3d()[source]

Register lattice.subm_conv3d.

Return type:

None

lattice_contract.dialect.normalized_subm_conv3d()[source]

Register lattice.normalized_subm_conv3d.

Return type:

None

lattice_contract.dialect.target_conv3d()[source]

Register lattice.target_conv3d.

Return type:

None

lattice_contract.dialect.conv_transpose3d()[source]

Register lattice.conv_transpose3d.

Return type:

None

lattice_contract.dialect.target_conv_transpose3d()[source]

Register lattice.target_conv_transpose3d.

Return type:

None

lattice_contract.dialect.normalized_conv_transpose3d()[source]

Register lattice.normalized_conv_transpose3d.

Return type:

None

lattice_contract.dialect.target_normalized_conv_transpose3d()[source]

Register lattice.target_normalized_conv_transpose3d.

Return type:

None

lattice_contract.dialect.generative_conv_transpose3d()[source]

Register lattice.generative_conv_transpose3d.

Return type:

None

lattice_contract.dialect.normalized_generative_conv_transpose3d()[source]

Register lattice.normalized_generative_conv_transpose3d.

Return type:

None

lattice_contract.dialect.pool3d()[source]

Register lattice.pool3d.

Return type:

None

lattice_contract.dialect.pool_transpose3d()[source]

Register lattice.pool_transpose3d.

Return type:

None

lattice_contract.dialect.trilinear_upsample3d()[source]

Register lattice.trilinear_upsample3d.

Return type:

None

lattice_contract.dialect.global_pool()[source]

Register lattice.global_pool.

Return type:

None

lattice_contract.dialect.voxelize()[source]

Register lattice.voxelize.

Return type:

None

lattice_contract.dialect.devoxelize()[source]

Register lattice.devoxelize.

Return type:

None

lattice_contract.dialect.linear()[source]

Register lattice.linear.

Return type:

None

lattice_contract.dialect.embedding_lookup()[source]

Register lattice.embedding_lookup.

Return type:

None

lattice_contract.dialect.elementwise()[source]

Register lattice.elementwise.

Return type:

None

lattice_contract.dialect.softmax()[source]

Register lattice.softmax.

Return type:

None

lattice_contract.dialect.activation()[source]

Register lattice.activation.

Return type:

None

lattice_contract.dialect.batch_norm()[source]

Register lattice.batch_norm.

Return type:

None

lattice_contract.dialect.layer_norm()[source]

Register lattice.layer_norm.

Return type:

None

lattice_contract.dialect.rms_norm()[source]

Register lattice.rms_norm.

Return type:

None

lattice_contract.dialect.sparse_binary()[source]

Register lattice.sparse.binary.

Return type:

None

lattice_contract.dialect.sparse_cat()[source]

Register lattice.sparse.cat.

Return type:

None

MLIR builder

The builder emits textual lattice MLIR from the annotated schema. It is intended for exporters and tests; semantic verification remains the job of lattice-opt and the MLIR dialect verifier.

Generated modules include the required artifact ABI metadata: lattice.ir_version = 2 and lattice.weight_file = "weights.safetensors". These attributes are part of the stable exchange contract, not optional documentation. The corresponding Python constants are exported as CURRENT_DIALECT_VERSION, ARTIFACT_GRAPH_FILE, and ARTIFACT_WEIGHT_FILE.

class lattice_contract.mlir.builder.TensorType(text)[source]

Bases: object

Raw MLIR tensor type used at graph ABI boundaries.

Parameters:

text (str)

text: str
mlir()[source]
Return type:

str

class lattice_contract.mlir.builder.SparseTensorType(dtype='f16', rank=3, coord='batch_x_y_z', feature='row_channel')[source]

Bases: object

Lattice sparse tensor type.

Parameters:
dtype: str
rank: int
coord: str
feature: str
mlir()[source]
Return type:

str

class lattice_contract.mlir.builder.WeightType(family, dtype='f16')[source]

Bases: object

Lattice symbolic weight type.

Parameters:
family: str
dtype: str
mlir()[source]
Return type:

str

class lattice_contract.mlir.builder.Packing(kind='dense', group_size=0, scale_dtype='', mode='')[source]

Bases: object

Lattice weight packing attribute.

Parameters:
kind: str
group_size: int
scale_dtype: str
mode: str
mlir()[source]
Return type:

str

class lattice_contract.mlir.builder.SSAValue(name, type)[source]

Bases: object

Named SSA value plus its MLIR type.

Parameters:
name: str
type: TensorType | SparseTensorType | WeightType
ref()[source]
Return type:

str

lattice_contract.mlir.builder.dense_packing()[source]

Return dense weight packing.

Return type:

Packing

lattice_contract.mlir.builder.quantized_packing(kind, *, group_size, scale_dtype='f16', mode='affine')[source]

Return affine int4/int8 weight packing.

Return type:

Packing

Parameters:
type lattice_contract.mlir.builder.EmitResult = SSAValue | tuple[SSAValue, ...]
type lattice_contract.mlir.builder.OpHandle = str | OpDef | Callable[[...], Any]
class lattice_contract.mlir.builder.EmitFn(*args, **kwargs)[source]

Bases: Protocol

Textual MLIR emitter callable with a stable function name.

class lattice_contract.mlir.builder.EmitRegistry(functions, default)[source]

Bases: object

Annotation-backed textual assembly emitter registry.

Parameters:
functions: dict[str, EmitFn]
default: EmitFn
register(op, fn)[source]

Register a specialized emitter for a dialect operation.

Return type:

EmitFn

Parameters:
emit(builder, op, kwargs)[source]

Emit op through a specialized or default emitter.

Return type:

SSAValue | tuple[SSAValue, ...]

Parameters:
lattice_contract.mlir.builder.mlir_emitter(*, op)[source]

Bind a textual assembly hook to a dialect declaration.

Return type:

Callable[[EmitFn], EmitFn]

Parameters:

op (OpHandle)

class lattice_contract.mlir.builder.MLIRModuleBuilder(name='forward')[source]

Bases: object

Annotation-driven textual MLIR module builder.

Operation methods are generated dynamically from lattice_contract.dialect.LATTICE_DIALECT. The builder is therefore a convenience layer over the annotated schema, not a second op registry.

Parameters:

name (str)

argument(name, type, *, role='tensor')[source]

Append a function argument and return its SSA value.

Return type:

SSAValue

Parameters:
return_(*values, names=None, roles=None)[source]

Set function return values.

Return type:

None

Parameters:
to_mlir()[source]

Render the module as textual MLIR.

Return type:

str

emit(definition, **kwargs)[source]

Emit an operation declared by the annotated schema.

Return type:

SSAValue | tuple[SSAValue, ...]

Parameters:
lattice_contract.mlir.builder.functional_emitter(builder, op, kwargs)[source]
Return type:

SSAValue

Parameters:
lattice_contract.mlir.builder.weight_emitter(builder, op, kwargs)[source]
Return type:

SSAValue

Parameters:
lattice_contract.mlir.builder.sparse_decompose_emitter(builder, op, kwargs)[source]
Return type:

tuple[SSAValue, ...]

Parameters: