Skip to content

Add Pytorch Backend Support to Ivy Transpiler  #28848

@YushaArif99

Description

@YushaArif99

Description:
The current implementation of ivy.transpile supports "jax","numpy" and "tensorflow" as valid target argument. This allows transpiling framework-specific code such as PyTorch functions or classes to target frameworks like TensorFlow, JAX, or NumPy. This task aims to extend the functionality by adding Pytorch as a valid target, enabling code to be transpiled to Pytorch.

For example, after completing this task, we should be able to transpile TensorFlow code using:

ivy.transpile(func, source="tensorflow", target="torch")

Goals:

The main objective is to implement the final stage of the transpilation pipeline:

  1. Lower Ivy IR representation to native Pytorch code.

Once this stage is complete, the rest of the pipeline can be reused to target other frameworks like JAX, PyTorch, or NumPy. The steps would look as follows:

source='tensorflow' → target='tensorflow_frontend'  
source='tensorflow_frontend' → target='ivy'  
source='ivy' → target='torch'  

This mirrors the existing pipeline for eg: PyTorch -> TF:

source='torch' → target='torch_frontend'  
source='torch_frontend' → target='ivy'  
source='ivy' → target='tensorflow'  

Key Tasks:

  1. Add Native Framework-Specific Implementations for Core Transformation Passes:
    • For example, implement the ivy_to_torch_postprocessing_transformer.py for doing postprocessing on the Ivy code when lowering it to Pytorch (eg: mapping ivy dtypes (eg: ivy.float32) to torch dtypes(torch.float32)
    • Use ivy_to_jax_postprocessing_transformer.py as a reference (example here)

2 Define the Transformation Pipeline for Ivy IR to Torch

  • Create a new pipeline in ivy_to_source_translator_config.py to handle the stage source='ivy', target='torch' (example here).
  1. Add Stateful Classes for torch.nn APIs:

    • Add a subclass implementation for torch.nn.Module that exposes the similar interface as that of ivy.Module.
    • Reference the existing implementation for keras.Model or nnx.Module(example 1) (example 2)
    • This allows for sequential lowering for eg:
      keras.Model → (frontend keras.Model) → ivy.Module → nn.Module
      
  2. Understand and Leverage Reusability:

    • Explore reusable components in the existing PyTorch pipeline, especially for AST transformers and configuration management.

Testing:

  • Familiarize yourself with the transpilation flow by exploring transpiler tests
  • Add appropriate tests to validate Pytorch target transpilation at the final stage of the pipeline.

Additional Notes:

  • Keep in mind the modular and extensible design of the transpiler, ensuring that the new implementation integrates smoothly into the existing architecture.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ToDoA ToDo list of tasksTranspilerAnything related to transpiling

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions