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][PASS] FoldScaleAxis Backward #2024

Merged
merged 2 commits into from
Oct 30, 2018
Merged

[RELAY][PASS] FoldScaleAxis Backward #2024

merged 2 commits into from
Oct 30, 2018

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented Oct 29, 2018

This is a followup of #2020 , this PR implemented the infrastructure to do backward folding of scale on axis.

Goal

Fold the scaling of axis(usually caused by BatchNorm) into weight of conv2d in the past. For example

Old:

%1 = conv2d(%1, %w, data_layout="NHWC")
%2 = multiply(%x, %scale)

Transformed:

# scale weight's output channel
%1 = multiply(%w, expand_dims(%scale, axis=1, num_newaxis=3))
%2 = conv2d(%x, %1, data_layout="NHWC")

Further constant folding can fold the multiplication and we remove the scaling in the network.

The Algorithm

The general idea is similar to the Forward algorithm. We pass additional argument (axes, scale), when doing transformation, to request the result to satisfy

result = value
for i, k in enumerate(axes):
   k-ith dimension of result *= i-th dimension of scale

The problem is again we don;t want to blindly propagate scale backward it won't get consumed. So we run a forward "preparation phase", which propagates the demand of the potential axes scaling.

The new pass is more general than the FoldScaleAxis in nnvm

  • The new pass support arbitrary scaling of multiple axes(although the further implementation is necessary), which could be helpful in NHWCc case,
  • The new pass support folding backward into sum of two of conv2d, which was not possible in NNVM.

@tqchen
Copy link
Member Author

tqchen commented Oct 29, 2018

call->args[1]->type_as<TensorTypeNode>()->shape,
weight_layout, kOIHW);
return is_const_int(wshape[0], param->groups) &&
is_const_int(wshape[1], 1);
Copy link
Member

Choose a reason for hiding this comment

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

Could we use is_const_int(wshape[0], param->groups) to check depthwise convolution? In theory, wshape[1] could be not equal to 1, although TF / MXNet / Caffe and so on frontends will make it be 1 even though the depth channel multiplier not be 1 (such as 0.25)

Copy link
Member Author

Choose a reason for hiding this comment

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

The current code makes the assumption of input and output groups being even, and the special logic relies on this

A better approach would enhance the Conv2D code itself to handle general group convolution, which should not be too hard as a followup contribution from anyone in the community:)

return CallNode::make(call->op, {input}, call->attrs, call->type_args);
}

RELAY_REGISTER_OP("nn.relu")
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 add clip operator like relu? Like Tensorflow frontend or Keras frontend, which use clip to implement relu6 operator.

Copy link
Member Author

Choose a reason for hiding this comment

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

clip can be supported, this PR mainly introduces the scaffolding of things necessary, with new folder infrastructure, we can support clip as well as other operators(flatten, transpose) by more registrations

// - Prepare phase: backward propagation of demand.
// - Transform phase: forward transformation,
//
// Similarly, borward folding process is done in two steps:
Copy link
Member

Choose a reason for hiding this comment

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

backward

class BackwardTransformer;

/*!
* \brief Preparation function for for pass scale backward.
Copy link
Member

Choose a reason for hiding this comment

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

for

@tqchen
Copy link
Member Author

tqchen commented Oct 29, 2018

Thanks, @masahi @FrozenGene I have updated the PR according to your comments

@tqchen tqchen merged commit d5103bb into apache:master Oct 30, 2018
@tqchen
Copy link
Member Author

tqchen commented Oct 30, 2018

Thanks @FrozenGene @masahi , this is merged. Followup PRs to support more ops(clip) and grouped conv is more than welcomed!

FrozenGene pushed a commit to FrozenGene/tvm that referenced this pull request Dec 27, 2018
wweic pushed a commit to neo-ai/tvm that referenced this pull request Feb 20, 2019
wweic pushed a commit to neo-ai/tvm that referenced this pull request Feb 20, 2019
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.

3 participants