whispercpp 1.2.0.2 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/LICENSE +1 -1
  4. data/README.md +165 -434
  5. data/Rakefile +46 -86
  6. data/ext/.gitignore +13 -0
  7. data/ext/cpu.mk +9 -0
  8. data/ext/{dr_wav.h → examples/dr_wav.h} +3560 -1179
  9. data/ext/extconf.rb +185 -7
  10. data/ext/ggml/include/ggml-alloc.h +76 -0
  11. data/ext/ggml/include/ggml-backend.h +352 -0
  12. data/ext/ggml/include/ggml-blas.h +25 -0
  13. data/ext/ggml/include/ggml-cann.h +123 -0
  14. data/ext/ggml/include/ggml-cpp.h +38 -0
  15. data/ext/ggml/include/ggml-cpu.h +135 -0
  16. data/ext/ggml/include/ggml-cuda.h +47 -0
  17. data/ext/ggml/include/ggml-kompute.h +50 -0
  18. data/ext/ggml/include/ggml-metal.h +66 -0
  19. data/ext/ggml/include/ggml-opencl.h +26 -0
  20. data/ext/ggml/include/ggml-opt.h +216 -0
  21. data/ext/ggml/include/ggml-rpc.h +28 -0
  22. data/ext/ggml/include/ggml-sycl.h +49 -0
  23. data/ext/ggml/include/ggml-vulkan.h +31 -0
  24. data/ext/ggml/include/ggml.h +2285 -0
  25. data/ext/ggml/src/ggml-alloc.c +1037 -0
  26. data/ext/ggml/src/ggml-amx/common.h +94 -0
  27. data/ext/ggml/src/ggml-amx/ggml-amx.cpp +446 -0
  28. data/ext/ggml/src/ggml-amx/mmq.cpp +2510 -0
  29. data/ext/ggml/src/ggml-amx/mmq.h +17 -0
  30. data/ext/ggml/src/ggml-backend-impl.h +256 -0
  31. data/ext/ggml/src/ggml-backend-reg.cpp +552 -0
  32. data/ext/ggml/src/ggml-backend.cpp +1999 -0
  33. data/ext/ggml/src/ggml-blas/ggml-blas.cpp +517 -0
  34. data/ext/ggml/src/ggml-cann/acl_tensor.cpp +175 -0
  35. data/ext/ggml/src/ggml-cann/acl_tensor.h +258 -0
  36. data/ext/ggml/src/ggml-cann/aclnn_ops.cpp +3427 -0
  37. data/ext/ggml/src/ggml-cann/aclnn_ops.h +592 -0
  38. data/ext/ggml/src/ggml-cann/common.h +286 -0
  39. data/ext/ggml/src/ggml-cann/ggml-cann.cpp +2188 -0
  40. data/ext/ggml/src/ggml-cann/kernels/ascendc_kernels.h +19 -0
  41. data/ext/ggml/src/ggml-cann/kernels/dup.cpp +236 -0
  42. data/ext/ggml/src/ggml-cann/kernels/get_row_f16.cpp +197 -0
  43. data/ext/ggml/src/ggml-cann/kernels/get_row_f32.cpp +190 -0
  44. data/ext/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +204 -0
  45. data/ext/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +191 -0
  46. data/ext/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +218 -0
  47. data/ext/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +216 -0
  48. data/ext/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +295 -0
  49. data/ext/ggml/src/ggml-common.h +1853 -0
  50. data/ext/ggml/src/ggml-cpu/amx/amx.cpp +220 -0
  51. data/ext/ggml/src/ggml-cpu/amx/amx.h +8 -0
  52. data/ext/ggml/src/ggml-cpu/amx/common.h +91 -0
  53. data/ext/ggml/src/ggml-cpu/amx/mmq.cpp +2511 -0
  54. data/ext/ggml/src/ggml-cpu/amx/mmq.h +10 -0
  55. data/ext/ggml/src/ggml-cpu/cpu-feats-x86.cpp +323 -0
  56. data/ext/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +4262 -0
  57. data/ext/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +8 -0
  58. data/ext/ggml/src/ggml-cpu/ggml-cpu-hbm.cpp +55 -0
  59. data/ext/ggml/src/ggml-cpu/ggml-cpu-hbm.h +8 -0
  60. data/ext/ggml/src/ggml-cpu/ggml-cpu-impl.h +386 -0
  61. data/ext/ggml/src/ggml-cpu/ggml-cpu-quants.c +10835 -0
  62. data/ext/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
  63. data/ext/ggml/src/ggml-cpu/ggml-cpu-traits.cpp +36 -0
  64. data/ext/ggml/src/ggml-cpu/ggml-cpu-traits.h +38 -0
  65. data/ext/ggml/src/ggml-cpu/ggml-cpu.c +14123 -0
  66. data/ext/ggml/src/ggml-cpu/ggml-cpu.cpp +622 -0
  67. data/ext/ggml/src/ggml-cpu/llamafile/sgemm.cpp +1884 -0
  68. data/ext/ggml/src/ggml-cpu/llamafile/sgemm.h +14 -0
  69. data/ext/ggml/src/ggml-cuda/vendors/cuda.h +14 -0
  70. data/ext/ggml/src/ggml-cuda/vendors/hip.h +186 -0
  71. data/ext/ggml/src/ggml-cuda/vendors/musa.h +134 -0
  72. data/ext/ggml/src/ggml-impl.h +556 -0
  73. data/ext/ggml/src/ggml-kompute/ggml-kompute.cpp +2251 -0
  74. data/ext/ggml/src/ggml-metal/ggml-metal-impl.h +288 -0
  75. data/ext/ggml/src/ggml-metal/ggml-metal.m +4884 -0
  76. data/ext/ggml/src/ggml-metal/ggml-metal.metal +6732 -0
  77. data/ext/ggml/src/ggml-opt.cpp +854 -0
  78. data/ext/ggml/src/ggml-quants.c +5238 -0
  79. data/ext/ggml/src/ggml-quants.h +100 -0
  80. data/ext/ggml/src/ggml-rpc/ggml-rpc.cpp +1406 -0
  81. data/ext/ggml/src/ggml-sycl/common.cpp +95 -0
  82. data/ext/ggml/src/ggml-sycl/concat.cpp +196 -0
  83. data/ext/ggml/src/ggml-sycl/conv.cpp +99 -0
  84. data/ext/ggml/src/ggml-sycl/convert.cpp +547 -0
  85. data/ext/ggml/src/ggml-sycl/dmmv.cpp +1023 -0
  86. data/ext/ggml/src/ggml-sycl/element_wise.cpp +1030 -0
  87. data/ext/ggml/src/ggml-sycl/ggml-sycl.cpp +4729 -0
  88. data/ext/ggml/src/ggml-sycl/im2col.cpp +126 -0
  89. data/ext/ggml/src/ggml-sycl/mmq.cpp +3031 -0
  90. data/ext/ggml/src/ggml-sycl/mmvq.cpp +1015 -0
  91. data/ext/ggml/src/ggml-sycl/norm.cpp +378 -0
  92. data/ext/ggml/src/ggml-sycl/outprod.cpp +56 -0
  93. data/ext/ggml/src/ggml-sycl/rope.cpp +276 -0
  94. data/ext/ggml/src/ggml-sycl/softmax.cpp +251 -0
  95. data/ext/ggml/src/ggml-sycl/tsembd.cpp +72 -0
  96. data/ext/ggml/src/ggml-sycl/wkv6.cpp +141 -0
  97. data/ext/ggml/src/ggml-threading.cpp +12 -0
  98. data/ext/ggml/src/ggml-threading.h +14 -0
  99. data/ext/ggml/src/ggml-vulkan/ggml-vulkan.cpp +8657 -0
  100. data/ext/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +593 -0
  101. data/ext/ggml/src/ggml.c +7694 -0
  102. data/ext/include/whisper.h +672 -0
  103. data/ext/metal-embed.mk +17 -0
  104. data/ext/metal.mk +6 -0
  105. data/ext/ruby_whisper.cpp +1608 -159
  106. data/ext/ruby_whisper.h +10 -0
  107. data/ext/scripts/get-flags.mk +38 -0
  108. data/ext/src/coreml/whisper-decoder-impl.h +146 -0
  109. data/ext/src/coreml/whisper-decoder-impl.m +201 -0
  110. data/ext/src/coreml/whisper-encoder-impl.h +142 -0
  111. data/ext/src/coreml/whisper-encoder-impl.m +197 -0
  112. data/ext/src/coreml/whisper-encoder.h +26 -0
  113. data/ext/src/openvino/whisper-openvino-encoder.cpp +108 -0
  114. data/ext/src/openvino/whisper-openvino-encoder.h +31 -0
  115. data/ext/src/whisper.cpp +7393 -0
  116. data/extsources.rb +6 -0
  117. data/lib/whisper/model/uri.rb +157 -0
  118. data/lib/whisper.rb +2 -0
  119. data/tests/helper.rb +7 -0
  120. data/tests/jfk_reader/.gitignore +5 -0
  121. data/tests/jfk_reader/extconf.rb +3 -0
  122. data/tests/jfk_reader/jfk_reader.c +68 -0
  123. data/tests/test_callback.rb +160 -0
  124. data/tests/test_error.rb +20 -0
  125. data/tests/test_model.rb +71 -0
  126. data/tests/test_package.rb +31 -0
  127. data/tests/test_params.rb +160 -0
  128. data/tests/test_segment.rb +83 -0
  129. data/tests/test_whisper.rb +211 -123
  130. data/whispercpp.gemspec +36 -0
  131. metadata +137 -11
  132. data/ext/ggml.c +0 -8616
  133. data/ext/ggml.h +0 -748
  134. data/ext/whisper.cpp +0 -4829
  135. data/ext/whisper.h +0 -402
