Tool/software:
Hello,
We are trying to run out model on EdgeAI 9.20.07 https://github.com/TexasInstruments/edgeai-tidl-tools/tree/09_02_07_00?tab=readme-ov-file.
It has TopK, So out team came up with the implementation in the attached files. Other changes required in the SDK9.2 we are able to make and build and are able to run for fp32, i.e. tensor_bits 32. Also our model works fine without C7x offload (running with -d option). We are currently having problems running int16 i.e tensor_bits 16. The Topk output is correct when the values are checked inside the function. The following Data Convert layer mutates the values and the final output is incorrect.
Since two output buffers are not supported yet in SDK9.2, we are just passing the 'indexes' for now. Since, the 'indexes' output is independent of the input type and quantization parameters, the following Data Convert should not apply those parameters.
Please help review the attached implementation and let us know the changes required for int16 execution.
/* * * Copyright (c) {2015 - 2020} Texas Instruments Incorporated * * All rights reserved not granted herein. * * Limited License. * * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive * license under copyrights and patents it now or hereafter owns or controls to make, * have made, use, import, offer to sell and sell ("Utilize") this software subject to the * terms herein. With respect to the foregoing patent license, such license is granted * solely to the extent that any such patent is necessary to Utilize the software alone. * The patent license shall not apply to any combinations which include this software, * other than combinations with devices manufactured by or for TI ("TI Devices"). * No hardware patent is licensed hereunder. * * Redistributions must preserve existing copyright notices and reproduce this license * (including the above copyright notice and the disclaimer and (if applicable) source * code license limitations below) in the documentation and/or other materials provided * with the distribution * * Redistribution and use in binary form, without modification, are permitted provided * that the following conditions are met: * * * No reverse engineering, decompilation, or disassembly of this software is * permitted with respect to any software provided in binary form. * * * any redistribution and use are licensed by TI for use only with TI Devices. * * * Nothing shall obligate TI to provide you with source code for the software * licensed and provided to you in object code. * * If software source code is provided to you, modification and redistribution of the * source code are permitted provided that the following conditions are met: * * * any redistribution and use of the source code, including any resulting derivative * works, are licensed by TI for use only with TI Devices. * * * any redistribution and use of any object code compiled from the source code * and any resulting derivative works, are licensed by TI for use only with TI Devices. * * Neither the name of Texas Instruments Incorporated nor the names of its suppliers * * may be used to endorse or promote products derived from this software without * specific prior written permission. * * DISCLAIMER. * * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * */ /** * \file tidl_topK.c * * \brief This file defines kernel functions for TopK layer */ /* ========================================================================== */ /* Include Files */ /* ========================================================================== */ #include "tidl_alg_int.h" #include "tidl_commonUtils.h" #include "tidl_topK.h" #include "math.h" #include <limits> //#define TIDL_REF_BATCH_NORM_DEBUG /* ========================================================================== */ /* Macros & Typedefs */ /* ========================================================================== */ /* None */ /* ========================================================================== */ /* Structure Declarations */ /* ========================================================================== */ /* None */ /* ========================================================================== */ /* Function Declarations */ /* ========================================================================== */ /* None */ /* ========================================================================== */ /* Global Variables */ /* ========================================================================== */ /* None */ /* ========================================================================== */ /* Internal/Private Function Declarations */ /* ========================================================================== */ /* ========================================================================== */ /* Function Definitions */ /* ========================================================================== */ /* ========================================================================== */ /* Static Function Definitions */ /* ========================================================================== */ template <class Tin, class Tout0, class Tout1> static void TIDL_refTopK_new(sTIDL_Network_t *net, Tin *pIn, int32_t length, int32_t k, Tout0 *pOut0, Tout1 *pOut1, const sTIDL_Layer_t *tidlLayer) { Tin value; Tin *inData = (Tin *)pIn; Tout0 *outData0 = (Tout0 *)pOut0; Tout1 *outData1 = (Tout1 *)pOut1; int32_t x1, x2, x3; for(x1 = 0; x1 < k; x1++) { outData0[x1] = (Tout0)0; } for(x1 = 0; x1 < k; x1++) { outData1[x1] = std::numeric_limits<Tout1>::lowest(); } for(x1 = 0; x1 < length; x1++) { value = inData[x1]; for(x2 = 0; x2 < k; x2++) { if(value > (Tin)(outData1[x2])) { for(x3 = k - 1; x3 >= x2 + 1; x3--) { outData0[x3] = outData0[x3 - 1]; outData1[x3] = outData1[x3 - 1]; } outData0[x2] = (Tout0)x1; outData1[x2] = (Tout1)value; std::cout << "Inside TopK: index-" << outData0[x2] << ", value-" << outData1[x2] << std::endl; break; } } } } static int32_t TIDL_refTopKProcess(TIDL_NetworkCommonParams *commonParams, sTIDL_AlgLayer_t *algLayer, const sTIDL_Layer_t *tidlLayer, sTIDL_TopKParams_t *params, void *inPtr, void *outPtr0, void *outPtr1, const sTIDL_DataParams_t *inDataParams, const sTIDL_DataParams_t *outDataParams) { int32_t status = TIDL_SUCCESS; int32_t k = params->k; if (inDataParams->elementType == TIDL_SinglePrecFloat) { TIDL_refTopK_new(commonParams->createParams->net, (float32_tidl*)inPtr, inDataParams->dimValues[TIDL_DIM_WIDTH], k, (float32_tidl*)outPtr0, (float32_tidl*)(((float32_tidl*)outPtr0) + (k)), tidlLayer ); } else if (inDataParams->elementType == TIDL_UnsignedShort) { TIDL_refTopK_new(commonParams->createParams->net, (uint16_t*)inPtr, inDataParams->dimValues[TIDL_DIM_WIDTH], k, (uint16_t*)outPtr0, (uint16_t*)(((uint16_t*)outPtr0) + (k)), tidlLayer ); } else if (inDataParams->elementType == TIDL_SignedShort) { TIDL_refTopK_new(commonParams->createParams->net, (int16_t*)inPtr, inDataParams->dimValues[TIDL_DIM_WIDTH], k, (int16_t*)outPtr0, (int16_t*)(((int16_t*)outPtr0) + (k)), tidlLayer ); } else if (inDataParams->elementType == TIDL_UnsignedChar) { TIDL_refTopK_new(commonParams->createParams->net, (uint8_t*)inPtr, inDataParams->dimValues[TIDL_DIM_WIDTH], k, (uint8_t*)outPtr0, (uint8_t*)(((uint8_t*)outPtr0) + (k)), tidlLayer ); } else if (inDataParams->elementType == TIDL_SignedChar) { TIDL_refTopK_new(commonParams->createParams->net, (int8_t*)inPtr, inDataParams->dimValues[TIDL_DIM_WIDTH], k, (int8_t*)outPtr0, (int8_t*)(((int8_t*)outPtr0) + (k)), tidlLayer ); } return status; } int32_t TIDL_topKProcess(TIDL_NetworkCommonParams *commonParams, sTIDL_AlgLayer_t *algLayer, sTIDL_Layer_t *tidlLayer, void *inPtrs[], void *outPtrs[], int32_t layerIdx) { int32_t status = TIDL_SUCCESS; uint8_t (*inPtr) = (uint8_t (*))(inPtrs[0]); int8_t (*outPtr0) = (int8_t (*))(outPtrs[0]); int8_t (*outPtr1) = (int8_t (*))(outPtrs[1]); printf("\n****** in TOPK ***********\n"); sTIDL_TopKParams_t *params = &tidlLayer->layerParams.topKParams; sTIDL_DataParams_t *inDataParams; inDataParams = TIDL_getDataParams(commonParams->createParams->net, tidlLayer->inData[0]); status = TIDL_refTopKProcess(commonParams, algLayer, tidlLayer, params, inPtr, outPtr0, outPtr1, inDataParams, &tidlLayer->outData); //print values printf("debug_TopK_output: k-%d, eltType-%d\n", params->k, tidlLayer->outData.elementType); if(tidlLayer->outData.elementType == TIDL_SinglePrecFloat) { float *tst = (float *)(outPtr0); printf("indexes - "); for (int i = 0; i < params->k; i++) { printf("%f ", tst[i]); } printf("\nvalues - "); float *gst = (float *)(&(tst[params->k])); for (int i = 0; i < params->k; i++) { printf("%f ", gst[i]); } printf("\n"); } else if (tidlLayer->outData.elementType == TIDL_UnsignedShort) { uint16_t *tst = (uint16_t *)(outPtr0); printf("indexes - "); for (int i = 0; i < params->k; i++) { printf("%d ", tst[i]); } printf("\nvalues - "); uint16_t *gst = (uint16_t *)(&(tst[params->k])); for (int i = 0; i < params->k; i++) { printf("%d ", gst[i]); } printf("\n"); } else if (tidlLayer->outData.elementType == TIDL_SignedShort) { int16_t *tst = (int16_t *)(outPtr0); printf("indexes - "); for (int i = 0; i < params->k; i++) { printf("%d ", tst[i]); } printf("\nvalues - "); int16_t *gst = (int16_t *)(&(tst[params->k])); for (int i = 0; i < params->k; i++) { printf("%d ", gst[i]); } printf("\n"); } else if (tidlLayer->outData.elementType == TIDL_UnsignedChar) { uint8_t *tst = (uint8_t *)(outPtr0); printf("indexes - "); for (int i = 0; i < params->k; i++) { printf("%d ", tst[i]); } printf("\nvalues - "); uint8_t *gst = (uint8_t *)(&(tst[params->k])); for (int i = 0; i < params->k; i++) { printf("%d ", gst[i]); } printf("\n"); } else if (tidlLayer->outData.elementType == TIDL_SignedChar) { int8_t *tst = (int8_t *)(outPtr0); printf("indexes - "); for (int i = 0; i < params->k; i++) { printf("%d ", tst[i]); } printf("\nvalues - "); int8_t *gst = (int8_t *)(&(tst[params->k])); for (int i = 0; i < params->k; i++) { printf("%d ", gst[i]); } printf("\n"); } commonParams->createParams->net->TIDLLayers[layerIdx].outData.tensorScale = 1.0f; if((commonParams->createParams->flowCtrl & TIDL_FLOW_CTRL_REF_STAT) == TIDL_FLOW_CTRL_REF_STAT) { TIDL_Obj intAlgObj; TIDL_CreateParams createParams; memcpy(&createParams, commonParams->createParams, sizeof(TIDL_CreateParams)); intAlgObj.createParams = (TIDL_CreateParams *) &createParams; TIDL_UpdateScaleFactors(&intAlgObj, layerIdx, 0, 0, 1); } TIDL_enableL1DandL2CacheWb(); return status; }