GREEN-CODE: Optimizing Energy Efficiency in Large Language Models for Code Generation

Shashikant Ilager, Lukas Florian Briem, Ivona Brandic · DOI

Introduction

Large Language Models (LLMs) have revolutionized code generation, but their widespread adoption in tools like GitHub Copilot comes with a significant environmental footprint. While model training is energy-intensive, the cumulative cost of inference—performed billions of times daily—represents a massive long-term energy challenge. GREEN-CODE addresses this by introducing a reinforcement learning (RL) framework for dynamic "early exiting," allowing models to stop inference as soon as a reliable prediction is made, thereby cutting energy consumption by 23–50% without significantly degrading code quality.

Methodology: The GREEN-CODE Framework

The authors propose a two-phase approach to tackle the early-exit challenge: single-head fine-tuning and RL-driven dynamic exiting.

The GREEN-CODE system architecture, illustrating the offline RL training phase and the online energy-aware code generation pipeline.

Single-Head Fine-Tuning

Traditional early-exit methods often require a separate Language Model (LM) head for every potential exit layer, leading to significant memory overhead. GREEN-CODE avoids this by fine-tuning base models (such as Llama 3.2 3B and OPT 2.7B) using a weighted aggregated loss function. This technique trains the model to produce accurate tokens from intermediate hidden states using only the original, single LM head, making the approach viable for memory-constrained deployments.

RL-Driven Dynamic Exiting

To determine the optimal exit point for each token, the researchers employed a Proximal Policy Optimization (PPO) agent. This agent monitors the hidden state at each layer and decides whether to "continue" to the next layer or "exit" and decode the current state. The reward function is carefully crafted to balance accuracy and efficiency, penalizing both incorrect early exits and redundant deep-layer processing. This dynamic approach outperforms static confidence thresholds because it adapts to the varying difficulty of token prediction in different contexts.

Evaluation and Key Results

The framework was evaluated on line-level code completion tasks using the JavaCorpus and PY150 datasets from the CodeXGlue benchmark. These datasets consist of real-world code files scraped from open-source GitHub repositories.

Comparison of performance metrics (CodeBLEU, ROUGE-L), total energy consumption, and total time across different exit indices for Llama and OPT models.

Energy and Throughput Gains

The results demonstrate a clear advantage for dynamic exiting:

  • Energy Efficiency: GREEN-CODE achieved energy savings between 23% and 50% compared to full-layer inference across different models and datasets.
  • Performance Stability: For Llama 3.2 on JavaCorpus, the model maintained a ROUGE-L score of 0.39 (down only slightly from a 0.425 baseline) while significantly reducing energy usage.
  • Throughput Boost: Inference speed improved from approximately 13 tokens/second to 18 tokens/second using the dynamic approach.
  • Low Overhead: The computational cost of running the RL agent's policy network remained below 20% of the total runtime, which is easily offset by the resources saved from skipping LLM layers.

Architecture-Specific Strategies

The RL agent successfully adapted to different architectures. For Llama 3.2 (28 layers), the agent learned that nearly 60% of tokens could be correctly predicted within the first five layers. In contrast, for the OPT model (32 layers), optimal exits were distributed more evenly across its depth, highlighting the importance of a dynamic, model-aware exit strategy.

Discussion and Future Work

GREEN-CODE proves that shallow layers in modern LLMs are often sufficient for accurate code generation. By replacing static thresholds with a dynamic RL agent, the framework provides a flexible way to trade off minor accuracy for major energy and latency gains.

However, some challenges remain. Standard KV caching is currently incompatible with early exits because skipping layers prevents the computation of caches for deeper layers needed for future tokens. Future work will focus on optimizing parallel cache computation and scaling the framework to larger models and complex multi-agent software engineering workflows where long-running inference benefits most from efficiency optimizations.

Summary updated

BibTeX
@misc{ilager2025green,
  author = {Shashikant Ilager and Lukas Florian Briem and Ivona Brandic},
  title = {{GREEN-CODE: Optimizing Energy Efficiency in Large Language Models for Code Generation}},
  year = {2025},
  eprint = {2501.11006},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2501.11006},
}
Figure