advanced_usage.rst 11.8 KB
Newer Older
L
Liangliang He 已提交
1
Advanced usage
L
update  
liutuo 已提交
2
===============
L
Liangliang He 已提交
3

L
liutuo 已提交
4 5
This part contains the full usage of MACE.

L
update  
liutuo 已提交
6
=========
L
liutuo 已提交
7
Overview
L
update  
liutuo 已提交
8
=========
L
liutuo 已提交
9 10 11 12

As mentioned in the previous part, a model deployment file defines a case of model deployment.
The whole building process is loading a deployment file, converting models, building MACE and packing generated files.

L
update  
liutuo 已提交
13
================
L
liutuo 已提交
14
Deployment file
L
update  
liutuo 已提交
15
================
L
liutuo 已提交
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 44 45 46


One deployment file will generate one library normally, but if more than one ABIs are specified,
one library will be generated for each ABI.
A deployment file can also contain multiple models. For example, an AI camera application may
contain face recognition, object recognition, and voice recognition models, all of which can be defined
in one deployment file.

* **Example**

    Here is an example deployment file used by an Android demo application.

    .. literalinclude:: models/demo_app_models.yml
        :language: yaml


* **Configurations**


.. list-table::
    :header-rows: 1

    * - Options
      - Usage
    * - library_name
      - Library name.
    * - target_abis
      - The target ABI(s) to build, could be 'host', 'armeabi-v7a' or 'arm64-v8a'.
        If more than one ABIs will be used, seperate them by comas.
    * - target_socs
      - [optional] Build for specific SoCs.
47 48 49 50
    * - model_graph_format
      - model graph format, could be 'file' or 'code'. 'file' for converting model graph to ProtoBuf file(.pb) and 'code' for converting model graph to c++ code.
    * - model_data_format
      - model data format, could be 'file' or 'code'. 'file' for converting model weight to data file(.data) and 'code' for converting model weight to c++ code.
L
liutuo 已提交
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 90 91 92 93 94 95 96 97 98 99
    * - model_name
      - model name, should be unique if there are more than one models.
        **LIMIT: if build_type is code, model_name will be used in c++ code so that model_name must comply with c++ name specification.**
    * - platform
      - The source framework, tensorflow or caffe.
    * - model_file_path
      - The path of your model file, can be local path or remote url.
    * - model_sha256_checksum
      - The SHA256 checksum of the model file.
    * - weight_file_path
      - [optional] The path of Caffe model weights file.
    * - weight_sha256_checksum
      - [optional] The SHA256 checksum of Caffe model weights file.
    * - subgraphs
      - subgraphs key. **DO NOT EDIT**
    * - input_tensors
      - The input tensor name(s) (tensorflow) or top name(s) of inputs' layer (caffe).
        If there are more than one tensors, use one line for a tensor.
    * - output_tensors
      - The output tensor name(s) (tensorflow) or top name(s) of outputs' layer (caffe).
        If there are more than one tensors, use one line for a tensor.
    * - input_shapes
      - The shapes of the input tensors, in NHWC order.
    * - output_shapes
      - The shapes of the output tensors, in NHWC order.
    * - input_ranges
      - The numerical range of the input tensors' data, default [-1, 1]. It is only for test.
    * - validation_inputs_data
      - [optional] Specify Numpy validation inputs. When not provided, [-1, 1] random values will be used.
    * - runtime
      - The running device, one of [cpu, gpu, dsp, cpu_gpu]. cpu_gpu contains CPU and GPU model definition so you can run the model on both CPU and GPU.
    * - data_type
      - [optional] The data type used for specified runtime. [fp16_fp32, fp32_fp32] for GPU, default is fp16_fp32, [fp32] for CPU and [uint8] for DSP.
    * - limit_opencl_kernel_time
      - [optional] Whether splitting the OpenCL kernel within 1 ms to keep UI responsiveness, default is 0.
    * - nnlib_graph_mode
      - [optional] Control the DSP precision and performance, default to 0 usually works for most cases.
    * - obfuscate
      - [optional] Whether to obfuscate the model operator name, default to 0.
    * - winograd
      - [optional] Whether to enable Winograd convolution, **will increase memory consumption**.


.. note::

    Some command tools:

    .. code:: bash

L
liuqi 已提交
100 101
        # Get device's soc info.
        adb shell getprop | grep platform
L
liutuo 已提交
102 103

        # command for generating sha256_sum
L
liutuo 已提交
104
        sha256sum /path/to/your/file
L
liutuo 已提交
105 106


