Skip to main content

High-performance Vector Symbolic Architecture with balanced ternary arithmetic

Project description

trinity-vsa

PyPI Python License: MIT

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

  1. Kanerva, P. (2009). "Hyperdimensional Computing"
  2. Trinity Project

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trinity_vsa-0.1.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trinity_vsa-0.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file trinity_vsa-0.1.0.tar.gz.

File metadata

  • Download URL: trinity_vsa-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for trinity_vsa-0.1.0.tar.gz
Algorithm Hash digest
SHA256 07141d70d321cde43b1f794d445cab2851143e8c3bd24b99c8a499e56db37c5a
MD5 1192e8ba0b77a7934c5a65c7e5a635c9
BLAKE2b-256 7f57fa91bbb827f9017c83a986d0240baccc9c80c4954e7c724a65cdf670e3e9

See more details on using hashes here.

File details

Details for the file trinity_vsa-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: trinity_vsa-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for trinity_vsa-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b0b25c7f4bcb39d11f5c21382522d58984a75bb21c58ac97384dd8704ead9a2
MD5 2e9b72c53dda0e47fd2f18b8133df521
BLAKE2b-256 5e660b77b73340856ce710e93c2d26c114a6f9a493957c1dd957a7477718b5f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page