Yes, you only need to copy.deepcopy your model before calling dfs if you dont want to modify it inplace. modify_module is your function that does with a given child module what you want, for example you can add more child modules with .add_module, or modify the forward behavior by registering forward pre hooks (executed before forward call) and forward hooks (executed after forward call see here 1.