Lab Workspace

Doc-to-LoRA (Context Internalization)

Doc-to-LoRA (D2L) is context internalization: a hypernetwork generates LoRA adapters from documents, so context is served from the model's weights instead of the prompt. You train the hypernetwork once, then run it per document.

How it works

D2L is built around a hypernetwork — a model that takes a document as input and emits a LoRA adapter as output. Instead of stuffing a document into every prompt, you internalize it once: the hypernetwork generates an adapter that encodes the document, and the runtime serves requests through that adapter. There are two operations:

  • POST /v1/train/d2l — trains the hypernetwork itself (an asynchronous training job).
  • POST /v1/d2l/internalizeruns a trained hypernetwork on one document to produce and register an adapter (a synchronous call, no training job).

This is not classical fine-tuning — there is no LoRA rank or alpha for you to tune; the hypernetwork generates the adapter. For classical parameter-efficient fine-tuning with explicit rank/alpha/target modules, see PEFT Tuning (LoRA).

Supported model families

Familyfamily value
Qwen3 4Bqwen3-4b
Qwen3 0.6Bqwen3-0.6b
Mistral 7Bmistral-7b

Train the hypernetwork

POST/v1/train/d2l

Trains the D2L hypernetwork for a supported model family. The call returns a job id immediately (202 Accepted); training continues asynchronously with checkpoints.

ParameterTypeDescription
familyrequiredstringBase model family — qwen3-4b, qwen3-0.6b, or mistral-7b.
num_examplesintegerNumber of training examples to generate/use from the source documents.
epochsintegerNumber of training epochs.
lrfloatLearning rate.
eval_examplesintegerSize of the held-out eval set scored automatically when the job finishes.
seedintegerSeed for reproducible example generation.
devicestringDevice to train on (e.g. Metal or CUDA hardware available to the runtime).
model_dirstringOptional base-model directory override.
checkpoint_dirstringArtifact destination. Default ~/hybrie-mounts/d2l-artifacts/train-<job_id>/.
curl
curl -X POST http://localhost:8080/v1/train/d2l \
  -H "Content-Type: application/json" \
  -d '{
    "family": "qwen3-4b",
    "num_examples": 200,
    "epochs": 3,
    "lr": 0.0001
  }'

Response (202 Accepted):

json
{
  "job_id": "train-1718102400",
  "checkpoint_dir": "~/hybrie-mounts/d2l-artifacts/train-1718102400/"
}

Job ids follow the train-<timestamp> format. Poll Training Jobs for status and progress points (step, loss, reward). The artifact is a hypernet checkpoint — score it on held-out NIAH with POST /v1/eval/niah.

Internalize a document

POST/v1/d2l/internalize

Runs the hypernetwork on a single document: it generates an adapter encoding the document and registers it with the runtime in one call, ready to serve.

ParameterTypeDescription
document_textrequiredstringThe source document to internalize into the adapter.
namestringHuman-readable name for the resulting adapter.
execution_modestringWhere internalization runs. local.
pinbooleanPin the adapter after registration so it is not evicted — see Hot-swap Inference.
ttl_secondsintegerOptional time-to-live. The adapter auto-expires after this many seconds.
session_keystringOptional session key tying the adapter to a session.
curl
curl -X POST http://localhost:8080/v1/d2l/internalize \
  -H "Content-Type: application/json" \
  -d '{
    "document_text": "Our refund policy: ...",
    "name": "refund-policy",
    "execution_mode": "local",
    "ttl_seconds": 86400
  }'

The response identifies the generated-and-registered artifact:

json
{
  "adapter_id": "d2l-7f3a9c",
  "version": "v1",
  "artifact_dir": "~/hybrie-mounts/d2l-artifacts/d2l-7f3a9c/v1",
  "source_document": "Our refund policy: ...",
  "status": "registered"
}

Internalizing a changed document produces a new immutable version instead of overwriting the old one. The emitted adapter artifacts are PEFT-format, so they can also be scored with POST /v1/eval/adapter and served like any registered adapter.

From the CLI

bash
stimulir lab train d2l --family qwen3-4b --epochs 3

The CLI flag is --examples; it maps to the num_examples field in the request body.

Next