|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * Copyright (c) 2019 by Contributors |
| 22 | + * \file tensorcore_fragment.cc |
| 23 | + */ |
| 24 | +#include <tvm/ir.h> |
| 25 | +#include <tvm/ir_pass.h> |
| 26 | +#include <tvm/ir_mutator.h> |
| 27 | +#include <tvm/ir_visitor.h> |
| 28 | +#include <unordered_map> |
| 29 | +#include <unordered_set> |
| 30 | +#include "ir_util.h" |
| 31 | +#include "storage_access.h" |
| 32 | +#include "../runtime/thread_storage_scope.h" |
| 33 | + |
| 34 | +namespace tvm { |
| 35 | +namespace ir { |
| 36 | + |
| 37 | +class FragmentGetter : public IRVisitor { |
| 38 | + public: |
| 39 | + struct FragmentInfo { |
| 40 | + int m, n, k; |
| 41 | + std::string layout; |
| 42 | + FragmentInfo() = default; |
| 43 | + FragmentInfo(int _m, int _n, int _k, const std::string& _layout) |
| 44 | + : m(_m), n(_n), k(_k), layout(_layout) {} |
| 45 | + }; |
| 46 | + |
| 47 | + void Visit_(const Call* op) final { |
| 48 | + IRVisitor::Visit_(op); |
| 49 | + |
| 50 | + if (op->is_intrinsic(intrinsic::tvm_load_matrix_sync) || |
| 51 | + op->is_intrinsic(intrinsic::tvm_store_matrix_sync)) { |
| 52 | + CHECK_EQ(op->args.size(), 8U); |
| 53 | + const Variable* buffer_var = op->args[0].as<Variable>(); |
| 54 | + CHECK(buffer_var); |
| 55 | + const IntImm* m = op->args[1].as<IntImm>(); |
| 56 | + const IntImm* n = op->args[2].as<IntImm>(); |
| 57 | + const IntImm* k = op->args[3].as<IntImm>(); |
| 58 | + const StringImm* layout = op->args[7].as<StringImm>(); |
| 59 | + CHECK(m); |
| 60 | + CHECK(n); |
| 61 | + CHECK(k); |
| 62 | + CHECK(layout); |
| 63 | + |
| 64 | + std::string scope = scopes[buffer_var]; |
| 65 | + if (fragments.count(buffer_var)) { |
| 66 | + FragmentInfo info = fragments[buffer_var]; |
| 67 | + CHECK_EQ(m->value, info.m); |
| 68 | + CHECK_EQ(n->value, info.n); |
| 69 | + CHECK_EQ(k->value, info.k); |
| 70 | + if (scope == "wmma.matrix_a" || scope == "wmma.matrix_b") { |
| 71 | + CHECK_EQ(layout->value, info.layout); |
| 72 | + } |
| 73 | + } else { |
| 74 | + FragmentInfo info; |
| 75 | + if (scope == "wmma.matrix_a" || scope == "wmma.matrix_b") { |
| 76 | + info = FragmentInfo(m->value, n->value, k->value, layout->value); |
| 77 | + } else if (scope == "wmma.accumulator") { |
| 78 | + info = FragmentInfo(m->value, n->value, k->value, ""); |
| 79 | + } |
| 80 | + fragments[buffer_var] = info; |
| 81 | + } |
| 82 | + } else if (op->is_intrinsic(intrinsic::tvm_fill_fragment)) { |
| 83 | + CHECK_EQ(op->args.size(), 6U); |
| 84 | + const Variable* buffer_var = op->args[0].as<Variable>(); |
| 85 | + CHECK(buffer_var); |
| 86 | + const IntImm* m = op->args[1].as<IntImm>(); |
| 87 | + const IntImm* n = op->args[2].as<IntImm>(); |
| 88 | + const IntImm* k = op->args[3].as<IntImm>(); |
| 89 | + CHECK(m); |
| 90 | + CHECK(n); |
| 91 | + CHECK(k); |
| 92 | + |
| 93 | + std::string scope = scopes[buffer_var]; |
| 94 | + CHECK_EQ(scope, "wmma.accumulator"); |
| 95 | + if (fragments.count(buffer_var)) { |
| 96 | + FragmentInfo info = fragments[buffer_var]; |
| 97 | + CHECK_EQ(m->value, info.m); |
| 98 | + CHECK_EQ(n->value, info.n); |
| 99 | + CHECK_EQ(k->value, info.k); |
| 100 | + } else { |
| 101 | + FragmentInfo info(m->value, n->value, k->value, ""); |
| 102 | + fragments[buffer_var] = info; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + void Visit_(const AttrStmt* op) final { |
| 108 | + if (op->attr_key == attr::storage_scope) { |
| 109 | + const Variable* buffer = op->node.as<Variable>(); |
| 110 | + CHECK(buffer); |
| 111 | + scopes[buffer] = op->value.as<StringImm>()->value; |
| 112 | + } |
| 113 | + IRVisitor::Visit_(op); |
| 114 | + } |
| 115 | + |
| 116 | + std::unordered_map<const Variable*, std::string> scopes; |
| 117 | + std::unordered_map<const Variable*, FragmentInfo> fragments; |
| 118 | +}; |
| 119 | + |
| 120 | +class FragmentChecker : public IRVisitor { |
| 121 | + public: |
| 122 | + FragmentChecker(const FragmentGetter &getter) : fragment_getter(getter) {} |
| 123 | + |
| 124 | + void Visit_(const Call* op) final { |
| 125 | + if (op->is_intrinsic(intrinsic::tvm_mma_sync)) { |
| 126 | + CHECK_EQ(op->args.size(), 8U); |
| 127 | + const Variable* buffer_var_d = op->args[0].as<Variable>(); |
| 128 | + const Variable* buffer_var_a = op->args[2].as<Variable>(); |
| 129 | + const Variable* buffer_var_b = op->args[4].as<Variable>(); |
| 130 | + const Variable* buffer_var_c = op->args[6].as<Variable>(); |
| 131 | + CHECK(buffer_var_d); |
| 132 | + CHECK(buffer_var_a); |
| 133 | + CHECK(buffer_var_b); |
| 134 | + CHECK(buffer_var_c); |
| 135 | + CHECK(CheckShape(buffer_var_d, buffer_var_a)); |
| 136 | + CHECK(CheckShape(buffer_var_d, buffer_var_b)); |
| 137 | + CHECK(CheckShape(buffer_var_d, buffer_var_c)); |
| 138 | + } |
| 139 | + } |
| 140 | + private: |
| 141 | + bool CheckShape(const Variable* buffer1, const Variable* buffer2) { |
| 142 | + CHECK(fragment_getter.fragments.count(buffer1)); |
| 143 | + CHECK(fragment_getter.fragments.count(buffer2)); |
| 144 | + FragmentGetter::FragmentInfo info1 = fragment_getter.fragments.at(buffer1); |
| 145 | + FragmentGetter::FragmentInfo info2 = fragment_getter.fragments.at(buffer2); |
| 146 | + return info1.m == info2.m && info1.n == info2.n && info1.k == info2.k; |
| 147 | + |
| 148 | + } |
| 149 | + const FragmentGetter &fragment_getter; |
| 150 | + |
| 151 | +}; |
| 152 | + |
| 153 | +class InferFragmenter : public IRMutator { |
| 154 | + public: |
| 155 | + InferFragmenter(const FragmentGetter &getter) : fragment_getter(getter) {} |
| 156 | + |
| 157 | + Stmt Mutate_(const Allocate* op, const Stmt& s) final { |
| 158 | + Stmt stmt = IRMutator::Mutate_(op, s); |
| 159 | + const Variable* buffer = op->buffer_var.get(); |
| 160 | + if (fragment_getter.fragments.count(buffer)) { |
| 161 | + FragmentGetter::FragmentInfo info = fragment_getter.fragments.at(buffer); |
| 162 | + std::string shape = std::to_string(info.n) + ", " + |
| 163 | + std::to_string(info.m) + ", " + |
| 164 | + std::to_string(info.k); |
| 165 | + Expr shape_expr = StringImm::make(shape); |
| 166 | + Stmt shape_attr = AttrStmt::make(op->buffer_var, attr::fragment_shape, shape_expr, stmt); |
| 167 | + if (info.layout != "") { |
| 168 | + Stmt layout_attr = AttrStmt::make(op->buffer_var, attr::fragment_layout, |
| 169 | + StringImm::make(info.layout), shape_attr); |
| 170 | + return layout_attr; |
| 171 | + } else { |
| 172 | + return shape_attr; |
| 173 | + } |
| 174 | + } |
| 175 | + return stmt; |
| 176 | + } |
| 177 | + private: |
| 178 | + const FragmentGetter &fragment_getter; |
| 179 | +}; |
| 180 | + |
| 181 | +Stmt InferFragment(Stmt stmt) { |
| 182 | + FragmentGetter getter; |
| 183 | + getter.Visit(stmt); |
| 184 | + FragmentChecker(getter).Visit(stmt); |
| 185 | + stmt = InferFragmenter(getter).Mutate(stmt); |
| 186 | + return stmt; |
| 187 | +} |
| 188 | + |
| 189 | +LoweredFunc InferFragment(LoweredFunc f) { |
| 190 | + CHECK_NE(f->func_type, kHostFunc); |
| 191 | + auto n = make_node<LoweredFuncNode>(*f.operator->()); |
| 192 | + n->body = InferFragment(f->body); |
| 193 | + return LoweredFunc(n); |
| 194 | +} |
| 195 | + |
| 196 | +} // namespace ir |
| 197 | +} // namespace tvm |
0 commit comments