Modify TorchScript model parameters using libtorch C++ API

Not a Contribution.

Is it possible to access and modify the parameters of a TorchScript model using the libtorch C++ API?

Suppose for eg, that we want to load the parameters in a std vector/map, perform some operations on them and write them back to the TorchScript model. I believe this is possible using the torch::nn::Module API, but if we do not have the model definition in C++, would something like this be doable using the TorchScript model?


Hey @ptrblck, any pointers? Or know anyone who could help? :slight_smile:

I’m not entirely sure, as I haven’t experimented with such a use case, but based on this Python behavior:

model = nn.Linear(10, 10)
traced = torch.jit.script(model)


x = torch.randn(1, 10)
out = traced(x)

with torch.no_grad():
out = traced(x)

I would guess you could also directly manipulate the parameters of the scripted models.
Are you able to access these parameters or are you getting an error?

Thanks for your response.

I tried a similar approach (something like this), but I can only get const references to the parameters, so cannot manipulate their values for a scripted model. The manipulation only works with a torch::nn::Module instance.

Is there any API in libtorch that would get me either pointers or non-const references to a scripted models’ parameters?

Ah OK, I didn’t know that. As I haven’t used this approach before, maybe @tom has experimented with a similar use case or would know if this use case is supported at all?

@pmandke Do you solve it ? I met the same question