Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0b94be01
提交
0b94be01
authored
1月 23, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
2月 12, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reduce overhead of Scalar python implementation
上级
0b07727b
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
16 行增加
和
17 行删除
+16
-17
basic.py
pytensor/scalar/basic.py
+16
-17
没有找到文件。
pytensor/scalar/basic.py
浏览文件 @
0b94be01
...
@@ -36,7 +36,6 @@ from pytensor.printing import pprint
...
@@ -36,7 +36,6 @@ from pytensor.printing import pprint
from
pytensor.utils
import
(
from
pytensor.utils
import
(
apply_across_args
,
apply_across_args
,
difference
,
difference
,
from_return_values
,
to_return_values
,
to_return_values
,
)
)
...
@@ -1081,6 +1080,16 @@ def real_out(type):
...
@@ -1081,6 +1080,16 @@ def real_out(type):
return
(
type
,)
return
(
type
,)
def
_cast_to_promised_scalar_dtype
(
x
,
dtype
):
try
:
return
x
.
astype
(
dtype
)
except
AttributeError
:
if
dtype
==
"bool"
:
return
np
.
bool_
(
x
)
else
:
return
getattr
(
np
,
dtype
)(
x
)
class
ScalarOp
(
COp
):
class
ScalarOp
(
COp
):
nin
=
-
1
nin
=
-
1
nout
=
1
nout
=
1
...
@@ -1134,28 +1143,18 @@ class ScalarOp(COp):
...
@@ -1134,28 +1143,18 @@ class ScalarOp(COp):
else
:
else
:
raise
NotImplementedError
(
f
"Cannot calculate the output types for {self}"
)
raise
NotImplementedError
(
f
"Cannot calculate the output types for {self}"
)
@staticmethod
def
_cast_scalar
(
x
,
dtype
):
if
hasattr
(
x
,
"astype"
):
return
x
.
astype
(
dtype
)
elif
dtype
==
"bool"
:
return
np
.
bool_
(
x
)
else
:
return
getattr
(
np
,
dtype
)(
x
)
def
perform
(
self
,
node
,
inputs
,
output_storage
):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
if
self
.
nout
==
1
:
if
self
.
nout
==
1
:
dtype
=
node
.
outputs
[
0
]
.
dtype
output_storage
[
0
][
0
]
=
_cast_to_promised_scalar_dtype
(
output_storage
[
0
][
0
]
=
self
.
_cast_scalar
(
self
.
impl
(
*
inputs
),
dtype
)
self
.
impl
(
*
inputs
),
node
.
outputs
[
0
]
.
dtype
,
)
else
:
else
:
variables
=
from_return_values
(
self
.
impl
(
*
inputs
))
assert
len
(
variables
)
==
len
(
output_storage
)
# strict=False because we are in a hot loop
# strict=False because we are in a hot loop
for
out
,
storage
,
variable
in
zip
(
for
out
,
storage
,
variable
in
zip
(
node
.
outputs
,
output_storage
,
variables
,
strict
=
False
node
.
outputs
,
output_storage
,
self
.
impl
(
*
inputs
)
,
strict
=
False
):
):
dtype
=
out
.
dtype
storage
[
0
]
=
_cast_to_promised_scalar_dtype
(
variable
,
out
.
dtype
)
storage
[
0
]
=
self
.
_cast_scalar
(
variable
,
dtype
)
def
impl
(
self
,
*
inputs
):
def
impl
(
self
,
*
inputs
):
raise
MethodNotDefined
(
"impl"
,
type
(
self
),
self
.
__class__
.
__name__
)
raise
MethodNotDefined
(
"impl"
,
type
(
self
),
self
.
__class__
.
__name__
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论