network.py 33.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# -*- coding: utf-8 -*-

from ctypes import *

import numpy as np

from .base import _Cnetwork, _Ctensor, _lib, _LiteCObjBase
from .struct import *
from .tensor import *


class LiteOptions(Structure):
    """
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    the inference options which can optimize the network forwarding
    performance

    Attributes:
        weight_preprocess: is the option which optimize the inference performance
            with processing the weights of the network ahead

        fuse_preprocess: fuse preprocess patten, like astype + pad_channel +
            dimshuffle

        fake_next_exec: whether only to perform non-computing tasks (like
            memory allocation and queue initialization) for next exec. This will be
            reset to false when the graph is executed.

        var_sanity_check_first_run: Disable var sanity check on the first run.
            Var sanity check is enabled on the first-time execution by default, and can
            be used to find some potential memory access errors in the operator

        const_shape: used to reduce memory usage and improve performance since some
            static inference data structures can be omitted and some operators can be
            compute before forwarding

        force_dynamic_alloc: force dynamic allocate memory for all vars

        force_output_dynamic_alloc: force dynamic allocate memory for output tensor
            which are used as the input of CallbackCaller Operator

        no_profiling_on_shape_change: do not re-profile to select best implement
            algo when input shape changes (use previous algo)

44 45
        jit_level: Execute supported operators with JIT, please check with MGB_JIT_BACKEND
            for more details, this value indicates JIT level:
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

            level 1: for JIT execute with basic elemwise operator

            level 2: for JIT execute elemwise and reduce operators

        record_level: flags to optimize the inference performance with record the
            kernel tasks in first run, hereafter the inference all need is to execute the
            recorded tasks.

            level = 0 means the normal inference

            level = 1 means use record inference

            level = 2 means record inference with free the extra memory


        graph_opt_level: network optimization level:

            0: disable

            1: level-1: inplace arith transformations during graph construction

            2: level-2: level-1, plus global optimization before graph compiling

            3: also enable JIT

        async_exec_level: level of dispatch on separate threads for different comp_node.

            0: do not perform async dispatch

            1: dispatch async if there are more than one comp node with limited queue

            mask 0b10: async if there are multiple comp nodes with

            mask 0b100: always async

    Examples:
        .. code-block::

            from megenginelite import *
            options = LiteOptions()
            options.weight_preprocess = true
            options.record_level = 1
            options.fuse_preprocess = true
90 91 92 93 94 95 96 97 98 99
    """

    _fields_ = [
        ("weight_preprocess", c_int),
        ("fuse_preprocess", c_int),
        ("fake_next_exec", c_int),
        ("var_sanity_check_first_run", c_int),
        ("const_shape", c_int),
        ("force_dynamic_alloc", c_int),
        ("force_output_dynamic_alloc", c_int),
100
        ("force_output_use_user_specified_memory", c_int),
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        ("no_profiling_on_shape_change", c_int),
        ("jit_level", c_int),
        ("comp_node_seq_record_level", c_int),
        ("graph_opt_level", c_int),
        ("async_exec_level", c_int),
        # layout transform options
        ("enable_nchw44", c_int),
        ("enable_nchw44_dot", c_int),
        ("enable_nchw88", c_int),
        ("enable_nhwcd4", c_int),
        ("enable_nchw4", c_int),
        ("enable_nchw32", c_int),
        ("enable_nchw64", c_int),
    ]

    def __init__(self):
117

118 119 120 121 122 123 124
        self.weight_preprocess = False
        self.fuse_preprocess = False
        self.fake_next_exec = False
        self.var_sanity_check_first_run = True
        self.const_shape = False
        self.force_dynamic_alloc = False
        self.force_output_dynamic_alloc = False
