program_config.py 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import enum
16
from typing import Any, Callable, Dict, List, Optional
17

18
import numpy as np
19

20
import paddle
21
from paddle import fluid
22
from paddle.fluid import core, framework
23
from paddle.fluid.executor import global_scope
24 25 26 27
from paddle.fluid.framework import (
    IrGraph,
    IrNode,
    Operator,
28
    OpProtoHolder,
29 30
    convert_np_dtype_to_dtype_,
)
31 32 33 34
from paddle.static.quantization import (
    QuantizationFreezePass,
    QuantizationTransformPass,
)
35

36 37 38 39 40 41

class TensorConfig:
    '''
    A config builder for a input or a weight.
    '''

42 43 44 45 46 47
    def __init__(
        self,
        lod: Optional[List[List[int]]] = None,
        data_gen: Optional[Callable[..., np.array]] = None,
        shape: Optional[List[List[int]]] = None,
    ):
48 49 50
        '''
        shape: The shape of the tensor.
        dtype: The data type of the tensor.
51
        data: The value of WeightVar. for input, it should be None
52
        '''
W
Wilber 已提交
53
        self.lod = lod
J
Jason 已提交
54 55 56 57 58 59
        if data_gen is not None:
            self.data_gen = data_gen
            self.data = data_gen()
            self.dtype = data_gen().dtype
            self.shape = data_gen().shape
        else:
60 61 62
            assert (
                shape is not None
            ), "While data_gen is not defined, shape must not be None"
J
Jason 已提交
63 64 65
            self.data = np.random.normal(0.0, 1.0, shape).astype(np.float32)
            self.shape = shape
            self.dtype = self.data.dtype
66 67 68

    def __repr__(self):
        return str({'shape': self.shape, 'lod': self.lod, 'dtype': self.dtype})
69 70


W
Wilber 已提交
71 72 73 74 75 76
class VarType(enum.Enum):
    LOD_TENSOR = 1
    LOD_TENSOR_ARRAY = 2
    STEP_SCOPES = 3


77
class OpConfig:
78 79 80 81 82 83 84 85 86 87 88 89
    '''A config builder for generating a Op.'''

    def __init__(
        self,
        type: str,
        inputs: Dict[str, List[str]],
        outputs: Dict[str, List[str]],
        attrs: Dict[str, Any] = None,
        outputs_var_type: Dict[str, VarType] = None,
        outputs_dtype: Dict[str, np.dtype] = None,
        **kwargs,
    ):
90 91 92
        self.type = type
        self.inputs = inputs
        self.outputs = outputs
W
Wilber 已提交
93 94
        self.outputs_dtype = outputs_dtype
        self.outputs_var_type = outputs_var_type
95
        self.attrs = attrs
J
Jason 已提交
96
        if self.attrs is None:
97
            self.attrs = {}
J
Jason 已提交
98
        self.attrs.update(kwargs)
99

100 101 102 103 104
    def __repr__(self):
        log_str = self.type
        log_str += str(self.attrs)
        return log_str

105

W
Wilber 已提交
106
_OP_WITHOUT_KERNEL_SET = {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    'feed',
    'fetch',
    'recurrent',
    'go',
    'rnn_memory_helper_grad',
    'conditional_block',
    'while',
    'send',
    'recv',
    'listen_and_serv',
    'fl_listen_and_serv',
    'ncclInit',
    'select',
    'checkpoint_notify',
    'gen_bkcl_id',
    'c_gen_bkcl_id',
    'gen_nccl_id',
    'c_gen_nccl_id',
    'c_comm_init',
    'c_sync_calc_stream',
    'c_sync_comm_stream',
    'queue_generator',
    'dequeue',
    'enqueue',
    'heter_listen_and_serv',
    'c_wait_comm',
    'c_wait_compute',
    'copy_cross_scope',
W
Wilber 已提交
135 136 137 138
}


class BlockConfig:
139 140 141 142 143 144 145 146 147 148
    '''A config builder for generating a Block.'''

    def __init__(
        self,
        ops: List[OpConfig],
        vars: List[str],
        vars_dtype: Dict[str, np.dtype] = None,
        vars_var_type: Dict[str, VarType] = None,
        vars_lod_level: Dict[str, int] = None,
    ):
W
Wilber 已提交
149 150 151 152 153 154 155 156
        self.ops = ops
        self.vars = vars
        self.vars_dtype = vars_dtype
        self.vars_var_type = vars_var_type
        self.vars_lod_level = vars_lod_level

    def fill_block_desc(self, block_desc):
        for name in self.vars:
157
            var_desc = block_desc.var(name.encode())
