Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lava top-level API #745

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9825acc
Added top-level __init__ file to simplify imports for Lava scripts.
mathisrichter Jul 23, 2023
796f781
Added missing blank line at end of file.
mathisrichter Jul 23, 2023
0c1c190
Fixed broken import.
mathisrichter Jul 23, 2023
1bb8065
Explicitly imported and renamed RingBuffer classes.
mathisrichter Jul 23, 2023
d68eb0b
Imported some utils modules.
mathisrichter Jul 23, 2023
f988adb
Imported relevant run-configs.
mathisrichter Jul 23, 2023
0a4d687
Updated all tutorials to use new lava module API.
mathisrichter Jul 28, 2023
0e324f7
Merge branch 'main' of https://github.com/lava-nc/lava into lava_api
mathisrichter Aug 2, 2023
96a00f3
Merge branch 'main' into lava_api
PhilippPlank Aug 7, 2023
cd9bcb4
Merge branch 'main' into lava_api
PhilippPlank Aug 8, 2023
cf7906c
Possible fix for broken tutorial unit tests.
mathisrichter Aug 11, 2023
463aa82
Merge branch 'lava_api' of https://github.com/lava-nc/lava into lava_api
mathisrichter Aug 11, 2023
2d9707a
Removed unnecessary import.
mathisrichter Aug 11, 2023
db67116
Possible tutorial test fix based on importlib.
mathisrichter Aug 11, 2023
edd5821
Ignoring unused imports in init-file.
mathisrichter Aug 11, 2023
d8c9769
Work on importlib solution
mathisrichter Aug 11, 2023
1023f81
Fixed import errors in tutorials.
mathisrichter Aug 11, 2023
3dac858
Unit test for lava module imports.
mathisrichter Aug 11, 2023
83398ef
Removed imports for CLP Processes and Spiker as they are not common.
mathisrichter Aug 11, 2023
971dd9c
Removed noqa from init file
mathisrichter Aug 11, 2023
b7104b8
Added all imports in init file to __all__.
mathisrichter Aug 11, 2023
a58e60a
Fixed broken import in tutorial.
mathisrichter Aug 11, 2023
afd8097
Fixed __all__ to use strings instead of module names.
mathisrichter Aug 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/lava/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
# See: https://spdx.org/licenses/

# Creates a pkgutil-style namespace package that extends the namespace over
# multiple directory structures.
__path__ = __import__('pkgutil').extend_path(__path__, __name__)

######
# IMPORTS

# Import the most common classes and functions to the top level to enable
# >>> import lava
# as the only required import for the most common Lava programs.

# MOST COMMON PROCESSES
from lava.proc.conv.process import Conv
from lava.proc.dense.process import Dense, DelayDense, LearningDense
from lava.proc.io.dataloader import SpikeDataloader, StateDataloader
from lava.proc.io.source import RingBuffer as SourceRingBuffer
from lava.proc.io.sink import RingBuffer as SinkRingBuffer
from lava.proc.io.encoder import DeltaEncoder
from lava.proc.io.injector import Injector
from lava.proc.io.extractor import Extractor
from lava.proc.io.reset import Reset
from lava.proc.learning_rules.r_stdp_learning_rule import RewardModulatedSTDP
from lava.proc.learning_rules.stdp_learning_rule import STDPLoihi
from lava.proc.lif.process import LIF, LearningLIF, LIFRefractory
from lava.proc.monitor.process import Monitor
from lava.proc.receiver.process import Receiver
from lava.proc.rf.process import RF
from lava.proc.rf_iz.process import RF_IZ
from lava.proc.sdn.process import Sigma, Delta, SigmaDelta, ActivationMode
from lava.proc.sparse.process import Sparse, LearningSparse, DelaySparse

# RUN CONFIGURATIONS & CONDITIONS
from lava.magma.core.run_configs import Loihi2SimCfg, Loihi2HwCfg
from lava.magma.core.run_conditions import RunContinuous, RunSteps

# MAGMA
from lava.magma.core.process.process import LogConfig

# UTILS
from lava.utils import loihi
from lava.utils import plots
from lava.utils.serialization import save, load