125
        self.force_output_use_user_specified_memory = False
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        self.no_profiling_on_shape_change = False
        self.jit_level = 0
        self.comp_node_seq_record_level = 0
        self.graph_opt_level = 2
        self.async_exec_level = 1

    def __repr__(self):
        data = {
            "weight_preprocess": bool(self.weight_preprocess),
            "fuse_preprocess": bool(self.fuse_preprocess),
            "fake_next_exec": bool(self.fake_next_exec),
            "var_sanity_check_first_run": bool(self.var_sanity_check_first_run),
            "const_shape": bool(self.const_shape),
            "force_dynamic_alloc": bool(self.force_dynamic_alloc),
            "force_output_dynamic_alloc": bool(self.force_output_dynamic_alloc),
141 142 143
            "force_output_use_user_specified_memory": bool(
                self.force_output_use_user_specified_memory
            ),
144 145 146 147 148 149 150 151 152 153 154
            "no_profiling_on_shape_change": bool(self.no_profiling_on_shape_change),
            "jit_level": self.jit_level,
            "comp_node_seq_record_level": self.comp_node_seq_record_level,
            "graph_opt_level": self.graph_opt_level,
            "async_exec_level": self.async_exec_level,
        }
        return data.__repr__()


class LiteConfig(Structure):
    """
155 156 157 158 159
    Configuration when load and compile a network

    Attributes:
        has_compression: flag whether the model is compressed, the compress
            method is stored in the model
160

161
        device_id: configure the device id of a network
162

163
        device_type: configure the device type of a network
164

165 166 167 168 169 170 171 172 173
        backend: configure the inference backend of a network, now only support
            megengine

        bare_model_cryption_name: is the bare model encryption method name, bare
            model is not packed with json information, this encryption method name is
            useful to decrypt the encrypted bare model

        options: configuration of Options

174 175
        auto_optimize_inference: lite will detect the device information add set the options heuristically

176 177
        discrete_input_name: configure which input is composed of discrete multiple tensors

178 179 180 181 182
    Examples:
        .. code-block::

            from megenginelite import *
            config = LiteConfig()
183
            config.has_compression = False
184 185 186
            config.device_type = LiteDeviceType.LITE_CPU
            config.backend = LiteBackend.LITE_DEFAULT
            config.bare_model_cryption_name = "AES_default".encode("utf-8")
187
            config.auto_optimize_inference = False
188 189 190 191 192 193 194
    """

    _fields_ = [
        ("has_compression", c_int),
        ("device_id", c_int),
        ("device_type", c_int),
        ("backend", c_int),
195
        ("_bare_model_cryption_name", c_char_p),
196
        ("options", LiteOptions),
197
        ("auto_optimize_inference", c_int),
198
        ("discrete_input_name", c_char_p),
199 200 201 202 203 204 205 206 207
    ]

    def __init__(self, device_type=LiteDeviceType.LITE_CPU, option=None):
        self.device_type = device_type
        if option:
            self.options = option
        else:
            self.options = LiteOptions()

208
        self._bare_model_cryption_name = c_char_p(b"")
209 210 211
        self.use_loader_dynamic_param = 0
        self.has_compression = 0
        self.backend = LiteBackend.LITE_DEFAULT
212
        self.auto_optimize_inference = 0
213
        self.discrete_input_name = c_char_p(b"")
214

215 216 217 218 219 220 221 222 223 224 225 226
    @property
    def bare_model_cryption_name(self):
        return self._bare_model_cryption_name.decode("utf-8")

    @bare_model_cryption_name.setter
    def bare_model_cryption_name(self, name):
        if isinstance(name, str):
            self._bare_model_cryption_name = name.encode("utf-8")
        else:
            assert isinstance(name, bytes), "name should be str or bytes type."
            self._bare_model_cryption_name = name

227 228 229 230 231 232
    def __repr__(self):
        data = {
            "has_compression": bool(self.has_compression),
            "device_id": LiteDeviceType(self.device_id),
            "device_type": LiteDeviceType(self.device_type),
            "backend": LiteBackend(self.backend),
233
            "bare_model_cryption_name": self.bare_model_cryption_name,
234
            "options": self.options,
235
            "auto_optimize_inference": self.auto_optimize_inference,
236
            "discrete_input_name": self.discrete_input_name,
237 238 239 240
        }
        return data.__repr__()


241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
class LiteExtraConfig(Structure):
    """
    Extra configuration when load and compile the graph

    disable_configure_by_model_info: disable the configuration dumped with
    model, if set true, all configuration in the model will not apply, users
    should configure the network.
    """

    _fields_ = [
        ("disable_configure_by_model_info", c_int),
    ]

    def __init__(self, disable_model_config=False):
        self.disable_configure_by_model_info = disable_model_config

    def __repr__(self):
        data = {
            "disable_configure_by_model_info": bool(
                self.disable_configure_by_model_info
            ),
        }
        return data.__repr__()


