{"id":45904985,"url":"https://github.com/k1nght/rain-merging","last_synced_at":"2026-02-28T02:00:37.119Z","repository":{"id":340691157,"uuid":"1166239797","full_name":"K1nght/RAIN-Merging","owner":"K1nght","description":"RAIN-Merging (ICLR 2026 Oral)","archived":false,"fork":false,"pushed_at":"2026-02-26T02:48:46.000Z","size":2540,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-02-26T07:52:41.903Z","etag":null,"topics":["instruction-following","model-merging","reasoning-language-models"],"latest_commit_sha":null,"homepage":"https://openreview.net/forum?id=PO2iULmu5e","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/K1nght.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-25T02:39:25.000Z","updated_at":"2026-02-26T02:50:38.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/K1nght/RAIN-Merging","commit_stats":null,"previous_names":["k1nght/rain-merging"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/K1nght/RAIN-Merging","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/K1nght%2FRAIN-Merging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/K1nght%2FRAIN-Merging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/K1nght%2FRAIN-Merging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/K1nght%2FRAIN-Merging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/K1nght","download_url":"https://codeload.github.com/K1nght/RAIN-Merging/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/K1nght%2FRAIN-Merging/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29922697,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T19:37:42.220Z","status":"online","status_checked_at":"2026-02-28T02:00:07.010Z","response_time":90,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["instruction-following","model-merging","reasoning-language-models"],"created_at":"2026-02-28T02:00:14.998Z","updated_at":"2026-02-28T02:00:37.106Z","avatar_url":"https://github.com/K1nght.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RAIN-Merging: A Gradient-Free Method to Enhance Instruction Following in Large Reasoning Models with Preserved Thinking Format (ICLR 2026 Oral)\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://openreview.net/forum?id=PO2iULmu5e\"\u003e\u003cimg src=\"https://img.shields.io/badge/OpenReview-ICLR-blue.svg\" alt=\"OpenReview\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://arxiv.org/abs/2602.22538\"\u003e\u003cimg src=\"https://img.shields.io/badge/arXiv-Paper-b31b1b.svg\" alt=\"arXiv\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\u003cimg src=\"https://img.shields.io/badge/License-MIT-green.svg\" alt=\"License: MIT\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nImplementation of **RAIN-Merging**. RAIN-Merging is a gradient-free model merging method that integrates instruction-following capability from an instruction-tuned model (ITM) into a large reasoning model (LRM), while preserving the LRM's structured thinking format (`\u003cthink\u003e` / response segments) and reasoning quality. The method requires only small calibration sets and no gradient computation.\n\n\u003ctable align=\"center\"\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \n      \u003cimg src=\"asset/RAIN-Merge-overview.png\" alt=\"Teaser\" style=\"width: 1000px;\"/\u003e \n      \u003cbr\u003e\n      \u003cem style=\"font-size: 18px;\"\u003e\u003cstrong style=\"font-size: 18px;\"\u003e\u003cstrong\u003eOverview of RAIN-Merging\u003c/strong\u003e\u003c/em\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nThe two core stages of RAIN-Merging are:\n1. **Reasoning-aware Null-space Projection** — projects the ITM task vector onto the null space of forward features at thinking special tokens, so the LRM's structured reasoning mechanism is left intact.\n2. **Instruction-attention Guided Merging Coefficients** — estimates per-module merging coefficients that amplify instruction-relevant components and suppress leakage into the reasoning region, using a small instruction calibration set.\n\n\u003ctable align=\"center\"\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \n      \u003cimg src=\"asset/RAIN-Merge-Method.png\" alt=\"Teaser\" style=\"width: 1000px;\"/\u003e \n      \u003cbr\u003e\n      \u003cem style=\"font-size: 18px;\"\u003e\u003cstrong style=\"font-size: 18px;\"\u003e\u003cstrong\u003eTwo stages of our RAIN-Merging pipeline\u003c/strong\u003e\u003c/em\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n## 📁 Project Structure\n\n```\nRAIN-Merging/\n├── scripts/                          # Execution scripts\n│   ├── run_stage1.sh                 # Stage 1: Reasoning-aware Null-space Projection\n│   ├── run_stage2.sh                 # Stage 2: Instruction-attention Guided Merging Coefficients\n│   └── run_stage3.sh                 # Stage 3: Model merging\n├── nullspace_projection_compute.py   # Stage 1 implementation\n├── qp_true_forward_fast.py           # Stage 2 implementation\n├── unified_model_merge.py            # Stage 3 implementation\n├── pipeline.py                       # End-to-end pipeline\n├── data/                             # Calibration set\n├── requirements.txt                  # Dependencies\n└── README.md                         # This file\n```\n\n## 🛠 Installation\n\n**Install dependencies:**\n```bash\npip install -r requirements.txt\n```\n\n**Optional optimizations:**\n```bash\n# For Flash Attention (recommended)\npip install flash-attn\n\n# For quantization support\npip install bitsandbytes\n```\n\n## 📋 Quick Start\n\n### Three-Stage Pipeline\n\nThe following examples use:\n- **Base model** (`BASE`): `Qwen/Qwen2.5-7B`\n- **Instruction model** (`ITM`): `Qwen/Qwen2.5-7B-Instruct`\n- **Target / reasoning model** (`LRM`): `deepseek-ai/DeepSeek-R1-Distill-Qwen-7B`\n\n#### Stage 1: Null-space Projection\n\nCompute null-space projections for the ITM task vector, constrained to preserve forward features at thinking special tokens.\n\n```bash\n./scripts/run_stage1.sh \\\n    Qwen/Qwen2.5-7B \\\n    Qwen/Qwen2.5-7B-Instruct \\\n    deepseek-ai/DeepSeek-R1-Distill-Qwen-7B \\\n    ./data/reasoning_calibration_set.json \\\n    ./stage1_output\n```\n\n**Key options** (set via environment variables before the command):\n\n| Variable | Default | Description |\n|---|---|---|\n| `MAX_SAMPLES` | `1000` | Number of reasoning calibration samples |\n| `LAYERS_TAIL` | `27` | Process the last N layers |\n| `MERGE_TYPES` | `qkvof` | Parameter groups to project (`q`, `k`, `v`, `o`, `f`) |\n| `COMPUTE_PRECISION` | `fp32` | Solver precision (`fp32` / `fp64`) |\n| `MAX_SEQ_LEN` | `7168` | Max sequence length (BF16 optimised; caps attention memory) |\n| `LAMBDA_RIDGE` | `1e-4` | Ridge regularisation for the null-space solver |\n| `QK_DEVICE` | `auto` | Device for Q/K constraint computation |\n| `VO_DEVICE` | `auto` | Device for V/O constraint computation |\n| `FFN_DEVICE` | `auto` | Device for FFN constraint computation |\n\n---\n\n#### Stage 2: QP Optimisation\n\nOptimise per-head merging coefficients (α) using a small instruction calibration set and quadratic programming.\n\n```bash\n./scripts/run_stage2.sh \\\n    deepseek-ai/DeepSeek-R1-Distill-Qwen-7B \\\n    ./data/instruction_calibration_set.jsonl \\\n    ./stage1_output/projected_task_vectors.pkl \\\n    ./stage2_output\n```\n\n---\n\n#### Stage 3: Model Merging\n\nApply the projected task vectors and optimised $\\alpha$ coefficients to produce the final merged model.\n\n```bash\n./scripts/run_stage3.sh \\\n    deepseek-ai/DeepSeek-R1-Distill-Qwen-7B \\\n    ./stage1_output/projected_task_vectors.pkl \\\n    ./stage2_output/alpha_true_forward_two_pass.pt \\\n    ./final_merged_model\n```\n\nTwo merge modes are supported:\n- **Alpha mode**: provide an alpha file from Stage 2 (recommended).\n- **Scaling factor mode**: omit alpha file, set `SCALING_FACTOR` instead.\n\n---\n\n### One-Command Pipeline\n\nFor convenience, the full three-stage pipeline can be run as a single command:\n\n```bash\npython pipeline.py \\\n    --base_model Qwen/Qwen2.5-7B \\\n    --instruct_model Qwen/Qwen2.5-7B-Instruct \\\n    --target_model deepseek-ai/DeepSeek-R1-Distill-Qwen-7B \\\n    --data_file ./data/instruction_calibration_set.jsonl \\\n    --output_dir ./merged_model_output\n```\n\n\n## 📄 Citation\n\nIf you find this work useful, please cite:\n\n```bibtex\n@inproceedings{\nhuang2026rainmerging,\ntitle={{RAIN}-Merging: A Gradient-Free Method to Enhance Instruction Following in Large Reasoning Models with Preserved Thinking Format},\nauthor={Zhehao Huang and Yuhang Liu and Baijiong Lin and Yixin Lou and Zhengbao He and Hanling Tian and Tao Li and Xiaolin Huang},\nbooktitle={The Fourteenth International Conference on Learning Representations},\nyear={2026},\nurl={https://openreview.net/forum?id=PO2iULmu5e}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk1nght%2Frain-merging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk1nght%2Frain-merging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk1nght%2Frain-merging/lists"}