未验证 提交 87f72107 编写于 作者: L LoneRanger 提交者: GitHub

remove nets.py in fluid (#51717)

* remove function sequence_conv_pool

* fix test_glu.py

* remove function scaled_dot_product_attention

* remove function img_conv_group

* remove function simple_img_conv_pool

* delete the nets.py

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* Update test_image_classification.py

* Update test_recognize_digits.py

* Update test_recommender_system.py

* Update test_image_classification_fp16.py

* fix bug

* fix bug

* fix bug

* remove the nets.py in fluid

* fix bug

* fix the codestyle

* fix conflict

* fix bug

* Fix TODO
上级 272ed912
...@@ -54,7 +54,6 @@ from .initializer import set_global_initializer ...@@ -54,7 +54,6 @@ from .initializer import set_global_initializer
from . import layers from . import layers
from . import dygraph from . import dygraph
from . import contrib from . import contrib
from . import nets
from . import optimizer from . import optimizer
from . import backward from . import backward
from .backward import gradients from .backward import gradients
...@@ -112,7 +111,6 @@ __all__ = ( ...@@ -112,7 +111,6 @@ __all__ = (
'disable_dygraph', 'disable_dygraph',
'enable_imperative', 'enable_imperative',
'disable_imperative', 'disable_imperative',
'nets',
'optimizer', 'optimizer',
'backward', 'backward',
'LoDTensor', 'LoDTensor',
......
...@@ -20,6 +20,10 @@ import unittest ...@@ -20,6 +20,10 @@ import unittest
import numpy as np import numpy as np
# TODO: remove sys.path.append
sys.path.append("../legacy_test")
import nets
import paddle import paddle
from paddle import fluid from paddle import fluid
...@@ -30,14 +34,14 @@ def convolution_net( ...@@ -30,14 +34,14 @@ def convolution_net(
emb = fluid.layers.embedding( emb = fluid.layers.embedding(
input=data, size=[input_dim, emb_dim], is_sparse=True input=data, size=[input_dim, emb_dim], is_sparse=True
) )
conv_3 = fluid.nets.sequence_conv_pool( conv_3 = nets.sequence_conv_pool(
input=emb, input=emb,
num_filters=hid_dim, num_filters=hid_dim,
filter_size=3, filter_size=3,
act="tanh", act="tanh",
pool_type="sqrt", pool_type="sqrt",
) )
conv_4 = fluid.nets.sequence_conv_pool( conv_4 = nets.sequence_conv_pool(
input=emb, input=emb,
num_filters=hid_dim, num_filters=hid_dim,
filter_size=4, filter_size=4,
......
...@@ -21,6 +21,10 @@ import unittest ...@@ -21,6 +21,10 @@ import unittest
import numpy import numpy
# TODO: remove sys.path.append
sys.path.append("../legacy_test")
import nets
import paddle import paddle
from paddle import fluid from paddle import fluid
...@@ -74,7 +78,7 @@ def resnet_cifar10(input, depth=32): ...@@ -74,7 +78,7 @@ def resnet_cifar10(input, depth=32):
def vgg16_bn_drop(input): def vgg16_bn_drop(input):
def conv_block(input, num_filter, groups, dropouts): def conv_block(input, num_filter, groups, dropouts):
return fluid.nets.img_conv_group( return nets.img_conv_group(
input=input, input=input,
pool_size=2, pool_size=2,
pool_stride=2, pool_stride=2,
......
...@@ -19,6 +19,10 @@ import unittest ...@@ -19,6 +19,10 @@ import unittest
import numpy import numpy
# TODO: remove sys.path.append
sys.path.append("../legacy_test")
import nets
import paddle import paddle
from paddle import fluid from paddle import fluid
from paddle.fluid import core from paddle.fluid import core
...@@ -45,7 +49,7 @@ def mlp(img, label): ...@@ -45,7 +49,7 @@ def mlp(img, label):
def conv_net(img, label): def conv_net(img, label):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=img, input=img,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -54,7 +58,7 @@ def conv_net(img, label): ...@@ -54,7 +58,7 @@ def conv_net(img, label):
act="relu", act="relu",
) )
conv_pool_1 = paddle.static.nn.batch_norm(conv_pool_1) conv_pool_1 = paddle.static.nn.batch_norm(conv_pool_1)
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -19,9 +19,13 @@ import tempfile ...@@ -19,9 +19,13 @@ import tempfile
import numpy as np import numpy as np
# TODO: remove sys.path.append
sys.path.append("../legacy_test")
import nets
import paddle import paddle
from paddle import fluid from paddle import fluid
from paddle.fluid import framework, layers, nets from paddle.fluid import framework, layers
from paddle.fluid.executor import Executor from paddle.fluid.executor import Executor
from paddle.fluid.optimizer import SGDOptimizer from paddle.fluid.optimizer import SGDOptimizer
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
from legacy_test import nets
from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -42,7 +43,7 @@ def cnn_model(data): ...@@ -42,7 +43,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
from legacy_test import nets
from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -42,7 +43,7 @@ def cnn_model(data): ...@@ -42,7 +43,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
from legacy_test import nets
from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -42,7 +43,7 @@ def cnn_model(data): ...@@ -42,7 +43,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -22,6 +22,10 @@ import unittest ...@@ -22,6 +22,10 @@ import unittest
import numpy import numpy
# TODO: remove sys.path.append
sys.path.append("../legacy_test")
import nets
import paddle import paddle
from paddle import fluid from paddle import fluid
from paddle.static.amp import decorate from paddle.static.amp import decorate
...@@ -76,7 +80,7 @@ def resnet_cifar10(input, depth=32): ...@@ -76,7 +80,7 @@ def resnet_cifar10(input, depth=32):
def vgg16_bn_drop(input): def vgg16_bn_drop(input):
def conv_block(input, num_filter, groups, dropouts): def conv_block(input, num_filter, groups, dropouts):
return fluid.nets.img_conv_group( return nets.img_conv_group(
input=input, input=input,
pool_size=2, pool_size=2,
pool_stride=2, pool_stride=2,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
import nets
from test_dist_base import TestDistRunnerBase, runtime_main from test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -42,7 +43,7 @@ def cnn_model(data): ...@@ -42,7 +43,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
import nets
from test_dist_base import TestDistRunnerBase, runtime_main from test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -32,7 +33,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -32,7 +33,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -43,7 +44,7 @@ def cnn_model(data): ...@@ -43,7 +44,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
import nets
from test_dist_base import TestDistRunnerBase, runtime_main from test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -32,7 +33,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -32,7 +33,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -43,7 +44,7 @@ def cnn_model(data): ...@@ -43,7 +44,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
import nets
from test_dist_base import TestDistRunnerBase, runtime_main from test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -31,7 +32,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -42,7 +43,7 @@ def cnn_model(data): ...@@ -42,7 +43,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from functools import reduce from functools import reduce
from legacy_test.nets import simple_img_conv_pool
from legacy_test.test_dist_base import ( from legacy_test.test_dist_base import (
TestDistRunnerBase, TestDistRunnerBase,
_insert_comm_op, _insert_comm_op,
...@@ -34,7 +35,7 @@ fluid.default_main_program().random_seed = 1 ...@@ -34,7 +35,7 @@ fluid.default_main_program().random_seed = 1
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -45,7 +46,7 @@ def cnn_model(data): ...@@ -45,7 +46,7 @@ def cnn_model(data):
initializer=paddle.nn.initializer.Constant(value=0.01) initializer=paddle.nn.initializer.Constant(value=0.01)
), ),
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -17,6 +17,7 @@ import re ...@@ -17,6 +17,7 @@ import re
import string import string
import tarfile import tarfile
import nets
from test_dist_base import TestDistRunnerBase, runtime_main from test_dist_base import TestDistRunnerBase, runtime_main
import paddle import paddle
...@@ -63,7 +64,7 @@ def conv_net( ...@@ -63,7 +64,7 @@ def conv_net(
), ),
) )
conv_3 = fluid.nets.sequence_conv_pool( conv_3 = nets.sequence_conv_pool(
input=emb, input=emb,
num_filters=num_filters, num_filters=num_filters,
filter_size=window_size, filter_size=window_size,
......
...@@ -13,10 +13,8 @@ ...@@ -13,10 +13,8 @@
# limitations under the License. # limitations under the License.
import paddle import paddle
from . import layers from paddle.fluid.data_feeder import check_variable_and_dtype, convert_dtype
from .data_feeder import check_variable_and_dtype, convert_dtype from paddle.utils import deprecated
from ..utils import deprecated
import paddle
__all__ = [ __all__ = [
"simple_img_conv_pool", "simple_img_conv_pool",
...@@ -494,9 +492,8 @@ def scaled_dot_product_attention( ...@@ -494,9 +492,8 @@ def scaled_dot_product_attention(
if not (queries.dtype == keys.dtype == values.dtype): if not (queries.dtype == keys.dtype == values.dtype):
raise TypeError( raise TypeError(
"The dtype of keys, values and queries should be the same." "The dtype of keys, values and queries should be the same."
"But received queries.dtype = %s, " "But received queries.dtype = {}, "
" keys.dtype = %s, values.dtype) = %s." " keys.dtype = {}, values.dtype) = {}.".format(
% (
convert_dtype(queries.dtype), convert_dtype(queries.dtype),
convert_dtype(keys.dtype), convert_dtype(keys.dtype),
convert_dtype(values.dtype), convert_dtype(values.dtype),
......
...@@ -16,6 +16,8 @@ import collections ...@@ -16,6 +16,8 @@ import collections
import functools import functools
import unittest import unittest
import nets
import paddle import paddle
from paddle import fluid from paddle import fluid
from paddle.fluid import core from paddle.fluid import core
...@@ -29,7 +31,7 @@ paddle.enable_static() ...@@ -29,7 +31,7 @@ paddle.enable_static()
# random seed must set before configuring the network. # random seed must set before configuring the network.
# fluid.default_startup_program().random_seed = SEED # fluid.default_startup_program().random_seed = SEED
def cnn_model(data): def cnn_model(data):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=data, input=data,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -37,7 +39,7 @@ def cnn_model(data): ...@@ -37,7 +39,7 @@ def cnn_model(data):
pool_stride=2, pool_stride=2,
act="relu", act="relu",
) )
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -32,26 +32,6 @@ def glu(x, dim=-1): ...@@ -32,26 +32,6 @@ def glu(x, dim=-1):
return out return out
class TestGLUCase(unittest.TestCase):
def setUp(self):
self.x = np.random.randn(5, 20)
self.dim = -1
self.out = glu(self.x, self.dim)
def check_identity(self, place):
with dg.guard(place):
x_var = dg.to_variable(self.x)
y_var = fluid.nets.glu(x_var, self.dim)
y_np = y_var.numpy()
np.testing.assert_allclose(y_np, self.out)
def test_case(self):
self.check_identity(fluid.CPUPlace())
if fluid.is_compiled_with_cuda():
self.check_identity(fluid.CUDAPlace(0))
class TestGLUV2(unittest.TestCase): class TestGLUV2(unittest.TestCase):
def setUp(self): def setUp(self):
self.x = np.random.randn(5, 20) self.x = np.random.randn(5, 20)
......
...@@ -14,9 +14,10 @@ ...@@ -14,9 +14,10 @@
import unittest import unittest
import nets
import paddle import paddle
from paddle import fluid from paddle import fluid
from paddle.fluid import nets
from paddle.fluid.framework import Program from paddle.fluid.framework import Program
......
...@@ -16,6 +16,7 @@ import contextlib ...@@ -16,6 +16,7 @@ import contextlib
import inspect import inspect
import unittest import unittest
import nets
import numpy as np import numpy as np
from decorator_helper import prog_scope from decorator_helper import prog_scope
from test_imperative_base import new_program_scope from test_imperative_base import new_program_scope
...@@ -23,7 +24,7 @@ from test_imperative_base import new_program_scope ...@@ -23,7 +24,7 @@ from test_imperative_base import new_program_scope
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
from paddle import fluid from paddle import fluid
from paddle.fluid import core, layers, nets from paddle.fluid import core, layers
from paddle.fluid.dygraph import base, to_variable from paddle.fluid.dygraph import base, to_variable
from paddle.fluid.framework import Program, default_main_program, program_guard from paddle.fluid.framework import Program, default_main_program, program_guard
from paddle.incubate.layers.nn import ( from paddle.incubate.layers.nn import (
......
...@@ -16,6 +16,7 @@ import os ...@@ -16,6 +16,7 @@ import os
import tempfile import tempfile
import unittest import unittest
import nets
import numpy as np import numpy as np
from test_imperative_base import new_program_scope from test_imperative_base import new_program_scope
...@@ -25,7 +26,7 @@ from paddle.fluid import core ...@@ -25,7 +26,7 @@ from paddle.fluid import core
def convolutional_neural_network(img): def convolutional_neural_network(img):
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=img, input=img,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -34,7 +35,7 @@ def convolutional_neural_network(img): ...@@ -34,7 +35,7 @@ def convolutional_neural_network(img):
act="relu", act="relu",
) )
conv_pool_1 = paddle.static.nn.batch_norm(conv_pool_1) conv_pool_1 = paddle.static.nn.batch_norm(conv_pool_1)
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
import unittest import unittest
import nets
import numpy as np import numpy as np
from parallel_executor_test_base import DeviceType, TestParallelExecutorBase from parallel_executor_test_base import DeviceType, TestParallelExecutorBase
from simple_nets import init_data from simple_nets import init_data
...@@ -41,7 +42,7 @@ def conv_net(use_feed): ...@@ -41,7 +42,7 @@ def conv_net(use_feed):
) )
label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=img, input=img,
filter_size=5, filter_size=5,
num_filters=20, num_filters=20,
...@@ -52,7 +53,7 @@ def conv_net(use_feed): ...@@ -52,7 +53,7 @@ def conv_net(use_feed):
conv_pool_1 = paddle.static.nn.batch_norm(conv_pool_1) conv_pool_1 = paddle.static.nn.batch_norm(conv_pool_1)
conv_pool_1 = paddle.cast(conv_pool_1, np.float32) conv_pool_1 = paddle.cast(conv_pool_1, np.float32)
conv_pool_2 = fluid.nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
num_filters=50, num_filters=50,
......
# Copyright (c) 2018 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.
import unittest
import numpy as np
import paddle
from paddle import fluid
from paddle.fluid import core
class TestMultiheadAttention(unittest.TestCase):
def gen_random_input(self):
"""Generate random input data."""
# batch_size, max_sequence_length, hidden dimension
self.input_shape = (3, 13, 16)
self.queries = np.random.random(size=self.input_shape).astype("float32")
self.keys = np.random.random(size=self.input_shape).astype("float32")
def set_program(self):
"""Build the test program."""
queries = paddle.static.data(
name="queries",
shape=self.input_shape,
dtype="float32",
)
queries.stop_gradient = False
keys = paddle.static.data(
name="keys",
shape=self.input_shape,
dtype="float32",
)
keys.stop_gradient = False
contexts = fluid.nets.scaled_dot_product_attention(
queries=queries,
keys=keys,
values=keys,
num_heads=8,
dropout_rate=0.0,
)
out = paddle.sum(contexts, axis=None)
fluid.backward.append_backward(loss=out)
self.fetch_list = [contexts]
def run_program(self):
"""Run the test program."""
places = [core.CPUPlace()]
if core.is_compiled_with_cuda():
places.append(core.CUDAPlace(0))
for place in places:
self.set_inputs(place)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
output = exe.run(
fluid.default_main_program(),
feed=self.inputs,
fetch_list=self.fetch_list,
return_numpy=True,
)
self.op_output = output
def set_inputs(self, place):
"""Set the randomly generated data to the test program."""
self.inputs = {}
queries = fluid.Tensor()
queries.set(self.queries, place)
keys = fluid.Tensor()
keys.set(self.keys, place)
self.inputs["keys"] = keys
self.inputs["queries"] = queries
def test_multihead_attention(self):
self.gen_random_input()
self.set_program()
self.run_program()
# fixme(caoying) add more meaningfull unittest.
if __name__ == '__main__':
unittest.main()
# Copyright (c) 2020 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.
import unittest
import numpy as np
import paddle
from paddle import fluid
from paddle.fluid import Program, program_guard
class TestScaledDotProductAttentionError(unittest.TestCase):
def test_errors(self):
with program_guard(Program(), Program()):
queries = paddle.static.data(
name="queries", shape=[3, 5, 9], dtype="float32"
)
keys = paddle.static.data(
name="keys", shape=[3, 6, 9], dtype="float32"
)
values = paddle.static.data(
name="values", shape=[3, 6, 10], dtype="float32"
)
def test_queries_Variable():
queries_data = np.random.rand(3, 5, 9).astype("float32")
fluid.nets.scaled_dot_product_attention(
queries_data, keys, values
)
self.assertRaises(TypeError, test_queries_Variable)
def test_keys_Variable():
keys_data = np.random.rand(3, 6, 9).astype("float32")
fluid.nets.scaled_dot_product_attention(
queries, keys_data, values
)
self.assertRaises(TypeError, test_keys_Variable)
def test_values_Variable():
values_data = np.random.rand(3, 6, 10).astype("float32")
fluid.nets.scaled_dot_product_attention(
queries, keys, values_data
)
self.assertRaises(TypeError, test_values_Variable)
def test_diff_dtype():
keys_error = paddle.static.data(
name="keys_error", shape=[3, 6, 9], dtype="float64"
)
values_error = paddle.static.data(
name="values_error", shape=[3, 6, 10], dtype="float64"
)
fluid.nets.scaled_dot_product_attention(
queries, keys_error, values_error
)
self.assertRaises(TypeError, test_diff_dtype)
def test_diff_dim():
keys_error_dim = paddle.static.data(
name="keys_error_dim", shape=[3, 6], dtype="float32"
)
values_error_dim = paddle.static.data(
name="values_error_dim", shape=[3], dtype="float32"
)
fluid.nets.scaled_dot_product_attention(
queries, keys_error_dim, values_error_dim
)
self.assertRaises(ValueError, test_diff_dim)
def test_diff_hidden_size():
queries_error_hs = paddle.static.data(
name="queries_error_hs", shape=[3, 5, 9], dtype="float32"
)
keys_error_hs = paddle.static.data(
name="keys_error_hs", shape=[3, 6, 10], dtype="float32"
)
fluid.nets.scaled_dot_product_attention(
queries_error_hs, keys_error_hs, values
)
self.assertRaises(ValueError, test_diff_hidden_size)
def test_diff_max_len():
keys_error_len = paddle.static.data(
name="keys_error_len", shape=[3, 7, 9], dtype="float32"
)
values_error_len = paddle.static.data(
name="values_error_len", shape=[3, 6, 10], dtype="float32"
)
fluid.nets.scaled_dot_product_attention(
queries, keys_error_len, values_error_len
)
self.assertRaises(ValueError, test_diff_max_len)
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册