266 267
class LiteIO(Structure):
    """
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    config the network input and output item, the input and output tensor
    information will describe there

    Attributes:
        name: the tensor name in the graph corresponding to the IO
            is_host: Used to mark where the input tensor comes from and where the output
            tensor will copy to, if is_host is true, the input is from host and output copy
            to host, otherwise in device. Sometimes the input is from device and output no need
            copy to host, default is true.

        io_type: The IO type, it can be SHAPE or VALUE, when SHAPE is set, the input or
            output tensor value is invaid, only shape will be set, default is VALUE

        config_layout: The layout of the config from user, if other layout is set before
            forward or get after forward, this layout will by pass. if no other
            layout is set before forward, this layout will work. if this layout is
            no set, the model will forward with its origin layout. if in output, it
            will used to check.

    Note:
        if other layout is set to input tensor before forwarding, this layout will not work
289

290
        if no layout is set before forwarding, the model will forward with its origin layout
291

292
        if layout is set in output tensor, it will used to check whether the layout computed from the network is correct
293

294 295 296 297 298 299 300 301 302 303
    Examples:
        .. code-block::

            from megenginelite import *
            io = LiteIO(
                "data2",
                is_host=True,
                io_type=LiteIOType.LITE_IO_SHAPE,
                layout=LiteLayout([2, 4, 4]),
            )
304 305 306 307

    """

    _fields_ = [
308
        ("_name", c_char_p),
309 310 311 312 313 314 315 316 317
        ("is_host", c_int),
        ("io_type", c_int),
        ("config_layout", LiteLayout),
    ]

    def __init__(
        self, name, is_host=True, io_type=LiteIOType.LITE_IO_VALUE, layout=None
    ):
        if type(name) == str:
318
            self._name = c_char_p(name.encode("utf-8"))
319
        else:
320
            self._name = c_char_p(name)
321 322 323 324 325 326 327 328 329

        if layout:
            self.config_layout = layout
        else:
            self.config_layout = LiteLayout()

        self.is_host = is_host
        self.io_type = io_type

330 331
    @property
    def name(self):
332 333 334
        """
        get the name of IO item
        """
335 336 337 338
        return self._name.decode("utf-8")

    @name.setter
    def name(self, name):
339 340 341
        """
        set the name of IO item
        """
342 343 344 345 346 347
        if isinstance(name, str):
            self._name = name.encode("utf-8")
        else:
            assert isinstance(name, bytes), "name should be str or bytes type."
            self._name = name

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    def __repr__(self):
        data = {
            "name": self.name,
            "is_host": bool(self.is_host),
            "io_type": LiteIOType(self.io_type),
            "config_layout": self.config_layout,
        }
        return data.__repr__()

    def __hash__(self):
        return hash(self.name)


class _LiteNetworkIO(Structure):

    _fields_ = [
        ("inputs", POINTER(LiteIO)),
        ("outputs", POINTER(LiteIO)),
        ("input_size", c_size_t),
        ("output_size", c_size_t),
    ]

    def __init__(self):
        self.inputs = POINTER(LiteIO)()
        self.outputs = POINTER(LiteIO)()
        self.input_size = 0
        self.output_size = 0


class LiteNetworkIO(object):
    """
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    the input and output information when load the network for user
    the NetworkIO will remain in the network until the network is destroyed.

    Attributes:
        inputs: The all input tensors information that will configure to the network

        outputs: The all output tensors information that will configure to the network

    Examples:
        .. code-block::

            from megenginelite import *
            input_io = LiteIO("data", is_host=False, io_type=LiteIOType.LITE_IO_VALUE)
            io = LiteNetworkIO()
            io.add_input(input_io)
            output_io = LiteIO("out", is_host=True, layout=LiteLayout([1, 1000]))
            io.add_output(output_io)

397 398
    """

399
    def __init__(self, inputs=None, outputs=None):
400 401
        self.inputs = []
        self.outputs = []
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
        if inputs:
            for i in inputs:
                if isinstance(i, list):
                    self.inputs.append(LiteIO(*i))
                else:
                    assert isinstance(
                        i, LiteIO
                    ), "the param to construct LiteNetworkIO must be list of the LiteIO member or the LiteIO."
                    self.inputs.append(i)
        if outputs:
            for i in outputs:
                if isinstance(i, list):
                    self.outputs.append(LiteIO(*i))
                else:
                    assert isinstance(
                        i, LiteIO
                    ), "the param to construct LiteNetworkIO must be list of the LiteIO member or the LiteIO."
                    self.outputs.append(i)

    def add_input(
        self, obj, is_host=True, io_type=LiteIOType.LITE_IO_VALUE, layout=None
    ):
424 425 426
        """
        add input information into LiteNetworkIO
        """
427 428 429 430 431
        if isinstance(obj, LiteIO):
            self.inputs.append(obj)
        else:
            name = obj
            self.add_input(LiteIO(name, is_host, io_type, layout))
432

433 434 435
    def add_output(
        self, obj, is_host=True, io_type=LiteIOType.LITE_IO_VALUE, layout=None
    ):
436 437 438
        """
        add output information into LiteNetworkIO
        """
439 440 441 442 443
        if isinstance(obj, LiteIO):
            self.outputs.append(obj)
        else:
            name = obj
            self.add_output(LiteIO(name, is_host, io_type, layout))
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

    def _create_network_io(self):
        network_io = _LiteNetworkIO()
        length = 1 if len(self.inputs) == 0 else len(self.inputs)
        self.c_inputs = (LiteIO * length)(*self.inputs)
        length = 1 if len(self.outputs) == 0 else len(self.outputs)
        self.c_outputs = (LiteIO * length)(*self.outputs)
        network_io.inputs = pointer(self.c_inputs[0])
        network_io.outputs = pointer(self.c_outputs[0])
        network_io.input_size = len(self.inputs)
        network_io.output_size = len(self.outputs)
        return network_io

    def __repr__(self):
        data = {"inputs": list(self.inputs), "outputs": list(self.outputs)}
        return data.__repr__()


LiteAsyncCallback = CFUNCTYPE(c_int)
463 464 465 466 467 468 469 470 471 472 473 474
LiteStartCallback = CFUNCTYPE(c_int, POINTER(LiteIO), POINTER(_Ctensor), c_size_t)
LiteFinishCallback = CFUNCTYPE(c_int, POINTER(LiteIO), POINTER(_Ctensor), c_size_t)


def wrap_async_callback(func):
    global wrapper

    @CFUNCTYPE(c_int)
    def wrapper():
        return func()

    return wrapper
475 476 477


def start_finish_callback(func):
478 479
    global wrapper

480 481 482 483
    @CFUNCTYPE(c_int, POINTER(LiteIO), POINTER(_Ctensor), c_size_t)
    def wrapper(c_ios, c_tensors, size):
        ios = {}
        for i in range(size):
484
            tensor = LiteTensor(physic_construct=False)
485
            tensor._tensor = c_void_p(c_tensors[i])
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 517 518 519 520 521 522 523 524 525 526 527 528 529
            tensor.update()
            io = c_ios[i]
            ios[io] = tensor
        return func(ios)

    return wrapper


