• Github
  • Docs
  • Discord
  1. Learn the Basics
  2. Unify code
  • Quickstart
  • Learn the Basics
    • Write Ivy code
    • Unify code
    • Compile code
    • Transpile code
    • Lazy vs Eager
    • How to use decorators
    • Transpile any library
    • Transpile any model
  • Guides
    • Transpiling a PyTorch model to build on top
    • Transpiling a Tensorflow model to build on top
  • Examples and Demos
    • Using Ivy ResNet
    • Accelerating PyTorch models with JAX
    • Accelerating MMPreTrain models with JAX
    • Image Segmentation with Ivy UNet
    • Ivy AlexNet demo

On this page

  • Round Up

Unify code

Unify a simple torch function and use it alongside any ML framework!

⚠️ If you are running this notebook in Colab, you will have to install Ivy and some dependencies manually. You can do so by running the cell below ⬇️

If you want to run the notebook locally but don’t have Ivy installed just yet, you can check out the Get Started section of the docs.

!pip install ivy

Firstly, let’s import the dependencies and define a torch function.

import ivy
import torch

def normalize(x):
    mean = torch.mean(x)
    std = torch.std(x)
    return torch.div(torch.sub(x, mean), std)

By using ivy.unify(), you can convert any code from any framework into Ivy code, which as we have already seen, can be executed using any framework as the backend.

Let’s unify the function!

normalize = ivy.unify(normalize, source="torch")

And that’s it! The normalize function can now be used with any ML framework. It’s as simple as that!

So, let’s give it a try!

# import the frameworks
import numpy as np
import jax.numpy as jnp
import tensorflow as tf
# create random numpy arrays for testing
x = np.random.uniform(size=10).astype(np.float32)
ivy.set_backend("numpy")
print(normalize(x))

# jax
x_ = jnp.array(x)
ivy.set_backend("jax")
print(normalize(x_))

# tensorflow
x_ = tf.constant(x)
ivy.set_backend("tensorflow")
print(normalize(x_))

# torch
x_ = torch.tensor(x)
ivy.set_backend("torch")
print(normalize(x_))
ivy.array([ 0.82997245,  0.44733784, -0.32163444, -1.93330479, -0.52438271,
       -0.20438017,  1.252316  ,  0.0827222 ,  1.26017165, -0.88881904])
ivy.array([ 0.82997245,  0.44733784, -0.32163444, -1.93330479, -0.52438271,
       -0.20438017,  1.252316  ,  0.0827222 ,  1.26017165, -0.88881904])
ivy.array([ 0.82997245,  0.44733784, -0.32163444, -1.93330479, -0.52438271,
       -0.20438017,  1.252316  ,  0.0827222 ,  1.26017165, -0.88881904])
ivy.array([ 0.82997245,  0.44733784, -0.32163444, -1.93330479, -0.52438271,
       -0.20438017,  1.252316  ,  0.0827222 ,  1.26017165, -0.88881904])

We can see that the new normalize function can operate with any ML framework. ivy.unify converts the framework-specific torch implementation into a framework-agnostic ivy implementation, which is compatible with all frameworks.

Round Up

That’s it, you can now unify ML code! However, there are several other important topics to master before you’re ready to unify ML code like a pro 🥷. Next, we’ll be learning how to make our unified Ivy code run much more efficiently! ⚡

Write Ivy code
Compile code