Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8fd4ef91
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8fd4ef91
编写于
6月 02, 2023
作者:
傅
傅剑寒
提交者:
GitHub
6月 02, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add mixed bool and int index support for index_put (#54195)
上级
9a8e9417
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
110 addition
and
50 deletion
+110
-50
paddle/phi/kernels/funcs/index_put_utils.h
paddle/phi/kernels/funcs/index_put_utils.h
+28
-35
test/legacy_test/test_index_put_op.py
test/legacy_test/test_index_put_op.py
+82
-15
未找到文件。
paddle/phi/kernels/funcs/index_put_utils.h
浏览文件 @
8fd4ef91
...
...
@@ -74,57 +74,50 @@ std::vector<const phi::DenseTensor*> DealWithBoolIndices(
std
::
vector
<
phi
::
DenseTensor
>*
tmp_indices_v
)
{
std
::
vector
<
const
phi
::
DenseTensor
*>
res
(
indices_v
.
begin
(),
indices_v
.
end
());
bool
contains_bool_tensor
=
false
;
for
(
size_t
i
=
0
;
i
<
indices_v
.
size
();
++
i
)
{
if
(
indices_v
[
i
]
->
dtype
()
==
phi
::
DataType
::
BOOL
)
{
contains_bool_tensor
=
true
;
int
rank
=
indices_v
[
i
]
->
dims
().
size
();
PADDLE_ENFORCE_GE
(
rank
,
1UL
,
phi
::
errors
::
InvalidArgument
(
"the only bool tensor in indices should "
"have number of dimension at least 1"
));
phi
::
DenseTensor
nonzero_indices
(
phi
::
DataType
::
INT64
);
nonzero_indices
.
Resize
(
phi
::
make_ddim
({
-
1
,
rank
}));
NonZeroKernel
<
bool
,
Context
>
(
dev_ctx
,
*
indices_v
[
i
],
&
nonzero_indices
);
std
::
vector
<
phi
::
DenseTensor
*>
integer_indices
(
rank
,
nullptr
);
const
int
tmp_ix
=
tmp_indices_v
->
size
();
for
(
int
i
=
0
;
i
<
rank
;
++
i
)
{
tmp_indices_v
->
emplace_back
(
DenseTensor
(
phi
::
DataType
::
INT64
)
.
Resize
(
phi
::
make_ddim
({
nonzero_indices
.
dims
()[
0
]})));
}
for
(
int
i
=
0
;
i
<
rank
;
++
i
)
{
integer_indices
[
i
]
=
&
((
*
tmp_indices_v
)[
i
+
tmp_ix
]);
}
SplitWithNumKernel
<
int64_t
,
Context
>
(
dev_ctx
,
nonzero_indices
,
rank
,
1
,
integer_indices
);
}
else
if
((
indices_v
[
i
]
->
dtype
()
==
phi
::
DataType
::
INT64
)
||
(
indices_v
[
i
]
->
dtype
()
==
phi
::
DataType
::
INT32
))
{
PADDLE_ENFORCE_EQ
(
contains_bool_tensor
,
false
,
phi
::
errors
::
InvalidArgument
(
"indices contains bool tensor and int32/int64 tensor at the same "
"time"
));
tmp_indices_v
->
emplace_back
(
*
indices_v
[
i
]);
}
else
{
PADDLE_THROW
(
phi
::
errors
::
InvalidArgument
(
"data type of tensor in indices must be int32, int64 or bool"
));
}
}
if
(
contains_bool_tensor
)
{
if
(
indices_v
.
size
()
!=
1
)
{
PADDLE_THROW
(
phi
::
errors
::
InvalidArgument
(
"the size of indices must be 1 when it containts bool tensor"
));
}
int
rank
=
indices_v
[
0
]
->
dims
().
size
();
PADDLE_ENFORCE_GE
(
rank
,
1UL
,
phi
::
errors
::
InvalidArgument
(
"the only bool tensor in indices should "
"have number of dimension at least 1"
));
phi
::
DenseTensor
nonzero_indices
(
phi
::
DataType
::
INT64
);
nonzero_indices
.
Resize
(
phi
::
make_ddim
({
-
1
,
rank
}));
NonZeroKernel
<
bool
,
Context
>
(
dev_ctx
,
*
indices_v
[
0
],
&
nonzero_indices
);
std
::
vector
<
phi
::
DenseTensor
*>
integer_indices
(
rank
,
nullptr
);
for
(
int
i
=
0
;
i
<
rank
;
++
i
)
{
tmp_indices_v
->
emplace_back
(
DenseTensor
(
phi
::
DataType
::
INT64
)
.
Resize
(
phi
::
make_ddim
({
nonzero_indices
.
dims
()[
0
]})));
}
for
(
int
i
=
0
;
i
<
rank
;
++
i
)
{
integer_indices
[
i
]
=
&
((
*
tmp_indices_v
)[
i
]);
}
SplitWithNumKernel
<
int64_t
,
Context
>
(
dev_ctx
,
nonzero_indices
,
rank
,
1
,
integer_indices
);
std
::
vector
<
const
phi
::
DenseTensor
*>
res_tmp
(
integer_indices
.
size
(),
std
::
vector
<
const
phi
::
DenseTensor
*>
res_tmp
(
tmp_indices_v
->
size
(),
nullptr
);
for
(
int
i
=
0
;
i
<
rank
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
res_tmp
.
size
()
;
++
i
)
{
res_tmp
[
i
]
=
&
((
*
tmp_indices_v
)[
i
]);
}
res
.
swap
(
res_tmp
);
}
return
res
;
}
...
...
test/legacy_test/test_index_put_op.py
浏览文件 @
8fd4ef91
...
...
@@ -77,13 +77,26 @@ def gen_indices_np(x_shape, indices_shapes, index_type):
class
TestIndexPutAPIBase
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
mixed_indices
=
False
self
.
init_dtype_type
()
self
.
setPlace
()
self
.
x_np
=
np
.
random
.
random
(
self
.
x_shape
).
astype
(
self
.
dtype_np
)
self
.
value_np
=
np
.
random
.
random
(
self
.
value_shape
).
astype
(
self
.
dtype_np
)
self
.
indices_np
=
gen_indices_np
(
self
.
x_shape
,
self
.
indices_shapes
,
self
.
index_type_np
)
if
self
.
mixed_indices
:
tmp_indices_np1
=
gen_indices_np
(
self
.
x_shape
,
self
.
indices_shapes
,
self
.
index_type_np
)
tmp_indices_np2
=
gen_indices_np
(
self
.
x_shape
,
self
.
indices_shapes1
,
self
.
index_type_np1
)
self
.
indices_np
=
tuple
(
list
(
tmp_indices_np1
)
+
list
(
tmp_indices_np2
)
)
else
:
self
.
indices_np
=
gen_indices_np
(
self
.
x_shape
,
self
.
indices_shapes
,
self
.
index_type_np
)
def
init_dtype_type
(
self
):
self
.
dtype_np
=
np
.
float64
...
...
@@ -109,8 +122,7 @@ class TestIndexPutAPIBase(unittest.TestCase):
self
.
x_pd
=
paddle
.
to_tensor
(
self
.
x_np
,
dtype
=
self
.
dtype_pd
)
self
.
value_pd
=
paddle
.
to_tensor
(
self
.
value_np
,
dtype
=
self
.
dtype_pd
)
self
.
indices_pd
=
[
paddle
.
to_tensor
(
indice
,
dtype
=
self
.
index_type_pd
)
for
indice
in
self
.
indices_np
paddle
.
to_tensor
(
indice
)
for
indice
in
self
.
indices_np
]
self
.
indices_pd
=
tuple
(
self
.
indices_pd
)
ref_res
=
compute_index_put_ref
(
...
...
@@ -128,16 +140,37 @@ class TestIndexPutAPIBase(unittest.TestCase):
x
=
paddle
.
static
.
data
(
name
=
"x"
,
shape
=
self
.
x_shape
,
dtype
=
self
.
dtype_pd
)
indices
=
tuple
(
[
paddle
.
static
.
data
(
name
=
"indice"
+
str
(
i
),
shape
=
self
.
indices_shapes
[
i
],
dtype
=
self
.
index_type_pd
,
)
for
i
in
range
(
len
(
self
.
indices_shapes
))
]
)
if
self
.
mixed_indices
:
indices
=
tuple
(
[
paddle
.
static
.
data
(
name
=
"indice"
+
str
(
i
),
shape
=
self
.
indices_shapes
[
i
],
dtype
=
self
.
index_type_pd
,
)
for
i
in
range
(
len
(
self
.
indices_shapes
))
]
+
[
paddle
.
static
.
data
(
name
=
"indice"
+
str
(
i
+
len
(
self
.
indices_shapes
)),
shape
=
self
.
indices_shapes1
[
i
],
dtype
=
self
.
index_type_pd1
,
)
for
i
in
range
(
len
(
self
.
indices_shapes1
))
]
)
else
:
indices
=
tuple
(
[
paddle
.
static
.
data
(
name
=
"indice"
+
str
(
i
),
shape
=
self
.
indices_shapes
[
i
],
dtype
=
self
.
index_type_pd
,
)
for
i
in
range
(
len
(
self
.
indices_shapes
))
]
)
value
=
paddle
.
static
.
data
(
name
=
"value"
,
shape
=
self
.
value_shape
,
dtype
=
self
.
dtype_pd
)
...
...
@@ -822,5 +855,39 @@ class TestIndexPutAPIBackward(unittest.TestCase):
)
class
TestIndexPutAPIMixedIndices
(
TestIndexPutAPIBase
):
def
init_dtype_type
(
self
):
self
.
dtype_np
=
np
.
float64
self
.
index_type_np
=
np
.
int32
self
.
x_shape
=
(
110
,
42
,
32
,
56
)
self
.
indices_shapes
=
((
16
,
16
),
(
16
,
16
))
self
.
value_shape
=
(
16
,
16
,
56
)
self
.
dtype_pd
=
paddle
.
float64
self
.
index_type_pd
=
paddle
.
int32
self
.
accumulate
=
False
self
.
mixed_indices
=
True
self
.
index_type_np1
=
np
.
bool_
self
.
indices_shapes1
=
[(
32
,)]
self
.
index_type_pd1
=
paddle
.
bool
class
TestIndexPutAPIMixedIndices1
(
TestIndexPutAPIBase
):
def
init_dtype_type
(
self
):
self
.
dtype_np
=
np
.
float64
self
.
index_type_np
=
np
.
int32
self
.
x_shape
=
(
110
,
42
,
32
,
56
)
self
.
indices_shapes
=
((
16
,
16
),
(
16
,
16
))
self
.
value_shape
=
(
16
,
16
,
56
)
self
.
dtype_pd
=
paddle
.
float64
self
.
index_type_pd
=
paddle
.
int32
self
.
accumulate
=
True
self
.
mixed_indices
=
True
self
.
index_type_np1
=
np
.
bool_
self
.
indices_shapes1
=
[(
32
,)]
self
.
index_type_pd1
=
paddle
.
bool
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录