Copying From Flowcharts When the Arrows Might Be Wrong

Turning a flowchart image into working code takes two different skills. The model has to recover the program's structure: which box runs after which, and which branch loops back. It also has to reproduce identifiers and constants exactly as they are written in the boxes. Current vision-language models handle the second skill much better than the first. They read node text reasonably well, but they misjudge arrows and their direction, and they do worse on flowcharts than on equivalent text descriptions. Older structured systems get the structure right only because they assume the flowchart graph is handed to them clean. Both habits break in the realistic setting, where the graph is extracted from an image and some arrows are flipped or missing.

We propose a flowchart-to-code generator that treats each extracted arrow's direction as uncertain evidence rather than as fact. A bidirectional graph encoder sends information along each edge forward and backward in two separate channels. Each edge's contribution to the two channels is weighted by how confident the image parser is about that arrow's direction. The decoder is a pointer-generator, meaning it can either write a token from its vocabulary or copy one verbatim from the input. It copies identifiers from two places: text anchored to graph nodes, and a flat reading-order stream of every token the text recognizer found. A learned weight decides, at each decoding step, how much to trust the graph-anchored copy versus the flat one. The claim is that this per-edge, per-step weighting of uncertain evidence makes generation degrade gracefully as structure extraction gets worse. The proposal is built so that this claim can fail cleanly.

Core Claim

The research file shows three directions that each solve half of this problem. Bidirectional graph encoding keeps separate forward and reverse message passing over a directed graph, so each node sees both its causes and its consequences without erasing direction. Systems of this kind include the forward/backward aggregators of SQL-to-Text Generation with Graph-to-Sequence Model and the top-down/bottom-up encoders of Enhancing AMR-to-Text Generation with Dual Graph Representations. The flowchart system of Code Generation From Flowcharts with Texts applies the same forward/reverse idea to flowcharts. All three assume the graph is given exactly. Pointer-generators, introduced for summarization in Get To The Point, copy rare tokens but point over a single stream. Dynamic modality weighting computes instance-dependent trust weights. Examples include channel gating across RGB, depth and infrared in A Dataset and Benchmark for Large-Scale Multi-Modal Face Anti-Spoofing and category-dependent text/image gates in Price Suggestion for Online Second-hand Items. These weights are coarse: one scalar per modality, feeding a classifier or a fused feature vector, never a copy distribution.

The obvious combination would add a gate per modality to a pointer-generator. That is weak for a specific reason. A single attention softmax over concatenated sources already assigns mass to each source in a query-dependent way, so a per-source scalar gate is largely redundant.

What attention over a clean graph cannot do is discount structure that is wrong. Our version therefore moves the dynamic weight to the place where unreliability actually enters, which is individual edges. Let $c_{uv} \in [0,1]$ be the parser's probability that the arrow between boxes $u$ and $v$ points from $u$ to $v$. The message from $u$ into node $v$'s forward channel is scaled by $c_{uv}$, and into $v$'s reverse channel by $1-c_{uv}$. This construction reduces to the hard forward/reverse encoders of prior work when $c_{uv}\in\{0,1\}$, and to a symmetrized graph when $c_{uv}=0.5$.

On the decoding side, the copy distribution mixes two pointer streams:

$$P(w) = p_{gen} P_{vocab}(w) + (1-p_{gen})\Big[\beta_t \sum_{i:\,w_i=w} a_i^t + (1-\beta_t)\sum_{j:\,o_j=w} b_j^t\Big]$$

Here $p_{gen}$ is the usual generate-versus-copy probability. $a_i^t$ is attention at step $t$ over tokens $w_i$ anchored to graph nodes. $b_j^t$ is attention over the flat recognized-text stream $o_j$. $\beta_t$ is a step-dependent source weight computed from the decoder state, and from summary statistics of the edge confidences around the currently attended nodes. The two terms differ in two ways. Graph-anchored copies carry structural context from the bidirectional encoder. Flat-stream copies survive when the parser dropped or merged a box.

Research Program

The system has three parts.

The first is an image-to-graph parser. It produces boxes, recognized text with per-token confidence, and undirected edge proposals, each with a direction probability $c_{uv}$ from a small classifier on the arrowhead crop. We train it on the simulated subset of FlowLearn, whose 10,000 Mermaid diagrams come with ground-truth boxes and code. The parser is deliberately ordinary. The research question is what the generator does with its uncertainty, not how to build a better parser.

