// baracuda-kernels Phase 4 reduction fanout: axis-max for FP types.
//
// `y = max(x, dim=k)` with keepdim=true. `init = -INFINITY`; op uses
// `fmax` / `fmaxf`; `finalize` is pass-through. For f16 / bf16 the
// compare detours through f32 (same pattern as Sum/Mean fanout).

#include "../include/baracuda_elementwise.cuh"

#include <cmath>

namespace baracuda { namespace elementwise {

template <typename T>
struct MaxReduce {
    static __device__ __forceinline__ T init();
    static __device__ __forceinline__ T finalize(T acc, int32_t /*extent*/) { return acc; }
    __device__ __forceinline__ T operator()(T acc, T x) const;
};

template <>
struct MaxReduce<float> {
    static __device__ __forceinline__ float init() { return -INFINITY; }
    static __device__ __forceinline__ float finalize(float acc, int32_t /*extent*/) {
        return acc;
    }
    __device__ __forceinline__ float operator()(float acc, float x) const {
        return fmaxf(acc, x);
    }
    static __device__ __forceinline__ float merge(float a, float b) {
        return fmaxf(a, b);
    }
};

template <>
struct MaxReduce<double> {
    static __device__ __forceinline__ double init() {
        return -static_cast<double>(INFINITY);
    }
    static __device__ __forceinline__ double finalize(double acc, int32_t /*extent*/) {
        return acc;
    }
    __device__ __forceinline__ double operator()(double acc, double x) const {
        return fmax(acc, x);
    }
    static __device__ __forceinline__ double merge(double a, double b) {
        return fmax(a, b);
    }
};

template <>
struct MaxReduce<__half> {
    static __device__ __forceinline__ __half init() { return __float2half(-INFINITY); }
    static __device__ __forceinline__ __half finalize(__half acc, int32_t /*extent*/) {
        return acc;
    }
    __device__ __forceinline__ __half operator()(__half acc, __half x) const {
        return __float2half(fmaxf(__half2float(acc), __half2float(x)));
    }
    static __device__ __forceinline__ __half merge(__half a, __half b) {
        return __float2half(fmaxf(__half2float(a), __half2float(b)));
    }
};

template <>
struct MaxReduce<__nv_bfloat16> {
    static __device__ __forceinline__ __nv_bfloat16 init() {
        return __float2bfloat16(-INFINITY);
    }
    static __device__ __forceinline__ __nv_bfloat16 finalize(
        __nv_bfloat16 acc, int32_t /*extent*/)
    {
        return acc;
    }
    __device__ __forceinline__ __nv_bfloat16 operator()(
        __nv_bfloat16 acc, __nv_bfloat16 x) const
    {
        return __float2bfloat16(fmaxf(__bfloat162float(acc), __bfloat162float(x)));
    }
    static __device__ __forceinline__ __nv_bfloat16 merge(
        __nv_bfloat16 a, __nv_bfloat16 b)
    {
        return __float2bfloat16(fmaxf(__bfloat162float(a), __bfloat162float(b)));
    }
};

} } // namespace baracuda::elementwise

// =============================================================================
// Instantiations
// =============================================================================

BARACUDA_KERNELS_REDUCE_AXIS_INSTANTIATE(
    reduce_max_f32, float, baracuda::elementwise::MaxReduce<float>)

BARACUDA_KERNELS_REDUCE_AXIS_INSTANTIATE(
    reduce_max_f16, __half, baracuda::elementwise::MaxReduce<__half>)

BARACUDA_KERNELS_REDUCE_AXIS_INSTANTIATE(
    reduce_max_bf16, __nv_bfloat16, baracuda::elementwise::MaxReduce<__nv_bfloat16>)

BARACUDA_KERNELS_REDUCE_AXIS_INSTANTIATE(
    reduce_max_f64, double, baracuda::elementwise::MaxReduce<double>)
