#include "var_cpu.h"
#include "../../../../utils.h"
#include "../../../devices/cpu/common_cpu.h"
namespace op::var::cpu {

Descriptor::~Descriptor() {}
infiniStatus_t Descriptor::create(
    infiniopHandle_t handle,
    Descriptor **desc_ptr,
    infiniopTensorDescriptor_t var_output_desc,
    infiniopTensorDescriptor_t input_desc,
    size_t *dim,
    size_t dim_size,
    bool unbiased,
    bool keepdim) {
    auto result = VarInfo::create(var_output_desc, input_desc, dim, dim_size, unbiased, keepdim);
    CHECK_RESULT(result);

    *desc_ptr = new Descriptor(nullptr, result.take(), 0, handle->device, handle->device_id);
    return INFINI_STATUS_SUCCESS;
}

// welford
namespace {
bool IsNanOut(const VarInfo &info) {
    return (info.reduce_num == 0) || (info.reduce_num == 1 && info.unbiased_var == true);
}
// 直接用float计算
template <typename Tdata>
void computeVarUsingWelfordCpu(const Tdata *input_ptr, float &var_output, size_t start, size_t end, const VarInfo &info) {
    if (start >= end) {
        return;
    }
    float old_mean = 0.0f; // previous mean
    float mean = 0.0f;     // new mean
    float M2 = 0.0f;       // variance sum
    size_t count = 0;      // element count of new sum
    for (size_t idx = start; idx < end; ++idx) {
        size_t input_offset = op::common_cpu::indexToOffset(idx, info.permuted_input_shape.size(), info.permuted_input_shape.data(), info.permuted_input_strides.data());
        ;
        float value = utils::cast<float>(input_ptr[input_offset]);
        count++;
        old_mean = mean;
        mean += (value - mean) / count;
        M2 += (value - old_mean) * (value - mean);
    }
    var_output = M2 / (info.unbiased_var ? (count - 1) : count);
}

template <typename Tdata>
infiniStatus_t calculateVar(
    const VarInfo &info,
    Tdata *var_output,
    const Tdata *input) {
    Tdata nan_value = utils::cast<Tdata>(NAN);
    bool is_scalar = (info.reduce_dim_size == info.permuted_input_shape.size());
    for (size_t i = 0; i < info.output_size; ++i) {
        size_t output_offset = op::common_cpu::indexToOffset(i, info.output_shape.size(), info.output_shape.data(), info.output_strides.data());
        if (IsNanOut(info)) {
            var_output[output_offset] = nan_value;
        } else {
            size_t start = is_scalar ? 0 : i * info.reduce_num;
            size_t end = is_scalar ? info.input_size : (i + 1) * info.reduce_num;
            float var = 0.0f;
            computeVarUsingWelfordCpu(input, var, start, end, info);
            var_output[output_offset] = utils::cast<Tdata>(var);
        }
    }
    return INFINI_STATUS_SUCCESS;
}
} // namespace

infiniStatus_t Descriptor::calculate(
    void *workspace,
    size_t workspace_size,
    void *var_output,
    const void *input,
    bool unbiased,
    bool keepdim,
    void *stream) const {
    switch (_info.dtype) {
    case INFINI_DTYPE_F16:
        return calculateVar<fp16_t>(_info, (fp16_t *)var_output, reinterpret_cast<const fp16_t *>(input));
    case INFINI_DTYPE_F32:
        return calculateVar<float>(_info, (float *)var_output, reinterpret_cast<const float *>(input));
    case INFINI_DTYPE_BF16:
        return calculateVar<bf16_t>(_info, (bf16_t *)var_output, reinterpret_cast<const bf16_t *>(input));
    default:
        return INFINI_STATUS_BAD_TENSOR_DTYPE;
    }

    return INFINI_STATUS_SUCCESS;
}
} // namespace op::var::cpu
