pymc-devs / pymc-devs/pytensor

Default casting to int8 leads to easy overflow errors

Open
#1,073 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug NumPy compatibility
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Describe the issue:

Minimal example below but if you pass an 'int8' to the gamma distribution, you cannot draw from it. If you make it int16, however, no problem. This problem can show up unexpectedly when a chain of pytensor operations downcasts.

It is also sensitive to the value of mu. If mu is 51, it doesn't happen.

Reproduceable code example:
import pymc as pm
import pytensor.tensor as pt

mu = pt.as_tensor(50,dtype="int8")

rv = pm.Gamma.dist(
    mu = mu,
    sigma = 1
)
pm.draw(rv)
Error message:
ValueError: shape < 0
Apply node that caused the error: gamma_rv{0, (0, 0), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x1C107CE2260>), [], 11, -60.0, 0.02)
Toposort index: 0
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(0,)), TensorType(int64, shape=()), TensorType(float64, shape=()), TensorType(float64, shape=())]
Inputs shapes: ['No shapes', (0,), (), (), ()]
Inputs strides: ['No strides', (0,), (), (), ()]
Inputs values: [Generator(PCG64) at 0x1C107CE2260, array([], dtype=int64), array(11, dtype=int64), array(-60.), array(0.02)]
Outputs clients: [['output'], ['output']]
PyMC version information:

pymc: 5.14.0
pytensor: 2.20.0

Context for the issue:

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal PyMC and PyTensor example and trace the gamma_rv inputs shown in the error, especially the int8-derived values and resulting negative shape. The issue is done when an int8 mu no longer causes overflow or a negative shape during Gamma sampling, with regression coverage for the reported example.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.