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

Add int8 data loader #14123

Merged
merged 8 commits into from
Mar 4, 2019
Merged

Add int8 data loader #14123

merged 8 commits into from
Mar 4, 2019

Conversation

ZhennanQin
Copy link
Contributor

@ZhennanQin ZhennanQin commented Feb 12, 2019

@reminisce @zheng-da @szha @KellenSunderland @pengzhao-intel @TaoLv
PR major change:

  • introduce int8 data loader. This can be used for quantized network with rgb_mean != (0, 0, 0).

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://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

Change-Id: I3d97ef80b7466d7555f4970e24f02e8dfba6be2b
@ankkhedia
Copy link
Contributor

@ZhennanQin Thanks for your contribution!
@mxnet-label-bot add [pr-awaiting-review]

@marcoabreu marcoabreu added the pr-awaiting-review PR is waiting for code review label Feb 12, 2019
Copy link
Member

@anirudh2290 anirudh2290 left a comment

Choose a reason for hiding this comment

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

Can we modify quantize_v2 operator to work with int8 dtypes. This way we dont limit quantization users to using specific dtypes for their inference datasets. Also we won't need an additional argument for the quantize_model API.

@ZhennanQin
Copy link
Contributor Author

@anirudh2290 Thanks for reviewing. That idea was in my head, but I was a bit worried about letting quantize_v2 accept int8 dtypes looks a bit weird, and would not be accepted by community. I have to say, that way is good for user experience as user can use single int8 model for both fp32 and int8 data loader. If you think it's doable, then I will refactor towards it.

@anirudh2290
Copy link
Member

Yes I would prefer that and I think it would be doable. Would need to take care of inplace optimization if input and output is int8.

@ZhennanQin
Copy link
Contributor Author

@anirudh2290 I tried to add FInplaceOption and FInplaceIdentity to quantize_v2 to optimize the case that input and ouput is int8. But failed with this check in plan memory pass:
https://github.com/dmlc/tvm/blob/master/nnvm/src/pass/plan_memory.cc#L227

sid_in >= 0 &&

export MXNET_MEM_PLAN_VERBOSE_LOGGING=1 provides below logging:

[14:02:23] src/executor/../common/exec_utils.h:300: node 0 var
[14:02:23] src/executor/../common/exec_utils.h:302: node 1 _contrib_quantize_v2
[14:02:23] src/executor/../common/exec_utils.h:306: 		input 0: [1,3,224,224] (147 KB) -> var (-1)
[14:02:23] src/executor/../common/exec_utils.h:312: 		output 1: [1,3,224,224] (147 KB) -> group 0
[14:02:23] src/executor/../common/exec_utils.h:312: 		output 2: [1] (0 KB) -> group 2
[14:02:23] src/executor/../common/exec_utils.h:312: 		output 3: [1] (0 KB) -> group 1

It seems that, data is an variable of this network, so its sid_in is -1, making it can't be inplaced with quantize_v2 output. Is there any reason we can't inplace var with op output?

@szha szha requested a review from zhreshold February 19, 2019 06:15
@anirudh2290
Copy link
Member

apologies for the delay, I made an attempt on my end to make quantize use inplace optimization when in dtype is also int8. I couldnt find a way to save state in InferType to reuse it within function registered for Inplace optimization. Here is my attempt: anirudh2290@cc6bf9e . Also, I am not sure why input var cant be inplaced and would like to reproduce this issue at my end. Would you be able to share your current code for the inplace optimization for quantization op. If you are short on time, I think we can proceed with the quantization implementation without inplace optimization.

@ZhennanQin
Copy link
Contributor Author

@anirudh2290 No need to apologies, you're one of the most active reviewers I've seen in MXNet project:)
The patch letting quantize_v2 accept int8 is ready at ZhennanQin#5. You can apply it based on this PR. Alternately, you can pick whole patch with int8 data loader at ZhennanQin#6.

I couldnt find a way to save state in InferType to reuse it within function registered for Inplace optimization. Yes, that's the major problem when apply inplace optimization. But luckily, there's a strict dtype check in memory planning pass, that allows memory inplace happen only when input and output has same dtype. So registering FInplaceOption and FInplaceIdentity to quantize_v2 is enough.

@anirudh2290
Copy link
Member

Thanks @ZhennanQin ! Good to know that PlanMemory pass checks for the dtypes. will try out your patch later today.

@anirudh2290
Copy link
Member

I guess there are good reasons for not allowing inplace operations for inputs. One reason could be that the data allocation for inputs is controlled by the user instead of engine and it should not be mutated. We can argue that identity operations should still be supported, but this means that the memory cannot be reused if identity is followed by an inplace operation. Also, the reference count for reused input memory will never drop to 0. This means that it cannot be recycled and reused in the computation graph.

According to me, even if we cannot do inplace optimization we should still add support in the operator for int8 to make the user experience easier.

@ZhennanQin
Copy link
Contributor Author

@anirudh2290 Thanks for having a look on this. I agree with your point that user experience is important. Then I will add operator support without inplace optimization. In future, we can implement another pass to delete such redundant op to get it optimized.

@ZhennanQin
Copy link
Contributor Author

PR is updated to address all comments received. Please review again. Thanks. @anirudh2290 @zhreshold @reminisce

@anirudh2290
Copy link
Member

Looks like other comments have been addressed. Merging now.

@anirudh2290 anirudh2290 merged commit df7771b into apache:master Mar 4, 2019
vdantu pushed a commit to vdantu/incubator-mxnet that referenced this pull request Mar 31, 2019
* Enable int8 data layer

Change-Id: I3d97ef80b7466d7555f4970e24f02e8dfba6be2b

* fix lint

* Add parameter description

* Fix imagenet_inference.py

* Allow quantize_v2 to accept int8

* make float32 default
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* Enable int8 data layer

Change-Id: I3d97ef80b7466d7555f4970e24f02e8dfba6be2b

* fix lint

* Add parameter description

* Fix imagenet_inference.py

* Allow quantize_v2 to accept int8

* make float32 default
@ZhennanQin ZhennanQin deleted the int8_dataloader branch September 16, 2019 07:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-review PR is waiting for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants