Skip to main content
Back

Shanghai AI Lab's EndoCoT Teaches Image Models to Solve Mazes at 92.1% Accuracy

A new framework from Shanghai AI Lab teaches diffusion models to reason iteratively in latent space, hitting 92.1% average accuracy on Maze, TSP, Sudoku, and VSP.

Pro

  • EndoCoT lets diffusion models reason iteratively in latent space instead of using a static text embedding, paper here.
  • Two components: Iterative Thought Guidance updates latent states across reasoning steps, Terminal Thought Grounding anchors the final state to text supervision.
  • Hits 92.1% average across Maze, TSP, Sudoku, and VSP, beating DiffThinker by 8.3 points and scoring 90% on Maze-32.
  • Built on Qwen-Image-Edit-2511 with LoRA rank 32, jointly fine-tuning both the MLLM encoder and the diffusion transformer.
  • Discrete text chain-of-thought variant collapses to 0% on Maze-32; continuous latent tokens avoid autoregressive error accumulation.
  • Code and merged checkpoints available at InternLM/EndoCoT, accepted to ECCV 2026.

Diffusion models can paint photorealistic worlds, but ask one to actually solve a maze or a Sudoku puzzle and it usually falls apart. A team from Shanghai AI Laboratory and several Chinese universities argues the reason is structural: when a multimodal LLM is bolted onto a diffusion transformer as a text encoder, it thinks once, hands over an embedding, and then never thinks again. Their new framework, EndoCoT, rewires that pipeline so the encoder keeps reasoning throughout generation, and the results are striking: 92.1% average accuracy across four visual reasoning benchmarks, beating the previous best by 8.3 percentage points.

Why diffusion models flunk logic puzzles

Recent image models like Qwen-Image-Edit and FLUX use a multimodal LLM as their text encoder, which sounds like it should be enough to handle logical instructions. But MLLM text encoders exhibit insufficient reasoning depth, single-step encoding fails to activate the chain-of-thought process, and the guidance remains invariant during the decoding process. The encoder essentially produces one static blob of text embeddings, and the diffusion transformer (DiT) has to hallucinate a solution around it.

The authors did a layer-wise sensitivity analysis on Qwen-Image-Edit-2511 to figure out where reasoning actually happens, and the diagnosis is sharp. In simple 8x8 mazes the DiT draws a trajectory in roughly the right place, but the paths pass through walls, meaning the MLLM never fully encoded the constraints. In dense 32x32 mazes the cross-attention entropy between spatial patches and reasoning tokens goes globally diffuse, so the DiT loses the ability to anchor pixels to specific logic tokens at all. Worse, vanilla denoising commits to a final answer within the first few steps and spends the rest of the schedule just polishing pixels rather than reasoning.

Layer-wise sensitivity heatmaps and maze reasoning failure cases

An endogenous chain of thought

EndoCoT replaces that one-shot handoff with a real reasoning loop. The design has two components working together:

  • Iterative Thought Guidance. Instead of encoding the prompt once, the MLLM runs T reasoning steps. At each step it takes the previous latent thought state as input, produces a new hidden state, and that new state conditions a full denoising pass through the DiT to generate an intermediate visual output. The latent thought is fed back in as a high-dimensional token, bypassing the discrete embedding lookup entirely, so the model reasons in continuous space rather than in text.
  • Terminal Thought Grounding. Because purely visual supervision would let the latent trajectory drift off into nonsense, an auxiliary L2 loss aligns the final thought state with the embedding of the ground-truth reasoning steps. This anchors the last step of the chain to real textual supervision without forcing every intermediate step to be interpretable text.

Both the MLLM and the DiT are fine-tuned jointly with LoRA (rank 32), which the sensitivity analysis showed is necessary since the reasoning signal lives at the junction between the two components. Ablations confirm the pairing matters: an MLLM-only variant scores 0% on maze tasks because language models can plan abstractly but cannot map coordinates to pixels, while a DiT-only variant tops out at 18% on Maze-32 because it can render but not plan.

Two-stage training keeps intermediate steps honest

Training runs in two phases with different objectives. Stage 1, Reasoning Development, supervises every intermediate step with flow-matching loss so the model learns coherent step-by-step trajectories. Stage 2, Terminal Consolidation, freezes gradients on the intermediate states and only optimizes the final output, using short cycles to avoid degrading the reasoning chain that stage 1 built. Dropping stage 2 tanks Maze-32 accuracy from 90% to 14%, so the two objectives really are in tension when optimized together.

The team also tested the obvious alternative: have the MLLM produce discrete text tokens for its intermediate reasoning, like a normal LLM chain of thought. That variant collapses spectacularly, hitting 0% on Maze-32 and degenerating into repetitive token loops. Continuous latent tokens sidestep autoregressive error accumulation.

The numbers

Evaluation covers Maze navigation, Traveling Salesman Problem, Sudoku, and Visual Spatial Planning (VSP), each at multiple difficulty scales. Highlights from the task-specific training setting:

Task (scale)DiffThinkerEndoCoT
Maze-3265%90%
Sudoku-3555%95%
Sudoku-3013%64%
TSP-1859%73%
VSP-Super-3280%85%
Average83.8%92.1%

Zero-shot baselines including Qwen3-VL-8B and Qwen-Image-Edit-2511 hit essentially 0% on Maze, TSP, and Sudoku, so this is not a domain where prompting alone gets you anywhere. EndoCoT also holds up under unified training, where a single model learns all four tasks together, reaching 84.2% average versus DiffThinker's 77.1%.

Step-by-step reasoning across maze, TSP, Sudoku, VSP tasks

Inference-time scaling for images

One of the more interesting properties is that reasoning depth is a knob at inference. Increasing the number of latent thought steps tau smoothly improves accuracy on hard problems: Maze-32 accuracy climbs from 27% at tau=2 to 90% at tau=50. Because the intermediate visual states do not need to be decoded during inference, the compute cost scales predictably, and the relative overhead shrinks as image resolution grows since expensive DiT denoising is not repeated per reasoning step.

The team also shows the framework carries over to progressive image editing. Given multi-step editing instructions, EndoCoT sequentially adds or transforms objects (a stone lantern, a deer that morphs into a sheep), with the reasoning-step counter controlling how many edits are actually applied.

Where it fits and where it does not

This is useful anywhere generation has to obey hard constraints that a single-pass encoder cannot enforce: spatial planning, path drawing, constraint satisfaction, and multi-step edits where later operations depend on earlier ones. The paper does not claim to improve open-domain aesthetics or general text-to-image benchmarks, and the authors flag two clear limitations. First, the optimal number of reasoning steps is picked manually per task rather than learned. Second, training requires datasets with explicit intermediate supervision, which the team had to synthesize procedurally for each of the four benchmark tasks.

Getting the code

The official implementation is on GitHub under InternLM. The repo ships a customized Qwen-Image-Edit checkpoint merged from the original safetensors for compatibility with Diffsynth-Studio training, and the authors ask users to load that version rather than the official weights. The paper has been accepted to ECCV 2026 as an official implementation of EndoCoT, scaling endogenous chain-of-thought reasoning in diffusion models for complex structured generation.