unary-ops.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "unary-ops.h"
  2. static inline float op_abs(float x) {
  3. return fabsf(x);
  4. }
  5. static inline float op_sgn(float x) {
  6. return (x > 0.f) ? 1.f : ((x < 0.f) ? -1.f : 0.f);
  7. }
  8. static inline float op_neg(float x) {
  9. return -x;
  10. }
  11. static inline float op_step(float x) {
  12. return (x > 0.f) ? 1.f : 0.f;
  13. }
  14. static inline float op_tanh(float x) {
  15. return tanhf(x);
  16. }
  17. static inline float op_elu(float x) {
  18. return (x > 0.f) ? x : expm1f(x);
  19. }
  20. static inline float op_relu(float x) {
  21. return (x > 0.f) ? x : 0.f;
  22. }
  23. static inline float op_sigmoid(float x) {
  24. return 1.f / (1.f + expf(-x));
  25. }
  26. static inline float op_hardsigmoid(float x) {
  27. return fminf(1.0f, fmaxf(0.0f, (x + 3.0f) / 6.0f));
  28. }
  29. static inline float op_exp(float x) {
  30. return expf(x);
  31. }
  32. static inline float op_hardswish(float x) {
  33. return x * fminf(1.0f, fmaxf(0.0f, (x + 3.0f) / 6.0f));
  34. }
  35. static inline float op_sqr(float x) {
  36. return x * x;
  37. }
  38. static inline float op_sqrt(float x) {
  39. return sqrtf(x);
  40. }
  41. static inline float op_sin(float x) {
  42. return sinf(x);
  43. }
  44. static inline float op_cos(float x) {
  45. return cosf(x);
  46. }
  47. static inline float op_log(float x) {
  48. return logf(x);
  49. }
  50. template <float (*op)(float), typename src0_t, typename dst_t>
  51. static inline void vec_unary_op(int64_t n, dst_t * y, const src0_t * x) {
  52. constexpr auto src0_to_f32 = type_conversion_table<src0_t>::to_f32;
  53. constexpr auto f32_to_dst = type_conversion_table<dst_t >::from_f32;
  54. for (int i = 0; i < n; i++) {
  55. y[i] = f32_to_dst(op(src0_to_f32(x[i])));
  56. }
  57. }
  58. template <float (*op)(float), typename src0_t, typename dst_t>
  59. static void apply_unary_op(const ggml_compute_params * params, ggml_tensor * dst) {
  60. const ggml_tensor * src0 = dst->src[0];
  61. GGML_ASSERT(ggml_is_contiguous_1(src0) && ggml_is_contiguous_1(dst) && ggml_are_same_shape(src0, dst));
  62. GGML_TENSOR_UNARY_OP_LOCALS
  63. GGML_ASSERT( nb0 == sizeof(dst_t));
  64. GGML_ASSERT(nb00 == sizeof(src0_t));
  65. const auto [ir0, ir1] = get_thread_range(params, src0);
  66. for (int64_t ir = ir0; ir < ir1; ++ir) {
  67. const int64_t i03 = ir/(ne02*ne01);
  68. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  69. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  70. dst_t * dst_ptr = (dst_t *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  71. const src0_t * src0_ptr = (const src0_t *) ((const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  72. vec_unary_op<op>(ne0, dst_ptr, src0_ptr);
  73. }
  74. }
  75. // TODO: Use the 'traits' lookup table (for type conversion fns), instead of a mass of 'if' conditions with long templates
  76. template <float (*op)(float)>
  77. static void unary_op(const ggml_compute_params * params, ggml_tensor * dst) {
  78. const ggml_tensor * src0 = dst->src[0];
  79. /* */ if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) { // all f32
  80. apply_unary_op<op, float, float>(params, dst);
  81. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) { // all f16
  82. apply_unary_op<op, ggml_fp16_t, ggml_fp16_t>(params, dst);
  83. } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_BF16) { // all bf16
  84. apply_unary_op<op, ggml_bf16_t, ggml_bf16_t>(params, dst);
  85. } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_F32) {
  86. apply_unary_op<op, ggml_bf16_t, float>(params, dst);
  87. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  88. apply_unary_op<op, ggml_fp16_t, float>(params, dst);
  89. } else {
  90. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s\n", __func__,
  91. ggml_type_name(dst->type), ggml_type_name(src0->type));
  92. GGML_ABORT("fatal error");
  93. }
  94. }
  95. void ggml_compute_forward_abs(const ggml_compute_params * params, ggml_tensor * dst) {
  96. unary_op<op_abs>(params, dst);
  97. }
  98. void ggml_compute_forward_sgn(const ggml_compute_params * params, ggml_tensor * dst) {
  99. unary_op<op_sgn>(params, dst);
  100. }
  101. void ggml_compute_forward_neg(const ggml_compute_params * params, ggml_tensor * dst) {
  102. unary_op<op_neg>(params, dst);
  103. }
  104. void ggml_compute_forward_step(const ggml_compute_params * params, ggml_tensor * dst) {
  105. unary_op<op_step>(params, dst);
  106. }
  107. void ggml_compute_forward_tanh(const ggml_compute_params * params, ggml_tensor * dst) {
  108. unary_op<op_tanh>(params, dst);
  109. }
  110. void ggml_compute_forward_elu(const ggml_compute_params * params, ggml_tensor * dst) {
  111. unary_op<op_elu>(params, dst);
  112. }
  113. void ggml_compute_forward_relu(const ggml_compute_params * params, ggml_tensor * dst) {
  114. unary_op<op_relu>(params, dst);
  115. }
  116. void ggml_compute_forward_sigmoid(const ggml_compute_params * params, ggml_tensor * dst) {
  117. unary_op<op_sigmoid>(params, dst);
  118. }
  119. void ggml_compute_forward_hardsigmoid(const ggml_compute_params * params, ggml_tensor * dst) {
  120. unary_op<op_hardsigmoid>(params, dst);
  121. }
  122. void ggml_compute_forward_exp(const ggml_compute_params * params, ggml_tensor * dst) {
  123. unary_op<op_exp>(params, dst);
  124. }
  125. void ggml_compute_forward_hardswish(const ggml_compute_params * params, ggml_tensor * dst) {
  126. unary_op<op_hardswish>(params, dst);
  127. }
  128. void ggml_compute_forward_sqr(const ggml_compute_params * params, ggml_tensor * dst) {
  129. unary_op<op_sqr>(params, dst);
  130. }
  131. void ggml_compute_forward_sqrt(const ggml_compute_params * params, ggml_tensor * dst) {
  132. unary_op<op_sqrt>(params, dst);
  133. }
  134. void ggml_compute_forward_sin(const ggml_compute_params * params, ggml_tensor * dst) {
  135. unary_op<op_sin>(params, dst);
  136. }
  137. void ggml_compute_forward_cos(const ggml_compute_params * params, ggml_tensor * dst) {
  138. unary_op<op_cos>(params, dst);
  139. }
  140. void ggml_compute_forward_log(const ggml_compute_params * params, ggml_tensor * dst) {
  141. unary_op<op_log>(params, dst);
  142. }