Skip to content

Speedup import time with lazy import of scipy.stats #1268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pytensor/tensor/random/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Literal

import numpy as np
import scipy.stats as stats
from numpy import broadcast_shapes as np_broadcast_shapes
from numpy import einsum as np_einsum
from numpy import sqrt as np_sqrt
Expand All @@ -21,6 +20,11 @@
)


# Scipy.stats is considerably slow to import
# We import scipy.stats lazily inside `ScipyRandomVariable`
stats = None


try:
broadcast_shapes = np.broadcast_shapes
except AttributeError:
Expand Down Expand Up @@ -57,6 +61,9 @@ def rng_fn_scipy(cls, rng, *args, **kwargs):

@classmethod
def rng_fn(cls, *args, **kwargs):
global stats
if stats is None:
import scipy.stats as stats
size = args[-1]
res = cls.rng_fn_scipy(*args, **kwargs)

Expand Down