MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / SpacedDiffusion

Class SpacedDiffusion

Large-DiT-ImageNet/diffusion/respace.py:65–114  ·  view source on GitHub ↗

A diffusion process which can skip steps in a base diffusion process. :param use_timesteps: a collection (sequence or set) of timesteps from the original diffusion process to retain. :param kwargs: the kwargs to create the base diffusion process.

Source from the content-addressed store, hash-verified

63
64
65class SpacedDiffusion(GaussianDiffusion):
66 """
67 A diffusion process which can skip steps in a base diffusion process.
68 :param use_timesteps: a collection (sequence or set) of timesteps from the
69 original diffusion process to retain.
70 :param kwargs: the kwargs to create the base diffusion process.
71 """
72
73 def __init__(self, use_timesteps, **kwargs):
74 self.use_timesteps = set(use_timesteps)
75 self.timestep_map = []
76 self.original_num_steps = len(kwargs["betas"])
77
78 base_diffusion = GaussianDiffusion(**kwargs) # pylint: disable=missing-kwoa
79 last_alpha_cumprod = 1.0
80 new_betas = []
81 for i, alpha_cumprod in enumerate(base_diffusion.alphas_cumprod):
82 if i in self.use_timesteps:
83 new_betas.append(1 - alpha_cumprod / last_alpha_cumprod)
84 last_alpha_cumprod = alpha_cumprod
85 self.timestep_map.append(i)
86 kwargs["betas"] = np.array(new_betas)
87 super().__init__(**kwargs)
88
89 def p_mean_variance(
90 self, model, *args, **kwargs
91 ): # pylint: disable=signature-differs
92 return super().p_mean_variance(self._wrap_model(model), *args, **kwargs)
93
94 def training_losses(
95 self, model, *args, **kwargs
96 ): # pylint: disable=signature-differs
97 return super().training_losses(self._wrap_model(model), *args, **kwargs)
98
99 def condition_mean(self, cond_fn, *args, **kwargs):
100 return super().condition_mean(self._wrap_model(cond_fn), *args, **kwargs)
101
102 def condition_score(self, cond_fn, *args, **kwargs):
103 return super().condition_score(self._wrap_model(cond_fn), *args, **kwargs)
104
105 def _wrap_model(self, model):
106 if isinstance(model, _WrappedModel):
107 return model
108 return _WrappedModel(
109 model, self.timestep_map, self.original_num_steps
110 )
111
112 def _scale_timesteps(self, t):
113 # Scaling is done by the wrapped model.
114 return t
115
116
117class _WrappedModel:

Callers 1

create_diffusionFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected