Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0f165f0b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
0f165f0b
编写于
4月 04, 2022
作者:
F
From00
提交者:
GitHub
4月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add yaml for randint OP (#41375)
上级
bcb663cc
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
93 addition
and
10 deletion
+93
-10
paddle/phi/infermeta/nullary.cc
paddle/phi/infermeta/nullary.cc
+28
-0
paddle/phi/infermeta/nullary.h
paddle/phi/infermeta/nullary.h
+3
-0
python/paddle/fluid/tests/unittests/test_randint_op.py
python/paddle/fluid/tests/unittests/test_randint_op.py
+42
-5
python/paddle/tensor/random.py
python/paddle/tensor/random.py
+7
-4
python/paddle/utils/code_gen/api.yaml
python/paddle/utils/code_gen/api.yaml
+13
-1
未找到文件。
paddle/phi/infermeta/nullary.cc
浏览文件 @
0f165f0b
...
@@ -63,6 +63,34 @@ void RandpermInferMeta(int n, DataType dtype, MetaTensor* out) {
...
@@ -63,6 +63,34 @@ void RandpermInferMeta(int n, DataType dtype, MetaTensor* out) {
out
->
set_dtype
(
dtype
);
out
->
set_dtype
(
dtype
);
}
}
void
RandintInferMeta
(
int
low
,
int
high
,
const
IntArray
&
shape
,
DataType
dtype
,
MetaTensor
*
out
)
{
PADDLE_ENFORCE_NOT_NULL
(
out
,
errors
::
InvalidArgument
(
"Output(Out) of RandintOp is null."
));
PADDLE_ENFORCE_LT
(
low
,
high
,
errors
::
InvalidArgument
(
"randint's low must less then high, "
"but received: low = %d, high = %d."
,
low
,
high
));
auto
&
shape_vector
=
shape
.
GetData
();
PADDLE_ENFORCE_EQ
(
shape_vector
.
empty
(),
false
,
errors
::
InvalidArgument
(
"The shape information should not be empty, it "
"must be set by Attr(shape)."
));
std
::
vector
<
int64_t
>
tensor_shape
;
tensor_shape
.
reserve
(
shape_vector
.
size
());
for
(
auto
dim
:
shape_vector
)
{
tensor_shape
.
push_back
(
static_cast
<
int64_t
>
(
dim
));
}
out
->
set_dims
(
make_ddim
(
tensor_shape
));
out
->
set_dtype
(
dtype
);
}
void
TruncatedGaussianRandomInferMeta
(
const
std
::
vector
<
int
>&
shape
,
void
TruncatedGaussianRandomInferMeta
(
const
std
::
vector
<
int
>&
shape
,
float
mean
,
float
mean
,
float
std
,
float
std
,
...
...
paddle/phi/infermeta/nullary.h
浏览文件 @
0f165f0b
...
@@ -55,6 +55,9 @@ void GaussianRandomInferMeta(const IntArray& shape,
...
@@ -55,6 +55,9 @@ void GaussianRandomInferMeta(const IntArray& shape,
void
RandpermInferMeta
(
int
n
,
DataType
dtype
,
MetaTensor
*
out
);
void
RandpermInferMeta
(
int
n
,
DataType
dtype
,
MetaTensor
*
out
);
void
RandintInferMeta
(
int
low
,
int
high
,
const
IntArray
&
shape
,
DataType
dtype
,
MetaTensor
*
out
);
void
TruncatedGaussianRandomInferMeta
(
const
std
::
vector
<
int
>&
shape
,
void
TruncatedGaussianRandomInferMeta
(
const
std
::
vector
<
int
>&
shape
,
float
mean
,
float
mean
,
float
std
,
float
std
,
...
...
python/paddle/fluid/tests/unittests/test_randint_op.py
浏览文件 @
0f165f0b
...
@@ -14,13 +14,14 @@
...
@@ -14,13 +14,14 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
os
import
paddle
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
from
op_test
import
OpTest
from
op_test
import
OpTest
import
paddle
from
paddle.fluid
import
core
from
paddle.fluid
import
core
from
paddle.fluid.framework
import
_test_eager_guard
from
paddle.static
import
program_guard
,
Program
from
paddle.static
import
program_guard
,
Program
import
os
paddle
.
enable_static
()
paddle
.
enable_static
()
...
@@ -53,6 +54,10 @@ class TestRandintOp(OpTest):
...
@@ -53,6 +54,10 @@ class TestRandintOp(OpTest):
np
.
allclose
(
np
.
allclose
(
hist
,
prob
,
rtol
=
0
,
atol
=
0.001
),
"hist: "
+
str
(
hist
))
hist
,
prob
,
rtol
=
0
,
atol
=
0.001
),
"hist: "
+
str
(
hist
))
def
test_check_output_eager
(
self
):
with
_test_eager_guard
():
self
.
test_check_output
()
class
TestRandintOpError
(
unittest
.
TestCase
):
class
TestRandintOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
def
test_errors
(
self
):
...
@@ -67,6 +72,10 @@ class TestRandintOpError(unittest.TestCase):
...
@@ -67,6 +72,10 @@ class TestRandintOpError(unittest.TestCase):
self
.
assertRaises
(
self
.
assertRaises
(
TypeError
,
paddle
.
randint
,
5
,
shape
=
[
shape_tensor
])
TypeError
,
paddle
.
randint
,
5
,
shape
=
[
shape_tensor
])
def
test_errors_eager
(
self
):
with
_test_eager_guard
():
self
.
test_errors
()
class
TestRandintOp_attr_tensorlist
(
OpTest
):
class
TestRandintOp_attr_tensorlist
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -93,6 +102,10 @@ class TestRandintOp_attr_tensorlist(OpTest):
...
@@ -93,6 +102,10 @@ class TestRandintOp_attr_tensorlist(OpTest):
np
.
allclose
(
np
.
allclose
(
hist
,
prob
,
rtol
=
0
,
atol
=
0.001
),
"hist: "
+
str
(
hist
))
hist
,
prob
,
rtol
=
0
,
atol
=
0.001
),
"hist: "
+
str
(
hist
))
def
test_check_output_eager
(
self
):
with
_test_eager_guard
():
self
.
test_check_output
()
class
TestRandint_attr_tensor
(
OpTest
):
class
TestRandint_attr_tensor
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -114,6 +127,10 @@ class TestRandint_attr_tensor(OpTest):
...
@@ -114,6 +127,10 @@ class TestRandint_attr_tensor(OpTest):
np
.
allclose
(
np
.
allclose
(
hist
,
prob
,
rtol
=
0
,
atol
=
0.001
),
"hist: "
+
str
(
hist
))
hist
,
prob
,
rtol
=
0
,
atol
=
0.001
),
"hist: "
+
str
(
hist
))
def
test_check_output_eager
(
self
):
with
_test_eager_guard
():
self
.
test_check_output
()
# Test python API
# Test python API
class
TestRandintAPI
(
unittest
.
TestCase
):
class
TestRandintAPI
(
unittest
.
TestCase
):
...
@@ -145,18 +162,30 @@ class TestRandintAPI(unittest.TestCase):
...
@@ -145,18 +162,30 @@ class TestRandintAPI(unittest.TestCase):
feed
=
{
'var_shape'
:
np
.
array
([
100
,
100
]).
astype
(
'int64'
)},
feed
=
{
'var_shape'
:
np
.
array
([
100
,
100
]).
astype
(
'int64'
)},
fetch_list
=
[
out1
,
out2
,
out3
,
out4
,
out5
])
fetch_list
=
[
out1
,
out2
,
out3
,
out4
,
out5
])
def
test_api_eager
(
self
):
with
_test_eager_guard
():
self
.
test_api
()
class
TestRandintImperative
(
unittest
.
TestCase
):
class
TestRandintImperative
(
unittest
.
TestCase
):
def
test_api
(
self
):
def
test_api
(
self
):
n
=
10
paddle
.
disable_static
()
paddle
.
disable_static
()
self
.
run_test_case
()
with
_test_eager_guard
():
self
.
run_test_case
()
paddle
.
enable_static
()
def
run_test_case
(
self
):
n
=
10
x1
=
paddle
.
randint
(
n
,
shape
=
[
10
],
dtype
=
"int32"
)
x1
=
paddle
.
randint
(
n
,
shape
=
[
10
],
dtype
=
"int32"
)
x2
=
paddle
.
tensor
.
randint
(
n
)
x2
=
paddle
.
tensor
.
randint
(
n
)
x3
=
paddle
.
tensor
.
random
.
randint
(
n
)
x3
=
paddle
.
tensor
.
random
.
randint
(
n
)
for
i
in
[
x1
,
x2
,
x3
]:
for
i
in
[
x1
,
x2
,
x3
]:
for
j
in
i
.
numpy
().
tolist
():
for
j
in
i
.
numpy
().
tolist
():
self
.
assertTrue
((
j
>=
0
and
j
<
n
))
self
.
assertTrue
((
j
>=
0
and
j
<
n
))
paddle
.
enable_static
()
class
TestRandomValue
(
unittest
.
TestCase
):
class
TestRandomValue
(
unittest
.
TestCase
):
...
@@ -174,6 +203,15 @@ class TestRandomValue(unittest.TestCase):
...
@@ -174,6 +203,15 @@ class TestRandomValue(unittest.TestCase):
print
(
"Test Fixed Random number on GPU------>"
)
print
(
"Test Fixed Random number on GPU------>"
)
paddle
.
disable_static
()
paddle
.
disable_static
()
self
.
run_test_case
()
with
_test_eager_guard
():
self
.
run_test_case
()
paddle
.
enable_static
()
def
run_test_case
(
self
):
paddle
.
set_device
(
'gpu'
)
paddle
.
set_device
(
'gpu'
)
paddle
.
seed
(
100
)
paddle
.
seed
(
100
)
...
@@ -198,7 +236,6 @@ class TestRandomValue(unittest.TestCase):
...
@@ -198,7 +236,6 @@ class TestRandomValue(unittest.TestCase):
self
.
assertTrue
(
np
.
array_equal
(
x
[
20
,
1
,
600
,
600
:
605
],
expect
))
self
.
assertTrue
(
np
.
array_equal
(
x
[
20
,
1
,
600
,
600
:
605
],
expect
))
expect
=
[
3581
,
3420
,
-
8027
,
-
5237
,
-
2436
]
expect
=
[
3581
,
3420
,
-
8027
,
-
5237
,
-
2436
]
self
.
assertTrue
(
np
.
array_equal
(
x
[
30
,
2
,
1000
,
1000
:
1005
],
expect
))
self
.
assertTrue
(
np
.
array_equal
(
x
[
30
,
2
,
1000
,
1000
:
1005
],
expect
))
paddle
.
enable_static
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
python/paddle/tensor/random.py
浏览文件 @
0f165f0b
...
@@ -22,7 +22,7 @@ from ..fluid.layers import utils
...
@@ -22,7 +22,7 @@ from ..fluid.layers import utils
import
paddle
import
paddle
from
paddle
import
_C_ops
from
paddle
import
_C_ops
from
paddle.static
import
Variable
from
paddle.static
import
Variable
from
..fluid.framework
import
_in_legacy_dygraph
,
in_dygraph_mod
e
from
paddle.fluid.framework
import
in_dygraph_mode
,
_in_legacy_dygraph
,
_current_expected_plac
e
__all__
=
[]
__all__
=
[]
...
@@ -687,7 +687,11 @@ def randint(low=0, high=None, shape=[1], dtype=None, name=None):
...
@@ -687,7 +687,11 @@ def randint(low=0, high=None, shape=[1], dtype=None, name=None):
if
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
):
if
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
if
paddle
.
in_dynamic_mode
():
if
in_dygraph_mode
():
shape
=
utils
.
convert_shape_to_list
(
shape
)
place
=
_current_expected_place
()
return
_C_ops
.
final_state_randint
(
low
,
high
,
shape
,
dtype
,
place
)
if
_in_legacy_dygraph
():
shape
=
utils
.
convert_shape_to_list
(
shape
)
shape
=
utils
.
convert_shape_to_list
(
shape
)
return
_C_ops
.
randint
(
'shape'
,
shape
,
'low'
,
low
,
'high'
,
high
,
'seed'
,
return
_C_ops
.
randint
(
'shape'
,
shape
,
'low'
,
low
,
'high'
,
high
,
'seed'
,
0
,
'dtype'
,
dtype
)
0
,
'dtype'
,
dtype
)
...
@@ -920,8 +924,7 @@ def randperm(n, dtype="int64", name=None):
...
@@ -920,8 +924,7 @@ def randperm(n, dtype="int64", name=None):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
final_state_randperm
(
return
_C_ops
.
final_state_randperm
(
n
,
dtype
,
_current_expected_place
())
n
,
dtype
,
paddle
.
fluid
.
framework
.
_current_expected_place
())
if
_in_legacy_dygraph
():
if
_in_legacy_dygraph
():
return
_C_ops
.
randperm
(
'n'
,
n
,
'seed'
,
0
,
'dtype'
,
dtype
)
return
_C_ops
.
randperm
(
'n'
,
n
,
'seed'
,
0
,
'dtype'
,
dtype
)
...
...
python/paddle/utils/code_gen/api.yaml
浏览文件 @
0f165f0b
...
@@ -1265,6 +1265,18 @@
...
@@ -1265,6 +1265,18 @@
data_type
:
x
data_type
:
x
backward
:
put_along_axis_grad
backward
:
put_along_axis_grad
-
api
:
randint
args
:
(int low, int high, IntArray shape, DataType dtype=DataType::INT64, Place place={})
output
:
Tensor(out)
infer_meta
:
func
:
RandintInferMeta
param
:
[
low
,
high
,
shape
,
dtype
]
kernel
:
func
:
randint
param
:
[
low
,
high
,
shape
,
dtype
]
data_type
:
dtype
backend
:
place
-
api
:
randperm
-
api
:
randperm
args
:
(int n, DataType dtype, Place place={})
args
:
(int n, DataType dtype, Place place={})
output
:
Tensor
output
:
Tensor
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录