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

Kate jing1212 patch 1 #1

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def initTestCase(self):
self.shape = (4, 8, 16)
self.max_norm = 1.0

class TestClipByNormOp(OpTest):
class TestClipByNormOpBf16(OpTest):
def setUp(self):
self.max_relative_error = 0.006
self.python_api = clip.clip_by_norm
Expand Down
50 changes: 49 additions & 1 deletion python/paddle/fluid/tests/unittests/test_multi_dot_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import numpy as np
from numpy.linalg import multi_dot
from op_test import OpTest
from op_test import OpTest, convert_float_to_uint16

import paddle

Expand Down Expand Up @@ -287,5 +287,53 @@ def test_dygraph_without_out(self):
np.testing.assert_allclose(expected_result, out.numpy(), rtol=1e-05)


class TestMultiDotFP16OP(OpTest):
def setUp(self):
self.op_type = "multi_dot"
self.python_api = paddle.linalg.multi_dot
self.dtype = self.get_dtype()
self.get_inputs_and_outputs()

def get_dtype(self):
return "float16"

def get_inputs_and_outputs(self):
self.A = np.random.random((2, 8)).astype(self.dtype)
self.B = np.random.random((8, 4)).astype(self.dtype)
self.inputs = {'X': [('x0', self.A), ('x1', self.B)]}
self.outputs = {'Out': multi_dot([self.A, self.B])}

def test_check_output(self):
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(['x0'], 'Out', check_eager=True)
self.check_grad(['x1'], 'Out', check_eager=True)


class TestMultiDotBF16OP(OpTest):
def setUp(self):
self.op_type = "multi_dot"
self.python_api = paddle.linalg.multi_dot
self.dtype = self.get_dtype()
self.get_inputs_and_outputs()

def get_dtype(self):
return "float32"

def get_inputs_and_outputs(self):
self.A = np.random.random((2, 8)).astype(self.dtype)
self.B = np.random.random((8, 4)).astype(self.dtype)
self.inputs = {'X': convert_float_to_uint16([('x0', self.A), ('x1', self.B)])}
self.outputs = {'Out': convert_float_to_uint16(multi_dot([self.A, self.B]))}

def test_check_output(self):
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(['x0'], 'Out', check_eager=True)
self.check_grad(['x1'], 'Out', check_eager=True)


if __name__ == "__main__":
unittest.main()
126 changes: 125 additions & 1 deletion python/paddle/fluid/tests/unittests/test_multinomial_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import unittest

import numpy as np
from op_test import OpTest
from op_test import OpTest, convert_float_to_uint16
from test_attribute_var import UnittestBase

import paddle
Expand Down Expand Up @@ -335,5 +335,129 @@ def test_static(self):
np.testing.assert_equal(infer_outs[1].shape, (3, 3))


class TestMultinomialFP16OP(OpTest):
def setUp(self):
paddle.enable_static()
self.op_type = "multinomial"
self.init_data()
self.inputs = {"X": self.input_np}

def init_data(self):
# input probability is a vector, and replacement is True
self.input_np = np.random.rand(4)
self.outputs = {"Out": np.zeros(100000).astype("float16")}
self.attrs = {"num_samples": 100000, "replacement": True}

def test_check_output(self):
self.check_output_customized(self.verify_output)

def sample_output(self, out):
return sample_output_one_dimension(out, 4)

def verify_output(self, outs):
# normalize the input to get the probability
prob = self.input_np / self.input_np.sum(axis=-1, keepdims=True)
sample_prob = self.sample_output(np.array(outs[0]))
np.testing.assert_allclose(
sample_prob,
prob,
rtol=0,
atol=0.01,
err_msg='sample_prob: ' + str(sample_prob) + '\nprob: ' + str(prob),
)


class TestMultinomialFP16OP2(TestMultinomialFP16OP):
def init_data(self):
# input probability is a matrix
self.input_np = np.random.rand(3, 4)
self.outputs = {"Out": np.zeros((3, 100000)).astype("float16")}
self.attrs = {"num_samples": 100000, "replacement": True}

def sample_output(self, out):
return sample_output_two_dimension(out, [3, 4])


class TestMultinomialFP16OP3(TestMultinomialFP16OP):
def init_data(self):
# replacement is False. number of samples must be less than number of categories.
self.input_np = np.random.rand(1000)
self.outputs = {"Out": np.zeros(100).astype("float16")}
self.attrs = {"num_samples": 100, "replacement": False}


def verify_output(self, outs):
out = np.array(outs[0])
unique_out = np.unique(out)
self.assertEqual(
len(unique_out),
100,
"replacement is False. categories can't be sampled repeatedly",
)


class TestMultinomialBF16OP(OpTest):
def setUp(self):
paddle.enable_static()
self.op_type = "multinomial"
self.init_data()
self.inputs = {"X": self.input_np}

def init_data(self):
# input probability is a vector, and replacement is True
self.input_np = np.random.rand(4)
self.input_np = (self.input_np * 65535).astype('uint16')
self.input_np = np.clip(self.input_np, 0, 65535)
self.outputs = {"Out": np.zeros(100000).astype("uint16")}
self.attrs = {"num_samples": 100000, "replacement": True}

def test_check_output(self):
self.check_output_customized(self.verify_output)

def sample_output(self, out):
return sample_output_one_dimension(out, 4)

