Beispiel #1
0
    def test_decorate(self):
        param_names = [param.name for param in self.layer.parameters()]
        self.optimizer = paddle.incubate.asp.decorate(self.optimizer)

        program = paddle.static.default_main_program()

        for name in param_names:
            mask_var = ASPHelper._get_program_asp_info(program).mask_vars.get(
                name, None)
            if ASPHelper._is_supported_layer(program, name):
                self.assertTrue(mask_var is not None)
            else:
                self.assertTrue(mask_var is None)
    def test_save_and_load(self):
        path = "/tmp/paddle_asp_save_dy/"
        net_path = path + "asp_net.pdparams"
        opt_path = path + "asp_opt.pdopt"

        paddle.save(self.layer.state_dict(), net_path)
        paddle.save(self.optimizer.state_dict(), opt_path)

        asp_info = ASPHelper._get_program_asp_info(
            paddle.static.default_main_program())
        for param_name in asp_info.mask_vars:
            mask = asp_info.mask_vars[param_name]
            asp_info.update_mask_vars(
                param_name, paddle.ones(shape=mask.shape, dtype=mask.dtype))
            asp_info.update_masks(param_name, np.ones(shape=mask.shape))

        net_state_dict = paddle.load(net_path)
        opt_state_dict = paddle.load(opt_path)

        self.layer.set_state_dict(net_state_dict)
        self.optimizer.set_state_dict(opt_state_dict)

        imgs = paddle.to_tensor(np.random.randn(64, 3, 32, 32),
                                dtype='float32',
                                place=self.place,
                                stop_gradient=False)
        labels = paddle.to_tensor(np.random.randint(10, size=(64, 1)),
                                  dtype='float32',
                                  place=self.place,
                                  stop_gradient=False)

        loss_fn = paddle.nn.MSELoss(reduction='mean')

        output = self.layer(imgs)
        loss = loss_fn(output, labels)
        loss.backward()
        self.optimizer.step()
        self.optimizer.clear_grad()

        for param in self.layer.parameters():
            if ASPHelper._is_supported_layer(
                    paddle.static.default_main_program(), param.name):
                mat = param.numpy()
                self.assertTrue(
                    paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
                                                                 n=2,
                                                                 m=4))