Noise Level at a Target SNR
Problem Statement
Given a schedule of alpha_bar values, find the timestep whose signal-to-noise ratio is closest to a target SNR — the operation behind picking where to start editing/inpainting in the middle of a trajectory.
Background
The per-step SNR is SNR_t = alpha_bar_t / (1 - alpha_bar_t), monotonically decreasing in t. To place an operation at a desired noise level you pick the timestep whose SNR is nearest the target:
t⋆=argmint∣SNRt−target∣
Ties go to the smaller timestep. A schedule value of exactly 1.0 has infinite SNR; treat its distance to any finite target as infinite so it is never chosen unless it is the only option.
Your Task
Implement:
def timestep_for_snr(alpha_bar, target_snr):
Return the chosen timestep index (an int).
Input Format
- alpha_bar: list of cumulative products.
- target_snr (float): the desired SNR, >= 0.
Output Format
- A single int index.
Sample
print(timestep_for_snr([0.9, 0.5, 0.1], 1.0))
Output:
1
Example:
print(timestep_for_snr([0.9, 0.5, 0.1], 1.0))
1
- Compute the Signal-to-Noise Ratio (SNR) for each timestep t using the formula SNRt=1−αtˉαtˉ, where αtˉ is the value at index t.
- For t=0, α0ˉ=0.9, so SNR0=1−0.90.9=0.10.9=9.0.
- For t=1, α1ˉ=0.5, so SNR1=1−0.50.5=0.50.5=1.0.
- For t=2, α2ˉ=0.1, so SNR2=1−0.10.1=0.90.1≈0.111.
- Calculate the absolute difference between each SNR and the target SNR of 1.0: ∣9.0−1.0∣=8.0, ∣1.0−1.0∣=0.0, and ∣0.111−1.0∣≈0.889.
- Select the timestep with the minimum difference; since 0.0 is the smallest, the chosen index is 1.
- The final output is 1
Constraints:
1 <= len(alpha_bar) <= 100000,target_snr >= 0.SNR_t = alpha_bar_t / (1 - alpha_bar_t); a value of1.0has infinite SNR.- Ties in the distance go to the smaller index.
1. Background Knowledge
In denoising diffusion probabilistic models (DDPMs), the forward process gradually adds Gaussian noise to a data sample over T discrete timesteps. The cumulative product αˉt=∏s=1tαs controls how much of the original signal remains at step t. As t increases, αˉt decreases toward zero, meaning the sample becomes increasingly dominated by noise.
The signal-to-noise ratio (SNR) at timestep t is defined as:
SNRt=1−αˉtαˉtThis quantity is monotonically decreasing in t because αˉt is monotonically decreasing. A high SNR means the signal is strong relative to noise (early timesteps), while a low SNR means noise dominates (late timesteps). When αˉt=1.0, the denominator becomes zero, yielding an infinite SNR — the pure signal with no noise added.
In practice, operations like inpainting, image editing, or classifier-free guidance are often applied at a specific noise level rather than a fixed timestep. Finding the timestep whose SNR is closest to a target value allows you to place such an operation at the appropriate point along the diffusion trajectory.
2. Algorithm Approach
This is a nearest-value search over a monotonically decreasing sequence. The straightforward approach is to compute the SNR at every timestep and track the index with the smallest absolute distance to the target. Because the SNR sequence is monotonic, a binary search could also be used for O(logT) lookup, but for typical schedule lengths (a few hundred to a few thousand steps), a linear scan is perfectly adequate and simpler to implement correctly.
The key insight is that you do not need to store all SNR values — you can compute each one on the fly while iterating.
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.