def verify_output(self, outs):
# normalize the input to get the probability
prob = self.input_np / self.input_np.sum(axis=-1, keepdims=True)
sample_prob = self.sample_output(np.array(outs[0]).astype("uint16"))
np.testing.assert_allclose(
sample_prob,
prob,
rtol=0,
atol=0.01,
err_msg='sample_prob: ' + str(sample_prob) + '\nprob: ' + str(prob),
)


class TestMultinomialBF16OP2(TestMultinomialBF16OP):
def init_data(self):
# input probability is a matrix
self.input_np = (np.random.rand(3, 4)*65535).astype("uint16")
self.outputs = {"Out": np.zeros((3, 100000)).astype("uint16")}
self.attrs = {"num_samples": 100000, "replacement": True}

def sample_output(self, out):
return sample_output_two_dimension(out, [3, 4])


class TestMultinomialBF16OP3(TestMultinomialBF16OP):
def init_data(self):
# replacement is False. number of samples must be less than number of categories.
self.input_np = (np.random.rand(1000)*65535).astype("uint16")
self.outputs = {"Out": np.zeros(100).astype("uint16")}
self.attrs = {"num_samples": 100, "replacement": False}

def verify_output(self, outs):
out = np.array(outs[0])
unique_out = np.unique(out)
self.assertEqual(
len(unique_out),
100,
"replacement is False. categories can't be sampled repeatedly",
)



if __name__ == "__main__":
unittest.main()
63 changes: 62 additions & 1 deletion python/paddle/fluid/tests/unittests/test_overlap_add_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest

import numpy as np
from op_test import OpTest
from op_test import OpTest, convert_float_to_uint16

import paddle

Expand Down Expand Up @@ -156,5 +156,66 @@ def initTestCase(self):
return input_shape, input_type, attrs


class TestOverlapAddFP16OP(OpTest):
def setUp(self):
self.op_type = "overlap_add"
self.python_api = paddle.signal.overlap_add
self.shape, self.type, self.attrs = self.initTestCase()
self.inputs = {
'X': np.random.random(size=self.shape).astype(self.type)
}

self.outputs = {'Out': overlap_add(x=self.inputs['X'], **self.attrs).astype(self.type)}

def initTestCase(self):
input_shape = (50, 3)
input_type = 'float16'
attrs = {
'hop_length': 4,
'axis': -1,
}
return input_shape, input_type, attrs

def test_check_output(self):
paddle.enable_static()
self.check_output(check_eager=True,atol=1e-3, rtol=1e-3)
paddle.disable_static()

def test_check_grad_normal(self):
paddle.enable_static()
self.check_grad(['X'], 'Out', check_eager=True,atol=1e-3, rtol=1e-3)
paddle.disable_static()


class TestOverlapAddBF16OP(OpTest):
def setUp(self):
self.op_type = "overlap_add"
self.python_api = paddle.signal.overlap_add
self.shape, self.type, self.attrs = self.initTestCase()
self.inputs = {
'X': convert_float_to_uint16(np.random.random(size=self.shape).astype(self.type)),
}
self.outputs = {'Out': convert_float_to_uint16(overlap_add(x=self.inputs['X'], **self.attrs))}

def initTestCase(self):
input_shape = (50, 3)
input_type = 'float64'
attrs = {
'hop_length': 4,
'axis': -1,
}
return input_shape, input_type, attrs

def test_check_output(self):
paddle.enable_static()
self.check_output(check_eager=True)
paddle.disable_static()

def test_check_grad_normal(self):
paddle.enable_static()
self.check_grad(['X'], 'Out', check_eager=True)
paddle.disable_static()


if __name__ == '__main__':
unittest.main()
63 changes: 62 additions & 1 deletion python/paddle/fluid/tests/unittests/test_randperm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest

import numpy as np
from op_test import OpTest
from op_test import OpTest, convert_float_to_uint16

import paddle
import paddle.fluid.core as core
Expand Down Expand Up @@ -323,5 +323,66 @@ def test_fixed_random_number(self):
paddle.enable_static()


class TestRandpermFP16OP(OpTest):

def setUp(self):
self.op_type = "randperm"
self.python_api = paddle.randperm
self.n = 200
self.dtype = "float16"

self.inputs = {}
self.outputs = {"Out": np.zeros((self.n)).astype(self.dtype)}
self.init_attrs()
self.attrs = {
"n": self.n,
"dtype": convert_dtype(self.dtype),
}

def init_attrs(self):
pass

def test_check_output(self):
self.check_output_customized(self.verify_output)

def verify_output(self, outs):
out_np = np.array(outs[0]).astype(np.float16)
self.assertTrue(
check_randperm_out(self.n, out_np), msg=error_msg(out_np)
)


class TestRandpermOp(OpTest):
"""Test randperm op."""

def setUp(self):
self.op_type = "randperm"
self.python_api = paddle.randperm
self.n = 200
self.dtype = "int64"

self.inputs = {}
x = np.random.random((self.n,)).astype(np.float32)
self.inputs['X'] = convert_float_to_uint16(x)
self.outputs = {"Out": np.zeros((self.n)).astype(np.uint16)}
self.init_attrs()
self.attrs = {
"n": self.n,
"dtype": convert_dtype(self.dtype),
}

def init_attrs(self):
pass

def test_check_output(self):
self.check_output_customized(self.verify_output)

def verify_output(self, outs):
out_np = np.array(outs[0])
self.assertTrue(
check_randperm_out(self.n, out_np), msg=error_msg(out_np)
)


if __name__ == "__main__":
unittest.main()
Loading