argmax.cu 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include <algorithm>
  27. #include <cstdint>
  28. #include "argmax.cuh"
  29. #include "common.cuh"
  30. #include "sum.cuh"
  31. static __global__ void argmax_f32(const float * __restrict__ x, int32_t * __restrict__ dst, const int64_t ncols) {
  32. const int64_t row = blockIdx.x;
  33. float maxval = -FLT_MAX;
  34. int argmax = -1;
  35. const float * rowx = x + row * ncols;
  36. for (int32_t col = threadIdx.x; col < ncols; col += blockDim.x) {
  37. const float val = rowx[col];
  38. if (val > maxval) {
  39. maxval = val;
  40. argmax = col;
  41. }
  42. }
  43. #pragma unroll
  44. for (int offset = 16; offset > 0; offset >>= 1) {
  45. const float val = __shfl_xor_sync(0xFFFFFFFF, maxval, offset, WARP_SIZE);
  46. const int col = __shfl_xor_sync(0xFFFFFFFF, argmax, offset, WARP_SIZE);
  47. if (val > maxval) {
  48. maxval = val;
  49. argmax = col;
  50. }
  51. }
  52. const int n_warps = blockDim.x / WARP_SIZE;
  53. const int lane_id = threadIdx.x % WARP_SIZE;
  54. const int warp_id = threadIdx.x / WARP_SIZE;
  55. if (n_warps > 1) {
  56. constexpr int max_warps = 1024 / WARP_SIZE;
  57. __shared__ float shared_maxval[max_warps];
  58. __shared__ int shared_argmax[max_warps];
  59. if (lane_id == 0) {
  60. shared_maxval[warp_id] = maxval;
  61. shared_argmax[warp_id] = argmax;
  62. }
  63. __syncthreads();
  64. if (warp_id == 0) {
  65. if (lane_id < n_warps) {
  66. maxval = shared_maxval[lane_id];
  67. argmax = shared_argmax[lane_id];
  68. }
  69. #pragma unroll
  70. for (int offset = 16; offset > 0; offset >>= 1) {
  71. const float val = __shfl_xor_sync(0xFFFFFFFF, maxval, offset, WARP_SIZE);
  72. const int col = __shfl_xor_sync(0xFFFFFFFF, argmax, offset, WARP_SIZE);
  73. if (val > maxval) {
  74. maxval = val;
  75. argmax = col;
  76. }
  77. }
  78. }
  79. }
  80. if (warp_id == 0 && lane_id == 0) {
  81. dst[row] = argmax;
  82. }
  83. }
  84. void ggml_cuda_argmax(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  85. const ggml_tensor * src0 = dst->src[0];
  86. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  87. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  88. GGML_ASSERT(ggml_is_contiguous(src0));
  89. const int64_t ne00 = src0->ne[0];
  90. const int64_t nrows = ggml_nrows(src0);
  91. const float * src0_d = (const float *) src0->data;
  92. int32_t * dst_d = (int32_t *) dst->data;
  93. cudaStream_t stream = ctx.stream();
  94. const int64_t num_blocks = nrows;
  95. const int64_t num_threads = std::min<int64_t>(1024, (ne00 + WARP_SIZE - 1) / WARP_SIZE * WARP_SIZE);
  96. const dim3 blocks_dim(num_threads, 1, 1);
  97. const dim3 blocks_num(num_blocks, 1, 1);
  98. argmax_f32<<<blocks_num, blocks_dim, 0, stream>>>(src0_d, dst_d, ne00);
  99. }