graph TD
A[源框架代码<br/>PyTorch / TF / JAX / NumPy] --> B[前端解析器<br/>Frontend Parser]
B --> C[Ivy 抽象语法树<br/>统一 API 调用]
C --> D[Ivy 核心层<br/>可微分 - 可组合函数]
D --> E{"目标后端?"}
E -->|"PyTorch"| F[PyTorch 后端映射]
E -->|"TensorFlow"| G[TensorFlow 后端映射]
E -->|"JAX"| H[JAX 后端映射]
E -->|"NumPy"| I[NumPy 后端映射]
F --> J[生成目标代码]
G --> J
H --> J
I --> J
但生产落地要谨慎。首先,Ivy 的转换覆盖面依赖于它对各框架 API 的支持程度,冷门算子或自定义 CUDA 扩展很可能未被覆盖,需要手写后端映射。其次,代码级转换会带来额外解析开销,对延迟敏感的服务不建议在运行时做转换,而应在构建阶段预生成目标代码。最后,务必对转换后的数值结果做逐层比对,尤其是涉及随机数和精度敏感的操作。
ivy enables you to convert ML models, tools and libraries between frameworks using ivy.transpile
Installation
The easiest way to install ivy is using pip:
pip install ivy
From Source
You can also install ivy from source if you want to take advantage of
the latest changes:
git clone https://github.com/ivy-llc/ivy.git
cd ivy
pip install --user -e .
Supported Frameworks
These are the frameworks that ivy.transpile currently supports conversions from and to.
Framework
Source
Target
PyTorch
✓
planned
TensorFlow
planned
✓
JAX
planned
✓
NumPy
planned
✓
Using ivy
Here’s some examples, to help you get started using ivy! The examples page also features a wide range of
demos and tutorials showcasing some more use cases for ivy.
Transpiling any code from one framework to another
import ivy
import torch
import tensorflow as tf
def torch_fn(x):
a = torch.mul(x, x)
b = torch.mean(x)
return x * a + b
tf_fn = ivy.transpile(torch_fn, source="torch", target="tensorflow")
tf_x = tf.convert_to_tensor([1., 2., 3.])
ret = tf_fn(tf_x)
Tracing a computational graph of any code
import ivy
import torch
def torch_fn(x):
a = torch.mul(x, x)
b = torch.mean(x)
return x * a + b
torch_x = torch.tensor([1., 2., 3.])
graph = ivy.trace_graph(jax_fn, to="torch", args=(torch_x,))
ret = graph(torch_x)
How does ivy work?
ivy's transpiler allows you to use code from any other framework in your own code.
Feel free to head over to the docs for the full API
reference, but the functions you'd most likely want to use are:
# Converts framework-specific code to a target framework of choice. See usage in the documentation
ivy.transpile()
# Traces an efficient fully-functional graph from a function, removing all wrapping and redundant code. See usage in the documentation
ivy.trace_graph()
ivy.transpile will eagerly transpile if a class or function is provided
import ivy
import torch
import tensorflow as tf
def torch_fn(x):
x = torch.abs(x)
return torch.sum(x)
x1 = torch.tensor([1., 2.])
x1 = tf.convert_to_tensor([1., 2.])
# Transpilation happens eagerly
tf_fn = ivy.transpile(test_fn, source="torch", target="tensorflow")
# tf_fn is now tensorflow code and runs efficiently
ret = tf_fn(x1)
ivy.transpile will lazily transpile if a module (library) is provided
import ivy
import kornia
import tensorflow as tf
x2 = tf.random.normal((5, 3, 4, 4))
# Module is provided -> transpilation happens lazily
tf_kornia = ivy.transpile(kornia, source="torch", target="tensorflow")
# The transpilation is initialized here, and this function is converted to tensorflow
ret = tf_kornia.color.rgb_to_grayscale(x2)
# Transpilation has already occurred, the tensorflow function runs efficiently
ret = tf_kornia.color.rgb_to_grayscale(x2)
Contributing
We believe that everyone can contribute and make a difference. Whether
it's writing code, fixing bugs, or simply sharing feedback,
your contributions are definitely welcome and appreciated"
@article{lenton2021ivy,
title={Ivy: Templated deep learning for inter-framework portability},
author={Lenton, Daniel and Pardo, Fabio and Falck, Fabian and James, Stephen and Clark, Ronald},
journal={arXiv preprint arXiv:2102.02886},
year={2021}
}
评论