Support server based rollout in Verlengine (#4848)

Co-authored-by: Jin Pan <jpan236@wisc.edu>
Co-authored-by: Chayenne <zhaochen20@outlook.com>
Co-authored-by: Jinn <47354855+jhinpan@users.noreply.github.com>
This commit is contained in:
tianlian yi
2025-04-12 10:07:52 -07:00
committed by GitHub
co-authored by Jin Pan Chayenne Jinn
parent 3e4794aad8
commit bc92107b03
10 changed files with 720 additions and 29 deletions
+31 -2
View File
@@ -1480,14 +1480,43 @@ def permute_weight(x: torch.Tensor) -> torch.Tensor:
class MultiprocessingSerializer:
@staticmethod
def serialize(obj):
def serialize(obj, output_str: bool = False):
"""
Serialize a Python object using ForkingPickler.
Args:
obj: The object to serialize.
output_str (bool): If True, return a base64-encoded string instead of raw bytes.
Returns:
bytes or str: The serialized object.
"""
buf = io.BytesIO()
ForkingPickler(buf).dump(obj)
buf.seek(0)
return buf.read()
output = buf.read()
if output_str:
# Convert bytes to base64-encoded string
output = base64.b64encode(output).decode("utf-8")
return output
@staticmethod
def deserialize(data):
"""
Deserialize a previously serialized object.
Args:
data (bytes or str): The serialized data, optionally base64-encoded.
Returns:
The deserialized Python object.
"""
if isinstance(data, str):
# Decode base64 string to bytes
data = base64.b64decode(data)
return ForkingPickler.loads(data)