W
Wilber 已提交
158
            var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR)
159 160 161
            if (
                self.vars_lod_level is not None
                and name in self.vars_lod_level.keys()
W
Wilber 已提交
162 163
            ):
                var_desc.set_lod_level(self.vars_lod_level[name])
164 165 166
            if (
                self.vars_var_type is not None
                and name in self.vars_var_type.keys()
W
Wilber 已提交
167 168 169 170 171 172 173 174 175
            ):
                if self.vars_var_type[name] == VarType.LOD_TENSOR_ARRAY:
                    var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR_ARRAY)
                elif self.vars_var_type[name] == VarType.STEP_SCOPES:
                    var_desc.set_type(core.VarDesc.VarType.STEP_SCOPES)
                    continue
            var_desc.set_dtype(convert_np_dtype_to_dtype_(np.float32))
            if self.vars_dtype is not None and name in self.vars_dtype.keys():
                var_desc.set_dtype(
176 177
                    convert_np_dtype_to_dtype_(self.vars_dtype[name])
                )
W
Wilber 已提交
178 179 180 181 182 183

        for op_config in self.ops:
            op_desc = block_desc.append_op()
            op_desc.set_type(op_config.type)
            for name, values in op_config.inputs.items():
                op_desc.set_input(name, values)
184 185 186 187 188 189 190 191 192
            # canonicalize scalar attrs
            if OpProtoHolder.instance().has_op_proto(op_config.type):
                proto = OpProtoHolder.instance().get_op_proto(op_config.type)
                canonicalized_attrs = framework.canonicalize_attrs(
                    op_config.attrs, proto
                )
            else:
                canonicalized_attrs = op_config.attrs
            for name, values in canonicalized_attrs.items():
W
Wilber 已提交
193 194 195 196
                op_desc._set_attr(name, values)
            for name, values in op_config.outputs.items():
                op_desc.set_output(name, values)
                for v in values:
197
                    if block_desc.has_var_recursive(v.encode()):
W
Wilber 已提交
198
                        continue
199
                    var_desc = block_desc.var(v.encode())
W
Wilber 已提交
200
                    var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR)
201 202 203
                    if (
                        op_config.outputs_var_type is not None
                        and v in op_config.outputs_var_type.keys()
W
Wilber 已提交
204
                    ):
205 206 207 208
                        if (
                            op_config.outputs_var_type[v]
                            == VarType.LOD_TENSOR_ARRAY
                        ):
W
Wilber 已提交
209
                            var_desc.set_type(
210 211 212 213 214
                                core.VarDesc.VarType.LOD_TENSOR_ARRAY
                            )
                        elif (
                            op_config.outputs_var_type[v] == VarType.STEP_SCOPES
                        ):
W
Wilber 已提交
215 216 217
                            var_desc.set_type(core.VarDesc.VarType.STEP_SCOPES)
                            continue
                    var_desc.set_dtype(convert_np_dtype_to_dtype_(np.float32))
218 219 220
                    if (
                        op_config.outputs_dtype is not None
                        and v in op_config.outputs_dtype.keys()
W
Wilber 已提交
221 222
                    ):
                        var_desc.set_dtype(
223
                            convert_np_dtype_to_dtype_(
224 225 226
                                op_config.outputs_dtype[v]
                            )
                        )
W
Wilber 已提交
227 228 229 230 231 232
            if op_config.type not in _OP_WITHOUT_KERNEL_SET:
                op_desc.infer_var_type(block_desc)
                op_desc.infer_shape(block_desc)
            op_desc.check_attrs()


233
class ProgramConfig:
234 235 236 237 238 239 240 241 242
    '''A config builder for generating a Program.'''

    def __init__(
        self,
        ops: List[OpConfig],
        weights: Dict[str, TensorConfig],
        inputs: Dict[str, TensorConfig],
        outputs: List[str],
    ):
243
        self.ops = ops
W
Wilber 已提交
244 245 246 247 248 249 250 251 252 253 254
        # if no weight need to save, we create a place_holder to help seriazlie params.
        if not weights:

            def generate_weight():
                return np.array([1]).astype(np.float32)

            self.weights = {
                "place_holder_weight": TensorConfig(data_gen=generate_weight)
            }
        else:
            self.weights = weights
255 256 257
        self.inputs = inputs
        self.outputs = outputs

258 259 260 261 262 263 264 265 266 267
    def __repr__(self):
        log_str = ''
        for i in range(len(self.ops)):
            if i != len(self.ops) - 1:
                log_str += repr(self.ops[i]) + ' + '
            else:
                log_str += repr(self.ops[i])
        log_str += ' -- '
        for t, v in self.inputs.items():
            log_str += '[' + t + ': ' + str(v) + ']'
