test_jax.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. from sympy.concrete.summations import Sum
  2. from sympy.core.mod import Mod
  3. from sympy.core.relational import (Equality, Unequality)
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.functions.elementary.piecewise import Piecewise
  6. from sympy.matrices.expressions.blockmatrix import BlockMatrix
  7. from sympy.matrices.expressions.matexpr import MatrixSymbol
  8. from sympy.matrices.expressions.special import Identity
  9. from sympy.utilities.lambdify import lambdify
  10. from sympy.abc import x, i, j, a, b, c, d
  11. from sympy.core import Pow
  12. from sympy.codegen.matrix_nodes import MatrixSolve
  13. from sympy.codegen.numpy_nodes import logaddexp, logaddexp2
  14. from sympy.codegen.cfunctions import log1p, expm1, hypot, log10, exp2, log2, Sqrt
  15. from sympy.tensor.array import Array
  16. from sympy.tensor.array.expressions.array_expressions import ArrayTensorProduct, ArrayAdd, \
  17. PermuteDims, ArrayDiagonal
  18. from sympy.printing.numpy import JaxPrinter, _jax_known_constants, _jax_known_functions
  19. from sympy.tensor.array.expressions.from_matrix_to_array import convert_matrix_to_array
  20. from sympy.testing.pytest import skip, raises
  21. from sympy.external import import_module
  22. # Unlike NumPy which will aggressively promote operands to double precision,
  23. # jax always uses single precision. Double precision in jax can be
  24. # configured before the call to `import jax`, however this must be explicitly
  25. # configured and is not fully supported. Thus, the tests here have been modified
  26. # from the tests in test_numpy.py, only in the fact that they assert lambdify
  27. # function accuracy to only single precision accuracy.
  28. # https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
  29. jax = import_module('jax')
  30. if jax:
  31. deafult_float_info = jax.numpy.finfo(jax.numpy.array([]).dtype)
  32. JAX_DEFAULT_EPSILON = deafult_float_info.eps
  33. def test_jax_piecewise_regression():
  34. """
  35. NumPyPrinter needs to print Piecewise()'s choicelist as a list to avoid
  36. breaking compatibility with numpy 1.8. This is not necessary in numpy 1.9+.
  37. See gh-9747 and gh-9749 for details.
  38. """
  39. printer = JaxPrinter()
  40. p = Piecewise((1, x < 0), (0, True))
  41. assert printer.doprint(p) == \
  42. 'jax.numpy.select([jax.numpy.less(x, 0),True], [1,0], default=jax.numpy.nan)'
  43. assert printer.module_imports == {'jax.numpy': {'select', 'less', 'nan'}}
  44. def test_jax_logaddexp():
  45. lae = logaddexp(a, b)
  46. assert JaxPrinter().doprint(lae) == 'jax.numpy.logaddexp(a, b)'
  47. lae2 = logaddexp2(a, b)
  48. assert JaxPrinter().doprint(lae2) == 'jax.numpy.logaddexp2(a, b)'
  49. def test_jax_sum():
  50. if not jax:
  51. skip("JAX not installed")
  52. s = Sum(x ** i, (i, a, b))
  53. f = lambdify((a, b, x), s, 'jax')
  54. a_, b_ = 0, 10
  55. x_ = jax.numpy.linspace(-1, +1, 10)
  56. assert jax.numpy.allclose(f(a_, b_, x_), sum(x_ ** i_ for i_ in range(a_, b_ + 1)))
  57. s = Sum(i * x, (i, a, b))
  58. f = lambdify((a, b, x), s, 'jax')
  59. a_, b_ = 0, 10
  60. x_ = jax.numpy.linspace(-1, +1, 10)
  61. assert jax.numpy.allclose(f(a_, b_, x_), sum(i_ * x_ for i_ in range(a_, b_ + 1)))
  62. def test_jax_multiple_sums():
  63. if not jax:
  64. skip("JAX not installed")
  65. s = Sum((x + j) * i, (i, a, b), (j, c, d))
  66. f = lambdify((a, b, c, d, x), s, 'jax')
  67. a_, b_ = 0, 10
  68. c_, d_ = 11, 21
  69. x_ = jax.numpy.linspace(-1, +1, 10)
  70. assert jax.numpy.allclose(f(a_, b_, c_, d_, x_),
  71. sum((x_ + j_) * i_ for i_ in range(a_, b_ + 1) for j_ in range(c_, d_ + 1)))
  72. def test_jax_codegen_einsum():
  73. if not jax:
  74. skip("JAX not installed")
  75. M = MatrixSymbol("M", 2, 2)
  76. N = MatrixSymbol("N", 2, 2)
  77. cg = convert_matrix_to_array(M * N)
  78. f = lambdify((M, N), cg, 'jax')
  79. ma = jax.numpy.array([[1, 2], [3, 4]])
  80. mb = jax.numpy.array([[1,-2], [-1, 3]])
  81. assert (f(ma, mb) == jax.numpy.matmul(ma, mb)).all()
  82. def test_jax_codegen_extra():
  83. if not jax:
  84. skip("JAX not installed")
  85. M = MatrixSymbol("M", 2, 2)
  86. N = MatrixSymbol("N", 2, 2)
  87. P = MatrixSymbol("P", 2, 2)
  88. Q = MatrixSymbol("Q", 2, 2)
  89. ma = jax.numpy.array([[1, 2], [3, 4]])
  90. mb = jax.numpy.array([[1,-2], [-1, 3]])
  91. mc = jax.numpy.array([[2, 0], [1, 2]])
  92. md = jax.numpy.array([[1,-1], [4, 7]])
  93. cg = ArrayTensorProduct(M, N)
  94. f = lambdify((M, N), cg, 'jax')
  95. assert (f(ma, mb) == jax.numpy.einsum(ma, [0, 1], mb, [2, 3])).all()
  96. cg = ArrayAdd(M, N)
  97. f = lambdify((M, N), cg, 'jax')
  98. assert (f(ma, mb) == ma+mb).all()
  99. cg = ArrayAdd(M, N, P)
  100. f = lambdify((M, N, P), cg, 'jax')
  101. assert (f(ma, mb, mc) == ma+mb+mc).all()
  102. cg = ArrayAdd(M, N, P, Q)
  103. f = lambdify((M, N, P, Q), cg, 'jax')
  104. assert (f(ma, mb, mc, md) == ma+mb+mc+md).all()
  105. cg = PermuteDims(M, [1, 0])
  106. f = lambdify((M,), cg, 'jax')
  107. assert (f(ma) == ma.T).all()
  108. cg = PermuteDims(ArrayTensorProduct(M, N), [1, 2, 3, 0])
  109. f = lambdify((M, N), cg, 'jax')
  110. assert (f(ma, mb) == jax.numpy.transpose(jax.numpy.einsum(ma, [0, 1], mb, [2, 3]), (1, 2, 3, 0))).all()
  111. cg = ArrayDiagonal(ArrayTensorProduct(M, N), (1, 2))
  112. f = lambdify((M, N), cg, 'jax')
  113. assert (f(ma, mb) == jax.numpy.diagonal(jax.numpy.einsum(ma, [0, 1], mb, [2, 3]), axis1=1, axis2=2)).all()
  114. def test_jax_relational():
  115. if not jax:
  116. skip("JAX not installed")
  117. e = Equality(x, 1)
  118. f = lambdify((x,), e, 'jax')
  119. x_ = jax.numpy.array([0, 1, 2])
  120. assert jax.numpy.array_equal(f(x_), [False, True, False])
  121. e = Unequality(x, 1)
  122. f = lambdify((x,), e, 'jax')
  123. x_ = jax.numpy.array([0, 1, 2])
  124. assert jax.numpy.array_equal(f(x_), [True, False, True])
  125. e = (x < 1)
  126. f = lambdify((x,), e, 'jax')
  127. x_ = jax.numpy.array([0, 1, 2])
  128. assert jax.numpy.array_equal(f(x_), [True, False, False])
  129. e = (x <= 1)
  130. f = lambdify((x,), e, 'jax')
  131. x_ = jax.numpy.array([0, 1, 2])
  132. assert jax.numpy.array_equal(f(x_), [True, True, False])
  133. e = (x > 1)
  134. f = lambdify((x,), e, 'jax')
  135. x_ = jax.numpy.array([0, 1, 2])
  136. assert jax.numpy.array_equal(f(x_), [False, False, True])
  137. e = (x >= 1)
  138. f = lambdify((x,), e, 'jax')
  139. x_ = jax.numpy.array([0, 1, 2])
  140. assert jax.numpy.array_equal(f(x_), [False, True, True])
  141. # Multi-condition expressions
  142. e = (x >= 1) & (x < 2)
  143. f = lambdify((x,), e, 'jax')
  144. x_ = jax.numpy.array([0, 1, 2])
  145. assert jax.numpy.array_equal(f(x_), [False, True, False])
  146. e = (x >= 1) | (x < 2)
  147. f = lambdify((x,), e, 'jax')
  148. x_ = jax.numpy.array([0, 1, 2])
  149. assert jax.numpy.array_equal(f(x_), [True, True, True])
  150. def test_jax_mod():
  151. if not jax:
  152. skip("JAX not installed")
  153. e = Mod(a, b)
  154. f = lambdify((a, b), e, 'jax')
  155. a_ = jax.numpy.array([0, 1, 2, 3])
  156. b_ = 2
  157. assert jax.numpy.array_equal(f(a_, b_), [0, 1, 0, 1])
  158. a_ = jax.numpy.array([0, 1, 2, 3])
  159. b_ = jax.numpy.array([2, 2, 2, 2])
  160. assert jax.numpy.array_equal(f(a_, b_), [0, 1, 0, 1])
  161. a_ = jax.numpy.array([2, 3, 4, 5])
  162. b_ = jax.numpy.array([2, 3, 4, 5])
  163. assert jax.numpy.array_equal(f(a_, b_), [0, 0, 0, 0])
  164. def test_jax_pow():
  165. if not jax:
  166. skip('JAX not installed')
  167. expr = Pow(2, -1, evaluate=False)
  168. f = lambdify([], expr, 'jax')
  169. assert f() == 0.5
  170. def test_jax_expm1():
  171. if not jax:
  172. skip("JAX not installed")
  173. f = lambdify((a,), expm1(a), 'jax')
  174. assert abs(f(1e-10) - 1e-10 - 5e-21) <= 1e-10 * JAX_DEFAULT_EPSILON
  175. def test_jax_log1p():
  176. if not jax:
  177. skip("JAX not installed")
  178. f = lambdify((a,), log1p(a), 'jax')
  179. assert abs(f(1e-99) - 1e-99) <= 1e-99 * JAX_DEFAULT_EPSILON
  180. def test_jax_hypot():
  181. if not jax:
  182. skip("JAX not installed")
  183. assert abs(lambdify((a, b), hypot(a, b), 'jax')(3, 4) - 5) <= JAX_DEFAULT_EPSILON
  184. def test_jax_log10():
  185. if not jax:
  186. skip("JAX not installed")
  187. assert abs(lambdify((a,), log10(a), 'jax')(100) - 2) <= JAX_DEFAULT_EPSILON
  188. def test_jax_exp2():
  189. if not jax:
  190. skip("JAX not installed")
  191. assert abs(lambdify((a,), exp2(a), 'jax')(5) - 32) <= JAX_DEFAULT_EPSILON
  192. def test_jax_log2():
  193. if not jax:
  194. skip("JAX not installed")
  195. assert abs(lambdify((a,), log2(a), 'jax')(256) - 8) <= JAX_DEFAULT_EPSILON
  196. def test_jax_Sqrt():
  197. if not jax:
  198. skip("JAX not installed")
  199. assert abs(lambdify((a,), Sqrt(a), 'jax')(4) - 2) <= JAX_DEFAULT_EPSILON
  200. def test_jax_sqrt():
  201. if not jax:
  202. skip("JAX not installed")
  203. assert abs(lambdify((a,), sqrt(a), 'jax')(4) - 2) <= JAX_DEFAULT_EPSILON
  204. def test_jax_matsolve():
  205. if not jax:
  206. skip("JAX not installed")
  207. M = MatrixSymbol("M", 3, 3)
  208. x = MatrixSymbol("x", 3, 1)
  209. expr = M**(-1) * x + x
  210. matsolve_expr = MatrixSolve(M, x) + x
  211. f = lambdify((M, x), expr, 'jax')
  212. f_matsolve = lambdify((M, x), matsolve_expr, 'jax')
  213. m0 = jax.numpy.array([[1, 2, 3], [3, 2, 5], [5, 6, 7]])
  214. assert jax.numpy.linalg.matrix_rank(m0) == 3
  215. x0 = jax.numpy.array([3, 4, 5])
  216. assert jax.numpy.allclose(f_matsolve(m0, x0), f(m0, x0))
  217. def test_16857():
  218. if not jax:
  219. skip("JAX not installed")
  220. a_1 = MatrixSymbol('a_1', 10, 3)
  221. a_2 = MatrixSymbol('a_2', 10, 3)
  222. a_3 = MatrixSymbol('a_3', 10, 3)
  223. a_4 = MatrixSymbol('a_4', 10, 3)
  224. A = BlockMatrix([[a_1, a_2], [a_3, a_4]])
  225. assert A.shape == (20, 6)
  226. printer = JaxPrinter()
  227. assert printer.doprint(A) == 'jax.numpy.block([[a_1, a_2], [a_3, a_4]])'
  228. def test_issue_17006():
  229. if not jax:
  230. skip("JAX not installed")
  231. M = MatrixSymbol("M", 2, 2)
  232. f = lambdify(M, M + Identity(2), 'jax')
  233. ma = jax.numpy.array([[1, 2], [3, 4]])
  234. mr = jax.numpy.array([[2, 2], [3, 5]])
  235. assert (f(ma) == mr).all()
  236. from sympy.core.symbol import symbols
  237. n = symbols('n', integer=True)
  238. N = MatrixSymbol("M", n, n)
  239. raises(NotImplementedError, lambda: lambdify(N, N + Identity(n), 'jax'))
  240. def test_jax_array():
  241. assert JaxPrinter().doprint(Array(((1, 2), (3, 5)))) == 'jax.numpy.array([[1, 2], [3, 5]])'
  242. assert JaxPrinter().doprint(Array((1, 2))) == 'jax.numpy.array((1, 2))'
  243. def test_jax_known_funcs_consts():
  244. assert _jax_known_constants['NaN'] == 'jax.numpy.nan'
  245. assert _jax_known_constants['EulerGamma'] == 'jax.numpy.euler_gamma'
  246. assert _jax_known_functions['acos'] == 'jax.numpy.arccos'
  247. assert _jax_known_functions['log'] == 'jax.numpy.log'
  248. def test_jax_print_methods():
  249. prntr = JaxPrinter()
  250. assert hasattr(prntr, '_print_acos')
  251. assert hasattr(prntr, '_print_log')