SemRep : Generative Code Representation Learning with Code Transformations
Abstract
Lay Summary
Code transformation, e.g., editing code to fix bugs, add features, or improve performance, is a core task in software engineering. Current large language models attempt these edits end-to-end, jumping directly from input code to the desired output. This often leads to errors because the model never explicitly reasons about what the original code does before changing it. SemRep addresses this by disentangling semantic understanding from task-specific code editing. The model is first trained to generate semantically equivalent rewrites of the input code — programs that look different but behave identically, verified through test execution. This serves as a form of generative representation learning, forcing the model to internalize code semantics as explicit, human-readable intermediate programs rather than latent weight parameters. The model is then finetuned for the specific editing task. Crucially, both training phases share a fixed total budget, so the approach introduces no additional training cost over standard finetuning. During inference, SemRep allows the model to explore semantically equivalent variants of the input before applying the requested transformation. This naturally supports test-time scaling through evolutionary search, where diverse equivalent rewrites serve as stepping stones toward better solutions. Experiments on GPU kernel optimization (KernelBench) and real-world code editing (EditBench) show that SemRep enables a 32B model to match or exceed commercial systems and open-weight models up to 12× larger. It improves correctness by up to 43% over the previous state of the art, generalizes 13.9% better to unseen hardware, and is 6.7% more robust to surface-level code perturbations. When integrated with an evolutionary coding agent, SemRep discovers optimizations that 685B-parameter models fail to find while using 25% less inference compute.