From 693ea874807d335be0bc68d684e61b4f42cf8113 Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Tue, 11 Jul 2023 04:45:35 -0400 Subject: [PATCH 01/17] allow not tensor args --- analogvnn/graph/ForwardGraph.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/analogvnn/graph/ForwardGraph.py b/analogvnn/graph/ForwardGraph.py index d394f58..50ccb42 100644 --- a/analogvnn/graph/ForwardGraph.py +++ b/analogvnn/graph/ForwardGraph.py @@ -73,8 +73,15 @@ def calculate( inputs = (inputs,) if not self.graph_state.use_autograd_graph and is_training: + value_tensor = False for i in inputs: + if not isinstance(i, torch.Tensor): + continue i.requires_grad = True + value_tensor = True + + if not value_tensor: + raise ValueError('At least one input must be a tensor.') input_output_graph = self._pass(self.INPUT, *inputs) if is_training: @@ -102,8 +109,14 @@ def _pass(self, from_node: GraphEnum, *inputs: Tensor) -> Dict[GraphEnum, InputO if module != from_node: inputs = self.parse_args_kwargs(input_output_graph, module, predecessors) if not self.graph_state.use_autograd_graph: - inputs.args = [self._detach_tensor(i) for i in inputs.args] - inputs.kwargs = {k: self._detach_tensor(v) for k, v in inputs.kwargs.items()} + inputs.args = [ + self._detach_tensor(i) if isinstance(i, torch.Tensor) else i + for i in inputs.args + ] + inputs.kwargs = { + k: self._detach_tensor(v) if isinstance(v, torch.Tensor) else v + for k, v in inputs.kwargs.items() + } input_output_graph[module] = InputOutput(inputs=inputs) if isinstance(module, GraphEnum): From becd0d9894549913708e35fed429d3e5a174e7f0 Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Tue, 11 Jul 2023 04:46:27 -0400 Subject: [PATCH 02/17] bug fix --- README.md | 4 +++- analogvnn/graph/AccumulateGrad.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7916090..35f459f 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,9 @@ Documentation: [https://analogvnn.readthedocs.io/](https://analogvnn.readthedocs ## Abstract -![3 Layered Linear Photonic Analog Neural Network](https://github.com/Vivswan/AnalogVNN/raw/release/docs/_static/analogvnn_model.png) +![3 Layered Linear Photonic Analog Neural Network](docs/_static/analogvnn_model.png) + +[//]: # (![3 Layered Linear Photonic Analog Neural Network](https://github.com/Vivswan/AnalogVNN/raw/release/docs/_static/analogvnn_model.png)) **AnalogVNN** is a simulation framework built on PyTorch which can simulate the effects of optoelectronic noise, limited precision, and signal normalization present in photonic diff --git a/analogvnn/graph/AccumulateGrad.py b/analogvnn/graph/AccumulateGrad.py index 2b656e6..6f4127f 100644 --- a/analogvnn/graph/AccumulateGrad.py +++ b/analogvnn/graph/AccumulateGrad.py @@ -1,4 +1,4 @@ -from typing import Dict, Union, Callable +from typing import Dict, Union, Callable, List import torch from torch import nn @@ -9,6 +9,10 @@ __all__ = ['AccumulateGrad'] +def _get_index(tensor: torch.Tensor, tensor_list: List[torch.Tensor]) -> int: + return [(i.shape == tensor.shape and torch.all(torch.eq(i, tensor))) for i in tensor_list].index(True) + + class AccumulateGrad: """AccumulateGrad is a module that accumulates the gradients of the outputs of the module it is attached to. @@ -74,14 +78,14 @@ def __call__( # noqa: C901 if forward_out_arg is True and isinstance(forward_in_arg, int) and not isinstance(forward_in_arg, bool): forward_inputs = forward_input_output_graph[predecessor].inputs.args forward_outputs = forward_input_output_graph[self.module].outputs.args - forward_out_arg = forward_inputs.index(forward_outputs[forward_in_arg]) + forward_out_arg = _get_index(forward_outputs[forward_in_arg], forward_inputs) grad_output = grad_output[forward_out_arg] # 7 if forward_out_arg is True and isinstance(forward_in_kwarg, str): forward_inputs = forward_input_output_graph[predecessor].inputs.args forward_outputs = forward_input_output_graph[self.module].outputs.kwargs - forward_out_arg = forward_inputs.index(forward_outputs[forward_in_kwarg]) + forward_out_arg = _get_index(forward_outputs[forward_in_kwarg], forward_inputs) grad_output = grad_output[forward_out_arg] # 1 @@ -92,7 +96,7 @@ def __call__( # noqa: C901 if forward_inputs[i] not in forward_outputs: continue - value_index = forward_outputs.index(forward_inputs[i]) + value_index = _get_index(forward_inputs[i], forward_outputs) if value_index not in grad_inputs_args: grad_inputs_args[value_index] = torch.zeros_like(grad_output[i]) grad_inputs_args[value_index] += grad_output[i] @@ -103,7 +107,7 @@ def __call__( # noqa: C901 forward_inputs = forward_input_output_graph[predecessor].inputs.args forward_outputs = forward_input_output_graph[self.module].outputs.kwargs for i in forward_outputs: - value_index = forward_inputs.index(forward_outputs[i]) + value_index = _get_index(forward_outputs[i], forward_inputs) if i not in grad_inputs_kwargs: grad_inputs_kwargs[i] = torch.zeros_like(grad_output[value_index]) From c1b04325695842be033b3751b2ca1ae64ff55556 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Wed, 16 Aug 2023 04:18:51 +0000 Subject: [PATCH 03/17] fix: requirements/requirements-docs.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-TORNADO-5840803 --- requirements/requirements-docs.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/requirements-docs.txt b/requirements/requirements-docs.txt index b5ead38..8b24c37 100644 --- a/requirements/requirements-docs.txt +++ b/requirements/requirements-docs.txt @@ -11,3 +11,4 @@ sphinx-notfound-page sphinx-inline-tabs sphinxext-opengraph sphinxcontrib-katex # math +tornado>=6.3.3 # not directly required, pinned by Snyk to avoid a vulnerability From 6eaa0abf6825236dad626cff710132a662dd43e2 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Wed, 16 Aug 2023 09:53:44 +0000 Subject: [PATCH 04/17] fix: requirements/requirements-docs.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-TORNADO-5537286 - https://snyk.io/vuln/SNYK-PYTHON-TORNADO-5840803 From bdef1c48eaefdb7a547454092f846a5544bbbda2 Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Wed, 16 Aug 2023 05:59:39 -0400 Subject: [PATCH 05/17] SNYK-PYTHON-NUMPY-2321964 Signed-off-by: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 61ad414..ede89e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ torch torchvision torchaudio -numpy +numpy>=1.22.2 scipy networkx importlib-metadata; python_version < '3.8' From 34ac864c2d5f4c5d6c86d785639f9a4541f787d0 Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Wed, 16 Aug 2023 06:02:04 -0400 Subject: [PATCH 06/17] Update requirements.txt Signed-off-by: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 61ad414..ede89e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ torch torchvision torchaudio -numpy +numpy>=1.22.2 scipy networkx importlib-metadata; python_version < '3.8' From 95e0a73c15ab534e71655b071f2da46430a3258d Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Wed, 16 Aug 2023 06:04:09 -0400 Subject: [PATCH 07/17] SNYK-PYTHON-WHEEL-3180413 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 95496fc..e135a42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ # pyproject-build [build-system] -requires = ["wheel", "setuptools>=61.0.0", "flit_core >=3.2,<4"] +requires = ["wheel>=0.38.0", "setuptools>=61.0.0", "flit_core>=3.2,<4"] build-backend = "flit_core.buildapi" [tool.flit.module] From 65febfc7d5714b8ae1eccacdc51dc1669516154d Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Wed, 16 Aug 2023 06:06:27 -0400 Subject: [PATCH 08/17] CVE-2022-40898 --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 61ad414..97ca9a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ networkx importlib-metadata; python_version < '3.8' # Full +wheel>=0.38.0 tensorflow>=2.0.0 tensorboard>=2.0.0 torchinfo From c29a916880964f26b0615351d225aaf717c63805 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Wed, 20 Sep 2023 18:56:57 +0000 Subject: [PATCH 09/17] fix: requirements/requirements-dev.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-SETUPTOOLS-3180412 --- requirements/requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/requirements-dev.txt b/requirements/requirements-dev.txt index 30603a0..04d60e8 100644 --- a/requirements/requirements-dev.txt +++ b/requirements/requirements-dev.txt @@ -1,6 +1,6 @@ # Development flit -setuptools>=61.0.0 +setuptools>=65.5.1 build # building the package {pyproject-build} twine # to publish on pypi {twine upload --repository-url=https://test.pypi.org/legacy/ dist/*} {twine upload dist/*} johnnydep # to see dependencies {johnnydep } From 7916dafe2c4d5094a76eb86b4abd85851ba345de Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Wed, 27 Sep 2023 06:29:58 +0000 Subject: [PATCH 10/17] fix: requirements/requirements-docs.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-PILLOW-5918878 --- requirements/requirements-docs.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/requirements-docs.txt b/requirements/requirements-docs.txt index 8b24c37..a4a7d56 100644 --- a/requirements/requirements-docs.txt +++ b/requirements/requirements-docs.txt @@ -12,3 +12,4 @@ sphinx-inline-tabs sphinxext-opengraph sphinxcontrib-katex # math tornado>=6.3.3 # not directly required, pinned by Snyk to avoid a vulnerability +pillow>=10.0.1 # not directly required, pinned by Snyk to avoid a vulnerability From 895bc26d438cc8d7822b34d8b19fed3b122b883a Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Wed, 27 Sep 2023 07:36:55 +0000 Subject: [PATCH 11/17] fix: requirements.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-PILLOW-5918878 --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 6492ffb..de488f6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ torchinfo # conda install graphviz python-graphviz pydot pydotplus python-dotenv # conda install --channel conda-forge pygraphviz graphviz +pillow>=10.0.1 # not directly required, pinned by Snyk to avoid a vulnerability From a7dd53c5e4706b2c7b3e0a928f68c2063a1fa8ba Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Wed, 27 Sep 2023 15:19:06 +0000 Subject: [PATCH 12/17] fix: requirements/requirements-docs.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-PILLOW-5918878 From 548ccf911faaceae3d4d267d11b46d0e1add08a9 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Fri, 27 Oct 2023 08:09:47 +0000 Subject: [PATCH 13/17] fix: requirements.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-WERKZEUG-6035177 --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index de488f6..95f993f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ torchinfo # conda install --channel conda-forge pygraphviz graphviz pillow>=10.0.1 # not directly required, pinned by Snyk to avoid a vulnerability +werkzeug>=3.0.1 # not directly required, pinned by Snyk to avoid a vulnerability From 9afd7f8b8d537069e5088c8a5948e00218f3db59 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Thu, 2 Nov 2023 05:25:42 +0000 Subject: [PATCH 14/17] fix: requirements/requirements-docs.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-TORNADO-6041512 From 9d0e122434b38f2300a9cdf20734db6cf0dc87aa Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Fri, 3 Nov 2023 20:29:18 +0000 Subject: [PATCH 15/17] fix: requirements/requirements-docs.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-PILLOW-6043904 From 561f8d3b2b4ff543cce5204be4ecba4d8029cc38 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Fri, 3 Nov 2023 20:52:47 +0000 Subject: [PATCH 16/17] fix: requirements.txt to reduce vulnerabilities The following vulnerabilities are fixed by pinning transitive dependencies: - https://snyk.io/vuln/SNYK-PYTHON-WERKZEUG-6041510 --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index de488f6..95f993f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ torchinfo # conda install --channel conda-forge pygraphviz graphviz pillow>=10.0.1 # not directly required, pinned by Snyk to avoid a vulnerability +werkzeug>=3.0.1 # not directly required, pinned by Snyk to avoid a vulnerability From 38a966c4d6266dc59b3f44e3d01d6fd8bf8a61d9 Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Wed, 22 Nov 2023 01:40:20 -0500 Subject: [PATCH 17/17] GeLU backward eq fix --- CHANGELOG.md | 3 +++ analogvnn/nn/activation/Gaussian.py | 2 +- pyproject.toml | 2 +- requirements.txt | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3058eb4..724fcd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 1.0.7 +* Fixed `GeLU` backward function equation. + ## 1.0.6 * `Model` is subclass of `BackwardModule` for additional functionality. diff --git a/analogvnn/nn/activation/Gaussian.py b/analogvnn/nn/activation/Gaussian.py index 2d0661e..d18ec81 100644 --- a/analogvnn/nn/activation/Gaussian.py +++ b/analogvnn/nn/activation/Gaussian.py @@ -68,6 +68,6 @@ def backward(self, grad_output: Optional[Tensor]) -> Optional[Tensor]: x = self.inputs grad = (1 / 2) * ( - (1 + torch.erf(x / math.sqrt(2))) + x * ((2 / math.sqrt(math.pi)) * torch.exp(-torch.pow(x, 2))) + (1 + torch.erf(x / math.sqrt(2))) + x * (math.sqrt(2 * math.pi) * torch.exp(-torch.pow(x, 2) / 2)) ) return grad_output * grad diff --git a/pyproject.toml b/pyproject.toml index e135a42..e04526a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ where = ["analogvnn"] [project] # $ pip install analogvnn name = "analogvnn" -version = "1.0.6" +version = "1.0.7" description = "A fully modular framework for modeling and optimizing analog/photonic neural networks" readme = "README.md" requires-python = ">=3.7" diff --git a/requirements.txt b/requirements.txt index 95f993f..82526c1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ ---extra-index-url https://download.pytorch.org/whl/cu118 +--extra-index-url https://download.pytorch.org/whl/cu121 torch torchvision torchaudio