Skip to content

Add tests for tf.linalg.global_norm - #130336

Open
Shizoqua wants to merge 1 commit into
tensorflow:masterfrom
Shizoqua:add-global-norm-test
Open

Shizoqua wants to merge 1 commit into
tensorflow:masterfrom
Shizoqua:add-global-norm-test

Conversation

@Shizoqua

Copy link
Copy Markdown
Contributor

tf.linalg.global_norm (used by tf.clip_by_global_norm for gradient clipping) had no direct tests. It was only covered through clip_by_global_norm, which does not check its dtype handling or its input validation.

This adds tests to clip_ops_test.py for the behavior described in its docstring:

  • list and tuple inputs, with None entries ignored
  • IndexedSlices, where only the values count
  • a norm of 0 for an empty list or a list of only None
  • float16, bfloat16 and float64 inputs keep their dtype
  • TypeError for a tensor, a string or a dict instead of a sequence

Test only, no library changes and no new dependencies.

Testing: I did not run Bazel locally. I ran clip_ops_test.py with Python against tf-nightly 2.23.0-dev20261107 (its clip_ops.py is identical to master):

  • before: 33 tests OK, 1 skipped. After: 38 tests OK (5 new), the same 1 skipped.
  • I checked the new tests catch regressions by breaking global_norm in five ways (keeping None entries, removing the TypeError check, a wrong norm value, returning float32 for every input, counting the IndexedSlices dense shape). The new tests fail for all five. The existing tests miss two of them: the removed TypeError check and the lost dtype.
  • pylint --rcfile=tensorflow/tools/ci_build/pylintrc on the changed file: 10.00/10

global_norm was only tested through clip_by_global_norm. Add direct
tests for the documented behavior: list and tuple inputs, ignored None
entries, IndexedSlices (only the values count), a zero norm for an empty
or all-None list, keeping the float16, bfloat16 and float64 dtypes, and
the TypeError for inputs that are not sequences.
@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 adds comprehensive unit tests for the global_norm operation in tensorflow/python/kernel_tests/math_ops/clip_ops_test.py. The new tests cover standard global norm calculations, handling of IndexedSlices, empty or None inputs, preservation of data types (such as float16, bfloat16, and float64), and verification that appropriate exceptions are raised for non-sequence inputs. There are no review comments, and 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