AOTInductor External Weight Storage and Weight Streaming Update A new GitHub example demonstrates how to store AOTInductor model weights outside the shared library and update them at inference runtime in a thread-safe manner, addressing the lack of documentation for advanced AOTInductor features. The example, created by Lei Mao, sets aot_inductor.package_constants_in_so to False and freezing to False to keep weights updatable, and is available at https://github.com/leimao/AOTInductor-External-Weight-Storage-Weight-Streaming-Update-Example. AOTInductor External Weight Storage and Weight Streaming Update Introduction AOTInductor is a PyTorch compiler backend that compiles PyTorch models into optimized shared libraries for efficient inference, which is similar to NVIDIA TensorRT. However, compared to NVIDIA TensorRT, AOTInductor is somewhat less well documented. It is unclear how to use some advanced features with AOTInductor, such as model weight updates at inference runtime. In this blog post, I would like to share an example of how to store the model weights outside the AOTInductor shared library so file, and how to update the model weights at inference runtime in a thread-safe fashion. AOTInductor Weight Storage and Update The example is based on a simple PyTorch model with a single linear layer. The AOTInductor engine and the model weights are compiled and packaged using a Python script. The AOTInductor engine inference and runtime weight updates are performed using a C++ program. The example is available on GitHub https://github.com/leimao/AOTInductor-External-Weight-Storage-Weight-Streaming-Update-Example . In some computer platforms, there might be some problems if the shared library so file is too large. Consequently, we would like to store the model weights outside the AOTInductor shared library so file, which is archived in the pt2 file. To enable this, we have to set the aot inductor.package constants in so configuration to False when compiling the AOTInductor model. The always keep tensor constants configuration is also set to True to ensure that the model constants are always kept as updatable constants, even if they are small. In our case, if we did not set it, the bias term of the linear layer would be folded into the graph and would not be exposed as an updatable constant. The AOTInductor configuration freezing is also set to False to allow model constants to be updated. Otherwise, the model constants will be frozen as model attributes and its layouts might be changed to a layout that is more efficient for inference, and they can no longer be updated according to the configuration comment https://github.com/pytorch/pytorch/blob/v2.13.0/torch/ inductor/config.py L1559 . For example, in convolution layers, the weight layout might be changed from NCHW in the PyTorch model to NHWC in the AOTInductor model, because CUDA kernels favor the NHWC layout for better memory coalescing. Of course, since we disabled the freezing configuration, some performance optimization opportunities might be lost. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 | python import osimport torchimport torch. inductorDEVICE = "cuda"assert torch.cuda.is available , "CUDA is required to run this script." Define a PyTorch Moduleclass SampleModel torch.nn.Module : def init self : super . init self.fc = torch.nn.Linear 4, 2 def forward self, x : return self.fc x OUTPUT DIR = os.path.join os.path.dirname os.path.abspath file , "aoti package" torch::pickle load in C++ hardcodes the archive's internal folder name to "data", which only happens when the saved file itself is named "data.