Full-Stack Fine-Tuning for the Q Programming Language

1Morgan Stanley, New York, NY 2Prime Intellect, San Francisco, CA
Q Model Performance

Our fully trained models outperform frontier models on Q programming tasks, with our 32B model surpassing Claude Opus-4 by 29.5%. Individual bars represent pass@(1, 2, 4, 6, 8, 16, 40) for 40 completions per problem.

Abstract

Even though large language models are becoming increasingly capable, it is still unreasonable to expect them to excel at tasks that are under-represented on the Internet. Leveraging LLMs for specialized applications, particularly in niche programming languages and private domains, remains challenging and largely unsolved.

In this work, we address this gap by presenting a comprehensive, open-source approach for adapting LLMs to the Q programming language, a popular tool in quantitative finance. We introduce a new LeetCode-style evaluation dataset for Q, benchmark major frontier models, then perform pretraining, supervised fine-tuning, and reinforcement learning to train a suite of models based on the Qwen-2.5 series, spanning five parameter sizes (1.5B, 3B, 7B, 14B, 32B).

Our best model achieves a pass@1 accuracy of 59% on our Q benchmark, surpassing the best-performing frontier model, Claude Opus-4, by 29.5%. Additionally, all our models, even our 1.5B variant, outperform GPT-4.1 on this task. We provide a detailed blueprint for dataset construction, model pretraining, supervised fine-tuning, and reinforcement learning that is broadly applicable to other specialized domains.

Note for Q Practitioners

Our Q benchmark uses a LeetCode-style format that produces "Pythonic Q" code, which does not reflect typical Q usage in practice (database queries and analytics). While our models show strong performance on algorithmic tasks, practitioners may find that our pretrained models (before SFT/RL) provide better general-purpose Q assistance. This work serves as a blueprint for further adaptation to more representative Q datasets.

Training Pipeline & Results

Building the Dataset

We started by creating a verifiable Q dataset through a model-in-the-loop approach. Using LeetCode problems as a foundation, we iteratively generated Q solutions, verified them programmatically, and used successful examples to train better models. This bootstrapping process involved careful separation of solution and test generation to prevent reward hacking, ultimately resulting in a diverse dataset covering multiple problem types and difficulty levels.

Dataset Distribution

Distribution of problem difficulty levels and categories in our Q-LeetCode dataset, showing comprehensive coverage across algorithmic topics.

Domain-Adaptive Pretraining

We collected and curated Q code from open-source repositories and official documentation, creating a high-quality corpus for domain adaptation. Pretraining on this data gave our models foundational knowledge of Q syntax and idioms, providing a crucial boost that helped break through the bootstrapping plateau and established a strong foundation for subsequent fine-tuning stages.

Pretraining Performance

Performance improvements across all model sizes after domain-adaptive pretraining, with larger models showing greater gains.

Supervised Fine-Tuning

Using our curated LeetCode-Q dataset, we performed supervised fine-tuning on multiple task types: description-to-Q, Python-to-Q, and Q-to-Python translation. This stage directly optimized our models for the specific algorithmic challenges in our benchmark, building on the general Q knowledge from pretraining to achieve substantial performance improvements across all model sizes.

SFT Performance

Significant performance gains from supervised fine-tuning across all model sizes, demonstrating the value of task-specific training.

Reinforcement Learning

Finally, we applied reinforcement learning using programmatic rewards based on test case correctness. We explored both reasoning and non-reasoning variants, with particularly strong results for our 14B and 32B reasoning models. RL provided the final boost in performance, with reasoning models showing the ability to think through complex problems step-by-step before generating code solutions.

RL Performance

Performance improvements from reinforcement learning across model sizes, with reasoning variants showing particularly strong gains for larger models.

Interestingly, during RL training, we observed that our reasoning models learned to generate longer, more thoughtful completions over time. This suggests the models discovered that taking more time to reason through problems led to better solutions, naturally developing a "slow thinking" approach for complex algorithmic challenges.

Completion Length Evolution

Evolution of mean completion length during RL training, showing how the reasoning model learned to generate longer, more thoughtful responses for better results.

Cumulative Impact

Each stage of our pipeline contributed meaningfully to the final performance. The chart below shows how pretraining, supervised fine-tuning, and reinforcement learning each added value, with larger models benefiting more from each adaptation stage.

Cumulative Gains

Stacked chart showing the cumulative contribution of each training stage across all model sizes.

Key Contributions

  • New Q Benchmark: First LeetCode-style evaluation dataset for Q programming language with rigorous programmatic verification
  • Complete Model Suite: Five model sizes (1.5B-32B) all outperforming GPT-4.1, with top models exceeding Claude Opus-4
  • Practical Blueprint: End-to-end methodology for LLM domain adaptation with all code, data, and training scripts released
  • Methodological Insights: Lessons on reward hacking, evaluation design, and scaling effects in specialized domains

BibTeX

@article{hogan2025fullstack,
  author    = {Hogan, Brendan R. and Brown, Will and Boyarsky, Adel and Schneider, Anderson and Nevmyvaka, Yuriy},
  title     = {Full-Stack Fine-Tuning for the Q Programming Language},
  journal   = {arXiv preprint},
  year      = {2025},
  url       = {https://arxiv.org/abs/XXXX.XXXXX}
}