meta-pytorch / meta-pytorch/data

Add the functionality to read chunk by chunk

Open
#439 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.3k
Forks
179
Avg merge
6d 1h
Merged PRs (30d)
2

Description

🚀 The feature

This issue is a continuation of the discussion from here.

We are proposing to add some functionality to allow reading chunk by chunk from a stream. Ideally, this should be done without loading the entire stream/response into memory first, but this may not be possible in all cases.

Motivation, pitch

From @pmeier

Having something like a ChunkReader datapipe can be useful in general not just for HTTP requests, right?

from torchdata.datapipes.iter import IterDataPipe


class ChunkReader(IterDataPipe):
    def __init__(self, datapipe, *, chunk_size=32 * 1024 * 1024):
        self.datapipe = datapipe
        self.chunk_size = chunk_size

    def __iter__(self):
        for path, stream in self.datapipe:
            for chunk in iter(lambda: stream.read(self.chunk_size), b""):
                # filter out keep-alive new chunks from HTTP streams
                if not chunk:
                    continue

                yield path, chunk

A utility like that is needed anyway for Saver anyway. AFAIK, there is currently no builtin functionality to read data from a stream, correct? I'm guessing that is why the Saver example uses bytes as input.

This can be useful for reading from file stream in chunks (e.g. after FileOpener), writing to files in chunks (e.g. Saver, and etc (e.g. HttpReader).

Alternatives

Instead of adding a new DataPipe, we can consider modifying specific existing DataPipes to add the functionality of reading by chunk. This may be useful in avoiding the need to read the entire stream/response into memory (as I believe it is the case in the current implementation of HttpReader, please correct me if this is wrong).

We can also do both.

Additional context

No response

cc: @ejguan @VitalyFedyunin

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

Start by reading the existing FileOpener, Saver, and HttpReader DataPipes, especially the Saver example at torchdata/datapipes/iter/util/saver.py. Review the linked discussion to determine whether a new ChunkReader or changes to existing DataPipes are expected. Done means streams can be consumed chunk by chunk without unnecessarily loading the entire response into memory, with the behavior covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.