I am attempting to search for good branches in a model by dynamically adding a branch in the forward function by editing the forward function ast, unparsing the updated ast and writing it back to the file where the model is stored, and making a new object based on the updated model, with the old weights reloaded. But I can’t get the model to use the new forward function unless the process is completely shutdown and manually restarted. Is it even possible to edit a model in such a manner, or to reload its forward function at runtime?
An example workflow of how I’m attempting it is:
- Have a model ModelA in models.py
- In training.py, train the model
- In generation.py, take a reference to the model, get the ast of the model, and edit the forward function
- Unparse the edited ast and rewrite to models.py
- Save the weights of the model
- Reload models.py with importlb.reload
- Create a new model object
- Load the saved weights to the new object
- Train again to train the branch layers
After using importlib.reload, the model still has the old forward function in memory and does not use the updated one unless I kill the script and restart it, but I need to reload during runtime without killing the script.