Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d954becb
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
d954becb
编写于
1月 11, 2018
作者:
E
emailweixu
提交者:
GitHub
1月 11, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #7371 from emailweixu/assign_value_op
assign_value operator
上级
5dbd5370
25ecd206
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
220 addition
and
8 deletion
+220
-8
paddle/framework/tensor_util.h
paddle/framework/tensor_util.h
+2
-2
paddle/operators/assign_value_op.cc
paddle/operators/assign_value_op.cc
+73
-0
paddle/operators/assign_value_op.cu.cc
paddle/operators/assign_value_op.cu.cc
+19
-0
paddle/operators/assign_value_op.h
paddle/operators/assign_value_op.h
+50
-0
python/paddle/v2/fluid/layers/tensor.py
python/paddle/v2/fluid/layers/tensor.py
+36
-6
python/paddle/v2/fluid/tests/test_assign_value_op.py
python/paddle/v2/fluid/tests/test_assign_value_op.py
+40
-0
未找到文件。
paddle/framework/tensor_util.h
浏览文件 @
d954becb
...
@@ -116,8 +116,8 @@ inline void Copy(const Tensor& src, const platform::Place& dst_place,
...
@@ -116,8 +116,8 @@ inline void Copy(const Tensor& src, const platform::Place& dst_place,
* @param[in] src The external tensor.
* @param[in] src The external tensor.
* @param[in] ctx The device context contains device resources.
* @param[in] ctx The device context contains device resources.
*
*
* * @note CopyFromVector
assumes that the tensor has been resized
* * @note CopyFromVector
will resize dst to an 1D tensor with the same
*
before invoking
.
*
size as src
.
*/
*/
template
<
typename
T
>
template
<
typename
T
>
inline
void
CopyFromVector
(
const
std
::
vector
<
T
>&
src
,
inline
void
CopyFromVector
(
const
std
::
vector
<
T
>&
src
,
...
...
paddle/operators/assign_value_op.cc
0 → 100644
浏览文件 @
d954becb
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/assign_value_op.h"
namespace
paddle
{
namespace
operators
{
class
AssignValueOp
:
public
framework
::
OperatorWithKernel
{
public:
AssignValueOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorWithKernel
(
type
,
inputs
,
outputs
,
attrs
)
{}
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of AssignValueOp should not be null."
);
auto
shape
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"shape"
);
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
shape
));
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
return
framework
::
OpKernelType
(
framework
::
proto
::
DataType
(
ctx
.
Attr
<
int
>
(
"dtype"
)),
ctx
.
GetPlace
());
}
};
class
AssignValueOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
AssignValueOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddOutput
(
"Out"
,
"(Tensor) Output tensor of assign_value operator."
);
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"(vector<int>) "
"Shape of values."
);
AddAttr
<
int
>
(
"dtype"
,
"data type of values"
)
.
InEnum
({
framework
::
proto
::
DataType
::
INT32
,
framework
::
proto
::
DataType
::
FP32
});
AddAttr
<
std
::
vector
<
float
>>
(
"fp32_values"
,
"store the float values"
)
.
SetDefault
({});
AddAttr
<
std
::
vector
<
int
>>
(
"int32_values"
,
"store the int values"
)
.
SetDefault
({});
AddComment
(
R"DOC(
AssignValue operator
$$Out = values$$
)DOC"
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
assign_value
,
ops
::
AssignValueOp
,
ops
::
AssignValueOpMaker
);
REGISTER_OP_CPU_KERNEL
(
assign_value
,
ops
::
AssignValueKernel
<
int
>
,
ops
::
AssignValueKernel
<
float
>
);
paddle/operators/assign_value_op.cu.cc
0 → 100644
浏览文件 @
d954becb
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Indicesou may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/assign_value_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
assign_value
,
ops
::
AssignValueKernel
<
int
>
,
ops
::
AssignValueKernel
<
float
>
);
paddle/operators/assign_value_op.h
0 → 100644
浏览文件 @
d954becb
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/platform/enforce.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
class
AssignValueKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
virtual
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
shape
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"shape"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
int
dtype
=
ctx
.
Attr
<
int
>
(
"dtype"
);
const
char
*
value_name
=
nullptr
;
switch
(
dtype
)
{
case
framework
::
proto
::
DataType
::
INT32
:
value_name
=
"int32_values"
;
break
;
case
framework
::
proto
::
DataType
::
FP32
:
value_name
=
"fp32_values"
;
break
;
default:
PADDLE_THROW
(
"Unsupported dtype for assign_value_op: %d"
,
dtype
);
break
;
}
auto
values
=
ctx
.
Attr
<
std
::
vector
<
T
>>
(
value_name
);
framework
::
CopyFromVector
(
values
,
ctx
.
device_context
(),
out
);
out
->
Resize
(
framework
::
make_ddim
(
shape
));
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/v2/fluid/layers/tensor.py
浏览文件 @
d954becb
from
..layer_helper
import
LayerHelper
from
..layer_helper
import
LayerHelper
from
..param_attr
import
ParamAttr
from
..param_attr
import
ParamAttr
from
..framework
import
convert_np_dtype_to_dtype_
from
..framework
import
Variable
from
..core
import
DataType
import
numpy
__all__
=
[
__all__
=
[
'create_tensor'
,
'create_parameter'
,
'cast'
,
'concat'
,
'sums'
,
'assign'
,
'create_tensor'
,
'create_parameter'
,
'cast'
,
'concat'
,
'sums'
,
'assign'
,
...
@@ -121,7 +125,7 @@ def assign(input, output):
...
@@ -121,7 +125,7 @@ def assign(input, output):
This function copies the *input* Variable to the *output* Variable.
This function copies the *input* Variable to the *output* Variable.
Args:
Args:
input(Variable): The source variable
input(Variable
|numpy.ndarray
): The source variable
output(Variable): The destination variable
output(Variable): The destination variable
Returns:
Returns:
...
@@ -134,11 +138,37 @@ def assign(input, output):
...
@@ -134,11 +138,37 @@ def assign(input, output):
fluid.layers.assign(hidden, out)
fluid.layers.assign(hidden, out)
"""
"""
helper
=
LayerHelper
(
'assign'
,
**
locals
())
helper
=
LayerHelper
(
'assign'
,
**
locals
())
helper
.
append_op
(
if
isinstance
(
input
,
Variable
):
type
=
'scale'
,
helper
.
append_op
(
inputs
=
{
'X'
:
[
input
]},
type
=
'scale'
,
outputs
=
{
'Out'
:
[
output
]},
inputs
=
{
'X'
:
[
input
]},
attrs
=
{
'scale'
:
1.0
})
outputs
=
{
'Out'
:
[
output
]},
attrs
=
{
'scale'
:
1.0
})
elif
isinstance
(
input
,
numpy
.
ndarray
):
dtype
=
convert_np_dtype_to_dtype_
(
input
.
dtype
)
if
dtype
==
DataType
.
FP32
:
value_name
=
"fp32_values"
values
=
[
float
(
v
)
for
v
in
input
.
flat
]
elif
dtype
==
DataType
.
INT32
:
value_name
=
"int32_values"
values
=
[
int
(
v
)
for
v
in
input
.
flat
]
else
:
raise
ValueError
(
"Unsupported dtype %s"
,
input
.
dtype
)
if
input
.
size
>
1024
*
1024
:
raise
ValueError
(
"The size of input is too big. Please consider "
"saving it to file and 'load_op' to load it"
)
helper
.
append_op
(
type
=
'assign_value'
,
outputs
=
{
'Out'
:
[
output
]},
attrs
=
{
'dtype'
:
dtype
,
'shape'
:
list
(
input
.
shape
),
value_name
:
values
})
else
:
raise
ValueError
(
"Wrong type for assign input: %s"
%
type
(
input
))
return
output
return
output
...
...
python/paddle/v2/fluid/tests/test_assign_value_op.py
0 → 100644
浏览文件 @
d954becb
import
paddle.v2.fluid
as
fluid
import
paddle.v2.fluid.layers
as
layers
import
op_test
import
numpy
import
unittest
import
paddle.v2.fluid.framework
as
framework
class
TestAssignValueOp
(
op_test
.
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"assign_value"
x
=
numpy
.
random
.
random
(
size
=
(
2
,
5
)).
astype
(
numpy
.
float32
)
self
.
inputs
=
{}
self
.
outputs
=
{
'Out'
:
x
}
self
.
attrs
=
{
'shape'
:
x
.
shape
,
'dtype'
:
framework
.
convert_np_dtype_to_dtype_
(
x
.
dtype
),
'fp32_values'
:
[
float
(
v
)
for
v
in
x
.
flat
]
}
def
test_forward
(
self
):
self
.
check_output
()
def
test_assign
(
self
):
val
=
(
-
100
+
200
*
numpy
.
random
.
random
(
size
=
(
2
,
5
))).
astype
(
numpy
.
int32
)
x
=
layers
.
create_tensor
(
dtype
=
"float32"
)
layers
.
assign
(
input
=
val
,
output
=
x
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
fetched_x
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{},
fetch_list
=
[
x
])[
0
]
self
.
assertTrue
(
numpy
.
array_equal
(
fetched_x
,
val
),
"fetch_x=%s val=%s"
%
(
fetched_x
,
val
))
self
.
assertEqual
(
fetched_x
.
dtype
,
val
.
dtype
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录