File size: 2,060 Bytes
ca114db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from diffusers import StableDiffusion3Pipeline
from sd_embed.embedding_funcs import get_weighted_text_embeddings_sd3

class WeightedSD35Embeddings:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "model_path": ("STRING", {"default":"stabilityai/stable-diffusion-3.5-large"}),
                "prompt":     ("STRING", {"multiline":True}),
                "neg_prompt": ("STRING", {"multiline":True}),
            }
        }
    RETURN_TYPES = ("CONDITIONING","CONDITIONING")
    FUNCTION     = "run"
    CATEGORY     = "embedding/SD35"
    _PIPELINE_CACHE = {}
    
    def run(self, model_path, prompt, neg_prompt):
        # load or re-use the SD3.5 pipeline
        pipe = self._PIPELINE_CACHE.get(model_path)
        if pipe is None:
            pipe = StableDiffusion3Pipeline.from_pretrained(
                model_path,
                torch_dtype=torch.float16
            ).to("cuda")
            self._PIPELINE_CACHE[model_path] = pipe
        
        # get the 4 embeddings (all torch.Tensor on cuda)
        prompt_embeds, prompt_neg_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds = get_weighted_text_embeddings_sd3(
            pipe, prompt=prompt, neg_prompt=neg_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()
        prompt_neg_embeds_cpu = prompt_neg_embeds.detach().cpu()
        negative_pooled_prompt_embeds_cpu = negative_pooled_prompt_embeds.detach().cpu()
        
        # Format for ComfyUI
        positive = [[prompt_embeds_cpu, {"pooled_output": pooled_prompt_embeds_cpu}]]
        negative = [[prompt_neg_embeds_cpu, {"pooled_output": negative_pooled_prompt_embeds_cpu}]]
        
        return (positive, negative)

NODE_CLASS_MAPPINGS = {
    "WeightedSD35Embeddings": WeightedSD35Embeddings
}

NODE_DISPLAY_NAME_MAPPINGS = {
    "WeightedSD35Embeddings": "Weighted SD3.5 Embeddings"
}