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

[Relay][AlterOp] NHWC to NCHWc support for Pool, concatenate, sum. #4059

Merged
merged 1 commit into from
Oct 11, 2019

Conversation

anijain2305
Copy link
Contributor

@anijain2305 anijain2305 commented Oct 4, 2019

For quantized InceptionV3, the number of layout transforms are reduced with this PR

Original - 260 Layout transforms / 1836 total operators
After PR - 172 Layout transforms / 1718 total operators
(Many of the transforms out of 172 are cheaper than the original 260 LT).

@anijain2305 anijain2305 changed the title [Relay][AlterOp] WIP - NHWC to NCHWC16c support for Pool, pad, concatenate. [Relay][AlterOp] NHWC to NCHWC16c support for Pool, pad, concatenate. Oct 5, 2019
@anijain2305
Copy link
Contributor Author

@yzhliu @vinx13 @ZihengJiang @tqchen Please review when you get time.

@anijain2305 anijain2305 changed the title [Relay][AlterOp] NHWC to NCHWC16c support for Pool, pad, concatenate. [Relay][AlterOp] NHWC to NCHWc support for Pool, pad, concatenate. Oct 5, 2019
@anijain2305 anijain2305 force-pushed the alter branch 6 times, most recently from 2d2794e to 254adbe Compare October 7, 2019 17:18
@anijain2305 anijain2305 changed the title [Relay][AlterOp] NHWC to NCHWc support for Pool, pad, concatenate. [Relay][AlterOp] NHWC to NCHWc support for Pool, pad, concatenate, sum. Oct 7, 2019
bool is_layout_modified = new_in_layouts.defined();
if (new_in_layouts.defined()) {
LOG(INFO) << "New = " << new_in_layouts[0];
LOG(INFO) << "Old = " << old_in_layouts[0];
Copy link
Member

Choose a reason for hiding this comment

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

remove

new_pad_width.push_back(axis_pad_width.at(axis_name));
} else {
// This is the axis that got split. So, check that pad_width was [0, 0] originally.
LOG(INFO) << "Old axis " << axis_name;
Copy link
Member

Choose a reason for hiding this comment

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

remove

ret = Layout::Undef();
}
}
LOG(INFO) << "Final layout = " << ret;
Copy link
Member

Choose a reason for hiding this comment

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

remove

// split.

// 1) Create a map from axis to param_width using old layout.
std::map<std::string, tvm::Array<tvm::Expr>> axis_pad_width;
Copy link
Member

Choose a reason for hiding this comment

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

do you think it is more convenient to add a hash function in LayoutAxis, and use const LayoutAxis& as key?

std::map<std::string, tvm::Array<tvm::Expr>> axis_pad_width;
int index_counter = 0;
CHECK_EQ(new_in_layouts.size(), 1);
for (auto axis : old_in_layouts[0]->axes) {
Copy link
Member

Choose a reason for hiding this comment

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

CHECK_EQ(old_in_layouts[0]->axes.size(), params->pad_width.size())

for (auto axis : new_in_layouts[0]->axes) {
auto axis_name = axis->var->name_hint;
if (axis_pad_width.count(axis_name) != 0) {
// This is primal axis. So, directly use the original pad_width.
Copy link
Member

Choose a reason for hiding this comment

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

there's no guarantee that the old layouts are all primal, right?

@@ -119,6 +119,56 @@ Array<Integer> GetExcludeAxes(size_t indim,
return r_axes;
}

Array<Array<Layout>> SumInferCorrectLayout(const Attrs& attrs, const Array<Layout>& new_in_layouts,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Array<Array<Layout>> SumInferCorrectLayout(const Attrs& attrs, const Array<Layout>& new_in_layouts,
Array<Array<Layout>> ReduceInferCorrectLayout(const Attrs& attrs, const Array<Layout>& new_in_layouts,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}
// Collect only the primal axis.
if (LayoutAxis::Get(layout_axis).IsPrimal()) {
new_layout_string += layout_dim;
Copy link
Member

Choose a reason for hiding this comment

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

how about the sub-axis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sub axis, we dont have to do anything. Basically, this will insert a layout transform from NCHWC16c to NCHW before reduce.

std::string new_layout_string = "";
int axis_index = 0;
for (auto layout_axis : new_in_layouts[0]->axes) {
char layout_dim = layout_axis->var->name_hint.at(0);
Copy link
Member

Choose a reason for hiding this comment

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

would be better to consistently use LayoutAxis

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@anijain2305 anijain2305 force-pushed the alter branch 4 times, most recently from c65a536 to 87f012e Compare October 9, 2019 00:35
src/relay/op/nn/pad.cc Outdated Show resolved Hide resolved
src/relay/op/nn/pad.cc Outdated Show resolved Hide resolved
src/relay/op/nn/pad.cc Outdated Show resolved Hide resolved
@yzhliu
Copy link
Member

yzhliu commented Oct 10, 2019

@anijain2305 Could you take a look at the failure test?

@anijain2305
Copy link
Contributor Author

I am getting some coreml flaky errors with pad operator. I will try to reproduce them. Currently, removing Pad changes from this PR. Will send separately.

@anijain2305
Copy link
Contributor Author

@yzhliu @zhiics can we get this merged? I will send a PR for pad separately.

@zhiics
Copy link
Member

zhiics commented Oct 11, 2019

I will TAL later.

@zhiics zhiics changed the title [Relay][AlterOp] NHWC to NCHWc support for Pool, pad, concatenate, sum. [Relay][AlterOp] NHWC to NCHWc support for Pool, concatenate, sum. Oct 11, 2019
Copy link
Member

@zhiics zhiics left a comment

Choose a reason for hiding this comment

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

Only a few small questions to clarify. It looks good to me.

(pad_top, pad_bottom),
(pad_left, pad_right),
(0, 0)))
do_pad = not (pad_top == 0 and pad_bottom == 0 and pad_left == 0 and pad_right == 0)
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed? or should it be done with pad?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one is fine. It is just an optimization that ensures that we do not redundant pads.

const Array<Layout>& old_in_layouts,
const Array<Array<IndexExpr>>& old_in_shapes) {
// NOTE: Discard "const" qualifier here.
ReduceAttrs* params = const_cast<ReduceAttrs*>(attrs.as<ReduceAttrs>());
Copy link
Member

Choose a reason for hiding this comment

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

Why const_cast here? is it because we need to change the axis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

Copy link
Member

@zhiics zhiics left a comment

Choose a reason for hiding this comment

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

LGTM

@zhiics zhiics merged commit d69c6fd into apache:master Oct 11, 2019
@zhiics
Copy link
Member

zhiics commented Oct 11, 2019

Thanks @anijain2305 @yzhliu @tmoreau89. This is now merged.

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

Successfully merging this pull request may close these issues.

6 participants