Skip to content

Commit

Permalink
Merge pull request #202 from intel-innersource/doc/tut-3-rm-nc
Browse files Browse the repository at this point in the history
rm nc proc model from tutorial 3
  • Loading branch information
weidel-p authored Jul 4, 2022
2 parents 369deab + 185ff33 commit 087e6c1
Showing 1 changed file with 3 additions and 115 deletions.
118 changes: 3 additions & 115 deletions tutorials/in_depth/tutorial03_process_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"cells": [
{
"cell_type": "markdown",
"id": "d181cab5",
"metadata": {},
"source": [
"*Copyright (C) 2021 Intel Corporation*<br>\n",
Expand All @@ -20,23 +19,20 @@
},
{
"cell_type": "markdown",
"id": "4439ffa3",
"metadata": {},
"source": [
"<img src=\"https://raw.githubusercontent.com/lava-nc/lava-nc.github.io/main/_static/images/tutorial03/fig01_leafprocessmodel.png\"/>"
]
},
{
"cell_type": "markdown",
"id": "17de7976",
"metadata": {},
"source": [
"In this tutorial, we walk through the creation of multiple _LeafProcessModels_ that could be used to implement the behavior of a Leaky Integrate-and-Fire (LIF) neuron _Process_."
]
},
{
"cell_type": "markdown",
"id": "85659276",
"metadata": {},
"source": [
"## Recommended tutorials before starting: \n",
Expand All @@ -46,15 +42,13 @@
},
{
"cell_type": "markdown",
"id": "9c609a10",
"metadata": {},
"source": [
"## Create a LIF _Process_"
]
},
{
"cell_type": "markdown",
"id": "908bb4b2",
"metadata": {},
"source": [
"First, we will define our LIF _Process_ exactly as it is defined in the `Magma` core library of Lava. (For more information on defining Lava Processes, see the [previous tutorial](./tutorial02_processes.ipynb).) Here the LIF neural _Process_ accepts activity from synaptic inputs via _InPort_ `a_in` and outputs spiking activity via _OutPort_ `s_out`."
Expand All @@ -63,7 +57,6 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "2ee8fe6a",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -113,31 +106,27 @@
},
{
"cell_type": "markdown",
"id": "5e7dfbc5",
"metadata": {},
"source": [
"## Create a Python _LeafProcessModel_ that implements the LIF _Process_"
]
},
{
"cell_type": "markdown",
"id": "01fe8392",
"metadata": {},
"source": [
"Now, we will create a Python _ProcessModel_, or _PyProcessModel_, that runs on a CPU compute resource and implements the LIF _Process_ behavior."
]
},
{
"cell_type": "markdown",
"id": "98b0d6d1",
"metadata": {},
"source": [
"#### Setup"
]
},
{
"cell_type": "markdown",
"id": "c1dfd862",
"metadata": {},
"source": [
"We begin by importing the required Lava classes.\n",
Expand All @@ -147,7 +136,6 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "f126e6e0",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -159,7 +147,6 @@
},
{
"cell_type": "markdown",
"id": "cdfe855a",
"metadata": {
"tags": []
},
Expand All @@ -170,7 +157,6 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "678d1cf5",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -181,15 +167,13 @@
},
{
"cell_type": "markdown",
"id": "655850b3",
"metadata": {},
"source": [
"#### Defining a _PyLifModel_ for LIF"
]
},
{
"cell_type": "markdown",
"id": "8678f2ef",
"metadata": {
"tags": []
},
Expand All @@ -206,7 +190,6 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "aadc040b",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -245,7 +228,6 @@
},
{
"cell_type": "markdown",
"id": "5fde8b8e",
"metadata": {},
"source": [
"#### Compile and run _PyLifModel_"
Expand All @@ -254,7 +236,6 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "52d7aed6",
"metadata": {},
"outputs": [
{
Expand All @@ -278,117 +259,24 @@
},
{
"cell_type": "markdown",
"id": "2cb5249d",
"metadata": {},
"source": [
"## Create an _NcProcessModel_ that implements the LIF _Process_ "
]
},
{
"cell_type": "markdown",
"id": "052390c3",
"metadata": {},
"source": [
"_Processes_ can have more than one _ProcessModel_, and different _ProcessModels_ can enable execution on different compute resources. The Lava _Compiler_ supports the execution of _Processes_ on Loihi 2 Neurocores using the _Loihi2NeuroCore_ class. Below is an example _NcLifModel_ that implements our same LIF _Process_."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b72741d7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from lava.magma.core.model.nc.model import AbstractNcProcessModel\n",
"from lava.magma.core.resources import Loihi2NeuroCore\n",
"from lava.magma.core.model.nc.type import LavaNcType\n",
"from lava.magma.core.nets.table_container import NcVar\n",
"from lava.magma.core.model.nc.ports import NcInPort, NcOutPort\n",
"try:\n",
" from lava.magma.compiler.subcompilers.nc.net import NetL2\n",
"except ImportError:\n",
" class NetL2:\n",
" pass\n",
"\n",
"\n",
"\n",
"@implements(proc=LIF, protocol=LoihiProtocol)\n",
"@requires(Loihi2NeuroCore)\n",
"class NcModelLif(AbstractNcProcessModel):\n",
" \"\"\"Implementation of a Leaky Integrate-and-Fire (LIF) neural process\n",
" model that defines the behavior on Loihi 2.\n",
" \"\"\"\n",
" shape: NcVar = LavaNcType(NcVar, np.int16, precision=16)\n",
"\n",
" # Declare port implementation\n",
" a_in: NcInPort = LavaNcType(NcInPort, np.int16, precision=16)\n",
" s_out: NcOutPort = LavaNcType(NcOutPort, np.int32, precision=24)\n",
" \n",
" # Declare variable implementation\n",
" u: NcVar = LavaNcType(NcVar, np.int32, precision=24)\n",
" v: NcVar = LavaNcType(NcVar, np.int32, precision=24)\n",
" du: NcVar = LavaNcType(NcVar, np.uint16, precision=12)\n",
" dv: NcVar = LavaNcType(NcVar, np.uint16, precision=12)\n",
" bias: NcVar = LavaNcType(NcVar, np.int16, precision=13) \n",
" vth: NcVar = LavaNcType(NcVar, np.int32, precision=17)\n",
"\n",
" def allocate(self, net: NetL2):\n",
" \"\"\"Allocates neural resources in 'virtual' neuro core.\"\"\"\n",
"\n",
" # Allocate neurons\n",
" neurons_cfg: Nodes = net.neurons_cfg.allocate(\n",
" size=1,\n",
" du=self.du,\n",
" dv=self.dv,\n",
" vth=self.vth)\n",
" \n",
" neurons: Nodes = net.neurons.allocate_hcode(\n",
" size=self.shape[0],\n",
" u=self.u,\n",
" v=self.v,\n",
" bias=self.bias,\n",
" cfg=neurons_cfg)\n",
"\n",
" # Allocate output axons\n",
" ax_out: Nodes = net.ax_out.allocate(\n",
" size=(self.shape,),\n",
" dly=0)\n",
"\n",
" # Connect InPort of Process to neurons\n",
" self.a_in.connect(neurons)\n",
" \n",
" # Connect neurons to output axons\n",
" neurons.connect(ax_out)\n",
" \n",
" # Connect output axons to OutPort of Process\n",
" ax_out.connect(self.s_out)"
]
},
{
"cell_type": "markdown",
"id": "85900a3c",
"metadata": {},
"source": [
"## Selecting 1 _ProcessModel_: More on _LeafProcessModel_ attributes and relations"
]
},
{
"cell_type": "markdown",
"id": "e4ef2584",
"metadata": {},
"source": [
"We have demonstrated multiple _ProcessModel_ implementations of a single LIF _Process_. How is one of several _ProcessModels_ then selected as the implementation of a _Process_ during runtime? To answer that question, we take a deeper dive into the attributes of a _LeafProcessModel_ and the relationship between a _LeafProcessModel_, a _Process_, and a _SyncProtocol_. \n",
"\n",
"As shown below, a _LeafProcessModel_ implements both a Process (in our example, LIF) and a _SyncProtocol_ (in our example, the _LoihiProtocol_). A _LeafProcessModel_ has a single _Type_. In this tutorial `PyLifModel` has Type `PyLoihiProcessModel`, while `NcLifModel` has Type `NcLoihiProcessModel`. A _LeafProcessModel_ also has one or more resource _Requirements_ that specify the compute resources (for example, a CPU, a GPU, or Loihi Neurocores) or peripheral resources (like access to a camera) that are required for execution. Finally, a _LeafProcessModel_ can have one and more user-defineable _Tags_. _Tags_ can be used, among other customizable reasons, to group multiple _ProcessModels_ for a multi- _Process_ application or to distinguish between multiple _LeafProcessModel_ implementations with the same _Type_ and _SyncProtocol_. As an example, we illustrated above a `PyLoihiProcessModel` for LIF that uses floating point precision and has the tag `@tag('floating_pt')`. There also exists a `PyLoihiProcessModel` that uses fixed point precision and has behavior that is bit-accurate with LIF execution on a Loihi chip; this _ProcessModel_ is distinguished by the tag `@tag('fixed_pt')`. Together, the _Type_, _Tag_ and _Requirement_ attributes of a _LeafProcessModel_ allow users to define a _RunConfig_ that chooses which of several _LeafProcessModels_ is used to implement a _Process_ at runtime. The Core Lava Library will also provide several preconfigured _RunConfigs_. \n",
"As shown below, a _LeafProcessModel_ implements both a Process (in our example, LIF) and a _SyncProtocol_ (in our example, the _LoihiProtocol_). A _LeafProcessModel_ has a single _Type_. In this tutorial `PyLifModel` has Type `PyLoihiProcessModel`. A _LeafProcessModel_ also has one or more resource _Requirements_ that specify the compute resources (for example, a CPU, a GPU, or Loihi Neurocores) or peripheral resources (like access to a camera) that are required for execution. Finally, a _LeafProcessModel_ can have one and more user-defineable _Tags_. _Tags_ can be used, among other customizable reasons, to group multiple _ProcessModels_ for a multi- _Process_ application or to distinguish between multiple _LeafProcessModel_ implementations with the same _Type_ and _SyncProtocol_. As an example, we illustrated above a `PyLoihiProcessModel` for LIF that uses floating point precision and has the tag `@tag('floating_pt')`. There also exists a `PyLoihiProcessModel` that uses fixed point precision and has behavior that is bit-accurate with LIF execution on a Loihi chip; this _ProcessModel_ is distinguished by the tag `@tag('fixed_pt')`. Together, the _Type_, _Tag_ and _Requirement_ attributes of a _LeafProcessModel_ allow users to define a _RunConfig_ that chooses which of several _LeafProcessModels_ is used to implement a _Process_ at runtime. The Core Lava Library will also provide several preconfigured _RunConfigs_. \n",
"\n",
"<img src=\"https://raw.githubusercontent.com/lava-nc/lava-nc.github.io/main/_static/images/tutorial03/fig02_processmodel_tags_reqs_syncprotocols.png\"/>"
]
},
{
"cell_type": "markdown",
"id": "d9f8b184",
"metadata": {
"tags": []
},
Expand All @@ -405,7 +293,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -419,7 +307,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 087e6c1

Please sign in to comment.