Skip to content

check element dtype against parent in batch_util ValidateInput - #130332

Open
madib06ops wants to merge 1 commit into
tensorflow:masterfrom
madib06ops:batch-util-slice-dtype-check
Open

madib06ops wants to merge 1 commit into
tensorflow:masterfrom
madib06ops:batch-util-slice-dtype-check

Conversation

@madib06ops

Copy link
Copy Markdown
Contributor

batch_util::ValidateInput only compares element counts, so CopyElementToSlice, CopySliceToElement and MaybeMoveSliceToElement copy a slice using one tensor's dtype while indexing the other's buffer; when a tf.data element's dtype differs from the batch tensor (for example a crafted SnapshotChunkDataset file whose records are parsed with Tensor::FromProto and never checked against output_types), Batch writes num_values*sizeof(T) bytes at the wrong stride past the end of the batch buffer, and on TF 2.20 an int8 record followed by an int64 record of 65536 elements batched by 2 corrupts the heap and segfaults. This rejects mismatched dtypes in ValidateInput, matching the check CopyContiguousSlices and MaybeMoveContiguousSlices already do, and adds DifferentDtypes tests to batch_util_test.cc that fail before the change and pass after.

CopyElementToSlice, CopySliceToElement and MaybeMoveSliceToElement only
compared element counts before indexing the parent with base<T>() of one
side's dtype, so an element whose dtype differs from the parent was
copied with the wrong element size, past the end of the parent buffer.
Reject mismatched dtypes the same way CopyContiguousSlices and
MaybeMoveContiguousSlices already do.
@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Nov 14, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a data type validation check in ValidateInput within tensorflow/core/util/batch_util.cc to ensure that the element and parent tensors have matching data types, returning an absl::InternalError if they do not. It also adds corresponding unit tests in tensorflow/core/framework/batch_util_test.cc to verify this behavior for CopyElementToSlice, CopySliceToElement, and MaybeMoveSliceToElement. There are no review comments to evaluate, and the implementation is correct and well-tested, so I have no feedback to provide.

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

Labels

size:S CL Change Size: Small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant