This example illustrates how to perform an elementwise trinary operation using the hipTensor library.
The operation calculates the following:
where:
-
$\alpha$ ,$\beta$ , and$\gamma$ are scalars -
$A$ ,$B$ , and$C$ are tensors of dimensions$w \times h \times c$ -
$D$ is a tensor of dimensions$c \times w \times h$
- Check if F32 operations are supported on the current device.
- Define data types and set up tensor data type descriptors.
- Set scalar coefficient values.
- Define tensor modes and extents.
- Allocate host and device memory.
- Initialize tensor data.
- Create a hipTensor handle.
- Create tensor descriptors.
- Create the elementwise trinary operation descriptor.
- Create an execution plan.
- Execute the elementwise trinary operation.
- Clean up all resources.
-
hipTensor Initialization: The hipTensor library is initialized by creating a handle with
hiptensorCreate()and released withhiptensorDestroy(). -
Tensor Descriptors:
hiptensorCreateTensorDescriptor(): Creates a descriptor for a tensor, defining its data type, dimensions, and strides.hiptensorDestroyTensorDescriptor(): Frees the tensor descriptor.
-
Elementwise Trinary Operation Descriptor:
hiptensorCreateElementwiseTrinary(): Creates a descriptor for an elementwise trinary operation, specifying the tensors, their modes, and the operation types.hiptensorDestroyOperationDescriptor(): Frees the elementwise trinary operation descriptor.
-
Algorithm Selection and Execution Plan:
hiptensorCreatePlanPreference(): Creates a preference object to guide the algorithm selection process.hiptensorCreatePlan(): Creates an execution plan based on the operation descriptors and preferences.hiptensorDestroyPlanPreference(): Frees the preference object.hiptensorDestroyPlan(): Frees the execution plan.
-
Execution:
hiptensorElementwiseTrinaryExecute(): Executes the elementwise trinary operation using the created plan.
-
Key Enumerations:
hiptensorDataType_t: Defines the data type of tensors (e.g.,HIPTENSOR_R_32Ffor single-precision).hiptensorComputeDescriptor_t: Sets the precision for the computation (e.g.,HIPTENSOR_COMPUTE_32F).hiptensorOperator_t: Specifies tensor operations, such asHIPTENSOR_OP_IDENTITYandHIPTENSOR_OP_ADD.
hiptensorCreatehiptensorCreateElementwiseTrinaryhiptensorCreatePlanhiptensorCreatePlanPreferencehiptensorCreateTensorDescriptorhiptensorDestroyhiptensorDestroyOperationDescriptorhiptensorDestroyPlanhiptensorDestroyPlanPreferencehiptensorDestroyTensorDescriptorhiptensorElementwiseTrinaryExecutehiptensorLoggerSetMask
hipFreehipHostFreehipHostMallochipMallochipMemcpy
hiptensorHandle_thiptensorTensorDescriptor_thiptensorOperationDescriptor_thiptensorPlan_thiptensorPlanPreference_thiptensorDataType_thiptensorComputeDescriptor_thiptensorAlgo_tHIPTENSOR_R_32FHIPTENSOR_COMPUTE_32FHIPTENSOR_OP_IDENTITYHIPTENSOR_OP_ADDHIPTENSOR_ALGO_DEFAULT