Skip to content

Commit

Permalink
Restore _init_weights value in no_init_weights (huggingface#18504)
Browse files Browse the repository at this point in the history
* Recover _init_weights value in no_init_weights

For potential nested use. 
In addition, users might modify private no_init_weights as well.

* Apply suggestions from code review

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>

* Remove private variable change check

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
  • Loading branch information
2 people authored and JingyaHuang committed Aug 11, 2022
1 parent a22ca6e commit 4c522b3
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ def no_init_weights(_enable=True):
TODO(Patrick): Delete safety argument `_enable=True` at next major version. .
"""
global _init_weights
old_init_weights = _init_weights
if _enable:
_init_weights = False
try:
yield
finally:
_init_weights = True
_init_weights = old_init_weights


try:
Expand Down

0 comments on commit 4c522b3

Please sign in to comment.