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 Setting Up section of the docs.

!git clone https://github.com/unifyai/ivy.git
!cd ivy && git checkout d6bc18c64a47a135fe18404d9f83f98d9f3b63cf && python3 -m pip install --user -e .

For the installed packages to be available you will have to restart your kernel. In Colab, you can do this by clicking on “Runtime > Restart Runtime”. Once the runtime has been restarted you should skip the previous cell 😄

To use the compiler and the transpiler now you will need an API Key. If you already have one, you should replace the string in the next cell.

API_KEY = "PASTE_YOUR_KEY_HERE"
!mkdir -p .ivy
!echo -n $API_KEY > .ivy/key.pem

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! ⚡