diff --git a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_user_defined.py b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_user_defined.py index 21c7eda8cfa34c4c309e770808a52b2c102013cf..ead2a89c372a1bafbbfcac54c44f93e136ee4c12 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_user_defined.py +++ b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_user_defined.py @@ -23,7 +23,6 @@ from paddle.optimizer import Adam from paddle.fluid.contrib.slim.quantization import ImperativeQuantAware from paddle.fluid.contrib.slim.quantization import QuantizationTransformPass from paddle.nn import Sequential -from paddle.fluid.dygraph import Pool2D from paddle.fluid.dygraph import Linear from paddle.nn.quant.quant_layers import QuantizedConv2DTranspose from paddle.fluid.log_helper import get_logger @@ -132,7 +131,7 @@ class ImperativeLenet(paddle.nn.Layer): stride=1, padding=1, ), - Pool2D(pool_size=2, pool_type='max', pool_stride=2), + paddle.nn.MaxPool2D(kernel_size=2, stride=2), paddle.nn.Conv2D( in_channels=6, out_channels=16, @@ -140,7 +139,7 @@ class ImperativeLenet(paddle.nn.Layer): stride=1, padding=0, ), - Pool2D(pool_size=2, pool_type='max', pool_stride=2), + paddle.nn.MaxPool2D(kernel_size=2, stride=2), ) self.fc = Sequential( diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_dist_save_load.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_dist_save_load.py index cae578b66d695491ce654316227a28ce328522f1..a9c525daae41f9552c01331867acce8800ef6fb1 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_dist_save_load.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_dist_save_load.py @@ -33,6 +33,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage2 import ) from paddle.fluid.dygraph.nn import Linear from paddle.incubate.distributed.utils.io import load, save +from paddle.nn import Linear print(load) epoch = 2 diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api.py index eba6bab976bd7db50ca51c733ea1a57d1f228230..6ecf1ca72d40af36892f305545a4e6c46b9d385e 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api.py @@ -26,6 +26,7 @@ from paddle.distributed.sharding import ( ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear epoch = 10 paddle.seed(2022) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api_eager.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api_eager.py index 14272aba8133bfc34ca0a6d32dda8a57ff30ac14..3849b806085186bc0e12b2fc800f0ffdde66b0dd 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api_eager.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_api_eager.py @@ -24,6 +24,7 @@ from paddle.distributed.sharding import ( ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear epoch = 10 paddle.seed(2022) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2.py index ec3baef9a9a263b81e0ec3a9c2bdaa58d2d571be..fc562ffae22360aea3e3aee85f3a6a138b88bc49 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2.py @@ -30,6 +30,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage2 import ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear seed = 2022 epoch = 2 diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2_comm_overlap.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2_comm_overlap.py index 66d975c18888a819a0d851128f69e636c1432ccc..7e2b626ec45ec49c7bc0516e45e58fde595e1288 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2_comm_overlap.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage2_comm_overlap.py @@ -30,6 +30,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage2 import ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear seed = 2022 epoch = 2 diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3.py index 28f11d93ef7feb106e4cbb258625b35011ab912c..d462eb339b80df0fac58acbf53ef990e408a9431 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3.py @@ -36,6 +36,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear epoch = 10 paddle.seed(2022) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3_offload.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3_offload.py index d8bc6a3363386e612ed6a824d363b0c94908af35..3bb6ed15a69da61565001de7fd98191bd82e0095 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3_offload.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_group_sharded_stage3_offload.py @@ -26,6 +26,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear epoch = 10 paddle.seed(2022) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_save_for_auto_infer.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_save_for_auto_infer.py index 5b92edb9032bad119daed93d145d2471ea8487f5..f63cfc089ed8a5d13eb264e883d0fcbd8960b84c 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_save_for_auto_infer.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_save_for_auto_infer.py @@ -39,8 +39,9 @@ from paddle.distributed.fleet.meta_parallel.parallel_layers.pp_layers import ( from paddle.distributed.sharding.group_sharded import group_sharded_parallel from paddle.distributed.utils.log_utils import get_logger from paddle.fluid.dataloader.dataset import IterableDataset -from paddle.fluid.dygraph.nn import Embedding, Linear +from paddle.fluid.dygraph.nn import Embedding from paddle.incubate.distributed.utils.io import save_for_auto_inference +from paddle.nn import Linear logger = get_logger("INFO", __file__) @@ -76,7 +77,7 @@ class MLP_pipe(PipelineLayer): gather_output=True, has_bias=True, ), - LayerDesc(Linear, input_dim=linear_size, output_dim=10), + LayerDesc(Linear, in_features=linear_size, out_features=10), ] super(MLP_pipe, self).__init__( desc, diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_optimizer_stage2.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_optimizer_stage2.py index d8b8eb886806190d2b353cbb11ad15ecd201a4fc..2b4237507360ba86b6d1275fffd9a4697de7b2d8 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_optimizer_stage2.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_optimizer_stage2.py @@ -25,6 +25,7 @@ from paddle.distributed.fleet.meta_optimizers.dygraph_optimizer.sharding_optimiz from paddle.distributed.fleet.utils.internal_storage import GradStorage from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear base_lr = 0.1 momentum_rate = 0.9 diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage2.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage2.py index 2cf3fd920be041318326f438388f99039cd0fa87..9a7d1081a633cebad0f403dc0202f9f3c8238ae9 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage2.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage2.py @@ -31,6 +31,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_stage2 import ( ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear seed = 2022 epoch = 2 diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3.py index 7a37890a1c255bf36ad2a4dd1a3c943dff1c993b..6822872a39834e139f321ae2b4c9b318154ab635 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3.py @@ -37,6 +37,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_utils import ( ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear epoch = 10 paddle.seed(2021) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3_offload.py b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3_offload.py index 18a601c5257f92481685f895e36d48df77caac63..1ebcff04c832705158af8aad369fd094962a0c7a 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3_offload.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dygraph_sharding_stage3_offload.py @@ -27,6 +27,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_utils import ( ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear epoch = 10 paddle.seed(2022) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_control_flow_same.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_control_flow_same.py index 9c4e1750744ac9bbc8ba3441afbe92250626bb6b..9bf0f50676be0c6925f938aa58de27e8e9d2c328 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_control_flow_same.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_control_flow_same.py @@ -19,6 +19,7 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear np.random.seed(2021) paddle.seed(1024) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync.py index 5bef19776302e3d59d83f26c7676e3fee74559a8..970b0cee70e0f4cc52c39dd5d7172aa6f9d7aba7 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync.py @@ -27,6 +27,7 @@ import paddle import paddle.distributed as dist import paddle.fluid as fluid from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear seed = 90 RUN_STEP = 20 @@ -37,9 +38,9 @@ batch_num = 1000 class SimpleNet(fluid.Layer): def __init__(self): super().__init__() - self.net_a = Linear(input_dim=10, output_dim=20) - self.net_b = Linear(input_dim=20, output_dim=5) - self.net_c = Linear(input_dim=5, output_dim=10) + self.net_a = Linear(10, 20) + self.net_b = Linear(20, 5) + self.net_c = Linear(5, 10) def forward(self, x): x = self.net_a(x) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_control_flow.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_control_flow.py index f9502244d2fe34045c30c2545b88d15ed867e217..8ac9d6b2113d1fade61d0e7292f6ee62b90287aa 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_control_flow.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_control_flow.py @@ -19,6 +19,7 @@ from test_dist_base import runtime_main import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear seed = 90 RUN_STEP = 20 @@ -29,9 +30,9 @@ batch_num = 1000 class SimpleNetControlFlow(fluid.Layer): def __init__(self): super().__init__() - self.net_a = Linear(input_dim=10, output_dim=20) - self.net_b = Linear(input_dim=20, output_dim=5) - self.net_c = Linear(input_dim=5, output_dim=10) + self.net_a = Linear(10, 20) + self.net_b = Linear(20, 5) + self.net_c = Linear(5, 10) self.step = 0 def forward(self, x): diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_gradient_check.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_gradient_check.py index 4cb32e5109e30a86bf498558db85e6ebcc992e71..6ea8c59f806c354afdd99c1c4b768a55de1ef9b0 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_gradient_check.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_gradient_check.py @@ -19,7 +19,7 @@ import numpy as np import paddle import paddle.distributed as dist import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear paddle.seed(1024) np.random.seed(2021) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_unused_params.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_unused_params.py index 2a558953d13e7efd8095afd7e92894dff56ddb53..ace0da64b49bfb9ebdec1ef2abebbc2bc2dbae19 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_unused_params.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_no_sync_unused_params.py @@ -18,7 +18,7 @@ from test_dist_base import runtime_main import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear seed = 90 RUN_STEP = 20 @@ -29,11 +29,11 @@ batch_num = 1000 class SimpleNetUnusedParam(fluid.Layer): def __init__(self): super().__init__() - self.net_a = Linear(input_dim=10, output_dim=20) - self.net_b = Linear(input_dim=20, output_dim=5) - self.net_c = Linear(input_dim=5, output_dim=10) + self.net_a = Linear(10, 20) + self.net_b = Linear(20, 5) + self.net_c = Linear(5, 10) - self.net_d = Linear(input_dim=20, output_dim=10) + self.net_d = Linear(20, 10) def forward(self, x): x = self.net_a(x) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py index 95f596c20562c27884ddb3b63775fa05362bb4e2..13e83741ea6956c79a2af1d138cbb345a6acea17 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py @@ -20,7 +20,8 @@ from test_dist_base import TestParallelDyGraphRunnerBase, runtime_main import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable -from paddle.fluid.dygraph.nn import Linear, Pool2D +from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear batch_size = 64 momentum_rate = 0.9 @@ -114,31 +115,33 @@ class SqueezeExcitation(fluid.dygraph.Layer): super().__init__() self._num_channels = num_channels - self._pool = Pool2D(pool_size=0, pool_type='avg', global_pooling=True) + self._pool = paddle.fluid.dygraph.nn.Pool2D( + pool_size=0, pool_type='avg', global_pooling=True + ) stdv = 1.0 / math.sqrt(num_channels * 1.0) self._squeeze = Linear( num_channels, num_channels // reduction_ratio, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Uniform(-stdv, stdv) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), - act='relu', ) stdv = 1.0 / math.sqrt(num_channels / 16.0 * 1.0) self._excitation = Linear( num_channels // reduction_ratio, num_channels, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Uniform(-stdv, stdv) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), - act='sigmoid', ) def forward(self, input): y = self._pool(input) y = paddle.reshape(y, shape=[-1, self._num_channels]) y = self._squeeze(y) + y = paddle.nn.functional.relu(y) y = self._excitation(y) + y = paddle.nn.functional.sigmoid(y) y = fluid.layers.elementwise_mul(x=input, y=y, axis=0) return y @@ -231,9 +234,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=2, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) elif layers == 101: cardinality = 32 reduction_ratio = 16 @@ -246,9 +247,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=2, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) elif layers == 152: cardinality = 64 reduction_ratio = 16 @@ -275,9 +274,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=1, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) self.bottleneck_block_list = [] num_channels = 64 @@ -299,7 +296,7 @@ class SeResNeXt(fluid.dygraph.Layer): self.bottleneck_block_list.append(bottleneck_block) shortcut = True - self.pool2d_avg = Pool2D( + self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( pool_size=7, pool_type='avg', global_pooling=True ) stdv = 1.0 / math.sqrt(2048 * 1.0) @@ -309,8 +306,8 @@ class SeResNeXt(fluid.dygraph.Layer): self.out = Linear( self.pool2d_avg_output, class_dim, - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.Uniform(-stdv, stdv) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), ) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_hybrid_meta_optimizer.py b/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_hybrid_meta_optimizer.py index cf73e6ad6e13127122ff5fa2a28bfd2eee1e44c3..46c0b4fc58f65b6c1e46a5d2c0de60b3b6b63704 100755 --- a/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_hybrid_meta_optimizer.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_hybrid_meta_optimizer.py @@ -994,7 +994,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer): [ 'recv_v2', 'cast', - 'matmul', + 'matmul_v2', 'cast', 'reduce_mean', 'elementwise_mul', @@ -1002,7 +1002,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer): 'elementwise_mul_grad', 'reduce_mean_grad', 'cast', - 'matmul_grad', + 'matmul_v2_grad', 'c_sync_calc_stream', 'send_v2', 'fill_constant', @@ -1087,7 +1087,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer): [ 'recv_v2', 'cast', - 'matmul', + 'matmul_v2', 'cast', 'reduce_mean', 'elementwise_mul', @@ -1095,7 +1095,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer): 'elementwise_mul_grad', 'reduce_mean_grad', 'cast', - 'matmul_grad', + 'matmul_v2_grad', 'c_sync_calc_stream', 'send_v2', 'fill_constant', diff --git a/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_group_sharded_stage3.py b/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_group_sharded_stage3.py index 84f7ce37c1cb45ca796e0f02570d943b6f5bc667..5c328591bda7542887623bc7d2ab3d732584bfc8 100644 --- a/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_group_sharded_stage3.py +++ b/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_group_sharded_stage3.py @@ -36,6 +36,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear epoch = 10 paddle.seed(2022) diff --git a/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_sharding_stage2.py b/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_sharding_stage2.py index ab7f9c9ea2f7d9a7469df77ece4d128065769b70..d68a6cb5880dab0d389a8c33a74c13c0822703a9 100644 --- a/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_sharding_stage2.py +++ b/python/paddle/fluid/tests/unittests/collective/multinode/mn_dygraph_sharding_stage2.py @@ -31,6 +31,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_stage2 import ( ) from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear seed = 2022 epoch = 2 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py index b41e7614ce83b7fdb1986ead80698eda56d44ac4..8eb757d87ac4f90ad1aad644272699cde777f5c3 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py @@ -16,7 +16,8 @@ from transformer_dygraph_model import MultiHeadAttention, PrePostProcessLayer import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph import Embedding, Layer, Linear +from paddle.fluid.dygraph import Embedding, Layer +from paddle.nn import Linear from paddle.jit.api import declarative @@ -33,19 +34,18 @@ class PositionwiseFeedForwardLayer(Layer): super().__init__() self._i2h = Linear( - input_dim=d_model, - output_dim=d_inner_hid, - param_attr=fluid.ParamAttr( + in_features=d_model, + out_features=d_inner_hid, + weight_attr=fluid.ParamAttr( name=name + '_fc_0.w_0', initializer=param_initializer ), bias_attr=name + '_fc_0.b_0', - act=hidden_act, ) self._h2o = Linear( - input_dim=d_inner_hid, - output_dim=d_model, - param_attr=fluid.ParamAttr( + in_features=d_inner_hid, + out_features=d_model, + weight_attr=fluid.ParamAttr( name=name + '_fc_1.w_0', initializer=param_initializer ), bias_attr=name + '_fc_1.b_0', @@ -234,13 +234,12 @@ class BertModelLayer(Layer): ) self.pooled_fc = Linear( - input_dim=self._emb_size, - output_dim=self._emb_size, - param_attr=fluid.ParamAttr( + in_features=self._emb_size, + out_features=self._emb_size, + weight_attr=fluid.ParamAttr( name="pooled_fc.w_0", initializer=self._param_initializer ), bias_attr="pooled_fc.b_0", - act="tanh", ) self.pre_process_layer = PrePostProcessLayer( @@ -295,6 +294,8 @@ class BertModelLayer(Layer): input=enc_output, axes=[1], starts=[0], ends=[1] ) next_sent_feat = self.pooled_fc(next_sent_feat) + + next_sent_feat = paddle.tanh(next_sent_feat) next_sent_feat = paddle.reshape( next_sent_feat, shape=[-1, self._emb_size] ) @@ -334,13 +335,12 @@ class PretrainModelLayer(Layer): ) self.pooled_fc = Linear( - input_dim=self._emb_size, - output_dim=self._emb_size, - param_attr=fluid.ParamAttr( + in_features=self._emb_size, + out_features=self._emb_size, + weight_attr=fluid.ParamAttr( name="mask_lm_trans_fc.w_0", initializer=self._param_initializer ), bias_attr="mask_lm_trans_fc.b_0", - act="tanh", ) self.mask_lm_out_bias_attr = fluid.ParamAttr( @@ -350,9 +350,9 @@ class PretrainModelLayer(Layer): if not self._weight_sharing: self.out_fc = Linear( - input_dim=self._emb_size, - output_dim=self._voc_size, - param_attr=fluid.ParamAttr( + in_features=self._emb_size, + out_features=self._voc_size, + weight_attr=fluid.ParamAttr( name="mask_lm_out_fc.w_0", initializer=self._param_initializer, ), @@ -367,9 +367,9 @@ class PretrainModelLayer(Layer): ) self.next_sent_fc = Linear( - input_dim=self._emb_size, - output_dim=2, - param_attr=fluid.ParamAttr( + in_features=self._emb_size, + out_features=2, + weight_attr=fluid.ParamAttr( name="next_sent_fc.w_0", initializer=self._param_initializer ), bias_attr="next_sent_fc.b_0", @@ -397,6 +397,7 @@ class PretrainModelLayer(Layer): mask_feat = paddle.gather(reshaped_emb_out, index=mask_pos) mask_trans_feat = self.pooled_fc(mask_feat) + mask_trans_feat = paddle.tanh(mask_trans_feat) mask_trans_feat = self.pre_process_layer(mask_trans_feat) if self._weight_sharing: diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py index 3862ab6f9420a4c6b2d5bf263567b18384659e5d..fd084e06649080d82fcea1c4283b5cf2d35b120a 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py @@ -232,14 +232,14 @@ class NetWithControlFlowIf(fluid.dygraph.Layer): def __init__(self, hidden_dim=16): super().__init__() self.hidden_dim = hidden_dim - self.fc = fluid.dygraph.Linear( - input_dim=hidden_dim, - output_dim=5, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.99) + self.fc = paddle.nn.Linear( + in_features=hidden_dim, + out_features=5, + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ), ) self.alpha = 10.0 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py index e7af14446410f63ddb749e89582ed52bf0e8e50e..cb9e92bf629cea50929129013dc2d8e104e598ee 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py @@ -166,10 +166,14 @@ class BaseModel(fluid.dygraph.Layer): ) ) - self.fc = fluid.dygraph.nn.Linear( + self.fc = paddle.nn.Linear( self.hidden_size, self.tar_vocab_size, - param_attr=param_attr, + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Uniform( + low=-self.init_scale, high=self.init_scale + ) + ), bias_attr=False, ) @@ -611,31 +615,38 @@ class AttentionModel(fluid.dygraph.Layer): ) ) - self.attn_fc = fluid.dygraph.nn.Linear( + self.attn_fc = paddle.nn.Linear( self.hidden_size, self.hidden_size, - param_attr=ParamAttr( + weight_attr=paddle.ParamAttr( name="self_attn_fc", - initializer=uniform_initializer(self.init_scale), + initializer=paddle.nn.initializer.Uniform( + low=-self.init_scale, high=self.init_scale + ), ), bias_attr=False, ) - self.concat_fc = fluid.dygraph.nn.Linear( + self.concat_fc = paddle.nn.Linear( 2 * self.hidden_size, self.hidden_size, - param_attr=ParamAttr( + weight_attr=paddle.ParamAttr( name="self_concat_fc", - initializer=uniform_initializer(self.init_scale), + initializer=paddle.nn.initializer.Uniform( + low=-self.init_scale, high=self.init_scale + ), ), bias_attr=False, ) - self.fc = fluid.dygraph.nn.Linear( + self.fc = paddle.nn.Linear( self.hidden_size, self.tar_vocab_size, - param_attr=ParamAttr( - name="self_fc", initializer=uniform_initializer(self.init_scale) + weight_attr=paddle.ParamAttr( + name="self_fc", + initializer=paddle.nn.initializer.Uniform( + low=-self.init_scale, high=self.init_scale + ), ), bias_attr=False, ) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py index b10a5dc55806c861a8e3e8de3236e1ba4812abd0..10e4b9d85ec1612340b0655810f668cf6d6305e9 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py @@ -17,7 +17,10 @@ from functools import reduce import paddle import paddle.fluid as fluid import paddle.fluid.param_attr as attr -from paddle.fluid.dygraph import Embedding, Layer, Linear + +from functools import reduce + +from paddle.fluid.dygraph import Embedding, Layer from paddle.jit.api import declarative from paddle.static import Variable @@ -490,7 +493,7 @@ class BOW(Layer): self.emb_layer = EmbeddingLayer( self.dict_size, self.emb_dim, "emb" ).ops() - self.bow_layer = Linear(self.bow_dim, self.bow_dim) + self.bow_layer = paddle.nn.Linear(self.bow_dim, self.bow_dim) self.bow_layer_po = FCLayer(self.bow_dim, None, "fc").ops() self.softmax_layer = FCLayer(2, "softmax", "cos_sim").ops() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py index 69765c1b80f2259016c1aeec502e4b0049d1e33b..34a65913c5ae56cbf11d4273d2d4f8a87db86bf5 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py @@ -149,15 +149,15 @@ def dyfunc_Conv2D(input): def dyfunc_Conv3D(input): - conv3d = fluid.dygraph.Conv3D( - num_channels=3, - num_filters=2, - filter_size=3, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.99) + conv3d = paddle.nn.Conv3D( + in_channels=3, + out_channels=2, + kernel_size=3, + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.99) ), bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + initializer=paddle.nn.initializer.Constant(value=0.5) ), ) res = conv3d(input) @@ -182,16 +182,15 @@ def dyfunc_Conv2DTranspose(input): def dyfunc_Conv3DTranspose(input): - conv3dTranspose = fluid.dygraph.nn.Conv3DTranspose( - num_channels=3, - num_filters=12, - filter_size=12, - use_cudnn=False, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.99) + conv3dTranspose = paddle.nn.Conv3DTranspose( + in_channels=3, + out_channels=12, + kernel_size=12, + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ), ) ret = conv3dTranspose(input) @@ -199,28 +198,24 @@ def dyfunc_Conv3DTranspose(input): def dyfunc_Linear(input): - fc = fluid.dygraph.Linear( - input_dim=10, - output_dim=5, - act='relu', - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.99) + fc = paddle.nn.Linear( + in_features=10, + out_features=5, + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ), ) + m = paddle.nn.ReLU() res = fc(input) - return res + return m(res) def dyfunc_Pool2D(input): - fluid.dygraph.Pool2D( - pool_size=2, pool_type='avg', pool_stride=1, global_pooling=False - ) - pool2d = fluid.dygraph.Pool2D( - pool_size=2, pool_type='avg', pool_stride=1, global_pooling=False - ) + paddle.nn.AvgPool2D(kernel_size=2, stride=1) + pool2d = paddle.nn.AvgPool2D(kernel_size=2, stride=1) res = pool2d(input) return res diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py index c0f5e8c0c3ce105b2ab9a7a8dcb1c5dc72f1e4aa..90a7b4d35efd9d043b99fbac34493b0b9d9647ee 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py @@ -218,15 +218,14 @@ class BMN(fluid.dygraph.Layer): self.sample_mask = fluid.dygraph.base.to_variable(sample_mask) self.sample_mask.stop_gradient = True - self.p_conv3d1 = fluid.dygraph.Conv3D( - num_channels=128, - num_filters=self.hidden_dim_3d, - filter_size=(self.num_sample, 1, 1), + self.p_conv3d1 = paddle.nn.Conv3D( + in_channels=128, + out_channels=self.hidden_dim_3d, + kernel_size=(self.num_sample, 1, 1), stride=(self.num_sample, 1, 1), padding=0, - act="relu", - param_attr=ParamAttr(name="PEM_3d1_w"), - bias_attr=ParamAttr(name="PEM_3d1_b"), + weight_attr=paddle.ParamAttr(name="PEM_3d1_w"), + bias_attr=paddle.ParamAttr(name="PEM_3d1_b"), ) self.p_conv2d1 = paddle.nn.Conv2D( @@ -287,6 +286,7 @@ class BMN(fluid.dygraph.Layer): xp = paddle.reshape(xp, shape=[0, 0, -1, self.dscale, self.tscale]) xp = self.p_conv3d1(xp) + xp = paddle.tanh(xp) xp = paddle.squeeze(xp, axis=[2]) xp = paddle.nn.functional.relu(self.p_conv2d1(xp)) xp = paddle.nn.functional.relu(self.p_conv2d2(xp)) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py index 3922c60bafc2f042e49e39c1683170a79c19db48..302045ed4038f22861db0019756958a663e37c11 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py @@ -155,23 +155,23 @@ class MyLayer(fluid.dygraph.Layer): super().__init__() self.conv = MyConvLayer() - self.fc = fluid.dygraph.Linear( - input_dim=5, - output_dim=1, - act='relu', - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.99) + self.fc = paddle.nn.Linear( + in_features=5, + out_features=1, + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ), ) + self.act = paddle.nn.ReLU() @paddle.jit.to_static def forward(self, inputs): h = self.conv(inputs) out = self.fc(h) - return out + return self.act(out) class TestRecursiveCall2(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py index b9ae4c5759d7619d3462321c33811d0667660d32..4ac7ca9e3c926d6c1df5bcaece4cd6ea24141eb8 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py @@ -36,7 +36,7 @@ program_trans = ProgramTranslator() class SimpleNet(Layer): def __init__(self): super().__init__() - self.linear = fluid.dygraph.Linear(10, 3) + self.linear = paddle.nn.Linear(10, 3) @declarative(input_spec=[InputSpec(shape=[None, 10], dtype='float32')]) def forward(self, x, a=1, b=2): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py index 2f049581ecec9bf9757b3b0e532d50602d6b2f51..57bd7c2936e8e06c7f3fdcbc8b6acac4ea86c090 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py @@ -30,27 +30,27 @@ class SubNetWithDict(fluid.dygraph.Layer): def __init__(self, hidden_size=16, output_size=16): super().__init__() - init_weight = lambda x: fluid.ParamAttr( - initializer=fluid.initializer.Constant(x) + init_weight = lambda x: paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(x) ) - self.q_fc = fluid.dygraph.Linear( - input_dim=hidden_size, - output_dim=output_size, + self.q_fc = paddle.nn.Linear( + in_features=hidden_size, + out_features=output_size, bias_attr=False, - param_attr=init_weight(0.6), + weight_attr=init_weight(0.6), ) - self.k_fc = fluid.dygraph.Linear( - input_dim=hidden_size, - output_dim=output_size, + self.k_fc = paddle.nn.Linear( + in_features=hidden_size, + out_features=output_size, bias_attr=False, - param_attr=init_weight(0.5), + weight_attr=init_weight(0.5), ) - self.v_fc = fluid.dygraph.Linear( - input_dim=hidden_size, - output_dim=output_size, + self.v_fc = paddle.nn.Linear( + in_features=hidden_size, + out_features=output_size, bias_attr=False, - param_attr=init_weight(0.2), + weight_attr=init_weight(0.2), ) def forward(self, input, cache=None): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py index d7a21f3be6b08bad4b1c7e3a84450a841fd32f54..6faed1a61e809d06f168a9c2f3c8f19a1ee3e64b 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py @@ -68,7 +68,7 @@ def func_decorated_by_other_2(): class LayerErrorInCompiletime(fluid.dygraph.Layer): def __init__(self, fc_size=20): super().__init__() - self._linear = fluid.dygraph.Linear(fc_size, fc_size) + self._linear = paddle.nn.Linear(fc_size, fc_size) @paddle.jit.to_static( input_spec=[paddle.static.InputSpec(shape=[20, 20], dtype='float32')] diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py index b09ce1eab4e439a4b2f86784fe233e472c7015ea..74dd84720f50fc3b3be17ba9eeb660d2045a6331 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py @@ -27,9 +27,7 @@ SEED = 2020 class Pool2D(fluid.dygraph.Layer): def __init__(self): super().__init__() - self.pool2d = fluid.dygraph.Pool2D( - pool_size=2, pool_type='avg', pool_stride=1, global_pooling=False - ) + self.pool2d = paddle.nn.AvgPool2D(kernel_size=2, stride=1) @declarative def forward(self, x): @@ -44,21 +42,22 @@ class Pool2D(fluid.dygraph.Layer): class Linear(fluid.dygraph.Layer): def __init__(self, input_dim=10, output_dim=5): super().__init__() - self.fc = fluid.dygraph.Linear( + self.fc = paddle.nn.Linear( input_dim, output_dim, - act='relu', - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.99) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ), ) + self.act = paddle.nn.ReLU() @declarative def forward(self, x): pre = self.fc(x) + pre = self.act(pre) loss = paddle.mean(pre) return pre, loss diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py index 1d4b5850e4fd331ba68ba3d5ffc49dbd6e207c4e..dd4e7e6746d1ce8b7114e2728b772cef06704b01 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py @@ -24,8 +24,10 @@ os.environ["CUDA_VISIBLE_DEVICES"] = "2" import paddle import paddle.fluid as fluid +from paddle.fluid.dygraph import to_variable +from paddle.fluid.dygraph import Embedding, GRUUnit + from paddle import _legacy_C_ops -from paddle.fluid.dygraph import Embedding, GRUUnit, Linear, to_variable from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.fluid.framework import _non_static_mode from paddle.jit import ProgramTranslator @@ -100,10 +102,10 @@ class BiGRU(fluid.dygraph.Layer): def __init__(self, input_dim, grnn_hidden_dim, init_bound, h_0=None): super().__init__() - self.pre_gru = Linear( - input_dim=input_dim, - output_dim=grnn_hidden_dim * 3, - param_attr=fluid.ParamAttr( + self.pre_gru = paddle.nn.Linear( + in_features=input_dim, + out_features=grnn_hidden_dim * 3, + weight_attr=fluid.ParamAttr( initializer=fluid.initializer.Uniform( low=-init_bound, high=init_bound ), @@ -126,10 +128,10 @@ class BiGRU(fluid.dygraph.Layer): ), ) - self.pre_gru_r = Linear( - input_dim=input_dim, - output_dim=grnn_hidden_dim * 3, - param_attr=fluid.ParamAttr( + self.pre_gru_r = paddle.nn.Linear( + in_features=input_dim, + out_features=grnn_hidden_dim * 3, + weight_attr=fluid.ParamAttr( initializer=fluid.initializer.Uniform( low=-init_bound, high=init_bound ), @@ -417,10 +419,10 @@ class LexNet(fluid.dygraph.Layer): ) ) - self.fc = Linear( - input_dim=self.grnn_hidden_dim * 2, - output_dim=self.num_labels, - param_attr=fluid.ParamAttr( + self.fc = paddle.nn.Linear( + in_features=self.grnn_hidden_dim * 2, + out_features=self.num_labels, + weight_attr=fluid.ParamAttr( initializer=fluid.initializer.Uniform( low=-self.init_bound, high=self.init_bound ), diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mnist.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mnist.py index af942cdb8d70d2f6b20d5ba4e771e516330b6ff3..fe7e463e1db2f3403bcd3737f4a2c4b284e10712 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mnist.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mnist.py @@ -23,9 +23,9 @@ from predictor_utils import PredictorTools import paddle import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable +from paddle.nn import Linear from paddle.fluid.dygraph.base import switch_to_static_graph from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX -from paddle.fluid.dygraph.nn import Linear, Pool2D from paddle.fluid.framework import _test_eager_guard from paddle.fluid.optimizer import AdamOptimizer @@ -69,7 +69,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer): bias_attr=None, ) - self._pool2d = Pool2D( + self._pool2d = paddle.fluid.dygraph.nn.Pool2D( pool_size=pool_size, pool_type=pool_type, pool_stride=pool_stride, @@ -102,12 +102,9 @@ class MNIST(fluid.dygraph.Layer): self._fc = Linear( self.pool_2_shape, 10, - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=scale - ) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale) ), - act="softmax", ) def forward(self, inputs, label=None): @@ -126,6 +123,7 @@ class MNIST(fluid.dygraph.Layer): x = self._simple_img_conv_pool_2(x) x = paddle.reshape(x, shape=[-1, self.pool_2_shape]) x = self._fc(x) + x = paddle.nn.functional.softmax(x) return x diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py index 068046e00bde084ef288f7ab675e087abe081115..b40eb92753dacca665c1f3fa968f1fb3101a9fbd 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py @@ -23,11 +23,19 @@ from predictor_utils import PredictorTools import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX -from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D +from paddle.fluid.dygraph.nn import BatchNorm, Linear from paddle.fluid.initializer import MSRA from paddle.fluid.param_attr import ParamAttr -from paddle.jit import ProgramTranslator +from paddle.fluid.dygraph.nn import BatchNorm +from paddle.nn import Linear from paddle.jit.api import declarative +from paddle.jit import ProgramTranslator + +from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX + +import unittest + +from predictor_utils import PredictorTools # Note: Set True to eliminate randomness. # 1. For one operation, cuDNN has several algorithms, @@ -255,12 +263,14 @@ class MobileNetV1(fluid.dygraph.Layer): ) self.dwsl.append(dws6) - self.pool2d_avg = Pool2D(pool_type='avg', global_pooling=True) + self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( + pool_type='avg', global_pooling=True + ) self.out = Linear( int(1024 * scale), class_dim, - param_attr=ParamAttr( + weight_attr=ParamAttr( initializer=MSRA(), name=self.full_name() + "fc7_weights" ), bias_attr=ParamAttr(name="fc7_offset"), @@ -421,14 +431,16 @@ class MobileNetV2(fluid.dygraph.Layer): ) # 4. pool - self._pool2d_avg = Pool2D(pool_type='avg', global_pooling=True) + self._pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( + pool_type='avg', global_pooling=True + ) # 5. fc tmp_param = ParamAttr(name=self.full_name() + "fc10_weights") self._fc = Linear( self._out_c, class_dim, - param_attr=tmp_param, + weight_attr=tmp_param, bias_attr=ParamAttr(name="fc10_offset"), ) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py index 86f5626f344c51aaab24a63160f124e9d20bb5c3..c61fdcccf015ac682177e3bc7babe5b085b29650 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py @@ -204,7 +204,7 @@ class StaticCode2: class NetWithError(fluid.dygraph.layers.Layer): @declarative def forward(self, x): - linear = fluid.dygraph.Linear(32, 64) + linear = paddle.nn.Linear(32, 64) y = linear(x) return y diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py index 3b6da7e23c1c5cb76eeb27a5d897b072ffb98871..6423d0d6bbcbf17f43125c43237727e8e89ccfee 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py @@ -21,8 +21,7 @@ import numpy as np import paddle import paddle.fluid as fluid -import paddle.fluid.dygraph.nn as nn -from paddle.fluid.dygraph import Layer, to_variable +from paddle.fluid.dygraph import to_variable, Layer from paddle.jit import ProgramTranslator from paddle.jit.api import declarative @@ -34,8 +33,8 @@ class Policy(Layer): def __init__(self): super().__init__() - self.affine1 = nn.Linear(4, 128) - self.affine2 = nn.Linear(128, 2) + self.affine1 = paddle.nn.Linear(4, 128) + self.affine2 = paddle.nn.Linear(128, 2) self.dropout_ratio = 0.6 self.saved_log_probs = [] diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py index 5851f82630569de6c8a9b02d1a38cac600e8cba6..fec0109168b50d91cd9a59c6a526ca17063ca037 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py @@ -17,14 +17,16 @@ import os import tempfile import time import unittest - +import paddle import numpy as np from predictor_utils import PredictorTools import paddle import paddle.fluid as fluid + +from paddle.fluid.dygraph.nn import BatchNorm +from paddle.jit import ProgramTranslator from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX -from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D from paddle.jit import ProgramTranslator SEED = 2020 @@ -165,9 +167,7 @@ class ResNet(fluid.dygraph.Layer): self.conv = ConvBNLayer( num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu' ) - self.pool2d_max = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool2d_max = paddle.nn.MaxPool2D(kernel_size=3, stride=2) self.bottleneck_block_list = [] for block in range(len(depth)): @@ -186,8 +186,7 @@ class ResNet(fluid.dygraph.Layer): ) self.bottleneck_block_list.append(bottleneck_block) shortcut = True - - self.pool2d_avg = Pool2D( + self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( pool_size=7, pool_type='avg', global_pooling=True ) @@ -195,11 +194,10 @@ class ResNet(fluid.dygraph.Layer): stdv = 1.0 / math.sqrt(2048 * 1.0) - self.out = Linear( + self.out = paddle.nn.Linear( self.pool2d_avg_output, class_dim, - act='softmax', - param_attr=fluid.param_attr.ParamAttr( + weight_attr=fluid.param_attr.ParamAttr( initializer=fluid.initializer.Uniform(-stdv, stdv) ), ) @@ -212,6 +210,7 @@ class ResNet(fluid.dygraph.Layer): y = self.pool2d_avg(y) y = paddle.reshape(y, shape=[-1, self.pool2d_avg_output]) pred = self.out(y) + pred = paddle.nn.functional.softmax(pred) return pred diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py index 112ff2f1d0f95a869c7409ed84d1d6da9cf57224..00e423d686fab3298281757752f12575162b66d9 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py @@ -163,8 +163,8 @@ class ResNet(paddle.nn.Layer): self.conv = ConvBNLayer( num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu' ) - self.pool2d_max = paddle.fluid.dygraph.Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' + self.pool2d_max = paddle.nn.MaxPool2D( + kernel_size=3, stride=2, padding=1 ) self.bottleneck_block_list = [] @@ -184,7 +184,6 @@ class ResNet(paddle.nn.Layer): ) self.bottleneck_block_list.append(bottleneck_block) shortcut = True - self.pool2d_avg = paddle.fluid.dygraph.Pool2D( pool_size=7, pool_type='avg', global_pooling=True ) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py index 043ad587fe77b3a6ce82c3cfc1962002ca221ffc..c22c78fefe3d2f5822a0ceb4b15995e2637290ab 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py @@ -39,7 +39,7 @@ program_translator = ProgramTranslator() class SimpleFcLayer(fluid.dygraph.Layer): def __init__(self, fc_size): super().__init__() - self._linear = fluid.dygraph.Linear(fc_size, fc_size) + self._linear = paddle.nn.Linear(fc_size, fc_size) @declarative def forward(self, x): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py index 7d3b07a395c907090398b9e90ed918f76e798af9..4b1aad178d0208bd1fb2123b2a9c842afa719fe2 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py @@ -25,8 +25,9 @@ from predictor_utils import PredictorTools import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable +from paddle.fluid.dygraph.nn import BatchNorm +from paddle.nn import Linear from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX -from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D from paddle.jit import ProgramTranslator from paddle.jit.api import declarative @@ -126,31 +127,33 @@ class SqueezeExcitation(fluid.dygraph.Layer): super().__init__() self._num_channels = num_channels - self._pool = Pool2D(pool_size=0, pool_type='avg', global_pooling=True) + self._pool = paddle.fluid.dygraph.nn.Pool2D( + pool_size=0, pool_type='avg', global_pooling=True + ) stdv = 1.0 / math.sqrt(num_channels * 1.0) self._fc = Linear( num_channels, num_channels // reduction_ratio, - param_attr=fluid.ParamAttr( + weight_attr=fluid.ParamAttr( initializer=fluid.initializer.Uniform(-stdv, stdv) ), - act='relu', ) stdv = 1.0 / math.sqrt(num_channels / 16.0 * 1.0) self._excitation = Linear( num_channels // reduction_ratio, num_channels, - param_attr=fluid.ParamAttr( + weight_attr=fluid.ParamAttr( initializer=fluid.initializer.Uniform(-stdv, stdv) ), - act='sigmoid', ) def forward(self, input): y = self._pool(input) y = paddle.reshape(y, shape=[-1, self._num_channels]) y = self._fc(y) + y = paddle.nn.functional.relu(y) y = self._excitation(y) + y = paddle.nn.functional.sigmoid(y) y = fluid.layers.elementwise_mul(x=input, y=y, axis=0) return y @@ -243,9 +246,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=2, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) elif layers == 101: cardinality = 32 reduction_ratio = 16 @@ -258,9 +259,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=2, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) elif layers == 152: cardinality = 64 reduction_ratio = 16 @@ -287,9 +286,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=1, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) self.bottleneck_block_list = [] num_channels = 64 @@ -312,8 +309,7 @@ class SeResNeXt(fluid.dygraph.Layer): num_channels = bottleneck_block._num_channels_out self.bottleneck_block_list.append(bottleneck_block) shortcut = True - - self.pool2d_avg = Pool2D( + self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( pool_size=7, pool_type='avg', global_pooling=True ) stdv = 1.0 / math.sqrt(2048 * 1.0) @@ -323,7 +319,7 @@ class SeResNeXt(fluid.dygraph.Layer): self.out = Linear( self.pool2d_avg_output, class_dim, - param_attr=fluid.param_attr.ParamAttr( + weight_attr=fluid.param_attr.ParamAttr( initializer=fluid.initializer.Uniform(-stdv, stdv) ), ) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py index 98ce0ca778050470ba27909abd4fef53500468a7..b02f6f418b3afa2494e179d6a97fc82f97477fcb 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py @@ -19,6 +19,8 @@ from test_lac import DynamicGRU import paddle import paddle.fluid as fluid +from paddle.fluid.dygraph.nn import Embedding +from paddle.nn import Linear from paddle.fluid.dygraph import to_variable from paddle.fluid.dygraph.nn import Embedding, Linear from paddle.jit import ProgramTranslator @@ -83,13 +85,11 @@ class CNN(fluid.dygraph.Layer): batch_size=self.batch_size, ) self._fc1 = Linear( - input_dim=self.hid_dim * self.seq_len, - output_dim=self.fc_hid_dim, - act="softmax", - ) - self._fc_prediction = Linear( - input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax" + self.hid_dim * self.seq_len, + self.fc_hid_dim, ) + self._fc1_act = paddle.nn.Softmax() + self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim) @declarative def forward(self, inputs, label=None): @@ -104,7 +104,9 @@ class CNN(fluid.dygraph.Layer): ) conv_3 = self._simple_conv_pool_1(emb) fc_1 = self._fc1(conv_3) + fc_1 = self._fc1_act(fc_1) prediction = self._fc_prediction(fc_1) + prediction = self._fc1_act(prediction) cost = fluid.layers.cross_entropy(input=prediction, label=label) avg_cost = paddle.mean(x=cost) @@ -127,15 +129,9 @@ class BOW(fluid.dygraph.Layer): dtype='float32', is_sparse=False, ) - self._fc1 = Linear( - input_dim=self.hid_dim, output_dim=self.hid_dim, act="tanh" - ) - self._fc2 = Linear( - input_dim=self.hid_dim, output_dim=self.fc_hid_dim, act="tanh" - ) - self._fc_prediction = Linear( - input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax" - ) + self._fc1 = Linear(self.hid_dim, self.hid_dim) + self._fc2 = Linear(self.hid_dim, self.fc_hid_dim) + self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim) @declarative def forward(self, inputs, label=None): @@ -149,8 +145,11 @@ class BOW(fluid.dygraph.Layer): bow_1 = paddle.sum(emb, axis=1) bow_1 = paddle.tanh(bow_1) fc_1 = self._fc1(bow_1) + fc_1 = paddle.tanh(fc_1) fc_2 = self._fc2(fc_1) + fc_2 = paddle.tanh(fc_2) prediction = self._fc_prediction(fc_2) + prediction = paddle.nn.functional.softmax(prediction) cost = fluid.layers.cross_entropy(input=prediction, label=label) avg_cost = paddle.mean(x=cost) @@ -176,13 +175,9 @@ class GRU(fluid.dygraph.Layer): ) h_0 = np.zeros((self.batch_size, self.hid_dim), dtype="float32") h_0 = to_variable(h_0) - self._fc1 = Linear(input_dim=self.hid_dim, output_dim=self.hid_dim * 3) - self._fc2 = Linear( - input_dim=self.hid_dim, output_dim=self.fc_hid_dim, act="tanh" - ) - self._fc_prediction = Linear( - input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax" - ) + self._fc1 = Linear(self.hid_dim, self.hid_dim * 3) + self._fc2 = Linear(self.hid_dim, self.fc_hid_dim) + self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim) self._gru = DynamicGRU(size=self.hid_dim, h_0=h_0) @declarative @@ -199,8 +194,9 @@ class GRU(fluid.dygraph.Layer): gru_hidden = paddle.max(gru_hidden, axis=1) tanh_1 = paddle.tanh(gru_hidden) fc_2 = self._fc2(tanh_1) + fc_2 = paddle.tanh(fc_2) prediction = self._fc_prediction(fc_2) - + prediction = paddle.nn.functional.softmax(prediction) cost = fluid.layers.cross_entropy(input=prediction, label=label) avg_cost = paddle.mean(x=cost) acc = fluid.layers.accuracy(input=prediction, label=label) @@ -225,13 +221,9 @@ class BiGRU(fluid.dygraph.Layer): ) h_0 = np.zeros((self.batch_size, self.hid_dim), dtype="float32") h_0 = to_variable(h_0) - self._fc1 = Linear(input_dim=self.hid_dim, output_dim=self.hid_dim * 3) - self._fc2 = Linear( - input_dim=self.hid_dim * 2, output_dim=self.fc_hid_dim, act="tanh" - ) - self._fc_prediction = Linear( - input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax" - ) + self._fc1 = Linear(self.hid_dim, self.hid_dim * 3) + self._fc2 = Linear(self.hid_dim * 2, self.fc_hid_dim) + self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim) self._gru_forward = DynamicGRU( size=self.hid_dim, h_0=h_0, is_reverse=False ) @@ -259,7 +251,9 @@ class BiGRU(fluid.dygraph.Layer): ) encoded_vector = paddle.max(encoded_vector, axis=1) fc_2 = self._fc2(encoded_vector) + fc_2 = paddle.tanh(fc_2) prediction = self._fc_prediction(fc_2) + prediction = paddle.nn.functional.softmax(prediction) # TODO(Aurelius84): Uncomment the following codes when we support return variable-length vars. # if label is not None: cost = fluid.layers.cross_entropy(input=prediction, label=label) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py index d5bd239afd4ad7edd388809cbe50e02529214c4b..0919e4bced39b076d5012236c342461bb1933619 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py @@ -23,10 +23,12 @@ from tsm_config_utils import merge_configs, parse_config, print_configs import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph import to_variable -from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D -from paddle.jit import ProgramTranslator +from paddle.fluid.dygraph.nn import BatchNorm +from paddle.nn import Linear from paddle.jit.api import declarative +from paddle.jit import ProgramTranslator +from paddle.fluid.dygraph import to_variable +from tsm_config_utils import merge_configs, parse_config, print_configs random.seed(0) np.random.seed(0) @@ -159,8 +161,8 @@ class TSM_ResNet(fluid.dygraph.Layer): self.conv = ConvBNLayer( num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu' ) - self.pool2d_max = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' + self.pool2d_max = paddle.nn.MaxPool2D( + kernel_size=3, stride=2, padding=1 ) self.bottleneck_block_list = [] @@ -182,10 +184,9 @@ class TSM_ResNet(fluid.dygraph.Layer): num_channels = int(bottleneck_block._num_channels_out) self.bottleneck_block_list.append(bottleneck_block) shortcut = True - self.pool2d_avg = Pool2D( + self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( pool_size=7, pool_type='avg', global_pooling=True ) - import math stdv = 1.0 / math.sqrt(2048 * 1.0) @@ -193,12 +194,11 @@ class TSM_ResNet(fluid.dygraph.Layer): self.out = Linear( 2048, self.class_dim, - act="softmax", - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.Uniform(-stdv, stdv) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), - bias_attr=fluid.param_attr.ParamAttr( - learning_rate=2.0, regularizer=fluid.regularizer.L2Decay(0.0) + bias_attr=paddle.ParamAttr( + learning_rate=2.0, regularizer=paddle.regularizer.L1Decay() ), ) @@ -215,6 +215,7 @@ class TSM_ResNet(fluid.dygraph.Layer): y = paddle.mean(y, axis=1) y = paddle.reshape(y, shape=[-1, 2048]) y = self.out(y) + y = paddle.nn.functional.softmax(y) return y diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py index d0f329b96cfb81fa4a6d919fef598af94531fe1e..50d00a653170c1d5243b8dba7cc63d3191c7388d 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py @@ -22,9 +22,9 @@ from paddle.fluid.dygraph import ( Embedding, Layer, LayerNorm, - Linear, to_variable, ) +from paddle.nn import Linear from paddle.fluid.layers.utils import map_structure from paddle.jit.api import dygraph_to_static_func @@ -107,28 +107,28 @@ class MultiHeadAttention(Layer): self.d_model = d_model self.dropout_rate = dropout_rate self.q_fc = Linear( - input_dim=d_model, - output_dim=d_key * n_head, + in_features=d_model, + out_features=d_key * n_head, bias_attr=False, - param_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=fluid.ParamAttr(initializer=param_initializer), ) self.k_fc = Linear( - input_dim=d_model, - output_dim=d_key * n_head, + in_features=d_model, + out_features=d_key * n_head, bias_attr=False, - param_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=fluid.ParamAttr(initializer=param_initializer), ) self.v_fc = Linear( - input_dim=d_model, - output_dim=d_value * n_head, + in_features=d_model, + out_features=d_value * n_head, bias_attr=False, - param_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=fluid.ParamAttr(initializer=param_initializer), ) self.proj_fc = Linear( - input_dim=d_value * n_head, - output_dim=d_model, + in_features=d_value * n_head, + out_features=d_model, bias_attr=False, - param_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=fluid.ParamAttr(initializer=param_initializer), ) def forward(self, queries, keys, values, attn_bias, cache=None): @@ -174,11 +174,12 @@ class FFN(Layer): def __init__(self, d_inner_hid, d_model, dropout_rate): super().__init__() self.dropout_rate = dropout_rate - self.fc1 = Linear(input_dim=d_model, output_dim=d_inner_hid, act="relu") - self.fc2 = Linear(input_dim=d_inner_hid, output_dim=d_model) + self.fc1 = Linear(d_model, d_inner_hid) + self.fc2 = Linear(d_inner_hid, d_model) def forward(self, x): hidden = self.fc1(x) + hidden = paddle.nn.functional.relu(hidden) if self.dropout_rate: hidden = layers.dropout(hidden, dropout_prob=self.dropout_rate) out = self.fc2(hidden) diff --git a/python/paddle/fluid/tests/unittests/fleet_meta_optimizer_base.py b/python/paddle/fluid/tests/unittests/fleet_meta_optimizer_base.py index 7bc12a99c3abc5ccf768e154294f32f54544bb7e..225bff65114bd06cfdb6ad22abd936813a99093a 100755 --- a/python/paddle/fluid/tests/unittests/fleet_meta_optimizer_base.py +++ b/python/paddle/fluid/tests/unittests/fleet_meta_optimizer_base.py @@ -117,10 +117,10 @@ class TestFleetMetaOptimizer(unittest.TestCase): fleet.init(is_collective=True) x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32') with paddle.static.device_guard('gpu:0'): - linear = fluid.Linear(4, 8, bias_attr=False) + linear = paddle.nn.Linear(4, 8, bias_attr=False) out = linear(x) with paddle.static.device_guard('gpu:1'): - linear = fluid.Linear(8, 5, bias_attr=False) + linear = paddle.nn.Linear(8, 5, bias_attr=False) out = linear(out) avg_cost = paddle.mean(out) strategy = fleet.DistributedStrategy() diff --git a/python/paddle/fluid/tests/unittests/mlu/test_pool2d_op_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_pool2d_op_mlu.py index 1a7a2f2255145e0963d49c6e257c39796287f3ed..1620e6093794d15e154a51fb3143a2a244e3714c 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_pool2d_op_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_pool2d_op_mlu.py @@ -998,110 +998,6 @@ class TestPool2DAPI_Error(unittest.TestCase): self.assertRaises(ValueError, run_5) -class TestDygraphPool2DAPIError(unittest.TestCase): - def test_errors(self): - with program_guard(Program(), Program()): - # the input of Pool2D must be Variable. - data1 = np.random.random((3, 32, 32, 5)).astype('float32') - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='max', - pool_stride=1, - global_pooling=False, - ) - self.assertRaises(TypeError, pool2d, data1) - - # the input dtype of mlu Pool2D must be float16 or float32 - data2 = fluid.layers.data( - name='x1', shape=[3, 32, 32, 5], dtype="int32" - ) - self.assertRaises(TypeError, pool2d, data2) - - def test_data_format_error(self): - with program_guard(Program(), Program()): - # the data_format must be 'NCHW' or 'NHWC' - data1 = np.random.random((3, 32, 32, 5)).astype('float32') - self.assertRaises( - ValueError, - fluid.dygraph.Pool2D, - pool_size=2, - pool_type='max', - pool_stride=1, - global_pooling=False, - data_format='NWHC', - ) - - -class TestDygraphPool2DAPI(unittest.TestCase): - def test_nhwc(self): - with fluid.dygraph.guard(): - data = np.random.random((3, 32, 32, 5)).astype('float32') - x = fluid.dygraph.to_variable(data) - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='max', - pool_stride=1, - pool_padding=[0, 0], - global_pooling=False, - data_format='NHWC', - ) - out1 = pool2d(x) - out2 = pool2D_forward_naive( - data, - [2, 2], - [1, 1], - paddings=[0, 0], - pool_type='max', - data_format='NHWC', - ) - np.testing.assert_allclose(out1.numpy(), out2) - - def test_lower_case(self): - with fluid.dygraph.guard(): - data = np.random.random((3, 32, 32, 5)).astype('float32') - x = fluid.dygraph.to_variable(data) - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='max', - pool_stride=1, - pool_padding=[0, 0], - global_pooling=False, - data_format='nhwc', - ) - out1 = pool2d(x) - out2 = pool2D_forward_naive( - data, - [2, 2], - [1, 1], - paddings=[0, 0], - pool_type='max', - data_format='NHWC', - ) - np.testing.assert_allclose(out1.numpy(), out2) - - def test_upper_case(self): - with fluid.dygraph.guard(): - data = np.random.random((3, 32, 32, 5)).astype('float32') - x = fluid.dygraph.to_variable(data) - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='MAX', - pool_stride=1, - pool_padding=[0, 0], - global_pooling=False, - data_format='nhwc', - ) - out1 = pool2d(x) - out2 = pool2D_forward_naive( - data, - [2, 2], - [1, 1], - paddings=[0, 0], - pool_type='max', - data_format='NHWC', - ) - np.testing.assert_allclose(out1.numpy(), out2) - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check.py b/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check.py index eb98bcd6c0933904049a8dc09098ca70cb616872..036b9d967e8616f7d6c8b2a9dcc9752e2a489d4e 100644 --- a/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check.py +++ b/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check.py @@ -19,7 +19,7 @@ import numpy as np import paddle import paddle.distributed as dist import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear paddle.seed(1024) np.random.seed(2021) diff --git a/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check_in_eager_mode.py b/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check_in_eager_mode.py index a5b3c584e5e3b12ba4bcabbac9324dde43bed38a..5680f7a40e16ca4c1e04f86bb792367a123aa62b 100644 --- a/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check_in_eager_mode.py +++ b/python/paddle/fluid/tests/unittests/parallel_dygraph_gradient_check_in_eager_mode.py @@ -19,8 +19,8 @@ import numpy as np import paddle import paddle.distributed as dist import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Linear from paddle.fluid.framework import _test_eager_guard +from paddle.nn import Linear paddle.seed(1024) np.random.seed(2021) diff --git a/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py b/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py index ec7044e8d51ae7d7164cf0e492902dbfefd4832d..150abe911e5018dab42f9098a167b27b63bb8faf 100644 --- a/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py +++ b/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py @@ -18,7 +18,6 @@ from test_dist_base import TestParallelDyGraphRunnerBase, runtime_main import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable -from paddle.fluid.dygraph.nn import Linear, Pool2D class SimpleImgConvPool(fluid.dygraph.Layer): @@ -55,7 +54,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer): bias_attr=None, ) - self._pool2d = Pool2D( + self._pool2d = paddle.fluid.dygraph.nn.Pool2D( pool_size=pool_size, pool_type=pool_type, pool_stride=pool_stride, @@ -85,23 +84,21 @@ class MNIST(fluid.dygraph.Layer): self.pool_2_shape = 50 * 4 * 4 SIZE = 10 scale = (2.0 / (self.pool_2_shape**2 * SIZE)) ** 0.5 - self._fc = Linear( + self._fc = paddle.nn.Linear( self.pool_2_shape, 10, - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=scale - ) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale) ), - act="softmax", ) + self.act = paddle.nn.Softmax() def forward(self, inputs, label): x = self._simple_img_conv_pool_1(inputs) x = self._simple_img_conv_pool_2(x) x = paddle.reshape(x, shape=[-1, self.pool_2_shape]) cost = self._fc(x) - loss = fluid.layers.cross_entropy(cost, label) + loss = fluid.layers.cross_entropy(self.act(cost), label) avg_loss = paddle.mean(loss) return avg_loss diff --git a/python/paddle/fluid/tests/unittests/parallel_dygraph_shared_unused_var.py b/python/paddle/fluid/tests/unittests/parallel_dygraph_shared_unused_var.py index b5d584327b84c736413f886a5d6066c6249a4a01..1f063f849e35098aa5b44d4915b869b98854886d 100644 --- a/python/paddle/fluid/tests/unittests/parallel_dygraph_shared_unused_var.py +++ b/python/paddle/fluid/tests/unittests/parallel_dygraph_shared_unused_var.py @@ -19,6 +19,7 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear np.random.seed(2021) paddle.seed(1024) @@ -28,7 +29,7 @@ class SimpleNet(fluid.Layer): def __init__(self): # bias is unused parameters, and it share with net_a super().__init__() - self.net_a = Linear(input_dim=10, output_dim=5) + self.net_a = Linear(10, 5) self.net_b = Linear(10, 10) self.bias = self.net_a.bias diff --git a/python/paddle/fluid/tests/unittests/test_adam_op.py b/python/paddle/fluid/tests/unittests/test_adam_op.py index 07fef0b4603e88113037c1f864d64bb700ccec1b..d84366efdcb6989d9ef7ff9516df5910649ef3d2 100644 --- a/python/paddle/fluid/tests/unittests/test_adam_op.py +++ b/python/paddle/fluid/tests/unittests/test_adam_op.py @@ -641,7 +641,7 @@ class TestAdamOpV2(unittest.TestCase): paddle.disable_static() value = np.arange(26).reshape(2, 13).astype("float32") a = fluid.dygraph.to_variable(value) - linear = fluid.Linear(13, 5, dtype="float32") + linear = paddle.nn.Linear(13, 5) adam = paddle.optimizer.Adam( learning_rate=0.01, parameters=linear.parameters() @@ -690,7 +690,7 @@ class TestAdamOpV2(unittest.TestCase): paddle.disable_static() value = np.arange(26).reshape(2, 13).astype("float32") a = fluid.dygraph.to_variable(value) - linear = fluid.Linear(13, 5, dtype="float32") + linear = paddle.nn.Linear(13, 5) clip = fluid.clip.GradientClipByGlobalNorm(clip_norm=1.0) adam = paddle.optimizer.Adam( 0.1, parameters=linear.parameters(), grad_clip=clip @@ -1095,7 +1095,7 @@ class TestMultiTensorAdam(unittest.TestCase): trainable=True, ) if use_param_attr: - model = paddle.nn.Linear(5, 5, weight_attr) + model = paddle.nn.Linear(5, 5, weight_attr=weight_attr) else: model = paddle.nn.Linear(5, 5) diff --git a/python/paddle/fluid/tests/unittests/test_detach.py b/python/paddle/fluid/tests/unittests/test_detach.py index bd976cf39434777cc16e076be6c393e138432e77..cf7214b858889b8f0bf98793c2fc0c53a1ab3d33 100644 --- a/python/paddle/fluid/tests/unittests/test_detach.py +++ b/python/paddle/fluid/tests/unittests/test_detach.py @@ -18,7 +18,7 @@ import numpy as np import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph import Linear +from paddle.nn import Linear from paddle.fluid.dygraph.base import to_variable @@ -32,40 +32,40 @@ class Test_Detach(unittest.TestCase): def no_detach_multi(self): data = self.generate_Data() with fluid.dygraph.guard(): - linear_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(5.0) + linear_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(5.0) ) - linear_b_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(6.0) + linear_b_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(6.0) ) linear = Linear( 4, 10, - param_attr=linear_w_param_attrs, + weight_attr=linear_w_param_attrs, bias_attr=linear_b_param_attrs, ) - linear1_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(7.0) + linear1_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(7.0) ) - linear1_b_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(8.0) + linear1_b_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(8.0) ) linear1 = Linear( 10, 1, - param_attr=linear1_w_param_attrs, + weight_attr=linear1_w_param_attrs, bias_attr=linear1_b_param_attrs, ) - linear2_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(9.0) + linear2_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(9.0) ) - linear2_b_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(10.0) + linear2_b_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(10.0) ) linear2 = Linear( 10, 1, - param_attr=linear2_w_param_attrs, + weight_attr=linear2_w_param_attrs, bias_attr=linear2_b_param_attrs, ) data = to_variable(data) @@ -80,28 +80,28 @@ class Test_Detach(unittest.TestCase): def no_detach_single(self): data = self.generate_Data() with fluid.dygraph.guard(): - linear_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(5.0) + linear_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(5.0) ) - linear_b_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(6.0) + linear_b_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(6.0) ) linear = Linear( 4, 10, - param_attr=linear_w_param_attrs, + weight_attr=linear_w_param_attrs, bias_attr=linear_b_param_attrs, ) - linear1_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(7.0) + linear1_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(7.0) ) - linear1_b_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(8.0) + linear1_b_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(8.0) ) linear1 = Linear( 10, 1, - param_attr=linear1_w_param_attrs, + weight_attr=linear1_w_param_attrs, bias_attr=linear1_b_param_attrs, ) data = to_variable(data) @@ -115,8 +115,8 @@ class Test_Detach(unittest.TestCase): def detach_multi(self): data = self.generate_Data() with fluid.dygraph.guard(): - linear_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(5.0) + linear_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(5.0) ) linear_b_param_attrs = fluid.ParamAttr( initializer=fluid.initializer.Constant(6.0) @@ -124,11 +124,11 @@ class Test_Detach(unittest.TestCase): linear = Linear( 4, 10, - param_attr=linear_w_param_attrs, + weight_attr=linear_w_param_attrs, bias_attr=linear_b_param_attrs, ) - linear1_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(7.0) + linear1_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(7.0) ) linear1_b_param_attrs = fluid.ParamAttr( initializer=fluid.initializer.Constant(8.0) @@ -136,19 +136,19 @@ class Test_Detach(unittest.TestCase): linear1 = Linear( 10, 1, - param_attr=linear1_w_param_attrs, + weight_attr=linear1_w_param_attrs, bias_attr=linear1_b_param_attrs, ) - linear2_w_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(9.0) + linear2_w_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(9.0) ) - linear2_b_param_attrs = fluid.ParamAttr( - initializer=fluid.initializer.Constant(10.0) + linear2_b_param_attrs = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(10.0) ) linear2 = Linear( 10, 1, - param_attr=linear2_w_param_attrs, + weight_attr=linear2_w_param_attrs, bias_attr=linear2_b_param_attrs, ) data = to_variable(data) diff --git a/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py b/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py index 204361fc92d8b8a8012f7a3bd8fb42c7d17ae235..b9a130be6bfbb85dab769834d5664283b6548f6b 100644 --- a/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py +++ b/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py @@ -18,7 +18,7 @@ import numpy as np import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Linear, Pool2D +from paddle.nn import Linear from paddle.fluid.framework import _test_eager_guard @@ -57,7 +57,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer): bias_attr=bias_attr, ) - self._pool2d = Pool2D( + self._pool2d = paddle.fluid.dygraph.nn.Pool2D( pool_size=pool_size, pool_type=pool_type, pool_stride=pool_stride, @@ -104,13 +104,9 @@ class MNIST(fluid.dygraph.Layer): self._linear = Linear( self.pool_2_shape, 10, - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=scale - ) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale) ), - act="softmax", - dtype=dtype, ) def forward(self, inputs, label): @@ -118,6 +114,7 @@ class MNIST(fluid.dygraph.Layer): x = paddle.nn.functional.relu(self._simple_img_conv_pool_2(x)) x = paddle.reshape(x, shape=[-1, self.pool_2_shape]) cost = self._linear(x) + cost = paddle.nn.functional.softmax(cost) loss = fluid.layers.cross_entropy(cost, label) avg_loss = paddle.mean(loss) return avg_loss diff --git a/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py b/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py index 3c099642553035c124f563584437c64af75f0e69..d8b8c2ac4f0dff01fa477fe91967c4a189a7648f 100644 --- a/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py +++ b/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py @@ -20,8 +20,8 @@ from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid from paddle.fluid import core +from paddle.nn import Linear from paddle.fluid.dygraph.base import to_variable -from paddle.fluid.dygraph.nn import Linear, Pool2D from paddle.fluid.optimizer import SGDOptimizer SEED = 123123111 @@ -61,7 +61,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer): bias_attr=None, ) - self._pool2d = Pool2D( + self._pool2d = paddle.fluid.dygraph.nn.Pool2D( pool_size=pool_size, pool_type=pool_type, pool_stride=pool_stride, @@ -94,12 +94,9 @@ class MNIST(fluid.dygraph.Layer): self._fc = Linear( self.pool_2_shape, SIZE, - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=scale - ) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale) ), - act="softmax", ) def forward(self, inputs): @@ -107,6 +104,7 @@ class MNIST(fluid.dygraph.Layer): x = self._simple_img_conv_pool_2(x) x = paddle.reshape(x, shape=[-1, self.pool_2_shape]) x = self._fc(x) + x = paddle.nn.functional.softmax(x) return x diff --git a/python/paddle/fluid/tests/unittests/test_exception.py b/python/paddle/fluid/tests/unittests/test_exception.py index a42c2f5bad0f0a13405914957c5c7ef36cf2d2f7..c627f8688a158014952cae93bf47f4fd3ead1454 100644 --- a/python/paddle/fluid/tests/unittests/test_exception.py +++ b/python/paddle/fluid/tests/unittests/test_exception.py @@ -66,7 +66,7 @@ class TestExceptionNoCStack(unittest.TestCase): place = fluid.CPUPlace() with fluid.dygraph.guard(place): x = numpy.random.random(size=(10, 2)).astype('float32') - linear = fluid.dygraph.Linear(1, 10) + linear = paddle.nn.Linear(1, 10) data = fluid.dygraph.to_variable(x) with self.assertRaises(ValueError): res = linear(data) diff --git a/python/paddle/fluid/tests/unittests/test_gradient_clip.py b/python/paddle/fluid/tests/unittests/test_gradient_clip.py index ec2812e4ff32b7441eb2e06c4067d499d5b60584..71952b73f5bdced05ac56372f3de4d0758b2a36c 100644 --- a/python/paddle/fluid/tests/unittests/test_gradient_clip.py +++ b/python/paddle/fluid/tests/unittests/test_gradient_clip.py @@ -407,7 +407,7 @@ class TestGradientClipByValue(TestGradientClip): class TestDygraphGradientClip(unittest.TestCase): def test_gradient_clip(self): with fluid.dygraph.guard(): - linear = fluid.dygraph.Linear(5, 5) + linear = paddle.nn.Linear(5, 5) inputs = fluid.layers.uniform_random( [16, 5], min=-10, max=10 ).astype('float32') @@ -602,8 +602,8 @@ class TestDygraphGradientClipFP64(unittest.TestCase): with fluid.dygraph.guard(): inputs = fluid.layers.uniform_random( [16, 5], min=-10, max=10 - ).astype('float64') - linear = fluid.dygraph.Linear(5, 5, dtype="float64") + ).astype('float32') + linear = paddle.nn.Linear(5, 5) out = linear(fluid.dygraph.to_variable(inputs)) loss = fluid.layers.reduce_mean(out) loss.backward() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py b/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py index 5889d8299dc38913e3b94e6dfab7b14521130571..5fc83145d24f445cc62b311476df48fc5accb4a6 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py @@ -24,16 +24,20 @@ from paddle.fluid.framework import _test_eager_guard class AutoPruneLayer0(fluid.Layer): def __init__(self, input_size): super().__init__() - self.linear1 = fluid.dygraph.Linear( + self.linear1 = paddle.nn.Linear( input_size, 5, - param_attr=fluid.initializer.ConstantInitializer(value=2), + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=2) + ), bias_attr=False, ) - self.linear2 = fluid.dygraph.Linear( + self.linear2 = paddle.nn.Linear( 5, 5, - param_attr=fluid.initializer.ConstantInitializer(value=2), + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=2) + ), bias_attr=False, ) @@ -48,16 +52,20 @@ class AutoPruneLayer0(fluid.Layer): class AutoPruneLayer1(fluid.Layer): def __init__(self, input_size): super().__init__() - self.linear1 = fluid.dygraph.Linear( + self.linear1 = paddle.nn.Linear( input_size, 5, - param_attr=fluid.initializer.ConstantInitializer(value=2), + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=2) + ), bias_attr=False, ) - self.linear2 = fluid.dygraph.Linear( + self.linear2 = paddle.nn.Linear( 5, 5, - param_attr=fluid.initializer.ConstantInitializer(value=2), + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=2) + ), bias_attr=False, ) @@ -73,8 +81,8 @@ class AutoPruneLayer1(fluid.Layer): class AutoPruneLayer2(fluid.Layer): def __init__(self, input_size): super().__init__() - self.linear = fluid.dygraph.Linear(input_size, 10, act=None) - self.linear2 = fluid.dygraph.Linear(1, 1, act=None) + self.linear = paddle.nn.Linear(input_size, 10) + self.linear2 = paddle.nn.Linear(1, 1) def forward(self, x, label): feature = self.linear(x) @@ -90,7 +98,7 @@ class AutoPruneLayer2(fluid.Layer): class AutoPruneLayer3(fluid.Layer): def __init__(self, input_size): super().__init__() - self.linear = fluid.dygraph.Linear(input_size, 20, act=None) + self.linear = paddle.nn.Linear(input_size, 20) def forward(self, x, label, test_num): feature = self.linear(x) @@ -111,8 +119,8 @@ class MyLayer(fluid.Layer): super().__init__(dtype=dtype) self.embed0 = fluid.Embedding(size=(vocab_size, size)) self.embed1 = fluid.Embedding(size=(vocab_size, size)) - self.linear_0 = fluid.Linear(input_size, size, dtype=dtype) - self.linear_1 = fluid.Linear(input_size, size, dtype=dtype) + self.linear_0 = paddle.nn.Linear(input_size, size) + self.linear_1 = paddle.nn.Linear(input_size, size) def forward(self, x): # this method involves only the linear layers @@ -133,8 +141,8 @@ class MyLayer2(fluid.Layer): super().__init__(dtype=dtype) self.embed0 = fluid.Embedding(size=(vocab_size, size)) self.embed1 = fluid.Embedding(size=(vocab_size, size)) - self.linear_0 = fluid.Linear(input_size, size, dtype=dtype) - self.linear_1 = fluid.Linear(input_size, size, dtype=dtype) + self.linear_0 = paddle.nn.Linear(input_size, size) + self.linear_1 = paddle.nn.Linear(input_size, size) def forward(self, indices): # mind the difference with MyLayer @@ -253,8 +261,8 @@ class TestImperativeAutoPrune(unittest.TestCase): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") - linear = fluid.Linear(13, 5, dtype="float32") - linear2 = fluid.Linear(3, 3, dtype="float32") + linear = paddle.nn.Linear(13, 5) + linear2 = paddle.nn.Linear(3, 3) a = fluid.dygraph.to_variable(value0) b = fluid.dygraph.to_variable(value1) c = fluid.dygraph.to_variable(value2) @@ -276,8 +284,8 @@ class TestImperativeAutoPrune(unittest.TestCase): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") - linear = fluid.Linear(13, 5, dtype="float32") - linear2 = fluid.Linear(3, 3, dtype="float32") + linear = paddle.nn.Linear(13, 5) + linear2 = paddle.nn.Linear(3, 3) a = fluid.dygraph.to_variable(value0) b = fluid.dygraph.to_variable(value1) c = fluid.dygraph.to_variable(value2) @@ -299,8 +307,8 @@ class TestImperativeAutoPrune(unittest.TestCase): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") - linear = fluid.Linear(13, 5, dtype="float32") - linear2 = fluid.Linear(5, 3, dtype="float32") + linear = paddle.nn.Linear(13, 5) + linear2 = paddle.nn.Linear(5, 3) a = fluid.dygraph.to_variable(value0) b = fluid.dygraph.to_variable(value1) c = fluid.dygraph.to_variable(value2) @@ -332,8 +340,8 @@ class TestImperativeAutoPrune(unittest.TestCase): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") - linear = fluid.Linear(13, 5, dtype="float32") - linear2 = fluid.Linear(5, 3, dtype="float32") + linear = paddle.nn.Linear(13, 5) + linear2 = paddle.nn.Linear(5, 3) a = fluid.dygraph.to_variable(value0) b = fluid.dygraph.to_variable(value1) c = fluid.dygraph.to_variable(value2) @@ -367,8 +375,8 @@ class TestImperativeAutoPrune(unittest.TestCase): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") - linear = fluid.Linear(13, 5, dtype="float32") - linear2 = fluid.Linear(3, 3, dtype="float32") + linear = paddle.nn.Linear(13, 5) + linear2 = paddle.nn.Linear(3, 3) a = fluid.dygraph.to_variable(value0) b = fluid.dygraph.to_variable(value1) c = fluid.dygraph.to_variable(value2) @@ -462,7 +470,7 @@ class TestImperativeAutoPrune(unittest.TestCase): def func_case3_prune_no_grad_branch2(self): with fluid.dygraph.guard(): value1 = np.arange(1).reshape(1, 1) - linear = fluid.dygraph.Linear(1, 1, act=None) + linear = paddle.nn.Linear(1, 1) label = fluid.dygraph.to_variable(value1).astype("float32") label = linear(label) label = fluid.layers.cast(label, dtype="float32") diff --git a/python/paddle/fluid/tests/unittests/test_imperative_basic.py b/python/paddle/fluid/tests/unittests/test_imperative_basic.py index 595ec4fe3e60a0ec01fbb3b3892a7b402b2e9f3b..d6d40dfc61c73bcf2cee7d9b21dfbcf306bbb6cd 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_basic.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_basic.py @@ -19,8 +19,8 @@ from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid +from paddle.fluid import core import paddle.fluid.dygraph_utils as dygraph_utils -from paddle.fluid import Linear, core from paddle.fluid.dygraph.layer_object_helper import LayerObjectHelper from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard from paddle.fluid.layer_helper import LayerHelper @@ -41,24 +41,24 @@ class MyLayer(fluid.Layer): class MLP(fluid.Layer): def __init__(self, input_size): super().__init__() - self._linear1 = Linear( + self._linear1 = paddle.nn.Linear( input_size, 3, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), ) - self._linear2 = Linear( + self._linear2 = paddle.nn.Linear( 3, 4, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), ) @@ -269,9 +269,9 @@ class TestImperative(unittest.TestCase): def test_no_grad_guard(self): data = np.array([[2, 3], [4, 5]]).astype('float32') with fluid.dygraph.guard(): - l0 = fluid.Linear(2, 2) + l0 = paddle.nn.Linear(2, 2) self.assertIsNone(l0.weight._grad_ivar()) - l1 = fluid.Linear(2, 2) + l1 = paddle.nn.Linear(2, 2) with fluid.dygraph.no_grad(): self.assertTrue(l1.weight.stop_gradient is False) tmp = l1.weight * 2 @@ -287,9 +287,9 @@ class TestImperative(unittest.TestCase): def test_paddle_imperative_no_grad_guard(self): data = np.array([[2, 3], [4, 5]]).astype('float32') with fluid.dygraph.guard(): - l0 = fluid.Linear(2, 2) + l0 = paddle.nn.Linear(2, 2) self.assertIsNone(l0.weight._grad_ivar()) - l1 = fluid.Linear(2, 2) + l1 = paddle.nn.Linear(2, 2) with paddle.no_grad(): self.assertTrue(l1.weight.stop_gradient is False) tmp = l1.weight * 2 @@ -305,9 +305,9 @@ class TestImperative(unittest.TestCase): def test_paddle_imperative_set_grad_enabled(self): data = np.array([[2, 3], [4, 5]]).astype('float32') with fluid.dygraph.guard(): - l0 = fluid.Linear(2, 2) + l0 = paddle.nn.Linear(2, 2) self.assertIsNone(l0.weight._grad_ivar()) - l1 = fluid.Linear(2, 2) + l1 = paddle.nn.Linear(2, 2) with paddle.set_grad_enabled(False): self.assertTrue(l1.weight.stop_gradient is False) tmp = l1.weight * 2 @@ -863,7 +863,7 @@ class TestImperative(unittest.TestCase): self.assertRaises(TypeError, my_layer.__setattr__, 'w1', 'str') my_layer.w1 = None self.assertEqual(len(my_layer.parameters()), 0) - my_layer.l1 = fluid.dygraph.Linear(3, 3) + my_layer.l1 = paddle.nn.Linear(3, 3) self.assertEqual(len(my_layer.sublayers()), 1) self.assertRaises(TypeError, my_layer.__setattr__, 'l1', 'str') my_layer.l1 = None diff --git a/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py b/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py index e90a16def8f5bc5951ea7731f3ac235bba01534f..0675a67193781b37e10619b866b0d62a7c6eadda 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py @@ -35,7 +35,7 @@ class MyLayer(fluid.Layer): class TestImperativeContainer(unittest.TestCase): def paddle_imperative_list(self): return paddle.nn.LayerList( - [fluid.dygraph.Linear(2**i, 2 ** (i + 1)) for i in range(6)] + [paddle.nn.Linear(2**i, 2 ** (i + 1)) for i in range(6)] ) def layer_list(self, use_fluid_api): @@ -48,13 +48,13 @@ class TestImperativeContainer(unittest.TestCase): model = MyLayer(layerlist) res1 = model(x) self.assertListEqual(res1.shape, [5, 2**size]) - model.layerlist[size - 1] = fluid.dygraph.Linear(2 ** (size - 1), 5) + model.layerlist[size - 1] = paddle.nn.Linear(2 ** (size - 1), 5) res2 = model(x) self.assertListEqual(res2.shape, [5, 5]) del model.layerlist[size - 1] res3 = model(x) self.assertListEqual(res3.shape, [5, 2 ** (size - 1)]) - model.layerlist.append(fluid.dygraph.Linear(2 ** (size - 1), 3)) + model.layerlist.append(paddle.nn.Linear(2 ** (size - 1), 3)) res4 = model(x) self.assertListEqual(res4.shape, [5, 3]) res4.backward() @@ -68,14 +68,14 @@ class TestImperativeContainer(unittest.TestCase): res6.backward() model3 = MyLayer(layerlist[:-2]) - model3.layerlist.append(fluid.dygraph.Linear(3, 1)) + model3.layerlist.append(paddle.nn.Linear(3, 1)) model3.layerlist.insert( - size - 2, fluid.dygraph.Linear(2 ** (size - 2), 3) + size - 2, paddle.nn.Linear(2 ** (size - 2), 3) ) res7 = model3(x) self.assertListEqual(res7.shape, [5, 1]) to_be_extended = [ - fluid.dygraph.Linear(3**i, 3 ** (i + 1)) for i in range(3) + paddle.nn.Linear(3**i, 3 ** (i + 1)) for i in range(3) ] model3.layerlist.extend(to_be_extended) res8 = model3(x) @@ -83,13 +83,13 @@ class TestImperativeContainer(unittest.TestCase): res8.backward() model4 = MyLayer(layerlist[:3]) - model4.layerlist[-1] = fluid.dygraph.Linear(4, 5) + model4.layerlist[-1] = paddle.nn.Linear(4, 5) res9 = model4(x) self.assertListEqual(res9.shape, [5, 5]) del model4.layerlist[-1] res10 = model4(x) self.assertListEqual(res10.shape, [5, 4]) - model4.layerlist.insert(-1, fluid.dygraph.Linear(2, 2)) + model4.layerlist.insert(-1, paddle.nn.Linear(2, 2)) res11 = model4(x) self.assertListEqual(res11.shape, [5, 4]) res11.backward() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_container_sequential.py b/python/paddle/fluid/tests/unittests/test_imperative_container_sequential.py index 7ed45d58703c22d747982d7da54dcd746acdf4f6..eca6e5d81d0109bd6b6f9e889c207ecdcff873ec 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_container_sequential.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_container_sequential.py @@ -15,6 +15,7 @@ import unittest import numpy as np +from paddle.nn import Linear import paddle.fluid as fluid from paddle.fluid.framework import _test_eager_guard @@ -25,19 +26,17 @@ class TestImperativeContainerSequential(unittest.TestCase): data = np.random.uniform(-1, 1, [5, 10]).astype('float32') with fluid.dygraph.guard(): data = fluid.dygraph.to_variable(data) - model1 = fluid.dygraph.Sequential( - fluid.Linear(10, 1), fluid.Linear(1, 2) - ) + model1 = fluid.dygraph.Sequential(Linear(10, 1), Linear(1, 2)) res1 = model1(data) self.assertListEqual(res1.shape, [5, 2]) - model1[1] = fluid.Linear(1, 3) + model1[1] = Linear(1, 3) res1 = model1(data) self.assertListEqual(res1.shape, [5, 3]) loss1 = fluid.layers.reduce_mean(res1) loss1.backward() - l1 = fluid.Linear(10, 1) - l2 = fluid.Linear(1, 3) + l1 = Linear(10, 1) + l2 = Linear(1, 3) model2 = fluid.dygraph.Sequential(('l1', l1), ('l2', l2)) self.assertEqual(len(model2), 2) res2 = model2(data) @@ -48,8 +47,8 @@ class TestImperativeContainerSequential(unittest.TestCase): self.assertEqual(len(model2), 1) res2 = model2(data) self.assertListEqual(res2.shape, [5, 1]) - model2.add_sublayer('l3', fluid.Linear(1, 3)) - model2.add_sublayer('l4', fluid.Linear(3, 4)) + model2.add_sublayer('l3', Linear(1, 3)) + model2.add_sublayer('l4', Linear(3, 4)) self.assertEqual(len(model2), 3) res2 = model2(data) self.assertListEqual(res2.shape, [5, 4]) @@ -66,19 +65,17 @@ class TestImperativeContainerSequential(unittest.TestCase): data = np.random.uniform(-1, 1, [5, 10]).astype('float32') with fluid.dygraph.guard(): data = fluid.dygraph.to_variable(data) - model1 = fluid.dygraph.Sequential( - fluid.Linear(10, 1), fluid.Linear(1, 2) - ) + model1 = fluid.dygraph.Sequential(Linear(10, 1), Linear(1, 2)) res1 = model1(data) self.assertListEqual(res1.shape, [5, 2]) - model1[1] = fluid.Linear(1, 3) + model1[1] = Linear(1, 3) res1 = model1(data) self.assertListEqual(res1.shape, [5, 3]) loss1 = fluid.layers.reduce_mean(res1) loss1.backward() - l1 = fluid.Linear(10, 1) - l2 = fluid.Linear(1, 3) + l1 = Linear(10, 1) + l2 = Linear(1, 3) model2 = fluid.dygraph.Sequential(['l1', l1], ['l2', l2]) self.assertEqual(len(model2), 2) res2 = model2(data) @@ -89,8 +86,8 @@ class TestImperativeContainerSequential(unittest.TestCase): self.assertEqual(len(model2), 1) res2 = model2(data) self.assertListEqual(res2.shape, [5, 1]) - model2.add_sublayer('l3', fluid.Linear(1, 3)) - model2.add_sublayer('l4', fluid.Linear(3, 4)) + model2.add_sublayer('l3', Linear(1, 3)) + model2.add_sublayer('l4', Linear(3, 4)) self.assertEqual(len(model2), 3) res2 = model2(data) self.assertListEqual(res2.shape, [5, 4]) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py b/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py index b02b51050001636c9fa00fcd9309eca5022fa64f..e3b50aa123b36f3334067fbb0a8c0ae74042d7e6 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py @@ -19,7 +19,7 @@ import numpy as np import paddle.fluid as fluid import paddle.fluid.core as core import paddle.fluid.dygraph as dygraph -from paddle.fluid.dygraph.nn import Linear +from paddle.nn import Linear class MLP(fluid.Layer): diff --git a/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py b/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py index 38c19677f79c1cc80571f194d2b393fc9162aa35..15f9365772b3ebe9db47938aaca3da636b429001 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py @@ -23,8 +23,8 @@ from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid import paddle.fluid.core as core -from paddle.fluid.dygraph import Linear from paddle.fluid.dygraph.base import to_variable +from paddle.nn import Linear from paddle.fluid.framework import _test_eager_guard @@ -44,20 +44,30 @@ class DMF(fluid.Layer): Linear( 256 if i == 0 else self._hid_sizes[i - 1], self._hid_sizes[i], - act='relu', ), ) ) + self._user_layers.append( + self.add_sublayer( + 'user_layer_act_%d' % i, + paddle.nn.ReLU(), + ) + ) self._item_layers.append( self.add_sublayer( 'item_layer_%d' % i, Linear( 256 if i == 0 else self._hid_sizes[i - 1], self._hid_sizes[i], - act='relu', ), ) ) + self._item_layers.append( + self.add_sublayer( + 'item_layer_act_%d' % i, + paddle.nn.ReLU(), + ) + ) def forward(self, users, items): users = self._user_latent(users) @@ -83,10 +93,15 @@ class MLP(fluid.Layer): Linear( 256 * 2 if i == 0 else self._hid_sizes[i - 1], self._hid_sizes[i], - act='relu', ), ) ) + self._match_layers.append( + self.add_sublayer( + 'match_layer_act_%d' % i, + paddle.nn.ReLU(), + ) + ) def forward(self, users, items): users = self._user_latent(users) @@ -115,7 +130,7 @@ class DeepCF(fluid.Layer): self._mlp = MLP() self._dmf = DMF() - self._match_fc = Linear(128, 1, act='sigmoid') + self._match_fc = Linear(128, 1) def forward(self, users, items): # users_emb = self._user_emb(users) @@ -134,6 +149,7 @@ class DeepCF(fluid.Layer): [mlp_predictive, dmf_predictive], axis=len(mlp_predictive.shape) - 1 ) prediction = self._match_fc(predictive) + prediction = paddle.nn.functional.sigmoid(prediction) return prediction diff --git a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py index ec879d9cf0178c43fa610a5820981af6e3e4b853..2c020c0465bb71ae03f0be338de1c6afe96548e2 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py @@ -586,7 +586,7 @@ class TestDygraphDoubleGradVisitedUniq(TestCase): ) def model_f(input): - linear = fluid.dygraph.Linear(5, 3, bias_attr=False) + linear = paddle.nn.Linear(5, 3) for i in range(10): if i == 0: out = linear(input) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_framework.py b/python/paddle/fluid/tests/unittests/test_imperative_framework.py index fd86679a77a203f7af7f8850017920083d6b4097..4aec8b308a6c8d51bc9d8daf6e22051387c43dfa 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_framework.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_framework.py @@ -20,29 +20,30 @@ from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid from paddle.fluid.framework import _test_eager_guard +import paddle class MLP(fluid.Layer): def __init__(self, input_size): super().__init__() - self._linear1 = fluid.dygraph.Linear( + self._linear1 = paddle.nn.Linear( input_size, 3, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), ) - self._linear2 = fluid.dygraph.Linear( + self._linear2 = paddle.nn.Linear( 3, 4, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.1) ), ) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_gan.py b/python/paddle/fluid/tests/unittests/test_imperative_gan.py index 781253449d58a8770a1e00cb924a1a578a5baf12..0b1ee16d32f58398ad8b46aad4ad219e9f4dc785 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_gan.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_gan.py @@ -20,7 +20,7 @@ from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid import paddle.fluid.core as core -from paddle.fluid import Linear +from paddle.nn import Linear from paddle.fluid.dygraph.base import to_variable from paddle.fluid.framework import _test_eager_guard from paddle.fluid.optimizer import SGDOptimizer @@ -29,11 +29,12 @@ from paddle.fluid.optimizer import SGDOptimizer class Discriminator(fluid.Layer): def __init__(self): super().__init__() - self._fc1 = Linear(1, 32, act='elu') + self._fc1 = Linear(1, 32) self._fc2 = Linear(32, 1) def forward(self, inputs): x = self._fc1(inputs) + x = paddle.nn.functional.elu(x) x = self._fc2(x) return x @@ -41,13 +42,15 @@ class Discriminator(fluid.Layer): class Generator(fluid.Layer): def __init__(self): super().__init__() - self._fc1 = Linear(2, 64, act='elu') - self._fc2 = Linear(64, 64, act='elu') + self._fc1 = Linear(2, 64) + self._fc2 = Linear(64, 64) self._fc3 = Linear(64, 1) def forward(self, inputs): x = self._fc1(inputs) + x = paddle.nn.functional.elu(x) x = self._fc2(x) + x = paddle.nn.functional.elu(x) x = self._fc3(x) return x diff --git a/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py b/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py index 577569455233644af3b8f0602c229fcc6269cb85..5354b6c403096f704309a20a11be3b911ccfec0c 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py @@ -29,10 +29,10 @@ class LeNetDygraph(fluid.dygraph.Layer): self.features = nn.Sequential( nn.Conv2D(1, 6, 3, stride=1, padding=1), nn.ReLU(), - paddle.fluid.dygraph.Pool2D(2, 'max', 2), + paddle.nn.MaxPool2D(2, 2), nn.Conv2D(6, 16, 5, stride=1, padding=0), nn.ReLU(), - paddle.fluid.dygraph.Pool2D(2, 'max', 2), + paddle.nn.MaxPool2D(2, 2), ) if num_classes > 0: diff --git a/python/paddle/fluid/tests/unittests/test_imperative_layer_children.py b/python/paddle/fluid/tests/unittests/test_imperative_layer_children.py index 59717d48949332a8e7e95e246f86511f51c55ca1..410ed77de26e83bd29cfae91b8817d9d4819b608 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_layer_children.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_layer_children.py @@ -28,10 +28,10 @@ class LeNetDygraph(fluid.dygraph.Layer): self.features = nn.Sequential( nn.Conv2D(1, 6, 3, stride=1, padding=1), nn.ReLU(), - paddle.fluid.dygraph.Pool2D(2, 'max', 2), + paddle.nn.MaxPool2D(2, 2), nn.Conv2D(6, 16, 5, stride=1, padding=0), nn.ReLU(), - paddle.fluid.dygraph.Pool2D(2, 'max', 2), + paddle.nn.MaxPool2D(2, 2), ) def forward(self, inputs): diff --git a/python/paddle/fluid/tests/unittests/test_imperative_layer_trainable.py b/python/paddle/fluid/tests/unittests/test_imperative_layer_trainable.py index abcb811f671a6c1f8a9bc5b0115402d6d3a3df72..335db28d70c2d036e53f7a07d5902067530efad2 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_layer_trainable.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_layer_trainable.py @@ -19,6 +19,7 @@ import numpy as np import paddle.fluid as fluid import paddle.fluid.dygraph as dygraph from paddle.fluid.framework import _test_eager_guard +import paddle class TestImperativeLayerTrainable(unittest.TestCase): @@ -28,7 +29,7 @@ class TestImperativeLayerTrainable(unittest.TestCase): label = dygraph.to_variable(label) - linear = dygraph.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) y = linear(label) self.assertFalse(y.stop_gradient) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_load_static_param.py b/python/paddle/fluid/tests/unittests/test_imperative_load_static_param.py index b3e23565799b18629b15b0d9d37face00f004aa8..8e9b6c7f2ff8ef40b0cd7fa0fa2fe97c41fd21b1 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_load_static_param.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_load_static_param.py @@ -24,13 +24,16 @@ import paddle.fluid.framework as framework from paddle.fluid.dygraph.nn import ( NCE, BatchNorm, - Conv3D, Embedding, GroupNorm, LayerNorm, - Linear, + NCE, PRelu, ) +from paddle.nn import Linear +import numpy as np +import os +import tempfile class TestDygraphLoadStatic(unittest.TestCase): @@ -200,11 +203,11 @@ class TestDygraphLoadStatic(unittest.TestCase): in_channels=10, out_channels=10, kernel_size=5 ) - self.conv3d_1 = Conv3D( - num_channels=3, num_filters=2, filter_size=3, act="relu" + self.conv3d_1 = paddle.nn.Conv3D( + in_channels=3, out_channels=2, kernel_size=3 ) - self.conv3d_2 = Conv3D( - num_channels=3, num_filters=2, filter_size=3, act="relu" + self.conv3d_2 = paddle.nn.Conv3D( + in_channels=3, out_channels=2, kernel_size=3 ) self.batch_norm_1 = BatchNorm(10) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_mnist.py b/python/paddle/fluid/tests/unittests/test_imperative_mnist.py index 2ca175390d5516fd724f8c31bcf1bd3701543749..69796f69c6b23c9da03ccd350fd92ff6b605ca2a 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_mnist.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_mnist.py @@ -15,15 +15,16 @@ import unittest import numpy as np -from test_imperative_base import new_program_scope from utils import DyGraphProgramDescTracerTestHelper import paddle import paddle.fluid as fluid from paddle.fluid import core -from paddle.fluid.dygraph.nn import Linear, Pool2D -from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard from paddle.fluid.optimizer import SGDOptimizer +from paddle.nn import Linear +from test_imperative_base import new_program_scope +from utils import DyGraphProgramDescTracerTestHelper +from paddle.fluid.framework import _test_eager_guard, _in_legacy_dygraph class SimpleImgConvPool(fluid.dygraph.Layer): @@ -59,8 +60,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer): weight_attr=None, bias_attr=None, ) - - self._pool2d = Pool2D( + self._pool2d = paddle.fluid.dygraph.nn.Pool2D( pool_size=pool_size, pool_type=pool_type, pool_stride=pool_stride, @@ -93,12 +93,9 @@ class MNIST(fluid.dygraph.Layer): self._fc = Linear( self.pool_2_shape, 10, - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=scale - ) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale) ), - act="softmax", ) def forward(self, inputs): @@ -106,6 +103,7 @@ class MNIST(fluid.dygraph.Layer): x = self._simple_img_conv_pool_2(x) x = paddle.reshape(x, shape=[-1, self.pool_2_shape]) x = self._fc(x) + x = paddle.nn.functional.softmax(x) return x diff --git a/python/paddle/fluid/tests/unittests/test_imperative_named_members.py b/python/paddle/fluid/tests/unittests/test_imperative_named_members.py index 622839253d2ab25b419d7a005a52149a1346b613..faaa02ea46a5d0a5d11c21661d92fb85bfd04c2a 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_named_members.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_named_members.py @@ -24,7 +24,7 @@ from paddle.fluid.framework import _test_eager_guard class MyLayer(fluid.Layer): def __init__(self, num_channel, dim, num_filter=5): super().__init__() - self.fc = fluid.dygraph.Linear(dim, dim) + self.fc = paddle.nn.Linear(dim, dim) self.conv = paddle.nn.Conv2D(num_channel, num_channel, num_filter) def forward(self, x): @@ -36,8 +36,8 @@ class MyLayer(fluid.Layer): class TestImperativeNamedSubLayers(unittest.TestCase): def func_test_named_sublayers(self): with fluid.dygraph.guard(): - fc1 = fluid.Linear(10, 3) - fc2 = fluid.Linear(3, 10, bias_attr=False) + fc1 = paddle.nn.Linear(10, 3) + fc2 = paddle.nn.Linear(3, 10, bias_attr=False) custom = MyLayer(3, 10) model = paddle.nn.Sequential(fc1, fc2, custom) named_sublayers = model.named_sublayers() @@ -71,8 +71,8 @@ class TestImperativeNamedSubLayers(unittest.TestCase): class TestImperativeNamedParameters(unittest.TestCase): def func_test_named_parameters(self): with fluid.dygraph.guard(): - fc1 = fluid.Linear(10, 3) - fc2 = fluid.Linear(3, 10, bias_attr=False) + fc1 = paddle.nn.Linear(10, 3) + fc2 = paddle.nn.Linear(3, 10, bias_attr=False) custom = MyLayer(3, 10) model = paddle.nn.Sequential(fc1, fc2, custom) @@ -98,8 +98,8 @@ class TestImperativeNamedParameters(unittest.TestCase): class Mymodel(fluid.dygraph.Layer): def __init__(self): super().__init__() - self.linear1 = fluid.dygraph.Linear(10, 10) - self.linear2 = fluid.dygraph.Linear(5, 5) + self.linear1 = paddle.nn.Linear(10, 10) + self.linear2 = paddle.nn.Linear(5, 5) self.conv2d = paddle.nn.Conv2D(3, 2, 3) self.embedding = fluid.dygraph.Embedding(size=[128, 16]) self.h_0 = fluid.dygraph.to_variable( diff --git a/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py b/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py index fcaafc72b8cce0c8f6af3a96405999fb5e9ead32..d89d7d6b258b24f4041623425aaff305cb965769 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py @@ -26,8 +26,8 @@ from paddle.fluid.dygraph.nn import ( Embedding, GRUUnit, Linear, - Pool2D, ) +from paddle.nn import Linear from paddle.fluid.framework import _test_eager_guard @@ -112,11 +112,9 @@ class ConvBNPool(fluid.dygraph.Layer): self.bn_1_layer = BatchNorm(out_ch[1], act=act, is_test=is_test) if self.pool: - self.pool_layer = Pool2D( - pool_size=2, - pool_type='max', - pool_stride=2, - use_cudnn=use_cudnn, + self.pool_layer = paddle.nn.MaxPool2D( + kernel_size=2, + stride=2, ceil_mode=True, ) @@ -232,10 +230,10 @@ class EncoderNet(fluid.dygraph.Layer): self.ocr_convs = OCRConv(is_test=is_test, use_cudnn=use_cudnn) self.fc_1_layer = Linear( - 32, rnn_hidden_size * 3, param_attr=para_attr, bias_attr=False + 32, rnn_hidden_size * 3, weight_attr=para_attr, bias_attr=False ) self.fc_2_layer = Linear( - 32, rnn_hidden_size * 3, param_attr=para_attr, bias_attr=False + 32, rnn_hidden_size * 3, weight_attr=para_attr, bias_attr=False ) self.gru_forward_layer = DynamicGRU( size=rnn_hidden_size, @@ -295,10 +293,8 @@ class SimpleAttention(fluid.dygraph.Layer): def __init__(self, decoder_size): super().__init__() - self.fc_1 = Linear( - decoder_size, decoder_size, act=None, bias_attr=False - ) - self.fc_2 = Linear(decoder_size, 1, act=None, bias_attr=False) + self.fc_1 = Linear(decoder_size, decoder_size, bias_attr=False) + self.fc_2 = Linear(decoder_size, 1, bias_attr=False) def forward(self, encoder_vec, encoder_proj, decoder_state): @@ -344,9 +340,7 @@ class GRUDecoderWithAttention(fluid.dygraph.Layer): self.gru_unit = GRUUnit( size=decoder_size * 3, param_attr=None, bias_attr=None ) - self.out_layer = Linear( - decoder_size, num_classes + 2, bias_attr=None, act='softmax' - ) + self.out_layer = Linear(decoder_size, num_classes + 2, bias_attr=None) self.decoder_size = decoder_size @@ -373,6 +367,7 @@ class GRUDecoderWithAttention(fluid.dygraph.Layer): h, _, _ = self.gru_unit(decoder_inputs, hidden_mem) hidden_mem = h out = self.out_layer(h) + out = paddle.nn.functional.softmax(out) res.append(out) res1 = fluid.layers.concat(res, axis=1) @@ -388,7 +383,6 @@ class OCRAttention(fluid.dygraph.Layer): Config.encoder_size, Config.decoder_size, bias_attr=False, - act='relu', ) self.embedding = Embedding( [Config.num_classes + 2, Config.word_vector_dim], dtype='float32' @@ -406,6 +400,7 @@ class OCRAttention(fluid.dygraph.Layer): backward_first, [-1, backward_first.shape[2]] ) decoder_boot = self.fc(backward_first) + decoder_boot = paddle.nn.functional.relu(decoder_boot) label_in = paddle.reshape(label_in, [-1]) trg_embedding = self.embedding(label_in) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_optimizer.py b/python/paddle/fluid/tests/unittests/test_imperative_optimizer.py index 521ff77d58f35810cf9c58af70d92a0c8f1a0408..1650532e49d7a98da6b302543671172de3f69258 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_optimizer.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_optimizer.py @@ -16,14 +16,10 @@ import itertools import unittest import numpy as np -from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid -from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer from paddle.fluid import core -from paddle.fluid.dygraph import Linear -from paddle.fluid.framework import _test_eager_guard from paddle.fluid.optimizer import ( AdadeltaOptimizer, AdagradOptimizer, @@ -43,6 +39,10 @@ from paddle.fluid.optimizer import ( RMSPropOptimizer, SGDOptimizer, ) +from test_imperative_base import new_program_scope +from paddle.fluid.framework import _test_eager_guard + +from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer # Note(wangzhongpu) # In dygraph, don't support ModelAverage, DGCMomentumOptimizer, ExponentialMovingAverage, PipelineOptimizer, LookaheadOptimizer, RecomputeOptimizer. @@ -52,8 +52,8 @@ class MLP(fluid.Layer): def __init__(self, param_attr=None, bias_attr=None): super().__init__() - self._fc1 = Linear(784, 10) - self._fc2 = Linear(10, 10) + self._fc1 = paddle.nn.Linear(784, 10) + self._fc2 = paddle.nn.Linear(10, 10) def forward(self, inputs): y = self._fc1(inputs) @@ -473,7 +473,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) @@ -504,7 +504,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) @@ -540,7 +540,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) @@ -579,7 +579,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) @@ -951,8 +951,8 @@ class TestImperativeRecomputeOptimizer(TestImperativeOptimizerBase): class TestImperativeOptimizerList(unittest.TestCase): def func_test_parameter_list(self): with fluid.dygraph.guard(): - linear_1 = Linear(10, 10) - linear_2 = Linear(10, 10) + linear_1 = paddle.nn.Linear(10, 10) + linear_2 = paddle.nn.Linear(10, 10) sgd = SGDOptimizer( 1.0, diff --git a/python/paddle/fluid/tests/unittests/test_imperative_optimizer_v2.py b/python/paddle/fluid/tests/unittests/test_imperative_optimizer_v2.py index 8bc9a953aaf2976ee67503e4118f99da9c2f4a26..48ee814b4ddc9196d2cf89a1f0f22c7efee90be3 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_optimizer_v2.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_optimizer_v2.py @@ -16,14 +16,10 @@ import itertools import unittest import numpy as np -from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid -from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer from paddle.fluid import core -from paddle.fluid.dygraph import Linear -from paddle.fluid.framework import _test_eager_guard from paddle.fluid.optimizer import ( AdadeltaOptimizer, AdagradOptimizer, @@ -40,6 +36,10 @@ from paddle.fluid.optimizer import ( RecomputeOptimizer, RMSPropOptimizer, ) +from test_imperative_base import new_program_scope +from paddle.fluid.framework import _test_eager_guard + +from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer # Note(wangzhongpu) # In dygraph, don't support ModelAverage, DGCMomentumOptimizer, ExponentialMovingAverage, PipelineOptimizer, LookaheadOptimizer, RecomputeOptimizer. @@ -49,8 +49,8 @@ class MLP(fluid.Layer): def __init__(self, param_attr=None, bias_attr=None): super().__init__() - self._fc1 = Linear(784, 10) - self._fc2 = Linear(10, 10) + self._fc1 = paddle.nn.Linear(784, 10) + self._fc2 = paddle.nn.Linear(10, 10) def forward(self, inputs): y = self._fc1(inputs) @@ -611,7 +611,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) @@ -640,7 +640,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) @@ -674,7 +674,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) b = linear(a) @@ -704,7 +704,7 @@ class TestOptimizerLearningRate(unittest.TestCase): with fluid.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) a = fluid.dygraph.to_variable(a) @@ -1071,8 +1071,8 @@ class TestImperativeRecomputeOptimizer(TestImperativeOptimizerBase): class TestImperativeOptimizerList(unittest.TestCase): def func_test_parameter_list(self): with fluid.dygraph.guard(): - linear_1 = Linear(10, 10) - linear_2 = Linear(10, 10) + linear_1 = paddle.nn.Linear(10, 10) + linear_2 = paddle.nn.Linear(10, 10) sgd = paddle.optimizer.SGD( 1.0, diff --git a/python/paddle/fluid/tests/unittests/test_imperative_partitial_backward.py b/python/paddle/fluid/tests/unittests/test_imperative_partitial_backward.py index 042fb294ff9fd103d07c968b5007791822f2edfb..29d42076e2c73956113d6d8b3d335023cc3230f2 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_partitial_backward.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_partitial_backward.py @@ -18,6 +18,7 @@ import numpy as np import paddle.fluid as fluid from paddle.fluid.framework import _test_eager_guard +import paddle class TestImperativePartitialBackward(unittest.TestCase): @@ -25,8 +26,8 @@ class TestImperativePartitialBackward(unittest.TestCase): with fluid.dygraph.guard(): x = np.random.randn(2, 4, 5).astype("float32") x = fluid.dygraph.to_variable(x) - linear1 = fluid.dygraph.Linear(5, 10) - linear2 = fluid.dygraph.Linear(5, 10) + linear1 = paddle.nn.Linear(5, 10) + linear2 = paddle.nn.Linear(5, 10) y = linear1(x[:, :2]) z = linear2(x[:, 2:]) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py b/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py index 0181c7a431c369fab5f2edc9be24a5303c46fba9..bea24aa27393275e43aae7a976ae72db1c118b54 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py @@ -15,12 +15,11 @@ import unittest import numpy as np -from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid -import paddle.fluid.dygraph.nn as nn from paddle.fluid import core +from test_imperative_base import new_program_scope from paddle.fluid.framework import _test_eager_guard from paddle.fluid.optimizer import SGDOptimizer @@ -29,8 +28,8 @@ class Policy(fluid.dygraph.Layer): def __init__(self, input_size): super().__init__() - self.affine1 = nn.Linear(input_size, 128) - self.affine2 = nn.Linear(128, 2) + self.affine1 = paddle.nn.Linear(input_size, 128) + self.affine2 = paddle.nn.Linear(128, 2) self.dropout_ratio = 0.6 self.saved_log_probs = [] diff --git a/python/paddle/fluid/tests/unittests/test_imperative_resnet.py b/python/paddle/fluid/tests/unittests/test_imperative_resnet.py index eca1e2d8cce26300d62e4a7f33e800d3f29cbed1..6c3fdf77a2d1c739475d428ea132472db41ec05c 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_resnet.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_resnet.py @@ -20,7 +20,8 @@ from utils import DyGraphProgramDescTracerTestHelper, is_equal_program import paddle import paddle.fluid as fluid -from paddle.fluid import BatchNorm, Linear, Pool2D, core +from paddle.fluid import core +from paddle.fluid import BatchNorm from paddle.fluid.dygraph.base import to_variable from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard from paddle.fluid.layer_helper import LayerHelper @@ -193,8 +194,8 @@ class ResNet(fluid.Layer): act='relu', use_cudnn=use_cudnn, ) - self.pool2d_max = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' + self.pool2d_max = paddle.nn.MaxPool2D( + kernel_size=3, stride=2, padding=1 ) self.bottleneck_block_list = [] @@ -215,8 +216,7 @@ class ResNet(fluid.Layer): ) self.bottleneck_block_list.append(bottleneck_block) shortcut = True - - self.pool2d_avg = Pool2D( + self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( pool_size=7, pool_type='avg', global_pooling=True ) @@ -226,11 +226,10 @@ class ResNet(fluid.Layer): stdv = 1.0 / math.sqrt(2048 * 1.0) - self.out = Linear( + self.out = paddle.nn.Linear( self.pool2d_avg_output, class_dim, - act='softmax', - param_attr=fluid.param_attr.ParamAttr( + weight_attr=fluid.param_attr.ParamAttr( initializer=fluid.initializer.Uniform(-stdv, stdv) ), ) @@ -243,6 +242,7 @@ class ResNet(fluid.Layer): y = self.pool2d_avg(y) y = paddle.reshape(y, shape=[-1, self.pool2d_avg_output]) y = self.out(y) + y = paddle.nn.functional.softmax(y) return y diff --git a/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py b/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py index 1970c63bace0576eef28dbdfedaee72c01ebeb57..265f3720680088cce378c6593d159b12943c474b 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py @@ -20,7 +20,8 @@ from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid from paddle.fluid import core -from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D +from paddle.fluid.dygraph.nn import BatchNorm +from test_imperative_base import new_program_scope from paddle.fluid.framework import _test_eager_guard from paddle.fluid.layer_helper import LayerHelper @@ -104,29 +105,34 @@ class SqueezeExcitation(fluid.dygraph.Layer): super().__init__() self._num_channels = num_channels - self._pool = Pool2D(pool_size=0, pool_type='avg', global_pooling=True) - self._squeeze = Linear( + self._pool = paddle.fluid.dygraph.nn.Pool2D( + pool_size=0, pool_type='avg', global_pooling=True + ) + self._squeeze = paddle.nn.Linear( num_channels, num_channels // reduction_ratio, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.05) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.05) ), - act='relu', ) - self._excitation = Linear( + self.act_1 = paddle.nn.ReLU() + self._excitation = paddle.nn.Linear( num_channels // reduction_ratio, num_channels, - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.05) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.05) ), - act='sigmoid', ) + self.act_2 = paddle.nn.Softmax() + def forward(self, input): y = self._pool(input) y = paddle.reshape(y, shape=[-1, self._num_channels]) y = self._squeeze(y) + y = self.act_1(y) y = self._excitation(y) + y = self.act_2(y) y = fluid.layers.elementwise_mul(x=input, y=y, axis=0) return y @@ -218,9 +224,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=2, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) elif layers == 101: cardinality = 32 reduction_ratio = 16 @@ -233,9 +237,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=2, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) elif layers == 152: cardinality = 64 reduction_ratio = 16 @@ -262,9 +264,7 @@ class SeResNeXt(fluid.dygraph.Layer): stride=1, act='relu', ) - self.pool = Pool2D( - pool_size=3, pool_stride=2, pool_padding=1, pool_type='max' - ) + self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1) self.bottleneck_block_list = [] num_channels = 64 @@ -287,8 +287,7 @@ class SeResNeXt(fluid.dygraph.Layer): num_channels = bottleneck_block._num_channels_out self.bottleneck_block_list.append(bottleneck_block) shortcut = True - - self.pool2d_avg = Pool2D( + self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D( pool_size=7, pool_type='avg', global_pooling=True ) import math @@ -297,14 +296,14 @@ class SeResNeXt(fluid.dygraph.Layer): self.pool2d_avg_output = num_filters[-1] * 4 * 1 * 1 - self.out = Linear( + self.out = paddle.nn.Linear( self.pool2d_avg_output, class_dim, - act='softmax', - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.Uniform(-stdv, stdv) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), ) + self.out_act = paddle.nn.Softmax() def forward(self, inputs): if self.layers == 50 or self.layers == 101: @@ -321,7 +320,7 @@ class SeResNeXt(fluid.dygraph.Layer): y = self.pool2d_avg(y) y = paddle.reshape(y, shape=[-1, self.pool2d_avg_output]) y = self.out(y) - return y + return self.out_act(y) class TestImperativeResneXt(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/test_imperative_trace_non_persistable_inputs.py b/python/paddle/fluid/tests/unittests/test_imperative_trace_non_persistable_inputs.py index 72d987b0d4c5fdb0a8173de42e41faafa5f78e38..959ed8bbfec38c1ceb0c4d3c1cf1d4dc6e9d4cf2 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_trace_non_persistable_inputs.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_trace_non_persistable_inputs.py @@ -16,14 +16,15 @@ import os import unittest import numpy as np - +import os +import paddle import paddle.fluid as fluid class SimpleFCLayer(fluid.dygraph.Layer): def __init__(self, feature_size, batch_size, fc_size): super().__init__() - self._linear = fluid.dygraph.Linear(feature_size, fc_size) + self._linear = paddle.nn.Linear(feature_size, fc_size) self._offset = fluid.dygraph.to_variable( np.random.random((batch_size, fc_size)).astype('float32') ) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py b/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py index ccdf99f0f783dd685e240882a25268b67f5fae32..f5737956c3b600a05114f92b8a4844d6e1d27360 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py @@ -19,10 +19,14 @@ from test_imperative_base import new_program_scope import paddle import paddle.fluid as fluid -import paddle.nn.functional as F -from paddle.fluid import Embedding, Layer, LayerNorm, Linear, core -from paddle.fluid.dygraph import guard, to_variable +from paddle.fluid import Embedding, LayerNorm, Layer +from paddle.nn import Linear +from paddle.fluid.dygraph import to_variable, guard +from test_imperative_base import new_program_scope from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard +from paddle.fluid import core +import numpy as np +import paddle.nn.functional as F from paddle.jit import TracedLayer np.set_printoptions(suppress=True) @@ -428,12 +432,13 @@ class PrePostProcessLayer(Layer): class PositionwiseFeedForwardLayer(Layer): def __init__(self, d_inner_hid, d_hid, dropout_rate): super().__init__() - self._i2h = Linear(d_hid, d_inner_hid, act="relu") + self._i2h = Linear(d_hid, d_inner_hid) self._h2o = Linear(d_inner_hid, d_hid) self._dropout_rate = dropout_rate def forward(self, x): hidden = self._i2h(x) + hidden = paddle.nn.functional.relu(hidden) if self._dropout_rate: hidden = fluid.layers.dropout( hidden, diff --git a/python/paddle/fluid/tests/unittests/test_jit_save_load.py b/python/paddle/fluid/tests/unittests/test_jit_save_load.py index faf6a61df3f3902aba572f1098fbc6b4b8d92148..eac870650489549ba920d217919850f1dac23f87 100644 --- a/python/paddle/fluid/tests/unittests/test_jit_save_load.py +++ b/python/paddle/fluid/tests/unittests/test_jit_save_load.py @@ -23,8 +23,8 @@ import numpy as np import paddle import paddle.fluid as fluid +from paddle.nn import Linear from paddle.fluid import unique_name -from paddle.fluid.dygraph import Linear from paddle.fluid.dygraph.io import INFER_PARAMS_INFO_SUFFIX from paddle.fluid.layers.utils import flatten from paddle.jit.api import declarative diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index f3f5598f52c7330ceb511ad546fc451c7e317654..f08c0d1176cfd52870908c86232e4e36360ecf98 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -91,10 +91,12 @@ class TestLayer(LayerTest): class CustomLayer(fluid.Layer): def __init__(self, input_size, linear1_size=4): super().__init__() - self.linear1 = nn.Linear( + self.linear1 = paddle.nn.Linear( input_size, linear1_size, bias_attr=False ) - self.linear2 = nn.Linear(linear1_size, 1, bias_attr=False) + self.linear2 = paddle.nn.Linear( + linear1_size, 1, bias_attr=False + ) def forward(self, x, do_linear2=False): ret = self.linear1(x) @@ -172,7 +174,7 @@ class TestLayer(LayerTest): dtype='float32', append_batch_size=False, ) - linear = nn.Linear( + linear = paddle.nn.Linear( 32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1) ) ret = linear(t) @@ -182,7 +184,7 @@ class TestLayer(LayerTest): with self.dynamic_graph(): with _test_eager_guard(): t = base.to_variable(inp) - linear = nn.Linear( + linear = paddle.nn.Linear( 32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1), @@ -191,7 +193,7 @@ class TestLayer(LayerTest): dy_eager_ret_value = dy_eager_ret.numpy() t = base.to_variable(inp) - linear = nn.Linear( + linear = paddle.nn.Linear( 32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1) ) dy_ret = linear(t) @@ -205,7 +207,7 @@ class TestLayer(LayerTest): # the input of Linear must be Variable. def test_Variable(): inp = np.ones([3, 32, 32], dtype='float32') - linear = nn.Linear( + linear = paddle.nn.Linear( 32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1), @@ -218,7 +220,7 @@ class TestLayer(LayerTest): # float16 only can be set on GPU place def test_type(): inp = np.ones([3, 32, 32], dtype='int32') - linear = nn.Linear( + linear = paddle.nn.Linear( 32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1), @@ -261,7 +263,7 @@ class TestLayer(LayerTest): # the input of Linear must be Variable. def test_Variable(): inp = np.ones([3, 32, 32], dtype='float32') - linear = nn.Linear( + linear = paddle.nn.Linear( 32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1), @@ -274,7 +276,7 @@ class TestLayer(LayerTest): # float16 only can be set on GPU place def test_type(): inp = np.ones([3, 32, 32], dtype='int32') - linear = nn.Linear( + linear = paddle.nn.Linear( 32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1), @@ -1703,7 +1705,9 @@ class TestLayer(LayerTest): images = layers.data( name='pixel', shape=[3, 6, 6, 6], dtype='float32' ) - conv3d = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2) + conv3d = paddle.nn.Conv3D( + in_channels=3, out_channels=3, kernel_size=2 + ) ret = conv3d(images) static_ret2 = self.get_static_graph_result( feed={'pixel': np.ones([2, 3, 6, 6, 6], dtype='float32')}, @@ -1713,12 +1717,16 @@ class TestLayer(LayerTest): with self.dynamic_graph(): with _test_eager_guard(): images = np.ones([2, 3, 6, 6, 6], dtype='float32') - conv3d = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2) + conv3d = paddle.nn.Conv3D( + in_channels=3, out_channels=3, kernel_size=2 + ) dy_eager_ret = conv3d(base.to_variable(images)) dy_eager_rlt_value = dy_eager_ret.numpy() images = np.ones([2, 3, 6, 6, 6], dtype='float32') - conv3d = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2) + conv3d = paddle.nn.Conv3D( + in_channels=3, out_channels=3, kernel_size=2 + ) dy_ret = conv3d(base.to_variable(images)) dy_rlt_value = dy_ret.numpy() @@ -1735,14 +1743,14 @@ class TestLayer(LayerTest): custom_weight ) ) - conv3d1 = nn.Conv3D( - num_channels=3, num_filters=3, filter_size=2 + conv3d1 = paddle.nn.Conv3D( + in_channels=3, out_channels=3, kernel_size=2 ) - conv3d2 = nn.Conv3D( - num_channels=3, - num_filters=3, - filter_size=2, - param_attr=weight_attr, + conv3d2 = paddle.nn.Conv3D( + in_channels=3, + out_channels=3, + kernel_size=2, + weight_attr=weight_attr, ) dy_ret1 = conv3d1(base.to_variable(images)) dy_ret2 = conv3d2(base.to_variable(images)) @@ -1780,12 +1788,14 @@ class TestLayer(LayerTest): custom_weight ) ) - conv3d1 = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2) - conv3d2 = nn.Conv3D( - num_channels=3, - num_filters=3, - filter_size=2, - param_attr=weight_attr, + conv3d1 = paddle.nn.Conv3D( + in_channels=3, out_channels=3, kernel_size=2 + ) + conv3d2 = paddle.nn.Conv3D( + in_channels=3, + out_channels=3, + kernel_size=2, + weight_attr=weight_attr, ) dy_ret1 = conv3d1(base.to_variable(images)) dy_ret2 = conv3d2(base.to_variable(images)) @@ -2277,15 +2287,15 @@ class TestLayer(LayerTest): with self.static_graph(): img = layers.data(name='pixel', shape=[3, 2, 2, 2], dtype='float32') out = paddle.static.nn.conv3d_transpose( - input=img, num_filters=12, filter_size=12, use_cudnn=False + input=img, num_filters=12, filter_size=12, use_cudnn=True ) static_rlt = self.get_static_graph_result( feed={'pixel': input_array}, fetch_list=[out] )[0] with self.static_graph(): img = layers.data(name='pixel', shape=[3, 2, 2, 2], dtype='float32') - conv3d_transpose = nn.Conv3DTranspose( - num_channels=3, num_filters=12, filter_size=12, use_cudnn=False + conv3d_transpose = paddle.nn.Conv3DTranspose( + in_channels=3, out_channels=12, kernel_size=12 ) out = conv3d_transpose(img) static_rlt2 = self.get_static_graph_result( @@ -2293,17 +2303,16 @@ class TestLayer(LayerTest): )[0] with self.dynamic_graph(): with _test_eager_guard(): - conv3d_transpose = nn.Conv3DTranspose( - num_channels=3, - num_filters=12, - filter_size=12, - use_cudnn=False, + conv3d_transpose = paddle.nn.Conv3DTranspose( + in_channels=3, + out_channels=12, + kernel_size=12, ) dy_eager_rlt = conv3d_transpose(base.to_variable(input_array)) dy_eager_rlt_value = dy_eager_rlt.numpy() - conv3d_transpose = nn.Conv3DTranspose( - num_channels=3, num_filters=12, filter_size=12, use_cudnn=False + conv3d_transpose = paddle.nn.Conv3DTranspose( + in_channels=3, out_channels=12, kernel_size=12 ) dy_rlt = conv3d_transpose(base.to_variable(input_array)) dy_rlt_value = dy_rlt.numpy() @@ -2320,20 +2329,18 @@ class TestLayer(LayerTest): custom_weight ) ) - conv3d1 = nn.Conv3DTranspose( - num_channels=3, - num_filters=3, - filter_size=2, + conv3d1 = paddle.nn.Conv3DTranspose( + in_channels=3, + out_channels=3, + kernel_size=2, bias_attr='eager_conv3d1_b', - use_cudnn=False, ) - conv3d2 = nn.Conv3DTranspose( - num_channels=3, - num_filters=3, - filter_size=2, - param_attr=weight_attr, + conv3d2 = paddle.nn.Conv3DTranspose( + in_channels=3, + out_channels=3, + kernel_size=2, + weight_attr=weight_attr, bias_attr='eager_conv3d2_b', - use_cudnn=False, ) dy_ret1 = conv3d1(base.to_variable(images)) dy_ret2 = conv3d2(base.to_variable(images)) @@ -2371,20 +2378,18 @@ class TestLayer(LayerTest): custom_weight ) ) - conv3d1 = nn.Conv3DTranspose( - num_channels=3, - num_filters=3, - filter_size=2, + conv3d1 = paddle.nn.Conv3DTranspose( + in_channels=3, + out_channels=3, + kernel_size=2, bias_attr='conv3d1_b', - use_cudnn=False, ) - conv3d2 = nn.Conv3DTranspose( - num_channels=3, - num_filters=3, - filter_size=2, - param_attr=weight_attr, + conv3d2 = paddle.nn.Conv3DTranspose( + in_channels=3, + out_channels=3, + kernel_size=2, + weight_attr=weight_attr, bias_attr='conv3d2_b', - use_cudnn=False, ) dy_ret1 = conv3d1(base.to_variable(images)) dy_ret2 = conv3d2(base.to_variable(images)) diff --git a/python/paddle/fluid/tests/unittests/test_learning_rate_scheduler.py b/python/paddle/fluid/tests/unittests/test_learning_rate_scheduler.py index ffe45f3ec312e97df979eea44660bacb4093d429..dd37c2bff66a7a67cb27094ea0641ef465ae8be0 100644 --- a/python/paddle/fluid/tests/unittests/test_learning_rate_scheduler.py +++ b/python/paddle/fluid/tests/unittests/test_learning_rate_scheduler.py @@ -16,7 +16,7 @@ import copy import math import numpy as np import unittest - +import paddle import paddle.fluid as fluid import paddle.fluid.layers as layers import paddle.fluid.framework as framework @@ -120,7 +120,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase): def test_LR_state_dict(self): with fluid.dygraph.guard(): x = np.random.uniform(-1, 1, [3, 10]).astype("float32") - linear = fluid.dygraph.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) input = fluid.dygraph.to_variable(x) Exponential_scheduler = fluid.dygraph.ExponentialDecay( @@ -291,7 +291,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase): learning_rate = 0.5 milestones = [2, 4, 8] decay_rate = 0.2 - linear = fluid.dygraph.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) scheduler = fluid.dygraph.MultiStepDecay( learning_rate, milestones, decay_rate @@ -364,7 +364,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase): lr_lambda = lambda x: 0.95**x scheduler = fluid.dygraph.LambdaDecay(learning_rate, lr_lambda) - linear = fluid.dygraph.nn.Linear(10, 10) + linear = paddle.nn.Linear(10, 10) adam = fluid.optimizer.Adam( scheduler, parameter_list=linear.parameters() ) diff --git a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py index b2b0d32d72a2caf1a686b734eda00e758815e2d1..2ea1b4faf8d24a2e5efd2b2a6276bf638972aa9c 100644 --- a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py +++ b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py @@ -15,6 +15,9 @@ import sys import time import unittest +import numpy as np +import paddle +from paddle.nn import Linear import numpy as np from test_multiprocess_dataloader_static import ( @@ -29,7 +32,6 @@ from test_multiprocess_dataloader_static import ( ) import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Linear from paddle.io import DataLoader @@ -37,11 +39,11 @@ class SimpleFCNet(fluid.dygraph.Layer): def __init__(self): super().__init__() - param_attr = fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.8) + param_attr = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.8) ) - bias_attr = fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ) self._fcs = [] in_channel = IMAGE_SIZE @@ -50,21 +52,21 @@ class SimpleFCNet(fluid.dygraph.Layer): Linear( in_channel, hidden_size, - act='tanh', - param_attr=param_attr, + weight_attr=param_attr, bias_attr=bias_attr, ) ) + self._fcs.append(paddle.nn.Tanh()) in_channel = hidden_size self._fcs.append( Linear( in_channel, CLASS_NUM, - act='softmax', - param_attr=param_attr, + weight_attr=param_attr, bias_attr=bias_attr, ) ) + self._fcs.append(paddle.nn.Softmax()) def forward(self, image): out = image diff --git a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py index 1ce77249ea5787773a05ce0701b1dc5f78f559bb..1f15241b26c6a03a993b75bb3ad7b3c70d0f4bbd 100644 --- a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py +++ b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py @@ -15,6 +15,10 @@ import sys import time import unittest +import numpy as np + +import paddle +from paddle.nn import Linear import numpy as np from test_multiprocess_dataloader_iterable_dataset_static import ( @@ -29,7 +33,6 @@ from test_multiprocess_dataloader_iterable_dataset_static import ( ) import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Linear from paddle.io import DataLoader @@ -37,11 +40,11 @@ class SimpleFCNet(fluid.dygraph.Layer): def __init__(self): super().__init__() - param_attr = fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.8) + param_attr = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.8) ) - bias_attr = fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr = paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ) self._fcs = [] in_channel = IMAGE_SIZE @@ -50,21 +53,22 @@ class SimpleFCNet(fluid.dygraph.Layer): Linear( in_channel, hidden_size, - act='tanh', - param_attr=param_attr, + weight_attr=param_attr, bias_attr=bias_attr, ) ) + self._fcs.append(paddle.nn.Tanh()) + in_channel = hidden_size self._fcs.append( Linear( in_channel, CLASS_NUM, - act='softmax', - param_attr=param_attr, + weight_attr=param_attr, bias_attr=bias_attr, ) ) + self._fcs.append(paddle.nn.Softmax()) def forward(self, image): out = image diff --git a/python/paddle/fluid/tests/unittests/test_optimizer_in_control_flow.py b/python/paddle/fluid/tests/unittests/test_optimizer_in_control_flow.py index de9c02f776835db5165bd9317d92b4c38669dd3e..3294b6f37067ce5baa47701d38f9d44f13961654 100644 --- a/python/paddle/fluid/tests/unittests/test_optimizer_in_control_flow.py +++ b/python/paddle/fluid/tests/unittests/test_optimizer_in_control_flow.py @@ -138,34 +138,34 @@ def static( class DygraphLayer(fluid.dygraph.Layer): def __init__(self): super().__init__() - self.fc_1 = fluid.dygraph.nn.Linear( + self.fc_1 = paddle.nn.Linear( INPUT_SIZE, FC_SIZE, - act='relu', - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.99) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.5) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.5) ), ) - - self.fc_2 = fluid.dygraph.nn.Linear( + self.act_1 = paddle.nn.ReLU() + self.fc_2 = paddle.nn.Linear( FC_SIZE, CLASS_NUM, - act='softmax', - param_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=1.2) + weight_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=1.2) ), - bias_attr=fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.8) + bias_attr=paddle.ParamAttr( + initializer=paddle.nn.initializer.Constant(value=0.8) ), ) + self.act_2 = paddle.nn.Softmax() + def forward(self, inputs): hidden = self.fc_1(inputs) prediction = self.fc_2(hidden) - return hidden, prediction + return self.act_1(hidden), self.act_2(prediction) def dynamic(train_data, use_cuda=False, use_parallel_exe=False): diff --git a/python/paddle/fluid/tests/unittests/test_pool2d_op.py b/python/paddle/fluid/tests/unittests/test_pool2d_op.py index fb802b60b872740eeb0cc214785d28e0422ab98a..5285b7767a35a5c8a385b1494f73c28d4fd0b834 100644 --- a/python/paddle/fluid/tests/unittests/test_pool2d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pool2d_op.py @@ -18,7 +18,6 @@ import numpy as np import paddle.fluid as fluid import paddle.fluid.core as core -from paddle.fluid import Program, program_guard from paddle.fluid.tests.unittests.op_test import OpTest @@ -1460,112 +1459,5 @@ class TestPool2DAPI_Error(unittest.TestCase): self.assertRaises(ValueError, run_5) -class TestDygraphPool2DAPIError(unittest.TestCase): - def test_errors(self): - with program_guard(Program(), Program()): - # the input of Pool2D must be Variable. - data1 = np.random.random((3, 32, 32, 5)).astype('float32') - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='max', - pool_stride=1, - global_pooling=False, - ) - self.assertRaises(TypeError, pool2d, data1) - - # the input dtype of Pool2D must be uint8 or int8 or float16 or float32 or float64 - # uint8 and int8 only can be set on mkldnn - # float16 only can be set on GPU place - data2 = fluid.layers.data( - name='x1', shape=[3, 32, 32, 5], dtype="int32" - ) - self.assertRaises(TypeError, pool2d, data2) - - def test_data_format_error(self): - with program_guard(Program(), Program()): - # the data_format must be 'NCHW' or 'NHWC' - data1 = np.random.random((3, 32, 32, 5)).astype('float32') - self.assertRaises( - ValueError, - fluid.dygraph.Pool2D, - pool_size=2, - pool_type='max', - pool_stride=1, - global_pooling=False, - data_format='NWHC', - ) - - -class TestDygraphPool2DAPI(unittest.TestCase): - def test_nhwc(self): - with fluid.dygraph.guard(): - data = np.random.random((3, 32, 32, 5)).astype('float32') - x = fluid.dygraph.to_variable(data) - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='max', - pool_stride=1, - pool_padding=[0, 0], - global_pooling=False, - data_format='NHWC', - ) - out1 = pool2d(x) - out2 = pool2D_forward_naive( - data, - [2, 2], - [1, 1], - paddings=[0, 0], - pool_type='max', - data_format='NHWC', - ) - np.testing.assert_allclose(out1.numpy(), out2, rtol=1e-05) - - def test_lower_case(self): - with fluid.dygraph.guard(): - data = np.random.random((3, 32, 32, 5)).astype('float32') - x = fluid.dygraph.to_variable(data) - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='max', - pool_stride=1, - pool_padding=[0, 0], - global_pooling=False, - data_format='nhwc', - ) - out1 = pool2d(x) - out2 = pool2D_forward_naive( - data, - [2, 2], - [1, 1], - paddings=[0, 0], - pool_type='max', - data_format='NHWC', - ) - np.testing.assert_allclose(out1.numpy(), out2, rtol=1e-05) - - def test_upper_case(self): - with fluid.dygraph.guard(): - data = np.random.random((3, 32, 32, 5)).astype('float32') - x = fluid.dygraph.to_variable(data) - pool2d = fluid.dygraph.Pool2D( - pool_size=2, - pool_type='MAX', - pool_stride=1, - pool_padding=[0, 0], - global_pooling=False, - data_format='nhwc', - ) - out1 = pool2d(x) - out2 = pool2D_forward_naive( - data, - [2, 2], - [1, 1], - paddings=[0, 0], - pool_type='max', - data_format='NHWC', - ) - np.testing.assert_allclose(out1.numpy(), out2, rtol=1e-05) - - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_regularizer.py b/python/paddle/fluid/tests/unittests/test_regularizer.py index ba81625a04d0859a98de520551d32cc625575940..f162a8e829fe8164ddcf0b136400371f88e9d9f7 100644 --- a/python/paddle/fluid/tests/unittests/test_regularizer.py +++ b/python/paddle/fluid/tests/unittests/test_regularizer.py @@ -259,7 +259,9 @@ class TestRegularizer(unittest.TestCase): def test_repeated_regularization(self): l1 = fluid.regularizer.L1Decay(regularization_coeff=0.1) l2 = fluid.regularizer.L2Decay(regularization_coeff=0.01) - fc_param_attr = fluid.ParamAttr(regularizer=l1) + fc_param_attr = paddle.ParamAttr( + regularizer=paddle.regularizer.L1Decay() + ) with fluid.program_guard(fluid.Program(), fluid.Program()): x = fluid.layers.uniform_random([2, 2, 3]) out = fluid.layers.fc(x, 5, param_attr=fc_param_attr) @@ -273,11 +275,11 @@ class TestRegularizer(unittest.TestCase): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - linear1 = fluid.dygraph.Linear( - 2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr + linear1 = paddle.nn.Linear( + 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr ) - linear2 = fluid.dygraph.Linear( - 2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr + linear2 = paddle.nn.Linear( + 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr ) loss1 = linear1(input) diff --git a/python/paddle/fluid/tests/unittests/test_regularizer_api.py b/python/paddle/fluid/tests/unittests/test_regularizer_api.py index aee1e8c25eee6a9bd0d7a162e20698138a6e78fc..c3adc0cf0b359165cd5528b02bbab4232d52b16b 100644 --- a/python/paddle/fluid/tests/unittests/test_regularizer_api.py +++ b/python/paddle/fluid/tests/unittests/test_regularizer_api.py @@ -169,7 +169,9 @@ class TestRegularizer(unittest.TestCase): paddle.enable_static() l1 = paddle.regularizer.L1Decay(0.1) l2 = paddle.regularizer.L2Decay(0.01) - fc_param_attr = fluid.ParamAttr(regularizer=l1) + fc_param_attr = paddle.ParamAttr( + regularizer=paddle.regularizer.L1Decay() + ) with fluid.program_guard(fluid.Program(), fluid.Program()): x = fluid.layers.uniform_random([2, 2, 3]) out = fluid.layers.fc(x, 5, param_attr=fc_param_attr) @@ -183,11 +185,11 @@ class TestRegularizer(unittest.TestCase): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - linear1 = fluid.dygraph.Linear( - 2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr + linear1 = paddle.nn.Linear( + 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr ) - linear2 = fluid.dygraph.Linear( - 2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr + linear2 = paddle.nn.Linear( + 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr ) loss1 = linear1(input) diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index e4db05ecc34231f4912413ef7c05fff685436bfb..439ff3d2b822264467aeec547c0d6ef856f43ab3 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -354,7 +354,7 @@ class TestVarBase(unittest.TestCase): var = fluid.dygraph.to_variable("test", name="abc") # test to_variable of LayerObjectHelper(LayerHelperBase) with self.assertRaises(TypeError): - linear = fluid.dygraph.Linear(32, 64) + linear = paddle.nn.Linear(32, 64) var = linear._helper.to_variable("test", name="abc") def test_to_variable(self): @@ -1170,13 +1170,13 @@ class TestVarBase(unittest.TestCase): self._assert_to_static(var_base, static_param, True) # Convert ParamBase into Parameter - fc = fluid.dygraph.Linear( + fc = paddle.nn.Linear( 10, 20, - param_attr=fluid.ParamAttr( + weight_attr=paddle.ParamAttr( learning_rate=0.001, do_model_average=True, - regularizer=fluid.regularizer.L1Decay(), + regularizer=paddle.regularizer.L1Decay(), ), ) weight = fc.parameters()[0]