Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(minimax-h3): keep the first-party canvas/frame presets out of the vendored module

Lincoln Stein3bb5e169b6d2
diff --git a/invokeai/app/invocations/minimax_h3_denoise.py b/invokeai/app/invocations/minimax_h3_denoise.py
index 570fc5555f..e4bf3cbaee 100644
--- a/invokeai/app/invocations/minimax_h3_denoise.py
+++ b/invokeai/app/invocations/minimax_h3_denoise.py
@@ -37,13 +37,13 @@ from invokeai.backend.minimax_h3.int8_convrot import Int8ConvrotLinear
from invokeai.backend.minimax_h3.packing import (
MINIMAX_H3_CANVAS_MULTIPLE,
MINIMAX_H3_FPS,
- MINIMAX_H3_VIDEO_FRAME_CHOICES,
MiniMaxH3PackedSequence,
audio_latent_num_frames,
unpack_audio_tokens,
unpatchify_video_tokens,
video_latent_num_frames,
)
+from invokeai.backend.minimax_h3.presets import MINIMAX_H3_VIDEO_FRAME_CHOICES
from invokeai.backend.minimax_h3.pruned_adaln_lora import (
MINIMAX_H3_SILU_TEMB_GRID_URL,
MiniMaxH3SiluTembGrid,
diff --git a/invokeai/app/invocations/minimax_h3_ideal_dimensions.py b/invokeai/app/invocations/minimax_h3_ideal_dimensions.py
index 5baabb2387..c543397a71 100644
--- a/invokeai/app/invocations/minimax_h3_ideal_dimensions.py
+++ b/invokeai/app/invocations/minimax_h3_ideal_dimensions.py
@@ -7,7 +7,7 @@ of 768x1344, both axes rounded to the nearest multiple of 32, aspect ratios limi
the released pipeline's canvas resolution). This node wraps it for workflow graphs so
image-to-video workflows can derive the canvas from the uploaded keyframe instead of
asking the user to type matching width/height values, and adds a reduced "768 lowres"
-preset (:func:`~invokeai.backend.minimax_h3.packing.resolve_lowres_canvas_size`) that
+preset (:func:`~invokeai.backend.minimax_h3.presets.resolve_lowres_canvas_size`) that
pins the LONG edge to 768 for cheaper preview/test renders.
"""
@@ -17,7 +17,8 @@ from invokeai.app.invocations.baseinvocation import BaseInvocation, Classificati
from invokeai.app.invocations.fields import InputField
from invokeai.app.invocations.ideal_size import IdealSizeOutput
from invokeai.app.services.shared.invocation_context import InvocationContext
-from invokeai.backend.minimax_h3.packing import resolve_canvas_size, resolve_lowres_canvas_size
+from invokeai.backend.minimax_h3.packing import resolve_canvas_size
+from invokeai.backend.minimax_h3.presets import resolve_lowres_canvas_size
MiniMaxH3TargetResolution = Literal["768 highres", "768 lowres"]
diff --git a/invokeai/backend/minimax_h3/autoencoder_kl_minimax_h3_audio.py b/invokeai/backend/minimax_h3/autoencoder_kl_minimax_h3_audio.py
index 48ca8cff1b..8e0ea9a370 100644
--- a/invokeai/backend/minimax_h3/autoencoder_kl_minimax_h3_audio.py
+++ b/invokeai/backend/minimax_h3/autoencoder_kl_minimax_h3_audio.py
@@ -410,7 +410,10 @@ class MiniMaxH3AudioAMPBlock(nn.Module):
[_wn_conv1d(channels, channels, kernel_size, dilation=1, padding=(kernel_size - 1) // 2) for _ in dilation]
)
self.activations = nn.ModuleList(
- [MiniMaxH3AudioActivation1d(activation=MiniMaxH3AudioSnakeBeta(channels)) for _ in range(2 * len(dilation))]
+ [
+ MiniMaxH3AudioActivation1d(activation=MiniMaxH3AudioSnakeBeta(channels))
+ for _ in range(2 * len(dilation))
+ ]
)
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
diff --git a/invokeai/backend/minimax_h3/packing.py b/invokeai/backend/minimax_h3/packing.py
index 53ae0ccc32..e34de27e74 100644
--- a/invokeai/backend/minimax_h3/packing.py
+++ b/invokeai/backend/minimax_h3/packing.py
@@ -59,24 +59,11 @@ MINIMAX_H3_MAX_ASPECT_RATIO = 4
MINIMAX_H3_MIN_DURATION = 5.0
MINIMAX_H3_MAX_DURATION = 15.0
-# The released model targets the 5-15 s window above, but shorter grid-aligned clips render and are useful as fast
-# test passes. 90 frames (3.75 s) is the floor we accept for video requests; below that only the 5-frame
-# still-image block is meaningful.
-MINIMAX_H3_MIN_VIDEO_FRAMES = 90
-
# The video VAE encodes 17 pixel frames per chunk and drops the 3 trailing latent frames of every chunk, so
# `17 * n + 5` pixel frames map to `5 * n + 2` latent frames.
MINIMAX_H3_FRAMES_PER_CHUNK = 17
MINIMAX_H3_LATENTS_PER_CHUNK = 5
-# Every legal video frame count, i.e. the `17n + 5` grid points from the accepted floor up to the 15 s ceiling
-# (90 ... 345). The nodes offer exactly these as a choice list, so a request can never miss the grid.
-MINIMAX_H3_VIDEO_FRAME_CHOICES = tuple(
- n
- for n in range(MINIMAX_H3_MIN_VIDEO_FRAMES, int(MINIMAX_H3_MAX_DURATION * MINIMAX_H3_FPS) + 1)
- if n % MINIMAX_H3_FRAMES_PER_CHUNK == MINIMAX_H3_LATENTS_PER_CHUNK
-)
-
# The pixel convention of the video VAE: ImageNet-normalized RGB over a `[0, 1]` base range.
MINIMAX_H3_PIXEL_MEAN = (0.485, 0.456, 0.406)
MINIMAX_H3_PIXEL_STD = (0.229, 0.224, 0.225)
@@ -139,19 +126,6 @@ class MiniMaxH3PackedSequence:
num_condition_audio_rows: int
-def _validated_aspect_ratio(aspect_width: float, aspect_height: float) -> float:
- """Validate an aspect pair against H3's 1:4 – 4:1 envelope and return width/height."""
- if aspect_width <= 0 or aspect_height <= 0:
- raise ValueError(f"The aspect ratio must be positive, got {aspect_width}:{aspect_height}.")
-
- ratio = aspect_width / aspect_height
- if not MINIMAX_H3_MIN_ASPECT_RATIO <= ratio <= MINIMAX_H3_MAX_ASPECT_RATIO:
- raise ValueError(
- f"MiniMax-H3 supports aspect ratios from 1:4 to 4:1, got {aspect_width}:{aspect_height} ({ratio:g})."
- )
- return ratio
-
-
def resolve_canvas_size(aspect_width: float, aspect_height: float) -> tuple[int, int]:
r"""
Resolve a display aspect ratio into a MiniMax-H3 canvas.
@@ -167,7 +141,14 @@ def resolve_canvas_size(aspect_width: float, aspect_height: float) -> tuple[int,
Returns:
`tuple[int, int]`: the `(height, width)` of the canvas.
"""
- ratio = _validated_aspect_ratio(aspect_width, aspect_height)
+ if aspect_width <= 0 or aspect_height <= 0:
+ raise ValueError(f"The aspect ratio must be positive, got {aspect_width}:{aspect_height}.")
+
+ ratio = aspect_width / aspect_height
+ if not MINIMAX_H3_MIN_ASPECT_RATIO <= ratio <= MINIMAX_H3_MAX_ASPECT_RATIO:
+ raise ValueError(
+ f"MiniMax-H3 supports aspect ratios from 1:4 to 4:1, got {aspect_width}:{aspect_height} ({ratio:g})."
+ )
if ratio >= 1.0:
width, height = MINIMAX_H3_SHORT_EDGE * ratio, float(MINIMAX_H3_SHORT_EDGE)
@@ -183,34 +164,6 @@ def resolve_canvas_size(aspect_width: float, aspect_height: float) -> tuple[int,
return max(multiple, round(height / multiple) * multiple), max(multiple, round(width / multiple) * multiple)
-def resolve_lowres_canvas_size(aspect_width: float, aspect_height: float) -> tuple[int, int]:
- r"""
- Resolve a display aspect ratio into a reduced-size MiniMax-H3 canvas with a 768 px LONG edge.
-
- This is not a released H3 canvas family: the model was trained for a 768 short edge
- (see :func:`resolve_canvas_size`). Pinning the long edge instead yields a canvas with roughly half the pixels
- for non-square ratios — useful for fast preview/test renders at some cost in fidelity. The short edge follows
- the aspect ratio and both axes are rounded to the nearest multiple of 32; the area always sits far below the
- `768 * 1344` cap, so no cap scaling is applied. Only the ratio of the two arguments matters.
-
- Args:
- aspect_width (`float`): Width of the target ratio.
- aspect_height (`float`): Height of the target ratio.
-
- Returns:
- `tuple[int, int]`: the `(height, width)` of the canvas.
- """
- ratio = _validated_aspect_ratio(aspect_width, aspect_height)
-
- if ratio >= 1.0:
- width, height = float(MINIMAX_H3_SHORT_EDGE), MINIMAX_H3_SHORT_EDGE / ratio
- else:
- width, height = MINIMAX_H3_SHORT_EDGE * ratio, float(MINIMAX_H3_SHORT_EDGE)
-
- multiple = MINIMAX_H3_CANVAS_MULTIPLE
- return max(multiple, round(height / multiple) * multiple), max(multiple, round(width / multiple) * multiple)
-
-
def align_num_frames(num_frames: int) -> int:
r"""
Snap a frame count up to the next `17 * n + 5` the video VAE can encode.
@@ -240,7 +193,9 @@ def video_latent_num_frames(num_frames: int) -> int:
"""
if num_frames % MINIMAX_H3_FRAMES_PER_CHUNK != MINIMAX_H3_LATENTS_PER_CHUNK:
raise ValueError(f"`num_frames` must be of the form 17 * n + 5, got {num_frames}.")
- return (num_frames - MINIMAX_H3_LATENTS_PER_CHUNK) // MINIMAX_H3_FRAMES_PER_CHUNK * MINIMAX_H3_LATENTS_PER_CHUNK + 2
+ return (
+ num_frames - MINIMAX_H3_LATENTS_PER_CHUNK
+ ) // MINIMAX_H3_FRAMES_PER_CHUNK * MINIMAX_H3_LATENTS_PER_CHUNK + 2
def audio_latent_num_frames(num_frames: int) -> int:
diff --git a/invokeai/backend/minimax_h3/presets.py b/invokeai/backend/minimax_h3/presets.py
new file mode 100644
index 0000000000..e2d11fbb8a
--- /dev/null
+++ b/invokeai/backend/minimax_h3/presets.py
@@ -0,0 +1,78 @@
+"""InvokeAI's canvas and frame-count presets for MiniMax H3.
+
+First-party policy that sits on top of the released model's geometry. It lives here rather
+than in :mod:`invokeai.backend.minimax_h3.packing` because that module is vendored from the
+diffusers MiniMax-H3 branch and is kept byte-identical to upstream (modulo the absolute-import
+rewrite) so the eventual re-sync is a clean copy — see the vendoring note in
+``invokeai/backend/minimax_h3/__init__.py``. Anything InvokeAI adds belongs on this side of
+the line.
+
+Two additions:
+
+- :data:`MINIMAX_H3_VIDEO_FRAME_CHOICES`, the ``17n + 5`` grid points the nodes offer as a
+ choice list, so a request can never miss the grid.
+- :func:`resolve_lowres_canvas_size`, a reduced-size canvas family for fast preview renders.
+"""
+
+from invokeai.backend.minimax_h3.packing import (
+ MINIMAX_H3_CANVAS_MULTIPLE,
+ MINIMAX_H3_FPS,
+ MINIMAX_H3_FRAMES_PER_CHUNK,
+ MINIMAX_H3_LATENTS_PER_CHUNK,
+ MINIMAX_H3_MAX_ASPECT_RATIO,
+ MINIMAX_H3_MAX_DURATION,
+ MINIMAX_H3_MIN_ASPECT_RATIO,
+ MINIMAX_H3_SHORT_EDGE,
+)
+
+# The released model targets the 5-15 s window, but shorter grid-aligned clips render and are useful as fast
+# test passes. 90 frames (3.75 s) is the floor we accept for video requests; below that only the 5-frame
+# still-image block is meaningful.
+MINIMAX_H3_MIN_VIDEO_FRAMES = 90
+
+# Every legal video frame count, i.e. the `17n + 5` grid points from the accepted floor up to the 15 s ceiling
+# (90 ... 345). The nodes offer exactly these as a choice list, so a request can never miss the grid.
+MINIMAX_H3_VIDEO_FRAME_CHOICES = tuple(
+ n
+ for n in range(MINIMAX_H3_MIN_VIDEO_FRAMES, int(MINIMAX_H3_MAX_DURATION * MINIMAX_H3_FPS) + 1)
+ if n % MINIMAX_H3_FRAMES_PER_CHUNK == MINIMAX_H3_LATENTS_PER_CHUNK
+)
+
+
+def resolve_lowres_canvas_size(aspect_width: float, aspect_height: float) -> tuple[int, int]:
+ r"""
+ Resolve a display aspect ratio into a reduced-size MiniMax-H3 canvas with a 768 px LONG edge.
+
+ This is not a released H3 canvas family: the model was trained for a 768 short edge (see
+ :func:`~invokeai.backend.minimax_h3.packing.resolve_canvas_size`). Pinning the long edge instead yields a
+ canvas with roughly half the pixels for non-square ratios — useful for fast preview/test renders at some
+ cost in fidelity. The short edge follows the aspect ratio and both axes are rounded to the nearest multiple
+ of 32; the area always sits far below the `768 * 1344` cap, so no cap scaling is applied. Only the ratio of
+ the two arguments matters.
+
+ The aspect validation is repeated here rather than shared with ``resolve_canvas_size``: factoring it out
+ would mean editing the vendored module.
+
+ Args:
+ aspect_width (`float`): Width of the target ratio.
+ aspect_height (`float`): Height of the target ratio.
+
+ Returns:
+ `tuple[int, int]`: the `(height, width)` of the canvas.
+ """
+ if aspect_width <= 0 or aspect_height <= 0:
+ raise ValueError(f"The aspect ratio must be positive, got {aspect_width}:{aspect_height}.")
+
+ ratio = aspect_width / aspect_height
+ if not MINIMAX_H3_MIN_ASPECT_RATIO <= ratio <= MINIMAX_H3_MAX_ASPECT_RATIO:
+ raise ValueError(
+ f"MiniMax-H3 supports aspect ratios from 1:4 to 4:1, got {aspect_width}:{aspect_height} ({ratio:g})."
+ )
+
+ if ratio >= 1.0:
+ width, height = float(MINIMAX_H3_SHORT_EDGE), MINIMAX_H3_SHORT_EDGE / ratio
+ else:
+ width, height = MINIMAX_H3_SHORT_EDGE * ratio, float(MINIMAX_H3_SHORT_EDGE)
+
+ multiple = MINIMAX_H3_CANVAS_MULTIPLE
+ return max(multiple, round(height / multiple) * multiple), max(multiple, round(width / multiple) * multiple)
diff --git a/invokeai/backend/minimax_h3/sampling.py b/invokeai/backend/minimax_h3/sampling.py
index 8ae8863c57..4f12408caa 100644
--- a/invokeai/backend/minimax_h3/sampling.py
+++ b/invokeai/backend/minimax_h3/sampling.py
@@ -24,13 +24,13 @@ from invokeai.backend.minimax_h3.packing import (
MINIMAX_H3_KEYFRAME_NOISE_AUG,
MINIMAX_H3_LATENTS_PER_CHUNK,
MINIMAX_H3_MAX_DURATION,
- MINIMAX_H3_MIN_VIDEO_FRAMES,
MiniMaxH3PackedSequence,
build_packed_sequence,
build_row_timesteps,
keyframe_condition_noise,
patchify_video_latents,
)
+from invokeai.backend.minimax_h3.presets import MINIMAX_H3_MIN_VIDEO_FRAMES
from invokeai.backend.minimax_h3.scheduling_minimax_h3 import MiniMaxH3Scheduler
# The released FL2VA checkpoint's two sigma shifts (scheduler/ and audio_scheduler/ configs).
diff --git a/invokeai/backend/minimax_h3/transformer_minimax_h3.py b/invokeai/backend/minimax_h3/transformer_minimax_h3.py
index 666d52d778..2181533a33 100644
--- a/invokeai/backend/minimax_h3/transformer_minimax_h3.py
+++ b/invokeai/backend/minimax_h3/transformer_minimax_h3.py
@@ -147,7 +147,9 @@ class MiniMaxH3AdaLayerNormOut(nn.Module):
shift, scale = self.linear(nn.functional.silu(temb).to(self.linear.weight.dtype)).chunk(2, dim=-1)
# The modulation itself stays at the block stack's precision; `forward` casts to the output heads' dtype.
hidden_states = self.norm(hidden_states)
- return hidden_states * (1.0 + scale.index_select(0, timestep_indices)) + shift.index_select(0, timestep_indices)
+ return hidden_states * (1.0 + scale.index_select(0, timestep_indices)) + shift.index_select(
+ 0, timestep_indices
+ )
class MiniMaxH3AttnProcessor:
diff --git a/pyproject.toml b/pyproject.toml
index 04828e8b73..59534aba76 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -309,6 +309,23 @@ select = ["B", "C", "E", "F", "W", "I", "TID"]
"invokeai/backend/minimax_h3/autoencoder_kl_minimax_h3_audio.py" = ["B905"]
"invokeai/backend/minimax_h3/scheduling_minimax_h3.py" = ["B905"]
+[tool.ruff.format]
+# The vendored MiniMax-H3 files are kept byte-identical to upstream diffusers (modulo the
+# absolute-import rewrite) so that re-syncing when a tagged diffusers ships them is a clean copy
+# rather than a manual re-application of formatting. Upstream wraps at a narrower line length, so
+# `ruff format` would rewrap lines for no semantic gain. `ruff check` still runs on them, so the
+# import sorting this project does enforce stays enforced.
+#
+# Listed file by file rather than as a directory glob: the package also holds first-party modules
+# (presets.py, sampling.py, denoise.py, int8_convrot.py, ...) which must stay formatted.
+exclude = [
+ "invokeai/backend/minimax_h3/packing.py",
+ "invokeai/backend/minimax_h3/transformer_minimax_h3.py",
+ "invokeai/backend/minimax_h3/autoencoder_kl_minimax_h3.py",
+ "invokeai/backend/minimax_h3/autoencoder_kl_minimax_h3_audio.py",
+ "invokeai/backend/minimax_h3/scheduling_minimax_h3.py",
+]
+
[tool.ruff.lint.flake8-tidy-imports]
# Disallow all relative imports.
ban-relative-imports = "all"
diff --git a/tests/app/invocations/test_minimax_h3_denoise_num_frames.py b/tests/app/invocations/test_minimax_h3_denoise_num_frames.py
index fecfd18376..3a716931a9 100644
--- a/tests/app/invocations/test_minimax_h3_denoise_num_frames.py
+++ b/tests/app/invocations/test_minimax_h3_denoise_num_frames.py
@@ -12,7 +12,7 @@ from invokeai.app.invocations.minimax_h3_denoise import (
MINIMAX_H3_NUM_FRAMES_LABELS,
MiniMaxH3DenoiseInvocation,
)
-from invokeai.backend.minimax_h3.packing import MINIMAX_H3_VIDEO_FRAME_CHOICES
+from invokeai.backend.minimax_h3.presets import MINIMAX_H3_VIDEO_FRAME_CHOICES
from invokeai.backend.minimax_h3.sampling import MINIMAX_H3_STILL_NUM_FRAMES, validate_num_frames
diff --git a/tests/backend/minimax_h3/test_sampling.py b/tests/backend/minimax_h3/test_sampling.py
index 818806f6f6..714522a68b 100644
--- a/tests/backend/minimax_h3/test_sampling.py
+++ b/tests/backend/minimax_h3/test_sampling.py
@@ -4,13 +4,12 @@ import pytest
import torch
from invokeai.backend.minimax_h3.packing import (
- MINIMAX_H3_VIDEO_FRAME_CHOICES,
align_num_frames,
audio_latent_num_frames,
resolve_canvas_size,
- resolve_lowres_canvas_size,
video_latent_num_frames,
)
+from invokeai.backend.minimax_h3.presets import MINIMAX_H3_VIDEO_FRAME_CHOICES, resolve_lowres_canvas_size
from invokeai.backend.minimax_h3.sampling import build_denoise_state, validate_num_frames