halide recursive filtering inhalide-lang.org/assets/lectures/cvpr_halide_recursivefiltering.pdf ·...

47
Dillon Sharlet, Google Recursive filtering in Halide

Upload: dinhliem

Post on 02-Aug-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Dillon Sharlet, Google

Recursive filtering in Halide

Page 2: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

2

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

Page 3: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

3

x =

y =

n = 0

Page 4: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

4

x =

y =

n = 1

Page 5: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

5

x =

y =

n = 2

Page 6: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

6

x =

y =

n = 3

Page 7: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

7

x =

y =

n = 4

Page 8: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

8

x =

y =

n = 5

Page 9: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

9

x =

y =

n = 6

Page 10: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

10

x =

y =

n = 7

Page 11: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

11

x =

y =

n = 8

Page 12: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

12

x =

y =

n = 9

Page 13: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

13

x =

y =

n = 10

Page 14: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● Recursive filter for a 1D signalyn = (1 - A) yn-1 + A xn

where x is input, y is output, A is the filter coefficient

● Example applied to a delta function

14

x =

y =

Page 15: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Description

● To apply this recursive filter to an image, apply it four times:a. Up and down the columnsb. Right and left across the rows

15

Input

Page 16: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Reductions● To implement this algorithm:

○ Need to reference output at previous pixel to compute current output

● This cannot be done with a pure definition● We can do this with update stages and

RDoms○ RDom (Reduction Domain) provides a serial loop○ Can have dependencies between loop iterations

16

Page 17: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

f(x, 0) += 5;

17

Funcs can have multiple stages

We call the additional ones “update” stages

They run in sequence

Multi-stage Funcs

Page 18: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

f(x, 0) += 5;

18

They can use arbitrary index expressions on the left-hand-side

Page 19: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

f(x, 0) += 5;

// f(x, 0) = f(x, 0) + 5;

19

They can recursively load values defined by the previous stage

Page 20: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

f(x, 0) += 5;

f.vectorize(x, 8);

20

They are scheduled independently

Page 21: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

f(x, 0) += 5;

f.vectorize(x, 8);

f.update(0)

.unroll(x, 2);

21

They are scheduled independently

Page 22: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

f(x, 0) += 5;

22

for y:

for x:

f[x,y] = x + y

Page 23: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

f(x, 0) += 5;

23

for y:

for x:

f[x,y] = x + y

for x:

f[x,0] = f[x,0] + 5;

Page 24: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

RDom r(1, 10);

f(x, 0) += f(x, r);

24

An update stage can be a reduction over some domain“RDom”

Page 25: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

RDom r(1, 10);

f(x, 0) += f(x, r);

25

This just throws an extra loop around the loop nest for that stage:

for r from 1 to 10:

for x:

f[x,0] = f[x,0] + f[x,r];

Page 26: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

RDom r(1, 10);

f(x, 0) += f(x, r);

f.update(0)

.unroll(r);

26

You can schedule RDom variables

Page 27: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

RDom r(1, 10);

f(x, 0) += f(x, r);

f.update(0)

.reorder(r, x);

27

You can schedule RDom variables

Page 28: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

f(x, y) = x + y;

RDom r(1, 10);

f(x, 0) += f(x, r);

f.update(0)

.parallel(r);

ERROR: Potential Race Condition

28

But only when we can prove there’s no race condition or change in meaning.

Halide’s promise: Scheduling never changes the results!

Page 29: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Generators

● Two ways to call Halide code○ JIT: Halide pipelines executed in the same process

they are defined in○ AOT: Halide pipelines compiled to object files (.o, .

obj) and linked into/called from another program via C ABI (i.e. extern “C”)

29

Page 30: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Generators● Generators are C++ programs that, when run, produce

objects (.o, .obj) and C headers (.h) containing compiled pipelines

● Applications #include generated header files declaring the functions, link to generated objects

● Pipeline functions are declared with arguments corresponding to Param objects, including ImageParams in buffer_t objects.○ Holds pointer, element size and strides of each dimension of an image○ Halide never assumes ownership of the memory a buffer_t points to

30

Page 31: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Using Generators with Matlab● Generators can also be used within Matlab (or Octave)

via the mex library interface● Halide pipeline compiled with matlab target feature

defines a suitable mexFunction wrapper○ Validates and converts mxArray to buffer_t (or scalar params)

● mex_halide Matlab function performs all the required steps to build a mex library from a source file containing a generator

31

Page 32: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Code!

Page 33: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

● So far, we’ve talked about some scheduling operators○ vectorize, unroll, etc.

● We’ve also briefly discussed compute_at● To significantly improve performance, we

need to use compute_at to improve locality

Scheduling for locality

Page 34: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_rootHere is a simple two stage pipeline

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

Page 35: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_rootThis means compute all of f, followed by all of g

Poor locality!

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

f.compute_root();

g.compute_root();

Page 36: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_rootfor f.y:

for f.x:

f[f.x,f.y] = f.x + f.y

for g.y:

for g.x:

g[g.x,g.y] = 2*f[g.x,g.y]

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

f.compute_root();

g.compute_root();

Page 37: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_at“Compute f at each iteration of y when computing g”

All stages of a Func share the same compute_at location

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

f.compute_at(g, y);

g.compute_root();

Page 38: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_atfor g.y:

for g.x:

g[g.x,g.y] = 2*f[g.x,g.y]

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

f.compute_at(g, y);

g.compute_root();

Page 39: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_atfor g.y:

for f.x:

f[f.x,g.y] = f.x + g.y

for g.x:

g[g.x,g.y] = 2*f[g.x,g.y]

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

f.compute_at(g, y);

g.compute_root();

Page 40: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_atfor g.y:

for g.x:

g[g.x,g.y] = 2*f[g.x,g.y]

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

f.compute_at(g, x);

g.compute_root();

Page 41: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

compute_atfor g.y:

for g.x:

f[g.x,g.y] = g.x + g.y

g[g.x,g.y] = 2*f[g.x,g.y]

f(x, y) = x + y;

g(x, y) = 2*f(x, y);

f.compute_at(g, x);

g.compute_root();

Page 42: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

42

Input Blur y Transpose

ImageParam

Func

Allocation

Legend:

IIR blur compute_root visualization

Transpose Blur y

Page 43: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

43

TransposeBlur y

IIR blur locality schedule visualization

Page 44: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

44

TransposeBlur y

IIR blur locality schedule visualization

Page 45: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

45

TransposeBlur y

IIR blur locality schedule visualization

Page 46: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

46

TransposeBlur y

IIR blur locality schedule visualization

Page 47: Halide Recursive filtering inhalide-lang.org/assets/lectures/CVPR_Halide_RecursiveFiltering.pdf · To apply this recursive filter to an image, ... Reductions To implement this algorithm:

Code!https://github.com/halide/CVPR2015/tree/master/RecursiveFilter