tsembd.cu 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - 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 "tsembd.cuh"
  27. static __global__ void timestep_embedding_f32(const float * timesteps, float * dst, const int nb1, const int dim, const int max_period) {
  28. // blockIDx.y: idx of timesteps->ne[0]
  29. // blockIDx.x: idx of ((dim + 1) / 2) / BLOCK_SIZE
  30. int i = blockIdx.y;
  31. int j = threadIdx.x + blockIdx.x * blockDim.x;
  32. float * embed_data = (float *)((char *)dst + i*nb1);
  33. if (dim % 2 != 0 && j == ((dim + 1) / 2)) {
  34. embed_data[dim] = 0.f;
  35. }
  36. int half = dim / 2;
  37. if (j >= half) {
  38. return;
  39. }
  40. float timestep = timesteps[i];
  41. float freq = (float)expf(-logf(max_period) * j / half);
  42. float arg = timestep * freq;
  43. embed_data[j] = cosf(arg);
  44. embed_data[j + half] = sinf(arg);
  45. }
  46. static void timestep_embedding_f32_cuda(const float * x, float * dst, const int ne00, const int nb1,
  47. const int dim, const int max_period, cudaStream_t stream) {
  48. int half_ceil = (dim + 1) / 2;
  49. int num_blocks = (half_ceil + CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE - 1) / CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE;
  50. dim3 gridDim(num_blocks, ne00, 1);
  51. timestep_embedding_f32<<<gridDim, CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE, 0, stream>>>(x, dst, nb1, dim, max_period);
  52. }
  53. void ggml_cuda_op_timestep_embedding(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  54. const ggml_tensor * src0 = dst->src[0];
  55. const float * src0_d = (const float *)src0->data;
  56. float * dst_d = (float *)dst->data;
  57. cudaStream_t stream = ctx.stream();
  58. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  59. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  60. const int dim = dst->op_params[0];
  61. const int max_period = dst->op_params[1];
  62. timestep_embedding_f32_cuda(src0_d, dst_d, src0->ne[0], dst->nb[1], dim, max_period, stream);
  63. }