Getting Started

Getting Started

Recognise handwritten digits on Sparsr, then run the same file on bigger hardware without editing it.

This page runs one example on your own machine and then on a cheap cloud instance. The Python file stays the same throughout. One string at the top of it chooses where the vector operations go. The last step says what running it on a real Sparsr card needs.

What you are running

Hyperdimensional computing represents an image as one 4,096-bit hypervector, and a digit class as another. Recognising a digit means finding the class hypervector the image hypervector resembles most. Training reads the data once and uses integer bit operations throughout. The algorithm has no gradients and no training loop.

That maps onto Sparsr directly. A hypervector is one wide register, so comparing an image against a class prototype is one wide instruction and its population count.

The example classifies the full MNIST test set, 10,000 images, at about 79% accuracy, after training on all 60,000 training images. A full run takes about half a minute.

1. On your own machine

You need Linux on x86-64 and CPython 3.10 to 3.13. You do not need Sparsr hardware, a cross-toolchain or the Kernel SDK.

pip install torchhd-sparsr torchvision

Get the example and run it:

git clone https://github.com/Sparsr/torchhd-sparsr
cd torchhd-sparsr/examples/mnist
python example.py

MNIST is about 11 MB, and torchvision downloads it into ./data the first time. For a quicker look, run on a slice:

python example.py --train 6000 --test 1000

It prints the accuracy, a per-digit table, and a line saying how much of the work ran on Sparsr.

What just happened. The comparison against every class prototype ran on the Sparsr VM, a model of the Sparsr processor in software that is installed with the package. Every number the example printed came from that model executing real Sparsr instructions. Encoding and training ran on the host, in ordinary Torchhd code.

2. On a cloud instance

Nothing about the example changes. This step exists to show that, and to give you somewhere to run a larger job than your laptop wants to hold.

Any general-purpose Linux instance will do. The example does not use a GPU or an FPGA, and its memory appetite comes from the PyTorch install rather than from the data. On AWS, a general-purpose instance with 2 vCPUs and 8 GB of memory is comfortable, running Amazon Linux 2023 or Ubuntu 24.04.

sudo dnf install -y python3-pip git    # Amazon Linux 2023
pip install torchhd-sparsr torchvision
git clone https://github.com/Sparsr/torchhd-sparsr
cd torchhd-sparsr/examples/mnist
python3 example.py

Same commands, same output, same accuracy. It is still the Sparsr VM doing the comparisons, because the VM is where the package points by default.

3. On a Sparsr card

An F2 instance on AWS carries the FPGA that Sparsr runs on. The card works today: the Quick start on AWS launches an F2 instance in your own AWS account, loads the Sparsr FPGA image and runs an LDPC sample on the card.

This example stays on the Sparsr VM for now. It needs a wide popcount instruction that the card does not have yet. When the card has it, moving the example onto the card is meant to be one string, at the top of example.py:

os.environ["SPARSR_BACKEND"] = "fpgaf2"

Everything below that line stays as it is, and you move no tensor by hand.

Where to go next

  • The HDC library - the same four operations from C, without Python.
  • torchhd-sparsr - which Torchhd calls run on Sparsr, which ones refuse, and why.
  • The Sparsr VM - what the software model does and does not model.