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.
- pytensor/__init__.py +61 -0
- pytensor/_sparse_lazy.py +31 -0
- pytensor/_version.py +21 -0
- pytensor/assumptions/__init__.py +108 -0
- pytensor/assumptions/alloc.py +181 -0
- pytensor/assumptions/blockwise.py +13 -0
- pytensor/assumptions/bundles.py +55 -0
- pytensor/assumptions/core.py +684 -0
- pytensor/assumptions/diagonal.py +153 -0
- pytensor/assumptions/dimshuffle.py +54 -0
- pytensor/assumptions/dot.py +58 -0
- pytensor/assumptions/elemwise.py +84 -0
- pytensor/assumptions/orthogonal.py +85 -0
- pytensor/assumptions/permutation.py +80 -0
- pytensor/assumptions/positive_definite.py +153 -0
- pytensor/assumptions/reshape.py +28 -0
- pytensor/assumptions/selection.py +131 -0
- pytensor/assumptions/shape.py +30 -0
- pytensor/assumptions/specify.py +189 -0
- pytensor/assumptions/subtensor.py +88 -0
- pytensor/assumptions/symmetric.py +129 -0
- pytensor/assumptions/triangular.py +203 -0
- pytensor/assumptions/tridiagonal.py +108 -0
- pytensor/assumptions/unit_magnitude.py +41 -0
- pytensor/basic.py +46 -0
- pytensor/bin/__init__.py +0 -0
- pytensor/bin/pytensor_cache.py +112 -0
- pytensor/breakpoint.py +161 -0
- pytensor/compile/__init__.py +65 -0
- pytensor/compile/aliasing.py +260 -0
- pytensor/compile/builders.py +980 -0
- pytensor/compile/compiledir.py +272 -0
- pytensor/compile/compilelock.py +87 -0
- pytensor/compile/debug/__init__.py +0 -0
- pytensor/compile/debug/debugmode.py +2310 -0
- pytensor/compile/debug/dump.py +75 -0
- pytensor/compile/debug/monitormode.py +111 -0
- pytensor/compile/debug/nanguardmode.py +260 -0
- pytensor/compile/debug/profiling.py +1573 -0
- pytensor/compile/executor.py +879 -0
- pytensor/compile/inner_function.py +74 -0
- pytensor/compile/io.py +255 -0
- pytensor/compile/maker.py +722 -0
- pytensor/compile/mode.py +633 -0
- pytensor/compile/ops.py +315 -0
- pytensor/compile/rebuild.py +548 -0
- pytensor/compile/rewriting.py +221 -0
- pytensor/compile/sharedvalue.py +229 -0
- pytensor/conditional/__init__.py +7 -0
- pytensor/conditional/op.py +455 -0
- pytensor/conditional/rewriting.py +651 -0
- pytensor/configdefaults.py +1143 -0
- pytensor/configparser.py +584 -0
- pytensor/d3viz/__init__.py +1 -0
- pytensor/d3viz/css/d3-context-menu.css +29 -0
- pytensor/d3viz/css/d3viz.css +98 -0
- pytensor/d3viz/d3viz.py +130 -0
- pytensor/d3viz/formatting.py +369 -0
- pytensor/d3viz/html/template.html +101 -0
- pytensor/d3viz/js/d3-context-menu.js +42 -0
- pytensor/d3viz/js/d3.v3.min.js +5 -0
- pytensor/d3viz/js/d3viz.js +969 -0
- pytensor/d3viz/js/dagre-d3.min.js +2 -0
- pytensor/d3viz/js/graphlib-dot.min.js +4 -0
- pytensor/gradient.py +2508 -0
- pytensor/graph/__init__.py +19 -0
- pytensor/graph/basic.py +1324 -0
- pytensor/graph/destroyhandler.py +788 -0
- pytensor/graph/features.py +959 -0
- pytensor/graph/fg.py +1253 -0
- pytensor/graph/null_type.py +42 -0
- pytensor/graph/op.py +780 -0
- pytensor/graph/replace.py +345 -0
- pytensor/graph/rewriting/__init__.py +4 -0
- pytensor/graph/rewriting/basic.py +3139 -0
- pytensor/graph/rewriting/db.py +547 -0
- pytensor/graph/rewriting/kanren.py +329 -0
- pytensor/graph/rewriting/reachability.py +135 -0
- pytensor/graph/rewriting/unify.py +594 -0
- pytensor/graph/rewriting/utils.py +116 -0
- pytensor/graph/traversal.py +783 -0
- pytensor/graph/type.py +242 -0
- pytensor/graph/utils.py +350 -0
- pytensor/ifelse.py +37 -0
- pytensor/ipython.py +215 -0
- pytensor/link/__init__.py +1 -0
- pytensor/link/basic.py +745 -0
- pytensor/link/c/__init__.py +0 -0
- pytensor/link/c/basic.py +2012 -0
- pytensor/link/c/c_code/lazylinker_c.c +1027 -0
- pytensor/link/c/c_code/pytensor_mod_helper.h +25 -0
- pytensor/link/c/cmodule.py +2937 -0
- pytensor/link/c/cutils.py +116 -0
- pytensor/link/c/cvm.py +29 -0
- pytensor/link/c/exceptions.py +12 -0
- pytensor/link/c/interface.py +632 -0
- pytensor/link/c/lazylinker_c.py +147 -0
- pytensor/link/c/op.py +646 -0
- pytensor/link/c/params_type.py +885 -0
- pytensor/link/c/type.py +787 -0
- pytensor/link/jax/__init__.py +1 -0
- pytensor/link/jax/dispatch/__init__.py +23 -0
- pytensor/link/jax/dispatch/basic.py +159 -0
- pytensor/link/jax/dispatch/blas.py +14 -0
- pytensor/link/jax/dispatch/blockwise.py +63 -0
- pytensor/link/jax/dispatch/einsum.py +20 -0
- pytensor/link/jax/dispatch/elemwise.py +107 -0
- pytensor/link/jax/dispatch/extra_ops.py +143 -0
- pytensor/link/jax/dispatch/linalg/__init__.py +8 -0
- pytensor/link/jax/dispatch/linalg/constructors.py +12 -0
- pytensor/link/jax/dispatch/linalg/decomposition.py +140 -0
- pytensor/link/jax/dispatch/linalg/inverse.py +20 -0
- pytensor/link/jax/dispatch/linalg/products.py +43 -0
- pytensor/link/jax/dispatch/linalg/solvers.py +103 -0
- pytensor/link/jax/dispatch/linalg/summary.py +20 -0
- pytensor/link/jax/dispatch/math.py +73 -0
- pytensor/link/jax/dispatch/pad.py +53 -0
- pytensor/link/jax/dispatch/random.py +454 -0
- pytensor/link/jax/dispatch/scalar.py +357 -0
- pytensor/link/jax/dispatch/scan.py +247 -0
- pytensor/link/jax/dispatch/shape.py +106 -0
- pytensor/link/jax/dispatch/signal/__init__.py +1 -0
- pytensor/link/jax/dispatch/signal/conv.py +24 -0
- pytensor/link/jax/dispatch/sort.py +26 -0
- pytensor/link/jax/dispatch/sparse.py +38 -0
- pytensor/link/jax/dispatch/subtensor.py +73 -0
- pytensor/link/jax/dispatch/tensor_basic.py +195 -0
- pytensor/link/jax/linker.py +142 -0
- pytensor/link/jax/ops.py +568 -0
- pytensor/link/mlx/__init__.py +1 -0
- pytensor/link/mlx/dispatch/__init__.py +21 -0
- pytensor/link/mlx/dispatch/basic.py +244 -0
- pytensor/link/mlx/dispatch/blas.py +42 -0
- pytensor/link/mlx/dispatch/blockwise.py +88 -0
- pytensor/link/mlx/dispatch/einsum.py +14 -0
- pytensor/link/mlx/dispatch/elemwise.py +133 -0
- pytensor/link/mlx/dispatch/extra_ops.py +35 -0
- pytensor/link/mlx/dispatch/linalg/__init__.py +7 -0
- pytensor/link/mlx/dispatch/linalg/decomposition.py +183 -0
- pytensor/link/mlx/dispatch/linalg/inverse.py +26 -0
- pytensor/link/mlx/dispatch/linalg/products.py +22 -0
- pytensor/link/mlx/dispatch/linalg/solvers.py +95 -0
- pytensor/link/mlx/dispatch/linalg/summary.py +40 -0
- pytensor/link/mlx/dispatch/math.py +82 -0
- pytensor/link/mlx/dispatch/pad.py +55 -0
- pytensor/link/mlx/dispatch/random.py +276 -0
- pytensor/link/mlx/dispatch/scalar/__init__.py +10 -0
- pytensor/link/mlx/dispatch/scalar/basic.py +127 -0
- pytensor/link/mlx/dispatch/scalar/bessel.py +508 -0
- pytensor/link/mlx/dispatch/scalar/beta.py +1131 -0
- pytensor/link/mlx/dispatch/scalar/erf.py +316 -0
- pytensor/link/mlx/dispatch/scalar/erfcinv.py +152 -0
- pytensor/link/mlx/dispatch/scalar/gamma.py +58 -0
- pytensor/link/mlx/dispatch/scalar/gammainc.py +552 -0
- pytensor/link/mlx/dispatch/scalar/helpers.py +214 -0
- pytensor/link/mlx/dispatch/scalar/math.py +271 -0
- pytensor/link/mlx/dispatch/shape.py +56 -0
- pytensor/link/mlx/dispatch/signal/__init__.py +0 -0
- pytensor/link/mlx/dispatch/signal/conv.py +33 -0
- pytensor/link/mlx/dispatch/sort.py +38 -0
- pytensor/link/mlx/dispatch/subtensor.py +118 -0
- pytensor/link/mlx/dispatch/tensor_basic.py +256 -0
- pytensor/link/mlx/linker.py +143 -0
- pytensor/link/numba/__init__.py +1 -0
- pytensor/link/numba/cache.py +207 -0
- pytensor/link/numba/dispatch/__init__.py +29 -0
- pytensor/link/numba/dispatch/_gamma_tables.py +821 -0
- pytensor/link/numba/dispatch/_llvmlite_self_ref.py +66 -0
- pytensor/link/numba/dispatch/_patch_list_to_tuple.py +330 -0
- pytensor/link/numba/dispatch/_patch_pointer_add.py +40 -0
- pytensor/link/numba/dispatch/array_utils.py +26 -0
- pytensor/link/numba/dispatch/basic.py +583 -0
- pytensor/link/numba/dispatch/blockwise.py +125 -0
- pytensor/link/numba/dispatch/compile_ops.py +178 -0
- pytensor/link/numba/dispatch/cython_support.py +197 -0
- pytensor/link/numba/dispatch/elemwise.py +1598 -0
- pytensor/link/numba/dispatch/extra_ops.py +351 -0
- pytensor/link/numba/dispatch/linalg/_BLAS.py +195 -0
- pytensor/link/numba/dispatch/linalg/_LAPACK.py +2003 -0
- pytensor/link/numba/dispatch/linalg/__init__.py +6 -0
- pytensor/link/numba/dispatch/linalg/constructors.py +90 -0
- pytensor/link/numba/dispatch/linalg/decomposition/__init__.py +0 -0
- pytensor/link/numba/dispatch/linalg/decomposition/cholesky.py +81 -0
- pytensor/link/numba/dispatch/linalg/decomposition/dispatch.py +671 -0
- pytensor/link/numba/dispatch/linalg/decomposition/eigen.py +1029 -0
- pytensor/link/numba/dispatch/linalg/decomposition/lu.py +192 -0
- pytensor/link/numba/dispatch/linalg/decomposition/lu_factor.py +90 -0
- pytensor/link/numba/dispatch/linalg/decomposition/qr.py +722 -0
- pytensor/link/numba/dispatch/linalg/decomposition/qz.py +1285 -0
- pytensor/link/numba/dispatch/linalg/decomposition/schur.py +225 -0
- pytensor/link/numba/dispatch/linalg/decomposition/svd.py +709 -0
- pytensor/link/numba/dispatch/linalg/inverse.py +40 -0
- pytensor/link/numba/dispatch/linalg/products.py +881 -0
- pytensor/link/numba/dispatch/linalg/solvers/__init__.py +0 -0
- pytensor/link/numba/dispatch/linalg/solvers/cholesky.py +85 -0
- pytensor/link/numba/dispatch/linalg/solvers/dispatch.py +189 -0
- pytensor/link/numba/dispatch/linalg/solvers/general.py +85 -0
- pytensor/link/numba/dispatch/linalg/solvers/hermitian.py +136 -0
- pytensor/link/numba/dispatch/linalg/solvers/linear_control.py +98 -0
- pytensor/link/numba/dispatch/linalg/solvers/lu_solve.py +134 -0
- pytensor/link/numba/dispatch/linalg/solvers/posdef.py +110 -0
- pytensor/link/numba/dispatch/linalg/solvers/symmetric.py +134 -0
- pytensor/link/numba/dispatch/linalg/solvers/triangular.py +132 -0
- pytensor/link/numba/dispatch/linalg/solvers/tridiagonal.py +368 -0
- pytensor/link/numba/dispatch/linalg/solvers/utils.py +11 -0
- pytensor/link/numba/dispatch/linalg/summary.py +46 -0
- pytensor/link/numba/dispatch/linalg/utils.py +133 -0
- pytensor/link/numba/dispatch/optimize/__init__.py +2 -0
- pytensor/link/numba/dispatch/optimize/scalar_minimize.py +489 -0
- pytensor/link/numba/dispatch/optimize/scalar_root.py +652 -0
- pytensor/link/numba/dispatch/optimize/utils.py +48 -0
- pytensor/link/numba/dispatch/random.py +518 -0
- pytensor/link/numba/dispatch/scalar.py +655 -0
- pytensor/link/numba/dispatch/scan.py +457 -0
- pytensor/link/numba/dispatch/shape.py +78 -0
- pytensor/link/numba/dispatch/signal/__init__.py +1 -0
- pytensor/link/numba/dispatch/signal/conv.py +125 -0
- pytensor/link/numba/dispatch/sort.py +65 -0
- pytensor/link/numba/dispatch/sparse/__init__.py +1 -0
- pytensor/link/numba/dispatch/sparse/basic.py +914 -0
- pytensor/link/numba/dispatch/sparse/math.py +1367 -0
- pytensor/link/numba/dispatch/sparse/variable.py +651 -0
- pytensor/link/numba/dispatch/string_codegen.py +31 -0
- pytensor/link/numba/dispatch/subtensor.py +887 -0
- pytensor/link/numba/dispatch/tensor_basic.py +501 -0
- pytensor/link/numba/dispatch/typed_list.py +251 -0
- pytensor/link/numba/dispatch/vectorize_codegen.py +1519 -0
- pytensor/link/numba/linker.py +32 -0
- pytensor/link/pytorch/dispatch/__init__.py +15 -0
- pytensor/link/pytorch/dispatch/basic.py +226 -0
- pytensor/link/pytorch/dispatch/blas.py +14 -0
- pytensor/link/pytorch/dispatch/blockwise.py +32 -0
- pytensor/link/pytorch/dispatch/elemwise.py +231 -0
- pytensor/link/pytorch/dispatch/extra_ops.py +53 -0
- pytensor/link/pytorch/dispatch/linalg/__init__.py +6 -0
- pytensor/link/pytorch/dispatch/linalg/decomposition.py +77 -0
- pytensor/link/pytorch/dispatch/linalg/inverse.py +22 -0
- pytensor/link/pytorch/dispatch/linalg/products.py +12 -0
- pytensor/link/pytorch/dispatch/linalg/summary.py +20 -0
- pytensor/link/pytorch/dispatch/math.py +14 -0
- pytensor/link/pytorch/dispatch/scalar.py +115 -0
- pytensor/link/pytorch/dispatch/shape.py +58 -0
- pytensor/link/pytorch/dispatch/sort.py +27 -0
- pytensor/link/pytorch/dispatch/subtensor.py +134 -0
- pytensor/link/pytorch/linker.py +104 -0
- pytensor/link/utils.py +949 -0
- pytensor/link/vm.py +1335 -0
- pytensor/misc/__init__.py +0 -0
- pytensor/misc/check_blas.py +300 -0
- pytensor/misc/check_blas_many.sh +21 -0
- pytensor/misc/check_duplicate_key.py +71 -0
- pytensor/misc/elemwise_openmp_speedup.py +77 -0
- pytensor/misc/elemwise_time_test.py +76 -0
- pytensor/misc/frozendict.py +44 -0
- pytensor/misc/may_share_memory.py +38 -0
- pytensor/misc/ordered_set.py +44 -0
- pytensor/misc/pkl_utils.py +64 -0
- pytensor/npy_2_compat.py +22 -0
- pytensor/printing.py +2731 -0
- pytensor/py.typed +0 -0
- pytensor/raise_op.py +203 -0
- pytensor/scalar/__init__.py +2 -0
- pytensor/scalar/basic.py +4357 -0
- pytensor/scalar/c_code/Faddeeva.cc +2517 -0
- pytensor/scalar/c_code/Faddeeva.hh +62 -0
- pytensor/scalar/c_code/gamma.c +375 -0
- pytensor/scalar/c_code/incbet.c +313 -0
- pytensor/scalar/loop.py +328 -0
- pytensor/scalar/math.py +1913 -0
- pytensor/scalar/sharedvar.py +61 -0
- pytensor/scan/__init__.py +57 -0
- pytensor/scan/basic.py +1192 -0
- pytensor/scan/checkpoints.py +191 -0
- pytensor/scan/op.py +3633 -0
- pytensor/scan/rewriting/__init__.py +30 -0
- pytensor/scan/rewriting/constant_folding.py +63 -0
- pytensor/scan/rewriting/db.py +283 -0
- pytensor/scan/rewriting/inner_graph.py +319 -0
- pytensor/scan/rewriting/inplace.py +197 -0
- pytensor/scan/rewriting/io.py +306 -0
- pytensor/scan/rewriting/merge.py +562 -0
- pytensor/scan/rewriting/push_out.py +986 -0
- pytensor/scan/rewriting/trace.py +1080 -0
- pytensor/scan/rewriting/utils.py +151 -0
- pytensor/scan/scan_perform.c +40260 -0
- pytensor/scan/scan_perform.cp315-win_amd64.pyd +0 -0
- pytensor/scan/scan_perform.pyx +603 -0
- pytensor/scan/scan_perform_ext.py +17 -0
- pytensor/scan/utils.py +909 -0
- pytensor/scan/views.py +260 -0
- pytensor/sparse/__init__.py +5 -0
- pytensor/sparse/basic.py +1919 -0
- pytensor/sparse/linalg.py +96 -0
- pytensor/sparse/math.py +2151 -0
- pytensor/sparse/rewriting.py +2057 -0
- pytensor/sparse/sharedvar.py +30 -0
- pytensor/sparse/type.py +253 -0
- pytensor/sparse/utils.py +20 -0
- pytensor/sparse/variable.py +481 -0
- pytensor/tensor/__init__.py +162 -0
- pytensor/tensor/basic.py +4514 -0
- pytensor/tensor/blas/__init__.py +145 -0
- pytensor/tensor/blas/_core.py +189 -0
- pytensor/tensor/blas/batched.py +278 -0
- pytensor/tensor/blas/blas_c.py +110 -0
- pytensor/tensor/blas/c_code/__init__.py +0 -0
- pytensor/tensor/blas/c_code/alt_blas_common.h +21 -0
- pytensor/tensor/blas/c_code/alt_blas_template.c +369 -0
- pytensor/tensor/blas/c_code/blas_headers.py +1210 -0
- pytensor/tensor/blas/c_code/codegen.py +888 -0
- pytensor/tensor/blas/gemm.py +362 -0
- pytensor/tensor/blas/gemv.py +117 -0
- pytensor/tensor/blockwise.py +635 -0
- pytensor/tensor/einsum.py +818 -0
- pytensor/tensor/elemwise.py +1839 -0
- pytensor/tensor/elemwise_cgen.py +761 -0
- pytensor/tensor/exceptions.py +16 -0
- pytensor/tensor/extra_ops.py +1975 -0
- pytensor/tensor/fft.py +222 -0
- pytensor/tensor/fourier.py +188 -0
- pytensor/tensor/functional.py +131 -0
- pytensor/tensor/interpolate.py +200 -0
- pytensor/tensor/linalg/__init__.py +134 -0
- pytensor/tensor/linalg/_lazy.py +12 -0
- pytensor/tensor/linalg/constructors.py +121 -0
- pytensor/tensor/linalg/decomposition/__init__.py +1 -0
- pytensor/tensor/linalg/decomposition/cholesky.py +198 -0
- pytensor/tensor/linalg/decomposition/eigen.py +516 -0
- pytensor/tensor/linalg/decomposition/lu.py +347 -0
- pytensor/tensor/linalg/decomposition/qr.py +383 -0
- pytensor/tensor/linalg/decomposition/schur.py +607 -0
- pytensor/tensor/linalg/decomposition/svd.py +244 -0
- pytensor/tensor/linalg/dtype_utils.py +42 -0
- pytensor/tensor/linalg/inverse.py +218 -0
- pytensor/tensor/linalg/products.py +302 -0
- pytensor/tensor/linalg/solvers/__init__.py +0 -0
- pytensor/tensor/linalg/solvers/core.py +126 -0
- pytensor/tensor/linalg/solvers/general.py +269 -0
- pytensor/tensor/linalg/solvers/linear_control.py +462 -0
- pytensor/tensor/linalg/solvers/lstsq.py +98 -0
- pytensor/tensor/linalg/solvers/psd.py +108 -0
- pytensor/tensor/linalg/solvers/triangular.py +142 -0
- pytensor/tensor/linalg/solvers/tridiagonal.py +227 -0
- pytensor/tensor/linalg/summary.py +344 -0
- pytensor/tensor/math.py +4397 -0
- pytensor/tensor/nlinalg.py +58 -0
- pytensor/tensor/optimize.py +1370 -0
- pytensor/tensor/pad.py +686 -0
- pytensor/tensor/random/__init__.py +7 -0
- pytensor/tensor/random/basic.py +2179 -0
- pytensor/tensor/random/op.py +543 -0
- pytensor/tensor/random/rewriting/__init__.py +11 -0
- pytensor/tensor/random/rewriting/basic.py +385 -0
- pytensor/tensor/random/rewriting/jax.py +196 -0
- pytensor/tensor/random/rewriting/numba.py +192 -0
- pytensor/tensor/random/type.py +175 -0
- pytensor/tensor/random/utils.py +352 -0
- pytensor/tensor/random/variable.py +252 -0
- pytensor/tensor/reshape.py +528 -0
- pytensor/tensor/rewriting/__init__.py +22 -0
- pytensor/tensor/rewriting/assumptions.py +65 -0
- pytensor/tensor/rewriting/basic.py +1630 -0
- pytensor/tensor/rewriting/blas.py +885 -0
- pytensor/tensor/rewriting/blas_c.py +52 -0
- pytensor/tensor/rewriting/blockwise.py +372 -0
- pytensor/tensor/rewriting/einsum.py +53 -0
- pytensor/tensor/rewriting/elemwise.py +1221 -0
- pytensor/tensor/rewriting/extra_ops.py +119 -0
- pytensor/tensor/rewriting/fused_elemwise.py +955 -0
- pytensor/tensor/rewriting/inplace.py +352 -0
- pytensor/tensor/rewriting/jax.py +201 -0
- pytensor/tensor/rewriting/linalg/__init__.py +12 -0
- pytensor/tensor/rewriting/linalg/decomposition.py +732 -0
- pytensor/tensor/rewriting/linalg/inverse.py +177 -0
- pytensor/tensor/rewriting/linalg/products.py +434 -0
- pytensor/tensor/rewriting/linalg/reassociate_matmul.py +685 -0
- pytensor/tensor/rewriting/linalg/solvers.py +892 -0
- pytensor/tensor/rewriting/linalg/summary.py +324 -0
- pytensor/tensor/rewriting/linalg/utils.py +181 -0
- pytensor/tensor/rewriting/math.py +4232 -0
- pytensor/tensor/rewriting/numba.py +120 -0
- pytensor/tensor/rewriting/ofg.py +82 -0
- pytensor/tensor/rewriting/optimize.py +105 -0
- pytensor/tensor/rewriting/reshape.py +72 -0
- pytensor/tensor/rewriting/scalarize.py +268 -0
- pytensor/tensor/rewriting/shape.py +1041 -0
- pytensor/tensor/rewriting/sort.py +98 -0
- pytensor/tensor/rewriting/special.py +184 -0
- pytensor/tensor/rewriting/subtensor.py +3199 -0
- pytensor/tensor/rewriting/subtensor_lift.py +1567 -0
- pytensor/tensor/rewriting/uncanonicalize.py +232 -0
- pytensor/tensor/shape.py +1007 -0
- pytensor/tensor/sharedvar.py +137 -0
- pytensor/tensor/signal/__init__.py +4 -0
- pytensor/tensor/signal/conv.py +348 -0
- pytensor/tensor/slinalg.py +65 -0
- pytensor/tensor/sort.py +213 -0
- pytensor/tensor/special.py +341 -0
- pytensor/tensor/subtensor.py +3058 -0
- pytensor/tensor/symbolic.py +45 -0
- pytensor/tensor/type.py +1416 -0
- pytensor/tensor/type_other.py +143 -0
- pytensor/tensor/utils.py +307 -0
- pytensor/tensor/var.py +11 -0
- pytensor/tensor/variable.py +1122 -0
- pytensor/tensor/xlogx.py +37 -0
- pytensor/typed_list/__init__.py +3 -0
- pytensor/typed_list/basic.py +663 -0
- pytensor/typed_list/rewriting.py +26 -0
- pytensor/typed_list/type.py +141 -0
- pytensor/utils.py +426 -0
- pytensor/xtensor/__init__.py +9 -0
- pytensor/xtensor/basic.py +153 -0
- pytensor/xtensor/indexing.py +265 -0
- pytensor/xtensor/linalg.py +102 -0
- pytensor/xtensor/math.py +653 -0
- pytensor/xtensor/random/__init__.py +2 -0
- pytensor/xtensor/random/basic.py +254 -0
- pytensor/xtensor/random/type.py +88 -0
- pytensor/xtensor/random/variable.py +204 -0
- pytensor/xtensor/reduction.py +136 -0
- pytensor/xtensor/rewriting/__init__.py +6 -0
- pytensor/xtensor/rewriting/basic.py +87 -0
- pytensor/xtensor/rewriting/indexing.py +213 -0
- pytensor/xtensor/rewriting/math.py +47 -0
- pytensor/xtensor/rewriting/reduction.py +72 -0
- pytensor/xtensor/rewriting/shape.py +197 -0
- pytensor/xtensor/rewriting/utils.py +71 -0
- pytensor/xtensor/rewriting/vectorization.py +114 -0
- pytensor/xtensor/shape.py +707 -0
- pytensor/xtensor/signal.py +74 -0
- pytensor/xtensor/type.py +1143 -0
- pytensor/xtensor/vectorization.py +686 -0
- pytensor-3.4.0.dist-info/METADATA +208 -0
- pytensor-3.4.0.dist-info/RECORD +439 -0
- pytensor-3.4.0.dist-info/WHEEL +5 -0
- pytensor-3.4.0.dist-info/entry_points.txt +2 -0
- pytensor-3.4.0.dist-info/licenses/LICENSE.txt +41 -0
- 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
|