You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "chunit-quic (via GitHub)" <gi...@apache.org> on 2023/05/17 06:05:53 UTC

[GitHub] [tvm] chunit-quic commented on a diff in pull request #14574: [IR][SIBuilder]

chunit-quic commented on code in PR #14574:
URL: https://github.com/apache/tvm/pull/14574#discussion_r1195922401


##########
include/tvm/ir/si_builder.h:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 tvm/ir/si_builder.h
+ * \brief build a source info during rewriting expressions.
+ */
+#ifndef TVM_IR_SI_BUILDER_H_
+#define TVM_IR_SI_BUILDER_H_
+
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/tir/stmt.h>
+
+#include <memory>
+#include <unordered_set>
+
+namespace tvm {
+
+/*!
+ * \brief SIBuilder provides helper APIs for filling spans,
+ *        particularly useful for one-to-many, many-to-one and many-to-many pass transformations.

Review Comment:
   Will change



##########
src/ir/si_builder.cc:
##########
@@ -0,0 +1,341 @@
+/*
+ * 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 tvm/ir/si_builder.cc
+ * \brief Implementation for building a source info during rewriting expressions.
+ */
+#include <tvm/ir/si_builder.h>
+#include <tvm/ir/transform.h>
+#include <tvm/tir/stmt_functor.h>
+
+#include <vector>
+
+namespace tvm {
+
+using RelayExprSet = std::unordered_set<relay::Expr, ObjectPtrHash, ObjectPtrEqual>;
+using PrimExprSet = std::unordered_set<PrimExpr, ObjectPtrHash, ObjectPtrEqual>;
+using StmtSet = std::unordered_set<tir::Stmt, ObjectPtrHash, ObjectPtrEqual>;
+
+class RelayCollapse : public relay::ExprVisitor {
+ public:
+  explicit RelayCollapse(const RelayExprSet& inputs = {}) : inputs_(inputs) {}
+
+  Span Collapse(const relay::Expr& entry);

Review Comment:
   Thanks a lot for the naming suggestion!
   CollectSpans is more straightforward.
   
   Will add documentations and change the name.



##########
src/ir/si_builder.cc:
##########
@@ -0,0 +1,341 @@
+/*
+ * 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 tvm/ir/si_builder.cc
+ * \brief Implementation for building a source info during rewriting expressions.
+ */
+#include <tvm/ir/si_builder.h>
+#include <tvm/ir/transform.h>
+#include <tvm/tir/stmt_functor.h>
+
+#include <vector>
+
+namespace tvm {
+
+using RelayExprSet = std::unordered_set<relay::Expr, ObjectPtrHash, ObjectPtrEqual>;
+using PrimExprSet = std::unordered_set<PrimExpr, ObjectPtrHash, ObjectPtrEqual>;
+using StmtSet = std::unordered_set<tir::Stmt, ObjectPtrHash, ObjectPtrEqual>;
+
+class RelayCollapse : public relay::ExprVisitor {
+ public:
+  explicit RelayCollapse(const RelayExprSet& inputs = {}) : inputs_(inputs) {}
+
+  Span Collapse(const relay::Expr& entry);
+
+  void VisitExpr(const relay::Expr& expr) final;
+
+ private:
+  Array<Span> spans_;
+  const RelayExprSet& inputs_;
+};
+
+void RelayCollapse::VisitExpr(const relay::Expr& expr) {
+  if (visit_counter_.count(expr.get())) {
+    return;
+  }
+  if (expr->span.defined()) {
+    spans_.push_back(expr->span);
+  }
+  if (inputs_.find(expr) != inputs_.end()) {
+    // becuase it returns directly, it should be recorded as visted manually.
+    visit_counter_.insert({expr.get(), 1});
+    return;
+  }
+  relay::ExprVisitor::VisitExpr(expr);
+}
+
+Span RelayCollapse::Collapse(const relay::Expr& entry) {
+  VisitExpr(entry);
+  return SequentialSpan(spans_);
+}
+
+class RelayRecursivelyFill : public relay::ExprMutator {
+ public:
+  explicit RelayRecursivelyFill(const Span& span, const RelayExprSet& inputs = {})
+      : span_(span), inputs_(inputs) {}
+
+  void Fill(const relay::Expr& entry);
+
+  relay::Expr VisitExpr(const relay::Expr& expr) final;
+
+ private:
+  const Span& span_;
+  const RelayExprSet& inputs_;
+};
+
+relay::Expr RelayRecursivelyFill::VisitExpr(const relay::Expr& expr) {
+  if (inputs_.find(expr) != inputs_.end()) {
+    return expr;
+  }
+  // Skip op node. Align with python frontend
+  if (!expr.as<OpNode>()) {
+    expr->span = span_;
+  }
+
+  return relay::ExprMutator::VisitExpr(expr);
+}
+
+void RelayRecursivelyFill::Fill(const relay::Expr& entry) { Mutate(entry); }
+
+class TirCollapse : public tir::StmtExprVisitor {
+ public:
+  explicit TirCollapse(const PrimExprSet& expr_inputs = {}, const StmtSet& stmt_inputs = {})
+      : expr_inputs_(expr_inputs), stmt_inputs_(stmt_inputs) {}
+
+  void VisitExpr(const PrimExpr& expr) final;
+  void VisitStmt(const tir::Stmt& stmt) final;
+
+  bool IsInput(const PrimExpr& expr);
+  bool IsInput(const tir::Stmt& stmt);
+
+  Span Collapse(const PrimExpr& expr);
+  Span Collapse(const tir::Stmt& stmt);
+
+ private:
+  Array<Span> spans_;
+  std::unordered_map<const Object*, size_t> visit_counter_;
+  const PrimExprSet& expr_inputs_;
+  const StmtSet& stmt_inputs_;
+};
+
+Span TirCollapse::Collapse(const PrimExpr& expr) {
+  operator()(expr);
+  return SequentialSpan(spans_);
+}
+
+Span TirCollapse::Collapse(const tir::Stmt& stmt) {
+  operator()(stmt);
+  return SequentialSpan(spans_);
+}
+
+bool TirCollapse::IsInput(const PrimExpr& expr) {
+  return expr_inputs_.find(expr) != expr_inputs_.end();
+}
+
+bool TirCollapse::IsInput(const tir::Stmt& stmt) {
+  return stmt_inputs_.find(stmt) != stmt_inputs_.end();
+}
+
+void TirCollapse::VisitExpr(const PrimExpr& expr) {
+  if (visit_counter_.count(expr.get())) {
+    return;
+  }
+  if (expr->span.defined()) {
+    spans_.push_back(expr->span);
+  }
+  if (IsInput(expr)) {
+    // becuase it returns directly, it should be recorded as visted manually.
+    visit_counter_.insert({expr.get(), 1});
+    return;
+  }
+  StmtExprVisitor::VisitExpr(expr);
+}
+
+void TirCollapse::VisitStmt(const tir::Stmt& stmt) {
+  if (visit_counter_.count(stmt.get())) {
+    return;
+  }
+  if (stmt->span.defined()) {
+    spans_.push_back(stmt->span);
+  }
+  if (IsInput(stmt)) {
+    // becuase it returns directly, it should be recorded as visted manually.
+    visit_counter_.insert({stmt.get(), 1});
+    return;
+  }
+  StmtExprVisitor::VisitStmt(stmt);
+}
+
+class TirRecursivelyFill : public tir::StmtExprMutator {
+ public:
+  TirRecursivelyFill(const Span& span, const PrimExprSet& expr_inputs = {},
+                     const StmtSet& stmt_inputs = {})
+      : span_(span), expr_inputs_(expr_inputs), stmt_inputs_(stmt_inputs) {}
+
+  tir::Stmt Fill(const tir::Stmt& s) { return operator()(s); }
+  PrimExpr Fill(const PrimExpr& e) { return operator()(e); }
+
+  bool IsInput(const PrimExpr& expr);
+  bool IsInput(const tir::Stmt& stmt);
+
+  PrimExpr VisitExpr(const PrimExpr& expr) final;
+  tir::Stmt VisitStmt(const tir::Stmt& stmt) final;
+
+ private:
+  const Span& span_;
+  const PrimExprSet& expr_inputs_;
+  const StmtSet& stmt_inputs_;
+};
+
+tir::Stmt TirRecursivelyFill::VisitStmt(const tir::Stmt& stmt) {
+  if (IsInput(stmt)) {
+    return stmt;
+  }
+  stmt->span = span_;
+  return StmtExprMutator::VisitStmt(stmt);
+}
+
+bool TirRecursivelyFill::IsInput(const PrimExpr& expr) {
+  return expr_inputs_.find(expr) != expr_inputs_.end();
+}
+
+bool TirRecursivelyFill::IsInput(const tir::Stmt& stmt) {
+  return stmt_inputs_.find(stmt) != stmt_inputs_.end();
+}
+
+PrimExpr TirRecursivelyFill::VisitExpr(const PrimExpr& expr) {
+  if (IsInput(expr)) {
+    return expr;
+  }
+  expr->span = span_;
+  return StmtExprMutator::VisitExpr(expr);
+}
+
+struct SIBuilder::Impl {
+  virtual Span CreateSpan() const = 0;
+  virtual void RecursivelyFillSpan(const relay::Expr& entry, const RelayExprSet& inputs) const = 0;
+  virtual void RecursivelyFillSpan(const PrimExpr& entry, const PrimExprSet& inputs) const = 0;
+  virtual void RecursivelyFillSpan(const tir::Stmt& entry, const PrimExprSet& inputs) const = 0;
+  virtual void RecursivelyFillSpan(const tir::Stmt& entry, const StmtSet& inputs) const = 0;
+  virtual void CollapseSpan(const relay::Expr& entry, const RelayExprSet& inputs) = 0;
+  virtual void CollapseSpan(const PrimExpr& entry, const PrimExprSet& inputs) = 0;
+  virtual void CollapseSpan(const tir::Stmt& entry, const PrimExprSet& inputs) = 0;
+  virtual void CollapseSpan(const tir::Stmt& entry, const StmtSet& inputs) = 0;
+};
+
+SIBuilder::~SIBuilder() = default;
+
+Span SIBuilder::CreateSpan() const { return impl_->CreateSpan(); }
+
+template <>
+void SIBuilder::RecursivelyFillSpan(const relay::Expr& entry, const RelayExprSet& inputs) const {
+  impl_->RecursivelyFillSpan(entry, inputs);
+}
+
+template <>
+void SIBuilder::RecursivelyFillSpan(const PrimExpr& entry, const PrimExprSet& inputs) const {
+  impl_->RecursivelyFillSpan(entry, inputs);
+}
+
+void SIBuilder::RecursivelyFillSpan(const tir::Stmt& entry, const PrimExprSet& inputs) const {
+  impl_->RecursivelyFillSpan(entry, inputs);
+}
+
+void SIBuilder::RecursivelyFillSpan(const tir::Stmt& entry, const StmtSet& inputs) const {
+  impl_->RecursivelyFillSpan(entry, inputs);
+}
+
+std::unique_ptr<SIBuilder::Impl> SIBuilder::CreateImpl(const Span& span) {
+  struct NullImpl : public SIBuilder::Impl {

Review Comment:
   Thank you for pointing out! In case I misunderstand what you said. Please allow me to briefly describe what we going to do, and feel free to correct me if any. :D
   
   1. Delete base (virtual) class ` SIBuilder::Impl`
   2. Use the NullImpl as the parent(default) class for SIBuilder::Impl
   3. Create a class with functional implementations, which is derived from NullImpl class
   4. When `enable_si_builder` is not enable, use the NullImpl as _Impl driectly. otherwise assign the functional one to the _impl.
   
   We can remove some redundant codes by doing so.



##########
include/tvm/ir/si_builder.h:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 tvm/ir/si_builder.h
+ * \brief build a source info during rewriting expressions.
+ */
+#ifndef TVM_IR_SI_BUILDER_H_
+#define TVM_IR_SI_BUILDER_H_
+
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/tir/stmt.h>
+
+#include <memory>
+#include <unordered_set>
+
+namespace tvm {
+
+/*!
+ * \brief SIBuilder provides helper APIs for filling spans,
+ *        particularly useful for one-to-many, many-to-one and many-to-many pass transformations.
+ */
+class SIBuilder {

Review Comment:
   Will add to comments. It stands for source information builder.



##########
python/tvm/ir/base.py:
##########
@@ -69,6 +69,20 @@ def __init__(self, source_name, line, end_line, column, end_column):
         )
 
 
+@register_object("SequentialSpan")
+class SequentialSpan(Object):
+    """Specifies a location in a source program.

Review Comment:
   No problem, will update the description.
   
   About being exposed to Python side, it would be quite helpful in the following situations:
   1. Writing relay passes tests cases in ${tvm}/tests/python/relay/test_pass*. 
   2. User can set a span to their custom python passes for those own many-to-one expression transformations.
   



##########
include/tvm/ir/si_builder.h:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 tvm/ir/si_builder.h
+ * \brief build a source info during rewriting expressions.
+ */
+#ifndef TVM_IR_SI_BUILDER_H_
+#define TVM_IR_SI_BUILDER_H_
+
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/tir/stmt.h>
+
+#include <memory>
+#include <unordered_set>
+
+namespace tvm {
+
+/*!
+ * \brief SIBuilder provides helper APIs for filling spans,
+ *        particularly useful for one-to-many, many-to-one and many-to-many pass transformations.
+ */
+class SIBuilder {
+ public:
+  /*!
+   * \brief Create SIBuilder from a given span
+   */
+  explicit SIBuilder(const Span& span = Span());
+
+  /*!
+   * \brief Create SIBuilder from a given span sequence
+   */
+  explicit SIBuilder(const Array<Span>& spans = Array<Span>());
+  explicit SIBuilder(const std::initializer_list<Span>& init);
+
+  /*!
+   * \brief Create SIBuilder via a subgraph,
+   *        Will construct span based on the exprs in the subgraph. Including the inputs exprs.
+   *
+   * \param entry Entry expr for subgraph
+   * \param inputs End exprs for subgraph
+   */
+  template <typename T, typename = std::enable_if_t<std::is_base_of<BaseExpr, T>::value>>
+  explicit SIBuilder(const T& entry, const tvm::Array<T>& inputs = {});
+  explicit SIBuilder(const tir::Stmt& entry, const tvm::Array<PrimExpr>& inputs = {});
+  explicit SIBuilder(const tir::Stmt& entry, const tvm::Array<tir::Stmt>& inputs = {});
+
+  ~SIBuilder();
+
+  SIBuilder(const SIBuilder&) = delete;
+  SIBuilder& operator=(const SIBuilder&) = delete;
+
+  /*!
+   * \brief create new source info based on the given span or subgraph.

Review Comment:
   Sorry for the ambiguous naming. Would it be better if we change function name and comments as following?
   ```c++
     /*!
      * \brief build a span of source information, which is based on the given span or subgraph.
      *
      * \return the built span 
      */
     Span Build() const;
   ```



-- 
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@tvm.apache.org

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