Skip to content

fix(dtensor): guard against nullptr from TF_TensorData in ExtractSmallTensorValue #96866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025

Conversation

zqw86713
Copy link
Contributor

@zqw86713 zqw86713 commented Jul 12, 2025

This PR replaces #96430, which had messy commit history due to local squash and rebase attempts.

The code changes are the same, but now consolidated into a single, clean commit that conforms to TensorFlow’s guidelines.


Summary

Adds a defensive check in ExtractSmallTensorValue to handle cases where TF_TensorData() may return nullptr despite a successful TFE_TensorHandleResolve(). This prevents potential segmentation faults when working with invalid, zero-sized, or uninitialized tensors.

Also introduces a focused C++ unit test to validate the nullptr handling path. The test uses Layout::Empty() to ensure coverage without requiring GPU/TPU hardware, enabling rapid verification across all environments.

Local testing:
bazel test //tensorflow/dtensor/... --test_tag_filters=-gpu,-tpu
— All DTensor tests pass on WSL-CPU; full matrix coverage will be validated by CI.


What this PR changes

Area Change
Safety check Add a null-pointer guard before dereferencing the raw buffer returned by TF_TensorData(...); if it’s nullptr, set INVALID_ARGUMENT and return early.
Test coverage New target small_constant_optimization_test in tensorflow/dtensor/cc/BUILD that asserts the function now returns nullopt and the correct error code when given nullptr. The test relies on Layout::Empty() to avoid any device-specific setup.
Scope Applies to all supported scalar types: TF_INT32, TF_INT64, TF_FLOAT, TF_STRING.

Why this matters

  • Prevents hard crashes when tensors are uninitialized or zero-sized.
  • Strengthens DTensor constant-folding reliability across the C ↔ C++ boundary.
  • Provides regression coverage to ensure the path remains safe.

Risk

Low. The patch is a defensive check only; it does not affect behavior for valid tensors.

Recommendation

Safe to merge — improves robustness and now has a self-contained unit-test to prove it.

(Final note for reviewers: see small_constant_optimization_test.cc for the minimal reproduction of the null-tensor case.)

@google-ml-butler google-ml-butler bot added the size:M CL Change Size: Medium label Jul 12, 2025
@zqw86713 zqw86713 marked this pull request as draft July 12, 2025 20:17
@zqw86713 zqw86713 marked this pull request as ready for review July 12, 2025 20:29
@zqw86713
Copy link
Contributor Author

Hi @mihaimaruseac, thank you for your previous feedback in #96866.

I've opened this new PR with the same code changes, but with a cleaned-up commit history (now squashed into a single commit to conform with TensorFlow's standards).

Appreciate it if you could take a look when you have time. Thanks again!

Copy link
Collaborator

@mihaimaruseac mihaimaruseac left a comment

Choose a reason for hiding this comment

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

It would have been better to keep the same change, as now the import into the internal system will be impacted. git rebase -i should be used for squashing commits, but LLMs can hardly perform that.

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jul 12, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jul 13, 2025
@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label Jul 13, 2025
@zqw86713
Copy link
Contributor Author

@mihaimaruseac Thanks for the feedback!
I've cleaned up the commit history using git rebase -i, and the PR now contains a single, clean commit as expected.
The previous version inadvertently included some commits from master, but that has been corrected.
Appreciate your patience and guidance!

@zqw86713
Copy link
Contributor Author

Thanks again for your earlier feedback regarding the commit history.
In the previous PR, I had mistakenly merged master into my feature branch, which introduced unrelated commits. I now understand how that affects the internal import process.
For this PR, I’ve switched to a clean branch and used git rebase -i upstream/master to ensure a linear, single-commit history.
I appreciate your guidance — please let me know if anything else needs adjustment.

@zqw86713
Copy link
Contributor Author

Hi @mihaimaruseac ,

Thank you again for reviewing and merging PR #96429 via the Copybara‑Service—it’s an honor to have made my first contribution to TensorFlow!

I’ve just squashed my two commits and force‑pushed on this PR. Could you please take a look and approve again when convenient? Thank you!

Also, I noticed that in PR #96429 my three commits were merged into master rather than being squashed. I had assumed Copybara‑Service would handle that. To align better with TensorFlow’s workflow, would you recommend I generally keep one commit per PR going forward?

I appreciate your guidance and support!

Best regards,
Qingwei

@mihaimaruseac
Copy link
Collaborator

mihaimaruseac commented Jul 15, 2025

Hi,

#96429 was closed by a merge commit. On master branch there is only the merge commit, your commits are on the merged branch.

It is recommended to not edit the PR after the approval, unless really needed (test failures, requested fixes). Remember that the PR is being imported into the internal system and each modification adds additional delays as the new change also needs to be imported and reviewed again. It might happen that the new change is being reviewed by other people, adding more delays as new people also need to get up to date with the history of the change, etc.

Please only edit PRs if requested by a reviewer. Don't merge master back into the PR, only rebase if GitHub shows a merge conflict.

Right now, because this PR was changed after approval, it needed a new approval here to be imported into the system. I missed that notification, so the PR stalled until today.

