Dervlex commited on
Commit
7f2508e
·
verified ·
1 Parent(s): a0c956e

Upload 3 files

Browse files
flux_embedding_node.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import DiffusionPipeline, FluxTransformer2DModel
3
+ from torchao.quantization import quantize_, int8_weight_only
4
+ from sd_embed.embedding_funcs import get_weighted_text_embeddings_flux1
5
+
6
+ class WeightedFluxEmbeddings:
7
+ @classmethod
8
+ def INPUT_TYPES(cls):
9
+ return {
10
+ "required": {
11
+ "model_path": ("STRING", {"default":"black-forest-labs/FLUX.1-dev"}),
12
+ "prompt": ("STRING", {"multiline":True}),
13
+ "quantize": ("BOOLEAN", {"default": True}),
14
+ }
15
+ }
16
+ RETURN_TYPES = ("CONDITIONING",) # Note: Flux only returns positive conditioning
17
+ FUNCTION = "run"
18
+ CATEGORY = "embedding/FLUX"
19
+ _PIPELINE_CACHE = {}
20
+
21
+ def run(self, model_path, prompt, quantize):
22
+ # load or re-use the FLUX pipeline
23
+ pipe = self._PIPELINE_CACHE.get(model_path)
24
+ if pipe is None:
25
+ if quantize:
26
+ transformer = FluxTransformer2DModel.from_pretrained(
27
+ model_path,
28
+ subfolder="transformer",
29
+ torch_dtype=torch.bfloat16
30
+ )
31
+ quantize_(transformer, int8_weight_only())
32
+
33
+ pipe = DiffusionPipeline.from_pretrained(
34
+ model_path,
35
+ transformer=transformer,
36
+ torch_dtype=torch.bfloat16
37
+ )
38
+ pipe.enable_model_cpu_offload()
39
+ else:
40
+ pipe = DiffusionPipeline.from_pretrained(
41
+ model_path,
42
+ torch_dtype=torch.bfloat16
43
+ )
44
+ pipe.to("cuda")
45
+
46
+ self._PIPELINE_CACHE[model_path] = pipe
47
+
48
+ # get the embeddings (all torch.Tensor)
49
+ prompt_embeds, pooled_prompt_embeds = get_weighted_text_embeddings_flux1(
50
+ pipe=pipe, prompt=prompt
51
+ )
52
+
53
+ # Keep as tensors, just move to CPU if needed
54
+ prompt_embeds_cpu = prompt_embeds.detach().cpu()
55
+ pooled_prompt_embeds_cpu = pooled_prompt_embeds.detach().cpu()
56
+
57
+ # Format for ComfyUI - Note that FLUX only has positive embeddings
58
+ positive = [[prompt_embeds_cpu, {"pooled_output": pooled_prompt_embeds_cpu}]]
59
+
60
+ return (positive,)
61
+
62
+ NODE_CLASS_MAPPINGS = {
63
+ "WeightedFluxEmbeddings": WeightedFluxEmbeddings
64
+ }
65
+
66
+ NODE_DISPLAY_NAME_MAPPINGS = {
67
+ "WeightedFluxEmbeddings": "Weighted FLUX Embeddings"
68
+ }
sd3_embedding_node.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import StableDiffusion3Pipeline
3
+ from sd_embed.embedding_funcs import get_weighted_text_embeddings_sd3
4
+
5
+ class WeightedSD35Embeddings:
6
+ @classmethod
7
+ def INPUT_TYPES(cls):
8
+ return {
9
+ "required": {
10
+ "model_path": ("STRING", {"default":"stabilityai/stable-diffusion-3.5-large"}),
11
+ "prompt": ("STRING", {"multiline":True}),
12
+ "neg_prompt": ("STRING", {"multiline":True}),
13
+ }
14
+ }
15
+ RETURN_TYPES = ("CONDITIONING","CONDITIONING")
16
+ FUNCTION = "run"
17
+ CATEGORY = "embedding/SD35"
18
+ _PIPELINE_CACHE = {}
19
+
20
+ def run(self, model_path, prompt, neg_prompt):
21
+ # load or re-use the SD3.5 pipeline
22
+ pipe = self._PIPELINE_CACHE.get(model_path)
23
+ if pipe is None:
24
+ pipe = StableDiffusion3Pipeline.from_pretrained(
25
+ model_path,
26
+ torch_dtype=torch.float16
27
+ ).to("cuda")
28
+ self._PIPELINE_CACHE[model_path] = pipe
29
+
30
+ # get the 4 embeddings (all torch.Tensor on cuda)
31
+ prompt_embeds, prompt_neg_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds = get_weighted_text_embeddings_sd3(
32
+ pipe, prompt=prompt, neg_prompt=neg_prompt
33
+ )
34
+
35
+ # Keep as tensors, just move to CPU if needed
36
+ prompt_embeds_cpu = prompt_embeds.detach().cpu()
37
+ pooled_prompt_embeds_cpu = pooled_prompt_embeds.detach().cpu()
38
+ prompt_neg_embeds_cpu = prompt_neg_embeds.detach().cpu()
39
+ negative_pooled_prompt_embeds_cpu = negative_pooled_prompt_embeds.detach().cpu()
40
+
41
+ # Format for ComfyUI
42
+ positive = [[prompt_embeds_cpu, {"pooled_output": pooled_prompt_embeds_cpu}]]
43
+ negative = [[prompt_neg_embeds_cpu, {"pooled_output": negative_pooled_prompt_embeds_cpu}]]
44
+
45
+ return (positive, negative)
46
+
47
+ NODE_CLASS_MAPPINGS = {
48
+ "WeightedSD35Embeddings": WeightedSD35Embeddings
49
+ }
50
+
51
+ NODE_DISPLAY_NAME_MAPPINGS = {
52
+ "WeightedSD35Embeddings": "Weighted SD3.5 Embeddings"
53
+ }
sdxl_embedding_node.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # custom_nodes/sdxl_embed_node.py
2
+
3
+ import torch
4
+ import numpy as np
5
+ from diffusers import StableDiffusionXLPipeline
6
+ from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl
7
+
8
+ class WeightedSDXLEmbeddings:
9
+ @classmethod
10
+ def INPUT_TYPES(cls):
11
+ return {
12
+ "required": {
13
+ "model_path": ("STRING", {"default":"fluently/Fluently-XL-Final"}),
14
+ "prompt": ("STRING", {"multiline":True}),
15
+ "neg_prompt": ("STRING", {"multiline":True}),
16
+ }
17
+ }
18
+ RETURN_TYPES = ("CONDITIONING","CONDITIONING")
19
+ FUNCTION = "run"
20
+ CATEGORY = "embedding/SDXL"
21
+
22
+ # cache so we don’t reload every batch
23
+ _PIPELINE_CACHE = {}
24
+
25
+ import torch
26
+ import numpy as np
27
+ from diffusers import StableDiffusionXLPipeline
28
+ from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl
29
+
30
+ class WeightedSDXLEmbeddings:
31
+ @classmethod
32
+ def INPUT_TYPES(cls):
33
+ return {
34
+ "required": {
35
+ "model_path": ("STRING", {"default":"fluently/Fluently-XL-Final"}),
36
+ "prompt": ("STRING", {"multiline":True}),
37
+ "neg_prompt": ("STRING", {"multiline":True}),
38
+ }
39
+ }
40
+ RETURN_TYPES = ("CONDITIONING","CONDITIONING")
41
+ FUNCTION = "run"
42
+ CATEGORY = "embedding/SDXL"
43
+ # cache so we don't reload every batch
44
+ _PIPELINE_CACHE = {}
45
+
46
+ def run(self, model_path, prompt, neg_prompt):
47
+ # load or re-use the SDXL pipeline
48
+ pipe = self._PIPELINE_CACHE.get(model_path)
49
+ if pipe is None:
50
+ pipe = StableDiffusionXLPipeline.from_pretrained(
51
+ model_path,
52
+ torch_dtype=torch.float16
53
+ ).to("cuda")
54
+ self._PIPELINE_CACHE[model_path] = pipe
55
+
56
+ # get the 4 embeddings (all torch.Tensor on cuda)
57
+ pe, ne, pooled_pe, pooled_ne = get_weighted_text_embeddings_sdxl(
58
+ pipe, prompt=prompt, neg_prompt=neg_prompt
59
+ )
60
+
61
+ # Keep as tensors instead of converting to NumPy
62
+ # Just move to CPU if needed for ComfyUI
63
+ pe_tensor = pe.detach().cpu()
64
+ pooled_pe_tensor = pooled_pe.detach().cpu()
65
+ ne_tensor = ne.detach().cpu()
66
+ pooled_ne_tensor = pooled_ne.detach().cpu()
67
+
68
+ # Format correctly for ComfyUI
69
+ positive = [[pe_tensor, {"pooled_output": pooled_pe_tensor}]]
70
+ negative = [[ne_tensor, {"pooled_output": pooled_ne_tensor}]]
71
+
72
+ return (positive, negative)
73
+
74
+
75
+ NODE_CLASS_MAPPINGS = {
76
+ "WeightedSDXLEmbeddings": WeightedSDXLEmbeddings
77
+ }
78
+ NODE_DISPLAY_NAME_MAPPINGS = {
79
+ "WeightedSDXLEmbeddings": "Weighted SDXL Embeddings"
80
+ }