pytensor 3.4.0__cp315-cp315-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (439) hide show
  1. pytensor/__init__.py +61 -0
  2. pytensor/_sparse_lazy.py +31 -0
  3. pytensor/_version.py +21 -0
  4. pytensor/assumptions/__init__.py +108 -0
  5. pytensor/assumptions/alloc.py +181 -0
  6. pytensor/assumptions/blockwise.py +13 -0
  7. pytensor/assumptions/bundles.py +55 -0
  8. pytensor/assumptions/core.py +684 -0
  9. pytensor/assumptions/diagonal.py +153 -0
  10. pytensor/assumptions/dimshuffle.py +54 -0
  11. pytensor/assumptions/dot.py +58 -0
  12. pytensor/assumptions/elemwise.py +84 -0
  13. pytensor/assumptions/orthogonal.py +85 -0
  14. pytensor/assumptions/permutation.py +80 -0
  15. pytensor/assumptions/positive_definite.py +153 -0
  16. pytensor/assumptions/reshape.py +28 -0
  17. pytensor/assumptions/selection.py +131 -0
  18. pytensor/assumptions/shape.py +30 -0
  19. pytensor/assumptions/specify.py +189 -0
  20. pytensor/assumptions/subtensor.py +88 -0
  21. pytensor/assumptions/symmetric.py +129 -0
  22. pytensor/assumptions/triangular.py +203 -0
  23. pytensor/assumptions/tridiagonal.py +108 -0
  24. pytensor/assumptions/unit_magnitude.py +41 -0
  25. pytensor/basic.py +46 -0
  26. pytensor/bin/__init__.py +0 -0
  27. pytensor/bin/pytensor_cache.py +112 -0
  28. pytensor/breakpoint.py +161 -0
  29. pytensor/compile/__init__.py +65 -0
  30. pytensor/compile/aliasing.py +260 -0
  31. pytensor/compile/builders.py +980 -0
  32. pytensor/compile/compiledir.py +272 -0
  33. pytensor/compile/compilelock.py +87 -0
  34. pytensor/compile/debug/__init__.py +0 -0
  35. pytensor/compile/debug/debugmode.py +2310 -0
  36. pytensor/compile/debug/dump.py +75 -0
  37. pytensor/compile/debug/monitormode.py +111 -0
  38. pytensor/compile/debug/nanguardmode.py +260 -0
  39. pytensor/compile/debug/profiling.py +1573 -0
  40. pytensor/compile/executor.py +879 -0
  41. pytensor/compile/inner_function.py +74 -0
  42. pytensor/compile/io.py +255 -0
  43. pytensor/compile/maker.py +722 -0
  44. pytensor/compile/mode.py +633 -0
  45. pytensor/compile/ops.py +315 -0
  46. pytensor/compile/rebuild.py +548 -0
  47. pytensor/compile/rewriting.py +221 -0
  48. pytensor/compile/sharedvalue.py +229 -0
  49. pytensor/conditional/__init__.py +7 -0
  50. pytensor/conditional/op.py +455 -0
  51. pytensor/conditional/rewriting.py +651 -0
  52. pytensor/configdefaults.py +1143 -0
  53. pytensor/configparser.py +584 -0
  54. pytensor/d3viz/__init__.py +1 -0
  55. pytensor/d3viz/css/d3-context-menu.css +29 -0
  56. pytensor/d3viz/css/d3viz.css +98 -0
  57. pytensor/d3viz/d3viz.py +130 -0
  58. pytensor/d3viz/formatting.py +369 -0
  59. pytensor/d3viz/html/template.html +101 -0
  60. pytensor/d3viz/js/d3-context-menu.js +42 -0
  61. pytensor/d3viz/js/d3.v3.min.js +5 -0
  62. pytensor/d3viz/js/d3viz.js +969 -0
  63. pytensor/d3viz/js/dagre-d3.min.js +2 -0
  64. pytensor/d3viz/js/graphlib-dot.min.js +4 -0
  65. pytensor/gradient.py +2508 -0
  66. pytensor/graph/__init__.py +19 -0
  67. pytensor/graph/basic.py +1324 -0
  68. pytensor/graph/destroyhandler.py +788 -0
  69. pytensor/graph/features.py +959 -0
  70. pytensor/graph/fg.py +1253 -0
  71. pytensor/graph/null_type.py +42 -0
  72. pytensor/graph/op.py +780 -0
  73. pytensor/graph/replace.py +345 -0
  74. pytensor/graph/rewriting/__init__.py +4 -0
  75. pytensor/graph/rewriting/basic.py +3139 -0
  76. pytensor/graph/rewriting/db.py +547 -0
  77. pytensor/graph/rewriting/kanren.py +329 -0
  78. pytensor/graph/rewriting/reachability.py +135 -0
  79. pytensor/graph/rewriting/unify.py +594 -0
  80. pytensor/graph/rewriting/utils.py +116 -0
  81. pytensor/graph/traversal.py +783 -0
  82. pytensor/graph/type.py +242 -0
  83. pytensor/graph/utils.py +350 -0
  84. pytensor/ifelse.py +37 -0
  85. pytensor/ipython.py +215 -0
  86. pytensor/link/__init__.py +1 -0
  87. pytensor/link/basic.py +745 -0
  88. pytensor/link/c/__init__.py +0 -0
  89. pytensor/link/c/basic.py +2012 -0
  90. pytensor/link/c/c_code/lazylinker_c.c +1027 -0
  91. pytensor/link/c/c_code/pytensor_mod_helper.h +25 -0
  92. pytensor/link/c/cmodule.py +2937 -0
  93. pytensor/link/c/cutils.py +116 -0
  94. pytensor/link/c/cvm.py +29 -0
  95. pytensor/link/c/exceptions.py +12 -0
  96. pytensor/link/c/interface.py +632 -0
  97. pytensor/link/c/lazylinker_c.py +147 -0
  98. pytensor/link/c/op.py +646 -0
  99. pytensor/link/c/params_type.py +885 -0
  100. pytensor/link/c/type.py +787 -0
  101. pytensor/link/jax/__init__.py +1 -0
  102. pytensor/link/jax/dispatch/__init__.py +23 -0
  103. pytensor/link/jax/dispatch/basic.py +159 -0
  104. pytensor/link/jax/dispatch/blas.py +14 -0
  105. pytensor/link/jax/dispatch/blockwise.py +63 -0
  106. pytensor/link/jax/dispatch/einsum.py +20 -0
  107. pytensor/link/jax/dispatch/elemwise.py +107 -0
  108. pytensor/link/jax/dispatch/extra_ops.py +143 -0
  109. pytensor/link/jax/dispatch/linalg/__init__.py +8 -0
  110. pytensor/link/jax/dispatch/linalg/constructors.py +12 -0
  111. pytensor/link/jax/dispatch/linalg/decomposition.py +140 -0
  112. pytensor/link/jax/dispatch/linalg/inverse.py +20 -0
  113. pytensor/link/jax/dispatch/linalg/products.py +43 -0
  114. pytensor/link/jax/dispatch/linalg/solvers.py +103 -0
  115. pytensor/link/jax/dispatch/linalg/summary.py +20 -0
  116. pytensor/link/jax/dispatch/math.py +73 -0
  117. pytensor/link/jax/dispatch/pad.py +53 -0
  118. pytensor/link/jax/dispatch/random.py +454 -0
  119. pytensor/link/jax/dispatch/scalar.py +357 -0
  120. pytensor/link/jax/dispatch/scan.py +247 -0
  121. pytensor/link/jax/dispatch/shape.py +106 -0
  122. pytensor/link/jax/dispatch/signal/__init__.py +1 -0
  123. pytensor/link/jax/dispatch/signal/conv.py +24 -0
  124. pytensor/link/jax/dispatch/sort.py +26 -0
  125. pytensor/link/jax/dispatch/sparse.py +38 -0
  126. pytensor/link/jax/dispatch/subtensor.py +73 -0
  127. pytensor/link/jax/dispatch/tensor_basic.py +195 -0
  128. pytensor/link/jax/linker.py +142 -0
  129. pytensor/link/jax/ops.py +568 -0
  130. pytensor/link/mlx/__init__.py +1 -0
  131. pytensor/link/mlx/dispatch/__init__.py +21 -0
  132. pytensor/link/mlx/dispatch/basic.py +244 -0
  133. pytensor/link/mlx/dispatch/blas.py +42 -0
  134. pytensor/link/mlx/dispatch/blockwise.py +88 -0
  135. pytensor/link/mlx/dispatch/einsum.py +14 -0
  136. pytensor/link/mlx/dispatch/elemwise.py +133 -0
  137. pytensor/link/mlx/dispatch/extra_ops.py +35 -0
  138. pytensor/link/mlx/dispatch/linalg/__init__.py +7 -0
  139. pytensor/link/mlx/dispatch/linalg/decomposition.py +183 -0
  140. pytensor/link/mlx/dispatch/linalg/inverse.py +26 -0
  141. pytensor/link/mlx/dispatch/linalg/products.py +22 -0
  142. pytensor/link/mlx/dispatch/linalg/solvers.py +95 -0
  143. pytensor/link/mlx/dispatch/linalg/summary.py +40 -0
  144. pytensor/link/mlx/dispatch/math.py +82 -0
  145. pytensor/link/mlx/dispatch/pad.py +55 -0
  146. pytensor/link/mlx/dispatch/random.py +276 -0
  147. pytensor/link/mlx/dispatch/scalar/__init__.py +10 -0
  148. pytensor/link/mlx/dispatch/scalar/basic.py +127 -0
  149. pytensor/link/mlx/dispatch/scalar/bessel.py +508 -0
  150. pytensor/link/mlx/dispatch/scalar/beta.py +1131 -0
  151. pytensor/link/mlx/dispatch/scalar/erf.py +316 -0
  152. pytensor/link/mlx/dispatch/scalar/erfcinv.py +152 -0
  153. pytensor/link/mlx/dispatch/scalar/gamma.py +58 -0
  154. pytensor/link/mlx/dispatch/scalar/gammainc.py +552 -0
  155. pytensor/link/mlx/dispatch/scalar/helpers.py +214 -0
  156. pytensor/link/mlx/dispatch/scalar/math.py +271 -0
  157. pytensor/link/mlx/dispatch/shape.py +56 -0
  158. pytensor/link/mlx/dispatch/signal/__init__.py +0 -0
  159. pytensor/link/mlx/dispatch/signal/conv.py +33 -0
  160. pytensor/link/mlx/dispatch/sort.py +38 -0
  161. pytensor/link/mlx/dispatch/subtensor.py +118 -0
  162. pytensor/link/mlx/dispatch/tensor_basic.py +256 -0
  163. pytensor/link/mlx/linker.py +143 -0
  164. pytensor/link/numba/__init__.py +1 -0
  165. pytensor/link/numba/cache.py +207 -0
  166. pytensor/link/numba/dispatch/__init__.py +29 -0
  167. pytensor/link/numba/dispatch/_gamma_tables.py +821 -0
  168. pytensor/link/numba/dispatch/_llvmlite_self_ref.py +66 -0
  169. pytensor/link/numba/dispatch/_patch_list_to_tuple.py +330 -0
  170. pytensor/link/numba/dispatch/_patch_pointer_add.py +40 -0
  171. pytensor/link/numba/dispatch/array_utils.py +26 -0
  172. pytensor/link/numba/dispatch/basic.py +583 -0
  173. pytensor/link/numba/dispatch/blockwise.py +125 -0
  174. pytensor/link/numba/dispatch/compile_ops.py +178 -0
  175. pytensor/link/numba/dispatch/cython_support.py +197 -0
  176. pytensor/link/numba/dispatch/elemwise.py +1598 -0
  177. pytensor/link/numba/dispatch/extra_ops.py +351 -0
  178. pytensor/link/numba/dispatch/linalg/_BLAS.py +195 -0
  179. pytensor/link/numba/dispatch/linalg/_LAPACK.py +2003 -0
  180. pytensor/link/numba/dispatch/linalg/__init__.py +6 -0
  181. pytensor/link/numba/dispatch/linalg/constructors.py +90 -0
  182. pytensor/link/numba/dispatch/linalg/decomposition/__init__.py +0 -0
  183. pytensor/link/numba/dispatch/linalg/decomposition/cholesky.py +81 -0
  184. pytensor/link/numba/dispatch/linalg/decomposition/dispatch.py +671 -0
  185. pytensor/link/numba/dispatch/linalg/decomposition/eigen.py +1029 -0
  186. pytensor/link/numba/dispatch/linalg/decomposition/lu.py +192 -0
  187. pytensor/link/numba/dispatch/linalg/decomposition/lu_factor.py +90 -0
  188. pytensor/link/numba/dispatch/linalg/decomposition/qr.py +722 -0
  189. pytensor/link/numba/dispatch/linalg/decomposition/qz.py +1285 -0
  190. pytensor/link/numba/dispatch/linalg/decomposition/schur.py +225 -0
  191. pytensor/link/numba/dispatch/linalg/decomposition/svd.py +709 -0
  192. pytensor/link/numba/dispatch/linalg/inverse.py +40 -0
  193. pytensor/link/numba/dispatch/linalg/products.py +881 -0
  194. pytensor/link/numba/dispatch/linalg/solvers/__init__.py +0 -0
  195. pytensor/link/numba/dispatch/linalg/solvers/cholesky.py +85 -0
  196. pytensor/link/numba/dispatch/linalg/solvers/dispatch.py +189 -0
  197. pytensor/link/numba/dispatch/linalg/solvers/general.py +85 -0
  198. pytensor/link/numba/dispatch/linalg/solvers/hermitian.py +136 -0
  199. pytensor/link/numba/dispatch/linalg/solvers/linear_control.py +98 -0
  200. pytensor/link/numba/dispatch/linalg/solvers/lu_solve.py +134 -0
  201. pytensor/link/numba/dispatch/linalg/solvers/posdef.py +110 -0
  202. pytensor/link/numba/dispatch/linalg/solvers/symmetric.py +134 -0
  203. pytensor/link/numba/dispatch/linalg/solvers/triangular.py +132 -0
  204. pytensor/link/numba/dispatch/linalg/solvers/tridiagonal.py +368 -0
  205. pytensor/link/numba/dispatch/linalg/solvers/utils.py +11 -0
  206. pytensor/link/numba/dispatch/linalg/summary.py +46 -0
  207. pytensor/link/numba/dispatch/linalg/utils.py +133 -0
  208. pytensor/link/numba/dispatch/optimize/__init__.py +2 -0
  209. pytensor/link/numba/dispatch/optimize/scalar_minimize.py +489 -0
  210. pytensor/link/numba/dispatch/optimize/scalar_root.py +652 -0
  211. pytensor/link/numba/dispatch/optimize/utils.py +48 -0
  212. pytensor/link/numba/dispatch/random.py +518 -0
  213. pytensor/link/numba/dispatch/scalar.py +655 -0
  214. pytensor/link/numba/dispatch/scan.py +457 -0
  215. pytensor/link/numba/dispatch/shape.py +78 -0
  216. pytensor/link/numba/dispatch/signal/__init__.py +1 -0
  217. pytensor/link/numba/dispatch/signal/conv.py +125 -0
  218. pytensor/link/numba/dispatch/sort.py +65 -0
  219. pytensor/link/numba/dispatch/sparse/__init__.py +1 -0
  220. pytensor/link/numba/dispatch/sparse/basic.py +914 -0
  221. pytensor/link/numba/dispatch/sparse/math.py +1367 -0
  222. pytensor/link/numba/dispatch/sparse/variable.py +651 -0
  223. pytensor/link/numba/dispatch/string_codegen.py +31 -0
  224. pytensor/link/numba/dispatch/subtensor.py +887 -0
  225. pytensor/link/numba/dispatch/tensor_basic.py +501 -0
  226. pytensor/link/numba/dispatch/typed_list.py +251 -0
  227. pytensor/link/numba/dispatch/vectorize_codegen.py +1519 -0
  228. pytensor/link/numba/linker.py +32 -0
  229. pytensor/link/pytorch/dispatch/__init__.py +15 -0
  230. pytensor/link/pytorch/dispatch/basic.py +226 -0
  231. pytensor/link/pytorch/dispatch/blas.py +14 -0
  232. pytensor/link/pytorch/dispatch/blockwise.py +32 -0
  233. pytensor/link/pytorch/dispatch/elemwise.py +231 -0
  234. pytensor/link/pytorch/dispatch/extra_ops.py +53 -0
  235. pytensor/link/pytorch/dispatch/linalg/__init__.py +6 -0
  236. pytensor/link/pytorch/dispatch/linalg/decomposition.py +77 -0
  237. pytensor/link/pytorch/dispatch/linalg/inverse.py +22 -0
  238. pytensor/link/pytorch/dispatch/linalg/products.py +12 -0
  239. pytensor/link/pytorch/dispatch/linalg/summary.py +20 -0
  240. pytensor/link/pytorch/dispatch/math.py +14 -0
  241. pytensor/link/pytorch/dispatch/scalar.py +115 -0
  242. pytensor/link/pytorch/dispatch/shape.py +58 -0
  243. pytensor/link/pytorch/dispatch/sort.py +27 -0
  244. pytensor/link/pytorch/dispatch/subtensor.py +134 -0
  245. pytensor/link/pytorch/linker.py +104 -0
  246. pytensor/link/utils.py +949 -0
  247. pytensor/link/vm.py +1335 -0
  248. pytensor/misc/__init__.py +0 -0
  249. pytensor/misc/check_blas.py +300 -0
  250. pytensor/misc/check_blas_many.sh +21 -0
  251. pytensor/misc/check_duplicate_key.py +71 -0
  252. pytensor/misc/elemwise_openmp_speedup.py +77 -0
  253. pytensor/misc/elemwise_time_test.py +76 -0
  254. pytensor/misc/frozendict.py +44 -0
  255. pytensor/misc/may_share_memory.py +38 -0
  256. pytensor/misc/ordered_set.py +44 -0
  257. pytensor/misc/pkl_utils.py +64 -0
  258. pytensor/npy_2_compat.py +22 -0
  259. pytensor/printing.py +2731 -0
  260. pytensor/py.typed +0 -0
  261. pytensor/raise_op.py +203 -0
  262. pytensor/scalar/__init__.py +2 -0
  263. pytensor/scalar/basic.py +4357 -0
  264. pytensor/scalar/c_code/Faddeeva.cc +2517 -0
  265. pytensor/scalar/c_code/Faddeeva.hh +62 -0
  266. pytensor/scalar/c_code/gamma.c +375 -0
  267. pytensor/scalar/c_code/incbet.c +313 -0
  268. pytensor/scalar/loop.py +328 -0
  269. pytensor/scalar/math.py +1913 -0
  270. pytensor/scalar/sharedvar.py +61 -0
  271. pytensor/scan/__init__.py +57 -0
  272. pytensor/scan/basic.py +1192 -0
  273. pytensor/scan/checkpoints.py +191 -0
  274. pytensor/scan/op.py +3633 -0
  275. pytensor/scan/rewriting/__init__.py +30 -0
  276. pytensor/scan/rewriting/constant_folding.py +63 -0
  277. pytensor/scan/rewriting/db.py +283 -0
  278. pytensor/scan/rewriting/inner_graph.py +319 -0
  279. pytensor/scan/rewriting/inplace.py +197 -0
  280. pytensor/scan/rewriting/io.py +306 -0
  281. pytensor/scan/rewriting/merge.py +562 -0
  282. pytensor/scan/rewriting/push_out.py +986 -0
  283. pytensor/scan/rewriting/trace.py +1080 -0
  284. pytensor/scan/rewriting/utils.py +151 -0
  285. pytensor/scan/scan_perform.c +40260 -0
  286. pytensor/scan/scan_perform.cp315-win_amd64.pyd +0 -0
  287. pytensor/scan/scan_perform.pyx +603 -0
  288. pytensor/scan/scan_perform_ext.py +17 -0
  289. pytensor/scan/utils.py +909 -0
  290. pytensor/scan/views.py +260 -0
  291. pytensor/sparse/__init__.py +5 -0
  292. pytensor/sparse/basic.py +1919 -0
  293. pytensor/sparse/linalg.py +96 -0
  294. pytensor/sparse/math.py +2151 -0
  295. pytensor/sparse/rewriting.py +2057 -0
  296. pytensor/sparse/sharedvar.py +30 -0
  297. pytensor/sparse/type.py +253 -0
  298. pytensor/sparse/utils.py +20 -0
  299. pytensor/sparse/variable.py +481 -0
  300. pytensor/tensor/__init__.py +162 -0
  301. pytensor/tensor/basic.py +4514 -0
  302. pytensor/tensor/blas/__init__.py +145 -0
  303. pytensor/tensor/blas/_core.py +189 -0
  304. pytensor/tensor/blas/batched.py +278 -0
  305. pytensor/tensor/blas/blas_c.py +110 -0
  306. pytensor/tensor/blas/c_code/__init__.py +0 -0
  307. pytensor/tensor/blas/c_code/alt_blas_common.h +21 -0
  308. pytensor/tensor/blas/c_code/alt_blas_template.c +369 -0
  309. pytensor/tensor/blas/c_code/blas_headers.py +1210 -0
  310. pytensor/tensor/blas/c_code/codegen.py +888 -0
  311. pytensor/tensor/blas/gemm.py +362 -0
  312. pytensor/tensor/blas/gemv.py +117 -0
  313. pytensor/tensor/blockwise.py +635 -0
  314. pytensor/tensor/einsum.py +818 -0
  315. pytensor/tensor/elemwise.py +1839 -0
  316. pytensor/tensor/elemwise_cgen.py +761 -0
  317. pytensor/tensor/exceptions.py +16 -0
  318. pytensor/tensor/extra_ops.py +1975 -0
  319. pytensor/tensor/fft.py +222 -0
  320. pytensor/tensor/fourier.py +188 -0
  321. pytensor/tensor/functional.py +131 -0
  322. pytensor/tensor/interpolate.py +200 -0
  323. pytensor/tensor/linalg/__init__.py +134 -0
  324. pytensor/tensor/linalg/_lazy.py +12 -0
  325. pytensor/tensor/linalg/constructors.py +121 -0
  326. pytensor/tensor/linalg/decomposition/__init__.py +1 -0
  327. pytensor/tensor/linalg/decomposition/cholesky.py +198 -0
  328. pytensor/tensor/linalg/decomposition/eigen.py +516 -0
  329. pytensor/tensor/linalg/decomposition/lu.py +347 -0
  330. pytensor/tensor/linalg/decomposition/qr.py +383 -0
  331. pytensor/tensor/linalg/decomposition/schur.py +607 -0
  332. pytensor/tensor/linalg/decomposition/svd.py +244 -0
  333. pytensor/tensor/linalg/dtype_utils.py +42 -0
  334. pytensor/tensor/linalg/inverse.py +218 -0
  335. pytensor/tensor/linalg/products.py +302 -0
  336. pytensor/tensor/linalg/solvers/__init__.py +0 -0
  337. pytensor/tensor/linalg/solvers/core.py +126 -0
  338. pytensor/tensor/linalg/solvers/general.py +269 -0
  339. pytensor/tensor/linalg/solvers/linear_control.py +462 -0
  340. pytensor/tensor/linalg/solvers/lstsq.py +98 -0
  341. pytensor/tensor/linalg/solvers/psd.py +108 -0
  342. pytensor/tensor/linalg/solvers/triangular.py +142 -0
  343. pytensor/tensor/linalg/solvers/tridiagonal.py +227 -0
  344. pytensor/tensor/linalg/summary.py +344 -0
  345. pytensor/tensor/math.py +4397 -0
  346. pytensor/tensor/nlinalg.py +58 -0
  347. pytensor/tensor/optimize.py +1370 -0
  348. pytensor/tensor/pad.py +686 -0
  349. pytensor/tensor/random/__init__.py +7 -0
  350. pytensor/tensor/random/basic.py +2179 -0
  351. pytensor/tensor/random/op.py +543 -0
  352. pytensor/tensor/random/rewriting/__init__.py +11 -0
  353. pytensor/tensor/random/rewriting/basic.py +385 -0
  354. pytensor/tensor/random/rewriting/jax.py +196 -0
  355. pytensor/tensor/random/rewriting/numba.py +192 -0
  356. pytensor/tensor/random/type.py +175 -0
  357. pytensor/tensor/random/utils.py +352 -0
  358. pytensor/tensor/random/variable.py +252 -0
  359. pytensor/tensor/reshape.py +528 -0
  360. pytensor/tensor/rewriting/__init__.py +22 -0
  361. pytensor/tensor/rewriting/assumptions.py +65 -0
  362. pytensor/tensor/rewriting/basic.py +1630 -0
  363. pytensor/tensor/rewriting/blas.py +885 -0
  364. pytensor/tensor/rewriting/blas_c.py +52 -0
  365. pytensor/tensor/rewriting/blockwise.py +372 -0
  366. pytensor/tensor/rewriting/einsum.py +53 -0
  367. pytensor/tensor/rewriting/elemwise.py +1221 -0
  368. pytensor/tensor/rewriting/extra_ops.py +119 -0
  369. pytensor/tensor/rewriting/fused_elemwise.py +955 -0
  370. pytensor/tensor/rewriting/inplace.py +352 -0
  371. pytensor/tensor/rewriting/jax.py +201 -0
  372. pytensor/tensor/rewriting/linalg/__init__.py +12 -0
  373. pytensor/tensor/rewriting/linalg/decomposition.py +732 -0
  374. pytensor/tensor/rewriting/linalg/inverse.py +177 -0
  375. pytensor/tensor/rewriting/linalg/products.py +434 -0
  376. pytensor/tensor/rewriting/linalg/reassociate_matmul.py +685 -0
  377. pytensor/tensor/rewriting/linalg/solvers.py +892 -0
  378. pytensor/tensor/rewriting/linalg/summary.py +324 -0
  379. pytensor/tensor/rewriting/linalg/utils.py +181 -0
  380. pytensor/tensor/rewriting/math.py +4232 -0
  381. pytensor/tensor/rewriting/numba.py +120 -0
  382. pytensor/tensor/rewriting/ofg.py +82 -0
  383. pytensor/tensor/rewriting/optimize.py +105 -0
  384. pytensor/tensor/rewriting/reshape.py +72 -0
  385. pytensor/tensor/rewriting/scalarize.py +268 -0
  386. pytensor/tensor/rewriting/shape.py +1041 -0
  387. pytensor/tensor/rewriting/sort.py +98 -0
  388. pytensor/tensor/rewriting/special.py +184 -0
  389. pytensor/tensor/rewriting/subtensor.py +3199 -0
  390. pytensor/tensor/rewriting/subtensor_lift.py +1567 -0
  391. pytensor/tensor/rewriting/uncanonicalize.py +232 -0
  392. pytensor/tensor/shape.py +1007 -0
  393. pytensor/tensor/sharedvar.py +137 -0
  394. pytensor/tensor/signal/__init__.py +4 -0
  395. pytensor/tensor/signal/conv.py +348 -0
  396. pytensor/tensor/slinalg.py +65 -0
  397. pytensor/tensor/sort.py +213 -0
  398. pytensor/tensor/special.py +341 -0
  399. pytensor/tensor/subtensor.py +3058 -0
  400. pytensor/tensor/symbolic.py +45 -0
  401. pytensor/tensor/type.py +1416 -0
  402. pytensor/tensor/type_other.py +143 -0
  403. pytensor/tensor/utils.py +307 -0
  404. pytensor/tensor/var.py +11 -0
  405. pytensor/tensor/variable.py +1122 -0
  406. pytensor/tensor/xlogx.py +37 -0
  407. pytensor/typed_list/__init__.py +3 -0
  408. pytensor/typed_list/basic.py +663 -0
  409. pytensor/typed_list/rewriting.py +26 -0
  410. pytensor/typed_list/type.py +141 -0
  411. pytensor/utils.py +426 -0
  412. pytensor/xtensor/__init__.py +9 -0
  413. pytensor/xtensor/basic.py +153 -0
  414. pytensor/xtensor/indexing.py +265 -0
  415. pytensor/xtensor/linalg.py +102 -0
  416. pytensor/xtensor/math.py +653 -0
  417. pytensor/xtensor/random/__init__.py +2 -0
  418. pytensor/xtensor/random/basic.py +254 -0
  419. pytensor/xtensor/random/type.py +88 -0
  420. pytensor/xtensor/random/variable.py +204 -0
  421. pytensor/xtensor/reduction.py +136 -0
  422. pytensor/xtensor/rewriting/__init__.py +6 -0
  423. pytensor/xtensor/rewriting/basic.py +87 -0
  424. pytensor/xtensor/rewriting/indexing.py +213 -0
  425. pytensor/xtensor/rewriting/math.py +47 -0
  426. pytensor/xtensor/rewriting/reduction.py +72 -0
  427. pytensor/xtensor/rewriting/shape.py +197 -0
  428. pytensor/xtensor/rewriting/utils.py +71 -0
  429. pytensor/xtensor/rewriting/vectorization.py +114 -0
  430. pytensor/xtensor/shape.py +707 -0
  431. pytensor/xtensor/signal.py +74 -0
  432. pytensor/xtensor/type.py +1143 -0
  433. pytensor/xtensor/vectorization.py +686 -0
  434. pytensor-3.4.0.dist-info/METADATA +208 -0
  435. pytensor-3.4.0.dist-info/RECORD +439 -0
  436. pytensor-3.4.0.dist-info/WHEEL +5 -0
  437. pytensor-3.4.0.dist-info/entry_points.txt +2 -0
  438. pytensor-3.4.0.dist-info/licenses/LICENSE.txt +41 -0
  439. pytensor-3.4.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,352 @@
