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

[WIP][Experiment] Use multi-threading for multi-file json reads #94

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
20 changes: 19 additions & 1 deletion nemo_curator/utils/distributed_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@

import ast
import os
from io import BytesIO

import fsspec

os.environ["RAPIDS_NO_INITIALIZE"] = "1"
import warnings
from contextlib import nullcontext
from pathlib import Path
from typing import Union

import dask
import dask.dataframe as dd
import pandas as pd
from dask.base import tokenize
from dask.distributed import Client, LocalCluster, get_worker, performance_report

from nemo_curator.utils.gpu_utils import GPU_INSTALL_STRING, is_cudf_type
Expand Down Expand Up @@ -240,7 +245,7 @@ def read_single_partition(
if add_filename:
read_files_one_at_a_time = True
else:
if backend == "cudf":
if backend == "cudf" or filetype == "jsonl":
# cuDF supports reading multiple files at once
read_files_one_at_a_time = False
else:
Expand All @@ -259,6 +264,19 @@ def read_single_partition(
df["filename"] = os.path.basename(file)
df_ls.append(df)
df = concat_f(df_ls, ignore_index=True)
elif filetype == "jsonl":
fs = fsspec.core.get_fs_token_paths(files[0])[0]
token = tokenize(files)
name = f"get_bytes-{token}"
dsk = {(name, i): (fs.cat_file, path) for i, path in enumerate(files)}
if backend == "cudf":
# Cudf can read from list of bytes
dsk[name] = (lambda x: x, list(dsk.keys()))
df = read_f(dask.threaded.get(dsk, name), **read_kwargs)
else:
# Pandas requires single BytesIO object
dsk[name] = (b"".join, list(dsk.keys()))
df = read_f(BytesIO(dask.threaded.get(dsk, name)), **read_kwargs)
else:
df = read_f(files, **read_kwargs)
df = df[sorted(df.columns)]
Expand Down