common.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include "ggml.h"
  3. #include "traits.h"
  4. #include "ggml-cpu-impl.h"
  5. #include "ggml-impl.h"
  6. #include "simd-mappings.h"
  7. #ifdef __cplusplus
  8. #include <utility>
  9. // convenience functions/macros for use in template calls
  10. // note: these won't be required after the 'traits' lookup table is used.
  11. static inline ggml_fp16_t f32_to_f16(float x) {
  12. return GGML_CPU_FP32_TO_FP16(x);
  13. }
  14. static inline float f16_to_f32(ggml_fp16_t x) {
  15. return GGML_CPU_FP16_TO_FP32(x);
  16. }
  17. static inline ggml_bf16_t f32_to_bf16(float x) {
  18. return GGML_FP32_TO_BF16(x);
  19. }
  20. static inline float bf16_to_f32(ggml_bf16_t x) {
  21. return GGML_BF16_TO_FP32(x);
  22. }
  23. static inline float f32_to_f32(float x) {
  24. return x;
  25. }
  26. // TODO - merge this into the traits table, after using row-based conversions
  27. template <class T>
  28. struct type_conversion_table;
  29. template <>
  30. struct type_conversion_table<ggml_fp16_t> {
  31. static constexpr float (*to_f32)(ggml_fp16_t) = f16_to_f32;
  32. static constexpr ggml_fp16_t (*from_f32)(float) = f32_to_f16;
  33. };
  34. template <>
  35. struct type_conversion_table<float> {
  36. static constexpr float (*to_f32)(float) = f32_to_f32;
  37. static constexpr float (*from_f32)(float) = f32_to_f32;
  38. };
  39. template <>
  40. struct type_conversion_table<ggml_bf16_t> {
  41. static constexpr float (*to_f32)(ggml_bf16_t) = bf16_to_f32;
  42. static constexpr ggml_bf16_t (*from_f32)(float) = f32_to_bf16;
  43. };
  44. static std::pair<int64_t, int64_t> get_thread_range(const struct ggml_compute_params * params, const struct ggml_tensor * src0) {
  45. const int64_t ith = params->ith;
  46. const int64_t nth = params->nth;
  47. const int64_t nr = ggml_nrows(src0);
  48. // rows per thread
  49. const int64_t dr = (nr + nth - 1)/nth;
  50. // row range for this thread
  51. const int64_t ir0 = dr*ith;
  52. const int64_t ir1 = MIN(ir0 + dr, nr);
  53. return {ir0, ir1};
  54. }
  55. #endif