Hardware advances can no longer compensate for inefficient software, placing performance at the center of rising operational costs and energy consump- tion. This reality is particularly clear at Eni S.p.A., a global energy company whose operations depend on large-scale numerical simulations. In this context, computational efficiency directly affects costs and energy consumption, motivating research on automated code optimization with Large Language Models (LLMs). Despite this industrial relevance, code optimization remains underexplored. Al- though LLMs excel at code generation and understanding, their application to optimization is limited. This work investigates whether code-specialized LLMs can be systematically adapted to perform code optimization. The original contribution lies in the adopted two-stage framework that explicitly decouples structural learning from execution- aware refinement. Unlike prior approaches that combine heterogeneous losses, we first apply pure Supervised Fine-Tuning (SFT) on inefficient–efficient code pairs to isolate the structural impact of supervision. Only subsequently do we intro- duce Reinforcement Learning Fine-Tuning (RLFT) to align the model using exe- cution feedback. RLFT is implemented using Group Relative Policy Optimization (GRPO), which stabilizes training by averaging rewards across groups of candidate solutions. The framework is evaluated on Qwen2.5-Coder and DeepSeek-Coder models us- ing the ACEOB benchmark. Performance is assessed along structural alignment, functional correctness, and relative execution metrics. Results show that SFT con- sistently improves structural alignment and memory efficiency, with correctness gains emerging at medium and larger scales. RLFT increases functional reliabil- ity by rapidly saturating the correctness component of the reward, but tends to degrade structural alignment and memory efficiency. Notably, neither approach produces consistent runtime improvements during training, as execution-based sig- nals are inherently noisy and unstable. These findings indicate that optimization patterns can be reliably acquired through supervision alone, whereas RL fails to achieve stable efficiency alignment.
I progressi dell’hardware non sono più sufficienti a compensare inefficienze software, rendendo le prestazioni un fattore centrale nell’aumento dei costi operativi e dei consumi energetici. Questa dinamica è particolar- mente evidente in Eni S.p.A., le cui attività si fondano su simulazioni numeriche su larga scala, dove l’efficienza computazionale incide direttamente su costi, tempi di esecuzione e impatto energetico. Ciò motiva la ricerca sull’ottimizzazione automatica del codice mediante Large Language Models (LLM). Nonostante tale rilevanza, l’ottimizzazione orientata alle prestazioni resta meno esplorata rispetto ai task di generazione e comprensione del codice. Questo lavoro indaga se code-LLM possano essere adattati sistematicamente all’ottimizzazione del codice. Il contributo principale consiste in un framework a due stadi che separa l’apprendimento strutturale dal raffi- namento guidato dall’esecuzione. In una prima fase, un puro Supervised Fine-Tuning (SFT) su coppie ineffi- ciente–efficiente consente di interiorizzare pattern di trasformazione ricorrenti. Successivamente, il modello è adattato tramite Reinforcement Learning Fine-Tuning (RLFT), basato su Group Relative Policy Optimization (GRPO), che stabilizza l’addestramento mediante confronti relativi tra soluzioni candidate e allinea la gener- azione del modello a segnali derivanti dall’esecuzione del codice. Il framework è valutato sui modelli Qwen2.5-Coder e DeepSeek-Coder, a diverse scale parametriche, utiliz- zando il benchmark ACEOB. Le prestazioni sono misurate in termini di allineamento strutturale, correttezza funzionale ed efficienza relativa. I risultati mostrano che lo SFT migliora in modo consistente allineamento strutturale ed efficienza in memoria, con incrementi di correttezza alle scale medio-grandi. L’RLFT rafforza principalmente l’affidabilità funzionale, ma non produce miglioramenti stabili sul runtime, evidenziando i limiti dei segnali esecutivi rumorosi. Nel complesso, emerge che i pattern di ottimizzazione possono essere appresi efficacemente tramite supervi- sione, mentre l’allineamento all’efficienza mediante RL rimane fortemente dipendente dalla qualità del segnale di reward
An LLM-based reinforcement learning framework for code optimization
Curti, Angelo
2024/2025
Abstract
Hardware advances can no longer compensate for inefficient software, placing performance at the center of rising operational costs and energy consump- tion. This reality is particularly clear at Eni S.p.A., a global energy company whose operations depend on large-scale numerical simulations. In this context, computational efficiency directly affects costs and energy consumption, motivating research on automated code optimization with Large Language Models (LLMs). Despite this industrial relevance, code optimization remains underexplored. Al- though LLMs excel at code generation and understanding, their application to optimization is limited. This work investigates whether code-specialized LLMs can be systematically adapted to perform code optimization. The original contribution lies in the adopted two-stage framework that explicitly decouples structural learning from execution- aware refinement. Unlike prior approaches that combine heterogeneous losses, we first apply pure Supervised Fine-Tuning (SFT) on inefficient–efficient code pairs to isolate the structural impact of supervision. Only subsequently do we intro- duce Reinforcement Learning Fine-Tuning (RLFT) to align the model using exe- cution feedback. RLFT is implemented using Group Relative Policy Optimization (GRPO), which stabilizes training by averaging rewards across groups of candidate solutions. The framework is evaluated on Qwen2.5-Coder and DeepSeek-Coder models us- ing the ACEOB benchmark. Performance is assessed along structural alignment, functional correctness, and relative execution metrics. Results show that SFT con- sistently improves structural alignment and memory efficiency, with correctness gains emerging at medium and larger scales. RLFT increases functional reliabil- ity by rapidly saturating the correctness component of the reward, but tends to degrade structural alignment and memory efficiency. Notably, neither approach produces consistent runtime improvements during training, as execution-based sig- nals are inherently noisy and unstable. These findings indicate that optimization patterns can be reliably acquired through supervision alone, whereas RL fails to achieve stable efficiency alignment.| File | Dimensione | Formato | |
|---|---|---|---|
|
2026_03_Curti_Tesi.pdf
accessibile in internet per tutti
Dimensione
6.43 MB
Formato
Adobe PDF
|
6.43 MB | Adobe PDF | Visualizza/Apri |
|
2026_03_Curti_Executive Summary.pdf
accessibile in internet per tutti
Dimensione
460.61 kB
Formato
Adobe PDF
|
460.61 kB | Adobe PDF | Visualizza/Apri |
I documenti in POLITesi sono protetti da copyright e tutti i diritti sono riservati, salvo diversa indicazione.
https://hdl.handle.net/10589/253377