vllm.distributed.device_communicators.shm_object_storage ¶
MsgpackSerde ¶
Bases: ObjectSerde
Source code in vllm/distributed/device_communicators/shm_object_storage.py
__init__ ¶
Source code in vllm/distributed/device_communicators/shm_object_storage.py
deserialize ¶
deserialize(data_view: memoryview) -> Any
Source code in vllm/distributed/device_communicators/shm_object_storage.py
serialize ¶
Source code in vllm/distributed/device_communicators/shm_object_storage.py
ObjectSerde ¶
Bases: ABC
Source code in vllm/distributed/device_communicators/shm_object_storage.py
deserialize abstractmethod
¶
deserialize(data: memoryview) -> Any
serialize abstractmethod
¶
ShmObjectStorageHandle dataclass
¶
Source code in vllm/distributed/device_communicators/shm_object_storage.py
SingleWriterShmObjectStorage ¶
A single-writer, multiple-reader object storage system built on top of a shared memory ring buffer. Provides key-value storage with automatic memory management and cross-process serialization support.
This storage system follows a FIFO (First-In-First-Out) eviction policy where the oldest objects are automatically freed when memory runs low. Memory is reclaimed based on reader reference counting - objects are only freed when all readers have finished accessing them.
Architecture: - Single writer process can put(key, value) objects - Multiple reader processes can get(address, monotonic_id) objects - Built on SingleWriterShmRingBuffer for efficient shared memory management - Thread-safe operations with reader synchronization via locks
Key Features: - FIFO Eviction: Oldest objects are evicted first when memory is full - Reference Counting: Objects are only freed when no readers are accessing them - Duplicate Key Handling: Existing keys are not overwritten, just re-referenced - Customized Serialization: By default uses Msgpack for efficient serialization of Python objects, but can be extended for custom types - Cross-Process Safety: Uses shared memory with proper synchronization - Automatic Cleanup: Garbage collection happens transparently during allocation
Memory Layout per Object: [4-byte reference_count][metadata_size][serialized_object_data]
Thread Safety: - Writer operations (put, clear) are single-threaded by design - Reader operations (get) are thread-safe with lock-based reference counting - Memory reclamation is handled exclusively by the writer process
Source code in vllm/distributed/device_communicators/shm_object_storage.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
|
__init__ ¶
__init__(
max_object_size: int,
n_readers: int,
ring_buffer: SingleWriterShmRingBuffer,
serde_class: type[ObjectSerde] = MsgpackSerde,
reader_lock: Optional[Lock] = None,
)
Initialize the object storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_object_size | int | Maximum size for a single object in bytes. | required |
n_readers | int | Number of reader processes that can access the storage. | required |
ring_buffer | SingleWriterShmRingBuffer | The shared memory ring buffer for storing objects. | required |
serde_class | type[ObjectSerde] | Serializer/deserializer for objects. | MsgpackSerde |
reader_lock | Optional[Lock] | Optional lock for synchronizing reader access. | None |
Raises: ValueError: If reader_lock is None for readers.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
clear ¶
Clear the object storage.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
copy_to_buffer ¶
copy_to_buffer(
data: Union[bytes, list[bytes]],
data_bytes: int,
metadata: bytes,
md_bytes: int,
data_view: memoryview,
) -> None
Source code in vllm/distributed/device_communicators/shm_object_storage.py
create_from_handle staticmethod
¶
create_from_handle(
handle: ShmObjectStorageHandle,
) -> SingleWriterShmObjectStorage
Source code in vllm/distributed/device_communicators/shm_object_storage.py
default_is_free_check ¶
default_is_free_check(id: int, buf: memoryview) -> bool
Default is_free function that checks if the first 4 bytes are zero. This indicates that the buffer is free.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
free_unused ¶
Free unused buffers in the ring buffer.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
get ¶
Source code in vllm/distributed/device_communicators/shm_object_storage.py
get_cached ¶
Get the cached object by key if it exists.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
handle ¶
Get handle for sharing across processes.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
increment_reader_flag ¶
increment_reader_flag(data_view: memoryview) -> None
Set the in-use flag for the reader.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
is_cached ¶
put ¶
Store a key-value pair in the object storage. Attempts to free max_object_size bytes using FIFO order when the ring buffer runs out of space during a put() operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key | str | String key to identify the object | required |
value | Any | Any serializable Python object | required |
Raises:
Type | Description |
---|---|
MemoryError | If there's not enough space in the buffer |
ValueError | If the serialized object is too large |
ValueError | If the key already exists in the storage |
Source code in vllm/distributed/device_communicators/shm_object_storage.py
SingleWriterShmRingBuffer ¶
A single-writer, multiple-reader ring buffer implementation using shared memory. This class provides a thread-safe ring buffer where one process can write data while multiple processes/threads can read from it.
Architecture: - Uses shared memory for cross-process communication - Maintains metadata for each allocated buffer chunk in the writer process - Supports custom "is_free_fn" functions to determine when buffers can be reused - Each buffer chunk contains: [4-byte id][4-byte size][actual_data]
Key Concepts: - monotonic_id_start/end: Track the range of active buffer IDs - data_buffer_start/end: Track the physical memory range in use - Automatic wraparound when reaching buffer end - Lazy garbage collection based on is_free_fn checks
Example Usage Scenarios:
Scenario 1: Simple Linear Allocation
Buffer size: 100 bytes
Initial state: [................................................. ]
^start=end(0)
After allocating 20 bytes (id=0):
[id:0|size:20|data........][...................................]
^start(0) ^end(28)
After allocating 30 bytes (id=1):
[id:0|size:20|data........][id:1|size:30|data..............][..]
^start(0) ^end(66)
Scenario 2: Memory Reclamation
Before freeing (both buffers still in use):
[id:0|size:20|data........][id:1|size:30|data..............][..]
^start(0) ^end(66)
After id:0 is marked free by readers:
[FREED.................... ][id:1|size:30|data..............][..]
^start(28) ^end(66)
After both are freed:
[FREED..............................................][..]
^start=end(66)
Scenario 3: Wraparound Allocation (continuing from Scenario 2)
Starting from after memory reclamation in Scenario 2:
[FREED..............................................][..]
^start=end(66)
Allocate 40 bytes (id=2) - only 34 bytes available at end, so wraparound:
[id:2|size:40|data........................][FREED.............][..]
^end(148) ^start(66)
Scenario 4: Error Handling - Out of Space
Starting from after wraparound allocation in Scenario 3:
[id:2|size:40|data........................][FREED.............][..]
^end(148) ^start(66)
Trying to allocate 20 more bytes:
occupied_size_new = end + size - start = 148 + 28 - 66 > buffer_size(100)
-> Raises MemoryError: "Not enough space in the data buffer"
Thread Safety: - Single writer: Only one process/thread should write (allocate_buf) - Multiple readers: Multiple processes/threads can read (access_buf) - Reader synchronization handled by is_free_fn callback - Writer handles garbage collection (free_buf) based on reader feedback
Memory Layout per Buffer Chunk: [4-byte monotonic_id][4-byte chunk_size][actual_data...]
^metadata_start ^data_start
The monotonic_id ensures data integrity - readers can verify they're accessing the correct data even after buffer wraparound or reuse.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
__del__ ¶
__init__ ¶
Source code in vllm/distributed/device_communicators/shm_object_storage.py
access_buf ¶
access_buf(address: int)
Source code in vllm/distributed/device_communicators/shm_object_storage.py
allocate_buf ¶
Allocate a buffer MD_SIZE
+ size
bytes in the shared memory. Memory layout: [4-byte monotonic_id][4-byte size][buffer data...]
Source code in vllm/distributed/device_communicators/shm_object_storage.py
byte2int ¶
clear ¶
Clear the ring buffer.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
free_buf ¶
free_buf(
is_free_fn: Callable[[int, memoryview], bool],
nbytes: Optional[int] = None,
) -> Iterable[int]
Free a buffer of the given size. This is a no-op in shared memory, but we need to keep track of the metadata.
If freed memory spreads across the end and start of the ring buffer, the actual freed memory will be in two segments. In this case there still might not be a contiguous space of nbytes
available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nbytes | int | The size of the buffer to free. If None, frees the maximum size of the ring buffer. | None |