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 | * \brief External computation rule. |
22 | * \file extern_op.cc |
23 | */ |
24 | #include <tvm/arith/analyzer.h> |
25 | #include <tvm/runtime/registry.h> |
26 | #include <tvm/te/operation.h> |
27 | #include <tvm/tir/expr.h> |
28 | |
29 | #include <unordered_set> |
30 | |
31 | #include "op_utils.h" |
32 | |
33 | namespace tvm { |
34 | namespace te { |
35 | using namespace tir; |
36 | // ExternOpNode |
37 | TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable) |
38 | .set_dispatch<ExternOpNode>([](const ObjectRef& node, ReprPrinter* p) { |
39 | auto* op = static_cast<const ExternOpNode*>(node.get()); |
40 | p->stream << "extern(" << op->name << ", " << op << ")" ; |
41 | }); |
42 | |
43 | TVM_REGISTER_NODE_TYPE(ExternOpNode); |
44 | |
45 | int ExternOpNode::num_outputs() const { return static_cast<int>(output_placeholders.size()); } |
46 | |
47 | Array<IterVar> ExternOpNode::root_iter_vars() const { return {}; } |
48 | |
49 | DataType ExternOpNode::output_dtype(size_t i) const { return output_placeholders[i]->dtype; } |
50 | |
51 | Array<PrimExpr> ExternOpNode::output_shape(size_t i) const { return output_placeholders[i]->shape; } |
52 | |
53 | ExternOp::ExternOp(std::string name, std::string tag, Map<String, ObjectRef> attrs, |
54 | Array<Tensor> inputs, Array<Buffer> input_placeholders, |
55 | Array<Buffer> output_placeholders, Stmt body) { |
56 | if (!attrs.defined()) { |
57 | attrs = Map<String, ObjectRef>(); |
58 | } |
59 | auto n = make_object<ExternOpNode>(); |
60 | n->name = std::move(name); |
61 | n->tag = std::move(tag); |
62 | n->attrs = std::move(attrs); |
63 | ICHECK_EQ(inputs.size(), input_placeholders.size()); |
64 | for (size_t i = 0; i < inputs.size(); ++i) { |
65 | ICHECK_EQ(inputs[i]->dtype, input_placeholders[i]->dtype); |
66 | ICHECK_EQ(inputs[i]->shape.size(), input_placeholders[i]->shape.size()); |
67 | for (size_t dim = 0; dim < inputs[i]->shape.size(); ++dim) { |
68 | ICHECK(inputs[i]->shape[dim].same_as(input_placeholders[i]->shape[dim])); |
69 | } |
70 | ICHECK_EQ(input_placeholders[i]->strides.size(), 0U); |
71 | } |
72 | n->inputs = std::move(inputs); |
73 | n->input_placeholders = std::move(input_placeholders); |
74 | n->output_placeholders = std::move(output_placeholders); |
75 | n->body = std::move(body); |
76 | data_ = std::move(n); |
77 | } |
78 | |
79 | TVM_REGISTER_GLOBAL("te.ExternOp" ) |
80 | .set_body_typed([](std::string name, std::string tag, Map<String, ObjectRef> attrs, |
81 | Array<Tensor> inputs, Array<Buffer> input_placeholders, |
82 | Array<Buffer> output_placeholders, Stmt body) { |
83 | return ExternOp(name, tag, attrs, inputs, input_placeholders, output_placeholders, body); |
84 | }); |
85 | |
86 | Array<Tensor> ExternOpNode::InputTensors() const { return inputs; } |
87 | |
88 | Operation ExternOpNode::ReplaceInputs(const Operation& self, |
89 | const std::unordered_map<Tensor, Tensor>& rmap) const { |
90 | ICHECK_EQ(self.operator->(), this); |
91 | auto n = make_object<ExternOpNode>(*this); |
92 | n->body = ReplaceTensor(this->body, rmap); |
93 | for (size_t i = 0; i < n->inputs.size(); ++i) { |
94 | Tensor t = n->inputs[i]; |
95 | if (rmap.count(t)) { |
96 | n->inputs.Set(i, rmap.at(t)); |
97 | } |
98 | } |
99 | |
100 | if (body.same_as(n->body) && inputs.same_as(n->inputs)) { |
101 | return self; |
102 | } else { |
103 | return Operation(n); |
104 | } |
105 | } |
106 | |
107 | void ExternOpNode::PropBoundToInputs(const Operation& self, arith::Analyzer* analyzer, |
108 | const std::unordered_map<const VarNode*, IntSet>& dom_map, |
109 | std::unordered_map<Tensor, TensorDom>* out_dom_map) const { |
110 | for (Tensor t : this->inputs) { |
111 | auto it = out_dom_map->find(t); |
112 | if (it == out_dom_map->end()) continue; |
113 | TensorDom& dom = it->second; |
114 | for (size_t i = 0; i < t->shape.size(); ++i) { |
115 | dom.data[i].emplace_back( |
116 | IntSet::FromRange(Range::FromMinExtent(make_const(t->shape[i].dtype(), 0), t->shape[i]))); |
117 | } |
118 | } |
119 | } |
120 | |
121 | void ExternOpNode::GatherBound(const Operation& self, |
122 | const std::unordered_map<Tensor, TensorDom>& tensor_dom, |
123 | std::unordered_map<IterVar, Range>* out_dom_map) const {} |
124 | |
125 | Stmt ExternOpNode::BuildRealize(const Stage& stage, |
126 | const std::unordered_map<IterVar, Range>& realize_map, |
127 | const Stmt& body, String storage_scope) const { |
128 | ICHECK_EQ(stage->op.get(), this); |
129 | Stmt realize_body = body; |
130 | for (int k = 0; k < num_outputs(); ++k) { |
131 | Tensor t = stage->op.output(k); |
132 | Region bounds; |
133 | for (size_t i = 0; i < t->shape.size(); ++i) { |
134 | bounds.push_back(Range::FromMinExtent(make_const(t->shape[i].dtype(), 0), t->shape[i])); |
135 | } |
136 | realize_body = tir::ProducerRealize(t, bounds, const_true(), realize_body, storage_scope); |
137 | } |
138 | return realize_body; |
139 | } |
140 | |
141 | Stmt ExternOpNode::BuildProvide(const Stage& stage, |
142 | const std::unordered_map<IterVar, Range>& dom_map, |
143 | bool debug_keep_trivial_loop) const { |
144 | ICHECK_EQ(stage->op.operator->(), this); |
145 | Stmt ret = AttrStmt(make_zero(DataType::Int(32)), tir::attr::extern_scope, 0, this->body); |
146 | auto f_push_bind = [&ret](Buffer buffer, Tensor tensor) { |
147 | Array<ObjectRef> bind_spec; |
148 | Array<PrimExpr> tuple; |
149 | bind_spec.push_back(buffer); |
150 | bind_spec.push_back(tensor); |
151 | for (size_t k = 0; k < buffer->shape.size(); ++k) { |
152 | tuple.push_back(make_const(buffer->shape[k].dtype(), 0)); |
153 | tuple.push_back(buffer->shape[k]); |
154 | } |
155 | ret = AttrStmt(bind_spec, tir::attr::buffer_bind_scope, |
156 | Call(DataType::Handle(), builtin::tvm_tuple(), tuple), ret); |
157 | }; |
158 | for (size_t i = output_placeholders.size(); i != 0; --i) { |
159 | f_push_bind(output_placeholders[i - 1], stage->op.output(i - 1)); |
160 | } |
161 | for (size_t i = inputs.size(); i != 0; --i) { |
162 | f_push_bind(input_placeholders[i - 1], inputs[i - 1]); |
163 | } |
164 | return ret; |
165 | } |
166 | } // namespace te |
167 | } // namespace tvm |
168 | |