Unverified Commit ee112246 authored by Michael Benayoun's avatar Michael Benayoun Committed by GitHub
Browse files

FX submodule naming fix (#13016)



Changed the way dynamically inserted submodules are named and the method used to insert them
Authored-by: default avatarMichael Benayoun <michael@huggingface.co>
parent 9870093f
...@@ -238,6 +238,10 @@ class HFTracer(Tracer): ...@@ -238,6 +238,10 @@ class HFTracer(Tracer):
clone(**inputs) clone(**inputs)
# Useful because sometime the config is changed at inference time, for instance for
# classification tasks where config.problem_type can be set.
model.config = clone.config
_reset_tensor_methods(original_methods) _reset_tensor_methods(original_methods)
self.recorded_methods = { self.recorded_methods = {
...@@ -266,27 +270,14 @@ class HFTracer(Tracer): ...@@ -266,27 +270,14 @@ class HFTracer(Tracer):
""" """
Helper method which tries to insert a module that was not declared as submodule. Helper method which tries to insert a module that was not declared as submodule.
""" """
# First, retrieve the parent module. idx = 0
if self.prev_module is None: mod_name = mod.__class__.__name__.lower()
return None path = f"{mod_name}_{idx}"
parent_path = self.prev_module.rsplit(".", 1)[0] while hasattr(self.root, path):
parent_mod = None path = f"{mod_name}_{idx}"
for path, module in self.root.named_modules(): idx += 1
if path == parent_path:
parent_mod = module self.root.add_module(path, mod)
break
if parent_mod is None:
return None
# If retrieving the parent module was possible, set the module not declared as a submodule
# as a parent module attribute.
path = None
for var_name, var_val in inspect.currentframe().f_back.f_locals.items():
if mod is var_val:
setattr(parent_mod, var_name, mod)
path = f"{parent_path}.{var_name}"
break
return path return path
def path_of_module(self, mod: nn.Module) -> str: def path_of_module(self, mod: nn.Module) -> str:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment