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 unwrap model #9480

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions nemo/collections/nlp/parts/mixins/nlp_adapter_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ def _get_all_keys(
"""
Returns all the keys in the model
"""
k = [n for n, p in self._unwrap_model().named_parameters()]
k = [n for n, p in self._unwrap_model().named_parameters(prefix="model")]
b = [
n
for n, p in self._unwrap_model().named_buffers()
if n.replace("model.module.", "model.", 1) in self._unwrap_model().state_dict().keys()
for n, p in self._unwrap_model().named_buffers(prefix="model")
if n.replace("model.module.", "model.", 1) in self._unwrap_model().state_dict(prefix="model.").keys()
]
# we include buffers because ptuning representations are cached in a buffer and saved to state_dict for inference time use.
return set(k + b)
Expand Down Expand Up @@ -292,13 +292,13 @@ def setup_optimizer_param_groups(self):
self.freeze(training=True) # Freeze the entire model
if not self.ptuning_only_and_non_first_stage:
opt_params = []
for _, module in self._unwrap_model().named_modules():
for _, module in self._unwrap_model().named_modules(prefix="model"):
if isinstance(module, AdapterModuleMixin) and module.is_adapter_available():
module.set_enabled_adapters(enabled=True)
module.unfreeze_enabled_adapters() # selectively unfreeze the adapter modules.
opt_params += [p for p in module.parameters() if p.requires_grad]

for name, param in self._unwrap_model().named_parameters():
for name, param in self._unwrap_model().named_parameters(prefix="model"):
if name in self.tunable_base_param_keys:
param.requires_grad = True
opt_params += [param]
Expand Down Expand Up @@ -397,11 +397,11 @@ def get_peft_state_dict(self):
"""
Gets the keys associated with the adapters only.
"""
state_dict = self._unwrap_model().state_dict()
state_dict = self._unwrap_model().state_dict(prefix="model.")
peft_state_dict = {}
for k in self.adapter_keys.union(self.tunable_base_param_keys):
# state_dict keys needs to be in non-O2 format and will be corrected in PEFTSaveRestoreConnector if O2=True
new_k = k.replace("module.", "", 1)
new_k = k.replace("model.module.", "model.", 1)
peft_state_dict[new_k] = state_dict[new_k]
return peft_state_dict

Expand Down
Loading