Skip to content

VSAMemory

Dictionary-style symbol table for managing named hypervectors.

VSAMemory provides a convenient interface for creating, storing, and accessing hypervectors associated with symbolic names. It automatically handles vector sampling and wrapping using the model's configuration.

vsax.core.memory.VSAMemory

Symbol table for storing and managing named basis vectors.

VSAMemory provides a dictionary-style interface for creating, storing, and retrieving named hypervectors. Each symbol is associated with a randomly sampled hypervector from the model's sampling distribution.

Parameters:

Name Type Description Default
model VSAModel

VSAModel instance defining the representation and operations.

required
key Optional[Array]

Optional JAX PRNG key for reproducible sampling. If None, uses a default key.

None
Example

from vsax import create_fhrr_model, VSAMemory model = create_fhrr_model(dim=512) memory = VSAMemory(model) memory.add("dog") memory.add_many(["cat", "bird"]) dog = memory["dog"] assert "cat" in memory print(memory.keys()) ['dog', 'cat', 'bird']

Source code in vsax/core/memory.py
class VSAMemory:
    """Symbol table for storing and managing named basis vectors.

    VSAMemory provides a dictionary-style interface for creating, storing, and
    retrieving named hypervectors. Each symbol is associated with a randomly
    sampled hypervector from the model's sampling distribution.

    Args:
        model: VSAModel instance defining the representation and operations.
        key: Optional JAX PRNG key for reproducible sampling. If None, uses a
            default key.

    Example:
        >>> from vsax import create_fhrr_model, VSAMemory
        >>> model = create_fhrr_model(dim=512)
        >>> memory = VSAMemory(model)
        >>> memory.add("dog")
        >>> memory.add_many(["cat", "bird"])
        >>> dog = memory["dog"]
        >>> assert "cat" in memory
        >>> print(memory.keys())
        ['dog', 'cat', 'bird']
    """

    def __init__(self, model: VSAModel, key: Optional[jax.Array] = None) -> None:
        """Initialize VSAMemory with a model.

        Args:
            model: VSAModel instance defining the VSA algebra.
            key: Optional JAX PRNG key for reproducible sampling.
        """
        self._model = model
        self._symbols: dict[str, AbstractHypervector] = {}
        self._key = key if key is not None else jax.random.PRNGKey(0)
        self._counter = 0

    @property
    def model(self) -> VSAModel:
        """Get the underlying VSAModel."""
        return self._model

    def add(self, name: str) -> AbstractHypervector:
        """Add a new symbol to memory with a randomly sampled hypervector.

        If the symbol already exists, returns the existing hypervector without
        resampling.

        Args:
            name: Name of the symbol to add.

        Returns:
            The hypervector associated with the symbol.

        Example:
            >>> memory = VSAMemory(model)
            >>> dog = memory.add("dog")
            >>> assert "dog" in memory
        """
        if name in self._symbols:
            return self._symbols[name]

        # Split key for this sample
        self._key, subkey = jax.random.split(self._key)

        # Sample a new vector
        vec = self._model.sampler(self._model.dim, 1, subkey)[0]

        # Wrap in representation
        hv = self._model.rep_cls(vec)

        # Store and return
        self._symbols[name] = hv
        self._counter += 1
        return hv

    def add_many(self, names: Iterable[str]) -> list[AbstractHypervector]:
        """Add multiple symbols to memory.

        Args:
            names: Iterable of symbol names to add.

        Returns:
            List of hypervectors corresponding to the added symbols.

        Example:
            >>> memory = VSAMemory(model)
            >>> colors = memory.add_many(["red", "green", "blue"])
            >>> assert len(colors) == 3
        """
        return [self.add(name) for name in names]

    def get(self, name: str) -> AbstractHypervector:
        """Get a hypervector by name.

        Args:
            name: Name of the symbol to retrieve.

        Returns:
            The hypervector associated with the symbol.

        Raises:
            KeyError: If the symbol does not exist in memory.

        Example:
            >>> memory = VSAMemory(model)
            >>> memory.add("dog")
            >>> dog = memory.get("dog")
        """
        return self._symbols[name]

    def __getitem__(self, name: str) -> AbstractHypervector:
        """Get a hypervector by name using dictionary syntax.

        Args:
            name: Name of the symbol to retrieve.

        Returns:
            The hypervector associated with the symbol.

        Raises:
            KeyError: If the symbol does not exist in memory.

        Example:
            >>> memory = VSAMemory(model)
            >>> memory.add("dog")
            >>> dog = memory["dog"]
        """
        return self.get(name)

    def __contains__(self, name: str) -> bool:
        """Check if a symbol exists in memory.

        Args:
            name: Name of the symbol to check.

        Returns:
            True if the symbol exists, False otherwise.

        Example:
            >>> memory = VSAMemory(model)
            >>> memory.add("dog")
            >>> assert "dog" in memory
            >>> assert "cat" not in memory
        """
        return name in self._symbols

    def keys(self) -> list[str]:
        """Get all symbol names in memory.

        Returns:
            List of symbol names.

        Example:
            >>> memory = VSAMemory(model)
            >>> memory.add_many(["a", "b", "c"])
            >>> assert memory.keys() == ["a", "b", "c"]
        """
        return list(self._symbols.keys())

    def __len__(self) -> int:
        """Get the number of symbols in memory.

        Returns:
            Number of stored symbols.

        Example:
            >>> memory = VSAMemory(model)
            >>> memory.add_many(["a", "b", "c"])
            >>> assert len(memory) == 3
        """
        return len(self._symbols)

    def clear(self) -> None:
        """Remove all symbols from memory.

        Example:
            >>> memory = VSAMemory(model)
            >>> memory.add_many(["a", "b", "c"])
            >>> memory.clear()
            >>> assert len(memory) == 0
        """
        self._symbols.clear()
        self._counter = 0

    def __repr__(self) -> str:
        """String representation of VSAMemory."""
        return f"VSAMemory(model={self._model.rep_cls.__name__}, symbols={len(self._symbols)})"

