File: configure_jittest.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (255 lines) | stat: -rw-r--r-- 11,506 bytes parent folder | download | duplicates (2)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "Halide.h"

// Include the machine-generated .stub.h header file.
#include "configure.stub.h"

using namespace Halide;

const int kSize = 32;

void verify(const Buffer<int32_t, 3> &img, float compiletime_factor, float runtime_factor, int channels) {
    img.for_each_element([=](int x, int y, int c) {
        int expected = (int32_t)(compiletime_factor * runtime_factor * c * (x > y ? x : y));
        int actual = img(x, y, c);
        assert(expected == actual);
    });
}

int main(int argc, char **argv) {
    GeneratorContext context(get_jit_target_from_environment());

    Buffer<int, 3> input(kSize, kSize, 3);
    input.for_each_element([&](int x, int y, int c) {
        input(x, y, c) = (x * 3 + y * 5 + c * 7);
    });

    std::vector<Buffer<uint8_t, 2>> extras;
    int extra_value = 0;
    for (int i = 0; i < 3; ++i) {
        extras.push_back(Buffer<uint8_t, 2>(kSize, kSize));
        extras.back().fill((uint8_t)i);
        extra_value += i;
    }

    constexpr uint16_t typed_extra_value = 4;
    Buffer<int16_t, 2> typed_extra(kSize, kSize);
    typed_extra.fill(typed_extra_value);

    constexpr int extra_scalar = 7;
    constexpr int8_t extra_dynamic_scalar = 13;
    constexpr uint16_t extra_func_value = 5;

    constexpr int bias = 1;

    extra_value += extra_scalar + extra_dynamic_scalar + extra_func_value + typed_extra_value + bias;

    // Use a Generator Stub to create the Halide IR,
    // then call realize() to JIT and execute it.
    {
        // When calling a Stub, Func inputs must be actual Halide::Func.
        Var x, y, c;
        Func func_extra;
        func_extra(x, y, c) = cast<uint16_t>(extra_func_value);

        auto result = configure::generate(context, configure::Inputs{
                                                       input,
                                                       bias,
                                                       extras[0], extras[1], extras[2],
                                                       typed_extra,
                                                       func_extra,
                                                       extra_scalar,
                                                       cast<int8_t>(extra_dynamic_scalar)});

        Buffer<int32_t, 3> output = result.output.realize({kSize, kSize, 3});
        Buffer<float, 3> extra_buffer_output = result.extra_buffer_output.realize({kSize, kSize, 3});
        Buffer<double, 2> extra_func_output = result.extra_func_output.realize({kSize, kSize});

        output.for_each_element([&](int x, int y, int c) {
            assert(output(x, y, c) == input(x, y, c) + extra_value);
        });

        extra_buffer_output.for_each_element([&](int x, int y, int c) {
            assert(extra_buffer_output(x, y, c) == output(x, y, c));
        });

        extra_func_output.for_each_element([&](int x, int y) {
            assert(extra_func_output(x, y) == output(x, y, 0));
        });
    }

    // Alternately, instead of using Generator Stubs, we can just use the Callable interface.
    // We can call this on any Generator that is registered in the current process.
    {
        Callable configure = create_callable_from_generator(context, "configure");

        Buffer<int, 3> output(kSize, kSize, 3);
        Buffer<float, 3> extra_buffer_output(kSize, kSize, 3);
        Buffer<double, 2> extra_func_output(kSize, kSize);
        Buffer<double, 2> extra_tuple_func_output_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_func_output_1(kSize, kSize);
        Buffer<double, 2> extra_tuple_buffer_output_static_dims_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_buffer_output_static_dims_1(kSize, kSize);
        Buffer<double, 2> extra_tuple_buffer_output_dynamic_dims_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_buffer_output_dynamic_dims_1(kSize, kSize);
        Buffer<double, 2> extra_tuple_buffer_output_unset_types_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_buffer_output_unset_types_1(kSize, kSize);

        // All inputs to a Callable must be fully realized, so any Func inputs
        // that the Generator has implicitly become Buffer inputs of the same type
        // and dimensionality.
        Buffer<uint16_t, 3> func_extra(kSize, kSize, 3);
        func_extra.fill(extra_func_value);

        int r = configure(input, bias,
                          // extra inputs are in the order they were added, after all predeclared inputs
                          extras[0], extras[1], extras[2],
                          typed_extra,
                          func_extra,
                          extra_scalar,
                          extra_dynamic_scalar,
                          output,
                          // extra outputs are in the order they were added, after all predeclared outputs
                          extra_buffer_output,
                          extra_func_output,
                          extra_tuple_func_output_0,
                          extra_tuple_func_output_1,
                          extra_tuple_buffer_output_static_dims_0,
                          extra_tuple_buffer_output_static_dims_1,
                          extra_tuple_buffer_output_dynamic_dims_0,
                          extra_tuple_buffer_output_dynamic_dims_1,
                          extra_tuple_buffer_output_unset_types_0,
                          extra_tuple_buffer_output_unset_types_1);
        assert(r == 0);

        output.for_each_element([&](int x, int y, int c) {
            assert(output(x, y, c) == input(x, y, c) + extra_value);
        });

        extra_buffer_output.for_each_element([&](int x, int y, int c) {
            assert(extra_buffer_output(x, y, c) == output(x, y, c));
        });

        extra_func_output.for_each_element([&](int x, int y) {
            assert(extra_func_output(x, y) == output(x, y, 0));
        });

        for (auto &buf : {extra_tuple_func_output_0,
                          extra_tuple_buffer_output_static_dims_0,
                          extra_tuple_buffer_output_dynamic_dims_0,
                          extra_tuple_buffer_output_unset_types_0}) {
            buf.for_each_element([&](int x, int y) {
                assert(buf(x, y) == output(x, y, 0));
            });
        }

        for (auto &buf : {extra_tuple_func_output_1,
                          extra_tuple_buffer_output_static_dims_1,
                          extra_tuple_buffer_output_dynamic_dims_1,
                          extra_tuple_buffer_output_unset_types_1}) {
            buf.for_each_element([&](int x, int y) {
                assert(buf(x, y) == output(x, y, 1));
            });
        }
    }

    // We can also make an explicitly-typed std::function if we prefer. This
    // code is almost identical to the above, but we will repeat it just to make
    // these two usage examples each entirely self-contained.
    {
        auto configure = create_callable_from_generator(context, "configure")
                             .make_std_function<
                                 Buffer<int, 3>,
                                 int32_t,
                                 Buffer<uint8_t, 2>,
                                 Buffer<uint8_t, 2>,
                                 Buffer<uint8_t, 2>,
                                 Buffer<int16_t, 2>,
                                 Buffer<uint16_t, 3>,
                                 int32_t,
                                 int8_t,
                                 Buffer<int, 3>,
                                 Buffer<float, 3>,
                                 Buffer<double, 2>,
                                 Buffer<double, 2>,
                                 Buffer<int, 2>,
                                 Buffer<double, 2>,
                                 Buffer<int, 2>,
                                 Buffer<double, 2>,
                                 Buffer<int, 2>,
                                 Buffer<double, 2>,
                                 Buffer<int, 2>>();

        Buffer<int, 3> output(kSize, kSize, 3);
        Buffer<float, 3> extra_buffer_output(kSize, kSize, 3);
        Buffer<double, 2> extra_func_output(kSize, kSize);
        Buffer<double, 2> extra_tuple_func_output_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_func_output_1(kSize, kSize);
        Buffer<double, 2> extra_tuple_buffer_output_static_dims_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_buffer_output_static_dims_1(kSize, kSize);
        Buffer<double, 2> extra_tuple_buffer_output_dynamic_dims_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_buffer_output_dynamic_dims_1(kSize, kSize);
        Buffer<double, 2> extra_tuple_buffer_output_unset_types_0(kSize, kSize);
        Buffer<int, 2> extra_tuple_buffer_output_unset_types_1(kSize, kSize);

        // All inputs to a Callable must be fully realized, so any Func inputs
        // that the Generator has implicitly become Buffer inputs of the same type
        // and dimensionality.
        Buffer<uint16_t, 3> func_extra(kSize, kSize, 3);
        func_extra.fill(extra_func_value);

        int r = configure(input, bias,
                          // extra inputs are in the order they were added, after all predeclared inputs
                          extras[0], extras[1], extras[2],
                          typed_extra,
                          func_extra,
                          extra_scalar,
                          extra_dynamic_scalar,
                          output,
                          // extra outputs are in the order they were added, after all predeclared outputs
                          extra_buffer_output,
                          extra_func_output,
                          extra_tuple_func_output_0,
                          extra_tuple_func_output_1,
                          extra_tuple_buffer_output_static_dims_0,
                          extra_tuple_buffer_output_static_dims_1,
                          extra_tuple_buffer_output_dynamic_dims_0,
                          extra_tuple_buffer_output_dynamic_dims_1,
                          extra_tuple_buffer_output_unset_types_0,
                          extra_tuple_buffer_output_unset_types_1);

        assert(r == 0);

        output.for_each_element([&](int x, int y, int c) {
            assert(output(x, y, c) == input(x, y, c) + extra_value);
        });

        extra_buffer_output.for_each_element([&](int x, int y, int c) {
            assert(extra_buffer_output(x, y, c) == output(x, y, c));
        });

        extra_func_output.for_each_element([&](int x, int y) {
            assert(extra_func_output(x, y) == output(x, y, 0));
        });

        for (auto &buf : {extra_tuple_func_output_0,
                          extra_tuple_buffer_output_static_dims_0,
                          extra_tuple_buffer_output_dynamic_dims_0,
                          extra_tuple_buffer_output_unset_types_0}) {
            buf.for_each_element([&](int x, int y) {
                assert(buf(x, y) == output(x, y, 0));
            });
        }

        for (auto &buf : {extra_tuple_func_output_1,
                          extra_tuple_buffer_output_static_dims_1,
                          extra_tuple_buffer_output_dynamic_dims_1,
                          extra_tuple_buffer_output_unset_types_1}) {
            buf.for_each_element([&](int x, int y) {
                assert(buf(x, y) == output(x, y, 1));
            });
        }
    }

    printf("Success!\n");
    return 0;
}