diff --git a/backend/moss_image/LICENSE b/backend/moss_image/LICENSE new file mode 100644 index 00000000..0e5a49f5 --- /dev/null +++ b/backend/moss_image/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [2023] [Zhongjie Duan] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/backend/moss_image/NOTICE b/backend/moss_image/NOTICE new file mode 100644 index 00000000..b67b97fa --- /dev/null +++ b/backend/moss_image/NOTICE @@ -0,0 +1,33 @@ +MOSS-Image inference port for SSUI + +dit.py, vae.py, ops.py: +Adapted from modelscope/DiffSynth-Studio, Apache-2.0, +commit 08fdc0da8f41426885d9432e9007e6e460a4c30d: + diffsynth/models/moss_image_dit.py + diffsynth/models/flux2_vae.py + diffsynth/models/general_modules.py (RMSNorm) +Changes: removed DiffSynth runtime, training/checkpoint wrappers and optimized +attention dependency; use native Torch SDPA; calculate FP64 time sinusoid on CPU +for MPS tensors. The public SSUI loader currently enables CUDA/ROCm and CPU only. + +qwen_ops.py and text_encoder.py: +Copyright 2025 The Qwen Team and The HuggingFace Inc. team. All rights reserved. +Adapted from huggingface/transformers, Apache-2.0, +commit aad13b87ed59f2afcfaebc985f403301887a35fc (v5.3.0): + src/transformers/models/qwen3_5/modeling_qwen3_5.py +Text configuration from DiffSynth-Studio's moss_image_text_encoder.py at the +commit above. Changes: standalone text-only forward, no cache or generation, +pure Torch gated delta rule, native SDPA, text-only default RoPE, per-prompt +unpadded inference. No Transformers 5 internals are imported or monkey-patched. + +pipeline.py sigma schedule and prompt formatting: +Adapted from OpenMOSS-Team/MOSS-Image-Base, Apache-2.0 pipeline code, +Hugging Face revision 85975793d8b3d432f1b6e27f352abaa4c2a4c0d3: + pipeline_moss_image.py + tokenizer/chat_template.jinja (single-user text prompt) +Changes: standalone Euler loop, explicit component offload, local safetensors +loader, no downloaded Python execution or Diffusers pipeline dependency. + +LICENSE contains the Apache License 2.0 applicable to these upstream portions. +Model weights and other downloaded assets are not distributed with this code; +their respective model cards and licenses apply separately. diff --git a/backend/moss_image/__init__.py b/backend/moss_image/__init__.py new file mode 100644 index 00000000..ef9d05a3 --- /dev/null +++ b/backend/moss_image/__init__.py @@ -0,0 +1 @@ +"""Standalone MOSS-Image inference; no Transformers 5 or remote Python required.""" diff --git a/backend/moss_image/dit.py b/backend/moss_image/dit.py new file mode 100644 index 00000000..2a23acdb --- /dev/null +++ b/backend/moss_image/dit.py @@ -0,0 +1,225 @@ +# SPDX-License-Identifier: Apache-2.0 +# Adapted from DiffSynth-Studio; see NOTICE for pinned sources and changes. +import torch +import torch.nn as nn +from typing import Optional + +from .ops import RMSNorm, attention_forward + + +def situ(hidden: torch.Tensor, gate: torch.Tensor) -> torch.Tensor: + return (4.0 * torch.tanh(gate / 4.0) * torch.sigmoid(gate)) * (25.0 * torch.tanh(hidden / 25.0)) + + +def validate_skip_layers(layers, depth: int, name: str = "skip_layers"): + layers = () if layers is None else tuple(layers) + if any(type(index) is not int or not 0 <= index < depth for index in layers): + raise ValueError(f"{name} must contain integer block indices in [0, {depth}).") + return frozenset(layers) + + +def rotate(x: torch.Tensor, rope: torch.Tensor) -> torch.Tensor: + sin, cos = rope.chunk(2, -1) + sin = torch.cat((sin, sin), -1).unsqueeze(-2) + cos = torch.cat((cos, cos), -1).unsqueeze(-2) + a, b = x.chunk(2, -1) + return x * cos + torch.cat((-b, a), -1) * sin + + +def attention(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, rope: torch.Tensor, heads: int) -> torch.Tensor: + shape = (*q.shape[:2], heads, q.shape[-1] // heads) + q = rotate(q.reshape(shape), rope) + k = rotate(k.reshape(shape), rope) + v = v.reshape(shape) + out = attention_forward( + q.to(v.dtype), k.to(v.dtype), v, + q_pattern="b s n d", k_pattern="b s n d", v_pattern="b s n d", out_pattern="b s n d", + ) + return out.flatten(2) + + +class MOSSImageTextBlock(nn.Module): + def __init__(self, dim: int, ffn_dim: int, heads: int): + super().__init__() + self.heads = heads + self.norm1, self.norm2 = RMSNorm(dim, eps=1e-6), RMSNorm(dim, eps=1e-6) + self.q_norm, self.k_norm = RMSNorm(dim // heads, eps=1e-6), RMSNorm(dim // heads, eps=1e-6) + self.qkv = nn.Linear(dim, 3 * dim) + self.proj = nn.Linear(dim, dim) + self.ffn_gate, self.ffn_up = nn.Linear(dim, ffn_dim), nn.Linear(dim, ffn_dim) + self.ffn_down = nn.Linear(ffn_dim, dim) + + def forward(self, x: torch.Tensor, rope: torch.Tensor) -> torch.Tensor: + q, k, v = self.qkv(self.norm1(x)).chunk(3, -1) + q = self.q_norm(q.unflatten(-1, (self.heads, -1))).flatten(2) + k = self.k_norm(k.unflatten(-1, (self.heads, -1))).flatten(2) + x = x + self.proj(attention(q, k, v, rope, self.heads)) + + y = self.norm2(x) + return x + self.ffn_down(situ(self.ffn_up(y), self.ffn_gate(y))) + + +class MOSSImageTextProjection(nn.Module): + def __init__(self, text_dim: int, dim: int, ffn_dim: int, heads: int): + super().__init__() + self.proj = nn.Linear(text_dim, dim) + self.blocks = nn.ModuleList([MOSSImageTextBlock(dim, ffn_dim, heads) for _ in range(2)]) + + def forward(self, x: torch.Tensor, rope: torch.Tensor) -> torch.Tensor: + x = self.proj(x) + for block in self.blocks: + x = block(x, rope) + return x + + +class MOSSImageBlock(nn.Module): + def __init__(self, dim: int, ffn_dim: int, heads: int): + super().__init__() + self.heads = heads + self.norm1, self.norm2 = RMSNorm(dim, eps=1e-6), RMSNorm(dim, eps=1e-6) + self.q_norm, self.k_norm = RMSNorm(dim // heads, eps=1e-6), RMSNorm(dim // heads, eps=1e-6) + self.proj = nn.ModuleDict({ + name: nn.Linear(dim, dim, bias=False) for name in ("q", "k", "v", "o") + }) + self.proj["ffn_up_gate"] = nn.Linear(dim, 2 * ffn_dim, bias=False) + self.proj["ffn_down"] = nn.Linear(ffn_dim, dim, bias=False) + + def forward(self, x: torch.Tensor, rope: torch.Tensor, modulation: torch.Tensor) -> torch.Tensor: + shift_a, scale_a, gate_a, shift_f, scale_f, gate_f = modulation.unbind(1) + y = self.norm1(x) * (1 + scale_a[:, None]) + shift_a[:, None] + dtype = x.dtype + y = y.to(dtype) + q = self.q_norm(self.proj["q"](y).unflatten(-1, (self.heads, -1))).flatten(2) + k = self.k_norm(self.proj["k"](y).unflatten(-1, (self.heads, -1))).flatten(2) + x = x + self.proj["o"](attention(q, k, self.proj["v"](y), rope, self.heads)) * gate_a[:, None] + + y = self.norm2(x) * (1 + scale_f[:, None]) + shift_f[:, None] + hidden, gate = self.proj["ffn_up_gate"](y.to(dtype)).chunk(2, -1) + return x + self.proj["ffn_down"](situ(hidden, gate)) * gate_f[:, None] + + +class MOSSImageRoPE(nn.Module): + def __init__(self, head_dim: int, base: float): + super().__init__() + self.num_bands = head_dim // 8 + self.base = base + self.register_buffer("bands", self._default_bands(torch.device("cpu"))) + + def _default_bands(self, device) -> torch.Tensor: + exponent = torch.arange(self.num_bands, device="cpu", dtype=torch.float32) / self.num_bands + return (1 / (self.base ** exponent)).to(device) + + def _apply(self, fn, recurse=True): + super()._apply(fn, recurse=recurse) + self.bands = self._default_bands(self.bands.device) + return self + + def _load_from_state_dict(self, *args, **kwargs): + super()._load_from_state_dict(*args, **kwargs) + self.bands = self._default_bands(self.bands.device) + + def forward(self, height: int, width: int, text_length: int, device) -> torch.Tensor: + device = torch.device(device) + bands = self.bands + if bands.dtype != torch.float32 or bands.device != device: + bands = self._default_bands(device) + + h, w = torch.meshgrid(torch.arange(height, device=device), torch.arange(width, device=device), indexing="ij") + image = torch.stack((torch.zeros_like(h), torch.full_like(h, text_length), h, w), -1).flatten(0, 1).float() + image[:, 2] -= (height - 1) / 2 + image[:, 3] -= (width - 1) / 2 + + text = torch.zeros(text_length, 4, device=device, dtype=torch.float32) + text[:, 1] = torch.arange(text_length, device=device) + + phase = torch.cat((image, text), 0).unsqueeze(-1) * bands + return torch.cat((phase.sin(), phase.cos()), -2).flatten(-2).unsqueeze(0) + + +class MOSSImageDiT(nn.Module): + def __init__( + self, + dim: int = 3072, + ffn_dim: int = 8192, + num_heads: int = 24, + num_layers: int = 42, + text_dim: int = 4096, + freq_dim: int = 256, + latent_channels: int = 128, + rope_base: float = 10000.0, + ): + super().__init__() + if dim % num_heads or (dim // num_heads) % 8 or freq_dim % 2: + raise ValueError("Require dim divisible by heads, head_dim divisible by 8, and even freq_dim.") + self.dim = dim + self.ffn_dim = ffn_dim + self.num_heads = num_heads + self.num_layers = num_layers + self.text_dim = text_dim + self.freq_dim = freq_dim + self.latent_channels = latent_channels + self.rope_base = rope_base + + self.video_embedder = nn.Linear(latent_channels, dim) + self.text_embedder = MOSSImageTextProjection(text_dim, dim, ffn_dim, num_heads) + self.rope = MOSSImageRoPE(dim // num_heads, rope_base) + self.time_embedding = nn.Sequential(nn.Linear(freq_dim, dim), nn.SiLU(), nn.Linear(dim, dim)) + self.shared_block_modulation = nn.Linear(dim, 6 * dim) + self.layers = nn.ModuleList([MOSSImageBlock(dim, ffn_dim, num_heads) for _ in range(num_layers)]) + self.final_norm_video = RMSNorm(dim, eps=1e-6) + self.final_linear_video = nn.Linear(dim, latent_channels, bias=False) + self.video_head_modulation = nn.Linear(dim, 2 * dim) + + def forward( + self, + hidden_states: torch.Tensor, + timestep, + encoder_hidden_states: torch.Tensor, + encoder_attention_mask: Optional[torch.Tensor] = None, + skip_layers=None, + ): + skip_layers = validate_skip_layers(skip_layers, len(self.layers)) + if hidden_states.ndim != 4 or hidden_states.shape[1] != self.latent_channels: + raise ValueError("hidden_states must be [B, latent_channels, H, W].") + batch, _, height, width = hidden_states.shape + if encoder_hidden_states.ndim != 3 or encoder_hidden_states.shape[0] != batch: + raise ValueError("encoder_hidden_states must be [B, S, text_dim].") + mask = encoder_attention_mask + if mask is None: + mask = torch.ones(encoder_hidden_states.shape[:2], dtype=torch.bool, device=hidden_states.device) + if mask.shape != encoder_hidden_states.shape[:2] or not torch.all((mask == 0) | (mask == 1)): + raise ValueError("encoder_attention_mask must be a binary [B, S] mask.") + + times = torch.as_tensor(timestep, device=hidden_states.device, dtype=torch.float32).reshape(-1) + times = times.expand(batch) + half = self.freq_dim // 2 + sinusoid_device = 'cpu' if times.device.type == 'mps' else times.device + freq = 10000 ** (-torch.arange(half, device=sinusoid_device, dtype=torch.float64) / half) + phase = times.to(sinusoid_device).double()[:, None] * freq + embedding = torch.cat((phase.cos(), phase.sin()), -1).float().to(times.device) + + time = self.time_embedding(embedding.to(hidden_states.dtype)).float() + mod = self.shared_block_modulation(time.to(hidden_states.dtype)).view(batch, 6, -1) + head = self.video_head_modulation(time.to(hidden_states.dtype)).view(batch, 2, -1) + + outputs = [] + for index in range(batch): + context = encoder_hidden_states[index:index + 1, mask[index].bool()] + if context.shape[1] == 0: + raise ValueError("Each sample needs at least one valid text token.") + + video = hidden_states[index:index + 1].flatten(2).transpose(1, 2) + video = self.video_embedder(video.contiguous()) + rope = self.rope(height, width, context.shape[1], hidden_states.device).to(video.dtype) + text = self.text_embedder(context, rope[:, height * width:]) + x = torch.cat((video, text), 1) + + for layer_index, layer in enumerate(self.layers): + if layer_index not in skip_layers: + x = layer(x, rope, mod[index:index + 1]) + + x = self.final_norm_video(x[:, :height * width]) + x = x * (1 + head[index:index + 1, 1, None]) + head[index:index + 1, 0, None] + x = self.final_linear_video(x) + outputs.append(x.transpose(1, 2).reshape(1, -1, height, width)) + return torch.cat(outputs) diff --git a/backend/moss_image/ops.py b/backend/moss_image/ops.py new file mode 100644 index 00000000..3714bbb5 --- /dev/null +++ b/backend/moss_image/ops.py @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: Apache-2.0 +# RMSNorm adapted from DiffSynth-Studio; see NOTICE. +import torch +from torch import nn +from torch.nn import functional as F + + +class RMSNorm(nn.Module): + def __init__(self, dim, eps): + super().__init__() + self.eps = eps + self.weight = nn.Parameter(torch.ones(dim)) + + def forward(self, x): + variance = x.float().square().mean(-1, keepdim=True) + return (x * torch.rsqrt(variance + self.eps)).to(x.dtype) * self.weight + + +def attention_forward(q, k, v, **_patterns): + # All MOSS callers use [batch, sequence, heads, head_dim]. + return F.scaled_dot_product_attention( + q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2) + ).transpose(1, 2) diff --git a/backend/moss_image/pipeline.py b/backend/moss_image/pipeline.py new file mode 100644 index 00000000..63610d14 --- /dev/null +++ b/backend/moss_image/pipeline.py @@ -0,0 +1,171 @@ +"""Local MOSS inference using the existing Torch 2.4 / Transformers 4 runtime.""" +from pathlib import Path +from threading import RLock + +import torch + +from .dit import MOSSImageDiT, validate_skip_layers +from .text_encoder import TextEncoder +from .vae import Flux2VAE +from .weights import load_component, read_config, text_weight_key + + +def sampling_sigmas(steps, shift, training_steps=1000, training_shift=1.0): + if type(steps) is not int or steps < 1: + raise ValueError("steps must be a positive integer") + if not 0 < shift < float("inf"): + raise ValueError("shift must be finite and positive") + if not 0 < training_shift < float("inf") or type(training_steps) is not int or training_steps < 2: + raise ValueError("Invalid training sigma grid") + # Official V2 schedule: shifted grid snapped to the nearest training sigma. + # This differs from a plain linspace Euler schedule, including its final step. + def grid(count, amount): + lower = amount / (training_steps + amount - 1) + values = torch.linspace(1, lower, count, dtype=torch.float32) + return amount * values / (1 + (amount - 1) * values) + sigmas = grid(steps, shift) + training = grid(training_steps, training_shift).flip(0) + upper = torch.searchsorted(training, sigmas).clamp(max=training_steps - 1) + lower = (upper - 1).clamp(min=0) + nearest = torch.where((sigmas - training[lower]).abs() <= (training[upper] - sigmas).abs(), lower, upper) + return torch.cat((torch.unique_consecutive(training[nearest]), torch.zeros(1))) + + +def format_prompt(prompt): + if not isinstance(prompt, str): + raise ValueError("MOSS prompt must be a string") + return f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n\n" + + +class MOSSPipeline: + def __init__(self, transformer, text_encoder, vae, tokenizer, device="cpu", cpu_offload=True, + training_steps=1000, training_shift=1.0): + self.transformer = transformer + self.text_encoder = text_encoder + self.vae = vae + self.tokenizer = tokenizer + self.device = torch.device(device) + self.cpu_offload = cpu_offload + self.training_steps = training_steps + self.training_shift = training_shift + # Component placement and inference share mutable module state. + self.lock = RLock() + + @classmethod + def from_pretrained(cls, model_path, device="cpu", cpu_offload=True, revision=None): + from accelerate import init_empty_weights + from transformers import PreTrainedTokenizerFast + + root = Path(model_path).expanduser() + if not root.is_dir(): + if root.is_absolute() or str(model_path).startswith((".", "~")): + raise FileNotFoundError(root) + from huggingface_hub import snapshot_download + root = Path(snapshot_download( + model_path, revision=revision, + allow_patterns=["*/config.json", "scheduler/scheduler_config.json", "tokenizer/*", + "*/*.safetensors", "*/*.safetensors.index.json"], + )) + dtype = torch.float32 if torch.device(device).type == "cpu" else torch.bfloat16 + dit_config = read_config(root / "transformer") + if dit_config.get("_class_name") not in ("MOSSImageTransformer", "MOSImageTransformer"): + raise ValueError("Expected a MOSS-Image transformer bundle") + dit_keys = ("dim", "ffn_dim", "num_heads", "num_layers", "text_dim", "freq_dim", "latent_channels", "rope_base") + te_config = read_config(root / "text_encoder") + if te_config.get("model_type") not in ("qwen3_5", "qwen3_5_text"): + raise ValueError("Expected a Qwen3.5 text encoder") + vae_config = read_config(root / "vae") + if vae_config.get("_class_name") != "AutoencoderKLFlux2": + raise ValueError("Expected an AutoencoderKLFlux2 VAE") + if vae_config.get("batch_norm_eps", 1e-4) != 1e-4 or vae_config.get("act_fn", "silu") != "silu": + raise ValueError("Unsupported FLUX.2 VAE normalization or activation") + vae_keys = ("in_channels", "out_channels", "latent_channels", "block_out_channels", "decoder_block_out_channels", + "layers_per_block", "patch_size", "use_quant_conv", "use_post_quant_conv", "mid_block_add_attention") + kwargs = {k: vae_config[k] for k in vae_keys if k in vae_config} + kwargs["num_groups"] = vae_config.get("norm_num_groups", 32) + # No Transformers model loading and no trust_remote_code: implementations + # live in backend/moss_image. Build parameters on meta before shard loads. + with init_empty_weights(): + transformer = MOSSImageDiT(**{k: dit_config[k] for k in dit_keys if k in dit_config}) + text_encoder = TextEncoder(te_config) + vae = Flux2VAE(**kwargs) + load_component(transformer, root / "transformer", "diffusion_pytorch_model", dtype) + load_component(text_encoder, root / "text_encoder", "model", dtype, text_weight_key) + load_component(vae, root / "vae", "diffusion_pytorch_model", torch.float32) + if transformer.text_dim != text_encoder.config.hidden_size or transformer.latent_channels != vae.num_latent_channels: + raise ValueError("MOSS components have incompatible text or latent dimensions") + # Read tokenizer.json directly: newer bundles may name a tokenizer class + # unknown to Transformers 4. The actual tokenizer runs in tokenizers. + tokenizer = PreTrainedTokenizerFast( + tokenizer_file=str(root / "tokenizer" / "tokenizer.json"), + eos_token="<|im_end|>", pad_token="<|im_end|>", + ) + import json + scheduler_path = root / "scheduler" / "scheduler_config.json" + scheduler = json.loads(scheduler_path.read_text(encoding="utf-8")) if scheduler_path.is_file() else {} + if scheduler.get("_class_name", "MOSSImageScheduler") != "MOSSImageScheduler": + raise ValueError("Expected a MOSSImageScheduler") + pipe = cls(transformer, text_encoder, vae, tokenizer, device, cpu_offload, + scheduler.get("num_train_timesteps", 1000), scheduler.get("training_shift", 1.0)) + if not cpu_offload: + for model in (transformer, text_encoder, vae): + model.to(device) + return pipe + + def _encode(self, prompt, max_sequence_length): + # Official MOSS Qwen3.5 chat template, including the thinking prefix. + text = format_prompt(prompt) + tokens = self.tokenizer(text, return_tensors="pt", add_special_tokens=False, + truncation=True, max_length=max_sequence_length) + ids = tokens["input_ids"].to(self.device) + mask = tokens["attention_mask"].to(self.device) + return self.text_encoder(ids, mask), mask.bool() + + @torch.inference_mode() + def __call__(self, prompt, negative_prompt="", width=1024, height=1024, + steps=50, guidance=3.0, shift=5.0, seed=42, max_sequence_length=4096, + uncond_skip_layers=(22,)): + from PIL import Image + + if any(type(n) is not int or n < 16 or n % 16 for n in (width, height)): + raise ValueError("Image dimensions must be positive multiples of 16") + if not 1 <= max_sequence_length <= 4096: + raise ValueError("max_sequence_length must be in [1, 4096]") + if not 0 <= guidance < float("inf"): + raise ValueError("guidance must be finite and nonnegative") + skip = validate_skip_layers(uncond_skip_layers, len(self.transformer.layers)) + sigmas = sampling_sigmas(steps, shift, self.training_steps, self.training_shift).to(self.device) + with self.lock: + try: + self.text_encoder.to(self.device) + positive, mask = self._encode(prompt, max_sequence_length) + negative, neg_mask = self._encode(negative_prompt, max_sequence_length) if guidance != 1 else (None, None) + if self.cpu_offload: + self.text_encoder.cpu() + self.transformer.to(self.device) + dtype = next(self.transformer.parameters()).dtype + generator = torch.Generator(device="cpu").manual_seed(int(seed)) + latents = torch.randn((1, self.transformer.latent_channels, height // 16, width // 16), + generator=generator, dtype=torch.float32).to(self.device) + for sigma, next_sigma in zip(sigmas[:-1], sigmas[1:]): + x = latents.to(dtype) + velocity = self.transformer(x, sigma * self.training_steps, positive.to(dtype), mask).float() + if negative is not None: + uncond = self.transformer(x, sigma * self.training_steps, negative.to(dtype), neg_mask, skip_layers=skip).float() + velocity = uncond + guidance * (velocity - uncond) + latents = latents + (next_sigma - sigma) * velocity + if self.cpu_offload: + self.transformer.cpu() + self.vae.to(self.device) + image = self.vae.decode(latents.float()).float() + pixels = ((image[0].permute(1, 2, 0).clamp(-1, 1) + 1) * 127.5).round().byte().cpu().numpy() + return Image.fromarray(pixels) + finally: + if self.cpu_offload: + for model in (self.text_encoder, self.transformer, self.vae): + model.cpu() + + def close(self): + with self.lock: + for model in (self.text_encoder, self.transformer, self.vae): + model.cpu() diff --git a/backend/moss_image/qwen_ops.py b/backend/moss_image/qwen_ops.py new file mode 100644 index 00000000..374d3fd0 --- /dev/null +++ b/backend/moss_image/qwen_ops.py @@ -0,0 +1,180 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2025 The Qwen Team and The HuggingFace Inc. team. All rights reserved. +# Text inference subset of Transformers v5.3.0; see NOTICE. +import torch +from torch import nn +import torch.nn.functional as F + +class Qwen3_5RMSNormGated(nn.Module): + def __init__(self, hidden_size, eps=1e-6, **kwargs): + super().__init__() + self.weight = nn.Parameter(torch.ones(hidden_size)) + self.variance_epsilon = eps + + def forward(self, hidden_states, gate=None): + input_dtype = hidden_states.dtype + hidden_states = hidden_states.to(torch.float32) + variance = hidden_states.pow(2).mean(-1, keepdim=True) + # Norm before gate + hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) + hidden_states = self.weight * hidden_states.to(input_dtype) + hidden_states = hidden_states * F.silu(gate.to(torch.float32)) + + return hidden_states.to(input_dtype) + +def l2norm(x: torch.FloatTensor, dim: int = -1, eps: float = 1e-6): + """This function is intended to align with the l2norm implementation in the FLA library.""" + inv_norm = torch.rsqrt((x * x).sum(dim=dim, keepdim=True) + eps) + return x * inv_norm + +def torch_chunk_gated_delta_rule( + query, + key, + value, + g, + beta, + chunk_size=64, + initial_state=None, + output_final_state=False, + use_qk_l2norm_in_kernel=False, +): + initial_dtype = query.dtype + if use_qk_l2norm_in_kernel: + query = l2norm(query, dim=-1, eps=1e-6) + key = l2norm(key, dim=-1, eps=1e-6) + query, key, value, beta, g = [ + x.transpose(1, 2).contiguous().to(torch.float32) for x in (query, key, value, beta, g) + ] + + batch_size, num_heads, sequence_length, k_head_dim = key.shape + v_head_dim = value.shape[-1] + pad_size = (chunk_size - sequence_length % chunk_size) % chunk_size + query = F.pad(query, (0, 0, 0, pad_size)) + key = F.pad(key, (0, 0, 0, pad_size)) + value = F.pad(value, (0, 0, 0, pad_size)) + beta = F.pad(beta, (0, pad_size)) + g = F.pad(g, (0, pad_size)) + total_sequence_length = sequence_length + pad_size + scale = 1 / (query.shape[-1] ** 0.5) + query = query * scale + + v_beta = value * beta.unsqueeze(-1) + k_beta = key * beta.unsqueeze(-1) + # reshape to chunks + query, key, value, k_beta, v_beta = [ + x.reshape(x.shape[0], x.shape[1], -1, chunk_size, x.shape[-1]) for x in (query, key, value, k_beta, v_beta) + ] + g = g.reshape(g.shape[0], g.shape[1], -1, chunk_size) + mask = torch.triu(torch.ones(chunk_size, chunk_size, dtype=torch.bool, device=query.device), diagonal=0) + + # chunk decay + g = g.cumsum(dim=-1) + decay_mask = ((g.unsqueeze(-1) - g.unsqueeze(-2)).tril().exp().float()).tril() + attn = -((k_beta @ key.transpose(-1, -2)) * decay_mask).masked_fill(mask, 0) + for i in range(1, chunk_size): + row = attn[..., i, :i].clone() + sub = attn[..., :i, :i].clone() + attn[..., i, :i] = row + (row.unsqueeze(-1) * sub).sum(-2) + attn = attn + torch.eye(chunk_size, dtype=attn.dtype, device=attn.device) + value = attn @ v_beta + k_cumdecay = attn @ (k_beta * g.exp().unsqueeze(-1)) + last_recurrent_state = ( + torch.zeros(batch_size, num_heads, k_head_dim, v_head_dim).to(value) + if initial_state is None + else initial_state.to(value) + ) + core_attn_out = torch.zeros_like(value) + mask = torch.triu(torch.ones(chunk_size, chunk_size, dtype=torch.bool, device=query.device), diagonal=1) + + # for each chunk + for i in range(0, total_sequence_length // chunk_size): + q_i, k_i, v_i = query[:, :, i], key[:, :, i], value[:, :, i] + attn = (q_i @ k_i.transpose(-1, -2) * decay_mask[:, :, i]).masked_fill_(mask, 0) + v_prime = (k_cumdecay[:, :, i]) @ last_recurrent_state + v_new = v_i - v_prime + attn_inter = (q_i * g[:, :, i, :, None].exp()) @ last_recurrent_state + core_attn_out[:, :, i] = attn_inter + attn @ v_new + last_recurrent_state = ( + last_recurrent_state * g[:, :, i, -1, None, None].exp() + + (k_i * (g[:, :, i, -1, None] - g[:, :, i]).exp()[..., None]).transpose(-1, -2) @ v_new + ) + + if not output_final_state: + last_recurrent_state = None + core_attn_out = core_attn_out.reshape(core_attn_out.shape[0], core_attn_out.shape[1], -1, core_attn_out.shape[-1]) + core_attn_out = core_attn_out[:, :, :sequence_length] + core_attn_out = core_attn_out.transpose(1, 2).contiguous().to(initial_dtype) + return core_attn_out, last_recurrent_state + +def rotate_half(x): + """Rotates half the hidden dims of the input.""" + x1 = x[..., : x.shape[-1] // 2] + x2 = x[..., x.shape[-1] // 2 :] + return torch.cat((-x2, x1), dim=-1) + +def apply_rotary_pos_emb(q, k, cos, sin, unsqueeze_dim=1): + """Applies Rotary Position Embedding to the query and key tensors. + + Removes the interleaving of cos and sin from GLM + + Args: + q (`torch.Tensor`): The query tensor. + k (`torch.Tensor`): The key tensor. + cos (`torch.Tensor`): The cosine part of the rotary embedding. + sin (`torch.Tensor`): The sine part of the rotary embedding. + unsqueeze_dim (`int`, *optional*, defaults to 1): + The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and + sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note + that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and + k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes + cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have + the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. + Returns: + `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. + """ + cos = cos.unsqueeze(unsqueeze_dim) + sin = sin.unsqueeze(unsqueeze_dim) + + # Keep half or full tensor for later concatenation + rotary_dim = cos.shape[-1] + q_rot, q_pass = q[..., :rotary_dim], q[..., rotary_dim:] + k_rot, k_pass = k[..., :rotary_dim], k[..., rotary_dim:] + + # Apply rotary embeddings on the first half or full tensor + q_embed = (q_rot * cos) + (rotate_half(q_rot) * sin) + k_embed = (k_rot * cos) + (rotate_half(k_rot) * sin) + + # Concatenate back to full shape + q_embed = torch.cat([q_embed, q_pass], dim=-1) + k_embed = torch.cat([k_embed, k_pass], dim=-1) + return q_embed, k_embed + +def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: + """ + This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, + num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) + """ + batch, num_key_value_heads, slen, head_dim = hidden_states.shape + if n_rep == 1: + return hidden_states + hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) + return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) + +class Qwen3_5RMSNorm(nn.Module): + def __init__(self, dim: int, eps: float = 1e-6): + super().__init__() + self.eps = eps + self.weight = nn.Parameter(torch.zeros(dim)) + + def _norm(self, x): + return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) + + def forward(self, x): + output = self._norm(x.float()) + # Llama does x.to(float16) * w whilst Qwen3_5 is (x * w).to(float16) + # See https://github.com/huggingface/transformers/pull/29402 + output = output * (1.0 + self.weight.float()) + return output.type_as(x) + + def extra_repr(self): + return f"{tuple(self.weight.shape)}, eps={self.eps}" diff --git a/backend/moss_image/text_encoder.py b/backend/moss_image/text_encoder.py new file mode 100644 index 00000000..45988245 --- /dev/null +++ b/backend/moss_image/text_encoder.py @@ -0,0 +1,186 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2025 The Qwen Team and The HuggingFace Inc. team. All rights reserved. +# Adapted text-only, cache-free Qwen3.5 inference. See NOTICE for sources. +from types import SimpleNamespace + +import torch +from torch import nn +from torch.nn import functional as F + +from .qwen_ops import ( + Qwen3_5RMSNorm, Qwen3_5RMSNormGated, apply_rotary_pos_emb, + repeat_kv, torch_chunk_gated_delta_rule, +) + + +def text_config(values=None): + values = values or {} + values = values.get("text_config", values) + defaults = dict( + hidden_size=4096, intermediate_size=12288, num_hidden_layers=32, + num_attention_heads=16, num_key_value_heads=4, head_dim=256, + vocab_size=248320, rms_norm_eps=1e-6, attention_bias=False, + linear_num_key_heads=16, linear_num_value_heads=32, + linear_key_head_dim=128, linear_value_head_dim=128, + linear_conv_kernel_dim=4, hidden_act="silu", full_attention_interval=4, + ) + defaults.update(values) + if defaults["hidden_act"] != "silu": + raise ValueError("MOSS Qwen3.5 supports only silu activation") + if not defaults.get("attn_output_gate", True): + raise ValueError("MOSS requires gated Qwen3.5 full attention") + defaults.setdefault("layer_types", [ + "full_attention" if (i + 1) % defaults["full_attention_interval"] == 0 + else "linear_attention" for i in range(defaults["num_hidden_layers"]) + ]) + if len(defaults["layer_types"]) != defaults["num_hidden_layers"]: + raise ValueError("Qwen3.5 layer_types does not match num_hidden_layers") + rope = defaults.setdefault("rope_parameters", {}) + if rope.get("rope_type", "default") != "default": + raise ValueError("MOSS supports only default Qwen3.5 RoPE") + defaults["rope_theta"] = rope.get("rope_theta", defaults.get("rope_theta", 10000000.0)) + defaults["partial_rotary_factor"] = rope.get("partial_rotary_factor", defaults.get("partial_rotary_factor", 0.25)) + return SimpleNamespace(**defaults) + + +class LinearAttention(nn.Module): + def __init__(self, c): + super().__init__() + self.num_k_heads = c.linear_num_key_heads + self.num_v_heads = c.linear_num_value_heads + self.head_k_dim = c.linear_key_head_dim + self.head_v_dim = c.linear_value_head_dim + self.key_dim = self.num_k_heads * self.head_k_dim + self.value_dim = self.num_v_heads * self.head_v_dim + if self.num_v_heads % self.num_k_heads: + raise ValueError("Linear value heads must be divisible by key heads") + dim = self.key_dim * 2 + self.value_dim + self.conv1d = nn.Conv1d(dim, dim, c.linear_conv_kernel_dim, + groups=dim, padding=c.linear_conv_kernel_dim - 1, bias=False) + self.dt_bias = nn.Parameter(torch.ones(self.num_v_heads)) + self.A_log = nn.Parameter(torch.zeros(self.num_v_heads)) + self.norm = Qwen3_5RMSNormGated(self.head_v_dim, eps=c.rms_norm_eps) + self.out_proj = nn.Linear(self.value_dim, c.hidden_size, bias=False) + self.in_proj_qkv = nn.Linear(c.hidden_size, dim, bias=False) + self.in_proj_z = nn.Linear(c.hidden_size, self.value_dim, bias=False) + self.in_proj_b = nn.Linear(c.hidden_size, self.num_v_heads, bias=False) + self.in_proj_a = nn.Linear(c.hidden_size, self.num_v_heads, bias=False) + + def forward(self, x, mask): + # Inputs are unpadded individually by TextEncoder, matching training lengths. + if mask is not None: + x = (x * mask[:, :, None]).to(x.dtype) + b, s, _ = x.shape + qkv = F.silu(self.conv1d(self.in_proj_qkv(x).transpose(1, 2))[:, :, :s]).transpose(1, 2) + q, k, v = qkv.split((self.key_dim, self.key_dim, self.value_dim), dim=-1) + q = q.reshape(b, s, self.num_k_heads, self.head_k_dim) + k = k.reshape_as(q) + v = v.reshape(b, s, self.num_v_heads, self.head_v_dim) + repeats = self.num_v_heads // self.num_k_heads + q, k = q.repeat_interleave(repeats, 2), k.repeat_interleave(repeats, 2) + beta = self.in_proj_b(x).sigmoid() + g = -self.A_log.float().exp() * F.softplus(self.in_proj_a(x).float() + self.dt_bias) + out, _ = torch_chunk_gated_delta_rule(q, k, v, g, beta, use_qk_l2norm_in_kernel=True) + gate = self.in_proj_z(x).reshape(-1, self.head_v_dim) + out = self.norm(out.reshape(-1, self.head_v_dim), gate).reshape(b, s, self.value_dim) + return self.out_proj(out) + + +class FullAttention(nn.Module): + def __init__(self, c): + super().__init__() + self.head_dim = c.head_dim + self.num_heads = c.num_attention_heads + self.num_kv_heads = c.num_key_value_heads + if self.num_heads % self.num_kv_heads: + raise ValueError("Attention heads must be divisible by key/value heads") + self.q_proj = nn.Linear(c.hidden_size, self.num_heads * self.head_dim * 2, bias=c.attention_bias) + self.k_proj = nn.Linear(c.hidden_size, self.num_kv_heads * self.head_dim, bias=c.attention_bias) + self.v_proj = nn.Linear(c.hidden_size, self.num_kv_heads * self.head_dim, bias=c.attention_bias) + self.o_proj = nn.Linear(self.num_heads * self.head_dim, c.hidden_size, bias=c.attention_bias) + self.q_norm = Qwen3_5RMSNorm(self.head_dim, eps=c.rms_norm_eps) + self.k_norm = Qwen3_5RMSNorm(self.head_dim, eps=c.rms_norm_eps) + + def forward(self, x, rope): + b, s, _ = x.shape + q, gate = self.q_proj(x).view(b, s, self.num_heads, self.head_dim * 2).chunk(2, -1) + q = self.q_norm(q).transpose(1, 2) + k = self.k_norm(self.k_proj(x).view(b, s, self.num_kv_heads, self.head_dim)).transpose(1, 2) + v = self.v_proj(x).view(b, s, self.num_kv_heads, self.head_dim).transpose(1, 2) + q, k = apply_rotary_pos_emb(q, k, *rope) + k, v = repeat_kv(k, self.num_heads // self.num_kv_heads), repeat_kv(v, self.num_heads // self.num_kv_heads) + out = F.scaled_dot_product_attention(q, k, v, is_causal=True).transpose(1, 2).reshape(b, s, -1) + return self.o_proj(out * gate.reshape(b, s, -1).sigmoid()) + + +class MLP(nn.Module): + def __init__(self, c): + super().__init__() + self.gate_proj = nn.Linear(c.hidden_size, c.intermediate_size, bias=False) + self.up_proj = nn.Linear(c.hidden_size, c.intermediate_size, bias=False) + self.down_proj = nn.Linear(c.intermediate_size, c.hidden_size, bias=False) + + def forward(self, x): + return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x)) + + +class TextLayer(nn.Module): + def __init__(self, c, index): + super().__init__() + self.layer_type = c.layer_types[index] + if self.layer_type == "linear_attention": + self.linear_attn = LinearAttention(c) + elif self.layer_type == "full_attention": + self.self_attn = FullAttention(c) + else: + raise ValueError(f"Unsupported Qwen3.5 layer type: {self.layer_type}") + self.input_layernorm = Qwen3_5RMSNorm(c.hidden_size, eps=c.rms_norm_eps) + self.post_attention_layernorm = Qwen3_5RMSNorm(c.hidden_size, eps=c.rms_norm_eps) + self.mlp = MLP(c) + + def forward(self, x, rope): + h = self.input_layernorm(x) + x = x + (self.linear_attn(h, None) if self.layer_type == "linear_attention" else self.self_attn(h, rope)) + return x + self.mlp(self.post_attention_layernorm(x)) + + +class TextEncoder(nn.Module): + """Qwen3.5 text backbone, retaining Hugging Face language_model weight keys.""" + def __init__(self, config=None): + super().__init__() + self.config = c = text_config(config) + self.embed_tokens = nn.Embedding(c.vocab_size, c.hidden_size) + self.layers = nn.ModuleList([TextLayer(c, i) for i in range(c.num_hidden_layers)]) + self.norm = Qwen3_5RMSNorm(c.hidden_size, eps=c.rms_norm_eps) + + def _forward_unpadded(self, ids): + x = self.embed_tokens(ids) + c = self.config + dim = int(c.head_dim * c.partial_rotary_factor) + # Text-only positions coincide along all three MRoPE axes. Recompute in + # FP32 after device/dtype changes instead of downcasting a RoPE buffer. + with torch.autocast(device_type=x.device.type, enabled=False): + inv = 1.0 / (c.rope_theta ** (torch.arange(0, dim, 2, device=x.device).float() / dim)) + phase = torch.arange(ids.shape[1], device=x.device).float()[:, None] * inv + emb = torch.cat((phase, phase), -1)[None] + rope = emb.cos().to(x.dtype), emb.sin().to(x.dtype) + for layer in self.layers: + x = layer(x, rope) + return self.norm(x) + + def forward(self, input_ids, attention_mask=None): + if input_ids.ndim != 2 or input_ids.shape[1] == 0: + raise ValueError("Expected nonempty [batch, sequence] token IDs") + if attention_mask is None: + return self._forward_unpadded(input_ids) + if attention_mask.shape != input_ids.shape or not torch.all((attention_mask == 0) | (attention_mask == 1)): + raise ValueError("Expected binary attention mask matching token IDs") + outputs = [] + for ids, mask in zip(input_ids, attention_mask.bool()): + if not mask.any(): + raise ValueError("Each prompt needs at least one valid token") + encoded = self._forward_unpadded(ids[mask][None])[0] + out = encoded.new_zeros((ids.shape[0], encoded.shape[-1])) + out[mask] = encoded + outputs.append(out) + return torch.stack(outputs) diff --git a/backend/moss_image/vae.py b/backend/moss_image/vae.py new file mode 100644 index 00000000..ccb0b07d --- /dev/null +++ b/backend/moss_image/vae.py @@ -0,0 +1,337 @@ +# SPDX-License-Identifier: Apache-2.0 +# Adapted from DiffSynth-Studio; see NOTICE for pinned sources and changes. +import torch +import torch.nn.functional as F +from einops import rearrange + + +class Flux2VAEResnetBlock(torch.nn.Module): + + def __init__(self, in_channels, out_channels=None, num_groups=32, eps=1e-6): + super().__init__() + out_channels = out_channels or in_channels + self.norm1 = torch.nn.GroupNorm(num_groups, in_channels, eps=eps) + self.conv1 = torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1) + self.norm2 = torch.nn.GroupNorm(num_groups, out_channels, eps=eps) + self.conv2 = torch.nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1) + self.nonlinearity = torch.nn.SiLU() + self.conv_shortcut = None + if in_channels != out_channels: + self.conv_shortcut = torch.nn.Conv2d(in_channels, out_channels, kernel_size=1) + + def forward(self, hidden_states): + residual = hidden_states + hidden_states = self.conv1(self.nonlinearity(self.norm1(hidden_states))) + hidden_states = self.conv2(self.nonlinearity(self.norm2(hidden_states))) + if self.conv_shortcut is not None: + residual = self.conv_shortcut(residual) + return residual + hidden_states + + +class Flux2VAEAttention(torch.nn.Module): + + def __init__(self, channels, num_groups=32, eps=1e-6): + super().__init__() + self.group_norm = torch.nn.GroupNorm(num_groups, channels, eps=eps) + self.to_q = torch.nn.Linear(channels, channels) + self.to_k = torch.nn.Linear(channels, channels) + self.to_v = torch.nn.Linear(channels, channels) + self.to_out = torch.nn.Sequential(torch.nn.Linear(channels, channels), torch.nn.Dropout(0.0)) + + def forward(self, hidden_states): + batch_size, channels, height, width = hidden_states.shape + residual = hidden_states + + # (B, C, H, W) -> (B, H * W, C) + hidden_states = self.group_norm(hidden_states).view(batch_size, channels, height * width).transpose(1, 2) + query = self.to_q(hidden_states).unsqueeze(1) + key = self.to_k(hidden_states).unsqueeze(1) + value = self.to_v(hidden_states).unsqueeze(1) + hidden_states = F.scaled_dot_product_attention(query, key, value) + hidden_states = self.to_out(hidden_states.squeeze(1)) + + # (B, H * W, C) -> (B, C, H, W) + hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, channels, height, width) + return residual + hidden_states + + +class Flux2VAEUpsampler(torch.nn.Module): + + def __init__(self, channels): + super().__init__() + self.conv = torch.nn.Conv2d(channels, channels, kernel_size=3, padding=1) + + def forward(self, hidden_states): + hidden_states = F.interpolate(hidden_states, scale_factor=2.0, mode="nearest") + return self.conv(hidden_states) + + +class Flux2VAEDownsampler(torch.nn.Module): + + def __init__(self, channels): + super().__init__() + self.conv = torch.nn.Conv2d(channels, channels, kernel_size=3, stride=2) + + def forward(self, hidden_states): + hidden_states = F.pad(hidden_states, (0, 1, 0, 1), mode="constant", value=0) + return self.conv(hidden_states) + + +class Flux2VAEMidBlock(torch.nn.Module): + + def __init__(self, channels, num_layers=1, num_groups=32, eps=1e-6, add_attention=True): + super().__init__() + self.resnets = torch.nn.ModuleList([ + Flux2VAEResnetBlock(channels, num_groups=num_groups, eps=eps) for _ in range(num_layers + 1) + ]) + self.attentions = torch.nn.ModuleList([ + Flux2VAEAttention(channels, num_groups=num_groups, eps=eps) if add_attention else None + for _ in range(num_layers) + ]) + + def forward(self, hidden_states): + hidden_states = self.resnets[0](hidden_states) + for attention, resnet in zip(self.attentions, self.resnets[1:]): + if attention is not None: + hidden_states = attention(hidden_states) + hidden_states = resnet(hidden_states) + return hidden_states + + +class Flux2VAEDownBlock(torch.nn.Module): + + def __init__(self, in_channels, out_channels, num_layers, num_groups=32, eps=1e-6, add_downsample=True): + super().__init__() + self.resnets = torch.nn.ModuleList([ + Flux2VAEResnetBlock( + in_channels if i == 0 else out_channels, + out_channels, + num_groups=num_groups, + eps=eps, + ) + for i in range(num_layers) + ]) + self.downsamplers = None + if add_downsample: + self.downsamplers = torch.nn.ModuleList([Flux2VAEDownsampler(out_channels)]) + + def forward(self, hidden_states): + for resnet in self.resnets: + hidden_states = resnet(hidden_states) + if self.downsamplers is not None: + for downsampler in self.downsamplers: + hidden_states = downsampler(hidden_states) + return hidden_states + + +class Flux2VAEUpBlock(torch.nn.Module): + + def __init__(self, in_channels, out_channels, num_layers, num_groups=32, eps=1e-6, add_upsample=True): + super().__init__() + self.resnets = torch.nn.ModuleList([ + Flux2VAEResnetBlock( + in_channels if i == 0 else out_channels, + out_channels, + num_groups=num_groups, + eps=eps, + ) + for i in range(num_layers) + ]) + self.upsamplers = None + if add_upsample: + self.upsamplers = torch.nn.ModuleList([Flux2VAEUpsampler(out_channels)]) + + def forward(self, hidden_states): + for resnet in self.resnets: + hidden_states = resnet(hidden_states) + if self.upsamplers is not None: + for upsampler in self.upsamplers: + hidden_states = upsampler(hidden_states) + return hidden_states + + +class Flux2VAEEncoder(torch.nn.Module): + + def __init__( + self, + in_channels=3, + latent_channels=32, + block_out_channels=(128, 256, 512, 512), + layers_per_block=2, + num_groups=32, + add_attention=True, + ): + super().__init__() + self.conv_in = torch.nn.Conv2d(in_channels, block_out_channels[0], kernel_size=3, padding=1) + self.down_blocks = torch.nn.ModuleList([]) + for i, out_channels in enumerate(block_out_channels): + in_channels_ = block_out_channels[0] if i == 0 else block_out_channels[i - 1] + self.down_blocks.append(Flux2VAEDownBlock( + in_channels_, + out_channels, + num_layers=layers_per_block, + num_groups=num_groups, + add_downsample=i < len(block_out_channels) - 1, + )) + self.mid_block = Flux2VAEMidBlock( + block_out_channels[-1], + num_groups=num_groups, + add_attention=add_attention, + ) + self.conv_norm_out = torch.nn.GroupNorm(num_groups, block_out_channels[-1], eps=1e-6) + self.conv_act = torch.nn.SiLU() + self.conv_out = torch.nn.Conv2d(block_out_channels[-1], 2 * latent_channels, kernel_size=3, padding=1) + + def forward(self, hidden_states): + hidden_states = self.conv_in(hidden_states) + for down_block in self.down_blocks: + hidden_states = down_block(hidden_states) + hidden_states = self.mid_block(hidden_states) + hidden_states = self.conv_norm_out(hidden_states) + hidden_states = self.conv_act(hidden_states) + return self.conv_out(hidden_states) + + +class Flux2VAEDecoder(torch.nn.Module): + + def __init__( + self, + out_channels=3, + latent_channels=32, + block_out_channels=(128, 256, 512, 512), + layers_per_block=2, + num_groups=32, + add_attention=True, + ): + super().__init__() + block_out_channels = list(reversed(block_out_channels)) + self.conv_in = torch.nn.Conv2d(latent_channels, block_out_channels[0], kernel_size=3, padding=1) + self.mid_block = Flux2VAEMidBlock( + block_out_channels[0], + num_groups=num_groups, + add_attention=add_attention, + ) + self.up_blocks = torch.nn.ModuleList([]) + for i, out_channels_ in enumerate(block_out_channels): + in_channels_ = block_out_channels[0] if i == 0 else block_out_channels[i - 1] + self.up_blocks.append(Flux2VAEUpBlock( + in_channels_, + out_channels_, + num_layers=layers_per_block + 1, + num_groups=num_groups, + add_upsample=i < len(block_out_channels) - 1, + )) + self.conv_norm_out = torch.nn.GroupNorm(num_groups, block_out_channels[-1], eps=1e-6) + self.conv_act = torch.nn.SiLU() + self.conv_out = torch.nn.Conv2d(block_out_channels[-1], out_channels, kernel_size=3, padding=1) + + def forward(self, hidden_states): + hidden_states = self.conv_in(hidden_states) + hidden_states = self.mid_block(hidden_states) + for up_block in self.up_blocks: + hidden_states = up_block(hidden_states) + hidden_states = self.conv_norm_out(hidden_states) + hidden_states = self.conv_act(hidden_states) + return self.conv_out(hidden_states) + + +class Flux2VAELatentNorm(torch.nn.Module): + + def __init__(self, num_features): + super().__init__() + self.running_mean = torch.nn.Parameter(torch.zeros(num_features), requires_grad=False) + self.running_var = torch.nn.Parameter(torch.ones(num_features), requires_grad=False) + self.num_batches_tracked = torch.nn.Parameter(torch.zeros((), dtype=torch.long), requires_grad=False) + + def forward(self): + return self.running_mean, self.running_var + + +class Flux2VAE(torch.nn.Module): + """ + The VAE used by FLUX.2 and related models. Image -> latent (`encode`) and latent -> image (`decode`). + Latents are normalized with statistics stored in the checkpoint (`bn.running_mean` / `bn.running_var`). + """ + + def __init__( + self, + in_channels=3, + out_channels=3, + latent_channels=32, + block_out_channels=(128, 256, 512, 512), + decoder_block_out_channels=None, + layers_per_block=2, + num_groups=32, + patch_size=(2, 2), + use_quant_conv=True, + use_post_quant_conv=True, + mid_block_add_attention=True, + ): + super().__init__() + self.patch_size = patch_size + self.num_latent_channels = patch_size[0] * patch_size[1] * latent_channels + self.encoder = Flux2VAEEncoder( + in_channels=in_channels, + latent_channels=latent_channels, + block_out_channels=block_out_channels, + layers_per_block=layers_per_block, + num_groups=num_groups, + add_attention=mid_block_add_attention, + ) + self.decoder = Flux2VAEDecoder( + out_channels=out_channels, + latent_channels=latent_channels, + block_out_channels=decoder_block_out_channels or block_out_channels, + layers_per_block=layers_per_block, + num_groups=num_groups, + add_attention=mid_block_add_attention, + ) + self.quant_conv = torch.nn.Conv2d(2 * latent_channels, 2 * latent_channels, 1) if use_quant_conv else None + self.post_quant_conv = torch.nn.Conv2d(latent_channels, latent_channels, 1) if use_post_quant_conv else None + self.bn = Flux2VAELatentNorm(self.num_latent_channels) + + @property + def vae_dtype(self): + for module in self.modules(): + if hasattr(module, "computation_dtype"): + return module.computation_dtype + return next(self.parameters()).dtype + + def latent_stats(self, device, dtype): + # Call the module instead of reading its parameters, so that VRAM management loads the statistics on demand. + mean, var = self.bn() + mean = mean.to(device=device, dtype=dtype).view(1, -1, 1, 1) + std = torch.sqrt(var.to(device=device, dtype=dtype) + 1e-4).view(1, -1, 1, 1) + return mean, std + + def _encode(self, image): + # Returns the predicted mean and variance, both are not normalized or packed. + hidden_states = self.encoder(image.to(dtype=self.vae_dtype)) + if self.quant_conv is not None: + hidden_states = self.quant_conv(hidden_states) + return hidden_states + + def _decode(self, latents): + latents = latents.to(dtype=self.vae_dtype) + if self.post_quant_conv is not None: + latents = self.post_quant_conv(latents) + return self.decoder(latents) + + def encode(self, image): + hidden_states = self._encode(image) + hidden_states = rearrange( + hidden_states, "B C (H P) (W Q) -> B (C P Q) H W", P=self.patch_size[0], Q=self.patch_size[1] + ) + # The encoder predicts mean and variance, only the mean is used as the latent. + hidden_states = hidden_states[:, :self.num_latent_channels] + mean, std = self.latent_stats(hidden_states.device, hidden_states.dtype) + return (hidden_states - mean) / std + + def decode(self, latents): + latents = latents.to(dtype=self.vae_dtype) + mean, std = self.latent_stats(latents.device, latents.dtype) + latents = latents * std + mean + latents = rearrange( + latents, "B (C P Q) H W -> B C (H P) (W Q)", P=self.patch_size[0], Q=self.patch_size[1] + ) + return self._decode(latents) diff --git a/backend/moss_image/weights.py b/backend/moss_image/weights.py new file mode 100644 index 00000000..a4a25117 --- /dev/null +++ b/backend/moss_image/weights.py @@ -0,0 +1,69 @@ +"""Read local safetensors bundles without executing downloaded Python code.""" +import json +from pathlib import Path + + +def read_config(directory): + return json.loads((Path(directory) / "config.json").read_text(encoding="utf-8")) + + +def weight_shards(directory, stem): + directory = Path(directory).resolve() + index = directory / f"{stem}.safetensors.index.json" + if index.is_file(): + mapping = json.loads(index.read_text(encoding="utf-8"))["weight_map"] + if not mapping or not all(isinstance(k, str) and isinstance(v, str) for k, v in mapping.items()): + raise ValueError("Invalid safetensors weight map") + names = sorted(set(mapping.values())) + else: + mapping = None + names = [f"{stem}.safetensors"] + shards = [] + for name in names: + path = (directory / name).resolve() + if not path.is_relative_to(directory) or path.suffix != ".safetensors": + raise ValueError(f"Shard path escapes component directory or is not safetensors: {name}") + if not path.is_file(): + raise FileNotFoundError(path) + expected = {k for k, v in mapping.items() if v == name} if mapping is not None else None + shards.append((path, expected)) + return shards + + +def text_weight_key(key): + # Official bundles contain a multimodal Qwen3.5 model. Image generation uses + # only the language backbone, never visual/LM-head/MTP weights. + for prefix in ("model.language_model.", "language_model."): + if key.startswith(prefix): + return key[len(prefix):] + if key.startswith(("visual.", "model.visual.", "lm_head.", "mtp.", "model.mtp.")): + return None + return key + + +def load_component(model, directory, stem, dtype, key_transform=None): + import torch + from safetensors.torch import load_file + + expected = set(model.state_dict()) + loaded = set() + for path, shard_keys in weight_shards(directory, stem): + state = load_file(str(path), device="cpu") + if shard_keys is not None and set(state) != shard_keys: + raise ValueError(f"Shard contents do not match index: {path.name}") + selected = {} + for key, tensor in state.items(): + target = key_transform(key) if key_transform else key + if target is None: + continue + if target not in expected or target in loaded: + raise ValueError(f"Unexpected or duplicate weight: {key}") + selected[target] = tensor.to(dtype) if tensor.is_floating_point() else tensor + loaded.add(target) + model.load_state_dict(selected, strict=False, assign=True) + del selected, state + if missing := expected - loaded: + raise ValueError(f"Missing weights in {directory}: {sorted(missing)[:10]}") + if any(p.device.type == "meta" for p in model.parameters()): + raise ValueError("Component still contains uninitialized weights") + return model.eval().requires_grad_(False) diff --git a/doc/MOSSImage.md b/doc/MOSSImage.md new file mode 100644 index 00000000..10b8eeea --- /dev/null +++ b/doc/MOSSImage.md @@ -0,0 +1,97 @@ +# MOSS-Image(共享运行环境) + +MOSS-Image 的推理实现在主仓库 `backend/moss_image/`,Image 扩展只提供节点。 +无需新建虚拟环境,也无需升级到 Transformers 5 或 Torch 2.9。 +此次移植没有修改共享依赖约束或平台锁文件。 + +## 使用 + +打开 `examples/basic/workflow-moss-image.py`,模型输入使用 +`ssui_image.MOSSImage.MOSSImageModel.load`: + +- `model_path`:完整的本地 Hugging Face Base/SFT 模型目录,或 + `OpenMOSS-Team/MOSS-Image-Base` / `OpenMOSS-Team/MOSS-Image-SFT`。 +- `cpu_offload=True`:在文本编码、去噪、解码阶段之间将组件移回 CPU。 +- `revision`:Hub 分支、标签或 commit;生产使用建议固定 commit。 + 本次核对 Base 的版本为 `85975793d8b3d432f1b6e27f352abaa4c2a4c0d3`。 + +节点默认 1024×1024、50 步、CFG 3、shift 5,负分支跳过第 22 层。 +负提示词可以为空。正提示词可输入自然语言或官方布局 JSON,布局坐标必须 +与生成尺寸一致。尺寸必须是 16 的倍数。 + +也可直接调用后端: + +```python +from backend.moss_image.pipeline import MOSSPipeline + +pipe = MOSSPipeline.from_pretrained( + "/path/to/MOSS-Image-SFT", device="cuda", cpu_offload=True, +) +image = pipe("一张写着「你好世界」的海报", negative_prompt="", seed=42) +image.save("moss.png") +pipe.close() +``` + +本地目录需包含 `transformer/`、`text_encoder/`、`vae/` 的配置和 +safetensors 权重,以及 `tokenizer/tokenizer.json`。支持 safetensors 分片索引。 +Hub 加载只下载配置、tokenizer 和 safetensors,不执行模型仓库里的 Python。 +配置内的 `auto_map` 等远程代码入口不会被使用。 + +## 运行范围与内存 + +- CUDA/ROCm 使用 BF16 文本编码器和 DiT;VAE 固定 FP32。 +- 无 CUDA 时使用 CPU FP32,速度慢,完整模型需要较大系统内存。 + 当前节点尚未启用 MPS,也未验证真实 ROCm 硬件。 +- 组件卸载仍会把完整 DiT 放到 GPU,所有组件权重常驻系统内存。 + **本实现不是官方 minimal/hybrid 模式**,不能沿用官方约 4GB 显存的数据。 + 文本编码器的 BF16 权重约 17GB、DiT 约 10GB,推理还需要激活内存。 +- 暂未接入 LoRA、FP8、逐层/磁盘卸载、图生图或额外的 SLG 控制。 + 布局输入是文本条件生成,不等同于像素级编辑。 +- 模型节点缓存加载结果;切换多个模型前可调用 + `MOSSImageModel.clear_cache()` 释放缓存引用和 GPU 权重。 + +## 验证 + +本次兼容验证环境为 Torch **2.4.1+cpu**、Transformers **4.55.4**、 +Diffusers **0.38.0**、Accelerate **1.10.1**。后端推理不依赖 Diffusers Pipeline, +Diffusers 仅用于 VAE 对齐测试。 + +```bash +yarn test_on moss_image_test +yarn test_on moss_image_nodes_test +yarn test_on moss_image_e2e_test +yarn test_on flow_operator_discovery_test +yarn test_on diffusers_compat_test +``` + +测试覆盖 Qwen3.5 上游 FP32 数值基线(含跨 64-token 分块)、独立循环式 +Gated DeltaNet 方程、训练网格时间表、文本掩码、负分支跳层、FLUX.2 VAE +解码对齐、分片校验,以及小尺寸本地模型目录加载到图片输出的完整链路。 +基线来源和修改点记录于 `backend/moss_image/NOTICE`;许可证见同目录 `LICENSE`。 + +`moss_image_e2e_test` 使用正常的 Image 扩展导入路径,通过 HTTP 接口、真实 +本地 WebSocket 调度器和沙盒执行示例工作流。它在三个组件各含两个 +safetensors 分片的模拟模型上,覆盖准备阶段无权重运行、文本与布局输入、 +第 22 层负分支跳层、种子复现、改变种子、PNG 落盘和失败后继续执行。 +该测试需要允许本地 loopback socket,不需要访问互联网。 +设置 `SSUI_MOSS_TEST_OUTPUT=/path/to/output` 可以保留测试 PNG。 + +本次补齐了测试环境缺失的 ONNX Runtime、ONNX、Spandrel、GGUF、Compel、 +TorchSDE 等依赖;`pip check` 通过,Torch/Transformers 主版本未变。 +同步当前 dev 后,MOSS、节点发现、Diffusers 兼容、FLUX.2 和工作流编译 +相关验证共 26 项通过(含模拟权重端到端测试)。额外执行器回归有两项 +受测试环境缺少 imageio 和本地 socket 权限限制,未记为通过。完整模型 +权重测试未运行。 + +端到端测试覆盖了工作流字符串注解解析、IPv4 调度器连接和 PNG 文件名 +唯一性。本改动补齐字符串注解解析;IPv4 和结果序列化沿用当前 dev 实现。 + +真模型测试需已有模型目录和 CUDA GPU: + +```bash +SSUI_RUN_MODEL_TESTS=1 SSUI_MOSS_IMAGE_MODEL=/path/to/MOSS-Image-SFT \ + yarn test_on moss_image_model_test +``` + +该测试不会自动下载完整权重。CPU 数值和小模型测试不能代替真实 Base/SFT +画质、显存峰值、速度和各平台硬件验证。 diff --git a/examples/basic/workflow-moss-image.py b/examples/basic/workflow-moss-image.py new file mode 100644 index 00000000..88c7704b --- /dev/null +++ b/examples/basic/workflow-moss-image.py @@ -0,0 +1,13 @@ +from ssui import Image, Prompt, workflow +from ssui.config import SSUIConfig +from ssui_image.MOSSImage import MOSSImageGenerate, MOSSImageModel + + +config = SSUIConfig() + + +@workflow +def txt2img(model: MOSSImageModel, prompt: Prompt, negative_prompt: Prompt) -> Image: + # Load OpenMOSS-Team/MOSS-Image-SFT (or a local Base/SFT bundle) in the model + # input. A layout JSON string can be supplied directly as the positive prompt. + return MOSSImageGenerate(config("MOSS Generate"), model, prompt, negative_prompt) diff --git a/extensions/Image/ssui_image/MOSSImage.py b/extensions/Image/ssui_image/MOSSImage.py new file mode 100644 index 00000000..90f9a421 --- /dev/null +++ b/extensions/Image/ssui_image/MOSSImage.py @@ -0,0 +1,74 @@ +"""MOSS-Image nodes using SSUI's shared runtime and local backend implementation.""" +from pathlib import Path +from threading import RLock + +import torch + +from ssui.annotation import param +from ssui.base import Image, Prompt +from ssui.config import SSUIConfig +from ssui.controller import Random, Slider, Switch + + +_PIPELINE_CACHE = {} +_CACHE_LOCK = RLock() + + +class MOSSImageModel: + def __init__(self, model_path: str = "", pipeline=None): + self.model_path = model_path + self.pipeline = pipeline + + @staticmethod + def load(model_path: str, cpu_offload: bool = True, revision: str = "") -> "MOSSImageModel": + """Load an official Base/SFT HF bundle, local directory or Hub repo ID. + + No remote Python is executed. CUDA/ROCm uses BF16 and CPU uses FP32; + VAE always uses FP32. Component offload requires sufficient system RAM. + """ + from backend.moss_image.pipeline import MOSSPipeline + + # MPS requires separate real-weight validation before being enabled. + device = "cuda" if torch.cuda.is_available() else "cpu" + path = Path(model_path).expanduser() + source = str(path.resolve()) if path.is_dir() else model_path + key = (source, cpu_offload, revision, device) + with _CACHE_LOCK: + if key not in _PIPELINE_CACHE: + _PIPELINE_CACHE[key] = MOSSPipeline.from_pretrained( + source, device=device, cpu_offload=cpu_offload, revision=revision or None, + ) + return MOSSImageModel(source, _PIPELINE_CACHE[key]) + + @staticmethod + def clear_cache() -> None: + with _CACHE_LOCK: + for pipeline in _PIPELINE_CACHE.values(): + pipeline.close() + _PIPELINE_CACHE.clear() + if torch.cuda.is_available(): + torch.cuda.empty_cache() + + +@param("width", Slider(256, 2048, 16), default=1024) +@param("height", Slider(256, 2048, 16), default=1024) +@param("steps", Slider(1, 100, 1), default=50) +@param("guidance", Slider(0, 10, 0.1), default=3.0) +@param("shift", Slider(0.1, 10, 0.1), default=5.0) +@param("skip_negative_layer_22", Switch(), default=True) +@param("seed", Random(), default=42) +def MOSSImageGenerate(config: SSUIConfig, model: MOSSImageModel, prompt: Prompt, + negative_prompt: Prompt) -> Image: + """Generate from a caption or a MOSS layout JSON string in the prompt.""" + if config.is_prepare(): + return Image() + if model.pipeline is None: + raise ValueError("MOSS-Image model is not loaded") + image = model.pipeline( + prompt=prompt.text, negative_prompt=negative_prompt.text, + width=int(config["width"]), height=int(config["height"]), + steps=int(config["steps"]), guidance=float(config["guidance"]), + shift=float(config["shift"]), seed=int(config["seed"]), + uncond_skip_layers=(22,) if config["skip_negative_layer_22"] else (), + ) + return Image(image) diff --git a/ssui/annotation.py b/ssui/annotation.py index 2a3ae3c0..01b301b2 100644 --- a/ssui/annotation.py +++ b/ssui/annotation.py @@ -6,7 +6,9 @@ # 用于记录 API 的装饰器 def workflow(func): - signature = inspect.signature(func) + # RestrictedPython and postponed annotations may provide strings. Resolve + # them in the workflow's namespace before exposing types to the UI. + signature = inspect.signature(func, eval_str=True) param_types = { param.name: param.annotation for param in signature.parameters.values() diff --git a/tests/fixtures/moss_image/qwen35_hf53.json b/tests/fixtures/moss_image/qwen35_hf53.json new file mode 100644 index 00000000..958c4024 --- /dev/null +++ b/tests/fixtures/moss_image/qwen35_hf53.json @@ -0,0 +1,1319 @@ +{ + "config": { + "hidden_size": 16, + "intermediate_size": 32, + "num_hidden_layers": 4, + "num_attention_heads": 2, + "num_key_value_heads": 1, + "head_dim": 8, + "vocab_size": 32, + "linear_num_key_heads": 1, + "linear_num_value_heads": 2, + "linear_key_head_dim": 4, + "linear_value_head_dim": 4 + }, + "outputs": { + "5": [ + [ + -1.164039969444275, + -1.049953818321228, + -0.1948765218257904, + 0.5380091071128845, + 1.3607040643692017, + 0.9585875868797302, + 0.5658016204833984, + -0.4798696041107178, + -0.8376278281211853, + -0.5014169812202454, + 0.277002215385437, + 1.4381792545318604, + 1.3275915384292603, + 0.993557333946228, + -0.3389389216899872, + -0.9313124418258667 + ], + [ + -0.9712284803390503, + -1.0136313438415527, + -0.4036758542060852, + 0.3366009593009949, + 0.812628984451294, + 0.9115378260612488, + 0.24947436153888702, + -0.7665430307388306, + -1.5988675355911255, + -1.4016238451004028, + -0.33212876319885254, + 0.687294065952301, + 0.9358994364738464, + 0.20112645626068115, + -0.6903186440467834, + -1.4083582162857056 + ], + [ + -1.6510083675384521, + -1.1183364391326904, + -0.20070436596870422, + 0.2084674835205078, + 1.1851657629013062, + 0.26407426595687866, + 0.4110660254955292, + -0.7940851449966431, + -0.6455191373825073, + -0.2250099927186966, + 0.45288610458374023, + 1.7768884897232056, + 1.0730425119400024, + 1.2035143375396729, + -0.421682745218277, + -0.3268611431121826 + ], + [ + -0.38617050647735596, + -0.658473551273346, + 0.24073657393455505, + 0.5588428378105164, + 1.6069738864898682, + 0.8832697868347168, + 0.6736375093460083, + -0.5082011222839355, + -1.1430617570877075, + -0.9511402249336243, + -0.6829851865768433, + 0.7789334058761597, + 0.4526312053203583, + 0.4303947985172272, + -1.1025643348693848, + -1.7907897233963013 + ], + [ + -1.597585916519165, + -0.534119725227356, + 0.2867249846458435, + 0.8182143568992615, + 0.1604759395122528, + -0.5075376629829407, + -1.3202698230743408, + -1.344304084777832, + -0.6606301069259644, + 0.26528939604759216, + 1.279880404472351, + 1.121403455734253, + 0.5775984525680542, + -0.5999237895011902, + -0.969266951084137, + -0.5017529129981995 + ] + ], + "67": [ + [ + -1.164039969444275, + -1.049953818321228, + -0.1948765218257904, + 0.5380091071128845, + 1.3607040643692017, + 0.9585875868797302, + 0.5658016204833984, + -0.4798696041107178, + -0.8376278281211853, + -0.5014169812202454, + 0.277002215385437, + 1.4381792545318604, + 1.3275915384292603, + 0.993557333946228, + -0.3389389216899872, + -0.9313124418258667 + ], + [ + -0.9712282419204712, + -1.0136312246322632, + -0.40367627143859863, + 0.3366009294986725, + 0.8126288056373596, + 0.9115374684333801, + 0.2494744062423706, + -0.7665427923202515, + -1.5988670587539673, + -1.4016236066818237, + -0.3321289122104645, + 0.6872941851615906, + 0.9358996748924255, + 0.20112647116184235, + -0.6903181672096252, + -1.408358097076416 + ], + [ + -1.6510083675384521, + -1.1183363199234009, + -0.20070400834083557, + 0.20846766233444214, + 1.1851656436920166, + 0.26407381892204285, + 0.41106581687927246, + -0.7940852046012878, + -0.6455188393592834, + -0.2250097692012787, + 0.45288658142089844, + 1.7768888473510742, + 1.0730419158935547, + 1.2035142183303833, + -0.4216831624507904, + -0.32686084508895874 + ], + [ + -0.3861706852912903, + -0.6584736704826355, + 0.24073660373687744, + 0.5588428974151611, + 1.6069740056991577, + 0.8832697868347168, + 0.673636794090271, + -0.5082013607025146, + -1.1430617570877075, + -0.9511405825614929, + -0.6829846501350403, + 0.7789332270622253, + 0.4526319205760956, + 0.43039435148239136, + -1.1025644540786743, + -1.79079008102417 + ], + [ + -1.5975861549377441, + -0.5341197848320007, + 0.28672510385513306, + 0.8182142972946167, + 0.160475954413414, + -0.5075376629829407, + -1.3202698230743408, + -1.344304084777832, + -0.6606301665306091, + 0.26528939604759216, + 1.2798805236816406, + 1.121403455734253, + 0.577598512172699, + -0.5999237895011902, + -0.969266951084137, + -0.5017529726028442 + ], + [ + -0.6408452391624451, + -0.059179600328207016, + 1.1147202253341675, + 1.312432050704956, + 1.5172048807144165, + 0.53593510389328, + 0.09540443122386932, + -0.4937427043914795, + -0.46372878551483154, + 0.27166682481765747, + 0.7506978511810303, + 1.4623221158981323, + 0.5652449727058411, + -0.1820865273475647, + -1.3038376569747925, + -1.3479784727096558 + ], + [ + -1.4253203868865967, + -0.9955440163612366, + -0.23597775399684906, + 0.5088338255882263, + 0.4211076498031616, + 0.18321481347084045, + -0.8493101596832275, + -1.3994417190551758, + -1.518614649772644, + -0.7480484843254089, + 0.42531484365463257, + 0.81798255443573, + 0.7367205023765564, + -0.36241060495376587, + -0.9004812836647034, + -1.1961452960968018 + ], + [ + -1.0954169034957886, + -0.9060242176055908, + 0.030598171055316925, + 0.6989502310752869, + 1.3113006353378296, + 1.0750263929367065, + 0.5162970423698425, + -0.39340341091156006, + -0.9057000279426575, + -0.41233304142951965, + 0.4892832040786743, + 1.5278542041778564, + 1.3773328065872192, + 0.6968449950218201, + -0.3779577910900116, + -1.000784993171692 + ], + [ + -0.7541239261627197, + -1.1651415824890137, + -0.690609872341156, + 0.4630681872367859, + 1.0646435022354126, + 0.8736756443977356, + -0.3484256863594055, + -1.1604670286178589, + -1.4022297859191895, + -0.9553280472755432, + -0.2459965944290161, + 0.280487596988678, + 0.7876825332641602, + 0.34288692474365234, + -0.546269416809082, + -1.7099673748016357 + ], + [ + -1.202487587928772, + -1.3923338651657104, + -0.582356870174408, + 0.4099172055721283, + 1.2064658403396606, + 0.8560125231742859, + 0.2598564922809601, + -0.4803113043308258, + -0.7105759382247925, + -0.4136415719985962, + 0.17781415581703186, + 1.1931109428405762, + 1.5053672790527344, + 1.250840187072754, + 0.024574216455221176, + -0.8629685640335083 + ], + [ + -0.6118713021278381, + -0.6836802363395691, + 0.016520099714398384, + 0.710783064365387, + 1.0220693349838257, + 1.0989290475845337, + 0.45534956455230713, + -0.43055030703544617, + -1.4102927446365356, + -1.3668699264526367, + -0.4101804792881012, + 0.6201058030128479, + 0.9005406498908997, + -0.01545728836208582, + -0.9517270922660828, + -1.71709406375885 + ], + [ + -1.6383992433547974, + -0.4209318161010742, + 0.4563879072666168, + 0.738257884979248, + 0.09733365476131439, + -0.6730002164840698, + -1.2178758382797241, + -1.2302972078323364, + -0.4966357946395874, + 0.45729607343673706, + 1.393896460533142, + 1.3133251667022705, + 0.5020183324813843, + -0.5741690993309021, + -0.9427080154418945, + -0.21470078825950623 + ], + [ + -0.48837924003601074, + -0.045005738735198975, + 1.0030243396759033, + 1.5613867044448853, + 1.3472342491149902, + 0.5864694714546204, + -0.35941243171691895, + -0.5586330890655518, + -0.43751269578933716, + 0.29072481393814087, + 0.8262207508087158, + 1.049198031425476, + 0.6298632621765137, + -0.4866768717765808, + -1.2814654111862183, + -1.6056658029556274 + ], + [ + -1.5212408304214478, + -0.9611939191818237, + -0.1785643845796585, + 0.5458647608757019, + 0.3332148790359497, + 0.050280097872018814, + -0.965904712677002, + -1.4202070236206055, + -1.4181594848632812, + -0.6047500371932983, + 0.5929673314094543, + 0.8935425877571106, + 0.7477717399597168, + -0.414517879486084, + -0.9024643301963806, + -1.0616236925125122 + ], + [ + -1.0754565000534058, + -0.6882664561271667, + 0.36403512954711914, + 0.8564308285713196, + 1.26297128200531, + 1.032820701599121, + 0.5788488984107971, + -0.24485132098197937, + -0.8537949323654175, + -0.3510182201862335, + 0.6173743009567261, + 1.6755584478378296, + 1.352644443511963, + 0.44228655099868774, + -0.5609309077262878, + -0.981488823890686 + ], + [ + -0.8998610973358154, + -1.1190211772918701, + -0.5903055667877197, + 0.4906935691833496, + 0.9304161667823792, + 0.7264536619186401, + -0.5223097205162048, + -1.292228102684021, + -1.4869532585144043, + -0.9165506362915039, + -0.07675345242023468, + 0.3753853142261505, + 0.7387347221374512, + 0.12164635211229324, + -0.6670708656311035, + -1.6555167436599731 + ], + [ + -1.106446623802185, + -1.3263819217681885, + -0.575316309928894, + 0.45437613129615784, + 1.2224061489105225, + 0.9966624975204468, + 0.33751150965690613, + -0.4354648292064667, + -0.7714909911155701, + -0.5154042840003967, + 0.14325298368930817, + 1.123288631439209, + 1.5119633674621582, + 1.1798359155654907, + 0.009247206151485443, + -0.9513022303581238 + ], + [ + -0.6656404137611389, + -0.8805939555168152, + -0.3420822322368622, + 0.650628924369812, + 0.9964033961296082, + 1.1094447374343872, + 0.2541984021663666, + -0.5979883074760437, + -1.3788729906082153, + -1.368480920791626, + -0.41984325647354126, + 0.4169483780860901, + 0.9842543005943298, + 0.17942968010902405, + -0.7560757398605347, + -1.712511658668518 + ], + [ + -1.7605836391448975, + -0.4703567922115326, + 0.45243969559669495, + 0.608209490776062, + 0.10390915721654892, + -0.640379011631012, + -0.9861994981765747, + -1.1029797792434692, + -0.4479992389678955, + 0.5168781876564026, + 1.4839533567428589, + 1.5755866765975952, + 0.626151442527771, + -0.3620416820049286, + -0.7732274532318115, + 0.0500757098197937 + ], + [ + -0.31862929463386536, + 0.034987255930900574, + 0.8310803771018982, + 1.6412018537521362, + 1.3071775436401367, + 0.7507400512695312, + -0.48749300837516785, + -0.6600319743156433, + -0.47495996952056885, + 0.20087666809558868, + 0.8342134356498718, + 0.7248284816741943, + 0.5871127843856812, + -0.6102708578109741, + -1.238926649093628, + -1.746717095375061 + ], + [ + -1.593424677848816, + -0.8985556364059448, + -0.07558580487966537, + 0.6505300402641296, + 0.2727259397506714, + -0.11561392992734909, + -1.1138840913772583, + -1.412674903869629, + -1.224486231803894, + -0.39311668276786804, + 0.7774990200996399, + 0.9556491374969482, + 0.7588520050048828, + -0.46377021074295044, + -0.933270275592804, + -0.9432682394981384 + ], + [ + -0.9557304382324219, + -0.46718594431877136, + 0.616603672504425, + 0.9876304864883423, + 1.2941133975982666, + 1.0305593013763428, + 0.5782337188720703, + -0.20445194840431213, + -0.8043456673622131, + -0.2272191047668457, + 0.732978880405426, + 1.7115086317062378, + 1.229839563369751, + 0.22298741340637207, + -0.703389585018158, + -1.025354027748108 + ], + [ + -1.074096441268921, + -1.0714272260665894, + -0.4633736312389374, + 0.5216711163520813, + 0.7693930268287659, + 0.5518480539321899, + -0.6586092710494995, + -1.3615003824234009, + -1.5302565097808838, + -0.8813692927360535, + 0.08961216360330582, + 0.5079103112220764, + 0.7256325483322144, + -0.08082190901041031, + -0.7861016988754272, + -1.5519378185272217 + ], + [ + -1.0744714736938477, + -1.2459017038345337, + -0.5015496015548706, + 0.5006703734397888, + 1.2522687911987305, + 1.0639829635620117, + 0.38347893953323364, + -0.4476964771747589, + -0.8353737592697144, + -0.5514346361160278, + 0.17699670791625977, + 1.1475378274917603, + 1.4807393550872803, + 1.0833643674850464, + -0.06398358941078186, + -1.0065667629241943 + ], + [ + -0.5265975594520569, + -1.1065393686294556, + -0.7456641793251038, + 0.5049360990524292, + 1.1690247058868408, + 1.1252858638763428, + 0.009975678287446499, + -0.8272430300712585, + -1.2382018566131592, + -1.1091575622558594, + -0.47359368205070496, + 0.13981415331363678, + 0.9087688326835632, + 0.5541602969169617, + -0.42925867438316345, + -1.7459819316864014 + ], + [ + -1.9444597959518433, + -0.6739075183868408, + 0.38399380445480347, + 0.5060243606567383, + 0.18996396660804749, + -0.2603961229324341, + -0.39172419905662537, + -0.7652850151062012, + -0.6624763011932373, + 0.12658217549324036, + 1.3463751077651978, + 1.9397307634353638, + 1.0722606182098389, + -0.04880305379629135, + -0.6042197346687317, + 0.11479832977056503 + ], + [ + -0.3177393078804016, + 0.07901760935783386, + 0.6864266395568848, + 1.600429892539978, + 1.1538989543914795, + 1.065060019493103, + -0.11339499801397324, + -0.4988024830818176, + -0.7631477117538452, + -0.43068718910217285, + 0.5917710065841675, + 0.6009714603424072, + 0.7455675005912781, + -0.6999343037605286, + -1.2780832052230835, + -1.8128997087478638 + ], + [ + -1.6073206663131714, + -0.7949252128601074, + 0.06083976477384567, + 0.7665043473243713, + 0.2528064250946045, + -0.2956639230251312, + -1.265607476234436, + -1.3993879556655884, + -0.969467282295227, + -0.10414137691259384, + 0.9698559641838074, + 1.0009300708770752, + 0.7167158126831055, + -0.5023120641708374, + -0.9651982188224792, + -0.8239924311637878 + ], + [ + -0.8018336296081543, + -0.2425672709941864, + 0.8679617643356323, + 1.1787500381469727, + 1.355481505393982, + 0.9451133012771606, + 0.4496104121208191, + -0.22638659179210663, + -0.6801307797431946, + -0.042770981788635254, + 0.8207042217254639, + 1.6487243175506592, + 1.0444767475128174, + -0.009629827924072742, + -0.9108079075813293, + -1.1448392868041992 + ], + [ + -1.2420305013656616, + -1.0274678468704224, + -0.34924858808517456, + 0.5229823589324951, + 0.611286997795105, + 0.3857468068599701, + -0.7391401529312134, + -1.3905582427978516, + -1.54706871509552, + -0.847335934638977, + 0.2371656894683838, + 0.6527329683303833, + 0.7233318090438843, + -0.23051133751869202, + -0.8684247136116028, + -1.405374526977539 + ], + [ + -1.0704166889190674, + -1.1429455280303955, + -0.3673015236854553, + 0.5671685338020325, + 1.291686773300171, + 1.1000021696090698, + 0.41321101784706116, + -0.468648761510849, + -0.879602313041687, + -0.518608570098877, + 0.2802424430847168, + 1.2414973974227905, + 1.442934513092041, + 0.9651402831077576, + -0.15803056955337524, + -1.0332725048065186 + ], + [ + -0.5953807234764099, + -1.159062147140503, + -0.7910851240158081, + 0.4566574692726135, + 1.1517143249511719, + 1.0504611730575562, + -0.11522231996059418, + -0.9601908922195435, + -1.2777509689331055, + -1.0390831232070923, + -0.4071734845638275, + 0.16231775283813477, + 0.8680253028869629, + 0.5501804947853088, + -0.4137510061264038, + -1.7167832851409912 + ], + [ + -0.9885039329528809, + -1.0922762155532837, + -0.36895450949668884, + 0.5558915734291077, + 1.3880037069320679, + 1.1663881540298462, + 0.40418264269828796, + -0.5507913827896118, + -0.885470449924469, + -0.4060516059398651, + 0.3567737936973572, + 1.2376433610916138, + 1.3571888208389282, + 0.9745978713035583, + -0.12103214114904404, + -1.050339698791504 + ], + [ + -0.9476694464683533, + -1.025107979774475, + -0.47476109862327576, + 0.3992656171321869, + 0.8238491415977478, + 0.9599352478981018, + 0.17619502544403076, + -0.7915993332862854, + -1.5555957555770874, + -1.3751834630966187, + -0.2891117334365845, + 0.6067301630973816, + 0.9826776385307312, + 0.21548932790756226, + -0.6477399468421936, + -1.4478063583374023 + ], + [ + -1.295867681503296, + -1.3799935579299927, + -0.6607632040977478, + 0.38627487421035767, + 1.1403460502624512, + 0.816582441329956, + 0.09381674975156784, + -0.6708844304084778, + -0.7382984757423401, + -0.2995455861091614, + 0.4019167721271515, + 1.2158337831497192, + 1.4731626510620117, + 1.1952219009399414, + 0.08092116564512253, + -0.7536155581474304 + ], + [ + -0.3643765151500702, + -0.44756874442100525, + -0.12821635603904724, + 0.7845453023910522, + 1.053889274597168, + 1.4349242448806763, + 0.7150254249572754, + -0.28693342208862305, + -1.252160668373108, + -1.449127435684204, + -0.3534066081047058, + 0.3932977020740509, + 0.9351462721824646, + 0.057365961372852325, + -0.7848420143127441, + -1.638017177581787 + ], + [ + -1.5943313837051392, + -0.5487427115440369, + 0.30074742436408997, + 0.7917003035545349, + 0.17872880399227142, + -0.5202001929283142, + -1.314677357673645, + -1.3501386642456055, + -0.675672709941864, + 0.283552348613739, + 1.2645962238311768, + 1.1449106931686401, + 0.5569849610328674, + -0.5875182151794434, + -0.9674743413925171, + -0.5043992400169373 + ], + [ + -0.5973982810974121, + -0.05746559798717499, + 1.0751941204071045, + 1.4187334775924683, + 1.4133118391036987, + 0.6655622720718384, + 0.00018308091966900975, + -0.4248787462711334, + -0.49371764063835144, + 0.23785588145256042, + 0.8356324434280396, + 1.366840124130249, + 0.7072526812553406, + -0.312264084815979, + -1.2186297178268433, + -1.4115229845046997 + ], + [ + -1.4307762384414673, + -0.9915216565132141, + -0.2393122762441635, + 0.5099037885665894, + 0.4235718548297882, + 0.17928418517112732, + -0.8454284071922302, + -1.4053993225097656, + -1.5129488706588745, + -0.7509901523590088, + 0.4266444742679596, + 0.8179399967193604, + 0.7336462140083313, + -0.35671570897102356, + -0.9065841436386108, + -1.190860629081726 + ], + [ + -1.0996603965759277, + -0.9007138609886169, + 0.02524939738214016, + 0.7018545866012573, + 1.3094886541366577, + 1.0764250755310059, + 0.5186862945556641, + -0.3980233371257782, + -0.9025735259056091, + -0.41742977499961853, + 0.494530588388443, + 1.5251927375793457, + 1.3779510259628296, + 0.697712242603302, + -0.38056349754333496, + -0.9956237077713013 + ], + [ + -0.7606219053268433, + -1.1562541723251343, + -0.7017390131950378, + 0.474063903093338, + 1.0565778017044067, + 0.8785082697868347, + -0.3493037819862366, + -1.164616346359253, + -1.3945870399475098, + -0.9659641981124878, + -0.23516276478767395, + 0.27030879259109497, + 0.7952726483345032, + 0.3402610421180725, + -0.548898458480835, + -1.7035590410232544 + ], + [ + -1.2011027336120605, + -1.3841577768325806, + -0.6053658127784729, + 0.439991295337677, + 1.1762272119522095, + 0.8876980543136597, + 0.24013005197048187, + -0.4721260070800781, + -0.709912121295929, + -0.43277794122695923, + 0.205641508102417, + 1.162361979484558, + 1.541782021522522, + 1.2237930297851562, + 0.04145561158657074, + -0.867385745048523 + ], + [ + -0.6441279649734497, + -0.6198112964630127, + -0.0543200746178627, + 0.7918713688850403, + 0.9556578397750854, + 1.14281165599823, + 0.44006043672561646, + -0.4481901526451111, + -1.3647522926330566, + -1.4377315044403076, + -0.3307535648345947, + 0.5425298810005188, + 0.9585237503051758, + -0.05363250523805618, + -0.9610311388969421, + -1.6809543371200562 + ], + [ + -1.6408262252807617, + -0.41506072878837585, + 0.44867658615112305, + 0.7459040880203247, + 0.09027539193630219, + -0.6677783131599426, + -1.2192450761795044, + -1.231066107749939, + -0.4921787977218628, + 0.45015648007392883, + 1.401381492614746, + 1.3049447536468506, + 0.5084493160247803, + -0.5776823163032532, + -0.9421577453613281, + -0.2110709249973297 + ], + [ + -0.4909762740135193, + -0.04245099425315857, + 1.0012829303741455, + 1.5628938674926758, + 1.3471187353134155, + 0.5857561826705933, + -0.35790786147117615, + -0.5609139800071716, + -0.4351523816585541, + 0.288590669631958, + 0.8280783891677856, + 1.0486533641815186, + 0.6295892000198364, + -0.4855066239833832, + -1.2839877605438232, + -1.6031086444854736 + ], + [ + -1.5235487222671509, + -0.95978844165802, + -0.17900000512599945, + 0.5452322959899902, + 0.3347386419773102, + 0.04804413393139839, + -0.963528037071228, + -1.4225857257843018, + -1.4164668321609497, + -0.6056485772132874, + 0.5928240418434143, + 0.8947778344154358, + 0.7456545233726501, + -0.41189226508140564, + -0.9052771329879761, + -1.059258222579956 + ], + [ + -1.0782485008239746, + -0.6879091262817383, + 0.36709457635879517, + 0.8501724600791931, + 1.269677758216858, + 1.0248314142227173, + 0.5853052735328674, + -0.2489621788263321, + -0.8518797159194946, + -0.34908679127693176, + 0.6122110486030579, + 1.6819331645965576, + 1.3437669277191162, + 0.45016536116600037, + -0.5668429732322693, + -0.9775899648666382 + ], + [ + -0.9034074544906616, + -1.1156208515167236, + -0.593532383441925, + 0.4929908514022827, + 0.9296751022338867, + 0.7259833216667175, + -0.5206737518310547, + -1.2954353094100952, + -1.4838553667068481, + -0.9200454354286194, + -0.07403511554002762, + 0.3738037645816803, + 0.7388937473297119, + 0.12300242483615875, + -0.6700897812843323, + -1.6521098613739014 + ], + [ + -1.1071343421936035, + -1.3244655132293701, + -0.5789175629615784, + 0.45858803391456604, + 1.2185848951339722, + 1.000205397605896, + 0.3355753421783447, + -0.4352206289768219, + -0.7703307271003723, + -0.5184826850891113, + 0.14724403619766235, + 1.1190956830978394, + 1.5163182020187378, + 1.1770732402801514, + 0.010519122704863548, + -0.9508309364318848 + ], + [ + -0.6718427538871765, + -0.8745784163475037, + -0.3472983241081238, + 0.6545040011405945, + 0.9955683946609497, + 1.1084822416305542, + 0.2577197849750519, + -0.6034337878227234, + -1.373462200164795, + -1.3747237920761108, + -0.4155499041080475, + 0.4146167039871216, + 0.9842654466629028, + 0.18210071325302124, + -0.7617881894111633, + -1.7066277265548706 + ], + [ + -1.766434907913208, + -0.46392250061035156, + 0.4476107954978943, + 0.6087154746055603, + 0.1058865338563919, + -0.643764078617096, + -0.9785965085029602, + -1.1089503765106201, + -0.4411636292934418, + 0.5108706951141357, + 1.484747290611267, + 1.5745434761047363, + 0.6222309470176697, + -0.3550501763820648, + -0.7801976799964905, + 0.05793677642941475 + ], + [ + -0.32252752780914307, + 0.03299900144338608, + 0.8388262987136841, + 1.6321511268615723, + 1.320858359336853, + 0.7394967079162598, + -0.47832414507865906, + -0.666218638420105, + -0.4749615490436554, + 0.20579496026039124, + 0.8256369829177856, + 0.7380152344703674, + 0.5741950273513794, + -0.5986000299453735, + -1.248751163482666, + -1.745105266571045 + ], + [ + -1.5935065746307373, + -0.9005561470985413, + -0.072603240609169, + 0.647042453289032, + 0.2764710783958435, + -0.11864744126796722, + -1.1121671199798584, + -1.4134342670440674, + -1.225824236869812, + -0.3907281160354614, + 0.7741718888282776, + 0.9597859978675842, + 0.7553480863571167, + -0.46106991171836853, + -0.93478924036026, + -0.9439358115196228 + ], + [ + -0.9559301733970642, + -0.4670034348964691, + 0.6163948774337769, + 0.9877870082855225, + 1.2940460443496704, + 1.0305733680725098, + 0.5783073902130127, + -0.20461659133434296, + -0.8041779398918152, + -0.22743523120880127, + 0.7331542372703552, + 1.7113957405090332, + 1.229899525642395, + 0.22304664552211761, + -0.7035313248634338, + -1.0251743793487549 + ], + [ + -1.0767024755477905, + -1.0707858800888062, + -0.4626493453979492, + 0.5195989012718201, + 0.7727698683738708, + 0.5482318997383118, + -0.6553067564964294, + -1.3645656108856201, + -1.5290966033935547, + -0.881462037563324, + 0.08803731948137283, + 0.5109956860542297, + 0.7218805551528931, + -0.07681058347225189, + -0.7899293899536133, + -1.549831509590149 + ], + [ + -1.0744887590408325, + -1.2456820011138916, + -0.5020342469215393, + 0.5012831091880798, + 1.251681923866272, + 1.0645532608032227, + 0.38312196731567383, + -0.4475809633731842, + -0.8352697491645813, + -0.5518292188644409, + 0.17756322026252747, + 1.1469115018844604, + 1.4814213514328003, + 1.0828840732574463, + -0.06370988488197327, + -1.0065829753875732 + ], + [ + -0.5268889665603638, + -1.106719970703125, + -0.7452055811882019, + 0.5042079091072083, + 1.1699626445770264, + 1.124475121498108, + 0.010665606707334518, + -0.8276886343955994, + -1.2382136583328247, + -1.1088753938674927, + -0.4742739200592041, + 0.14070947468280792, + 0.9078428149223328, + 0.5550608038902283, + -0.42990872263908386, + -1.7458148002624512 + ], + [ + -1.9473530054092407, + -0.670668363571167, + 0.38246166706085205, + 0.5050343871116638, + 0.19213387370109558, + -0.26370278000831604, + -0.3872169852256775, + -0.7688759565353394, + -0.6587569117546082, + 0.12445855885744095, + 1.3456699848175049, + 1.9401506185531616, + 1.0683447122573853, + -0.04423849284648895, + -0.6086107492446899, + 0.11913500726222992 + ], + [ + -0.3225119113922119, + 0.08588328957557678, + 0.6811192631721497, + 1.60562264919281, + 1.15054190158844, + 1.065084457397461, + -0.11205045133829117, + -0.502777636051178, + -0.7571278810501099, + -0.43609359860420227, + 0.597827672958374, + 0.5966195464134216, + 0.746801495552063, + -0.6999244093894958, + -1.2819963693618774, + -1.8069000244140625 + ], + [ + -1.607440710067749, + -0.7970885634422302, + 0.06414515525102615, + 0.7626834511756897, + 0.25691816210746765, + -0.29907506704330444, + -1.2637468576431274, + -1.4002505540847778, + -0.9708702564239502, + -0.10145813971757889, + 0.966241717338562, + 1.005496621131897, + 0.7128263115882874, + -0.49934038519859314, + -0.9669041037559509, + -0.8246712684631348 + ], + [ + -0.8008646368980408, + -0.24072538316249847, + 0.8627475500106812, + 1.1870710849761963, + 1.3476479053497314, + 0.9535476565361023, + 0.4435791075229645, + -0.2237432450056076, + -0.6801859140396118, + -0.04693170264363289, + 0.8280904293060303, + 1.6407607793807983, + 1.0542504787445068, + -0.01749221235513687, + -0.9061947464942932, + -1.1469393968582153 + ], + [ + -1.2436163425445557, + -1.0272537469863892, + -0.34845489263534546, + 0.521293044090271, + 0.613739013671875, + 0.38315629959106445, + -0.7368883490562439, + -1.3924806118011475, + -1.5464550256729126, + -0.8471076488494873, + 0.23582462966442108, + 0.6550784707069397, + 0.7206065654754639, + -0.22774609923362732, + -0.8709244132041931, + -1.404083013534546 + ], + [ + -1.0706971883773804, + -1.1425001621246338, + -0.3679627776145935, + 0.5678698420524597, + 1.2911226749420166, + 1.1004713773727417, + 0.4130442440509796, + -0.46876993775367737, + -0.8792726397514343, + -0.5192131400108337, + 0.2809382975101471, + 1.2408454418182373, + 1.4435503482818604, + 0.9648374915122986, + -0.1580069214105606, + -1.0330195426940918 + ], + [ + -0.5954207181930542, + -1.158829689025879, + -0.7914441227912903, + 0.4570871591567993, + 1.1512812376022339, + 1.050805687904358, + -0.11543697863817215, + -0.9601454138755798, + -1.2776031494140625, + -1.0393811464309692, + -0.40675580501556396, + 0.1618519276380539, + 0.8684467077255249, + 0.5498623251914978, + -0.41360917687416077, + -1.7167065143585205 + ], + [ + -0.9880082011222839, + -1.0928834676742554, + -0.36816975474357605, + 0.5551477074623108, + 1.388512372970581, + 1.1660208702087402, + 0.40417802333831787, + -0.5504702925682068, + -0.8859694600105286, + -0.40529340505599976, + 0.3560023903846741, + 1.2382807731628418, + 1.3566513061523438, + 0.974727213382721, + -0.12084214389324188, + -1.0508023500442505 + ], + [ + -0.9472335577011108, + -1.024848461151123, + -0.47552528977394104, + 0.4004342555999756, + 0.8224352598190308, + 0.961240828037262, + 0.17511962354183197, + -0.7909474968910217, + -1.5555881261825562, + -1.375663161277771, + -0.28805798292160034, + 0.6053137183189392, + 0.9841509461402893, + 0.21412287652492523, + -0.646746814250946, + -1.4481185674667358 + ], + [ + -1.2907118797302246, + -1.3814276456832886, + -0.6653552055358887, + 0.39528948068618774, + 1.1298291683197021, + 0.829016387462616, + 0.0831858441233635, + -0.6639829874038696, + -0.7419065237045288, + -0.3019647002220154, + 0.40915802121162415, + 1.2057472467422485, + 1.4868508577346802, + 1.1828171014785767, + 0.09100992232561111, + -0.7603098750114441 + ] + ] + }, + "reference": "huggingface/transformers@aad13b87ed59f2afcfaebc985f403301887a35fc, Qwen3_5TextModel, eager attention, no cache, FP32", + "weights": "For each parameter in registration order: sin(arange(numel, float32) * 0.17 + parameter_index) * 0.15" +} diff --git a/tests/flow_operator_discovery_test.py b/tests/flow_operator_discovery_test.py index b20505fb..be0b19cf 100644 --- a/tests/flow_operator_discovery_test.py +++ b/tests/flow_operator_discovery_test.py @@ -39,3 +39,5 @@ def test_discovers_extension_generation_and_model_load_nodes(self): names = {(item.module, item.callable) for item in operators} self.assertIn(("ssui_image.Flux2", "Flux2KleinModel.load"), names) self.assertIn(("ssui_image.Flux2", "Flux2KleinGenerate"), names) + self.assertIn(("ssui_image.MOSSImage", "MOSSImageModel.load"), names) + self.assertIn(("ssui_image.MOSSImage", "MOSSImageGenerate"), names) diff --git a/tests/moss_image_e2e_test.py b/tests/moss_image_e2e_test.py new file mode 100644 index 00000000..a85c2736 --- /dev/null +++ b/tests/moss_image_e2e_test.py @@ -0,0 +1,139 @@ +"""HTTP -> WebSocket scheduler -> sandbox -> local model -> saved PNG. + +Only weights are synthetic. All application components and inference operations +are real, and no model downloads or mocked pipeline calls are used. +""" +import asyncio +import contextlib +import json +import os +from pathlib import Path +import shutil +import tempfile +import unittest +from unittest.mock import patch + +from fastapi import FastAPI +import httpx +from PIL import Image +import torch + +from server.routes.script_routes import router +from server.script_service import ScriptService +from ss_executor.__main__ import Executor +from ss_executor.model import TaskStatus +from ss_executor.scheduler import TaskScheduler +from tests.moss_image_fixtures import create_moss_bundle + + +class MOSSImageEndToEndTest(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + from ssui_image.MOSSImage import MOSSImageModel + + self.model_class = MOSSImageModel + self.model_class.clear_cache() + self.previous_threads = torch.get_num_threads() + torch.set_num_threads(1) + self.temp = tempfile.TemporaryDirectory() + self.root = Path(self.temp.name) + self.bundle = create_moss_bundle(self.root / "model") + (self.root / "ssproject.yaml").write_text("ssui_version: 1.0.0\ndependencies: []\n", encoding="utf-8") + self.script = self.root / "workflow.py" + example = Path(__file__).resolve().parents[1] / "examples/basic/workflow-moss-image.py" + shutil.copyfile(example, self.script) + self.scheduler = TaskScheduler() + with patch.dict(os.environ, {"SSUI_SCHEDULER_HOST": "127.0.0.1", "SSUI_SCHEDULER_PORT": "0"}): + await self.scheduler.start() + self.addAsyncCleanup(self.shutdown) + port = self.scheduler.server.sockets[0].getsockname()[1] + self.executor = Executor(f"ws://127.0.0.1:{port}") + self.connection = asyncio.create_task(self.executor.connect()) + # Registration is exercised by the prepare request queued immediately below. + app = FastAPI() + app.state.script_service = ScriptService(self.scheduler) + app.include_router(router) + self.client = httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://ssui-test") + + async def shutdown(self): + if hasattr(self, "client"): + await self.client.aclose() + if hasattr(self, "executor"): + self.executor.is_running = False + await self.scheduler.stop() + if hasattr(self, "connection"): + self.connection.cancel() + with contextlib.suppress(asyncio.CancelledError): + await self.connection + self.model_class.clear_cache() + self.temp.cleanup() + torch.set_num_threads(self.previous_threads) + + async def post(self, endpoint, body=None): + return await asyncio.wait_for(self.client.post( + endpoint, params={"script_path": str(self.script), "callable": "txt2img"}, json=body, + ), timeout=30) + + async def execute(self, prompt="a cat", **settings): + values = dict(width=32, height=32, steps=2, guidance=3., shift=5., seed=42, + skip_negative_layer_22=True) + values.update(settings) + response = await self.post("/api/execute", { + "params": { + "model": {"function": "ssui_image.MOSSImage.MOSSImageModel.load", "params": {"model_path": str(self.bundle)}}, + "prompt": {"function": "ssui.base.Prompt.create", "params": {"text": prompt}}, + "negative_prompt": {"function": "ssui.base.Prompt.create", "params": {"text": ""}}, + }, + "details": {"MOSS Generate": values}, + }) + self.assertEqual(response.status_code, 200) + return response.json() + + def read_image(self, result, name): + self.assertIsInstance(result, list, result) + self.assertEqual(result[0]["type"], "image") + path = Path(result[0]["path"]) + self.assertEqual(path.parent, self.root / "output") + with Image.open(path) as image: + self.assertEqual(image.format, "PNG") + self.assertEqual(image.mode, "RGB") + pixels = image.tobytes() + size = image.size + if destination := os.environ.get("SSUI_MOSS_TEST_OUTPUT"): + folder = Path(destination) + folder.mkdir(parents=True, exist_ok=True) + shutil.copyfile(path, folder / f"{name}.png") + return path, size, pixels + + async def test_prepare_generate_layout_and_failure_recovery(self): + response = await self.client.get("/api/script", params={"script_path": str(self.script)}) + self.assertEqual(response.status_code, 200) + self.assertIn("txt2img", response.json()["functions"]) + self.assertEqual(response.json()["functions"]["txt2img"]["params"]["model"], "ssui_image.MOSSImage.MOSSImageModel") + # Prepare must succeed before any component weights are available. + hidden = self.root / "hidden-model" + self.bundle.rename(hidden) + try: + prepared = await self.post("/api/prepare") + finally: + hidden.rename(self.bundle) + self.assertEqual(prepared.status_code, 200) + self.assertEqual(prepared.json()["MOSS Generate"]["steps"]["default"], 50) + first, size, pixels = self.read_image(await self.execute(), "text") + self.assertEqual(size, (32, 32)) + again, _, repeated = self.read_image(await self.execute(), "text-repeat") + self.assertEqual(pixels, repeated) + self.assertNotEqual(first, again) + _, _, changed = self.read_image(await self.execute(seed=43), "text-seed43") + self.assertNotEqual(pixels, changed) + layout = json.dumps({"summary": {"caption": "你好世界海报"}, "items": []}, ensure_ascii=False) + _, layout_size, _ = self.read_image(await self.execute(layout, width=48, height=32, + skip_negative_layer_22=False), "layout") + self.assertEqual(layout_size, (48, 32)) + failed = await self.execute(width=31) + self.assertIn("multiples of 16", failed["error"]) + _, recovered_size, _ = self.read_image(await self.execute(), "recovered") + self.assertEqual(recovered_size, (32, 32)) + statuses = [task.status for task in self.scheduler.tasks.values()] + self.assertEqual(statuses.count(TaskStatus.FAILED), 1) + self.assertEqual(statuses.count(TaskStatus.COMPLETED), 6) + self.assertTrue(all(executor.current_tasks == 0 for executor in self.scheduler.executors.values())) diff --git a/tests/moss_image_fixtures.py b/tests/moss_image_fixtures.py new file mode 100644 index 00000000..a3f00eb7 --- /dev/null +++ b/tests/moss_image_fixtures.py @@ -0,0 +1,59 @@ +"""Deterministic, small real safetensors bundles for offline MOSS integration tests.""" +import json +from pathlib import Path + +import torch +from safetensors.torch import save_file +from tokenizers import Tokenizer +from tokenizers.models import WordLevel + +from backend.moss_image.dit import MOSSImageDiT +from backend.moss_image.text_encoder import TextEncoder +from backend.moss_image.vae import Flux2VAE + + +def create_moss_bundle(root, dit_layers=24, sharded=True): + root = Path(root) + root.mkdir(parents=True, exist_ok=True) + text = dict(hidden_size=16, intermediate_size=32, num_hidden_layers=4, + num_attention_heads=2, num_key_value_heads=1, head_dim=8, vocab_size=32, + linear_num_key_heads=1, linear_num_value_heads=2, + linear_key_head_dim=4, linear_value_head_dim=4) + dit = dict(dim=32, ffn_dim=64, num_heads=2, num_layers=dit_layers, + text_dim=16, freq_dim=8, latent_channels=8) + vae = dict(block_out_channels=(32, 32, 32, 32), decoder_block_out_channels=(32, 32, 32, 32), + latent_channels=2, layers_per_block=1) + with torch.random.fork_rng(devices=[]): + torch.manual_seed(23) + components = [ + ("transformer", "diffusion_pytorch_model", MOSSImageDiT(**dit), dict(_class_name="MOSSImageTransformer", **dit)), + ("text_encoder", "model", TextEncoder(text), dict(model_type="qwen3_5", text_config=text)), + ("vae", "diffusion_pytorch_model", Flux2VAE(**vae), dict(_class_name="AutoencoderKLFlux2", **vae)), + ] + for directory, stem, model, config in components: + folder = root / directory + folder.mkdir() + (folder / "config.json").write_text(json.dumps(config), encoding="utf-8") + state = model.state_dict() + if directory == "text_encoder": + state = {"language_model." + key: value for key, value in state.items()} + state["visual.unused"] = torch.ones(1) + if sharded: + mapping = {} + items = list(state.items()) + for shard in range(2): + name = f"{stem}-{shard + 1:05d}-of-00002.safetensors" + values = dict(items[shard::2]) + save_file(values, str(folder / name)) + mapping.update({key: name for key in values}) + (folder / f"{stem}.safetensors.index.json").write_text(json.dumps({"weight_map": mapping}), encoding="utf-8") + else: + save_file(state, str(folder / f"{stem}.safetensors")) + (root / "tokenizer").mkdir() + Tokenizer(WordLevel({"[UNK]": 0}, unk_token="[UNK]")).save(str(root / "tokenizer/tokenizer.json")) + (root / "scheduler").mkdir() + (root / "scheduler/scheduler_config.json").write_text(json.dumps({ + "_class_name": "MOSSImageScheduler", "num_train_timesteps": 1000, "training_shift": 1., "shift": 5., + }), encoding="utf-8") + (root / "pipeline_moss_image.py").write_text("raise RuntimeError('remote code executed')\n", encoding="utf-8") + return root diff --git a/tests/moss_image_model_test.py b/tests/moss_image_model_test.py new file mode 100644 index 00000000..5828b494 --- /dev/null +++ b/tests/moss_image_model_test.py @@ -0,0 +1,28 @@ +"""Opt-in real MOSS bundle smoke test; never downloads multi-GB weights implicitly.""" +import os +from pathlib import Path +import unittest + +import torch + +from tests.utils import should_run_model_tests + + +@unittest.skipUnless(should_run_model_tests(), "Set SSUI_RUN_MODEL_TESTS=1 for real model tests") +class MOSSImageModelTest(unittest.TestCase): + def test_local_bundle_generates_image(self): + model_path = os.environ.get("SSUI_MOSS_IMAGE_MODEL") + if not model_path: + self.skipTest("Set SSUI_MOSS_IMAGE_MODEL to a complete local Base/SFT bundle") + self.assertTrue(Path(model_path).expanduser().is_dir(), "SSUI_MOSS_IMAGE_MODEL must be a local directory") + if not torch.cuda.is_available(): + self.skipTest("Real MOSS regression requires a CUDA GPU") + from backend.moss_image.pipeline import MOSSPipeline + + pipe = MOSSPipeline.from_pretrained(model_path, device="cuda", cpu_offload=True) + try: + image = pipe('一张写着「你好世界」的海报', width=512, height=512, steps=10, seed=42) + self.assertEqual(image.size, (512, 512)) + self.assertGreater(max(image.getextrema()[0]) - min(image.getextrema()[0]), 0) + finally: + pipe.close() diff --git a/tests/moss_image_nodes_test.py b/tests/moss_image_nodes_test.py new file mode 100644 index 00000000..54ebe64a --- /dev/null +++ b/tests/moss_image_nodes_test.py @@ -0,0 +1,56 @@ +"""Exercise node contracts through the normal Image extension import path.""" +import unittest +from unittest.mock import Mock, patch + +from PIL import Image as PILImage + +from ssui.base import Image, Prompt +from ssui.config import SSUIConfig + + +class MOSSImageNodesTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + from ssui_image import MOSSImage + cls.nodes = MOSSImage + + def tearDown(self): + self.nodes.MOSSImageModel.clear_cache() + + def test_loader_caches_by_revision_and_clears_pipeline(self): + from backend.moss_image.pipeline import MOSSPipeline + + with patch.object(MOSSPipeline, "from_pretrained", side_effect=lambda *a, **kw: Mock()) as load: + first = self.nodes.MOSSImageModel.load("test/moss", revision="a") + second = self.nodes.MOSSImageModel.load("test/moss", revision="a") + third = self.nodes.MOSSImageModel.load("test/moss", revision="b") + self.assertIs(first.pipeline, second.pipeline) + self.assertIsNot(first.pipeline, third.pipeline) + self.assertEqual(load.call_count, 2) + self.nodes.MOSSImageModel.clear_cache() + first.pipeline.close.assert_called_once() + third.pipeline.close.assert_called_once() + + def test_generation_forwards_layout_and_sampling_defaults(self): + pipeline = Mock(return_value=PILImage.new("RGB", (32, 32))) + config = SSUIConfig()("Generate") + layout = '{"summary":{"caption":"海报"},"items":[]}' + result = self.nodes.MOSSImageGenerate(config, self.nodes.MOSSImageModel(pipeline=pipeline), + Prompt(layout), Prompt("")) + self.assertIsInstance(result, Image) + self.assertEqual(pipeline.call_args.kwargs["prompt"], layout) + self.assertEqual(pipeline.call_args.kwargs["steps"], 50) + self.assertEqual(pipeline.call_args.kwargs["shift"], 5) + self.assertEqual(pipeline.call_args.kwargs["uncond_skip_layers"], (22,)) + config["skip_negative_layer_22"] = False + self.nodes.MOSSImageGenerate(config, self.nodes.MOSSImageModel(pipeline=pipeline), Prompt("cat"), Prompt("")) + self.assertEqual(pipeline.call_args.kwargs["uncond_skip_layers"], ()) + + def test_prepare_does_not_require_loaded_weights(self): + config = SSUIConfig()("Prepare") + config.set_prepared() + result = self.nodes.MOSSImageGenerate(config, self.nodes.MOSSImageModel(), Prompt("cat"), Prompt("")) + self.assertIsNone(result._image) + config.set_prepared(False) + with self.assertRaisesRegex(ValueError, "not loaded"): + self.nodes.MOSSImageGenerate(config, self.nodes.MOSSImageModel(), Prompt("cat"), Prompt("")) diff --git a/tests/moss_image_test.py b/tests/moss_image_test.py new file mode 100644 index 00000000..7d040753 --- /dev/null +++ b/tests/moss_image_test.py @@ -0,0 +1,177 @@ +import json +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch + +import torch +from torch import nn +from safetensors.torch import save_file + +from backend.moss_image.dit import MOSSImageDiT +from backend.moss_image.pipeline import MOSSPipeline, format_prompt, sampling_sigmas +from backend.moss_image.qwen_ops import torch_chunk_gated_delta_rule +from backend.moss_image.text_encoder import TextEncoder +from backend.moss_image.vae import Flux2VAE +from backend.moss_image.weights import load_component, text_weight_key, weight_shards + + +class MOSSImageTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.previous_threads = torch.get_num_threads() + torch.set_num_threads(1) + + @classmethod + def tearDownClass(cls): + torch.set_num_threads(cls.previous_threads) + + def test_text_encoder_matches_upstream_golden_across_chunk_boundary(self): + fixture = json.loads((Path(__file__).parent / "fixtures/moss_image/qwen35_hf53.json").read_text()) + model = TextEncoder(fixture["config"]).eval() + with torch.no_grad(): + for index, p in enumerate(model.parameters()): + p.copy_(torch.sin(torch.arange(p.numel()).float() * .17 + index).reshape(p.shape) * .15) + for length, reference in fixture["outputs"].items(): + ids = (torch.arange(int(length)) % fixture["config"]["vocab_size"])[None] + torch.testing.assert_close(model(ids)[0], torch.tensor(reference), atol=1e-6, rtol=1e-5) + ids = torch.tensor([[0, 1, 2, 0], [3, 4, 0, 0]]) + mask = torch.tensor([[0, 1, 1, 0], [1, 1, 0, 0]]) + encoded = model(ids, mask) + torch.testing.assert_close(encoded[0, mask[0].bool()], model(torch.tensor([[1, 2]]))[0]) + self.assertEqual(encoded[~mask.bool()].abs().sum().item(), 0) + + def test_chunk_delta_rule_matches_independent_recurrent_equations(self): + torch.manual_seed(12) + q, k, v = [torch.randn(1, 67, 2, 4) for _ in range(3)] + beta = torch.rand(1, 67, 2) + g = -torch.rand(1, 67, 2) + actual, _ = torch_chunk_gated_delta_rule(q, k, v, g, beta, use_qk_l2norm_in_kernel=True) + q = q * torch.rsqrt(q.square().sum(-1, keepdim=True) + 1e-6) / 2 + k = k * torch.rsqrt(k.square().sum(-1, keepdim=True) + 1e-6) + state = torch.zeros(1, 2, 4, 4) + expected = [] + for i in range(67): + state = state * g[:, i].exp()[..., None, None] + update = beta[:, i, :, None] * (v[:, i] - torch.einsum("bhkv,bhk->bhv", state, k[:, i])) + state = state + torch.einsum("bhk,bhv->bhkv", k[:, i], update) + expected.append(torch.einsum("bhkv,bhk->bhv", state, q[:, i])) + torch.testing.assert_close(actual, torch.stack(expected, 1), atol=1e-6, rtol=1e-5) + + def test_official_nearest_training_grid_schedule(self): + # Official MOSS V2 scheduler produces [1, .024, 0] at two steps, + # unlike a plain shifted linspace schedule. + torch.testing.assert_close(sampling_sigmas(2, 5), torch.tensor([1., .024, 0.]), atol=1e-7, rtol=0) + sigmas = sampling_sigmas(50, 5) + self.assertEqual(sigmas[0].item(), 1) + self.assertEqual(sigmas[-1].item(), 0) + self.assertTrue(torch.all(sigmas[:-1] > sigmas[1:])) + torch.testing.assert_close(sigmas[:-1] * 1000, (sigmas[:-1] * 1000).round(), atol=1e-4, rtol=0) + for steps, shift in [(0, 5), (2, 0), (2, float("nan"))]: + with self.assertRaises(ValueError): + sampling_sigmas(steps, shift) + + def test_dit_mask_and_negative_branch_skipping(self): + torch.manual_seed(2) + model = MOSSImageDiT(dim=32, ffn_dim=64, num_heads=2, num_layers=2, + text_dim=16, freq_dim=8, latent_channels=4).eval() + x, text = torch.randn(1, 4, 2, 3), torch.randn(1, 5, 16) + mask = torch.tensor([[1, 1, 1, 0, 0]]) + with torch.no_grad(): + result = model(x, 500, text, mask) + text[:, 3:] = 1e6 + torch.testing.assert_close(result, model(x, 500, text, mask)) + self.assertFalse(torch.allclose(result, model(x, 500, text, mask, skip_layers=(1,)))) + with self.assertRaises(ValueError): + model(x, 500, text, mask, skip_layers=(22,)) + + def test_flux2_vae_matches_diffusers_decode_with_latent_normalization(self): + from backend.diffusers_compat import preload_attention_dispatch_for_torch_24 + preload_attention_dispatch_for_torch_24() + from diffusers import AutoencoderKLFlux2 + + options = dict(block_out_channels=(32, 32, 32, 32), + decoder_block_out_channels=(32, 32, 32, 32), latent_channels=2, layers_per_block=1) + model, reference = Flux2VAE(**options).eval(), AutoencoderKLFlux2(**options).eval() + with torch.no_grad(): + model.bn.running_mean.fill_(.2) + model.bn.running_var.fill_(1.5) + reference.load_state_dict(model.state_dict()) + z = torch.randn(1, 8, 2, 2) + mean, std = model.latent_stats("cpu", torch.float32) + unpacked = (z * std + mean).reshape(1, 2, 2, 2, 2, 2).permute(0, 1, 4, 2, 5, 3).reshape(1, 2, 4, 4) + torch.testing.assert_close(model.decode(z), reference.decode(unpacked).sample, atol=1e-6, rtol=1e-5) + + def test_safe_sharded_loading_rejects_escape_missing_and_unexpected_keys(self): + with tempfile.TemporaryDirectory() as temp: + root = Path(temp) + save_file({"weight": torch.ones(2, 2)}, str(root / "part.safetensors")) + index = root / "model.safetensors.index.json" + index.write_text(json.dumps({"weight_map": {"weight": "../escape.safetensors"}})) + with self.assertRaises(ValueError): + weight_shards(root, "model") + index.write_text(json.dumps({"weight_map": {"weight": "part.safetensors"}})) + with torch.device("meta"): + model = nn.Linear(2, 2, bias=False) + load_component(model, root, "model", torch.float32) + self.assertEqual(model.weight.device.type, "cpu") + torch.testing.assert_close(model.weight, torch.ones(2, 2)) + with self.assertRaisesRegex(ValueError, "Missing weights"): + load_component(nn.Linear(2, 2), root, "model", torch.float32) + index.write_text(json.dumps({"weight_map": {"other": "part.safetensors"}})) + with self.assertRaisesRegex(ValueError, "do not match"): + load_component(model, root, "model", torch.float32) + self.assertEqual(text_weight_key("language_model.norm.weight"), "norm.weight") + self.assertIsNone(text_weight_key("visual.patch_embed.weight")) + + def test_local_bundle_load_and_generation_without_remote_python(self): + from tokenizers import Tokenizer + from tokenizers.models import WordLevel + + with tempfile.TemporaryDirectory() as temp: + root = Path(temp) + te_options = dict(hidden_size=16, intermediate_size=32, num_hidden_layers=4, + num_attention_heads=2, num_key_value_heads=1, head_dim=8, vocab_size=32, + linear_num_key_heads=1, linear_num_value_heads=2, + linear_key_head_dim=4, linear_value_head_dim=4) + dit_options = dict(dim=32, ffn_dim=64, num_heads=2, num_layers=2, text_dim=16, + freq_dim=8, latent_channels=8) + vae_options = dict(block_out_channels=(32, 32, 32, 32), decoder_block_out_channels=(32, 32, 32, 32), + latent_channels=2, layers_per_block=1) + components = [ + ("transformer", "diffusion_pytorch_model", MOSSImageDiT(**dit_options), + dict(_class_name="MOSSImageTransformer", **dit_options)), + ("text_encoder", "model", TextEncoder(te_options), dict(model_type="qwen3_5", text_config=te_options)), + ("vae", "diffusion_pytorch_model", Flux2VAE(**vae_options), dict(_class_name="AutoencoderKLFlux2", **vae_options)), + ] + for directory, stem, model, config in components: + folder = root / directory + folder.mkdir() + (folder / "config.json").write_text(json.dumps(config)) + state = model.state_dict() + if directory == "text_encoder": + state = {"language_model." + k: v for k, v in state.items()} + state["visual.unused"] = torch.ones(1) + save_file(state, str(folder / f"{stem}.safetensors")) + (root / "tokenizer").mkdir() + Tokenizer(WordLevel({"[UNK]": 0}, unk_token="[UNK]")).save(str(root / "tokenizer/tokenizer.json")) + # A downloaded Python file must never be imported by the loader. + (root / "pipeline_moss_image.py").write_text("raise RuntimeError('remote code executed')") + pipe = MOSSPipeline.from_pretrained(str(root)) + image = pipe("a cat", width=32, height=32, steps=2, seed=9, uncond_skip_layers=()) + again = pipe("a cat", width=32, height=32, steps=2, seed=9, uncond_skip_layers=()) + self.assertEqual(image.size, (32, 32)) + self.assertEqual(image.tobytes(), again.tobytes()) + self.assertEqual(next(pipe.vae.parameters()).dtype, torch.float32) + with patch.object(pipe.vae, "decode", side_effect=RuntimeError("decode failed")): + with self.assertRaisesRegex(RuntimeError, "decode failed"): + pipe("a cat", width=32, height=32, steps=1, uncond_skip_layers=()) + for model in (pipe.transformer, pipe.text_encoder, pipe.vae): + self.assertEqual(next(model.parameters()).device.type, "cpu") + with self.assertRaises(ValueError): + pipe("a cat", width=31, height=32, steps=1, uncond_skip_layers=()) + self.assertEqual(format_prompt("你好"), "<|im_start|>user\n你好<|im_end|>\n<|im_start|>assistant\n\n") + + +if __name__ == "__main__": + unittest.main()