rocm_jax/tests/pmap_test.py

70 lines
1.9 KiB
Python
Raw Normal View History

2019-01-28 11:13:34 -08:00
# Copyright 2018 Google LLC
#
# 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.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
2019-02-23 20:34:14 -08:00
from functools import partial
2019-01-28 11:13:34 -08:00
import numpy as onp
from absl.testing import absltest
from absl.testing import parameterized
import jax.numpy as np
from jax import test_util as jtu
2019-02-23 20:34:14 -08:00
from jax import lax
from jax.api import pmap, vmap, jvp, grad, make_jaxpr, linearize
2019-01-31 22:08:51 -08:00
from jax.lax import psum
2019-02-23 20:34:14 -08:00
from jax.lib import xla_bridge
2019-01-28 11:13:34 -08:00
from jax.config import config
config.parse_flags_with_absl()
class PmapTest(jtu.JaxTestCase):
2019-02-23 20:34:14 -08:00
@jtu.skip_on_devices("gpu", "tpu")
def testNestedWithClosure(self):
assert xla_bridge.get_replica_count() == 1 # OSS CPU testing only
x = onp.arange(3, dtype=onp.float32).reshape(1, 1, 3)
@partial(pmap, axis_name='i')
2019-02-23 20:34:14 -08:00
def test_fun(x):
y = np.sum(np.sin(x))
@partial(pmap, axis_name='j')
2019-02-23 20:34:14 -08:00
def g(z):
return 3. * np.exp(np.sin(x).sum() * np.cos(y) * np.tan(z))
return grad(lambda w: np.sum(g(w)))(x)
@vmap
def baseline_fun(x):
y = np.sum(np.sin(x))
@vmap
def g(z):
return 3. * np.exp(np.sin(x).sum() * np.cos(y) * np.tan(z))
return grad(lambda w: np.sum(g(w)))(x)
ans = grad(lambda x: np.sum(test_fun(x)))(x)
expected = grad(lambda x: np.sum(baseline_fun(x)))(x)
self.assertAllClose(ans, expected, check_dtypes=True)
2019-02-01 16:59:28 -08:00
2019-01-28 11:13:34 -08:00
if __name__ == '__main__':
absltest.main()