* 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.
*/
#include "../demo_util.h"
using namespace atb;
using namespace std;
const int VECTOR_SIZE = 4;
const float INIT_VALUE = 2.0f;
* @brief 准备atb::VariantPack中的所有输入tensor
* @param contextPtr context指针
* @param stream stream
* @param inTensors atb::VariantPack中的输入tensor
* @note 需要传入所有host侧tensor
* @return atb::Status 错误码
*/
atb::Status PrepareInTensor(atb::Context *contextPtr, aclrtStream stream, atb::SVector<atb::Tensor> &inTensors)
{
uint32_t dim0 = 2;
uint32_t dim1 = 2;
std::vector<float> tensormul0(VECTOR_SIZE, INIT_VALUE);
atb::Tensor tensorMul0;
CHECK_STATUS(CreateTensorFromVector(contextPtr, stream, tensormul0, ACL_FLOAT16, aclFormat::ACL_FORMAT_ND,
{dim0, dim1}, tensorMul0));
std::vector<float> tensormul1(VECTOR_SIZE, INIT_VALUE);
atb::Tensor tensorMul1;
CHECK_STATUS(CreateTensorFromVector(contextPtr, stream, tensormul1, ACL_FLOAT16, aclFormat::ACL_FORMAT_ND,
{dim0, dim1}, tensorMul1));
inTensors = {tensorMul0, tensorMul1};
return atb::ErrorType::NO_ERROR;
}
* @brief 创建一个ELEWISE_MUL的Operation,并设置参数
* @param atb::Operation * 创建一个Operation指针
* @return atb::Status 错误码
*/
atb::Status PrepareOperation(atb::Operation **op)
{
atb::infer::ElewiseParam mulParam;
mulParam.elewiseType = atb::infer::ElewiseParam::ElewiseType::ELEWISE_MUL;
return atb::CreateOperation(mulParam, op);
}
int main(int argc, char **argv)
{
CHECK_STATUS(aclInit(nullptr));
int32_t deviceId = 0;
CHECK_STATUS(aclrtSetDevice(deviceId));
atb::Context *context = nullptr;
CHECK_STATUS(atb::CreateContext(&context));
void *stream = nullptr;
CHECK_STATUS(aclrtCreateStream(&stream));
context->SetExecuteStream(stream);
atb::Operation *op = nullptr;
CHECK_STATUS(PrepareOperation(&op));
atb::VariantPack variantPack;
PrepareInTensor(context, stream, variantPack.inTensors);
atb::Tensor tensorOut;
CreateTensor(ACL_FLOAT16, aclFormat::ACL_FORMAT_ND, {2, 2}, tensorOut);
variantPack.outTensors.push_back(tensorOut);
uint64_t workspaceSize = 0;
CHECK_STATUS(op->Setup(variantPack, workspaceSize, context));
uint8_t *workspacePtr = nullptr;
if (workspaceSize > 0) {
CHECK_STATUS(aclrtMalloc((void **)(&workspacePtr), workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST));
}
op->Execute(variantPack, workspacePtr, workspaceSize, context);
CHECK_STATUS(aclrtSynchronizeStream(stream));
for (atb::Tensor &inTensor : variantPack.inTensors) {
CHECK_STATUS(aclrtFree(inTensor.deviceData));
}
if (workspaceSize > 0) {
CHECK_STATUS(aclrtFree(workspacePtr));
}
CHECK_STATUS(atb::DestroyOperation(op));
CHECK_STATUS(aclrtDestroyStream(stream));
CHECK_STATUS(atb::DestroyContext(context));
CHECK_STATUS((aclFinalize()));
std::cout << "elewise_mul demo success!" << std::endl;
return 0;
}