From d790cbd9c43a6337cbfec474a247de193a424d6e Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Thu, 11 May 2023 06:01:27 -0400 Subject: [PATCH 1/3] no longer necessary Signed-off-by: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> --- analogvnn/parameter/PseudoParameter.py | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/analogvnn/parameter/PseudoParameter.py b/analogvnn/parameter/PseudoParameter.py index 2270f2b..41c9576 100644 --- a/analogvnn/parameter/PseudoParameter.py +++ b/analogvnn/parameter/PseudoParameter.py @@ -122,26 +122,6 @@ def __repr__(self): f', original={self.original}' \ f')' - @property - def grad(self): - """Returns the gradient of the parameter. - - Returns: - Tensor: the gradient. - """ - - return self._transformed.grad - - @grad.setter - def grad(self, grad: Tensor): - """Sets the gradient of the parameter. - - Args: - grad (Tensor): the gradient. - """ - - self._transformed.grad = grad - @property def transformation(self): """Returns the transformation. @@ -187,8 +167,8 @@ def substitute_member( """Substitutes a member of a tensor as property of another tensor. Args: - tensor_from (Any): the tensor to substitute from. - tensor_to (Any): the tensor to substitute to. + tensor_from (Any): the tensor property to substitute. + tensor_to (Any): the tensor property to substitute to. property_name (str): the name of the property. setter (bool): whether to substitute the setter. """ From 759cecb1e9c7d456d5c6639d360bc369ed4b55cf Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Mon, 15 May 2023 02:53:52 -0400 Subject: [PATCH 2/3] using ids and filtered_inputs Signed-off-by: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> --- analogvnn/graph/BackwardGraph.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/analogvnn/graph/BackwardGraph.py b/analogvnn/graph/BackwardGraph.py index 9d16686..9a04f1d 100644 --- a/analogvnn/graph/BackwardGraph.py +++ b/analogvnn/graph/BackwardGraph.py @@ -267,7 +267,6 @@ def _calculate_gradients( # noqa: C901 grad_inputs = module._call_impl_backward(*grad_outputs.inputs.args, **grad_outputs.inputs.kwargs) return ArgsKwargs.to_args_kwargs_object(grad_inputs) - grad_dict = {} inputs = module_inputs_outputs.inputs.args + list(module_inputs_outputs.inputs.kwargs.values()) outputs = [] outputs_grads = [] @@ -291,26 +290,28 @@ def _calculate_gradients( # noqa: C901 module_parameters = list(module.parameters()) inputs += module_parameters + grad_dict = {id(i): None for i in inputs} + filtered_inputs = [i for i in inputs if i is not None and i.requires_grad] out_grads = torch.autograd.grad( outputs=outputs, - inputs=inputs, + inputs=filtered_inputs, grad_outputs=outputs_grads, retain_graph=True, allow_unused=True ) for i, v in enumerate(out_grads): - grad_dict[inputs[i]] = v + grad_dict[id(filtered_inputs[i])] = v for i in module_parameters: - if grad_dict[i] is None: + if grad_dict[id(i)] is None: continue if i.grad is None: - i.grad = grad_dict[i] + i.grad = grad_dict[id(i)] else: - i.grad += grad_dict[i] + i.grad += grad_dict[id(i)] return ArgsKwargs( - args=[grad_dict[i] for i in module_inputs_outputs.inputs.args], - kwargs={key: grad_dict[value] for key, value in module_inputs_outputs.inputs.kwargs.items()} + args=[grad_dict[id(i)] for i in module_inputs_outputs.inputs.args], + kwargs={key: grad_dict[id(value)] for key, value in module_inputs_outputs.inputs.kwargs.items()} ) From 14f5240f648f13aba2f980473b67384c973c21d1 Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Mon, 15 May 2023 02:56:31 -0400 Subject: [PATCH 3/3] v1.0.5 Signed-off-by: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> --- CHANGELOG.md | 5 +++++ pyproject.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f0b445..40fba0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.0.5 (Patches for Pytorch 2.0.1) + +* Removed unnecessary `PseudoParameter.grad` property. +* Patch for Pytorch 2.0.1, add filtering inputs in `BackwardGraph._calculate_gradients`. + ## 1.0.4 * Combined `PseudoParameter` and `PseudoParameterModule` for better visibility diff --git a/pyproject.toml b/pyproject.toml index f2ac1f1..4d06222 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ py-modules = ['analogvnn'] [project] # $ pip install analogvnn name = "analogvnn" -version = "1.0.4" +version = "1.0.5" description = "A fully modular framework for modeling and optimizing analog/photonic neural networks" # Optional readme = "README.md" requires-python = ">=3.7"