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()
# You cannot initialize the optimizer at this point. You need to wait until
# you have executed the model at least once, so SOL has compiled it.
optimizer = None
for batch in ...:
input, target = ...
output = sol_model(input)
loss = loss_func(output, target)
# After running the model once, you can safely initialize the optimizer
if optimizer is None:
optimizer = torch.optim.Adam(sol_model.parameters(), ...)
optimizer.zero_grad()
loss.backward()
optimizer.step()
...
''' 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 |
Can I use torch.compile(...) with SOL? | |
---|---|
Yes, with SOL ≥ v0.5.2 and PyTorch ≥ 2.0 you can use
torch.compile(model, backend='sol') with SOL! But it provides less
features than using sol.optimize(...) , e.g., you cannot specify the
vdims . Instead vdims=[True] is used by default. You
also need to manually import import sol.pytorch to ensure that SOL
is correctly registered as backend into PyTorch. This support is still
experimental!
|
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 to
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. |
How can I use Pytorch Lightning with SOL? | |
---|---|
You can just pass your Pytorch Lightning model to SOL's `sol.optimize(...)` method.
|
Can I implement custom layers using SOL? | |
---|---|
Please refer to Custom Layers. |
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.