* Copyright (c) 2026 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_compute_buffer_reuse.cpp
* \brief 测试compute表达的input buffer复用
*/
#include "atvoss.h"
#include "example_common.h"
#include "utils/layout/shape.h"
static constexpr int32_t MAX_DIM = 8;
static constexpr int32_t WIDTH = 32;
template <typename T1, typename T2>
struct ComputeTestConfig {
struct ComputeTestCompute {
template <template <typename> class Tensor>
__host_aicore__ constexpr auto Compute() const
{
auto in1 = Atvoss::PlaceHolder<1, Tensor<T1>, Atvoss::ParamUsage::IN>();
auto out = Atvoss::PlaceHolder<2, Tensor<T2>, Atvoss::ParamUsage::OUT>();
return (in1 = in1 + in1, out = in1);
};
};
using ArchTag = Atvoss::Arch::DAV_3510;
using BlockOp = Atvoss::Ele::BlockBuilder<ComputeTestCompute, ArchTag>;
using KernelOp = Atvoss::Ele::KernelBuilder<BlockOp>;
using DeviceOp = Atvoss::DeviceAdapter<KernelOp>;
};
template <typename T1, typename T2>
static void Run()
{
CHECK_ACL_RET(aclInit(nullptr));
auto finalizeGuard = ReleaseSource([]() { aclFinalize(); });
const int32_t deviceId = 0;
CHECK_ACL_RET(aclrtSetDevice(deviceId));
auto deviceResetGuard = ReleaseSource([deviceId]() { aclrtResetDevice(deviceId); });
aclrtContext context = nullptr;
CHECK_ACL_RET(aclrtCreateContext(&context, deviceId));
auto contextDestroyGuard = ReleaseSource([context]() { aclrtDestroyContext(context); });
aclrtStream stream = nullptr;
CHECK_ACL_RET(aclrtCreateStream(&stream));
auto streamDestroyGuard = ReleaseSource([stream]() { aclrtDestroyStream(stream); });
void* in1 = nullptr;
CHECK_ACL_RET(aclrtMalloc(&in1, 8 * sizeof(T1), ACL_MEM_MALLOC_HUGE_FIRST));
auto input1FreeGuard = ReleaseSource([in1]() { aclrtFree(in1); });
T1* deviceIn1 = static_cast<T1*>(in1);
void* out = nullptr;
CHECK_ACL_RET(aclrtMalloc(&out, 8 * sizeof(T2), ACL_MEM_MALLOC_HUGE_FIRST));
auto outputFreeGuard = ReleaseSource([out]() { aclrtFree(out); });
T2* deviceOutput = static_cast<T2*>(out);
std::vector<T1> hostInput1(8 * sizeof(T1), static_cast<T1>(3));
CHECK_ACL_RET(aclrtMemcpy(deviceIn1, 8 * sizeof(T1), hostInput1.data(), 8 * sizeof(T1), ACL_MEMCPY_HOST_TO_DEVICE));
uint64_t shapeArray[MAX_DIM] = {8, 0, 0, 0, 0, 0, 0, 0};
Atvoss::Tensor<T1> t1(deviceIn1, shapeArray, 1);
Atvoss::Tensor<T2> t2(deviceOutput, shapeArray, 1);
auto arguments = Atvoss::ArgumentsBuilder{}.inputOutput(t1, t2).build();
using DeviceOp = typename ComputeTestConfig<T1, T2>::DeviceOp;
DeviceOp deviceOp;
deviceOp.Run(arguments, stream);
CHECK_ACL_RET(aclrtSynchronizeStream(stream));
std::vector<T2> hostOutput(8);
CHECK_ACL_RET(
aclrtMemcpy(hostOutput.data(), 8 * sizeof(T2), deviceOutput, 8 * sizeof(T2), ACL_MEMCPY_DEVICE_TO_HOST));
std::vector<T2> golden(8, 6);
if (!VerifyResults(golden, hostOutput)) {
std::cout << "Accuracy verification failed." << std::endl;
} else {
std::cout << "Accuracy verification passed." << std::endl;
}
}
int main(int argc, char const* argv[])
{
Run<int32_t, int32_t>();
return 0;
}