Text Generation
PEFT
TensorBoard
Safetensors
Arabic
English
Generated from Trainer
trl
grpo
math
reasoning
R1
conversational
Run this model with HF Jobs
Run Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("QCRI/Fanar-1-9B-Instruct") model = PeftModel.from_pretrained(base_model, "Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files6 unmodified lines789101112131415161718192021222324252627282930313233343536373839404142434445466 unmodified lines- generated_from_trainer- trl- grpolicence: license---# Model Card for Fanar-0.5B-GRPO-testThis model is a fine-tuned version of [QCRI/Fanar-1-9B-Instruct](https://huggingface.co/QCRI/Fanar-1-9B-Instruct) on the [AI-MO/NuminaMath-TIR](https://huggingface.co/datasets/AI-MO/NuminaMath-TIR) dataset.It has been trained using [TRL](https://github.com/huggingface/trl).## Quick start```pythonfrom transformers import pipelinequestion = "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?"generator = pipeline("text-generation", model="Omartificial-Intelligence-Space/Fanar-0.5B-GRPO-test", device="cuda")output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]print(output["generated_text"])```## Training procedureThis model was trained with GRPO, a method introduced in [DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models](https://huggingface.co/papers/2402.03300).### Framework versions- TRL: 0.14.0- Transformers: 4.47.1- Pytorch: 2.4.1- Datasets: 2.21.0- Tokenizers: 0.21.0## CitationsCite GRPO as:6 unmodified lines7891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921936 unmodified lines- generated_from_trainer- trl- grpo- math- reasoning- R1licence: licenselicense: apache-2.0language:- ar- en---# ๐ง Fanar-Math-R1-GRPO**Fanar-Math-R1-GRPO** is a reasoning-optimized language model built on [`QCRI/Fanar-1-9B-Instruct`](https://huggingface.co/QCRI/Fanar-1-9B-Instruct). This version is fine-tuned using **Group Relative Policy Optimization (GRPO)** from the DeepSeekMath framework on the [`AI-MO/NuminaMath-TIR`](https://huggingface.co/datasets/AI-MO/NuminaMath-TIR) dataset. It is designed for step-by-step mathematical problem-solving with structured reasoning in both English and Arabic.---## ๐ Model Highlights- ๐ Fine-tuned with **GRPO**, a sample-efficient reinforcement learning method- ๐งฎ Specializes in **multi-step mathematical reasoning**- ๐ฌ Outputs responses in a structured conversational format using `<think>` and `<answer>` tags- ๐ง Trained using **TRL** (`transformers`, `peft`, and `math_verify`)- ๐ท๏ธ Useful for both instruction-following and math-heavy dialogue generation---## ๐ฆ Model Details| Component | Description ||------------------|-----------------------------------------------------------------------------|| **Base Model** | [`QCRI/Fanar-1-9B-Instruct`](https://huggingface.co/QCRI/Fanar-1-9B-Instruct) || **Fine-Tuning** | GRPO via Hugging Face [TRL](https://github.com/huggingface/trl) || **Dataset** | [`AI-MO/NuminaMath-TIR`](https://huggingface.co/datasets/AI-MO/NuminaMath-TIR) || **Format** | `<think> ... </think> <answer> ... </answer>` tagged reasoning structure || **LoRA** | Enabled (modules: `q_proj`, `v_proj`, rank=8) || **Epochs** | 1 (lightweight test configuration) || **Tokenizer** | Same as base model |---## ๐งช Inference Example```pythonfrom transformers import AutoTokenizer, AutoModelForCausalLMimport torchimport timemodel_id = "Omartificial-Intelligence-Space/Fanar-Math-R1-GRPO"model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")tokenizer = AutoTokenizer.from_pretrained(model_id)def generate_with_reasoning(prompt_text):inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)start = time.time()with torch.no_grad():output = model.generate(**inputs, max_length=1024)end = time.time()generated = tokenizer.decode(output[0], skip_special_tokens=True)duration = end - startnum_input_tokens = inputs["input_ids"].shape[1]num_generated_tokens = output.shape[1] - num_input_tokensreturn generated, duration, num_generated_tokens# Example Arabic math problemprompt = """A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant first thinks about the reasoning process in the mind and then provides the user with the answer either in Arabic or English based on user's language. The reasoning process and answer are enclosed within <think> </think> and <answer> </answer> tags, respectively, i.e., <think> reasoning process here </think><answer> answer here </answer> ูู ู ุฏููุฉ ูุจูุบ ุนุฏุฏ ุณูุงููุง 1 ู ูููู ูุณู ุฉุ ุฅุฐุง ูุงู 60% ู ู ุงูุณูุงู ุจุงูุบููุ ู40% ู ู ุงูุจุงูุบูู ูุนู ูููุ ููู ุนุฏุฏ ุงูุนุงู ููู ูู ุงูู ุฏููุฉุ"""result, time_taken, tokens = generate_with_reasoning(prompt)print(result)```---## ๐ ๏ธ Training Setup### Configuration Summary- **learning_rate**: 1e-5- **epochs**: 1- **max_completion_length**: 64- **num_generations**: 4- **gradient_accumulation_steps**: 16- **logging_steps**: 10### Reward Functions- **accuracy_reward**: validates correctness of the answer using `math_verify`- **format_reward**: checks for proper usage of `<think>` and `<answer>` tags### Libraries & Versions```transformers==4.47.1trl==0.14.0peft==0.14.0datasets==2.21.0math_verify==0.3.3torch==2.4.1```---## ๐ Training Metrics (Snapshot)| Step | Reward (avg) | Accuracy Reward | Format Reward | Loss | KL Divergence ||------|--------------|-----------------|---------------|-------|---------------|| 10 | 0.029 | 0.029 | 0.0 | 0.0 | 0.00024 || 100 | 0.039 | 0.039 | 0.0 | 0.0001| 0.00188 || 200 | 0.033 | 0.033 | 0.0 | 0.0001| 0.00183 || 300 | 0.045 | 0.045 | 0.0 | 0.0001| 0.00127 |*Note: Training was run with a small config for notebook-friendly experimentation.*---## ๐ Output FormatThe model is trained to follow a reasoning-first format:```<think> First, we calculate 60% of 1 million, which is 600,000. Then, 40% of that is 240,000. </think><answer> 240,000 </answer>```---## ๐ฌ Citations### GRPO โ DeepSeekMath```bibtex@article{zhihong2024deepseekmath,title={DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models},author={Shao, Zhihong and Wang, Peiyi and Zhu, Qihao and Xu, Runxin and Song, Junxiao and Zhang, Mingchuan and Li, Y.K. and Wu, Y. and Guo, Daya},journal={arXiv preprint arXiv:2402.03300},year={2024}}```### TRL Library```bibtex@misc{vonwerra2022trl,title={TRL: Transformer Reinforcement Learning},author={von Werra, Leandro and Belkada, Younes and Tunstall, Lewis and Beeching, Edward and Thrush, Tristan and Lambert, Nathan and Huang, Shengyi and Rasul, Kashif and Gallouรฉdec, Quentin},year={2022},howpublished={\url{https://github.com/huggingface/trl}}}```---## ๐ Resources- [DeepSeekMath Paper](https://arxiv.org/abs/2402.03300)- [TRL Documentation](https://huggingface.co/docs/trl)- [Open-R1 Project](https://github.com/huggingface/open-r1)---## ๐งโ๐ฌ AuthorsDeveloped and trained by **Omar Paniego** with adaptation of the DeepSeek-R1 training recipe using Hugging Face's open tools and datasets.---## ๐ข LicenseRefer to the license file in the repository.---## โค๏ธ AcknowledgementsThanks to:- **Hugging Face Science Team** for `trl` and `math_verify`- **AI-MO** for the NuminaMath-TIR dataset- **DeepSeek Team** for releasing their methodology and insightsHappy reasoning! ๐โจ## CitationsCite GRPO as:
README.md+165-18