Try to delay the replacement, as you are currently manipulating the model while also iterating its named_modules. To do so, store the module_name in a separate list and perform the replacement after the initial loop.