2022-09-22 12:26:48 -07:00
|
|
|
# Copyright 2020 The JAX Authors.
|
2020-06-22 16:01:03 -07:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2022-11-08 10:49:22 -08:00
|
|
|
import os
|
2022-01-12 14:27:17 -08:00
|
|
|
import gzip
|
|
|
|
import json
|
|
|
|
|
2023-08-08 10:08:19 -07:00
|
|
|
from absl.testing import absltest
|
|
|
|
|
2022-11-08 10:49:22 -08:00
|
|
|
import jax
|
2023-08-08 10:08:19 -07:00
|
|
|
from jax import jit, make_jaxpr, numpy as jnp
|
2023-10-12 13:15:22 +01:00
|
|
|
from jax._src import config
|
2023-08-08 10:08:19 -07:00
|
|
|
from jax._src import jaxpr_util
|
2021-09-24 07:02:08 -07:00
|
|
|
from jax._src import test_util as jtu
|
2023-10-12 13:15:22 +01:00
|
|
|
from jax._src.lib import xla_client
|
2020-06-22 16:01:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
config.parse_flags_with_absl()
|
|
|
|
|
|
|
|
|
|
|
|
class JaxprStatsTest(jtu.JaxTestCase):
|
|
|
|
|
|
|
|
def test_primitives(self):
|
|
|
|
def f(x, y):
|
|
|
|
s = jit(jnp.sin)(x)
|
|
|
|
return jnp.sin(s) + jnp.cos(y)
|
|
|
|
|
2020-07-20 19:04:43 -07:00
|
|
|
hist = jaxpr_util.primitives(make_jaxpr(f)(1., 1.).jaxpr)
|
2020-06-22 16:01:03 -07:00
|
|
|
|
2023-01-09 16:49:35 -08:00
|
|
|
primitives = ['add', 'sin', 'cos']
|
2023-03-15 20:06:42 -07:00
|
|
|
primitives.append('pjit')
|
2023-01-09 16:49:35 -08:00
|
|
|
for k in primitives:
|
2020-06-22 16:01:03 -07:00
|
|
|
assert k in hist, k
|
|
|
|
self.assertEqual(hist['sin'], 2)
|
|
|
|
self.assertTrue(all(count == 1 for k, count in hist.items() if k != 'sin'))
|
|
|
|
|
|
|
|
def test_primitives_by_source(self):
|
|
|
|
def f(x, y):
|
|
|
|
s = jnp.sin(x)
|
|
|
|
return jnp.sin(s) + jnp.cos(y)
|
|
|
|
|
2020-07-20 19:04:43 -07:00
|
|
|
hist = jaxpr_util.primitives_by_source(make_jaxpr(f)(1., 1.).jaxpr)
|
2020-06-22 16:01:03 -07:00
|
|
|
|
|
|
|
sin_keys = [k for k in hist.keys() if k.startswith('sin @ ')]
|
2020-10-13 15:48:04 -07:00
|
|
|
rem_keys = [k for k in hist.keys() if not k.startswith('sin @ ')]
|
|
|
|
|
|
|
|
self.assertEqual(sum(hist[k] for k in sin_keys), 2)
|
|
|
|
self.assertTrue(all(hist[k] == 1 for k in rem_keys))
|
2020-06-22 16:01:03 -07:00
|
|
|
|
|
|
|
def test_primitives_by_shape(self):
|
|
|
|
def f(x, y):
|
|
|
|
def sub(x, y):
|
2024-05-24 01:14:16 +00:00
|
|
|
return jnp.sum(jnp.array([x, y]))
|
|
|
|
s = jit(sub)(x, y)
|
2020-06-22 16:01:03 -07:00
|
|
|
return jnp.sin(s) + jnp.cos(y)
|
|
|
|
|
2020-07-20 19:04:43 -07:00
|
|
|
hist = jaxpr_util.primitives_by_shape(make_jaxpr(f)(1., 1.).jaxpr)
|
2020-06-22 16:01:03 -07:00
|
|
|
|
2023-10-12 13:15:22 +01:00
|
|
|
t = '64' if config.enable_x64.value else '32'
|
2020-06-22 16:01:03 -07:00
|
|
|
shapes = [
|
2020-08-19 07:48:25 -07:00
|
|
|
f'add :: float{t}[]',
|
|
|
|
f'sin :: float{t}[]',
|
|
|
|
f'cos :: float{t}[]',
|
|
|
|
f'reduce_sum :: float{t}[]',
|
|
|
|
f'concatenate :: float{t}[2]',
|
2024-05-24 01:14:16 +00:00
|
|
|
f'pjit :: float{t}[]',
|
2020-06-22 16:01:03 -07:00
|
|
|
]
|
|
|
|
for k in shapes:
|
|
|
|
self.assertEqual(hist[k], 1)
|
|
|
|
|
|
|
|
def test_source_locations(self):
|
|
|
|
def f(x, y):
|
|
|
|
s = jnp.sin(x) # sin
|
|
|
|
return jnp.sin(s) + jnp.cos(y) # sin, cos, add
|
|
|
|
|
2020-07-20 19:04:43 -07:00
|
|
|
hist = jaxpr_util.source_locations(make_jaxpr(f)(1., 1.).jaxpr)
|
2020-10-13 15:48:04 -07:00
|
|
|
self.assertEqual(sum(hist.values()), 4)
|
2020-06-22 16:01:03 -07:00
|
|
|
|
2022-11-08 10:49:22 -08:00
|
|
|
def test_source_locations_exclude_contextlib(self):
|
|
|
|
|
|
|
|
def f(x):
|
|
|
|
# This generates a stack where the most recent non-jax frame
|
|
|
|
# comes from contextlib.
|
|
|
|
return jax.named_call(jnp.cos, name='test')(x)
|
|
|
|
|
2023-02-08 10:16:42 -08:00
|
|
|
hist = jaxpr_util.source_locations(make_jaxpr(f)(jnp.arange(8.)).jaxpr)
|
2022-11-08 10:49:22 -08:00
|
|
|
for filename in hist.keys():
|
|
|
|
self.assertIn(os.path.basename(__file__), filename)
|
|
|
|
|
2020-06-22 16:01:03 -07:00
|
|
|
def test_print_histogram(self):
|
|
|
|
def f(x, y):
|
|
|
|
s = jit(jnp.sin)(x)
|
|
|
|
return jnp.sin(s) + jnp.cos(y)
|
2020-07-20 19:04:43 -07:00
|
|
|
hist = jaxpr_util.primitives_by_source(make_jaxpr(f)(1., 1.).jaxpr)
|
|
|
|
jaxpr_util.print_histogram(hist)
|
2020-06-22 16:01:03 -07:00
|
|
|
|
2022-01-12 14:27:17 -08:00
|
|
|
def test_pprof_equation_profile(self):
|
|
|
|
def f(x, y):
|
|
|
|
s = jit(jnp.sin)(x)
|
|
|
|
return jnp.sin(s) + jnp.cos(y)
|
|
|
|
profile_gz = jaxpr_util.pprof_equation_profile(make_jaxpr(f)(1., 1.).jaxpr)
|
|
|
|
profile_proto = gzip.decompress(profile_gz)
|
|
|
|
json_str = xla_client._xla.pprof_profile_to_json(profile_proto)
|
|
|
|
profile = json.loads(json_str)
|
|
|
|
self.assertSetEqual(
|
|
|
|
{"sampleType", "sample", "stringTable", "location", "function"},
|
|
|
|
set(profile.keys()))
|
|
|
|
|
2020-06-22 16:01:03 -07:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
absltest.main()
|