268 269
        for t, v in self.weights.items():
            log_str += '[' + t + ': ' + str(v) + ']'
270 271 272

        return log_str

273 274

def create_fake_model(program_config):
275
    '''Create a Paddle model(in memory) according to the given config.'''
276 277 278 279 280
    paddle.enable_static()
    main_program_desc = core.ProgramDesc()
    util_program = fluid.Program()
    main_block_desc = main_program_desc.block(0)

281
    var_desc = main_block_desc.var(b"feed")
282 283 284 285 286
    var_desc.set_type(core.VarDesc.VarType.FEED_MINIBATCH)
    var_desc.set_persistable(True)

    index = 0
    for name, tensor_config in program_config.inputs.items():
287
        var_desc = main_block_desc.var(name.encode())
288 289 290 291
        var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR)
        var_desc.set_dtype(convert_np_dtype_to_dtype_(tensor_config.dtype))
        var_desc.set_shape(tensor_config.shape)
        var_desc.set_need_check_feed(True)
W
Wilber 已提交
292 293
        if tensor_config.lod is not None:
            var_desc.set_lod_level(len(tensor_config.lod))
294 295 296 297 298 299 300 301 302
        op_desc = main_block_desc._prepend_op()
        op_desc.set_type("feed")
        op_desc.set_input('X', ["feed"])
        op_desc.set_output('Out', [name])
        op_desc._set_attr("col", index)
        index = index + 1

    save_var_map = {}
    for name, tensor_config in program_config.weights.items():
303
        var_desc = main_block_desc.var(name.encode())
304 305 306 307 308 309 310 311 312 313
        var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR)
        var_desc.set_dtype(convert_np_dtype_to_dtype_(tensor_config.dtype))
        var_desc.set_shape(tensor_config.shape)
        var_desc.set_persistable(True)

        save_var_map[name] = util_program.global_block().create_parameter(
            dtype=tensor_config.dtype,
            shape=tensor_config.shape,
            type=core.VarDesc.VarType.LOD_TENSOR,
            name=name,
314
            initializer=paddle.nn.initializer.Assign(tensor_config.data),
315
        )
316 317 318 319 320
    in_vars = []
    for name in sorted(save_var_map.keys()):
        in_vars.append(save_var_map[name])

    out_var = util_program.global_block().create_var(
321 322
        type=core.VarDesc.VarType.RAW, name="out_var_0"
    )
323
    out_var.desc.set_persistable(True)
324 325 326 327 328 329
    util_program.global_block().append_op(
        type='save_combine',
        inputs={'X': in_vars},
        outputs={'Y': out_var},
        attrs={'file_path': '', 'save_to_memory': True},
    )
330 331 332
    for op_config in program_config.ops:
        op_desc = main_block_desc.append_op()
        op_desc.set_type(op_config.type)
333 334 335 336 337 338 339 340 341
        # canonicalize scalar attrs
        if OpProtoHolder.instance().has_op_proto(op_config.type):
            proto = OpProtoHolder.instance().get_op_proto(op_config.type)
            canonicalized_attrs = framework.canonicalize_attrs(
                op_config.attrs, proto
            )
        else:
            canonicalized_attrs = op_config.attrs

342 343
        for name, values in op_config.inputs.items():
            op_desc.set_input(name, values)
344
        for name, values in canonicalized_attrs.items():
W
Wilber 已提交
345 346 347 348 349 350
            if name == 'sub_block':
                sub_block_desc = main_program_desc.append_block(main_block_desc)
                values.fill_block_desc(sub_block_desc)
                op_desc._set_attr(name, sub_block_desc)
            else:
                op_desc._set_attr(name, values)
351 352
        for name, values in op_config.outputs.items():
            op_desc.set_output(name, values)
353
            for v in values:
354
                if main_block_desc.has_var_recursive(v.encode()):
W
Wilber 已提交
355
                    continue
356
                var_desc = main_block_desc.var(v.encode())
357
                var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR)
358 359 360
                if (
                    op_config.outputs_var_type is not None
                    and v in op_config.outputs_var_type.keys()
W
Wilber 已提交
361
                ):
362 363 364 365
                    if (
                        op_config.outputs_var_type[v]
                        == VarType.LOD_TENSOR_ARRAY
                    ):
W
Wilber 已提交
366 367 368 369 370
                        var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR_ARRAY)
                    elif op_config.outputs_var_type[v] == VarType.STEP_SCOPES:
                        var_desc.set_type(core.VarDesc.VarType.STEP_SCOPES)
                        continue
                var_desc.set_dtype(convert_np_dtype_to_dtype_(np.float32))
