-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbb.h
More file actions
215 lines (166 loc) · 7.63 KB
/
bb.h
File metadata and controls
215 lines (166 loc) · 7.63 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef ION_BB_DNN_BB_H
#define ION_BB_DNN_BB_H
// For before uuid-v2014.01 undefined UUID_STR_LEN
#ifndef UUID_STR_LEN
#define UUID_STR_LEN 36
#endif
#include <ion/ion.h>
#include "sole.hpp"
namespace ion {
namespace bb {
namespace dnn {
template<typename T>
class ReorderHWC2CHW : public BuildingBlock<ReorderHWC2CHW<T>> {
public:
constexpr static const int dim = 3;
GeneratorInput<Halide::Func> input{"input", Halide::type_of<T>(), dim};
GeneratorOutput<Halide::Func> output{"output", Halide::type_of<T>(), dim};
void generate() {
output(x, y, c) = input(c, x, y);
}
private:
Halide::Var x, y, c;
};
template<typename T>
class ReorderCHW2HWC : public BuildingBlock<ReorderCHW2HWC<T>> {
public:
constexpr static const int dim = 3;
GeneratorInput<Halide::Func> input{"input", Halide::type_of<T>(), dim};
GeneratorOutput<Halide::Func> output{"output", Halide::type_of<T>(), dim};
void generate() {
output(c, x, y) = input(x, y, c);
}
private:
Halide::Var c, x, y;
};
template<typename X, int32_t D>
class ObjectDetectionBase : public BuildingBlock<X> {
static_assert(D == 3 || D == 4, "D must be 3 or 4.");
public:
GeneratorParam<std::string> gc_description{"gc_description", "Detect objects by various DNN models."};
GeneratorParam<std::string> gc_inference{"gc_inference", R"((function(v){ return { output: v.input }}))"};
GeneratorParam<std::string> gc_tags{"gc_tags", "processing,recognition"};
GeneratorParam<std::string> gc_mandatory{"gc_mandatory", ""};
GeneratorParam<std::string> gc_strategy{"gc_strategy", "self"};
GeneratorParam<std::string> gc_prefix{"gc_prefix", ""};
GeneratorParam<std::string> model_root_url_{"model_base_url", "http://ion-archives.s3-us-west-2.amazonaws.com/models/"};
GeneratorParam<std::string> cache_root_{"cache_root", "/tmp/"};
// TODO: Embed model at compilation time
// GeneratorParam<bool> embed_model{"embed_model", false};
GeneratorInput<Halide::Func> input_{"input", Halide::type_of<float>(), D};
GeneratorOutput<Halide::Func> output{"output", Halide::type_of<float>(), D};
void generate() {
using namespace Halide;
std::string session_id = sole::uuid4().str();
Buffer<uint8_t> session_id_buf(session_id.size() + 1);
session_id_buf.fill(0);
std::memcpy(session_id_buf.data(), session_id.c_str(), session_id.size());
const std::string model_root_url(model_root_url_);
Halide::Buffer<uint8_t> model_root_url_buf(model_root_url.size() + 1);
model_root_url_buf.fill(0);
std::memcpy(model_root_url_buf.data(), model_root_url.c_str(), model_root_url.size());
const std::string cache_root(cache_root_);
Halide::Buffer<uint8_t> cache_path_buf(cache_root.size() + 1);
cache_path_buf.fill(0);
std::memcpy(cache_path_buf.data(), cache_root.c_str(), cache_root.size());
const bool cuda_enable = this->get_target().has_feature(Target::Feature::CUDA);
input = Func{static_cast<std::string>(gc_prefix) + "in"};
input(_) = input_(_);
std::vector<ExternFuncArgument> params{input, session_id_buf, model_root_url_buf, cache_path_buf, cuda_enable};
Func object_detection(static_cast<std::string>(gc_prefix) + "object_detection");
object_detection.define_extern("ion_bb_dnn_generic_object_detection", params, Float(32), D);
object_detection.compute_root();
output(_) = object_detection(_);
}
void schedule() {
using namespace Halide;
Var c = input.args()[0];
Var x = input.args()[1];
Var y = input.args()[2];
input.bound(c, 0, 3).unroll(c);
if (this->get_target().has_gpu_feature()) {
Var xi, yi;
input.gpu_tile(x, y, xi, yi, 32, 16);
} else {
input.vectorize(x, this->natural_vector_size(Float(32))).parallel(y, 16);
}
input.compute_root();
}
private:
Halide::Func input;
};
class ObjectDetection : public ObjectDetectionBase<ObjectDetection, 3> {
public:
GeneratorParam<std::string> gc_title{"gc_title", "Object Detection"};
};
class ObjectDetectionArray : public ObjectDetectionBase<ObjectDetectionArray, 4> {
public:
GeneratorParam<std::string> gc_title{"gc_title", "Object Detection (Array)"};
};
} // dnn
} // bb
} // ion
ION_REGISTER_BUILDING_BLOCK(ion::bb::dnn::ReorderCHW2HWC<uint8_t>, dnn_reorder_chw2hwc);
ION_REGISTER_BUILDING_BLOCK(ion::bb::dnn::ReorderHWC2CHW<uint8_t>, dnn_reorder_hwc2chw);
ION_REGISTER_BUILDING_BLOCK(ion::bb::dnn::ObjectDetection, dnn_object_detection);
ION_REGISTER_BUILDING_BLOCK(ion::bb::dnn::ObjectDetectionArray, dnn_object_detection_array);
namespace ion {
namespace bb {
namespace dnn {
class TLTObjectDetectionSSD : public BuildingBlock<TLTObjectDetectionSSD> {
public:
GeneratorParam<std::string> gc_title{"gc_title", "TLT Object Detection SSD"};
GeneratorParam<std::string> gc_description{"gc_description", "Detect objects by TLT Object Detection SSD models."};
GeneratorParam<std::string> gc_inference{"gc_inference", R"((function(v){ return { output: v.input }}))"};
GeneratorParam<std::string> gc_tags{"gc_tags", "processing,recognition"};
GeneratorParam<std::string> gc_mandatory{"gc_mandatory", ""};
GeneratorParam<std::string> gc_strategy{"gc_strategy", "self"};
GeneratorParam<std::string> gc_prefix{"gc_prefix", ""};
GeneratorParam<std::string> model_root_url_{"model_base_url", "http://ion-archives.s3-us-west-2.amazonaws.com/models/tlt_object_detection_ssd_resnet18/"};
GeneratorParam<std::string> cache_root_{"cache_root", "/tmp/"};
GeneratorInput<Halide::Func> input_{"input", Halide::type_of<float>(), 3};
GeneratorOutput<Halide::Func> output{"output", Halide::type_of<float>(), 3};
void generate() {
using namespace Halide;
std::string session_id = sole::uuid4().str();
Buffer<uint8_t> session_id_buf(session_id.size() + 1);
session_id_buf.fill(0);
std::memcpy(session_id_buf.data(), session_id.c_str(), session_id.size());
const std::string model_root_url(model_root_url_);
Halide::Buffer<uint8_t> model_root_url_buf(model_root_url.size() + 1);
model_root_url_buf.fill(0);
std::memcpy(model_root_url_buf.data(), model_root_url.c_str(), model_root_url.size());
const std::string cache_root(cache_root_);
Halide::Buffer<uint8_t> cache_path_buf(cache_root.size() + 1);
cache_path_buf.fill(0);
std::memcpy(cache_path_buf.data(), cache_root.c_str(), cache_root.size());
input = Func{static_cast<std::string>(gc_prefix) + "in"};
input(_) = input_(_);
std::vector<ExternFuncArgument> params{input, session_id_buf, model_root_url_buf, cache_path_buf};
Func object_detection(static_cast<std::string>(gc_prefix) + "tlt_object_detection_ssd");
object_detection.define_extern("ion_bb_dnn_tlt_object_detection_ssd", params, Float(32), 3);
object_detection.compute_root();
output(_) = object_detection(_);
}
void schedule() {
using namespace Halide;
Var c = input.args()[0];
Var x = input.args()[1];
Var y = input.args()[2];
input.bound(c, 0, 3).unroll(c);
if (this->get_target().has_gpu_feature()) {
Var xi, yi;
input.gpu_tile(x, y, xi, yi, 32, 16);
} else {
input.vectorize(x, this->natural_vector_size(Float(32))).parallel(y, 16);
}
input.compute_root();
}
private:
Halide::Func input;
};
} // dnn
} // bb
} // ion
ION_REGISTER_BUILDING_BLOCK(ion::bb::dnn::TLTObjectDetectionSSD, dnn_tlt_object_detection_ssd);
#endif // ION_BB_DNN_BB_H