forked from lava-nc/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime error handling (lava-nc#135)
* - Initial commit on passing exception from ProcessModels up to the runtime * - Initial commit on passing exception from ProcessModels up to the runtime * - Updated commit on passing exception from ProcessModels up to the runtime * - Updated commit on passing exception from ProcessModels up to the runtime
- Loading branch information
1 parent
fa908f4
commit 31495d6
Showing
7 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# Copyright (C) 2021 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# See: https://spdx.org/licenses/ | ||
|
||
import unittest | ||
|
||
from lava.magma.core.decorator import implements, requires, tag | ||
from lava.magma.core.model.py.model import PyLoihiProcessModel | ||
from lava.magma.core.model.py.ports import PyOutPort, PyInPort | ||
from lava.magma.core.model.py.type import LavaPyType | ||
from lava.magma.core.process.ports.ports import OutPort, InPort | ||
from lava.magma.core.process.process import AbstractProcess | ||
from lava.magma.core.resources import CPU | ||
from lava.magma.core.run_configs import Loihi1SimCfg | ||
from lava.magma.core.sync.protocols.loihi_protocol import LoihiProtocol | ||
from lava.magma.core.run_conditions import RunSteps | ||
|
||
|
||
# A minimal process with an OutPort | ||
class P1(AbstractProcess): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.out = OutPort(shape=(2,)) | ||
|
||
|
||
# A minimal process with an InPort | ||
class P2(AbstractProcess): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.inp = InPort(shape=(2,)) | ||
|
||
|
||
# A minimal process with an InPort | ||
class P3(AbstractProcess): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.inp = InPort(shape=(2,)) | ||
|
||
|
||
# A minimal PyProcModel implementing P1 | ||
@implements(proc=P1, protocol=LoihiProtocol) | ||
@requires(CPU) | ||
@tag('floating_pt') | ||
class PyProcModel1(PyLoihiProcessModel): | ||
out: PyOutPort = LavaPyType(PyOutPort.VEC_DENSE, int) | ||
|
||
def run_spk(self): | ||
if self.current_ts > 1: | ||
# Raise exception | ||
raise AssertionError("All the error info") | ||
|
||
|
||
# A minimal PyProcModel implementing P2 | ||
@implements(proc=P2, protocol=LoihiProtocol) | ||
@requires(CPU) | ||
@tag('floating_pt') | ||
class PyProcModel2(PyLoihiProcessModel): | ||
inp: PyInPort = LavaPyType(PyInPort.VEC_DENSE, int) | ||
|
||
def run_spk(self): | ||
if self.current_ts > 1: | ||
# Raise exception | ||
raise TypeError("All the error info") | ||
|
||
|
||
# A minimal PyProcModel implementing P3 | ||
@implements(proc=P3, protocol=LoihiProtocol) | ||
@requires(CPU) | ||
@tag('floating_pt') | ||
class PyProcModel3(PyLoihiProcessModel): | ||
inp: PyInPort = LavaPyType(PyInPort.VEC_DENSE, int) | ||
|
||
def run_spk(self): | ||
... | ||
|
||
|
||
class TestExceptionHandling(unittest.TestCase): | ||
def test_one_pm(self): | ||
"""Checks the forwarding of exceptions within a ProcessModel to the | ||
runtime.""" | ||
|
||
# Create an instance of P1 | ||
proc = P1() | ||
|
||
# Run the network for 1 time step -> no exception | ||
proc.run(condition=RunSteps(num_steps=1), run_cfg=Loihi1SimCfg()) | ||
|
||
# Run the network for another time step -> expect exception | ||
with self.assertRaises(RuntimeError) as context: | ||
proc.run(condition=RunSteps(num_steps=1), run_cfg=Loihi1SimCfg()) | ||
|
||
exception = context.exception | ||
self.assertEqual(RuntimeError, type(exception)) | ||
# 1 exception in the ProcessModel expected | ||
self.assertTrue('1 Exception(s) occurred' in str(exception)) | ||
|
||
def test_two_pm(self): | ||
"""Checks the forwarding of exceptions within two ProcessModel to the | ||
runtime.""" | ||
|
||
# Create a sender instance of P1 and a receiver instance of P2 | ||
sender = P1() | ||
recv = P2() | ||
|
||
# Connect sender with receiver | ||
sender.out.connect(recv.inp) | ||
|
||
# Run the network for 1 time step -> no exception | ||
sender.run(condition=RunSteps(num_steps=1), run_cfg=Loihi1SimCfg()) | ||
|
||
# Run the network for another time step -> expect exception | ||
with self.assertRaises(RuntimeError) as context: | ||
sender.run(condition=RunSteps(num_steps=1), run_cfg=Loihi1SimCfg()) | ||
|
||
exception = context.exception | ||
self.assertEqual(RuntimeError, type(exception)) | ||
# 2 Exceptions in the ProcessModels expected | ||
self.assertTrue('2 Exception(s) occurred' in str(exception)) | ||
|
||
def test_three_pm(self): | ||
"""Checks the forwarding of exceptions within three ProcessModel to the | ||
runtime.""" | ||
|
||
# Create a sender instance of P1 and receiver instances of P2 and P3 | ||
sender = P1() | ||
recv1 = P2() | ||
recv2 = P3() | ||
|
||
# Connect sender with receiver | ||
sender.out.connect([recv1.inp, recv2.inp]) | ||
|
||
# Run the network for 1 time step -> no exception | ||
sender.run(condition=RunSteps(num_steps=1), run_cfg=Loihi1SimCfg()) | ||
|
||
# Run the network for another time step -> expect exception | ||
with self.assertRaises(RuntimeError) as context: | ||
sender.run(condition=RunSteps(num_steps=1), run_cfg=Loihi1SimCfg()) | ||
|
||
exception = context.exception | ||
self.assertEqual(RuntimeError, type(exception)) | ||
# 2 Exceptions in the ProcessModels expected | ||
self.assertTrue('2 Exception(s) occurred' in str(exception)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main(buffer=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters