Lava Software Framework

repository·main·Indexed 20 days ago

https://github.com/lava-nc/lava

An open-source software framework for developing applications for neuromorphic hardware architectures. Lava provides abstractions for distributed, massively parallel applications capable of running on conventional CPUs or neuromorphic chips such as Intel's Loihi. The framework includes tools for defining ProcessModels with specific hardware requirements (CPU, GPU, NeuroCore), managing symbolic equations for learning rules, and implementing stochastic rounding for trace decay and synaptic variables.

Tokens
27.2K
Snippets
69
Records
87
Agent score
72%

What's inside lava-nc

  1. Install Lava from GitHub Release binaries

    main

    You can install Lava using published binaries from GitHub Releases. This is intended for users and does not include test access.

    Steps:

    1. Download the .tar.gz package for the desired version.
    2. Create and activate a virtual environment.
    3. Install the local binary using pip.
    python -m venv .venv
    source .venv/bin/activate ## Or Windows: .venv\Scripts\activate
    pip install -U pip
    # Substitute lava version needed for lava-nc-<version here>.tar.gz below
    pip install lava-nc-0.9.0.tar.gz
  2. Install Lava via PyPI

    main

    For standard users who do not need to run tests or modify the source code, install the lava-nc package via pip.

    Note: This method does not provide access to run the project's internal unit tests.

    python -m venv .venv
    source .venv/bin/activate ## Or Windows: .venv\Scripts\activate
    pip install -U pip
    pip install lava-nc
  3. Install Lava via Conda

    main

    If you use the Conda package manager, you can install Lava directly from conda-forge.

    For optimized performance with Intel numpy and scipy, follow the specific environment creation steps to ensure compatibility.

    # Standard installation
    conda install lava -c conda-forge
    
    # Installation with Intel numpy and scipy
    conda create -n lava python=3.9 -c intel
    conda activate lava
    conda install -n lava -c intel numpy scipy
    conda install -n lava -c conda-forge lava --freeze-installed
  4. Install Lava from source (Windows)

    main

    To develop in Lava on Windows, use PowerShell to set up a virtual environment and poetry for dependency management.

    Steps:

    1. Clone the lava repository.
    2. Checkout the desired version (e.g., v0.9.0).
    3. Create and activate a Python virtual environment.
    4. Upgrade pip.
    5. Install poetry.
    6. Configure poetry to use in-project virtual environments.
    7. Install dependencies and run pytest to verify.
    # Commands using PowerShell
    cd $HOME
    git clone git@github.com:lava-nc/lava.git
    cd lava
    git checkout v0.9.0
    python3 -m venv .venv
    .venv\Scripts\activate
    pip install -U pip
    curl -sSL https://install.python-poetry.org | python3 -
    poetry config virtualenvs.in-project true
    poetry install
    pytest
  5. Run Linting, Testing, and Security Checks in Lava

    main

    If you are developing in the Lava repository, use the following commands to maintain code quality:

    • Linting: Use flakeheaven to lint the source and tests.
    • Unit Tests: Use pytest.
    • Security: Use bandit to scan the source code for security vulnerabilities.
    • Packaging: Use poetry build to create distributions.
    # Install poetry
    curl -sSL https://install.python-poetry.org | python3 -
    poetry config virtualenvs.in-project true
    poetry install
    poetry shell
    
    # Run linting
    flakeheaven lint src/lava tests
    
    # Run unit tests
    pytest
    
    # Create distribution
    poetry build
    
    # Run Security Linting
    bandit -r src/lava/.
    
    # Run Security Linting with custom format if failures occur
    bandit -r src/lava/. --format custom --msg-template '{abspath}:{line}: {test_id}[bandit]: {severity}: {msg}'
  6. Install Lava from source (Linux/MacOS)

    main

    To develop in Lava and modify its source code, clone the repository and use poetry for environment management. This method allows you to run the project's unit tests.

    Prerequisites:

    • Python 3
    • git
    • poetry (installed via the official installer)

    Steps:

    1. Install poetry.
    2. Clone the lava repository.
    3. Checkout the desired version (e.g., v0.9.0).
    4. Install git hooks.
    5. Configure poetry to use in-project virtual environments.
    6. Install dependencies and activate the environment.
    7. Run pytest to verify the installation.
    cd $HOME
    curl -sSL https://install.python-poetry.org | python3 -
    git clone git@github.com:lava-nc/lava.git
    cd lava
    git checkout v0.9.0
    ./utils/githook/install-hook.sh
    poetry config virtualenvs.in-project true
    poetry install
    source .venv/bin/activate
    pytest
  7. What is a Process in Lava?

    main

    A Process is the fundamental computational unit in Lava. It encapsulates four key components:

    1. Data: State stored in Var objects.
    2. Algorithms: Logic that manipulates the data.
    3. Ports: Interfaces (InPort, OutPort) used to share data with other processes via channels.
    4. API: Public methods and variables that facilitate user interaction.

    Processes are independent, operating on local memory and communicating via messages. This design allows for simultaneous, asynchronous computation, mirroring neuromorphic hardware parallelism. A single Process can be backend-agnostic, meaning the same Python code can run on a CPU, GPU, or neuromorphic cores (like Loihi) by using different ProcessModel implementations.

  8. Accessing Lava on Intel Loihi hardware

    main

    Lava can be used with Intel Loihi 1 or 2 research systems. Because these systems are not commercially available, you must join the Intel Neuromorphic Research Community (INRC) to gain access to cloud-hosted or physical Loihi systems.

    Upon joining the INRC, you will receive specific instructions for running Lava on Loihi. Intel also provides proprietary components of the magma library for Loihi compilation, which are installed into the same Lava namespace.

  9. Understand the IdGeneratorSingleton interface

    main

    The IdGeneratorSingleton is an abstract base class used to implement singleton patterns for generating globally unique integer IDs. This is used to distinguish unique objects within the system.

    Implementations of this interface typically provide:

    • get_next_id(): Returns the next available unique integer ID.
    • reset_singleton(): A class method to reset the singleton instance, setting cls.instance to None and cls.is_not_initialized to True.
  10. How three-factor learning works in Lava

    main

    Three-factor learning in Lava simulates Loihi 2's capability to use localized, graded reward signals to modulate synaptic plasticity.

    1. Two-Factor Component: Standard STDP (Spike-Timing Dependent Plasticity) creates an eligibility trace (or 'tag') based on the correlation between pre-synaptic and post-synaptic spikes.
    2. Third-Factor Component: A reward signal (the third factor) is provided to the post-synaptic neuron (e.g., via an RSTDPLIF process).
    3. Modulation: The reward signal modulates the weight update. The synaptic weight $W$ is updated according to the product of the eligibility trace and the reward signal: $\dot{W} = R \cdot E$.

    In a Lava network, this is achieved by connecting a reward source to a post-synaptic neuron's a_third_factor_in port, and then connecting the neuron's reward trace outputs (s_out_y1, s_out_y2, etc.) to the LearningDense process's input ports (s_in_y1, s_in_y2, etc.).

  11. Choose between fixed-point and floating-point simulations

    main

    Lava allows you to simulate the Loihi learning engine in two modes using the select_tag parameter in the run_cfg. This is critical for approximating hardware behavior:

    • floating_pt: Uses floating-point arithmetic for high precision.
    • fixed_pt: Uses fixed-point arithmetic, which approximates the quantized behavior of the Loihi neuromorphic hardware.

    When using fixed_pt, you must ensure your LIF parameters (like du and dv) are scaled appropriately for fixed-point representation.

    from lava.magma.core.run_configs import Loihi2SimCfg
    
    # Use 'fixed_pt' to approximate Loihi hardware behavior
    SELECT_TAG = "fixed_pt"
    
    # Run the simulation with the chosen tag
    pattern_pre.run(condition=RunSteps(num_steps=num_steps), 
                    run_cfg=Loihi2SimCfg(select_tag=SELECT_TAG))
  12. How ProcessModels define Process behavior

    main

    While an AbstractProcess defines the interface (ports and variables), a ProcessModel defines the behavior (how states evolve over time).

    A single Process can have multiple ProcessModels—one for each backend (e.g., one for CPU, one for Loihi).

    To implement a model, you typically use decorators like @implements(proc=..., protocol=...) and @requires(...) to bind the model to a specific process and hardware resource. The model defines how inputs are received, how variables are updated, and how outputs are sent.

    from lava.magma.core.decorator import implements, requires
    from lava.magma.core.resources import CPU
    from lava.magma.core.model.py.model import PyLoihiProcessModel
    
    @implements(proc=MyProcess, protocol=LoihiProtocol)
    @requires(CPU)
    class MyProcessModel(PyLoihiProcessModel):
        # Define types for ports and vars
        # Implement logic in methods like run_spk(self)
        pass