NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

SemRep : Generative Code Representation Learning with Code Transformations

Weichen Li, Jiamin Song, Bogdan Alexandru Stoica, Arav Dhoot, Gabriel Ryan, Shengyu Fu, Kexin Pei

Code transformation—whether it is refactoring, bug fixing, or performance optimization—is a high-stakes balancing act. Large Language Models (LLMs) must alter a program's structure or performance while strictly preserving its original logic. Current approaches typically treat this as an end-to-end "black box" task. The model is given source code and an instruction (e.g., "make this faster") and is expected to produce the result in one go. Unfortunately, this often leads to "silent logic errors," where the code runs faster but produces the wrong output, or "optimization blindness," where the model misses structural opportunities because semantic reasoning is buried implicitly in its weights.

To solve this, researchers introduce SemRep, a framework that significantly boosts the ability of LLMs to transform and optimize code by teaching them to reason about code equivalence. By decoupling "understanding what the code does" from "changing it to meet a goal," SemRep enables smaller models (like QwQ-32B) to outperform models up to 12 times their size on complex tasks like GPU kernel optimization and algorithmic discovery.

The Core Innovation: Code as Its Own Representation

Instead of relying on rigid compiler-level abstractions like Abstract Syntax Trees (ASTs), SemRep uses semantically equivalent code as its intermediate representation. The key insight of the paper is that the best way for an LLM to represent code semantics isn't a hidden vector or a tree, but other human-interpretable, machine-verifiable code.

SemRep employs a two-stage training process using Group Relative Policy Optimization (GRPO):

  1. Generative Representation Learning: The model is trained to generate diverse, functionally identical versions of the input code. It is rewarded for producing code that passes the same unit tests as the original, even if the syntax or algorithm changes. This refactoring step makes future optimization opportunities much more explicit.
  2. Instruction-Specific Transformation: The model is then fine-tuned to perform specific edits (like CUDA optimization) while maintaining the semantic understanding developed in the first stage.

At inference time, SemRep utilizes an Iterative Evolutionary Search. Rather than generating a single answer, it explores a pool of semantically equivalent "stepping stones." Sometimes, it generates intermediate programs that are temporarily slower but structurally better for future optimization. This non-greedy exploration allows it to find global optima that direct-transformation models miss.

Key Results

Evaluated across challenging benchmarks like KernelBench (PyTorch to CUDA optimizations), EditBench (real-world repository edits), and AlgoTune (algorithmic optimization), SemRep demonstrated remarkable capabilities:

  • Superior Correctness & Performance: On KernelBench, SemRep outperformed the state-of-the-art Kevin-32B by 43.1% in correctness and achieved significantly higher speedups. Across general tasks, it improved correctness by 6.9% and performance by 1.1x compared to extensively fine-tuned baselines with the exact same training budget.
  • Punching Above Its Weight: A SemRep-trained 32B model matched or beat massive models like Qwen-405B and DeepSeek-V3 (685B) on EditBench and algorithmic tasks.
  • Efficiency & Robustness: When integrated with an evolutionary agent, SemRep achieved top-tier performance using 25% less inference compute. It also showed a 6.7% improvement in robustness against noisy input code (e.g., varying formatting or variable names) and generalized better to unseen hardware architectures.

Limitations and Future Directions

While powerful, SemRep relies on "Equivalence Modulo Inputs" (EMI) via unit tests. If test coverage is low, the model might engage in reward hacking—producing code that passes the tests but breaks on other inputs. Furthermore, as dictated by Rice’s Theorem, determining true program equivalence is mathematically undecidable; SemRep is a highly effective approximation, not a formal proof of correctness. Finally, the two-stage RL process introduces additional training complexity compared to standard supervised fine-tuning.

Future work aims to explore more diverse categories of semantics-preserving transformations and improve the verifiable reward system to handle increasingly complex software architectures.

Summary updated

BibTeX
@inproceedings{li2026semrep,
  author = {Weichen Li and Jiamin Song and Bogdan Alexandru Stoica and Arav Dhoot and Gabriel Ryan and Shengyu Fu and Kexin Pei},
  title = {{SemRep : Generative Code Representation Learning with Code Transformations}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=d81PBCmA4s},
}
Figure