Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
51cda52b
提交
51cda52b
authored
5月 07, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
5月 09, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid numpy broadcast_to and ndindex in hot loops
上级
10105bea
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
39 行增加
和
21 行删除
+39
-21
basic.py
pytensor/tensor/random/basic.py
+12
-17
utils.py
pytensor/tensor/random/utils.py
+2
-1
utils.py
pytensor/tensor/utils.py
+23
-0
test_basic.py
tests/tensor/random/test_basic.py
+2
-3
没有找到文件。
pytensor/tensor/random/basic.py
浏览文件 @
51cda52b
...
@@ -18,6 +18,7 @@ from pytensor.tensor.random.utils import (
...
@@ -18,6 +18,7 @@ from pytensor.tensor.random.utils import (
broadcast_params
,
broadcast_params
,
normalize_size_param
,
normalize_size_param
,
)
)
from
pytensor.tensor.utils
import
faster_broadcast_to
,
faster_ndindex
# Scipy.stats is considerably slow to import
# Scipy.stats is considerably slow to import
...
@@ -976,19 +977,13 @@ class DirichletRV(RandomVariable):
...
@@ -976,19 +977,13 @@ class DirichletRV(RandomVariable):
@classmethod
@classmethod
def
rng_fn
(
cls
,
rng
,
alphas
,
size
):
def
rng_fn
(
cls
,
rng
,
alphas
,
size
):
if
alphas
.
ndim
>
1
:
if
alphas
.
ndim
>
1
:
if
size
is
None
:
if
size
is
not
None
:
size
=
()
alphas
=
faster_broadcast_to
(
alphas
,
size
+
alphas
.
shape
[
-
1
:])
size
=
tuple
(
np
.
atleast_1d
(
size
))
if
size
:
alphas
=
np
.
broadcast_to
(
alphas
,
size
+
alphas
.
shape
[
-
1
:])
samples_shape
=
alphas
.
shape
samples_shape
=
alphas
.
shape
samples
=
np
.
empty
(
samples_shape
)
samples
=
np
.
empty
(
samples_shape
)
for
index
in
np
.
ndindex
(
*
samples_shape
[:
-
1
]):
for
index
in
faster_ndindex
(
samples_shape
[:
-
1
]):
samples
[
index
]
=
rng
.
dirichlet
(
alphas
[
index
])
samples
[
index
]
=
rng
.
dirichlet
(
alphas
[
index
])
return
samples
return
samples
else
:
else
:
return
rng
.
dirichlet
(
alphas
,
size
=
size
)
return
rng
.
dirichlet
(
alphas
,
size
=
size
)
...
@@ -1800,11 +1795,11 @@ class MultinomialRV(RandomVariable):
...
@@ -1800,11 +1795,11 @@ class MultinomialRV(RandomVariable):
if
size
is
None
:
if
size
is
None
:
n
,
p
=
broadcast_params
([
n
,
p
],
[
0
,
1
])
n
,
p
=
broadcast_params
([
n
,
p
],
[
0
,
1
])
else
:
else
:
n
=
np
.
broadcast_to
(
n
,
size
)
n
=
faster_
broadcast_to
(
n
,
size
)
p
=
np
.
broadcast_to
(
p
,
size
+
p
.
shape
[
-
1
:])
p
=
faster_
broadcast_to
(
p
,
size
+
p
.
shape
[
-
1
:])
res
=
np
.
empty
(
p
.
shape
,
dtype
=
cls
.
dtype
)
res
=
np
.
empty
(
p
.
shape
,
dtype
=
cls
.
dtype
)
for
idx
in
np
.
ndindex
(
p
.
shape
[:
-
1
]):
for
idx
in
faster_
ndindex
(
p
.
shape
[:
-
1
]):
res
[
idx
]
=
rng
.
multinomial
(
n
[
idx
],
p
[
idx
])
res
[
idx
]
=
rng
.
multinomial
(
n
[
idx
],
p
[
idx
])
return
res
return
res
else
:
else
:
...
@@ -1978,13 +1973,13 @@ class ChoiceWithoutReplacement(RandomVariable):
...
@@ -1978,13 +1973,13 @@ class ChoiceWithoutReplacement(RandomVariable):
p
.
shape
[:
batch_ndim
],
p
.
shape
[:
batch_ndim
],
)
)
a
=
np
.
broadcast_to
(
a
,
size
+
a
.
shape
[
batch_ndim
:])
a
=
faster_
broadcast_to
(
a
,
size
+
a
.
shape
[
batch_ndim
:])
if
p
is
not
None
:
if
p
is
not
None
:
p
=
np
.
broadcast_to
(
p
,
size
+
p
.
shape
[
batch_ndim
:])
p
=
faster_
broadcast_to
(
p
,
size
+
p
.
shape
[
batch_ndim
:])
a_indexed_shape
=
a
.
shape
[
len
(
size
)
+
1
:]
a_indexed_shape
=
a
.
shape
[
len
(
size
)
+
1
:]
out
=
np
.
empty
(
size
+
core_shape
+
a_indexed_shape
,
dtype
=
a
.
dtype
)
out
=
np
.
empty
(
size
+
core_shape
+
a_indexed_shape
,
dtype
=
a
.
dtype
)
for
idx
in
np
.
ndindex
(
size
):
for
idx
in
faster_
ndindex
(
size
):
out
[
idx
]
=
rng
.
choice
(
out
[
idx
]
=
rng
.
choice
(
a
[
idx
],
p
=
None
if
p
is
None
else
p
[
idx
],
size
=
core_shape
,
replace
=
False
a
[
idx
],
p
=
None
if
p
is
None
else
p
[
idx
],
size
=
core_shape
,
replace
=
False
)
)
...
@@ -2097,10 +2092,10 @@ class PermutationRV(RandomVariable):
...
@@ -2097,10 +2092,10 @@ class PermutationRV(RandomVariable):
if
size
is
None
:
if
size
is
None
:
size
=
x
.
shape
[:
batch_ndim
]
size
=
x
.
shape
[:
batch_ndim
]
else
:
else
:
x
=
np
.
broadcast_to
(
x
,
size
+
x
.
shape
[
batch_ndim
:])
x
=
faster_
broadcast_to
(
x
,
size
+
x
.
shape
[
batch_ndim
:])
out
=
np
.
empty
(
size
+
x
.
shape
[
batch_ndim
:],
dtype
=
x
.
dtype
)
out
=
np
.
empty
(
size
+
x
.
shape
[
batch_ndim
:],
dtype
=
x
.
dtype
)
for
idx
in
np
.
ndindex
(
size
):
for
idx
in
faster_
ndindex
(
size
):
out
[
idx
]
=
rng
.
permutation
(
x
[
idx
])
out
[
idx
]
=
rng
.
permutation
(
x
[
idx
])
return
out
return
out
...
...
pytensor/tensor/random/utils.py
浏览文件 @
51cda52b
...
@@ -15,6 +15,7 @@ from pytensor.tensor.extra_ops import broadcast_arrays, broadcast_to
...
@@ -15,6 +15,7 @@ from pytensor.tensor.extra_ops import broadcast_arrays, broadcast_to
from
pytensor.tensor.math
import
maximum
from
pytensor.tensor.math
import
maximum
from
pytensor.tensor.shape
import
shape_padleft
,
specify_shape
from
pytensor.tensor.shape
import
shape_padleft
,
specify_shape
from
pytensor.tensor.type
import
int_dtypes
from
pytensor.tensor.type
import
int_dtypes
from
pytensor.tensor.utils
import
faster_broadcast_to
from
pytensor.tensor.variable
import
TensorVariable
from
pytensor.tensor.variable
import
TensorVariable
...
@@ -125,7 +126,7 @@ def broadcast_params(
...
@@ -125,7 +126,7 @@ def broadcast_params(
shapes
=
params_broadcast_shapes
(
shapes
=
params_broadcast_shapes
(
param_shapes
,
ndims_params
,
use_pytensor
=
use_pytensor
param_shapes
,
ndims_params
,
use_pytensor
=
use_pytensor
)
)
broadcast_to_fn
=
broadcast_to
if
use_pytensor
else
np
.
broadcast_to
broadcast_to_fn
=
broadcast_to
if
use_pytensor
else
faster_
broadcast_to
# zip strict not specified because we are in a hot loop
# zip strict not specified because we are in a hot loop
bcast_params
=
[
bcast_params
=
[
...
...
pytensor/tensor/utils.py
浏览文件 @
51cda52b
import
re
import
re
from
collections.abc
import
Sequence
from
collections.abc
import
Sequence
from
itertools
import
product
from
typing
import
cast
from
typing
import
cast
import
numpy
as
np
import
numpy
as
np
from
numpy
import
nditer
import
pytensor
import
pytensor
from
pytensor.graph
import
FunctionGraph
,
Variable
from
pytensor.graph
import
FunctionGraph
,
Variable
...
@@ -233,3 +235,24 @@ def normalize_reduce_axis(axis, ndim: int) -> tuple[int, ...] | None:
...
@@ -233,3 +235,24 @@ def normalize_reduce_axis(axis, ndim: int) -> tuple[int, ...] | None:
# TODO: If axis tuple is equivalent to None, return None for more canonicalization?
# TODO: If axis tuple is equivalent to None, return None for more canonicalization?
return
cast
(
tuple
,
axis
)
return
cast
(
tuple
,
axis
)
def
faster_broadcast_to
(
x
,
shape
):
# Stripped down core logic of `np.broadcast_to`
return
nditer
(
(
x
,),
flags
=
[
"multi_index"
,
"zerosize_ok"
],
op_flags
=
[
"readonly"
],
itershape
=
shape
,
order
=
"C"
,
)
.
itviews
[
0
]
def
faster_ndindex
(
shape
:
Sequence
[
int
]):
"""Equivalent to `np.ndindex` but usually 10x faster.
Unlike `np.ndindex`, this function expects a single sequence of integers
https://github.com/numpy/numpy/issues/28921
"""
return
product
(
*
(
range
(
s
)
for
s
in
shape
))
tests/tensor/random/test_basic.py
浏览文件 @
51cda52b
...
@@ -746,9 +746,8 @@ test_mvnormal_cov_decomposition_method = create_mvnormal_cov_decomposition_metho
...
@@ -746,9 +746,8 @@ test_mvnormal_cov_decomposition_method = create_mvnormal_cov_decomposition_metho
],
],
)
)
def
test_dirichlet_samples
(
alphas
,
size
):
def
test_dirichlet_samples
(
alphas
,
size
):
def
dirichlet_test_fn
(
mean
=
None
,
cov
=
None
,
size
=
None
,
random_state
=
None
):
# FIXME: Is this just testing itself against itself?
if
size
is
None
:
def
dirichlet_test_fn
(
alphas
,
size
,
random_state
):
size
=
()
return
dirichlet
.
rng_fn
(
random_state
,
alphas
,
size
)
return
dirichlet
.
rng_fn
(
random_state
,
alphas
,
size
)
compare_sample_values
(
dirichlet
,
alphas
,
size
=
size
,
test_fn
=
dirichlet_test_fn
)
compare_sample_values
(
dirichlet
,
alphas
,
size
=
size
,
test_fn
=
dirichlet_test_fn
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论