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

[ETHOSU][MicroNPU][Pass] Add a pass to replicate pads #14909

Merged
merged 7 commits into from
Jul 17, 2023

Conversation

sergio-grovety
Copy link
Contributor

@sergio-grovety sergio-grovety commented May 22, 2023

Added a pass to to handle the situation when nn.pad operator has more than one qnn.conv2d consumer.

       pad
     /     \
 Conv2D    Conv2D

In this case, because of the peculiarities of pattern parsing, conv2d does not get into the composite for the NPU. Therefore, pads are added so that each has only one consumer.

@tvm-bot
Copy link
Collaborator

tvm-bot commented May 22, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@sergio-grovety
Copy link
Contributor Author

cc @neildhickey, @ekalda, @ilyag-grovety, @Aleksei-grovety @arina-grovety
Please review the PR

@sergio-grovety sergio-grovety force-pushed the ethosu-branching-inside-composite branch from ff1fd37 to 7836a42 Compare June 9, 2023 10:30
@sergio-grovety sergio-grovety changed the title [microNPU][ETHOSU] Added NPU offloading patterns for branching inside composite case [ETHOSU][MicroNPU][Pass] Add a pass to replicate pads Jun 9, 2023
@Aleksei-grovety
Copy link
Contributor

@tvm-bot rerun

Comment on lines 3689 to 3704
if op_pairs[0] == "depthwise":
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 1]
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
x1 = tf.nn.depthwise_conv2d(
x, weight, strides=tf_strides, padding=op_padding, dilations=dilation
)
else:
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 3]
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
x1 = tf.nn.conv2d(
x,
weight,
strides=tf_strides,
padding=op_padding,
dilations=dilation,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here the same code as on lines 3706 - 3721, it can be put into a separate function.

# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"Adds pads so that each conv2d operator has only one consumer"
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe "Adds pads to each conv2d operator so that each pad has only one consumer."?

new_conv2d_args = []
for i, arg in enumerate(call.args):
if i == 0:
new_conv2d_args.append(self.visit(new_pad))
Copy link
Contributor

Choose a reason for hiding this comment

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

new_pad is visited twice the first time on line 59.

@arina-grovety
Copy link
Contributor

@tvm-bot rerun

Copy link
Contributor

@ekalda ekalda left a comment

Choose a reason for hiding this comment

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

Thanks @sergio-grovety, great work! Bar @Aleksei-grovety suggestions, I only had one nit and two more general points:

  1. This pass is necessary due to how we do pattern matching for Ethos-U and is therefore coupled to Ethos-U integration, so I don't think it makes much sense to have it among generic Relay transformation passes. Maybe move it to somewhere into ethosu namespace, e.g. into codegen.
  2. Testing - while legalization test is the most important test for this kind of change, I think it would also be good to have a simple lightweight codegen test for this kind of graph topology. Since the pass creates a new conv2d, there is some risk of constants getting messed up, but a simple codegen test should eliminate that risk.


def __init__(self):
ExprMutator.__init__(self)
self.hashes = set()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Maybe add a comment what self.hashes represents

@arina-grovety arina-grovety force-pushed the ethosu-branching-inside-composite branch from bf520bd to 4678cbf Compare June 29, 2023 14:37
@sergio-grovety
Copy link
Contributor Author

@tvm-bot rerun

@sergio-grovety sergio-grovety requested a review from ekalda July 1, 2023 10:55
Copy link
Contributor

@ekalda ekalda left a comment

Choose a reason for hiding this comment

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

Almost there, one suggestion regarding to testing!

Comment on lines 160 to 171
@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
@pytest.mark.parametrize("ifm_shape", [(1, 55, 32, 3)])
@pytest.mark.parametrize(
"kernel_shape, activation_function",
[((3, 3), "RELU"), ((1, 2), "NONE")],
)
@pytest.mark.parametrize("strides, dilation", [((3, 2), (1, 1))])
@pytest.mark.parametrize("op_padding", ["SAME", "VALID"])
@pytest.mark.parametrize("sep_padding", [(0, 0, 1, 1), (7, 5, 4, 5)])
@pytest.mark.parametrize(
"op_pairs", [("conv2d", "conv2d"), ("depthwise", "depthwise"), ("conv2d", "depthwise")]
)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is 128 codegen tests, which would noticeably increase CI time. I think the variations on kernel_shape, activation_function, op_padding and sep_padding are exercised by other tests as well, so these values could be kept constant. Also, if we are parametrizing over one value, it doesn't need to be expressed with parametrize and can be included into the test code.

Copy link
Contributor

@ekalda ekalda left a comment

Choose a reason for hiding this comment

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

@ekalda ekalda merged commit 6c63e0d into apache:main Jul 17, 2023
@ekalda
Copy link
Contributor

ekalda commented Jul 17, 2023

Thanks all, this is merged now!

junrushao pushed a commit to junrushao/tvm that referenced this pull request Jul 24, 2023
Added a pass to to handle the situation when nn.pad operator has more than one qnn.conv2d consumer.

       pad
     /     \
 Conv2D    Conv2D

In this case, because of the peculiarities of pattern parsing, conv2d does not get into the composite for the NPU. Therefore, pads are added so that each has only one consumer.


---------

Co-authored-by: Sergey Smirnov <89378719+sergey-grovety@users.noreply.github.com>
Co-authored-by: Arina <117634809+arina-grovety@users.noreply.github.com>
Co-authored-by: arina.naumova <naumova@grovety.com>
junrushao pushed a commit to junrushao/tvm that referenced this pull request Jul 27, 2023
Added a pass to to handle the situation when nn.pad operator has more than one qnn.conv2d consumer.

       pad
     /     \
 Conv2D    Conv2D

In this case, because of the peculiarities of pattern parsing, conv2d does not get into the composite for the NPU. Therefore, pads are added so that each has only one consumer.


---------

Co-authored-by: Sergey Smirnov <89378719+sergey-grovety@users.noreply.github.com>
Co-authored-by: Arina <117634809+arina-grovety@users.noreply.github.com>
Co-authored-by: arina.naumova <naumova@grovety.com>
junrushao pushed a commit to junrushao/tvm that referenced this pull request Jul 30, 2023
Added a pass to to handle the situation when nn.pad operator has more than one qnn.conv2d consumer.

       pad
     /     \
 Conv2D    Conv2D

In this case, because of the peculiarities of pattern parsing, conv2d does not get into the composite for the NPU. Therefore, pads are added so that each has only one consumer.


---------

Co-authored-by: Sergey Smirnov <89378719+sergey-grovety@users.noreply.github.com>
Co-authored-by: Arina <117634809+arina-grovety@users.noreply.github.com>
Co-authored-by: arina.naumova <naumova@grovety.com>
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.

5 participants