File size: 4,202 Bytes
afe9f02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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