v9.1.1
Menu

Quadratic Stride Timestep Subsequence

mediumPRODDPM and DDIM SamplingDiffusion ModelsDiffusion Models 1

Problem Statement

The DDIM paper's "quadratic" spacing places more sampling steps near the low-noise end, which improves sample quality for very few steps. Build that descending subsequence.

Background

For S steps out of T, quadratic spacing takes S points evenly on [0, sqrt(T * 0.8)], squares them, and floors to integer timesteps:

ti=⌊(iS−1 0.8 T)2⌋,i=0,…,S−1t_i = \left\lfloor \left(\frac{i}{S-1}\,\sqrt{0.8\,T}\right)^2 \right\rfloor, \quad i = 0, \dots, S-1

then reverse for high-to-low sampling. (The 0.8 factor keeps the largest index below T.) For S == 1 return [0].

Your Task

Implement:

def quadratic_timesteps(T, S):

Return the descending list of S integer timesteps.

Input Format

  • T (int): number of training steps.
  • S (int): sampling steps, 1 <= S <= T.

Output Format

  • A list of S ints, descending.

Sample

print(quadratic_timesteps(1000, 5))

Output:

[800, 450, 200, 50, 0]

Example

Input
print(quadratic_timesteps(1000, 5))
Output
[800, 450, 200, 50, 0]
Reasoning
  • Since S=5>1S = 5 > 1, we first determine the upper bound for the square root calculation: 0.8×1000=800≈28.284\sqrt{0.8 \times 1000} = \sqrt{800} \approx 28.284.
  • We generate the ascending sequence of S=5S=5 points by evaluating ti=⌊(i4×28.284)2⌋t_i = \lfloor (\frac{i}{4} \times 28.284)^2 \rfloor for i=0,1,2,3,4i = 0, 1, 2, 3, 4.
  • For i=0i=0 and i=1i=1, the values are ⌊0⌋=0\lfloor 0 \rfloor = 0 and ⌊(7.071)2⌋=⌊50.0⌋=50\lfloor (7.071)^2 \rfloor = \lfloor 50.0 \rfloor = 50.
  • For i=2i=2 and i=3i=3, the values are ⌊(14.142)2⌋=⌊200.0⌋=200\lfloor (14.142)^2 \rfloor = \lfloor 200.0 \rfloor = 200 and ⌊(21.213)2⌋=⌊450.0⌋=450\lfloor (21.213)^2 \rfloor = \lfloor 450.0 \rfloor = 450.
  • For i=4i=4, the value is ⌊(28.284)2⌋=⌊800.0⌋=800\lfloor (28.284)^2 \rfloor = \lfloor 800.0 \rfloor = 800, yielding the ascending list [0,50,200,450,800][0, 50, 200, 450, 800].
  • Reversing this list to create the descending timestep sequence results in the final output [800, 450, 200, 50, 0].

Constraints

  • 1 <= S <= T <= 1000000.
  • t_i = floor((i/(S-1) * sqrt(0.8*T))**2), then reversed; S==1 gives [0].
  • Return exactly S descending ints.

More from Diffusion Models 1

PRO

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.

solution.py

Test results

Sign in to run your code against the test cases.