invoke-ai/InvokeAI · error · ValueError
activation_chunk_size must be positive
Error message
activation_chunk_size must be positive
What it means
wan_memory_optimization is a context manager that patches Wan transformer blocks to chunk pointwise activations in fixed-size groups, trading compute for peak memory. A non-positive activation_chunk_size (0 or negative) is meaningless for chunking and would break the internal batching logic, so the generator raises this ValueError before patching anything.
Source
Thrown at invokeai/backend/wan/memory_optimization.py:208
)
output[:, start:end].copy_(output_chunk)
return output
@contextmanager
def wan_memory_optimization(
transformer: torch.nn.Module,
*,
enabled: bool,
activation_chunk_size: int = WAN_ACTIVATION_CHUNK_SIZE,
) -> Iterator[None]:
"""Temporarily chunk Wan transformer pointwise activations during inference."""
if not enabled:
yield
return
if activation_chunk_size <= 0:
raise ValueError("activation_chunk_size must be positive")
blocks: Any = getattr(transformer, "blocks", None)
if blocks is None:
raise TypeError(f"Expected a Wan transformer with blocks, got {type(transformer).__name__}.")
blocks = list(blocks)
if hasattr(transformer, "_invokeai_original_forward") or any(
hasattr(block, "_invokeai_original_forward") for block in blocks
):
raise RuntimeError("Wan memory optimization context cannot be nested.")
patched_blocks: list[tuple[torch.nn.Module, Any, bool]] = []
original_transformer_forward = transformer.forward
transformer_had_instance_forward = "forward" in transformer.__dict__
patch_transformer_forward = all(
hasattr(transformer, name)
for name in ("condition_embedder", "patch_embedding", "proj_out", "rope", "scale_shift_table")
)
try:View on GitHub (pinned to 0b6a024f2f)
Solutions
- Pass a positive activation_chunk_size (e.g. 8, 16, or 32) when enabling the optimization.
- Set enabled=False instead of activation_chunk_size=0 to disable chunking.
- Clamp user config: activation_chunk_size = max(1, int(cfg_value)).
Example fix
// before with wan_memory_optimization(transformer, enabled=True, activation_chunk_size=0): // after with wan_memory_optimization(transformer, enabled=True, activation_chunk_size=16):
Defensive patterns
Strategy: validation
Validate before calling
chunk = max(1, int(config.get("activation_chunk_size", 16)))
with wan_memory_optimization(transformer, enabled=enable, activation_chunk_size=chunk):
run_diffusion(...) Try / catch
try:
with wan_memory_optimization(transformer, True, activation_chunk_size=cfg_chunk):
run_diffusion()
except ValueError as e:
if "must be positive" in str(e):
run_diffusion() # proceed without chunking Prevention
- Treat 0 as 'disabled' by mapping it to enabled=False instead of chunk_size=0.
- Clamp config values with max(1, value).
- Validate chunk-size configs at startup, not at inference time.
When it happens
Trigger: Entering `with wan_memory_optimization(transformer, enabled=True, activation_chunk_size=0)` (or any value <= 0); e.g. chunk size loaded from config as 0 or computed as len//x when the denominator exceeds length.
Common situations: Config file with chunk_size: 0 intending 'auto', dividing to get zero, CLI flag defaulting to 0.
Related errors
- Reference-image dimensions must be multiples of 8 (got {widt
- last_image (FLF2V) interpolation requires num_frames > 1.
- Expected a Wan transformer with blocks, got {type(transforme
- The Anima ControlNet-LLLite model '{lllite_field.control_mod
- This Anima ControlNet-LLLite adapter is an inpainting adapte
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/b6c3cb893014cc0b.
Report an issue: GitHub.