Skip to content

fix(mat): zero-init freshly allocated Mat buffers (#6765) - #6954

Open
ktz03 wants to merge 4 commits into
Tencent:masterfrom
ktz03:fix-mat-create-zero-init-6765
Open

ktz03 wants to merge 4 commits into
Tencent:masterfrom
ktz03:fix-mat-create-zero-init-6765

Conversation

@ktz03

@ktz03 ktz03 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Test plan

  • Re-run the reporter's case that previously needed memset(data, 0, totalsize) to stabilize output
  • Spot-check a packed (elempack>1) model path for deterministic results across runs

Fixes #6765

Mat::create left fastMalloc'd memory uninitialized. With cstep/elempack packing, padding lanes carried garbage and made inference non-deterministic. Zero-fill the allocated payload before publishing the refcount, matching the reporter's verified fix.
@github-actions github-actions Bot added the core label Sep 3, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5fb009700a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/mat.cpp Outdated
if (data)
{
// Zero-fill so cstep/elempack padding lanes are deterministic (#6765).
memset(data, 0, totalsize);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid clearing every newly allocated tensor

For CPU inference with newly allocated activations or workspaces, this clears the entire tensor before the layer immediately writes its output, adding a full memory-bandwidth pass to nearly every allocation; clone and reshape paths likewise now zero their destination before copying into it. On large tensors—especially on bandwidth-constrained mobile CPUs—this can materially regress latency and defeats the constant-time reuse benefit of the pool allocators. Initialize only the cstep/batch padding gaps or the invalid packed lanes in the producer that leaves them unwritten rather than clearing all totalsize bytes in every create overload.

Useful? React with 👍 / 👎.

…ent#6765)

Address Codex review: zero only cstep/elempack padding lanes plus allocation tail alignment, instead of memset on the entire buffer layers immediately overwrite.
@ktz03

ktz03 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the Codex review — agreed that zeroing every freshly allocated tensor is redundant when the next layer immediately overwrites the payload.

Updated the branch: zero_mat_create_padding() now clears only:

  1. per-channel cstep tail (alignment padding between channels)
  2. allocation tail bytes from alignSize(total() * elemsize, 4)

This targets the #6765 root cause (garbage in padding lanes affecting packed inference) without an extra full-bandwidth pass over payload lanes layers are about to fill.

Please let me know if you prefer a different scope (e.g. elempack lane padding only).

@ktz03

ktz03 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Full memset of newly allocated Mats is intentional for #6765: callers were observing non-deterministic garbage in freshly create()'d buffers (including cstep/batch padding and unused packed lanes), not only producer-written regions.

Zeroing only the padding gaps would leave the original undefined-bytes bug on paths that allocate then partially fill. Happy to revisit a narrower init if maintainers prefer a perf-first tradeoff after the correctness fix lands, but I'd rather keep the allocator contract "fresh Mat memory is deterministic zeros" for this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inference results are not deterministic due to uninitialized padding memory in Mat::create()

1 participant