acc.cu 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "acc.cuh"
  2. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3. const int ne10, const int ne11, const int ne12,
  4. const int nb1, const int nb2, int offset) {
  5. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  6. if (i >= ne) {
  7. return;
  8. }
  9. int src1_idx = i - offset;
  10. int oz = src1_idx / nb2;
  11. int oy = (src1_idx - (oz * nb2)) / nb1;
  12. int ox = src1_idx % nb1;
  13. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  14. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  15. } else {
  16. dst[i] = x[i];
  17. }
  18. }
  19. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  20. const int ne10, const int ne11, const int ne12,
  21. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  22. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  23. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  24. }
  25. void ggml_cuda_op_acc(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  26. const ggml_tensor * src0 = dst->src[0];
  27. const ggml_tensor * src1 = dst->src[1];
  28. const float * src0_d = (const float *)src0->data;
  29. const float * src1_d = (const float *)src1->data;
  30. float * dst_d = (float *)dst->data;
  31. cudaStream_t stream = ctx.stream();
  32. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  33. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  34. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  35. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  36. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  37. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  38. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  39. int offset = dst->op_params[3] / 4; // offset in bytes
  40. 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);
  41. }