Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
b1d6ef0e
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b1d6ef0e
编写于
7月 06, 2020
作者:
B
baihuawei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix GatherV2 index out of bounds
上级
5359d63e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
25 addition
and
21 deletion
+25
-21
mindspore/ccsrc/kernel/cpu/gather_cpu_kernel.cc
mindspore/ccsrc/kernel/cpu/gather_cpu_kernel.cc
+25
-21
未找到文件。
mindspore/ccsrc/kernel/cpu/gather_cpu_kernel.cc
浏览文件 @
b1d6ef0e
...
...
@@ -21,17 +21,14 @@ namespace mindspore {
namespace
kernel
{
void
GatherV2CPUKernel
::
InitKernel
(
const
CNodePtr
&
kernel_node
)
{
CheckParam
(
kernel_node
);
input_shape_
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
kernel_node
,
0
);
indices_shape_
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
kernel_node
,
1
);
output_shape_
=
AnfAlgo
::
GetOutputInferShape
(
kernel_node
,
0
);
axis_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
AXIS
);
if
(
axis_
<
0
)
{
axis_
=
axis_
+
SizeToInt
(
input_shape_
.
size
());
}
axis_
+=
4
-
input_shape_
.
size
();
CPUKernelUtils
::
ExpandDimsTo4
(
&
input_shape_
);
CPUKernelUtils
::
ExpandDimsTo4
(
&
output_shape_
);
}
...
...
@@ -44,7 +41,6 @@ bool GatherV2CPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
size_t
dim0
=
input_shape_
[
0
];
size_t
dim1
=
input_shape_
[
1
];
size_t
dim2
=
input_shape_
[
2
];
if
(
axis_
==
3
)
{
for
(
size_t
i
=
0
;
i
<
dim0
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<
dim1
;
++
j
)
{
...
...
@@ -66,7 +62,6 @@ bool GatherV2CPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
}
else
if
(
axis_
==
0
)
{
CopyDataToOutput
(
inputs
,
0
,
0
,
0
,
&
output_addr
,
&
buff_size
);
}
return
true
;
}
...
...
@@ -75,34 +70,43 @@ void GatherV2CPUKernel::CopyDataToOutput(const std::vector<kernel::AddressPtr> &
auto
input_addr
=
reinterpret_cast
<
float
*>
(
inputs
[
0
]
->
addr
);
auto
indices_addr
=
reinterpret_cast
<
int
*>
(
inputs
[
1
]
->
addr
);
size_t
elem_num
=
inputs
[
1
]
->
size
/
4
;
size_t
num
=
CPUKernelUtils
::
GetElementNumOnAxis
(
input_shape_
,
axis_
);
for
(
size_t
i
=
0
;
i
<
elem_num
;
++
i
)
{
size_t
index
=
IntToSize
(
indices_addr
[
i
]);
size_t
pos
=
0
;
if
(
axis_
==
3
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
dim0
,
dim1
,
dim2
,
index
);
}
else
if
(
axis_
==
2
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
dim0
,
dim1
,
index
,
0
);
}
else
if
(
axis_
==
1
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
dim0
,
index
,
0
,
0
);
}
else
if
(
axis_
==
0
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
index
,
0
,
0
,
0
);
if
(
indices_addr
[
i
]
<
0
)
{
MS_LOG
(
EXCEPTION
)
<<
"The indices value is less than 0."
;
}
size_t
num
=
CPUKernelUtils
::
GetElementNumOnAxis
(
input_shape_
,
axis_
);
auto
ret
=
memcpy_s
(
*
output_addr
,
*
buff_size
,
input_addr
+
pos
,
num
*
sizeof
(
float
));
if
(
ret
!=
EOK
)
{
MS_LOG
(
EXCEPTION
)
<<
"memcpy failed."
;
size_t
index
=
IntToSize
(
indices_addr
[
i
]);
if
(
index
>=
input_shape_
[
IntToSize
(
axis_
)])
{
auto
ret
=
memset_s
(
*
output_addr
,
*
buff_size
,
0.
,
num
*
sizeof
(
float
));
if
(
ret
!=
EOK
)
{
MS_LOG
(
EXCEPTION
)
<<
"memset failed."
;
}
}
else
{
size_t
pos
=
0
;
if
(
axis_
==
3
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
dim0
,
dim1
,
dim2
,
index
);
}
else
if
(
axis_
==
2
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
dim0
,
dim1
,
index
,
0
);
}
else
if
(
axis_
==
1
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
dim0
,
index
,
0
,
0
);
}
else
if
(
axis_
==
0
)
{
pos
=
CPUKernelUtils
::
CalcOffset
(
input_shape_
,
index
,
0
,
0
,
0
);
}
auto
ret
=
memcpy_s
(
*
output_addr
,
*
buff_size
,
input_addr
+
pos
,
num
*
sizeof
(
float
));
if
(
ret
!=
EOK
)
{
MS_LOG
(
EXCEPTION
)
<<
"memcpy failed."
;
}
}
*
output_addr
+=
num
;
*
buff_size
-=
num
*
sizeof
(
float
);
}
}
}
// namespace kernel
void
GatherV2CPUKernel
::
CheckParam
(
const
CNodePtr
&
kernel_node
)
{
auto
input_shape
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
kernel_node
,
0
);
if
(
input_shape
.
size
()
>
4
)
{
MS_LOG
(
EXCEPTION
)
<<
"Input dims is "
<<
input_shape
.
size
()
<<
", but GatherV2CPUKernel olny support 4d or lower."
;
}
size_t
input_num
=
AnfAlgo
::
GetInputTensorNum
(
kernel_node
);
if
(
input_num
!=
2
)
{
MS_LOG
(
EXCEPTION
)
<<
"Argument number is "
<<
input_num
<<
", but GatherV2CPUKernel needs 2."
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录