# Copyright 2020 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 absl.testing import absltest

from jax import jaxpr_util, jit, make_jaxpr, numpy as jnp
from jax import test_util as jtu
from jax.config import config


config.parse_flags_with_absl()
FLAGS = config.FLAGS


class JaxprStatsTest(jtu.JaxTestCase):

  def test_primitives(self):
    def f(x, y):
      s = jit(jnp.sin)(x)
      return jnp.sin(s) + jnp.cos(y)

    hist = jaxpr_util.primitives(make_jaxpr(f)(1., 1.).jaxpr)

    for k in ['add', 'sin', 'cos', 'xla_call']:
      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)

    hist = jaxpr_util.primitives_by_source(make_jaxpr(f)(1., 1.).jaxpr)

    sin_keys = [k for k in hist.keys() if k.startswith('sin @ ')]
    self.assertEqual(len(sin_keys), 2)
    self.assertTrue(all(count == 1 for count in hist.values()))

  def test_primitives_by_shape(self):
    def f(x, y):
      def sub(x, y):
        return jnp.sum(jnp.array([x, y])), y
      s, _ = jit(sub)(x, y)
      return jnp.sin(s) + jnp.cos(y)

    hist = jaxpr_util.primitives_by_shape(make_jaxpr(f)(1., 1.).jaxpr)

    shapes = [
        'add :: float32[]',
        'sin :: float32[]',
        'cos :: float32[]',
        'reduce_sum :: float32[]',
        'concatenate :: float32[2]',
        'xla_call :: float32[] *',
    ]
    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

    hist = jaxpr_util.source_locations(make_jaxpr(f)(1., 1.).jaxpr)
    self.assertEqual(set(hist.values()), set([1, 3]))

  def test_print_histogram(self):
    def f(x, y):
      s = jit(jnp.sin)(x)
      return jnp.sin(s) + jnp.cos(y)
    hist = jaxpr_util.primitives_by_source(make_jaxpr(f)(1., 1.).jaxpr)
    jaxpr_util.print_histogram(hist)


if __name__ == "__main__":
  absltest.main()

@@ -1799,24 +1799,71 @@
 1.).jaxpr)%0A%0A
+    t = '64' if FLAGS.jax_enable_x64 else '32'%0A
     shapes =
@@ -1873,16 +1873,17 @@
         
+f
 'add :: 
@@ -1879,34 +1879,35 @@
   f'add :: float
-32
+%7Bt%7D
 %5B%5D',%0A        'si
@@ -1903,16 +1903,17 @@
         
+f
 'sin :: 
@@ -1909,34 +1909,35 @@
   f'sin :: float
-32
+%7Bt%7D
 %5B%5D',%0A        'co
@@ -1933,16 +1933,17 @@
         
+f
 'cos :: 
@@ -1939,34 +1939,35 @@
   f'cos :: float
-32
+%7Bt%7D
 %5B%5D',%0A        're
@@ -1963,16 +1963,17 @@
         
+f
 'reduce_
@@ -1984,18 +1984,19 @@
 :: float
-32
+%7Bt%7D
 %5B%5D',%0A   
@@ -2000,16 +2000,17 @@
         
+f
 'concate
@@ -2022,18 +2022,19 @@
 :: float
-32
+%7Bt%7D
 %5B2%5D',%0A  
@@ -2039,16 +2039,17 @@
         
+f
 'xla_cal
@@ -2058,18 +2058,19 @@
 :: float
-32
+%7Bt%7D
 %5B%5D *',%0A 

fix jaxpr util test under enable_x64