idol_custom_blocks / before_denoise.py
OzzyGT's picture
OzzyGT HF Staff
add bocks and code
afe9f02
Raw History Blame Contribute Delete
4.2 kB
# Copyright 2026 Sperid Labs and The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import torch
from diffusers.modular_pipelines import (
ModularPipeline,
ModularPipelineBlocks,
PipelineState,
)
from diffusers.modular_pipelines.modular_pipeline_utils import (
ComponentSpec,
InputParam,
OutputParam,
)
from diffusers.utils.torch_utils import randn_tensor
from .components import scheduler_spec, transformer_spec
class IdolSetTimestepsStep(ModularPipelineBlocks):
model_name = "idol"
@property
def description(self) -> str:
return "Sets the shifted flow-matching timesteps (time shift from the scheduler's `shift`, 3.0 by default)."
@property
def expected_components(self) -> list[ComponentSpec]:
return [scheduler_spec()]
@property
def inputs(self) -> list[InputParam]:
return [InputParam.template("num_inference_steps")]
@property
def intermediate_outputs(self) -> list[OutputParam]:
return [
OutputParam("timesteps", type_hint=torch.Tensor, description="The timesteps of the denoising loop."),
OutputParam("num_inference_steps", type_hint=int, description="The number of denoising steps."),
]
@torch.no_grad()
def __call__(self, components: ModularPipeline, state: PipelineState) -> PipelineState:
block_state = self.get_block_state(state)
device = components._execution_device
sigmas = np.linspace(1.0, 0.0, block_state.num_inference_steps + 1)[:-1]
components.scheduler.set_timesteps(sigmas=sigmas, device=device)
block_state.timesteps = components.scheduler.timesteps
self.set_block_state(state, block_state)
return components, state
class IdolPrepareLatentsStep(ModularPipelineBlocks):
model_name = "idol"
@property
def description(self) -> str:
return "Draws the initial noise for the object latent and the camera latent."
@property
def expected_components(self) -> list[ComponentSpec]:
return [transformer_spec()]
@property
def inputs(self) -> list[InputParam]:
return [
InputParam(
"image_embeds",
required=True,
type_hint=torch.Tensor,
description="DINOv3 image tokens; sets the batch size.",
),
InputParam.template("generator"),
]
@property
def intermediate_outputs(self) -> list[OutputParam]:
return [
OutputParam("latents", type_hint=torch.Tensor, description="Initial noise of the object latent."),
OutputParam("camera_latents", type_hint=torch.Tensor, description="Initial noise of the camera latent."),
]
@torch.no_grad()
def __call__(self, components: ModularPipeline, state: PipelineState) -> PipelineState:
block_state = self.get_block_state(state)
device = components._execution_device
config = components.transformer.config
batch_size = block_state.image_embeds.shape[0]
# the sampler integrates in float32, like the reference
block_state.latents = randn_tensor(
(batch_size, config.num_latent_tokens, config.in_channels),
generator=block_state.generator,
device=device,
dtype=torch.float32,
)
block_state.camera_latents = randn_tensor(
(batch_size, 1, config.camera_channels),
generator=block_state.generator,
device=device,
dtype=torch.float32,
)
self.set_block_state(state, block_state)
return components, state