Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5229feba
提交
5229feba
authored
5月 06, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
5月 19, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement C code for ExtractDiagonal and ARange
Set view flag of ExtractDiagonal to True and respect by default
上级
d9a8471b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
89 行增加
和
35 行删除
+89
-35
basic.py
pytensor/tensor/basic.py
+89
-35
没有找到文件。
pytensor/tensor/basic.py
浏览文件 @
5229feba
...
@@ -3207,13 +3207,14 @@ def tile(
...
@@ -3207,13 +3207,14 @@ def tile(
return
A_replicated
.
reshape
(
tiled_shape
)
return
A_replicated
.
reshape
(
tiled_shape
)
class
ARange
(
Op
):
class
ARange
(
C
Op
):
"""Create an array containing evenly spaced values within a given interval.
"""Create an array containing evenly spaced values within a given interval.
Parameters and behaviour are the same as numpy.arange().
Parameters and behaviour are the same as numpy.arange().
"""
"""
# TODO: Arange should work with scalars as inputs, not arrays
__props__
=
(
"dtype"
,)
__props__
=
(
"dtype"
,)
def
__init__
(
self
,
dtype
):
def
__init__
(
self
,
dtype
):
...
@@ -3293,13 +3294,30 @@ class ARange(Op):
...
@@ -3293,13 +3294,30 @@ class ARange(Op):
)
)
]
]
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
start
,
stop
,
step
=
inp
start
,
stop
,
step
=
inputs
(
out
,)
=
out_
output_storage
[
0
][
0
]
=
np
.
arange
(
start
=
start
.
item
()
start
.
item
(),
stop
.
item
(),
step
.
item
(),
dtype
=
self
.
dtype
stop
=
stop
.
item
()
)
step
=
step
.
item
()
out
[
0
]
=
np
.
arange
(
start
,
stop
,
step
,
dtype
=
self
.
dtype
)
def
c_code
(
self
,
node
,
nodename
,
input_names
,
output_names
,
sub
):
[
start_name
,
stop_name
,
step_name
]
=
input_names
[
out_name
]
=
output_names
typenum
=
np
.
dtype
(
self
.
dtype
)
.
num
return
f
"""
double start = ((dtype_{start_name}*)PyArray_DATA({start_name}))[0];
double stop = ((dtype_{stop_name}*)PyArray_DATA({stop_name}))[0];
double step = ((dtype_{step_name}*)PyArray_DATA({step_name}))[0];
//printf("start:
%
f, stop:
%
f, step:
%
f
\\
n", start, stop, step);
Py_XDECREF({out_name});
{out_name} = (PyArrayObject*) PyArray_Arange(start, stop, step, {typenum});
if (!{out_name}) {{
{sub["fail"]}
}}
"""
def
c_code_cache_version
(
self
):
return
(
0
,)
def
connection_pattern
(
self
,
node
):
def
connection_pattern
(
self
,
node
):
return
[[
True
],
[
False
],
[
True
]]
return
[[
True
],
[
False
],
[
True
]]
...
@@ -3685,8 +3703,7 @@ def inverse_permutation(perm):
...
@@ -3685,8 +3703,7 @@ def inverse_permutation(perm):
)
)
# TODO: optimization to insert ExtractDiag with view=True
class
ExtractDiag
(
COp
):
class
ExtractDiag
(
Op
):
"""
"""
Return specified diagonals.
Return specified diagonals.
...
@@ -3742,7 +3759,7 @@ class ExtractDiag(Op):
...
@@ -3742,7 +3759,7 @@ class ExtractDiag(Op):
__props__
=
(
"offset"
,
"axis1"
,
"axis2"
,
"view"
)
__props__
=
(
"offset"
,
"axis1"
,
"axis2"
,
"view"
)
def
__init__
(
self
,
offset
=
0
,
axis1
=
0
,
axis2
=
1
,
view
=
Fals
e
):
def
__init__
(
self
,
offset
=
0
,
axis1
=
0
,
axis2
=
1
,
view
=
Tru
e
):
self
.
view
=
view
self
.
view
=
view
if
self
.
view
:
if
self
.
view
:
self
.
view_map
=
{
0
:
[
0
]}
self
.
view_map
=
{
0
:
[
0
]}
...
@@ -3765,24 +3782,74 @@ class ExtractDiag(Op):
...
@@ -3765,24 +3782,74 @@ class ExtractDiag(Op):
if
x
.
ndim
<
2
:
if
x
.
ndim
<
2
:
raise
ValueError
(
"ExtractDiag needs an input with 2 or more dimensions"
,
x
)
raise
ValueError
(
"ExtractDiag needs an input with 2 or more dimensions"
,
x
)
out_shape
=
[
if
(
dim1
:
=
x
.
type
.
shape
[
self
.
axis1
])
is
not
None
and
(
st_dim
dim2
:
=
x
.
type
.
shape
[
self
.
axis2
]
for
i
,
st_dim
in
enumerate
(
x
.
type
.
shape
)
)
is
not
None
:
if
i
not
in
(
self
.
axis1
,
self
.
axis2
)
offset
=
self
.
offset
]
+
[
None
]
if
offset
>
0
:
diag_size
=
int
(
np
.
clip
(
dim2
-
offset
,
0
,
dim1
))
elif
offset
<
0
:
diag_size
=
int
(
np
.
clip
(
dim1
+
offset
,
0
,
dim2
))
else
:
diag_size
=
int
(
np
.
minimum
(
dim1
,
dim2
))
else
:
diag_size
=
None
out_shape
=
(
*
(
dim
for
i
,
dim
in
enumerate
(
x
.
type
.
shape
)
if
i
not
in
(
self
.
axis1
,
self
.
axis2
)
),
diag_size
,
)
return
Apply
(
return
Apply
(
self
,
self
,
[
x
],
[
x
],
[
x
.
type
.
clone
(
dtype
=
x
.
dtype
,
shape
=
tuple
(
out_shape
)
)()],
[
x
.
type
.
clone
(
dtype
=
x
.
dtype
,
shape
=
out_shape
)()],
)
)
def
perform
(
self
,
node
,
inputs
,
output
s
):
def
perform
(
self
,
node
,
inputs
,
output
_storage
):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
out
=
x
.
diagonal
(
self
.
offset
,
self
.
axis1
,
self
.
axis2
)
z
[
0
]
=
x
.
diagonal
(
self
.
offset
,
self
.
axis1
,
self
.
axis2
)
if
self
.
view
:
if
not
self
.
view
:
try
:
z
[
0
]
=
z
[
0
]
.
copy
()
out
.
flags
.
writeable
=
True
except
ValueError
:
# We can't make this array writable
out
=
out
.
copy
()
else
:
out
=
out
.
copy
()
output_storage
[
0
][
0
]
=
out
def
c_code
(
self
,
node
,
nodename
,
input_names
,
output_names
,
sub
):
[
x_name
]
=
input_names
[
out_name
]
=
output_names
return
f
"""
Py_XDECREF({out_name});
{out_name} = (PyArrayObject*) PyArray_Diagonal({x_name}, {self.offset}, {self.axis1}, {self.axis2});
if (!{out_name}) {{
{sub["fail"]} // Error already set by Numpy
}}
if ({int(self.view)} && PyArray_ISWRITEABLE({x_name})) {{
// Make output writeable if input was writeable
PyArray_ENABLEFLAGS({out_name}, NPY_ARRAY_WRITEABLE);
}} else {{
// Make a copy
PyArrayObject *{out_name}_copy = (PyArrayObject*) PyArray_Copy({out_name});
Py_DECREF({out_name});
if (!{out_name}_copy) {{
{sub['fail']}; // Error already set by Numpy
}}
{out_name} = {out_name}_copy;
}}
"""
def
c_code_cache_version
(
self
):
return
(
0
,)
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
# Avoid circular import
# Avoid circular import
...
@@ -3829,19 +3896,6 @@ class ExtractDiag(Op):
...
@@ -3829,19 +3896,6 @@ class ExtractDiag(Op):
out_shape
.
append
(
diag_size
)
out_shape
.
append
(
diag_size
)
return
[
tuple
(
out_shape
)]
return
[
tuple
(
out_shape
)]
def
__setstate__
(
self
,
state
):
self
.
__dict__
.
update
(
state
)
if
self
.
view
:
self
.
view_map
=
{
0
:
[
0
]}
if
"offset"
not
in
state
:
self
.
offset
=
0
if
"axis1"
not
in
state
:
self
.
axis1
=
0
if
"axis2"
not
in
state
:
self
.
axis2
=
1
def
extract_diag
(
x
):
def
extract_diag
(
x
):
warnings
.
warn
(
warnings
.
warn
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论