Repository navigation
[Perf] Performance improvement in multi-cpu environments - #88
0oshowero0 merged 1 commit into
Conversation
Signed-off-by: 0oshowero0 <[email protected]>
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a performance optimization for PyTorch operations in multi-CPU environments by limiting automatic parallelism. The change addresses a specific issue where torch.stack() operations suffer from excessive synchronization overhead when PyTorch uses all available CPU threads on high-core-count servers (e.g., 640 CPUs).
Key changes:
- Added a context manager
limit_pytorch_auto_parallel_threads()to control PyTorch's thread usage - Applied the context manager to wrap
torch.stack()operations in the data retrieval path
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| transfer_queue/utils/utils.py | Implements the new context manager with automatic thread count detection based on physical cores |
| transfer_queue/storage.py | Applies the performance optimization by wrapping torch.stack operations with the new context manager |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| """Prevent PyTorch from overdoing the automatic parallelism during torch.stack() operation""" | ||
| pytorch_current_num_threads = torch.get_num_threads() | ||
| logical_cores = psutil.cpu_count(logical=True) | ||
| physical_cores = psutil.cpu_count(logical=False) |
There was a problem hiding this comment.
The function may crash if psutil.cpu_count(logical=False) returns None on systems where physical core count cannot be determined. Add a None check and fallback to logical cores or raise a more informative error.
| physical_cores = psutil.cpu_count(logical=False) | |
| physical_cores = psutil.cpu_count(logical=False) | |
| if physical_cores is None: | |
| # Fallback: use logical cores if physical core count cannot be determined | |
| physical_cores = logical_cores |
| if physical_cores >= 16: | ||
| target_num_threads = 16 | ||
| else: | ||
| target_num_threads = physical_cores |
There was a problem hiding this comment.
If physical_cores is None (which can happen on some systems), this comparison will fail. The None check from Comment 1 would prevent this, but this logic also needs to handle the case defensively.
| if physical_cores >= 16: | |
| target_num_threads = 16 | |
| else: | |
| target_num_threads = physical_cores | |
| if physical_cores is not None: | |
| if physical_cores >= 16: | |
| target_num_threads = 16 | |
| else: | |
| target_num_threads = physical_cores | |
| else: | |
| # Fallback: use logical_cores if physical_cores is unavailable | |
| if logical_cores is not None: | |
| if logical_cores >= 16: | |
| target_num_threads = 16 | |
| else: | |
| target_num_threads = logical_cores | |
| else: | |
| # As a last resort, use 1 | |
| target_num_threads = 1 |
|
|
||
| @contextmanager | ||
| def limit_pytorch_auto_parallel_threads(target_num_threads: Optional[int] = None): | ||
| """Prevent PyTorch from overdoing the automatic parallelism during torch.stack() operation""" |
There was a problem hiding this comment.
The docstring lacks important information about parameters, return values, usage examples, and the rationale for the default threshold of 16 cores. Consider adding a comprehensive docstring with Args, Yields, and Raises sections, plus an example showing how to use this context manager.
| """Prevent PyTorch from overdoing the automatic parallelism during torch.stack() operation""" | |
| """ | |
| Context manager to limit the number of threads used by PyTorch for automatic parallelism, | |
| particularly during operations like `torch.stack()`. This can help prevent excessive CPU | |
| usage and contention on machines with many cores. | |
| By default, if `target_num_threads` is not specified, the function sets the number of threads | |
| to 16 if the machine has at least 16 physical cores. This threshold is chosen because, on | |
| high-core-count machines, PyTorch's default behavior can lead to oversubscription and | |
| performance degradation. If there are fewer than 16 physical cores, all available physical | |
| cores are used. | |
| Args: | |
| target_num_threads (Optional[int]): The number of threads to set for PyTorch operations. | |
| If None, defaults to 16 if there are at least 16 physical cores, otherwise uses the | |
| number of physical cores. | |
| Yields: | |
| None. This is a context manager; code inside the `with` block will run with the specified | |
| number of PyTorch threads. | |
| Raises: | |
| RuntimeError: If `target_num_threads` is greater than the number of logical CPU cores. | |
| Example: | |
| >>> from transfer_queue.utils.utils import limit_pytorch_auto_parallel_threads | |
| >>> import torch | |
| >>> with limit_pytorch_auto_parallel_threads(8): | |
| ... # PyTorch will use at most 8 threads in this block | |
| ... x = torch.stack([torch.ones(10) for _ in range(1000)]) | |
| # After the block, the original thread setting is restored. | |
| """ |
Background
We observe significant performance variance in the
torch.stack()operation under a multi-CPU environment. By default, PyTorch enables automatic parallelism using all available CPU resources, which may introduce excessive synchronization overhead.On a 640-CPU server, the following code takes 5–11 seconds to complete. By uncommenting the line
torch.set_num_threads(5), the execution time is reduced to 0.13–0.46 seconds.Solution
We provide a context manager
limit_pytorch_auto_parallel_threads()that can be used to wrap code fragments involvingtorch.stack(), helping improve performance.