Manuscript Offprint

arXiv:2610.16897

DeepGroundRepair: From API-Grounded Generation to Shape-Aware Repair for Deep Learning Programs

Xiangyue Ma, Zhen Wang, Xuhui Lu, Xiaoting Du, Chenglong Li, Jun Li, Haotian Li, Zheng Zheng

XIANGYUE MA, Beihang University, China

ZHEN WANG, Beihang University, China

XUHUI LU, Beihang University, China

XIAOTING DU, Beijing University of Posts and Telecommunications, China

CHENGLONG LI, Beijing University of Technology, China

JUN LI, Beihang University, China

HAOTIAN LI, Beihang University, China

ZHENG ZHENG ∗^{*}, Beihang University, China

Deep learning (DL) programs are fundamental to modern AI applications, and automatically generating them from natural-language requirements can substantially reduce DL development effort. Large language models (LLMs) have demonstrated promising capabilities for this direction. However, generated DL programs often remain unreliable because they must satisfy domain-specific API usage constraints and tensor-shape dependencies, violations of which can cause API-level syntactic issues and runtime failures. Existing retrieval-augmented generation and feedback-guided code repair methods do not adequately address these DL-specific constraints.

In this paper, we propose DEEPGROUNDREPAIR, a three-stage framework that establishes a generation-localization-repair workflow to improve the reliability of LLM-generated DL programs. Its key idea is to progressively narrow the generation and repair spaces using DL-specific knowledge and runtime tensor information. Specifically, it grounds generation in task-relevant API knowledge, localizes the upstream causes of tensor-shape mismatch failures, and uses runtime tensor shapes to guide parameter repair. Together, these mechanisms transform open-ended generation and repair into a knowledge-grounded and shape-aware process.

We evaluate DEEPGROUNDREPAIR on the 100 model-level DL programming tasks in DeepEval using six open-weight LLMs across TensorFlow, PyTorch, and PaddlePaddle. We compare it with four baselines: two generation-only methods and their counterparts incorporating traceback-guided self-debugging. The results show that, relative to these baselines, DEEPGROUNDREPAIR reduces the combined number of API-level syntactic issues by an average of 5.4 per 10 program files and achieves an average relative improvement of 85.3% in code executability. Compared with the two self-debugging baselines, it improves semantic consistency by an average of 18.1% and reduces token cost by an average of 28.4%. The consistent improvements across model families, model scales, and DL libraries provide evidence of its generalizability within the evaluated scope.

CCS Concepts: • Software and its engineering → Automatic programming; Domain specific languages; • Computing methodologies → Artificial intelligence.

Additional Key Words and Phrases: LLM-based code generation, deep learning programs, retrieval-augmented generation, automated program repair

∗^{*}Corresponding author

Authors' Contact Information: Xiangyue Ma, Beihang University, Beijing, China, xiangy_ma@buaa.edu.cn; Zhen Wang, Beihang University, Beijing, China, wz2603520@buaa.edu.cn; Xuhui Lu, Beihang University, Beijing, China, luxuhui@buaa.edu.cn; Xiaoting Du, Beijing University of Posts and Telecommunications, Beijing, China, duxiaoting@bupt.edu.cn; Chenglong Li, Beijing University of Technology, Beijing, China, lichenglong@bjut.edu.cn; Jun Li, Beihang University, Beijing, China, 2503515@buaa.edu.cn; Haotian Li, Beihang University, Beijing, China, lihaotian08@buaa.edu.cn; Zheng Zheng, Beihang University, Beijing, China, zhengz@buaa.edu.cn.

Ma et al.

1 Introduction

In recent years, large language models (LLMs) have demonstrated strong capabilities in generating code from natural language [22, 24, 25]. By translating programming intent into executable code, LLMs can lower barriers to and reduce the effort required for software development. Yet, performance on general-purpose code generation does not necessarily extend to domain-specific programming tasks. This challenge is especially pronounced in DL programming, where structural and semantic dependencies make DL programs prone to bugs involving API usage and tensor compatibility [20, 27, 42]. Therefore, improving the reliability of LLM-generated DL programs remains a challenge.

In DL programs, DL models are constructed as computation graphs of interdependent API calls and tensor transformations. Program correctness therefore depends on library-specific API configurations and tensor compatibility across the graph. As demonstrated in our prior study [31], LLM-generated DL code frequently encounters two prevalent problems. Problem 1: API-Level Syntactic Issues. LLMs frequently hallucinate or misuse DL APIs, including references to nonexistent members and violations of API signatures. Problem 2: Runtime Execution Failures. LLM-generated DL code frequently fails at runtime due to violations of implicit constraints among DL operations. Among such failures, tensor-shape mismatches are the predominant type, accounting for 77.34% of execution failures on average [31].

