Skip to content

Add sparse tensor primitives with einsum contraction#48

Open
imlvts wants to merge 2 commits intotrueagi-io:mainfrom
imlvts:sparse-tensor-integration
Open

Add sparse tensor primitives with einsum contraction#48
imlvts wants to merge 2 commits intotrueagi-io:mainfrom
imlvts:sparse-tensor-integration

Conversation

@imlvts
Copy link
Copy Markdown
Collaborator

@imlvts imlvts commented Apr 4, 2026

Integrate N-dimensional sparse tensors into MORK expressions:

  • SparseTensorF64: PathMap-backed sparse tensor with BOB encoding, arbitrary rank, element-wise add/mul via lattice operations
  • einsum-dyn: copied into workspace as local crate, runtime Einstein summation with VM compiler supporting arbitrary N-D specs
  • Tensor sinks (tensor_collect, tensor_einsum, tensor_add, tensor_mul, tensor_free) using WriteResource::TensorStore through the standard resource infrastructure — no thread-locals
  • tensor_get/tensor_nnz pure functions via ExprSource.context pointer propagated through EvalScope
  • End-to-end test: load matrix data as S-expressions, collect into tensors via exec, einsum multiply, verify result

MeTTa usage:

(exec P1 (, (a $r $c $v)) (O (tensor_collect A $r $c $v)))
(exec P2 (, (b $r $c $v)) (O (tensor_collect B $r $c $v)))
(exec P3 (,) (O (tensor_einsum "ab,bc->ac" A B C)))

imlvts and others added 2 commits April 4, 2026 16:59
Integrate N-dimensional sparse tensors into MORK expressions:

- SparseTensorF64: PathMap-backed sparse tensor with BOB encoding,
  arbitrary rank, element-wise add/mul via lattice operations
- einsum-dyn: copied into workspace as local crate, runtime Einstein
  summation with VM compiler supporting arbitrary N-D specs
- Tensor sinks (tensor_collect, tensor_einsum, tensor_add, tensor_mul,
  tensor_free) using WriteResource::TensorStore through the standard
  resource infrastructure — no thread-locals
- tensor_get/tensor_nnz pure functions via ExprSource.context pointer
  propagated through EvalScope
- End-to-end test: load matrix data as S-expressions, collect into
  tensors via exec, einsum multiply, verify result

MeTTa usage:
  (exec P1 (, (a $r $c $v)) (O (tensor_collect A $r $c $v)))
  (exec P2 (,) (O (tensor_einsum "ab,bc->ac" A B C)))

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- tensor_get now parses indices from string symbols (not binary u32),
  matching how MeTTa S-expressions encode numbers
- End-to-end test exercises full pipeline: load data → tensor_collect →
  tensor_einsum → tensor_get to verify C[0,0]=19.0 and C[1,1]=50.0
- TensorEinsumSink strips quotes from spec string
- Command sinks (einsum, binop, free) parse args in new() for literal
  templates, or in sink() for variable-bound templates

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant