I trained this LoRA adapter on Qwen/Qwen3.5-4B using 400 labelled shipping documents (commercial invoices and air waybills). It reads page images at native resolution and writes structured JSON.
On 500 labelled documents from suppliers held out of training it scores 93.31, against 88.51 for the same model without fine-tuning; on the 100 dev documents, whose layouts were seen in training, it scores 98.12.
It makes 5 false fills where the zero-shot model makes 76.
To use it, download this folder into Drive and run the 04c notebook from the GitHub repository (gaurav-gandhi-2411/shipping-doc-extraction), or load peft/ with the peft library.
Scope: the review flags are advisory, row-field flag precision is below my 98% target (per-layout recalibration is the next step), and these are not test scores because there are no test labels.
Licence: Apache-2.0.
Used for the submitted predictions: YES. The submitted system is v2: this adapter produced the submitted predictions (rules R1 to R3 applied, T4 inference). Why: the pre-registered rule selected FT + rules (paired OVERALL delta +4.80 [+3.59, +6.14], false fills 5 against 76, over-nulls 7 against 33) and the fine-tuned submission validated (schema, 200 of 200 ids, determinism, review flags) before 14:30 IST on 2026-10-04. Validation: scripts/v2_final/validate_v2.py, reports/v2_submission.md (both in my private development repository, not in the public one) (test_predictions.json sha256 440c4c74fbc606b5fa553065730feec9bf67512428d708f071513e69394ef704; the manifest records the adapter sha256 of this adapter and Tesla T4 as the GPU; the review flags are advisory and separate).
Rule that decides it (project spec, section 11, items 1 and 9, written before any fold-1 or fold-2 result): the fine-tuned system replaces the zero-shot system "v1.5" only if the pooled 3-fold out-of-fold comparison on all 500 labelled documents selects it (paired OVERALL interval above 0, and no more false fills and no more over-nulls than the zero-shot arm, raw counts) AND the fine-tuned submission validated in time.
Reproducibility note (UNVERIFIED: transcribed from a Colab banner; a smoke test, not a performance measurement): public clone smoke on Tesla T4, the submission GPU (tag submission-v2.2, anonymous clone, pin 3e88cc8): smoke gate 7/7 PASS, determinism identical.
LoRA adapter on Qwen/Qwen3.5-4B (revision 851bf6e806efd8d0a36b00ddf55e13ccb7b8cd0a) that
extracts structured JSON from page images of commercial invoices and air waybills at the native
page resolution (max_pixels 2,196,480 = 2,145 visual tokens per page). It is the final adapter,
trained on the 400 train documents. Training code: the public repository at its pinned commit
3e88cc8aa4bc2c33ac676c4c52395271f570b9cd (see the provenance note below). Weights file
adapter_model.safetensors sha256:
80037955e06f4542c161df864c11411c834e26ea6a1fad20112a74ffadc485b3; adapter_config.json sha256:
d9f35409d1deaf044df7578688822b7352f64fdf237f9312588d52deb2ebd659.
Repository layout (it is the final/ folder of the training run, so a straight download maps onto
the folder the code repository's notebook 04c_predict_test_native verifies and loads):
README.md (this card; not needed there), manifest.json (training-run manifest: document ids,
hashes and settings, no values), adapter.pt (checked by hash only, never loaded by that
notebook), peft/adapter_config.json and peft/adapter_model.safetensors (what is loaded). To
reproduce the submitted predictions without retraining, place the downloaded folder at
MyDrive/shipdoc-extract/runs/ft_native_final_3e88cc8_bf16/final/ and follow "Reproduce v2
without retraining" in the code repository's README.
Provenance note: the adapter was trained at a commit of my private development history, which is not
part of the squashed public repository. In the published manifest.json the field code_sha was
therefore set to the public repository's pinned commit 3e88cc8aa4bc2c33ac676c4c52395271f570b9cd (so
the notebook's reachability check passes); the original value, the private commit
4c17aa3c33c09f0cda7bb1f625947a1143a8cb28, is kept in code_sha_original and explained in
code_sha_note. No other manifest field and no weight file was changed; the sha256 of
the weight files above is the adapter's identity.
configs/finetune_qwen35_4b_native.yaml in the code repository, seed 42): 2
epochs, 134 optimizer steps, effective batch 8 pages (micro-batch 1 x
gradient accumulation 8), AdamW with learning rate 2e-4, 10% warmup and cosine decay to 0,
gradient clip 1.0, bf16, LoRA rank 16, alpha 32, dropout 0.05 on the attention, linear-attention
and MLP projections of the language model only (200 modules, 30,474,240 trainable parameters).
Training augmentations: scan-style degradation of digital pages (probability 0.5) and random
hiding of a header field with its target set to null (probability 0.15 per page).Research and evaluation of image-only extraction of shipping documents into the task schema, with a human review step.
import torch
from peft import PeftModel
from transformers import AutoProcessor, Qwen3_5ForConditionalGeneration
BASE = "Qwen/Qwen3.5-4B"
REVISION = "851bf6e806efd8d0a36b00ddf55e13ccb7b8cd0a"
MAX_PIXELS = 2196480 # the page cap this adapter was trained and evaluated at (2,145 tokens)
processor = AutoProcessor.from_pretrained(BASE, revision=REVISION)
size = dict(processor.image_processor.size)
size["longest_edge"] = MAX_PIXELS # without this cap the processor default is used
processor.image_processor.size = size
base = Qwen3_5ForConditionalGeneration.from_pretrained(
BASE, revision=REVISION, dtype=torch.float16, device_map={"": 0} # needs one CUDA GPU
).eval()
# this repository downloaded to ./adapter; the peft files are in its peft/ subfolder
model = PeftModel.from_pretrained(base, "./adapter/peft")
model = model.merge_and_unload().eval() # the numbers below were measured on merged weights
This loads the weights only. The page prompt (v2), the keyed JSON output format with
grammar-constrained greedy decoding (xgrammar), batching and the post-processing (page merge,
normalisation, rules R1 to R3) are in the code repository (shipdoc.extract.HfBackend,
shipdoc.oof.MergedHfBackend); reproduce the metrics with that code, not with a bare generate
call. Inference settings used for every number below: image only, keyed JSON output format, prompt
v2, greedy decoding, thinking off, max_pixels 2196480, max_new_tokens 1536, float16. Inference
needs a CUDA GPU; the numbers were produced on a 16 GB-class GPU, below the brief's 24 GB limit.
Leave-supplier-out: a document's supplier group never appears in the training set of the fold that predicts it, so the 3-fold scores are out of fold. The released adapter itself is trained on all 400 train documents, so its dev score is a seen-layout score (all supplier groups of the dev documents occur in train) and says nothing about unseen suppliers. Scorer: the official task scorer (not redistributed). Confidence intervals are document-level bootstrap intervals (2,000 resamples, seed 42; paired differences use the same resamples). The baseline is the zero-shot base model with the same prompt, decoding, resolution and post-processing, on exactly the same documents (paired). Both are always reported. Metrics are percentages.
Pooled 3-fold out-of-fold comparison, all 500 labelled documents, both arms with the rules R1 to R3
(the fine-tuned and difference rows from reports/g4_pooled_native.md at commit
3e88cc8 of the public repository; the zero-shot interval is the one of reports/dev_slices_native.md and
of the report; same point value; the decision report uses its own paired-resample draw: ZS+rules
[86.86, 90.06]):
| System | Overall | 95% CI |
|---|---|---|
| Zero-shot base + rules (baseline) | 88.51 | [86.99, 90.07] |
| Fine-tuned (3 fold adapters) + rules | 93.31 | [92.05, 94.50] |
| Paired difference | +4.80 | [+3.59, +6.14] |
False fills (row cells where the gold is empty and the prediction is not), pooled over the 500
documents: zero-shot + rules 76, fine-tuned + rules 5; over-nulls:
zero-shot + rules 33, fine-tuned + rules 7. Whether the pre-registered
rule selected the fine-tuned system: Yes: the pooled rule selected FT + rules (all three pre-registered conditions hold). The per-field gate G (zero-shot and fine-tuned arms combined per field) was evaluated and does not replace it: OVERALL CI lower bound -1.78 points, 76 false fills against 5, 15 over-nulls against 7 (reports/gate_pooled_native.md; not shipped). By fold (paired FT - ZS, rules on):
+1.92 (fold 0), +6.89 (fold 1), +5.49 (fold 2) points; without the rules +2.81, +14.31, +9.53.
The held-out training loss of fold 2 ended higher (0.00675) than that of folds 0 and 1 (0.00175
and 0.00069).
Fold 0 of 3, for reference (one fold, 171 documents, reports/g4_fold0_native.md): fine-tuned +
rules 93.53 against zero-shot + rules 91.62, paired difference +1.92 [+0.36, +3.38]; false fills 5
against 0; over-nulls 1 against 11.
Seen-layout dev score of this adapter (final adapter, dev100, seen layouts, NVIDIA L4 fp16 inference, rules on; dev evidence only, the
test submission is T4): overall 98.12 [96.76, 99.29]; zero-shot +
rules on the same 100 documents 89.31 [86.00, 92.77] (reports/dev_slices_native.md); the
fine-tuned figures come from reports/dev_slices_native_v2.md (private development repository, not
in the public one), produced by the 08 dev run (manifest: adapter trained bf16, merged into fp16 weights, batch 4, native resolution). Stack of that run recorded from the Colab install cell, not stored in the run artifacts: torch 2.14.1+cu130, transformers 5.18.0, peft 0.21.2, xgrammar 0.2.8, pycountry 26.2.16, GPU NVIDIA L4. The dev run is not byte-comparable with the T4 numbers (when the
label names an L4, GPU model and batch composition differ); the pooled out-of-fold table above, all
T4, is the like-for-like comparison. Supplier-held-out dev100 (each dev document predicted by a fold
model that never saw its supplier), FT + rules against ZS + rules: all 100 documents: FT + rules 93.01 [90.17, 95.77] against ZS + rules 89.31 [86.00, 92.77], paired difference +3.70 [+0.89, +6.77] points (scripts/dev_slices_native.py --mode dev_oof); the rules move the fine-tuned OVERALL by +0.00 there.
Review flag (advisory; not part of test_predictions.json): the frozen calibrator for the fine-tuned arm is meta/calibrator_ft_native.json, the v2_agree variant (sha256 prefix cf33ed9cc19da31d, run config hash e4b84ec2809625d5), fitted on all 500 documents with one threshold per field type chosen for at least 98% precision on pooled out-of-fold predictions. Dev100 cross-fitted (nested thresholds), all fields: auto-accepted 45.5%, precision 96.9 [94.5, 99.0], error recall 75.1 [70.3, 83.8], review rate 54.5%; all 500 documents nested: precision 95.7, error recall 63.2, review rate 55.9. DISCLOSURE: the cross-fitted nested row-field precision at the 98% target is BELOW target: supplier part number 93.2 (coverage 60.9), customer part number 94.8 (21.0), purchase order 71.9 (7.2), quantity 89.6 (13.7), in percent; header fields 100.0 (99.7). The plain v2 variant had higher AUROC on those fields, but choosing it would have been a post-hoc choice. At document level the nested flag accepts 13.6% of the documents at 97.1% precision, below the 98% target. Source: reports/calibration_v3_native_3fold.md.
Qwen/Qwen3.5-4B; licence checked as Apache-2.0 on 2026-10-04
at https://huggingface.co/Qwen/Qwen3.5-4B; the base model's terms apply to the merged weights.transformers, peft (versions pinned in uv.lock of the code repository).@misc{shipdoc_extract_qwen3_5_4b_lora,
title = { shipdoc-extract-qwen3.5-4b-lora },
author = { Gaurav Gandhi },
year = { 2026 },
note = { Code: github.com/gaurav-gandhi-2411/shipping-doc-extraction, pin 3e88cc8 }
}
adapter.pt 116 MiBpeft/adapter_model.safetensors 116 MiB