Skip to content
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

Allow JSON encoder to handle ndarray #777

Merged
merged 3 commits into from
Sep 9, 2024
Merged

Conversation

srowen
Copy link
Contributor

@srowen srowen commented Sep 7, 2024

Description of changes:

The JSON encoder encodes a variety of data types via json.dumps, including primitives, lists, dicts, etc.
However json.dumps doesn't work with ndarrays ; it won't serialize them.
Conceptually, there isn't a strong reason to refuse to encode input as JSON where ndarray is used instead of lists, when one could simply transform even multi-dimensional ndarrays to supported Python lists with .tolist().

This change simply has the encoder call .tolist() on its arg if passed an ndarray, and the rest works as expected.

This is actually relevant because the serialization of array type data from Spark to pandas will use ndarrays (being based on Arrow), and the result can't be passed to dataframe_to_mds even if the output type is given as 'json'. It's possible to workaround by passing a transformation of the pandas DF to this function. But, it seemed simple and natural to just support this directly.

Merge Checklist:

General

  • I have read the contributor guidelines
  • This is a documentation change or typo fix. If so, skip the rest of this checklist.
  • I certify that the changes I am introducing will be backward compatible, and I have discussed concerns about this, if any, with the MosaicML team.
  • I have updated any necessary documentation, including README and API docs (if appropriate).

Tests

  • I ran pre-commit on my change. (check out the pre-commit section of prerequisites)
  • I have added tests that prove my fix is effective or that my feature works (if appropriate).
  • I ran the tests locally to make sure it pass. (check out testing)
  • I have added unit and/or integration tests as appropriate to ensure backward compatibility of the changes.

@srowen srowen added the enhancement New feature or request label Sep 7, 2024
Copy link
Collaborator

@snarayan21 snarayan21 left a comment

Choose a reason for hiding this comment

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

why not just use the ndarray encoding type?

streaming/base/format/mds/encodings.py Show resolved Hide resolved
Copy link
Collaborator

@snarayan21 snarayan21 left a comment

Choose a reason for hiding this comment

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

Lgtm

@snarayan21 snarayan21 enabled auto-merge (squash) September 9, 2024 18:18
@snarayan21 snarayan21 merged commit 06fd29f into mosaicml:main Sep 9, 2024
7 checks passed
@srowen srowen deleted the ndarray_json branch September 9, 2024 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants