Skip to content

Commit

Permalink
Add initial nnx/optax test
Browse files Browse the repository at this point in the history
  • Loading branch information
jakevdp committed Aug 19, 2024
1 parent 46cf3a0 commit 259cf37
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Test

on:
# Trigger the workflow on push or pull request, but only on main branch
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read # to fetch code

jobs:
build:
name: ${{ matrix.os }} Python ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
include:
- os: macOS-11
python-version: "3.11"
- os: windows-2019
python-version: "3.11"

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # ratchet:actions/checkout@v4
with:
submodules: true
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # ratchet:actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[dev]
- name: Run tests
run: |
pytest -n auto jax_ml_stack
Empty file added jax_ml_stack/tests/__init__.py
Empty file.
53 changes: 53 additions & 0 deletions jax_ml_stack/tests/test_nnx_with_optax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2024 The JAX Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import jax
import jax.numpy as jnp
from flax import nnx
import optax


class SimpleModel(nnx.Module):

def __init__(self, rngs):
self.layer1 = nnx.Linear(2, 5, rngs=rngs)
self.layer2 = nnx.Linear(5, 3, rngs=rngs)

def __call__(self, x):
for layer in [self.layer1, self.layer2]:
x = layer(x)
return x


class NNXOptaxTest(unittest.TestCase):

def test_nnx_optax(self):
key = jax.random.key(1701)
x = jax.random.normal(key, (1, 2))
y = jnp.ones((1, 3))

model = SimpleModel(nnx.Rngs(0))
optimizer = optax.adam(learning_rate=1e-3)
state = nnx.Optimizer(model, optimizer)

def loss(model, x=x, y=y):
return jnp.mean((model(x) - y) ** 2)

initial_loss = loss(model)
grads = nnx.grad(loss, wrt=nnx.Param)(state.model)
state.update(grads)
final_loss = loss(model)

self.assertNotAlmostEqual(initial_loss, final_loss)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ keywords = []
# pip dependencies of the project
dependencies = [
"jax",
"flax",
"optax",
]

[project.urls]
Expand All @@ -29,8 +31,6 @@ repository = "https://github.com/jax-ml/jax_ml_stack"
dev = [
"pytest",
"pytest-xdist",
"pylint>=2.6.0",
"pyink",
]

[tool.pyink]
Expand Down

0 comments on commit 259cf37

Please sign in to comment.