Skip to content

Perf: batch all_reduce, replace deprecated torch.inverse, optimize hot-path numpy ops, enable pin_memory - #1

Draft
xukechun with Copilot wants to merge 2 commits into
mainfrom
copilot/improve-slow-code-performance
Draft

Perf: batch all_reduce, replace deprecated torch.inverse, optimize hot-path numpy ops, enable pin_memory#1
xukechun with Copilot wants to merge 2 commits into
mainfrom
copilot/improve-slow-code-performance

Conversation

Copilot AI commented Mar 16, 2026

Copy link
Copy Markdown

Targeted performance improvements across training loop, data loading, and matrix operations.

Distributed training

  • Batch reduce_metrics: Single dist.all_reduce on a stacked tensor instead of N separate calls — eliminates N-1 unnecessary synchronization barriers per invocation
  • Reduce sync frequency: reduce_metrics now only called at save/log intervals; per-iteration printing uses local averages

Data loading

  • pin_memory=True on all DataLoaders — enables async CPU→GPU transfer paired with existing non_blocking=True in preprocess_data()

Deprecated API replacement

  • torch.inverse()torch.linalg.inv() across 5 files (11 callsites) — deprecated since PyTorch 1.9, linalg.inv is better optimized on modern CUDA backends

NumPy hot-path optimization

  • find_closest_ind: Replace 25-line np.digitize + 3 mask branches + concatenation with 5-line vectorized np.searchsorted + np.clip:
def find_closest_ind(train, query):
    idx = np.searchsorted(train, query, side='left')
    idx = np.clip(idx, 1, len(train) - 1)
    left = idx - 1
    right = idx
    return np.where(np.abs(query - train[left]) < np.abs(query - train[right]), left, right)
  • np.digitizenp.searchsorted in align.py and ensemble.pydigitize wraps searchsorted internally

Transform object overhead

  • pad2square: Replace per-call v2.Pad() construction with direct F.pad, with reshape handling for 6D+ tensors in replicate mode

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

… torch.linalg.inv, optimize find_closest_ind, add pin_memory, use F.pad, replace np.digitize with np.searchsorted

Co-authored-by: xukechun <48900301+xukechun@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements to slow code Perf: batch all_reduce, replace deprecated torch.inverse, optimize hot-path numpy ops, enable pin_memory Mar 16, 2026
Copilot AI requested a review from xukechun March 16, 2026 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants