Gradient Clipping by Value and Global Norm

Quick Overview

Implement element-wise value clipping and joint global L2 norm clipping for a list of NumPy gradient arrays. It tests numerical care with all-zero gradients, returning float64 copies that leave the inputs untouched, and how the two clipping methods differ in their effect on the update direction.

Gradient Clipping by Value and Global Norm

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given a list of NumPy gradient arrays `grads`, implement two functions: - `clip_by_value(grads, clip_value)` clips every element to `[-clip_value, clip_value]`. - `clip_by_global_norm(grads, max_norm)` rescales all gradients jointly so their combined L2 norm is at most `max_norm`. The functions must meet these requirements: - `clip_by_value` returns a list of clipped arrays. `clip_by_global_norm` returns `(clipped_grads, global_norm)`, where `global_norm` is the pre-clipping norm as a float. - The global norm is `sqrt(sum of squares over every element of every array)`. If it exceeds `max_norm`, multiply every array by `max_norm / global_norm`. - If the norm is at most `max_norm` (including all-zero gradients), the arrays are returned unchanged, with no `NaN`. - Outputs are `float64` (even for integer inputs), keep the input shapes, and never mutate the inputs. - If `clip_value` or `max_norm` is at most 0, raise `ValueError`. Example: ```python grads = [np.array([3.0, 4.0]), np.array([[12.0]])] clip_by_global_norm(grads, 6.5) # -> ([array([1.5, 2.0]), array([[6.0]])], 13.0) clip_by_value([np.array([-3.0, 0.7, 2.5])], 1.0) # -> [array([-1.0, 0.7, 1.0])] ``` ```hint Zero gradients Treat the whole list as one long vector when you compute the norm, and check which branch of your code would divide by the norm when every gradient is zero. ``` ### What a Strong Answer Covers - Computes one norm over every element of every array, instead of a separate norm per array. - Applies a single shared scale factor only when the norm exceeds `max_norm`, with no division by zero for all-zero gradients. - Returns new `float64` arrays with the input shapes and leaves the caller's arrays untouched, including when an input is already `float64`. - Validates both thresholds and raises `ValueError` for non-positive values. - Explains how the two methods differ in their effect on the update direction. ### Follow-up Questions - Why prefer global-norm clipping over per-element value clipping? - What happens if one gradient contains `inf` or `NaN`, and how would you guard the training step against it? - How would you compute the global norm without overflow when gradient values are very large? - In data-parallel or mixed-precision training, where in the step should clipping happen?

Overview: Implement element-wise value clipping and joint global L2 norm clipping for a list of NumPy gradient arrays. It tests numerical care with all-zero gradients, returning float64 copies that leave the inputs untouched, and how the two clipping methods differ in their effect on the update direction.

PracHub logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Given a list of NumPy gradient arrays grads, implement two functions:

  • clip_by_value(grads, clip_value) clips every element to [-clip_value, clip_value] .
  • clip_by_global_norm(grads, max_norm) rescales all gradients jointly so their combined L2 norm is at most max_norm .

The functions must meet these requirements:

  • clip_by_value returns a list of clipped arrays. clip_by_global_norm returns (clipped_grads, global_norm) , where global_norm is the pre-clipping norm as a float.
  • The global norm is sqrt(sum of squares over every element of every array) . If it exceeds max_norm , multiply every array by max_norm / global_norm .
  • If the norm is at most max_norm (including all-zero gradients), the arrays are returned unchanged, with no NaN .
  • Outputs are float64 (even for integer inputs), keep the input shapes, and never mutate the inputs.
  • If clip_value or max_norm is at most 0, raise ValueError .

Example:

grads = [np.array([3.0, 4.0]), np.array([[12.0]])]
clip_by_global_norm(grads, 6.5)
# -> ([array([1.5, 2.0]), array([[6.0]])], 13.0)

clip_by_value([np.array([-3.0, 0.7, 2.5])], 1.0)
# -> [array([-1.0, 0.7, 1.0])]

What a Strong Answer Covers Guidance

  • Computes one norm over every element of every array, instead of a separate norm per array.
  • Applies a single shared scale factor only when the norm exceeds max_norm , with no division by zero for all-zero gradients.
  • Returns new float64 arrays with the input shapes and leaves the caller's arrays untouched, including when an input is already float64 .
  • Validates both thresholds and raises ValueError for non-positive values.
  • Explains how the two methods differ in their effect on the update direction.

Follow-up Questions Guidance

  • Why prefer global-norm clipping over per-element value clipping?
  • What happens if one gradient contains inf or NaN , and how would you guard the training step against it?
  • How would you compute the global norm without overflow when gradient values are very large?
  • In data-parallel or mixed-precision training, where in the step should clipping happen?