Importing an AI Engine Kernel with Function Template - 2023.2 English

Vitis Model Composer User Guide (UG1483)

Document ID
UG1483
Release Date
2023-11-15
Version
2023.2 English

You might require a generic function that can be used for different datatypes. Using templates, you can pass a datatype as a parameter and Vitis Model Composer supports importing an AI Engine Kernel with a function template. To do this, use the same AIE Kernel block used earlier to import the ordinary C++ functions.

As an example to import the kernel function with templates, consider below header file kernel.h, containing the declaration of a function template. Here, the template has typename template parameter T, and a non-type (integral) template parameter N.

kernel.h

#ifndef _AIE_TEMP_KERNELS_H_
#define _AIE_TEMP_KERNELS_H_

#include <adf.h>

using namespace adf;

template<typename T, int N>
void myFunc(input_buffer<T> *i1,
 output_buffer<T> *o1
 );

#endif // ifndef _AIE_TEMP_KERNELS_H

The definition of the function template is in the source file kernel.cpp as shown below.

kernel.cpp

#include "kernel.h"

template<typename T, int N>
void myFunc(input_buffer<T> *i1, output_buffer<T> *o1)
{
     auto pIn  = aie::begin(i1);
     auto pOut = aie::begin(o1);

     for(int i = 0; i < 8; i++)
     { 
          *pOut++ *= *pIn++;
     }
}

Notice the usage of the non-type template parameter 'N' in the kernel output computation. To import the template function as a block into Model Composer, double-click the AIE Kernel block and update the parameters as follows.

Kernel header file
kernel.h
Kernel function
myFunc
Kernel Init function
Leave empty
Kernel source file
kernel.cpp
Kernel search path
Leave empty
Preprocessor options
Leave empty

When you click the Import button in the Block Parameters dialog box, the Function tab displays automatically. Enter the values of a template type parameter 'T' and a template non-type parameter of integral type within the Function Template Parameters section as shown in the following figure. Double-click the appropriate editable field and enter the values. You can also review the declaration of the template function in the Function declaration section.

Figure 1. AIE Kernel: Function declaration Section

The following typenames are supported as type template parameters:

  • int8, int16, int32, int64
  • uint8, uint16, uint32, uint64
  • float, double
  • cint16, cint32, cfloat

Scroll down to the Port attributes section in the Function tab and enter appropriate user-editable configuration parameters as shown.

After entering the appropriate values in the Function tab, click Apply. Notice the updated interface of the AIE kernel block GUI as shown in the following figure.

Figure 2. AIE Kernel: Updated