@@ -0,0 +1,286 @@
1
+ /*
2
+ * Copyright (c) 2023-2024 The ggml authors
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to
6
+ * deal in the Software without restriction, including without limitation the
7
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
+ * sell copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
+ * IN THE SOFTWARE.
21
+ */
22
+
23
+ #ifndef CANN_COMMON_H
24
+ #define CANN_COMMON_H
25
+
26
+ #include <acl/acl.h>
27
+
28
+ #include <cstdio>
29
+ #include <iostream>
30
+ #include <map>
31
+ #include <memory>
32
+ #include <string>
33
+ #include <vector>
34
+
35
+ #include "../include/ggml-cann.h"
36
+ #include "../include/ggml.h"
37
+
38
+ #define MATRIX_ROW_PADDING 512
39
+ #define GGML_CANN_MAX_STREAMS 8
40
+
41
+ /**
42
+ * @brief Handles CANN-related errors by printing an error message and
43
+ * terminating the program.
44
+ * @param stmt The statement that caused the error.
45
+ * @param func The function in which the error occurred.
46
+ * @param file The file in which the error occurred.
47
+ * @param line The line number at which the error occurred.
48
+ * @param msg The error message.
49
+ */
50
+ [[noreturn]] void ggml_cann_error(const char* stmt, const char* func,
51
+ const char* file, int line, const char* msg);
52
+
53
+ /**
54
+ * @brief Checks the result of a CANN function call and invokes the error
55
+ * handler if the call fails.
56
+ * @param stmt The CANN function call to check.
57
+ * @param success The success code that indicates the call was successful.
58
+ * @param error_fn The function to call to retrieve the error message.
59
+ */
60
+ #define ACL_CHECK_GEN(stmt, success, error_fn) \
61
+ do { \
62
+ int err_code = (stmt); \
63
+ if (err_code != (success)) { \
64
+ ggml_cann_error(#stmt, __func__, __FILE__, __LINE__, error_fn()); \
65
+ } \
66
+ } while (0);
67
+
68
+ #define ACL_CHECK(stmt) ACL_CHECK_GEN(stmt, 0, aclGetRecentErrMsg)
69
+
70
+ /**
71
+ * @brief Contains information about CANN devices.
72
+ */
73
+ struct ggml_cann_device_info {
74
+ /**
75
+ * @brief Number of CANN devices available.
76
+ */
77
+ int32_t device_count;
78
+
79
+ /**
80
+ * @brief Information about a single CANN device.
81
+ */
82
+ struct cann_device_info {
83
+ int cc; /**< Compute capability. */
84
+ size_t smpb; /**< Maximum shared memory per block. */
85
+ bool vmm; /**< Virtual memory support. */
86
+ size_t vmm_granularity; /**< Granularity of virtual memory. */
87
+ size_t total_vram; /**< Total video RAM available on the device. */
88
+ };
89
+
90
+ cann_device_info devices[GGML_CANN_MAX_DEVICES] =
91
+ {}; /**< Array of CANN device information. */
92
+ };
93
+
94
+ const ggml_cann_device_info& ggml_cann_info();
95
+
96
+ void ggml_cann_set_device(int32_t device);
97
+ int32_t ggml_cann_get_device();
98
+
99
+ /**
100
+ * @brief Abstract base class for memory pools used by CANN.
101
+ */
102
+ struct ggml_cann_pool {
103
+ /**
104
+ * @brief Virtual destructor for the memory pool.
105
+ */
106
+ virtual ~ggml_cann_pool() = default;
107
+
108
+ /**
109
+ * @brief Allocates memory from the pool.
110
+ *
111
+ * @param size The size of the memory block to allocate.
112
+ * @param actual_size Pointer to a variable where the actual allocated size
113
+ * will be stored.
114
+ * @return Pointer to the allocated memory block.
115
+ */
116
+ virtual void* alloc(size_t size, size_t* actual_size) = 0;
117
+
118
+ /**
119
+ * @brief Frees a previously allocated memory block.
120
+ *
121
+ * @param ptr Pointer to the memory block to free.
122
+ * @param size Size of the memory block to free.
123
+ * @note Note that all CANN opertors are running async. Make sure memory is
124
+ * still avaiable before this operator finished.
125
+ */
126
+ virtual void free(void* ptr, size_t size) = 0;
127
+ };
128
+
129
+ /**
130
+ * @brief RAII wrapper for managing memory allocations from a CANN memory pool.
131
+ */
132
+ struct ggml_cann_pool_alloc {
133
+ ggml_cann_pool* pool = nullptr; /**< Pointer to the memory pool. */
134
+ void* ptr = nullptr; /**< Pointer to the allocated memory block. */
135
+ size_t actual_size = 0; /**< Actual size of the allocated memory block. */
136
+
137
+ /**
138
+ * @brief Default constructor.
139
+ */
140
+ ggml_cann_pool_alloc() = default;
141
+
142
+ /**
143
+ * @brief Constructor that initializes the memory pool.
144
+ * @param pool Reference to the memory pool.
145
+ */
146
+ explicit ggml_cann_pool_alloc(ggml_cann_pool& pool) : pool(&pool) {}
147
+
148
+ /**
149
+ * @brief Constructor that initializes the memory pool and allocates memory.
150
+ * @param pool Reference to the memory pool.
151
+ * @param size Size of the memory block to allocate.
152
+ */
153
+ ggml_cann_pool_alloc(ggml_cann_pool& pool, size_t size) : pool(&pool) {
154
+ alloc(size);
155
+ }
156
+
157
+ /**
158
+ * @brief Destructor that frees the allocated memory block.
159
+ */
160
+ ~ggml_cann_pool_alloc() {
161
+ if (ptr != nullptr) {
162
+ pool->free(ptr, actual_size);
163
+ }
164
+ }
165
+
166
+ /**
167
+ * @brief Allocates memory from the pool.
168
+ * @param size Size of the memory block to allocate.
169
+ * @return Pointer to the allocated memory block.
170
+ */
171
+ void* alloc(size_t size) {
172
+ GGML_ASSERT(pool != nullptr);
173
+ GGML_ASSERT(ptr == nullptr);
174
+ ptr = pool->alloc(size, &this->actual_size);
175
+ return ptr;
176
+ }
177
+
178
+ /**
179
+ * @brief Allocates memory from a specific memory pool.
180
+ * @param pool Reference to the memory pool.
181
+ * @param size Size of the memory block to allocate.
182
+ * @return Pointer to the allocated memory block.
183
+ */
184
+ void* alloc(ggml_cann_pool& pool, size_t size) {
185
+ this->pool = &pool;
186
+ return alloc(size);
187
+ }
188
+
189
+ /**
190
+ * @brief Gets the pointer to the allocated memory block.
191
+ * @return Pointer to the allocated memory block.
192
+ */
193
+ void* get() { return ptr; }
194
+
195
+ // Deleted copy constructor
196
+ ggml_cann_pool_alloc(const ggml_cann_pool_alloc&) = delete;
197
+
198
+ // Deleted move constructor
199
+ ggml_cann_pool_alloc(ggml_cann_pool_alloc&&) = delete;
200
+
201
+ // Deleted copy assignment operator
202
+ ggml_cann_pool_alloc& operator=(const ggml_cann_pool_alloc&) = delete;
203
+
204
+ // Deleted move assignment operator
205
+ ggml_cann_pool_alloc& operator=(ggml_cann_pool_alloc&&) = delete;
206
+ };
207
+
208
+ /**
209
+ * @brief Context for managing CANN backend operations.
210
+ */
211
+ struct ggml_backend_cann_context {
212
+ int32_t device; /**< Device ID. */
213
+ std::string name; /**< Name of the device. */
214
+ std::string description; /**< Description of the device. */
215
+ aclrtEvent copy_event = nullptr; /**< Event for managing copy operations. */
216
+
217
+ aclrtStream streams[GGML_CANN_MAX_STREAMS] = {nullptr}; /**< Array of streams for the device. */
218
+
219
+ /**
220
+ * @brief Constructor for initializing the context with a given device.
221
+ * @param device Device ID.
222
+ */
223
+ explicit ggml_backend_cann_context(int device)
224
+ : device(device), name("CANN" + std::to_string(device)) {
225
+ ggml_cann_set_device(device);
226
+ description = aclrtGetSocName();
227
+ }
228
+
229
+ /**
230
+ * @brief Destructor for cleaning up resources.
231
+ */
232
+ ~ggml_backend_cann_context() {
233
+ ggml_cann_set_device(device);
234
+ if (copy_event != nullptr) {
235
+ ACL_CHECK(aclrtDestroyEvent(copy_event));
236
+ }
237
+ for (int i = 0; i < GGML_CANN_MAX_STREAMS; ++i) {
238
+ if (streams[i] != nullptr) {
239
+ ACL_CHECK(aclrtDestroyStream(streams[i]));
240
+ }
241
+ }
242
+ }
243
+
244
+ /**
245
+ * @brief Get or create a stream for a given index.
246
+ * @param stream Index of the stream.
247
+ * @return The stream corresponding to the given index.
248
+ */
249
+ aclrtStream stream(int stream) {
250
+ if (streams[stream] == nullptr) {
251
+ ggml_cann_set_device(device);
252
+ ACL_CHECK(aclrtCreateStream(&streams[stream]));
253
+ }
254
+ return streams[stream];
255
+ }
256
+
257
+ /**
258
+ * @brief Get or create the default stream (index 0).
259
+ * @return The default stream.
260
+ */
261
+ aclrtStream stream() { return stream(0); }
262
+
263
+ // TODO: each stream should have a memory pool.
264
+ std::unique_ptr<ggml_cann_pool>
265
+ mem_pool; /**< Memory pool for the device. */
266
+
267
+ /**
268
+ * @brief Create a new memory pool for a given device.
269
+ * @param device Device ID.
270
+ * @return A unique pointer to the new memory pool.
271
+ */
272
+ static std::unique_ptr<ggml_cann_pool> new_pool_for_device(int device);
273
+
274
+ /**
275
+ * @brief Get or create the memory pool for the context.
276
+ * @return Reference to the memory pool.
277
+ */
278
+ ggml_cann_pool& pool() {
279
+ if (mem_pool == nullptr) {
280
+ mem_pool = new_pool_for_device(device);
281
+ }
282
+ return *mem_pool;
283
+ }
284
+ };
285
+
286
+ #endif // CANN_COMMON_H