MathFimer: Enhancing Mathematical Reasoning by Expanding Reasoning Steps through Fill-in-the-Middle Task

Yuchen Yan, Yongliang Shen, Yang Liu, Jin Jiang, Xin Xu, Mengdi Zhang, Jian Shao, Yueting Zhuang

TL;DR

Comparison of traditional code Fill-in-the-Middle (FIM) and the proposed MathFimer for mathematical reasoning.

MathFimer adapts the "Fill-in-the-Middle" (FIM) paradigm—originally used for code completion—to automatically expand the granularity of mathematical reasoning steps in training data. By training a specialized 7B model to reconstruct missing intermediate steps, the researchers created "denser" reasoning chains that significantly boost LLM performance across major math benchmarks without the need for expensive teacher models or complex search algorithms.

What Problem Does This Paper Address?

The performance of Large Language Models (LLMs) on mathematical tasks is heavily dependent on Chain-of-Thought (CoT) prompting, where the model explains its work step-by-step. However, the quality and "granularity" (the level of detail) of these steps in training datasets are often inconsistent.

Existing methods to improve this data usually rely on two expensive paths:

  1. Model Distillation: Using a massive "teacher" model (like GPT-4) to rewrite solutions, which is costly and creates a dependency on closed-source models.
  2. Search-based Methods: Using Monte Carlo Tree Search (MCTS) to find better paths, which is computationally grueling.

Furthermore, these methods often replace original human-verified solutions entirely, potentially introducing new errors. MathFimer seeks a middle ground: a way to "zoom in" on existing solutions to add detail while preserving the original logical structure.

Methods

The three-stage MathFimer framework: FIM Data Construction, Training, and Expanding Reasoning Steps.

The authors introduce a framework called MathFimer, inspired by how code models learn to fill in missing snippets of logic.

  1. The FIM Task: Instead of traditional causal language modeling (predicting the next token), the model is trained on a "Fill-in-the-Middle" task. Given a mathematical Question, a Prefix (steps before the gap), and a Suffix (steps after the gap), the model must predict the Middle (the missing step).
  2. Data Construction: They took the high-quality NuminaMath-CoT dataset and decomposed the solutions into individual steps. They then randomly masked steps to create 2.5 million FIM training triplets, forming the NuminaMath-FIM dataset.
  3. MathFimer-7B: They fine-tuned a Qwen2.5-Math-7B base model on this FIM data.
  4. The Expansion Pipeline: To enhance a new dataset, they pass it through MathFimer. The model looks at every pair of consecutive steps and attempts to insert a new intermediate step.
  5. Quality Control: To prevent redundancy, they use a Similarity Check. If a generated step is more than 80% similar to the following step, it’s discarded as "invalid" (meaning it didn't actually add new information).

Datasets

  • Training: NuminaMath-FIM (newly created from NuminaMath-CoT).
  • Expansion Targets: The authors applied MathFimer to GSM8K, MATH, MathInstruct-CoT, MetaMathQA, and ScaleQuest-Math.
  • Evaluation: Models were tested on GSM8K (elementary), MATH (high school), Math Odyssey, and OlympiadBench-EN (competition level).
  • Out-of-Distribution (OOD): BBH (general reasoning), GPQA (science), and LogicBench (logic).

Key Insights and Ideas

  • Structural Preservation: By filling in the middle rather than rewriting from scratch, MathFimer anchors the reasoning to the original human-verified solution, reducing the risk of "hallucinated" logic paths.
  • The "Detail" Transfer: Interestingly, training on math data with more granular steps improved performance on non-math tasks like Big-Bench Hard (BBH). This suggests that learning to reason in smaller, more explicit increments is a fundamental skill that generalizes beyond arithmetic.
  • Model Scale Efficiency: The researchers found that a 7B model performed nearly as well as a 72B model at the FIM task. This suggests that "expanding steps" is a structural task that doesn't necessarily require massive parameter counts, making it a highly scalable solution for data augmentation.
  • Iterative Refinement: The process is recursive. You can run MathFimer on a dataset multiple times to create increasingly "slow" and detailed reasoning trajectories.

Key Results

The results show consistent improvements across different base models (Llama 3.1 and Qwen 2.5) and sizes (8B to 72B):

  • Meta-Llama3.1-8B: When trained on MathFimer-expanded MathInstruct-CoT, the average accuracy across four benchmarks rose from 27.75% to 31.52% (+3.77 pp).
  • Qwen2.5-Math-72B: On the MetaMathQA dataset, expansion led to a jump from 52.71% to 57.14% (+4.43 pp).
  • GSM8K Gains: In some cases, simply expanding the training data led to a 7.43 percentage point increase in GSM8K accuracy.
  • Verification: Using a Process Reward Model (PRM), the authors confirmed that ~90% of the generated steps maintained high logical correctness (scores > 0.8).

Limitations and Future Work

  • Linearity Bias: The current FIM approach assumes reasoning is a linear chain. It may struggle with complex, non-linear reasoning structures like trees or graphs.
  • Error Accumulation: While similarity checks help, there is still a risk that an early hallucinated "middle step" could derail the rest of the solution during multi-round iterations.
  • Domain Specificity: The model was primarily trained on math. While it showed OOD promise, its effectiveness in specialized fields like legal reasoning or complex programming remains to be seen.
  • Future Direction: The authors suggest applying this to "R1-style" long-thought trajectories to see if FIM can help models learn the "internal monologue" required for extremely difficult problems.

Summary updated

BibTeX
@inproceedings{yan2026mathfimer,
  author = {Yuchen Yan and Yongliang Shen and Yang Liu and Jin Jiang and Xin Xu and Mengdi Zhang and Jian Shao and Yueting Zhuang},
  title = {{MathFimer: Enhancing Mathematical Reasoning by Expanding Reasoning Steps through Fill-in-the-Middle Task}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=14i2wzPPfn},
}
Figure