Skip to content

Default casting to int8 leads to easy overflow errors #1073

Open
@daniel-s-tccc

Description

@daniel-s-tccc

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions