BiliSakura commited on
Commit
d13cb41
·
verified ·
1 Parent(s): 943b32f

Fix generator determinism: forward generator through scheduler steps and seeded noise

Browse files
DiT-XL-2-256/pipeline.py CHANGED
@@ -1,56 +1,16 @@
1
- # Copyright 2026 The HuggingFace Team. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
 
15
  import inspect
16
  from typing import Any, Dict, List, Optional, Tuple, Union
17
 
18
  import torch
19
-
20
  from diffusers.image_processor import VaeImageProcessor
21
  from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
22
  from diffusers.schedulers import KarrasDiffusionSchedulers
23
- from diffusers.utils import replace_example_docstring
24
  from diffusers.utils.torch_utils import randn_tensor
25
 
26
- EXAMPLE_DOC_STRING = """
27
- Examples:
28
- ```py
29
- >>> from pathlib import Path
30
- >>> import torch
31
- >>> from diffusers import DiffusionPipeline
32
-
33
- >>> model_dir = Path("path/to/BiliSakura/DiT-diffusers/DiT-XL-2-256")
34
- >>> pipe = DiffusionPipeline.from_pretrained(
35
- ... str(model_dir),
36
- ... local_files_only=True,
37
- ... custom_pipeline=str(model_dir / "pipeline.py"),
38
- ... trust_remote_code=True,
39
- ... torch_dtype=torch.float16,
40
- ... )
41
- >>> pipe = pipe.to("cuda")
42
-
43
- >>> class_id = pipe.get_label_ids("golden retriever")[0]
44
- >>> image = pipe(
45
- ... class_labels=class_id,
46
- ... height=256,
47
- ... width=256,
48
- ... num_inference_steps=250,
49
- ... guidance_scale=4.0,
50
- ... ).images[0]
51
- ```
52
- """
53
-
54
 
55
  class DiTPipeline(DiffusionPipeline):
56
  r"""Class-conditional Diffusion Transformer pipeline with custom pipeline loading support."""
@@ -104,7 +64,6 @@ class DiTPipeline(DiffusionPipeline):
104
  return self._id2label
105
 
106
  def get_label_ids(self, label: Union[str, List[str]]) -> List[int]:
107
- r"""Map English ImageNet labels to class ids."""
108
  labels = [label] if isinstance(label, str) else label
109
  if not self.labels:
110
  raise ValueError("No id2label mapping is available in this checkpoint.")
@@ -133,8 +92,8 @@ class DiTPipeline(DiffusionPipeline):
133
  @staticmethod
134
  def prepare_extra_step_kwargs(
135
  scheduler: KarrasDiffusionSchedulers,
136
- generator: Optional[Union[torch.Generator, List[torch.Generator]]],
137
- eta: float,
138
  ) -> Dict[str, Any]:
139
  kwargs: Dict[str, Any] = {}
140
  step_params = set(inspect.signature(scheduler.step).parameters.keys())
@@ -153,7 +112,6 @@ class DiTPipeline(DiffusionPipeline):
153
  return timestep.expand(batch_size)
154
 
155
  @torch.no_grad()
156
- @replace_example_docstring(EXAMPLE_DOC_STRING)
157
  def __call__(
158
  self,
159
  class_labels: Union[int, str, List[Union[int, str]], torch.Tensor] = 207,
@@ -167,13 +125,6 @@ class DiTPipeline(DiffusionPipeline):
167
  output_type: str = "pil",
168
  return_dict: bool = True,
169
  ) -> Union[ImagePipelineOutput, Tuple]:
170
- r"""
171
- Generate class-conditional samples from a DiT checkpoint.
172
-
173
- Examples:
174
- <!-- this section is replaced by replace_example_docstring -->
175
- """
176
- # Stage 1: check inputs
177
  class_labels_list = self._normalize_class_labels(class_labels)
178
  batch_size = len(class_labels_list)
179
  native_size = int(getattr(self.transformer.config, "sample_size", 32)) * self.vae_scale_factor
@@ -187,7 +138,6 @@ class DiTPipeline(DiffusionPipeline):
187
  if output_type not in {"pil", "np", "pt", "latent"}:
188
  raise ValueError(f"Unsupported `output_type`: {output_type}")
189
 
190
- # Stage 2: define call parameters
191
  device = self._execution_device
192
  do_classifier_free_guidance = float(guidance_scale) > 1.0
193
  latent_h = height // self.vae_scale_factor
@@ -195,17 +145,14 @@ class DiTPipeline(DiffusionPipeline):
195
  latent_channels = int(getattr(self.transformer.config, "in_channels", 4))
196
  extra_step_kwargs = self.prepare_extra_step_kwargs(self.scheduler, generator=generator, eta=float(eta))
