To use SOL in PyTorch just import the package and optimize the model using
sol.optimize(model)
.
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)
...
This example requires the torchvision package: https://github.com/pytorch/vision/ .
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.
By default SOL only optimized the primary execution of the model model(input)
.
If you want to optimize helper functions such as model.predict(...)
you need
optimize these separately using:
model.predict = torch.compile(model.predict, backend='sol')
Optimizing helper functions requires PyTorch v2. You can also not use
sol.optimize(...)
as model.predict
is a function, not a model and
sol.optimize
will not be able to identify the correct parser automatically.
Option | Default | Effect |
---|---|---|
parser | None | compile: always uses torch.compile , jit: always uses torch.jit.script or torch.jit.trace , symbolic: always uses torch.fx.symbolic_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
|
I get strange errors when running sol.optimize(model, ...), e.g., in Huggingface Transformers. | |
---|---|
Please read this guide how to convert the transformer into a TorchScript compatible model. |
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 |
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. |
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 |
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.