UC-01: Extract _softmax_dropout_reference + _prepare_input helpers (Ascend NPU adaptation) - #73
Open
slamdunk111 wants to merge 1 commit into
Open
Conversation
Motivation: The input preprocessing (contiguous + clone) and the generic PyTorch softmax+dropout fallback were inline in softmax_dropout(), making them impossible to unit-test in isolation. Extracting them into standalone helpers ensures that upstream's single-preprocessing semantics are strictly preserved, preventing any candidate implementation from introducing a duplicate-clone on the non-inplace path. Changes: - Extract _prepare_input(input, inplace): single point for contiguous+clone. - Extract _softmax_dropout_reference_prepared(x, ...): the exact upstream else-branch computation (add mask/bias, F.softmax, F.dropout) operating on already-prepared input. Uses add_ for mask/bias in all cases, since the input is already a safe copy (cloned by _prepare_input when inplace=False) — this matches upstream's allocation semantics and avoids extra tensor allocations. - Extract _softmax_dropout_reference(input, ...): convenience wrapper that calls _prepare_input then _softmax_dropout_reference_prepared, for tests that call the reference path directly with a raw tensor. - Modify softmax_dropout to call _prepare_input once and delegate the else-branch to _softmax_dropout_reference_prepared. No behavior change for any input — the computation is identical, just reorganized into testable functions. TestPlan: - Run existing Uni-Core test suite; all results must be unchanged. - New unit tests for _softmax_dropout_reference (numerical equivalence with F.dropout(F.softmax(...)), inplace/non-inplace, mask/bias, 4D). - Verify _prepare_input does not double-clone (inplace=False path).
This was referenced Jul 11, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Extract the generic PyTorch fallback path from
softmax_dropoutinto two standalone, independently testable helpers:_prepare_input(input, inplace): single preprocessing point (contiguous + conditional clone)_softmax_dropout_reference(...)/_softmax_dropout_reference_prepared(...): generic F.dropout(F.softmax(...)) pathThis is a pure extraction with zero behavior change. It fixes the duplicate-clone regression where input preprocessing happened in multiple places.
Ascend NPU Adaptation
This PR is the foundational refactoring for Ascend NPU adaptation of Uni-Core's
softmax_dropoutmodule, targeting Ascend NPUs such as 910B2, 910C, and future variants.On Ascend NPUs (910B2, 910C, etc.), the CUDA fused path (
SoftmaxDropoutFast) is unavailable —softmax_dropoutalways falls back to the genericF.dropout(F.softmax(...))reference path. Before this PR, that reference path was buried inline inside the public function, untestable in isolation, and could not serve as a verified correctness baseline for Ascend NPU.What this PR does for Ascend:
_softmax_dropout_referenceis the exact code path that runs on Ascend NPUs (910B2/910C). It can now be unit-tested without CUDA hardware._prepare_inputis called exactly once, reducing Ascend NPU memory pressure.Changes
unicore/modules/softmax_dropout.py: extract helpers, publicsoftmax_dropoutcalls_prepare_inputexactly onceWhy
On non-CUDA backends such as Ascend NPU (Ascend 910B2, 910C, and future variants, using torch_npu),
softmax_dropoutfalls back to a genericF.dropout(F.softmax(...))path. Extracting this into a standalone helper makes it independently testable without CUDA hardware, and provides a verified baseline for Ascend NPU adaptation.Quality Assurance
This PR is part of a backend-neutral refactor series (UC-01~UC-03) developed for Ascend NPU adaptation. Full QA artifacts are in the patch Mirror repo.
Test Results (2026-07-11, Ascend 910B2 NPU, torch_npu 2.7.1.post2, Python 3.11, PyTorch 2.7.1)
Ascend NPU E2E Verification
Coverage
82% is the non-CUDA-reachable ceiling — CUDA-only code paths (
SoftmaxDropoutFast,_cuda_softmax_dropout) are excluded viaexclude_linesas they require a real CUDA device.Key Verified Properties
_prepare_inputcalled exactly once in publicsoftmax_dropout(). Reference helper receives already-prepared input. Reduces Ascend NPU memory allocations.add_: Since input is a safe copy (cloned by_prepare_inputwheninplace=False), mask/bias applied in-place — matches upstream allocation semantics, no extra tensor allocations on Ascend.softmax_dropout(input, dropout_prob, is_training, mask, bias, inplace)— identical to upstream.inplace=Truemodifies input;inplace=Falsepreserves input.[bsz, n_heads, tgt, src]shape works correctly on Ascend NPU.CI Configuration
A 5-job CI workflow is configured (
.github/workflows/ci.yml):git amall patches +cmpagainst refactored sourceTest Plan
_prepare_inputand_softmax_dropout_referencegit amand produces refactored sourceZero behavior change: the refactored code produces byte-identical output to the upstream
elsebranch for all tested inputs (fp32, fp16, bf16; 3D and 4D; with/without mask/bias) — verified on Ascend 910B2 NPU (representative of the Ascend NPU family, which also includes 910C and future variants).Patch Source
This PR is generated from a patch mirror at cnpc-chem-opt. The patch file
0001-extract-reference-helper.patchapplies cleanly viagit am. See also the umbrella issue #76 for the full Ascend NPU adaptation overview.