197
 
198
- # Stage 3: encode class conditioning
199
  class_labels_tensor = torch.tensor(class_labels_list, device=device, dtype=torch.long)
200
  if do_classifier_free_guidance:
201
  null_class = int(self.config.null_class_id)
202
  uncond = torch.full((batch_size,), null_class, device=device, dtype=torch.long)
203
  class_labels_tensor = torch.cat([class_labels_tensor, uncond], dim=0)
204
 
205
- # Stage 4: prepare timesteps
206
  self.scheduler.set_timesteps(num_inference_steps, device=device)
207
 
208
- # Stage 5: prepare latent variables
209
  if latents is None:
210
  latents = randn_tensor(
211
  (batch_size, latent_channels, latent_h, latent_w),
@@ -219,9 +166,6 @@ class DiTPipeline(DiffusionPipeline):
219
  if tuple(latents.shape) != expected:
220
  raise ValueError(f"Invalid `latents` shape: {tuple(latents.shape)}. Expected {expected}.")
221
 
222
- # Stage 6: prepare extra step kwargs (already done above for clarity)
223
-
224
- # Stage 7: run denoising loop
225
  for timestep in self.progress_bar(self.scheduler.timesteps):
226
  latent_model_input = latents
227
  if do_classifier_free_guidance:
 
1
+ """Hub custom pipeline: DiTPipeline."""
2
+
3
+ from __future__ import annotations
 
 
 
 
 
 
 
 
 
 
4
 
5
  import inspect
6
  from typing import Any, Dict, List, Optional, Tuple, Union
7
 
8
  import torch
 
9
  from diffusers.image_processor import VaeImageProcessor
10
  from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
11
  from diffusers.schedulers import KarrasDiffusionSchedulers
 
12
  from diffusers.utils.torch_utils import randn_tensor
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  class DiTPipeline(DiffusionPipeline):
16
  r"""Class-conditional Diffusion Transformer pipeline with custom pipeline loading support."""
 
64
  return self._id2label
65
 
66
  def get_label_ids(self, label: Union[str, List[str]]) -> List[int]:
 
67
  labels = [label] if isinstance(label, str) else label
68
  if not self.labels:
69
  raise ValueError("No id2label mapping is available in this checkpoint.")
 
92
  @staticmethod
93
  def prepare_extra_step_kwargs(
94
  scheduler: KarrasDiffusionSchedulers,
95
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
96
+ eta: float = 0.0,
97
  ) -> Dict[str, Any]:
98
  kwargs: Dict[str, Any] = {}
99
  step_params = set(inspect.signature(scheduler.step).parameters.keys())
 
112
  return timestep.expand(batch_size)
113
 
114
  @torch.no_grad()
 
115
  def __call__(
116
  self,
117
  class_labels: Union[int, str, List[Union[int, str]], torch.Tensor] = 207,
 
125
  output_type: str = "pil",
126
  return_dict: bool = True,
127
  ) -> Union[ImagePipelineOutput, Tuple]:
 
 
 
 
 
 
 
128
  class_labels_list = self._normalize_class_labels(class_labels)
129
  batch_size = len(class_labels_list)
130
  native_size = int(getattr(self.transformer.config, "sample_size", 32)) * self.vae_scale_factor
 
138
  if output_type not in {"pil", "np", "pt", "latent"}:
139
  raise ValueError(f"Unsupported `output_type`: {output_type}")
140
 
 
141
  device = self._execution_device
142
  do_classifier_free_guidance = float(guidance_scale) > 1.0
143
  latent_h = height // self.vae_scale_factor
 
145
  latent_channels = int(getattr(self.transformer.config, "in_channels", 4))
146
  extra_step_kwargs = self.prepare_extra_step_kwargs(self.scheduler, generator=generator, eta=float(eta))
147
 
 
148
  class_labels_tensor = torch.tensor(class_labels_list, device=device, dtype=torch.long)
149
  if do_classifier_free_guidance:
150
  null_class = int(self.config.null_class_id)
151
  uncond = torch.full((batch_size,), null_class, device=device, dtype=torch.long)
152
  class_labels_tensor = torch.cat([class_labels_tensor, uncond], dim=0)
153
 
 
154
  self.scheduler.set_timesteps(num_inference_steps, device=device)
155
 
 
156
  if latents is None:
157
  latents = randn_tensor(
158
  (batch_size, latent_channels, latent_h, latent_w),
 
166
  if tuple(latents.shape) != expected:
167
  raise ValueError(f"Invalid `latents` shape: {tuple(latents.shape)}. Expected {expected}.")
168
 
 
 
 
169
  for timestep in self.progress_bar(self.scheduler.timesteps):
170
  latent_model_input = latents
171
  if do_classifier_free_guidance:
DiT-XL-2-512/pipeline.py CHANGED
@@ -1,56 +1,16 @@
1
- # Copyright 2026 The HuggingFace Team. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
 
15
  import inspect
16
  from typing import Any, Dict, List, Optional, Tuple, Union
17
 
18
  import torch
19
-
20
  from diffusers.image_processor import VaeImageProcessor
21
  from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
22
  from diffusers.schedulers import KarrasDiffusionSchedulers
23
- from diffusers.utils import replace_example_docstring
24
  from diffusers.utils.torch_utils import randn_tensor
25
 
26
- EXAMPLE_DOC_STRING = """
27
- Examples:
28
- ```py
29
- >>> from pathlib import Path
30
- >>> import torch
31
- >>> from diffusers import DiffusionPipeline
32
-
33
- >>> model_dir = Path("path/to/BiliSakura/DiT-diffusers/DiT-XL-2-512")
34
- >>> pipe = DiffusionPipeline.from_pretrained(
35
- ... str(model_dir),
36
- ... local_files_only=True,
37
- ... custom_pipeline=str(model_dir / "pipeline.py"),
38
- ... trust_remote_code=True,
39
- ... torch_dtype=torch.float16,
40
- ... )
41
- >>> pipe = pipe.to("cuda")
42
-
43
- >>> class_id = pipe.get_label_ids("golden retriever")[0]
44
- >>> image = pipe(
45
- ... class_labels=class_id,
46
- ... height=512,
47
- ... width=512,
48
- ... num_inference_steps=250,
49
- ... guidance_scale=4.0,
50
- ... ).images[0]
51
- ```
52
- """
53
-
54
 
55
  class DiTPipeline(DiffusionPipeline):
56
  r"""Class-conditional Diffusion Transformer pipeline with custom pipeline loading support."""
@@ -104,7 +64,6 @@ class DiTPipeline(DiffusionPipeline):
104
  return self._id2label
105
 
106
  def get_label_ids(self, label: Union[str, List[str]]) -> List[int]:
107
- r"""Map English ImageNet labels to class ids."""
108
  labels = [label] if isinstance(label, str) else label
109
  if not self.labels:
110
  raise ValueError("No id2label mapping is available in this checkpoint.")
@@ -133,8 +92,8 @@ class DiTPipeline(DiffusionPipeline):
133
  @staticmethod
134
  def prepare_extra_step_kwargs(
135
  scheduler: KarrasDiffusionSchedulers,
136
- generator: Optional[Union[torch.Generator, List[torch.Generator]]],
137
- eta: float,
138
  ) -> Dict[str, Any]:
139
  kwargs: Dict[str, Any] = {}
140
  step_params = set(inspect.signature(scheduler.step).parameters.keys())
@@ -153,7 +112,6 @@ class DiTPipeline(DiffusionPipeline):
153
  return timestep.expand(batch_size)
154
 
155
  @torch.no_grad()
156
- @replace_example_docstring(EXAMPLE_DOC_STRING)
157
  def __call__(
158
  self,
159
  class_labels: Union[int, str, List[Union[int, str]], torch.Tensor] = 207,
@@ -167,13 +125,6 @@ class DiTPipeline(DiffusionPipeline):
167
  output_type: str = "pil",
168
  return_dict: bool = True,
169
  ) -> Union[ImagePipelineOutput, Tuple]:
170
- r"""
171
- Generate class-conditional samples from a DiT checkpoint.
172
-
173
- Examples:
174
- <!-- this section is replaced by replace_example_docstring -->
175
- """
176
- # Stage 1: check inputs
177
  class_labels_list = self._normalize_class_labels(class_labels)
178
  batch_size = len(class_labels_list)
179
  native_size = int(getattr(self.transformer.config, "sample_size", 32)) * self.vae_scale_factor
@@ -187,7 +138,6 @@ class DiTPipeline(DiffusionPipeline):
187
  if output_type not in {"pil", "np", "pt", "latent"}:
188
  raise ValueError(f"Unsupported `output_type`: {output_type}")
189
 
190
- # Stage 2: define call parameters
191
  device = self._execution_device
192
  do_classifier_free_guidance = float(guidance_scale) > 1.0
193
  latent_h = height // self.vae_scale_factor
@@ -195,17 +145,14 @@ class DiTPipeline(DiffusionPipeline):
195
  latent_channels = int(getattr(self.transformer.config, "in_channels", 4))
196
  extra_step_kwargs = self.prepare_extra_step_kwargs(self.scheduler, generator=generator, eta=float(eta))
197
 
198
- # Stage 3: encode class conditioning
199
  class_labels_tensor = torch.tensor(class_labels_list, device=device, dtype=torch.long)
200
  if do_classifier_free_guidance:
201
  null_class = int(self.config.null_class_id)
202
  uncond = torch.full((batch_size,), null_class, device=device, dtype=torch.long)
203
  class_labels_tensor = torch.cat([class_labels_tensor, uncond], dim=0)
204
 
205
- # Stage 4: prepare timesteps
206
  self.scheduler.set_timesteps(num_inference_steps, device=device)
207
 
208
- # Stage 5: prepare latent variables
209
  if latents is None:
210
  latents = randn_tensor(
211
  (batch_size, latent_channels, latent_h, latent_w),
@@ -219,9 +166,6 @@ class DiTPipeline(DiffusionPipeline):
219
  if tuple(latents.shape) != expected:
220
  raise ValueError(f"Invalid `latents` shape: {tuple(latents.shape)}. Expected {expected}.")
221
 
222
- # Stage 6: prepare extra step kwargs (already done above for clarity)
223
-
224
- # Stage 7: run denoising loop
225
  for timestep in self.progress_bar(self.scheduler.timesteps):
226
  latent_model_input = latents
227
  if do_classifier_free_guidance:
 
1
+ """Hub custom pipeline: DiTPipeline."""
2
+
3
+ from __future__ import annotations
 
 
 
 
 
 
 
 
 
 
4
 
5
  import inspect
6
  from typing import Any, Dict, List, Optional, Tuple, Union
7
 
8
  import torch
 
9
  from diffusers.image_processor import VaeImageProcessor
10
  from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
11
  from diffusers.schedulers import KarrasDiffusionSchedulers
 
12
  from diffusers.utils.torch_utils import randn_tensor
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  class DiTPipeline(DiffusionPipeline):
16
  r"""Class-conditional Diffusion Transformer pipeline with custom pipeline loading support."""
 
64
  return self._id2label
65
 
66
  def get_label_ids(self, label: Union[str, List[str]]) -> List[int]:
 
67
  labels = [label] if isinstance(label, str) else label
68
  if not self.labels:
69
  raise ValueError("No id2label mapping is available in this checkpoint.")
 
92
  @staticmethod
93
  def prepare_extra_step_kwargs(
94
  scheduler: KarrasDiffusionSchedulers,
95
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
96
+ eta: float = 0.0,
97
  ) -> Dict[str, Any]:
98
  kwargs: Dict[str, Any] = {}
99
  step_params = set(inspect.signature(scheduler.step).parameters.keys())
 
112
  return timestep.expand(batch_size)
113
 
114
  @torch.no_grad()
 
115
  def __call__(
116
  self,
117
  class_labels: Union[int, str, List[Union[int, str]], torch.Tensor] = 207,
 
125
  output_type: str = "pil",
126
  return_dict: bool = True,
127
  ) -> Union[ImagePipelineOutput, Tuple]:
 
 
 
 
 
 
 
128
  class_labels_list = self._normalize_class_labels(class_labels)
129
  batch_size = len(class_labels_list)
130
  native_size = int(getattr(self.transformer.config, "sample_size", 32)) * self.vae_scale_factor
 
138
  if output_type not in {"pil", "np", "pt", "latent"}:
139
  raise ValueError(f"Unsupported `output_type`: {output_type}")
140
 
 
141
  device = self._execution_device
142
  do_classifier_free_guidance = float(guidance_scale) > 1.0
143
  latent_h = height // self.vae_scale_factor
 
145
  latent_channels = int(getattr(self.transformer.config, "in_channels", 4))
146
  extra_step_kwargs = self.prepare_extra_step_kwargs(self.scheduler, generator=generator, eta=float(eta))
147
 
 
148
  class_labels_tensor = torch.tensor(class_labels_list, device=device, dtype=torch.long)
149
  if do_classifier_free_guidance:
150
  null_class = int(self.config.null_class_id)
151
  uncond = torch.full((batch_size,), null_class, device=device, dtype=torch.long)
152
  class_labels_tensor = torch.cat([class_labels_tensor, uncond], dim=0)
153
 
 
154
  self.scheduler.set_timesteps(num_inference_steps, device=device)
155
 
 
156
  if latents is None:
157
  latents = randn_tensor(
158
  (batch_size, latent_channels, latent_h, latent_w),
 
166
  if tuple(latents.shape) != expected:
167
  raise ValueError(f"Invalid `latents` shape: {tuple(latents.shape)}. Expected {expected}.")
168
 
 
 
 
169
  for timestep in self.progress_bar(self.scheduler.timesteps):
170
  latent_model_input = latents
171
  if do_classifier_free_guidance: