opt-step-adamw.cu 4.5 KB

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