-
Notifications
You must be signed in to change notification settings - Fork 731
⚡ Bolt: Optimize RequestMetrics.to_dict() serialization #7022
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| import json | ||
| import time | ||
| import traceback | ||
| from dataclasses import asdict, dataclass, fields | ||
| from dataclasses import asdict, dataclass, fields, is_dataclass | ||
| from enum import Enum | ||
| from typing import Any, Dict, Generic, Optional | ||
| from typing import TypeVar as TypingTypeVar | ||
|
|
@@ -896,7 +896,23 @@ def to_dict(self): | |
| """ | ||
| Convert the RequestMetrics object to a dictionary. | ||
| """ | ||
| return {k: v for k, v in asdict(self).items()} | ||
| res = {} | ||
| for k in self.__dataclass_fields__: | ||
| v = getattr(self, k) | ||
| if type(v) in (int, float, str, bool, type(None)): | ||
| res[k] = v | ||
| elif type(v) is list: | ||
| res[k] = list(v) | ||
| elif type(v) is dict: | ||
| res[k] = dict(v) | ||
| elif is_dataclass(v): | ||
| if hasattr(v, "to_dict"): | ||
| res[k] = v.to_dict() | ||
| else: | ||
| res[k] = asdict(v) | ||
| else: | ||
| res[k] = v | ||
| return res | ||
|
Comment on lines
+899
to
+915
|
||
|
|
||
| def record_recv_first_token(self): | ||
| cur_time = time.time() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,6 +164,20 @@ class SpeculateMetrics: | |
| """ | ||
| accept_ratio_per_head: list[float] | ||
|
|
||
| def to_dict(self): | ||
| res = {} | ||
| for k in self.__dataclass_fields__: | ||
| v = getattr(self, k) | ||
| if type(v) in (int, float, str, bool, type(None)): | ||
| res[k] = v | ||
| elif type(v) is list: | ||
| res[k] = list(v) | ||
| elif type(v) is dict: | ||
| res[k] = dict(v) | ||
| else: | ||
| res[k] = v | ||
| return res | ||
|
Comment on lines
+167
to
+179
|
||
|
|
||
|
|
||
| @dataclass | ||
| class SamplerOutput: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR 标题目前未按仓库 PR 模板要求包含至少一个标签(形如
[Optimization]...)。建议将标题改为例如[Optimization] Optimize RequestMetrics.to_dict() serialization(可按需要叠加[Engine]等标签),以满足模板检查项并便于后续检索。