-
Notifications
You must be signed in to change notification settings - Fork 318
Add Operator-Level Parallel Data Processing with Ray Actors #761
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
Cccccc0630
wants to merge
18
commits into
datajuicer:dev/community_contributions
Choose a base branch
from
Cccccc0630:my_pr
base: dev/community_contributions
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
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4bf83aa
Implementing op parallel data processing based on Ray Actor
54434df
Combined: update ray_dataset.py + Ray Actor implementation
490746f
update ray_dataset.py
d3b7d51
Implementing op parallel data processing based on Ray Actor
a81e90c
update 0805
fc8968a
update 0808
95de6e3
update for code review
da962a5
update for conflicts
8cb420e
update for conflicts2
aa25820
update for pre-commit
95ca976
update for pre-commit
8aa253c
Merge branch 'main' into my_pr
Cccccc0630 7aa2f79
update for pre-commit
bb62e90
rename ray_actor.py
a978375
update for pre-commit
042a0b4
Fix trailing whitespace and pre-commit issues
6875b7d
Fix unittest error
4a13102
Merge branch 'dev/community_contributions' into my_pr
HYLcool 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| from functools import partial | ||
| import ray | ||
| import pyarrow | ||
|
|
||
| from data_juicer.ops.base_op import Filter, Mapper | ||
| from loguru import logger | ||
|
|
||
|
|
||
|
|
||
| def filter_batch(batch, filter_func): | ||
| mask = pyarrow.array(filter_func(batch.to_pydict())) | ||
| return batch.filter(mask) | ||
|
|
||
| @ray.remote(num_gpus=0.0) | ||
| class Actor: | ||
| def __init__(self, op, rank=None): | ||
|
|
||
| self.op = op | ||
| self._model_loaded = False # 标记模型是否已加载 | ||
| self.rank = rank | ||
| self.model = None | ||
| self.processor = None | ||
|
|
||
| def load_model(self): | ||
|
|
||
| if self.op.use_cuda() and not self._model_loaded: | ||
|
|
||
| self.model, self.processor = self.op.load_model(rank=self.rank) | ||
| self._model_loaded = True | ||
|
|
||
| def mapper_cuda(self, data): | ||
| if not self._model_loaded: | ||
| self.load_model() # 确保调用前模型已加载 | ||
| data = self.op.process_single(data, self.model, self.processor) | ||
| return data | ||
|
|
||
| def mapper_cuda_batched(self, data): | ||
| if not self._model_loaded: | ||
| self.load_model() # 确保调用前模型已加载 | ||
| data = self.op.process_batched_actor(data, self.model, self.processor) | ||
| return data | ||
|
|
||
| def mapper_cpu(self, data): | ||
| # 处理数据 | ||
| processed_data = self.op.process_single(data) | ||
| return processed_data | ||
|
|
||
| def filter_cuda_single(self, data): | ||
| if not self._model_loaded: | ||
| self.load_model() | ||
| data = self.op.compute_stats_single_actor(data, self.model, self.processor) | ||
| keep = self.op.process_single(data) | ||
|
|
||
| if keep: | ||
| return data | ||
| else: | ||
| return None | ||
|
|
||
| def filter_cuda_batched(self, data): | ||
| if not self._model_loaded: | ||
| self.load_model() | ||
| # data = self.op.compute_stats_batched(data, self.model, self.processor) | ||
| data = self.op.compute_stats_batched(data) | ||
| keep_mask = list(self.op.process_batched(data)) # 将map对象转换为列表 | ||
|
|
||
| # 如果没有数据需要保留,返回None | ||
| if not any(keep_mask): | ||
| return None | ||
|
|
||
| # 根据掩码过滤数据 | ||
| if isinstance(data, dict): | ||
| # 如果data是字典(假设每个key对应一个列表) | ||
| filtered_data = { | ||
| key: [value for value, keep in zip(values, keep_mask) if keep] | ||
| for key, values in data.items() | ||
| } | ||
| elif isinstance(data, list): | ||
| # 如果data是列表 | ||
| filtered_data = [item for item, keep in zip(data, keep_mask) if keep] | ||
| else: | ||
| # 其他情况(如Ray Dataset的批处理) | ||
| raise ValueError("Unsupported data type for batch filtering") | ||
|
|
||
| return filtered_data | ||
|
|
||
|
|
||
| def filter_cpu_single(self, data): | ||
| data = self.op.compute_stats_single(data) | ||
| keep = self.op.process_single(data) | ||
| if keep: | ||
| return data | ||
| else: | ||
| return None | ||
|
|
||
| def filter_cpu_batched(self, data): | ||
| # data = self.op.compute_stats_batched(data, self.model, self.processor) | ||
| data = self.op.compute_stats_batched(data) | ||
| keep_mask = list(self.op.process_batched(data)) # 将map对象转换为列表 | ||
|
|
||
| # 如果没有数据需要保留,返回None | ||
| if not any(keep_mask): | ||
| return None | ||
|
|
||
| # 根据掩码过滤数据 | ||
| if isinstance(data, dict): | ||
| # 如果data是字典(假设每个key对应一个列表) | ||
| filtered_data = { | ||
| key: [value for value, keep in zip(values, keep_mask) if keep] | ||
| for key, values in data.items() | ||
| } | ||
| elif isinstance(data, list): | ||
| # 如果data是列表 | ||
| filtered_data = [item for item, keep in zip(data, keep_mask) if keep] | ||
| else: | ||
| # 其他情况(如Ray Dataset的批处理) | ||
| raise ValueError("Unsupported data type for batch filtering") | ||
|
|
||
| return filtered_data | ||
|
|
||
|
|
||
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.