示例#1
0
 def test_errors(self):
     with program_guard(Program(), Program()):
         # The input type of mul_op must be Variable.
         x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]],
                                      fluid.XPUPlace(0))
         x2 = fluid.create_lod_tensor(np.array([[-1]]), [[1]],
                                      fluid.XPUPlace(0))
         self.assertRaises(TypeError, fluid.layers.mul, x1, x2)
         # The input dtype of mul_op must be float32.
         x3 = fluid.layers.data(name='x3', shape=[4], dtype="int32")
         x4 = fluid.layers.data(name='x4', shape=[4], dtype="int32")
         self.assertRaises(TypeError, fluid.layers.mul, x3, x4)
    def test_errors(self):
        with program_guard(Program(), Program()):
            # the input of elementwise_mul must be Variable.
            x1 = fluid.create_lod_tensor(
                np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.XPUPlace(0))
            y1 = fluid.create_lod_tensor(
                np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.XPUPlace(0))
            self.assertRaises(TypeError, fluid.layers.elementwise_mul, x1, y1)

            # the input dtype of elementwise_mul must be float32
            x2 = fluid.layers.data(name='x2', shape=[3, 4, 5, 6], dtype="uint8")
            y2 = fluid.layers.data(name='y2', shape=[3, 4, 5, 6], dtype="uint8")
            self.assertRaises(TypeError, fluid.layers.elementwise_mul, x2, y2)
 def run_trainer(self, args):
     train_prog = fluid.Program()
     startup_prog = fluid.Program()
     endpoints = args["endpoints"].split(",")
     rank = args["trainerid"]
     current_endpoint = args["currentendpoint"]
     nranks = 2
     paddle.distributed.init_parallel_env()
     if args['backend'] == 'nccl':
         device_id = int(os.getenv("FLAGS_selected_gpus", "0"))
         place = fluid.CUDAPlace(
             device_id)  #if args.use_gpu else fluid.CPUPlace()
     elif args['backend'] == 'bkcl':
         device_id = int(os.getenv("FLAGS_selected_xpus", "0"))
         place = fluid.XPUPlace(device_id)
     else:
         place = fluid.CPUPlace()
     np.random.seed(os.getpid())
     indata = np.random.random((10, 1000)).astype("float32")
     if args['static_mode']:
         result = self.get_model(train_prog, startup_prog, rank)
         exe = fluid.Executor(place)
         exe.run(startup_prog)
         fetch_list = []
         for elem in result:
             fetch_list.append(elem.name)
         out = exe.run(train_prog,
                       feed={'tindata': indata},
                       fetch_list=fetch_list)
     else:
         out = self.get_model(train_prog, startup_prog, rank, indata)
         #print(out, sys.stderr)
     sys.stdout.buffer.write(pickle.dumps(out))
示例#4
0
 def setUp(self):
     self.op_type = op_type
     self.input_x = np.array([1, 2, 3, 4]).astype(np.int64)
     self.input_y = np.array([1, 3, 2, 4]).astype(np.int64)
     self.real_result = callback(self.input_x, self.input_y)
     self.place = fluid.XPUPlace(
         0) if fluid.core.is_compiled_with_xpu() else fluid.CPUPlace()
    def loss_scaling_check(self, scope=fluid.Scope()):
        a = fluid.data(name="a", shape=[1024, 1024], dtype='float32')
        b = fluid.data(name="b", shape=[512, 128], dtype='float32')
        x = [a, b]
        found_inf = fluid.data(name="found_inf", shape=[1], dtype='bool')
        prev_loss_scaling = fluid.data(
            name="prev_loss_scaling", shape=[1], dtype='float32')
        num_good_steps = fluid.data(
            name="num_good_steps", shape=[1], dtype='int32')
        num_bad_steps = fluid.data(
            name="num_bad_steps", shape=[1], dtype='int32')

        a_v = np.random.random([1024, 1024]).astype('float32')
        b_v = np.random.random([512, 128]).astype('float32')
        found_inf_v = np.array([False]).astype('bool')
        prev_loss_scaling_v = np.array([2048]).astype('float32')
        num_good_steps_v = np.array([999], dtype=np.int32)
        num_bad_steps_v = np.array([1], dtype=np.int32)

        incr_every_n_steps = 1000
        decr_every_n_nan_or_inf = 2
        incr_ratio = 2
        decr_ratio = 0.8

        result = amp_nn.update_loss_scaling(
            x,
            found_inf,
            prev_loss_scaling,
            num_good_steps,
            num_bad_steps,
            incr_every_n_steps,
            decr_every_n_nan_or_inf,
            incr_ratio,
            decr_ratio,
            name="update_loss_scaling")

        place = fluid.XPUPlace(0)
        exe = fluid.Executor(place)
        with fluid.scope_guard(scope):
            exe.run(fluid.default_startup_program())
            result_v = exe.run(feed={
                'a': a_v,
                'b': b_v,
                'found_inf': found_inf_v,
                'prev_loss_scaling': prev_loss_scaling_v,
                'num_good_steps': num_good_steps_v,
                'num_bad_steps': num_bad_steps_v
            },
                               fetch_list=[
                                   result, x, found_inf, prev_loss_scaling,
                                   num_good_steps, num_bad_steps
                               ])
        assert np.array_equal(result_v[0], a_v)
        assert np.array_equal(result_v[1], b_v)
        assert np.array_equal(result_v[0], result_v[2])
        assert np.array_equal(result_v[1], result_v[3])
        assert np.array_equal(result_v[4], found_inf_v)
        assert np.array_equal(result_v[5], prev_loss_scaling_v * incr_ratio)
        assert np.array_equal(result_v[6], np.zeros_like(num_good_steps_v))
        assert np.array_equal(result_v[7], np.zeros_like(num_bad_steps_v))
示例#6
0
 def test_errors(self):
     with program_guard(Program(), Program()):
         # The type of input must be Variable or numpy.ndarray.
         x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]],
                                      fluid.XPUPlace(0))
         self.assertRaises(TypeError, fluid.layers.assign, x1)
         x2 = np.array([[2.5, 2.5]], dtype='uint8')
         self.assertRaises(TypeError, fluid.layers.assign, x2)
示例#7
0
    def run_trainer(self, args):
        train_prog = fluid.Program()
        startup_prog = fluid.Program()
        endpoints = args["endpoints"].split(",")
        rank = args["trainerid"]
        current_endpoint = args["currentendpoint"]
        nranks = 2
        paddle.distributed.init_parallel_env()
        if args['backend'] == 'nccl':
            device_id = int(os.getenv("FLAGS_selected_gpus", "0"))
            place = fluid.CUDAPlace(
                device_id)  #if args.use_gpu else fluid.CPUPlace()
        elif args['backend'] == 'bkcl':
            device_id = int(os.getenv("FLAGS_selected_xpus", "0"))
            place = fluid.XPUPlace(device_id)
        else:
            place = fluid.CPUPlace()

        in_feat = 2
        n_expert = 2
        world_size = 2
        tot_expert = n_expert * world_size
        paddle.disable_static()

        # Call paddle.distributed.alltoall() under legacy dygraph
        _enable_legacy_dygraph()
        np.random.seed(os.getpid())
        local_expert_count = np.random.randint(1, 4,
                                               size=tot_expert).astype("int64")
        local_expert_count = paddle.to_tensor(local_expert_count)
        global_expert_count = []
        paddle.distributed.alltoall(
            paddle.split(local_expert_count, 2, axis=0), global_expert_count)
        global_expert_count = paddle.concat(global_expert_count, axis=0)
        global_expert_count = global_expert_count.numpy()
        local_expert_count = local_expert_count.numpy()
        fwd_expert_count = sum(global_expert_count)
        np.random.seed(os.getpid())
        local_input_buf = np.random.rand(fwd_expert_count,
                                         in_feat).astype("float32")

        paddle.enable_static()
        if args['static_mode']:
            result = self.get_model(train_prog, startup_prog, rank)
            exe = fluid.Executor(place)
            exe.run(startup_prog)
            fetch_list = []
            for elem in result:
                fetch_list.append(elem.name)
            out = exe.run(train_prog,
                          feed={
                              'local_expert_count': local_expert_count,
                              'global_expert_count': global_expert_count,
                              'local_input_buf': local_input_buf
                          },
                          fetch_list=fetch_list)

        sys.stdout.buffer.write(pickle.dumps(out))
 def test_dygraph_without_out(self):
     device = fluid.XPUPlace(0)
     with fluid.dygraph.guard(device):
         input_array1 = np.random.rand(3, 4).astype("float64")
         input_array2 = np.random.rand(4, 3).astype("float64")
         data1 = fluid.dygraph.to_variable(input_array1)
         data2 = fluid.dygraph.to_variable(input_array2)
         out = paddle.matmul(data1, data2)
         expected_result = np.matmul(input_array1, input_array2)
     self.assertTrue(np.allclose(expected_result, out.numpy()))
示例#9
0
def get_place(target):
    if target == "cuda":
        return fluid.CUDAPlace(0)
    elif target == "xpu":
        return fluid.XPUPlace(0)
    elif target == "cpu":
        return fluid.CPUPlace()
    else:
        raise ValueError(
            "Target `{0}` is not on the support list: `cuda`, `xpu` and `cpu`."
            .format(target))
示例#10
0
 def test_shape_with_batch_sizes(self):
     with fluid.program_guard(fluid.Program()):
         x_var = fluid.data(
             name='x', dtype='float32', shape=[None, 3, None, None])
         one = 2.
         out = one / x_var
         exe = fluid.Executor(fluid.XPUPlace(0))
         x = np.random.uniform(0.1, 0.6,
                               (1, 3, 32, 32)).astype('float32')
         out_result, = exe.run(feed={'x': x}, fetch_list=[out])
         self.assertEqual((out_result == (2 / x)).all(), True)
示例#11
0
    def test_load_xpu(self):
        main_prog = fluid.Program()
        start_prog = fluid.Program()
        with fluid.program_guard(main_prog, start_prog):
            var = layers.create_tensor(dtype='float32')
            layers.load(var, file_path='./model/w')

        exe = fluid.Executor(fluid.XPUPlace(0))
        exe.run(start_prog)
        ret = exe.run(main_prog, fetch_list=[var.name])
        self.assertTrue(np.array_equal(self.ones, ret[0]))
示例#12
0
 def test_errors(self):
     with program_guard(Program(), Program()):
         # The type of input must be Variable or numpy.ndarray.
         x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]],
                                      fluid.XPUPlace(0))
         self.assertRaises(TypeError, fluid.layers.assign, x1)
         # When the type of input is Variable, the dtype of input must be float16, float32, float64, int32, int64, bool.
         x3 = fluid.layers.data(name='x3', shape=[4], dtype="uint8")
         self.assertRaises(TypeError, fluid.layers.assign, x3)
         # When the type of input is numpy.ndarray, the dtype of input must be float32, int32.
         x4 = np.array([[2.5, 2.5]], dtype='float64')
         self.assertRaises(TypeError, fluid.layers.assign, x4)
         x5 = np.array([[2.5, 2.5]], dtype='uint8')
         self.assertRaises(TypeError, fluid.layers.assign, x5)
 def _get_executor(self):
     if self.role_maker._is_heter_worker():
         if self.role_maker._get_heter_worker_device() == "GPU":
             gpu_id = int(os.getenv("FLAGS_selected_gpus", "0"))
             executor = Executor(fluid.CUDAPlace(gpu_id))
         elif self.role_maker._get_heter_worker_device() == "XPU":
             xpu_id = int(os.getenv("FLAGS_selected_xpus", "0"))
             executor = Executor(fluid.XPUPlace(xpu_id))
         else:
             raise ValueError("Not Support Device {}".format(
                 self.role_maker._get_heter_worker_device()))
     else:
         executor = fluid.Executor(fluid.CPUPlace())
     return executor
示例#14
0
    def test_errors(self):
        with program_guard(Program(), Program()):
            # The input type of cast_op must be Variable.
            x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]],
                                         fluid.XPUPlace(0))
            self.assertRaises(TypeError, fluid.layers.cast, x1, 'int32')
            # The input dtype of cast_op must be float32, int32, int64.
            x2 = fluid.layers.data(name='x2', shape=[4], dtype='int16')
            self.assertRaises(TypeError, fluid.layers.cast, x2, 'int32')

            def test_dtype_type():
                x4 = fluid.layers.data(name='x4', shape=[4], dtype='int32')
                output = fluid.layers.cast(x=x4, dtype='int16')

            self.assertRaises(TypeError, test_dtype_type)
    def _run(self, depth):
        label = fluid.layers.data(name="label", shape=[1], dtype="int64")
        one_hot_label = fluid.one_hot(input=label, depth=depth)

        place = fluid.XPUPlace(0)
        label_data = np.array([np.random.randint(0, 10 - 1)
                               for i in range(6)]).reshape([6, 1])

        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())
        ret = exe.run(feed={
            'label': label_data,
        },
                      fetch_list=[one_hot_label],
                      return_numpy=False)
示例#16
0
 def setUp(self):
     self.ones = np.ones((4, 4)).astype('float32')
     main_prog = fluid.Program()
     start_prog = fluid.Program()
     with fluid.program_guard(main_prog, start_prog):
         input = fluid.data('input', shape=[-1, 4], dtype='float32')
         output = layers.fc(
             input,
             4,
             param_attr=fluid.ParamAttr(
                 name='w',
                 initializer=fluid.initializer.NumpyArrayInitializer(
                     self.ones)))
     exe = fluid.Executor(fluid.XPUPlace(0))
     exe.run(start_prog)
     fluid.io.save_persistables(exe,
                                dirname="./model",
                                main_program=main_prog)
    def test_declarative(self):
        with fluid.program_guard(fluid.Program()):

            def gen_data():
                return {
                    "x": np.array([2, 3, 4]).astype('float32'),
                    "y": np.array([1, 5, 2]).astype('float32')
                }

            x = fluid.data(name="x", shape=[3], dtype='float32')
            y = fluid.data(name="y", shape=[3], dtype='float32')
            z = paddle.add(x, y)

            place = fluid.XPUPlace(0)
            exe = fluid.Executor(place)
            z_value = exe.run(feed=gen_data(), fetch_list=[z.name])
            z_expected = np.array([3., 8., 6.])
            self.assertEqual((z_value == z_expected).all(), True)
示例#18
0
 def _get_executor(self):
     executor = fluid.Executor(fluid.CPUPlace())
     if self.role_maker._is_heter_parameter_server_mode:
         heter_worker_device_guard = self.context[
             "valid_strategy"].a_sync_configs[
                 "heter_worker_device_guard"].upper()
         if heter_worker_device_guard not in ["GPU", "XPU", "CPU"]:
             raise ValueError("Heter Worker Not Support Device {}".format(
                 heter_worker_device_guard))
         if self.role_maker._is_heter_worker():
             if heter_worker_device_guard == "GPU":
                 executor = Executor(
                     fluid.CUDAPlace(
                         int(os.getenv("FLAGS_selected_gpus", "0"))))
             elif heter_worker_device_guard == "XPU":
                 executor = Executor(
                     fluid.XPUPlace(
                         int(os.getenv("FLAGS_selected_xpus", "0"))))
     return executor
示例#19
0
    def test_clip_dygraph(self):
        paddle.disable_static()
        place = fluid.XPUPlace(
            0) if fluid.core.is_compiled_with_xpu() else fluid.CPUPlace()
        paddle.disable_static(place)
        data_shape = [1, 9, 9, 4]
        data = np.random.random(data_shape).astype('float32')
        images = paddle.to_tensor(data, dtype='float32')
        v_min = paddle.to_tensor(np.array([0.2], dtype=np.float32))
        v_max = paddle.to_tensor(np.array([0.8], dtype=np.float32))

        out_1 = self._executed_api(images, min=0.2, max=0.8)
        images = paddle.to_tensor(data, dtype='float32')
        out_2 = self._executed_api(images, min=0.2, max=0.9)
        images = paddle.to_tensor(data, dtype='float32')
        out_3 = self._executed_api(images, min=v_min, max=v_max)

        self.assertTrue(np.allclose(out_1.numpy(), data.clip(0.2, 0.8)))
        self.assertTrue(np.allclose(out_2.numpy(), data.clip(0.2, 0.9)))
        self.assertTrue(np.allclose(out_3.numpy(), data.clip(0.2, 0.8)))
    def test_out(self):
        with fluid.program_guard(fluid.Program()):
            x = fluid.data(name="x", shape=[2], dtype="float64")
            y = fluid.data(name='y', shape=[2], dtype='float64')
            res = fluid.data(name="output", shape=[1], dtype="float64")
            result = paddle.mm(x, y)
            exe = fluid.Executor(fluid.XPUPlace(0))
            data1 = np.random.rand(2)
            data2 = np.random.rand(2)
            np_res = exe.run(feed={
                'x': data1,
                'y': data2
            },
                             fetch_list=[result])
            expected_result = np.matmul(data1.reshape(1, 2),
                                        data2.reshape(2, 1))

        self.assertTrue(
            np.allclose(np_res, expected_result, atol=1e-5), "two value is\
            {}\n{}, check diff!".format(np_res, expected_result))
示例#21
0
    def test_clip(self):
        paddle.enable_static()
        data_shape = [1, 9, 9, 4]
        data = np.random.random(data_shape).astype('float32')
        images = fluid.data(name='image', shape=data_shape, dtype='float32')
        min = fluid.data(name='min', shape=[1], dtype='float32')
        max = fluid.data(name='max', shape=[1], dtype='float32')

        place = fluid.XPUPlace(
            0) if fluid.core.is_compiled_with_xpu() else fluid.CPUPlace()
        exe = fluid.Executor(place)

        out_1 = self._executed_api(images, min=min, max=max)
        out_2 = self._executed_api(images, min=0.2, max=0.9)
        out_3 = self._executed_api(images, min=0.3)
        out_4 = self._executed_api(images, max=0.7)
        out_5 = self._executed_api(images, min=min)
        out_6 = self._executed_api(images, max=max)
        out_7 = self._executed_api(images, max=-1.)
        out_8 = self._executed_api(images)

        res1, res2, res3, res4, res5, res6, res7, res8 = exe.run(
            fluid.default_main_program(),
            feed={
                "image": data,
                "min": np.array([0.2]).astype('float32'),
                "max": np.array([0.8]).astype('float32')
            },
            fetch_list=[
                out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8
            ])

        self.assertTrue(np.allclose(res1, data.clip(0.2, 0.8)))
        self.assertTrue(np.allclose(res2, data.clip(0.2, 0.9)))
        self.assertTrue(np.allclose(res3, data.clip(min=0.3)))
        self.assertTrue(np.allclose(res4, data.clip(max=0.7)))
        self.assertTrue(np.allclose(res5, data.clip(min=0.2)))
        self.assertTrue(np.allclose(res6, data.clip(max=0.8)))
        self.assertTrue(np.allclose(res7, data.clip(max=-1)))
        self.assertTrue(np.allclose(res8, data))
        paddle.disable_static()
示例#22
0
    def check_pass_conflict(cls,
                            method,
                            use_device=DeviceType.CUDA,
                            feed_dict=None,
                            get_data_from_feeder=None,
                            use_reduce=False,
                            use_ir_memory_optimize=True,
                            enable_inplace=True,
                            fuse_elewise_add_act_ops=False,
                            fuse_all_optimizer_ops=False,
                            fuse_all_reduce_ops=False,
                            fuse_relu_depthwise_conv=False,
                            optimizer=fluid.optimizer.Adam,
                            use_fast_executor=True,
                            enable_sequential_execution=False):

        main = fluid.Program()
        startup = fluid.Program()
        with fluid.program_guard(main, startup):
            feed_dict, loss = cls.build_model(feed_dict, get_data_from_feeder,
                                              main, method, optimizer)

        place = fluid.CUDAPlace(
            0) if use_device == DeviceType.CUDA else fluid.XPUPlace(
                0) if use_device == DeviceType.XPU else fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(startup)

        build_strategy, exec_strategy = cls.set_strategy(
            enable_inplace, enable_sequential_execution, fuse_all_optimizer_ops,
            fuse_all_reduce_ops, fuse_elewise_add_act_ops,
            fuse_relu_depthwise_conv, use_fast_executor, use_ir_memory_optimize,
            use_reduce, use_device)

        binary = compiler.CompiledProgram(main).with_data_parallel(
            loss_name=loss.name,
            build_strategy=build_strategy,
            exec_strategy=exec_strategy)

        exe.run(binary, feed=feed_dict, fetch_list=[loss.name])
示例#23
0
def main():
    args = parser.parse_args()
    set_models(args.model_category)
    print_arguments(args)

    model_path = os.path.join(args.model_save_dir + '/' + args.model)

    if args.place == "cuda":
        place = fluid.CUDAPlace(0)
    elif args.place == "xsim":
        place = fluid.XSIMPlace()
    elif args.place == "xpu":
        place = fluid.XPUPlace()
    else:
        print("Unsurpported place!")
        exit()

    if (args.run_mode == "train"):
        train(args, model_path, place)
    elif (args.run_mode == "infer" or
            args.run_mode == "fused_infer"):
        infer(args, model_path, place)
示例#24
0
        def test_adamw_op(self):
            paddle.enable_static()
            place = fluid.XPUPlace(0)
            shape = [2, 3, 8, 8]
            exe = fluid.Executor(place)
            train_prog = fluid.Program()
            startup = fluid.Program()
            with fluid.program_guard(train_prog, startup):
                with fluid.unique_name.guard():
                    data = fluid.data(name="data", shape=shape)
                    conv = fluid.layers.conv2d(data, 8, 3)
                    loss = paddle.mean(conv)

                    beta1 = fluid.layers.create_global_var(
                        shape=[1],
                        value=0.85,
                        dtype=self.in_type_str,
                        persistable=True)
                    beta2 = fluid.layers.create_global_var(
                        shape=[1],
                        value=0.95,
                        dtype=self.in_type_str,
                        persistable=True)
                    betas = [beta1, beta2]
                    opt = paddle.optimizer.AdamW(learning_rate=1e-5,
                                                 beta1=beta1,
                                                 beta2=beta2,
                                                 weight_decay=0.01,
                                                 epsilon=1e-8)
                    opt.minimize(loss)

            exe.run(startup)
            data_np = np.random.random(shape).astype(self.in_type_str)
            rets = exe.run(train_prog,
                           feed={"data": data_np},
                           fetch_list=[loss])
            assert rets[0] is not None
            paddle.disable_static()
def test_negative_dims_program(obj):
    for shape_x in generate_negative_dims(obj.shape_X):
        for shape_y in generate_negative_dims(obj.shape_Y):
            X = np.random.random(obj.shape_X).astype("float32")
            Y = np.random.random(obj.shape_Y).astype("float32")
            Ref = reference_matmul(X, Y, obj.transpose_X, obj.transpose_Y)
            with program_guard(Program(), Program()):
                x = fluid.data(name='x', shape=shape_x, dtype='float32')
                y = fluid.data(name='y', shape=shape_y, dtype='float32')
                output = fluid.layers.matmul(x, y, obj.transpose_X,
                                             obj.transpose_Y)
                obj.assertEqual(len(Ref.shape), len(output.shape))
                for idx in range(len(Ref.shape)):
                    if output.shape[idx] != -1:
                        obj.assertEqual(Ref.shape[idx], output.shape[idx])
                exe = fluid.Executor(fluid.XPUPlace(0))
                res, = exe.run(fluid.default_main_program(),
                               feed={
                                   'x': X,
                                   'y': Y
                               },
                               fetch_list=[output])
                np.allclose(res, Ref, atol=1e-5)
    def test_api(self):
        input1 = np.random.random([12, 14]).astype("float32")
        input2 = np.random.random([2, 12, 14]).astype("float32")
        x = fluid.layers.data(name='x',
                              shape=[12, 14],
                              append_batch_size=False,
                              dtype="float32")

        y = fluid.layers.data(name='target_tensor',
                              shape=[2, 12, 14],
                              append_batch_size=False,
                              dtype="float32")

        out_1 = paddle.expand_as(x, y=y)

        exe = fluid.Executor(place=fluid.XPUPlace(0))
        res_1 = exe.run(fluid.default_main_program(),
                        feed={
                            "x": input1,
                            "target_tensor": input2
                        },
                        fetch_list=[out_1])
        assert np.array_equal(res_1[0], np.tile(input1, (2, 1, 1)))
示例#27
0
    def test_api(self):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            input0 = fluid.layers.fill_constant(shape=[2, 3],
                                                dtype='int64',
                                                value=5)
            input1 = fluid.layers.fill_constant(shape=[2, 3],
                                                dtype='int64',
                                                value=3)
            expected_result = np.empty((2, 3))
            expected_result.fill(8)
            sum_value = paddle.add_n([input0, input1])
            exe = fluid.Executor(fluid.XPUPlace(0))
            result = exe.run(fetch_list=[sum_value])

            self.assertEqual((result == expected_result).all(), True)

        with fluid.dygraph.guard():
            input0 = paddle.ones(shape=[2, 3], dtype='float32')
            expected_result = np.empty((2, 3))
            expected_result.fill(2)
            sum_value = paddle.add_n([input0, input0])

            self.assertEqual((sum_value.numpy() == expected_result).all(),
                             True)
示例#28
0
def main():
    """
    Main evaluate function
    """
    cfg = load_config(FLAGS.config)
    merge_config(FLAGS.opt)
    check_config(cfg)
    # check if set use_gpu=True in paddlepaddle cpu version
    check_gpu(cfg.use_gpu)
    use_xpu = False
    if hasattr(cfg, 'use_xpu'):
        check_xpu(cfg.use_xpu)
        use_xpu = cfg.use_xpu
    # check if paddlepaddle version is satisfied
    check_version()

    assert not (use_xpu and cfg.use_gpu), \
            'Can not run on both XPU and GPU'

    main_arch = cfg.architecture

    multi_scale_test = getattr(cfg, 'MultiScaleTEST', None)

    # define executor
    if cfg.use_gpu:
        place = fluid.CUDAPlace(0)
    elif use_xpu:
        place = fluid.XPUPlace(0)
    else:
        place = fluid.CPUPlace()
    exe = fluid.Executor(place)

    # build program
    model = create(main_arch)
    startup_prog = fluid.Program()
    eval_prog = fluid.Program()
    with fluid.program_guard(eval_prog, startup_prog):
        with fluid.unique_name.guard():
            inputs_def = cfg['EvalReader']['inputs_def']
            feed_vars, loader = model.build_inputs(**inputs_def)
            if multi_scale_test is None:
                fetches = model.eval(feed_vars)
            else:
                fetches = model.eval(feed_vars, multi_scale_test)
    eval_prog = eval_prog.clone(True)

    reader = create_reader(cfg.EvalReader, devices_num=1)
    # When iterable mode, set set_sample_list_generator(reader, place)
    loader.set_sample_list_generator(reader)

    dataset = cfg['EvalReader']['dataset']

    # eval already exists json file
    if FLAGS.json_eval:
        logger.info(
            "In json_eval mode, PaddleDetection will evaluate json files in "
            "output_eval directly. And proposal.json, bbox.json and mask.json "
            "will be detected by default.")
        json_eval_results(
            cfg.metric, json_directory=FLAGS.output_eval, dataset=dataset)
        return

    compile_program = fluid.CompiledProgram(eval_prog).with_data_parallel()
    if use_xpu:
        compile_program = eval_prog

    assert cfg.metric != 'OID', "eval process of OID dataset \
                          is not supported."

    if cfg.metric == "WIDERFACE":
        raise ValueError("metric type {} does not support in tools/eval.py, "
                         "please use tools/face_eval.py".format(cfg.metric))
    assert cfg.metric in ['COCO', 'VOC'], \
            "unknown metric type {}".format(cfg.metric)
    extra_keys = []

    if cfg.metric == 'COCO':
        extra_keys = ['im_info', 'im_id', 'im_shape']
    if cfg.metric == 'VOC':
        extra_keys = ['gt_bbox', 'gt_class', 'is_difficult']

    keys, values, cls = parse_fetches(fetches, eval_prog, extra_keys)

    # whether output bbox is normalized in model output layer
    is_bbox_normalized = False
    if hasattr(model, 'is_bbox_normalized') and \
            callable(model.is_bbox_normalized):
        is_bbox_normalized = model.is_bbox_normalized()

    sub_eval_prog = None
    sub_keys = None
    sub_values = None
    # build sub-program
    if 'Mask' in main_arch and multi_scale_test:
        sub_eval_prog = fluid.Program()
        with fluid.program_guard(sub_eval_prog, startup_prog):
            with fluid.unique_name.guard():
                inputs_def = cfg['EvalReader']['inputs_def']
                inputs_def['mask_branch'] = True
                feed_vars, eval_loader = model.build_inputs(**inputs_def)
                sub_fetches = model.eval(
                    feed_vars, multi_scale_test, mask_branch=True)
                assert cfg.metric == 'COCO'
                extra_keys = ['im_id', 'im_shape']
        sub_keys, sub_values, _ = parse_fetches(sub_fetches, sub_eval_prog,
                                                extra_keys)
        sub_eval_prog = sub_eval_prog.clone(True)

    # load model
    exe.run(startup_prog)
    if 'weights' in cfg:
        checkpoint.load_params(exe, startup_prog, cfg.weights)

    resolution = None
    if 'Mask' in cfg.architecture or cfg.architecture == 'HybridTaskCascade':
        resolution = model.mask_head.resolution
    results = eval_run(exe, compile_program, loader, keys, values, cls, cfg,
                       sub_eval_prog, sub_keys, sub_values, resolution)

    # evaluation
    # if map_type not set, use default 11point, only use in VOC eval
    map_type = cfg.map_type if 'map_type' in cfg else '11point'
    save_only = getattr(cfg, 'save_prediction_only', False)
    eval_results(
        results,
        cfg.metric,
        cfg.num_classes,
        resolution,
        is_bbox_normalized,
        FLAGS.output_eval,
        map_type,
        dataset=dataset,
        save_only=save_only)
示例#29
0
 def test_xpu(self):
     if paddle.is_compiled_with_xpu():
         self.gaussian_random_test(place=fluid.XPUPlace(0))
示例#30
0
 def setUp(self):
     self.places = [fluid.CPUPlace()]
     self.places.append(fluid.XPUPlace(0))