PyTorch Multi-Process Inference Weight Sharing via Inter-Process Communication

eigenBasis1 pts0 comments

PyTorch Multi-Process Inference Weight Sharing Via Inter-Process Communication - Lei Mao's Log Book

PyTorch Multi-Process Inference Weight Sharing Via Inter-Process Communication<br>07-31-2026 07-31-2026 blog 17 minutes read (About 2500 words) visits

Introduction<br>PyTorch is probably the most popular deep learning framework for model training. Ideally, without any additional effort, PyTorch model could be used for inference seamlessly in Python. However, in practice, there are some concerns when using PyTorch for inference in Python, primarily because of the Global Interpreter Lock (GIL) in Python. The GIL prevents multiple threads from executing Python bytecodes at once, which can lead to performance bottlenecks in high-throughput applications. To overcome this limitation, PyTorch models can be run in multiple processes, where each process runs its own Python interpreter and has its own GIL. However, because model weights cannot be natively shared across processes in Python, multi-process inference on single-GPU can lead to duplicated model weights in GPU VRAM, which can quickly exhaust the available GPU memory. To mitigate this issue, model weights can be shared across processes using CUDA Inter-Process Communication (IPC).

It turns out that PyTorch multiprocessing module has abstracted away the details of CUDA IPC, and provides a simple API to share CUDA tensors across processes. In this blog post, I will demonstrate how to use PyTorch multiprocessing module to share model weights across multiple processes for inference, and avoid weight duplication in GPU VRAM.

PyTorch Multi-Process Inference Weight Sharing<br>In the following example, I will demonstrate how to share model weights via CUDA IPC across multiple processes for PyTorch and AOTInductor inference using PyTorch multiprocessing module.

pytorch_inference_ipc.py1<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>50<br>51<br>52<br>53<br>54<br>55<br>56<br>57<br>58<br>59<br>60<br>61<br>62<br>63<br>64<br>65<br>66<br>67<br>68<br>69<br>70<br>71<br>72<br>73<br>74<br>75<br>76<br>77<br>78<br>79<br>80<br>81<br>82<br>83<br>84<br>85<br>86<br>87<br>88<br>89<br>90<br>91<br>92<br>93<br>94<br>95<br>96<br>97<br>98<br>99<br>100<br>101<br>102<br>103<br>104<br>105<br>106<br>107<br>108<br>109<br>110<br>111<br>112<br>113<br>114<br>115<br>116<br>117<br>118<br>119<br>120<br>121<br>122<br>123<br>124<br>125<br>126<br>127<br>128<br>129<br>130<br>131<br>132<br>133<br>134<br>135<br>136<br>137<br>138<br>139<br>140<br>141<br>142<br>143<br>144<br>145<br>146<br>147<br>148<br>149<br>150<br>151<br>152<br>153<br>154<br>155<br>156<br>157<br>158<br>159<br>160<br>161<br>162<br>163<br>164<br>165<br>166<br>167<br>168<br>169<br>170<br>171<br>172<br>173<br>174<br>175<br>176<br>177<br>178<br>179<br>180<br>181<br>182<br>183<br>184<br>185<br>186<br>187<br>188<br>189<br>190<br>191<br>192<br>193<br>194<br>195<br>196<br>197<br>198<br>199<br>200<br>201<br>202<br>203<br>204<br>205<br>206<br>207<br>208<br>209<br>210<br>211<br>212<br>213<br>214<br>215<br>216<br>217<br>218<br>219<br>220<br>221<br>222<br>223<br>224<br>225<br>226<br>227<br>228<br>229<br>230<br>231<br>232<br>233<br>234<br>235<br>236<br>237<br>238<br>239<br>240<br>241<br>242<br>243<br>244<br>245<br>246<br>247<br>248<br>249<br>250<br>251<br>252<br>253<br>254<br>255<br>256<br>257<br>258<br>259<br>260<br>261<br>262<br>263<br>264<br>265<br>266<br>267<br>268<br>269<br>270<br>271<br>272<br>273<br>274<br>275<br>276<br>277<br>278<br>279<br>280<br>281<br>282<br>283<br>284<br>285<br>286<br>287<br>288<br>289<br>290<br>291<br>292<br>293<br>294<br>295<br>296<br>297<br>298<br>299<br>300<br>301<br>302<br>303<br>304<br>305<br>306<br>307<br>308<br>309<br>310<br>311<br>312<br>313<br>314<br>315<br>316<br>317<br>318<br>319<br>320<br>321<br>322<br>import argparse<br>import io<br>import os<br>import tempfile<br>import time

import torch<br>import torch.nn as nn<br>import torch.multiprocessing as mp<br>import torch._inductor

# =====================================================================<br># 1. MODEL DEFINITION<br># =====================================================================<br>class SharedWeightInferenceEngine(nn.Module):

def __init__(self,<br>in_features: int = 16384,<br>hidden_dim: int = 16384,<br>out_features: int = 12800):<br>super().__init__()<br>self.encoder = nn.Linear(in_features, hidden_dim)<br>self.head = nn.Linear(hidden_dim, out_features)

def forward(self, x: torch.Tensor) -> torch.Tensor:<br>x = torch.relu(self.encoder(x))<br>return self.head(x)

# =====================================================================<br># 2. HELPER FUNCTIONS<br># =====================================================================<br>def get_device_vram_usage_mb(device_id: int = 0) -> float:<br>"""Return device-wide GPU memory usage in MB."""<br>torch.cuda.set_device(device_id)<br>free_bytes, total_bytes = torch.cuda.mem_get_info(device_id)<br>return (total_bytes - free_bytes) / (1024**2)

def load_aoti_runner(package_path: str, state_dict: dict, device_id: int):<br>"""Load an AOTI runner and bind it to the supplied model weights."""<br>runner = torch._inductor.aoti_load_package(package_path,<br>device_index=device_id)

# To enable IPC weight sharing using the parent state dict,<br># uncomment the following lines:<br>expected_keys = set(state_dict.keys())<br>if hasattr(runner, "get_constant_fqns"):<br>assert set(runner.get_constant_fqns()) == expected_keys<br># Now the weight tensor pointers or references are pointing to the shared weights in the parent process.<br>runner.load_constants(state_dict,<br>check_full_update=True,<br>user_managed=False)

torch.cuda.synchronize()<br>time.sleep(0.2)<br>return runner

# =====================================================================<br># 3. WORKER PROCESS INFERENCE LOOPS<br>#...

pytorch process torch inference model import

Related Articles