mosaicml / mosaicml/streaming

Unexpected mds format data for json encoding / how to encode list of strings

Open
#613 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.6k
Forks
206
PR merge metrics
No merged PRs in 30d

Description

Create a simple dataset with lists

from torch.utils.data import DataLoader
import streaming
from streaming import StreamingDataset
from streaming.base.converters import dataframe_to_mds
from datasets import Dataset
import pandas as pd

df = pd.DataFrame(
    {
        "label": [0, 1, 0],
        "data1": [["a", "b", "c", "d"], ["a", "b", "c", "d"], ["a", "b", "c", "d"]],
        "data2": [[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]],
    }
)
df = spark.createDataFrame(df)
def udf_processing(df):
    hf_dataset = Dataset.from_pandas(df=df)
    for sample in hf_dataset:
        yield sample
        
mds_kwargs = {
    "out": "mds",
    "columns": {
        "data1": "json",
        "data2": "json",
        "label": "int64",
    },
}
dataframe_to_mds(
    df,
    merge_index=True,
    mds_kwargs=mds_kwargs,
    udf_iterable=udf_processing,
)

dataset = StreamingDataset(local="mds", remote=None, batch_size=1, predownload=4)

dataloader = DataLoader(dataset, batch_size=1, num_workers=1)
next(iter(dataloader))

Batch result:

{'data1': [('a',), ('b',), ('c',), ('d',)],
 'data2': [tensor([1.], dtype=torch.float64),
  tensor([2.], dtype=torch.float64),
  tensor([3.], dtype=torch.float64),
  tensor([4.], dtype=torch.float64)],
 'label': tensor([0])}

Expected behavior

I would expect json data to be decoded back into the original form. Is there any way to achieve this behavior?

{'data1': ["a", "b", "c", "d"],
 'data2': [1., 2., 3., 4.],
 'label': tensor([0])}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example using dataframe_to_mds, StreamingDataset, and DataLoader, then inspect how the json columns are decoded during batch creation. Done means list-valued data1 and data2 fields are returned in their original list forms while label remains a tensor, with the behavior verified against the shown example.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python, pytorch, spark
Domain
data-engineering, machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.