Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
d3b3443d
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d3b3443d
编写于
7月 03, 2019
作者:
Z
zhoukunsheng
提交者:
Tao Luo
7月 03, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add ones_like op (#17388)
上级
67b48d7f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
269 addition
and
1 deletion
+269
-1
paddle/fluid/API.spec
paddle/fluid/API.spec
+1
-0
paddle/fluid/operators/fill_any_like_op.cc
paddle/fluid/operators/fill_any_like_op.cc
+64
-0
paddle/fluid/operators/fill_any_like_op.cu
paddle/fluid/operators/fill_any_like_op.cu
+27
-0
paddle/fluid/operators/fill_any_like_op.h
paddle/fluid/operators/fill_any_like_op.h
+60
-0
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+36
-1
python/paddle/fluid/tests/unittests/test_fill_any_like_op.py
python/paddle/fluid/tests/unittests/test_fill_any_like_op.py
+81
-0
未找到文件。
paddle/fluid/API.spec
浏览文件 @
d3b3443d
...
...
@@ -300,6 +300,7 @@ paddle.fluid.layers.isfinite (ArgSpec(args=['x'], varargs=None, keywords=None, d
paddle.fluid.layers.range (ArgSpec(args=['start', 'end', 'step', 'dtype'], varargs=None, keywords=None, defaults=None), ('document', 'a45b42f21bc5a4e84b60981a3d629ab3'))
paddle.fluid.layers.linspace (ArgSpec(args=['start', 'stop', 'num', 'dtype'], varargs=None, keywords=None, defaults=None), ('document', '3663d1148946eed4c1c34c81be586b9e'))
paddle.fluid.layers.zeros_like (ArgSpec(args=['x', 'out'], varargs=None, keywords=None, defaults=(None,)), ('document', 'd88a23bcdc443719b3953593f7cef14a'))
paddle.fluid.layers.ones_like (ArgSpec(args=['x', 'out'], varargs=None, keywords=None, defaults=(None,)), ('document', '642afd126553337d6796600e886a6525'))
paddle.fluid.layers.diag (ArgSpec(args=['diagonal'], varargs=None, keywords=None, defaults=None), ('document', '88a15e15f0098d549f07a01eaebf9ce3'))
paddle.fluid.layers.While ('paddle.fluid.layers.control_flow.While', ('document', '50110155608a00f43d3d3fd1be41dcb4'))
paddle.fluid.layers.While.__init__ (ArgSpec(args=['self', 'cond', 'is_test', 'name'], varargs=None, keywords=None, defaults=(False, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
...
...
paddle/fluid/operators/fill_any_like_op.cc
0 → 100644
浏览文件 @
d3b3443d
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
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/fluid/operators/fill_any_like_op.h"
namespace
paddle
{
namespace
operators
{
class
FillAnyLikeOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of FillAnyLikeOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of FillAnyLikeOp should not be null."
);
ctx
->
SetOutputDim
(
"Out"
,
ctx
->
GetInputDim
(
"X"
));
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
class
FillAnyLikeOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"X"
,
"The input of fill-zeros-like op."
);
AddOutput
(
"Out"
,
"The variable will be filled up with specified value."
);
AddAttr
<
float
>
(
"value"
,
"The filled value"
).
SetDefault
(
0.0
);
AddComment
(
R"DOC(
FillAnyLike Operator.
Fill up a variable with Attr(value).
The output will have the same shape and dtype as the input.
)DOC"
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_WITHOUT_GRADIENT
(
fill_any_like
,
ops
::
FillAnyLikeOp
,
ops
::
FillAnyLikeOpMaker
);
REGISTER_OP_CPU_KERNEL
(
fill_any_like
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CPUDeviceContext
,
paddle
::
platform
::
float16
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CPUDeviceContext
,
bool
>
);
paddle/fluid/operators/fill_any_like_op.cu
0 → 100644
浏览文件 @
d3b3443d
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
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/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/fill_any_like_op.h"
#include "paddle/fluid/platform/float16.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
fill_any_like
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int32_t
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CUDADeviceContext
,
paddle
::
platform
::
float16
>
,
ops
::
FillAnyLikeKernel
<
paddle
::
platform
::
CUDADeviceContext
,
bool
>
);
paddle/fluid/operators/fill_any_like_op.h
0 → 100644
浏览文件 @
d3b3443d
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
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 <cmath>
#include <limits>
#include <type_traits>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
T
>
class
FillAnyLikeKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
using
CommonType
=
typename
std
::
common_type
<
float
,
typename
std
::
conditional
<
std
::
is_same
<
T
,
platform
::
float16
>::
value
,
float
,
T
>::
type
>::
type
;
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
out
=
context
.
Output
<
framework
::
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
// TODO(fangzeyang): Once context.Attribute supports double dtype, this
// kernel should be updated to support double dtype, too.
float
value
=
context
.
Attr
<
float
>
(
"value"
);
auto
common_type_value
=
static_cast
<
CommonType
>
(
value
);
PADDLE_ENFORCE
(
(
common_type_value
>=
static_cast
<
CommonType
>
(
std
::
numeric_limits
<
T
>::
lowest
()))
&&
(
common_type_value
<=
static_cast
<
CommonType
>
(
std
::
numeric_limits
<
T
>::
max
())),
"filled value is out of range for targeted type in fill_any_like "
"kernel"
);
PADDLE_ENFORCE
(
!
std
::
isnan
(
value
),
"filled value is NaN"
);
math
::
SetConstant
<
DeviceContext
,
T
>
setter
;
setter
(
context
.
template
device_context
<
DeviceContext
>(),
out
,
static_cast
<
T
>
(
value
));
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/layers/tensor.py
浏览文件 @
d3b3443d
...
...
@@ -28,7 +28,7 @@ __all__ = [
'tensor_array_to_tensor'
,
'concat'
,
'sums'
,
'assign'
,
'fill_constant_batch_size_like'
,
'fill_constant'
,
'argmin'
,
'argmax'
,
'argsort'
,
'ones'
,
'zeros'
,
'reverse'
,
'has_inf'
,
'has_nan'
,
'isfinite'
,
'range'
,
'linspace'
,
'zeros_like'
,
'diag'
'range'
,
'linspace'
,
'zeros_like'
,
'
ones_like'
,
'
diag'
]
...
...
@@ -989,3 +989,38 @@ def diag(diagonal):
out
.
stop_gradient
=
True
return
out
def
ones_like
(
x
,
out
=
None
):
"""
**ones_like**
This function creates a ones tensor which has identical shape and dtype
with `x`.
Args:
x(Variable): The input tensor which specifies shape and dtype.
out(Variable): The output tensor.
Returns:
x(Variable): The tensor variable storing the output.
Examples:
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name='x', dtype='float32', shape=[3], append_batch_size=False)
data = fluid.layers.ones_like(x) # [1.0, 1.0, 1.0]
"""
helper
=
LayerHelper
(
"ones_like"
,
**
locals
())
if
out
is
None
:
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
helper
.
append_op
(
type
=
'fill_any_like'
,
inputs
=
{
'X'
:
[
x
]},
attrs
=
{
'value'
:
1.0
},
outputs
=
{
'Out'
:
[
out
]})
return
out
python/paddle/fluid/tests/unittests/test_fill_any_like_op.py
0 → 100644
浏览文件 @
d3b3443d
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
from
__future__
import
print_function
import
paddle.fluid.core
as
core
import
paddle.compat
as
cpt
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestFillAnyLikeOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"fill_any_like"
self
.
dtype
=
np
.
int32
self
.
value
=
0.0
self
.
init
()
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
219
,
232
)).
astype
(
self
.
dtype
)}
self
.
attrs
=
{
'value'
:
self
.
value
}
self
.
outputs
=
{
'Out'
:
self
.
value
*
np
.
ones_like
(
self
.
inputs
[
"X"
])}
def
init
(
self
):
pass
def
test_check_output
(
self
):
self
.
check_output
()
class
TestFillAnyLikeOpFloat32
(
TestFillAnyLikeOp
):
def
init
(
self
):
self
.
dtype
=
np
.
float32
self
.
value
=
0.0
class
TestFillAnyLikeOpValue1
(
TestFillAnyLikeOp
):
def
init
(
self
):
self
.
value
=
1.0
class
TestFillAnyLikeOpValue2
(
TestFillAnyLikeOp
):
def
init
(
self
):
self
.
value
=
1e-10
class
TestFillAnyLikeOpValue3
(
TestFillAnyLikeOp
):
def
init
(
self
):
self
.
value
=
1e-100
class
TestFillAnyLikeOpOverflow
(
TestFillAnyLikeOp
):
def
init
(
self
):
self
.
value
=
1e100
def
test_check_output
(
self
):
exception
=
None
try
:
self
.
check_output
()
except
core
.
EnforceNotMet
as
ex
:
exception
=
ex
self
.
assertIsNotNone
(
exception
)
class
TestFillAnyLikeOpFloat16
(
TestFillAnyLikeOp
):
def
init
(
self
):
self
.
dtype
=
np
.
float16
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录