Hzfengsy commented on code in PR #15500:
URL: https://github.com/apache/tvm/pull/15500#discussion_r1286492526


##########
python/tvm/contrib/msc/core/transform/pattern.py:
##########
@@ -0,0 +1,490 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=unused-argument
+"""tvm.contrib.msc.core.transform.pattern"""
+
+import tvm
+from tvm.relax.dpl import pattern as relax_pattern
+from tvm.relay import dataflow_pattern as relay_pattern
+
+from tvm.relax.transform import PatternCheckContext
+from tvm.relax.backend.pattern_registry import register_patterns
+from tvm.relay.op.contrib.register import register_pattern_table
+
+
+def make_relax_conv_bias_pattern(op_name):

Review Comment:
   Please add type annotation if possible.
   ```suggestion
   def make_relax_conv_bias_pattern(op_name: str) -> Tuple[bool, Dict[str, 
Pattern]:
   ```
   



##########
python/tvm/contrib/msc/core/transform/pattern.py:
##########
@@ -0,0 +1,490 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=unused-argument
+"""tvm.contrib.msc.core.transform.pattern"""
+
+import tvm
+from tvm.relax.dpl import pattern as relax_pattern
+from tvm.relay import dataflow_pattern as relay_pattern
+
+from tvm.relax.transform import PatternCheckContext
+from tvm.relax.backend.pattern_registry import register_patterns
+from tvm.relay.op.contrib.register import register_pattern_table
+
+
+def make_relax_conv_bias_pattern(op_name):
+    """A simple utility to create patterns for an operation fused with bias.
+
+    Parameters
+    ----------
+    op_name: str
+        The name of a Relax op, such as "relax.nn.conv2d"
+
+    Returns
+    -------
+    pattern: DFPattern
+        The resulting pattern describing a conv_bias operation
+    """
+
+    data = relax_pattern.wildcard()
+    weight = relax_pattern.is_const()
+    conv = relax_pattern.is_op(op_name)(data, weight)
+    bias = relax_pattern.is_const()
+    shape = relax_pattern.wildcard()
+    reshape = relax_pattern.is_op("relax.reshape")(bias, shape)
+    out = relax_pattern.is_op("relax.add")(conv, reshape)
+    annotations = {"bias": bias, "reshape": reshape}
+    return out, annotations
+
+
+def _check_relax_conv_bias(context: PatternCheckContext) -> bool:
+    """Check if conv_bias fuse pattern is correct."""
+    bias = context.annotated_expr["bias"]
+    reshape = context.annotated_expr["reshape"]
+    non_one_dims = len([i for i in reshape.struct_info.shape.values if i > 1])
+    return non_one_dims <= 1 and bias.struct_info.ndim == 1
+
+
+def make_relax_linear_pattern():

Review Comment:
   Type annotation



##########
python/tvm/contrib/msc/core/transform/pattern.py:
##########
@@ -0,0 +1,490 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=unused-argument
+"""tvm.contrib.msc.core.transform.pattern"""
+
+import tvm
+from tvm.relax.dpl import pattern as relax_pattern
+from tvm.relay import dataflow_pattern as relay_pattern
+
+from tvm.relax.transform import PatternCheckContext
+from tvm.relax.backend.pattern_registry import register_patterns
+from tvm.relay.op.contrib.register import register_pattern_table
+
+
+def make_relax_conv_bias_pattern(op_name):
+    """A simple utility to create patterns for an operation fused with bias.
+
+    Parameters
+    ----------
+    op_name: str
+        The name of a Relax op, such as "relax.nn.conv2d"
+
+    Returns
+    -------
+    pattern: DFPattern

Review Comment:
   Looks like it's not correct



##########
python/tvm/contrib/msc/core/transform/pattern.py:
##########
@@ -0,0 +1,490 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=unused-argument
+"""tvm.contrib.msc.core.transform.pattern"""
+
+import tvm
+from tvm.relax.dpl import pattern as relax_pattern
+from tvm.relay import dataflow_pattern as relay_pattern
+
+from tvm.relax.transform import PatternCheckContext
+from tvm.relax.backend.pattern_registry import register_patterns
+from tvm.relay.op.contrib.register import register_pattern_table
+
+
+def make_relax_conv_bias_pattern(op_name):
+    """A simple utility to create patterns for an operation fused with bias.
+
+    Parameters
+    ----------
+    op_name: str
+        The name of a Relax op, such as "relax.nn.conv2d"
+
+    Returns
+    -------
+    pattern: DFPattern
+        The resulting pattern describing a conv_bias operation
+    """
+
+    data = relax_pattern.wildcard()
+    weight = relax_pattern.is_const()
+    conv = relax_pattern.is_op(op_name)(data, weight)
+    bias = relax_pattern.is_const()
+    shape = relax_pattern.wildcard()
+    reshape = relax_pattern.is_op("relax.reshape")(bias, shape)
+    out = relax_pattern.is_op("relax.add")(conv, reshape)
+    annotations = {"bias": bias, "reshape": reshape}
+    return out, annotations
+
+
+def _check_relax_conv_bias(context: PatternCheckContext) -> bool:
+    """Check if conv_bias fuse pattern is correct."""
+    bias = context.annotated_expr["bias"]
+    reshape = context.annotated_expr["reshape"]
+    non_one_dims = len([i for i in reshape.struct_info.shape.values if i > 1])
+    return non_one_dims <= 1 and bias.struct_info.ndim == 1
+
+
+def make_relax_linear_pattern():
+    """A simple utility to create patterns for linear.
+
+    Returns
+    -------
+    pattern: DFPattern
+        The resulting pattern describing a linear operation
+    """
+
+    data = relax_pattern.wildcard()
+    weight = relax_pattern.is_const()
+    permute = relax_pattern.is_op("relax.permute_dims")(weight)
+    out = relax_pattern.is_op("relax.matmul")(data, permute)
+    annotations = {"weight": weight, "permute": permute}
+    return out, annotations
+
+
+def _check_relax_linear(context: PatternCheckContext) -> bool:
+    """Check if linear pattern is correct."""
+    weight = context.annotated_expr["weight"]
+    permute = context.annotated_expr["permute"]
+    return weight.struct_info.ndim == 2 and not permute.attrs["axes"]
+
+
+def make_relax_linear_bias_pattern():

Review Comment:
   +1



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to