The second is the soft-direction bidirectional encoder: a gated graph network with separate forward and reverse parameter sets, following the dual design of Ribeiro et al., run for a small number of hops. SQL-to-Text Generation found performance peaked at six hops and declined beyond that. We also add a learned gate that weights each node's forward state, reverse state and text-only embedding per node. This replaces the static concatenation that Ribeiro et al. use.

The third is the two-stream pointer-generator decoder. It emits code tokens directly, rather than the grammar actions of the TranX parser used by Liu et al. That choice matters: the TranX decoder in Code Generation From Flowcharts with Texts has no copy mechanism.

Two known failure modes shape training. Gated fusion tends to collapse onto whichever source learns fastest, here probably the flat text stream. We counter this by randomly dropping each copy stream during training. We also corrupt parser outputs during training by flipping edges and deleting boxes. Without corruption, $\beta_t$ never sees a reason to move. The analysis will check whether $\beta_t$ and the learned node gates track actual corruption. We will correlate them with injected flips, and measure their calibration against whether the copied token was correct.

Baselines

The naive reference point is a text-only sequence-to-sequence model with a standard single-stream pointer over the flat recognized-text sequence, with no graph at all. It measures how much of the task is just "read the boxes in order and copy names."

The strongest structured competitor is the forward/reverse graph attention model of Code Generation From Flowcharts with Texts. On FC2Code it reports 55.68 BLEU, against 44.05 for a Transformer and 51.53 when graph-neighbor information is removed. We rerun it on the same parsed graphs, with directions hard-thresholded at $c_{uv}>0.5$ and the same hop count, training data and tuning budget as our model. Beating it means higher BLEU on FC2Code under clean graphs, or at least not lower. On corrupted graphs, we require a statistically significant gain in execution pass rate on held-out Flow2Code problems.

The strongest end-to-end competitor is a multimodal LLM fine-tuned on the image directly. Flow2Code reports a 103.8% relative boost from supervised fine-tuning of Qwen2-VL-7B. The file reports only relative figures, and we found no reported absolute pass rate on Lacuna for Flow2Code, so we will measure this baseline ourselves. We fine-tune the same model on the same training split, with matched GPU-hours. We also give the same model the parser's serialized graph as Mermaid text alongside the image.

Two ablations isolate the key ingredient. In the first, soft direction weights are replaced by hard thresholded directions. In the second, they are replaced by full symmetrization, which is what Pointer Graph Networks does to its directed pointers during message passing. A third ablation replaces the two-stream copy with one softmax over the concatenated streams.

Why not just…?

Why not just put both copy streams into one attention softmax? This is the most serious threat, because concatenated attention can already express any split between sources. Our bet is that the advantage appears only when the graph is corrupted, since $\beta_t$ is fed edge-confidence statistics that the attention keys do not encode as cleanly. Suppose the single-softmax ablation matches the two-stream decoder across the whole corruption curve, and not just on clean graphs. Then the two-stream decoder is unnecessary, and we will report that the contribution lies entirely in the soft-direction encoder.

Why not just fine-tune a multimodal LLM end to end? Flow2Code shows fine-tuning helps a lot. FlowLearn, however, shows these models fail specifically at link-level and arrow-direction judgments, with only 58% counting accuracy for GPT-4V. That is exactly the information our encoder makes explicit. Matched-compute fine-tuning is a baseline here, not a strawman. If it wins on long, branch-heavy programs, the structured route loses on this task.

Why not just symmetrize uncertain edges, or drop them? Transformers Meet Directed Graphs shows that erasing direction hurts on tasks that depend on execution order, such as directed reachability and sorting networks. Soft direction contains symmetrization as the special case $c_{uv}=0.5$. The ablation will show whether the parser's direction probabilities carry information beyond that case.

Proposed Experiments

The main testbed is Flow2Code. Its diagrams are generated from DOT sources, so ground-truth graphs exist, and we can inject controlled corruption: edge-direction flips at rates from 0 to 30%, box deletion, and recognized-text character noise. We report execution pass rate as a function of each corruption rate. We also report an identifier-fidelity metric: the fraction of variable names and constants in the reference program that appear verbatim in the output.

FC2Code (320 samples) serves as a small transfer test, using BLEU for comparability with the 55.68 figure. FlowLearn's scientific subset serves as a qualitative probe on real, non-synthetic diagrams.

The decisive check is the shape of the corruption curves. The proposal succeeds if three things hold together. First, its pass rate falls measurably more slowly than the hard-direction baseline as flip rate rises. Second, it does not lose on clean graphs. Third, the learned edge weights and $\beta_t$ correlate with injected corruption. If soft direction helps but the two-stream copy does not beat single-softmax copying, we will narrow the claim to the encoder.