Skip to content

perf(tensor): streamline broadcasting and dense tensor reordering - #7

Merged
BESTTOOLBOX merged 1 commit into
mainfrom
codex/qwen-head-tensor-iteration-performance
Nov 12, 2026
Merged

BESTTOOLBOX merged 1 commit into
mainfrom
codex/qwen-head-tensor-iteration-performance

Conversation

@AndyLu666

Copy link
Copy Markdown
Collaborator

Summary

  • Replace per-element broadcast coordinate allocation/division with precomputed aligned strides and contiguous row loops; preserve operand order and cache loop bounds.
  • Correct the adjacent zero/singleton broadcast-shape bug found in review: 0 with 1 must stay 0, not become 1 and read an empty tensor.
  • Share dtype-aware contiguous-block copying across dense transpose/permute materialization, retaining the existing member 2-D transpose fast path and existing gradient registration. This also corrects permute's previous float-pointer copying for non-FP32 tensors.
  • Add independent coordinate-oracle regression tests and Qwen output-head forward/backward coverage. Upstream already uses matmul_rhs_T, so the output-head production code is intentionally unchanged.

This is a small adaptation of local performance work to the upstream dense/eager tensor representation, not a wholesale copy of the local strided-view implementation. The separate reshape-gradient change in #6 is not duplicated here. No optimizer/hyperparameter, checkpoint format, model weights, or datasets are changed.

Validation

  • Pure C++ Release with assertions retained and warnings as errors: 22/22 CTest exits successful.
  • macOS Accelerate Release with assertions retained: 22/22 CTest exits successful.
  • In each suite, TokenizerHFGolden internally skips because no external fixture is configured: 21 exercised tests + 1 fixture-dependent skip, not 22 fully exercised tests.
  • New tests: 1395 copy/broadcast checks across seven dtypes and both operand orders, including empty/singleton broadcasts and add/mul gradient reductions; 8 output-head forward/backward comparisons.
  • AddressSanitizer + UndefinedBehaviorSanitizer: both new tests pass; custom memory pool and leak detection disabled.
  • Legacy autograd: 1363 forward copy/broadcast checks pass.
  • Android arm64-v8a/API 24: operator static library cross-build passes with NDK 27 and warnings as errors. No phone execution was performed.
  • Fixed-seed synthetic two-layer Qwen, 10 Adam updates versus upstream base, separately in pure C++ and Accelerate builds: raw FP32 losses, all LoRA gradients and all post-update parameters are byte-identical within each backend (71720 bytes per record file). This is a tiny numerical regression, not a full-size task-accuracy evaluation.
  • Package installation succeeds.

Reproduction commands and boundaries are in docs/tensor_iteration_performance.md.

Limits

No end-to-end speedup number is claimed for upstream. Earlier local Qwen/PIQA timing/accuracy figures used a different tensor implementation and cannot be transferred to this PR. No RSS improvement, official task accuracy, Linux runtime, Termux/phone result, or strict PyTorch/ORT numerical parity is claimed.

The existing source-archive data/ export-ignore issue is not part of this performance change. Use a full repository checkout for CMake's dataset-loader sources.

Precompute broadcast row strides and copy contiguous trailing blocks.
Preserve dense ownership and autograd registration; cover Qwen head parity.
Correct empty/singleton broadcast shapes and dtype-aware permute copying.
@BESTTOOLBOX
BESTTOOLBOX merged commit 8059265 into main Nov 12, 2026
2 checks passed
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