Skip to content

Add support for JSON encoding torch.tensor to keep it consistent with sagemaker-inference-toolkit #84

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 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def default(self, obj):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
elif isinstance(obj, np.ndarray) or hasattr(obj, "tolist"):
return obj.tolist()
elif isinstance(obj, datetime.datetime):
return obj.__str__()
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_decoder_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os

import pytest
from transformers.testing_utils import require_torch

from mms.service import PredictionException
from PIL import Image
Expand Down Expand Up @@ -84,6 +85,15 @@ def test_encode_json():
assert json.loads(encoded_data) == ENCODE_JSON_INPUT


@require_torch
def test_encode_json_torch():
import torch

DATA = [1, 0.5, 5.0]
encoded_data = decoder_encoder.encode_json({"data": torch.tensor(DATA)})
assert json.loads(encoded_data) == {"data": DATA}


def test_encode_csv():
decoded_data = decoder_encoder.encode_csv(ENCODE_CSV_INPUT)
assert (
Expand Down