Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PaddlePaddle hackathon] Add randint_like #36169

Merged
merged 22 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
from .tensor.random import randn # noqa: F401
from .tensor.random import rand # noqa: F401
from .tensor.random import randint # noqa: F401
from .tensor.random import randint_like # noqa: F401
JunnYu marked this conversation as resolved.
Show resolved Hide resolved
from .tensor.random import randperm # noqa: F401
from .tensor.search import argmax # noqa: F401
from .tensor.search import argmin # noqa: F401
Expand Down Expand Up @@ -373,6 +374,7 @@
'ParamAttr',
'stanh',
'randint',
'randint_like',
'assign',
'gather',
'scale',
Expand Down
199 changes: 199 additions & 0 deletions python/paddle/fluid/tests/unittests/test_randint_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
# Copyright (c) 2021 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 unittest
import numpy as np
import paddle
from paddle.static import program_guard, Program


# Test python API
class TestRandintLikeAPI(unittest.TestCase):
def setUp(self):
self.x_bool = np.zeros((10, 12)).astype("bool")
JunnYu marked this conversation as resolved.
Show resolved Hide resolved
self.x_int32 = np.zeros((10, 12)).astype("int32")
self.x_int64 = np.zeros((10, 12)).astype("int64")
self.x_float16 = np.zeros((10, 12)).astype("float16")
self.x_float32 = np.zeros((10, 12)).astype("float32")
self.x_float64 = np.zeros((10, 12)).astype("float64")

self.dtype = ["bool", "int32", "int64", "float16", "float32", "float64"]
self.place=paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() \
else paddle.CPUPlace()

def test_static_api(self):
paddle.enable_static()
with program_guard(Program(), Program()):
# results are from [-100, 100).
x_bool = paddle.fluid.data(
name="x_bool", shape=[10, 12], dtype="bool")
x_int32 = paddle.fluid.data(
name="x_int32", shape=[10, 12], dtype="int32")
x_int64 = paddle.fluid.data(
name="x_int64", shape=[10, 12], dtype="int64")
x_float16 = paddle.fluid.data(
name="x_float16", shape=[10, 12], dtype="float16")
x_float32 = paddle.fluid.data(
name="x_float32", shape=[10, 12], dtype="float32")
x_float64 = paddle.fluid.data(
name="x_float64", shape=[10, 12], dtype="float64")

exe = paddle.static.Executor(self.place)

# x dtype is bool output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist1 = [
paddle.randint_like(
x_bool, low=-100, high=100, dtype=dtype)
for dtype in self.dtype
]
outs1 = exe.run(feed={'x_bool': self.x_bool}, fetch_list=outlist1)

# x dtype is int32 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist2 = [
paddle.randint_like(
x_int32, low=-100, high=100, dtype=dtype)
for dtype in self.dtype
]
outs2 = exe.run(feed={'x_int32': self.x_int32}, fetch_list=outlist2)

# x dtype is int64 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist3 = [
paddle.randint_like(
x_int64, low=-100, high=100, dtype=dtype)
for dtype in self.dtype
]
outs3 = exe.run(feed={'x_int64': self.x_int64}, fetch_list=outlist3)

# x dtype is float16 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist4 = [
paddle.randint_like(
x_float16, low=-100, high=100, dtype=dtype)
for dtype in self.dtype
]
outs4 = exe.run(feed={'x_float16': self.x_float16},
fetch_list=outlist4)

# x dtype is float32 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist5 = [
paddle.randint_like(
x_float32, low=-100, high=100, dtype=dtype)
for dtype in self.dtype
]
outs5 = exe.run(feed={'x_float32': self.x_float32},
JunnYu marked this conversation as resolved.
Show resolved Hide resolved
fetch_list=outlist5)

# x dtype is float64 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist6 = [
paddle.randint_like(
x_float64, low=-100, high=100, dtype=dtype)
for dtype in self.dtype
]
outs6 = exe.run(feed={'x_float64': self.x_float64},
fetch_list=outlist6)

def test_dygraph_api(self):
paddle.disable_static(self.place)
# x dtype ["bool", "int32", "int64", "float16", "float32", "float64"]
for x in [
self.x_bool, self.x_int32, self.x_int64, self.x_float16,
self.x_float32, self.x_float64
]:
x_inputs = paddle.to_tensor(x)
# self.dtype ["bool", "int32", "int64", "float16", "float32", "float64"]
for dtype in self.dtype:
out = paddle.randint_like(
x_inputs, low=-100, high=100, dtype=dtype)
paddle.enable_static()

def test_errors(self):
paddle.enable_static()
with program_guard(Program(), Program()):
x_bool = paddle.fluid.data(
name="x_bool", shape=[10, 12], dtype="bool")
x_int32 = paddle.fluid.data(
name="x_int32", shape=[10, 12], dtype="int32")
x_int64 = paddle.fluid.data(
name="x_int64", shape=[10, 12], dtype="int64")
x_float16 = paddle.fluid.data(
name="x_float16", shape=[10, 12], dtype="float16")
x_float32 = paddle.fluid.data(
name="x_float32", shape=[10, 12], dtype="float32")
x_float64 = paddle.fluid.data(
name="x_float64", shape=[10, 12], dtype="float64")

# x dtype is bool
# low is 5 and high is 5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_bool, low=5, high=5)
# low(default value) is 0 and high is -5, low must less then high
self.assertRaises(ValueError, paddle.randint_like, x_bool, high=-5)
# if high is None, low must be greater than 0
self.assertRaises(ValueError, paddle.randint_like, x_bool, low=-5)

# x dtype is int32
# low is 5 and high is 5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_int32, low=5, high=5)
# low(default value) is 0 and high is -5, low must less then high
self.assertRaises(ValueError, paddle.randint_like, x_int32, high=-5)
# if high is None, low must be greater than 0
self.assertRaises(ValueError, paddle.randint_like, x_int32, low=-5)

# x dtype is int64
# low is 5 and high is 5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_int64, low=5, high=5)
# low(default value) is 0 and high is -5, low must less then high
self.assertRaises(ValueError, paddle.randint_like, x_int64, high=-5)
# if high is None, low must be greater than 0
self.assertRaises(ValueError, paddle.randint_like, x_int64, low=-5)

# x dtype is float16
# low is 5 and high is 5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_float16, low=5, high=5)
# low(default value) is 0 and high is -5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_float16, high=-5)
# if high is None, low must be greater than 0
self.assertRaises(
ValueError, paddle.randint_like, x_float16, low=-5)

# x dtype is float32
# low is 5 and high is 5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_float32, low=5, high=5)
# low(default value) is 0 and high is -5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_float32, high=-5)
# if high is None, low must be greater than 0
self.assertRaises(
ValueError, paddle.randint_like, x_float32, low=-5)

# x dtype is float64
# low is 5 and high is 5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_float64, low=5, high=5)
# low(default value) is 0 and high is -5, low must less then high
self.assertRaises(
ValueError, paddle.randint_like, x_float64, high=-5)
# if high is None, low must be greater than 0
self.assertRaises(
ValueError, paddle.randint_like, x_float64, low=-5)


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
from .random import randn # noqa: F401
from .random import rand # noqa: F401
from .random import randint # noqa: F401
from .random import randint_like # noqa: F401
from .random import randperm # noqa: F401
from .search import argmax # noqa: F401
from .search import argmin # noqa: F401
Expand Down
Loading