Many applications are built with small kernels, while many kernels may have different sizes of inputs, the computation patterns are exactly the same. Considering a neural network with tens of convolutional kernels, it may be tedious for users to manually add optimization primitives for each of them, even though they use the same optimization method.
To tackle the above challenge and provide a better interface for users to declare and reuse optimizations, we propose parameterized customization template. Three new operations are introduced:
hcl.customization: A customization is basically a sequence of optimization primitives. It can take in a stage with the loop axes and input/output tensors, and apply those primitives. It has a similar interface with builtin.func, where the optimization primitives are written in the body.
hcl.apply: This operation applies a customization to an algorithm. It acts like a call operation.
hcl.end: The terminator of the region of hcl.customization.
The following code snippet gives an example of defining a sequence of optimizations for a GEMM kernel. Here we take in two input matrices A and B and an output matrix C, with the stage handle and the loop handles. We can see that the types and the sizes of the tensor need not be given, since the optimization should work for different inputs once it is a GEMM kernel. We use ? to match a dimension with un unknown size, and !hcl.Type to match generic types. This provides a polymorphic way to define optimizations.
Defining optimizations like this, we can have the below advantages:
Modularity: It can be fully decoupled from algorithm specification. It is no longer a monolithic design, and users can easily know those optimizations are for which kernel.
Portability: The customization can be stored in a separate file and can be easily imported into different application programs.
Composability: The customization is not only in a readable format, but can also be parsed by the MLIR compiler. Users can read in the customization, add new optimizations, and store them back to file. It would be also helpful for the compiler to perform DSE.
The algorithm part is shown below, where there are two GEMM kernels with different size. We can easily apply the same customization without writing extra code.
Later users can also add constraints to the customization to further specify what kind of kernels can use this optimization. Since the customization is a template, partial specialization can later be supported.
Many applications are built with small kernels, while many kernels may have different sizes of inputs, the computation patterns are exactly the same. Considering a neural network with tens of convolutional kernels, it may be tedious for users to manually add optimization primitives for each of them, even though they use the same optimization method.
To tackle the above challenge and provide a better interface for users to declare and reuse optimizations, we propose parameterized customization template. Three new operations are introduced:
hcl.customization
: A customization is basically a sequence of optimization primitives. It can take in a stage with the loop axes and input/output tensors, and apply those primitives. It has a similar interface withbuiltin.func
, where the optimization primitives are written in the body.hcl.apply
: This operation applies acustomization
to an algorithm. It acts like acall
operation.hcl.end
: The terminator of the region ofhcl.customization
.The following code snippet gives an example of defining a sequence of optimizations for a GEMM kernel. Here we take in two input matrices
A
andB
and an output matrixC
, with the stage handle and the loop handles. We can see that the types and the sizes of the tensor need not be given, since the optimization should work for different inputs once it is a GEMM kernel. We use?
to match a dimension with un unknown size, and!hcl.Type
to match generic types. This provides a polymorphic way to define optimizations.Defining optimizations like this, we can have the below advantages:
The algorithm part is shown below, where there are two GEMM kernels with different size. We can easily apply the same
customization
without writing extra code.Later users can also add constraints to the customization to further specify what kind of kernels can use this optimization. Since the customization is a template, partial specialization can later be supported.