Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
cd2a4cdf
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
未验证
提交
cd2a4cdf
编写于
4月 11, 2022
作者:
X
xiongkun
提交者:
GitHub
4月 11, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Yaml] add yaml for Uniform random and add unit test. (#41517)
* gather op * add mod * [Yaml] final state for uniform and uniform_random
上级
9107dc67
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
64 addition
and
70 deletion
+64
-70
paddle/fluid/operators/uniform_random_op.cc
paddle/fluid/operators/uniform_random_op.cc
+7
-69
paddle/phi/infermeta/nullary.cc
paddle/phi/infermeta/nullary.cc
+12
-0
paddle/phi/infermeta/nullary.h
paddle/phi/infermeta/nullary.h
+7
-0
python/paddle/fluid/tests/unittests/test_uniform_random_op.py
...on/paddle/fluid/tests/unittests/test_uniform_random_op.py
+18
-0
python/paddle/tensor/random.py
python/paddle/tensor/random.py
+8
-1
python/paddle/utils/code_gen/api.yaml
python/paddle/utils/code_gen/api.yaml
+12
-0
未找到文件。
paddle/fluid/operators/uniform_random_op.cc
浏览文件 @
cd2a4cdf
...
...
@@ -16,9 +16,11 @@ limitations under the License. */
#include <string>
#include "paddle/fluid/framework/generator.h"
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/bfloat16.h"
#include "paddle/phi/infermeta/nullary.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -122,74 +124,6 @@ class UniformRandomOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"UniformRandomOp"
);
PADDLE_ENFORCE_LT
(
ctx
->
Attrs
().
Get
<
float
>
(
"min"
),
ctx
->
Attrs
().
Get
<
float
>
(
"max"
),
platform
::
errors
::
InvalidArgument
(
"The uniform_random's min must less then max. But received min = "
"%f great than or equal max = %f."
,
ctx
->
Attrs
().
Get
<
float
>
(
"min"
),
ctx
->
Attrs
().
Get
<
float
>
(
"max"
)));
PADDLE_ENFORCE_GE
(
ctx
->
Attrs
().
Get
<
int
>
(
"diag_num"
),
0
,
platform
::
errors
::
InvalidArgument
(
"The uniform_random's diag_num must greater than or "
"equal 0. But recevied diag_num (%d) < 0."
,
ctx
->
Attrs
().
Get
<
int
>
(
"diag_num"
)));
PADDLE_ENFORCE_GE
(
ctx
->
Attrs
().
Get
<
int
>
(
"diag_step"
),
0
,
platform
::
errors
::
InvalidArgument
(
"The uniform_random's diag_step must greater than or "
"equal 0. But recevied diag_step (%d) < 0."
,
ctx
->
Attrs
().
Get
<
int
>
(
"diag_step"
)));
if
(
ctx
->
HasInputs
(
"ShapeTensorList"
))
{
// top prority shape
auto
inputs_name
=
ctx
->
Inputs
(
"ShapeTensorList"
);
PADDLE_ENFORCE_GT
(
inputs_name
.
size
(),
0
,
platform
::
errors
::
InvalidArgument
(
"Input(ShapeTensorList)'size of "
"Op(uniform_random) can't be zero."
"Please check the Attr(shape)'s size of"
"Op(fluid.layers.uniform_random).)"
));
auto
out_dims
=
std
::
vector
<
int
>
(
inputs_name
.
size
(),
-
1
);
ctx
->
SetOutputDim
(
"Out"
,
phi
::
make_ddim
(
out_dims
));
return
;
}
auto
&
shape
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int64_t
>>
(
"shape"
);
if
(
ctx
->
HasInput
(
"ShapeTensor"
)
&&
shape
.
empty
())
{
auto
shape_dims
=
ctx
->
GetInputDim
(
"ShapeTensor"
);
PADDLE_ENFORCE_EQ
(
shape_dims
.
size
(),
1
,
platform
::
errors
::
InvalidArgument
(
"ShapeError: Input(ShapeTensor)' dimension size of "
"Op(uniform_random) must be 1."
"But received ShapeTensor's dimensions = %d, shape = [%s]"
,
shape_dims
.
size
(),
shape_dims
));
int
num_ele
=
1
;
for
(
int
i
=
0
;
i
<
shape_dims
.
size
();
++
i
)
{
num_ele
*=
shape_dims
[
i
];
}
auto
vec_dims
=
std
::
vector
<
int64_t
>
(
num_ele
,
-
1
);
auto
out_dims
=
phi
::
make_ddim
(
vec_dims
);
ctx
->
SetOutputDim
(
"Out"
,
out_dims
);
return
;
}
PADDLE_ENFORCE_EQ
(
shape
.
empty
(),
false
,
platform
::
errors
::
InvalidArgument
(
"if there is no Input(ShapeTensorList) and no "
"Input(ShapeTensor),the "
"attr(shape) information must "
"be set by Attr(shape)."
));
std
::
vector
<
int64_t
>
tensor_shape
;
tensor_shape
.
reserve
(
shape
.
size
());
for
(
auto
dim
:
shape
)
{
tensor_shape
.
push_back
(
static_cast
<
int64_t
>
(
dim
));
}
ctx
->
SetOutputDim
(
"Out"
,
phi
::
make_ddim
(
tensor_shape
));
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -274,12 +208,16 @@ class UniformRandomOpVarTypeInference : public framework::VarTypeInference {
}
// namespace operators
}
// namespace paddle
DECLARE_INFER_SHAPE_FUNCTOR
(
uniform_random
,
UniformRandomInferShapeFunctor
,
PD_INFER_META
(
phi
::
UniformRandomInferMeta
));
REGISTER_OPERATOR
(
uniform_random
,
paddle
::
operators
::
UniformRandomOp
,
paddle
::
operators
::
UniformRandomOpMaker
,
paddle
::
framework
::
EmptyGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
paddle
::
framework
::
EmptyGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
paddle
::
operators
::
UniformRandomOpVarTypeInference
);
paddle
::
operators
::
UniformRandomOpVarTypeInference
,
UniformRandomInferShapeFunctor
);
REGISTER_OP_CPU_KERNEL
(
uniform_random_batch_size_like
,
...
...
paddle/phi/infermeta/nullary.cc
浏览文件 @
cd2a4cdf
...
...
@@ -63,6 +63,18 @@ void RandpermInferMeta(int n, DataType dtype, MetaTensor* out) {
out
->
set_dtype
(
dtype
);
}
void
UniformRandomInferMeta
(
const
IntArray
&
shape
,
DataType
dtype
,
float
min
,
float
max
,
int
seed
,
MetaTensor
*
out
)
{
auto
out_dims
=
phi
::
make_ddim
(
shape
.
GetData
());
out
->
set_dims
(
out_dims
);
out
->
set_dtype
(
dtype
);
out
->
set_layout
(
DataLayout
::
NCHW
);
}
void
RandintInferMeta
(
int
low
,
int
high
,
const
IntArray
&
shape
,
DataType
dtype
,
MetaTensor
*
out
)
{
PADDLE_ENFORCE_NOT_NULL
(
...
...
paddle/phi/infermeta/nullary.h
浏览文件 @
cd2a4cdf
...
...
@@ -65,4 +65,11 @@ void TruncatedGaussianRandomInferMeta(const std::vector<int>& shape,
DataType
dtype
,
MetaTensor
*
out
);
void
UniformRandomInferMeta
(
const
IntArray
&
shape
,
DataType
dtype
,
float
min
,
float
max
,
int
seed
,
MetaTensor
*
out
);
}
// namespace phi
python/paddle/fluid/tests/unittests/test_uniform_random_op.py
浏览文件 @
cd2a4cdf
...
...
@@ -26,6 +26,7 @@ import paddle
from
paddle.fluid.op
import
Operator
import
paddle.fluid
as
fluid
from
paddle.fluid
import
Program
,
program_guard
from
paddle.fluid.framework
import
_test_eager_guard
def
output_hist
(
out
):
...
...
@@ -52,6 +53,7 @@ def output_hist_diag(out):
class
TestUniformRandomOp_attr_tensorlist
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"uniform_random"
self
.
python_api
=
paddle
.
uniform
self
.
new_shape
=
(
1000
,
784
)
shape_tensor
=
[]
for
index
,
ele
in
enumerate
(
self
.
new_shape
):
...
...
@@ -84,6 +86,7 @@ class TestMaxMinAreInt(TestUniformRandomOp_attr_tensorlist):
class
TestUniformRandomOp_attr_tensorlist_int32
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"uniform_random"
self
.
python_api
=
paddle
.
uniform
self
.
new_shape
=
(
1000
,
784
)
shape_tensor
=
[]
for
index
,
ele
in
enumerate
(
self
.
new_shape
):
...
...
@@ -110,6 +113,7 @@ class TestUniformRandomOp_attr_tensorlist_int32(OpTest):
class
TestUniformRandomOp_attr_tensor
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"uniform_random"
self
.
python_api
=
paddle
.
uniform
self
.
inputs
=
{
"ShapeTensor"
:
np
.
array
([
1000
,
784
]).
astype
(
"int64"
)}
self
.
init_attrs
()
self
.
outputs
=
{
"Out"
:
np
.
zeros
((
1000
,
784
)).
astype
(
"float32"
)}
...
...
@@ -131,6 +135,7 @@ class TestUniformRandomOp_attr_tensor(OpTest):
class
TestUniformRandomOp_attr_tensor_int32
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"uniform_random"
self
.
python_api
=
paddle
.
uniform
self
.
inputs
=
{
"ShapeTensor"
:
np
.
array
([
1000
,
784
]).
astype
(
"int32"
)}
self
.
init_attrs
()
self
.
outputs
=
{
"Out"
:
np
.
zeros
((
1000
,
784
)).
astype
(
"float32"
)}
...
...
@@ -152,6 +157,7 @@ class TestUniformRandomOp_attr_tensor_int32(OpTest):
class
TestUniformRandomOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"uniform_random"
self
.
python_api
=
paddle
.
uniform
self
.
inputs
=
{}
self
.
init_attrs
()
self
.
outputs
=
{
"Out"
:
np
.
zeros
((
1000
,
784
)).
astype
(
"float32"
)}
...
...
@@ -174,6 +180,18 @@ class TestUniformRandomOp(OpTest):
np
.
allclose
(
hist
,
prob
,
rtol
=
0
,
atol
=
0.01
),
"hist: "
+
str
(
hist
))
def
test_check_api
(
self
):
places
=
self
.
_get_places
()
for
place
in
places
:
with
fluid
.
dygraph
.
base
.
guard
(
place
=
place
):
out
=
self
.
python_api
(
self
.
attrs
[
'shape'
],
'float32'
,
self
.
attrs
[
'min'
],
self
.
attrs
[
'max'
],
self
.
attrs
[
'seed'
])
def
test_check_api_eager
(
self
):
with
_test_eager_guard
():
self
.
test_check_api
()
class
TestUniformRandomOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
...
...
python/paddle/tensor/random.py
浏览文件 @
cd2a4cdf
...
...
@@ -548,7 +548,14 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None):
if
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
if
paddle
.
in_dynamic_mode
():
if
in_dygraph_mode
():
shape
=
utils
.
convert_shape_to_list
(
shape
)
return
_C_ops
.
final_state_uniform_random
(
shape
,
dtype
,
float
(
min
),
float
(
max
),
seed
,
_current_expected_place
())
if
_in_legacy_dygraph
():
shape
=
utils
.
convert_shape_to_list
(
shape
)
return
_C_ops
.
uniform_random
(
'shape'
,
shape
,
'min'
,
float
(
min
),
'max'
,
...
...
python/paddle/utils/code_gen/api.yaml
浏览文件 @
cd2a4cdf
...
...
@@ -2035,6 +2035,18 @@
func
:
unfold
backward
:
unfold_grad
-
api
:
uniform_random
args
:
(IntArray shape, DataType dtype, float min, float max, int seed, Place place={})
output
:
Tensor(out)
infer_meta
:
func
:
UniformRandomInferMeta
param
:
[
shape
,
dtype
,
min
,
max
,
seed
]
kernel
:
func
:
uniform_random
param
:
[
shape
,
dtype
,
min
,
max
,
seed
]
data_type
:
dtype
backend
:
place
# The `axis` argument of Python API paddle.unique is not vector
-
api
:
unique
args
:
(Tensor x, bool return_index, bool return_inverse, bool return_counts, int[] axis, DataType dtype=DataType::INT64)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录