Skip to content

[Core] Zero-copy asdict for InputMetadata #3475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions vllm/model_executor/input_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Optional
from dataclasses import dataclass, fields
from typing import Optional, Any, Dict

import torch

Expand Down Expand Up @@ -31,3 +31,12 @@ class InputMetadata:
def __post_init__(self):
# will not appear in the __repr__ and __init__
self.attn_bias = None

def asdict_zerocopy(self) -> Dict[str, Any]:
"""Similar to dataclasses.asdict, but avoids deepcopying."""
# Note that if we add dataclasses as fields, they will need
# similar handling.
return {
field.name: getattr(self, field.name)
for field in fields(self)
}
3 changes: 1 addition & 2 deletions vllm/worker/model_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import contextlib
import dataclasses
import time
from typing import Dict, List, Optional, Tuple, Set, Union

Expand Down Expand Up @@ -527,7 +526,7 @@ def prepare_input_tensors(
"lora_requests": lora_requests,
"lora_mapping": lora_mapping,
}
metadata_dict.update(dataclasses.asdict(input_metadata))
metadata_dict.update(input_metadata.asdict_zerocopy())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just do input_metadata.__dict__ to avoid the new method at all?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, though that may lead to unexpected interactions if InputMetadata has extra properties added and such.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the post init adds self.attn_bias = None.

broadcast_tensor_dict(metadata_dict, src=0)
else:
metadata_dict = broadcast_tensor_dict(src=0)
Expand Down