SOL deployment compiles a trained framework model into a standalone inference
library. Unlike sol.optimize(), which keeps the
model inside your Python process and compiles it just in time (JIT),
sol.deploy() exports the model into a standalone library with C/C++ and Python
bindings.
Using sol.deploy work similar to sol.optimize with only few differences.
def deploy(
# identical to sol.optimize
model, args=[], kwargs={}, *, vdims = None, determinism = None,
# sol.deploy specific
device=None, call="predict", name=None, path=".",
**fwargs
):
...
The used model can be of any supported model type that is also supported by
sol.optimize. However, model cannot be a model that was previously optimized
via sol.optimize. If you want to export a model that was trained using SOL,
you first need to copy the trained parameters back into the original model.
sol_model = sol.optimize(pytorch_model, ...)
...
pytorch_model.load_state_dict(sol_model.state_dict())
# or
keras_model.set_weights(sol_model.weights())
Further, in contrast to sol.optimize the args and kwargs are not optional
and are needed to infer the correct shapes and data types. The sol.deploy
specific parameters control the following features:
| Param | Default | Description |
|---|---|---|
| call | ‘predict’ | Name used in C/C++ interface, e.g., sol_predict(...). |
| device | None | Sets the device type, e.g., ‘x86’, ’ve’ or ’nvidia’. If is None, SOL automatically determines the device type from the input data. |
| name | None | Name of the exported library name.so, name.h, name.py, … |
| path | ‘.’ | Folder all files get exported to. Gets created if does not exist. |
| fwargs | {} | Can be used to pass on device specific parameters to SOL. Please refer to the device pages for all available options. |
Device specific parameters can just be appended to the command, e.g.,
sol.deploy(..., nvidia_cc=50).
Here a full example for exporting a Resnet50 from PyTorch:
import torchvision
import torch
model = torchvision.models.resnet50()
model.eval()
import sol
sol.deploy(
model,
[torch.empty(1, 3, 224, 224)],
path='export',
name='resnet50',
call='predict'
)
In the rest of this documentation we assume path='export', name='resnet50
and call='predict'.
SOL deployment generates a C, C++ and Python interface.
To use the C-API, you need to include #include "export/resnet50.h" and link
against -L export -lresnet50. If name does not start with lib you might need
to specify it as -l:resnet50.so for the compiler to find the library.
Depending if you have a hybrid device (e.g. GPUs) SOL either generated a host-
or offloading-API. It’s important to notice, that SOL flattens the input and
outputs, so any nested {'output': [A, B, C], 'other': D} output is returned as
linear list {A, B, C, D}!
The host-API has the following function calls.
typedef void* sol_predict_result;
SOL_API sol_predict_handle* sol_predict_create ();
SOL_API void sol_predict_destroy(sol_predict_handle* hnd);
SOL_API sol_predict_result sol_predict (sol_predict_handle* hnd, void* I2_Output);
SOL_API void sol_predict_free (void* ptr);
In order to use the API the first need to create a model handle that loads all model parameters into memory, and then can immediately be used to execute the model.
Be aware that SOL expects the pointers to be of correct shape on the correct
device. The returned tensor pointers need to be freed using the
sol_predict_free(...) method.
If your model returns multiple outputs sol_predict_result will be an array of
void*. Further, if your model uses vdims, you need to provide the indices to
sol_predict(...) function call. If you model returns vdims, e.g, in models
that use tensor[bool_tensor], these are returned by sol_predict_vdims(...).
Please have a look at the generated resnet50.h file for details on the exact
call convention.
Similar offload-API has many similarities to the host-API, but differs in two aspects:
typedef void* sol_predict_result;
SOL_API sol_predict_handle* sol_predict_create (const int device_idx, void* stream);
SOL_API void sol_predict_destroy (sol_predict_handle* hnd);
SOL_API sol_predict_result sol_predict (sol_predict_handle* hnd, void* I2_Output, const int offload, void* stream);
SOL_API void sol_predict_free (void* ptr, const int device_idx, void* stream);
SOL_API void sol_predict_free_host (void* ptr);
SOL_API int sol_predict_device_idx(sol_predict_handle* hnd);
First, sol_predict_create(...) and sol_predict_free(...) expect a stream
and device_idx. stream can either be 0 or a device compatible stream,
e.g., VEDAstream, CUstream or cudaStream_t.
Further, sol_predict(...) has an offload option. If this is 0, SOL expects
the input pointers to be on the target device. Else the pointers are expected to
be host pointers, and SOL will automatically offload the data to the target
device.
It’s important, that pointers returned by offload==0 are freed using
sol_predict_free(...) and otherwise by sol_predict_free_host(...)!
The C++ API follows the same principles for the host-API. To access it you need
to include #include "export/resnet50.hpp" into your C++ code.
namespace sol {
class predict final : public sol_predict_handle {
...
public:
using Result = sol_predict_result;
predict (const predict&) = delete;
predict ();
Result operator()(void* x);
virtual ~predict ();
void free (void* ptr);
};
}
And the offload-API.
namespace sol {
class predict final : public sol_predict_handle {
...
public:
using Result = sol_predict_result;
predict (const predict&) = delete;
predict (const int deviceIdx, void* stream);
Result operator()(void* x, const int offload, void* stream);
virtual ~predict ();
void free (void* ptr, void* stream);
void freeHost (void* ptr);
int deviceIdx (void);
};
}
Both behave identical to the C-API, with the difference, that the C++ supports RAII, so you can use it as:
sol::predict model(0, stream);
auto output = model(input, 0, stream);
and it automatically gets destroyed after you exist the function scope.
To use the Python-API you either need to copy the exported model to your
Python’s site-packages, set a correct PYTHONPATH=... env var, or to set
import sys
sys.path.insert(0, 'path/to/deployed/model')
to ensure that the exported model’s Python module can be found. Then you can just execute the following lines to execute your model.
import export
model = export.resnet50(device_idx=0, stream=None) # parameters are optional
output = model(input)
input can either be a numpy.ndarray or a torch.Tensor. In case of
numpy.ndarray the data always gets automatically offloaded to the target
device, in case of torch.Tensor SOL will not copy the data if it is already on
the correct device!
The output is always of the same type and located on the same device as the
input. So if your data is on ve:1 and your model is loaded onto cuda:0, SOL
will copy the data to the host, execute the model in offloading-mode, and copies
back the result to ve:1.
SOL generates all previously mentioned APIs, but not all files are required in all application scenarios. See requirement in following table.
| File | C-API | C++-API | Python-API | Description |
|---|---|---|---|---|
| export/resnet18.h | X | X | - | C-API header |
| export/resnet18.hpp | - | X | - | C++-API header |
| export/resnet18.sol_weights | X | X | X | Exported model weights |
| export/resnet18.so | X | X | X | Exported model implementation |
| export/__init__.py | - | - | X | Loads Python API |
| export/resnet18.py | - | - | X | Python API implementation |
| export/resnet18_torch.cpp | - | - | X | Extension required to integrate into PyTorch |
SOL copies relevant libraries that are bundled with SOL (e.g. VEDNN) to the
deployment folder. System libraries such as CUDNN or MKL are not copied and need
to be manually installed or be populated via LD_LIBRARY_PATH. You can check
with ldd resnet18.so which dependencies are required, and where they are
located in your system:
ldd export/resnet18.so
linux-vdso.so.1 (0x00007f151a383000)
libcuda.so.1 => /lib64/libcuda.so.1 (0x00007f15142bd000)
libcudnn.so.9 => /venv/lib/python3.12/site-packages/nvidia/cudnn/lib/libcudnn.so.9 (0x00007f151409d000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f1513e99000)
libcudart.so.12 => /usr/local/cuda/lib64/libcudart.so.12 (0x00007f1513be5000)
libsol-dnn-cublaslt-0.9.0-12.so => /export/libsol-dnn-cublaslt-0.9.0-12.so (0x00007f151a306000)
libsol-dnn-cudnn_v9-0.9.0-9.24.so => /export/libsol-dnn-cudnn_v9-0.9.0-9.24.so (0x00007f151a2e2000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f1513850000)
libm.so.6 => /lib64/libm.so.6 (0x00007f15134ce000)
libgomp.so.1 => /lib64/libgomp.so.1 (0x00007f1513296000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f151307e000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f1512e5e000)
libc.so.6 => /lib64/libc.so.6 (0x00007f1512a88000)
/lib64/ld-linux-x86-64.so.2 (0x00007f151a156000)
librt.so.1 => /lib64/librt.so.1 (0x00007f1512880000)
libcublasLt.so.12 => /usr/local/cuda/lib64/libcublasLt.so.12 (0x00007f14e058d000)