1
+ import abc
2
+ import itertools
3
+ import sys
4
+ from collections import defaultdict
5
+ from collections.abc import Sequence
6
+
7
+ from pytensor.compile.aliasing import Supervisor
8
+ from pytensor.graph.basic import Apply, Variable
9
+ from pytensor.graph.destroyhandler import DestroyHandler
10
+ from pytensor.graph.fg import FunctionGraph
11
+ from pytensor.graph.op import Op
12
+ from pytensor.graph.rewriting.basic import (
13
+ GraphRewriter,
14
+ copy_stack_trace,
15
+ )
16
+ from pytensor.graph.rewriting.reachability import (
17
+ ancestor_bitsets,
18
+ )
19
+ from pytensor.graph.utils import InconsistencyError
20
+ from pytensor.tensor.variable import TensorVariable
21
+
22
+
23
+ def _root_and_readers(fgraph, var):
24
+ """The variable `var` views (its root) and the nodes that read the root or a view of it."""
25
+ view_i, view_o = fgraph.destroy_handler.view_i, fgraph.destroy_handler.view_o
26
+ while var in view_i:
27
+ var = view_i[var]
28
+ tree, readers = [var], set()
29
+ while tree:
30
+ v = tree.pop()
31
+ readers.update(c for c, _ in fgraph.clients[v])
32
+ tree.extend(view_o.get(v, ()))
33
+ return var, readers
34
+
35
+
36
+ def _descendants(fgraph, node):
37
+ """Nodes that run after `node`, through data edges or the destroy orderings."""
38
+ after = defaultdict(list)
39
+ for n, before in fgraph.orderings().items():
40
+ for b in before:
41
+ after[b].append(n)
42
+ seen, stack = set(), [node]
43
+ while stack:
44
+ n = stack.pop()
45
+ for c in itertools.chain(
46
+ (c for out in n.outputs for c, _ in fgraph.clients[out]), after[n]
47
+ ):
48
+ if c not in seen:
49
+ seen.add(c)
50
+ stack.append(c)
51
+ return seen
52
+
53
+
54
+ class InplaceGraphOptimizer(GraphRewriter):
55
+ op: type[Op]
56
+
57
+ def add_requirements(self, fgraph):
58
+ fgraph.attach_feature(DestroyHandler())
59
+
60
+ @abc.abstractmethod
61
+ def filter_candidate_pairs(
62
+ self, fgraph: FunctionGraph, node: Apply, protected_inputs: Sequence[Variable]
63
+ ) -> Sequence[tuple[tuple[int, Variable], tuple[int, Variable]]]:
64
+ pass
65
+
66
+ @abc.abstractmethod
67
+ def create_inplace_node(
68
+ self, node: Apply, inplace_pattern: dict[int, Sequence[int]]
69
+ ) -> Apply:
70
+ pass
71
+
72
+ def _get_protected_inputs(self, fgraph):
73
+ """Collect inputs protected from in-place destruction."""
74
+ protected = set(
75
+ itertools.chain.from_iterable(
76
+ f.protected for f in fgraph._features if isinstance(f, Supervisor)
77
+ )
78
+ )
79
+ protected.update(fgraph.outputs)
80
+ return protected
81
+
82
+ def try_inplace_on_node(
83
+ self,
84
+ fgraph,
85
+ node,
86
+ candidate_pairs=None,
87
+ reason="inplace_optimizer",
88
+ extra_protected_inputs=frozenset(),
89
+ ):
90
+ """Try to make a single node operate in-place.
91
+
92
+ First attempts all candidate pairs at once, then falls back to
93
+ one-at-a-time. Returns the (possibly replaced) node.
94
+
95
+ Parameters
96
+ ----------
97
+ candidate_pairs
98
+ Pre-filtered and sorted list of ``((out_idx, out), (in_idx, inp))``
99
+ pairs. If None, computed from ``filter_candidate_pairs`` using
100
+ the standard protections plus ``extra_protected_inputs``.
101
+ extra_protected_inputs
102
+ Additional variables that must not be used as in-place targets,
103
+ only used when ``candidate_pairs`` is None.
104
+ """
105
+ if candidate_pairs is None:
106
+ protected_inputs = self._get_protected_inputs(fgraph)
107
+ protected_inputs.update(extra_protected_inputs)
108
+ candidate_pairs = self.filter_candidate_pairs(
109
+ fgraph, node, protected_inputs
110
+ )
111
+ candidate_pairs = [
112
+ p for p in candidate_pairs if not fgraph.has_destroyers([p[1][1]])
113
+ ]
114
+ if len(candidate_pairs) > 1:
115
+ # Drop pairs whose destroyed memory is read after the node (they'd form a cycle),
116
+ # walking the graph only if the memory has other readers
117
+ readers = [
118
+ _root_and_readers(fgraph, p[1][1])[1] - {node} for p in candidate_pairs
119
+ ]
120
+ if any(readers):
121
+ after = _descendants(fgraph, node)
122
+ candidate_pairs = [
123
+ p for p, r in zip(candidate_pairs, readers) if after.isdisjoint(r)
124
+ ]
125
+ if not candidate_pairs:
126
+ return node
127
+
128
+ failed_pattern = None
129
+ # Try in-placing all outputs at once
130
+ tried_inputs = set()
131
+ inplace_pattern = {}
132
+ for (o, _), (i, _) in candidate_pairs:
133
+ if o not in inplace_pattern and i not in tried_inputs:
134
+ inplace_pattern[o] = [i]
135
+ tried_inputs.add(i)
136
+
137
+ inplace_node = self.create_inplace_node(node, inplace_pattern)
138
+ if inplace_node.op.destroy_map == inplace_pattern:
139
+ replacements = tuple(zip(node.outputs, inplace_node.outputs))
140
+ try:
141
+ fgraph.replace_all_validate(replacements, reason=reason)
142
+ except InconsistencyError:
143
+ failed_pattern = inplace_pattern
144
+ else:
145
+ copy_stack_trace(node.outputs, inplace_node.outputs)
146
+ return inplace_node
147
+
148
+ # Fall back to one output/input at a time
149
+ tried_inputs = set()
150
+ inplace_pattern = {}
151
+ original_node = node
152
+ for (o, _), (i, _) in candidate_pairs:
153
+ if o not in inplace_pattern and i not in tried_inputs:
154
+ inplace_pattern[o] = [i]
155
+ tried_inputs.add(i)
156
+
157
+ if node is original_node and inplace_pattern == failed_pattern:
158
+ inplace_pattern.pop(o)
159
+ tried_inputs.discard(i)
160
+ continue
161
+
162
+ inplace_node = self.create_inplace_node(node, inplace_pattern)
163
+ if inplace_node.op.destroy_map != inplace_pattern:
164
+ # This Op can't respect this partial inplace pattern,
165
+ # We assume it can't support any other cases
166
+ break
167
+ replacements = tuple(zip(node.outputs, inplace_node.outputs))
168
+ try:
169
+ fgraph.replace_all_validate(replacements, reason=reason)
170
+ node = inplace_node
171
+ except InconsistencyError:
172
+ inplace_pattern.pop(o)
173
+ # The input wasn't consumed, so let another output try to
174
+ # reuse it in place.
175
+ tried_inputs.discard(i)
176
+ if node is not original_node:
177
+ copy_stack_trace(original_node.outputs, node.outputs)
178
+ return node
179
+
180
+ def apply(self, fgraph):
181
+ r"""
182
+
183
+ Attempts to replace all `Op`\s by versions of them that operate
184
+ inplace. It operates greedily: for each `Op` that is encountered,
185
+ it tries to inplace all the valid inputs at once (if the Op supports it),
186
+ if that fails, it tries to inplace one input at a time.
187
+
188
+ Examples
189
+ --------
190
+
191
+ x + y + z -> x += y += z
192
+ (x + y) * (x * y) -> (x += y) *= (x * y) or (x + y) *= (x *= y)
193
+
194
+ """
195
+ reason = f"{self.op}_inplace_optimizer"
196
+ prof = {
197
+ "opt": self,
198
+ "node_before": len(fgraph.apply_nodes),
199
+ "nb_replaced": 0,
200
+ }
201
+
202
+ protected_inputs = self._get_protected_inputs(fgraph)
203
+ root_destroyer = fgraph.destroy_handler.root_destroyer
204
+
205
+ self_op = self.op
206
+ update_mapping = fgraph.update_mapping or {}
207
+ op_updates: dict[TensorVariable, TensorVariable] = {
208
+ out: fgraph.inputs[update_mapping[out_idx]]
209
+ for out_idx, out in enumerate(fgraph.outputs)
210
+ if (
211
+ out_idx in update_mapping
212
+ and out.owner
213
+ and isinstance(out.owner.op, self_op)
214
+ )
215
+ }
216
+ set_op_updates = set(op_updates.keys())
217
+
218
+ nodes = fgraph.toposort()
219
+ ancestors, bitflags = ancestor_bitsets(fgraph, nodes)
220
+
221
+ # Validating each in-placing on its own costs a whole-graph cycle check each.
222
+ # Pairs that would surely form a cycle (another reader of the destroyed memory
223
+ # depends on the node) are dropped up front, the rest validated in batches.
224
+ claimed, plan = set(), []
225
+ for node in nodes:
226
+ if not isinstance(node.op, self_op) or node.op.destroy_map:
227
+ continue
228
+
229
+ candidate_pairs = self.filter_candidate_pairs(
230
+ fgraph, node, protected_inputs
231
+ )
232
+
233
+ if not candidate_pairs:
234
+ continue
235
+
236
+ sorted_candidate_pairs = candidate_pairs
237
+ if op_updates and (node_updates := set(node.outputs) & set_op_updates):
238
+ # If the fgraph has updates, we try to prioritize in-placing on the pairs that correspond to the update
239
+ direct_update_pairs = []
240
+ indirect_update_pairs = []
241
+ other_update_pairs = []
242
+ for pair in candidate_pairs:
243
+ ((_o, out), (_i, inp)) = pair
244
+ if out in node_updates:
245
+ direct_update_inp = op_updates[out]
246
+ if direct_update_inp is inp:
247
+ # This pair is the whole graph update
248
+ direct_update_pairs.append(pair)
249
+ continue
250
+ elif (inp_node := inp.owner) is not None and any(
251
+ root_destroyer.get(up_inp, None) is inp_node
252
+ for up_inp in op_updates.values()
253
+ ):
254
+ # This pair connects to an updated input
255
+ indirect_update_pairs.append(pair)
256
+ continue
257
+ other_update_pairs.append(pair)
258
+
259
+ sorted_candidate_pairs = (
260
+ direct_update_pairs + indirect_update_pairs + other_update_pairs
261
+ )
262
+
263
+ flag, viable, pattern, roots, contended = (
264
+ bitflags[node],
265
+ [],
266
+ {},
267
+ set(),
268
+ False,
269
+ )
270
+ for pair in sorted_candidate_pairs:
271
+ (o, _), (i, inp) = pair
272
+ root, readers = _root_and_readers(fgraph, inp)
273
+ if any(c is not node and ancestors.get(c, 0) & flag for c in readers):
274
+ continue
275
+ viable.append(pair)
276
+ contended |= root in claimed
277
+ if (
278
+ root not in claimed | roots
279
+ and o not in pattern
280
+ and i not in pattern.values()
281
+ ):
282
+ pattern[o] = i
283
+ roots.add(root)
284
+ # a node contending for memory an earlier node claimed is tried on its own, in order
285
+ if viable:
286
+ plan.append((node, viable, None if contended else pattern))
287
+ claimed |= roots if not contended else set()
288
+
289
+ def try_batch(batch):
290
+ new_nodes = [
291
+ self.create_inplace_node(n, {o: [i] for o, i in p.items()})
292
+ for n, _, p in batch
293
+ ]
294
+ if any(
295
+ new.op.destroy_map != {o: [i] for o, i in p.items()}
296
+ for new, (_, _, p) in zip(new_nodes, batch)
297
+ ):
298
+ return False
299
+ # In reverse topological order, so a node is replaced after its clients
300
+ replacements = [
301
+ pair
302
+ for new, (n, _, _) in reversed(list(zip(new_nodes, batch)))
303
+ for pair in zip(n.outputs, new.outputs)
304
+ ]
305
+ try:
306
+ fgraph.replace_all_validate(replacements, reason=reason)
307
+ except InconsistencyError:
308
+ return False
309
+ for new, (n, _, _) in zip(new_nodes, batch):
310
+ copy_stack_trace(n.outputs, new.outputs)
311
+ prof["nb_replaced"] += len(batch)
312
+ return True
313
+
314
+ def try_one(node, viable):
315
+ # node.inputs, not the pairs' variables: earlier in-placings replace producers
316
+ viable = [((o, out), (i, node.inputs[i])) for (o, out), (i, _) in viable]
317
+ if self.try_inplace_on_node(fgraph, node, viable, reason) is not node:
318
+ prof["nb_replaced"] += 1
319
+
320
+ def run(batch):
321
+ if len(batch) == 1:
322
+ try_one(*batch[0][:2])
323
+ elif batch and not try_batch(batch):
324
+ run(batch[: len(batch) // 2])
325
+ run(batch[len(batch) // 2 :])
326
+
327
+ batch = []
328
+ for node, viable, pattern in plan:
329
+ if pattern:
330
+ batch.append((node, viable, pattern))
331
+ else:
332
+ run(batch)
333
+ batch = []
334
+ try_one(node, viable)
335
+ run(batch)
336
+ return prof
337
+
338
+ @classmethod
339
+ def print_profile(cls, stream, prof, level=0):
340
+ blanc = " " * level
341
+ print(blanc, cls.__name__, file=stream)
342
+ for k in [
343
+ "node_before",
344
+ "nb_replaced",
345
+ ]:
346
+ print(blanc, k, prof[k], file=stream)
347
+
348
+ def print_summary(self, stream=sys.stdout, level=0, depth=-1):
349
+ print(
350
+ f"{' ' * level}{self.__class__.__name__}",
351
+ file=stream,
352
+ )
@@ -0,0 +1,201 @@
1
+ import pytensor.tensor as pt
2
+ from pytensor.compile import optdb
3
+ from pytensor.graph.rewriting.basic import copy_stack_trace, dfs_rewriter, node_rewriter
4
+ from pytensor.tensor.basic import MakeVector, get_underlying_scalar_constant_value
5
+ from pytensor.tensor.blockwise import Blockwise, JAXBlockwise, _squeeze_left
6
+ from pytensor.tensor.elemwise import DimShuffle
7
+ from pytensor.tensor.math import Sum
8
+ from pytensor.tensor.rewriting.elemwise import apply_local_dimshuffle_merge
9
+ from pytensor.tensor.shape import Reshape
10
+ from pytensor.tensor.subtensor import AdvancedIncSubtensor, AdvancedSubtensor
11
+ from pytensor.tensor.variable import TensorVariable
12
+
13
+
14
+ @node_rewriter([AdvancedIncSubtensor])
15
+ def boolean_indexing_set_or_inc(fgraph, node):
16
+ """Replace `AdvancedIncSubtensor` when using boolean indexing using `Switch`.
17
+
18
+ JAX cannot JIT-compile functions that use boolean indexing to set values in
19
+ an array. A workaround is to re-express this logic using `jax.numpy.where`.
20
+ This rewrite allows to improve upon JAX's API.
21
+
22
+ """
23
+
24
+ # Get out if there is more than one indexing group
25
+ if len(node.inputs) != 3:
26
+ return None
27
+
28
+ op = node.op
29
+ [x, y, cond] = node.inputs
30
+
31
+ # This rewrite only works when `y` is a scalar, so it can broadcast to the shape of x[cond]
32
+ if y.type.ndim > 0:
33
+ return
34
+
35
+ if not isinstance(cond, TensorVariable):
36
+ return
37
+
38
+ if not cond.type.dtype == "bool":
39
+ return
40
+
41
+ if op.set_instead_of_inc:
42
+ out = pt.where(cond, y, x)
43
+ return out.owner.outputs
44
+ else:
45
+ out = pt.where(cond, x + y, x)
46
+ return out.owner.outputs
47
+
48
+
49
+ optdb.register(
50
+ "jax_boolean_indexing_set_or_inc",
51
+ dfs_rewriter(boolean_indexing_set_or_inc),
52
+ "jax",
53
+ position=100,
54
+ )
55
+
56
+
57
+ @node_rewriter([Sum])
58
+ def boolean_indexing_sum(fgraph, node):
59
+ """Replace the sum of `AdvancedSubtensor` with exclusively boolean indexing.
60
+
61
+ JAX cannot JIT-compile functions that use boolean indexing, but can compile
62
+ those expressions that can be re-expressed using `jax.numpy.where`. This
63
+ rewrite re-rexpressed the model on the behalf of the user and thus allows to
64
+ improve upon JAX's API.
65
+
66
+ """
67
+ operand = node.inputs[0]
68
+
69
+ if not isinstance(operand, TensorVariable):
70
+ return
71
+
72
+ # If it's not a scalar reduction, it couldn't have been a pure boolean mask
73
+ if node.outputs[0].ndim != 0:
74
+ return
75
+
76
+ if operand.owner is None:
77
+ return
78
+
79
+ if not isinstance(operand.owner.op, AdvancedSubtensor):
80
+ return
81
+
82
+ # Get out if AdvancedSubtensor has more than a single indexing operation
83
+ if len(operand.owner.inputs) > 2:
84
+ return
85
+
86
+ [x, cond] = operand.owner.inputs
87
+
88
+ if not isinstance(cond, TensorVariable):
89
+ return
90
+
91
+ if not cond.type.dtype == "bool":
92
+ return
93
+
94
+ # Output must be a scalar, since pure boolean indexing returns a vector
95
+ # No need to worry about axis
96
+ out = pt.sum(pt.where(cond, x, 0))
97
+ return out.owner.outputs
98
+
99
+
100
+ optdb.register(
101
+ "jax_boolean_indexing_sum", dfs_rewriter(boolean_indexing_sum), "jax", position=100
102
+ )
103
+
104
+
105
+ @node_rewriter([Reshape])
106
+ def shape_parameter_as_tuple(fgraph, node):
107
+ """Replace `MakeVector` and `DimShuffle` (when used to transform a scalar
108
+ into a 1d vector) when they are found as the input of a `shape`
109
+ parameter by `JAXShapeTuple` during transpilation.
110
+
111
+ The JAX implementations of `MakeVector` and `DimShuffle` always return JAX
112
+ `TracedArrays`, but JAX only accepts concrete values as inputs for the `size`
113
+ or `shape` parameter. When these `Op`s are used to convert scalar or tuple
114
+ inputs, however, we can avoid tracing by making them return a tuple of their
115
+ inputs instead.
116
+
117
+ Note that JAX does not accept scalar inputs for the `size` or `shape`
118
+ parameters, and this rewrite also ensures that scalar inputs are turned into
119
+ tuples during transpilation.
120
+
121
+ """
122
+ from pytensor.link.jax.dispatch.shape import JAXShapeTuple
123
+
124
+ shape_arg = node.inputs[1]
125
+ shape_node = shape_arg.owner
126
+
127
+ if shape_node is None:
128
+ return
129
+
130
+ if isinstance(shape_node.op, JAXShapeTuple):
131
+ return
132
+
133
+ if isinstance(shape_node.op, MakeVector) or (
134
+ isinstance(shape_node.op, DimShuffle)
135
+ and shape_node.op.input_ndim == 0
136
+ and shape_node.op.new_order == ("x",)
137
+ ):
138
+ # Here PyTensor converted a tuple or list to a tensor
139
+ new_shape_args = JAXShapeTuple()(*shape_node.inputs)
140
+ new_inputs = list(node.inputs)
141
+ new_inputs[1] = new_shape_args
142
+
143
+ new_node = node.clone_with_new_inputs(new_inputs)
144
+ return new_node.outputs
145
+
146
+
147
+ optdb.register(
148
+ "jax_shape_parameter_as_tuple",
149
+ dfs_rewriter(shape_parameter_as_tuple),
150
+ "jax",
151
+ position=100,
152
+ )
153
+
154
+
155
+ @node_rewriter([Blockwise])
156
+ def blockwise_unbatched_scalar_inputs(fgraph, node):
157
+ """Lower Blockwise with explicit scalar inputs that bypass vectorization.
158
+
159
+ Removing padding before JAX conversion preserves concrete values such as
160
+ ``Shape_i`` outputs. Unbatched scalars that are already traced stay traced.
161
+ """
162
+ unbatched_inputs = tuple(
163
+ i
164
+ for i, (inp, sig) in enumerate(
165
+ zip(node.inputs, node.op.inputs_sig, strict=True)
166
+ )
167
+ if not sig and all(inp.type.broadcastable)
168
+ )
169
+ if not unbatched_inputs:
170
+ return None
171
+
172
+ batch_ndim = node.op.batch_ndim(node)
173
+ inputs = list(node.inputs)
174
+ for i in unbatched_inputs:
175
+ scalar_input = apply_local_dimshuffle_merge(
176
+ fgraph, _squeeze_left(inputs[i], batch_ndim)
177
+ )
178
+ inputs[i] = pt.as_tensor_variable(
179
+ get_underlying_scalar_constant_value(
180
+ scalar_input, raise_not_constant=False
181
+ ),
182
+ dtype=scalar_input.dtype,
183
+ )
184
+ inner_inputs = [inp.type() for inp in inputs]
185
+ padded_inputs = [
186
+ pt.shape_padleft(inp, batch_ndim) if i in unbatched_inputs else inp
187
+ for i, inp in enumerate(inner_inputs)
188
+ ]
189
+ inner_outputs = node.op(*padded_inputs, return_list=True)
190
+ outputs = JAXBlockwise(
191
+ inner_inputs, inner_outputs, destroy_map=node.op.destroy_map
192
+ )(*inputs, return_list=True)
193
+ return copy_stack_trace(node.outputs, outputs)
194
+
195
+
196
+ optdb.register(
197
+ "jax_blockwise_unbatched_scalar_inputs",
198
+ dfs_rewriter(blockwise_unbatched_scalar_inputs),
199
+ "jax",
200
+ position=100,
201
+ )
@@ -0,0 +1,12 @@
1
+ """Linear algebra rewrites, organized by category.
2
+
3
+ Importing this package registers all linalg rewrites with the optimizer.
4
+ """
5
+
6
+ import pytensor.tensor.rewriting.linalg.decomposition
7
+ import pytensor.tensor.rewriting.linalg.inverse
8
+ import pytensor.tensor.rewriting.linalg.products
9
+ import pytensor.tensor.rewriting.linalg.reassociate_matmul
10
+ import pytensor.tensor.rewriting.linalg.solvers
11
+ import pytensor.tensor.rewriting.linalg.summary
12
+ import pytensor.tensor.rewriting.linalg.utils