|
| 1 | +.. currentmodule:: torchrl.modules.inference_server |
| 2 | + |
| 3 | +Inference Server |
| 4 | +================ |
| 5 | + |
| 6 | +.. _ref_inference_server: |
| 7 | + |
| 8 | +The inference server provides auto-batching model serving for RL actors. |
| 9 | +Multiple actors submit individual TensorDicts; the server transparently |
| 10 | +batches them, runs a single model forward pass, and routes results back. |
| 11 | + |
| 12 | +Core API |
| 13 | +-------- |
| 14 | + |
| 15 | +.. autosummary:: |
| 16 | + :toctree: generated/ |
| 17 | + :template: rl_template_noinherit.rst |
| 18 | + |
| 19 | + InferenceServer |
| 20 | + InferenceClient |
| 21 | + InferenceTransport |
| 22 | + |
| 23 | +Transport Backends |
| 24 | +------------------ |
| 25 | + |
| 26 | +.. autosummary:: |
| 27 | + :toctree: generated/ |
| 28 | + :template: rl_template_noinherit.rst |
| 29 | + |
| 30 | + ThreadingTransport |
| 31 | + MPTransport |
| 32 | + RayTransport |
| 33 | + MonarchTransport |
| 34 | + |
| 35 | +Usage |
| 36 | +----- |
| 37 | + |
| 38 | +The simplest setup uses :class:`ThreadingTransport` for actors that are |
| 39 | +threads in the same process: |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + from tensordict.nn import TensorDictModule |
| 44 | + from torchrl.modules.inference_server import ( |
| 45 | + InferenceServer, |
| 46 | + ThreadingTransport, |
| 47 | + ) |
| 48 | + import torch.nn as nn |
| 49 | + import concurrent.futures |
| 50 | +
|
| 51 | + policy = TensorDictModule( |
| 52 | + nn.Sequential(nn.Linear(8, 64), nn.ReLU(), nn.Linear(64, 4)), |
| 53 | + in_keys=["observation"], |
| 54 | + out_keys=["action"], |
| 55 | + ) |
| 56 | +
|
| 57 | + transport = ThreadingTransport() |
| 58 | + server = InferenceServer(policy, transport, max_batch_size=32) |
| 59 | + server.start() |
| 60 | + client = transport.client() |
| 61 | +
|
| 62 | + # actor threads call client(td) -- batched automatically |
| 63 | + with concurrent.futures.ThreadPoolExecutor(16) as pool: |
| 64 | + ... |
| 65 | +
|
| 66 | + server.shutdown() |
| 67 | +
|
| 68 | +Weight Synchronisation |
| 69 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 70 | + |
| 71 | +The server integrates with :class:`~torchrl.weight_update.WeightSyncScheme` |
| 72 | +to receive updated model weights from a trainer between inference batches: |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + from torchrl.weight_update import SharedMemWeightSyncScheme |
| 77 | +
|
| 78 | + weight_sync = SharedMemWeightSyncScheme() |
| 79 | + # Initialise on the trainer (sender) side first |
| 80 | + weight_sync.init_on_sender(model=training_model, ...) |
| 81 | +
|
| 82 | + server = InferenceServer( |
| 83 | + model=inference_model, |
| 84 | + transport=ThreadingTransport(), |
| 85 | + weight_sync=weight_sync, |
| 86 | + ) |
| 87 | + server.start() |
| 88 | +
|
| 89 | + # Training loop |
| 90 | + for batch in dataloader: |
| 91 | + loss = loss_fn(training_model(batch)) |
| 92 | + loss.backward() |
| 93 | + optimizer.step() |
| 94 | + weight_sync.send(model=training_model) # pushed to server |
| 95 | +
|
| 96 | +Integration with Collectors |
| 97 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 98 | + |
| 99 | +The easiest way to use the inference server with RL data collection is |
| 100 | +through :class:`~torchrl.collectors.AsyncBatchedCollector`, which |
| 101 | +creates the server, transport, and env pool automatically: |
| 102 | + |
| 103 | +.. code-block:: python |
| 104 | +
|
| 105 | + from torchrl.collectors import AsyncBatchedCollector |
| 106 | + from torchrl.envs import GymEnv |
| 107 | +
|
| 108 | + collector = AsyncBatchedCollector( |
| 109 | + create_env_fn=[lambda: GymEnv("CartPole-v1")] * 8, |
| 110 | + policy=my_policy, |
| 111 | + frames_per_batch=200, |
| 112 | + total_frames=10_000, |
| 113 | + max_batch_size=8, |
| 114 | + ) |
| 115 | +
|
| 116 | + for data in collector: |
| 117 | + # train on data ... |
| 118 | + pass |
| 119 | +
|
| 120 | + collector.shutdown() |
0 commit comments