Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
oneflow
提交
9714a9e3
O
oneflow
项目概览
Oneflow-Inc
/
oneflow
上一次同步 2 年多
通知
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,体验更适合开发者的 AI 搜索 >>
提交
9714a9e3
编写于
2月 09, 2018
作者:
X
Xinqi Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add tenor with different dimension
Former-commit-id: 29d73e1c3fae3b050a8d6362493a4decd6ff626d
上级
d7d745f2
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
126 addition
and
29 deletion
+126
-29
oneflow/core/auto_placement/buffer.h
oneflow/core/auto_placement/buffer.h
+9
-6
oneflow/core/auto_placement/df_demo.cpp
oneflow/core/auto_placement/df_demo.cpp
+40
-8
oneflow/core/auto_placement/df_func.cpp
oneflow/core/auto_placement/df_func.cpp
+63
-8
oneflow/core/auto_placement/df_func.h
oneflow/core/auto_placement/df_func.h
+5
-1
oneflow/core/auto_placement/tensor.h
oneflow/core/auto_placement/tensor.h
+9
-6
未找到文件。
oneflow/core/auto_placement/buffer.h
浏览文件 @
9714a9e3
...
...
@@ -28,25 +28,28 @@ class Buffer final {
const
Shape
&
shape
()
const
{
return
shape_
;
}
double
At
(
size_t
index
)
const
{
CHECK
(
shape_
.
NumAxes
()
=
=
1
);
CHECK
(
shape_
.
NumAxes
()
>
=
1
);
return
data_
.
at
(
index
);
}
double
At
(
size_t
x
,
size_t
y
)
const
{
CHECK
(
shape_
.
NumAxes
()
=
=
2
);
CHECK
(
shape_
.
NumAxes
()
>
=
2
);
return
data_
.
at
(
x
*
shape_
.
Count
(
1
)
+
y
);
}
double
At
(
size_t
x
,
size_t
y
,
size_t
z
)
const
{
CHECK
(
shape_
.
NumAxes
()
=
=
3
);
CHECK
(
shape_
.
NumAxes
()
>
=
3
);
return
data_
.
at
(
x
*
shape_
.
Count
(
1
)
+
y
*
shape_
.
Count
(
2
)
+
z
);
}
double
&
At
(
size_t
index
)
{
return
data_
.
at
(
index
);
}
double
&
At
(
size_t
index
)
{
CHECK
(
shape_
.
NumAxes
()
>=
1
);
return
data_
.
at
(
index
);
}
double
&
At
(
size_t
x
,
size_t
y
)
{
CHECK
(
shape_
.
NumAxes
()
=
=
2
);
CHECK
(
shape_
.
NumAxes
()
>
=
2
);
return
data_
.
at
(
x
*
shape_
.
Count
(
1
)
+
y
);
}
double
&
At
(
size_t
x
,
size_t
y
,
size_t
z
)
{
CHECK
(
shape_
.
NumAxes
()
==
2
);
CHECK
(
shape_
.
NumAxes
()
>=
3
);
return
data_
.
at
(
x
*
shape_
.
Count
(
1
)
+
y
*
shape_
.
Count
(
2
)
+
z
);
}
...
...
oneflow/core/auto_placement/df_demo.cpp
浏览文件 @
9714a9e3
...
...
@@ -5,15 +5,47 @@ namespace oneflow {
namespace
df
{
void
DifferentialDemo
()
{
Tensor
vec
(
Shape
({
3
,
3
}),
[](
size_t
index
)
{
return
(
index
+
1
)
*
0.095
;
});
FOR_RANGE
(
int
,
i
,
0
,
300
)
{
double
lr
=
0.01
;
if
(
i
>
200
)
{
lr
=
0.001
;
}
const
auto
&
x
=
Clone
(
Update
(
vec
,
lr
),
2
);
Backward
(
Max
(
TensorProduct
(
MatrixRowSum
(
x
.
at
(
0
)),
Reciprocal
(
MatrixColSum
(
x
.
at
(
1
))))));
for
(
double
x
:
vec
.
buffer
().
data
())
{
std
::
cout
<<
x
<<
" "
;
}
Tensor
vec
(
Shape
({
4
,
4
}),
[](
size_t
index
)
{
return
index
%
2
?
0
:
1000
;
});
Tensor
output
;
Tensor
table
;
Tensor
row
;
Tensor
col
;
Tensor
epsilon
(
0.000000001
);
FOR_RANGE
(
int
,
i
,
0
,
2000
)
{
double
lr
=
1
;
if
(
i
<
400
)
{
lr
=
0.1
;
}
else
if
(
i
<
800
)
{
lr
=
0.01
;
}
else
if
(
i
<
1200
)
{
lr
=
0.001
;
}
else
{
lr
=
0.0001
;
}
const
auto
&
x
=
Clone
(
Tee
(
Add
(
Square
(
FixedExpectation
(
Update
(
&
vec
,
lr
),
1
)),
epsilon
),
&
output
),
3
);
const
auto
&
load
=
Clone
(
MatrixColSum
(
x
.
at
(
1
)),
2
);
Backward
(
Add
(
Max
(
Tee
(
ElemWiseMul
(
TensorProduct
(
Tee
(
MatrixRowSum
(
x
.
at
(
0
)),
&
row
),
Reciprocal
(
Tee
(
load
.
at
(
0
),
&
col
))),
x
.
at
(
2
)),
&
table
)),
Max
(
load
.
at
(
1
))));
std
::
cout
<<
"output: "
;
for
(
double
x
:
output
.
buffer
().
data
())
{
std
::
cout
<<
x
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"row: "
;
for
(
double
x
:
row
.
buffer
().
data
())
{
std
::
cout
<<
x
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"col: "
;
for
(
double
x
:
col
.
buffer
().
data
())
{
std
::
cout
<<
x
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"table: "
;
for
(
double
x
:
table
.
buffer
().
data
())
{
std
::
cout
<<
x
<<
" "
;
}
std
::
cout
<<
std
::
endl
<<
std
::
endl
;
}
// Tensor var(Shape({1}), 0.5);
// FOR_RANGE(int, i, 0, 40) {
...
...
oneflow/core/auto_placement/df_func.cpp
浏览文件 @
9714a9e3
...
...
@@ -4,9 +4,9 @@ namespace oneflow {
namespace
df
{
Tensor
Update
(
Tensor
var
,
double
lr
)
{
auto
buffer
=
var
.
mut_buffer_ptr
();
return
Tensor
(
var
,
[
buffer
,
lr
](
const
Buffer
&
diff
)
{
Tensor
Update
(
Tensor
*
var
,
double
lr
)
{
auto
buffer
=
var
->
mut_buffer_ptr
();
return
Tensor
(
*
var
,
[
=
](
const
Buffer
&
diff
)
{
CHECK
(
buffer
->
data
().
size
()
==
diff
.
data
().
size
());
FOR_RANGE
(
int
,
i
,
0
,
buffer
->
data
().
size
())
{
double
&
w
=
buffer
->
mut_data
()
->
at
(
i
);
...
...
@@ -44,13 +44,68 @@ Tensor Minus(const Tensor& input) {
});
}
Tensor
Abs
(
const
Tensor
&
input
)
{
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
input
.
buffer
()));
FOR_RANGE
(
int
,
i
,
0
,
out
->
Size
())
{
double
&
x
=
out
->
mut_data
()
->
at
(
i
);
x
=
(
x
>
0
)
?
x
:
-
x
;
}
return
Tensor
(
out
,
[
=
](
const
Buffer
&
out_diff
)
{
Buffer
input_diff
(
out_diff
);
FOR_RANGE
(
int
,
i
,
0
,
input_diff
.
Size
())
{
double
&
diff
=
input_diff
.
mut_data
()
->
at
(
i
);
diff
*=
(
input
.
buffer
().
data
().
at
(
i
)
>
0
)
?
1
:
-
1
;
}
input
.
HandleDiff
(
input_diff
);
});
}
Tensor
Tee
(
const
Tensor
&
input
,
Tensor
*
out
)
{
*
out
=
input
;
return
Tensor
(
input
);
}
Tensor
Exp
(
const
Tensor
&
input
)
{
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
input
.
buffer
()));
FOR_RANGE
(
int
,
i
,
0
,
out
->
Size
())
{
double
&
x
=
out
->
mut_data
()
->
at
(
i
);
x
=
exp
(
x
);
}
return
Tensor
(
out
,
[
=
](
const
Buffer
&
out_diff
)
{
Buffer
input_diff
(
out_diff
);
FOR_RANGE
(
int
,
i
,
0
,
input_diff
.
Size
())
{
double
&
diff
=
input_diff
.
mut_data
()
->
at
(
i
);
diff
*=
out
->
data
().
at
(
i
);
}
input
.
HandleDiff
(
input_diff
);
});
}
Tensor
Add
(
const
Tensor
&
a
,
const
Tensor
&
b
)
{
CHECK
(
a
.
buffer
().
Size
()
==
b
.
buffer
().
Size
());
std
::
shared_ptr
<
Buffer
>
out
(
new
Buffer
(
a
.
buffer
()));
FOR_RANGE
(
int
,
i
,
0
,
out
->
Size
())
{
out
->
At
(
i
)
+=
b
.
At
(
i
);
}
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
)
{
a
.
HandleDiff
(
out_diff
);
b
.
HandleDiff
(
out_diff
);
big
.
HandleDiff
(
out_diff
);
Buffer
small_diff
(
small
.
shape
(),
0
);
FOR_RANGE
(
int
,
i
,
0
,
small_size
)
{
FOR_RANGE
(
int
,
j
,
0
,
group_size
)
{
small_diff
.
At
(
i
)
+=
out_diff
.
At
(
i
*
group_size
+
j
);
}
}
small
.
HandleDiff
(
small_diff
);
});
}
...
...
oneflow/core/auto_placement/df_func.h
浏览文件 @
9714a9e3
...
...
@@ -7,11 +7,15 @@ namespace oneflow {
namespace
df
{
Tensor
Update
(
Tensor
var
,
double
lr
);
Tensor
Update
(
Tensor
*
var
,
double
lr
);
std
::
vector
<
Tensor
>
Clone
(
const
Tensor
&
input
,
size_t
n
);
Tensor
Minus
(
const
Tensor
&
input
);
Tensor
Abs
(
const
Tensor
&
input
);
Tensor
Exp
(
const
Tensor
&
input
);
Tensor
Tee
(
const
Tensor
&
input
,
Tensor
*
out
);
Tensor
Add
(
const
Tensor
&
a
,
const
Tensor
&
b
);
...
...
oneflow/core/auto_placement/tensor.h
浏览文件 @
9714a9e3
...
...
@@ -8,16 +8,19 @@ namespace df {
class
Tensor
final
{
public:
Tensor
()
=
default
;
Tensor
(
const
Tensor
&
)
=
default
;
explicit
Tensor
(
const
Shape
&
shape
,
double
init
)
explicit
Tensor
(
std
::
shared_ptr
<
Buffer
>
buffer
)
:
buffer_
(
buffer
),
diff_handler_
([](
const
Buffer
&
)
{})
{}
Tensor
(
const
Shape
&
shape
,
double
init
)
:
buffer_
(
std
::
shared_ptr
<
Buffer
>
(
new
Buffer
(
shape
,
init
))),
diff_handler_
([](
const
Buffer
&
)
{})
{}
explicit
Tensor
(
const
Shape
&
shape
,
const
std
::
function
<
double
(
size_t
)
>&
Getter
)
Tensor
(
double
init
)
:
buffer_
(
std
::
shared_ptr
<
Buffer
>
(
new
Buffer
(
Shape
({
1
}),
init
))),
diff_handler_
([](
const
Buffer
&
)
{})
{}
Tensor
(
const
Shape
&
shape
,
const
std
::
function
<
double
(
size_t
)
>&
Getter
)
:
buffer_
(
std
::
shared_ptr
<
Buffer
>
(
new
Buffer
(
shape
,
Getter
))),
diff_handler_
([](
const
Buffer
&
)
{})
{}
explicit
Tensor
(
std
::
shared_ptr
<
Buffer
>
buffer
)
:
buffer_
(
buffer
),
diff_handler_
([](
const
Buffer
&
)
{})
{}
Tensor
(
std
::
shared_ptr
<
Buffer
>
buffer
,
const
std
::
function
<
void
(
const
Buffer
&
)
>&
diff_handler
)
:
buffer_
(
buffer
),
diff_handler_
(
diff_handler
)
{}
...
...
@@ -42,7 +45,7 @@ class Tensor final {
const
Buffer
&
buffer
()
const
{
return
*
buffer_
;
}
const
std
::
shared_ptr
<
Buffer
>&
buffer_ptr
()
const
{
return
buffer_
;
}
std
::
shared_ptr
<
Buffer
>
mut_buffer_ptr
()
{
return
buffer_
;
}
void
HandleDiff
(
const
Buffer
&
diff
)
const
{
diff_handler_
(
diff
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录