-
Notifications
You must be signed in to change notification settings - Fork 292
Add Lance reader stage #2111
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
Open
VibhuJawa
wants to merge
2
commits into
NVIDIA-NeMo:main
Choose a base branch
from
VibhuJawa:feat/lance-reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Lance reader stage #2111
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,10 +15,11 @@ | |
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING, Any | ||
| from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| import pyarrow as pa | ||
| import ray | ||
| from loguru import logger | ||
|
|
||
|
|
@@ -28,20 +29,31 @@ | |
| from nemo_curator.backends.utils import RayStageSpecKeys | ||
| from nemo_curator.stages.base import ProcessingStage | ||
| from nemo_curator.tasks import DocumentBatch, FileGroupTask | ||
| from nemo_curator.tasks.tasks import Task | ||
|
|
||
| ReaderTask = TypeVar("ReaderTask", bound=Task) | ||
| ReaderData: TypeAlias = pd.DataFrame | pa.Table | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ReaderOutput: | ||
| data: ReaderData | ||
| metadata: dict[str, Any] | None = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BaseReader(ProcessingStage[FileGroupTask, DocumentBatch]): | ||
| """Common base for tabular file readers. | ||
| class BaseReader(ProcessingStage[ReaderTask, DocumentBatch]): | ||
| """Common base for tabular readers. | ||
|
|
||
| Subclasses must implement the read_data method. | ||
| Subclasses must implement read_task for their input task type. | ||
| """ | ||
|
|
||
| fields: list[str] | None = None | ||
| read_kwargs: dict[str, Any] = field(default_factory=dict) | ||
| name: str = "" | ||
| _generate_ids: bool = False | ||
| _assign_ids: bool = False | ||
| allow_empty: bool = False | ||
|
|
||
| def __post_init__(self) -> None: | ||
| if self._generate_ids and self._assign_ids: | ||
|
|
@@ -52,7 +64,7 @@ def inputs(self) -> tuple[list[str], list[str]]: | |
| return [], [] | ||
|
|
||
| def outputs(self) -> tuple[list[str], list[str]]: | ||
| output_fields = self.fields or [] | ||
| output_fields = list(self.fields or []) | ||
| if self._generate_ids or self._assign_ids: | ||
| from nemo_curator.stages.deduplication.id_generator import CURATOR_DEDUP_ID_STR | ||
|
|
||
|
|
@@ -72,24 +84,16 @@ def setup(self, _: WorkerMetadata | None = None) -> None: | |
| ) | ||
| raise RuntimeError(msg) from None | ||
|
|
||
| def process(self, task: FileGroupTask) -> DocumentBatch: | ||
| # Merge read kwargs with storage options precedence: task.storage_options > self.read_kwargs | ||
| effective_read_kwargs: dict[str, Any] = {} | ||
| if self.read_kwargs: | ||
| effective_read_kwargs.update(self.read_kwargs) | ||
|
|
||
| # Read the files | ||
| result = self.read_data(task.data, effective_read_kwargs, self.fields) | ||
| def process(self, task: ReaderTask) -> DocumentBatch: | ||
| output = self.read_task(task, self._effective_read_kwargs(), self.fields) | ||
| self._validate_result(task, output.data) | ||
| return self._document_batch(task, output) | ||
|
|
||
| # Validate the result | ||
| if ( | ||
| (result is None) | ||
| or (hasattr(result, "empty") and result.empty) | ||
| or (hasattr(result, "num_rows") and result.num_rows == 0) | ||
| ): | ||
| msg = f"No data read from files in task {task.task_id}" | ||
| raise ValueError(msg) | ||
| def _effective_read_kwargs(self) -> dict[str, Any]: | ||
| return dict(self.read_kwargs or {}) | ||
|
|
||
| def _document_batch(self, task: ReaderTask, output: ReaderOutput) -> DocumentBatch: | ||
| result = output.data | ||
| # Apply IDs only for Pandas DataFrames | ||
| if isinstance(result, pd.DataFrame): | ||
| if self._generate_ids: | ||
|
|
@@ -100,16 +104,29 @@ def process(self, task: FileGroupTask) -> DocumentBatch: | |
| return DocumentBatch( | ||
| dataset_name=task.dataset_name, | ||
| data=result, | ||
| _metadata=task._metadata, | ||
| _metadata=self._output_metadata(task, output), | ||
| _stage_perf=task._stage_perf, | ||
| ) | ||
|
|
||
| def _output_metadata(self, task: ReaderTask, _output: ReaderOutput) -> dict[str, Any]: | ||
| return task._metadata | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. |
||
|
|
||
| def _validate_result(self, task: ReaderTask, result: ReaderData) -> None: | ||
| if self.allow_empty: | ||
| return | ||
| if (result is None) or (isinstance(result, pd.DataFrame) and result.empty) or ( | ||
| isinstance(result, pa.Table) and result.num_rows == 0 | ||
| ): | ||
| msg = f"No data read from files in task {task.task_id}" | ||
| raise ValueError(msg) | ||
|
|
||
| # Subclass responsibilities ------------------------------------------------- | ||
| def read_data( | ||
| def read_task( | ||
| self, | ||
| file_paths: list[str], | ||
| task: ReaderTask, | ||
| read_kwargs: dict[str, Any] | None, | ||
| fields: list[str] | None, | ||
| ) -> pd.DataFrame | None: # pragma: no cover - abstract | ||
| ) -> ReaderOutput: # pragma: no cover - abstract | ||
| raise NotImplementedError | ||
|
|
||
| # ID helpers ---------------------------------------------------------------- | ||
|
|
@@ -136,3 +153,24 @@ def _generate_ids_func(self, filepath: str | list[str], df: pd.DataFrame) -> pd. | |
|
|
||
| def ray_stage_spec(self) -> dict[str, Any]: | ||
| return {RayStageSpecKeys.IS_ACTOR_STAGE: self._generate_ids or self._assign_ids} | ||
|
|
||
|
|
||
| @dataclass | ||
| class BaseFileReader(BaseReader[FileGroupTask]): | ||
| """Base reader for file-group readers that consume lists of paths.""" | ||
|
|
||
| def read_task( | ||
| self, | ||
| task: FileGroupTask, | ||
| read_kwargs: dict[str, Any] | None, | ||
| fields: list[str] | None, | ||
| ) -> ReaderOutput: | ||
| return ReaderOutput(self.read_data(task.data, read_kwargs, fields)) | ||
|
|
||
| def read_data( | ||
| self, | ||
| file_paths: list[str], | ||
| read_kwargs: dict[str, Any] | None, | ||
| fields: list[str] | None, | ||
| ) -> ReaderData: # pragma: no cover - abstract | ||
| raise NotImplementedError | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit but I don't see a reason for having a 1 line helper function.