Giter VIP home page Giter VIP logo

pax's Introduction

PAX Logo

Sharing some of JAX's beautiful API with PyTorch users.

Disambiguation: for wardrobes, see link. For peace, you are at the right spot.

Installation

pip install paxlib

or

pip install git+https://github.com/epfml/pax.git

Pytrees in PyTorch

import torch
import pax

tree = {
    "a": [torch.tensor(3.0), torch.tensor(4.0)],
    "c": 4
}
pax.tree_map(lambda x: x*2, tree)

Note: we currently depend on jax for this functionality, but we could use dm-tree instead to drop the dependency.

Autodiff that looks like JAX

We follow the API of jax.grad:

import pax

f = lambda x: x**2
df_dx = pax.grad(f)
df_dx(2.0)  # tensor(4.0)

This works with any Pytree as input:

def f(x):
    return x["a"] * x["b"]

x = {"a": 2.0, "b": -1.5}
pax.value_and_grad(f)(x)  # (tensor(-3.), {'a': tensor(-1.5000), 'b': tensor(2.)})

PAX also supports higher-order derivatives:

f = lambda x: 1/6 * x**3
pax.grad(f)(2.0)  # tensor(2.)
pax.grad(pax.grad(f))(3.0) # tensor(3.)

Example: Minimal SGD

import torch
import pax

f = lambda x: x**2
df_dx = pax.grad(f)

x = torch.randn([])  # initialization
for step in range(20):
    x = x - 0.1 * df_dx(x)
    print(x, f(x))

Example: meta-learning the learning rate

f = lambda x: x**2
df_dx = pax.grad(f)

def sgd(x, lr=0.1, num_steps=10):
    for _ in range(num_steps):
        x = x - lr * df_dx(x)
    return x

# optimize the learning rate
def meta_loss(lr):
    x0 = 1.0
    return f(sgd(x0, lr=lr))

df_dlr = pax.grad(meta_loss)

lr = 0.1
for _ in range(100):
    lr = lr - 0.1 * df_dlr(lr)

Converting from PyTorch

We provide a small wrapper for PyTorch modules to make them behave like Haiku.

net = torch.nn.Linear(10, 1)  # any torch.nn.Module

# convert
forward = pax.functional_module(net)

# intialize
params, buffers = pax.get_params(net), pax.get_buffers(net)

# run
data_batch = torch.zeros(2, 10)
out, buffers = forward(params, data_batch, buffers=buffers, is_training=True)

and also a wrapper to make PyTorch optimizers functional like Optax:

optimizer = pax.functional_optimizer(torch.optim.Adam, lr=1e-3)

f = lambda x: x**2
df_dx = pax.grad(f)
params = torch.tensor(3.)
opt_state = optimizer.init(params)

for step in range(10):
    params, opt_state = optimizer.step(params, df_dx(params), opt_state)
    print(params.item())

Using PAX optimizers with learning rate schedulers looks like this:

optimizer = pax.functional_optimizer(torch.optim.SGD, lr=0)
lr_at_step = pax.functional_schedule(torch.optim.lr_scheduler.LambdaLR, lr_lambda=lambda step: 1/(step+1), initial_lr=0.1)

f = lambda x: x**2
df_dx = pax.grad(f)
params = torch.tensor(3.)
opt_state = optimizer.init(params)

for step in range(10):
    params, opt_state = optimizer.step(params, df_dx(params), opt_state, lr=lr_at_step(step))
    print(params.item())

Runtime overhead

We measured the time required for one epoch on CIFAR-10 with a batch size of 128. We compare a standard PyTorch implementation based on this tutorial to a PAX one, using pax.value_and_grad, pax.functional_module and pax.functional_optimizer. This is currently a little slower than regular PyTorch code. The peak memory usage could be larger too.

PAX Benchmark

pax's People

Contributors

pestrstr avatar tvogels avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.