"""DUMAS spatially tiled wrapper for the MiniMax H3 2D latent upscaler. The model implementation and checkpoint loader are vendored from the Apache-2.0 licensed LBH-123-AI ComfyUI node. DUMAS owns the workflow node and adds spatial tiling; time is always processed as one sequence. """ def _upscaler_module(): try: from .vendor import minimax_h3_latent_upscaler_2d as module except ImportError: from vendor import minimax_h3_latent_upscaler_2d as module return module class DumasMMH3LatentUpscale2D: """Apply the MiniMax H3 2D upscaler using X/Y tiles only.""" @classmethod def INPUT_TYPES(cls): import folder_paths names = [ name for name in folder_paths.get_filename_list("latent_upscale_models") if str(name).lower().endswith((".safetensors", ".pth")) ] return { "required": { "latent": ("LATENT",), "model_name": (names or ["(place model in models/latent_upscale_models)"],), "scale": ("FLOAT", {"default": 2.0, "min": 1.0, "max": 4.0, "step": 0.05}), "tile_size": ("INT", {"default": 64, "min": 8, "max": 1024, "step": 8}), "tile_overlap": ("INT", {"default": 8, "min": 0, "max": 128, "step": 1}), "sharpness": ("INT", {"default": 1, "min": 1, "max": 6, "step": 1}), "device": (["cuda", "rocm", "cpu"], {"default": "cuda"}), "precision": (["fp32", "fp16", "bf16"], {"default": "fp16"}), } } RETURN_TYPES = ("LATENT", "STRING") RETURN_NAMES = ("upscaled_latent", "info") FUNCTION = "upscale" CATEGORY = "Dumas/MiniMax H3/Upscale" @staticmethod def _ranges(length, tile, overlap): step = tile - overlap if step <= 0: raise ValueError("tile_overlap must be smaller than tile_size") starts = list(range(0, max(1, length - tile + 1), step)) if not starts or starts[-1] + tile < length: starts.append(max(0, length - tile)) return [(start, min(length, start + tile)) for start in sorted(set(starts))] def upscale(self, latent, model_name, scale, tile_size, tile_overlap, sharpness, device, precision): import torch if str(model_name).startswith("("): raise ValueError("Place the 2D H3 model in models/latent_upscale_models") source = latent.get("samples") if isinstance(latent, dict) else None if source is None or source.ndim != 5: raise ValueError("DUMAS H3 2D upscale expects a 5-D latent: B,C,T,H,W") if source.shape[1] != 24: raise ValueError(f"MiniMax H3 latents must have 24 channels, got {source.shape[1]}") module = _upscaler_module() dev = module._resolve_device(device) compute_dtype = {"fp32": torch.float32, "fp16": torch.float16, "bf16": torch.bfloat16}[precision] model = module.load_model(model_name, dev, precision) mean, std = module._make_norm_tensors(dev, compute_dtype) src = source.to(device=dev, dtype=compute_dtype, copy=True) b, c, t, h, w = src.shape out_h, out_w = int(round(h * float(scale))), int(round(w * float(scale))) if out_h == h and out_w == w: return latent, "DUMAS H3 2D upscale skipped: scale is 1" result = torch.zeros((b, c, t, out_h, out_w), device=dev, dtype=compute_dtype) weights = torch.zeros((1, 1, 1, out_h, out_w), device=dev, dtype=compute_dtype) ys = self._ranges(h, int(tile_size), int(tile_overlap)) xs = self._ranges(w, int(tile_size), int(tile_overlap)) with torch.inference_mode(): for y0, y1 in ys: for x0, x1 in xs: yi0, yi1 = max(0, y0 - tile_overlap), min(h, y1 + tile_overlap) xi0, xi1 = max(0, x0 - tile_overlap), min(w, x1 + tile_overlap) tile = (src[:, :, :, yi0:yi1, xi0:xi1] - mean) / std tile_out = model( tile, scale=float(scale), target_hw=(int(round((yi1 - yi0) * scale)), int(round((xi1 - xi0) * scale))), enable_chunking=False, tier=(int(sharpness) if getattr(model.resizer, "tier_dim", 0) else None), ) oy0, oy1 = int(round((y0 - yi0) * scale)), int(round((y1 - yi0) * scale)) ox0, ox1 = int(round((x0 - xi0) * scale)), int(round((x1 - xi0) * scale)) dy0, dy1 = int(round(y0 * scale)), int(round(y1 * scale)) dx0, dx1 = int(round(x0 * scale)), int(round(x1 * scale)) result[:, :, :, dy0:dy1, dx0:dx1] += tile_out[:, :, :, oy0:oy1, ox0:ox1] weights[:, :, :, dy0:dy1, dx0:dx1] += 1 del tile, tile_out out = (result / weights.clamp_min(1) * std + mean).to("cpu", dtype=source.dtype) if dev.type in ("cuda", "mps"): model.to("cpu") if dev.type == "cuda": torch.cuda.empty_cache() return {"samples": out}, ( f"DUMAS H3 2D spatial upscale: {h}x{w} -> {out_h}x{out_w}; " f"tiles={len(ys) * len(xs)}, tile={tile_size}, overlap={tile_overlap}, " f"time=single-pass, sharpness={sharpness}" ) NODE_CLASS_MAPPINGS = {"DumasMMH3LatentUpscale2D": DumasMMH3LatentUpscale2D} NODE_DISPLAY_NAME_MAPPINGS = { "DumasMMH3LatentUpscale2D": "DUMAS MiniMax-H3 2D Spatial Latent Upscale", }