Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

feat: Add MOSS-image support (#17095)

Jukka Seppänene087d8f6cb7c
diff --git a/comfy/ldm/moss_image/model.py b/comfy/ldm/moss_image/model.py
new file mode 100644
index 000000000..3122b19ac
--- /dev/null
+++ b/comfy/ldm/moss_image/model.py
@@ -0,0 +1,192 @@
+import torch
+from torch import nn
+
+import comfy.model_management
+import comfy.model_prefetch
+import comfy.ops
+import comfy.patcher_extension
+import comfy.quant_ops
+from comfy.ldm.flux.layers import EmbedND, timestep_embedding
+from comfy.ldm.modules.attention import AttentionTensorContainer, ComfyAttention, optimized_attention
+
+
+def situ(hidden, gate):
+ # soft-capped gated activation
+ return (4.0 * torch.tanh(gate / 4.0) * torch.sigmoid(gate)) * (25.0 * torch.tanh(hidden / 25.0))
+
+
+def modulated_rms_norm(x, norm, scale, shift):
+ if comfy.model_management.in_training:
+ return norm(x) * (1 + scale) + shift
+ # the norm weight is folded into the scale of the fused rmsnorm(x) * (1 + scale) + shift
+ weight, _, stream = comfy.ops.cast_bias_weight(norm, x, offloadable=True)
+ out = comfy.quant_ops.ck.rms_adaln(x, weight * (1 + scale) - 1, shift, norm.eps)
+ comfy.ops.uncast_bias_weight(norm, weight, None, stream)
+ return out
+
+
+def gated_residual(x, y, gate):
+ # in place inside the memory compiler region; out of place keeps x for backward when training
+ if comfy.model_management.in_training:
+ return torch.addcmul(x, y, gate)
+ return x.addcmul_(y, gate)
+
+
+def rope_split_half(x, pe):
+ # differentiable split-half RoPE for training, the kitchen ops have no autograd
+ x_ = x.to(pe.dtype).unflatten(-1, (2, -1)).movedim(-2, -1).unsqueeze(-2)
+ return (pe[..., 0] * x_[..., 0] + pe[..., 1] * x_[..., 1]).movedim(-1, -2).flatten(-2).type_as(x)
+
+
+def attention(q, k, v, q_norm, k_norm, pe, heads, comfy_attention, transformer_options):
+ B, N, _ = q.shape
+ q = q.view(B, N, heads, -1)
+ k = k.view(B, N, heads, -1)
+ if comfy.model_management.in_training:
+ q, k = rope_split_half(q_norm(q), pe), rope_split_half(k_norm(k), pe)
+ else:
+ q_scale, _, q_stream = comfy.ops.cast_bias_weight(q_norm, q, offloadable=True)
+ k_scale, _, k_stream = comfy.ops.cast_bias_weight(k_norm, k, offloadable=True)
+ q, k = comfy.quant_ops.ck.rms_rope_split_half(q, k, pe, q_scale, k_scale, q_norm.eps)
+ comfy.ops.uncast_bias_weight(q_norm, q_scale, None, q_stream)
+ comfy.ops.uncast_bias_weight(k_norm, k_scale, None, k_stream)
+ q, k, v = AttentionTensorContainer(q.flatten(2)), AttentionTensorContainer(k.flatten(2)), AttentionTensorContainer(v)
+ return optimized_attention(q, k, v, heads, transformer_options=transformer_options, preferred_attention=comfy_attention)
+
+
+class TextBlock(nn.Module):
+ def __init__(self, dim, ffn_dim, num_heads, dtype=None, device=None, operations=None):
+ super().__init__()
+ self.comfy_attention = ComfyAttention()
+ self.num_heads = num_heads
+ self.norm1 = operations.RMSNorm(dim, eps=1e-6, dtype=dtype, device=device)
+ self.norm2 = operations.RMSNorm(dim, eps=1e-6, dtype=dtype, device=device)
+ self.q_norm = operations.RMSNorm(dim // num_heads, eps=1e-6, dtype=dtype, device=device)
+ self.k_norm = operations.RMSNorm(dim // num_heads, eps=1e-6, dtype=dtype, device=device)
+ self.qkv = operations.Linear(dim, 3 * dim, dtype=dtype, device=device)
+ self.proj = operations.Linear(dim, dim, dtype=dtype, device=device)
+ self.ffn_gate = operations.Linear(dim, ffn_dim, dtype=dtype, device=device)
+ self.ffn_up = operations.Linear(dim, ffn_dim, dtype=dtype, device=device)
+ self.ffn_down = operations.Linear(ffn_dim, dim, dtype=dtype, device=device)
+
+ def forward(self, x, pe, transformer_options={}):
+ q, k, v = self.qkv(self.norm1(x)).chunk(3, dim=-1)
+ x = x + self.proj(attention(q, k, v, self.q_norm, self.k_norm, pe, self.num_heads, self.comfy_attention, transformer_options))
+ y = self.norm2(x)
+ return x + self.ffn_down(situ(self.ffn_up(y), self.ffn_gate(y)))
+
+
+class TextEmbedder(nn.Module):
+ def __init__(self, text_dim, dim, ffn_dim, num_heads, dtype=None, device=None, operations=None):
+ super().__init__()
+ self.proj = operations.Linear(text_dim, dim, dtype=dtype, device=device)
+ self.blocks = nn.ModuleList([TextBlock(dim, ffn_dim, num_heads, dtype=dtype, device=device, operations=operations) for _ in range(2)])
+
+ def forward(self, x, pe, transformer_options={}):
+ x = self.proj(x)
+ prefetch_queue = comfy.model_prefetch.make_prefetch_queue(list(self.blocks), x.device, transformer_options)
+ for block in self.blocks:
+ comfy.model_prefetch.prefetch_queue_pop(prefetch_queue, x.device, block, x.dtype)
+ x = block(x, pe, transformer_options)
+ comfy.model_prefetch.prefetch_queue_pop(prefetch_queue, x.device, None)
+ return x
+
+
+class Block(nn.Module):
+ def __init__(self, dim, ffn_dim, num_heads, fused_qkv=False, dtype=None, device=None, operations=None):
+ super().__init__()
+ self.comfy_attention = ComfyAttention()
+ self.num_heads = num_heads
+ self.fused_qkv = fused_qkv
+ self.norm1 = operations.RMSNorm(dim, eps=1e-6, dtype=dtype, device=device)
+ self.norm2 = operations.RMSNorm(dim, eps=1e-6, dtype=dtype, device=device)
+ self.q_norm = operations.RMSNorm(dim // num_heads, eps=1e-6, dtype=dtype, device=device)
+ self.k_norm = operations.RMSNorm(dim // num_heads, eps=1e-6, dtype=dtype, device=device)
+ # repacked files fuse q/k/v: one GEMM and one activation quantization instead of three
+ names = ("qkv", "o") if fused_qkv else ("q", "k", "v", "o")
+ self.proj = nn.ModuleDict({name: operations.Linear(dim, 3 * dim if name == "qkv" else dim, bias=False, dtype=dtype, device=device) for name in names})
+ self.proj["ffn_up_gate"] = operations.Linear(dim, 2 * ffn_dim, bias=False, dtype=dtype, device=device)
+ self.proj["ffn_down"] = operations.Linear(ffn_dim, dim, bias=False, dtype=dtype, device=device)
+
+ def forward(self, x, mod, pe, transformer_options={}):
+ shift_a, scale_a, gate_a, shift_f, scale_f, gate_f = mod
+ y = modulated_rms_norm(x, self.norm1, scale_a, shift_a)
+ if self.fused_qkv:
+ q, k, v = self.proj["qkv"](y).chunk(3, dim=-1)
+ else:
+ q, k, v = self.proj["q"](y), self.proj["k"](y), self.proj["v"](y)
+ out = attention(q, k, v, self.q_norm, self.k_norm, pe, self.num_heads, self.comfy_attention, transformer_options)
+ x = gated_residual(x, self.proj["o"](out), gate_a)
+ y = modulated_rms_norm(x, self.norm2, scale_f, shift_f)
+ hidden, gate = self.proj["ffn_up_gate"](y).chunk(2, dim=-1)
+ # situ without its 4 * 25 factor, applied on the gate instead (ffn_down has no bias): two fewer passes
+ out = self.proj["ffn_down"](torch.tanh(gate / 4.0) * torch.sigmoid(gate) * torch.tanh(hidden / 25.0))
+ return gated_residual(x, out, gate_f * 100.0)
+
+
+class MOSSImage(nn.Module):
+ def __init__(self, dim=3072, ffn_dim=8192, num_heads=24, num_layers=42, text_dim=4096, latent_channels=128, fused_qkv=False, image_model=None, dtype=None, device=None, operations=None):
+ super().__init__()
+ self.dtype = dtype
+ self.latent_channels = latent_channels
+ head_dim = dim // num_heads
+ self.pe_embedder = EmbedND(dim=head_dim, theta=10000, axes_dim=[head_dim // 4] * 4)
+ self.video_embedder = operations.Linear(latent_channels, dim, dtype=dtype, device=device)
+ self.text_embedder = TextEmbedder(text_dim, dim, ffn_dim, num_heads, dtype=dtype, device=device, operations=operations)
+ self.time_embedding = nn.Sequential(operations.Linear(256, dim, dtype=dtype, device=device), nn.SiLU(), operations.Linear(dim, dim, dtype=dtype, device=device))
+ self.shared_block_modulation = operations.Linear(dim, 6 * dim, dtype=dtype, device=device)
+ self.layers = nn.ModuleList([Block(dim, ffn_dim, num_heads, fused_qkv=fused_qkv, dtype=dtype, device=device, operations=operations) for _ in range(num_layers)])
+ self.final_norm_video = operations.RMSNorm(dim, eps=1e-6, dtype=dtype, device=device)
+ self.final_linear_video = operations.Linear(dim, latent_channels, bias=False, dtype=dtype, device=device)
+ self.video_head_modulation = operations.Linear(dim, 2 * dim, dtype=dtype, device=device)
+
+ def rope(self, h, w, text_len, device):
+ # (r, f, h, w): the image sits after the text on f, its rows and columns centered
+ ids = torch.zeros(h * w + text_len, 4, device=device, dtype=torch.float32)
+ ids[:h * w, 1] = text_len
+ ids[:h * w, 2] = (torch.arange(h, device=device, dtype=torch.float32) - (h - 1) / 2).repeat_interleave(w)
+ ids[:h * w, 3] = (torch.arange(w, device=device, dtype=torch.float32) - (w - 1) / 2).repeat(h)
+ ids[h * w:, 1] = torch.arange(text_len, device=device, dtype=torch.float32)
+ return self.pe_embedder(ids.unsqueeze(0)).transpose(1, 2).contiguous()
+
+ def forward(self, x, timestep, context, transformer_options={}, **kwargs):
+ return comfy.patcher_extension.WrapperExecutor.new_class_executor(
+ self._forward,
+ self,
+ comfy.patcher_extension.get_all_wrappers(comfy.patcher_extension.WrappersMP.DIFFUSION_MODEL, transformer_options)
+ ).execute(x, timestep, context, transformer_options, **kwargs)
+
+ def _forward(self, x, timestep, context, transformer_options={}, **kwargs):
+ B, _, H, W = x.shape
+ dtype, device = x.dtype, x.device
+ transformer_options = transformer_options.copy()
+ pe = self.rope(H, W, context.shape[1], device)
+
+ vec = self.time_embedding(timestep_embedding(timestep, 256).to(dtype))
+ mod = self.shared_block_modulation(vec).unflatten(-1, (6, -1)).unsqueeze(2).unbind(1)
+ head_shift, head_scale = self.video_head_modulation(vec).unflatten(-1, (2, -1)).unsqueeze(2).unbind(1)
+
+ txt = self.text_embedder(context, pe[:, H * W:], transformer_options)
+ # allocated outside the memory compiler region, the blocks update it in place
+ x = torch.cat((self.video_embedder(x.flatten(2).transpose(1, 2)), txt), dim=1)
+ del txt
+
+ blocks_replace = transformer_options.get("patches_replace", {}).get("dit", {})
+ transformer_options["total_blocks"] = len(self.layers)
+ transformer_options["block_type"] = "single"
+ prefetch_queue = comfy.model_prefetch.make_prefetch_queue(list(self.layers), device, transformer_options)
+ comfy.model_prefetch.malloc_graph_begin(device)
+ for i, block in enumerate(self.layers):
+ comfy.model_prefetch.prefetch_queue_pop(prefetch_queue, device, block, dtype, malloc_scope="block")
+ transformer_options["block_index"] = i
+ if ("single_block", i) in blocks_replace:
+ def block_wrap(args):
+ return {"img": block(args["img"], args["mod"], args["pe"], args["transformer_options"])}
+ x = blocks_replace[("single_block", i)]({"img": x, "vec": vec, "mod": mod, "pe": pe, "transformer_options": transformer_options}, {"original_block": block_wrap})["img"]
+ else:
+ x = block(x, mod, pe, transformer_options)
+ comfy.model_prefetch.prefetch_queue_pop(prefetch_queue, device, None, malloc_scope="block")
+ comfy.model_prefetch.malloc_graph_end()
+
+ out = self.final_linear_video(modulated_rms_norm(x[:, :H * W], self.final_norm_video, head_scale, head_shift))
+ return out.transpose(1, 2).reshape(B, self.latent_channels, H, W)
diff --git a/comfy/lora.py b/comfy/lora.py
index 061550067..d5b5c0c35 100644
--- a/comfy/lora.py
+++ b/comfy/lora.py
@@ -352,6 +352,15 @@ def model_lora_keys_unet(model, key_map={}):
key_map[key_lora] = (k, (0, i * third, third))
key_map["lora_unet_{}".format(key_lora[len("diffusion_model."):].replace(".", "_"))] = (k, (0, i * third, third))
+ if isinstance(model, comfy.model_base.MOSSImage):
+ for k in sdk:
+ if k.endswith(".proj.qkv.weight"): # fused q/k/v files; LoRAs address the separate projections
+ third = sd[k].shape[0] // 3
+ for i, proj in enumerate(("q", "k", "v")):
+ key_lora = "{}.{}".format(k[:-len(".qkv.weight")], proj)
+ key_map[key_lora] = (k, (0, i * third, third))
+ key_map["lora_unet_{}".format(key_lora[len("diffusion_model."):].replace(".", "_"))] = (k, (0, i * third, third))
+
if isinstance(model, comfy.model_base.Krea2):
diffusers_keys = comfy.utils.krea2_to_diffusers(model.model_config.unet_config, output_prefix="diffusion_model.")
for k in diffusers_keys:
diff --git a/comfy/model_base.py b/comfy/model_base.py
index f7514770f..12c61e53b 100644
--- a/comfy/model_base.py
+++ b/comfy/model_base.py
@@ -83,6 +83,7 @@ import comfy.ldm.sensenova.conditioning
import comfy.ldm.sensenova.model
from comfy.ldm.sensenova.sampling import SenseNovaModelSampling, time_snr_shift
import comfy.ldm.depth_anything_3.model
+import comfy.ldm.moss_image.model
import comfy.model_management
import comfy.patcher_extension
@@ -2795,6 +2796,18 @@ class Krea2(BaseModel):
out['ref_latents'] = list([1, 16, sum(map(lambda a: math.prod(a.size()), ref_latents)) // 16])
return out
+class MOSSImage(BaseModel):
+ def __init__(self, model_config, model_type=ModelType.FLOW, device=None):
+ super().__init__(model_config, model_type, device=device, unet_model=comfy.ldm.moss_image.model.MOSSImage)
+
+ def extra_conds(self, **kwargs):
+ out = super().extra_conds(**kwargs)
+ cross_attn = kwargs.get("cross_attn", None)
+ # rope positions follow the true text length, so no repeat padding: cond and uncond of different lengths run separately
+ if cross_attn is not None:
+ out['c_crossattn'] = comfy.conds.CONDRegular(cross_attn)
+ return out
+
class HunyuanImage21(BaseModel):
def __init__(self, model_config, model_type=ModelType.FLOW, device=None):
super().__init__(model_config, model_type, device=device, unet_model=comfy.ldm.hunyuan_video.model.HunyuanVideo)
diff --git a/comfy/model_detection.py b/comfy/model_detection.py
index 78e71d86b..78b9e52e3 100644
--- a/comfy/model_detection.py
+++ b/comfy/model_detection.py
@@ -1018,6 +1018,16 @@ def detect_unet_config(state_dict, key_prefix, metadata=None):
dit_config["num_layers"] = count_blocks(state_dict_keys, '{}layers.'.format(key_prefix) + '{}.')
return dit_config
+ if '{}shared_block_modulation.weight'.format(key_prefix) in state_dict_keys and '{}layers.0.proj.ffn_up_gate.weight'.format(key_prefix) in state_dict_keys: # MOSS-Image
+ dit_config = {}
+ dit_config["image_model"] = "moss_image"
+ dit_config["dim"] = state_dict['{}shared_block_modulation.weight'.format(key_prefix)].shape[0] // 6
+ dit_config["ffn_dim"] = state_dict['{}layers.0.proj.ffn_up_gate.weight'.format(key_prefix)].shape[0] // 2
+ dit_config["num_heads"] = dit_config["dim"] // 128
+ dit_config["num_layers"] = count_blocks(state_dict_keys, '{}layers.'.format(key_prefix) + '{}.')
+ dit_config["fused_qkv"] = '{}layers.0.proj.qkv.weight'.format(key_prefix) in state_dict_keys
+ return dit_config
+
if '{}txtfusion.projector.weight'.format(key_prefix) in state_dict_keys: # Krea 2 (K2)
dit_config = {}
dit_config["image_model"] = "krea2"
diff --git a/comfy/sd.py b/comfy/sd.py
index 02f8f3bb1..6d5ef40c5 100644
--- a/comfy/sd.py
+++ b/comfy/sd.py
@@ -92,6 +92,7 @@ import comfy.text_encoders.cogvideo
import comfy.text_encoders.sa3
import comfy.text_encoders.gpt_oss
import comfy.text_encoders.joyimage
+import comfy.text_encoders.moss_image
import comfy.model_patcher
import comfy.lora
@@ -1609,6 +1610,7 @@ class CLIPType(Enum):
MINIMAX = 35
YUE2 = 36
FLUX3 = 37
+ MOSS_IMAGE = 38
@@ -1962,9 +1964,13 @@ def load_text_encoder_state_dicts(state_dicts=[], embedding_directory=None, clip
clip_target.tokenizer = comfy.text_encoders.jina_clip_2.JinaClip2TokenizerWrapper
elif te_model in (TEModel.QWEN35_08B, TEModel.QWEN35_2B, TEModel.QWEN35_4B, TEModel.QWEN35_9B, TEModel.QWEN35_27B):
clip_data[0] = comfy.utils.state_dict_prefix_replace(clip_data[0], {"model.language_model.": "model.", "model.visual.": "visual.", "lm_head.": "model.lm_head."})
- qwen35_type = {TEModel.QWEN35_08B: "qwen35_08b", TEModel.QWEN35_2B: "qwen35_2b", TEModel.QWEN35_4B: "qwen35_4b", TEModel.QWEN35_9B: "qwen35_9b", TEModel.QWEN35_27B: "qwen35_27b"}[te_model]
- clip_target.clip = comfy.text_encoders.qwen35.te(**llama_detect(clip_data), model_type=qwen35_type, mtp="mtp.fc.weight" in clip_data[0])
- clip_target.tokenizer = comfy.text_encoders.qwen35.tokenizer(model_type=qwen35_type)
+ if clip_type == CLIPType.MOSS_IMAGE and te_model == TEModel.QWEN35_9B: # MOSS-Image: stock Qwen3.5-9B, last hidden state, open think block.
+ clip_target.clip = comfy.text_encoders.moss_image.te(**llama_detect(clip_data))
+ clip_target.tokenizer = comfy.text_encoders.moss_image.MOSSImageTokenizer
+ else:
+ qwen35_type = {TEModel.QWEN35_08B: "qwen35_08b", TEModel.QWEN35_2B: "qwen35_2b", TEModel.QWEN35_4B: "qwen35_4b", TEModel.QWEN35_9B: "qwen35_9b", TEModel.QWEN35_27B: "qwen35_27b"}[te_model]
+ clip_target.clip = comfy.text_encoders.qwen35.te(**llama_detect(clip_data), model_type=qwen35_type, mtp="mtp.fc.weight" in clip_data[0])
+ clip_target.tokenizer = comfy.text_encoders.qwen35.tokenizer(model_type=qwen35_type)
elif te_model in (TEModel.QWEN3VL_4B, TEModel.QWEN3VL_8B):
if clip_type == CLIPType.IDEOGRAM4 and te_model == TEModel.QWEN3VL_8B: # Ideogram4 reuses the full Qwen3-VL-8B (13-layer tap for conditioning + multimodal generate).
clip_data[0] = comfy.utils.state_dict_prefix_replace(clip_data[0], {"model.language_model.": "model.", "model.visual.": "visual.", "lm_head.": "model.lm_head."})
diff --git a/comfy/supported_models.py b/comfy/supported_models.py
index 939cbea9b..c136c95f1 100644
--- a/comfy/supported_models.py
+++ b/comfy/supported_models.py
@@ -42,6 +42,7 @@ import comfy.text_encoders.cogvideo
import comfy.text_encoders.hidream_o1
import comfy.text_encoders.sensenova
import comfy.text_encoders.pixeldit
+import comfy.text_encoders.moss_image
from . import supported_models_base
from . import latent_formats
@@ -2033,6 +2034,39 @@ class Krea2(supported_models_base.BASE):
hunyuan_detect = comfy.text_encoders.hunyuan_video.llama_detect(state_dict, "{}qwen3vl_4b.transformer.".format(pref))
return supported_models_base.ClipTarget(comfy.text_encoders.krea2.Krea2Tokenizer, comfy.text_encoders.krea2.te(**hunyuan_detect))
+class MOSSImage(supported_models_base.BASE):
+ unet_config = {
+ "image_model": "moss_image",
+ }
+
+ sampling_settings = {
+ "multiplier": 1.0,
+ "shift": 5.0,
+ }
+
+ memory_usage_factor = 14.6
+
+ unet_extra_config = {}
+ latent_format = latent_formats.Flux2
+
+ supported_inference_dtypes = [torch.bfloat16, torch.float32]
+
+ vae_key_prefix = ["vae."]
+ text_encoder_key_prefix = ["text_encoders."]
+
+ def process_unet_state_dict(self, state_dict):
+ state_dict.pop("rope.bands", None)
+ return state_dict
+
+ def get_model(self, state_dict, prefix="", device=None):
+ out = model_base.MOSSImage(self, device=device)
+ return out
+
+ def clip_target(self, state_dict={}):
+ pref = self.text_encoder_key_prefix[0]
+ hunyuan_detect = comfy.text_encoders.hunyuan_video.llama_detect(state_dict, "{}qwen35_9b.transformer.".format(pref))
+ return supported_models_base.ClipTarget(comfy.text_encoders.moss_image.MOSSImageTokenizer, comfy.text_encoders.moss_image.te(**hunyuan_detect))
+
class MageFlow(supported_models_base.BASE):
unet_config = {
"image_model": "mage_flow",
@@ -2684,6 +2718,7 @@ models = [
JoyImage,
Ideogram4,
Krea2,
+ MOSSImage,
Flux2,
Flux3,
Lens,
diff --git a/comfy/text_encoders/moss_image.py b/comfy/text_encoders/moss_image.py
new file mode 100644
index 000000000..bd26280ba
--- /dev/null
+++ b/comfy/text_encoders/moss_image.py
@@ -0,0 +1,31 @@
+import comfy.text_encoders.qwen35
+from comfy import sd1_clip
+
+
+class MOSSImageTokenizer(comfy.text_encoders.qwen35.Qwen35ImageTokenizer):
+ def __init__(self, embedding_directory=None, tokenizer_data={}):
+ super().__init__(embedding_directory=embedding_directory, tokenizer_data=tokenizer_data, model_type="qwen35_9b")
+
+ def tokenize_with_weights(self, text, return_word_ids=False, llama_template=None, thinking=None, **kwargs):
+ if llama_template is None and thinking is None and not text.startswith('<|im_start|>'):
+ # conditioning: the chat template with enable_thinking=True trims the user turn and leaves an open think block
+ text, llama_template, thinking = text.strip(), "<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n<think>\n", True
+ return super().tokenize_with_weights(text, return_word_ids=return_word_ids, llama_template=llama_template, thinking=bool(thinking), **kwargs)
+
+
+class MOSSImageTEModel(sd1_clip.SD1ClipModel):
+ def __init__(self, device="cpu", dtype=None, model_options={}):
+ clip_model = lambda **kw: comfy.text_encoders.qwen35.Qwen35ClipModel(**kw, layer="last", layer_idx=None, model_type="qwen35_9b")
+ super().__init__(device=device, dtype=dtype, name="qwen35_9b", clip_model=clip_model, model_options=model_options)
+
+
+def te(dtype_llama=None, llama_quantization_metadata=None):
+ class MOSSImageTEModel_(MOSSImageTEModel):
+ def __init__(self, device="cpu", dtype=None, model_options={}):
+ if dtype_llama is not None:
+ dtype = dtype_llama
+ if llama_quantization_metadata is not None:
+ model_options = model_options.copy()
+ model_options["quantization_metadata"] = llama_quantization_metadata
+ super().__init__(device=device, dtype=dtype, model_options=model_options)
+ return MOSSImageTEModel_
diff --git a/comfy_extras/nodes_moss_image.py b/comfy_extras/nodes_moss_image.py
new file mode 100644
index 000000000..6f8a4d16b
--- /dev/null
+++ b/comfy_extras/nodes_moss_image.py
@@ -0,0 +1,59 @@
+import json
+import math
+import re
+
+from typing_extensions import override
+from comfy_api.latest import ComfyExtension, io
+
+
+class MOSSImageLayoutPrompt(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(
+ node_id="MOSSImageLayoutPrompt",
+ display_name="MOSS-Image Layout Prompt",
+ category="model/conditioning/moss_image",
+ description="Builds a MOSS-Image layout prompt from the prompt and the boxes from Create Bounding Boxes.",
+ inputs=[
+ io.String.Input("prompt", multiline=True, dynamic_prompts=True, tooltip="Description of the whole image."),
+ io.Array.Input("element", tooltip="Prompt elements from the node Create Bounding Boxes. The description is the box's caption, text boxes also render their text."),
+ io.Int.Input("width", default=1024, min=16, max=16384, step=16, tooltip="Width of the generated image; the boxes are placed in its pixels."),
+ io.Int.Input("height", default=1024, min=16, max=16384, step=16, tooltip="Height of the generated image; the boxes are placed in its pixels."),
+ io.String.Input("character_boxes", default="", tooltip="Numbers of the object boxes to label as characters instead of objects, as tagged in the Create Bounding Boxes preview, e.g. \"1, 3\"."),
+ ],
+ outputs=[io.String.Output(display_name="prompt")],
+ is_experimental=True,
+ )
+
+ @classmethod
+ def execute(cls, prompt, element, width, height, character_boxes="") -> io.NodeOutput:
+ characters = {int(i) for i in re.findall(r"\d+", character_boxes)}
+ items = []
+ for i, el in enumerate(element if isinstance(element, list) else []):
+ label = "text" if el.get("type") == "text" else ("character" if i + 1 in characters else "object")
+ item = {"label": label, "caption": el.get("desc", "")}
+ if item["label"] == "text":
+ item["text"] = el.get("text", "")
+ if el.get("color_palette"):
+ item["palette"] = el["color_palette"]
+ # 0-1000 [y0, x0, y1, x1] -> pixel edges enclosing the box, origin at the canvas centre
+ y0, x0, y1, x1 = el["bbox"]
+ item["bbox"] = [math.floor(round(y0 * height / 1000, 9)) - height // 2, math.floor(round(x0 * width / 1000, 9)) - width // 2,
+ math.ceil(round(y1 * height / 1000, 9)) - height // 2, math.ceil(round(x1 * width / 1000, 9)) - width // 2]
+ items.append(item)
+ if not items:
+ return io.NodeOutput(prompt)
+ layout = {"summary": {"caption": prompt.strip()}, "items": items, "bbox_coord_space": "abs_centralized", "bbox_format": "yxyx"}
+ return io.NodeOutput(json.dumps(layout, ensure_ascii=False, separators=(",", ":")))
+
+
+class MOSSImageExtension(ComfyExtension):
+ @override
+ async def get_node_list(self) -> list[type[io.ComfyNode]]:
+ return [
+ MOSSImageLayoutPrompt,
+ ]
+
+
+async def comfy_entrypoint() -> MOSSImageExtension:
+ return MOSSImageExtension()
diff --git a/nodes.py b/nodes.py
index e0ef36232..fc30bed26 100644
--- a/nodes.py
+++ b/nodes.py
@@ -1018,7 +1018,7 @@ class CLIPLoader:
@classmethod
def INPUT_TYPES(s):
return {"required": { "clip_name": (folder_paths.get_filename_list("text_encoders"), ),
- "type": (["stable_diffusion", "stable_cascade", "sd3", "stable_audio", "mochi", "ltxv", "pixart", "cosmos", "lumina2", "wan", "hidream", "chroma", "ace", "omnigen2", "qwen_image", "hunyuan_image", "flux2", "ovis", "longcat_image", "cogvideox", "lens", "pixeldit", "ideogram4", "boogu", "krea2", "joyimage", "mage", "minimax", "yue2", "flux3"], ),
+ "type": (["stable_diffusion", "stable_cascade", "sd3", "stable_audio", "mochi", "ltxv", "pixart", "cosmos", "lumina2", "wan", "hidream", "chroma", "ace", "omnigen2", "qwen_image", "hunyuan_image", "flux2", "ovis", "longcat_image", "cogvideox", "lens", "pixeldit", "ideogram4", "boogu", "krea2", "joyimage", "mage", "minimax", "yue2", "flux3", "moss_image"], ),
},
"optional": {
"device": (["default", "cpu"], {"advanced": True}),
@@ -2507,6 +2507,7 @@ async def init_builtin_extra_nodes():
"nodes_mage.py",
"nodes_joyimage.py",
"nodes_boogu.py",
+ "nodes_moss_image.py",
"nodes_chroma_radiance.py",
"nodes_pid.py",
"nodes_model_patch.py",