Attributes

model property

Get the underlying VSAModel.

Functions

__init__(model, key=None)

Initialize VSAMemory with a model.

Parameters:

Name Type Description Default
model VSAModel

VSAModel instance defining the VSA algebra.

required
key Optional[Array]

Optional JAX PRNG key for reproducible sampling.

None
Source code in vsax/core/memory.py
def __init__(self, model: VSAModel, key: Optional[jax.Array] = None) -> None:
    """Initialize VSAMemory with a model.

    Args:
        model: VSAModel instance defining the VSA algebra.
        key: Optional JAX PRNG key for reproducible sampling.
    """
    self._model = model
    self._symbols: dict[str, AbstractHypervector] = {}
    self._key = key if key is not None else jax.random.PRNGKey(0)
    self._counter = 0

add(name)

Add a new symbol to memory with a randomly sampled hypervector.

If the symbol already exists, returns the existing hypervector without resampling.

Parameters:

Name Type Description Default
name str

Name of the symbol to add.

required

Returns:

Type Description
AbstractHypervector

The hypervector associated with the symbol.

Example

memory = VSAMemory(model) dog = memory.add("dog") assert "dog" in memory

Source code in vsax/core/memory.py
def add(self, name: str) -> AbstractHypervector:
    """Add a new symbol to memory with a randomly sampled hypervector.

    If the symbol already exists, returns the existing hypervector without
    resampling.

    Args:
        name: Name of the symbol to add.

    Returns:
        The hypervector associated with the symbol.

    Example:
        >>> memory = VSAMemory(model)
        >>> dog = memory.add("dog")
        >>> assert "dog" in memory
    """
    if name in self._symbols:
        return self._symbols[name]

    # Split key for this sample
    self._key, subkey = jax.random.split(self._key)

    # Sample a new vector
    vec = self._model.sampler(self._model.dim, 1, subkey)[0]

    # Wrap in representation
    hv = self._model.rep_cls(vec)

    # Store and return
    self._symbols[name] = hv
    self._counter += 1
    return hv

add_many(names)

Add multiple symbols to memory.

Parameters:

Name Type Description Default
names Iterable[str]

Iterable of symbol names to add.

required

Returns:

Type Description
list[AbstractHypervector]

List of hypervectors corresponding to the added symbols.

Example

memory = VSAMemory(model) colors = memory.add_many(["red", "green", "blue"]) assert len(colors) == 3

Source code in vsax/core/memory.py
def add_many(self, names: Iterable[str]) -> list[AbstractHypervector]:
    """Add multiple symbols to memory.

    Args:
        names: Iterable of symbol names to add.

    Returns:
        List of hypervectors corresponding to the added symbols.

    Example:
        >>> memory = VSAMemory(model)
        >>> colors = memory.add_many(["red", "green", "blue"])
        >>> assert len(colors) == 3
    """
    return [self.add(name) for name in names]

