Style2Code: A Style-Controllable Code Generation Framework with Dual-Modal Contrastive Representation Learning
Dutao Zhang, Nicolas Rafael Arroyo Arias, YuLong He, Sergey Kovalchuk
TL;DR
Style2Code addresses the challenge of controllable code generation by learning explicit style representations and aligning them with code semantics through a dual-modality contrastive framework. It introduces a two-stage training regime: first a Style Encoder learns a $34$-dimensional style vector $\mathbf{s}$ from target-style code fragments to produce a $z_s \in \mathbb{R}^{1024}$, then a decoder is guided by $z_s$ to generate functionally equivalent but stylistically aligned code. The key contributions include an explicit, interpretable Style Analyzer, a light-weight Style Encoder, and a joint training objective that combines semantic and style losses, enabling style interpolation and personalization without heavy user-specific retraining. Empirically, Style2Code shows strong improvements in lexical and stylistic metrics over baselines like Flan-T5 and competitive performance against state-of-the-art style-conditioned models, with practical implications for readable, maintainable, and project-consistent AI-assisted programming. The framework demonstrates the viability of multimodal conditioning for code synthesis and scales to large-model regimes, offering a promising direction for modality-aware code synthesis and style transfer in real-world development.”
Abstract
Controllable code generation, the ability to synthesize code that follows a specified style while maintaining functionality, remains a challenging task. We propose a two-stage training framework combining contrastive learning and conditional decoding to enable flexible style control. The first stage aligns code style representations with semantic and structural features. In the second stage, we fine-tune a language model (e.g., Flan-T5) conditioned on the learned style vector to guide generation. Our method supports style interpolation and user personalization via lightweight mixing. Compared to prior work, our unified framework offers improved stylistic control without sacrificing code correctness. This is among the first approaches to combine contrastive alignment with conditional decoding for style-guided code generation.
