"""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 DumasMMH3LatentUpscale2DModelParams: """Provide the H3_UPSCALE_PARAM contract for the DUMAS 2D path.""" @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": { "model_name": (names or ["(place model in models/latent_upscale_models)"],), "device": (["cuda", "rocm", "cpu"], {"default": "cuda"}), "precision": (["fp32", "fp16", "bf16"], {"default": "fp16"}), "width": ("INT", {"default": 1280, "min": 16, "max": 16384, "step": 16}), "height": ("INT", {"default": 704, "min": 16, "max": 16384, "step": 16}), "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}), }} RETURN_TYPES = ("H3_UPSCALE_PARAM",) RETURN_NAMES = ("latent_upscale_param",) FUNCTION = "build" CATEGORY = "Dumas/MiniMax H3/Upscale" def build(self, model_name, device, precision, width, height, tile_size, tile_overlap, sharpness): if str(model_name).startswith("("): raise ValueError("Place the 2D H3 model in models/latent_upscale_models") return ({ "dumas_h3_2d": True, "model_name": str(model_name), "device": str(device), "precision": str(precision), "target_width": int(width), "target_height": int(height), "tile_size": int(tile_size), "tile_overlap": int(tile_overlap), "sharpness": int(sharpness), },) 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)"],), "width": ("INT", {"default": 1280, "min": 16, "max": 16384, "step": 16}), "height": ("INT", {"default": 704, "min": 16, "max": 16384, "step": 16}), "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, width, height, 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 if int(width) % 16 or int(height) % 16: raise ValueError("Target width and height must be divisible by 16") out_w, out_h = int(width) // 16, int(height) // 16 if out_h < h or out_w < w: raise ValueError("Target dimensions must be at least as large as the input") if out_h == h and out_w == w: return latent, "DUMAS H3 2D upscale skipped: target dimensions equal input" 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 sy, sx = out_h / h, out_w / w tile_y0, tile_y1 = int(round(yi0 * sy)), int(round(yi1 * sy)) tile_x0, tile_x1 = int(round(xi0 * sx)), int(round(xi1 * sx)) tile_out = model( tile, scale=1.0, target_hw=(tile_y1 - tile_y0, tile_x1 - tile_x0), enable_chunking=False, tier=(int(sharpness) if getattr(model.resizer, "tier_dim", 0) else None), ) dy0, dy1 = int(round(y0 * sy)), int(round(y1 * sy)) dx0, dx1 = int(round(x0 * sx)), int(round(x1 * sx)) dh, dw = dy1 - dy0, dx1 - dx0 oy0, ox0 = dy0 - tile_y0, dx0 - tile_x0 result[:, :, :, dy0:dy1, dx0:dx1] += tile_out[:, :, :, oy0:oy0 + dh, ox0:ox0 + dw] 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: {w * 16}x{h * 16} -> {int(width)}x{int(height)}; " f"tiles={len(ys) * len(xs)}, tile={tile_size}, overlap={tile_overlap}, " f"time=single-pass, sharpness={sharpness}" ) NODE_CLASS_MAPPINGS = { "DumasMMH3LatentUpscale2DModelParams": DumasMMH3LatentUpscale2DModelParams, "DumasMMH3LatentUpscale2D": DumasMMH3LatentUpscale2D, } NODE_DISPLAY_NAME_MAPPINGS = { "DumasMMH3LatentUpscale2D": "DUMAS MiniMax-H3 2D Spatial Latent Upscale", "DumasMMH3LatentUpscale2DModelParams": "DUMAS MiniMax-H3 2D Latent Upscale Model", }