// Copyright (c) 2023 Huawei Technologies Co., Ltd
// All rights reserved.
//
// Licensed under the BSD 3-Clause License  (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "op_plugin/AclOpsInterface.h"
#include "op_plugin/utils/OpAdapter.h"
#include "op_plugin/utils/custom_functions/aclops/inner_compute.h"

namespace acl_op {
using npu_preparation = at_npu::native::OpPreparation;
using npu_utils = at_npu::native::NpuUtils;

at::Tensor _log_softmax(const at::Tensor& self, int64_t dim, bool half_to_float)
{
    at::Tensor result = npu_preparation::apply_tensor(self);
    c10::ScalarType result_type = half_to_float ? c10::ScalarType::Float : result.scalar_type();
    log_softmax_nocheck(result, self, dim, result_type);
    return result;
}

at::Tensor& _log_softmax_out(const at::Tensor& self, int64_t dim, bool half_to_float, at::Tensor& out)
{
    auto dst_type = half_to_float ? at::kFloat : self.scalar_type();
    npu_preparation::CheckOut(
        {self},
        out,
        npu_preparation::get_tensor_npu_format(out),
        dst_type,
        self.sizes());

    auto self_dtype = self.scalar_type();
    if (half_to_float) {
        TORCH_CHECK(self_dtype == at::kHalf, "conversion is supported for Half type only" + OPS_ERROR(ErrCode::TYPE));
    } else {
        TORCH_CHECK(at::isFloatingType(self_dtype), "_log_softmax_npu not implemented for '", toString(self_dtype),
            "'" + OPS_ERROR(ErrCode::NOT_SUPPORT));
    }

    at::Tensor self_cast = dst_type == self.scalar_type() ?
        self : at_npu::native::custom_ops::_npu_dtype_cast(self, dst_type);
    if (!npu_utils::check_match(&out)) {
        at::Tensor contiguous_result = npu_utils::format_contiguous(out);
        log_softmax_nocheck(contiguous_result, self_cast, dim, dst_type);
        npu_utils::format_fresh_view(out, contiguous_result);
    } else {
        log_softmax_nocheck(out, self_cast, dim, dst_type);
    }
    return out;
}
} // namespace acl_op