acc.cu 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * llama.cpp - commit 8962422b1c6f9b8b15f5aeaea42600bcc2d44177 - 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 "acc.cuh"
  27. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  28. const int ne10, const int ne11, const int ne12,
  29. const int nb1, const int nb2, int offset) {
  30. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  31. if (i >= ne) {
  32. return;
  33. }
  34. int src1_idx = i - offset;
  35. int oz = src1_idx / nb2;
  36. int oy = (src1_idx - (oz * nb2)) / nb1;
  37. int ox = src1_idx % nb1;
  38. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  39. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  40. } else {
  41. dst[i] = x[i];
  42. }
  43. }
  44. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  45. const int ne10, const int ne11, const int ne12,
  46. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  47. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  48. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  49. }
  50. void ggml_cuda_op_acc(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  51. const ggml_tensor * src0 = dst->src[0];
  52. const ggml_tensor * src1 = dst->src[1];
  53. const float * src0_d = (const float *)src0->data;
  54. const float * src1_d = (const float *)src1->data;
  55. float * dst_d = (float *)dst->data;
  56. cudaStream_t stream = ctx.stream();
  57. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  58. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  59. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  60. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  61. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  62. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  63. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  64. int offset = dst->op_params[3] / 4; // offset in bytes
  65. acc_f32_cuda(src0_d, src1_d, dst_d, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, stream);
  66. }