/src/llama.cpp/ggml/src/ggml-cpu/common.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "ggml.h" |
4 | | #include "traits.h" |
5 | | #include "ggml-cpu-impl.h" |
6 | | #include "ggml-impl.h" |
7 | | #include "simd-mappings.h" |
8 | | |
9 | | #ifdef __cplusplus |
10 | | |
11 | | #include <utility> |
12 | | |
13 | | // convenience functions/macros for use in template calls |
14 | | // note: these won't be required after the 'traits' lookup table is used. |
15 | 0 | static inline ggml_fp16_t f32_to_f16(float x) { |
16 | 0 | return GGML_CPU_FP32_TO_FP16(x); |
17 | 0 | } Unexecuted instantiation: binary-ops.cpp:f32_to_f16(float) Unexecuted instantiation: unary-ops.cpp:f32_to_f16(float) Unexecuted instantiation: ops.cpp:f32_to_f16(float) |
18 | | |
19 | 0 | static inline float f16_to_f32(ggml_fp16_t x) { |
20 | 0 | return GGML_CPU_FP16_TO_FP32(x); |
21 | 0 | } Unexecuted instantiation: binary-ops.cpp:f16_to_f32(unsigned short) Unexecuted instantiation: unary-ops.cpp:f16_to_f32(unsigned short) Unexecuted instantiation: ops.cpp:f16_to_f32(unsigned short) |
22 | | |
23 | 0 | static inline ggml_bf16_t f32_to_bf16(float x) { |
24 | 0 | return GGML_FP32_TO_BF16(x); |
25 | 0 | } Unexecuted instantiation: binary-ops.cpp:f32_to_bf16(float) Unexecuted instantiation: unary-ops.cpp:f32_to_bf16(float) Unexecuted instantiation: ops.cpp:f32_to_bf16(float) |
26 | | |
27 | 0 | static inline float bf16_to_f32(ggml_bf16_t x) { |
28 | 0 | return GGML_BF16_TO_FP32(x); |
29 | 0 | } Unexecuted instantiation: binary-ops.cpp:bf16_to_f32(ggml_bf16_t) Unexecuted instantiation: unary-ops.cpp:bf16_to_f32(ggml_bf16_t) Unexecuted instantiation: ops.cpp:bf16_to_f32(ggml_bf16_t) |
30 | | |
31 | 0 | static inline float i32_to_f32(int32_t x) { |
32 | 0 | return x; |
33 | 0 | } Unexecuted instantiation: binary-ops.cpp:i32_to_f32(int) Unexecuted instantiation: unary-ops.cpp:i32_to_f32(int) Unexecuted instantiation: ops.cpp:i32_to_f32(int) |
34 | | |
35 | 0 | static inline int32_t f32_to_i32(float x) { |
36 | 0 | return x; |
37 | 0 | } Unexecuted instantiation: binary-ops.cpp:f32_to_i32(float) Unexecuted instantiation: unary-ops.cpp:f32_to_i32(float) Unexecuted instantiation: ops.cpp:f32_to_i32(float) |
38 | | |
39 | 0 | static inline float f32_to_f32(float x) { |
40 | 0 | return x; |
41 | 0 | } Unexecuted instantiation: binary-ops.cpp:f32_to_f32(float) Unexecuted instantiation: unary-ops.cpp:f32_to_f32(float) Unexecuted instantiation: ops.cpp:f32_to_f32(float) |
42 | | |
43 | | // TODO - merge this into the traits table, after using row-based conversions |
44 | | template <class T> |
45 | | struct type_conversion_table; |
46 | | |
47 | | template <> |
48 | | struct type_conversion_table<ggml_fp16_t> { |
49 | | static constexpr float (*to_f32)(ggml_fp16_t) = f16_to_f32; |
50 | | static constexpr ggml_fp16_t (*from_f32)(float) = f32_to_f16; |
51 | | }; |
52 | | |
53 | | template <> |
54 | | struct type_conversion_table<float> { |
55 | | static constexpr float (*to_f32)(float) = f32_to_f32; |
56 | | static constexpr float (*from_f32)(float) = f32_to_f32; |
57 | | }; |
58 | | |
59 | | template <> |
60 | | struct type_conversion_table<ggml_bf16_t> { |
61 | | static constexpr float (*to_f32)(ggml_bf16_t) = bf16_to_f32; |
62 | | static constexpr ggml_bf16_t (*from_f32)(float) = f32_to_bf16; |
63 | | }; |
64 | | |
65 | | template <> |
66 | | struct type_conversion_table<int32_t> { |
67 | | static constexpr float (*to_f32)(int32_t) = i32_to_f32; |
68 | | static constexpr int32_t (*from_f32)(float) = f32_to_i32; |
69 | | }; |
70 | | |
71 | 0 | static std::pair<int64_t, int64_t> get_thread_range(const struct ggml_compute_params * params, const struct ggml_tensor * src0) { |
72 | 0 | const int64_t ith = params->ith; |
73 | 0 | const int64_t nth = params->nth; |
74 | |
|
75 | 0 | const int64_t nr = ggml_nrows(src0); |
76 | | |
77 | | // rows per thread |
78 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
79 | | |
80 | | // row range for this thread |
81 | 0 | const int64_t ir0 = dr*ith; |
82 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
83 | |
|
84 | 0 | return {ir0, ir1}; |
85 | 0 | } Unexecuted instantiation: binary-ops.cpp:get_thread_range(ggml_compute_params const*, ggml_tensor const*) Unexecuted instantiation: unary-ops.cpp:get_thread_range(ggml_compute_params const*, ggml_tensor const*) Unexecuted instantiation: ops.cpp:get_thread_range(ggml_compute_params const*, ggml_tensor const*) |
86 | | |
87 | | #endif |