Editing ScriptModule parameters


I know this seems like an anti-pattern question to ask, but I was wondering if there’s a way to edit a ScriptModule’s parameters and delete references to the old object at the same time?

Currently it is not allowed to delete children of ScriptModule._parameters, which I assume is because the management of this happens in the lower C++ levels of the code.

Are there any thoughts on supporting this in the future?

For now it is at least possible to replace a value in the same ScriptModule._parameters as long as it’s of lesser size than the current value.

Example of something that will still execute (after the model is traced):

class Net(nn.Module):
    def __init__(self, num_emb):
        super(Net, self).__init__()
        self.emb = nn.Embedding(num_emb, 100)
    def forward(self, x):
        return self.emb(x)
m = Net(10000)
*trace model*
subset = torch.randn_like(m.emb.weight[:80])
m.emb.weight.data = subset

Are there any huge drawbacks to doing this, other than being anti-pattern?
My usecase is training a model, shipping it to production, then trimming it down to a subset of our most recent items before accepting queries.

Bonus question: Are there any good ways to convert a traced model back to a “normal” model after doing torch.jit.load()?

Modifying a traced model, then doing a copy() forces a “proper” re-allocate at least. :slight_smile:
Any assistance with some of the other questions would still be appreciated! But my immediate needs are at least met.

lm = torch.jit.load('m.pt')
subset = torch.randn_like(lm.emb.weight[:80])
lm.emb.weight.data = subset
tmp = lm.copy()