Introduction
In the rapidly evolving field of Artificial Intelligence, organizations often face "vendor lock-in" with specific deep learning frameworks like TensorFlow or PyTorch. Migrating between these platforms is frequently necessary to leverage new features, optimize performance, or adapt to library deprecations. However, manual migration is labor-intensive due to fundamental differences in framework philosophies, such as implicit vs. explicit tensor shapes, data layouts (channel-first vs. channel-last), and varying API conventions.
While tools like ONNX facilitate model conversion for inference, they do not address the migration of source code. This paper introduces a model-driven approach to automatically migrate neural network source code, allowing developers to maintain, train, and debug models in a new environment without manual rewriting.
Methodology: The BESSER-NN Pipeline
The proposed solution utilizes a three-stage pipeline grounded in Model-Driven Engineering (MDE) principles to ensure functional equivalence across frameworks.
- AST Extraction: The source Python code is parsed into an Abstract Syntax Tree (AST). This structural representation allows the system to identify neural network components—such as layers, tensor operations, and hyperparameters—independently of specific coding styles.
- Transformation to a Pivot Model: The AST is mapped to a platform-independent representation called the BESSER-NN metamodel. This "pivot" model acts as a universal translator. To resolve framework-specific requirements, such as PyTorch's need for explicit input feature counts that TensorFlow often hides, the authors employ symbolic tracing. By propagating a symbolic tensor through the network, the tool calculates the exact shapes required by the target framework.
- Code Generation: Using Jinja templates, the BESSER-NN model is converted into executable Python code for the target framework. This stage handles architectural discrepancies, such as automatically inserting
permuteoperations to reconcile different image data formats.
Key Insights: Bridging the Framework Gap
A central innovation of this approach is the use of a pivot model. Instead of developing unique translators for every possible framework pair, adding support for a new framework only requires a single "connector" to the BESSER-NN abstraction.
As illustrated in the transformation example above, the tool successfully bridges the gap between high-level abstractions (like Keras's layers.Dense) and more explicit definitions (like PyTorch's nn.Linear). The symbolic tracing mechanism is particularly effective at discovering "hidden" metadata, ensuring the generated code is fully specified for the target environment. Furthermore, the tool supports both simple Sequential stacks and complex Subclassing architectures, accommodating a wide range of modern model designs.
Evaluation and Results
The researchers evaluated the migration pipeline across eight scenarios using five diverse architectures, including AlexNet, VGG16, LSTM, and hybrid CNN-RNN models. These were tested on benchmark datasets such as CIFAR-10, SVHN, IMDB, and SST-2.
- Functional Equivalence: Testing with identical weights and random inputs yielded a Maximum Absolute Difference (MAD) as low as $5.96 \times 10^{-8}$, confirming that the underlying mathematical operations remain consistent after migration.
- Training Parity: Migrated models achieved accuracy levels nearly identical to their originals. For instance, a VGG16 model migrated to PyTorch reached 77.5% accuracy on CIFAR-10, compared to 75.3% in the original TensorFlow implementation.
- Structural Integrity: All generated code executed without errors, demonstrating logical and syntactical soundness.
Limitations and Future Directions
While the current system effectively handles PyTorch and TensorFlow across 12 layer types, several areas for expansion remain. Future work will focus on extending support to newer frameworks like JAX and Flax, increasing the library of supported custom layers, and migrating associated testing scripts. Additionally, the authors plan to explore the integration of Large Language Models (LLMs) to assist in complex refactoring and improve user interaction during the migration process.
Summary updated