Dynamic Thresholding of the Predicted x0
Problem Statement
Imagen's dynamic thresholding prevents saturated images at high guidance by clamping the predicted x0 to a percentile-derived range each step. Implement it.
Background
Given the predicted x0 and a percentile p (e.g. 99.5), compute s = percentile(|x0|, p) over all elements. If s < 1, set s = 1 (never shrink below the natural [-1, 1] range). Then clip x0 to [-s, s] and rescale by dividing by s, pushing saturated pixels back inward:
s=max(percentile(∣x0∣,p),1),x0′=sclip(x0,−s,s)
Use linear interpolation for the percentile (NumPy's default).
Your Task
Implement:
def dynamic_threshold(x0, p):
Return the thresholded values as a list rounded to 4 decimals.
Input Format
- x0: list of floats.
- p (float): percentile in [0, 100].
Output Format
- A list of floats rounded to 4 decimals.
Sample
print(dynamic_threshold([2.0, -2.0, 0.5, 0.0], 50.0))
Output:
[1.0, -1.0, 0.4, 0.0]
Example:
print(dynamic_threshold([2.0, -2.0, 0.5, 0.0], 50.0))
[1.0, -1.0, 0.4, 0.0]
- Compute the absolute values of the input list [2.0,−2.0,0.5,0.0] to get [2.0,2.0,0.5,0.0], which are then sorted as [0.0,0.5,2.0,2.0] to prepare for percentile calculation.
- Determine the 50th percentile (median) of these absolute values using linear interpolation; with 4 elements, the median is the average of the two middle values: sraw=20.5+2.0=1.25.
- Apply the lower bound constraint by taking the maximum of the calculated percentile and 1: s=max(1.25,1.0)=1.25, ensuring the threshold never shrinks the range below [−1,1].
- Clip each original value in x0 to the range [−1.25,1.25]; since all values (2.0,−2.0,0.5,0.0) are within this range, the clipped list remains [2.0,−2.0,0.5,0.0].
- Rescale the clipped values by dividing each by s=1.25 to normalize them: 1.252.0=1.6, 1.25−2.0=−1.6, 1.250.5=0.4, and 1.250.0=0.0.
- The final output is [1.0, -1.0, 0.4, 0.0]
Constraints:
1 <= len(x0) <= 100000,0 <= p <= 100.s = max(percentile(|x0|, p), 1.0)(linear-interpolation percentile).- Clip to
[-s, s], divide bys; round to 4 decimals; avoid-0.0.
1. Background Knowledge
In diffusion models, the network often predicts the original clean sample x0 from a noisy observation. When classifier-free guidance is applied, the predicted x0 can contain extreme values that saturate the image (all pixels pinned to the maximum or minimum of the data range). Dynamic thresholding is a post-processing step introduced in Imagen that adaptively rescales these predictions so that the bulk of the distribution stays within a sensible range while still allowing rare outliers to exist.
The core idea is to compute a percentile of the absolute values of x0. This percentile acts as a soft upper bound: most values will be below it, but a small fraction (determined by p) may exceed it. By clipping to this bound and then dividing by it, we effectively normalize the output so that the largest typical magnitude becomes 1.0, preventing saturation while preserving relative structure.
The formula is:
s=max(percentile(∣x0∣,p),1),x0′=sclip(x0,−s,s)The max(⋅,1) term ensures we never shrink the range below the natural [−1,1] interval, which is the typical output range for image data. If the percentile is already below 1, the data is well-behaved and no aggressive rescaling is needed.
2. Algorithm Approach
This is a straightforward vectorized transformation problem. The approach involves:
- Compute the absolute values of all elements in x0.
- Find the p-th percentile of those absolute values using linear interpolation (NumPy's default method).
- Clamp the percentile to at least 1.0.
- Clip each element of x0 to the range [−s,s].
- Divide every clipped element by s.
- Round the result to 4 decimal places and return as a list.
No sorting or iterative logic is required beyond the percentile computation, which NumPy handles internally.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.