count-equal.cu 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - 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 "common.cuh"
  27. #include "count-equal.cuh"
  28. #include <cstdint>
  29. template <typename T>
  30. static __global__ void count_equal(const T * __restrict__ x, const T * __restrict__ y, int64_t * __restrict__ dst, const int64_t dk, const int64_t k) {
  31. const int64_t i0 = (int64_t) blockIdx.x*dk;
  32. const int64_t i1 = min(i0 + dk, k);
  33. int nequal = 0;
  34. for (int64_t i = i0 + threadIdx.x; i < i1; i += WARP_SIZE) {
  35. const T xi = x[i];
  36. const T yi = y[i];
  37. nequal += xi == yi;
  38. }
  39. nequal = warp_reduce_sum(nequal);
  40. if (threadIdx.x != 0) {
  41. return;
  42. }
  43. atomicAdd((int *) dst, nequal);
  44. }
  45. void ggml_cuda_count_equal(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  46. const ggml_tensor * src0 = dst->src[0];
  47. const ggml_tensor * src1 = dst->src[1];
  48. GGML_ASSERT(src0->type == src1->type);
  49. GGML_ASSERT( dst->type == GGML_TYPE_I64);
  50. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  51. GGML_ASSERT(ggml_is_contiguous(src0));
  52. GGML_ASSERT(ggml_is_contiguous(src1));
  53. GGML_ASSERT(ggml_is_contiguous(dst));
  54. int64_t * dst_d = (int64_t *) dst->data;
  55. cudaStream_t stream = ctx.stream();
  56. const int nsm = ggml_cuda_info().devices[ggml_cuda_get_device()].nsm;
  57. const int64_t ne = ggml_nelements(src0);
  58. GGML_ASSERT(ne < (1 << 30) && "atomicAdd implementation only supports int");
  59. const int64_t dne = GGML_PAD((ne + 4*nsm - 1) / (4*nsm), CUDA_COUNT_EQUAL_CHUNK_SIZE);
  60. CUDA_CHECK(cudaMemsetAsync(dst_d, 0, ggml_nbytes(dst), stream));
  61. const dim3 blocks_dim(WARP_SIZE, 1, 1);
  62. const dim3 blocks_num(std::min((int64_t)4*nsm, (ne + CUDA_COUNT_EQUAL_CHUNK_SIZE - 1)/CUDA_COUNT_EQUAL_CHUNK_SIZE), 1, 1);
  63. switch (src0->type) {
  64. case GGML_TYPE_I32: {
  65. const int * src0_d = (const int *) src0->data;
  66. const int * src1_d = (const int *) src1->data;
  67. count_equal<<<blocks_num, blocks_dim, 0, stream>>>(src0_d, src1_d, dst_d, dne, ne);
  68. } break;
  69. default:
  70. GGML_ASSERT(false);
  71. break;
  72. }
  73. }