/**

 * 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 "stateless_normal.h"

#include "op_api/aclnn_check.h"

#include "opdev/aicpu/aicpu_task.h"

#include "opdev/data_type_utils.h"

#include "opdev/format_utils.h"

#include "opdev/make_op_executor.h"

#include "opdev/op_def.h"

#include "opdev/op_dfx.h"

#include "opdev/op_executor.h"

#include "opdev/op_log.h"

#include "opdev/shape_utils.h"



using namespace op;



namespace l0op {



OP_TYPE_REGISTER(StatelessNormal);



static inline const aclTensor* StatelessNormalAiCore(

    const aclTensor* result, const aclTensor* shapeTensor, const aclTensor* seedTensor,

    const aclTensor* offsetTensor, const aclTensor* meanTensor, const aclTensor* stdevTensor,

    aclTensor* outTensor, aclOpExecutor* executor)

{

    L0_DFX(StatelessNormalAiCore, shapeTensor, seedTensor, offsetTensor, meanTensor, stdevTensor, outTensor);

    auto dtype = result->GetDataType();

    auto ret = ADD_TO_LAUNCHER_LIST_AICORE(

        StatelessNormal, OP_INPUT(shapeTensor, seedTensor, offsetTensor, meanTensor, stdevTensor),

        OP_OUTPUT(outTensor), OP_ATTR(dtype));

    CHECK_RET(ret == ACLNN_SUCCESS, nullptr);

    return outTensor;

}



const aclTensor* StatelessNormal(

    const aclTensor* result, const aclTensor* seed, const aclTensor* offset,

    const aclTensor* mean, const aclTensor* stdev, aclOpExecutor* executor)

{

    auto outTensor = executor->AllocTensor(result->GetViewShape(), result->GetDataType(), result->GetViewFormat());



    auto sizeV = op::ToShapeVector(result->GetViewShape());

    auto shapeTensor = executor->ConvertToTensor(sizeV.data(), sizeV.size(), op::ToOpDataType(ACL_INT64));



    return StatelessNormalAiCore(result, shapeTensor, seed, offset, mean, stdev, outTensor, executor);

}



const aclTensor* StatelessNormal(

    const aclTensor* result, const int64_t seed, const int64_t offset,

    const aclTensor* mean, const aclTensor* stdev, aclOpExecutor* executor)

{

    auto outTensor = executor->AllocTensor(result->GetViewShape(), result->GetDataType(), result->GetViewFormat());



    auto sizeV = op::ToShapeVector(result->GetViewShape());

    auto shapeTensor = executor->ConvertToTensor(sizeV.data(), sizeV.size(), op::ToOpDataType(ACL_INT64));



    auto seedTensor = executor->ConvertToTensor(&seed, 1, op::ToOpDataType(ACL_INT64));

    auto offsetTensor = executor->ConvertToTensor(&offset, 1, op::ToOpDataType(ACL_INT64));



    return StatelessNormalAiCore(result, shapeTensor, seedTensor, offsetTensor, mean, stdev, outTensor, executor);

}



} // namespace l0op