__all__ = ['Conv', 'Dense', 'DelayDense', 'LearningDense', 'SpikeDataloader',
'StateDataloader', 'SourceRingBuffer', 'SinkRingBuffer',
'DeltaEncoder', 'Injector', 'Extractor', 'Reset',
'RewardModulatedSTDP', 'STDPLoihi', 'LIF', 'LearningLIF',
'LIFRefractory', 'Monitor', 'Receiver', 'RF', 'RF_IZ',
'Sigma', 'Delta', 'SigmaDelta', 'ActivationMode', 'Sparse',
'LearningSparse', 'DelaySparse', 'Loihi2HwCfg', 'Loihi2SimCfg',
'RunContinuous', 'RunSteps', 'LogConfig', 'loihi', 'plots',
'save', 'load']
2 changes: 1 addition & 1 deletion src/lava/proc/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# SPDX-License-Identifier: BSD-3-Clause
# See: https://spdx.org/licenses/

from . import reset, source, sink, dataloader, encoder
from lava.proc.io import reset, source, sink, dataloader, encoder

__all__ = ['reset', 'source', 'sink', 'dataloader', 'encoder']
17 changes: 17 additions & 0 deletions tests/lava/test_init_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
# See: https://spdx.org/licenses/

import unittest
import importlib.util


class TestInitImports(unittest.TestCase):

def test_lava_init_file_imports_lif_class(self) -> None:
module_spec = importlib.util.find_spec("lava")
lava_module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(lava_module)

lif_importable = hasattr(lava_module, "LIF")
self.assertTrue(lif_importable)
40 changes: 20 additions & 20 deletions tests/lava/tutorials/test_tutorials.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
# See: https://spdx.org/licenses/
# See: https://

import importlib.util
import glob
import os
import platform
Expand All @@ -12,11 +13,8 @@
import unittest
from test import support

import lava
import nbformat

import tutorials


class TestTutorials(unittest.TestCase):
"""Export notebook, execute to check for errors."""
Expand Down Expand Up @@ -75,15 +73,10 @@ def _update_pythonpath(
os.chdir(base_dir + "/" + dir_name)

env = os.environ.copy()
module_path = [lava.__path__.__dict__["_path"][0]]

module_path.extend(
[os.path.dirname(module_path[0]), env.get("PYTHONPATH", "")]
)

sys_path = ":".join(map(str, sys.path))
env_path = env.get("PYTHONPATH", "")
mod_path = ":".join(map(str, module_path))
mod_path = ":".join(map(str, [get_module_path("lava"), env_path]))

env["PYTHONPATH"] = env_path + ":" + mod_path + ":" + sys_path

Expand Down Expand Up @@ -157,15 +150,14 @@ def _run_notebook(self, notebook: str, e2e_tutorial: bool = False):
end to end tutorial, by default False
"""
cwd = os.getcwd()
tutorials_temp_directory = tutorials.__path__.__dict__["_path"][0]
tutorials_directory = ""
tutorials_module_path = get_module_path("tutorials")

if not e2e_tutorial:
tutorials_temp_directory = tutorials_temp_directory + "/in_depth"
tutorials_module_path = tutorials_module_path + "/in_depth"
else:
tutorials_temp_directory = tutorials_temp_directory + "/end_to_end"
tutorials_module_path = tutorials_module_path + "/end_to_end"

tutorials_directory = os.path.realpath(tutorials_temp_directory)
tutorials_directory = os.path.realpath(tutorials_module_path)
os.chdir(tutorials_directory)

errors_record = {}
Expand All @@ -178,7 +170,7 @@ def _run_notebook(self, notebook: str, e2e_tutorial: bool = False):

self.assertTrue(
len(discovered_notebooks) != 0,
"Notebook not found. Input to function {}".format(notebook),
f"Notebook not found. Input to function {notebook}",
)

# If the notebook is found execute it and store any errors
Expand All @@ -194,10 +186,8 @@ def _run_notebook(self, notebook: str, e2e_tutorial: bool = False):

self.assertFalse(
errors_record,
"Failed to execute Jupyter Notebooks \
with errors: \n {}".format(
errors_record
),
f"Failed to execute Jupyter Notebooks "
f"with errors: \n {errors_record}",
)
finally:
os.chdir(cwd)
Expand Down Expand Up @@ -292,5 +282,15 @@ def test_in_depth_clp_01(self):
"clp/tutorial01_one-shot_learning_with_novelty_detection.ipynb")


def get_module_path(module_name: str) -> str:
spec = importlib.util.find_spec(module_name)

# Treat packages with init-files separately.
if spec.origin is None:
return spec.submodule_search_locations[0]

return os.path.dirname(spec.origin)


if __name__ == "__main__":
support.run_unittest(TestTutorials)
Loading