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

#include <memory>
#include <vector>

#include "formats/formats.h"
#include "formats/utils/formats_trans_utils.h"
#include "framework/common/framework_types_internal.h"
#include "host_kernels/kernel_utils.h"
#include "graph/utils/type_utils.h"
#include "host_kernels/kernel_factory.h"

namespace ge {
namespace {
const size_t kTransdataInputSize = 1;
}

Status TransdataKernel::ValidateInput(const OpDescPtr &op_desc_ptr, const std::vector<ConstGeTensorPtr> &input) const {
  if (input.empty()) {
    GELOGE(PARAM_INVALID, "Input tensor vector is empty");
    return PARAM_INVALID;
  }
  ConstGeTensorPtr const_weight_ptr = input[0];
  if (const_weight_ptr == nullptr) {
    GELOGE(PARAM_INVALID, "Input const_weight_ptr is nullptr.");
    return PARAM_INVALID;
  }

  // src_data == nullptr is supported
  if (op_desc_ptr == nullptr) {
    GELOGE(PARAM_INVALID, "Input opDescPtr is nullptr.");
    return PARAM_INVALID;
  }
  if (op_desc_ptr->GetInputsSize() != kTransdataInputSize) {
    GELOGW("trans_op has more than 1 input_size.");
    return NOT_CHANGED;
  }
  return SUCCESS;
}

Status TransdataKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
                                std::vector<GeTensorPtr> &v_output) {
  GE_CHECK_NOTNULL(op_desc_ptr);
  GELOGD("TransdataKernel begin");
  Status status = ValidateInput(op_desc_ptr, input);
  if (status != SUCCESS) {
    return status;
  }

  ConstGeTensorPtr const_weight_ptr = input[0];
  const auto &op_desc = op_desc_ptr->MutableOutputDesc(0);
  const auto &op_desc_in = op_desc_ptr->MutableInputDesc(0);
  GE_CHECK_NOTNULL(op_desc);
  GE_CHECK_NOTNULL(op_desc_in);
  const auto &src_format = op_desc_in->GetFormat();
  const auto &src_shape = op_desc_in->GetShape().GetDims();
  const auto &src_data_type = op_desc_in->GetDataType();
  const auto &data_shape = op_desc->GetShape().GetDims();
  const auto &data_format = op_desc->GetFormat();
  const auto &data_type = op_desc->GetDataType();
  const Format src_primary_format = static_cast<Format>(GetPrimaryFormat(src_format));
  const Format dst_primary_format = static_cast<Format>(GetPrimaryFormat(data_format));
  const Format src_sub_format = static_cast<Format>(GetSubFormat(src_format));
  const Format dst_sub_format = static_cast<Format>(GetSubFormat(data_format));
  const int64_t src_c0_format = GetC0Value(static_cast<int32_t>(src_format));
  const int64_t dts_c0_format = GetC0Value(static_cast<int32_t>(data_format));

  GELOGD(
      "current node %s, format %s, primary format %s, input shape %s, data type %s, weight format %s, shape %s, "
      "data type %s. output format %s, output primary format %s, shape %s, data type %s.",
      op_desc_ptr->GetName().c_str(), TypeUtils::FormatToSerialString(src_format).c_str(),
      TypeUtils::FormatToSerialString(src_primary_format).c_str(),
      formats::ShapeToString(src_shape).c_str(), TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
      TypeUtils::FormatToSerialString(const_weight_ptr->GetTensorDesc().GetFormat()).c_str(),
      formats::ShapeToString(const_weight_ptr->GetTensorDesc().GetShape()).c_str(),
      TypeUtils::DataTypeToSerialString(const_weight_ptr->GetTensorDesc().GetDataType()).c_str(),
      TypeUtils::FormatToSerialString(data_format).c_str(),
      TypeUtils::FormatToSerialString(dst_primary_format).c_str(), formats::ShapeToString(data_shape).c_str(),
      TypeUtils::DataTypeToSerialString(data_type).c_str());

  const uint8_t *src_data = const_weight_ptr->GetData().data();
  const formats::TransArgs trans_args{src_data, src_format, data_format, src_primary_format, dst_primary_format,
                                      src_sub_format, dst_sub_format, src_c0_format, dts_c0_format, src_shape,
                                      data_shape, src_data_type};
  formats::TransResult trans_result;
  GELOGD("Trans formats from %s to %s, primary formats from %s to %s, shape %s to %s, data type %s.",
         TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
         TypeUtils::FormatToSerialString(src_primary_format).c_str(),
         TypeUtils::FormatToSerialString(dst_primary_format).c_str(),
         formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
         TypeUtils::DataTypeToSerialString(src_data_type).c_str());

  if (src_data_type != data_type || data_shape.empty() || !formats::IsTransFormatSupport(trans_args)) {
    GELOGW("Transfer from format %s to %s, primary formats from %s to %s, shape %s to %s, data type %s to %s "
           "is not supported", TypeUtils::FormatToSerialString(src_format).c_str(),
           TypeUtils::FormatToSerialString(data_format).c_str(),
           TypeUtils::FormatToSerialString(src_primary_format).c_str(),
           TypeUtils::FormatToSerialString(dst_primary_format).c_str(),
           formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
           TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
           TypeUtils::DataTypeToSerialString(data_type).c_str());
    return NOT_CHANGED;
  }
  if (!KernelUtils::CheckSizeForTransOp(const_weight_ptr, op_desc_ptr)) {
    GELOGI("CheckSize result is: input size is not equal to weight size");
    return NOT_CHANGED;
  }
  if (formats::TransDataFormat(trans_args, trans_result) != SUCCESS) {
    GELOGW("Failed to trans formats from %s to %s, primary formats from %s to %s, shape %s to %s, data type %s",
           TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
           TypeUtils::FormatToSerialString(src_primary_format).c_str(),
           TypeUtils::FormatToSerialString(dst_primary_format).c_str(),
           formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
           TypeUtils::DataTypeToSerialString(src_data_type).c_str());
    return NOT_CHANGED;
  }

  GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(0));
  if (output_ptr == nullptr) {
    GELOGE(ge::PARAM_INVALID, "Make shared failed");
    return ge::PARAM_INVALID;
  }
  if (output_ptr->SetData(trans_result.data.get(), trans_result.length) != GRAPH_SUCCESS) {
    GELOGW("Compute: SetData failed");
  }
  v_output.push_back(output_ptr);
  return SUCCESS;
}

REGISTER_COMPUTE_NODE_KERNEL(TRANSDATA, TransdataKernel);
}  // namespace ge