-
Notifications
You must be signed in to change notification settings - Fork 7k
[Data] Update document embedding benchmark to use canonical Ray Data API #57977
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||
| import pymupdf | ||||||
| import ray | ||||||
| import ray.data | ||||||
| from ray.data.expressions import download | ||||||
| import torch | ||||||
| from langchain.text_splitter import RecursiveCharacterTextSplitter | ||||||
| from sentence_transformers import SentenceTransformer | ||||||
|
|
@@ -42,15 +43,15 @@ def extract_text_from_pdf(row): | |||||
| bs = row.pop("bytes") | ||||||
| doc = pymupdf.Document(stream=bs, filetype="pdf") | ||||||
| if len(doc) > MAX_PDF_PAGES: | ||||||
| path = row["path"] | ||||||
| path = row["uploaded_pdf_path"] | ||||||
| print(f"Skipping PDF {path} because it has {len(doc)} pages") | ||||||
| return | ||||||
| for page in doc: | ||||||
| row["page_text"] = page.get_text() | ||||||
| row["page_number"] = page.number | ||||||
| yield row | ||||||
| except Exception as e: | ||||||
| path = row["path"] | ||||||
| path = row["uploaded_pdf_path"] | ||||||
| print(f"Error extracting text from PDF {path}: {e}") | ||||||
| return | ||||||
|
|
||||||
|
|
@@ -83,19 +84,22 @@ def __call__(self, batch): | |||||
|
|
||||||
| start_time = time.time() | ||||||
|
|
||||||
| file_paths = ( | ||||||
| ( | ||||||
| ray.data.read_parquet(INPUT_PATH) | ||||||
| .filter(lambda row: row["file_name"].endswith(".pdf")) | ||||||
|
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. Using a
Suggested change
|
||||||
| .take_all() | ||||||
| ) | ||||||
| file_paths = [row["uploaded_pdf_path"] for row in file_paths] | ||||||
| ds = ray.data.read_binary_files(file_paths, include_paths=True) | ||||||
| ds = ds.flat_map(extract_text_from_pdf) | ||||||
| ds = ds.flat_map(chunker) | ||||||
| ds = ds.map_batches( | ||||||
| Embedder, concurrency=NUM_GPU_NODES, num_gpus=1.0, batch_size=EMBEDDING_BATCH_SIZE | ||||||
| .with_column("bytes", download("uploaded_pdf_path")) | ||||||
| .flat_map(extract_text_from_pdf) | ||||||
| .flat_map(chunker) | ||||||
| .map_batches( | ||||||
| Embedder, | ||||||
| concurrency=NUM_GPU_NODES, | ||||||
| num_gpus=1.0, | ||||||
| batch_size=EMBEDDING_BATCH_SIZE, | ||||||
| ) | ||||||
| .select_columns( | ||||||
| ["uploaded_pdf_path", "page_number", "chunk_id", "chunk", "embedding"] | ||||||
| ) | ||||||
| .write_parquet(OUTPUT_PATH) | ||||||
| ) | ||||||
| ds = ds.select_columns(["path", "page_number", "chunk_id", "chunk", "embedding"]) | ||||||
| ds.write_parquet(OUTPUT_PATH) | ||||||
|
|
||||||
| print("Runtime:", time.time() - start_time) | ||||||
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.
To make the code even more idiomatic and potentially more performant through expression optimization, consider importing
colhere. It can be used to replace the lambda in thefilteroperation below.