Strict Compilers as Perception Checkers for Diagram-to-Code

When a multimodal model writes code from a software diagram, such as a UML class diagram (boxes for classes, +/- marks for public/private members, arrows for inheritance), it does much better in Python than in Java, C#, or Scala. The usual explanation is that the model knows less about the typed languages. This note argues that much of the gap is a perception failure that Python hides. Python does not enforce visibility, types, or interface contracts, so code written from a misread diagram often still runs and passes tests. A Java or Scala compiler rejects the same misreading. If that is right, strict compilers do two jobs. They expose how carelessly the model reads the image, and they are a free source of training signal for teaching it to read carefully. That signal requires no hand-written programs.

The proposal trains a model to produce code in two stages. It first writes a short, language-neutral structural sketch of the diagram: classes, members, types, visibility, and relations. Each sketch line points back to the image regions it was read from. It then writes the code from that sketch. The sketch is trained without paired code. A sketch is rewarded only if the code generated from it compiles in several strict languages at once and, when turned back into a diagram by a deterministic tool, matches the input image. Because the sketch is shared across languages, perception learned under Java's and Scala's compilers should also improve Python, C++, and the rest. The aim is to shrink the language gap by fixing perception rather than by adding more data for each language.

What the literature establishes, and what it leaves open

The motivating evidence comes from From Diagrams to Code. That work builds M²EVAL (300 diagram-only problems across 10 languages) and trains M²-CODER-7B on 13.1M synthetic instruction pairs. It reports that models succeed far more often in Python, JavaScript, and PHP than in C# or Scala, and it points to tiny symbols like +/- that must map onto rigid typed syntax. Its generation step is a single pass from image to code, and its fix is large-scale supervision. We keep its benchmark and its observation. We drop both the single-pass design and the reliance on large paired corpora.

Two results constrain the design.

  • Intermediate text descriptions can hurt. MMCode evaluates only Python and finds that asking a model to caption the image before coding lowers pass rates, because hallucinated caption details carry into the code. MATH-Vision finds chain-of-thought inconsistent, and sometimes harmful, for multimodal models.
  • Text intermediates can help. Compositional Chain-of-Thought gains by having the model emit a scene graph first.

We read the conflict this way: an intermediate step helps only when it is structured and checkable. A free-text caption is neither. Our sketch is typed, and every field in it can be checked by a compiler or by rendering it back into a diagram.

The method draws on three lines of prior work.

  • Visual pointers. MINT-CoT attaches selected visual tokens to each reasoning step by matching a special token's hidden state against image tokens. It reaches 73.70% on the MathVista math subset, but its selector is trained on 54k GPT-4o-built annotations. We reuse the selection mechanism and train it only from reward.
  • Rejecting accidentally correct programs. Weakly Supervised Semantic Parsing with Abstract Examples attacks programs that are right for the wrong reasons by requiring a candidate to hold on four images at once. That cuts random false positives from 50% to 6.25% and reaches 83.5% on CNLVR. We carry this idea over from "many images" to "many compilers": a sketch must survive several independent compilers.
  • Rebuilding the input from the output. StructVAE treats programs as hidden trees and trains on unlabeled text by reconstructing it. We swap its learned decoder for a deterministic code-to-diagram renderer, which removes the high-variance decoder REINFORCE had to train.

Process-Supervised Reinforcement Learning for Code Generation shows that compiler checks can stand in for human step labels in text-only code generation, with +10.5% pass@1 on MBPP+. We move that idea into the visual setting.

Core Claim

The claim has three parts. First, a meaningful share of the typed-language deficit on M²EVAL comes from misreading fine diagram symbols, not from weak knowledge of the language. Second, a shared structural sketch, trained only with label-free signals, closes much of that deficit. Third, those signals are agreement across multiple strict compilers plus agreement between the input diagram and a re-rendered one. The gain should be largest in the languages that are currently worst, and it should come without paired diagram–code data.

Research Program

Diagnosis. We first test whether Python masks perception errors. We take M²EVAL diagrams and make controlled single-symbol edits: flip a + to -, change a member's type, or reverse an inheritance arrow. For each edit and each language, we measure how often the model's code changes, and how often the test verdict changes. If Python verdicts rarely change while Java and Scala verdicts do, the masking hypothesis holds. We then label a sample of real failures by type: misread symbol, wrong relation, or language-level error. This shows how much of the gap is perception.

Model. We start from Qwen2-VL-7B, the same backbone as M²-CODER. The model emits a sketch in a small typed format covering classes, fields, methods with signatures, visibility, and inheritance and association edges. Before each sketch line, an interleave token selects visual tokens in the MINT-CoT manner. Code in the requested language is then written from the sketch, and the generator may still attend to the image.

