Skip to content

Fix #418: add tests for padded_to_ragged2d#1068

Open
mhucka wants to merge 4 commits into
tensorflow:masterfrom
mhucka:fix-418
Open

Fix #418: add tests for padded_to_ragged2d#1068
mhucka wants to merge 4 commits into
tensorflow:masterfrom
mhucka:fix-418

Conversation

@mhucka
Copy link
Copy Markdown
Member

@mhucka mhucka commented May 12, 2026

This adds some simple tests for an untested function intfq_utility_ops_test.py.

mhucka added 2 commits May 12, 2026 03:14
This adds some simple tests for an untested function in
`tfq_utility_ops_test.py`.
@mhucka mhucka marked this pull request as ready for review May 12, 2026 03:37
Copy link
Copy Markdown
Contributor

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

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 updates tfq_utility_ops_test.py by correcting a typo, adding more test cases to test_padded_to_ragged, and implementing a new test for padded_to_ragged2d. Feedback indicates that the logic in test_padded_to_ragged2d contains an error in tensor slicing that only works for square matrices and suggests that the test implementation is too similar to the actual code, potentially missing bugs.

Comment on lines +170 to +173
col_mask = tf.abs(tensor_arr[:, 0]) < 1.1
masked = tf.ragged.boolean_mask(tensor_arr, col_mask)
mask = tf.abs(masked) < 1.1
expected = tf.ragged.boolean_mask(masked, mask)
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.

high

The logic used to calculate the expected value in test_padded_to_ragged2d is flawed and only works for square matrices.

  1. Incorrect Slicing: tensor_arr[:, 0] selects the first row of each matrix in the batch, resulting in a mask of shape [batch, columns]. When used in tf.ragged.boolean_mask(tensor_arr, col_mask), it attempts to mask the second dimension (rows) of tensor_arr. This will cause a shape mismatch error if the number of rows does not equal the number of columns. To correctly mask rows based on their first element, you should use tensor_arr[:, :, 0].
  2. Tautological Test: The test currently replicates the implementation of the op it is testing. This means that if the op contains a logic error (which it does, as it uses the same incorrect slicing), the test will still pass, failing to catch the bug.

Note that the implementation of padded_to_ragged2d in tfq_utility_ops.py likely requires a similar fix.

Suggested change
col_mask = tf.abs(tensor_arr[:, 0]) < 1.1
masked = tf.ragged.boolean_mask(tensor_arr, col_mask)
mask = tf.abs(masked) < 1.1
expected = tf.ragged.boolean_mask(masked, mask)
row_mask = tf.abs(tensor_arr[:, :, 0]) < 1.1
masked_rows = tf.ragged.boolean_mask(tensor_arr, row_mask)
element_mask = tf.abs(masked_rows) < 1.1
expected = tf.ragged.boolean_mask(masked_rows, element_mask)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant