BiliSakura commited on
Commit
bf56abe
·
verified ·
1 Parent(s): 8fd9f0b

Update PixNerd-XL-16-256/pipeline.py

Browse files
Files changed (1) hide show
  1. PixNerd-XL-16-256/pipeline.py +35 -28
PixNerd-XL-16-256/pipeline.py CHANGED
@@ -1,3 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Copyright 2026 The HuggingFace Team. All rights reserved.
2
  #
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,18 +24,12 @@
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
 
15
- from __future__ import annotations
16
-
17
  import json
18
  from pathlib import Path
19
- from typing import Dict, List, Optional, Tuple, Union
20
 
21
  import torch
22
 
23
- from diffusers.image_processor import VaeImageProcessor
24
- from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
25
- from diffusers.utils.torch_utils import randn_tensor
26
-
27
  DEFAULT_NATIVE_RESOLUTION = 512
28
 
29
  EXAMPLE_DOC_STRING = """
@@ -62,7 +68,6 @@ EXAMPLE_DOC_STRING = """
62
 
63
  ConditioningInput = Union[int, str, List[Union[int, str]], torch.LongTensor]
64
 
65
-
66
  class PixNerdPipeline(DiffusionPipeline):
67
  r"""
68
  Pipeline for class-conditional PixNerd pixel-space image generation.
@@ -80,6 +85,21 @@ class PixNerdPipeline(DiffusionPipeline):
80
  ImageNet class id to English label mapping. Values may contain comma-separated synonyms.
81
  """
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  model_cpu_offload_seq = "conditioner->transformer->vae"
84
  _callback_tensor_inputs = ["latents"]
85
  _optional_components = ["vae", "conditioner"]
@@ -88,8 +108,8 @@ class PixNerdPipeline(DiffusionPipeline):
88
  self,
89
  transformer,
90
  scheduler,
91
- vae=None,
92
- conditioner=None,
93
  id2label: Optional[Dict[Union[int, str], str]] = None,
94
  ):
95
  super().__init__()
@@ -106,10 +126,6 @@ class PixNerdPipeline(DiffusionPipeline):
106
  scheduler=scheduler,
107
  )
108
  self.image_processor = VaeImageProcessor(vae_scale_factor=1, do_normalize=False)
109
- if id2label is None:
110
- id2label = self._read_id2label_from_model_index(
111
- getattr(getattr(self, "config", None), "_name_or_path", None)
112
- )
113
  self._id2label = self._normalize_id2label(id2label)
114
  self.labels = self._build_label2id(self._id2label)
115
  self._labels_loaded_from_model_index = bool(self._id2label)
@@ -127,17 +143,6 @@ class PixNerdPipeline(DiffusionPipeline):
127
  return parameter.device
128
  return torch.device("cpu")
129
 
130
- @classmethod
131
- def from_pretrained(cls, pretrained_model_name_or_path=None, *args, **kwargs):
132
- id2label_override = kwargs.pop("id2label", None)
133
- pipe = super().from_pretrained(pretrained_model_name_or_path, *args, **kwargs)
134
- id2label = id2label_override or cls._read_id2label_from_model_index(pretrained_model_name_or_path)
135
- if id2label:
136
- pipe._id2label = cls._normalize_id2label(id2label)
137
- pipe.labels = cls._build_label2id(pipe._id2label)
138
- pipe._labels_loaded_from_model_index = True
139
- return pipe
140
-
141
  def _ensure_labels_loaded(self) -> None:
142
  if self._labels_loaded_from_model_index:
143
  return
@@ -154,7 +159,7 @@ class PixNerdPipeline(DiffusionPipeline):
154
  return {int(key): value for key, value in id2label.items()}
155
 
156
  @staticmethod
157
- def _read_id2label_from_model_index(variant_path: Optional[Union[str, Path]]) -> Dict[int, str]:
158
  if not variant_path:
159
  return {}
160
  model_index_path = Path(variant_path).resolve() / "model_index.json"
@@ -406,6 +411,8 @@ class PixNerdPipeline(DiffusionPipeline):
406
  device=device,
407
  )
408
 
 
 
409
  for timestep in self.progress_bar(self.scheduler.timesteps):
410
  cfg_latents = torch.cat([latents, latents], dim=0)
411
  cfg_t = timestep.repeat(cfg_latents.shape[0]).to(device=device, dtype=latents.dtype)
@@ -420,6 +427,7 @@ class PixNerdPipeline(DiffusionPipeline):
420
  model_output=model_output,
421
  timestep=timestep,
422
  sample=latents,
 
423
  ).prev_sample
424
 
425
  image = self.decode_latents(latents, output_type=output_type)
@@ -429,5 +437,4 @@ class PixNerdPipeline(DiffusionPipeline):
429
  return (image,)
430
  return ImagePipelineOutput(images=image)
431
 
432
-
433
- PixNerdPipelineOutput = ImagePipelineOutput
 
1
+ """Hub custom pipeline: PixNerdPipeline.
2
+ Load with native Hugging Face diffusers and trust_remote_code=True.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import inspect
8
+
9
+ from diffusers.image_processor import VaeImageProcessor
10
+ from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
11
+ from diffusers.utils import BaseOutput
12
+ from diffusers.utils.torch_utils import randn_tensor
13
  # Copyright 2026 The HuggingFace Team. All rights reserved.
14
  #
15
  # Licensed under the Apache License, Version 2.0 (the "License");
 
24
  # See the License for the specific language governing permissions and
25
  # limitations under the License.
26
 
 
 
27
  import json
28
  from pathlib import Path
29
+ from typing import Dict, List, Optional, Tuple, Union, Any
30
 
31
  import torch
32
 
 
 
 
 
33
  DEFAULT_NATIVE_RESOLUTION = 512
34
 
35
  EXAMPLE_DOC_STRING = """
 
68
 
69
  ConditioningInput = Union[int, str, List[Union[int, str]], torch.LongTensor]
70
 
 
71
  class PixNerdPipeline(DiffusionPipeline):
72
  r"""
73
  Pipeline for class-conditional PixNerd pixel-space image generation.
 
85
  ImageNet class id to English label mapping. Values may contain comma-separated synonyms.
86
  """
87
 
88
+ @staticmethod
89
+ def prepare_extra_step_kwargs(
90
+ scheduler,
91
+ generator=None,
92
+ eta: float | None = None,
93
+ ):
94
+ kwargs = {}
95
+ step_params = set(inspect.signature(scheduler.step).parameters.keys())
96
+ if "generator" in step_params:
97
+ kwargs["generator"] = generator
98
+ if eta is not None and "eta" in step_params:
99
+ kwargs["eta"] = eta
100
+ return kwargs
101
+
102
+
103
  model_cpu_offload_seq = "conditioner->transformer->vae"
104
  _callback_tensor_inputs = ["latents"]
105
  _optional_components = ["vae", "conditioner"]
 
108
  self,
109
  transformer,
110
  scheduler,
111
+ vae: Optional[PixNerdPixelVAE] = None,
112
+ conditioner: Optional[PixNerdLabelConditioner] = None,
113
  id2label: Optional[Dict[Union[int, str], str]] = None,
114
  ):
115
  super().__init__()
 
126
  scheduler=scheduler,
127
  )
128
  self.image_processor = VaeImageProcessor(vae_scale_factor=1, do_normalize=False)
 
 
 
 
129
  self._id2label = self._normalize_id2label(id2label)
130
  self.labels = self._build_label2id(self._id2label)
131
  self._labels_loaded_from_model_index = bool(self._id2label)
 
143
  return parameter.device
144
  return torch.device("cpu")
145
 
 
 
 
 
 
 
 
 
 
 
 
146
  def _ensure_labels_loaded(self) -> None:
147
  if self._labels_loaded_from_model_index:
148
  return
 
159
  return {int(key): value for key, value in id2label.items()}
160
 
161
  @staticmethod
162
+ def _read_id2label_from_model_index(variant_path: Optional[str]) -> Dict[int, str]:
163
  if not variant_path:
164
  return {}
165
  model_index_path = Path(variant_path).resolve() / "model_index.json"
 
411
  device=device,
412
  )
413
 
414
+ extra_step_kwargs = self.prepare_extra_step_kwargs(self.scheduler, generator=generator)
415
+
416
  for timestep in self.progress_bar(self.scheduler.timesteps):
417
  cfg_latents = torch.cat([latents, latents], dim=0)
418
  cfg_t = timestep.repeat(cfg_latents.shape[0]).to(device=device, dtype=latents.dtype)
 
427
  model_output=model_output,
428
  timestep=timestep,
429
  sample=latents,
430
+ **extra_step_kwargs,
431
  ).prev_sample
432
 
433
  image = self.decode_latents(latents, output_type=output_type)
 
437
  return (image,)
438
  return ImagePipelineOutput(images=image)
439
 
440
+ PixNerdPipelineOutput = ImagePipelineOutput