-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplified docs and added example page (#116)
* Simplified docs and added example page * Fixed links in README * General improvements
- Loading branch information
Showing
10 changed files
with
212 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Developing JAX extensions | ||
|
||
JAX is a popular deep learning framework that more and more of the NIR-supported libraries are built on. | ||
For PyTorch, we have built the [`nirtorch` package](https://github.com/neuromorphs/nirtorch), but *no such package exists for JAX*. | ||
If you're interested in developing such a package, please reach out to us! | ||
Either on [Discord](https://discord.gg/JRMRGP9h3c) or by [opening an issue](https://github.com/neuromorphs/NIR/issues). |
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,69 @@ | ||
# Developing PyTorch extensions | ||
|
||
PyTorch is a popular deep learning framework that many of the NIR-supported libraries are built on. | ||
We have built the [`nirtorch` package](https://github.com/neuromorphs/nirtorch) to make it easier to develop PyTorch extensions for the NIR-supported libraries. | ||
`nirtorch` helps you write PyTorch code that (1) exports NIR models from PyTorch and (2) imports NIR models into PyTorch. | ||
|
||
## Exporting NIR models from PyTorch | ||
Exporting a NIR model requires two things: exporting the model's nodes and edges. | ||
|
||
### Exporting edges | ||
Exporting edges is slightly complicated because PyTorch modules can have multiple inputs and outputs. | ||
And because PyTorch modules are connected via function calls, which only happen at runtime. | ||
Therefore, we need to trace the PyTorch module to get the edges with some sample input. | ||
Luckily, `nirtorch` package helps you do exactly that. | ||
It works behind the scenes, but you can read about it in the [`to_nir.py` file in `nirtorch`](https://github.com/neuromorphs/NIRTorch/blob/main/nirtorch/to_nir.py#L11). | ||
|
||
### Exporting nodes | ||
The only thing we really have to do to use `nirtorch` is to export modules. | ||
Since all PyTorch modules inherit from the `torch.nn.Module` class, exporting the nodes is straightforward: we simply need a function that looks at a PyTorch module and returns the corresponding NIR node. | ||
Assume this is done in a function called `export_node`. | ||
|
||
```python | ||
import nir | ||
import torch | ||
|
||
class MyModule(torch.nn.Module): | ||
weight: torch.Tensor | ||
bias: torch.Tensor | ||
|
||
|
||
def export_node(module: torch.nn.Module) -> Node: | ||
# Export the module to a NIR node | ||
if isinstance(module, MyModule): | ||
return nir.Linear(module.weight, module.bias) | ||
... | ||
``` | ||
This example converts a custom Linear module to a NIR Linear node. | ||
|
||
### Putting it all together | ||
The following code is a snippet taken from the [Norse library](https://github.com/norse/norse) that demonstrates how to export custom PyTorch models to a NIR using the `nirtorch` package. | ||
Note that we only have to declare the `export_node` function for each custom module we want to export. | ||
The edges are traced automatically by the `nirtorch` package. | ||
|
||
```python | ||
def _extract_norse_module(module: torch.nn.Module) -> Optional[nir.NIRNode]: | ||
if isinstance(module, LIFBoxCell): | ||
return nir.LIF( | ||
tau=module.p.tau_mem_inv, | ||
v_th=module.p.v_th, | ||
v_leak=module.p.v_leak, | ||
r=torch.ones_like(module.p.v_leak), | ||
) | ||
elif isinstance(module, torch.nn.Linear): | ||
return nir.Linear(module.weight, module.bias) | ||
elif ... | ||
|
||
return None | ||
|
||
def to_nir( | ||
module: torch.nn.Module, sample_data: torch.Tensor, model_name: str = "norse" | ||
) -> nir.NIRNode: | ||
return extract_nir_graph( | ||
module, _extract_norse_module, sample_data, model_name=model_name | ||
) | ||
``` | ||
|
||
## Importing NIR models into PyTorch | ||
Importing NIR models into PyTorch with `nirtorch` is also straightforward. | ||
Assuming you have a NIR graph in the Python object `nir_graph` (see [Usage](#usage)) |
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,33 @@ | ||
# Code examples | ||
|
||
NIR can be used to *export* or *import* models. | ||
*Exporting* is when you convert a model from a source platform to NIR, and *importing* is when you convert a model from NIR to a target platform. | ||
One typical workflow is to *export* a model from a simulator and *import* it to a hardware platform. | ||
|
||
In the menu, you see examples for how to use NIR with your favorite framework. | ||
But note that some frameworks only support importing or exporting. | ||
|
||
## Writing to and reading from files with NIR | ||
While NIR is typically integrated into your favorite framework, NIR supports writing to and reading from files directly. | ||
This is useful when you want to send a model over email, store it for later, or share it with a colleague. | ||
|
||
### Writing to a file | ||
To write a model to a file, use the `nir.write` function. | ||
Note that this requires you to provide a NIR model, so you need to find a way to convert your model to NIR within your framework. | ||
The `nir.write` function takes two arguments: the file path and the model to write. | ||
```python | ||
import nir | ||
my_nir_graph = ... | ||
nir.write("my_graph.nir", my_model) | ||
``` | ||
|
||
### Reading from a file | ||
To read a model from a file, use the `nir.read` function. | ||
This function takes a single argument: the file path. | ||
```python | ||
import nir | ||
imported_graph = nir.read("my_graph.nir") | ||
``` | ||
|
||
This gives you a NIR model, which then needs to be converted to your framework's model format. | ||
The NIR graph itself is just a data structure. |
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 @@ | ||
# Test |
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
Oops, something went wrong.