File: interpolate_generator.py

package info (click to toggle)
halide 21.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 55,412 kB
  • sloc: cpp: 289,327; 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 (235 lines) | stat: -rw-r--r-- 7,523 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
"""
Fast image interpolation using a pyramid.
"""

import halide as hl


def _func_list(name, size):
    """Return a list containing `size` Funcs, named `name_n` for n in 0..size-1."""
    return [hl.Func(f"{name}_{i}") for i in range(size)]


@hl.alias(
    interpolate_Mullapudi2016={"autoscheduler": "Mullapudi2016"},
)
@hl.generator()
class interpolate:
    levels = hl.GeneratorParam(10)

    input_buf = hl.InputBuffer(hl.Float(32), 3)
    output_buf = hl.OutputBuffer(hl.Float(32), 3)

    def generate(self):
        g = self

        x, y, c = hl.vars("x y c")

        # Input must have four color channels - rgba
        g.input_buf.dim(2).set_bounds(0, 4)

        downsampled = _func_list("downsampled", g.levels)
        downx = _func_list("downx", g.levels)
        interpolated = _func_list("interpolated", g.levels)
        upsampled = _func_list("upsampled", g.levels)
        upsampledx = _func_list("upsampledx", g.levels)

        clamped = hl.BoundaryConditions.repeat_edge(g.input_buf)

        downsampled[0][x, y, c] = hl.select(
            c < 3,
            clamped[x, y, c] * clamped[x, y, 3],
            clamped[x, y, 3],
        )

        for lv in range(1, g.levels):
            prev = downsampled[lv - 1]

            if lv == 4:
                # Also add a boundary condition at a middle pyramid level
                # to prevent the footprint of the downsamplings to extend
                # too far off the base image. Otherwise we look 512
                # pixels off each edge.
                w = g.input_buf.width() / (1 << (lv - 1))
                h = g.input_buf.height() / (1 << (lv - 1))
                prev = hl.lambda_func(
                    x, y, c, prev[hl.clamp(x, 0, w), hl.clamp(y, 0, h), c]
                )

            downx[lv][x, y, c] = (
                prev[x * 2 - 1, y, c] + 2 * prev[x * 2, y, c] + prev[x * 2 + 1, y, c]
            ) * 0.25

            downsampled[lv][x, y, c] = (
                downx[lv][x, y * 2 - 1, c]
                + 2 * downx[lv][x, y * 2, c]
                + downx[lv][x, y * 2 + 1, c]
            ) * 0.25

        interpolated[g.levels - 1][x, y, c] = downsampled[g.levels - 1][x, y, c]

        for lv in range(g.levels - 2, -1, -1):
            upsampledx[lv][x, y, c] = (
                interpolated[lv + 1][x / 2, y, c]
                + interpolated[lv + 1][(x + 1) / 2, y, c]
            ) / 2
            upsampled[lv][x, y, c] = (
                upsampledx[lv][x, y / 2, c] + upsampledx[lv][x, (y + 1) / 2, c]
            ) / 2
            alpha = 1.0 - downsampled[lv][x, y, 3]
            interpolated[lv][x, y, c] = (
                downsampled[lv][x, y, c] + alpha * upsampled[lv][x, y, c]
            )

        g.output_buf[x, y, c] = interpolated[0][x, y, c] / interpolated[0][x, y, 3]

        # Schedule
        if g.using_autoscheduler():
            # nothing
            pass
        elif g.target().has_gpu_feature():
            # 0.86ms on a 2060 RTX
            yo, yi, xo, xi, xii, yii = hl.vars("yo yi xo xi xii yii")

            (
                g.output_buf.bound(x, 0, g.input_buf.width())
                .bound(y, 0, g.input_buf.height())
                .bound(c, 0, 3)
                .reorder(c, x, y)
                .tile(x, y, xi, yi, 32, 32, hl.TailStrategy.RoundUp)
                .tile(xi, yi, xii, yii, 2, 2)
                .gpu_blocks(x, y)
                .gpu_threads(xi, yi)
                .unroll(xii)
                .unroll(yii)
                .unroll(c)
            )

            for lv in range(1, g.levels):
                (
                    downsampled[lv]
                    .compute_root()
                    .reorder(c, x, y)
                    .unroll(c)
                    .gpu_tile(x, y, xi, yi, 16, 16)
                )

            for lv in range(3, g.levels, 2):
                (
                    interpolated[lv]
                    .compute_root()
                    .reorder(c, x, y)
                    .tile(x, y, xi, yi, 32, 32, hl.TailStrategy.RoundUp)
                    .tile(xi, yi, xii, yii, 2, 2)
                    .gpu_blocks(x, y)
                    .gpu_threads(xi, yi)
                    .unroll(xii)
                    .unroll(yii)
                    .unroll(c)
                )

            (
                upsampledx[1]
                .compute_at(g.output_buf, x)
                .reorder(c, x, y)
                .tile(x, y, xi, yi, 2, 1)
                .unroll(xi)
                .unroll(yi)
                .unroll(c)
                .gpu_threads(x, y)
            )

            (
                interpolated[1]
                .compute_at(g.output_buf, x)
                .reorder(c, x, y)
                .tile(x, y, xi, yi, 2, 2)
                .unroll(xi)
                .unroll(yi)
                .unroll(c)
                .gpu_threads(x, y)
            )

            (
                interpolated[2]
                .compute_at(g.output_buf, x)
                .reorder(c, x, y)
                .unroll(c)
                .gpu_threads(x, y)
            )

        else:
            # 4.54ms on an Intel i9-9960X using 16 threads
            xo, xi, yo, yi = hl.vars("xo xi yo yi")
            vec = g.natural_vector_size(hl.Float(32))
            for lv in range(1, g.levels - 1):
                # We must refer to the downsampled stages in the
                # upsampling later, so they must all be
                # compute_root or redundantly recomputed, as in
                # the local_laplacian app.
                (
                    downsampled[lv]
                    .compute_root()
                    .reorder(x, c, y)
                    .split(y, yo, yi, 8)
                    .parallel(yo)
                    .vectorize(x, vec)
                )

            # downsampled[0] takes too long to compute_root, so
            # we'll redundantly recompute it instead.  Make a
            # separate clone of it in the first downsampled stage
            # so that we can schedule the two versions
            # separately.
            (
                downsampled[0]
                .clone_in(downx[1])
                .store_at(downsampled[1], yo)
                .compute_at(downsampled[1], yi)
                .reorder(c, x, y)
                .unroll(c)
                .vectorize(x, vec)
            )

            (
                g.output_buf.bound(x, 0, g.input_buf.width())
                .bound(y, 0, g.input_buf.height())
                .bound(c, 0, 3)
                .split(x, xo, xi, vec)
                .split(y, yo, yi, 32)
                .reorder(xi, c, xo, yi, yo)
                .unroll(c)
                .vectorize(xi)
                .parallel(yo)
            )

            for lv in range(1, g.levels):
                (
                    interpolated[lv]
                    .store_at(g.output_buf, yo)
                    .compute_at(g.output_buf, yi)
                    .vectorize(x, vec)
                )

        # Estimates (for autoscheduler; ignored otherwise)
        (
            g.input_buf.dim(0)
            .set_estimate(0, 1536)
            .dim(1)
            .set_estimate(0, 2560)
            .dim(2)
            .set_estimate(0, 4)
        )
        (
            g.output_buf.output_buffer()
            .dim(0)
            .set_estimate(0, 1536)
            .dim(1)
            .set_estimate(0, 2560)
            .dim(2)
            .set_estimate(0, 3)
        )


if __name__ == "__main__":
    hl.main()