File size: 2,492 Bytes
7f2508e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from diffusers import DiffusionPipeline, FluxTransformer2DModel
from torchao.quantization import quantize_, int8_weight_only
from sd_embed.embedding_funcs import get_weighted_text_embeddings_flux1

class WeightedFluxEmbeddings:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "model_path": ("STRING", {"default":"black-forest-labs/FLUX.1-dev"}),
                "prompt":     ("STRING", {"multiline":True}),
                "quantize":   ("BOOLEAN", {"default": True}),
            }
        }
    RETURN_TYPES = ("CONDITIONING",)  # Note: Flux only returns positive conditioning
    FUNCTION     = "run"
    CATEGORY     = "embedding/FLUX"
    _PIPELINE_CACHE = {}
    
    def run(self, model_path, prompt, quantize):
        # load or re-use the FLUX pipeline
        pipe = self._PIPELINE_CACHE.get(model_path)
        if pipe is None:
            if quantize:
                transformer = FluxTransformer2DModel.from_pretrained(
                    model_path,
                    subfolder="transformer",
                    torch_dtype=torch.bfloat16
                )
                quantize_(transformer, int8_weight_only())
                
                pipe = DiffusionPipeline.from_pretrained(
                    model_path,
                    transformer=transformer,
                    torch_dtype=torch.bfloat16
                )
                pipe.enable_model_cpu_offload()
            else:
                pipe = DiffusionPipeline.from_pretrained(
                    model_path,
                    torch_dtype=torch.bfloat16
                )
                pipe.to("cuda")
            
            self._PIPELINE_CACHE[model_path] = pipe
        
        # get the embeddings (all torch.Tensor)
        prompt_embeds, pooled_prompt_embeds = get_weighted_text_embeddings_flux1(
            pipe=pipe, prompt=prompt
        )
        
        # Keep as tensors, just move to CPU if needed
        prompt_embeds_cpu = prompt_embeds.detach().cpu()
        pooled_prompt_embeds_cpu = pooled_prompt_embeds.detach().cpu()
        
        # Format for ComfyUI - Note that FLUX only has positive embeddings
        positive = [[prompt_embeds_cpu, {"pooled_output": pooled_prompt_embeds_cpu}]]
        
        return (positive,)

NODE_CLASS_MAPPINGS = {
    "WeightedFluxEmbeddings": WeightedFluxEmbeddings
}

NODE_DISPLAY_NAME_MAPPINGS = {
    "WeightedFluxEmbeddings": "Weighted FLUX Embeddings"
}