How to use
The model can be used without search like any causal language model. To let it search: open a search block, let it write the query, run your own retriever, insert the result, and let it continue.
import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer repo = "Kausp11/querylm-wiki-360m"tok = AutoTokenizer.from_pretrained(repo)model = AutoModelForCausalLM.from_pretrained(repo, torch_dtype=torch.bfloat16).eval() Q_OPEN, Q_CLOSE = tok.convert_tokens_to_ids("<WEB_QUERY>"), tok.convert_tokens_to_ids("</WEB_QUERY>") def my_search(query: str) -> str: """Replace with your retriever (BM25 index, web search API, ...). Return one short passage.""" return "Canberra is the capital city of Australia. It was founded in 1913 and is the country's largest inland city." @torch.no_grad()def generate(text, max_new_tokens, stop_id=None, allow_search=True): x = tok(text, add_special_tokens=False, return_tensors="pt") kw = {} if allow_search else {"bad_words_ids": [[Q_OPEN]]} out = model.generate(**x, max_new_tokens=max_new_tokens, do_sample=False, eos_token_id=stop_id, pad_token_id=tok.eos_token_id, **kw) return tok.decode(out[0, x.input_ids.shape[1]:], skip_special_tokens=True) # Base model: it continues text. Few-shot examples (with search blocks) set the question-answer format.def shot(q, query, passage, a): return f"Question: {q}\n<WEB_QUERY>{query}</WEB_QUERY> <WEB_RESPONSE>{passage}</WEB_RESPONSE>\nAnswer: {a}\n\n"shots = (shot("Who wrote the novel Pride and Prejudice?", "Pride and Prejudice author", "Pride and Prejudice is an 1813 novel of manners written by Jane Austen.", "Jane Austen") + shot("What is the chemical symbol for gold?", "gold chemical symbol", "Gold is a chemical element with the symbol Au and atomic number 79.", "Au"))prompt = shots + "Question: What is the capital of Australia?\n" query = generate(prompt + "<WEB_QUERY>", max_new_tokens=24, stop_id=Q_CLOSE).strip() # 1) the model writes the queryblock = f"<WEB_QUERY>{query}</WEB_QUERY> <WEB_RESPONSE>{my_search(query)}</WEB_RESPONSE>" # 2) you retrieve and insert the blockanswer = generate(prompt + block + "\nAnswer:", max_new_tokens=16, allow_search=False) # 3) the model continuesprint("query :", query)print("answer:", answer.split("\n")[0].strip())
Output of this exact script (CPU, bfloat16, torch 2.8.0, transformers 4.56.0, run 2026-11-19 on the files in this repo):
query : Jane Austen Pride and Prejudice capital Australia answeranswer: The capital of Australia
The passage here is a fixed example string; the output shows the mechanics, not the model's accuracy.
Table with columns: Token, id| Token | id |
|---|
<WEB_QUERY> | 49152 |
</WEB_QUERY> | 49153 |
<WEB_RESPONSE> | 49154 |
</WEB_RESPONSE> | 49155 |
Layout in the training text: …text<WEB_QUERY>query</WEB_QUERY> <WEB_RESPONSE>passage</WEB_RESPONSE> text continues…. The tokenizer is the SmolLM2 tokenizer plus these four tokens (vocabulary 49,156). The loss covered the text, the query and the query tags; the response tags and the passage carried no loss, so the model does not generate passages. The caller supplies them.
Training data
Pretraining: English Wikipedia, 4,885,877 articles (54,582 further articles held out for evaluation), annotated with search blocks by a fine-tuned annotator model (Gemma 4 E4B-it with a LoRA adapter). The annotator marks positions where a search would help (insertion probability at least 0.05, at least 40 characters apart) and writes a query of at most 24 tokens for each. Passages come from BM25 retrieval over the public Sphere web corpus and are shortened by extractive cleaning. An insertion is kept only if the passage overlaps the text that follows it in the document (ROUGE-1 F1 of at least 0.1 against the next 260 characters).
Training procedure
Table | |
|---|
| Pretraining optimizer | AdamW, peak LR 5e-4, weight decay 0.1 |
| Batch | 256 sequences x 4,096 tokens = 1,048,576 tokens per update |
| Schedule | 2% warmup, cosine decay to zero; 4 epochs |
| Pretraining budget | 31,860 updates; 33.4B tokens processed, of which 16.0B document tokens; 6.31e19 FLOPs; bf16 compute |
Provenance
Table | |
|---|
| Checkpoint | run search_360M_v2_e4, step 31,860 of 31,860 (end of epoch 4; schedule finished) |
| Initialised from | scratch |
| Code | Hugging Face Trainer causal-LM script |
| Also published as | Kausp11/wiki6m-360m-v2-ckpts, subfolder search/ep4 (identical weights) |
model.safetensors sha256 | 8d5750a5f80cc257235e03339c7c94cdb5f53ea0ccce4101ed71aba788431187 |
Evaluation
Evaluation results will be added later.
Intended use and limitations
- A research artifact for studying models that learn to search during pretraining. Not a product.
- Base model: it continues text and has no chat format. For chat-style use see the fine-tuned version.
- English Wikipedia text only; no safety or refusal training of any kind. It will state false things fluently.
- It searches usefully only when the caller runs the loop above. Passages are inserted verbatim into the context: whatever the retriever returns, the model conditions on, including wrong or adversarial text. The retrieval source is the caller's choice and responsibility.
- A small model trained on Wikipedia only: weak outside encyclopedic text, and weaker than the DCLM-Edu models at question answering.
License
Apache-2.0 (licence text in LICENSE). Training text: English Wikipedia (CC BY-SA 4.0). Retrieved passages used as masked context during pretraining come from Sphere (CC BY-NC 4.0); they are not part of this repo.
Citation
Paper: Search From the Start: Search-Native Pretraining for Language Models · Project page: https://kaustubhp11.github.io/querylm/
@article{ponkshe2026searchfromthestart, title = {Search From the Start: Search-Native Pretraining for Language Models}, author = {Ponkshe, Kaustubh and Je, Gyung Hyun and Taga, Stefan and Raffel, Colin and Jaggi, Martin}, journal = {arXiv preprint arXiv:2611.26809}, year = {2026}}