forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_allocated_memory.cc
126 lines (111 loc) · 4.21 KB
/
calculate_allocated_memory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* 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 tir/analysis/calculate_allocated_memory.cc
* \brief Calculate allocated memory per memory scope required by PrimFuncs.
*/
#include <tvm/arith/analyzer.h>
#include <tvm/runtime/container/map.h>
#include <tvm/runtime/device_api.h>
#include <tvm/tir/analysis.h>
#include <tvm/tir/function.h>
#include <tvm/tir/stmt_functor.h>
#include <tvm/tir/usmp/utils.h>
#include <algorithm>
#include <map>
#include <unordered_map>
namespace tvm {
namespace tir {
template <typename T>
class AllocationCalculator : public StmtExprVisitor {
public:
AllocationCalculator() = default;
tvm::Map<String, Integer> operator()(const PrimFunc& func);
private:
void VisitStmt_(const T* op) override;
std::unordered_map<std::string, int64_t> _max_size;
std::unordered_map<std::string, int64_t> _current_size;
};
template <typename T>
tvm::Map<String, Integer> AllocationCalculator<T>::operator()(const PrimFunc& func) {
this->VisitStmt(func->body);
tvm::Map<String, Integer> res;
for (auto [k, v] : _max_size) {
res.Set(String(k), Integer(v));
}
return res;
}
std::string GetStorageScope(const Var& var) {
auto* ptr = var->type_annotation.as<PointerTypeNode>();
ICHECK(ptr) << "Buffer Var's type annotation must be of PointerType";
return ptr->storage_scope;
}
template <typename T>
void AllocationCalculator<T>::VisitStmt_(const T* op) {
std::string storage_scope = GetStorageScope(op->buffer_var);
auto search = _current_size.find(storage_scope);
if (search == _current_size.end()) {
_current_size[storage_scope] = 0;
_max_size[storage_scope] = 0;
}
auto size = op->ConstantAllocationSize() * op->dtype.bytes() * op->dtype.lanes();
_current_size[storage_scope] += size;
_max_size[storage_scope] = std::max(_current_size[storage_scope], _max_size[storage_scope]);
StmtExprVisitor::VisitStmt(op->body);
_current_size[storage_scope] -= size;
}
tvm::Map<String, Integer> CalculateAllocatedBytes(const PrimFunc& func) {
return AllocationCalculator<AllocateNode>()(func);
}
TVM_REGISTER_GLOBAL("tir.analysis.calculate_allocated_bytes").set_body_typed([](PrimFunc func) {
return CalculateAllocatedBytes(func);
});
bool VerifyVTCMLimit(const PrimFunc& func, Integer limit) {
auto sizes = CalculateAllocatedBytes(func);
const auto vtcm_allocated = sizes.Get("global.vtcm").value_or(0);
if (limit.IntValue() > 0 && vtcm_allocated.IntValue() > limit.IntValue()) {
return false;
}
return true;
}
namespace transform {
Pass VerifyVTCMLimit(const Integer& limit) {
auto pass_func = [=](IRModule mod, PassContext ctx) {
for (auto kv : mod->functions) {
if (auto* n = kv.second.as<PrimFuncNode>()) {
auto func = GetRef<PrimFunc>(n);
auto sizes = CalculateAllocatedBytes(func);
const auto vtcm_allocated = sizes.Get("global.vtcm").value_or(0);
if (limit.IntValue() > 0 && vtcm_allocated.IntValue() > limit.IntValue()) {
LOG(FATAL) << "RuntimeError: The global.vtcm memory allocation limit has been "
"exceeded(allocated: "
<< vtcm_allocated << ", limit: " << limit << ").\n"
<< "In function\n"
<< func;
}
}
}
return mod;
};
return tvm::transform::CreateModulePass(pass_func, 0, "tir.calculate_allocated_bytes", {});
}
TVM_REGISTER_GLOBAL("tir.transform.VerifyVTCMLimit").set_body_typed(VerifyVTCMLimit);
} // namespace transform
} // namespace tir
} // namespace tvm