Abstract
We propose the Adversarial DEep Learning Transpiler (ADELT), a novel approach to source-to-source transpilation between deep learning frameworks. ADELT uniquely decouples code skeleton transpilation and API keyword mapping. For code skeleton transpilation, it uses few-shot prompting on large language models (LLMs), while for API keyword mapping, it uses contextual embeddings from a code-specific BERT. These embeddings are trained in a domain-adversarial setup to generate a keyword translation dictionary. ADELT is trained on an unlabeled web-crawled deep learning corpus, without relying on any hand-crafted rules or parallel data. It outperforms state-of-the-art transpilers, improving pass@1 rate by 16.2 pts and 15.0 pts for PyTorch-Keras and PyTorch-MXNet transpilation pairs respectively.
Introduction
The deep learning (DL) ecosystem moves fast. New frameworks emerge, and old ones are deprecated, leaving researchers and engineers with the "porting tax"—the tedious, error-prone task of manually rewriting models from one framework to another. Existing solutions fall into two camps, both with significant flaws. Unsupervised Neural Machine Translation (NMT) models treat code like natural language but are notoriously data-hungry and struggle because parallel corpora (the same model written in two different frameworks) are incredibly rare. Conversely, Large Language Models (LLMs) like GPT-4 are great at understanding general programming logic but often stumble on the "last mile" of framework-specific details, such as exact function names or the specific order of keyword arguments.
Methods
The core innovation of ADELT is its decoupled architecture. Instead of trying to translate everything at once, it splits the problem into two distinct steps:
- Code Skeleton Transpilation: The authors observed that the "skeleton" of DL code (loops, variable assignments, class structures) is relatively framework-agnostic. ADELT replaces framework-specific API keywords with placeholders (e.g.,
PLHD_1) and uses an LLM (specifically OpenAI’s Codex) via few-shot prompting to translate this structural skeleton. - API Keyword Mapping: To fill in the placeholders, ADELT needs a dictionary that maps keywords (e.g., PyTorch’s
nn.Linearto Keras’slayers.Dense). To build this without parallel data, the authors used Domain-Adversarial Training. They utilized PyBERT to generate contextual embeddings for API keywords found in a massive unlabeled corpus. They then trained a Generator to map these embeddings into a shared space and a Discriminator to guess which framework the embedding originated from. Through this adversarial process, the model learns to align functionally similar keywords from different frameworks in the vector space.
Finally, a hierarchical algorithm uses this generated dictionary to swap the placeholders back into the translated skeleton.
Datasets and Evaluation
The authors constructed a massive DL Corpus from three primary sources: GitHub (2.5GB), JuiCe (10.7GB), and Kaggle (22.1GB). For evaluation, they developed a benchmark of expert-transpiled examples paired with unit tests to verify that the translated code actually runs and produces the correct output, measured by the pass@1 metric.
Key Results
ADELT significantly outperformed general-purpose LLMs across the board. In PyTorch to Keras translation, ADELT achieved a 73.0% pass@1 rate, beating GPT-4 (56.8%) by 16.2 points. For PyTorch to MXNet, it achieved a 70.0% pass@1 rate, beating GPT-4 (55.0%) by 15.0 points.
Ablation studies confirmed that removing the adversarial loss or the contextual embeddings caused performance to drop significantly, proving that the adversarial alignment of context-aware vectors is the critical component of the system. Furthermore, because it uses a dictionary lookup for keywords rather than generating every token from scratch, ADELT is much faster than running full LLM inference for entire code blocks.
Conclusion and Future Work
While highly effective, ADELT occasionally struggles with one-to-many mappings, such as when a single source API call must be split into multiple calls in the target framework. It also remains dependent on a large volume of unlabeled data for each framework to ensure accurate adversarial alignment. Future directions include "human-in-the-loop" implementations where developers can manually patch the generated dictionary to maintain large-scale codebases efficiently. ADELT represents a significant step toward automated, framework-agnostic deep learning development.
Summary updated