This example requires the torchvision package: https://github.com/pytorch/vision/ . Please note, that SOL does not support the use of model.eval()
or model.train()
. SOL always assumes model.eval()
for running inference, and model.train()
when running training.
import torch
import sol
import torchvision.models as models
''' Optimizing Model '''
py_model = models.__dict__["alexnet"]()
input = torch.rand(32, 3, 224, 224)
# Use vdims=[True] if you plan to use changing batchsizes
sol_model = sol.optimize(py_model, [input], vdims=[True])
''' Run training '''
sol_model.train()
for batch in ...:
input, target = ...
output = sol_model(input)
loss = loss_func(output, target)
loss.backward()
...
''' Run validation '''
opt.eval()
with torch.no_grad():
for batch in ...:
input = ...
output = opt(input)
...
IMPORTANT: SOL does not provide the “num_batches_tracked” value for BatchNormalization, therefore loading the state dict with
load_state_dict(…, strict=True)
will fail in these cases!
How do I store/load a Pytorch model? | |
---|---|
For storing/loading a SOL PyTorch model, use
More information on loading/storing the weights can be found here |
I get strange errors when running sol.optimize(model, ...), e.g., in Huggingface Transformers. | |
---|---|
Huggingface Transformers are incompatible to PyTorch's
torch.jit.script(...) parser and can only be used with
torch.jit.trace(...) (see here). As torch.jit.trace(...) is much more
restricted than torch.jit.script(...) in terms of the input and
output the models we use torch.jit.script(...) as default parser.
If you encounter problems, you can try running sol.optimize(model, ...,
trace=True) to use the torch.jit.trace(...) parser instead.
But be advised, that you might need to simplify your model input/output
accordingly. Please see the PyTorch documentation for more details.
|
The SOL model returns more outputs than the PyTorch model. | |
---|---|
This error occurs, i.e., in TorchVisions Inception V3 or GoogleNet. These models return 1 output in inference and 2 outputs in training mode. SOL relies on the TorchScript parser. Unfortunately the TorchVision models are build in a way that hides the change of output behavior from TorchScript. However, you can implement this yourself as follows:
SOL currently does not support to dynamically switch between these two modes and requires to compile the model for each mode separately. |
Please refer to https://pytorch.org/docs/stable/ for how these functions are used. This documentation only contains which layers, functions and tensor functionality is currently implemented within SOL.