graph.h 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*!
 *  Copyright (c) 2016 by Contributors
 * \file graph.h
 * \brief Utilities to get information about schedule graph.
 */
#ifndef TVM_SCHEDULE_GRAPH_H_
#define TVM_SCHEDULE_GRAPH_H_

#include <tvm/expr.h>
#include <tvm/schedule.h>
#include <unordered_map>
12
#include <unordered_set>
13 14 15 16 17 18 19 20
#include <vector>

namespace tvm {
namespace schedule {

/*!
 * \brief data structure of Operation->Tensors it reads
 */
21
using ReadGraph = Map<Operation, Array<Tensor> >;
22 23

/*!
24 25 26 27 28 29 30 31 32 33
 * \brief The map beteen tensor and operation it feeds to
 */
using FeedGraph = std::unordered_map<Tensor, std::vector<Operation> >;

/*!
 * \brief AttachPath maps op-> a list of IterVar
 */
using AttachPath = Map<Operation, Array<IterVar> >;

/*!
34 35 36 37
 * \brief Get read graph of each operation to all the
 *  Tensors that it directly depends on.
 *
 *  The result map contains Operations needed to finish root Operation.
38
 * \param roots The root operation.
39 40
 * \return The result map.
 */
41
ReadGraph CreateReadGraph(const Array<Operation>& roots);
42 43 44

/*!
 * \brief Get a post DFS ordered of operations in the graph.
45
 * \param roots The root of the graph.
46 47 48 49 50 51
 * \param g The read graph.
 * \return vector order of Operations in PostDFS order.
 *
 * \note PostDFSOrder is a special case of Topoligical order,
 *   and can be used when topoligical order is needed.
 */
52
Array<Operation> PostDFSOrder(
53
    const Array<Operation>& roots, const ReadGraph& g);
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/*!
 * \brief Create feedgraph for given Schedule
 * \param  g The read graph.
 * \return The created feedgraph.
 */
FeedGraph CreateFeedGraph(const ReadGraph& g);

/*!
 * \brief Create AttachPath that  maps op-> a list of IterVar
 *  That represents the loop nest op sits in from inner most to outermost
 *  Also inserts attach_stage for scan updates when needed.
 *
 * \param sch The schedule.
 * \return The attach path.
 */
AttachPath CreateAttachPath(Schedule sch);

/*!
 * \brief Get all operations inside the recursion of scan.
 * \param scan The scan node.
 * \param feed_graph The feed graph to help analysis.
 * \return The body operations, in read dependency order.
 */
Array<Operation> ScanGetBody_(
    const ScanOpNode* scan, const FeedGraph& feed_graph);
// same as ScanGetBody_, but create FeedGraph internally.
Array<Operation> ScanGetBody(const Operation& scan);

/*!
 * \brief Analyze each spatial dimension of scan's result.
 *  Give check on whether each dimension is fix point,
 *  An axis is a fixed point if it only refers back to itself in recursion
 *  and it is not used in axis of other recursion field.
 *
 *  next_state[t, ..., axis, ...] = f(prev_state[t-1, ...,axis,...]
 *
 * \param scan The scan node.
 * \param body The body of scan, sorted in reverse PostDFSOrder.
 * \return Map of spatial_axis -> IntImm
 */
Map<IterVar, Expr> ScanFixPointAnalysis(
    const Operation& scan, const Array<Operation>& body);

98 99 100 101
}  // namespace schedule
}  // namespace tvm

#endif  // TVM_SCHEDULE_GRAPH_H_