TilingData Structure Definition

Function Usage

Defines a TilingData class and adds required member variables (TilingData fields) to store required TilingData parameters. After the TilingData class is defined, it can inherit the TilingDef class (base class for storing and processing user-defined member variables of the Tiling structure) to provide the following APIs:

  • set_+field_name: sets field values of the TilingData class. field_name is the field name added when the TilingData class is defined.
  • SaveToBuffer: serializes and saves tiling data.
  • GetDataSize: obtains the length of tiling data.
  • CheckAlignAndGenPlaceHolder (internal associated API): checks whether member variables in the Tiling structure meet byte alignment requirements on the framework side and supplements unaligned variables. Developers can ignore this API.
  • SetDataPtr: reserved and can be ignored by developers.

Prototype

  • Define a TilingData class.

    BEGIN_TILING_DATA_DEF(class_name)

  • Add a TilingData field of a common data type.

    TILING_DATA_FIELD_DEF(data_type, field_name)

  • Add a TilingData field of the array type. The elements of the array are of a common data type.

    TILING_DATA_FIELD_DEF_ARR(arr_type, arr_size, field_name)

  • Add a TilingData field of the structure type.

    TILING_DATA_FIELD_DEF_STRUCT(struct_type, field_name)

  • End the definition.

    END_TILING_DATA_DEF

Parameters

Table 1 BEGIN_TILING_DATA_DEF parameters

Parameter

Input/Output

Description

class_name

Input

User-defined tiling structure name, whose naming rule must be consistent with that of the C++ variable name.

Table 2 TILING_DATA_FIELD_DEF parameters

Parameter

Input/Output

Description

data_type

Input

Field data type.

field_name

Input

Field name, whose naming rule must be consistent with that of the C++ variable name.

Table 3 TILING_DATA_FIELD_DEF_ARR parameters

Parameter

Input/Output

Description

arr_type

Input

Data type of array elements.

arr_size

Input

Number of array elements.

field_name

Input

Field name, whose naming rule must be consistent with that of the C++ variable name.

Table 4 TILING_DATA_FIELD_DEF_STRUCT parameters

Parameter

Input/Output

Description

struct_type

Input

Structure type.

field_name

Input

Field name, whose naming rule must be consistent with that of the C++ variable name.

Constraints

  • The header file register/tilingdata_base.h must be included during function use.
  • Variables defined in TILING_DATA_FIELD_DEF and TILING_DATA_FIELD_DEF_ARR support only the int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, and float data types.
  • The struct_type in TILING_DATA_FIELD_DEF_STRUCT supports only the tiling structure defined by BEGIN_TILING_DATA_DEF and does not support structures defined by C++ syntax.
  • To set parameter values and use tiling data on the host, you need to call the set_xxx and get_xxx APIs (replace xxx with corresponding field names). For details, see the calling examples.
  • The tiling data members must meet the byte alignment requirements. That is, the offset of the current data member dataVar in the structure must meet the following requirement: offset % sizeof(dataVar) == 0.
  • The tiling structure is a global attribute. Note that the structure name must be used as the globally unique identifier. If different operators register different tiling structures with the same name, undefined behavior occurs.

Example

#include "register/tilingdata_base.h"

// Define the TilingData class.
namespace optiling {
BEGIN_TILING_DATA_DEF(Matmul)
  TILING_DATA_FIELD_DEF(uint16_t, mmVar);
  TILING_DATA_FIELD_DEF_ARR(uint16_t, 3, mmArr);
END_TILING_DATA_DEF;
// Register an intermediate structure. The first parameter is fixed to struct_name#Op, and the second parameter is struct_name. For example, if struct_name is Matmul, the first parameter is MatmulOp and the second parameter is Matmul.
REGISTER_TILING_DATA_CLASS(MatmulOp, Matmul)      // Register an intermediate structure.

BEGIN_TILING_DATA_DEF(AddCustomTilingData)        // Register a tiling class and uses the tiling name as the input parameter.
  TILING_DATA_FIELD_DEF(uint32_t, blkDim);        // Add a tiling field of the variable type to compute the number of cores.
  TILING_DATA_FIELD_DEF(uint32_t, totalSize);     // Add a tiling field of the variable type to compute the total data size.
  TILING_DATA_FIELD_DEF(uint32_t, splitTile);     // Add a tiling field of the variable type and compute data processed by each core by block.
  TILING_DATA_FIELD_DEF_ARR(uint16_t, 3, arrSample);    // Add a tiling field of the array type.
  TILING_DATA_FIELD_DEF_STRUCT(Matmul, mm);             // Add a tiling field of the structure type.
END_TILING_DATA_DEF;                                    // Definition ends.
// Register the operator TilingData class with the corresponding AddCustom operator.
REGISTER_TILING_DATA_CLASS(AddCustom, AddCustomTilingData) 
}

// Set parameter values and use tiling parameters on the host.
static void TilingAddInit(AddCustomTilingData *tiling, uint32_t blockDim)
{
  // Set parameter values.
  tiling->set_blkDim(blockDim);                  // Assign a value to the blockDim variable of a common data type.
  uint16_t arr[] = {10,2,8,2,3,4,5,2,1,2,4,4,5,};
  tiling->set_arrSample(arr);                    // Assign a value to the arrSample array variable of a common data type. Only the first three pieces of arr data are copied, which are the same as those in arr_size in TILING_DATA_FIELD_DEF_ARR.
  tiling->mm.set_mmVar(1);                       // Assign a value to the mmVar variable of a common data type in the nested structure.
  tiling->mm.set_mmArr(arr);                     // Assign a value to the mmArr array of a common data type in the nested structure.
  
  // Use the parameter values.
  uint32_t useBlockDim = tiling->get_blkDim();    // Obtain the blockDim variable of a common data type.
  uint32_t* arrPoint = tiling->get_arrSample();   // Obtain the arrSample array variable of a common data type.
  useBlockDim = tiling->mm.get_mmVar();           // Obtain the mmVar variable of a common data type in the nested structure.
  arrPoint = tiling->mm.get_mmArr();              // Obtain the mmArr array of the common data type in the nested structure.
}