get(name)

Get a hypervector by name.

Parameters:

Name Type Description Default
name str

Name of the symbol to retrieve.

required

Returns:

Type Description
AbstractHypervector

The hypervector associated with the symbol.

Raises:

Type Description
KeyError

If the symbol does not exist in memory.

Example

memory = VSAMemory(model) memory.add("dog") dog = memory.get("dog")

Source code in vsax/core/memory.py
def get(self, name: str) -> AbstractHypervector:
    """Get a hypervector by name.

    Args:
        name: Name of the symbol to retrieve.

    Returns:
        The hypervector associated with the symbol.

    Raises:
        KeyError: If the symbol does not exist in memory.

    Example:
        >>> memory = VSAMemory(model)
        >>> memory.add("dog")
        >>> dog = memory.get("dog")
    """
    return self._symbols[name]

__getitem__(name)

Get a hypervector by name using dictionary syntax.

Parameters:

Name Type Description Default
name str

Name of the symbol to retrieve.

required

Returns:

Type Description
AbstractHypervector

The hypervector associated with the symbol.

Raises:

Type Description
KeyError

If the symbol does not exist in memory.

Example

memory = VSAMemory(model) memory.add("dog") dog = memory["dog"]

Source code in vsax/core/memory.py
def __getitem__(self, name: str) -> AbstractHypervector:
    """Get a hypervector by name using dictionary syntax.

    Args:
        name: Name of the symbol to retrieve.

    Returns:
        The hypervector associated with the symbol.

    Raises:
        KeyError: If the symbol does not exist in memory.

    Example:
        >>> memory = VSAMemory(model)
        >>> memory.add("dog")
        >>> dog = memory["dog"]
    """
    return self.get(name)

__contains__(name)

Check if a symbol exists in memory.

Parameters:

Name Type Description Default
name str

Name of the symbol to check.

required

Returns:

Type Description
bool

True if the symbol exists, False otherwise.

Example

memory = VSAMemory(model) memory.add("dog") assert "dog" in memory assert "cat" not in memory

Source code in vsax/core/memory.py
def __contains__(self, name: str) -> bool:
    """Check if a symbol exists in memory.

    Args:
        name: Name of the symbol to check.

    Returns:
        True if the symbol exists, False otherwise.

    Example:
        >>> memory = VSAMemory(model)
        >>> memory.add("dog")
        >>> assert "dog" in memory
        >>> assert "cat" not in memory
    """
    return name in self._symbols

keys()

Get all symbol names in memory.

Returns:

Type Description
list[str]

List of symbol names.

Example

memory = VSAMemory(model) memory.add_many(["a", "b", "c"]) assert memory.keys() == ["a", "b", "c"]

Source code in vsax/core/memory.py
def keys(self) -> list[str]:
    """Get all symbol names in memory.

    Returns:
        List of symbol names.

    Example:
        >>> memory = VSAMemory(model)
        >>> memory.add_many(["a", "b", "c"])
        >>> assert memory.keys() == ["a", "b", "c"]
    """
    return list(self._symbols.keys())

__len__()

Get the number of symbols in memory.

Returns:

Type Description
int

Number of stored symbols.

Example

memory = VSAMemory(model) memory.add_many(["a", "b", "c"]) assert len(memory) == 3

Source code in vsax/core/memory.py
def __len__(self) -> int:
    """Get the number of symbols in memory.

    Returns:
        Number of stored symbols.

    Example:
        >>> memory = VSAMemory(model)
        >>> memory.add_many(["a", "b", "c"])
        >>> assert len(memory) == 3
    """
    return len(self._symbols)

clear()

Remove all symbols from memory.

Example

memory = VSAMemory(model) memory.add_many(["a", "b", "c"]) memory.clear() assert len(memory) == 0

Source code in vsax/core/memory.py
def clear(self) -> None:
    """Remove all symbols from memory.

    Example:
        >>> memory = VSAMemory(model)
        >>> memory.add_many(["a", "b", "c"])
        >>> memory.clear()
        >>> assert len(memory) == 0
    """
    self._symbols.clear()
    self._counter = 0

__repr__()

String representation of VSAMemory.

Source code in vsax/core/memory.py
def __repr__(self) -> str:
    """String representation of VSAMemory."""
    return f"VSAMemory(model={self._model.rep_cls.__name__}, symbols={len(self._symbols)})"