371 372 373
                if (
                    op_config.outputs_dtype is not None
                    and v in op_config.outputs_dtype.keys()
W
Wilber 已提交
374 375
                ):
                    var_desc.set_dtype(
376 377
                        convert_np_dtype_to_dtype_(op_config.outputs_dtype[v])
                    )
W
Wilber 已提交
378 379 380 381
        if op_config.type not in _OP_WITHOUT_KERNEL_SET:
            op_desc.infer_var_type(main_block_desc)
            op_desc.infer_shape(main_block_desc)
        op_desc.check_attrs()
382 383

    for index, name in enumerate(program_config.outputs):
384
        var_desc = main_block_desc.var(b"fetch")
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
        var_desc.set_type(core.VarDesc.VarType.FETCH_LIST)
        var_desc.set_need_check_feed(True)
        op_desc = main_block_desc.append_op()
        op_desc.set_type("fetch")
        op_desc.set_input('X', [name])
        op_desc.set_output('Out', ["fetch"])
        op_desc._set_attr("col", index)

    model = main_program_desc.serialize_to_string()

    util_program._sync_with_cpp()
    place = fluid.CPUPlace()
    executor = fluid.Executor(place)
    scope = fluid.Scope()
    with fluid.scope_guard(scope):
        executor.run(util_program)
        params = scope.find_var("out_var_0").get_bytes()
402

403
    return model, params
404 405


406 407 408 409 410 411 412
def create_quant_model(
    model,
    params,
    activation_quantize_type='moving_average_abs_max',
    weight_quantize_type='channel_wise_abs_max',
    save=False,
):
413 414 415
    place = paddle.CUDAPlace(0)
    scope = global_scope()
    exe = paddle.static.Executor(place)
416 417 418 419 420 421 422 423 424 425
    [
        inference_program,
        feed_target_names,
        fetch_targets,
    ] = paddle.static.load_inference_model(
        path_prefix=None,
        executor=exe,
        model_filename=model,
        params_filename=params,
    )
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    graph = IrGraph(core.Graph(inference_program.desc), for_test=True)

    out_scale_op_list = [
        "conv2d",
        "depthwise_conv2d",
        "mul",
        "matmul",
        "relu",
        "leaky_relu",
        "relu6",
        "sigmoid",
        "tanh",
        "prelu",
        "swish",
        "softmax",
        "batch_norm",
        "layer_norm",
        "elementwise_add",
        "pool2d",
        "reshape2",
        "transpose2",
        "concat",
        "elementwise_mul",
        "scale",
        "slice",
        "hard_swish",
        "hard_sigmoid",
        "conv2d_transpose",
        "gru",
        "bilinear_interp",
        "nearest_interp",
        "trilinear_interp",
        "flatten",
        "flatten2",
        "transpose",
        "pad2d",
        "reshape",
        "layer_norm",
464 465
        "fusion_gru",
        "multi_gru",
466 467
        "quantize",
        "dequantize",
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    ]
    op_real_in_out_name = {
        "conv2d": [["Input", "Filter"], ["Output"]],
        "depthwise_conv2d": [["Input", "Filter"], ["Output"]],
        "conv2d_transpose": [["Input", "Filter"], ["Output"]],
        "mul": [["X", "Y"], ["Out"]],
        "matmul": [["X", "Y"], ["Out"]],
        "pool2d": [["X"], ["Out"]],
        "elementwise_add": [["X", "Y"], ["Out"]],
        "concat": [["X"], ["Out"]],
        "softmax": [["X"], ["Out"]],
        "argmax": [["X"], ["Out"]],
        "transpose": [["X"], ["Out"]],
        "equal": [["X", "Y"], ["Out"]],
        "gather": [["X"], ["Out"]],
        "greater_equal": [["X", "Y"], ["Out"]],
        "greater_than": [["X", "Y"], ["Out"]],
        "less_equal": [["X", "Y"], ["Out"]],
        "less_than": [["X", "Y"], ["Out"]],
        "mean": [["X"], ["Out"]],
        "not_equal": [["X", "Y"], ["Out"]],
        "reshape": [["X"], ["Out"]],
        "reshape2": [["X"], ["Out"]],
        "transpose2": [["X"], ["Out"]],
        "bilinear_interp": [["X"], ["Out"]],
        "nearest_interp": [["X"], ["Out"]],
        "trilinear_interp": [["X"], ["Out"]],
        "slice": [["Input"], ["Out"]],
        "squeeze": [["X"], ["Out"]],
        "elementwise_sub": [["X", "Y"], ["Out"]],
        "relu": [["X"], ["Out"]],
        "relu6": [["X"], ["Out"]],
        "leaky_relu": [["X"], ["Out"]],
        "prelu": [["X"], ["Out"]],
        "tanh": [["X"], ["Out"]],
        "swish": [["X"], ["Out"]],
        "dropout": [["X"], ["Out"]],
        "batch_norm": [["X"], ["Y"]],
        "layer_norm": [["X"], ["Y"]],
        "sigmoid": [["X"], ["Out"]],
        "elementwise_mul": [["X", "Y"], ["Out"]],
        "scale": [["X"], ["Out"]],
        "hard_swish": [["X"], ["Out"]],
        "hard_sigmoid": [["X"], ["Out"]],
        "gru": [["Input", "Weight"], ["Hidden"]],
        "lstm": [["Input", "Weight"], ["Hidden"]],
        "pad2d": [["X"], ["Out"]],
        "flatten": [["X"], ["Out"]],
        "flatten2": [["X"], ["Out"]],
517 518
        "fusion_gru": [["X", "WeightX", "WeightH"], ["Hidden", "XX"]],
        "multi_gru": [["X", "WeightX", "WeightH"], ["Hidden"]],
519 520
        "quantize": [["Input"], ["Output"]],
        "dequantize": [["Input"], ["Output"]],
521 522 523 524
    }

    def _get_op_output_var_names(op):
        """ """
525 526 527
        assert isinstance(
            op, (IrNode, Operator)
        ), "The input op should be IrNode or Operator."
528
        var_names = []
529
        op_name = op.name() if isinstance(op, IrNode) else op.type
530 531 532 533 534 535 536 537 538 539 540 541
        if op_name not in op_real_in_out_name:
            return []

        name_list = op_real_in_out_name[op_name][1]
        for name in name_list:
            var_name = op.output(name)
            if isinstance(var_name, list):
                var_names.extend(var_name)
            else:
                var_names.append(var_name)
        return var_names

W
Wilber 已提交
542 543 544 545
    transform_pass = QuantizationTransformPass(
        scope=scope,
        place=place,
        activation_quantize_type=activation_quantize_type,
546 547
        weight_quantize_type=weight_quantize_type,
    )
W
Wilber 已提交
548 549
    transform_pass.apply(graph)

550 551 552 553 554 555
    op_nodes = graph.all_op_nodes()
    for op_node in op_nodes:
        if op_node.name() in out_scale_op_list:
            var_names = _get_op_output_var_names(op_node)
            for var_name in var_names:
                in_node = graph._find_node_by_name(op_node.outputs, var_name)
556 557 558 559
                if in_node.dtype() not in [
                    core.VarDesc.VarType.FP64,
                    core.VarDesc.VarType.FP32,
                ]:
560 561 562 563 564 565
                    continue

                op_node.op()._set_attr("out_threshold", 3.0)

    # Freeze graph for inference, but the weight of fc/conv is still float type.
    freeze_pass = QuantizationFreezePass(
566 567
        scope=scope, place=place, weight_quantize_type=weight_quantize_type
    )
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    freeze_pass.apply(graph)

    main_program = graph.to_program()

    # modify fake_quantize_moving_average_abs_max(InScale) and fake_channel_wise_dequantize_max_abs(Scales)
    op_nodes = graph.all_op_nodes()
    for op_node in op_nodes:
        if op_node.name() == 'fake_quantize_moving_average_abs_max':
            var_name = op_node.input("InScale")[0]
            tensor = scope.var(var_name).get_tensor()
            tensor.set(np.array([1], dtype=np.float32), place)
        elif op_node.name() == 'fake_channel_wise_dequantize_max_abs':
            var_name = op_node.input("Scales")[0]
            tensor = scope.var(var_name).get_tensor()
            tensor.set(np.ones(tensor.shape(), dtype=np.float32), place)

    if save:
585 586 587 588 589 590 591
        fluid.io.save_inference_model(
            'test_inference_model',
            feed_target_names,
            fetch_targets,
            exe,
            main_program=main_program,
        )
592 593 594 595

    feed_vars = [
        main_program.global_block().var(name) for name in feed_target_names
    ]
596 597 598
    serialized_program = paddle.static.serialize_program(
        feed_vars, fetch_targets, program=main_program
    )
599
    serialized_params = paddle.static.serialize_persistables(
600 601
        feed_vars, fetch_targets, executor=exe, program=main_program
    )
602
    return serialized_program, serialized_params