|
| 1 | +import torch |
| 2 | +from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler |
| 3 | + |
| 4 | +from invokeai.app.invocations.fields import Input, InputField |
| 5 | +from invokeai.app.invocations.model import SubModelType, TransformerField |
| 6 | +from invokeai.app.invocations.primitives import ( |
| 7 | + BaseInvocationOutput, |
| 8 | + FieldDescriptions, |
| 9 | + Input, |
| 10 | + InputField, |
| 11 | + LatentsField, |
| 12 | + OutputField, |
| 13 | +) |
| 14 | +from invokeai.app.services.shared.invocation_context import InvocationContext |
| 15 | +from invokeai.invocation_api import BaseInvocation, Classification, InputField, invocation, invocation_output |
| 16 | + |
| 17 | +from invokeai.backend.bria.pipeline import get_original_sigmas, retrieve_timesteps |
| 18 | +from invokeai.backend.bria.transformer_bria import BriaTransformer2DModel |
| 19 | + |
| 20 | +@invocation_output("bria_denoise_output") |
| 21 | +class BriaDenoiseInvocationOutput(BaseInvocationOutput): |
| 22 | + latents: LatentsField = OutputField(description=FieldDescriptions.latents) |
| 23 | + |
| 24 | + |
| 25 | +@invocation( |
| 26 | + "bria_denoise", |
| 27 | + title="Denoise - Bria", |
| 28 | + tags=["image", "bria"], |
| 29 | + category="image", |
| 30 | + version="1.0.0", |
| 31 | + classification=Classification.Prototype, |
| 32 | +) |
| 33 | +class BriaDenoiseInvocation(BaseInvocation): |
| 34 | + num_steps: int = InputField( |
| 35 | + default=30, title="Number of Steps", description="The number of steps to use for the denoiser" |
| 36 | + ) |
| 37 | + guidance_scale: float = InputField( |
| 38 | + default=5.0, title="Guidance Scale", description="The guidance scale to use for the denoiser" |
| 39 | + ) |
| 40 | + |
| 41 | + transformer: TransformerField = InputField( |
| 42 | + description="Bria model (Transformer) to load", |
| 43 | + input=Input.Connection, |
| 44 | + title="Transformer", |
| 45 | + ) |
| 46 | + latents: LatentsField = InputField( |
| 47 | + description="Latents to denoise", |
| 48 | + input=Input.Connection, |
| 49 | + title="Latents", |
| 50 | + ) |
| 51 | + latent_image_ids: LatentsField = InputField( |
| 52 | + description="Latent Image IDs to denoise", |
| 53 | + input=Input.Connection, |
| 54 | + title="Latent Image IDs", |
| 55 | + ) |
| 56 | + pos_embeds: LatentsField = InputField( |
| 57 | + description="Positive Prompt Embeds", |
| 58 | + input=Input.Connection, |
| 59 | + title="Positive Prompt Embeds", |
| 60 | + ) |
| 61 | + neg_embeds: LatentsField = InputField( |
| 62 | + description="Negative Prompt Embeds", |
| 63 | + input=Input.Connection, |
| 64 | + title="Negative Prompt Embeds", |
| 65 | + ) |
| 66 | + text_ids: LatentsField = InputField( |
| 67 | + description="Text IDs", |
| 68 | + input=Input.Connection, |
| 69 | + title="Text IDs", |
| 70 | + ) |
| 71 | + |
| 72 | + @torch.no_grad() |
| 73 | + def invoke(self, context: InvocationContext) -> BriaDenoiseInvocationOutput: |
| 74 | + latents = context.tensors.load(self.latents.latents_name) |
| 75 | + pos_embeds = context.tensors.load(self.pos_embeds.latents_name) |
| 76 | + neg_embeds = context.tensors.load(self.neg_embeds.latents_name) |
| 77 | + text_ids = context.tensors.load(self.text_ids.latents_name) |
| 78 | + latent_image_ids = context.tensors.load(self.latent_image_ids.latents_name) |
| 79 | + scheduler_identifier = self.transformer.transformer.model_copy(update={"submodel_type": SubModelType.Scheduler}) |
| 80 | + |
| 81 | + device = None |
| 82 | + dtype = None |
| 83 | + with ( |
| 84 | + context.models.load(self.transformer.transformer) as transformer, |
| 85 | + context.models.load(scheduler_identifier) as scheduler, |
| 86 | + ): |
| 87 | + assert isinstance(transformer, BriaTransformer2DModel) |
| 88 | + assert isinstance(scheduler, FlowMatchEulerDiscreteScheduler) |
| 89 | + dtype = transformer.dtype |
| 90 | + device = transformer.device |
| 91 | + latents, pos_embeds, neg_embeds = map(lambda x: x.to(device, dtype), (latents, pos_embeds, neg_embeds)) |
| 92 | + prompt_embeds = torch.cat([neg_embeds, pos_embeds]) if self.guidance_scale > 1 else pos_embeds |
| 93 | + |
| 94 | + sigmas = get_original_sigmas(1000, self.num_steps) |
| 95 | + timesteps, _ = retrieve_timesteps(scheduler, self.num_steps, device, None, sigmas, mu=0.0) |
| 96 | + |
| 97 | + for t in timesteps: |
| 98 | + # Prepare model input efficiently |
| 99 | + if self.guidance_scale > 1: |
| 100 | + latent_model_input = torch.cat([latents] * 2) |
| 101 | + else: |
| 102 | + latent_model_input = latents |
| 103 | + |
| 104 | + # Prepare timestep tensor efficiently |
| 105 | + if isinstance(t, torch.Tensor): |
| 106 | + timestep_tensor = t.expand(latent_model_input.shape[0]) |
| 107 | + else: |
| 108 | + timestep_tensor = torch.tensor([t] * latent_model_input.shape[0], device=device, dtype=torch.float32) |
| 109 | + |
| 110 | + noise_pred = transformer( |
| 111 | + latent_model_input, |
| 112 | + encoder_hidden_states=prompt_embeds, |
| 113 | + timestep=timestep_tensor, |
| 114 | + img_ids=latent_image_ids, |
| 115 | + txt_ids=text_ids, |
| 116 | + guidance=None, |
| 117 | + return_dict=False, |
| 118 | + )[0] |
| 119 | + |
| 120 | + if self.guidance_scale > 1: |
| 121 | + noise_uncond, noise_text = noise_pred.chunk(2) |
| 122 | + noise_pred = noise_uncond + self.guidance_scale * (noise_text - noise_uncond) |
| 123 | + |
| 124 | + # Convert timestep for scheduler |
| 125 | + t_step = float(t.item()) if isinstance(t, torch.Tensor) else float(t) |
| 126 | + |
| 127 | + # Use scheduler step with proper dtypes |
| 128 | + latents = scheduler.step(noise_pred, t_step, latents, return_dict=False)[0] |
| 129 | + |
| 130 | + assert isinstance(latents, torch.Tensor) |
| 131 | + saved_input_latents_tensor = context.tensors.save(latents) |
| 132 | + latents_output = LatentsField(latents_name=saved_input_latents_tensor) |
| 133 | + return BriaDenoiseInvocationOutput(latents=latents_output) |
0 commit comments