Skip to content

Commit 49eedea

Browse files
authored
[Core] Zero-copy asdict for InputMetadata (#3475)
1 parent 9fdf3de commit 49eedea

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

vllm/model_executor/input_metadata.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from dataclasses import dataclass
2-
from typing import Optional
1+
from dataclasses import dataclass, fields
2+
from typing import Optional, Any, Dict
33

44
import torch
55

@@ -31,3 +31,12 @@ class InputMetadata:
3131
def __post_init__(self):
3232
# will not appear in the __repr__ and __init__
3333
self.attn_bias = None
34+
35+
def asdict_zerocopy(self) -> Dict[str, Any]:
36+
"""Similar to dataclasses.asdict, but avoids deepcopying."""
37+
# Note that if we add dataclasses as fields, they will need
38+
# similar handling.
39+
return {
40+
field.name: getattr(self, field.name)
41+
for field in fields(self)
42+
}

vllm/worker/model_runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import contextlib
2-
import dataclasses
32
import time
43
from typing import Dict, List, Optional, Tuple, Set, Union
54

@@ -527,7 +526,7 @@ def prepare_input_tensors(
527526
"lora_requests": lora_requests,
528527
"lora_mapping": lora_mapping,
529528
}
530-
metadata_dict.update(dataclasses.asdict(input_metadata))
529+
metadata_dict.update(input_metadata.asdict_zerocopy())
531530
broadcast_tensor_dict(metadata_dict, src=0)
532531
else:
533532
metadata_dict = broadcast_tensor_dict(src=0)

0 commit comments

Comments
 (0)