File: stubtest_aottest.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 (103 lines) | stat: -rw-r--r-- 4,156 bytes parent folder | download | duplicates (3)
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
#include <algorithm>
#include <cstdio>

#include "HalideBuffer.h"
#include "HalideRuntime.h"
#include "stubtest.h"

using Halide::Runtime::Buffer;

const int kSize = 32;

template<typename Type>
Buffer<Type, 3> make_image(int extra) {
    Buffer<Type, 3> im(kSize, kSize, 3);
    for (int x = 0; x < kSize; x++) {
        for (int y = 0; y < kSize; y++) {
            for (int c = 0; c < 3; c++) {
                im(x, y, c) = static_cast<Type>(x + y + c + extra);
            }
        }
    }
    return im;
}

template<typename InputType, typename OutputType, int Dims>
void verify(const Buffer<InputType, Dims> &input, float float_arg, int int_arg, const Buffer<OutputType, Dims> &output) {
    if (input.width() != output.width() ||
        input.height() != output.height()) {
        fprintf(stderr, "size mismatch: %dx%d vs %dx%d\n", input.width(), input.height(), output.width(), output.height());
        exit(1);
    }
    int channels = std::max(1, std::min(input.channels(), output.channels()));
    for (int x = 0; x < output.width(); x++) {
        for (int y = 0; y < output.height(); y++) {
            for (int c = 0; c < channels; c++) {
                const OutputType expected = static_cast<OutputType>(input(x, y, c) * float_arg + int_arg);
                const OutputType actual = output(x, y, c);
                if (expected != actual) {
                    fprintf(stderr, "img[%d, %d, %d] = %f, expected %f (input = %f)\n", x, y, c, (double)actual, (double)expected, (double)input(x, y, c));
                    exit(1);
                }
            }
        }
    }
}

int main(int argc, char **argv) {
    Buffer<uint8_t, 3> buffer_input = make_image<uint8_t>(0);
    Buffer<float, 3> simple_input = make_image<float>(0);
    Buffer<float, 3> array_input0 = make_image<float>(0);
    Buffer<float, 3> array_input1 = make_image<float>(1);
    Buffer<float, 3> typed_buffer_output(kSize, kSize, 3);
    Buffer<float, 3> untyped_buffer_output(kSize, kSize, 3);
    Buffer<float, 3> tupled_output0(kSize, kSize, 3);
    Buffer<int32_t, 3> tupled_output1(kSize, kSize, 3);
    Buffer<uint8_t, 3> array_buffer_input0 = make_image<uint8_t>(0);
    Buffer<uint8_t, 3> array_buffer_input1 = make_image<uint8_t>(1);
    Buffer<float, 3> simple_output(kSize, kSize, 3);
    // TODO: see Issues #3709, #3967
    Buffer<void, 3> float16_output(halide_type_t(halide_type_float, 16), kSize, kSize, 3);
    Buffer<void, 3> bfloat16_output(halide_type_t(halide_type_bfloat, 16), kSize, kSize, 3);
    Buffer<float, 3> tuple_output0(kSize, kSize, 3), tuple_output1(kSize, kSize, 3);
    Buffer<int16_t, 3> array_output0(kSize, kSize, 3), array_output1(kSize, kSize, 3);
    Buffer<uint8_t, 3> static_compiled_buffer_output(kSize, kSize, 3);
    Buffer<uint8_t, 3> array_buffer_output0(kSize, kSize, 3), array_buffer_output1(kSize, kSize, 3);

    stubtest(
        buffer_input,
        buffer_input,
        array_buffer_input0, array_buffer_input1,
        simple_input,
        array_input0, array_input1,
        1.25f,
        33,
        66,
        simple_output,
        tuple_output0, tuple_output1,
        array_output0, array_output1,
        typed_buffer_output,
        untyped_buffer_output,
        tupled_output0, tupled_output1,
        static_compiled_buffer_output,
        array_buffer_output0, array_buffer_output1,
        float16_output,
        bfloat16_output);

    verify(buffer_input, 1.f, 0, typed_buffer_output);
    verify(buffer_input, 1.f, 0, untyped_buffer_output);
    verify(simple_input, 1.f, 0, simple_output);
    verify(simple_input, 1.f, 0, tupled_output0);
    verify(simple_input, 1.f, 1, tupled_output1);
    verify(array_input0, 1.f, 0, simple_output);
    verify(array_input0, 1.25f, 0, tuple_output0);
    verify(array_input0, 1.25f, 33, tuple_output1);
    verify(array_input0, 1.0f, 33, array_output0);
    verify(array_input1, 1.0f, 66, array_output1);
    verify(buffer_input, 1.0f, 42, static_compiled_buffer_output);
    verify(array_buffer_input0, 1.f, 1, array_buffer_output0);
    verify(array_buffer_input1, 1.f, 2, array_buffer_output1);

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