Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3bd8ed9f
提交
3bd8ed9f
authored
2月 17, 2026
作者:
Tomas Capretto
提交者:
Ricardo Vieira
2月 18, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement specific make_node for sparse HStack/VStack with early static shape validation
上级
f0603aac
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
70 行增加
和
14 行删除
+70
-14
basic.py
pytensor/sparse/basic.py
+46
-10
test_basic.py
tests/link/numba/sparse/test_basic.py
+4
-4
test_basic.py
tests/sparse/test_basic.py
+20
-0
没有找到文件。
pytensor/sparse/basic.py
浏览文件 @
3bd8ed9f
...
@@ -1636,23 +1636,35 @@ class Stack(Op):
...
@@ -1636,23 +1636,35 @@ class Stack(Op):
raise
ValueError
(
"The output dtype must be specified."
)
raise
ValueError
(
"The output dtype must be specified."
)
self
.
dtype
=
dtype
self
.
dtype
=
dtype
def
make_node
(
self
,
*
mat
):
def
__str__
(
self
):
if
not
mat
:
return
f
"{self.__class__.__name__}({self.format},{self.dtype})"
class
HStack
(
Stack
):
def
make_node
(
self
,
*
blocks
):
if
not
blocks
:
raise
ValueError
(
"Cannot join an empty list of sparses."
)
raise
ValueError
(
"Cannot join an empty list of sparses."
)
var
=
[
as_sparse_variable
(
x
)
for
x
in
mat
]
input_blocks
=
[
as_sparse_variable
(
block
)
for
block
in
blocks
]
for
x
in
var
:
for
x
in
input_blocks
:
assert
x
.
format
in
(
"csr"
,
"csc"
)
assert
x
.
format
in
(
"csr"
,
"csc"
)
# Known rows numbers must be the same for all matrices.
static_n_rows
=
{
x
.
type
.
shape
[
0
]
for
x
in
input_blocks
if
x
.
type
.
shape
[
0
]
is
not
None
}
if
len
(
static_n_rows
)
>
1
:
raise
ValueError
(
"All matrices must have the same number of rows; "
f
"got row counts: {static_n_rows}."
)
return
Apply
(
return
Apply
(
self
,
var
,
[
SparseTensorType
(
dtype
=
self
.
dtype
,
format
=
self
.
format
)()]
self
,
input_blocks
,
[
SparseTensorType
(
dtype
=
self
.
dtype
,
format
=
self
.
format
)()],
)
)
def
__str__
(
self
):
return
f
"{self.__class__.__name__}({self.format},{self.dtype})"
class
HStack
(
Stack
):
def
perform
(
self
,
node
,
block
,
outputs
):
def
perform
(
self
,
node
,
block
,
outputs
):
(
out
,)
=
outputs
(
out
,)
=
outputs
for
b
in
block
:
for
b
in
block
:
...
@@ -1726,6 +1738,30 @@ def hstack(blocks, format=None, dtype=None):
...
@@ -1726,6 +1738,30 @@ def hstack(blocks, format=None, dtype=None):
class
VStack
(
Stack
):
class
VStack
(
Stack
):
def
make_node
(
self
,
*
blocks
):
if
not
blocks
:
raise
ValueError
(
"Cannot join an empty list of sparses."
)
input_blocks
=
[
as_sparse_variable
(
block
)
for
block
in
blocks
]
for
x
in
input_blocks
:
assert
x
.
format
in
(
"csr"
,
"csc"
)
# Known column numbers must be the same for all matrices.
static_n_cols
=
{
x
.
type
.
shape
[
1
]
for
x
in
input_blocks
if
x
.
type
.
shape
[
1
]
is
not
None
}
if
len
(
static_n_cols
)
>
1
:
raise
ValueError
(
"All matrices must have the same number of columns; "
f
"got column counts: {static_n_cols}."
)
return
Apply
(
self
,
input_blocks
,
[
SparseTensorType
(
dtype
=
self
.
dtype
,
format
=
self
.
format
)()],
)
def
perform
(
self
,
node
,
block
,
outputs
):
def
perform
(
self
,
node
,
block
,
outputs
):
(
out
,)
=
outputs
(
out
,)
=
outputs
for
b
in
block
:
for
b
in
block
:
...
...
tests/link/numba/sparse/test_basic.py
浏览文件 @
3bd8ed9f
...
@@ -409,8 +409,8 @@ def test_sparse_vstack(output_format, input_formats):
...
@@ -409,8 +409,8 @@ def test_sparse_vstack(output_format, input_formats):
def
test_sparse_hstack_mismatched_rows_raises
():
def
test_sparse_hstack_mismatched_rows_raises
():
x
=
ps
.
matrix
(
name
=
"x"
,
shape
=
(
3
,
5
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
x
=
ps
.
matrix
(
name
=
"x"
,
shape
=
(
None
,
5
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
y
=
ps
.
matrix
(
name
=
"y"
,
shape
=
(
4
,
7
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
y
=
ps
.
matrix
(
name
=
"y"
,
shape
=
(
None
,
7
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
z
=
ps
.
hstack
([
x
,
y
],
format
=
"csr"
,
dtype
=
config
.
floatX
)
z
=
ps
.
hstack
([
x
,
y
],
format
=
"csr"
,
dtype
=
config
.
floatX
)
fn
=
function
([
x
,
y
],
z
,
mode
=
"NUMBA"
)
fn
=
function
([
x
,
y
],
z
,
mode
=
"NUMBA"
)
...
@@ -422,8 +422,8 @@ def test_sparse_hstack_mismatched_rows_raises():
...
@@ -422,8 +422,8 @@ def test_sparse_hstack_mismatched_rows_raises():
def
test_sparse_vstack_mismatched_cols_raises
():
def
test_sparse_vstack_mismatched_cols_raises
():
x
=
ps
.
matrix
(
name
=
"x"
,
shape
=
(
10
,
3
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
x
=
ps
.
matrix
(
name
=
"x"
,
shape
=
(
10
,
None
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
y
=
ps
.
matrix
(
name
=
"y"
,
shape
=
(
13
,
4
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
y
=
ps
.
matrix
(
name
=
"y"
,
shape
=
(
13
,
None
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
z
=
ps
.
vstack
([
x
,
y
],
format
=
"csr"
,
dtype
=
config
.
floatX
)
z
=
ps
.
vstack
([
x
,
y
],
format
=
"csr"
,
dtype
=
config
.
floatX
)
fn
=
function
([
x
,
y
],
z
,
mode
=
"NUMBA"
)
fn
=
function
([
x
,
y
],
z
,
mode
=
"NUMBA"
)
...
...
tests/sparse/test_basic.py
浏览文件 @
3bd8ed9f
...
@@ -1583,3 +1583,23 @@ def test_hstack_vstack():
...
@@ -1583,3 +1583,23 @@ def test_hstack_vstack():
stacked_blocks
=
stack_function
(
blocks
,
dtype
=
to_dtype
)
stacked_blocks
=
stack_function
(
blocks
,
dtype
=
to_dtype
)
expected_dtype
=
get_expected_dtype
(
blocks
,
to_dtype
)
expected_dtype
=
get_expected_dtype
(
blocks
,
to_dtype
)
assert
stacked_blocks
.
dtype
==
expected_dtype
assert
stacked_blocks
.
dtype
==
expected_dtype
def
test_sparse_hstack_mismatched_static_rows_raises
():
x
=
sparse
.
matrix
(
name
=
"x"
,
shape
=
(
3
,
5
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
y
=
sparse
.
matrix
(
name
=
"y"
,
shape
=
(
4
,
7
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
with
pytest
.
raises
(
ValueError
,
match
=
"All matrices must have the same number of rows"
):
sparse
.
hstack
([
x
,
y
],
format
=
"csr"
,
dtype
=
config
.
floatX
)
def
test_sparse_vstack_mismatched_static_cols_raises
():
x
=
sparse
.
matrix
(
name
=
"x"
,
shape
=
(
10
,
3
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
y
=
sparse
.
matrix
(
name
=
"y"
,
shape
=
(
13
,
4
),
format
=
"csr"
,
dtype
=
config
.
floatX
)
with
pytest
.
raises
(
ValueError
,
match
=
"All matrices must have the same number of columns"
):
sparse
.
vstack
([
x
,
y
],
format
=
"csr"
,
dtype
=
config
.
floatX
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论