junrushao1994 commented on a change in pull request #8615:
URL: https://github.com/apache/tvm/pull/8615#discussion_r681357684



##########
File path: include/tvm/tir/schedule/trace.h
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.
+ */
+#ifndef TVM_TIR_SCHEDULE_TRACE_H_
+#define TVM_TIR_SCHEDULE_TRACE_H_
+
+#include <tvm/tir/schedule/instruction.h>
+
+namespace tvm {
+namespace tir {
+
+// Forward declaration
+class Trace;
+
+/*!
+ * \brief A callback that allows users to mutate decisions on the fly
+ * when applying instructions. The signature of the callback is:
+ * \param inst The instruction
+ * \param inputs The input random variables
+ * \param attrs The attributes
+ * \param decision The original decision
+ * \return A new decision
+ */
+using FTraceDecisionProvider = runtime::TypedPackedFunc<ObjectRef(
+    const Instruction& inst, const Array<ObjectRef>& inputs, const 
Array<ObjectRef>& attrs,
+    const Optional<ObjectRef>& decision)>;
+
+/*!
+ * \brief An execution trace of a scheduling program
+ *
+ * A trace has two parts:
+ * 1) The instructions invoked so far in the program execution
+ * 2) The random decisions made upon those instructions, if any
+ *
+ * A trace can be serialized to:
+ * 1) Roundtrippable JSON format: can be saved to file and loaded back
+ * 2) Python syntax: allows users to copy-paste the trace to reproduce the 
scheduling process
+ *
+ * A trace can be applied to a TensorIR schedule by re-applying all its 
instructions possibly with
+ * their decisions accordingly. Re-sampling is invoked if a sampling 
instruction doesn't have its
+ * corresponding decision; Otherwise the existing decision will be reused 
accordingly.
+ */
+class TraceNode : public runtime::Object {
+ public:
+  /*! \brief The instructions invoked so far in the program execution */
+  Array<Instruction> insts;
+  /*! \brief The random decisions made upon those instructions */
+  Map<Instruction, ObjectRef> decisions;
+
+  void VisitAttrs(tvm::AttrVisitor* v) {
+    v->Visit("insts", &insts);
+    v->Visit("decisions", &decisions);
+  }
+
+  static constexpr const char* _type_key = "tir.Trace";
+  TVM_DECLARE_FINAL_OBJECT_INFO(TraceNode, runtime::Object);
+
+ public:
+  /*!
+   * \brief Retrieve the decision made on a specific instruction
+   * \param inst The instruction whose decision is to be retrieved
+   * \return The corresponding decision; NullOpt if there is no decision made 
on the instruction
+   */
+  Optional<ObjectRef> GetDecision(const Instruction& inst) const;
+  /*!
+   * \brief Append a new instruction to the trace
+   * \param inst The new instruction to be appended
+   */
+  void Append(Instruction inst);

Review comment:
       To clarify, It is a pattern in TVM that the IR is immutable, where COW 
is applied, but other data structures are allowed to be mutable.
   
   More context: TVM provides a macro `TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS` 
in its core object system for users to define mutable objects.
   
   For example, there are a lot of data structures in TVM codebase that are not 
COW for efficiency:
   - te::Schedule
   - AutoScheduler's cost model
   - Relay operator strategy
   - ...
   




-- 
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