L
liuqi 已提交
107 108 109
==============
Advanced Usage
==============
L
liutuo 已提交
110

L
liuqi 已提交
111
There are two common advanced use cases: 1. convert model to CPP code. 2. tuning for specific SOC if use GPU.
L
liutuo 已提交
112

L
liuqi 已提交
113
* **Convert model(s) to CPP code**
L
liutuo 已提交
114

L
liuqi 已提交
115
    .. warning::
L
liutuo 已提交
116

L
liuqi 已提交
117
         If you want to use this case, you can just use static mace library.
L
liutuo 已提交
118

L
liuqi 已提交
119
    * **1. Change the model configuration file(.yml)**
L
liutuo 已提交
120

L
liuqi 已提交
121
        If you want to protect your model, you can convert model to CPP code. there are also two cases:
L
liutuo 已提交
122

L
liuqi 已提交
123
        * convert model graph to code and model weight to file with blow model configuration.
L
liutuo 已提交
124

L
liuqi 已提交
125
        .. code:: sh
L
liutuo 已提交
126

L
liuqi 已提交
127 128
            model_graph_format: code
            model_data_format: file
L
liutuo 已提交
129

L
liuqi 已提交
130
        * convert both model graph and model weight to code with blow model configuration.
L
liutuo 已提交
131

L
liuqi 已提交
132
        .. code:: sh
L
liutuo 已提交
133

L
liuqi 已提交
134 135
            model_graph_format: code
            model_data_format: code
L
liutuo 已提交
136

L
liuqi 已提交
137
        .. note::
L
liutuo 已提交
138

L
liuqi 已提交
139
             Another model protection method is using ``obfuscate`` to obfuscate the model operator name.
L
liutuo 已提交
140

L
liuqi 已提交
141
    * **2. Convert model(s) to code**
L
liutuo 已提交
142

L
liuqi 已提交
143
        .. code:: sh
L
liutuo 已提交
144

L
liuqi 已提交
145
            python tools/converter.py convert --config=/path/to/model_deployment_file.yml
L
liutuo 已提交
146

L
liuqi 已提交
147 148
        The command will generate **${library_name}.a** in **builds/${library_name}/model** directory and
        ** *.h ** in **builds/${library_name}/include** like blow dir-tree.
L
liutuo 已提交
149

L
liuqi 已提交
150
        .. code::
L
liutuo 已提交
151

L
liuqi 已提交
152 153 154 155 156 157 158 159 160
              builds
                ├── include
                │   └── mace
                │       └── public
                │           ├── mace_engine_factory.h
                │           └── mobilenet_v1.h
                └── model
                    ├── mobilenet-v1.a
                    └── mobilenet_v1.data
L
liutuo 已提交
161 162


L
liuqi 已提交
163 164
    * **3. Deployment**
        * Link `libmace.a` and `${library_name}.a` to your target.
165
        * Refer to \ ``mace/examples/example.cc``\ for full usage. The following list the key steps.
L
liutuo 已提交
166

L
liuqi 已提交
167
        .. code:: cpp
L
liutuo 已提交
168

L
liuqi 已提交
169 170 171 172 173 174
            // Include the headers
            #include "mace/public/mace.h"
            #include "mace/public/mace_runtime.h"
            // If the model_graph_format is code
            #include "mace/public/${model_name}.h"
            #include "mace/public/mace_engine_factory.h"
L
liutuo 已提交
175

L
liuqi 已提交
176
            // ... Same with the code in basic usage
L
liutuo 已提交
177

L
liuqi 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191
            // 4. Create MaceEngine instance
            std::shared_ptr<mace::MaceEngine> engine;
            MaceStatus create_engine_status;
            // Create Engine from compiled code
            create_engine_status =
                CreateMaceEngineFromCode(model_name.c_str(),
                                         nullptr,
                                         input_names,
                                         output_names,
                                         device_type,
                                         &engine);
            if (create_engine_status != MaceStatus::MACE_SUCCESS) {
              // Report error
            }
L
liutuo 已提交
192

L
liuqi 已提交
193
            // ... Same with the code in basic usage
L
liutuo 已提交
194 195


L
liuqi 已提交
196
* **Tuning for specific SOC's GPU**
L
liutuo 已提交
197

L
liuqi 已提交
198 199
    If you want to use GPU of a specific device, you could specify ``target_socs`` and
    tuning for the specific SOC. It may get 1~10% performance improvement.
L
liutuo 已提交
200

L
liuqi 已提交
201
    * **1. Change the model configuration file(.yml)**
L
liutuo 已提交
202

