Towards Migrating Neural Network Implementations

Nadia Daoudi, Iván Alfonso, Jordi Cabot

TL;DR

This paper introduces an automated approach to migrate neural network code between different deep learning frameworks, specifically PyTorch and TensorFlow. By using an intermediate "pivot" model, their method successfully converts existing neural network implementations, ensuring functional equivalence and saving significant manual effort.

What Problem Does This Paper Address?

The rapid evolution of AI has led to a proliferation of deep learning frameworks, each with its own strengths and ecosystems. While this offers flexibility, it creates a significant challenge: migrating existing neural network (NN) code from one framework to another. Currently, this process is largely manual, requiring developers to rewrite code from scratch, which is time-consuming, error-prone, and hinders the adoption of new features or the integration with other systems. This paper aims to bridge this gap by providing an automated solution for NN code migration.

Methods

The authors propose a three-step approach for migrating NN code:

  1. AST Extraction: The first step involves parsing the source NN code (e.g., in Python) into an Abstract Syntax Tree (AST). This tree structure represents the code's logical relationships, making it amenable to programmatic manipulation. They leverage Python's ast library and support both "Sequential" and "Subclassing" architectural styles common in frameworks like PyTorch and TensorFlow.
  2. Transformation: The extracted AST is then transformed into a platform-independent intermediate representation called the BESSER-NN model. This model acts as a "pivot," abstracting away framework-specific details. The authors map NN components (layers, tensor operations, hyperparameters, training configurations) from the source AST to this unified BESSER-NN format. They developed specific transformation modules for different framework/architecture combinations (e.g., PyTorch Subclassing to BESSER-NN).
  3. Code Generation: Finally, code generators are used to translate the BESSER-NN model into the target framework's code. The authors developed generators for both PyTorch and TensorFlow, allowing migration in either direction. This process involves a preprocessing step to refine the BESSER-NN model and then uses Jinja templates for code generation.

Datasets

The paper does not introduce new datasets. Instead, it uses existing, well-established datasets for evaluating the functional equivalence of the migrated neural networks:

  • CIFAR-10: A collection of 32x32 images across 10 object categories, commonly used for evaluating convolutional neural networks (CNNs).
  • SVHN (Street View House Numbers): Real-world house number images used for digit recognition tasks.
  • IMDB: A dataset of movie reviews labeled as positive or negative, used for sentiment analysis with recurrent neural networks (RNNs).
  • SST-2 (Stanford Sentiment Treebank): A dataset of movie reviews classified as positive or negative, also used for sentiment analysis.

Key Insights and Ideas

  • The Power of a Pivot Model: The core innovation is the use of an intermediate, framework-agnostic BESSER-NN model. This abstraction layer simplifies the migration process by decoupling the source and target frameworks, making the approach more extensible to other frameworks in the future.
  • Handling Framework Nuances: The paper highlights specific challenges, such as the differing conventions for activation functions (parameter vs. separate layer) and input data formats (channel-last in TensorFlow vs. channel-first in PyTorch). Their solutions, like symbolic tracing for missing attributes and using permute operations for channel compatibility, are crucial for achieving functional equivalence.
  • Beyond Code Conversion: Functional Equivalence: The authors don't just aim to generate syntactically correct code; they rigorously validate that the migrated networks behave identically to the originals, both in terms of raw output differences and performance on benchmark tasks. This is critical for building trust in automated migration tools.

Key Results

The authors evaluated their approach on five neural networks (AlexNet, VGG16, TF-Tutorial, CNN-RNN, and LSTM) across various migration scenarios between PyTorch and TensorFlow.

  • Migration Success: Their approach successfully migrated all five NNs, with the generated code executing without errors on dummy inputs.
  • Functional Equivalence:
    • When comparing outputs with identical random inputs and weights, the maximum absolute difference (MAD) between the original and migrated networks was extremely small (up to 5.96 × 10⁻⁸), indicating high fidelity.
  • When trained and evaluated on benchmark datasets, the migrated NNs achieved prediction accuracies very close to their original counterparts. The largest difference observed was 0.022 for VGG16 on CIFAR-10.

Limitations and Future Work

  • Limitations:
    • The current approach primarily focuses on PyTorch and TensorFlow. While the pivot model design is extensible, direct support for other frameworks requires developing new transformation modules and code generators.
  • Some specific NN architectures (e.g., LSTM) have inherent limitations in how they can be defined (e.g., requiring subclassing), which can constrain the migration scenarios tested.
  • Future Work:
    • Extend the approach to support a wider range of AI frameworks.
  • Incorporate more advanced deep learning concepts into the BESSER-NN metamodel, such as transformers, graph neural networks, and reinforcement learning.
  • Develop methods for migrating NN testing code to ensure a complete migration solution.
  • Explore refactoring transformations to help designers improve NN structures based on best practices.

Summary updated

BibTeX
@inproceedings{daoudi2026towards,
  author = {Nadia Daoudi and Iván Alfonso and Jordi Cabot},
  title = {{Towards Migrating Neural Network Implementations}},
  booktitle = {AIware},
  year = {2026},
  url = {https://openreview.net/forum?id=AUfwGQYa63},
}
Figure