Label-free training. We use diagram images from M²C-INSTRUCT with their code thrown away. Training uses group-relative policy optimization, which scores each sample of a group against the group's average rather than against a learned value model. Each sampled sketch receives two rewards.

  • Compiler agreement. The model writes code from the sketch in $K$ strict languages (Java, C#, Scala, C++). This reward is the fraction of those that compile and pass a set of tests generated automatically from the sketch itself, such as visibility probes and interface conformance.
  • Render-back agreement. The Java output is converted back into a class diagram with a deterministic code-to-UML tool. This reward is the F1 overlap between text tokens read off the input image and off the rendered image by OCR (names, types, visibility marks), plus the overlap of edge-type counts.

Compiler agreement alone can be satisfied by a self-consistent but wrong sketch. Render-back agreement alone ignores whether the code is valid. Combined, they play the role that the four-image check played for Goldman et al.

The approach is scoped to structural diagrams: class diagrams, design patterns, and flowcharts. For flowcharts, the render-back step uses control-flow graph extraction. Algorithmic picture problems are not covered.

Baselines

The naive reference point is the untuned Qwen2-VL-7B prompted to write code directly. No result for it on M²EVAL was found on Lacuna. We run it ourselves with the same decoding settings used for every system.

The strongest open method at this scale is M²-CODER-7B from From Diagrams to Code. It reports 25.3% average Pass@1 on M²EVAL, and 18.0% without its first training stage. It is the right competitor because it uses the same backbone and benchmark and attacks the same language gap with full supervision. Beating it means exceeding 25.3% average while using none of its paired code. The more important bar is improving its worst-language Pass@1 and narrowing the gap between Python and the strict languages. For context we also report the same paper's figures for Qwen2-VL-72B (24.7%) and GPT-4o (49.7%). GPT-4o is a ceiling, not a target.

A supervised-plus-ours variant applies our training on top of M²-CODER. This tests whether label-free signals add to supervision or merely replace it.

An outcome-reinforcement-learning oracle runs the same policy optimization, but rewards the model with M²EVAL-style hidden unit tests on held-out training problems. This shows what real labels would buy under an identical compute budget: the same number of policy updates and samples per prompt.

The ablations isolate each ingredient:

  • removing the sketch, rewarding the code directly;
  • removing the visual pointers from sketch lines;
  • using only the Python interpreter as the verifier, which tests the masking hypothesis directly;
  • using one strict compiler instead of $K$;
  • dropping the render-back reward.

Every trained variant gets the same unlabeled image pool and the same tuning sweep.

Why not just…?

Why not just transcribe the diagram into PlantUML or JSON with an off-the-shelf tool and hand it to a strong text-only code model? This is the most serious threat for class diagrams, because they are mostly text plus arrows. The evidence against it is indirect.

  • From Diagrams to Code shows DeepSeek-V3 scoring 0% without the image, which only shows the image is necessary.
  • GeoSQA finds failures persist even with expert transcriptions, but that is a different domain.

So we run this pipeline as a baseline: an OCR and layout parser feeding the same 7B code model, and separately a large text model. If it matches our method on the strict languages, the conclusion is that the bottleneck is transcription, not reasoning, and the contribution shrinks to "train a better transcriber." We would report that outcome as it stands.

Why not just generate Python and then translate it into the target language? That would work if the gap were about language knowledge. If the masking hypothesis is right, it should fail in a specific way: the Python draft already has the misread visibility and type information baked in, and translation cannot recover it. The pivot pipeline is included as a baseline, and the symbol-edit diagnosis predicts exactly where it breaks.

Why not just synthesize millions more pairs, or render diagrams from existing typed code to get free pairs? Rendered-from-code pairs are a real alternative and are cheap. Their limitation is that they cover only the diagram styles the renderer produces. M²EVAL diagrams are human-drawn. MINT-CoT and Explicit Knowledge Transfer also note that synthetic supervision is capped by what the generator or teacher covers. We test this directly by training on an equal-compute set of render-from-code pairs and comparing on M²EVAL's human-drawn diagrams. If those pairs win, the render-back reward is better used as a data generator than as a reward.

Decisive Experiments

The main evaluation is per-language Pass@1 on M²EVAL. We report the average, the worst language, and the gap between Python and the strict languages, against M²-CODER's 25.3% average. The symbol-edit diagnosis comes before training and is reported in its own right.

The single most decisive check is the Python-only-verifier ablation. If training with only the Python interpreter as the verifier fails to fix strict-language performance, while strict-compiler training improves every language including Python, that supports the claim that strict compilers carry perception signal. If the two verifiers perform alike, the claim is wrong.

Secondary checks:

  • How often accepted sketches are wrong, measured by human audit of 200 sketches, which tests whether the multi-compiler check filters out sketches that only look correct.
  • Transfer to the C++ and Java problems in M²EVAL that were held out of the $K$ training languages.

As an exploratory probe outside the method's intended scope, we run the Python subset of MMCode, where GPT-4V reports 19.4% Pass@1. We expect little gain there, and a gain would be a surprise worth explaining.