L
liuqi 已提交
203
        Specific ``target_socs`` in your model configuration file(.yml):
L
liutuo 已提交
204

L
liuqi 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        .. code:: sh

            target_socs: [sdm845]

        .. note::

            Get device's soc info: `adb shell getprop | grep platform`

    * **2. Convert model(s)**

        .. code:: sh

            python tools/converter.py convert --config=/path/to/model_deployment_file.yml

    * **3. Tuning**

        The tools/converter.py will enable automatic tuning for GPU kernels. This usually takes some
        time to finish depending on the complexity of your model.

        .. note::

             You should plug in device(s) with the specific SoC(s).


        .. code:: sh

            python tools/converter.py run --config=/path/to/model_deployment_file.yml --validate

        The command will generate two files in `builds/${library_name}/opencl`, like blow.

        .. code::

              builds
              └── mobilenet-v2
                  ├── model
                  │   ├── mobilenet_v2.data
                  │   └── mobilenet_v2.pb
                  └── opencl
                      └── arm64-v8a
                         ├── moblinet-v2_compiled_opencl_kernel.MiNote3.sdm660.bin
                         └── moblinet-v2_tuned_opencl_parameter.MiNote3.sdm660.bin


        * **mobilenet-v2-gpu_compiled_opencl_kernel.MI6.msm8998.bin** stands for the OpenCL binaries
          used for your models, which could accelerate the initialization stage.
          Details please refer to `OpenCL Specification <https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/clCreateProgramWithBinary.html>`__.
        * **mobilenet-v2-tuned_opencl_parameter.MI6.msm8998.bin** stands for the tuned OpenCL parameters
          for the SOC.

    * **4. Deployment**
        * Change the names of files generated above for not collision and push them to **your own device' directory**.
        * Usage like the previous procedure, blow list the key steps different.

        .. code:: cpp

            // Include the headers
            #include "mace/public/mace.h"
            #include "mace/public/mace_runtime.h"

            // 0. Set pre-compiled OpenCL binary program file paths and OpenCL parameters file path when available
            if (device_type == DeviceType::GPU) {
              mace::SetOpenCLBinaryPaths(path/to/opencl_binary_paths);
              mace::SetOpenCLParameterPath(path/to/opencl_parameter_file);
            }

            // ... Same with the code in basic usage.


===============
Useful Commands
===============
* **run the model**

.. code:: sh

    # Test model run time
    python tools/converter.py run --config=/path/to/model_deployment_file.yml --round=100

    # Validate the correctness by comparing the results against the
    # original model and framework, measured with cosine distance for similarity.
    python tools/converter.py run --config=/path/to/model_deployment_file.yml --validate

    # Check the memory usage of the model(**Just keep only one model in configuration file**)
    python tools/converter.py run --config=/path/to/model_deployment_file.yml --round=10000 &
    sleep 5
    adb shell dumpsys meminfo | grep mace_run
    kill %1


.. warning::

    ``run`` rely on ``convert`` command, you should ``run`` after ``convert``.

* **benchmark and profiling model**

.. code:: sh

    # Benchmark model, get detailed statistics of each Op.
    python tools/converter.py benchmark --config=/path/to/model_deployment_file.yml


.. warning::
L
liutuo 已提交
307

L
liuqi 已提交
308
    ``benchmark`` rely on ``convert`` command, you should ``benchmark`` after ``convert``.
L
liutuo 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

**Common arguments**

    .. list-table::
        :header-rows: 1

        * - option
          - type
          - default
          - commands
          - explanation
        * - --omp_num_threads
          - int
          - -1
          - ``run``/``benchmark``
          - number of threads
        * - --cpu_affinity_policy
          - int
          - 1
          - ``run``/``benchmark``
          - 0:AFFINITY_NONE/1:AFFINITY_BIG_ONLY/2:AFFINITY_LITTLE_ONLY
        * - --gpu_perf_hint
          - int
          - 3
          - ``run``/``benchmark``
          - 0:DEFAULT/1:LOW/2:NORMAL/3:HIGH
        * - --gpu_perf_hint
          - int
          - 3
          - ``run``/``benchmark``
          - 0:DEFAULT/1:LOW/2:NORMAL/3:HIGH
        * - --gpu_priority_hint
          - int
          - 3
          - ``run``/``benchmark``
          - 0:DEFAULT/1:LOW/2:NORMAL/3:HIGH

Use ``-h`` to get detailed help.

.. code:: sh

    python tools/converter.py -h
    python tools/converter.py build -h
    python tools/converter.py run -h
    python tools/converter.py benchmark -h