Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix qnn.TorchLayer attributes #4611

Merged
merged 10 commits into from
Sep 19, 2023
Merged

Fix qnn.TorchLayer attributes #4611

merged 10 commits into from
Sep 19, 2023

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Sep 18, 2023

Fixes bug #4584

torch.nn.Module has some special __getattr__ logic that we are ignoring if the qnode has been initialized. This bugfix makes it so the qnn.TorchLayer.__getattr__ uses torch.nn.Module.__getattr__ if the requested property is not on the QNode.

@albi3ro albi3ro requested a review from a team September 18, 2023 20:42
@albi3ro
Copy link
Contributor Author

albi3ro commented Sep 18, 2023

[sc-45054]

Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks pretty good, but I'm not sure what standard we want to set for __setattr__ in general so I'm asking here

pennylane/qnn/torch.py Outdated Show resolved Hide resolved
pennylane/qnn/torch.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Sep 18, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.01% ⚠️

Comparison is base (78a5852) 99.64% compared to head (e3b8fa7) 99.64%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4611      +/-   ##
==========================================
- Coverage   99.64%   99.64%   -0.01%     
==========================================
  Files         375      375              
  Lines       33398    33397       -1     
==========================================
- Hits        33280    33279       -1     
  Misses        118      118              
Files Changed Coverage Δ
pennylane/qnn/torch.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

pennylane/qnn/torch.py Outdated Show resolved Hide resolved
Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for addressing my comments, this looks great!

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comment, but not really blocking

pennylane/qnn/torch.py Show resolved Hide resolved
@albi3ro albi3ro enabled auto-merge (squash) September 19, 2023 20:54
@albi3ro albi3ro merged commit eb7766f into master Sep 19, 2023
38 checks passed
@albi3ro albi3ro deleted the fix-torch-layer-attributes branch September 19, 2023 21:23
mudit2812 pushed a commit that referenced this pull request Sep 20, 2023
Fixes bug #4584 

`torch.nn.Module` has some special `__getattr__` logic that we are
ignoring if the qnode has been initialized. This bugfix makes it so the
`qnn.TorchLayer.__getattr__` uses `torch.nn.Module.__getattr__` if the
requested property is not on the QNode.

---------

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
timmysilv added a commit that referenced this pull request Jan 5, 2024
**Context:**
Fixes #4999. We re-implemented the default `setattr` and `getattr`
dunders for `KerasLayer` (needed to modify the QNode saved on the layer)
but we should have called `super()`, because `keras.layer.Layer` has its
own custom implementation that we needed to use. Our `TorchLayer`
already calls `super` in these cases.

Note - our existing tests were passing because the weights used had
custom names ("w1", "w2", etc.), and this issue only comes up when your
weights are named "weights" (which is a rather common case, I'm sure).

**Description of the Change:**
- Replace our default-handling of `get/set-attr` dunders with a call to
`super()`
- add a test case matching the one in the bug report using the "weights"
identifier

**Benefits:**
Models using `KerasLayer` can be saved and loaded properly!

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
Reported initially in #4999, same as #4611 but for Keras
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants