Optimizing compiler for evaluating mathematical expressions on CPUs and GPUs.
Project description
PyTensor is a Python library that allows one to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays. It provides the computational backend for PyMC.
Features
A hackable, pure-Python codebase
Extensible graph framework suitable for rapid development of custom operators and symbolic optimizations
Implements an extensible graph transpilation framework that currently provides compilation via C, JAX, and Numba
Contrary to PyTorch and TensorFlow, PyTensor maintains a static graph which can be modified in-place to allow for advanced optimizations
Getting started
import pytensor
from pytensor import tensor as pt
# Declare two symbolic floating-point scalars
a = pt.dscalar("a")
b = pt.dscalar("b")
# Create a simple example expression
c = a + b
# Convert the expression into a callable object that takes `(a, b)`
# values as input and computes the value of `c`.
f_c = pytensor.function([a, b], c)
assert f_c(1.5, 2.5) == 4.0
# Compute the gradient of the example expression with respect to `a`
dc = pytensor.grad(c, a)
f_dc = pytensor.function([a, b], dc)
assert f_dc(1.5, 2.5) == 1.0
# Compiling functions with `pytensor.function` also optimizes
# expression graphs by removing unnecessary operations and
# replacing computations with more efficient ones.
v = pt.vector("v")
M = pt.matrix("M")
d = a/a + (M + a).dot(v)
pytensor.dprint(d)
# Add [id A]
# ├─ ExpandDims{axis=0} [id B]
# │ └─ True_div [id C]
# │ ├─ a [id D]
# │ └─ a [id D]
# └─ dot [id E]
# ├─ Add [id F]
# │ ├─ M [id G]
# │ └─ ExpandDims{axes=[0, 1]} [id H]
# │ └─ a [id D]
# └─ v [id I]
f_d = pytensor.function([a, v, M], d)
# `a/a` -> `1` and the dot product is replaced with a BLAS function
# (i.e. CGemv)
pytensor.dprint(f_d)
# Add [id A] 5
# ├─ [1.] [id B]
# └─ CGemv{inplace} [id C] 4
# ├─ AllocEmpty{dtype='float64'} [id D] 3
# │ └─ Shape_i{0} [id E] 2
# │ └─ M [id F]
# ├─ 1.0 [id G]
# ├─ Add [id H] 1
# │ ├─ M [id F]
# │ └─ ExpandDims{axes=[0, 1]} [id I] 0
# │ └─ a [id J]
# ├─ v [id K]
# └─ 0.0 [id L]
See the PyTensor documentation for in-depth tutorials.
Installation
The latest release of PyTensor can be installed from PyPI using pip:
pip install pytensor
Or via conda-forge:
conda install -c conda-forge pytensor
The current development branch of PyTensor can be installed from GitHub, also using pip:
pip install git+https://github.com/pymc-devs/pytensor
Background
Contributing
We welcome bug reports and fixes and improvements to the documentation.
For more information on contributing, please see the contributing guide.
A good place to start contributing is by looking through the issues here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pytensor-2.20.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8300df0ca05224d28b8a94522098294392f321708fd7db4d0a5986d66e569e5e |
|
MD5 | 1a3f69a65f748d7a55260a47d3718e7c |
|
BLAKE2b-256 | ffac3955b21972009cec21c5c357e9960c18b43909213c2c27fbd293055c8fd2 |
Hashes for pytensor-2.20.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75bcaca96486ee63cc61c304505e01adc7828b06f742357d2a5c5ea7bc753ad2 |
|
MD5 | 81bcb838d40e0db91e23b025734914c7 |
|
BLAKE2b-256 | d042e3305a8de5fc7e17d5c22ef1d9e7bd8d87cfb4a58dfc33ad61d41f4374ba |
Hashes for pytensor-2.20.0-cp312-cp312-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0649818d0acce09a7b46c0892675246c34e8c274e209c819dced67f09ce8e7b |
|
MD5 | 02504d7ff3d191e3fdaab0075080aa10 |
|
BLAKE2b-256 | 6cac1766a0c1ee207b6cf162cda447ec3902a830a5d0d2d31d335b59c1b76095 |
Hashes for pytensor-2.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3591171bb337601b855528e48f163b4032ce02c4b44696fc98871970820f8793 |
|
MD5 | 3ef4bcfd9708ef4f52a43e9849abdbbf |
|
BLAKE2b-256 | 2e207344af23c762cf3a483a041c087f27bbf8b2a1a1ef3cec99154e9ca1bfd9 |
Hashes for pytensor-2.20.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 217e4cfbbea218e6244b46ce0816b2cf7d3152d35d9f1743a42d8bc302b07495 |
|
MD5 | a625a29663327db25632a8b24bf71166 |
|
BLAKE2b-256 | 2fd55996fdaabb5db7b9e5a626675129837548834e67fde1e6018e494135dba9 |
Hashes for pytensor-2.20.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1289f5dabecda2211765bfe3e242547f6c790fe6ac8146a90335c2ca7145fc2c |
|
MD5 | f67308a0f52f0785888086f4d392b1a2 |
|
BLAKE2b-256 | 21a2c7b015578e168e9e73a6067f4db78651392682c8d1b646771ac1c2ee3fa6 |
Hashes for pytensor-2.20.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fa3cd3416f8f6eb4e58b61e36029531083a6eb099dcbb4a957d3d927c5aecc8 |
|
MD5 | ee9d2148fe65c15634e8e3be03fe2b53 |
|
BLAKE2b-256 | dcf7b9157a1dc22c0f62e0bd5cc62f643980f1dd9abbb29a717ccd9cfe2bb667 |
Hashes for pytensor-2.20.0-cp311-cp311-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cff07d2f3ef965b0dba6889d09c81856b57e7ae3a803e6ba287775c6c087f95 |
|
MD5 | 7e84d52555c9b38b453136e0f2339bf7 |
|
BLAKE2b-256 | 8804f206addd8970e859229f389f3008c570a3bd1e4a9581af0e912795e626ea |
Hashes for pytensor-2.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e19f2747c0152ccedd0cb5bc875509884ef3b0792772578b17437ebe2f1f40f7 |
|
MD5 | 952271fc977ed8fc95c84845653cdc21 |
|
BLAKE2b-256 | ea69bf43b8d33c8a700c552ddadbe559730eff26c0fece048c4275e0491f2378 |
Hashes for pytensor-2.20.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51053081cd3afc3e0012ad4e2e07079bf74dadafc5f110e1c478337124a621fd |
|
MD5 | f8fee8833bbf9ce831eaa8b06ffc1f68 |
|
BLAKE2b-256 | 47d10521e92904ac43fa3dda5e928e04508708b62d3374d30ad1a87666c09fbc |
Hashes for pytensor-2.20.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42391b52e411fb783d87cb42193a163d6fa23ed4be3df6d349731a4a2432f9c2 |
|
MD5 | 271189bead9ead6755708d96c927f495 |
|
BLAKE2b-256 | e42ad855933a13cfdee375bd9e4b2e0c4540f938f7646702887f20ee255c99f3 |
Hashes for pytensor-2.20.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1df41bfd00bd2f15f9c00d022d2b1d09e85cd1bd2655d2c2f43b74d2e3ad336 |
|
MD5 | 9dc349af54f63a70cef0a198d17960e1 |
|
BLAKE2b-256 | 5de5738aca1fd67d4f67ed36d07196e1c6c96ab8035d3b4128ea255213179e69 |
Hashes for pytensor-2.20.0-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29e40796ddc1ca6e928f7e5ed2ad1883982a3dfa2d43f971079c94183e5b1de8 |
|
MD5 | 5495dfb55773c56cae228039814b32af |
|
BLAKE2b-256 | 70e0a264c0273f8f94eac809712e944aaac4d333f65f9979187161f9059713c9 |
Hashes for pytensor-2.20.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 633a5c3fcadf46632b1614fc67ba5cafe0340aefbd7f9a2796f66839e52e3cdd |
|
MD5 | 911e6d6d998e349927e8c04588fb5e56 |
|
BLAKE2b-256 | 15e925b80d8f8ea703300768ce6587fb2fccacd5536e4d418a268683fe387a77 |
Hashes for pytensor-2.20.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cd0e66fab98530b5159481b682d63a8bebc9ca10d631b0d71802f367a8a8cfd |
|
MD5 | f63f3245e29d227ac3c19d0fb5513ea0 |
|
BLAKE2b-256 | 1700aed7e6972ff7e87906819a9ae1c9308c89eb1c3dda215d6384d6a48edbd1 |