class _NetworkAPI(_LiteCObjBase):
    """
    get the network api from the lib
    """

    _api_ = [
        ("LITE_make_default_network", [POINTER(_Cnetwork)]),
        ("LITE_make_network", [POINTER(_Cnetwork), LiteConfig, _LiteNetworkIO]),
        ("LITE_load_model_from_mem", [_Cnetwork, c_void_p, c_size_t]),
        ("LITE_load_model_from_path", [_Cnetwork, c_char_p]),
        ("LITE_shared_weight_with_network", [_Cnetwork, _Ctensor]),
        ("LITE_destroy_network", [_Cnetwork]),
        ("LITE_forward", [_Cnetwork]),
        ("LITE_wait", [_Cnetwork]),
        ("LITE_get_io_tensor", [_Cnetwork, c_char_p, c_int, POINTER(_Ctensor)]),
        ("LITE_get_input_name", [_Cnetwork, c_size_t, POINTER(c_char_p)]),
        ("LITE_get_output_name", [_Cnetwork, c_size_t, POINTER(c_char_p)]),
        ("LITE_get_all_input_name", [_Cnetwork, POINTER(c_size_t), POINTER(c_char_p)]),
        ("LITE_get_all_output_name", [_Cnetwork, POINTER(c_size_t), POINTER(c_char_p)]),
        ("LITE_is_cpu_inplace_mode", [_Cnetwork, POINTER(c_int)]),
        ("LITE_get_cpu_threads_number", [_Cnetwork, POINTER(c_size_t)]),
        ("LITE_get_device_id", [_Cnetwork, POINTER(c_int)]),
        ("LITE_set_device_id", [_Cnetwork, c_int]),
        ("LITE_set_cpu_inplace_mode", [_Cnetwork]),
        ("LITE_use_tensorrt", [_Cnetwork]),
        ("LITE_set_cpu_threads_number", [_Cnetwork, c_size_t]),
        ("LITE_set_stream_id", [_Cnetwork, c_int]),
        ("LITE_get_stream_id", [_Cnetwork, POINTER(c_int)]),
        ("LITE_set_network_algo_policy", [_Cnetwork, c_int]),
        ("LITE_set_network_algo_fastrun_config", [_Cnetwork, c_int, c_int]),
        ("LITE_set_network_algo_workspace_limit", [_Cnetwork, c_size_t]),
        ("LITE_share_runtime_memroy", [_Cnetwork, _Cnetwork]),
        ("LITE_enable_profile_performance", [_Cnetwork, c_char_p]),
        ("LITE_enable_io_txt_dump", [_Cnetwork, c_char_p]),
        ("LITE_enable_io_bin_dump", [_Cnetwork, c_char_p]),
        ("LITE_set_async_callback", [_Cnetwork, LiteAsyncCallback]),
530 531
        ("LITE_set_start_callback", [_Cnetwork, LiteStartCallback]),
        ("LITE_set_finish_callback", [_Cnetwork, LiteFinishCallback]),
532
        ("LITE_get_static_memory_alloc_info", [_Cnetwork, c_char_p]),
533 534
        ("LITE_enable_global_layout_transform", [_Cnetwork]),
        ("LITE_dump_layout_transform_model", [_Cnetwork, c_char_p]),
535 536 537 538 539 540 541 542
        (
            "LITE_get_model_io_info_by_path",
            [c_char_p, LiteConfig, POINTER(_LiteNetworkIO)],
        ),
        (
            "LITE_get_model_io_info_by_memory",
            [c_char_p, c_size_t, LiteConfig, POINTER(_LiteNetworkIO)],
        ),
543
        ("LITE_extra_configure", [_Cnetwork, LiteExtraConfig]),
544
        (
545
            "LITE_get_discrete_tensor",
546 547
            [_Cnetwork, c_char_p, c_size_t, c_int, POINTER(_Ctensor)],
        ),
548 549 550 551 552 553
    ]


class LiteNetwork(object):
    """
    the network to load a model and forward
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

    Examples:

        .. code-block::

            from megenginelite import *
            config = LiteConfig()
            config.device_type = LiteDeviceType.LITE_CPU
            network = LiteNetwork(config)
            network.load("model_path")

            input_name = network.get_input_name(0)
            input_tensor = network.get_io_tensor(input_name)
            output_name = network.get_output_name(0)
            output_tensor = network.get_io_tensor(output_name)

            input_tensor.set_data_by_copy(input_data)

            network.forward()
            network.wait()

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    """

    _api = _NetworkAPI()._lib

    def __init__(self, config=None, io=None):
        """
        create a network with config and networkio
        """
        self._network = _Cnetwork()

        if config:
            self.config = config
        else:
            self.config = LiteConfig()

        if io:
            self.network_io = io
        else:
            self.network_io = LiteNetworkIO()

        c_network_io = self.network_io._create_network_io()
        self._api.LITE_make_network(byref(self._network), self.config, c_network_io)

    def __repr__(self):
        data = {"config": self.config, "IOs": self.network_io}
        return data.__repr__()

    def __del__(self):
        self._api.LITE_destroy_network(self._network)

    def load(self, path):
606 607 608
        """
        load network from given path
        """
