-
Notifications
You must be signed in to change notification settings - Fork 313
fix: add thread limiting for multi-process workloads to prevent over-subscription #877
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
base: main
Are you sure you want to change the base?
fix: add thread limiting for multi-process workloads to prevent over-subscription #877
Conversation
…subscription When running data processing with multiple worker processes (num_proc > 1), each worker using multiple threads causes severe performance degradation due to thread contention. For example, 3 workers each using N threads creates 3N threads competing for N cores. Changes: - Add setup_worker_threads() utility in process_utils.py that: - Calls torch.set_num_threads(1) and torch.set_num_interop_threads(1) - Only configures once per process (idempotent) - Call setup_worker_threads() in get_model() for worker processes to limit threads before model loading/inference This fix significantly improves multi-process CPU performance by preventing thread over-subscription.
Summary of ChangesHello @XinyuLiu1999, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the performance of CPU-only multi-process workloads, particularly when using models like those based on CLIP. By preventing thread over-subscription in worker processes, it resolves a critical bottleneck that previously led to massive context switching overhead and cache thrashing, transforming execution times from minutes to seconds. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request addresses a performance degradation issue when running multi-process workloads on CPU by limiting PyTorch threads in worker processes. The solution involves adding a setup_worker_threads utility to configure thread limits and calling it during model initialization in worker processes. The changes are logical and directly address the problem described. My review includes a couple of suggestions to improve code style and error handling robustness.
| try: | ||
| import torch | ||
| torch.set_num_threads(num_threads) | ||
| torch.set_num_interop_threads(num_threads) | ||
| logger.debug(f"Set torch threads to {num_threads}") | ||
| except ImportError: | ||
| pass | ||
| except RuntimeError as e: | ||
| # torch.set_num_interop_threads can only be called once | ||
| logger.debug(f"Could not set torch interop threads: {e}") |
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.
The error handling in this block can be improved for better robustness and debugging experience:
- The
except ImportErrorblock currently usespass, which silently ignores the case wheretorchis not installed. Adding alogger.debugmessage would be helpful for tracing. - The
except RuntimeErrormessage is specific toset_num_interop_threads, but aRuntimeErrorcould potentially be raised by other calls in thetryblock. A more generic error message would be more accurate.
| try: | |
| import torch | |
| torch.set_num_threads(num_threads) | |
| torch.set_num_interop_threads(num_threads) | |
| logger.debug(f"Set torch threads to {num_threads}") | |
| except ImportError: | |
| pass | |
| except RuntimeError as e: | |
| # torch.set_num_interop_threads can only be called once | |
| logger.debug(f"Could not set torch interop threads: {e}") | |
| try: | |
| import torch | |
| torch.set_num_threads(num_threads) | |
| torch.set_num_interop_threads(num_threads) | |
| logger.debug(f"Set torch threads to {num_threads}") | |
| except ImportError: | |
| logger.debug("torch not found, skipping thread setup.") | |
| except RuntimeError as e: | |
| # This can happen if threads are already configured (e.g., by another library) | |
| logger.debug(f"Could not set torch threads: {e}") |
cb7e9ef to
192ba93
Compare
060a174 to
192ba93
Compare
Summary
Fixes severe performance degradation when running operators with CLIP-based models (e.g.,
image_aesthetics_filter,image_text_similarity_filter) withnum_proc > 1with CPU-only by limiting PyTorch threads in worker processes.Problem: When multiple worker processes are spawned, each worker defaults to using all CPU cores for PyTorch operations. This causes thread over-subscription (e.g., 3 workers × 8 threads = 24 threads competing for 8 cores), leading to massive context switching overhead and cache thrashing.
Solution: Call
torch.set_num_threads(1)andtorch.set_num_interop_threads(1)in worker processes when loading models, ensuring each worker uses only1thread.Changes:
setup_worker_threads()utility function in process_utils.pyget_model()inmodel_utils.pyfor non-main processesTest with num_proc=3, should complete in seconds instead of minutes with 8-core CPU
from data_juicer.core.data import NestedDataset as Dataset
from data_juicer.ops.filter.image_aesthetics_filter import ImageAestheticsFilter
from data_juicer.utils.constant import Fields
Create dataset with stats column
dataset = Dataset.from_list([{"images": ["tests/ops/data/img1.png"]}] * 10)
dataset = dataset.add_column(name=Fields.stats, column=[{}] * dataset.num_rows)
Run filter
op = ImageAestheticsFilter()
dataset = dataset.map(op.compute_stats, num_proc=3)