Relations¶
Relation objects connect coordinate semantics to backend execution. Start with Coordinates and relations for the conceptual model, then use this page for exact class members and builders. Convolution and pooling route details are documented in Convolution routes and Pooling routes.
Stable application code should normally depend on KernelSpec,
KernelRelation, NeighborRelation, and the functional relation builders.
Low-level CSR views, implicit-GEMM views, sorted implicit-GEMM views, and
relation contracts are exposed for inspection and debugging, but their exact
layout is provisional backend metadata.
Relation specifications¶
- class mlx_lattice.core.relations.specs.KernelSpec(size=3, stride=1, padding=0, dilation=1)[source]¶
Bases:
objectNormalized 3D kernel geometry for sparse relation builders.
KernelSpecaccepts integers or three-item sequences for size, stride, padding, and dilation and stores all values as triples. The object is hashable, so coordinate managers can use it directly as part of relation cache keys.- Parameters:
Relation views¶
The view classes below are advanced/provisional. They are useful when diagnosing relation construction or backend route selection; they should not be used as a long-term application storage format.
- class mlx_lattice.core.relations.views.RelationEdges(in_rows, out_rows, kernel_ids)[source]¶
Bases:
objectDiagnostic edge arrays for a logical sparse kernel relation.
Each edge stores
(in_row, out_row, kernel_id)at the same index in the three arrays. Edge arrays are useful for debugging and for generic sparse traversal routes; CSR views provide grouped execution order.- Parameters:
in_rows (array)
out_rows (array)
kernel_ids (array)
- in_rows: array¶
- out_rows: array¶
- kernel_ids: array¶
- class mlx_lattice.core.relations.views.RelationCSRView(row_offsets, edge_ids=None)[source]¶
Bases:
objectCSR execution view over relation edge arrays.
row_offsetshas shape(rows + 1,)andint32dtype. Optionaledge_idsremaps CSR order back to the canonical edge arrays.- Parameters:
row_offsets (array)
edge_ids (array | None)
- row_offsets: array¶
- mlx_lattice.core.relations.views.RelationView¶
alias of
RelationCSRView
- class mlx_lattice.core.relations.views.RelationImplicitGemmView(out_in_map, row_masks)[source]¶
Bases:
objectDense output-row by kernel-offset map for implicit-GEMM execution.
out_in_map[o, k]stores the input row that contributes to output rowoat kernel offsetk. Missing contributors are represented by the native builder’s sentinel value.row_masksstores bit masks describing populated kernel offsets for each output row.- Parameters:
out_in_map (array)
row_masks (array)
- out_in_map: array¶
- row_masks: array¶
- class mlx_lattice.core.relations.views.RelationSortedImplicitGemmView(sorted_out_in_map, sorted_kv_out_in_map, reorder_rows, tile_masks)[source]¶
Bases:
objectTile-sorted implicit-GEMM execution view for TensorOps kernels.
- Parameters:
sorted_out_in_map (array)
sorted_kv_out_in_map (array)
reorder_rows (array)
tile_masks (array)
- sorted_out_in_map: array¶
- sorted_kv_out_in_map: array¶
- reorder_rows: array¶
- tile_masks: array¶
- class mlx_lattice.core.relations.views.SparseRelationContract(counts, kernel_offsets, out_coords=None, n_in_capacity=None, n_out_capacity=None, n_kernels=None, source_coords=None, source_active_rows=None, target_coords=None, target_active_rows=None, stride=(1, 1, 1), padding=(0, 0, 0), kind='forward')[source]¶
Bases:
objectLogical sparse relation contract shared by all execution views.
The contract records counts, capacities, optional source/target coordinate buffers, kernel offsets, stride, padding, and relation kind. Backend routes use this metadata to validate specialized kernels before consuming CSR or implicit-GEMM views.
- Parameters:
counts (array)
out_coords (array | None)
n_in_capacity (int | None)
n_out_capacity (int | None)
n_kernels (int | None)
source_coords (array | None)
source_active_rows (array | None)
target_coords (array | None)
target_active_rows (array | None)
kind (Literal['forward', 'target', 'submanifold', 'transposed', 'generative'])
- counts: array¶
- property edge_count: array¶
- property out_count: array¶
- class mlx_lattice.core.relations.views.NeighborEdges(query_rows, source_rows, neighbor_ids)[source]¶
Bases:
objectSemantic neighbor edge arrays for query/source relations.
Each edge stores the query row, source row, and neighbor rank or identifier for geometric neighbor queries such as kNN and radius search.
- Parameters:
query_rows (array)
source_rows (array)
neighbor_ids (array)
- query_rows: array¶
- source_rows: array¶
- neighbor_ids: array¶
- class mlx_lattice.core.relations.views.KernelRelation(in_rows, out_rows, kernel_ids, *, row_offsets=None, counts=None, in_row_offsets=None, in_edge_ids=None, kernel_row_offsets=None, kernel_edge_ids=None, kernel_offsets=(), out_coords=None, n_in_capacity=None, n_out_capacity=None, n_kernels=None, source_coords=None, source_active_rows=None, target_coords=None, target_active_rows=None, stride=(1, 1, 1), padding=(0, 0, 0), kind='forward', implicit_gemm=None, sorted_implicit_gemm=None)[source]¶
Bases:
objectSparse kernel relation with multiple execution-oriented views.
A relation stores semantic edges
(in_row, out_row, kernel_id)plus CSR views grouped by output row, input row, and kernel id. Forward convolution, pooling, and backend-specific fast paths consume these views.- Parameters:
in_rows (mx.array)
out_rows (mx.array)
kernel_ids (mx.array)
row_offsets (mx.array | None)
counts (mx.array | None)
in_row_offsets (mx.array | None)
in_edge_ids (mx.array | None)
kernel_row_offsets (mx.array | None)
kernel_edge_ids (mx.array | None)
kernel_offsets (tuple[Triple, ...])
out_coords (mx.array | None)
n_in_capacity (int | None)
n_out_capacity (int | None)
n_kernels (int | None)
source_coords (mx.array | None)
source_active_rows (mx.array | None)
target_coords (mx.array | None)
target_active_rows (mx.array | None)
stride (Triple)
padding (Triple)
kind (RelationKind)
implicit_gemm (RelationImplicitGemmView | None)
sorted_implicit_gemm (RelationSortedImplicitGemmView | None)
- contract: SparseRelationContract¶
- edges: RelationEdges¶
- output_csr: RelationCSRView¶
- input_csr: RelationCSRView¶
- kernel_csr: RelationCSRView¶
- implicit_gemm: RelationImplicitGemmView | None¶
- sorted_implicit_gemm: RelationSortedImplicitGemmView | None¶
- property counts: array¶
Native counts array
[edge_count, out_count].
- property edge_count: array¶
- property out_count: array¶
- property row_offsets: array¶
- property in_row_offsets: array¶
- property in_edge_ids: array¶
- property kernel_row_offsets: array¶
- property kernel_edge_ids: array¶
- property out_view: RelationCSRView¶
- property in_view: RelationCSRView¶
- property kernel_view: RelationCSRView¶
- require_implicit_gemm()[source]¶
Return or lazily build the implicit-GEMM execution view.
- Return type:
- class mlx_lattice.core.relations.views.NeighborRelation(query_rows, source_rows, neighbor_ids, distances, *, row_offsets=None, counts=None, n_query_capacity=None, n_source_capacity=None, max_neighbors=None)[source]¶
Bases:
objectSparse query/source neighbor relation with row-offset metadata.
Neighbor relations are not convolution kernel relations. They describe geometric connectivity between query rows and source rows, carry distances in edge order, and expose row offsets grouped by query row.
- Parameters:
- edges: NeighborEdges¶
- distances: array¶
- row_offsets: array¶
- counts: array¶
- property edge_count: array¶
- property query_count: array¶
Relation builders¶
- type mlx_lattice.core.coords.builders.NativeKernelRelation = tuple[array, array, array, array, array, array, array, array, array, array]¶
- type mlx_lattice.core.coords.builders.NativeNeighborRelation = tuple[array, array, array, array, array, array]¶
- mlx_lattice.core.coords.builders.build_relation_implicit_gemm_view(relation)[source]¶
Build the dense output-to-input map used by implicit-GEMM kernels.
- Return type:
- Parameters:
relation (KernelRelation)
- mlx_lattice.core.coords.builders.build_target_kernel_relation(coords, target_coords, *, active_rows=None, target_active_rows=None, kernel_size=3, stride=1, padding=0, dilation=1)[source]¶
Build a sparse kernel relation from source coords to target coords.
- mlx_lattice.core.coords.builders.kernel_offsets(kernel_size, dilation=1)[source]¶
Enumerate spatial offsets for a dense 3D kernel footprint.
- mlx_lattice.core.coords.builders.build_kernel_relation(coords, active_rows=None, kernel_size=3, stride=1, padding=0, dilation=1)[source]¶
Build a forward sparse convolution/pooling relation.
- mlx_lattice.core.coords.builders.build_submanifold_kernel_relation(coords, active_rows=None, kernel_size=3, dilation=1)[source]¶
Build a submanifold relation whose output support is
coords.
- mlx_lattice.core.coords.builders.build_generative_relation(coords, active_rows=None, kernel_size=2, stride=2)[source]¶
Build a generative transpose-convolution relation.
- mlx_lattice.core.coords.builders.build_transposed_kernel_relation(coords, active_rows=None, kernel_size=2, stride=2, padding=0, dilation=1)[source]¶
Build a sparse transpose-convolution relation.
- mlx_lattice.core.coords.builders.build_knn_relation(source_coords, query_coords=None, *, source_active_rows=None, query_active_rows=None, k)[source]¶
Build a k-nearest-neighbor relation between source and query coords.
- Return type:
- Parameters:
source_coords (array)
query_coords (array | None)
source_active_rows (array | None)
query_active_rows (array | None)
k (int)