609 610 611 612
        c_path = c_char_p(path.encode("utf-8"))
        self._api.LITE_load_model_from_path(self._network, c_path)

    def forward(self):
613 614 615 616
        """
        forward the network with filled input data and fill the output data
        to the output tensor
        """
617 618 619
        self._api.LITE_forward(self._network)

    def wait(self):
620 621 622
        """
        wait until forward finish in sync model
        """
623 624 625 626 627
        self._api.LITE_wait(self._network)

    def is_cpu_inplace_mode(self):
        """
        whether the network run in cpu inpalce mode
628 629 630 631 632

        Returns:
            if use inpalce mode return True, else return False


633 634 635 636 637 638 639 640 641
        """
        inplace = c_int()
        self._api.LITE_is_cpu_inplace_mode(self._network, byref(inplace))
        return bool(inplace.value)

    def enable_cpu_inplace_mode(self):
        """
        set cpu forward in inplace mode with which cpu forward only create one
        thread
642 643 644 645

        Note:
            this must be set before the network loaded

646 647 648 649 650
        """
        self._api.LITE_set_cpu_inplace_mode(self._network)

    def use_tensorrt(self):
        """
651 652 653 654 655
        use TensorRT

        Note:
            this must be set before the network loaded

656 657 658 659 660 661 662
        """
        self._api.LITE_use_tensorrt(self._network)

    @property
    def device_id(self):
        """
        get the device id
663 664 665

        Returns:
            the device id of current network used
666 667 668 669 670 671 672 673 674
        """
        device_id = c_int()
        self._api.LITE_get_device_id(self._network, byref(device_id))
        return device_id.value

    @device_id.setter
    def device_id(self, device_id):
        """
        set the device id
675 676 677 678

        Note:
            this must be set before the network loaded

679 680 681 682 683 684 685
        """
        self._api.LITE_set_device_id(self._network, device_id)

    @property
    def stream_id(self):
        """
        get the stream id
686 687 688

        Returns:
            the value of stream id set for detwork
689 690 691 692 693 694 695 696 697
        """
        stream_id = c_int()
        self._api.LITE_get_stream_id(self._network, byref(stream_id))
        return stream_id.value

    @stream_id.setter
    def stream_id(self, stream_id):
        """
        set the stream id
698 699 700

        Note:
            this must be set before the network loaded
701 702 703 704 705 706 707
        """
        self._api.LITE_set_stream_id(self._network, stream_id)

    @property
    def threads_number(self):
        """
        get the thread number of the netwrok
708 709 710

        Returns:
            the number of thread set in the network
711 712 713 714 715 716 717 718 719
        """
        nr_thread = c_size_t()
        self._api.LITE_get_cpu_threads_number(self._network, byref(nr_thread))
        return nr_thread.value

    @threads_number.setter
    def threads_number(self, nr_threads):
        """
        set the network forward in multithread mode, and the thread number
720 721 722

        Note:
            this must be set before the network loaded
723 724 725 726 727 728
        """
        self._api.LITE_set_cpu_threads_number(self._network, nr_threads)

    def get_io_tensor(self, name, phase=LiteTensorPhase.LITE_IO):
        """
        get input or output tensor by its name
729 730 731 732 733 734 735

        Args:
            name: the name of io tensor
            phase: the type of LiteTensor, this is useful to separate input or output tensor with the same name

        Returns:
            the tensor with given name and type
736 737 738 739 740
        """
        if type(name) == str:
            c_name = c_char_p(name.encode("utf-8"))
        else:
            c_name = c_char_p(name)
741
        tensor = LiteTensor(physic_construct=False)
742 743 744 745 746 747
        self._api.LITE_get_io_tensor(
            self._network, c_name, phase, byref(tensor._tensor)
        )
        tensor.update()
        return tensor

748
    def get_discrete_tensor(self, name, n_idx, phase=LiteTensorPhase.LITE_INPUT):
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
        """
        get the n_idx'th tensor in the network input tensors whose
        input consists of discrete multiple tensors and tensor name is name

        Args:
            name: the name of input tensor
            n_idx: the tensor index
            phase: the type of LiteTensor, this is useful to separate input tensor with the same name

        Returns:
            the tensors with given name and type
        """
        if type(name) == str:
            c_name = c_char_p(name.encode("utf-8"))
        else:
            c_name = c_char_p(name)
        tensor = LiteTensor(physic_construct=False)
