Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tutorial - QNN] Prequantized MXNet model compilation. #5362

Closed
wants to merge 3 commits into from

Conversation

anijain2305
Copy link
Contributor

@masahi Earlier the plan was to have a single tutorial. However, I get seg faults if I import torch and mxnet in the same file. So, I am breaking it down into 3 parts - PyTorch, MXNet and TFLite. I added for Part 2 and massaged the intro to part 1.

@anijain2305
Copy link
Contributor Author

@jwfromm @siju-samuel You might also be interested

# print(mod)

# Compile Relay module. Set the target platform. Replace the target with the your target type.
target = 'llvm -mcpu=cascadelake'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't work on CI

Copy link
Member

@siju-samuel siju-samuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits.

###############################################################################
# Helper functions
# ----------------
def download_calib_dataset(dataset_url, calib_dataset):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use from tvm.contrib.download import download_testdata' as in other tutorials to download the calib record, currently its downloading to ./data`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need more images to perform the calibration. I moved the dataset to /tmp folder for now.

Comment on lines 27 to 31
In this series of tutorials, we demonstrate how to load and run models quantized by PyTorch (Part
1), MXNet (Part 2), and TFLite (Part 3). Once loaded, we can run compiled, quantized models on any
hardware TVM supports.

This is part 1 of the tutorial, where we will focus on PyTorch-prequantized models.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the 3 tutorials are in different files, suggest we can remove the references to MxNet and TFLite here. May be the below line is enough.

Here, we demonstrate how to load and run models quantized by PyTorch.
Once loaded, we can run compiled, quantized models on any hardware TVM supports.

def get_mxnet_fp32_model():
""" Read the MXNet symbol. """
model_name = 'resnet50_v1'
dir_path = os.path.dirname(os.path.realpath(__file__))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dir_path not used, can be removed.


import mxnet as mx
from gluoncv.model_zoo import get_model
from mxnet.contrib.quantization import *
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible, remove wildcard imports

# affected. Output of the following code is as follows
#
# TVM Top-5 labels: [236 211 178 165 168]
# MXNet Top-5 labels: [236 211 178 165 168]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed, while document is rendered, it will print.

@anijain2305
Copy link
Contributor Author

anijain2305 commented Apr 19, 2020

@tqchen This tutorial requires mxnet-mkl package.

Currently, the CI failure is

  File "/usr/local/lib/python3.6/dist-packages/sphinx_gallery/gen_rst.py", line 480, in _memory_usage

    out = func()

  File "/usr/local/lib/python3.6/dist-packages/sphinx_gallery/gen_rst.py", line 465, in __call__

    exec(self.code, self.globals)

  File "/workspace/tutorials/frontend/deploy_prequantized_mxnet.py", line 46, in <module>

    from mxnet.contrib.quantization import quantize_model_mkldnn

ImportError: cannot import name 'quantize_model_mkldnn'

There is no workaround here, I am using MXNet-MKL quantizer to quantize the model.

If we want to have this tutorial anyways, I can wrap the tutorial into a function and comment its invocation. And when in future, we have the package, I can remove the comment.

@masahi
Copy link
Member

masahi commented Apr 21, 2020

Can we skip calibration (uncomment to encourage testing locally) and still get meaningful output? I think it is unlikely we can update our CI for the sake of this tutorial.

@tqchen
Copy link
Member

tqchen commented Apr 21, 2020

We can just use the floating pt model for the reference pt

@anijain2305
Copy link
Contributor Author

anijain2305 commented Apr 21, 2020

@masahi @tqchen The MXNet quantized models has operators that can only work with MKLDNN. Example is as follows. Note the "op": "_sg_mkldnn_conv",

{
      "op": "_sg_mkldnn_conv",
      "name": "quantized_sg_mkldnn_conv_bn_act_18",
      "attrs": {
        "max_calib_range": "2.660447",
        "min_calib_range": "0.000000",
        "quantized": "true",
        "with_act": "true",
        "with_bn": "true"
      },
      "inputs": [[110, 0, 0], [111, 0, 0], [112, 0, 0], [113, 0, 0], [114, 0, 1], [115, 0, 1], [110, 1, 0], [110, 2, 0]],
      "subgraphs": [
        {
          "nodes": [
            {
              "op": "null",
              "name": "sg_mkldnn_conv_bn_add_act_15_output0",
              "inputs": []
            },
            {
              "op": "null",
              "name": "resnetv10_stage4_conv3_weight0",
              "inputs": []
            },
            {
              "op": "Convolution",
              "name": "resnetv10_stage4_conv3_fwd",
              "attrs": {
                "dilate": "(1, 1)",
                "kernel": "(3, 3)",
                "layout": "NCHW",
                "no_bias": "True",
                "num_filter": "512",
                "num_group": "1",
                "pad": "(1, 1)",
                "stride": "(1, 1)"
              },
              "inputs": [[0, 0, 0], [1, 0, 0]]
            },
            {
              "op": "null",
              "name": "resnetv10_stage4_batchnorm3_gamma0",
              "inputs": []
            },
            {
              "op": "null",
              "name": "resnetv10_stage4_batchnorm3_beta0",
              "inputs": []
            },
            {
              "op": "null",
              "name": "resnetv10_stage4_batchnorm3_running_mean0",
              "inputs": []
            },
            {
              "op": "null",
              "name": "resnetv10_stage4_batchnorm3_running_var0",
              "inputs": []
            },
            {
              "op": "BatchNorm",
              "name": "resnetv10_stage4_batchnorm3_fwd",
              "attrs": {
                "axis": "1",
                "eps": "1e-05",
                "fix_gamma": "False",
                "momentum": "0.9",
                "use_global_stats": "False"
              },
              "inputs": [[2, 0, 0], [3, 0, 0], [4, 0, 0], [5, 0, 0], [6, 0, 0]]
            },
            {
              "op": "Activation",
              "name": "resnetv10_stage4_relu1_fwd",
              "attrs": {"act_type": "relu"},
              "inputs": [[7, 0, 0]]
            }
          ],
          "arg_nodes": [0, 1, 3, 4, 5, 6],
          "node_row_ptr": [0, 1, 2, 3, 4, 5, 6, 7, 10, 11],
          "heads": [[8, 0, 0]]
        }
      ]
    },

Even if I quantize the model outside of this tutorial. I would still need mxnet-mkl to read the MXNet quantized model in the Relay parser.

@tqchen
Copy link
Member

tqchen commented Apr 21, 2020

OK, let us wait for the mxnet-mkl then, currently blocked by #5396 hopefully we can land this week

@masahi
Copy link
Member

masahi commented May 11, 2020

@anijain2305 @tqchen What is the status on this? I see there have been a recent CI change. Are we ready to have mxnet-mkl?

@masahi masahi self-assigned this May 11, 2020
@tqchen
Copy link
Member

tqchen commented May 11, 2020

see #5458

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants