Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
b0963833
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看板
提交
b0963833
编写于
6月 15, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
6月 15, 2020
浏览文件
操作
浏览文件
下载
差异文件
!2045 optimize cpu ftrl
Merge pull request !2045 from kisnwang/optimize-cpu-ftrl
上级
9442516f
abf0ee6f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
68 addition
and
11 deletion
+68
-11
mindspore/ccsrc/kernel/common_utils.cc
mindspore/ccsrc/kernel/common_utils.cc
+51
-0
mindspore/ccsrc/kernel/common_utils.h
mindspore/ccsrc/kernel/common_utils.h
+2
-0
mindspore/ccsrc/kernel/cpu/cpu_kernel.cc
mindspore/ccsrc/kernel/cpu/cpu_kernel.cc
+1
-1
mindspore/ccsrc/kernel/cpu/sparse_apply_ftrl_cpu_kernel.cc
mindspore/ccsrc/kernel/cpu/sparse_apply_ftrl_cpu_kernel.cc
+13
-9
mindspore/ccsrc/kernel/cpu/sparse_apply_ftrl_cpu_kernel.h
mindspore/ccsrc/kernel/cpu/sparse_apply_ftrl_cpu_kernel.h
+1
-1
未找到文件。
mindspore/ccsrc/kernel/common_utils.cc
浏览文件 @
b0963833
...
...
@@ -18,6 +18,7 @@
#include <unordered_map>
#include <map>
#include <iostream>
#include <utility>
#include <fstream>
#include "nlohmann/json.hpp"
#include "session/anf_runtime_algorithm.h"
...
...
@@ -583,5 +584,55 @@ void DeduplicateIndexedSlices(const SparseGradient &origin_sparse_grad, SparseGr
}
unique_grad
->
indices_size_
=
unique_indices_size
;
}
void
ReduceSparseGradient
(
const
SparseGradient
&
origin_sparse_grad
,
SparseGradient
*
unique_grad
,
size_t
first_dim
,
size_t
outer_dim
)
{
MS_EXCEPTION_IF_NULL
(
origin_sparse_grad
.
value_
);
MS_EXCEPTION_IF_NULL
(
origin_sparse_grad
.
indices_
);
MS_EXCEPTION_IF_NULL
(
unique_grad
);
MS_EXCEPTION_IF_NULL
(
unique_grad
->
value_
);
MS_EXCEPTION_IF_NULL
(
unique_grad
->
indices_
);
size_t
unique_indices_size
=
0
;
std
::
vector
<
std
::
pair
<
int
,
size_t
>>
sorted_indices
;
sorted_indices
.
reserve
(
origin_sparse_grad
.
indices_size_
);
for
(
size_t
i
=
0
;
i
<
origin_sparse_grad
.
indices_size_
;
++
i
)
{
int
index
=
origin_sparse_grad
.
indices_
[
i
];
if
(
index
<
0
||
IntToSize
(
index
)
>=
first_dim
)
{
continue
;
}
sorted_indices
.
emplace_back
(
std
::
pair
<
int
,
size_t
>
(
index
,
i
*
outer_dim
));
}
std
::
sort
(
sorted_indices
.
begin
(),
sorted_indices
.
end
(),
[](
const
std
::
pair
<
int
,
size_t
>
&
left
,
const
std
::
pair
<
int
,
size_t
>
&
right
)
{
return
left
.
first
<
right
.
first
;
});
int
last_index
=
0
;
size_t
indices_size
=
sorted_indices
.
size
();
size_t
start_index
=
0
;
size_t
end_index
=
outer_dim
;
size_t
dst_len
=
indices_size
*
outer_dim
;
for
(
size_t
i
=
0
;
i
<
indices_size
;
++
i
)
{
int
index
=
sorted_indices
[
i
].
first
;
if
(
i
==
0
||
last_index
!=
index
)
{
if
(
i
>
0
&&
last_index
!=
index
)
{
unique_indices_size
++
;
start_index
+=
outer_dim
;
end_index
+=
outer_dim
;
}
unique_grad
->
indices_
[
unique_indices_size
]
=
index
;
auto
ret_code
=
memcpy_s
(
unique_grad
->
value_
+
start_index
,
dst_len
-
start_index
,
origin_sparse_grad
.
value_
+
sorted_indices
[
i
].
second
,
outer_dim
);
if
(
ret_code
!=
EOK
)
{
MS_LOG
(
EXCEPTION
)
<<
"Failed to copy data!"
;
}
}
else
{
for
(
size_t
j
=
start_index
,
k
=
sorted_indices
[
i
].
second
;
j
<
end_index
;
++
j
,
++
k
)
{
unique_grad
->
value_
[
j
]
+=
origin_sparse_grad
.
value_
[
k
];
}
}
last_index
=
index
;
}
unique_grad
->
indices_size_
=
unique_indices_size
;
}
}
// namespace kernel
}
// namespace mindspore
mindspore/ccsrc/kernel/common_utils.h
浏览文件 @
b0963833
...
...
@@ -92,6 +92,8 @@ bool IsSameShape(const std::vector<size_t> &shape_a, const std::vector<size_t> &
int
Sign
(
float
x
);
void
DeduplicateIndexedSlices
(
const
SparseGradient
&
origin_sparse_grad
,
SparseGradient
*
unique_grad
,
size_t
first_dim
,
size_t
outer_dim
);
void
ReduceSparseGradient
(
const
SparseGradient
&
origin_sparse_grad
,
SparseGradient
*
unique_grad
,
size_t
first_dim
,
size_t
outer_dim
);
}
// namespace kernel
}
// namespace mindspore
...
...
mindspore/ccsrc/kernel/cpu/cpu_kernel.cc
浏览文件 @
b0963833
...
...
@@ -37,8 +37,8 @@ void CPUKernel::InitInputOutputSize(const CNodePtr &kernel_node) {
}
void
CPUKernel
::
Init
(
const
CNodePtr
&
kernel_node
)
{
InitInputOutputSize
(
kernel_node
);
InitKernel
(
kernel_node
);
InitInputOutputSize
(
kernel_node
);
}
void
CPUKernelUtils
::
ExpandDimsTo4
(
std
::
vector
<
size_t
>
*
shape
)
{
...
...
mindspore/ccsrc/kernel/cpu/sparse_apply_ftrl_cpu_kernel.cc
浏览文件 @
b0963833
...
...
@@ -23,6 +23,13 @@ namespace {
constexpr
size_t
kSparseApplyFtrlInputSize
=
5
;
}
// namespace
void
SparseApplyFtrlCPUKernel
::
InitInputOutputSize
(
const
CNodePtr
&
kernel_node
)
{
CPUKernel
::
InitInputOutputSize
(
kernel_node
);
MS_EXCEPTION_IF_NULL
(
kernel_node
);
workspace_size_list_
.
emplace_back
(
indices_size_
*
var_outer_dim_size_
*
sizeof
(
float
));
workspace_size_list_
.
emplace_back
(
indices_size_
*
sizeof
(
int
));
}
void
SparseApplyFtrlCPUKernel
::
InitKernel
(
const
CNodePtr
&
kernel_node
)
{
MS_EXCEPTION_IF_NULL
(
kernel_node
);
std
::
vector
<
size_t
>
var_shape
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
kernel_node
,
0
);
...
...
@@ -72,7 +79,7 @@ void SparseApplyFtrlCPUKernel::InitKernel(const CNodePtr &kernel_node) {
}
bool
SparseApplyFtrlCPUKernel
::
Launch
(
const
std
::
vector
<
kernel
::
AddressPtr
>
&
inputs
,
const
std
::
vector
<
kernel
::
AddressPtr
>
&
/*workspace*/
,
const
std
::
vector
<
kernel
::
AddressPtr
>
&
workspace
,
const
std
::
vector
<
kernel
::
AddressPtr
>
&
/*outputs*/
)
{
if
(
inputs
.
size
()
<
kSparseApplyFtrlInputSize
)
{
MS_LOG
(
EXCEPTION
)
<<
"error input output size!"
;
...
...
@@ -83,14 +90,11 @@ bool SparseApplyFtrlCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inp
auto
linear
=
reinterpret_cast
<
float
*>
(
inputs
[
2
]
->
addr
);
auto
grad
=
reinterpret_cast
<
float
*>
(
inputs
[
3
]
->
addr
);
auto
indices
=
reinterpret_cast
<
int
*>
(
inputs
[
4
]
->
addr
);
std
::
vector
<
float
>
new_grad
;
new_grad
.
reserve
(
indices_size_
*
var_outer_dim_size_
);
std
::
vector
<
int
>
new_indices
;
new_indices
.
reserve
(
indices_size_
);
SparseGradient
unique_sparse_grad
({
new_grad
.
data
(),
new_indices
.
data
(),
indices_size_
});
DeduplicateIndexedSlices
(
SparseGradient
({
grad
,
indices
,
indices_size_
}),
&
unique_sparse_grad
,
var_first_dim_size_
,
var_outer_dim_size_
);
auto
new_grad
=
reinterpret_cast
<
float
*>
(
workspace
[
0
]
->
addr
);
auto
new_indices
=
reinterpret_cast
<
int
*>
(
workspace
[
1
]
->
addr
);
SparseGradient
unique_sparse_grad
({
new_grad
,
new_indices
,
indices_size_
});
ReduceSparseGradient
(
SparseGradient
({
grad
,
indices
,
indices_size_
}),
&
unique_sparse_grad
,
var_first_dim_size_
,
var_outer_dim_size_
);
for
(
size_t
i
=
0
;
i
<
unique_sparse_grad
.
indices_size_
;
++
i
)
{
int
index
=
unique_sparse_grad
.
indices_
[
i
];
...
...
mindspore/ccsrc/kernel/cpu/sparse_apply_ftrl_cpu_kernel.h
浏览文件 @
b0963833
...
...
@@ -28,7 +28,7 @@ class SparseApplyFtrlCPUKernel : public CPUKernel {
~
SparseApplyFtrlCPUKernel
()
override
=
default
;
void
InitKernel
(
const
CNodePtr
&
kernel_node
)
override
;
void
InitInputOutputSize
(
const
CNodePtr
&
kernel_node
)
override
;
bool
Launch
(
const
std
::
vector
<
AddressPtr
>
&
inputs
,
const
std
::
vector
<
AddressPtr
>
&
workspace
,
const
std
::
vector
<
AddressPtr
>
&
outputs
)
override
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录