Fix Transformer ignoring a custom encoder or decoder with no parameters - #3962
Open
ayaangazali wants to merge 1 commit into
Open
Fix Transformer ignoring a custom encoder or decoder with no parameters#3962ayaangazali wants to merge 1 commit into
ayaangazali wants to merge 1 commit into
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Transformerpicks its submodules withcustom_encoder or TransformerEncoder(...). SinceModulesubclassesdict, a module that holds no arrays is an empty dict and therefore falsy, so a parameterless custom encoder or decoder gets silently thrown away and the default one is built instead:Same for
custom_decoder. It only shows up when the custom module has no parameters of its own, so a custom encoder that happens to hold a weight works fine and this looks inconsistent rather than broken. Switched both to an explicitis not Nonecheck.Added a small test for it. It fails on main with the current truthiness check and passes with this change.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes(
test_nn.pypasses; happy to drop the test if you would rather keep this to the one line change)context on me: freshman contributor, and i lean on Claude Code when hunting for things like this, but i confirmed the empty-module-is-falsy behaviour myself and checked that a custom module holding a parameter behaves differently, which is what made it click. if you would rather express the fix as a conditional expression than an if/else, say the word.