trinity-vsa
High-performance Vector Symbolic Architecture (VSA) library with balanced ternary arithmetic.
Installation
pip install trinity-vsa
With PyTorch integration:
pip install trinity-vsa[torch]
Quick Start
from trinity_vsa import TritVector, bind, bundle, similarity
# Create random hypervectors
apple = TritVector.random(10000)
red = TritVector.random(10000)
fruit = TritVector.random(10000)
# Bind: create association "red apple"
red_apple = bind(apple, red)
# Bundle: combine concepts
fruits = bundle([apple, red_apple, fruit])
# Similarity: compare vectors
sim = similarity(red_apple, apple)
print(f"Similarity: {sim:.3f}")
# Unbind: retrieve associated concept
from trinity_vsa import unbind
recovered = unbind(red_apple, red)
recovery_sim = similarity(recovered, apple)
print(f"Recovery similarity: {recovery_sim:.3f}")
Features
- Balanced Ternary: Values in {-1, 0, +1}
- VSA Operations: bind, bundle, permute, similarity
- Multiple Storage: Dense, Packed (4x savings), Sparse
- NumPy Integration: Seamless array operations
- PyTorch/JAX: Optional deep learning integration
Storage Formats
from trinity_vsa import TritVector, PackedTritVec, SparseVec
# Dense: 1 byte per trit
v = TritVector.random(10000) # 10KB
# Packed: 2 bits per trit
packed = PackedTritVec.from_trit_vector(v) # 2.5KB
# Sparse: only non-zeros
sparse = SparseVec.from_trit_vector(v) # ~3KB for 33% density
VSA Theory
Binding (⊗): bind(a, b) = element-wise multiply
bind(a, a) = all +1
bind(a, bind(a, b)) = b
Bundling (+): bundle([a, b, c]) = majority vote
Result similar to all inputs
Permutation: permute(v, k) = circular shift by k
Used for sequence encoding
Examples
Associative Memory
from trinity_vsa import TritVector, bind, similarity
# Create item-attribute pairs
items = {
"apple": TritVector.random(10000),
"banana": TritVector.random(10000),
}
colors = {
"red": TritVector.random(10000),
"yellow": TritVector.random(10000),
}
# Store associations
memory = [
bind(items["apple"], colors["red"]),
bind(items["banana"], colors["yellow"]),
]
# Query: "What color is apple?"
query = bind(items["apple"], colors["red"])
for i, mem in enumerate(memory):
print(f"Memory {i}: {similarity(query, mem):.3f}")
Sequence Encoding
from trinity_vsa import TritVector, bind, permute, similarity
# Word vectors
words = {w: TritVector.random(10000) for w in ["the", "cat", "sat"]}
# Encode sequence with position
def encode_sequence(word_list):
result = words[word_list[0]]
for i, word in enumerate(word_list[1:], 1):
result = bind(result, permute(words[word], i))
return result
seq1 = encode_sequence(["the", "cat", "sat"])
seq2 = encode_sequence(["the", "sat", "cat"]) # Different order
print(f"Same order similarity: {similarity(seq1, seq1):.3f}")
print(f"Different order: {similarity(seq1, seq2):.3f}")
Benchmarks
| Operation | Dimension | Time |
|---|---|---|
| bind | 10,000 | 15 µs |
| bundle (5 vectors) | 10,000 | 45 µs |
| similarity | 10,000 | 12 µs |
| packed bind | 10,000 | 8 µs |
Why trinity-vsa?
| Feature | trit-vsa (Rust) | trinity-vsa |
|---|---|---|
| Language | Rust only | Python, Rust, C, Zig |
| NumPy integration | ❌ | ✅ |
| PyTorch integration | ❌ | ✅ |
| FPGA support | ❌ | ✅ |
| Knowledge Graph | ❌ | ✅ |
License
MIT License
References
- Kanerva, P. (2009). "Hyperdimensional Computing"
- Trinity Project
Metadata
Release files for trinity-vsa 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| trinity_vsa-0.1.0.tar.gz | 12.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| trinity_vsa-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.6 kB
Release files / trinity_vsa-0.1.0.tar.gz
| Download URL | trinity_vsa-0.1.0.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
07141d70d321cde43b1f794d445cab2851143e8c3bd24b99c8a499e56db37c5a
|
|
BLAKE2b-256 checksum How to use checksums |
7f57fa91bbb827f9017c83a986d0240baccc9c80c4954e7c724a65cdf670e3e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Release files / trinity_vsa-0.1.0-py3-none-any.whl
| Download URL | trinity_vsa-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b0b25c7f4bcb39d11f5c21382522d58984a75bb21c58ac97384dd8704ead9a2
|
|
BLAKE2b-256 checksum How to use checksums |
5e660b77b73340856ce710e93c2d26c114a6f9a493957c1dd957a7477718b5f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|