tkonolige commented on a change in pull request #8615: URL: https://github.com/apache/tvm/pull/8615#discussion_r681325567
########## File path: include/tvm/tir/schedule/instruction.h ########## @@ -0,0 +1,288 @@ +/* + * 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_INSTRUCTION_H_ +#define TVM_TIR_SCHEDULE_INSTRUCTION_H_ + +#include <tvm/node/reflection.h> + +#include <utility> + +namespace tvm { + +// Forward declaration +template <typename, typename> +class AttrRegistry; + +namespace tir { + +// Forward declaration +class Schedule; + +/*! + * \brief Type of the functor that applies the instruction to a TensorIR schedule + * \param sch The schedule to be applied on + * \param inputs The input random variables + * \param attrs Instruction attributes + * \param decision Decisions made on the instruction + * \return The functor returns an array of output random variables + */ +using FInstructionApply = runtime::TypedPackedFunc<Array<ObjectRef>( + Schedule sch, const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision)>; + +/*! + * \brief Type of the functor that converts the instruction to a statement in python syntax + * \param inputs Names of the input random variables + * \param attrs Instruction attributes + * \param decisions Decisions made on the instruction + * \param outputs Names of the output random variables + * \return A string representing the python api call + */ +using FInstructionAsPython = runtime::TypedPackedFunc<String( + const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision, const Array<String>& outputs)>; + +/*! + * \brief Type of the functor that serialize its attributes to JSON + * \param attrs The attributes to be serialized + * \return An array, serialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsAsJSON = runtime::TypedPackedFunc<ObjectRef(Array<ObjectRef> attrs)>; + +/*! + * \brief Type of the functor that deserialize its attributes from JSON + * \param json_attrs The attributes to be serialized + * \return An array, deserialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsFromJSON = runtime::TypedPackedFunc<Array<ObjectRef>(ObjectRef json_attrs)>; + +/*! + * \brief Kind of an instruction, e.g. Split, Reorder, etc. + * Besides the name, every kind of instruction has its own properties, including: + * 1) A boolean indicating if the instruction is pure, i.e. change nothing in the schedule state + * 2) A functor that applies the instruction to a TensorIR schedule + * 3) A functor that converts the instruction to a statement in python syntax + * 4) A functor that serialize its attributes to JSON + * 5) A functor that deserialize its attributes from JSON + * + * Unlike `tvm::OpNode`, `InstructionKindNode` doesn't support unstructured properties, + * mainly because there is no such usecase yet to add any other property. + */ +class InstructionKindNode : public runtime::Object { + public: + /*! \brief The name of a kind of instructions */ + String name; + /*! + * \brief Indicates if the instruction is pure, i.e. removing it alone doesn't mutate the schedule + * state. For example, the instruction `GetBlock` is pure because it changes + * nothing, while `ComputeInline` is not because removing it leads to a different resulting + * schedule. + */ + bool is_pure{false}; + /*! \brief A functor that applies the instruction to a TensorIR schedule */ + FInstructionApply f_apply_to_schedule{nullptr}; Review comment: Why are all these members typed packed functions and not member functions? ########## File path: include/tvm/tir/schedule/instruction.h ########## @@ -0,0 +1,288 @@ +/* + * 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_INSTRUCTION_H_ +#define TVM_TIR_SCHEDULE_INSTRUCTION_H_ + +#include <tvm/node/reflection.h> + +#include <utility> + +namespace tvm { + +// Forward declaration +template <typename, typename> +class AttrRegistry; + +namespace tir { + +// Forward declaration +class Schedule; + +/*! + * \brief Type of the functor that applies the instruction to a TensorIR schedule + * \param sch The schedule to be applied on + * \param inputs The input random variables + * \param attrs Instruction attributes + * \param decision Decisions made on the instruction + * \return The functor returns an array of output random variables + */ +using FInstructionApply = runtime::TypedPackedFunc<Array<ObjectRef>( + Schedule sch, const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision)>; + +/*! + * \brief Type of the functor that converts the instruction to a statement in python syntax + * \param inputs Names of the input random variables + * \param attrs Instruction attributes + * \param decisions Decisions made on the instruction + * \param outputs Names of the output random variables + * \return A string representing the python api call + */ +using FInstructionAsPython = runtime::TypedPackedFunc<String( + const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision, const Array<String>& outputs)>; + +/*! + * \brief Type of the functor that serialize its attributes to JSON + * \param attrs The attributes to be serialized + * \return An array, serialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsAsJSON = runtime::TypedPackedFunc<ObjectRef(Array<ObjectRef> attrs)>; + +/*! + * \brief Type of the functor that deserialize its attributes from JSON + * \param json_attrs The attributes to be serialized + * \return An array, deserialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsFromJSON = runtime::TypedPackedFunc<Array<ObjectRef>(ObjectRef json_attrs)>; + +/*! + * \brief Kind of an instruction, e.g. Split, Reorder, etc. + * Besides the name, every kind of instruction has its own properties, including: + * 1) A boolean indicating if the instruction is pure, i.e. change nothing in the schedule state + * 2) A functor that applies the instruction to a TensorIR schedule + * 3) A functor that converts the instruction to a statement in python syntax + * 4) A functor that serialize its attributes to JSON + * 5) A functor that deserialize its attributes from JSON + * + * Unlike `tvm::OpNode`, `InstructionKindNode` doesn't support unstructured properties, + * mainly because there is no such usecase yet to add any other property. + */ +class InstructionKindNode : public runtime::Object { + public: + /*! \brief The name of a kind of instructions */ + String name; + /*! + * \brief Indicates if the instruction is pure, i.e. removing it alone doesn't mutate the schedule + * state. For example, the instruction `GetBlock` is pure because it changes + * nothing, while `ComputeInline` is not because removing it leads to a different resulting + * schedule. + */ + bool is_pure{false}; Review comment: What is the purpose of marking a function as pure? ########## 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: Given most internal datastructures are COW, should this be too? ########## 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); + /*! + * \brief Append a new instruction with a random decision to the trace + * \param inst The new instruction to be appended + * \param decision The random decision made on this instruction + * The type of `decision` depends on the instruction, e.g. + * the decision of `SamplePerfectTile` has type `Array<IntImm>` + */ + void Append(Instruction inst, ObjectRef decision); + /*! + * \brief Remove the last instruction, along with the decision made on that instruction, if any + * \return The instruction removed; NullOpt if the trace is empty + */ + Optional<Instruction> Pop(); + /*! + * \brief Apply the trace to a TensorIR schedule + * \param sch The schedule to be applied onto + * \param remove_postproc If postprocessing instructions are removed + * \param decision_provider A callback that allows users to mutate decisions on the fly + * when applying instructions. + * \sa FTraceDecisionProvider + */ + void ApplyToSchedule(Schedule sch, bool remove_postproc, + FTraceDecisionProvider decision_provider = nullptr) const; Review comment: Callback can often lead to spaghetti code. Instead of providing a callback, can the flow to do this just be that you modify the trace to contain your decisions and then you apply it to the schedule? ########## 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 Review comment: What does "possibly with their decisions accordingly" mean? Is it optional to use the original decisions? Does re-sampling mean you are choosing a new decision? ########## 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; Review comment: If decisions can be arbitrary objects, how do we guarantee that a Trace is serializable? ########## File path: src/tir/schedule/trace.cc ########## @@ -0,0 +1,533 @@ +/* + * 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. + */ +#include "./utils.h" + +namespace tvm { +namespace tir { + +/**************** Constructors ****************/ + +Trace::Trace() { data_ = make_object<TraceNode>(); } + +Trace::Trace(Array<Instruction> insts, Map<Instruction, ObjectRef> decisions) { + ObjectPtr<TraceNode> n = make_object<TraceNode>(); + n->insts = std::move(insts); + n->decisions = std::move(decisions); + data_ = std::move(n); +} + +/**************** Utilities ****************/ + +bool IsPostproc(const InstructionKind& inst_kind) { + static InstructionKind inst_enter_postproc = InstructionKind::Get("EnterPostproc"); + return inst_kind.same_as(inst_enter_postproc); +} + +int GetNumValidInstructions(const Array<Instruction>& insts, bool remove_postproc) { + if (!remove_postproc) { + return insts.size(); + } + int n_insts = 0; + for (const Instruction& inst : insts) { + if (!IsPostproc(inst->kind)) { + ++n_insts; + } else { + break; + } + } + return n_insts; +} + +/**************** TranslateInputRVs ****************/ + +Array<ObjectRef> TranslateInputRVs(const Array<ObjectRef>& inputs, + const std::unordered_map<const Object*, const Object*>& rv_map) { + Array<ObjectRef> result; + result.reserve(inputs.size()); + for (const ObjectRef& input : inputs) { + if (!input.defined() || // constant: nullptr + input->IsInstance<StringObj>() || // constant: string + input->IsInstance<IntImmNode>() || // constant: integer + input->IsInstance<FloatImmNode>()) { // constant: float + result.push_back(input); + } else if (input->IsInstance<BlockRVNode>() || // RV: block + input->IsInstance<LoopRVNode>() || // RV: loop + input->IsInstance<VarNode>()) { // RV: var + auto it = rv_map.find(input.get()); + ICHECK(it != rv_map.end()) << "IndexError: Random variable doesn't exist: " << input; + result.push_back(GetRef<ObjectRef>(it->second)); + } else if (const auto* expr = input.as<PrimExprNode>()) { // RV: Expr + result.push_back( + Substitute(GetRef<PrimExpr>(expr), [&rv_map](const Var& var) -> Optional<PrimExpr> { + auto it = rv_map.find(var.get()); + if (it == rv_map.end()) { + return NullOpt; + } + const Object* dst = it->second; + ICHECK(dst->IsInstance<VarNode>()) + << "TypeError: Expect 'tir.Var', but gets: " << dst->GetTypeKey(); + return GetRef<Var>(static_cast<const VarNode*>(dst)); + })); + } else { + ICHECK(false) << "TypeError: Cannot recognize the type of an input random variable: " Review comment: Use LOG(FATAL) ########## File path: include/tvm/tir/schedule/instruction.h ########## @@ -0,0 +1,288 @@ +/* + * 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_INSTRUCTION_H_ +#define TVM_TIR_SCHEDULE_INSTRUCTION_H_ + +#include <tvm/node/reflection.h> + +#include <utility> + +namespace tvm { + +// Forward declaration +template <typename, typename> +class AttrRegistry; + +namespace tir { + +// Forward declaration +class Schedule; + +/*! + * \brief Type of the functor that applies the instruction to a TensorIR schedule + * \param sch The schedule to be applied on + * \param inputs The input random variables + * \param attrs Instruction attributes + * \param decision Decisions made on the instruction + * \return The functor returns an array of output random variables + */ +using FInstructionApply = runtime::TypedPackedFunc<Array<ObjectRef>( + Schedule sch, const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision)>; + +/*! + * \brief Type of the functor that converts the instruction to a statement in python syntax + * \param inputs Names of the input random variables + * \param attrs Instruction attributes + * \param decisions Decisions made on the instruction + * \param outputs Names of the output random variables + * \return A string representing the python api call + */ +using FInstructionAsPython = runtime::TypedPackedFunc<String( + const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision, const Array<String>& outputs)>; + +/*! + * \brief Type of the functor that serialize its attributes to JSON + * \param attrs The attributes to be serialized + * \return An array, serialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsAsJSON = runtime::TypedPackedFunc<ObjectRef(Array<ObjectRef> attrs)>; + +/*! + * \brief Type of the functor that deserialize its attributes from JSON + * \param json_attrs The attributes to be serialized + * \return An array, deserialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsFromJSON = runtime::TypedPackedFunc<Array<ObjectRef>(ObjectRef json_attrs)>; + +/*! + * \brief Kind of an instruction, e.g. Split, Reorder, etc. + * Besides the name, every kind of instruction has its own properties, including: + * 1) A boolean indicating if the instruction is pure, i.e. change nothing in the schedule state + * 2) A functor that applies the instruction to a TensorIR schedule + * 3) A functor that converts the instruction to a statement in python syntax + * 4) A functor that serialize its attributes to JSON + * 5) A functor that deserialize its attributes from JSON + * + * Unlike `tvm::OpNode`, `InstructionKindNode` doesn't support unstructured properties, + * mainly because there is no such usecase yet to add any other property. + */ +class InstructionKindNode : public runtime::Object { + public: + /*! \brief The name of a kind of instructions */ + String name; + /*! + * \brief Indicates if the instruction is pure, i.e. removing it alone doesn't mutate the schedule + * state. For example, the instruction `GetBlock` is pure because it changes + * nothing, while `ComputeInline` is not because removing it leads to a different resulting + * schedule. + */ + bool is_pure{false}; + /*! \brief A functor that applies the instruction to a TensorIR schedule */ + FInstructionApply f_apply_to_schedule{nullptr}; + /*! \brief A functor that converts the instruction to a statement in python syntax */ + FInstructionAsPython f_as_python{nullptr}; + /*! + * \brief A functor that serialize its attributes to JSON + * \note If the functor is null, it means no conversion is needed + */ + FInstructionAttrsAsJSON f_attrs_as_json{nullptr}; + /*! + * \brief A functor that deserialize its attributes from JSON + * \note If the functor is null, it means no conversion is needed + */ + FInstructionAttrsFromJSON f_attrs_from_json{nullptr}; + + void VisitAttrs(tvm::AttrVisitor* v) { + v->Visit("name", &name); + v->Visit("_is_pure", &is_pure); + // not visited: f_apply_to_schedule + // not visited: f_as_python + // not visited: f_attrs_as_json + // not visited: f_attrs_from_json + } + + static constexpr const char* _type_key = "tir.InstructionKind"; + TVM_DECLARE_FINAL_OBJECT_INFO(InstructionKindNode, runtime::Object); +}; + +/*! + * \brief Managed reference to InstructionKindNode + * \sa InstructionKindNode + */ +class InstructionKind : public runtime::ObjectRef { + public: + /*! + * \brief Retrieve an InstructionKind using its name + * \param name The registered name of the InstructionKind + * \return The InstructionKind retrieved + */ + static InstructionKind Get(const String& name); + TVM_DEFINE_OBJECT_REF_METHODS(InstructionKind, runtime::ObjectRef, InstructionKindNode); +}; + +/*! \brief Schedule instructions each corresponds to a schedule primitive */ +class InstructionNode : public runtime::Object { + public: + /*! \brief The kind of the instruction */ + InstructionKind kind; Review comment: What is the purpose of giving each instruction a kind. Why node use subclassing instead? ########## File path: include/tvm/tir/schedule/instruction.h ########## @@ -0,0 +1,288 @@ +/* + * 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_INSTRUCTION_H_ +#define TVM_TIR_SCHEDULE_INSTRUCTION_H_ + +#include <tvm/node/reflection.h> + +#include <utility> + +namespace tvm { + +// Forward declaration +template <typename, typename> +class AttrRegistry; + +namespace tir { + +// Forward declaration +class Schedule; + +/*! + * \brief Type of the functor that applies the instruction to a TensorIR schedule + * \param sch The schedule to be applied on + * \param inputs The input random variables + * \param attrs Instruction attributes + * \param decision Decisions made on the instruction + * \return The functor returns an array of output random variables + */ +using FInstructionApply = runtime::TypedPackedFunc<Array<ObjectRef>( + Schedule sch, const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision)>; + +/*! + * \brief Type of the functor that converts the instruction to a statement in python syntax + * \param inputs Names of the input random variables + * \param attrs Instruction attributes + * \param decisions Decisions made on the instruction + * \param outputs Names of the output random variables + * \return A string representing the python api call + */ +using FInstructionAsPython = runtime::TypedPackedFunc<String( + const Array<ObjectRef>& inputs, const Array<ObjectRef>& attrs, + const Optional<ObjectRef>& decision, const Array<String>& outputs)>; + +/*! + * \brief Type of the functor that serialize its attributes to JSON + * \param attrs The attributes to be serialized + * \return An array, serialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsAsJSON = runtime::TypedPackedFunc<ObjectRef(Array<ObjectRef> attrs)>; + +/*! + * \brief Type of the functor that deserialize its attributes from JSON + * \param json_attrs The attributes to be serialized + * \return An array, deserialized attributes + * \note This functor is nullable + */ +using FInstructionAttrsFromJSON = runtime::TypedPackedFunc<Array<ObjectRef>(ObjectRef json_attrs)>; + +/*! + * \brief Kind of an instruction, e.g. Split, Reorder, etc. + * Besides the name, every kind of instruction has its own properties, including: + * 1) A boolean indicating if the instruction is pure, i.e. change nothing in the schedule state + * 2) A functor that applies the instruction to a TensorIR schedule + * 3) A functor that converts the instruction to a statement in python syntax + * 4) A functor that serialize its attributes to JSON + * 5) A functor that deserialize its attributes from JSON + * + * Unlike `tvm::OpNode`, `InstructionKindNode` doesn't support unstructured properties, + * mainly because there is no such usecase yet to add any other property. + */ +class InstructionKindNode : public runtime::Object { + public: + /*! \brief The name of a kind of instructions */ + String name; + /*! + * \brief Indicates if the instruction is pure, i.e. removing it alone doesn't mutate the schedule + * state. For example, the instruction `GetBlock` is pure because it changes + * nothing, while `ComputeInline` is not because removing it leads to a different resulting + * schedule. + */ + bool is_pure{false}; + /*! \brief A functor that applies the instruction to a TensorIR schedule */ + FInstructionApply f_apply_to_schedule{nullptr}; + /*! \brief A functor that converts the instruction to a statement in python syntax */ + FInstructionAsPython f_as_python{nullptr}; + /*! + * \brief A functor that serialize its attributes to JSON + * \note If the functor is null, it means no conversion is needed + */ + FInstructionAttrsAsJSON f_attrs_as_json{nullptr}; + /*! + * \brief A functor that deserialize its attributes from JSON + * \note If the functor is null, it means no conversion is needed + */ + FInstructionAttrsFromJSON f_attrs_from_json{nullptr}; + + void VisitAttrs(tvm::AttrVisitor* v) { + v->Visit("name", &name); + v->Visit("_is_pure", &is_pure); + // not visited: f_apply_to_schedule + // not visited: f_as_python + // not visited: f_attrs_as_json + // not visited: f_attrs_from_json + } + + static constexpr const char* _type_key = "tir.InstructionKind"; + TVM_DECLARE_FINAL_OBJECT_INFO(InstructionKindNode, runtime::Object); +}; + +/*! + * \brief Managed reference to InstructionKindNode + * \sa InstructionKindNode + */ +class InstructionKind : public runtime::ObjectRef { + public: + /*! + * \brief Retrieve an InstructionKind using its name + * \param name The registered name of the InstructionKind + * \return The InstructionKind retrieved + */ + static InstructionKind Get(const String& name); + TVM_DEFINE_OBJECT_REF_METHODS(InstructionKind, runtime::ObjectRef, InstructionKindNode); +}; + +/*! \brief Schedule instructions each corresponds to a schedule primitive */ +class InstructionNode : public runtime::Object { + public: + /*! \brief The kind of the instruction */ + InstructionKind kind; + /*! + * \brief The input random variables of the instruction, and the type of each element can be one + * of the following: + * - BlockRV + * - LoopRV + * - ExprRV + * - FloatImm + * - IntImm + * - String + * - null pointer + */ + Array<ObjectRef> inputs; + /*! + * \brief The attributes of the instruction. Similar to attributes of an operator, + * attributes of an instruction are arbitrary constant metadata required by the instructions. + * For example, the name of the block to be retrieved in `GetBlock`. + */ + Array<ObjectRef> attrs; + /*! \brief The output random variables of the instruction, and the type of each element can be one + * of the following: + * - BlockRV + * - LoopRV + * - ExprRV, atomic variables only, won't be constants or composite PrimExpr + */ + Array<ObjectRef> outputs; + + void VisitAttrs(tvm::AttrVisitor* v) { + v->Visit("kind", &kind); + v->Visit("inputs", &inputs); + v->Visit("attrs", &attrs); + v->Visit("outputs", &outputs); + } + + static constexpr const char* _type_key = "tir.Instruction"; + TVM_DECLARE_FINAL_OBJECT_INFO(InstructionNode, runtime::Object); +}; + +/*! + * \brief Managed reference to InstructionNode + * \sa InstructionNode + */ +class Instruction : public runtime::ObjectRef { + public: + /*! + * \brief Constructor + * \param kind The kind of the instruction + * \param inputs The input random variables of the instruction + * \param attrs The attributes of the instruction + * \param outputs The output random variables of the instruction + */ + explicit Instruction(InstructionKind kind, Array<ObjectRef> inputs, Array<ObjectRef> attrs, + Array<ObjectRef> outputs); + + TVM_DEFINE_OBJECT_REF_METHODS(Instruction, runtime::ObjectRef, InstructionNode); +}; + +/*! + * \brief A helper macro to register InstructionKind, only used in `TVM_REGISTER_INST_KIND` + * \note This macro is not user-facing. + * \sa TVM_REGISTER_INST_KIND + */ +#define TVM_INST_KIND_REGISTER_VAR_DEF \ + static DMLC_ATTRIBUTE_UNUSED ::tvm::tir::InstructionKindRegEntry& __make_##InstructionKind + +/*! + * \brief Register an InstructionKind + * \param InstructionKindName The name of the InstructionKind + * + * Example: + * + * \code + * + * TVM_REGISTER_INST_KIND("ComputeInline") + * .set_is_pure(false) + * .set_apply_to_schedule(ApplyToSchedule) + * .set_attrs_as_json(AttrsAsJSON) + * .set_attrs_from_json(AttrsFromJSON) + * .set_as_python(AsPython); + * + * \endcode + */ +#define TVM_REGISTER_INST_KIND(InstructionKindName) \ + TVM_STR_CONCAT(TVM_INST_KIND_REGISTER_VAR_DEF, __COUNTER__) = \ + ::tvm::tir::InstructionKindRegEntry::RegisterOrGet(InstructionKindName).set_name() + +/*! \brief An entry in the registry of InstructionKind */ +class InstructionKindRegEntry { + public: + static InstructionKindRegEntry& RegisterOrGet(const String& name); + + InstructionKindRegEntry& set_name() { Review comment: Why do we want registered instructions to be mutable. It seems like this would cause issues where instructions could be mutated mid tuning. -- 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]