766
        self._api.LITE_get_discrete_tensor(
767 768 769 770 771
            self._network, c_name, n_idx, phase, byref(tensor._tensor)
        )
        tensor.update()
        return tensor

772 773 774
    def get_input_name(self, index):
        """
        get the input name by the index in the network
775 776 777 778 779 780

        Args:
            index: the index of the input name

        Returns:
            the name of input tesor with given index
781 782 783 784 785 786 787 788
        """
        c_name = c_char_p()
        self._api.LITE_get_input_name(self._network, index, byref(c_name))
        return c_name.value.decode("utf-8")

    def get_output_name(self, index):
        """
        get the output name by the index in the network
789 790 791 792 793 794

        Args:
            index: the index of the output name

        Returns:
            the name of output tesor with given index
795 796 797 798 799 800 801 802
        """
        c_name = c_char_p()
        self._api.LITE_get_output_name(self._network, index, byref(c_name))
        return c_name.value.decode("utf-8")

    def get_all_input_name(self):
        """
        get all the input tensor name in the network
803 804 805

        Returns:
            the names of all input tesor in the network
806 807 808 809 810 811 812 813 814 815 816 817 818
        """
        nr_input = c_size_t()
        self._api.LITE_get_all_input_name(self._network, byref(nr_input), None)

        if nr_input.value > 0:
            names = (c_char_p * nr_input.value)()
            self._api.LITE_get_all_input_name(self._network, None, names)
            ret_name = [names[i].decode("utf-8") for i in range(nr_input.value)]
            return ret_name

    def get_all_output_name(self):
        """
        get all the output tensor name in the network
819 820 821

        Returns:
            the names of all output tesor in the network
822 823 824 825 826 827 828 829 830 831
        """
        nr_output = c_size_t()
        self._api.LITE_get_all_output_name(self._network, byref(nr_output), None)

        if nr_output.value > 0:
            names = (c_char_p * nr_output.value)()
            self._api.LITE_get_all_output_name(self._network, None, names)
            ret_name = [names[i].decode("utf-8") for i in range(nr_output.value)]
            return ret_name

832 833 834 835 836 837
    def extra_configure(self, extra_config):
        """
        Extra Configuration to the network.
        """
        self._api.LITE_extra_configure(self._network, extra_config)

838 839 840
    def share_weights_with(self, src_network):
        """
        share weights with the loaded network
841 842 843

        Args:
            src_network: the network to share weights
844 845 846 847 848 849 850
        """
        assert isinstance(src_network, LiteNetwork)
        self._api.LITE_shared_weight_with_network(self._network, src_network._network)

    def share_runtime_memroy(self, src_network):
        """
        share runtime memory with the srouce network
851 852 853

        Args:
            src_network: the network to share runtime memory
854 855 856 857 858
        """
        assert isinstance(src_network, LiteNetwork)
        self._api.LITE_share_runtime_memroy(self._network, src_network._network)

    def async_with_callback(self, async_callback):
859 860 861 862 863 864 865
        """
        set the network forwarding in async mode and set the AsyncCallback callback
        function

        Args:
            async_callback: the callback to set for network
        """
866 867
        callback = wrap_async_callback(async_callback)
        self._api.LITE_set_async_callback(self._network, callback)
868 869 870 871 872 873

    def set_start_callback(self, start_callback):
        """
        when the network start forward, the callback will be called,
        the start_callback with param mapping from LiteIO to the corresponding
        LiteTensor
874 875 876

        Args:
            start_callback: the callback to set for network
877
        """
878 879
        callback = start_finish_callback(start_callback)
        self._api.LITE_set_start_callback(self._network, callback)
880 881 882 883 884 885

    def set_finish_callback(self, finish_callback):
        """
        when the network finish forward, the callback will be called,
        the finish_callback with param mapping from LiteIO to the corresponding
        LiteTensor
886 887 888

        Args:
            finish_callback: the callback to set for network
889
        """
890 891
        callback = start_finish_callback(finish_callback)
        self._api.LITE_set_finish_callback(self._network, callback)
