touch_extractor.h 4.1 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

20 21 22 23 24 25 26 27
/*!
 * \file touch_extractor.h
 * \brief Extract feature of touch pattern of axes in lowered IR
 */

#ifndef TVM_AUTOTVM_TOUCH_EXTRACTOR_H_
#define TVM_AUTOTVM_TOUCH_EXTRACTOR_H_

28 29
#include <tvm/tir/expr.h>
#include <tvm/tir/expr_functor.h>
30 31
#include <tvm/runtime/registry.h>

32 33 34 35 36
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <deque>
37
#include <unordered_map>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "feature_visitor.h"

namespace tvm {
namespace autotvm {

using TouchedBuffer = std::string;

// touch pattern buf[(stride * var) % mod) + other]
struct TouchPattern {
  int64_t stride{0};
  int64_t mod{-1};  // -1 for +inf

  int64_t count{1};
  int64_t reuse{1};
  int64_t thread_count{0};  // count when move thread axis into innermost
  int64_t thread_reuse{0};  // reuse ratio move thread axis into innermost
};

// all the feature of an iter var
struct ItervarFeature {
58
  ItervarFeature(Var var,
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
                 int64_t extent,
                 int nest,
                 AnnotationType ann_type,
                 int64_t topdown,
                 int counter)
      : length(extent), nest_level(nest), ann(ann_type), topdown_product(topdown), order(counter) {}
  ItervarFeature() {}

  // Axis Attributes
  int64_t length;
  int nest_level;
  AnnotationType ann;         // one-hot axis type
  int64_t topdown_product;    // accumulative product of axis length, in top-down order
  int64_t bottomup_product;   // accumulative product of axis length, in bottom-up order
  // bottomup_product = reuse * count for any touched buffer

  int order;  // used for soring axis

  // Arithmetic feature
  int add_ct{0};
  int mul_ct{0};
  int div_ct{0};

  // Memory Touch Feature
  std::unordered_map<TouchedBuffer, TouchPattern> touch_feature;
};

// extract iter vars and their touch pattern from ir
class TouchExtractor : public FeatureVisitor {
 public:
89 90
  void Analyze(const Stmt& stmt) {
    operator()(stmt);
91 92 93
  }

  // arithmetic stats
94
  void VisitExpr_(const AddNode* op) final {
95
    if (op->dtype.is_float())
96
      itervar_map[itervar_stack_.back()].add_ct++;
97
    FeatureVisitor::VisitExpr_(op);
98 99
  }

100
  void VisitExpr_(const SubNode* op) final {
101
    if (op->dtype.is_float())
102
      itervar_map[itervar_stack_.back()].add_ct++;
103
    FeatureVisitor::VisitExpr_(op);
104 105
  }

106
  void VisitExpr_(const MulNode* op) final {
107
    if (op->dtype.is_float())
108
      itervar_map[itervar_stack_.back()].mul_ct++;
109
    FeatureVisitor::VisitExpr_(op);
110 111
  }

112
  void VisitExpr_(const DivNode* op) final {
113
    if (op->dtype.is_float())
114
      itervar_map[itervar_stack_.back()].div_ct++;
115
    FeatureVisitor::VisitExpr_(op);
116 117
  }

118
  void VisitExpr_(const ModNode* op) final {
119
    if (op->dtype.is_float())
120
      itervar_map[itervar_stack_.back()].div_ct++;
121
    FeatureVisitor::VisitExpr_(op);
122 123
  }

124
  std::unordered_map<Var, ItervarFeature, tvm::ObjectHash, tvm::ObjectEqual> itervar_map;
125 126

 private:
127
  bool EnterItervar_(Var var, int64_t length, AnnotationType ann_type);
128
  void ExitItervar_();
129
  void EnterMem_(Var buffer_var, PrimExpr index);
130 131 132 133 134
  void ExitMem_();

  int64_t topdown_product_{1};
  std::map<std::string, size_t> buffer_counter_;
  size_t itervar_counter_{0};
135
  std::deque<Var> itervar_stack_;  // use deque instead of stack for indexing
136 137
  std::deque<size_t> skip_stack_size_;

138
  using FeatureVisitor::VisitExpr_;
139 140 141 142 143 144
};

}  // namespace autotvm
}  // namespace tvm

#endif  // TVM_AUTOTVM_TOUCH_EXTRACTOR_H_