Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
c8955d0d
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看板
未验证
提交
c8955d0d
编写于
8月 25, 2022
作者:
A
Aurelius84
提交者:
GitHub
8月 25, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[OpAttr]min/max of uniform_random support Tensor type (#45417)
* [OpAttr]min/max of Uniform_rand support Tensor type * fix typo
上级
4d78390e
变更
12
显示空白变更内容
内联
并排
Showing
12 changed file
with
83 addition
and
34 deletion
+83
-34
paddle/fluid/operators/uniform_random_op.cc
paddle/fluid/operators/uniform_random_op.cc
+4
-2
paddle/phi/api/yaml/legacy_api.yaml
paddle/phi/api/yaml/legacy_api.yaml
+2
-2
paddle/phi/infermeta/nullary.cc
paddle/phi/infermeta/nullary.cc
+0
-3
paddle/phi/infermeta/nullary.h
paddle/phi/infermeta/nullary.h
+0
-3
paddle/phi/kernels/cpu/uniform_random_kernel.cc
paddle/phi/kernels/cpu/uniform_random_kernel.cc
+6
-5
paddle/phi/kernels/gpu/uniform_random_kernel.cu
paddle/phi/kernels/gpu/uniform_random_kernel.cu
+7
-7
paddle/phi/kernels/selected_rows/uniform_random_kernel.cc
paddle/phi/kernels/selected_rows/uniform_random_kernel.cc
+4
-4
paddle/phi/kernels/selected_rows/uniform_random_kernel.h
paddle/phi/kernels/selected_rows/uniform_random_kernel.h
+5
-4
paddle/phi/kernels/uniform_random_kernel.h
paddle/phi/kernels/uniform_random_kernel.h
+5
-4
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+2
-0
python/paddle/fluid/tests/unittests/test_uniform_random_op.py
...on/paddle/fluid/tests/unittests/test_uniform_random_op.py
+46
-0
python/paddle/tensor/random.py
python/paddle/tensor/random.py
+2
-0
未找到文件。
paddle/fluid/operators/uniform_random_op.cc
浏览文件 @
c8955d0d
...
@@ -182,9 +182,11 @@ uniform distribution. The random result is in set [min, max).
...
@@ -182,9 +182,11 @@ uniform distribution. The random result is in set [min, max).
AddAttr
<
std
::
vector
<
int64_t
>>
(
"shape"
,
"The shape of the output tensor"
)
AddAttr
<
std
::
vector
<
int64_t
>>
(
"shape"
,
"The shape of the output tensor"
)
.
SetDefault
({});
.
SetDefault
({});
AddAttr
<
float
>
(
"min"
,
"Minimum value of uniform random. [default -1.0]."
)
AddAttr
<
float
>
(
"min"
,
"Minimum value of uniform random. [default -1.0]."
)
.
SetDefault
(
-
1.0
f
);
.
SetDefault
(
-
1.0
f
)
.
SupportTensor
();
AddAttr
<
float
>
(
"max"
,
"Maximun value of uniform random. [default 1.0]."
)
AddAttr
<
float
>
(
"max"
,
"Maximun value of uniform random. [default 1.0]."
)
.
SetDefault
(
1.0
f
);
.
SetDefault
(
1.0
f
)
.
SupportTensor
();
AddAttr
<
int
>
(
"seed"
,
AddAttr
<
int
>
(
"seed"
,
"Random seed used for generating samples. "
"Random seed used for generating samples. "
"0 means use a seed generated by the system."
"0 means use a seed generated by the system."
...
...
paddle/phi/api/yaml/legacy_api.yaml
浏览文件 @
c8955d0d
...
@@ -2754,11 +2754,11 @@
...
@@ -2754,11 +2754,11 @@
backward
:
unfold_grad
backward
:
unfold_grad
-
api
:
uniform_random
-
api
:
uniform_random
args
:
(IntArray shape, DataType dtype,
float min, float
max, int seed, Place place={})
args
:
(IntArray shape, DataType dtype,
Scalar min, Scalar
max, int seed, Place place={})
output
:
Tensor(out)
output
:
Tensor(out)
infer_meta
:
infer_meta
:
func
:
UniformRandomInferMeta
func
:
UniformRandomInferMeta
param
:
[
shape
,
dtype
,
min
,
max
,
seed
]
param
:
[
shape
,
dtype
]
kernel
:
kernel
:
func
:
uniform_random
func
:
uniform_random
param
:
[
shape
,
dtype
,
min
,
max
,
seed
]
param
:
[
shape
,
dtype
,
min
,
max
,
seed
]
...
...
paddle/phi/infermeta/nullary.cc
浏览文件 @
c8955d0d
...
@@ -79,9 +79,6 @@ void RandpermInferMeta(int n, DataType dtype, MetaTensor* out) {
...
@@ -79,9 +79,6 @@ void RandpermInferMeta(int n, DataType dtype, MetaTensor* out) {
void
UniformRandomInferMeta
(
const
IntArray
&
shape
,
void
UniformRandomInferMeta
(
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
float
max
,
int
seed
,
MetaTensor
*
out
)
{
MetaTensor
*
out
)
{
auto
out_dims
=
phi
::
make_ddim
(
shape
.
GetData
());
auto
out_dims
=
phi
::
make_ddim
(
shape
.
GetData
());
out
->
set_dims
(
out_dims
);
out
->
set_dims
(
out_dims
);
...
...
paddle/phi/infermeta/nullary.h
浏览文件 @
c8955d0d
...
@@ -67,9 +67,6 @@ void TruncatedGaussianRandomInferMeta(const std::vector<int>& shape,
...
@@ -67,9 +67,6 @@ void TruncatedGaussianRandomInferMeta(const std::vector<int>& shape,
void
UniformRandomInferMeta
(
const
IntArray
&
shape
,
void
UniformRandomInferMeta
(
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
float
max
,
int
seed
,
MetaTensor
*
out
);
MetaTensor
*
out
);
void
TrilIndicesInferMeta
(
void
TrilIndicesInferMeta
(
...
...
paddle/phi/kernels/cpu/uniform_random_kernel.cc
浏览文件 @
c8955d0d
...
@@ -47,8 +47,8 @@ template <typename T, typename Context>
...
@@ -47,8 +47,8 @@ template <typename T, typename Context>
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
int
diag_num
,
int
diag_num
,
int
diag_step
,
int
diag_step
,
...
@@ -64,7 +64,8 @@ void UniformRandomRawKernel(const Context &dev_ctx,
...
@@ -64,7 +64,8 @@ void UniformRandomRawKernel(const Context &dev_ctx,
}
else
{
}
else
{
engine
=
dev_ctx
.
GetGenerator
()
->
GetCPUEngine
();
engine
=
dev_ctx
.
GetGenerator
()
->
GetCPUEngine
();
}
}
UniformRealDistribution
<
T
>
(
data
,
size
,
min
,
max
,
engine
);
UniformRealDistribution
<
T
>
(
data
,
size
,
min
.
to
<
float
>
(),
max
.
to
<
float
>
(),
engine
);
if
(
diag_num
>
0
)
{
if
(
diag_num
>
0
)
{
PADDLE_ENFORCE_GT
(
PADDLE_ENFORCE_GT
(
size
,
size
,
...
@@ -88,8 +89,8 @@ template <typename T, typename Context>
...
@@ -88,8 +89,8 @@ template <typename T, typename Context>
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
DenseTensor
*
out
)
{
DenseTensor
*
out
)
{
UniformRandomRawKernel
<
T
>
(
UniformRandomRawKernel
<
T
>
(
...
...
paddle/phi/kernels/gpu/uniform_random_kernel.cu
浏览文件 @
c8955d0d
...
@@ -57,8 +57,8 @@ template <typename T, typename Context>
...
@@ -57,8 +57,8 @@ template <typename T, typename Context>
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
int
diag_num
,
int
diag_num
,
int
diag_step
,
int
diag_step
,
...
@@ -70,12 +70,12 @@ void UniformRandomRawKernel(const Context& dev_ctx,
...
@@ -70,12 +70,12 @@ void UniformRandomRawKernel(const Context& dev_ctx,
// Use global Generator seed
// Use global Generator seed
using
MT
=
typename
kps
::
details
::
MPTypeTrait
<
T
>::
Type
;
using
MT
=
typename
kps
::
details
::
MPTypeTrait
<
T
>::
Type
;
funcs
::
uniform_distribution
<
MT
>
dist
;
funcs
::
uniform_distribution
<
MT
>
dist
;
funcs
::
uniform_real_transform
<
MT
>
trans
(
min
,
max
);
funcs
::
uniform_real_transform
<
MT
>
trans
(
min
.
to
<
float
>
(),
max
.
to
<
float
>
()
);
funcs
::
distribution_and_transform
<
T
>
(
dev_ctx
,
out
,
dist
,
trans
);
funcs
::
distribution_and_transform
<
T
>
(
dev_ctx
,
out
,
dist
,
trans
);
}
else
{
}
else
{
// Use OP seed
// Use OP seed
auto
func
=
auto
func
=
UniformGenerator
<
T
>
(
UniformGenerator
<
T
>
(
min
,
max
,
seed
,
diag_num
,
diag_step
,
diag_val
);
min
.
to
<
float
>
(),
max
.
to
<
float
>
()
,
seed
,
diag_num
,
diag_step
,
diag_val
);
IndexKernel
<
T
,
UniformGenerator
<
T
>>
(
dev_ctx
,
out
,
func
);
IndexKernel
<
T
,
UniformGenerator
<
T
>>
(
dev_ctx
,
out
,
func
);
}
}
}
}
...
@@ -84,8 +84,8 @@ template <typename T, typename Context>
...
@@ -84,8 +84,8 @@ template <typename T, typename Context>
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
DenseTensor
*
out
)
{
DenseTensor
*
out
)
{
UniformRandomRawKernel
<
T
>
(
UniformRandomRawKernel
<
T
>
(
...
...
paddle/phi/kernels/selected_rows/uniform_random_kernel.cc
浏览文件 @
c8955d0d
...
@@ -26,8 +26,8 @@ template <typename T, typename Context>
...
@@ -26,8 +26,8 @@ template <typename T, typename Context>
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
int
diag_num
,
int
diag_num
,
int
diag_step
,
int
diag_step
,
...
@@ -49,8 +49,8 @@ template <typename T, typename Context>
...
@@ -49,8 +49,8 @@ template <typename T, typename Context>
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
SelectedRows
*
out
)
{
SelectedRows
*
out
)
{
phi
::
UniformRandomKernel
<
T
>
(
phi
::
UniformRandomKernel
<
T
>
(
...
...
paddle/phi/kernels/selected_rows/uniform_random_kernel.h
浏览文件 @
c8955d0d
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
#pragma once
#pragma once
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/selected_rows.h"
#include "paddle/phi/core/selected_rows.h"
namespace
phi
{
namespace
phi
{
...
@@ -24,8 +25,8 @@ template <typename T, typename Context>
...
@@ -24,8 +25,8 @@ template <typename T, typename Context>
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
int
diag_num
,
int
diag_num
,
int
diag_step
,
int
diag_step
,
...
@@ -36,8 +37,8 @@ template <typename T, typename Context>
...
@@ -36,8 +37,8 @@ template <typename T, typename Context>
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
SelectedRows
*
out
);
SelectedRows
*
out
);
...
...
paddle/phi/kernels/uniform_random_kernel.h
浏览文件 @
c8955d0d
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
#pragma once
#pragma once
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/core/device_context.h"
...
@@ -24,8 +25,8 @@ template <typename T, typename Context>
...
@@ -24,8 +25,8 @@ template <typename T, typename Context>
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomRawKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
int
diag_num
,
int
diag_num
,
int
diag_step
,
int
diag_step
,
...
@@ -36,8 +37,8 @@ template <typename T, typename Context>
...
@@ -36,8 +37,8 @@ template <typename T, typename Context>
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
void
UniformRandomKernel
(
const
Context
&
dev_ctx
,
const
IntArray
&
shape
,
const
IntArray
&
shape
,
DataType
dtype
,
DataType
dtype
,
float
min
,
const
Scalar
&
min
,
float
max
,
const
Scalar
&
max
,
int
seed
,
int
seed
,
DenseTensor
*
out
);
DenseTensor
*
out
);
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
c8955d0d
...
@@ -15767,6 +15767,8 @@ def uniform_random(shape,
...
@@ -15767,6 +15767,8 @@ def uniform_random(shape,
check_type(shape, 'shape', (list, tuple, Variable), 'uniform_random/rand')
check_type(shape, 'shape', (list, tuple, Variable), 'uniform_random/rand')
check_dtype(dtype, 'dtype', ('float32', 'float64', 'uint16'),
check_dtype(dtype, 'dtype', ('float32', 'float64', 'uint16'),
'uniform_random/rand')
'uniform_random/rand')
check_type(min, 'min', (float, int, Variable), 'uniform_random/rand')
check_type(max, 'max', (float, int, Variable), 'uniform_random/rand')
inputs = dict()
inputs = dict()
attrs = {'seed': seed, 'min': min, 'max': max, 'dtype': dtype}
attrs = {'seed': seed, 'min': min, 'max': max, 'dtype': dtype}
...
...
python/paddle/fluid/tests/unittests/test_uniform_random_op.py
浏览文件 @
c8955d0d
...
@@ -28,6 +28,8 @@ import paddle.fluid as fluid
...
@@ -28,6 +28,8 @@ import paddle.fluid as fluid
from
paddle.fluid
import
Program
,
program_guard
from
paddle.fluid
import
Program
,
program_guard
from
paddle.fluid.framework
import
_test_eager_guard
from
paddle.fluid.framework
import
_test_eager_guard
from
test_attribute_var
import
UnittestBase
def
output_hist
(
out
):
def
output_hist
(
out
):
hist
,
_
=
np
.
histogram
(
out
,
range
=
(
-
5
,
10
))
hist
,
_
=
np
.
histogram
(
out
,
range
=
(
-
5
,
10
))
...
@@ -648,5 +650,49 @@ class TestRandomValue(unittest.TestCase):
...
@@ -648,5 +650,49 @@ class TestRandomValue(unittest.TestCase):
paddle
.
enable_static
()
paddle
.
enable_static
()
class
TestUniformMinMaxTensor
(
UnittestBase
):
def
init_info
(
self
):
self
.
shapes
=
[[
2
,
3
,
4
]]
self
.
save_path
=
os
.
path
.
join
(
self
.
temp_dir
.
name
,
self
.
path_prefix
())
def
test_static
(
self
):
main_prog
=
Program
()
starup_prog
=
Program
()
with
program_guard
(
main_prog
,
starup_prog
):
fc
=
paddle
.
nn
.
Linear
(
4
,
10
)
x
=
paddle
.
randn
([
2
,
3
,
4
])
x
.
stop_gradient
=
False
feat
=
fc
(
x
)
# [2,3,10]
min_v
=
paddle
.
to_tensor
([
0.1
])
max_v
=
paddle
.
to_tensor
([
0.9
])
y
=
paddle
.
uniform
([
2
,
3
,
10
],
min
=
min_v
,
max
=
max_v
)
z
=
paddle
.
fluid
.
layers
.
uniform_random
([
2
,
3
,
10
],
min
=
min_v
,
max
=
max_v
)
out
=
feat
+
y
+
z
sgd
=
paddle
.
optimizer
.
SGD
()
sgd
.
minimize
(
paddle
.
mean
(
out
))
self
.
assertTrue
(
self
.
var_prefix
()
in
str
(
main_prog
))
exe
=
paddle
.
static
.
Executor
()
exe
.
run
(
starup_prog
)
res
=
exe
.
run
(
fetch_list
=
[
out
])
np
.
testing
.
assert_array_equal
(
res
[
0
].
shape
,
[
2
,
3
,
10
])
paddle
.
static
.
save_inference_model
(
self
.
save_path
,
[
x
],
[
out
],
exe
)
# Test for Inference Predictor
infer_out
=
self
.
infer_prog
()
np
.
testing
.
assert_array_equal
(
res
[
0
].
shape
,
[
2
,
3
,
10
])
def
path_prefix
(
self
):
return
'uniform_random'
def
var_prefix
(
self
):
return
"Var["
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
python/paddle/tensor/random.py
浏览文件 @
c8955d0d
...
@@ -568,6 +568,8 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None):
...
@@ -568,6 +568,8 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None):
check_type
(
shape
,
'shape'
,
(
list
,
tuple
,
Variable
),
'uniform/rand'
)
check_type
(
shape
,
'shape'
,
(
list
,
tuple
,
Variable
),
'uniform/rand'
)
check_dtype
(
dtype
,
'dtype'
,
(
'float32'
,
'float64'
),
'uniform/rand'
)
check_dtype
(
dtype
,
'dtype'
,
(
'float32'
,
'float64'
),
'uniform/rand'
)
check_type
(
min
,
'min'
,
(
float
,
int
,
Variable
),
'uniform/rand'
)
check_type
(
max
,
'max'
,
(
float
,
int
,
Variable
),
'uniform/rand'
)
inputs
=
dict
()
inputs
=
dict
()
attrs
=
{
'seed'
:
seed
,
'min'
:
min
,
'max'
:
max
,
'dtype'
:
dtype
}
attrs
=
{
'seed'
:
seed
,
'min'
:
min
,
'max'
:
max
,
'dtype'
:
dtype
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录