MCPcopy
hub / github.com/openai/guided-diffusion / space_timesteps

Function space_timesteps

guided_diffusion/respace.py:7–60  ·  view source on GitHub ↗

Create a list of timesteps to use from an original diffusion process, given the number of timesteps we want to take from equally-sized portions of the original process. For example, if there's 300 timesteps and the section counts are [10,15,20] then the first 100 timesteps are

(num_timesteps, section_counts)

Source from the content-addressed store, hash-verified

5
6
7def space_timesteps(num_timesteps, section_counts):
8 """
9 Create a list of timesteps to use from an original diffusion process,
10 given the number of timesteps we want to take from equally-sized portions
11 of the original process.
12
13 For example, if there's 300 timesteps and the section counts are [10,15,20]
14 then the first 100 timesteps are strided to be 10 timesteps, the second 100
15 are strided to be 15 timesteps, and the final 100 are strided to be 20.
16
17 If the stride is a string starting with "ddim", then the fixed striding
18 from the DDIM paper is used, and only one section is allowed.
19
20 :param num_timesteps: the number of diffusion steps in the original
21 process to divide up.
22 :param section_counts: either a list of numbers, or a string containing
23 comma-separated numbers, indicating the step count
24 per section. As a special case, use "ddimN" where N
25 is a number of steps to use the striding from the
26 DDIM paper.
27 :return: a set of diffusion steps from the original process to use.
28 """
29 if isinstance(section_counts, str):
30 if section_counts.startswith("ddim"):
31 desired_count = int(section_counts[len("ddim") :])
32 for i in range(1, num_timesteps):
33 if len(range(0, num_timesteps, i)) == desired_count:
34 return set(range(0, num_timesteps, i))
35 raise ValueError(
36 f"cannot create exactly {num_timesteps} steps with an integer stride"
37 )
38 section_counts = [int(x) for x in section_counts.split(",")]
39 size_per = num_timesteps // len(section_counts)
40 extra = num_timesteps % len(section_counts)
41 start_idx = 0
42 all_steps = []
43 for i, section_count in enumerate(section_counts):
44 size = size_per + (1 if i < extra else 0)
45 if size < section_count:
46 raise ValueError(
47 f"cannot divide section of {size} steps into {section_count}"
48 )
49 if section_count <= 1:
50 frac_stride = 1
51 else:
52 frac_stride = (size - 1) / (section_count - 1)
53 cur_idx = 0.0
54 taken_steps = []
55 for _ in range(section_count):
56 taken_steps.append(start_idx + round(cur_idx))
57 cur_idx += frac_stride
58 all_steps += taken_steps
59 start_idx += size
60 return set(all_steps)
61
62
63class SpacedDiffusion(GaussianDiffusion):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected