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.
In v0.5.1 we added an lazy evaluation of sol.optimize(...)
which removes
the necessity to provide an example input. The model instead gets created the
first time it gets executed.
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, 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 '''
sol_model.eval()
with torch.no_grad():
for batch in ...:
input = ...
output = sol_model(input)
...
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 arguments 'strict' and 'check_trace' are passed onto
torch.jit.trace(...) and are False by default.
|
How can I update/downgrade to another PyTorch version? | |
---|---|
Before switching version, please have a look at the compatibility list if your PyTorch version is supported by SOL. If yes, and if you are using SOL with the NEC SX-Aurora TSUBASA, you can switch PyTorch using pip3 install veda-pytorch~={VERSION} . If you are using SOL with any other device, then you can just use pip3 install torch~={VERSION} .
|
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.