892 893

    def enable_profile_performance(self, profile_file):
894 895 896 897 898 899
        """
        enable get the network performance profiled information and save into given file

        Args:
            profile_file: the file to save profile information
        """
900 901 902 903
        c_file = profile_file.encode("utf-8")
        self._api.LITE_enable_profile_performance(self._network, c_file)

    def set_network_algo_workspace_limit(self, size_limit):
904 905 906 907 908 909 910 911
        """
        set the opr workspace limitation in the target network, some opr
        maybe use large of workspace to get good performance, set workspace limitation
        can save memory but may influence the performance

        Args:
            size_limit: the byte size of workspace limitation
        """
912 913 914 915 916 917
        self._api.LITE_set_network_algo_workspace_limit(self._network, size_limit)

    def set_network_algo_policy(
        self, policy, shared_batch_size=0, binary_equal_between_batch=False
    ):
        """
918 919 920 921 922 923 924 925 926 927 928
        set the network algorithm search policy for fast-run

        Args:
            shared_batch_size: the batch size used by fastrun,
                Non-zero value means that fastrun use this batch size
                regardless of the batch size of the model. Zero means
                fastrun use batch size of the model

            binary_equal_between_batch: if the content of each input batch is
                binary equal,whether the content of each output batch is
                promised to be equal
929 930 931 932 933 934 935 936

        """
        self._api.LITE_set_network_algo_policy(self._network, policy)
        self._api.LITE_set_network_algo_fastrun_config(
            self._network, shared_batch_size, binary_equal_between_batch
        )

    def io_txt_dump(self, txt_file):
937 938 939 940 941 942 943
        """
        dump all input/output tensor of all operators to the output file, in txt
        format, user can use this function to debug compute error

        Args:
            txt_file: the txt file
        """
944 945 946 947
        c_file = txt_file.encode("utf-8")
        self._api.LITE_enable_io_txt_dump(self._network, c_file)

    def io_bin_dump(self, bin_dir):
948 949 950 951 952 953 954
        """
        dump all input/output tensor of all operators to the output file, in
        binary format, user can use this function to debug compute error

        Args:
            bin_dir: the binary file directory
        """
955 956
        c_dir = bin_dir.encode("utf-8")
        self._api.LITE_enable_io_bin_dump(self._network, c_dir)
957 958

    def get_static_memory_alloc_info(self, log_dir="logs/test"):
959 960 961 962 963 964
        """
        get static peak memory info showed by Graph visualization

        Args:
            log_dir: the directory to save information log
        """
965 966
        c_log_dir = log_dir.encode("utf-8")
        self._api.LITE_get_static_memory_alloc_info(self._network, c_log_dir)
967 968

    def enable_global_layout_transform(self):
969 970 971 972 973 974
        """
        set global layout transform optimization for network, global
        layout optimization can auto determine the layout of every operator in
        the network by profile, thus it can improve the performance of the
        network forwarding
        """
975 976 977
        self._api.LITE_enable_global_layout_transform(self._network)

    def dump_layout_transform_model(self, model_file):
978 979 980 981 982 983 984
        """
        dump network after global layout transform optimization to the
        specific path

        Args:
            model_file: the file path to dump model
        """
985 986
        c_file = model_file.encode("utf-8")
        self._api.LITE_dump_layout_transform_model(self._network, c_file)
987 988 989 990


def get_model_io_info(model_path, config=None):
    """
991 992 993 994 995 996 997 998
    get the model io information before model loaded by model path.

    Args:
        model_path: the model path to get the model IO information
        config the model configuration

    Returns:
        the input and output information in the network configuration
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    """
    api = _NetworkAPI()._lib
    c_path = c_char_p(model_path.encode("utf-8"))

    ios = _LiteNetworkIO()

    if config is not None:
        api.LITE_get_model_io_info_by_path(c_path, config, byref(ios))
    else:
        config = LiteConfig()
        api.LITE_get_model_io_info_by_path(c_path, config, byref(ios))

    ret_ios = LiteNetworkIO()
    for i in range(ios.input_size):
        ret_ios.add_input(ios.inputs[i])
    for i in range(ios.output_size):
        ret_ios.add_output(ios.outputs[i])
    return ret_ios