You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2022/05/30 17:30:40 UTC

[GitHub] [incubator-mxnet] DominikaJedynak commented on a diff in pull request #21020: [FEATURE] Add property removing duplicate Cast operations

DominikaJedynak commented on code in PR #21020:
URL: https://github.com/apache/incubator-mxnet/pull/21020#discussion_r884977232


##########
src/operator/subgraph/dnnl/dnnl_remove_casts_property.h:
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file dnnl_remove_casts_property.h
+ * \brief Graph property for removing two unnecessary Cast operations
+ *
+ * ... -> Cast(bool) -> expand_dims -> Cast(bool) -> Cast(bool) -> ...
+ *                                  ||
+ *                                  \/
+ *                ... -> Cast(bool) -> expand_dims -> ...
+ */
+
+#ifndef MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_REMOVE_CASTS_PROPERTY_H_
+#define MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_REMOVE_CASTS_PROPERTY_H_
+
+#if MXNET_USE_ONEDNN == 1
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include "operator/subgraph/common.h"
+#include "dnnl_subgraph_base-inl.h"
+
+namespace mxnet {
+namespace op {
+
+class SgDNNLRemoveCastsSelector : public SubgraphSelectorV2 {
+ private:
+  enum CastStatus { kStart, kExpand, kCast, kSuccess, kFail };
+  CastStatus status_  = kStart;
+  const char* boolStr = "bool";
+
+ public:
+  bool Select(const BiDirectedNode& seed_node,
+              const std::shared_ptr<NodeAttr>& node_attr) override {
+    if (seed_node.node->op() == Op::Get("expand_dims") && seed_node.node->num_inputs() == 1 &&
+        seed_node.node->num_outputs() == 1) {
+      status_ = kExpand;
+      return true;
+    }
+    return false;
+  }
+
+  bool SelectInput(const BiDirectedNode& n, const BiDirectedNode& input_node) override {
+    if (input_node.node->op() != Op::Get("Cast")) {
+      status_ = kFail;
+    } else if (input_node.node->attrs.dict["dtype"] != boolStr) {
+      status_ = kFail;
+    }
+    return false;
+  }
+
+  bool SelectOutput(const BiDirectedNode& n, const BiDirectedNode& output_node) override {
+    if (status_ == kFail || status_ == kSuccess || output_node.node->is_variable()) {
+      return false;
+    }
+    if (output_node.node->op() == Op::Get("Cast")) {
+      if (output_node.node->attrs.dict["dtype"] == boolStr) {

Review Comment:
   Maybe it could be:
   ```suggestion
       if (output_node.node->op() == Op::Get("Cast")
           && output_node.node->attrs.dict["dtype"] == boolStr) {
   ```



##########
src/operator/subgraph/dnnl/dnnl_remove_casts_property.h:
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file dnnl_remove_casts_property.h
+ * \brief Graph property for removing two unnecessary Cast operations
+ *
+ * ... -> Cast(bool) -> expand_dims -> Cast(bool) -> Cast(bool) -> ...
+ *                                  ||
+ *                                  \/
+ *                ... -> Cast(bool) -> expand_dims -> ...
+ */
+
+#ifndef MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_REMOVE_CASTS_PROPERTY_H_
+#define MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_REMOVE_CASTS_PROPERTY_H_
+
+#if MXNET_USE_ONEDNN == 1
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include "operator/subgraph/common.h"
+#include "dnnl_subgraph_base-inl.h"
+
+namespace mxnet {
+namespace op {
+
+class SgDNNLRemoveCastsSelector : public SubgraphSelectorV2 {
+ private:
+  enum CastStatus { kStart, kExpand, kCast, kSuccess, kFail };
+  CastStatus status_  = kStart;

Review Comment:
   Is kStart status actually necessary? It seems that it is not used, maybe we could just set status_ = kFail in here?



##########
src/operator/subgraph/dnnl/dnnl_remove_casts_property.h:
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file dnnl_remove_casts_property.h
+ * \brief Graph property for removing two unnecessary Cast operations
+ *
+ * ... -> Cast(bool) -> expand_dims -> Cast(bool) -> Cast(bool) -> ...
+ *                                  ||
+ *                                  \/
+ *                ... -> Cast(bool) -> expand_dims -> ...
+ */
+
+#ifndef MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_REMOVE_CASTS_PROPERTY_H_
+#define MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_REMOVE_CASTS_PROPERTY_H_
+
+#if MXNET_USE_ONEDNN == 1
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include "operator/subgraph/common.h"
+#include "dnnl_subgraph_base-inl.h"
+
+namespace mxnet {
+namespace op {
+
+class SgDNNLRemoveCastsSelector : public SubgraphSelectorV2 {
+ private:
+  enum CastStatus { kStart, kExpand, kCast, kSuccess, kFail };
+  CastStatus status_  = kStart;
+  const char* boolStr = "bool";
+
+ public:
+  bool Select(const BiDirectedNode& seed_node,
+              const std::shared_ptr<NodeAttr>& node_attr) override {
+    if (seed_node.node->op() == Op::Get("expand_dims") && seed_node.node->num_inputs() == 1 &&
+        seed_node.node->num_outputs() == 1) {
+      status_ = kExpand;
+      return true;
+    }
+    return false;
+  }
+
+  bool SelectInput(const BiDirectedNode& n, const BiDirectedNode& input_node) override {
+    if (input_node.node->op() != Op::Get("Cast")) {
+      status_ = kFail;
+    } else if (input_node.node->attrs.dict["dtype"] != boolStr) {
+      status_ = kFail;
+    }
+    return false;
+  }
+
+  bool SelectOutput(const BiDirectedNode& n, const BiDirectedNode& output_node) override {
+    if (status_ == kFail || status_ == kSuccess || output_node.node->is_variable()) {
+      return false;
+    }
+    if (output_node.node->op() == Op::Get("Cast")) {
+      if (output_node.node->attrs.dict["dtype"] == boolStr) {
+        if (status_ == kExpand) {
+          if (output_node.node->num_outputs() == 1) {

Review Comment:
   ```suggestion
           if (status_ == kExpand && output_node.node->num_outputs() == 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: commits-unsubscribe@mxnet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org