12 3 months ago

Reword generic AI patent spec body paragraphs in the voice of an experienced patent attorney (me). Works one paragraph at a time. Fine-tuned on ~20,000 of my body paragraphs. Based from Mistral-Small-24B-Instruct-2501.

ollama run seancrandall/patent-voice

Details

3 months ago

42c6e077b488 · 25GB

llama
·
23.6B
·
Q8_0
Rewrite into author voice. Preserve meaning. Do not add or remove technical content. Preserve all nu
{ "num_ctx": 4096, "num_predict": 2048, "repeat_penalty": 1.1, "temperature": 0.5,

Readme

patent-voice

A voice-transfer rewriter: given a generic, AI-voiced patent paragraph, it rewrites the paragraph in Sean Crandall’s author voice, one paragraph at a time, while preserving all reference numerals and numeric literals.

This is v1, trained via supervised A/B fine-tuning. It is usable but conservative on some paragraphs; see Limitations.

What it does

  • Input: one generic/AI-voiced patent paragraph (as a user message).
  • Output: the same paragraph rewritten in the author’s voice, preserving meaning, technical content, and all numerals (400, 404/406, FIGURE 5, etc.).
  • Scope: patent prose from past filings. It is not a general-purpose stylist; feeding it non-patent text is out of distribution.

The system prompt (the exact one used during SFT) is:

Rewrite into author voice. Preserve meaning. Do not add or remove technical content. Preserve all numeric literals and reference numerals. Do not shorten.

Model lineage

Base model mistralai/Mistral-Small-24B-Instruct-2501 (24B, has a chat template)
Method QLoRA SFT with assistant-only loss masking (learns the generic→voice mapping, not memorization of the prompt)
LoRA rank 16, alpha 32, dropout 0.05
Quantization merged to fp16 → quantized to q8_0 GGUF
GGUF voice_sft.q8_0.gguf (24 GB) at /data/models/services/Trainer/modelgen/voice_sft.q8_0.gguf
ollama tag patent-voice:latest (also seancrandall/patent-voice:latest)

The GGUF bakes the stock Mistral [SYSTEM_PROMPT]…[/SYSTEM_PROMPT] / [INST]…[/INST] chat template. The custom {% generation %} template used during training was for loss-masking only — the rendered text is identical, so inference via this Modelfile is unaffected.

Training data

A/B pairs (generic AI patent paragraph → Sean's edited paragraph) built from voice-editor/training_data/paragraphs-clean.db:

  • 18,792 train pairs / 500 val pairs taken from patents I have written over ~20 years
  • Backed into generic OpenAI via gpt-4o (75%) and local llama3.3:70b (25%)
  • emitted by emit_mixed_dataset.py → train.jsonl + val.jsonl (each record has a messages field: system / user / assistant)

How it was trained

2-GPU QLoRA pipeline

  • train.py SFT_MESSAGES=1 branch → SFTConfig(assistant_only_loss=True, max_length=1536, packing=False) + a custom Mistral training chat template with {% generation %} markers (trl has no Mistral auto-patch; the stock template lacks markers and crashes at init).
  • Run config: podman.sft.env — EPOCHS=3, EVAL_STEPS=200, SAVE_STEPS=200, MAX_SEQ_LEN=1536, LOAD_BEST_MODEL_AT_END=1, BF16=1, NPROC_PER_NODE=2, OUTPUT_DIR=/workspace/weights/voice_sft, SKIP_MERGE=0 (auto merge→GGUF).
  • Hardware: 2× RTX 3090 (48 GB VRAM sharded), 128 GB host RAM.
  • Early stop: training halted at step 2400 (epoch 2.04) when eval_loss reversed (0.3174 @ step 2200 → 0.3201 @ step 2400) and train loss crashed to 0.151 = overfitting onset.
  • Best checkpoint: step 2200 (eval_loss 0.3174, token accuracy 0.912), locked in by LOAD_BEST_MODEL_AT_END=1. Merged manually via MERGE_ONLY=1 + MERGE_ADAPTER_DIR=.../checkpoint-2200 (CPU fp16 merge + convert_hf_to_gguf.py q8_0).

Two infra notes for anyone re-running: (1) keep HUGGINGFACE_HUB_CACHE=/workspace/cache/hub + HF_HUB_OFFLINE=1 — the old root cache layout caused huggingface_hub to re-validate and wipe cached weights on launch. (2) For the one-off merge container, podman run rejects --shm-size together with --ipc=host (compose allows both) — drop --shm-size.

Usage

ollama run seancrandall/patent-voice
# then paste one paragraph as your message; it returns the rewritten paragraph

Or via API:

ollama run seancrandall/patent-voice "$(cat paragraph.txt)"

Inference parameters (from this Modelfile): temperature 0.5, top_p 0.9, repeat_penalty 1.1, num_predict 2048, num_ctx 4096.

Feed one paragraph at a time. The model was trained paragraph-by-paragraph; multi-paragraph batches drift.

Verification

Tested on held-out val paragraphs + figure5.md:

  • Test A (good): “is structured to comprise…may encompass…and additionally” → “includes…may include…as well as”; numerals 400/404/406 preserved. Fluent Sean-style prose.
  • Test B (under-transforms): a near-copy — kept some of the author’s own lexical swaps unlearned (“described herein” → “of the present specification” was not applied).
  • Test C (slight degradation on already-polished prose): dropped leading articles, added “For example/may”, one word error (“computation” → “compute”).

Limitations (v1)

  • Conservative / under-transforms on some paragraphs — it will sometimes return prose close to the input rather than fully rewrite.
  • Can slightly degrade already-polished prose — minor article/diction regressions (see Test C). Best used on rough or generic AI draft text, not on text you’ve already hand-edited.
  • q8_0 quantization — some quality headroom lost vs fp16; acceptable for a rewriter.
  • Narrow domain — patent prose only.

Ideas for v2

Higher LoRA rank, stronger de-voicing of the input side of the pairs, and a higher inference temperature to push it past its conservative tendency.