mihaimaruseac
mihaimaruseac previously approved these changes Jul 15, 2025
@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jul 15, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jul 15, 2025
@zqw86713
Copy link
Contributor Author

Hi @mihaimaruseac,

Thank you so much for the follow-up and re-approval! I appreciate your time and the clear guidance on the review and merge process. I've taken note of the recommendation to avoid modifying PRs post-approval unless necessary, and I’ll be sure to follow that going forward.

Looking forward to seeing this merged—thank you again!

Best regards,
Qingwei

@zqw86713
Copy link
Contributor Author

Hi @mihaimaruseac ,

I noticed that the CI failure was caused by a SHA‑256 mismatch error while downloading the pygments==2.19.1 wheel:

Expected sha256 61c16… or 9ea15…, but got ab1688…

This indicates that the requirements_lock.txt file needs to be updated to reflect the new checksum. My plan is to re-run:

bazel run //:requirements.update

or the equivalent lockfile generation, and then commit the updated lock file to resolve the mismatch.

Does that approach look good to you? Thanks for your guidance!

Best regards,
Qingwei

@mihaimaruseac
Copy link
Collaborator

I think the actual error to fix is

  /tensorflow/dtensor/cc/small_constant_optimization_test.cc:22 ClangTidy: no header providing "TF_DeleteStatus" is directly included
  /tensorflow/dtensor/cc/small_constant_optimization.cc:108 ClangTidy: no header providing "TF_TensorData" is directly included
  /tensorflow/dtensor/cc/small_constant_optimization_test.cc:13 ClangTidy: no header providing "TF_NewStatus" is directly included
  /tensorflow/dtensor/cc/small_constant_optimization_test.cc:15 ClangTidy: no header providing "std::optional" is directly included
  /tensorflow/dtensor/cc/small_constant_optimization_test.cc:20 ClangTidy: no header providing "TF_INVALID_ARGUMENT" is directly included

The dependency hash mismatch should be fixed by a concurrent change (already happening)

@zqw86713
Copy link
Contributor Author

Hi @mihaimaruseac

Thanks for pointing out the missing headers flagged by Clang-Tidy. I’ll make the necessary changes and push an update within the next two days.

Also, I noticed that these Clang-Tidy issues weren’t visible in the build logs I checked (e.g., build.log or CI output). Could you let me know where I can view these warnings directly in the future?

Appreciate your time and help!

Best regards,
Qingwei

@mihaimaruseac
Copy link
Collaborator

Actually, no need to change anything, it got fixed internally

@zqw86713
Copy link
Contributor Author

Hi @mihaimaruseac ,

Thank you for the update! Glad to hear it’s been resolved internally. I’ll leave the PR as is then.

Let me know if anything else is needed on my side.

Best regards,
Qingwei

Copy link
Collaborator

@MichaelHudgins MichaelHudgins left a comment

Choose a reason for hiding this comment

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

One small change needed, the test file needs a copyright header, please just take it from any other file and update the year as its a new file.

copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 781736287
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
Updates LLVM usage to match
[0d5325bb203f](llvm/llvm-project@0d5325bb203f)

Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783408931
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783830374
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
Generate Reduce Window on outer scan (Complexity 43 -> 40)

Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 782137536
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
…verride

how transfers happen in xla::ifrt::PjRtClient.

Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783881503
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 764901630
MichaelHudgins pushed a commit that referenced this pull request Jul 16, 2025
COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783878611
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783059223
@copybara-service copybara-service bot mentioned this pull request Jul 16, 2025
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783879670
Copy link
Collaborator

@MichaelHudgins MichaelHudgins left a comment

Choose a reason for hiding this comment

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

Approving back in with the header change. Request was already reviewed

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jul 16, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jul 16, 2025
copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783879670
@mihaimaruseac
Copy link
Collaborator

Thanks, Michael!

copybara-service bot pushed a commit that referenced this pull request Jul 16, 2025
Reverts changelist 783839015

FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783938941
@copybara-service copybara-service bot merged commit 4a91065 into tensorflow:master Jul 17, 2025
6 of 7 checks passed
@zqw86713 zqw86713 deleted the pr96430_ut2 branch July 17, 2025 16:45
copybara-service bot pushed a commit that referenced this pull request Jul 17, 2025
This PR updates test assertions in two XLA C++ test files by replacing `EXPECT_THAT(..., IsOkAndHolds(true)) with ASSERT_THAT(...)`.

Rationale:
- Consistency: Aligns with other XLA tests, which use ASSERT for pass.Run() calls when subsequent checks depend on successful execution.
- Correctness: Ensures test failures are caught immediately, as ASSERT_THAT is fatal and prevents further checks from running on invalid state.
FUTURE_COPYBARA_INTEGRATE_REVIEW=#96866 from zqw86713:pr96430_ut2 2fadd84
PiperOrigin-RevId: 783478468
CodeHotel pushed a commit to CodeHotel/tensorflow that referenced this pull request Jul 28, 2025
COPYBARA_INTEGRATE_REVIEW=tensorflow#96866 from zqw86713:pr96430_ut2 1256fef
PiperOrigin-RevId: 783878611
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review Pull request awaiting review ready to pull PR ready for merge process size:M CL Change Size: Medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy