Exemplo n.º 1
0
    def run_model(self, run_ipu):
        self.build_model()
        if run_ipu:
            place = paddle.IPUPlace()
        else:
            place = paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        exe.run(self.startup_prog)
        if run_ipu:
            ipu_strategy = paddle.static.IpuStrategy()
            ipu_strategy.set_graph_config(
                num_ipus=2,
                is_training=self.is_training,
                enable_manual_shard=True)
            ipu_strategy.set_pipelining_config(
                enable_pipelining=True, batches_per_step=3)
            program = paddle.static.IpuCompiledProgram(
                self.main_prog, ipu_strategy=ipu_strategy).compile(
                    self.feed_list, self.fetch_list)
        else:
            program = self.main_prog

        feed = self.feed_ipu if run_ipu else self.feed_cpu
        result = exe.run(program, feed=feed, fetch_list=self.fetch_list)
        return result[0]
Exemplo n.º 2
0
    def run_model(self, run_ipu=True):
        self.build_model()
        if run_ipu:
            place = paddle.IPUPlace()
        else:
            place = paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        exe.run(self.startup_prog)
        if run_ipu:
            ipu_strategy = paddle.static.IpuStrategy()
            ipu_strategy.set_graph_config(is_training=True)
            program = paddle.static.IpuCompiledProgram(
                self.main_prog,
                ipu_strategy=ipu_strategy).compile(self.feed_list,
                                                   self.fetch_list)
        else:
            program = self.main_prog

        result = []
        for _ in range(100):
            if hasattr(program, "lr_sheduler"):
                program.lr_sheduler.step()
            loss_res = exe.run(program,
                               feed=self.feed,
                               fetch_list=self.fetch_list)
            result.append(loss_res)
        return np.array(result)
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.fluid.layers.fill_constant(**self.attrs)
                out = paddle.fluid.layers.elementwise_add(x, x)

                fetch_list = [out.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = exe.run(program, feed=self.feed, fetch_list=fetch_list)
            return result[0]
    def _test_save(self):
        scope = paddle.static.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = self.SEED
        startup_prog.random_seed = self.SEED
        generator = paddle.fluid.unique_name.UniqueNameGenerator()
        self.full_name = '/'.join(
            [self.attrs['path'].name, self.attrs['model_name']])

        with paddle.fluid.unique_name.guard(generator):
            with paddle.static.scope_guard(scope):
                with paddle.static.program_guard(main_prog, startup_prog):
                    x = paddle.static.data(name=self.feed_list[0],
                                           shape=self.feed_shape[0],
                                           dtype='float32')
                    conv1 = paddle.static.nn.conv2d(x,
                                                    num_filters=3,
                                                    filter_size=3,
                                                    bias_attr=False,
                                                    name='conv2d')
                    loss = paddle.mean(conv1)

                    if self.attrs['is_training']:
                        if self.attrs['opt_type'] == 'sgd':
                            sgd = paddle.optimizer.SGD(learning_rate=1e-2)
                            sgd.minimize(loss)
                        elif self.attrs['opt_type'] == 'adam':
                            adam = paddle.optimizer.Adam(learning_rate=1e-2)
                            adam.minimize(loss)
                        elif self.attrs['opt_type'] == 'lamb':
                            lamb = paddle.optimizer.Lamb(learning_rate=1e-2)
                            lamb.minimize(loss)
                fetch_list = [loss.name]

                place = paddle.IPUPlace()
                exe = paddle.static.Executor(place)
                exe.run(startup_prog)

                ipu_strategy = paddle.static.IpuStrategy()
                ipu_strategy.set_graph_config(
                    is_training=self.attrs['is_training'])
                program = paddle.static.IpuCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(self.feed_list,
                                                       fetch_list)

                result = []
                for i in range(self.attrs['steps']):
                    tmp = exe.run(program,
                                  feed=self.feed,
                                  fetch_list=fetch_list)
                    result.append(tmp)

                paddle.static.save_inference_model(self.full_name,
                                                   x,
                                                   loss,
                                                   exe,
                                                   program=program.org_program)
Exemplo n.º 5
0
    def _test_optimizer(self, run_ipu=True):
        def exclude_fn(param):
            return param.name.endswith('.w_0')

        scope = paddle.static.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = self.SEED
        startup_prog.random_seed = self.SEED
        np.random.seed(self.SEED)

        with paddle.static.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                image = paddle.static.data(name='image',
                                           shape=[1, 3, 10, 10],
                                           dtype='float32')
                bias = paddle.fluid.layers.create_parameter(
                    shape=[1, 3, 10, 10], is_bias=True, dtype='float32')
                add1 = image + bias
                conv1 = paddle.static.nn.conv2d(add1,
                                                num_filters=3,
                                                filter_size=3,
                                                bias_attr=False)

                loss = paddle.mean(conv1)
                opt = paddle.optimizer.Lamb(
                    learning_rate=1e-1,
                    lamb_weight_decay=self.attrs['weight_decay'],
                    exclude_from_weight_decay_fn=exclude_fn)
                opt.minimize(loss)

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)
            paddle.static.save(main_prog, "weight_decay")

            if run_ipu:
                feed_list = [image.name]
                fetch_list = [loss.name]
                ipu_strategy = paddle.static.IpuStrategy()
                ipu_strategy.set_graph_config(is_training=True)
                ipu_strategy.set_options(
                    {'loss_scaling': self.attrs["loss_scaling"]})
                program = paddle.static.IpuCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = []
            for epoch in range(100):
                loss_res = exe.run(program, feed=self.feed, fetch_list=[loss])
                result.append(loss_res)

            return np.array(result)
Exemplo n.º 6
0
    def test_ipu_place(self):
        num_devices = fluid.core.get_ipu_device_count()
        self.assertGreater(num_devices, 0)

        for i in range(num_devices):
            place = paddle.IPUPlace()
            p = fluid.core.Place()
            p.set_place(place)
            self.assertTrue(p.is_ipu_place())
Exemplo n.º 7
0
    def _test(self, run_ipu=True):
        scope = paddle.static.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED
        np.random.seed(SEED)

        np_image = np.random.rand(1, 3, 10, 10).astype(np.float32)

        with paddle.static.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                image = paddle.static.data(name='image',
                                           shape=[1, 3, 10, 10],
                                           dtype='float32')
                with paddle.static.ipu_shard_guard(index=0):
                    conv1 = paddle.static.nn.conv2d(image,
                                                    num_filters=3,
                                                    filter_size=3,
                                                    bias_attr=False)
                with paddle.static.ipu_shard_guard(index=1):
                    conv2 = paddle.static.nn.conv2d(conv1,
                                                    num_filters=3,
                                                    filter_size=3,
                                                    bias_attr=False)
                    loss = paddle.mean(conv2)

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            executor = paddle.static.Executor(place)
            executor.run(startup_prog)

            if run_ipu:
                feed_list = [image.name]
                fetch_list = [loss.name]
                ipu_strategy = paddle.static.IpuStrategy()
                ipu_strategy.set_graph_config(num_ipus=2,
                                              is_training=False,
                                              enable_manual_shard=True)
                ipu_strategy.set_pipelining_config(enable_pipelining=False)
                program = paddle.static.IpuCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            loss_res = executor.run(program,
                                    feed={"image": np_image},
                                    fetch_list=[loss])
            return loss_res
Exemplo n.º 8
0
    def _test(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED
        np.random.seed(SEED)

        np_image = np.random.rand(1, 3, 10, 10).astype(np.float32)

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                image = paddle.static.data(name='image',
                                           shape=[1, 3, 10, 10],
                                           dtype='float32')
                conv1 = paddle.static.nn.conv2d(image,
                                                num_filters=3,
                                                filter_size=3,
                                                bias_attr=False)
                loss = paddle.mean(conv1)

                sgd = paddle.optimizer.SGD(learning_rate=LR_New())
                sgd.minimize(loss)

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = [image.name]
                fetch_list = [loss.name]
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = True
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = []
            for epoch in range(100):
                if hasattr(program, "lr_sheduler"):
                    program.lr_sheduler.step()
                loss_res = exe.run(program,
                                   feed={image.name: np_image},
                                   fetch_list=[loss])
                result.append(loss_res)

            return np.array(result)
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(
                    name=self.feed_list[0],
                    shape=self.feed_shape[0],
                    dtype=self.feed_dtype[0])

                # [warning] Copying (host) tensor input/1 from INT64 to INT32.
                #  Will only warn once
                if run_ipu:
                    label = paddle.static.data(
                        name=self.feed_list[1],
                        shape=self.feed_shape[1],
                        dtype='int32')
                else:
                    label = paddle.static.data(
                        name=self.feed_list[1],
                        shape=self.feed_shape[1],
                        dtype='int64')

                out = fluid.layers.cross_entropy(
                    input=x, label=label, **self.attrs)
                fetch_list = [out.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = exe.run(program, feed=self.feed, fetch_list=fetch_list)
            return result[0]
Exemplo n.º 10
0
    def _test_func(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED
        np.random.seed(SEED)

        bps = 5
        n = 1 if run_ipu else -1
        c, h, w = 3, 10, 10
        np_image = np.random.uniform(size=[1 * bps, c, h, w]).astype(np.float32)

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                image = paddle.static.data(
                    name='image', shape=[n, c, h, w], dtype='float32')
                conv2d = paddle.static.nn.conv2d(
                    image, num_filters=3, filter_size=3, bias_attr=False)

                # paddle.mean oshape on ipu is [bps], need another mean()
                # paddle.mean oshape on cpu is [1]
                # out = paddle.mean(conv2d)
                out = conv2d

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = [image.name]
                fetch_list = [out.name]
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = False
                ipu_strategy.batches_per_step = bps
                program = compiler.IPUCompiledProgram(
                    main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
                                                                  fetch_list)
            else:
                program = main_prog

            result = exe.run(program,
                             feed={image.name: np_image},
                             fetch_list=[out])
            return result[0]
Exemplo n.º 11
0
    def _test_base(self, run_ipu=True, op=None, data_feed=None):
        assert (op is not None)
        assert (data_feed is not None)
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(name=self.feed_list[0],
                                       shape=self.feed_shape[0],
                                       dtype='float32')
                if not self.use_K_as_const_variable:
                    topk_values, topk_indices = op(x, **self.attrs)
                else:
                    # !important, popart cannot accept non const tensor
                    # K_t = paddle.static.data(name="in_1", shape=[1], dtype='int32')
                    K_t = fluid.layers.fill_constant(shape=[1],
                                                     dtype='int32',
                                                     value=self.k,
                                                     name="in_2")
                    topk_values, topk_indices = op(x, K_t, **self.attrs)
                fetch_list = [topk_values.name, topk_indices.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            print("Running inference ...")
            result = exe.run(program, feed=data_feed, fetch_list=fetch_list)
            print("Complete running infrence.")
            return result
Exemplo n.º 12
0
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(
                    name=self.feed_list[0],
                    shape=self.feed_shape[0],
                    dtype=self.feed_dtype[0])
                starts = paddle.static.data(
                    name=self.feed_list[1],
                    shape=self.feed_shape[1],
                    dtype=self.feed_dtype[1])
                ends = paddle.static.data(
                    name=self.feed_list[2],
                    shape=self.feed_shape[2],
                    dtype=self.feed_dtype[2])
                out = paddle.fluid.layers.slice(
                    x, starts=starts, ends=ends, **self.attrs)

                fetch_list = [out.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = exe.run(program, feed=self.feed, fetch_list=fetch_list)
            return result[0]
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(
                    name=self.feed_list[0],
                    shape=self.feed_shape[0],
                    dtype='float32')
                conv1 = paddle.static.nn.conv2d(
                    x, num_filters=3, filter_size=3, bias_attr=False)
                conv2 = paddle.static.nn.conv2d(
                    conv1, num_filters=3, filter_size=3, bias_attr=False)
                conv3 = paddle.static.nn.conv2d(
                    conv2, num_filters=3, filter_size=3, bias_attr=False)
                conv4 = paddle.static.nn.conv2d(
                    conv3, num_filters=3, filter_size=3, bias_attr=False)

                fetch_list = [conv4.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                # set batch size
                ipu_strategy.batch_size = 2
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = exe.run(program, feed=self.feed, fetch_list=fetch_list)
            return result[0]
Exemplo n.º 14
0
    def run_model(self, exec_mode):
        # cast model to fp16
        if self.is_fp16_mode(exec_mode):
            amp_list = paddle.static.amp.CustomOpLists()
            amp_list.unsupported_list = {}
            to_fp16_var_names = paddle.static.amp.cast_model_to_fp16(
                self.main_prog, amp_list)
            self.dtype_check(self.main_prog, to_fp16_var_names)

        if self.is_ipu_mode(exec_mode):
            place = paddle.CPUPlace()
        else:
            place = paddle.IPUPlace()
        exe = paddle.static.Executor(place)
        exe.run(self.startup_prog)

        # cast parameters to fp16
        if self.is_fp16_mode(exec_mode):
            paddle.static.amp.cast_parameters_to_fp16(
                paddle.CPUPlace(),
                self.main_prog,
                to_fp16_var_names=to_fp16_var_names)

        if self.is_ipu_mode(exec_mode):
            ipu_strategy = paddle.static.IpuStrategy()
            ipu_strategy.set_graph_config(
                is_training=self.is_training,
                num_ipus=self.num_ipus,
                enable_manual_shard=self.enable_manual_shard)
            ipu_strategy.set_pipelining_config(
                enable_pipelining=self.enable_pipelining,
                batches_per_step=self.batches_per_step)
            program = paddle.static.IpuCompiledProgram(
                self.main_prog,
                ipu_strategy=ipu_strategy).compile(self.feed_list,
                                                   self.fetch_list)
        else:
            program = self.main_prog

        result = []
        for _ in range(self.epoch):
            out = exe.run(program,
                          feed=self.feed_fp32,
                          fetch_list=self.fetch_list)
            result.append(out)
        self.output_dict[exec_mode] = result
Exemplo n.º 15
0
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(name=self.feed_list[0],
                                       shape=self.feed_shape[0],
                                       dtype=self.feed_dtype[0])
                add1 = paddle.fluid.layers.elementwise_add(x, x)
                reshape = paddle.fluid.layers.reshape(add1, **self.attrs)
                add2 = paddle.fluid.layers.elementwise_add(reshape, reshape)
                scale1 = paddle.fluid.layers.scale(add2)
                scale2 = paddle.fluid.layers.scale(scale1, scale=1.3, bias=0.5)
                scale3 = paddle.fluid.layers.scale(scale2, scale=2, bias=0.7)

                fetch_list = [scale3.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)
            scale1_out = main_prog.global_block().ops[4].output("Out")[0]
            main_prog.global_block().ops[4]._rename_output(
                scale1_out, add2.name)
            main_prog.global_block().ops[5]._rename_input(
                scale1_out, add2.name)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = exe.run(program, feed=self.feed, fetch_list=fetch_list)
            return result[0]
Exemplo n.º 16
0
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(
                    name=self.feed_list[0],
                    shape=self.feed_shape[0],
                    dtype=self.feed_dtype[0])
                conv1 = paddle.static.nn.conv2d(
                    x, num_filters=3, filter_size=3, bias_attr=False)
                conv2 = paddle.static.nn.conv2d(
                    x, num_filters=3, filter_size=3, bias_attr=False)
                add1 = conv1 + conv2
                conv3 = paddle.static.nn.conv2d(
                    add1, num_filters=8, filter_size=8, bias_attr=False)
                out = paddle.fluid.layers.relu(conv3, **self.attrs)
                fetch_list = [out.name]
            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            feed = self.feed_ipu if run_ipu else self.feed_cpu
            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = False
                ipu_strategy.enable_fp16 = True
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                feed_list = self.feed_list
                program = main_prog
            result = exe.run(program, feed=feed, fetch_list=fetch_list)
            return result[0]
Exemplo n.º 17
0
 def run_model(self, run_ipu):
     self.build_model()
     if run_ipu:
         place = paddle.IPUPlace()
     else:
         place = paddle.CPUPlace()
     exe = paddle.static.Executor(place)
     exe.run(self.startup_prog)
     if run_ipu:
         feed_list = self.feed_list
         ipu_strategy = paddle.static.IpuStrategy()
         ipu_strategy.set_graph_config(is_training=self.is_training)
         program = paddle.static.IpuCompiledProgram(
             self.main_prog,
             ipu_strategy=ipu_strategy).compile(feed_list, self.fetch_list)
     else:
         program = self.main_prog
     result = exe.run(program, feed=self.feed, fetch_list=self.fetch_list)
     return result[0]
Exemplo n.º 18
0
    def run_op_test(self, exec_mode, ipu_strategy=None):
        # NOTE: some op has no inputs
        # if len(self.feed_list) == 0 or len(self.fetch_list) == 0:
        #     raise ValueError('feed_list or fetch_list is empty')
        if self.is_ipu_mode(exec_mode):
            place = paddle.IPUPlace()
        else:
            place = paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        exe.run(self.startup_prog)
        if self.is_ipu_mode(exec_mode):
            if ipu_strategy is None:
                ipu_strategy = paddle.static.IpuStrategy()
                ipu_strategy.set_graph_config(is_training=self.is_training)
            if self.is_fp16_mode(exec_mode):
                ipu_strategy.set_precision_config(enable_fp16=True)
                IPUOpTest.cast_model_to_fp16(self.main_prog)
            program = paddle.static.IpuCompiledProgram(
                self.main_prog,
                ipu_strategy=ipu_strategy).compile(self.feed_list,
                                                   self.fetch_list)
        else:
            program = self.main_prog

        feed = self.feed_fp32
        if self.is_fp16_mode(exec_mode):
            feed = self.feed_fp16

        if self.is_training:
            result = []
            for _ in range(self.epoch):
                loss_res = exe.run(program,
                                   feed=feed,
                                   fetch_list=self.fetch_list)
                result.append(loss_res)
        else:
            result = exe.run(program, feed=feed, fetch_list=self.fetch_list)

        if isinstance(result, list) and len(result) == 1:
            self.output_dict[exec_mode] = result[0]
        else:
            self.output_dict[exec_mode] = result
    def _test_load(self, run_ipu):
        if run_ipu:
            place = paddle.IPUPlace()
        else:
            place = paddle.CPUPlace()
        exe = paddle.static.Executor(place)

        [inference_program, feed_target_names, fetch_targets
         ] = (paddle.static.load_inference_model(self.full_name, exe))

        if run_ipu:
            feed_list = feed_target_names
            fetch_list = [fetch_targets[0].name]
            ipu_strategy = paddle.static.IpuStrategy()
            ipu_strategy.set_graph_config(is_training=False)
            program = paddle.static.IpuCompiledProgram(
                inference_program,
                ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
        else:
            program = inference_program

        tmp = exe.run(program, feed=self.feed, fetch_list=[fetch_targets])

        return np.array(tmp)
Exemplo n.º 20
0
    def _test_base(self, save_otherwise_load):
        scope = paddle.static.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = self.SEED
        startup_prog.random_seed = self.SEED
        generator = paddle.fluid.unique_name.UniqueNameGenerator()

        with paddle.fluid.unique_name.guard(generator):
            with paddle.static.scope_guard(scope):
                with paddle.static.program_guard(main_prog, startup_prog):
                    x = paddle.static.data(name=self.feed_list[0],
                                           shape=self.feed_shape[0],
                                           dtype='float32')
                    conv1 = paddle.static.nn.conv2d(x,
                                                    num_filters=3,
                                                    filter_size=3,
                                                    bias_attr=False,
                                                    name='conv2d')
                    loss = paddle.mean(conv1)

                    # apply optimizer
                    self.optimizer().minimize(loss)
                    fetch_list = [loss.name]

                place = paddle.IPUPlace()
                exe = paddle.static.Executor(place)
                exe.run(startup_prog)

                if not save_otherwise_load:
                    paddle.static.load(main_prog,
                                       self.attrs['model_path'].name)

                ipu_strategy = paddle.static.IpuStrategy()
                ipu_strategy.set_graph_config(is_training=True)
                ipu_strategy.set_precision_config(
                    enable_fp16=self.attrs['enable_fp16'])
                ipu_program = paddle.static.IpuCompiledProgram(
                    main_prog, ipu_strategy=ipu_strategy)
                program = ipu_program.compile(self.feed_list, fetch_list)

                result = []
                run_steps = self.attrs['steps'] if save_otherwise_load \
                    else self.attrs['steps'] - self.attrs['save_at_step']

                feed = self.feed_fp16 if self.attrs[
                    'enable_fp16'] else self.feed_fp32
                for i in range(run_steps):
                    tmp = exe.run(program, feed=feed, fetch_list=fetch_list)

                    if save_otherwise_load and \
                        i == self.attrs['save_at_step'] - 1:
                        ipu_program._backend.weights_to_host()
                        paddle.static.save(main_prog,
                                           self.attrs['model_path'].name)

                    if save_otherwise_load and i >= self.attrs['save_at_step']:
                        result.append(tmp)
                    elif not save_otherwise_load:
                        result.append(tmp)

                return np.asarray(result).flatten()
Exemplo n.º 21
0
    def _test_base(self, run_ipu=True):
        scope = paddle.static.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = self.SEED
        startup_prog.random_seed = self.SEED

        bs = self.ipu_bs if run_ipu else self.cpu_bs
        with paddle.static.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                image = paddle.static.data(
                    name='image', shape=[bs, 3, 10, 10], dtype='float32')
                with paddle.static.ipu_shard_guard(index=0):
                    conv1 = paddle.static.nn.conv2d(
                        image, num_filters=3, filter_size=3, bias_attr=False)
                with paddle.static.ipu_shard_guard(index=1):
                    conv2 = paddle.static.nn.conv2d(
                        conv1, num_filters=3, filter_size=3, bias_attr=False)
                    # should consider influence of bs
                    loss = paddle.mean(conv2)

                if self.is_training:
                    if self.optimizer == 'sgd':
                        opt = paddle.optimizer.SGD(learning_rate=1e-2)
                    elif self.optimizer == 'adam':
                        opt = paddle.optimizer.Adam(learning_rate=1e-2)
                    elif self.optimizer == 'lamb':
                        opt = paddle.optimizer.Lamb(learning_rate=1e-2)
                    else:
                        raise Exception('optimizer must be sgd, adam or lamb')

                    opt.minimize(loss)

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            executor = paddle.static.Executor(place)
            executor.run(startup_prog)

            if run_ipu:
                feed_list = [image.name]
                fetch_list = [loss.name]
                ipu_strategy = paddle.static.IpuStrategy()
                ipu_strategy.set_graph_config(
                    num_ipus=2 * self.ipu_options['replicated_graph_count'],
                    is_training=self.is_training,
                    enable_manual_shard=True)
                ipu_strategy.set_options(self.ipu_options)
                program = paddle.static.IpuCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            feed = self.feed_ipu if run_ipu else self.feed_cpu
            epoch = self.epoch
            if not run_ipu:
                epoch *= self.ipu_options['replicated_graph_count']
                epoch *= self.ipu_options['batches_per_step']
                epoch *= self.ipu_options['accumulation_factor']
                epoch = epoch / (self.cpu_bs / self.ipu_bs)
            result = []
            for i in range(int(epoch)):
                loss_res = executor.run(program, feed=feed, fetch_list=[loss])
                result.append(loss_res)
            return np.array(result).flatten()
Exemplo n.º 22
0
    # paddle input placeholder
    (src_ids, sent_ids, pos_ids, input_mask, mask_label, mask_pos) = inputs

    # ernie model
    ernie = ErnieModel(src_ids, sent_ids, pos_ids, input_mask, ernie_config)
    fetch_node = ernie.get_sequence_output()
    if args.is_training:
        with fluid.ipu_shard(ipu_index=1, ipu_stage=1):
            _, mean_mask_lm_loss = ernie.get_lm_output(mask_label, mask_pos)
            fetch_node = mean_mask_lm_loss
            adam = paddle.optimizer.Adam(learning_rate=1e-2)
            adam.minimize(mean_mask_lm_loss)

    # place = paddle.CPUPlace()
    if args.run_on_ipu:
        place = paddle.IPUPlace()
    else:
        place = paddle.CPUPlace()
    executor = paddle.static.Executor(place)

    # feed & fetch list
    if args.is_training:
        feed_list = input_fields['names']
    else:
        feed_list = input_fields['names'][:4]
    fetch_list = [fetch_node.name]

    # program
    startup_prog = paddle.static.default_startup_program()
    executor.run(startup_prog)
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        if run_ipu:
            self.feed = {
                "x":
                np.array([[[1], [3]], [[2], [4]], [[4],
                                                   [127]]]).astype(np.int32)
            }
        else:
            self.feed = {
                "x":
                np.array([[[1], [3]], [[2], [4]], [[4],
                                                   [127]]]).astype(np.int64)
            }

        self.set_feed_attr()

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(name=self.feed_list[0],
                                       shape=self.feed_shape[0],
                                       dtype=self.feed_dtype[0])
                out = paddle.fluid.layers.embedding(x, **self.attrs)

                if self.is_training:
                    loss = paddle.mean(out)
                    adam = paddle.optimizer.Adam(learning_rate=1e-2)
                    adam.minimize(loss)
                    fetch_list = [loss.name]
                else:
                    fetch_list = [out.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            if self.is_training:
                result = []
                for _ in range(self.epoch):
                    loss_res = exe.run(program,
                                       feed=self.feed,
                                       fetch_list=fetch_list)
                    result.append(loss_res[0])
                return np.array(result)
            else:
                result = exe.run(program,
                                 feed=self.feed,
                                 fetch_list=fetch_list)
                return result[0]
Exemplo n.º 24
0
    def _test_optimizer(self, run_ipu=True):
        scope = paddle.static.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = self.SEED
        startup_prog.random_seed = self.SEED
        np.random.seed(self.SEED)

        with paddle.static.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                image = paddle.static.data(name='image',
                                           shape=[1, 3, 10, 10],
                                           dtype='float32')
                conv1 = paddle.static.nn.conv2d(image,
                                                num_filters=3,
                                                filter_size=3,
                                                bias_attr=False)
                loss = paddle.mean(conv1)

                weight_decay = self.attrs['weight_decay']
                opt = paddle.optimizer.SGD(learning_rate=1e-1,
                                           weight_decay=weight_decay)
                if self.attrs['optimizer'] == 'adam':
                    opt = paddle.optimizer.Adam(learning_rate=1e-1,
                                                weight_decay=weight_decay)
                elif self.attrs['optimizer'] == 'lamb':

                    opt = paddle.optimizer.Lamb(learning_rate=1e-1,
                                                lamb_weight_decay=weight_decay)
                opt.minimize(loss)

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = [image.name]
                fetch_list = [loss.name]
                ipu_strategy = paddle.static.IpuStrategy()
                ipu_strategy.set_graph_config(is_training=True)
                ipu_strategy.set_options({"runtime_options.enable_eval": True})
                program = paddle.static.IpuCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            result = []
            if run_ipu:
                for epoch in range(200):
                    if epoch == 100:
                        ipu_strategy.set_options(
                            {"runtime_options.enable_eval": False})
                    loss_res = exe.run(program,
                                       feed=self.feed,
                                       fetch_list=[loss])
                    result.append(loss_res)
            else:
                for epoch in range(100):
                    loss_res = exe.run(program,
                                       feed=self.feed,
                                       fetch_list=[loss])
                    result.append(loss_res)
            return np.array(result)
Exemplo n.º 25
0
    def _test_base(self, save_otherwise_load):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        main_prog.random_seed = self.SEED
        startup_prog.random_seed = self.SEED
        generator = fluid.unique_name.UniqueNameGenerator()

        with fluid.unique_name.guard(generator):
            with fluid.scope_guard(scope):
                with paddle.static.program_guard(main_prog, startup_prog):
                    x = paddle.static.data(name=self.feed_list[0],
                                           shape=self.feed_shape[0],
                                           dtype='float32')
                    conv1 = paddle.static.nn.conv2d(x,
                                                    num_filters=3,
                                                    filter_size=3,
                                                    bias_attr=False,
                                                    name='conv2d')
                    loss = paddle.mean(conv1)

                    if self.attrs['is_training']:
                        if self.attrs['opt_type'] == 'sgd':
                            sgd = paddle.optimizer.SGD(learning_rate=1e-2)
                            sgd.minimize(loss)
                        elif self.attrs['opt_type'] == 'adam':
                            adam = paddle.optimizer.Adam(learning_rate=1e-2)
                            adam.minimize(loss)
                        elif self.attrs['opt_type'] == 'lamb':
                            lamb = paddle.optimizer.Lamb(learning_rate=1e-2)
                            lamb.minimize(loss)
                    fetch_list = [loss.name]

                place = paddle.IPUPlace()
                exe = paddle.static.Executor(place)
                exe.run(startup_prog)

                if not save_otherwise_load:
                    paddle.static.load(main_prog, "model/model")

                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.attrs['is_training']
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(self.feed_list,
                                                       fetch_list)

                result = []
                run_steps = self.attrs['steps'] if save_otherwise_load \
                    else self.attrs['steps'] - self.attrs['save_at_step']

                for i in range(run_steps):
                    tmp = exe.run(program,
                                  feed=self.feed,
                                  fetch_list=fetch_list)

                    # currently, we update opt state every sess.run,
                    # will optimize
                    if save_otherwise_load and \
                        i == self.attrs['save_at_step'] - 1:
                        paddle.static.save(main_prog, "model/model")

                    if save_otherwise_load and i >= self.attrs['save_at_step']:
                        result.append(tmp)
                    elif not save_otherwise_load:
                        result.append(tmp)

                return np.asarray(result).flatten()
Exemplo n.º 26
0
    def _test_base(self, run_ipu=True):
        scope = fluid.core.Scope()
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
        SEED = self.SEED
        main_prog.random_seed = SEED
        startup_prog.random_seed = SEED

        with fluid.scope_guard(scope):
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(name=self.feed_list[0],
                                       shape=self.feed_shape[0],
                                       dtype=self.feed_dtype[0])

                if self.is_training:
                    ch = self.feed_shape[0][1]
                    conv1 = paddle.static.nn.conv2d(x,
                                                    num_filters=ch,
                                                    filter_size=3,
                                                    bias_attr=False)
                    scale = paddle.ParamAttr(trainable=True)
                    bias = paddle.ParamAttr(trainable=True)
                    out = paddle.fluid.layers.nn.group_norm(conv1,
                                                            param_attr=scale,
                                                            bias_attr=bias,
                                                            **self.attrs)
                else:
                    scale = True
                    bias = True
                    out = paddle.fluid.layers.nn.group_norm(x,
                                                            param_attr=scale,
                                                            bias_attr=bias,
                                                            **self.attrs)

                if self.is_training:
                    loss = paddle.mean(out)
                    adam = paddle.optimizer.Adam(learning_rate=1e-2)
                    adam.minimize(loss)
                    fetch_list = [loss.name]
                else:
                    fetch_list = [out.name]

            if run_ipu:
                place = paddle.IPUPlace()
            else:
                place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

            if run_ipu:
                feed_list = self.feed_list
                ipu_strategy = compiler.get_ipu_strategy()
                ipu_strategy.is_training = self.is_training
                program = compiler.IPUCompiledProgram(
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

            if self.is_training:
                result = []
                for _ in range(self.epoch):
                    loss_res = exe.run(program,
                                       feed=self.feed,
                                       fetch_list=fetch_list)
                    result.append(loss_res[0])
                return np.array(result)
            else:
                result = exe.run(program,
                                 feed=self.feed,
                                 fetch_list=fetch_list)
                return result[0]