Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[C++] fix type inconsistent issue when loading quantized parameters #15038

Merged
merged 8 commits into from
May 24, 2019

Conversation

wuxun-zhang
Copy link
Contributor

Description

@pengzhao-intel @ZhennanQin
We want to use inception_inference.cpp to do inference with quantized models. But When I tried to run inference, there always have a type inconsistent error. We found that during loading parameters, new NDArrays will be created with default data type (float32) and copy the original NDArray to this new NDArray by using NDArray.Copy(context). So, the original int8 params will be converted into float32 params and this will raise the type inconsistent issue.

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http:https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Feature1, tests, (and when applicable, API doc)
  • Feature2, tests, (and when applicable, API doc)

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@wuxun-zhang wuxun-zhang requested a review from nswamy as a code owner May 22, 2019 08:48
@pengzhao-intel
Copy link
Contributor

@reminisce @ZhennanQin could you help take a review?

@pengzhao-intel pengzhao-intel added this to Review in progress in CPU Performance and Quantization May 22, 2019
@wuxun-zhang
Copy link
Contributor Author

I wrote a simple test to reproduce this issue.

#include <iostream>
#include <vector>
#include "mxnet-cpp/MxNetCpp.h"
using namespace mxnet::cpp;

enum TypeFlag {
  kFloat32 = 0,
  kInt8 = 1
};

int main(void){
    std::vector<mx_uint> shape{4, 5, 6};
    int src_dtype = kInt8;
    Context context = Context::cpu(); 
    // specify the data type according to the input NDArray
    NDArray src(shape, context, true, src_dtype);
    NDArray dst;
    dst = src.Copy(context);
    std::cout<<"src.dtype = "<<src.GetDType()<<std::endl;
    std::cout<<"dst.dtype = "<<dst.GetDType()<<std::endl;
    return 0;
}

Before

src.dtype = 1
dst.dtype = 0

After

src.dtype = 1
dst.dtype = 1

@pengzhao-intel
Copy link
Contributor

Please add a test case for the change

@pengzhao-intel
Copy link
Contributor

@ciyongch @TaoLv please help take a review.

CPU Performance and Quantization automation moved this from Review in progress to Reviewer approved May 23, 2019
Copy link
Contributor

@pengzhao-intel pengzhao-intel left a comment

Choose a reason for hiding this comment

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

LGTM

cpp-package/example/test_ndarray.cpp Outdated Show resolved Hide resolved
cpp-package/example/test_ndarray.cpp Outdated Show resolved Hide resolved
@karan6181
Copy link
Contributor

@mxnet-label-bot add [C++, NDArray, pr-awaiting-response]

@marcoabreu marcoabreu added C++ Related to C++ NDArray pr-awaiting-response PR is reviewed and waiting for contributor to respond labels May 23, 2019
@pengzhao-intel
Copy link
Contributor

@TaoLv @ciyongch please review again.

Copy link
Contributor

@ciyongch ciyongch left a comment

Choose a reason for hiding this comment

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

LGTM.

@TaoLv
Copy link
Member

TaoLv commented May 24, 2019

@wuxun-zhang Please also post the error log before this fix.

Copy link
Member

@TaoLv TaoLv left a comment

Choose a reason for hiding this comment

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

The code changes look good to me now. @szha @eric-haibin-lin can you take a look at the cpp API change?

Copy link
Contributor

@ZhennanQin ZhennanQin left a comment

Choose a reason for hiding this comment

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

LGTM

@pengzhao-intel pengzhao-intel merged commit 93fdcad into apache:master May 24, 2019
CPU Performance and Quantization automation moved this from Reviewer approved to Done May 24, 2019
@pengzhao-intel
Copy link
Contributor

Thanks for your contribution. Merged :)

@wuxun-zhang wuxun-zhang deleted the fix_c++_load_parameters branch June 2, 2019 16:06
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
…pache#15038)

* fix type inconsistent when using C++ API to load params file

* add test case

* fix cpplint

* address comment

* retrigger CI

* fix comments

* modify ci_test

* fix indentation
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C++ Related to C++ NDArray pr-awaiting-response PR is reviewed and waiting for contributor to respond
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

8 participants