Table of Contents
Fetching ...

SemRep: Generative Code Representation Learning with Code Transformations

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

Abstract

Code transformation is a foundational capability in the software development process, where its effectiveness relies on constructing a high-quality code representation to characterize the input code semantics and guide the transformation. Existing approaches treat code transformation as an end-to-end learning task, leaving the construction of the representation needed for semantic reasoning implicit in model weights or relying on rigid compiler-level abstractions. We present SemRep, a framework that improves code transformation through generative code representation learning. Our key insight is to employ the semantics-preserving transformations as the intermediate representation, which serves as both a generative mid-training task and the guidance for subsequent instruction-specific code transformations. Across general code editing and optimization tasks (e.g., GPU kernel optimization), SemRep outperforms the extensively finetuned baselines with strictly the same training budget by 6.9% in correctness, 1.1x in performance, 13.9% in generalization, and 6.7% in robustness. With the improved exploration of diverse code transformations, SemRep is particularly amenable to evolutionary search. Combined with an evolutionary coding agent, SemRep finds optimizations that 685B larger-weight baselines fail to discover while achieving the same performance with 25% less inference compute.

SemRep: Generative Code Representation Learning with Code Transformations

Abstract

Code transformation is a foundational capability in the software development process, where its effectiveness relies on constructing a high-quality code representation to characterize the input code semantics and guide the transformation. Existing approaches treat code transformation as an end-to-end learning task, leaving the construction of the representation needed for semantic reasoning implicit in model weights or relying on rigid compiler-level abstractions. We present SemRep, a framework that improves code transformation through generative code representation learning. Our key insight is to employ the semantics-preserving transformations as the intermediate representation, which serves as both a generative mid-training task and the guidance for subsequent instruction-specific code transformations. Across general code editing and optimization tasks (e.g., GPU kernel optimization), SemRep outperforms the extensively finetuned baselines with strictly the same training budget by 6.9% in correctness, 1.1x in performance, 13.9% in generalization, and 6.7% in robustness. With the improved exploration of diverse code transformations, SemRep is particularly amenable to evolutionary search. Combined with an evolutionary coding agent, SemRep finds optimizations that 685B larger-weight baselines fail to discover while achieving the same performance with 25% less inference compute.
Paper Structure (44 sections, 4 equations, 6 figures, 5 tables)

This paper contains 44 sections, 4 equations, 6 figures, 5 tables.

Figures (6)

  • Figure 1: A concrete example showing how SemRep optimizes CUDA kernels in Kernelbench ouyang2025kernelbench. SemRep first generates a semantically equivalent code that explicitly exposes the computation of MSE loss, and then transforms it into a more efficient implementation that optimizes memory access by replacing separate passes over large arrays with a single pass, where each thread computes its global index, reads inputs into fast registers for local computation, and fuses multiple operators into a single kernel execution. The existing approach attempts to directly optimize MSE but fails on several core operations.
  • Figure 2: SemRep framework. The upper shows how SemRep trains a model to instill semantics understanding through the generative code representation learning. SemRep explicitly encourages diverse semantically equivalent implementations, e.g., both faster and slower code variants, even when editing objectives are not fully satisfied. The total training budget $\mathcal{B}$ is fixed to ensure the fair comparison to those finetuning-only baselines. $\alpha$ modulates the training budget allocated to Stage 1 and Stage 2. The lower figure shows how SemRep performs iterative inference.
  • Figure 3: Scaling the number of parallel samples on KernelBench.
  • Figure 4: An example in EditBench showing how (a) direct transformation and (b) SemRep generate with "run these in parallel".
  • Figure 5: Best results over optimization steps with OpenEvolve for the minimum spanning tree and circle packing problem.
  • ...and 1 more figures