* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* \file test_expandv.cpp
* \brief
*/
#define DTYPE_X1 float
#include "../../../op_kernel/expandv.cpp"
#include "expandv_tiling.h"
#include <array>
#include <vector>
#include <iostream>
#include <string>
#include <cstdint>
#include <cstdlib>
#include "gtest/gtest.h"
#include "tikicpulib.h"
#include "data_utils.h"
using namespace std;
class ExpandvTest : public testing::Test {
protected:
static void SetUpTestCase()
{
cout << "ExpandvTest SetUp\n" << endl;
std::cout << "ExpandvTest SetUp" << std::endl;
const string cmd = "cp -rf " + dataPath + " ./";
system(cmd.c_str());
system("chmod -R 755 ./expandv_data/");
}
static void TearDownTestCase()
{
cout << "ExpandvTest TearDown\n" << endl;
}
private:
const static std::string rootPath;
const static std::string dataPath;
};
const std::string ExpandvTest::rootPath = "../../../../";
const std::string ExpandvTest::dataPath = rootPath + "experimental/math/expandv/tests/ut/op_kernel/expandv_data";
TEST_F(ExpandvTest, test_case_3d_shape)
{
const int shapeM = 4;
const int shapeN = 1;
const int shapeO = 3;
const int ExpectShapeM = 4;
const int ExpectShapeN = 5;
const int ExpectShapeO = 3;
size_t InputElements = shapeM * shapeN * shapeO;
size_t OutputElements = ExpectShapeM * ExpectShapeN * ExpectShapeO;
size_t xByteSize = InputElements * sizeof(DTYPE_X1);
size_t zByteSize = OutputElements * sizeof(DTYPE_X1);
size_t tiling_data_size = sizeof(ExpandvTilingData);
uint32_t blockDim = 4;
std::string dtype_str;
if constexpr (std::is_same<DTYPE_X1, float>::value) {
dtype_str = "float32";
} else if constexpr (std::is_same<DTYPE_X1, half>::value) {
dtype_str = "float16";
} else {
FAIL() << "Unsupported data type";
}
std::string gen_cmd = "cd ./expandv_data/ && python3 gen_data.py '(4, 1, 3)' '" + dtype_str + "'" + "'(4, 5, 3)' '";
system(gen_cmd.c_str());
uint8_t* x = (uint8_t*)AscendC::GmAlloc(xByteSize);
uint8_t* z = (uint8_t*)AscendC::GmAlloc(zByteSize);
uint8_t* workspace = (uint8_t*)AscendC::GmAlloc(1024 * 1024 * 16);
uint8_t* tiling = (uint8_t*)AscendC::GmAlloc(tiling_data_size);
std::string fileNameX = "./expandv_data/" + dtype_str + "_input_x_expandv.bin";
ReadFile(fileNameX, xByteSize, x, xByteSize);
ExpandvTilingData* tilingDatafromBin = reinterpret_cast<ExpandvTilingData*>(tiling);
if constexpr (std::is_same<DTYPE_X1, float>::value) {
tilingDatafromBin->smallCoreDataNum = 64;
tilingDatafromBin->bigCoreDataNum = 72;
tilingDatafromBin->finalBigTileNum = 1;
tilingDatafromBin->finalSmallTileNum = 1;
tilingDatafromBin->tileDataNum = 8184;
tilingDatafromBin->smallTailDataNum = 64;
tilingDatafromBin->bigTailDataNum = 72;
tilingDatafromBin->tailBlockNum = 0;
} else if constexpr (std::is_same<DTYPE_X1, half>::value) {
tilingDatafromBin->smallCoreDataNum = 64;
tilingDatafromBin->bigCoreDataNum = 80;
tilingDatafromBin->finalBigTileNum = 1;
tilingDatafromBin->finalSmallTileNum = 1;
tilingDatafromBin->tileDataNum = 16368;
tilingDatafromBin->smallTailDataNum = 64;
tilingDatafromBin->bigTailDataNum = 80;
tilingDatafromBin->tailBlockNum = 0;
}
tilingDatafromBin->in_rank = 3;
tilingDatafromBin->out_rank = 3;
tilingDatafromBin->inShapeArr[0] = 4;
tilingDatafromBin->inShapeArr[1] = 1;
tilingDatafromBin->inShapeArr[2] = 3;
tilingDatafromBin->outShapeArr[0] = 4;
tilingDatafromBin->outShapeArr[1] = 5;
tilingDatafromBin->outShapeArr[2] = 3;
tilingDatafromBin->inStrideArr[0] = 3;
tilingDatafromBin->inStrideArr[1] = 3;
tilingDatafromBin->inStrideArr[2] = 1;
tilingDatafromBin->outStrideArr[0] = 15;
tilingDatafromBin->outStrideArr[1] = 3;
tilingDatafromBin->outStrideArr[2] = 1;
ICPU_SET_TILING_KEY(0);
AscendC::SetKernelMode(KernelMode::AIV_MODE);
ICPU_RUN_KF(expandv<0>,
blockDim,
x,
z,
workspace,
(uint8_t *)(tilingDatafromBin));
std::string fileNameZ = "./expandv_data/" + dtype_str + "_output_expandv.bin";
WriteFile(fileNameZ, z, zByteSize);
AscendC::GmFree(x);
AscendC::GmFree(z);
AscendC::GmFree(workspace);
AscendC::GmFree(tiling);
std::string compare_cmd = "cd ./expandv_data/ && python3 compare_data.py '" + dtype_str + "'";
system(compare_cmd.c_str());
}