opt-step-adamw.cu 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "ggml-impl.h"
  27. #include "opt-step-adamw.cuh"
  28. #include <cstdint>
  29. static __global__ void opt_step_adamw_f32(
  30. float * __restrict__ x, const float * __restrict__ g, float * __restrict__ g_m, float * __restrict__ g_v,
  31. const float * __restrict__ pars, const int64_t k) {
  32. const int64_t i = (int64_t) blockIdx.x*blockDim.x + threadIdx.x;
  33. if (i >= k) {
  34. return;
  35. }
  36. const float alpha = pars[0];
  37. const float beta1 = pars[1];
  38. const float beta2 = pars[2];
  39. const float eps = pars[3];
  40. const float wd = pars[4];
  41. const float beta1h = pars[5];
  42. const float beta2h = pars[6];
  43. const float gi = g[i];
  44. const float gmi = g_m[i]*beta1 + gi*(1.0f - beta1);
  45. const float gvi = g_v[i]*beta2 + gi*gi*(1.0f - beta2);
  46. g_m[i] = gmi;
  47. g_v[i] = gvi;
  48. const float mh = gmi*beta1h;
  49. const float vh = sqrtf(gvi*beta2h) + eps;
  50. x[i] = x[i]*(1.0f - alpha*wd) - alpha*mh/vh;
  51. }
  52. static void opt_step_adamw_f32_cuda(
  53. float * x, const float * g, float * g_m, float * g_v, const float * pars, const int64_t k, cudaStream_t stream) {
  54. const dim3 block_dims(CUDA_OPT_STEP_ADAMW_BLOCK_SIZE, 1, 1);
  55. const dim3 block_nums((k + CUDA_OPT_STEP_ADAMW_BLOCK_SIZE - 1) / CUDA_OPT_STEP_ADAMW_BLOCK_SIZE, 1, 1);
  56. opt_step_adamw_f32<<<block_nums, block_dims, 0, stream>>>(x, g, g_m, g_v, pars, k);
  57. }
  58. void ggml_cuda_opt_step_adamw(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  59. const ggml_tensor * src0 = dst->src[0];
  60. const ggml_tensor * src0_grad = dst->src[1];
  61. const ggml_tensor * src0_grad_m = dst->src[2];
  62. const ggml_tensor * src0_grad_v = dst->src[3];
  63. const ggml_tensor * adamw_params = dst->src[4];
  64. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  65. GGML_ASSERT(src0_grad->type == GGML_TYPE_F32);
  66. GGML_ASSERT(src0_grad_m->type == GGML_TYPE_F32);
  67. GGML_ASSERT(src0_grad_v->type == GGML_TYPE_F32);
  68. GGML_ASSERT(adamw_params->type == GGML_TYPE_F32);
  69. GGML_ASSERT(ggml_is_contiguous(src0));
  70. GGML_ASSERT(ggml_is_contiguous(src0_grad));
  71. GGML_ASSERT(ggml_is_contiguous(src0_grad_m));
  72. GGML_ASSERT(ggml_is_contiguous(src0_grad_v));
  73. GGML_ASSERT(ggml_is_contiguous(adamw_params));
  74. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
  75. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m));
  76. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v));
  77. GGML_ASSERT(ggml_nelements(adamw_params) == 7);
  78. float * src0_d = (float *) src0->data;
  79. const float * src0_grad_d = (const float *) src0_grad->data;
  80. float * src0_grad_m_d = (float *) src0_grad_m->data;
  81. float * src0_grad_v_d = (float *) src0_grad_v->data;
  82. const float * adamw_params_d = (const float *) adamw_params->data;
  83. cudaStream_t stream = ctx.stream();
  84. const int64_t ne = ggml_nelements(src0);
  85. opt_step_adamw_f32_cuda(src0_d, src0_grad_d, src0_grad_m_d, src0_grad_v_d, adamw_params_d, ne, stream);
  86. }