Existing studies relevant to these problems mainly follow two directions. One line of work employs retrieval-augmented generation (RAG) to ground code generation in external knowledge [8, 21, 45]. Its effectiveness, however, depends heavily on retrieving task-relevant context, as irrelevant information can introduce noise and degrade code generation [17]. In DL programming, this challenge is further complicated by the gap between high-level task intent and fine-grained API requirements, making static query-document matching prone to retrieving irrelevant API knowledge [38]. Another line of work investigates feedback-guided code repair, in which failure information from program execution is fed back to LLMs for iterative refinement [11, 36, 44]. However, prior evidence shows that such repair often yields only limited improvements [33]. This limitation is particularly evident in DL programs, where raw tracebacks often reveal only downstream symptoms of tensor-shape errors. Consequently, an LLM reasoning solely from such feedback lacks the evidence needed to localize upstream root causes. Moreover, even after the root cause is localized, self-repair without explicit shape guidance remains insufficient: resolving such errors requires accounting for how multiple interdependent API parameters jointly affect tensor shapes [13].

As discussed above, existing approaches have shown promise in improving general-purpose LLM-generated code, but they do not fully address the two DL-specific problems. Therefore, this work aims to improve the syntactic correctness (Problem 1) and runtime executability (Problem 2) of LLM-generated DL programs. To this end, we identify one key retrieval challenge for mitigating API-level syntactic issues during code generation and two key challenges concerning the localization and repair of tensor-shape mismatches underlying runtime execution failures.

• Challenge 1 (Generation): How to provide LLMs with task-relevant DL API knowledge? High-level DL task requirements typically describe model structure and functionality rather than explicit API usage, creating a semantic gap between task intent and API documentation.

• Challenge 2 (Localization): How to localize the root-cause API of a tensor-shape mismatch? A tensor-shape mismatch may originate from an upstream API configuration but surface only at a downstream operation, separating the failure site from its root cause.

• Challenge 3 (Repair): How to infer shape-consistent API parameters for repair? Without explicit guidance on how interdependent API parameters jointly affect tensor shapes, LLMs struggle to identify configurations satisfying tensor-shape constraints.

To address these challenges, we propose DEEPGROUNDREPAIR, a three-stage framework that establishes a generation-localization-repair workflow for DL programs, progressing from API-grounded retrieval-augmented generation through root-cause localization to shape-aware repair. Its main idea is to progressively narrow the generation and repair spaces using DL API knowledge and runtime tensor shapes as complementary guidance. API knowledge grounds code generation, while runtime tensor shapes support root-cause localization and constrain subsequent parameter-level repair. The three stages work in concert to address the challenges outlined above. (1) Reasoning-Augmented Classification Retrieval. To accurately obtain task-relevant API knowledge, DEEPGROUNDREPAIR uses LLM-based API recommendation and existence verification to distinguish existing APIs from hallucinated ones, routing existing and nonexistent API candidates to sparse and dense retrieval, respectively. (2) Root-Cause API Localization. To localize where a tensor shape mismatch should be repaired, DEEPGROUNDREPAIR combines static program analysis with dynamic execution traces to capture both program structure and runtime behavior. This combined analysis traces the mismatch from its failure site back to the upstream root-cause API and masks suspicious parameters. (3) Shape-Aware Self-Repair. To determine how the localized API should be repaired, DEEPGROUNDREPAIR uses traced tensor shapes as explicit evidence and provides the LLM with retrieved shape-related knowledge for the root-cause API. This guidance enables the LLM to infer shape-consistent parameter values while respecting tensor-shape dependencies across the computation graph.

To evaluate the effectiveness of DEEPGROUNDREPAIR, we conduct experiments on the model-level DL code generation benchmark DeepEval using six open-weight LLMs from two model families across TensorFlow, PyTorch, and PaddlePaddle. We compare it against four baselines: two generation-only methods (vanilla LLM generation and Hybrid-RAG) and two methods combining their respective generation strategies with traceback-guided self-debugging. Across the three libraries, DEEPGROUNDREPAIR reduces API hallucinations and parameter misuses by an average of 2.14 and 3.26 issues per 10 program files, respectively. Most notably, it improves code executability by an average of 85.3% relative to the four baselines. It also achieves an average relative improvement of 18.1% in semantic consistency over the two self-debugging baselines. Compared with these baselines, DEEPGROUNDREPAIR further delivers higher code executability with 28.4% lower token cost on average.

In summary, this work makes the following contributions:

  • We propose DEEPGROUNDREPAIR, a novel framework that bridges API-grounded code generation and shape-aware repair, enabling DL programs to be progressively improved through task-relevant API knowledge and runtime tensor-shape feedback.

  • We conduct extensive experiments across multiple LLMs and DL libraries. The results demonstrate that DEEPGROUNDREPAIR consistently improves the syntactic correctness, code executability, and semantic consistency of LLM-generated DL programs, while achieving a favorable effectiveness–efficiency trade-off.

  • We perform ablation study and stage-specific analyses to quantify the contributions of the three stages and examine the mechanisms underlying the overall performance gains.

  • We release the replication package of DEEPGROUNDREPAIR to facilitate reproducibility and support future research on reliable LLM-based DL code generation.

References (22)