Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9da643e8
提交
9da643e8
authored
10月 09, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
10月 16, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move slice dispatcher functionality to subtensor.py
上级
131982de
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
82 行增加
和
78 行删除
+82
-78
basic.py
pytensor/link/numba/dispatch/basic.py
+2
-77
subtensor.py
pytensor/link/numba/dispatch/subtensor.py
+80
-1
没有找到文件。
pytensor/link/numba/dispatch/basic.py
浏览文件 @
9da643e8
import
operator
import
sys
import
warnings
import
warnings
from
copy
import
copy
from
copy
import
copy
from
functools
import
singledispatch
from
functools
import
singledispatch
...
@@ -8,11 +6,10 @@ from textwrap import dedent
...
@@ -8,11 +6,10 @@ from textwrap import dedent
import
numba
import
numba
import
numba.np.unsafe.ndarray
as
numba_ndarray
import
numba.np.unsafe.ndarray
as
numba_ndarray
import
numpy
as
np
import
numpy
as
np
from
llvmlite
import
ir
from
numba
import
types
from
numba
import
types
from
numba.core.errors
import
NumbaWarning
,
TypingError
from
numba.core.errors
import
NumbaWarning
,
TypingError
from
numba.cpython.unsafe.tuple
import
tuple_setitem
# noqa: F401
from
numba.cpython.unsafe.tuple
import
tuple_setitem
# noqa: F401
from
numba.extending
import
box
,
overload
from
numba.extending
import
overload
from
pytensor
import
In
,
config
from
pytensor
import
In
,
config
from
pytensor.compile
import
NUMBA
from
pytensor.compile
import
NUMBA
...
@@ -36,7 +33,7 @@ from pytensor.tensor.math import Dot
...
@@ -36,7 +33,7 @@ from pytensor.tensor.math import Dot
from
pytensor.tensor.shape
import
Reshape
,
Shape
,
Shape_i
,
SpecifyShape
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
MakeSlice
,
NoneConst
from
pytensor.tensor.type_other
import
NoneConst
def
numba_njit
(
*
args
,
fastmath
=
None
,
**
kwargs
):
def
numba_njit
(
*
args
,
fastmath
=
None
,
**
kwargs
):
...
@@ -149,69 +146,6 @@ def create_numba_signature(
...
@@ -149,69 +146,6 @@ def create_numba_signature(
return
numba
.
types
.
void
(
*
input_types
)
return
numba
.
types
.
void
(
*
input_types
)
def
slice_new
(
self
,
start
,
stop
,
step
):
fnty
=
ir
.
FunctionType
(
self
.
pyobj
,
[
self
.
pyobj
,
self
.
pyobj
,
self
.
pyobj
])
fn
=
self
.
_get_function
(
fnty
,
name
=
"PySlice_New"
)
return
self
.
builder
.
call
(
fn
,
[
start
,
stop
,
step
])
def
enable_slice_boxing
():
"""Enable boxing for Numba's native ``slice``s.
TODO: this can be removed when https://github.com/numba/numba/pull/6939 is
merged and a release is made.
"""
@box
(
types
.
SliceType
)
def
box_slice
(
typ
,
val
,
c
):
"""Implement boxing for ``slice`` objects in Numba.
This makes it possible to return an Numba's internal representation of a
``slice`` object as a proper ``slice`` to Python.
"""
start
=
c
.
builder
.
extract_value
(
val
,
0
)
stop
=
c
.
builder
.
extract_value
(
val
,
1
)
none_val
=
ir
.
Constant
(
ir
.
IntType
(
64
),
sys
.
maxsize
)
start_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
start
,
none_val
)
start
=
c
.
builder
.
select
(
start_is_none
,
c
.
pyapi
.
get_null_object
(),
c
.
box
(
types
.
int64
,
start
),
)
stop_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
stop
,
none_val
)
stop
=
c
.
builder
.
select
(
stop_is_none
,
c
.
pyapi
.
get_null_object
(),
c
.
box
(
types
.
int64
,
stop
),
)
if
typ
.
has_step
:
step
=
c
.
builder
.
extract_value
(
val
,
2
)
step_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
step
,
none_val
)
step
=
c
.
builder
.
select
(
step_is_none
,
c
.
pyapi
.
get_null_object
(),
c
.
box
(
types
.
int64
,
step
),
)
else
:
step
=
c
.
pyapi
.
get_null_object
()
slice_val
=
slice_new
(
c
.
pyapi
,
start
,
stop
,
step
)
return
slice_val
@numba.extending.overload
(
operator
.
contains
)
def
in_seq_empty_tuple
(
x
,
y
):
if
isinstance
(
x
,
types
.
Tuple
)
and
not
x
.
types
:
return
lambda
x
,
y
:
False
enable_slice_boxing
()
def
to_scalar
(
x
):
def
to_scalar
(
x
):
return
np
.
asarray
(
x
)
.
item
()
return
np
.
asarray
(
x
)
.
item
()
...
@@ -388,15 +322,6 @@ def numba_funcify_DeepCopyOp(op, node, **kwargs):
...
@@ -388,15 +322,6 @@ def numba_funcify_DeepCopyOp(op, node, **kwargs):
return
deepcopyop
return
deepcopyop
@numba_funcify.register
(
MakeSlice
)
def
numba_funcify_MakeSlice
(
op
,
**
kwargs
):
@numba_njit
def
makeslice
(
*
x
):
return
slice
(
*
x
)
return
makeslice
@numba_funcify.register
(
Shape
)
@numba_funcify.register
(
Shape
)
def
numba_funcify_Shape
(
op
,
**
kwargs
):
def
numba_funcify_Shape
(
op
,
**
kwargs
):
@numba_njit
@numba_njit
...
...
pytensor/link/numba/dispatch/subtensor.py
浏览文件 @
9da643e8
import
operator
import
sys
import
numba
import
numpy
as
np
import
numpy
as
np
from
llvmlite
import
ir
from
numba
import
types
from
numba.core.pythonapi
import
box
from
pytensor.graph
import
Type
from
pytensor.graph
import
Type
from
pytensor.link.numba.dispatch
import
numba_funcify
from
pytensor.link.numba.dispatch
import
numba_funcify
...
@@ -14,7 +21,79 @@ from pytensor.tensor.subtensor import (
...
@@ -14,7 +21,79 @@ from pytensor.tensor.subtensor import (
IncSubtensor
,
IncSubtensor
,
Subtensor
,
Subtensor
,
)
)
from
pytensor.tensor.type_other
import
NoneTypeT
,
SliceType
from
pytensor.tensor.type_other
import
MakeSlice
,
NoneTypeT
,
SliceType
def
slice_new
(
self
,
start
,
stop
,
step
):
fnty
=
ir
.
FunctionType
(
self
.
pyobj
,
[
self
.
pyobj
,
self
.
pyobj
,
self
.
pyobj
])
fn
=
self
.
_get_function
(
fnty
,
name
=
"PySlice_New"
)
return
self
.
builder
.
call
(
fn
,
[
start
,
stop
,
step
])
def
enable_slice_boxing
():
"""Enable boxing for Numba's native ``slice``s.
TODO: this can be removed when https://github.com/numba/numba/pull/6939 is
merged and a release is made.
"""
@box
(
types
.
SliceType
)
def
box_slice
(
typ
,
val
,
c
):
"""Implement boxing for ``slice`` objects in Numba.
This makes it possible to return an Numba's internal representation of a
``slice`` object as a proper ``slice`` to Python.
"""
start
=
c
.
builder
.
extract_value
(
val
,
0
)
stop
=
c
.
builder
.
extract_value
(
val
,
1
)
none_val
=
ir
.
Constant
(
ir
.
IntType
(
64
),
sys
.
maxsize
)
start_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
start
,
none_val
)
start
=
c
.
builder
.
select
(
start_is_none
,
c
.
pyapi
.
get_null_object
(),
c
.
box
(
types
.
int64
,
start
),
)
stop_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
stop
,
none_val
)
stop
=
c
.
builder
.
select
(
stop_is_none
,
c
.
pyapi
.
get_null_object
(),
c
.
box
(
types
.
int64
,
stop
),
)
if
typ
.
has_step
:
step
=
c
.
builder
.
extract_value
(
val
,
2
)
step_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
step
,
none_val
)
step
=
c
.
builder
.
select
(
step_is_none
,
c
.
pyapi
.
get_null_object
(),
c
.
box
(
types
.
int64
,
step
),
)
else
:
step
=
c
.
pyapi
.
get_null_object
()
slice_val
=
slice_new
(
c
.
pyapi
,
start
,
stop
,
step
)
return
slice_val
@numba.extending.overload
(
operator
.
contains
)
def
in_seq_empty_tuple
(
x
,
y
):
if
isinstance
(
x
,
types
.
Tuple
)
and
not
x
.
types
:
return
lambda
x
,
y
:
False
enable_slice_boxing
()
@numba_funcify.register
(
MakeSlice
)
def
numba_funcify_MakeSlice
(
op
,
**
kwargs
):
@numba_njit
def
makeslice
(
*
x
):
return
slice
(
*
x
)
return
makeslice
@numba_funcify.register
(
Subtensor
)
@numba_funcify.register
(
Subtensor
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论