Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ansor][AutoTVM v2.0] Phase 1: Access Analyzer #6103

Merged
merged 8 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,27 @@
*/

/*!
* \file auto_scheduler/auto_schedule.h
merrymercy marked this conversation as resolved.
Show resolved Hide resolved
* \brief The user interface of the TVM Auto-scheduler. This is the entry structure to get
* schedule search requirements from upper level (Python API), and returns a high performance
* schedule after search process.
* \file tvm/auto_scheduler/auto_schedule.h
* \brief The user interface of the auto scheduler.
*/

#ifndef TVM_AUTO_SCHEDULER_AUTO_SCHEDULE_H_
#define TVM_AUTO_SCHEDULER_AUTO_SCHEDULE_H_

#include <utility>
#include <tvm/auto_scheduler/measure.h>
#include <tvm/auto_scheduler/search_policy.h>

#include "measure.h"
#include "search_policy/search_policy.h"
#include <utility>

namespace tvm {
namespace auto_scheduler {

/*! \brief Tuning and measurement options. */
class TuningOptionsNode : public Object {
public:
/*! \brief Number of total measurement trials. */
/*! \brief The number of total measurement trials. */
int num_measure_trials;
/*! \brief Stops early the tuning if no improvement after n measurements. */
/*! \brief Stops the tuning early if no improvement after n measurements. */
int early_stopping;
/*! \brief The number of programs to be measured at each search round. */
int num_measures_per_round;
Expand All @@ -51,7 +49,7 @@ class TuningOptionsNode : public Object {
int verbose;
/*! \brief ProgramBuilder which builds the program */
ProgramBuilder builder;
/*! \brief ProgramRunner which runs the program and measure time costs */
/*! \brief ProgramRunner which runs the program and measures time costs */
ProgramRunner runner;
/*! \brief MeasureCallback functions to be called after each measure batch */
Optional<Array<MeasureCallback>> measure_callbacks;
Expand Down Expand Up @@ -81,8 +79,8 @@ class TuningOptions : public ObjectRef {
public:
/*!
* \brief The constructor
* \param num_measure_trials Number of total measurement trials.
* \param early_stopping Stops early the tuning if no improvement after n measurements.
* \param num_measure_trials The number of total measurement trials.
* \param early_stopping Stops the tuning early if no improvement after n measurements.
* \param num_measures_per_round The number of programs to be measured at each search round.
* \param verbose Verbosity level. 0 for silent, 1 to output information during schedule
* search.
Expand All @@ -100,11 +98,11 @@ class TuningOptions : public ObjectRef {
};

/*!
* \brief Auto schedule search for a given compute declaration.
* \brief Run schedule search for a given compute declaration.
* \param task The search task of the compute declaration.
* \param search_policy The search policy to be used for schedule search.
* \param search_policy The search policy to be used.
* \param tuning_options Tuning and measurement options.
* \return A `te::schedule` and the a Array of `te::Tensor` to be used in `tvm.lower` or
* \return A `te::schedule` and the an Array of `te::Tensor` to be used in `tvm.lower` or
* `tvm.build`.
*/
TVM_DLL std::pair<te::Schedule, Array<te::Tensor>> AutoSchedule(SearchTask task,
Expand Down
242 changes: 242 additions & 0 deletions include/tvm/auto_scheduler/compute_dag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*r
* 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/auto_scheduler/compute_dag.h
* \brief The auto-scheduler's computational graph and related program analyses.
*
* We convert a compute declaration described by `tvm.compute` (could be a single operator or a
* subgraph) to a ComputeDAG. It keeps the input/output tensors of the compute declaration,
* a list of all operations in the DAG as well as static analysis results for the DAG (e.g. the
* total float operation count, consumer/producer relations of each operation stage, whether an
* operation stage should be tiled/compute inlined ...). These analyses can help the search policy
* to make decisions during search process.
* ComputeDAG is also responsible for the interaction between TVM Auto-scheduler `LoopState` and
* TVM schedule (e.g. applying the `LoopState` transform steps to TVM schedule, providing
* `LoopState` with extra information got from TVM schedule ...).
*/

#ifndef TVM_AUTO_SCHEDULER_COMPUTE_DAG_H_
#define TVM_AUTO_SCHEDULER_COMPUTE_DAG_H_

#include <tvm/auto_scheduler/loop_state.h>
#include <tvm/runtime/c_runtime_api.h>
#include <tvm/te/schedule.h>

#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

namespace tvm {
namespace auto_scheduler {

/*! \brief Static analysis result for a ComputeDAG */
class AccessAnalyzerNode : public Object {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like AccessAnalyzer itself can be a much more principled and extensible component of the system, so shall we put it in a separate file instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Maybe have an analysis.h to expect more analyzers in the future. On the other hand, another direction might be renaming AccessAnalyzer to ComputeDAGAnalyzer, because it provides some APIs for the ops in a compute DAG, such as NeedMultiLevelTiling, IsOutput, etc.

public:
template <class T>
using OperationMap = std::unordered_map<te::Operation, T, ObjectHash, ObjectEqual>;
merrymercy marked this conversation as resolved.
Show resolved Hide resolved

/*! \brief Map an operation to all operations it reads from.
* For each operation pair, use a two-dimentional array to multiple multi-dimentional accesses*/
merrymercy marked this conversation as resolved.
Show resolved Hide resolved
OperationMap<OperationMap<std::vector<std::vector<PrimExpr>>>> read_from;
merrymercy marked this conversation as resolved.
Show resolved Hide resolved
/*! \brief Map an operation to all operations it is read by.
* For each operation pair, use a two-dimentional array to multiple multi-dimentional accesses*/
OperationMap<OperationMap<std::vector<std::vector<PrimExpr>>>> read_by;
/*! \brief Store the number of common outer iterators for operation pairs that have
* read-write relations. */
OperationMap<OperationMap<int>> num_common_outer_iterators;
/*! \brief Store whether the operation is injective */
OperationMap<bool> is_injective;
/*! \brief Store whether the operation is strictly-inlineable */
OperationMap<bool> is_strict_inlineable;
/*! \brief Store whether the operation needs multi-level tiling */
OperationMap<bool> needs_multi_level_tiling;
/*! \brief Store whether the operation is an output operation */
OperationMap<bool> is_output;
/*! \brief Store the topological order of operations */
Array<te::Operation> ops_topo_order;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the relationship between this array and ComputeDAG::ops?

Copy link
Member Author

@merrymercy merrymercy Jul 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are the same. I store it in AccessAnalyzer because it is used first here.
In the constructor of ComputeDAG, it copies ops_topo_order as its ops


static constexpr const char* _type_key = "auto_scheduler.AccessAnalyzer";
TVM_DECLARE_FINAL_OBJECT_INFO(AccessAnalyzerNode, Object);
};

/*!
* \brief Managed reference to AccessAnalyzerNode.
* \sa AccessAnalyzerNode
*/
class AccessAnalyzer : public ObjectRef {
public:
explicit AccessAnalyzer(const Array<te::Tensor>& tensors);

/*!
* \brief Return whether this operation needs multi-level tiling
* \param op The operation
*/
TVM_DLL bool NeedsMultiLevelTiling(const te::Operation& op) const;

/*!
* \brief Return whether this operation is an injective operation
* \param op The operation
*/
TVM_DLL bool IsInjective(const te::Operation& op) const;

/*!
* \brief Return whether this operation is strictly inlinable
* \param op The operation
*/
TVM_DLL bool IsStrictInlineable(const te::Operation& op) const;

/*!
* \brief Return whether this operation is an output op
* \param op The operation
*/
TVM_DLL bool IsOutput(const te::Operation& op) const;

/*!
* \brief Get all consumers of on operation
* \param state The current loop state
* \param op The operation
* \param consumers The return consumer set
* \note This function propagates the relation for inlined ops
*/
TVM_DLL void GetConsumers(
const State& state, const te::Operation& op,
std::unordered_set<te::Operation, ObjectHash, ObjectEqual>* consumers) const;
merrymercy marked this conversation as resolved.
Show resolved Hide resolved

/*!
* \brief Get all producers of on operation
* \param state The current loop state
* \param op The operation
* \param producers The return producer set
* \note This function propagates the relation for inlined ops
*/
TVM_DLL void GetProducers(
const State& state, const te::Operation& op,
std::unordered_set<te::Operation, ObjectHash, ObjectEqual>* producers) const;
merrymercy marked this conversation as resolved.
Show resolved Hide resolved

/*!
* \brief Get all direct producers of on operation
* \param op The operation
* \param producers The return producer set
* \note This function DOES NOT propagate the relation for inlined ops
*/
TVM_DLL void GetDirectProducers(
const te::Operation& op,
std::unordered_set<te::Operation, ObjectHash, ObjectEqual>* producers) const;
merrymercy marked this conversation as resolved.
Show resolved Hide resolved

/*!
* \brief Get the number of common outer iterators.
* \param op The operation
* \param target_op The target operation
* \note This function propagates the relation for chains with multiple ops.
*/
TVM_DLL int GetNumCommonOuterIterator(const te::Operation& op,
const te::Operation& target_op) const;

/*!
* \brief Return whether two operations are elementwise-matched
* (e.g. conv2d and relu are elementwise matched)
* \note This function propagates the relation for chains with multiple ops.
*/
TVM_DLL bool ElementWiseMatch(const te::Operation& op, const te::Operation& target_op) const;

TVM_DEFINE_OBJECT_REF_METHODS(AccessAnalyzer, ObjectRef, AccessAnalyzerNode);
};

/*! \brief The TVM Auto-scheduler computational graph and related program analyses. */
class ComputeDAGNode : public Object {
public:
/*!
* \brief Input and output tensors.
* This is used as the input of `tvm.lower` or `tvm.build`.
*/
Array<te::Tensor> tensors;
/*! \brief All related operations in topo order. */
Array<te::Operation> ops;
/*! \brief The number of total float operations for this ComputeDAG. */
double flop_ct;
/*! \brief The initial state without any transform steps. */
State init_state;
/*! \brief The static read-write access analyzer */
AccessAnalyzer access_analyzer;

void VisitAttrs(tvm::AttrVisitor* v) {
v->Visit("tensors", &tensors);
v->Visit("ops", &ops);
v->Visit("flop_ct", &flop_ct);
v->Visit("init_state", &init_state);
}

static constexpr const char* _type_key = "auto_scheduler.ComputeDAG";
TVM_DECLARE_FINAL_OBJECT_INFO(ComputeDAGNode, Object);
};

/*!
* \brief Managed reference to ComputeDAGNode.
* \sa ComputeDAGNode
*/
class ComputeDAG : public ObjectRef {
public:
/*! \brief The constructor.
* \param tensors `te::Tensor`s for a compute declaration.
*/
TVM_DLL explicit ComputeDAG(Array<te::Tensor> tensors);

/*!
* \brief Apply the history transform steps to get a TVM schedule.
* \param transform_steps Transform steps of a state.
* \param stages The list of stages after applying the steps.
* Pass a valid pointer if this information needs to be used outside this function.
* \param stage_to_axes The map that stores all axes for one stage.
* Pass a valid pointer if this information needs to be used outside this function.
* \return A `te.schedule` and the an Array of `te.Tensor` to be used in `tvm.lower`
* or `tvm.build`.
*/
std::pair<te::Schedule, Array<te::Tensor>> ApplySteps(
const Array<Step>& transform_steps, Array<te::Stage>* stages = nullptr,
StageToAxesMap* stage_to_axes = nullptr) const;

/*!
* \brief Print transform steps as equivalent python schedule API.
* This can be used for debugging.
* \param transform_steps Transform steps of a state.
* \return The Python schedule code.
*/
String PrintStepsAsPython(const Array<Step>& transform_steps) const;

/*!
* \brief Fill the correct bound information for a given state by calling ir_pass::InferBound.
* The states can lose complete bound information after some transform steps (e.g., compute_at).
* We can call this function to infer and fill all the bound information.
* This function calls TVM InferBound pass internally to get the bound.
* The returned state of this function is guaranteed to have complete bound information.
* \param state The input state.
* \return The State with complete bound information
*/
State InferBound(const State& state) const;

TVM_DEFINE_OBJECT_REF_METHODS(ComputeDAG, ObjectRef, ComputeDAGNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(ComputeDAGNode);
};

} // namespace auto_scheduler
} // namespace tvm

#endif // TVM_AUTO_SCHEDULER_COMPUTE_DAG_H_
Loading