[Core] Zero-copy asdict for InputMetadata (#3475)

This commit is contained in:
Antoni Baum
2024-03-18 22:56:40 +00:00
committed by GitHub
parent 9fdf3de346
commit 49eedea373
2 changed files with 12 additions and 4 deletions
+11 -2
View File
@@ -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
@@ -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)
}