Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c90ee4ba
提交
c90ee4ba
authored
10月 09, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
10月 16, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move shape Ops dispatchers to their own file
上级
9da643e8
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
179 行增加
和
165 行删除
+179
-165
__init__.py
pytensor/link/numba/dispatch/__init__.py
+1
-0
basic.py
pytensor/link/numba/dispatch/basic.py
+0
-73
shape.py
pytensor/link/numba/dispatch/shape.py
+78
-0
test_basic.py
tests/link/numba/test_basic.py
+0
-92
test_shape.py
tests/link/numba/test_shape.py
+100
-0
没有找到文件。
pytensor/link/numba/dispatch/__init__.py
浏览文件 @
c90ee4ba
...
@@ -9,6 +9,7 @@ import pytensor.link.numba.dispatch.nlinalg
...
@@ -9,6 +9,7 @@ import pytensor.link.numba.dispatch.nlinalg
import
pytensor.link.numba.dispatch.random
import
pytensor.link.numba.dispatch.random
import
pytensor.link.numba.dispatch.scan
import
pytensor.link.numba.dispatch.scan
import
pytensor.link.numba.dispatch.scalar
import
pytensor.link.numba.dispatch.scalar
import
pytensor.link.numba.dispatch.shape
import
pytensor.link.numba.dispatch.signal
import
pytensor.link.numba.dispatch.signal
import
pytensor.link.numba.dispatch.slinalg
import
pytensor.link.numba.dispatch.slinalg
import
pytensor.link.numba.dispatch.sparse
import
pytensor.link.numba.dispatch.sparse
...
...
pytensor/link/numba/dispatch/basic.py
浏览文件 @
c90ee4ba
import
warnings
import
warnings
from
copy
import
copy
from
copy
import
copy
from
functools
import
singledispatch
from
functools
import
singledispatch
from
textwrap
import
dedent
import
numba
import
numba
import
numba.np.unsafe.ndarray
as
numba_ndarray
import
numpy
as
np
import
numpy
as
np
from
numba
import
types
from
numba
import
types
from
numba.core.errors
import
NumbaWarning
,
TypingError
from
numba.core.errors
import
NumbaWarning
,
TypingError
...
@@ -22,7 +20,6 @@ from pytensor.graph.type import Type
...
@@ -22,7 +20,6 @@ from pytensor.graph.type import Type
from
pytensor.ifelse
import
IfElse
from
pytensor.ifelse
import
IfElse
from
pytensor.link.numba.dispatch.sparse
import
CSCMatrixType
,
CSRMatrixType
from
pytensor.link.numba.dispatch.sparse
import
CSCMatrixType
,
CSRMatrixType
from
pytensor.link.utils
import
(
from
pytensor.link.utils
import
(
compile_function_src
,
fgraph_to_python
,
fgraph_to_python
,
)
)
from
pytensor.scalar.basic
import
ScalarType
from
pytensor.scalar.basic
import
ScalarType
...
@@ -30,10 +27,8 @@ from pytensor.sparse import SparseTensorType
...
@@ -30,10 +27,8 @@ from pytensor.sparse import SparseTensorType
from
pytensor.tensor.basic
import
Nonzero
from
pytensor.tensor.basic
import
Nonzero
from
pytensor.tensor.blas
import
BatchedDot
from
pytensor.tensor.blas
import
BatchedDot
from
pytensor.tensor.math
import
Dot
from
pytensor.tensor.math
import
Dot
from
pytensor.tensor.shape
import
Reshape
,
Shape
,
Shape_i
,
SpecifyShape
from
pytensor.tensor.sort
import
ArgSortOp
,
SortOp
from
pytensor.tensor.sort
import
ArgSortOp
,
SortOp
from
pytensor.tensor.type
import
TensorType
from
pytensor.tensor.type
import
TensorType
from
pytensor.tensor.type_other
import
NoneConst
def
numba_njit
(
*
args
,
fastmath
=
None
,
**
kwargs
):
def
numba_njit
(
*
args
,
fastmath
=
None
,
**
kwargs
):
...
@@ -322,26 +317,6 @@ def numba_funcify_DeepCopyOp(op, node, **kwargs):
...
@@ -322,26 +317,6 @@ def numba_funcify_DeepCopyOp(op, node, **kwargs):
return
deepcopyop
return
deepcopyop
@numba_funcify.register
(
Shape
)
def
numba_funcify_Shape
(
op
,
**
kwargs
):
@numba_njit
def
shape
(
x
):
return
np
.
asarray
(
np
.
shape
(
x
))
return
shape
@numba_funcify.register
(
Shape_i
)
def
numba_funcify_Shape_i
(
op
,
**
kwargs
):
i
=
op
.
i
@numba_njit
def
shape_i
(
x
):
return
np
.
asarray
(
np
.
shape
(
x
)[
i
])
return
shape_i
@numba_funcify.register
(
SortOp
)
@numba_funcify.register
(
SortOp
)
def
numba_funcify_SortOp
(
op
,
node
,
**
kwargs
):
def
numba_funcify_SortOp
(
op
,
node
,
**
kwargs
):
@numba_njit
@numba_njit
...
@@ -423,54 +398,6 @@ def direct_cast(typingctx, val, typ):
...
@@ -423,54 +398,6 @@ def direct_cast(typingctx, val, typ):
return
sig
,
codegen
return
sig
,
codegen
@numba_funcify.register
(
Reshape
)
def
numba_funcify_Reshape
(
op
,
**
kwargs
):
ndim
=
op
.
ndim
if
ndim
==
0
:
@numba_njit
def
reshape
(
x
,
shape
):
return
np
.
asarray
(
x
.
item
())
else
:
@numba_njit
def
reshape
(
x
,
shape
):
# TODO: Use this until https://github.com/numba/numba/issues/7353 is closed.
return
np
.
reshape
(
np
.
ascontiguousarray
(
np
.
asarray
(
x
)),
numba_ndarray
.
to_fixed_tuple
(
shape
,
ndim
),
)
return
reshape
@numba_funcify.register
(
SpecifyShape
)
def
numba_funcify_SpecifyShape
(
op
,
node
,
**
kwargs
):
shape_inputs
=
node
.
inputs
[
1
:]
shape_input_names
=
[
"shape_"
+
str
(
i
)
for
i
in
range
(
len
(
shape_inputs
))]
func_conditions
=
[
f
"assert x.shape[{i}] == {shape_input_names}"
for
i
,
(
shape_input
,
shape_input_names
)
in
enumerate
(
zip
(
shape_inputs
,
shape_input_names
,
strict
=
True
)
)
if
shape_input
is
not
NoneConst
]
func
=
dedent
(
f
"""
def specify_shape(x, {create_arg_string(shape_input_names)}):
{"; ".join(func_conditions)}
return x
"""
)
specify_shape
=
compile_function_src
(
func
,
"specify_shape"
,
globals
())
return
numba_njit
(
specify_shape
)
def
int_to_float_fn
(
inputs
,
out_dtype
):
def
int_to_float_fn
(
inputs
,
out_dtype
):
"""Create a Numba function that converts integer and boolean ``ndarray``s to floats."""
"""Create a Numba function that converts integer and boolean ``ndarray``s to floats."""
...
...
pytensor/link/numba/dispatch/shape.py
0 → 100644
浏览文件 @
c90ee4ba
from
textwrap
import
dedent
import
numpy
as
np
from
numba.np.unsafe
import
ndarray
as
numba_ndarray
from
pytensor.link.numba.dispatch
import
numba_funcify
from
pytensor.link.numba.dispatch.basic
import
create_arg_string
,
numba_njit
from
pytensor.link.utils
import
compile_function_src
from
pytensor.tensor
import
NoneConst
from
pytensor.tensor.shape
import
Reshape
,
Shape
,
Shape_i
,
SpecifyShape
@numba_funcify.register
(
Shape
)
def
numba_funcify_Shape
(
op
,
**
kwargs
):
@numba_njit
def
shape
(
x
):
return
np
.
asarray
(
np
.
shape
(
x
))
return
shape
@numba_funcify.register
(
Shape_i
)
def
numba_funcify_Shape_i
(
op
,
**
kwargs
):
i
=
op
.
i
@numba_njit
def
shape_i
(
x
):
return
np
.
asarray
(
np
.
shape
(
x
)[
i
])
return
shape_i
@numba_funcify.register
(
SpecifyShape
)
def
numba_funcify_SpecifyShape
(
op
,
node
,
**
kwargs
):
shape_inputs
=
node
.
inputs
[
1
:]
shape_input_names
=
[
"shape_"
+
str
(
i
)
for
i
in
range
(
len
(
shape_inputs
))]
func_conditions
=
[
f
"assert x.shape[{i}] == {shape_input_names}"
for
i
,
(
shape_input
,
shape_input_names
)
in
enumerate
(
zip
(
shape_inputs
,
shape_input_names
,
strict
=
True
)
)
if
shape_input
is
not
NoneConst
]
func
=
dedent
(
f
"""
def specify_shape(x, {create_arg_string(shape_input_names)}):
{"; ".join(func_conditions)}
return x
"""
)
specify_shape
=
compile_function_src
(
func
,
"specify_shape"
,
globals
())
return
numba_njit
(
specify_shape
)
@numba_funcify.register
(
Reshape
)
def
numba_funcify_Reshape
(
op
,
**
kwargs
):
ndim
=
op
.
ndim
if
ndim
==
0
:
@numba_njit
def
reshape
(
x
,
shape
):
return
np
.
asarray
(
x
.
item
())
else
:
@numba_njit
def
reshape
(
x
,
shape
):
# TODO: Use this until https://github.com/numba/numba/issues/7353 is closed.
return
np
.
reshape
(
np
.
ascontiguousarray
(
np
.
asarray
(
x
)),
numba_ndarray
.
to_fixed_tuple
(
shape
,
ndim
),
)
return
reshape
tests/link/numba/test_basic.py
浏览文件 @
c90ee4ba
...
@@ -31,7 +31,6 @@ from pytensor.raise_op import assert_op
...
@@ -31,7 +31,6 @@ from pytensor.raise_op import assert_op
from
pytensor.scalar.basic
import
ScalarOp
,
as_scalar
from
pytensor.scalar.basic
import
ScalarOp
,
as_scalar
from
pytensor.tensor
import
blas
,
tensor
from
pytensor.tensor
import
blas
,
tensor
from
pytensor.tensor.elemwise
import
Elemwise
from
pytensor.tensor.elemwise
import
Elemwise
from
pytensor.tensor.shape
import
Reshape
,
Shape
,
Shape_i
,
SpecifyShape
from
pytensor.tensor.sort
import
ArgSortOp
,
SortOp
from
pytensor.tensor.sort
import
ArgSortOp
,
SortOp
...
@@ -332,22 +331,6 @@ def test_create_numba_signature(v, expected, force_scalar):
...
@@ -332,22 +331,6 @@ def test_create_numba_signature(v, expected, force_scalar):
assert
res
==
expected
assert
res
==
expected
@pytest.mark.parametrize
(
"x, i"
,
[
(
np
.
zeros
((
20
,
3
)),
1
),
],
)
def
test_Shape
(
x
,
i
):
g
=
Shape
()(
pt
.
as_tensor_variable
(
x
))
compare_numba_and_py
([],
[
g
],
[])
g
=
Shape_i
(
i
)(
pt
.
as_tensor_variable
(
x
))
compare_numba_and_py
([],
[
g
],
[])
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"x"
,
"x"
,
[
[
...
@@ -412,81 +395,6 @@ def test_ArgSort(x, axis, kind, exc):
...
@@ -412,81 +395,6 @@ def test_ArgSort(x, axis, kind, exc):
compare_numba_and_py
([],
[
g
],
[])
compare_numba_and_py
([],
[
g
],
[])
@pytest.mark.parametrize
(
"v, shape, ndim"
,
[
((
pt
.
vector
(),
np
.
array
([
4
],
dtype
=
config
.
floatX
)),
((),
None
),
0
),
((
pt
.
vector
(),
np
.
arange
(
4
,
dtype
=
config
.
floatX
)),
((
2
,
2
),
None
),
2
),
(
(
pt
.
vector
(),
np
.
arange
(
4
,
dtype
=
config
.
floatX
)),
(
pt
.
lvector
(),
np
.
array
([
2
,
2
],
dtype
=
"int64"
)),
2
,
),
],
)
def
test_Reshape
(
v
,
shape
,
ndim
):
v
,
v_test_value
=
v
shape
,
shape_test_value
=
shape
g
=
Reshape
(
ndim
)(
v
,
shape
)
inputs
=
[
v
]
if
not
isinstance
(
shape
,
Variable
)
else
[
v
,
shape
]
test_values
=
(
[
v_test_value
]
if
not
isinstance
(
shape
,
Variable
)
else
[
v_test_value
,
shape_test_value
]
)
compare_numba_and_py
(
inputs
,
[
g
],
test_values
,
)
def
test_Reshape_scalar
():
v
=
pt
.
vector
()
v_test_value
=
np
.
array
([
1.0
],
dtype
=
config
.
floatX
)
g
=
Reshape
(
1
)(
v
[
0
],
(
1
,))
compare_numba_and_py
(
[
v
],
g
,
[
v_test_value
],
)
@pytest.mark.parametrize
(
"v, shape, fails"
,
[
(
(
pt
.
matrix
(),
np
.
array
([[
1.0
]],
dtype
=
config
.
floatX
)),
(
1
,
1
),
False
,
),
(
(
pt
.
matrix
(),
np
.
array
([[
1.0
,
2.0
]],
dtype
=
config
.
floatX
)),
(
1
,
1
),
True
,
),
(
(
pt
.
matrix
(),
np
.
array
([[
1.0
,
2.0
]],
dtype
=
config
.
floatX
)),
(
1
,
None
),
False
,
),
],
)
def
test_SpecifyShape
(
v
,
shape
,
fails
):
v
,
v_test_value
=
v
g
=
SpecifyShape
()(
v
,
*
shape
)
cm
=
contextlib
.
suppress
()
if
not
fails
else
pytest
.
raises
(
AssertionError
)
with
cm
:
compare_numba_and_py
(
[
v
],
[
g
],
[
v_test_value
],
)
def
test_ViewOp
():
def
test_ViewOp
():
v
=
pt
.
vector
()
v
=
pt
.
vector
()
v_test_value
=
np
.
arange
(
4
,
dtype
=
config
.
floatX
)
v_test_value
=
np
.
arange
(
4
,
dtype
=
config
.
floatX
)
...
...
tests/link/numba/test_shape.py
0 → 100644
浏览文件 @
c90ee4ba
import
contextlib
import
numpy
as
np
import
pytest
from
pytensor
import
Variable
,
config
from
pytensor
import
tensor
as
pt
from
pytensor.tensor.shape
import
Reshape
,
Shape
,
Shape_i
,
SpecifyShape
from
tests.link.numba.test_basic
import
compare_numba_and_py
@pytest.mark.parametrize
(
"x, i"
,
[
(
np
.
zeros
((
20
,
3
)),
1
),
],
)
def
test_Shape
(
x
,
i
):
g
=
Shape
()(
pt
.
as_tensor_variable
(
x
))
compare_numba_and_py
([],
[
g
],
[])
g
=
Shape_i
(
i
)(
pt
.
as_tensor_variable
(
x
))
compare_numba_and_py
([],
[
g
],
[])
@pytest.mark.parametrize
(
"v, shape, ndim"
,
[
((
pt
.
vector
(),
np
.
array
([
4
],
dtype
=
config
.
floatX
)),
((),
None
),
0
),
((
pt
.
vector
(),
np
.
arange
(
4
,
dtype
=
config
.
floatX
)),
((
2
,
2
),
None
),
2
),
(
(
pt
.
vector
(),
np
.
arange
(
4
,
dtype
=
config
.
floatX
)),
(
pt
.
lvector
(),
np
.
array
([
2
,
2
],
dtype
=
"int64"
)),
2
,
),
],
)
def
test_Reshape
(
v
,
shape
,
ndim
):
v
,
v_test_value
=
v
shape
,
shape_test_value
=
shape
g
=
Reshape
(
ndim
)(
v
,
shape
)
inputs
=
[
v
]
if
not
isinstance
(
shape
,
Variable
)
else
[
v
,
shape
]
test_values
=
(
[
v_test_value
]
if
not
isinstance
(
shape
,
Variable
)
else
[
v_test_value
,
shape_test_value
]
)
compare_numba_and_py
(
inputs
,
[
g
],
test_values
,
)
def
test_Reshape_scalar
():
v
=
pt
.
vector
()
v_test_value
=
np
.
array
([
1.0
],
dtype
=
config
.
floatX
)
g
=
Reshape
(
1
)(
v
[
0
],
(
1
,))
compare_numba_and_py
(
[
v
],
g
,
[
v_test_value
],
)
@pytest.mark.parametrize
(
"v, shape, fails"
,
[
(
(
pt
.
matrix
(),
np
.
array
([[
1.0
]],
dtype
=
config
.
floatX
)),
(
1
,
1
),
False
,
),
(
(
pt
.
matrix
(),
np
.
array
([[
1.0
,
2.0
]],
dtype
=
config
.
floatX
)),
(
1
,
1
),
True
,
),
(
(
pt
.
matrix
(),
np
.
array
([[
1.0
,
2.0
]],
dtype
=
config
.
floatX
)),
(
1
,
None
),
False
,
),
],
)
def
test_SpecifyShape
(
v
,
shape
,
fails
):
v
,
v_test_value
=
v
g
=
SpecifyShape
()(
v
,
*
shape
)
cm
=
contextlib
.
suppress
()
if
not
fails
else
pytest
.
raises
(
AssertionError
)
with
cm
:
compare_numba_and_py
(
[
v
],
[
g
],
[
v_test_value
],
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论