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)
sol_model = sol.optimize(py_model)
''' 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)
...
Option | Default | Effect |
---|---|---|
use_torch_compile | None | True: always uses torch.compile , False: always uses torch.jit.script or torch.jit.trace , None: automatically determines which to use. Using torch.compile can add additional overhead to the execution time. |
trace | False | When not using torch.compile , enforces using torch.jit.trace . By default uses torch.jit.script . |
strict | False | When using torch.jit.trace , sets the strict argument. |
check_trace | False | When using torch.jit.trace , sets the check_trace argument. |
mode | None | When set to ‘reduce-overhead’ uses CUDAGraphs on NVIDIA GPUs. |
How can I use CUDA Graphs with SOL and PyTorch? | |
---|---|
CUDA Graphs support is still experimental in SOL! CUDA Graphs allow to significantly reduce the overhead when running GPU workloads but come with some restrictions, e.g., no changing dimensions can be used. For more information on CUDA Graphs please refer to this blog post. To use CUDA Graphs in SOL, just use:
|
I get the error `Unsupported at::Device: cuda:0`? | |
---|---|
This error is raised when SOL was unable to find the CUDA toolkit and you try to
execute a model on an NVIDIA GPU. Please verify that SOL is able to find the
CUDA toolkit by setting the |
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
|
Can I implement custom layers using SOL? | |
---|---|
Please refer to Custom Layers. |
Can I implement custom layers using SOL? | |
---|---|
Please refer to Custom Layers. |
I get NotImplementedError: Could not run 'aten::addmm.out'
with arguments from the 'VE' backend.' when trying to run an RNN on VE.
| |
---|---|
Most likely you have used torch.compile(model, backend="sol")
on an RNN model. Due to
this open issue it's impossible to compile RNNs using the torch.compile
API. Please use sol.optimize(model, use_torch_compile=False)
instead.
|
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.