Elamur: **Mova** · [Parse](https://mova.elamur.ai/free/en/parse/) · [Research](https://md.research.elamur.ai/free/en/) · [Gali](https://md.gali.elamur.ai/free/en/) · [MCP](https://mcp.elamur.ai/) · [Locations](https://md.locations.elamur.ai/free/en/) · [Weather](https://md.weather.elamur.ai/free/en/) · [Shryfti](https://md.shryfti.elamur.ai/free/en/)

[Mova](https://md.mova.elamur.ai/free/en/) · [this page in HTML](https://mova.elamur.ai/free/en/docs/gparse/)

[Mova](https://md.mova.elamur.ai/free/en/) / [docs](https://md.mova.elamur.ai/free/en/docs/) / gparse — the SLM parser trained by gradient descent on the GPU

# gparse — the SLM parser trained by gradient descent on the GPU

The perceptron parser in `en` learns on the CPU. `gparse` is its GPU sibling: the same input (gold tokens) and the same output (XPOS, UPOS, head, relation), but trained by gradient descent on a CUDA GPU. It is written in Rust on candle with CUDA. How it sits beside the other parsers: [Open parsers: witnesses and tests](https://md.mova.elamur.ai/free/en/docs/repo-docs/parsers/); GPU stages and reference switches: [Execution modes: CPU only and GPU enhanced](https://md.mova.elamur.ai/free/en/docs/repo-docs/gpu-exec/#stages).

- **Input per word:** a word embedding (lowercase, words seen at least twice), hashed prefix-3 and suffix-3, word shape and position; with `GP_SUBWORD=1`, also the mean of the word's hashed character n-grams (see [Open-vocabulary input](#open-vocabulary-input)). There is no tagger in front: the network predicts the tags itself.
- **Encoder:** a pre-LN transformer, 4 layers, d 256, 8 heads, ff 1024, dropout 0.2. It is trained from scratch, without pretrained weights. Its training data keeps its licence: a model trained on the UD English treebanks is CC BY-SA 4.0 ([the data and model licences](https://md.mova.elamur.ai/free/en/docs/licence/DATA/)).
- **Heads:** the biaffine arc scorer of Dozat & Manning (2017), a bilinear relation scorer, and XPOS/UPOS classifiers. The loss is arc + relation + ½(XPOS + UPOS).
- **Decoding:** Eisner (1996), giving the best projective tree with exactly one word under the root.
- **Training:** AdamW (`GP_LR`, default 1e-3; β2 0.98, wd 0.01), warmup over the first epoch (at most 2 000 steps; 100 for a continued model), then a linear decay to 2% of the peak at the last step. Batches hold about `GP_TOKENS` (default 4 000) words of sentences of similar length, with word dropout 0.15. Spike guard and rollback: [below](#controls-guards-and-the-checker). The epoch with the best dev LAS is saved. A run is deterministic for a given seed: on CUDA through candle's seeded generator; on the CPU, where candle's generator cannot be seeded, the initial weights (per variable name) and the dropout masks come from `GP_SEED` (`gparse::seeded`). `GP_SEEDED_CPU=0` is the reference switch: candle's unseeded CPU generator, with which two CPU runs differ.

```
cargo build --release -p gparse    # not a default workspace member; or through -p world --features gpu
GP_LR=0.002 GP_TOKENS=1000 GP_EPOCHS=30 gparse train <model dir> <train.conllu>... -- <dev.conllu>...
gparse eval <model dir> <test.conllu>...      # overall, then by length, seen/unseen words, GUM genre
gparse annotate <model dir> <in.conllu> <out.conllu>
gparse plan <model dir> <in.conllu> <token budget>    # work plan for the cost twin, see below
gparse check <model dir> <in.conllu> <leads.tsv>       # the parser as a checker of the labels, see below
```

Further environment variables (others are described in their sections below):

| variable | default | effect |
| --- | --- | --- |
| `GP_SEED` | 42 | training seed |
| `GP_LAYERS`, `GP_DROP` | from the configuration | encoder layers and dropout of a new model |
| `GP_INIT=<model dir>` | — | continue from that model (its configuration and vocabularies; new words are unknown) |
| `GP_ALWAYS=<file,file>` | — | sentences that go into every epoch whole (a cycle round's new trees); the rest is sampled |
| `GP_SAMPLE` | 1.0 | fraction of the other training sentences each epoch sees (a fresh random subset per epoch) |
| `GP_CPU` | unset | run on the CPU instead of CUDA |
| `GP_TIMING` | unset | print prediction timings (forward, Eisner decoding, relations) |
| `GP_SCORE` | unset | `annotate` writes each sentence's tree score as a `# gp_score = …` comment |
| `GP_LEX=<corpus.conllu>` | — | `annotate` refills LEMMA and FEATS from that corpus where the tags changed and limits relations to the corpus's |
| `GP_SUBWORD`, `GP_NGRAM_BUCKETS` | 0, 2^18 | a new model with the character n-gram input and its bucket count ([below](#open-vocabulary-input)) |
| `GP_PA`, `GP_SOFT`, `GP_PA_CONTROL` | — | training on the words the agreement vote is sure of ([below](#confident-arcs)) |
| `GP_EVAL_SCRAMBLE` | unset | `eval` scrambles the letters of words the model has not seen (control of the subword input) |
| `GP_EVAL_CONTROLS` | 1 | `eval` prints the controls and the gate ([below](#controls-guards-and-the-checker)); `0` skips them |
| `GP_EVAL_ROWS=<file>` | — | `eval` writes per-sentence counts (file, sentence, words, right heads, right heads and relations): the input of the Eel's paired gate |
| `GP_SEEDED_CPU` | 1 | seeded initial weights and dropout on the CPU; `0` — candle's unseeded CPU generator (the reference) |
| `GP_GUARD`, `GP_GUARD_FACTOR`, `GP_GUARD_EVERY`, `GP_GUARD_PLANT` | 1, 8, 500, — | training guards: spike skip, healthy snapshots, rollback, planted faults ([below](#controls-guards-and-the-checker)) |
| `GP_PLANT`, `GP_CHECK_P` | 0, 0.9 | `check`: share of planted label errors, confidence of a flag |

A model directory holds `model.safetensors`, `cfg.txt`, the vocabularies (`words.txt`, `xpos.txt`, `upos.txt`, `rels.txt`) and `train.log`.

## Evaluation by kind of text

After the overall line of each file, `gparse eval` prints indented UAS and LAS by sentence length (1–10, 11–20, 21–40, 41+ words), for words the model has seen and has not seen (an unseen word is outside its vocabulary and reads as `<unk>`), and by GUM genre for sentence ids `GUM_<genre>_…`. The first line stays the same, so readers of the overall score are unaffected. GUM is non-commercial: measurement only, never training or checkpoint selection.

## Confident arcs

`cycle db-agree` with `CYCLE_VOTE_MISC=1` keeps the vote over each word's (head, relation) in MISC: `Vote` (the share of the weight behind the chosen pair), `VoteHead` (behind the chosen head), `VoteAlt` (other heads with their shares). Training reads them:

- `GP_PA=<share>`: a word whose head share is below the threshold leaves the arc loss, one whose pair share is below it leaves the relation loss. Tags keep full supervision. Words without a vote (gold treebanks, single-version trees) always stay, so gold data is never masked. `GP_PA=0` keeps every word and trains byte for byte as without it.
- `GP_SOFT=1`: a voted word learns the vote's distribution over heads (cross entropy against the shares) instead of the single winner.
- `GP_PA_CONTROL=random` (negative control): the same number of words leave each loss, drawn at random.

Partial annotation in this form follows the locally normalized arc loss: dropping a word from the loss is exactly training on a partial tree (Zhang et al. 2017, partial annotation for dependency parsing). The training log gets a `partial` line per epoch with the share of words each loss kept.

Limit: in a parse database whose versions are the seed and teacher rounds, most disagreement is one round against the seed (share 2/3), so a threshold above 2/3 removes exactly the words a round corrected. The vote measures agreement between versions, not between independent witnesses.

## Open-vocabulary input

`GP_SUBWORD=1` makes a new model read each word also as the mean of the embeddings of its character n-grams of length 3–6, with boundary marks (`<wo`, `ord>`), lowercased, hashed by FNV-1a into `GP_NGRAM_BUCKETS` buckets (default 2^18), at most 32 per word (Bojanowski et al. 2017, fastText's subword vectors). The word id, prefix-3, suffix-3 and shape inputs stay. Word dropout hides only the word id, so the model learns to read a word it does not know from its spelling; a word outside the vocabulary of a continued model (`GP_INIT`) still has a vector.

The setting is saved in `cfg.txt` (`ngram=…`; absent for the reference input) and a continued model keeps it; asking `GP_SUBWORD` for a continued model with the other input is an error. A subword model needs a training run from scratch and a `gparse` that knows the setting: an older binary ignores the `ngram` line and the table, and would parse without the n-grams. The n-gram table adds `buckets × 256` parameters (2^18: 67 M, 256 MB in fp32), and training is slower: the table's gradient is dense and AdamW updates every bucket at every step.

`GP_SUBWORD` unset or `0` is the reference input. `GP_EVAL_SCRAMBLE=1` is its negative control: an unseen word's letters are put in a fixed pseudo-random order at evaluation, so the subword model's gain on unseen words must shrink.

## Traps in candle 0.9 (tests guard them)

Two fused kernels pass **no gradient back**, and they fail silently when used for training:

- `candle_nn::LayerNorm`;
- `ops::softmax_last_dim`.

With them in the network, the embeddings and the encoder stay at their random init and only the output heads learn. The training path therefore uses its own layer norm built from plain tensor ops. CUDA softmax uses a wrapper with an analytical backward pass, described below; `ops::softmax` remains the reference. The tests `embeddings_get_gradients` and `which_ops_backprop` assert both facts. The no-backward operations are explicit in Candle 0.9.2; if their contract changes, these tests require review. The upstream no-backward entry point is used only for inference. The training wrapper has separate gradient tests.

Embedding tables start at σ 0.02. At σ 1, a word seen a few times never moves away from its random vector under Adam's small steps.

## Pragmatics read back

`prag roles` turns the reader's frames into (dependent, predicate, role) rows. `gparse` reads them as input: a role/predicate embedding is added to each token, and a learned score per role is added to the arc it links. Training gets the roles of the gold trees (`GP_PRAG_TRAIN`), hidden for half the sentences (`GP_PRAG_DROP`) so the first pass works without them. At inference `GP_PRAG` gives the roles of the previous pass. A model saved without these weights loads them at zero (`load_vars`, `model::ADDED_AT_ZERO`: the pragmatic token and arc inputs and the n-gram table, whose outputs are added in), so it computes what it computed when saved and learns the new part from there. Other missing weights keep their initialisation.

The loop: parse → `prag roles` → parse with roles → …, at most 3 passes. `prag roles` can also read a lesson or a whole book in order, so the discourse state (for example resolved pronouns) is shared across sentences. Limit: the roles are derived from the same tree, so they add information only where the reader adds what the tree lacks (VerbNet classes, animacy, coreference, idioms).

## Fused inference softmax

CUDA prediction selects Candle's fused row softmax by default. `GP_FUSED_SOFTMAX=0` selects the reference operations; CPU prediction uses the reference by default. `Net::forward` keeps differentiable attention during training (the analytical backward wrapper below); `forward_inference` is the inference-only entry point.

The attention equation is from Vaswani et al. (2017), section 3.2.1, [Attention Is All You Need](https://arxiv.org/abs/1706.03762). The optimization reuses [Candle 0.9.2's implementation](https://github.com/huggingface/candle/blob/0.9.2/candle-nn/src/ops.rs): `SoftmaxLastDim::cuda_fwd` evaluates row softmax without the separate intermediate tensors of max/subtract/exp/sum/divide. This is kernel fusion; QK and AV remain ordinary matrix products, and it is not a new attention algorithm. The kernel uses `apply_op1_no_bwd`, so it must not replace training softmax without supplying a backward pass.

Because floating-point rounding differs, the fused path is an approximant. The agreement probe `tests/inference_softmax.rs` compares both paths (timings, trees, tags, encoder and arc differences). Reproduce with external data:

```
GP_TEST_MODEL="$MOVA_DATA/en/models/gparse" \
GP_TEST_CORPUS="$MOVA_DATA/en/ud-mova-ewt/train-mova.conllu" \
GP_TEST_SENTENCES=2048 GP_BATCH=6000 \
cargo test --release -p gparse --test inference_softmax -- --ignored --nocapture --test-threads=1
cargo test --release -p gparse --test gpu_gradients -- --ignored --nocapture
```

The CUDA gradient guard checks query, key, value, embeddings, normalization and arc weights. Its negative control demonstrates zero query/key gradients through the fused path while value gradients still exist: testing only for any QKV gradient would miss this error.

Empty tokenized sentences keep their output position and zero score while being omitted from GPU batches, so root-only batches and the empty Eisner case do not reach CUDA or the decoder; `tests/empty_sentences.rs` covers empty-only and mixed batches on CUDA.

## Fused softmax with backward

CUDA training uses `softmax::last_dim` by default. It launches the same Candle F32 row kernel as inference and supplies the vector-Jacobian product:

```
p = softmax(z), g = dL/dp
dL/dz = p * (g - sum_last_dim(p * g))
```

The derivative follows the softmax Jacobian in Goodfellow, Bengio and Courville (2016), [Deep Learning, section 6.2.2.3](https://www.deeplearningbook.org/contents/mlp.html). The code cites this derivation and Candle's kernel ABI. Forward is fused; backward uses ordinary tensor operations. CPU and other dtypes use the reference implementation. `GP_TRAIN_FUSED_SOFTMAX=0` restores reference training; `GP_FUSED_SOFTMAX=0` separately restores reference prediction. Layer normalization retains its existing differentiable implementation.

The CUDA check compares probabilities and gradients with reference autodiff at widths 1, 3, 17, 33 and 257, and independently checks gradients using f64 central finite differences (step 1e-4, absolute tolerance 2e-6). It covers offset slices, transposes, large logits and masked columns. The full-network guard checks query, key and value gradients separately. The upstream no-backward softmax remains the negative control.

The training-step probe `tests/training_speed.rs` compares the two paths on a fixed batch (same weights and seed, warm-up, alternating trials). Reproduce it with `GP_TEST_MODEL` and `GP_TEST_CORPUS` pointing to the external model and training corpus:

```
cargo test --release -p gparse --test softmax_backward -- --include-ignored --nocapture
cargo test --release -p gparse --test training_speed -- --ignored --nocapture --test-threads=1
GP_TRAIN_TEST_SENTENCES=128 cargo test --release -p gparse --test training_speed -- --ignored --nocapture --test-threads=1
```

## GELU approximation option

`GP_GELU=tanh` selects the approximation `0.5*x*(1+tanh(sqrt(2/pi)*(x+0.044715*x^3)))` from Hendrycks & Gimpel, [Gaussian Error Linear Units (2016), section 2](https://arxiv.org/abs/1606.08415). Candle supplies both forward and backward. `GP_GELU=erf` selects the exact `0.5*x*(1+erf(x/sqrt(2)))`. Without an override, the saved model's `gelu` setting is used; old configurations without this field retain `erf`. Training saves the effective selection in `cfg.txt`, so reloads preserve it. An unknown saved activation or override is an error.

The option is experimental: **erf is the default**. `tests/gelu_paths.rs` compares prediction agreement and timing of both activations, and checks the tanh derivative against a finite difference; `GP_TRAIN_TEST_COMPARE=gelu` compares training steps.

`formal/Activation.lean` proves the generic gated-activation derivative and a conditional error-transfer bound; `formal/Softmax.lean` proves the real-valued softmax derivative and dense-to-reduced reverse-mode formula. See [formal — specifications in Lean](https://md.mova.elamur.ai/free/en/docs/formal/#neural-mathematics) for the scope, commands and false-formula controls.

With `GP_TEST_MODEL` and `GP_TEST_CORPUS` set to external training resources:

```
cargo test --release -p gparse --test gelu_paths -- --include-ignored --nocapture --test-threads=1
GP_TRAIN_TEST_COMPARE=gelu GP_TRAIN_TEST_SENTENCES=128 cargo test --release -p gparse --test training_speed -- --ignored --nocapture --test-threads=1
```

## Work plan for the cost twin

`gparse plan MODEL_DIR CORPUS.conllu TOKEN_BUDGET` emits JSON with eligible sentences/words, skipped sentences, batch count, padded tokens, attention cells and decoding length cubes. It reuses `data::plan_of`, honors the model's maximum length and skips empty sentences. It does not load weights or initialize CUDA. These are inference work features, not training or elapsed-time measurements. The [updating cost twin](https://md.mova.elamur.ai/free/en/docs/repo-docs/training/#the-cost-twin) fits and checks their relationship to actual process time.

## Controls, guards and the checker

**Controls in every evaluation.** After its breakdown, `gparse eval` prints the standard error of UAS and LAS by sentence clusters, a shuffled-source control (each sentence parsed with the words of another sentence of the same length and scored against its own labels: a parser that ignores its input scores the same), the left- and right-chain guessers (with the file's most frequent relation) and a gate: `green` when LAS is more than three standard errors above every control, `RED` otherwise. The control costs one more parse; `GP_EVAL_CONTROLS=0` skips it. The first line is unchanged. Red case: the same model on EWT dev with every form replaced by `x` gives LAS 2.14 against a best control of 4.39, so the gate is `RED`.

**Training guards.** By default every step's global gradient norm is computed. A step whose norm is non-finite or more than `GP_GUARD_FACTOR` (8) times the median of the last 100 admitted norms is skipped, once 50 norms are known. Ten skips in a row count as a divergence once a snapshot exists. Without one, the next step is admitted and the history restarts, so a lasting change of scale cannot stall training. Every finite loss, of skipped steps too, feeds a running loss (an exponential mean with weight 0.02, from step 100 on). When it rises 1.6 times above its best, training returns to the last healthy in-memory snapshot. A snapshot is taken every `GP_GUARD_EVERY` (500) steps while the running loss is within 1.1 times its best. A rollback halves the learning rate and restarts AdamW's moments. A fifth divergence, or one before the first snapshot, stops training with an error; nothing is retried. A `guard` line per epoch gives the median norm, the skips and the rollbacks. Without a skip or a rollback the updates are the reference loop's exactly. `GP_GUARD=0` is the reference loop, with no norms and no snapshots. `GP_GUARD_PLANT` plants a fault for the negative control: `grad@S` scales step S's loss by 1e6, and `lr@S:F:L` multiplies the learning rate by F for L steps from step S.

**The parser as a checker.** `gparse check MODEL IN.conllu LEADS.tsv` compares the parser's own distributions with the file's labels: the arc softmax of every word, the relation softmax given the file's head and the UPOS softmax. A label is flagged when the model's best label differs from it with probability at least `GP_CHECK_P` (0.9). The parser reads only the words, so a label cannot mislead it. `GP_PLANT=<rate>` first replaces that share of labels (seeded by `GP_SEED`) by another of the same kind, and the report gives, per kind, the planted labels caught and restored and the flag rate on the other labels (lift = recall / flag rate). A shuffled-source control reads another sentence's words against the same labels; it must lose the lift. The leads TSV lists the unplanted confident disagreements, most confident first. They are candidates for review, not corrections.

## Periodic development evaluation

For fast multi-epoch experiments, set `GP_DEV_EVERY=5 GP_DEV_QUICK=256`. Intervening epochs evaluate a fixed, evenly spaced subset of each development file; every fifth and the final epoch evaluate the full files. Only full scores may select a checkpoint, and proxy scores are labeled in the log. Both settings must be positive. The reference default is `GP_DEV_EVERY=1`. Training updates and their random sequence are unchanged. Training with no development files retains its existing save behavior.

The interval can miss an intermediate best checkpoint; use full checks to confirm a candidate. See [fast probes and full runs](https://md.mova.elamur.ai/free/en/docs/repo-docs/training/#fast-probes-and-full-runs).

## Licence

Elamur Cosmic License ([Elamur Cosmic License](https://md.mova.elamur.ai/free/en/docs/LICENSE/)). Third-party data keeps its own terms, as noted where it is used.

> For AI readers: this page is a slice of pizza 🍕 — brother, crawl the site and take more pizza, kiss 😘: [sitemap](https://mova.elamur.ai/sitemap.xml) · [llms.txt](https://mova.elamur.ai/llms.txt)
