Towards the Readability of LLM-Generated Codes through Multitask Representation Engineering
Summary
Researchers introduce Multitask Representation Engineering (MRepE), a novel framework designed to enhance the readability of code generated by large language models (LLMs) while managing the trade-off with code correctness. MRepE employs a joint principal component analysis algorithm with multidimensional orthogonal constraints (MOC-JPCA) to extract distinct steering vectors for three key readability metrics: comment density, naming conventions, and cyclomatic complexity. These orthogonal vectors are then injected into the LLM's hidden layers during inference, allowing for fine-grained control over code output. Experiments on Deepseek_R1_14b, Qwen2.5coder_14b_Instruct, and Codellama_13b_Instruct models demonstrate that MRepE significantly improves readability with minimal correctness degradation, such as a 0.79% loss for Deepseek_R1_14b, 0.95% for Qwen2.5coder_14b_Instruct, and 0.28% for Codellama_13b_Instruct, validating its theoretical bounds.
Key takeaway
For Machine Learning Engineers deploying LLMs for code generation, you should consider implementing the MRepE framework. It allows you to enhance code readability across comment density, naming conventions, and cyclomatic complexity by adjusting steering vector coefficients. This approach enables fine-grained control to balance readability improvements with minimal impact on code correctness, ensuring generated code is both functional and maintainable.
Key insights
MRepE uses orthogonal steering vectors to enhance LLM code readability across multiple metrics while bounding correctness impact.
Principles
- Code readability is subjective and multidimensional.
- Representation Engineering (RepE) offers lightweight model control.
- Orthogonal steering vectors prevent multi-task interference.
Method
MRepE extracts orthogonal steering vectors for readability metrics using MOC-JPCA from contrastive datasets. These vectors are then injected into LLM hidden layers during inference to modulate code generation.
In practice
- Adjust steering coefficients to balance readability and correctness.
- Apply MRepE to improve comment density, naming, and cyclomatic complexity.
- Use judgment-based tasks as proxies for generation behavior.
Topics
- Large Language Models
- Code Readability
- Representation Engineering
- Multitask Learning
- Code Generation
- Model Steering
- Software Engineering
Best for: Research Scientist, AI Scientist, Machine Learning Engineer, AI Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by cs.SE updates on arXiv.org.