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

[MKLDNN] Enable convolution fusion. #12308

Closed
wants to merge 1 commit into from

Conversation

ZhennanQin
Copy link
Contributor

@ZhennanQin ZhennanQin commented Aug 23, 2018

Description

Implement mkldnn convlution fusion(eg. conv+relu, conv+bn, conv+sum) based on subgraph.
@pengzhao-intel @TaoLv @zheng-da @reminisce

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

}
const int GetHash() const {
int hash = 0;
hash = hash * 2 + this->with_bn ? 1 : 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Possible hash collision: with_bn=0 and with_relu=1 equals BN=1 and relu0. Consider using bitflags

Copy link
Contributor

Choose a reason for hiding this comment

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

why not std::hash?


static inline std::string PrintArguments(const ConvolutionParam& param_) {
auto args = ListArguments(param_);
std::string str = "[";
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to use std::stringstream to compose such string, otherwise it's a lot of redundant copying internally.

DMLC_DECLARE_FIELD(with_postsum_relu).set_default(false)
.describe("Add post relu after sum");
}
const int GetHash() const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can std::string be used here instead?

LOG(INFO) << "Conv req size: " << req.size();
for (size_t k = 0; k < inputs.size(); ++k) {
auto input = inputs[k];
printf("input %ld :", k);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sometimes it's LOG(INFO), sometimes it's printf. I think it's better to use LOG always.

if (it != attrs.dict.end() && it->second == "true") {
it = attrs.dict.find("in_sum_at_begin");
if (it != attrs.dict.end() && it->second == "true") {
return std::vector<std::pair<int, int>>{std::pair<int, int>{0, 0}};
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use std::make_pair and ommit the template argument types

DefaultSubgraphOpResourceRequest)
.set_attr<std::string>("key_var_num_args", "num_args")
.set_attr<nnvm::FInplaceOption>("FInplaceOption", [](const nnvm::NodeAttrs
&attrs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you specify the return type, like -> std::vector<std::pair<int, int>> then you can do things like return {}; further in the lambda or just return std::make_pair(0, 0).

class SgMKLDNNConvSelector : public SubgraphSelector {
public:
/*! \brief pattern match status */
enum SelectStatus {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to use typed enums since they have better scoping and you don't need any prefixes or all caps. Usage will be like: SelectStatus.Fail

if (new_node.inputs[1].node.get() == &n) {
sum_entry = new_node.inputs[0];
}
#if 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this code needed?

auto last_node = sym.outputs[0].node;
nnvm::Symbol new_sym;
new_sym.outputs.emplace_back(nnvm::NodeEntry{last_node, 0, 0});
std::string node_name = "";
Copy link
Contributor

Choose a reason for hiding this comment

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

std::stringstream

ConvFusionFallBackCompute();
}

class SgMKLDNNConvOperator {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not have the definition in the header file?

nnvm::NodeEntry conv_data;
std::vector<const nnvm::Node *> matched_list;

bool HandleMatchStatus() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not have the implementations for all methods in the .cc file?

@ZhennanQin
Copy link
Contributor Author

@marcoabreu @lebeg @larroy. Thanks for your reviewing comments, I will address them in next version.

@stu1130
Copy link
Contributor

stu1130 commented Sep 18, 2018

@ZhennanQin Thanks for your contribution. Could you address the feedback? Let us know if you need help!

@vandanavk
Copy link
Contributor

@mxnet-label-bot [pr-awaiting-response]

@marcoabreu marcoabreu added the pr-awaiting-response PR is reviewed and waiting for contributor to respond label Sep 24, 2018
@vrakesh
Copy link
Contributor

vrakesh commented Oct 9, 2018

@ZhennanQin Requesting an update on the PR, have the changes been addressed?

@ZhennanQin
Copy link
Contributor Author

@vrakesh @stu1130 Sorry for responding late. This PR is combined into #12530, leaving as a reference for the fusion part change. Shall I close this PR?

@ZhennanQin ZhennanQin closed this Oct 10, 2018
@ZhennanQin ZhennanQin deleted the conv_fusion branch October 10, 2018 00:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-response PR is reviewed and waiting for contributor to respond
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants