|
| 1 | +#include "ctranslate2/ops/median_filter.h" |
| 2 | + |
| 3 | +#include <cuda_fp16.h> |
| 4 | +#ifdef CUDA_BF16_AVAILABLE |
| 5 | +#include <cuda_bf16.h> |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "type_dispatch.h" |
| 9 | +#include "cuda/helpers.h" |
| 10 | +#include <type_traits> |
| 11 | + |
| 12 | +namespace ctranslate2 { |
| 13 | + namespace ops { |
| 14 | + |
| 15 | + constexpr dim_t num_threads = 256; |
| 16 | + |
| 17 | + // Conversion helpers |
| 18 | + __device__ __forceinline__ float to_float(float v) { return v; } |
| 19 | + __device__ __forceinline__ float to_float(const half v) { return __half2float(v); } |
| 20 | +#ifdef CUDA_BF16_AVAILABLE |
| 21 | + __device__ __forceinline__ float to_float(const __nv_bfloat16 v) { return __bfloat162float(v); } |
| 22 | +#endif |
| 23 | + |
| 24 | + __device__ __forceinline__ float from_float(float v) { return v; } |
| 25 | + __device__ __forceinline__ half from_float_half(float v) { return __float2half(v); } |
| 26 | +#ifdef CUDA_BF16_AVAILABLE |
| 27 | + __device__ __forceinline__ __nv_bfloat16 from_float_bf16(float v) { return __float2bfloat16(v); } |
| 28 | +#endif |
| 29 | + |
| 30 | + namespace { |
| 31 | + constexpr int kMaxWindow = 129; // supports window widths up to 129 (rank 64) |
| 32 | + } |
| 33 | + |
| 34 | + template <typename DeviceT, int kMax> |
| 35 | + __global__ void sliding_median_lastdim_kernel(const DeviceT* input, |
| 36 | + DeviceT* output, |
| 37 | + int rows, |
| 38 | + int depth, |
| 39 | + int width) { |
| 40 | + const int tid = blockIdx.x * blockDim.x + threadIdx.x; |
| 41 | + const int total = rows * depth; |
| 42 | + if (tid >= total) return; |
| 43 | + |
| 44 | + int row = tid / depth; |
| 45 | + int col = tid % depth; |
| 46 | + const int rank = width / 2; |
| 47 | + |
| 48 | + if (depth <= rank) { |
| 49 | + output[tid] = input[tid]; |
| 50 | + return; |
| 51 | + } |
| 52 | + if (width > kMax) { |
| 53 | + output[tid] = input[tid]; |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + float window[kMax]; |
| 58 | + |
| 59 | + const int row_offset = row * depth; |
| 60 | + // Reflection gather. |
| 61 | + for (int k = -rank; k <= rank; ++k) { |
| 62 | + int read = col + k; |
| 63 | + if (read < 0) read = -read; |
| 64 | + if (read >= depth) read = 2 * depth - read - 2; |
| 65 | + window[k + rank] = to_float(input[row_offset + read]); |
| 66 | + } |
| 67 | + |
| 68 | + // Insertion sort (width is small: <= kMax, typically < 129). |
| 69 | + for (int i = 1; i < width; ++i) { |
| 70 | + float key = window[i]; |
| 71 | + int j = i - 1; |
| 72 | + while (j >= 0 && window[j] > key) { |
| 73 | + window[j + 1] = window[j]; |
| 74 | + --j; |
| 75 | + } |
| 76 | + window[j + 1] = key; |
| 77 | + } |
| 78 | + float median = window[rank]; |
| 79 | + |
| 80 | + if constexpr (std::is_same<DeviceT, float>::value) { |
| 81 | + output[tid] = median; |
| 82 | + } else if constexpr (std::is_same<DeviceT, half>::value) { |
| 83 | + output[tid] = from_float_half(median); |
| 84 | +#ifdef CUDA_BF16_AVAILABLE |
| 85 | + } else if constexpr (std::is_same<DeviceT, __nv_bfloat16>::value) { |
| 86 | + output[tid] = from_float_bf16(median); |
| 87 | +#endif |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + template <Device D, typename T> |
| 92 | + void MedianFilter::compute(const StorageView& input, |
| 93 | + const dim_t axis_size, |
| 94 | + StorageView& output) const { |
| 95 | + output.resize_as(input); |
| 96 | + const int depth = static_cast<int>(axis_size); |
| 97 | + const int rows = static_cast<int>(input.size() / depth); |
| 98 | + const int width = static_cast<int>(_width); |
| 99 | + const int rank = width / 2; |
| 100 | + |
| 101 | + // Host-side guards and fallbacks. |
| 102 | + if (width <= 1) { |
| 103 | + if (&output != &input) |
| 104 | + output.copy_from(input); |
| 105 | + return; |
| 106 | + } |
| 107 | + if ((width & 1) == 0) |
| 108 | + throw std::invalid_argument("MedianFilter width must be odd"); |
| 109 | + if (width > kMaxWindow) |
| 110 | + throw std::invalid_argument("MedianFilter width exceeds supported GPU max (" + std::to_string(kMaxWindow) + ")"); |
| 111 | + if (depth <= rank) { |
| 112 | + if (&output != &input) |
| 113 | + output.copy_from(input); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // Grid configuration |
| 118 | + const int total = rows * depth; |
| 119 | + int blocks = (total + num_threads - 1) / num_threads; |
| 120 | + if (blocks > cuda::max_blocks) { |
| 121 | + blocks = cuda::max_blocks; |
| 122 | + } |
| 123 | + |
| 124 | + using device_t = cuda::device_type<T>; |
| 125 | + const device_t* in_ptr = cuda::device_cast(input.data<T>()); |
| 126 | + device_t* out_ptr = cuda::device_cast(output.data<T>()); |
| 127 | + sliding_median_lastdim_kernel<device_t, kMaxWindow><<<blocks, num_threads, 0, cuda::get_cuda_stream()>>>( |
| 128 | + in_ptr, |
| 129 | + out_ptr, |
| 130 | + rows, |
| 131 | + depth, |
| 132 | + width); |
| 133 | + CUDA_CHECK(cudaGetLastError()); |
| 134 | + CUDA_CHECK(cudaDeviceSynchronize()); |
| 135 | + } |
| 136 | + |
| 137 | +#define DECLARE_IMPL(T) \ |
| 138 | + template void \ |
| 139 | + MedianFilter::compute<Device::CUDA, T>(const StorageView& input, \ |
| 140 | + const dim_t axis_size, \ |
| 141 | + StorageView& output) const; |
| 142 | + |
| 143 | + DECLARE_IMPL(float) |
| 144 | + DECLARE_IMPL(float16_t) |
| 145 | + DECLARE_IMPL(bfloat16_t) |
| 146 | + |
| 147 | + } |
| 148 | +} |
0 commit comments