fix(compute): GPUEngine.Reshape honors dst argument (#81)#82
Merged
Conversation
The zero-copy GPUStorage and Float16Storage fast-paths in GPUEngine.Reshape used to construct a fresh tensor aliasing the source storage and silently drop the caller-provided dst, violating the compute.Engine.Reshape contract shared with CPUEngine. Callers that followed the common pattern of passing a pre-allocated dst and discarding the return value silently received stale pre-allocated storage. This is the root cause of zerfoo PatchTST GPU training convergence freeze (loss=0.268357 across all epochs on DGX GB10) tracked in #79. A one-line zerfoo workaround landed in zerfoo/zerfoo#371; this PR fixes the contract on the ztensor side so the next caller cannot hit the same trap. Fix: when dst is provided, mutate dst[0] to alias the reshaped view (SetStorage + SetShape + SetStrides) and return dst[0]. When no dst is provided, preserve current behavior (return a fresh tensor wrapping the view). The new aliasReshapeDst helper centralizes the dst-honoring logic for both Float16Storage and GPUStorage[T] fast-paths. Adds compute/gpu_reshape_dst_test.go with two regression tests: - TestGPUEngine_Reshape_HonorsDst: pre-allocates dst with a poison pattern, runs Reshape with dst, asserts (a) ret == dst, (b) shape is updated, (c) dst data reflects src not poison. - TestGPUEngine_Reshape_NoDst: preserves the no-dst fast-path behavior — returns a fresh tensor with correct shape and data. Both tests skip when CUDA is unavailable. Full ztensor test suite passes on CPU host. Closes #81 Refs #79 Refs zerfoo/zerfoo#371
This was referenced Apr 10, 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
GPUEngine.Reshapeignored the caller-provideddstand returned a fresh tensor aliasing the source storage. Callers that passed a pre-allocateddstand discarded the return value silently received stale pre-allocated storage — exactly the failure mode that froze PatchTST GPU training (loss=0.268357 across all epochs) on DGX GB10.dstis provided, mutatedst[0]to alias the reshaped view (SetStorage+SetShape+SetStrides) and returndst[0]. When nodstis provided, preserve current behavior. NewaliasReshapeDsthelper centralizes the dst-honoring logic for both Float16Storage and GPUStorage[T] fast-paths.Evidence (from #79 / zerfoo/zerfoo#371 investigation)
First-batch GPU probe in PatchTST backward, captured on DGX GB10:
MatMul wrote real gradients into
fc.dFlat. The very next call,engine.Reshape(ctx, fc.dFlat, [1600,64], fc.dX), returned a fresh tensoraliasing
fc.dFlatbut leftfc.dX(the dst) untouched at its pre-allocatedzeros. Caller discarded the return → encoderBackward saw all-zero input →
every downstream gradient was zero → frozen loss.
Tests
compute/gpu_reshape_dst_test.go:ret == dst, (b) shape becomes[2,8], (c) dst data reflects src (1..16) not poison. This is the regression test for the silent-zero trap.Both tests skip when CUDA is unavailable (consistent with other GPU tests in this package).
Test plan
go build ./...cleango vet ./...cleangofmtcleango test ./...ztensor test suite passes on CPU hostRefs