Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
oneflow
提交
b0736b8b
O
oneflow
项目概览
Oneflow-Inc
/
oneflow
上一次同步 接近 3 年
通知
13
Star
2733
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
oneflow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b0736b8b
编写于
2月 11, 2018
作者:
X
Xinqi Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
more df functions
Former-commit-id: 506df293b368d79a785cc68461c946ad414b79a0
上级
7fd121f7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
104 addition
and
16 deletion
+104
-16
oneflow/core/auto_placement/df_demo.cpp
oneflow/core/auto_placement/df_demo.cpp
+5
-5
oneflow/core/auto_placement/df_func.cpp
oneflow/core/auto_placement/df_func.cpp
+91
-11
oneflow/core/auto_placement/df_func.h
oneflow/core/auto_placement/df_func.h
+8
-0
未找到文件。
oneflow/core/auto_placement/df_demo.cpp
浏览文件 @
b0736b8b
...
...
@@ -7,23 +7,23 @@ namespace df {
namespace
{
void
AutoPlacementMemoryDemo
()
{
Tensor
var
(
Shape
({
4
,
4
}),
[](
size_t
index
)
{
return
index
%
2
?
0
:
100
0
;
});
Tensor
var
(
Shape
({
4
,
4
}),
[](
size_t
index
)
{
return
index
%
2
?
0
:
100
;
});
Tensor
row_ones
(
Shape
({
var
.
shape
().
At
(
0
)}),
1
);
Tensor
col_ones
(
Shape
({
var
.
shape
().
At
(
1
)}),
1
);
Tensor
epsilon
(
0.000000001
);
FOR_RANGE
(
int
,
i
,
0
,
2
000
)
{
FOR_RANGE
(
int
,
i
,
0
,
1
000
)
{
double
lr
=
1
;
if
(
i
<
400
)
{
lr
=
0.1
;
}
else
if
(
i
<
8
00
)
{
}
else
if
(
i
<
6
00
)
{
lr
=
0.01
;
}
else
if
(
i
<
12
00
)
{
}
else
if
(
i
<
8
00
)
{
lr
=
0.001
;
}
else
{
lr
=
0.0001
;
}
Tensor
x
=
Add
(
Square
(
FixedExpectation
(
Update
(
&
var
,
lr
),
1
)),
epsilon
);
Tensor
x
=
Add
(
Square
(
(
FixedExpectation
(
Update
(
&
var
,
lr
),
1
)
)),
epsilon
);
const
auto
&
x_copies
=
Clone
(
x
,
4
);
Tensor
row
=
MatrixRowSum
(
x_copies
.
at
(
0
));
Tensor
col
=
MatrixColSum
(
x_copies
.
at
(
1
));
...
...
oneflow/core/auto_placement/df_func.cpp
浏览文件 @
b0736b8b
...
...
@@ -64,6 +64,20 @@ Tensor Minus(const Tensor& input) {
});
}
Tensor
Relu
(
const
Tensor
&
input
)
{
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
input
.
buffer
()));
FOR_RANGE
(
int
,
i
,
0
,
out
->
Size
())
{
if
(
input
.
At
(
i
)
<
0
)
{
out
->
At
(
i
)
=
0
;
}
}
return
Tensor
(
out
,
[
=
](
const
Buffer
&
out_diff
)
{
Buffer
input_diff
(
out_diff
);
FOR_RANGE
(
int
,
i
,
0
,
input_diff
.
Size
())
{
if
(
input
.
At
(
i
)
<
0
)
{
input_diff
.
At
(
i
)
=
0
;
}
}
input
.
HandleDiff
(
input_diff
);
});
}
Tensor
Abs
(
const
Tensor
&
input
)
{
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
input
.
buffer
()));
FOR_RANGE
(
int
,
i
,
0
,
out
->
Size
())
{
...
...
@@ -147,19 +161,28 @@ Tensor Add(const Tensor& a, const Tensor& b) {
}
Tensor
Max
(
const
Tensor
&
a
,
const
Tensor
&
b
)
{
CHECK
(
a
.
Size
()
==
1
&&
b
.
Size
()
==
1
);
bool
is_b_gt_a
=
(
b
.
At
(
0
)
>
a
.
At
(
0
));
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
is_b_gt_a
?
b
.
buffer
()
:
a
.
buffer
()));
CHECK
(
a
.
shape
().
dim_vec
().
size
()
==
b
.
shape
().
dim_vec
().
size
());
FOR_RANGE
(
int
,
i
,
0
,
a
.
shape
().
dim_vec
().
size
())
{
CHECK
(
a
.
shape
().
dim_vec
().
at
(
i
)
==
b
.
shape
().
dim_vec
().
at
(
i
));
}
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
a
.
buffer
()));
FOR_RANGE
(
size_t
,
i
,
0
,
out
->
Size
())
{
out
->
At
(
i
)
=
std
::
max
(
a
.
At
(
i
),
b
.
At
(
i
));
}
return
Tensor
(
out
,
[
=
](
const
Buffer
&
out_diff
)
{
Buffer
zero_diff
(
out_diff
);
zero_diff
.
At
(
0
)
=
0
;
if
(
is_b_gt_a
)
{
a
.
HandleDiff
(
zero_diff
);
b
.
HandleDiff
(
out_diff
);
}
else
{
a
.
HandleDiff
(
out_diff
);
b
.
HandleDiff
(
zero_diff
);
Buffer
a_diff
(
out_diff
.
shape
(),
0
);
Buffer
b_diff
(
out_diff
.
shape
(),
0
);
FOR_RANGE
(
size_t
,
i
,
0
,
out_diff
.
Size
())
{
if
(
a
.
At
(
i
)
>
b
.
At
(
i
))
{
a_diff
.
At
(
i
)
=
out_diff
.
At
(
i
);
b_diff
.
At
(
i
)
=
0
;
}
else
{
b_diff
.
At
(
i
)
=
out_diff
.
At
(
i
);
a_diff
.
At
(
i
)
=
0
;
}
}
a
.
HandleDiff
(
a_diff
);
b
.
HandleDiff
(
b_diff
);
});
}
...
...
@@ -190,6 +213,23 @@ Tensor Max(const Tensor& input) {
});
}
Tensor
Min
(
const
Tensor
&
input
)
{
double
min_value
=
std
::
numeric_limits
<
double
>::
max
();
size_t
min_index
=
0
;
FOR_RANGE
(
int
,
i
,
0
,
input
.
Size
())
{
if
(
input
.
At
(
i
)
<
min_value
)
{
min_value
=
input
.
buffer
().
At
(
i
);
min_index
=
i
;
}
}
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
Shape
({
1
}),
min_value
));
return
Tensor
(
out
,
[
=
](
const
Buffer
&
out_diff
)
{
Buffer
input_diff
(
input
.
shape
(),
0
);
input_diff
.
At
(
min_index
)
=
out_diff
.
At
(
0
);
input
.
HandleDiff
(
input_diff
);
});
}
Tensor
Variance
(
const
Tensor
&
input
)
{
auto
copies
=
Clone
(
input
,
2
);
return
Avg
(
Square
(
Sub
(
copies
.
at
(
0
),
Avg
(
copies
.
at
(
1
)))));
...
...
@@ -200,6 +240,11 @@ Tensor AvgAbsDeviation(const Tensor& input) {
return
Avg
(
Abs
(
Sub
(
copies
.
at
(
0
),
Avg
(
copies
.
at
(
1
)))));
}
Tensor
MaxDeviation
(
const
Tensor
&
input
)
{
auto
copies
=
Clone
(
input
,
2
);
return
Sub
(
Max
(
copies
.
at
(
0
)),
Min
(
copies
.
at
(
1
)));
}
Tensor
Sum
(
const
Tensor
&
input
)
{
double
sum
=
0
;
FOR_RANGE
(
int
,
i
,
0
,
input
.
Size
())
{
sum
+=
input
.
At
(
i
);
}
...
...
@@ -219,6 +264,41 @@ Tensor Avg(const Tensor& input) {
return
Tensor
(
out
,
[
=
](
const
Buffer
&
out_diff
)
{
sum
.
HandleDiff
(
out_diff
);
});
}
Tensor
Mul
(
const
Tensor
&
a
,
const
Tensor
&
b
)
{
Tensor
big
=
a
;
Tensor
small
=
b
;
if
(
a
.
Size
()
<
b
.
Size
())
{
big
=
b
;
small
=
a
;
}
CHECK
(
big
.
Size
()
%
small
.
Size
()
==
0
);
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
big
.
buffer
()));
size_t
small_size
=
small
.
Size
();
size_t
group_size
=
big
.
Size
()
/
small_size
;
FOR_RANGE
(
int
,
i
,
0
,
small_size
)
{
FOR_RANGE
(
int
,
j
,
0
,
group_size
)
{
out
->
At
(
i
*
group_size
+
j
)
*=
small
.
At
(
i
);
}
}
return
Tensor
(
out
,
[
=
](
const
Buffer
&
out_diff
)
{
Buffer
big_diff
(
out_diff
);
FOR_RANGE
(
int
,
i
,
0
,
small_size
)
{
FOR_RANGE
(
int
,
j
,
0
,
group_size
)
{
big_diff
.
At
(
i
*
group_size
+
j
)
*=
small
.
At
(
i
);
}
}
big
.
HandleDiff
(
big_diff
);
Buffer
small_diff
(
small
.
shape
(),
0
);
FOR_RANGE
(
int
,
i
,
0
,
small_size
)
{
FOR_RANGE
(
int
,
j
,
0
,
group_size
)
{
size_t
index
=
i
*
group_size
+
j
;
small_diff
.
At
(
i
)
+=
out_diff
.
At
(
index
)
*
big
.
At
(
index
);
}
}
small
.
HandleDiff
(
small_diff
);
});
}
Tensor
ElemWiseMul
(
const
Tensor
&
a
,
const
Tensor
&
b
)
{
CHECK
(
a
.
Size
()
==
b
.
Size
());
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
a
.
buffer
()));
...
...
oneflow/core/auto_placement/df_func.h
浏览文件 @
b0736b8b
...
...
@@ -30,18 +30,26 @@ Tensor Sub(const Tensor& a, const Tensor& b);
Tensor
ElemWiseMul
(
const
Tensor
&
a
,
const
Tensor
&
b
);
Tensor
Mul
(
const
Tensor
&
a
,
const
Tensor
&
b
);
Tensor
Reciprocal
(
const
Tensor
&
input
);
Tensor
Max
(
const
Tensor
&
a
,
const
Tensor
&
b
);
Tensor
Max
(
const
Tensor
&
a
);
Tensor
Relu
(
const
Tensor
&
input
);
Tensor
Min
(
const
Tensor
&
a
);
Tensor
Sum
(
const
Tensor
&
a
);
Tensor
Avg
(
const
Tensor
&
a
);
Tensor
Variance
(
const
Tensor
&
a
);
Tensor
MaxDeviation
(
const
Tensor
&
a
);
Tensor
AvgAbsDeviation
(
const
Tensor
&
a
);
Tensor
Square
(
const
Tensor
&
input
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录