未验证 提交 c6c9c186 编写于 作者: N Nyakku Shigure 提交者: GitHub

[CodeStyle] remove crlf for python files (#46155)

上级 9941ec12
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 NVIDIA Corporation. All rights reserved. # Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import threading, time import threading, time
import paddle import paddle
import numpy as np import numpy as np
class TestASPUtils(unittest.TestCase): class TestASPUtils(unittest.TestCase):
def test_get_check_method(self): def test_get_check_method(self):
self.assertEqual( self.assertEqual(
paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method( paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method(
paddle.fluid.contrib.sparsity.MaskAlgo.MASK_1D), paddle.fluid.contrib.sparsity.MaskAlgo.MASK_1D),
paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D) paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D)
self.assertEqual( self.assertEqual(
paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method( paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method(
paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_GREEDY), paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_GREEDY),
paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D) paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D)
self.assertEqual( self.assertEqual(
paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method( paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method(
paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_BEST), paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_BEST),
paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D) paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D)
def test_density(self): def test_density(self):
x = np.array([[1.0, 1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0], x = np.array([[1.0, 1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 0.0, 1.0]]) [0.0, 1.0, 0.0, 0.0, 1.0]])
self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.56) self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.56)
x[:, 0] = 0.0 x[:, 0] = 0.0
self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.4) self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.4)
def test_check_mask_1d(self): def test_check_mask_1d(self):
x = np.array([[1.0, 0.0, 0.0, 1.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0], x = np.array([[1.0, 0.0, 0.0, 1.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 0.0, 1.0]]) [0.0, 1.0, 0.0, 0.0, 1.0]])
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 4)) self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 4))
self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 4)) self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 4))
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 5)) self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 5))
self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 5)) self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 5))
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 6)) self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 6))
self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 4, 6)) self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 4, 6))
def test_get_mask_1d(self): def test_get_mask_1d(self):
for _ in range(10): for _ in range(10):
x = np.random.randint(10, size=(5, 5)) x = np.random.randint(10, size=(5, 5))
x = paddle.fluid.contrib.sparsity.get_mask_1d(x, 2, 4) x = paddle.fluid.contrib.sparsity.get_mask_1d(x, 2, 4)
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d( self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(
x, 2, 4)) x, 2, 4))
x = np.random.randn(5, 4) x = np.random.randn(5, 4)
x = paddle.fluid.contrib.sparsity.get_mask_1d(x, 2, 4) x = paddle.fluid.contrib.sparsity.get_mask_1d(x, 2, 4)
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d( self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(
x, 2, 4)) x, 2, 4))
def test_check_mask_2d(self): def test_check_mask_2d(self):
x = np.array([[1.0, 0.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 0.0], x = np.array([[1.0, 0.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0, 1.0]]) [0.0, 1.0, 0.0, 0.0, 1.0]])
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 4)) self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 4))
self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 4)) self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 4))
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 5)) self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 5))
self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 5)) self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 5))
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 6)) self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 6))
self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 4, 6)) self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 4, 6))
def test_get_mask_2d_greedy(self): def test_get_mask_2d_greedy(self):
for _ in range(10): for _ in range(10):
x = np.random.randint(10, size=(5, 5)) x = np.random.randint(10, size=(5, 5))
x = paddle.fluid.contrib.sparsity.get_mask_2d_greedy(x, 2, 4) x = paddle.fluid.contrib.sparsity.get_mask_2d_greedy(x, 2, 4)
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d( self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(
x, 2, 4)) x, 2, 4))
x = np.random.randn(5, 4) x = np.random.randn(5, 4)
x = paddle.fluid.contrib.sparsity.get_mask_2d_greedy(x, 2, 4) x = paddle.fluid.contrib.sparsity.get_mask_2d_greedy(x, 2, 4)
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d( self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(
x, 2, 4)) x, 2, 4))
def test_get_mask_2d_best(self): def test_get_mask_2d_best(self):
for _ in range(10): for _ in range(10):
x = np.random.randint(10, size=(5, 5)) x = np.random.randint(10, size=(5, 5))
x = paddle.fluid.contrib.sparsity.get_mask_2d_best(x, 2, 4) x = paddle.fluid.contrib.sparsity.get_mask_2d_best(x, 2, 4)
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d( self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(
x, 2, 4)) x, 2, 4))
x = np.random.randn(5, 4) x = np.random.randn(5, 4)
x = paddle.fluid.contrib.sparsity.get_mask_2d_best(x, 2, 4) x = paddle.fluid.contrib.sparsity.get_mask_2d_best(x, 2, 4)
self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d( self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(
x, 2, 4)) x, 2, 4))
def test_threadsafe_valid_2d_patterns(self): def test_threadsafe_valid_2d_patterns(self):
def get_reference(m=4, n=2): def get_reference(m=4, n=2):
from itertools import permutations from itertools import permutations
patterns = np.zeros(m) patterns = np.zeros(m)
patterns[:n] = 1 patterns[:n] = 1
patterns = list(set(permutations(patterns.tolist()))) patterns = list(set(permutations(patterns.tolist())))
patterns = patterns + patterns patterns = patterns + patterns
patterns = np.asarray(list(set(permutations(patterns, m)))) patterns = np.asarray(list(set(permutations(patterns, m))))
valid = ((patterns.sum(axis=1) <= n).sum( valid = ((patterns.sum(axis=1) <= n).sum(
axis=1) == m).nonzero()[0].reshape(-1) axis=1) == m).nonzero()[0].reshape(-1)
valid_patterns = np.empty((valid.shape[0], m, m)) valid_patterns = np.empty((valid.shape[0], m, m))
valid_patterns[:] = patterns[valid[:]] valid_patterns[:] = patterns[valid[:]]
return valid_patterns return valid_patterns
for _ in range(4): for _ in range(4):
computing_thread = threading.Thread( computing_thread = threading.Thread(
target=paddle.fluid.contrib.sparsity.utils. target=paddle.fluid.contrib.sparsity.utils.
_compute_valid_2d_patterns, _compute_valid_2d_patterns,
args=(2, 4)) args=(2, 4))
computing_thread.start() computing_thread.start()
time.sleep(3) time.sleep(3)
patterns_map = paddle.fluid.contrib.sparsity.utils._valid_2d_patterns patterns_map = paddle.fluid.contrib.sparsity.utils._valid_2d_patterns
reference_patterns = get_reference() reference_patterns = get_reference()
reference_key = '4_2' reference_key = '4_2'
self.assertTrue(reference_key in patterns_map) self.assertTrue(reference_key in patterns_map)
self.assertTrue(len(patterns_map) == 1) self.assertTrue(len(patterns_map) == 1)
self.assertTrue( self.assertTrue(
(reference_patterns == patterns_map[reference_key]).all()) (reference_patterns == patterns_map[reference_key]).all())
def test_check_sparsity(self): def test_check_sparsity(self):
for _ in range(10): for _ in range(10):
x = np.random.randint(10, size=(5)) x = np.random.randint(10, size=(5))
x_2d = x.reshape(1, x.shape[0]) x_2d = x.reshape(1, x.shape[0])
self.__test_1D_2D_sparsity_checking_methods(x_2d) self.__test_1D_2D_sparsity_checking_methods(x_2d)
x = np.random.randint(10, size=(5, 5)) x = np.random.randint(10, size=(5, 5))
x_2d = x x_2d = x
self.__test_1D_2D_sparsity_checking_methods(x_2d) self.__test_1D_2D_sparsity_checking_methods(x_2d)
x = np.random.randint(10, size=(5, 5, 5)) x = np.random.randint(10, size=(5, 5, 5))
x_2d = x.reshape(x.shape[0] * x.shape[1], x.shape[2]) x_2d = x.reshape(x.shape[0] * x.shape[1], x.shape[2])
self.__test_1D_2D_sparsity_checking_methods(x_2d) self.__test_1D_2D_sparsity_checking_methods(x_2d)
x = np.random.randint(10, size=(5, 5, 5, 5)) x = np.random.randint(10, size=(5, 5, 5, 5))
x_2d = x.reshape(x.shape[0], x.shape[1] * x.shape[2] * x.shape[3]) x_2d = x.reshape(x.shape[0], x.shape[1] * x.shape[2] * x.shape[3])
self.__test_1D_2D_sparsity_checking_methods(x_2d) self.__test_1D_2D_sparsity_checking_methods(x_2d)
def test_create_mask(self): def test_create_mask(self):
for _ in range(10): for _ in range(10):
x = np.random.randint(10, size=(5)) x = np.random.randint(10, size=(5))
self.__test_1D_2D_sparse_mask_generation_methods(x) self.__test_1D_2D_sparse_mask_generation_methods(x)
x = np.random.randint(10, size=(5, 5)) x = np.random.randint(10, size=(5, 5))
self.__test_1D_2D_sparse_mask_generation_methods(x) self.__test_1D_2D_sparse_mask_generation_methods(x)
x = np.random.randint(10, size=(5, 5, 5)) x = np.random.randint(10, size=(5, 5, 5))
self.__test_1D_2D_sparse_mask_generation_methods(x) self.__test_1D_2D_sparse_mask_generation_methods(x)
x = np.random.randint(10, size=(5, 5, 5, 5)) x = np.random.randint(10, size=(5, 5, 5, 5))
self.__test_1D_2D_sparse_mask_generation_methods(x) self.__test_1D_2D_sparse_mask_generation_methods(x)
def __test_1D_2D_sparsity_checking_methods(self, x_2d): def __test_1D_2D_sparsity_checking_methods(self, x_2d):
mask = paddle.fluid.contrib.sparsity.get_mask_1d(x_2d, 2, 4) mask = paddle.fluid.contrib.sparsity.get_mask_1d(x_2d, 2, 4)
self.assertEqual( self.assertEqual(
paddle.fluid.contrib.sparsity.check_sparsity( paddle.fluid.contrib.sparsity.check_sparsity(
mask, mask,
func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D, func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D,
n=2, n=2,
m=4), paddle.fluid.contrib.sparsity.check_mask_1d(mask, 2, 4)) m=4), paddle.fluid.contrib.sparsity.check_mask_1d(mask, 2, 4))
mask = paddle.fluid.contrib.sparsity.get_mask_2d_best(x_2d, 2, 4) mask = paddle.fluid.contrib.sparsity.get_mask_2d_best(x_2d, 2, 4)
self.assertEqual( self.assertEqual(
paddle.fluid.contrib.sparsity.check_sparsity( paddle.fluid.contrib.sparsity.check_sparsity(
mask, mask,
func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D, func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D,
n=2, n=2,
m=4), paddle.fluid.contrib.sparsity.check_mask_2d(mask, 2, 4)) m=4), paddle.fluid.contrib.sparsity.check_mask_2d(mask, 2, 4))
def __test_1D_2D_sparse_mask_generation_methods(self, x): def __test_1D_2D_sparse_mask_generation_methods(self, x):
mask = paddle.fluid.contrib.sparsity.create_mask( mask = paddle.fluid.contrib.sparsity.create_mask(
x, x,
func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_1D, func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_1D,
n=2, n=2,
m=4) m=4)
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity( paddle.fluid.contrib.sparsity.check_sparsity(
mask, mask,
func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D, func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D,
n=2, n=2,
m=4)) m=4))
mask = paddle.fluid.contrib.sparsity.create_mask( mask = paddle.fluid.contrib.sparsity.create_mask(
x, x,
func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_GREEDY, func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_GREEDY,
n=2, n=2,
m=4) m=4)
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity( paddle.fluid.contrib.sparsity.check_sparsity(
mask, mask,
func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D, func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D,
n=2, n=2,
m=4)) m=4))
mask = paddle.fluid.contrib.sparsity.create_mask( mask = paddle.fluid.contrib.sparsity.create_mask(
x, x,
func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_BEST, func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_BEST,
n=2, n=2,
m=4) m=4)
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity( paddle.fluid.contrib.sparsity.check_sparsity(
mask, mask,
func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D, func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D,
n=2, n=2,
m=4)) m=4))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
from paddle import enable_static from paddle import enable_static
from paddle.fluid.tests.unittests.op_test import OpTest, OpTestTool, convert_float_to_uint16 from paddle.fluid.tests.unittests.op_test import OpTest, OpTestTool, convert_float_to_uint16
from paddle.fluid.framework import _current_expected_place from paddle.fluid.framework import _current_expected_place
import paddle.fluid.core as core import paddle.fluid.core as core
@OpTestTool.skip_if(not (isinstance(_current_expected_place(), core.CPUPlace)), @OpTestTool.skip_if(not (isinstance(_current_expected_place(), core.CPUPlace)),
"GPU is not supported") "GPU is not supported")
class TestMKLDNNElementwiseSubOp(OpTest): class TestMKLDNNElementwiseSubOp(OpTest):
def setUp(self): def setUp(self):
self.op_type = "elementwise_sub" self.op_type = "elementwise_sub"
self.init_dtype() self.init_dtype()
self.init_input_output() self.init_input_output()
self.init_kernel_type() self.init_kernel_type()
self.init_axis() self.init_axis()
self.inputs = { self.inputs = {
'X': OpTest.np_dtype_to_fluid_dtype(self.x), 'X': OpTest.np_dtype_to_fluid_dtype(self.x),
'Y': OpTest.np_dtype_to_fluid_dtype(self.y) 'Y': OpTest.np_dtype_to_fluid_dtype(self.y)
} }
self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}
self.outputs = {'Out': self.out} self.outputs = {'Out': self.out}
def init_input_output(self): def init_input_output(self):
self.x = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype) self.x = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
self.y = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype) self.y = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
def test_check_grad_normal(self): def test_check_grad_normal(self):
self.check_grad(['X', 'Y'], 'Out') self.check_grad(['X', 'Y'], 'Out')
def test_check_grad_ignore_x(self): def test_check_grad_ignore_x(self):
self.check_grad(['Y'], 'Out', no_grad_set=set("X")) self.check_grad(['Y'], 'Out', no_grad_set=set("X"))
def test_check_grad_ignore_y(self): def test_check_grad_ignore_y(self):
self.check_grad(['X'], 'Out', no_grad_set=set('Y')) self.check_grad(['X'], 'Out', no_grad_set=set('Y'))
def init_axis(self): def init_axis(self):
self.axis = -1 self.axis = -1
def init_kernel_type(self): def init_kernel_type(self):
self.use_mkldnn = True self.use_mkldnn = True
def init_dtype(self): def init_dtype(self):
self.dtype = np.float32 self.dtype = np.float32
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
class TestMKLDNNElementwiseSubOp2(TestMKLDNNElementwiseSubOp): class TestMKLDNNElementwiseSubOp2(TestMKLDNNElementwiseSubOp):
def init_input_output(self): def init_input_output(self):
self.x = np.random.random((100, )).astype(self.dtype) self.x = np.random.random((100, )).astype(self.dtype)
self.y = np.random.random((100, )).astype(self.dtype) self.y = np.random.random((100, )).astype(self.dtype)
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp3(TestMKLDNNElementwiseSubOp): class TestMKLDNNElementwiseSubOp3(TestMKLDNNElementwiseSubOp):
def init_input_output(self): def init_input_output(self):
self.x = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype) self.x = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.y = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype) self.y = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp4(TestMKLDNNElementwiseSubOp): class TestMKLDNNElementwiseSubOp4(TestMKLDNNElementwiseSubOp):
def init_input_output(self): def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 32]).astype(self.dtype) self.x = np.random.uniform(1, 2, [2, 3, 4, 32]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [4, 32]).astype(self.dtype) self.y = np.random.uniform(1, 2, [4, 32]).astype(self.dtype)
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp5(TestMKLDNNElementwiseSubOp): class TestMKLDNNElementwiseSubOp5(TestMKLDNNElementwiseSubOp):
def init_input_output(self): def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype) self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [100]).astype(self.dtype) self.y = np.random.uniform(1, 2, [100]).astype(self.dtype)
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp_broadcast(TestMKLDNNElementwiseSubOp): class TestMKLDNNElementwiseSubOp_broadcast(TestMKLDNNElementwiseSubOp):
def init_input_output(self): def init_input_output(self):
self.x = np.random.rand(2, 10, 12, 3).astype(self.dtype) self.x = np.random.rand(2, 10, 12, 3).astype(self.dtype)
self.y = np.random.rand(10, 12).astype(self.dtype) self.y = np.random.rand(10, 12).astype(self.dtype)
self.out = self.x - self.y.reshape(1, 10, 12, 1) self.out = self.x - self.y.reshape(1, 10, 12, 1)
def init_axis(self): def init_axis(self):
self.axis = 1 self.axis = 1
class TestElementwiseSubOp_xsize_lessthan_ysize_sub(TestMKLDNNElementwiseSubOp): class TestElementwiseSubOp_xsize_lessthan_ysize_sub(TestMKLDNNElementwiseSubOp):
def init_input_output(self): def init_input_output(self):
self.x = np.random.rand(10, 12).astype(self.dtype) self.x = np.random.rand(10, 12).astype(self.dtype)
self.y = np.random.rand(2, 2, 10, 12).astype(self.dtype) self.y = np.random.rand(2, 2, 10, 12).astype(self.dtype)
self.out = self.x - self.y self.out = self.x - self.y
def init_axis(self): def init_axis(self):
self.axis = 2 self.axis = 2
def test_check_grad_normal(self): def test_check_grad_normal(self):
pass pass
def test_check_grad_ignore_y(self): def test_check_grad_ignore_y(self):
pass pass
def test_check_grad_ignore_x(self): def test_check_grad_ignore_x(self):
pass pass
@OpTestTool.skip_if_not_cpu_bf16() @OpTestTool.skip_if_not_cpu_bf16()
class TestBf16(TestMKLDNNElementwiseSubOp): class TestBf16(TestMKLDNNElementwiseSubOp):
def setUp(self): def setUp(self):
self.op_type = "elementwise_sub" self.op_type = "elementwise_sub"
self.init_dtype() self.init_dtype()
self.init_input_output() self.init_input_output()
self.init_kernel_type() self.init_kernel_type()
self.init_axis() self.init_axis()
self.x_bf16 = convert_float_to_uint16(self.x) self.x_bf16 = convert_float_to_uint16(self.x)
self.y_bf16 = convert_float_to_uint16(self.y) self.y_bf16 = convert_float_to_uint16(self.y)
self.inputs = {'X': self.x_bf16, 'Y': self.y_bf16} self.inputs = {'X': self.x_bf16, 'Y': self.y_bf16}
self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}
self.outputs = {'Out': convert_float_to_uint16(self.out)} self.outputs = {'Out': convert_float_to_uint16(self.out)}
def init_dtype(self): def init_dtype(self):
self.dtype = np.float32 self.dtype = np.float32
self.mkldnn_data_type = "bfloat16" self.mkldnn_data_type = "bfloat16"
def init_input_output(self): def init_input_output(self):
self.x = np.random.random(100, ).astype(self.dtype) self.x = np.random.random(100, ).astype(self.dtype)
self.y = np.random.random(100, ).astype(self.dtype) self.y = np.random.random(100, ).astype(self.dtype)
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
def test_check_output(self): def test_check_output(self):
self.check_output_with_place(core.CPUPlace()) self.check_output_with_place(core.CPUPlace())
def test_check_grad_normal(self): def test_check_grad_normal(self):
self.check_grad_with_place(core.CPUPlace(), ["X", "Y"], self.check_grad_with_place(core.CPUPlace(), ["X", "Y"],
"Out", "Out",
user_defined_grads=[self.x, -self.x], user_defined_grads=[self.x, -self.x],
user_defined_grad_outputs=[self.x_bf16]) user_defined_grad_outputs=[self.x_bf16])
def test_check_grad_ignore_x(self): def test_check_grad_ignore_x(self):
self.check_grad_with_place(core.CPUPlace(), ["Y"], self.check_grad_with_place(core.CPUPlace(), ["Y"],
"Out", "Out",
user_defined_grads=[-self.y], user_defined_grads=[-self.y],
user_defined_grad_outputs=[self.y_bf16]) user_defined_grad_outputs=[self.y_bf16])
def test_check_grad_ignore_y(self): def test_check_grad_ignore_y(self):
self.check_grad_with_place(core.CPUPlace(), ["X"], self.check_grad_with_place(core.CPUPlace(), ["X"],
"Out", "Out",
user_defined_grads=[self.x], user_defined_grads=[self.x],
user_defined_grad_outputs=[self.x_bf16]) user_defined_grad_outputs=[self.x_bf16])
class TestBf16Broadcasting(TestBf16): class TestBf16Broadcasting(TestBf16):
def init_input_output(self): def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype) self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [100]).astype(self.dtype) self.y = np.random.uniform(1, 2, [100]).astype(self.dtype)
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
def compute_reduced_gradients(self, out_grads): def compute_reduced_gradients(self, out_grads):
part_sum = np.add.reduceat(out_grads, [0], axis=0) part_sum = np.add.reduceat(out_grads, [0], axis=0)
part_sum = np.add.reduceat(part_sum, [0], axis=1) part_sum = np.add.reduceat(part_sum, [0], axis=1)
part_sum = np.add.reduceat(part_sum, [0], axis=2) part_sum = np.add.reduceat(part_sum, [0], axis=2)
return -part_sum.flatten() return -part_sum.flatten()
def test_check_grad_normal(self): def test_check_grad_normal(self):
self.check_grad_with_place( self.check_grad_with_place(
core.CPUPlace(), ["X", "Y"], core.CPUPlace(), ["X", "Y"],
"Out", "Out",
user_defined_grads=[self.x, user_defined_grads=[self.x,
self.compute_reduced_gradients(self.x)], self.compute_reduced_gradients(self.x)],
user_defined_grad_outputs=[self.x_bf16]) user_defined_grad_outputs=[self.x_bf16])
def test_check_grad_ignore_x(self): def test_check_grad_ignore_x(self):
self.check_grad_with_place( self.check_grad_with_place(
core.CPUPlace(), ["Y"], core.CPUPlace(), ["Y"],
"Out", "Out",
user_defined_grads=[self.compute_reduced_gradients(self.x)], user_defined_grads=[self.compute_reduced_gradients(self.x)],
user_defined_grad_outputs=[self.x_bf16]) user_defined_grad_outputs=[self.x_bf16])
class TestInt8(TestMKLDNNElementwiseSubOp): class TestInt8(TestMKLDNNElementwiseSubOp):
def init_kernel_type(self): def init_kernel_type(self):
self.use_mkldnn = True self.use_mkldnn = True
self._cpu_only = True self._cpu_only = True
def init_dtype(self): def init_dtype(self):
self.dtype = np.int8 self.dtype = np.int8
def init_input_output(self): def init_input_output(self):
self.x = np.random.randint(0, 3, (12, 9)).astype("int8") self.x = np.random.randint(0, 3, (12, 9)).astype("int8")
self.y = np.random.randint(0, 3, (12, 9)).astype("int8") self.y = np.random.randint(0, 3, (12, 9)).astype("int8")
self.out = np.subtract(self.x, self.y) self.out = np.subtract(self.x, self.y)
def init_scales(self): def init_scales(self):
self.attrs['Scale_x'] = 1.0 self.attrs['Scale_x'] = 1.0
self.attrs['Scale_y'] = 1.0 self.attrs['Scale_y'] = 1.0
self.attrs['Scale_out'] = 1.0 self.attrs['Scale_out'] = 1.0
def test_check_output(self): def test_check_output(self):
self.init_scales() self.init_scales()
self.check_output() self.check_output()
def test_check_grad_normal(self): def test_check_grad_normal(self):
pass pass
def test_check_grad_ignore_x(self): def test_check_grad_ignore_x(self):
pass pass
def test_check_grad_ignore_y(self): def test_check_grad_ignore_y(self):
pass pass
if __name__ == '__main__': if __name__ == '__main__':
enable_static() enable_static()
unittest.main() unittest.main()
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
from paddle.fluid.tests.unittests.op_test import OpTest, OpTestTool from paddle.fluid.tests.unittests.op_test import OpTest, OpTestTool
import paddle import paddle
@OpTestTool.skip_if_not_cpu_bf16() @OpTestTool.skip_if_not_cpu_bf16()
class TestFillConstant2DOneDNNOp(OpTest): class TestFillConstant2DOneDNNOp(OpTest):
def setUp(self): def setUp(self):
self.op_type = "fill_constant" self.op_type = "fill_constant"
self.dtype = np.float32 self.dtype = np.float32
self.shape_tensor_list = None self.shape_tensor_list = None
self.shape_tensor = None self.shape_tensor = None
self.str_value = "" self.str_value = ""
real_shape = [] real_shape = []
self.value = 0.1 self.value = 0.1
self.set_inputs() self.set_inputs()
self.set_attrs() self.set_attrs()
if 'value' in self.attrs: if 'value' in self.attrs:
self.value = self.attrs['value'] self.value = self.attrs['value']
if self.str_value != "": if self.str_value != "":
self.value = float(self.str_value) self.value = float(self.str_value)
if 'ValueTensor' in self.inputs: if 'ValueTensor' in self.inputs:
self.value = self.inputs['ValueTensor'] self.value = self.inputs['ValueTensor']
if 'shape' in self.attrs: if 'shape' in self.attrs:
real_shape = self.attrs['shape'] real_shape = self.attrs['shape']
if 'ShapeTensor' in self.inputs: if 'ShapeTensor' in self.inputs:
real_shape = list(self.inputs['ShapeTensor']) real_shape = list(self.inputs['ShapeTensor'])
if 'ShapeTensorList' in self.inputs: if 'ShapeTensorList' in self.inputs:
real_shape = [] real_shape = []
for shape_tensor in self.inputs['ShapeTensorList']: for shape_tensor in self.inputs['ShapeTensorList']:
real_shape.append(shape_tensor[1].item()) real_shape.append(shape_tensor[1].item())
self.outputs = {'Out': np.full(real_shape, self.value)} self.outputs = {'Out': np.full(real_shape, self.value)}
def set_inputs(self): def set_inputs(self):
self.inputs = {} self.inputs = {}
def set_attrs(self): def set_attrs(self):
self.attrs = {'shape': (3, 5), 'use_mkldnn': True, 'value': self.value} self.attrs = {'shape': (3, 5), 'use_mkldnn': True, 'value': self.value}
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
class TestFillZerosLike4DShapeTensorPriorityOneDNNOp(TestFillConstant2DOneDNNOp class TestFillZerosLike4DShapeTensorPriorityOneDNNOp(TestFillConstant2DOneDNNOp
): ):
def set_inputs(self): def set_inputs(self):
self.inputs = {'ShapeTensor': np.array([5, 6, 7, 8]).astype("int32")} self.inputs = {'ShapeTensor': np.array([5, 6, 7, 8]).astype("int32")}
class TestFillZerosLike4DShapeTensorListPriorityOneDNNOp( class TestFillZerosLike4DShapeTensorListPriorityOneDNNOp(
TestFillConstant2DOneDNNOp): TestFillConstant2DOneDNNOp):
def set_inputs(self): def set_inputs(self):
shape = (4, 5, 6, 7) shape = (4, 5, 6, 7)
self.shape_tensor_list = [] self.shape_tensor_list = []
for index, elem in enumerate(shape): for index, elem in enumerate(shape):
self.shape_tensor_list.append(("x" + str(index), np.ones( self.shape_tensor_list.append(("x" + str(index), np.ones(
(1)).astype('int32') * elem)) (1)).astype('int32') * elem))
self.inputs = {'ShapeTensorList': self.shape_tensor_list} self.inputs = {'ShapeTensorList': self.shape_tensor_list}
class TestFillZerosLike2DStringValueInfOneDNNOp(TestFillConstant2DOneDNNOp): class TestFillZerosLike2DStringValueInfOneDNNOp(TestFillConstant2DOneDNNOp):
def set_attrs(self): def set_attrs(self):
self.str_value = "inf" self.str_value = "inf"
self.attrs = {'shape': (10, 13), 'use_mkldnn': True, 'str_value': "inf"} self.attrs = {'shape': (10, 13), 'use_mkldnn': True, 'str_value': "inf"}
class TestFillZerosLike2DStringValueMinusInfOneDNNOp(TestFillConstant2DOneDNNOp class TestFillZerosLike2DStringValueMinusInfOneDNNOp(TestFillConstant2DOneDNNOp
): ):
def set_attrs(self): def set_attrs(self):
self.str_value = "-inf" self.str_value = "-inf"
self.attrs = { self.attrs = {
'shape': (10, 13), 'shape': (10, 13),
'use_mkldnn': True, 'use_mkldnn': True,
'str_value': "-inf" 'str_value': "-inf"
} }
class TestFillZerosLike2DStringValueFloatOneDNNOp(TestFillConstant2DOneDNNOp): class TestFillZerosLike2DStringValueFloatOneDNNOp(TestFillConstant2DOneDNNOp):
def set_attrs(self): def set_attrs(self):
self.str_value = "0.123" self.str_value = "0.123"
self.attrs = { self.attrs = {
'shape': (10, 13), 'shape': (10, 13),
'use_mkldnn': True, 'use_mkldnn': True,
'str_value': "0.123" 'str_value': "0.123"
} }
class TestFillZerosLike2DValueTensorPriorityOneDNNOp( class TestFillZerosLike2DValueTensorPriorityOneDNNOp(
TestFillZerosLike2DStringValueFloatOneDNNOp): TestFillZerosLike2DStringValueFloatOneDNNOp):
def set_inputs(self): def set_inputs(self):
self.inputs = {'ValueTensor': np.atleast_1d(2.25).astype("float32")} self.inputs = {'ValueTensor': np.atleast_1d(2.25).astype("float32")}
if __name__ == "__main__": if __name__ == "__main__":
paddle.enable_static() paddle.enable_static()
unittest.main() unittest.main()
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.framework import convert_np_dtype_to_dtype_, Program, program_guard from paddle.fluid.framework import convert_np_dtype_to_dtype_, Program, program_guard
import paddle.fluid.core as core import paddle.fluid.core as core
import numpy as np import numpy as np
import copy import copy
import unittest import unittest
import sys import sys
sys.path.append("../") sys.path.append("../")
from op_test import OpTest from op_test import OpTest
class TestSequenceFirstStepOpError(unittest.TestCase): class TestSequenceFirstStepOpError(unittest.TestCase):
def test_errors(self): def test_errors(self):
with program_guard(Program(), Program()): with program_guard(Program(), Program()):
def test_Variable(): def test_Variable():
# the input must be Variable # the input must be Variable
input_data = np.random.randint(1, 5, [4]).astype("int64") input_data = np.random.randint(1, 5, [4]).astype("int64")
fluid.layers.sequence_last_step(input_data) fluid.layers.sequence_last_step(input_data)
self.assertRaises(TypeError, test_Variable) self.assertRaises(TypeError, test_Variable)
def test_input_dtype(): def test_input_dtype():
# the dtype of input must be int64 # the dtype of input must be int64
type_data = fluid.layers.data(name='type_data', type_data = fluid.layers.data(name='type_data',
shape=[7, 1], shape=[7, 1],
append_batch_size=False, append_batch_size=False,
dtype='int64', dtype='int64',
lod_level=1) lod_level=1)
fluid.layers.sequence_last_step(type_data) fluid.layers.sequence_last_step(type_data)
self.assertRaises(TypeError, test_input_dtype) self.assertRaises(TypeError, test_input_dtype)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.framework import convert_np_dtype_to_dtype_, Program, program_guard from paddle.fluid.framework import convert_np_dtype_to_dtype_, Program, program_guard
import paddle.fluid.core as core import paddle.fluid.core as core
import numpy as np import numpy as np
import copy import copy
import unittest import unittest
import sys import sys
sys.path.append("../") sys.path.append("../")
from op_test import OpTest from op_test import OpTest
class TestSequenceLastStepOpError(unittest.TestCase): class TestSequenceLastStepOpError(unittest.TestCase):
def test_errors(self): def test_errors(self):
with program_guard(Program(), Program()): with program_guard(Program(), Program()):
def test_Variable(): def test_Variable():
# the input must be Variable # the input must be Variable
input_data = np.random.randint(1, 5, [4]).astype("int64") input_data = np.random.randint(1, 5, [4]).astype("int64")
fluid.layers.sequence_last_step(input_data) fluid.layers.sequence_last_step(input_data)
self.assertRaises(TypeError, test_Variable) self.assertRaises(TypeError, test_Variable)
def test_input_dtype(): def test_input_dtype():
# the dtype of input must be int64 # the dtype of input must be int64
type_data = fluid.layers.data(name='type_data', type_data = fluid.layers.data(name='type_data',
shape=[7, 1], shape=[7, 1],
append_batch_size=False, append_batch_size=False,
dtype='int64', dtype='int64',
lod_level=1) lod_level=1)
fluid.layers.sequence_last_step(type_data) fluid.layers.sequence_last_step(type_data)
self.assertRaises(TypeError, test_input_dtype) self.assertRaises(TypeError, test_input_dtype)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
import math import math
import paddle.fluid.core as core import paddle.fluid.core as core
from op_test import OpTest from op_test import OpTest
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.layers as layers import paddle.fluid.layers as layers
import random import random
random.seed(2) random.seed(2)
np.set_printoptions(threshold=np.inf) np.set_printoptions(threshold=np.inf)
paddle.enable_static() paddle.enable_static()
SIGMOID_THRESHOLD_MIN = -40.0 SIGMOID_THRESHOLD_MIN = -40.0
SIGMOID_THRESHOLD_MAX = 13.0 SIGMOID_THRESHOLD_MAX = 13.0
EXP_MAX_INPUT = 40.0 EXP_MAX_INPUT = 40.0
class RandomWeight: class RandomWeight:
def __init__(self): def __init__(self):
pass pass
def updata_weight(self, hidden_size, input_size, dtype): def updata_weight(self, hidden_size, input_size, dtype):
std = 1.0 / math.sqrt(hidden_size) std = 1.0 / math.sqrt(hidden_size)
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.input_size = input_size self.input_size = input_size
self.dtype = dtype self.dtype = dtype
self.weight_ih = np.random.uniform(low=-std, self.weight_ih = np.random.uniform(low=-std,
high=std, high=std,
size=(4 * self.hidden_size, size=(4 * self.hidden_size,
self.input_size)).astype(dtype) self.input_size)).astype(dtype)
self.weight_hh = np.random.uniform( self.weight_hh = np.random.uniform(
low=-std, high=std, low=-std, high=std,
size=(4 * self.hidden_size, self.hidden_size)).astype(dtype) size=(4 * self.hidden_size, self.hidden_size)).astype(dtype)
self.bias_ih = np.random.uniform(low=-std, self.bias_ih = np.random.uniform(low=-std,
high=std, high=std,
size=(4 * size=(4 *
self.hidden_size)).astype(dtype) self.hidden_size)).astype(dtype)
self.bias_hh = np.random.uniform(low=-std, self.bias_hh = np.random.uniform(low=-std,
high=std, high=std,
size=(4 * size=(4 *
self.hidden_size)).astype(dtype) self.hidden_size)).astype(dtype)
weight = RandomWeight() weight = RandomWeight()
class LayerMixin(object): class LayerMixin(object):
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs) return self.forward(*args, **kwargs)
class LayerListMixin(LayerMixin): class LayerListMixin(LayerMixin):
def __init__(self, layers=None): def __init__(self, layers=None):
self._layers = list(layers) if layers else [] self._layers = list(layers) if layers else []
def append(self, layer): def append(self, layer):
self._layers.append(layer) self._layers.append(layer)
def __iter__(self): def __iter__(self):
return iter(self._layers) return iter(self._layers)
class LSTMCell(LayerMixin): class LSTMCell(LayerMixin):
def __init__(self, input_size, hidden_size, bias=True): def __init__(self, input_size, hidden_size, bias=True):
self.input_size = input_size self.input_size = input_size
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.bias = bias self.bias = bias
self.dtype = np.float64 self.dtype = np.float64
self.parameters = dict() self.parameters = dict()
self.weight_ih = weight.weight_ih self.weight_ih = weight.weight_ih
self.weight_hh = weight.weight_hh self.weight_hh = weight.weight_hh
self.parameters['weight_ih'] = self.weight_ih self.parameters['weight_ih'] = self.weight_ih
self.parameters['weight_hh'] = self.weight_hh self.parameters['weight_hh'] = self.weight_hh
if bias: if bias:
self.bias_ih = weight.bias_ih self.bias_ih = weight.bias_ih
self.bias_hh = weight.bias_hh self.bias_hh = weight.bias_hh
self.parameters['bias_ih'] = self.bias_ih self.parameters['bias_ih'] = self.bias_ih
self.parameters['bias_hh'] = self.bias_hh self.parameters['bias_hh'] = self.bias_hh
else: else:
self.bias_ih = None self.bias_ih = None
self.bias_hh = None self.bias_hh = None
def init_state(self, inputs): def init_state(self, inputs):
batch_size = inputs.shape[0] batch_size = inputs.shape[0]
init_h = np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype) init_h = np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype)
init_c = np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype) init_c = np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype)
return init_h, init_c return init_h, init_c
def forward(self, inputs, hx=None): def forward(self, inputs, hx=None):
if hx is None: if hx is None:
hx = self.init_state(inputs) hx = self.init_state(inputs)
pre_hidden, pre_cell = hx pre_hidden, pre_cell = hx
gates = np.matmul(inputs, self.weight_ih.T) gates = np.matmul(inputs, self.weight_ih.T)
if self.bias_ih is not None: if self.bias_ih is not None:
gates = gates + self.bias_ih gates = gates + self.bias_ih
gates += np.matmul(pre_hidden, self.weight_hh.T) gates += np.matmul(pre_hidden, self.weight_hh.T)
if self.bias_hh is not None: if self.bias_hh is not None:
gates = gates + self.bias_hh gates = gates + self.bias_hh
chunked_gates = np.split(gates, 4, -1) chunked_gates = np.split(gates, 4, -1)
i = 1.0 / (1.0 + np.exp(-chunked_gates[0])) i = 1.0 / (1.0 + np.exp(-chunked_gates[0]))
f = 1.0 / (1.0 + np.exp(-chunked_gates[1])) f = 1.0 / (1.0 + np.exp(-chunked_gates[1]))
o = 1.0 / (1.0 + np.exp(-chunked_gates[3])) o = 1.0 / (1.0 + np.exp(-chunked_gates[3]))
c = f * pre_cell + i * np.tanh(chunked_gates[2]) c = f * pre_cell + i * np.tanh(chunked_gates[2])
h = o * np.tanh(c) h = o * np.tanh(c)
return h, (h, c) return h, (h, c)
def sequence_mask(lengths, max_len=None): def sequence_mask(lengths, max_len=None):
if max_len is None: if max_len is None:
max_len = np.max(lengths) max_len = np.max(lengths)
else: else:
assert max_len >= np.max(lengths) assert max_len >= np.max(lengths)
return np.arange(max_len) < np.expand_dims(lengths, -1) return np.arange(max_len) < np.expand_dims(lengths, -1)
def update_state(mask, new, old): def update_state(mask, new, old):
if not isinstance(old, (tuple, list)): if not isinstance(old, (tuple, list)):
return np.where(mask, new, old) return np.where(mask, new, old)
else: else:
return tuple(map(lambda x, y: np.where(mask, x, y), new, old)) return tuple(map(lambda x, y: np.where(mask, x, y), new, old))
def rnn(cell, def rnn(cell,
inputs, inputs,
initial_states, initial_states,
sequence_length=None, sequence_length=None,
time_major=False, time_major=False,
is_reverse=False): is_reverse=False):
if not time_major: if not time_major:
inputs = np.transpose(inputs, [1, 0, 2]) inputs = np.transpose(inputs, [1, 0, 2])
if is_reverse: if is_reverse:
inputs = np.flip(inputs, 0) inputs = np.flip(inputs, 0)
if sequence_length is None: if sequence_length is None:
mask = None mask = None
else: else:
mask = np.transpose(sequence_mask(sequence_length), [1, 0]) mask = np.transpose(sequence_mask(sequence_length), [1, 0])
mask = np.expand_dims(mask, -1) mask = np.expand_dims(mask, -1)
if is_reverse: if is_reverse:
mask = np.flip(mask, 0) mask = np.flip(mask, 0)
time_steps = inputs.shape[0] time_steps = inputs.shape[0]
state = initial_states state = initial_states
outputs = [] outputs = []
for t in range(time_steps): for t in range(time_steps):
x_t = inputs[t] x_t = inputs[t]
if mask is not None: if mask is not None:
m_t = mask[t] m_t = mask[t]
y, new_state = cell(x_t, state) y, new_state = cell(x_t, state)
y = np.where(m_t, y, 0.) y = np.where(m_t, y, 0.)
outputs.append(y) outputs.append(y)
state = update_state(m_t, new_state, state) state = update_state(m_t, new_state, state)
else: else:
y, new_state = cell(x_t, state) y, new_state = cell(x_t, state)
outputs.append(y) outputs.append(y)
state = new_state state = new_state
outputs = np.stack(outputs) outputs = np.stack(outputs)
final_state = state final_state = state
if is_reverse: if is_reverse:
outputs = np.flip(outputs, 0) outputs = np.flip(outputs, 0)
if not time_major: if not time_major:
outputs = np.transpose(outputs, [1, 0, 2]) outputs = np.transpose(outputs, [1, 0, 2])
return outputs, final_state return outputs, final_state
def birnn(cell_fw, def birnn(cell_fw,
cell_bw, cell_bw,
inputs, inputs,
initial_states, initial_states,
sequence_length=None, sequence_length=None,
time_major=False): time_major=False):
states_fw, states_bw = initial_states states_fw, states_bw = initial_states
outputs_fw, states_fw = rnn(cell_fw, outputs_fw, states_fw = rnn(cell_fw,
inputs, inputs,
states_fw, states_fw,
sequence_length, sequence_length,
time_major=time_major) time_major=time_major)
outputs_bw, states_bw = rnn(cell_bw, outputs_bw, states_bw = rnn(cell_bw,
inputs, inputs,
states_bw, states_bw,
sequence_length, sequence_length,
time_major=time_major, time_major=time_major,
is_reverse=True) is_reverse=True)
outputs = np.concatenate((outputs_fw, outputs_bw), -1) outputs = np.concatenate((outputs_fw, outputs_bw), -1)
final_states = (states_fw, states_bw) final_states = (states_fw, states_bw)
return outputs, final_states return outputs, final_states
def flatten(nested): def flatten(nested):
return list(_flatten(nested)) return list(_flatten(nested))
def _flatten(nested): def _flatten(nested):
for item in nested: for item in nested:
if isinstance(item, (list, tuple)): if isinstance(item, (list, tuple)):
for subitem in _flatten(item): for subitem in _flatten(item):
yield subitem yield subitem
else: else:
yield item yield item
def unstack(array, axis=0): def unstack(array, axis=0):
num = array.shape[axis] num = array.shape[axis]
sub_arrays = np.split(array, num, axis) sub_arrays = np.split(array, num, axis)
return [np.squeeze(sub_array, axis) for sub_array in sub_arrays] return [np.squeeze(sub_array, axis) for sub_array in sub_arrays]
def dropout(array, p=0.0): def dropout(array, p=0.0):
if p == 0.0: if p == 0.0:
return array return array
mask = (np.random.uniform(size=array.shape) < (1 - p)).astype(array.dtype) mask = (np.random.uniform(size=array.shape) < (1 - p)).astype(array.dtype)
return array * (mask / (1 - p)) return array * (mask / (1 - p))
def split_states(states, bidirectional=False, state_components=1): def split_states(states, bidirectional=False, state_components=1):
if state_components == 1: if state_components == 1:
states = unstack(states) states = unstack(states)
if not bidirectional: if not bidirectional:
return states return states
else: else:
return list(zip(states[::2], states[1::2])) return list(zip(states[::2], states[1::2]))
else: else:
assert len(states) == state_components assert len(states) == state_components
states = tuple([unstack(item) for item in states]) states = tuple([unstack(item) for item in states])
if not bidirectional: if not bidirectional:
return list(zip(*states)) return list(zip(*states))
else: else:
states = list(zip(*states)) states = list(zip(*states))
return list(zip(states[::2], states[1::2])) return list(zip(states[::2], states[1::2]))
def concat_states(states, bidirectional=False, state_components=1): def concat_states(states, bidirectional=False, state_components=1):
if state_components == 1: if state_components == 1:
return np.stack(flatten(states)) return np.stack(flatten(states))
else: else:
states = flatten(states) states = flatten(states)
componnets = [] componnets = []
for i in range(state_components): for i in range(state_components):
componnets.append(states[i::state_components]) componnets.append(states[i::state_components])
return [np.stack(item) for item in componnets] return [np.stack(item) for item in componnets]
class RNN(LayerMixin): class RNN(LayerMixin):
def __init__(self, cell, is_reverse=False, time_major=False): def __init__(self, cell, is_reverse=False, time_major=False):
super(RNN, self).__init__() super(RNN, self).__init__()
self.cell = cell self.cell = cell
if not hasattr(self.cell, "call"): if not hasattr(self.cell, "call"):
# for non-dygraph mode, `rnn` api uses cell.call # for non-dygraph mode, `rnn` api uses cell.call
self.cell.call = self.cell.forward self.cell.call = self.cell.forward
self.is_reverse = is_reverse self.is_reverse = is_reverse
self.time_major = time_major self.time_major = time_major
def forward(self, inputs, initial_states=None, sequence_length=None): def forward(self, inputs, initial_states=None, sequence_length=None):
final_outputs, final_states = rnn(self.cell, final_outputs, final_states = rnn(self.cell,
inputs, inputs,
initial_states=initial_states, initial_states=initial_states,
sequence_length=sequence_length, sequence_length=sequence_length,
time_major=self.time_major, time_major=self.time_major,
is_reverse=self.is_reverse) is_reverse=self.is_reverse)
return final_outputs, final_states return final_outputs, final_states
class BiRNN(LayerMixin): class BiRNN(LayerMixin):
def __init__(self, cell_fw, cell_bw, time_major=False): def __init__(self, cell_fw, cell_bw, time_major=False):
super(BiRNN, self).__init__() super(BiRNN, self).__init__()
self.cell_fw = cell_fw self.cell_fw = cell_fw
self.cell_bw = cell_bw self.cell_bw = cell_bw
self.time_major = time_major self.time_major = time_major
def forward(self, def forward(self,
inputs, inputs,
initial_states=None, initial_states=None,
sequence_length=None, sequence_length=None,
**kwargs): **kwargs):
if isinstance(initial_states, (list, tuple)): if isinstance(initial_states, (list, tuple)):
assert len(initial_states) == 2, \ assert len(initial_states) == 2, \
"length of initial_states should be 2 when it is a list/tuple" "length of initial_states should be 2 when it is a list/tuple"
else: else:
initial_states = [initial_states, initial_states] initial_states = [initial_states, initial_states]
outputs, final_states = birnn(self.cell_fw, self.cell_bw, inputs, outputs, final_states = birnn(self.cell_fw, self.cell_bw, inputs,
initial_states, sequence_length, initial_states, sequence_length,
self.time_major) self.time_major)
return outputs, final_states return outputs, final_states
class RNNMixin(LayerListMixin): class RNNMixin(LayerListMixin):
def forward(self, inputs, initial_states=None, sequence_length=None): def forward(self, inputs, initial_states=None, sequence_length=None):
batch_index = 1 if self.time_major else 0 batch_index = 1 if self.time_major else 0
batch_size = inputs.shape[batch_index] batch_size = inputs.shape[batch_index]
dtype = inputs.dtype dtype = inputs.dtype
if initial_states is None: if initial_states is None:
state_shape = (self.num_layers * self.num_directions, batch_size, state_shape = (self.num_layers * self.num_directions, batch_size,
self.hidden_size) self.hidden_size)
if self.state_components == 1: if self.state_components == 1:
initial_states = np.zeros(state_shape, dtype) initial_states = np.zeros(state_shape, dtype)
else: else:
initial_states = tuple([ initial_states = tuple([
np.zeros(state_shape, dtype) np.zeros(state_shape, dtype)
for _ in range(self.state_components) for _ in range(self.state_components)
]) ])
states = split_states(initial_states, self.num_directions == 2, states = split_states(initial_states, self.num_directions == 2,
self.state_components) self.state_components)
final_states = [] final_states = []
for i, rnn_layer in enumerate(self): for i, rnn_layer in enumerate(self):
if i > 0: if i > 0:
inputs = dropout(inputs, self.dropout) inputs = dropout(inputs, self.dropout)
outputs, final_state = rnn_layer(inputs, states[i], sequence_length) outputs, final_state = rnn_layer(inputs, states[i], sequence_length)
final_states.append(final_state) final_states.append(final_state)
inputs = outputs inputs = outputs
final_states = concat_states(final_states, self.num_directions == 2, final_states = concat_states(final_states, self.num_directions == 2,
self.state_components) self.state_components)
return outputs, final_states return outputs, final_states
class LSTM(RNNMixin): class LSTM(RNNMixin):
def __init__(self, def __init__(self,
input_size, input_size,
hidden_size, hidden_size,
num_layers=1, num_layers=1,
direction="forward", direction="forward",
dropout=0., dropout=0.,
time_major=False): time_major=False):
super(LSTM, self).__init__() super(LSTM, self).__init__()
if direction in ["forward", "backward"]: if direction in ["forward", "backward"]:
is_reverse = direction == "backward" is_reverse = direction == "backward"
cell = LSTMCell(input_size, hidden_size) cell = LSTMCell(input_size, hidden_size)
self.append(RNN(cell, is_reverse, time_major)) self.append(RNN(cell, is_reverse, time_major))
for i in range(1, num_layers): for i in range(1, num_layers):
cell = LSTMCell(hidden_size, hidden_size) cell = LSTMCell(hidden_size, hidden_size)
self.append(RNN(cell, is_reverse, time_major)) self.append(RNN(cell, is_reverse, time_major))
elif direction == "bidirectional": elif direction == "bidirectional":
cell_fw = LSTMCell(input_size, hidden_size) cell_fw = LSTMCell(input_size, hidden_size)
cell_bw = LSTMCell(input_size, hidden_size) cell_bw = LSTMCell(input_size, hidden_size)
self.append(BiRNN(cell_fw, cell_bw, time_major)) self.append(BiRNN(cell_fw, cell_bw, time_major))
for i in range(1, num_layers): for i in range(1, num_layers):
cell_fw = LSTMCell(2 * hidden_size, hidden_size) cell_fw = LSTMCell(2 * hidden_size, hidden_size)
cell_bw = LSTMCell(2 * hidden_size, hidden_size) cell_bw = LSTMCell(2 * hidden_size, hidden_size)
self.append(BiRNN(cell_fw, cell_bw, time_major)) self.append(BiRNN(cell_fw, cell_bw, time_major))
else: else:
raise ValueError( raise ValueError(
"direction should be forward, backward or bidirectional, " "direction should be forward, backward or bidirectional, "
"received direction = {}".format(direction)) "received direction = {}".format(direction))
self.input_size = input_size self.input_size = input_size
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.dropout = dropout self.dropout = dropout
self.num_directions = 2 if direction == "bidirectional" else 1 self.num_directions = 2 if direction == "bidirectional" else 1
self.time_major = time_major self.time_major = time_major
self.num_layers = num_layers self.num_layers = num_layers
self.state_components = 2 self.state_components = 2
@unittest.skipIf(not core.is_compiled_with_cuda(), @unittest.skipIf(not core.is_compiled_with_cuda(),
"core is not compiled with CUDA") "core is not compiled with CUDA")
class TestCUDNNLstmOp(OpTest): class TestCUDNNLstmOp(OpTest):
def get_weight_names(self): def get_weight_names(self):
weight_names = [] weight_names = []
for i in range(2 * self.num_layers): for i in range(2 * self.num_layers):
weight_names.append('weight{}'.format(i)) weight_names.append('weight{}'.format(i))
for i in range(2 * self.num_layers): for i in range(2 * self.num_layers):
weight_names.append('bias{}'.format(i)) weight_names.append('bias{}'.format(i))
return weight_names return weight_names
def setUp(self): def setUp(self):
self.op_type = "cudnn_lstm" self.op_type = "cudnn_lstm"
self.dtype = np.float32 if core.is_compiled_with_rocm() else np.float64 self.dtype = np.float32 if core.is_compiled_with_rocm() else np.float64
self.sequence_length = None if core.is_compiled_with_rocm( self.sequence_length = None if core.is_compiled_with_rocm(
) else np.array([12, 11, 10, 9, 8], dtype=np.int32) ) else np.array([12, 11, 10, 9, 8], dtype=np.int32)
self.num_layers = 1 self.num_layers = 1
self.set_attrs() self.set_attrs()
seq_length = 12 seq_length = 12
batch_size = 5 batch_size = 5
input_size = 21 input_size = 21
hidden_size = 21 hidden_size = 21
input = np.random.uniform(low=-0.1, input = np.random.uniform(low=-0.1,
high=0.1, high=0.1,
size=(seq_length, batch_size, size=(seq_length, batch_size,
input_size)).astype(self.dtype) input_size)).astype(self.dtype)
input[11][1:][:] = 0 input[11][1:][:] = 0
input[10][2:][:] = 0 input[10][2:][:] = 0
input[9][3:][:] = 0 input[9][3:][:] = 0
input[8][4:][:] = 0 input[8][4:][:] = 0
weight.updata_weight(hidden_size, input_size, self.dtype) weight.updata_weight(hidden_size, input_size, self.dtype)
rnn1 = LSTM(input_size, rnn1 = LSTM(input_size,
hidden_size, hidden_size,
num_layers=self.num_layers, num_layers=self.num_layers,
time_major=True, time_major=True,
direction="forward") direction="forward")
output, (last_hidden, output, (last_hidden,
last_cell) = rnn1(input, sequence_length=self.sequence_length) last_cell) = rnn1(input, sequence_length=self.sequence_length)
flat_w = [] flat_w = []
num = 0 num = 0
for i in range(self.num_layers): for i in range(self.num_layers):
if i == 0: if i == 0:
weight_ih = weight.weight_ih weight_ih = weight.weight_ih
else: else:
weight_ih = weight.weight_hh weight_ih = weight.weight_hh
flat_w.append(("weight" + str(num), weight_ih)) flat_w.append(("weight" + str(num), weight_ih))
num += 1 num += 1
for i in range(self.num_layers): for i in range(self.num_layers):
weight_hh = weight.weight_hh weight_hh = weight.weight_hh
flat_w.append(("weight" + str(num), weight_hh)) flat_w.append(("weight" + str(num), weight_hh))
num += 1 num += 1
num = 0 num = 0
for i in range(self.num_layers): for i in range(self.num_layers):
bias_ih = weight.bias_ih bias_ih = weight.bias_ih
flat_w.append(("bias" + str(num), bias_ih)) flat_w.append(("bias" + str(num), bias_ih))
num += 1 num += 1
for i in range(self.num_layers): for i in range(self.num_layers):
bias_hh = weight.bias_hh bias_hh = weight.bias_hh
flat_w.append(("bias" + str(num), bias_hh)) flat_w.append(("bias" + str(num), bias_hh))
num += 1 num += 1
init_h = np.zeros( init_h = np.zeros(
(self.num_layers, batch_size, hidden_size)).astype(self.dtype) (self.num_layers, batch_size, hidden_size)).astype(self.dtype)
init_c = np.zeros( init_c = np.zeros(
(self.num_layers, batch_size, hidden_size)).astype(self.dtype) (self.num_layers, batch_size, hidden_size)).astype(self.dtype)
state_out = np.ndarray((300)).astype("uint8") state_out = np.ndarray((300)).astype("uint8")
if core.is_compiled_with_rocm(): if core.is_compiled_with_rocm():
for i in range(len(flat_w)): for i in range(len(flat_w)):
w = np.split(flat_w[i][1], 4, 0) w = np.split(flat_w[i][1], 4, 0)
w = [w[0], w[1], w[3], w[2]] w = [w[0], w[1], w[3], w[2]]
w = np.concatenate(w) w = np.concatenate(w)
flat_w[i] = (flat_w[i][0], w) flat_w[i] = (flat_w[i][0], w)
self.inputs = { self.inputs = {
'Input': input, 'Input': input,
'WeightList': flat_w, 'WeightList': flat_w,
'InitH': init_h, 'InitH': init_h,
'InitC': init_c, 'InitC': init_c,
'SequenceLength': self.sequence_length 'SequenceLength': self.sequence_length
} }
if self.sequence_length is None: if self.sequence_length is None:
self.inputs = { self.inputs = {
'Input': input, 'Input': input,
'WeightList': flat_w, 'WeightList': flat_w,
'InitH': init_h, 'InitH': init_h,
'InitC': init_c, 'InitC': init_c,
} }
self.attrs = { self.attrs = {
'dropout_prob': 0.0, 'dropout_prob': 0.0,
'is_bidirec': False, 'is_bidirec': False,
'input_size': input_size, 'input_size': input_size,
'hidden_size': hidden_size, 'hidden_size': hidden_size,
'num_layers': self.num_layers, 'num_layers': self.num_layers,
} }
self.outputs = { self.outputs = {
'Out': output, 'Out': output,
"LastH": last_hidden, "LastH": last_hidden,
'LastC': last_cell, 'LastC': last_cell,
'Reserve': np.ndarray((400)).astype("uint8"), 'Reserve': np.ndarray((400)).astype("uint8"),
'StateOut': state_out 'StateOut': state_out
} }
def set_attrs(self): def set_attrs(self):
pass pass
def test_output_with_place(self): def test_output_with_place(self):
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
if core.is_compiled_with_rocm(): if core.is_compiled_with_rocm():
self.check_output_with_place(place, self.check_output_with_place(place,
atol=1e-5, atol=1e-5,
no_check_set=['Reserve', 'StateOut']) no_check_set=['Reserve', 'StateOut'])
else: else:
self.check_output_with_place(place, self.check_output_with_place(place,
no_check_set=['Reserve', 'StateOut']) no_check_set=['Reserve', 'StateOut'])
def test_grad_with_place(self): def test_grad_with_place(self):
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
var_name_list = self.get_weight_names() var_name_list = self.get_weight_names()
for var_name in var_name_list: for var_name in var_name_list:
self.check_grad_with_place( self.check_grad_with_place(
place, set(['Input', var_name, 'InitH', 'InitC']), place, set(['Input', var_name, 'InitH', 'InitC']),
['Out', 'LastH', 'LastC']) ['Out', 'LastH', 'LastC'])
@unittest.skipIf(not core.is_compiled_with_cuda(), @unittest.skipIf(not core.is_compiled_with_cuda(),
"core is not compiled with CUDA") "core is not compiled with CUDA")
class TestCUDNNlstmAPI(unittest.TestCase): class TestCUDNNlstmAPI(unittest.TestCase):
def test_lstm(self): def test_lstm(self):
seq_len = 20 seq_len = 20
batch_size = 5 batch_size = 5
hidden_size = 20 hidden_size = 20
dropout_prob = 0.0 dropout_prob = 0.0
num_layers = 1 num_layers = 1
dtype = 'float32' if core.is_compiled_with_rocm() else 'float64' dtype = 'float32' if core.is_compiled_with_rocm() else 'float64'
input = fluid.data(name='input', input = fluid.data(name='input',
shape=[seq_len, batch_size, hidden_size], shape=[seq_len, batch_size, hidden_size],
dtype=dtype) dtype=dtype)
init_h = layers.fill_constant([num_layers, batch_size, hidden_size], init_h = layers.fill_constant([num_layers, batch_size, hidden_size],
dtype, 0.0) dtype, 0.0)
init_c = layers.fill_constant([num_layers, batch_size, hidden_size], init_c = layers.fill_constant([num_layers, batch_size, hidden_size],
dtype, 0.0) dtype, 0.0)
rnn_out, last_h, last_c = layers.lstm(input, init_h, init_c, seq_len, rnn_out, last_h, last_c = layers.lstm(input, init_h, init_c, seq_len,
hidden_size, num_layers, hidden_size, num_layers,
dropout_prob, False) dropout_prob, False)
exe = fluid.Executor(fluid.CUDAPlace(0)) exe = fluid.Executor(fluid.CUDAPlace(0))
exe.run(fluid.default_startup_program()) exe.run(fluid.default_startup_program())
input_i = np.random.uniform(low=-0.1, input_i = np.random.uniform(low=-0.1,
high=0.1, high=0.1,
size=(seq_len, batch_size, size=(seq_len, batch_size,
hidden_size)).astype("float64") hidden_size)).astype("float64")
out = exe.run(fluid.default_main_program(), out = exe.run(fluid.default_main_program(),
feed={'input': input_i}, feed={'input': input_i},
fetch_list=[rnn_out, last_h, last_c, 'cudnn_lstm_0.w_0']) fetch_list=[rnn_out, last_h, last_c, 'cudnn_lstm_0.w_0'])
@unittest.skipIf(not core.is_compiled_with_cuda(), @unittest.skipIf(not core.is_compiled_with_cuda(),
"core is not compiled with CUDA") "core is not compiled with CUDA")
class TestCUDNNlstmAPI(unittest.TestCase): class TestCUDNNlstmAPI(unittest.TestCase):
def test_lstm(self): def test_lstm(self):
seq_len = 20 seq_len = 20
batch_size = 5 batch_size = 5
hidden_size = 20 hidden_size = 20
dropout_prob = 0.0 dropout_prob = 0.0
num_layers = 2 num_layers = 2
dtype = 'float32' if core.is_compiled_with_rocm() else 'float64' dtype = 'float32' if core.is_compiled_with_rocm() else 'float64'
input = fluid.data(name='input', input = fluid.data(name='input',
shape=[seq_len, batch_size, hidden_size], shape=[seq_len, batch_size, hidden_size],
dtype=dtype) dtype=dtype)
init_h = layers.fill_constant([num_layers, batch_size, hidden_size], init_h = layers.fill_constant([num_layers, batch_size, hidden_size],
dtype, 0.0) dtype, 0.0)
init_c = layers.fill_constant([num_layers, batch_size, hidden_size], init_c = layers.fill_constant([num_layers, batch_size, hidden_size],
dtype, 0.0) dtype, 0.0)
rnn_out, last_h, last_c = layers.lstm(input, init_h, init_c, seq_len, rnn_out, last_h, last_c = layers.lstm(input, init_h, init_c, seq_len,
hidden_size, num_layers, hidden_size, num_layers,
dropout_prob, False, True) dropout_prob, False, True)
exe = fluid.Executor(fluid.CUDAPlace(0)) exe = fluid.Executor(fluid.CUDAPlace(0))
exe.run(fluid.default_startup_program()) exe.run(fluid.default_startup_program())
input_i = np.random.uniform(low=-0.1, input_i = np.random.uniform(low=-0.1,
high=0.1, high=0.1,
size=(seq_len, batch_size, size=(seq_len, batch_size,
hidden_size)).astype(dtype) hidden_size)).astype(dtype)
out = exe.run(fluid.default_main_program(), out = exe.run(fluid.default_main_program(),
feed={'input': input_i}, feed={'input': input_i},
fetch_list=[rnn_out, last_h, last_c, 'cudnn_lstm_0.w_0']) fetch_list=[rnn_out, last_h, last_c, 'cudnn_lstm_0.w_0'])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
import math import math
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.layers as layers import paddle.fluid.layers as layers
import random import random
import sys import sys
from op_test import OpTest from op_test import OpTest
sys.path.append("./rnn") sys.path.append("./rnn")
from rnn_numpy import SimpleRNN, LSTM, GRU from rnn_numpy import SimpleRNN, LSTM, GRU
from convert import get_params_for_net from convert import get_params_for_net
random.seed(2) random.seed(2)
np.set_printoptions(threshold=np.inf) np.set_printoptions(threshold=np.inf)
paddle.enable_static() paddle.enable_static()
class TestRNNOp(OpTest): class TestRNNOp(OpTest):
def get_weight_names(self): def get_weight_names(self):
weight_names = [] weight_names = []
for i in range(self.num_layers): for i in range(self.num_layers):
for j in range(0, 2 * self.direction_num): for j in range(0, 2 * self.direction_num):
weight_names.append("{}.weight_{}".format(i, j)) weight_names.append("{}.weight_{}".format(i, j))
for i in range(self.num_layers): for i in range(self.num_layers):
for j in range(0, 2 * self.direction_num): for j in range(0, 2 * self.direction_num):
weight_names.append("{}.bias_{}".format(i, j)) weight_names.append("{}.bias_{}".format(i, j))
return weight_names return weight_names
def setUp(self): def setUp(self):
self.op_type = "rnn" self.op_type = "rnn"
self.dtype = np.float32 if core.is_compiled_with_rocm() else np.float64 self.dtype = np.float32 if core.is_compiled_with_rocm() else np.float64
self.sequence_length = None if core.is_compiled_with_rocm( self.sequence_length = None if core.is_compiled_with_rocm(
) else np.array([12, 11, 10, 9, 8], dtype=np.int32) ) else np.array([12, 11, 10, 9, 8], dtype=np.int32)
self.num_layers = 1 self.num_layers = 1
self.is_bidirec = False self.is_bidirec = False
self.mode = "LSTM" self.mode = "LSTM"
self.is_test = False self.is_test = False
self.dropout = 0.0 self.dropout = 0.0
self.set_attrs() self.set_attrs()
self.direction_num = 2 if self.is_bidirec else 1 self.direction_num = 2 if self.is_bidirec else 1
direction = "bidirectional" if self.is_bidirec else "forward" direction = "bidirectional" if self.is_bidirec else "forward"
seq_length = 12 seq_length = 12
batch_size = 5 batch_size = 5
input_size = 3 input_size = 3
hidden_size = 2 hidden_size = 2
input = np.random.uniform(low=-0.1, input = np.random.uniform(low=-0.1,
high=0.1, high=0.1,
size=(seq_length, batch_size, size=(seq_length, batch_size,
input_size)).astype(self.dtype) input_size)).astype(self.dtype)
if self.sequence_length is not None: if self.sequence_length is not None:
input[11][1:][:] = 0 input[11][1:][:] = 0
input[10][2:][:] = 0 input[10][2:][:] = 0
input[9][3:][:] = 0 input[9][3:][:] = 0
input[8][4:][:] = 0 input[8][4:][:] = 0
rnn1 = LSTM(input_size, rnn1 = LSTM(input_size,
hidden_size, hidden_size,
num_layers=self.num_layers, num_layers=self.num_layers,
time_major=True, time_major=True,
direction=direction, direction=direction,
dropout=self.dropout, dropout=self.dropout,
dtype=self.dtype) dtype=self.dtype)
flat_w = get_params_for_net(rnn1) flat_w = get_params_for_net(rnn1)
output, (last_hidden, output, (last_hidden,
last_cell) = rnn1(input, sequence_length=self.sequence_length) last_cell) = rnn1(input, sequence_length=self.sequence_length)
if core.is_compiled_with_rocm(): if core.is_compiled_with_rocm():
def rocm_rnn_get_place(): def rocm_rnn_get_place():
places = [core.CUDAPlace(0)] places = [core.CUDAPlace(0)]
return places return places
self._get_places = rocm_rnn_get_place self._get_places = rocm_rnn_get_place
init_h = np.zeros((self.num_layers * self.direction_num, batch_size, init_h = np.zeros((self.num_layers * self.direction_num, batch_size,
hidden_size)).astype(self.dtype) hidden_size)).astype(self.dtype)
init_c = np.zeros((self.num_layers * self.direction_num, batch_size, init_c = np.zeros((self.num_layers * self.direction_num, batch_size,
hidden_size)).astype(self.dtype) hidden_size)).astype(self.dtype)
state_out = np.ndarray((300)).astype("uint8") state_out = np.ndarray((300)).astype("uint8")
self.inputs = { self.inputs = {
'Input': input, 'Input': input,
'WeightList': flat_w, 'WeightList': flat_w,
'PreState': [('init_h', init_h), ('init_c', init_c)], 'PreState': [('init_h', init_h), ('init_c', init_c)],
'SequenceLength': self.sequence_length 'SequenceLength': self.sequence_length
} }
if self.sequence_length is None: if self.sequence_length is None:
self.inputs = { self.inputs = {
'Input': input, 'Input': input,
'WeightList': flat_w, 'WeightList': flat_w,
'PreState': [('init_h', init_h), ('init_c', init_c)], 'PreState': [('init_h', init_h), ('init_c', init_c)],
} }
self.attrs = { self.attrs = {
'dropout_prob': self.dropout, 'dropout_prob': self.dropout,
'is_bidirec': self.is_bidirec, 'is_bidirec': self.is_bidirec,
'input_size': input_size, 'input_size': input_size,
'hidden_size': hidden_size, 'hidden_size': hidden_size,
'num_layers': self.num_layers, 'num_layers': self.num_layers,
'mode': self.mode, 'mode': self.mode,
'is_test': self.is_test 'is_test': self.is_test
} }
self.outputs = { self.outputs = {
'Out': output, 'Out': output,
"State": [('last_hidden', last_hidden), ('last_cell', last_cell)], "State": [('last_hidden', last_hidden), ('last_cell', last_cell)],
'Reserve': np.ndarray((400)).astype("uint8"), 'Reserve': np.ndarray((400)).astype("uint8"),
'DropoutState': state_out 'DropoutState': state_out
} }
def test_output(self): def test_output(self):
self.check_output(no_check_set=['Reserve', 'DropoutState']) self.check_output(no_check_set=['Reserve', 'DropoutState'])
def set_attrs(self): def set_attrs(self):
pass pass
def test_grad(self): def test_grad(self):
if not self.is_test: if not self.is_test:
var_name_list = self.get_weight_names() var_name_list = self.get_weight_names()
grad_check_list = ['Input', 'init_h', 'init_c'] grad_check_list = ['Input', 'init_h', 'init_c']
grad_check_list.extend(var_name_list) grad_check_list.extend(var_name_list)
self.check_grad(set(grad_check_list), self.check_grad(set(grad_check_list),
['Out', 'last_hidden', 'last_cell']) ['Out', 'last_hidden', 'last_cell'])
class TestRNNOp1(TestRNNOp): class TestRNNOp1(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.sequence_length = None self.sequence_length = None
class TestRNNOp2(TestRNNOp): class TestRNNOp2(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.sequence_length = None self.sequence_length = None
self.is_bidirec = True self.is_bidirec = True
class TestRNNOp3(TestRNNOp): class TestRNNOp3(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.is_test = True self.is_test = True
self.sequence_length = None self.sequence_length = None
class TestRNNOp4(TestRNNOp): class TestRNNOp4(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.is_test = True self.is_test = True
self.sequence_length = None self.sequence_length = None
self.is_bidirec = True self.is_bidirec = True
class TestRNNOp5(TestRNNOp): class TestRNNOp5(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.num_layers = 2 self.num_layers = 2
class TestRNNOp6(TestRNNOp): class TestRNNOp6(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.num_layers = 2 self.num_layers = 2
self.is_bidirec = True self.is_bidirec = True
class TestRNNOp7(TestRNNOp): class TestRNNOp7(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.num_layers = 2 self.num_layers = 2
self.is_bidirec = True self.is_bidirec = True
self.is_test = True self.is_test = True
class TestRNNOp8(TestRNNOp): class TestRNNOp8(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.num_layers = 2 self.num_layers = 2
self.is_bidirec = True self.is_bidirec = True
self.sequence_length = None self.sequence_length = None
class TestRNNOp9(TestRNNOp): class TestRNNOp9(TestRNNOp):
def set_attrs(self): def set_attrs(self):
self.num_layers = 3 self.num_layers = 3
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -21,7 +21,7 @@ def memory_optimize(input_program, ...@@ -21,7 +21,7 @@ def memory_optimize(input_program,
level=0, level=0,
skip_grads=True): skip_grads=True):
""" """
:api_attr: Static Graph :api_attr: Static Graph
This API is deprecated since 1.6. Please do not use it. The better This API is deprecated since 1.6. Please do not use it. The better
memory optimization strategies are enabled by default. memory optimization strategies are enabled by default.
...@@ -43,7 +43,7 @@ def memory_optimize(input_program, ...@@ -43,7 +43,7 @@ def memory_optimize(input_program,
def release_memory(input_program, skip_opt_set=None): def release_memory(input_program, skip_opt_set=None):
""" """
:api_attr: Static Graph :api_attr: Static Graph
This API is deprecated since 1.6. Please do not use it. The better This API is deprecated since 1.6. Please do not use it. The better
memory optimization strategies are enabled by default. memory optimization strategies are enabled by default.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册