interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-et / ggml-et.cpp

1884 lines · 89131 bytes Raw Blame History
1#include "ggml-et.h"
2
3#include "ggml-backend-impl.h"
4#include "ggml-backend.h"
5#include "ggml-et-common.h"
6#include "ggml-et-kernels.h"
7#include "ggml-et-memops.h"
8#include "ggml-et-ops.h"
9#include "ggml-impl.h"
10#include "ggml.h"
11
12#include <stdarg.h>
13
14#include <cstdarg>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <string>
19#include <vector>
20
21#if __has_include(<filesystem>)
22# include <filesystem>
23namespace fs = std::filesystem;
24#elif __has_include(<experimental/filesystem>)
25# include <experimental/filesystem>
26namespace fs = std::experimental::filesystem;
27#else
28# error "cannot include the filesystem library"
29#endif
30
31/*
32 * ggml_et_dump_tensor_metadata
33 * @brief prints the metadata of a single tensorf
34 */
35static void ggml_et_dump_tensor_metadata(const ggml_tensor * ggtensor, size_t indent_level, const char * title) {
36 std::string spaces(indent_level, ' ');
37 fprintf(stderr,
38 "%s%s: %s\n"
39 "%s type: %s\n"
40 "%s ne: %lld %lld %lld %lld\n"
41 "%s nb: %zu %zu %zu %zu\n"
42 "%s op: %s\n"
43 "%s data: %p\n"
44 "%s src0: %p\n",
45 spaces.c_str(), title, ggtensor->name, spaces.c_str(), ggml_type_name(ggtensor->type), spaces.c_str(), (long long) ggtensor->ne[0],
46 (long long) ggtensor->ne[1], (long long) ggtensor->ne[2], (long long) ggtensor->ne[3], spaces.c_str(),
47 ggtensor->nb[0], ggtensor->nb[1], ggtensor->nb[2], ggtensor->nb[3], spaces.c_str(), ggml_op_name(ggtensor->op),
48 spaces.c_str(), ggtensor->data, spaces.c_str(), (void *) ggtensor->src[0]);
49}
50
51/*
52 * ggml_et_dump_operator_metadata
53 * @brief prints the metadata of a single tensor (or operator) including it's input and views
54 */
55static void ggml_et_dump_operator_metadata(const ggml_tensor * ggtensor) {
56 GGML_ASSERT(ggtensor != NULL);
57 ggml_et_dump_tensor_metadata(ggtensor, 0, "GGML tensor");
58 for (int i = 0; i < GGML_MAX_SRC && ggtensor->src[i]; i++) {
59 char arr[16];
60 int n = snprintf(arr, sizeof(arr), "src[%i]->name", i);
61 GGML_ASSERT((unsigned) n < sizeof(arr) && "printed too much data to stack buffer");
62 ggml_et_dump_tensor_metadata(ggtensor->src[i], 2, arr);
63 }
64 if (ggtensor->view_src) {
65 ggml_et_dump_tensor_metadata(ggtensor, 2, "view_src");
66 }
67}
68
69static struct ggml_et_driver {
70 std::shared_ptr<dev::IDeviceLayer> device_layer;
71 std::shared_ptr<rt::IRuntime> runtime;
72 std::unique_ptr<std::ofstream> profile_stream;
73 std::unique_ptr<std::ofstream> kernel_id_stream;
74 std::vector<std::pair<std::string, rt::KernelId>> kernel_map;
75 bool profiling_enabled = false;
76} _drv;
77
78// Check at runtime environment variables for paths likely holding ET toolchain with sysemu elf files
79static std::string ggml_et_get_default_et_path() {
80 // List of environment variables to check in order of preference
81 const char * const env_vars[] = { "ET_TOOLCHAIN", "TOOLCHAIN_ROOT" };
82
83 for (const char * var : env_vars) {
84 if (const char * et_path = std::getenv(var)) {
85 if (et_path && *et_path != '\0') {
86 return fs::path(et_path).string();
87 }
88 }
89 }
90
91 // Otherwise assume default
92 return fs::path("/opt/et").string();
93}
94
95// config when using sysemu instead of PCIe hardware device
96// adapted from `ainekko/et-platform/esperanto-tools-libs/tools/src/bench.cpp`
97static inline auto ggml_et_get_default_sysemu_options() {
98 constexpr uint64_t kSysEmuMaxCycles = std::numeric_limits<uint64_t>::max();
99 constexpr uint64_t kSysEmuMinionShiresMask = 0x1FFFFFFFFu;
100 const std::string et_path = ggml_et_get_default_et_path() + "/";
101
102 emu::SysEmuOptions sysEmuOptions;
103
104 // Construct all paths
105 sysEmuOptions.bootromTrampolineToBL2ElfPath =
106 et_path + "lib/esperanto-fw/BootromTrampolineToBL2/BootromTrampolineToBL2.elf";
107 sysEmuOptions.spBL2ElfPath =
108 et_path + "lib/esperanto-fw/ServiceProcessorBL2/fast-boot/ServiceProcessorBL2_fast-boot.elf";
109 sysEmuOptions.machineMinionElfPath = et_path + "lib/esperanto-fw/MachineMinion/MachineMinion.elf";
110 sysEmuOptions.masterMinionElfPath = et_path + "lib/esperanto-fw/MasterMinion/MasterMinion.elf";
111 sysEmuOptions.workerMinionElfPath = et_path + "lib/esperanto-fw/WorkerMinion/WorkerMinion.elf";
112 sysEmuOptions.executablePath = et_path + "bin/sys_emu";
113
114 // Check that each path has a valid existing non-zero file otherwise emulator just silently hangs
115 const std::vector<std::string> required_files = {
116 sysEmuOptions.bootromTrampolineToBL2ElfPath, sysEmuOptions.spBL2ElfPath,
117 sysEmuOptions.machineMinionElfPath, sysEmuOptions.masterMinionElfPath,
118 sysEmuOptions.workerMinionElfPath, sysEmuOptions.executablePath,
119 };
120
121 for (const auto & file : required_files) {
122 if (!fs::exists(file) || fs::file_size(file) == 0) {
123 // Check that each path has a valid existing non-zero file otherwise emulator just silently hangs
124 GGML_LOG_ERROR("ET: Unable to find required sysemu file: %s", file.c_str());
125 GGML_LOG_ERROR("ET: Confirm et-platform is correctly installed at configured path.");
126 abort();
127 }
128 }
129
130 sysEmuOptions.runDir = (fs::current_path().string() + "/");
131 sysEmuOptions.maxCycles = kSysEmuMaxCycles;
132 sysEmuOptions.minionShiresMask = kSysEmuMinionShiresMask;
133 sysEmuOptions.puUart0Path = sysEmuOptions.runDir + "pu_uart0_tx.log";
134 sysEmuOptions.puUart1Path = sysEmuOptions.runDir + "pu_uart1_tx.log";
135 sysEmuOptions.spUart0Path = sysEmuOptions.runDir + "spio_uart0_tx.log";
136 sysEmuOptions.spUart1Path = sysEmuOptions.runDir + "spio_uart1_tx.log";
137 sysEmuOptions.startGdb = false;
138 sysEmuOptions.memcheck = false;
139
140 return sysEmuOptions;
141}
142
143// Forward declaration
144static void ggml_et_driver_cleanup();
145
146static bool ggml_et_driver_init() {
147 if (_drv.runtime != nullptr) {
148 assert(_drv.device_layer != nullptr);
149 } else {
150 try {
151#if defined GGML_ET_SYSEMU && GGML_ET_SYSEMU
152 // For emulator device using sysEmuOptions provided by function above enabled compiling with `-DGGML_ET_SYSEMU=ON`
153 _drv.device_layer = dev::IDeviceLayer::createSysEmuDeviceLayer(ggml_et_get_default_sysemu_options());
154#else
155 // For physical PCIe device
156 _drv.device_layer = dev::IDeviceLayer::createPcieDeviceLayer();
157#endif // GGML_ET_SYSEMU
158
159 _drv.runtime = rt::IRuntime::create(_drv.device_layer);
160
161 // Initialize profiler if requested via environment variable
162 const char * profile_path = getenv("GGML_ET_PROFILE");
163 if (profile_path) {
164 std::string output_path = std::string(profile_path) + "/et_runtime_trace.json";
165 std::string kernel_id_path = std::string(profile_path) + "/kernel_id.json";
166
167 _drv.profile_stream = std::make_unique<std::ofstream>(output_path);
168 _drv.kernel_id_stream = std::make_unique<std::ofstream>(kernel_id_path);
169 if (!_drv.profile_stream->is_open()) {
170 GGML_LOG_ERROR("ET: Failed to open profiling output file: %s", output_path.c_str());
171 abort();
172 }
173 if (!_drv.kernel_id_stream->is_open()) {
174 GGML_LOG_ERROR("ET: Failed to open profiling kernel map: %s", kernel_id_path.c_str());
175 abort();
176 }
177
178 auto * profiler = _drv.runtime->getProfiler();
179 profiler->start(*_drv.profile_stream, rt::IProfiler::OutputType::Json);
180 _drv.profiling_enabled = true;
181 GGML_LOG_INFO("ET: Runtime profiler started (JSON format)");
182
183 // Register cleanup at program exit
184 std::atexit(ggml_et_driver_cleanup);
185 }
186 } catch (const std::exception & e) {
187 GGML_LOG_ERROR("ggml_et: %s", e.what());
188 if (_drv.device_layer != nullptr) {
189 _drv.device_layer.reset();
190 }
191 if (_drv.runtime != nullptr) {
192 _drv.runtime.reset();
193 }
194 return false;
195 }
196 }
197 return true;
198}
199
200static std::shared_ptr<dev::IDeviceLayer> ggml_et_devicelayer() {
201 return _drv.device_layer;
202}
203
204std::shared_ptr<rt::IRuntime> ggml_et_runtime() {
205 return _drv.runtime;
206}
207
208static void ggml_et_driver_cleanup() {
209 if (_drv.profiling_enabled && _drv.runtime) {
210 GGML_LOG_INFO("ET: Stopping runtime profiler");
211 auto * profiler = _drv.runtime->getProfiler();
212 profiler->stop();
213 _drv.profiling_enabled = false;
214
215 if (_drv.profile_stream) {
216 _drv.profile_stream->close();
217 _drv.profile_stream.reset();
218 }
219
220 // Save kernel map
221 if (_drv.kernel_id_stream && !_drv.kernel_map.empty()) {
222 auto & os = *_drv.kernel_id_stream;
223 // XXX: Manual JSON construction. Not pretty but removes dependency
224 os << "{\n";
225 for (size_t i = 0; i < _drv.kernel_map.size(); i++) {
226 os << " \"" << _drv.kernel_map[i].first << "\": " << (int) _drv.kernel_map[i].second;
227 if (i + 1 < _drv.kernel_map.size()) {
228 os << ",";
229 }
230 os << "\n";
231 }
232 os << "}\n";
233 _drv.kernel_id_stream->close();
234 _drv.kernel_id_stream.reset();
235 }
236 }
237}
238
239static ggml_backend_dev_t ggml_backend_et_reg_get_device(ggml_backend_reg_t reg, size_t devidx);
240
241static void ggml_backend_et_buffer_free_buffer(ggml_backend_buffer_t buffer) {
242 ggml_backend_et_buffer_context * ctx = (ggml_backend_et_buffer_context *) buffer->context;
243 if (ctx->data != nullptr) {
244 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
245 if (runtime) {
246 runtime->freeDevice(ctx->rtid, static_cast<std::byte *>(ctx->data));
247 }
248 }
249 delete ctx;
250}
251
252static void * ggml_backend_et_buffer_get_base(ggml_backend_buffer_t buffer) {
253 ggml_backend_et_buffer_context * ctx = (ggml_backend_et_buffer_context *) buffer->context;
254 return ctx->data;
255}
256
257static ggml_status ggml_backend_et_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
258 // View tensors share buffer with their view_src, no additional initialization needed
259 if (tensor->view_src != NULL) {
260 return GGML_STATUS_SUCCESS;
261 }
262
263 const size_t original_size = ggml_nbytes(tensor);
264 const size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
265
266 // Clear padding bytes to avoid NaN values
267 // XXX: Martin - do we need this?
268 if (padded_size > original_size) {
269 const size_t padding_size = padded_size - original_size;
270
271 // Get device context to access memops kernel
272 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) buffer->buft->device->context;
273 if (!dev_ctx) {
274 GGML_LOG_ERROR("ET: Failed to get device context for padding clear");
275 return GGML_STATUS_FAILED;
276 }
277
278 // Use device-side memset kernel for efficient padding clear
279 std::byte * padding_ptr = static_cast<std::byte *>(tensor->data) + original_size;
280 if (!ggml_et_memset(dev_ctx, padding_ptr, 0, padding_size)) {
281 GGML_LOG_ERROR("ET: Failed to clear padding using memset kernel for tensor %s", tensor->name);
282 return GGML_STATUS_FAILED;
283 }
284 }
285
286 return GGML_STATUS_SUCCESS;
287}
288
289static void ggml_backend_et_buffer_set_tensor(ggml_backend_buffer_t buffer,
290 ggml_tensor * tensor,
291 const void * data,
292 size_t offset,
293 size_t size) {
294 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
295 if (!runtime) {
296 return;
297 }
298
299 // Create short-lived stream for this transfer
300 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) buffer->buft->device->context;
301 rt::StreamId stream = dev_ctx->default_stream;
302
303 std::byte * dst_ptr = static_cast<std::byte *>(tensor->data) + offset;
304 const std::byte * src_ptr = static_cast<const std::byte *>(data);
305
306 rt::EventId event = runtime->memcpyHostToDevice(stream, src_ptr, dst_ptr, size, true /*barrier*/);
307
308 runtime->waitForEvent(event);
309}
310
311static void ggml_backend_et_buffer_get_tensor(ggml_backend_buffer_t buffer,
312 const ggml_tensor * tensor,
313 void * data,
314 size_t offset,
315 size_t size) {
316 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
317 if (!runtime) {
318 return;
319 }
320
321 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) buffer->buft->device->context;
322 rt::StreamId stream = dev_ctx->default_stream;
323
324 const std::byte * src_ptr = static_cast<const std::byte *>(tensor->data) + offset;
325 std::byte * dst_ptr = static_cast<std::byte *>(data);
326
327 rt::EventId event = runtime->memcpyDeviceToHost(stream, src_ptr, dst_ptr, size, true /*barrier*/);
328
329 runtime->waitForEvent(event);
330}
331
332static bool ggml_backend_et_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
333 const ggml_tensor * src,
334 ggml_tensor * dst) {
335 GGML_UNUSED(buffer);
336 GGML_UNUSED(src);
337 GGML_UNUSED(dst);
338 return false;
339}
340
341static void ggml_backend_et_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
342 ggml_backend_et_buffer_context * ctx = (ggml_backend_et_buffer_context *) buffer->context;
343
344 if (ctx->size == 0 || ctx->data == nullptr) {
345 return;
346 }
347
348 // Get device context to access memops kernel
349 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) buffer->buft->device->context;
350 if (!dev_ctx) {
351 GGML_LOG_ERROR("ET: Failed to get device context for buffer clear");
352 return;
353 }
354
355 // Use device-side memset kernel for efficient clearing
356 if (!ggml_et_memset(dev_ctx, ctx->data, value, ctx->size)) {
357 GGML_LOG_ERROR("ET: buffer_clear failed using memset kernel");
358 return;
359 }
360
361 GGML_LOG_DEBUG("ET: Buffer cleared successfully using memops kernel");
362}
363
364static const struct ggml_backend_buffer_i ggml_backend_et_buffer_i = {
365 /* .free_buffer = */ ggml_backend_et_buffer_free_buffer,
366 /* .get_base = */ ggml_backend_et_buffer_get_base,
367 /* .init_tensor = */ ggml_backend_et_buffer_init_tensor,
368 /* .memset_tensor = */ NULL,
369 /* .set_tensor = */ ggml_backend_et_buffer_set_tensor,
370 /* .get_tensor = */ ggml_backend_et_buffer_get_tensor,
371 /* .set_tensor_2d = */ NULL,
372 /* .get_tensor_2d = */ NULL,
373 /* .cpy_tensor = */ ggml_backend_et_buffer_cpy_tensor,
374 /* .clear = */ ggml_backend_et_buffer_clear,
375 /* .reset = */ NULL,
376 /* .set_tensor_n = */ NULL,
377 /* .get_tensor_n = */ NULL,
378 /* .cpy_tensor_n = */ NULL,
379};
380
381static const char * ggml_backend_et_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
382 GGML_UNUSED(buft);
383 return GGML_ET_NAME;
384}
385
386static ggml_backend_buffer_t ggml_backend_et_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
387 ggml_backend_et_buffer_type_context * btctx = (ggml_backend_et_buffer_type_context *) buft->context;
388
389 ggml_backend_et_buffer_context * ctx = new ggml_backend_et_buffer_context;
390 ctx->devidx = btctx->devidx;
391 ctx->size = size;
392
393 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
394 if (!runtime) {
395 delete ctx;
396 return nullptr;
397 }
398
399 std::vector<rt::DeviceId> rtids = runtime->getDevices();
400 if (static_cast<size_t>(btctx->devidx) >= rtids.size()) {
401 delete ctx;
402 return nullptr;
403 }
404 ctx->rtid = rtids[btctx->devidx];
405
406 ctx->data = runtime->mallocDevice(ctx->rtid, size);
407 if (ctx->data == nullptr) {
408 delete ctx;
409 return nullptr;
410 }
411
412 return ggml_backend_buffer_init(buft, ggml_backend_et_buffer_i, ctx, size);
413}
414
415static size_t ggml_backend_et_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
416 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
417 if (!runtime || !buft->device) {
418 return GGML_MEM_ALIGN;
419 }
420
421 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) buft->device->context;
422 rt::DeviceProperties prop = runtime->getDeviceProperties(dev_ctx->rtid);
423 return prop.cacheLineSize_;
424}
425
426static size_t ggml_backend_et_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
427 if (buft->device) {
428 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) buft->device->context;
429 return dev_ctx->total_mem;
430 }
431 return SIZE_MAX;
432}
433
434static size_t ggml_backend_et_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
435 GGML_UNUSED(buft);
436 return ggml_nbytes_pad(tensor);
437}
438
439static bool ggml_backend_et_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
440 GGML_UNUSED(buft);
441 return false;
442}
443
444static const struct ggml_backend_buffer_type_i ggml_backend_et_buffer_type_i = {
445 /* .get_name = */ ggml_backend_et_buffer_type_get_name,
446 /* .alloc_buffer = */ ggml_backend_et_buffer_type_alloc_buffer,
447 /* .alloc_buffer_n = */ NULL,
448 /* .get_alignment = */ ggml_backend_et_buffer_type_get_alignment,
449 /* .get_max_size = */ ggml_backend_et_buffer_type_get_max_size,
450 /* .get_alloc_size = */ ggml_backend_et_buffer_type_get_alloc_size,
451 /* .get_alloc_size_n = */ NULL,
452 /* .is_host = */ ggml_backend_et_buffer_type_is_host,
453};
454
455static const char * ggml_backend_et_get_name(ggml_backend_t backend) {
456 GGML_UNUSED(backend);
457 return GGML_ET_NAME;
458}
459
460static void ggml_backend_et_free(ggml_backend_t backend) {
461 ggml_backend_et_context * et_ctx = (ggml_backend_et_context *) backend->context;
462 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
463
464 // Clean up kernels on this device before freeing backend
465 ggml_backend_dev_t dev = ggml_backend_et_reg_get_device(ggml_backend_et_reg(), et_ctx->devidx);
466 if (dev && dev->context) {
467 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
468
469 if (_drv.profiling_enabled) {
470 auto kernels = ggml_et_get_loaded_kernels(dev_ctx);
471 _drv.kernel_map.insert(_drv.kernel_map.end(), kernels.begin(), kernels.end());
472 }
473
474 ggml_et_unload_all_kernels(dev_ctx);
475
476 if (runtime) {
477 if (dev_ctx->trace_buffer) {
478 runtime->freeDevice(dev_ctx->rtid, dev_ctx->trace_buffer);
479 dev_ctx->trace_buffer = nullptr;
480 }
481 // Drain any in-flight uberkernel launches before freeing the
482 // device buffers they read from.
483 runtime->waitForStream(dev_ctx->default_stream);
484 for (auto & slot : dev_ctx->uberkernel.slots) {
485 if (slot.device_insts) {
486 runtime->freeDevice(dev_ctx->rtid, slot.device_insts);
487 slot.device_insts = nullptr;
488 }
489 if (slot.device_params) {
490 runtime->freeDevice(dev_ctx->rtid, slot.device_params);
491 slot.device_params = nullptr;
492 }
493 slot.has_pending = false;
494 }
495 }
496 }
497
498 delete et_ctx;
499 delete backend;
500}
501
502static ggml_backend_buffer_type_t ggml_backend_et_get_default_buffer_type(ggml_backend_t backend) {
503 ggml_backend_et_context * et_ctx = (ggml_backend_et_context *) backend->context;
504
505 return ggml_backend_et_buffer_type(et_ctx->devidx);
506}
507
508static void ggml_backend_et_set_tensor_async(ggml_backend_t backend,
509 ggml_tensor * tensor,
510 const void * data,
511 size_t offset,
512 size_t size) {
513 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
514 if (!runtime) {
515 return;
516 }
517
518 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) backend->device->context;
519 rt::StreamId stream = dev_ctx->default_stream;
520
521 std::byte * dst_ptr = static_cast<std::byte *>(tensor->data) + offset;
522 const std::byte * src_ptr = static_cast<const std::byte *>(data);
523
524 runtime->memcpyHostToDevice(stream, src_ptr, dst_ptr, size, true /*barrier*/);
525}
526
527static void ggml_backend_et_get_tensor_async(ggml_backend_t backend,
528 const ggml_tensor * tensor,
529 void * data,
530 size_t offset,
531 size_t size) {
532 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
533 if (!runtime) {
534 return;
535 }
536
537 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) backend->device->context;
538 rt::StreamId stream = dev_ctx->default_stream;
539
540 const std::byte * src_ptr = static_cast<const std::byte *>(tensor->data) + offset;
541 std::byte * dst_ptr = static_cast<std::byte *>(data);
542
543 runtime->memcpyDeviceToHost(stream, src_ptr, dst_ptr, size, true /*barrier*/);
544}
545
546static bool ggml_backend_et_cpy_tensor_async(ggml_backend_t backend_src,
547 ggml_backend_t backend_dst,
548 const ggml_tensor * src,
549 ggml_tensor * dst) {
550 GGML_UNUSED(backend_src);
551 GGML_UNUSED(backend_dst);
552 GGML_UNUSED(src);
553 GGML_UNUSED(dst);
554 return false;
555}
556
557static void ggml_backend_et_synchronize(ggml_backend_t backend) {
558 std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
559 if (!runtime) {
560 return;
561 }
562
563 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) backend->device->context;
564 runtime->waitForStream(dev_ctx->default_stream);
565
566 auto errors = runtime->retrieveStreamErrors(dev_ctx->default_stream);
567 if (errors.empty()) {
568 return;
569 }
570 for (const auto & err : errors) {
571 GGML_LOG_ERROR("ET: stream error detected at synchronization point. Code: %d,Type: %d\n", (int) err.errorCode_,
572 (int) err.errorContext_.value()[0].type_);
573 }
574 abort();
575}
576
577static bool ggml_et_can_fuse(const ggml_cgraph * cgraph, int node_idx, std::initializer_list<ggml_op> ops) {
578 if (!ggml_can_fuse(cgraph, node_idx, ops)) {
579 return false;
580 }
581
582 if (ops.size() == 2 && ops.begin()[0] == GGML_OP_MUL_MAT && ops.begin()[1] == GGML_OP_ADD) {
583 const ggml_tensor * mm = cgraph->nodes[node_idx];
584 const ggml_tensor * add = cgraph->nodes[node_idx + 1];
585
586 // Only Q8_0 weights x F32 activations -> F32 (the kernel that has
587 // the bias path). Other MM variants must wait for their own kernel
588 // bias support.
589 if (mm->type != GGML_TYPE_F32 || mm->src[0]->type != GGML_TYPE_Q8_0 || mm->src[1]->type != GGML_TYPE_F32) {
590 return false;
591 }
592
593 // ADD must be F32 and one of its operands must be the MM output.
594 if (add->type != GGML_TYPE_F32) {
595 return false;
596 }
597 if (add->src[0] != mm && add->src[1] != mm) {
598 return false;
599 }
600
601 const ggml_tensor * bias = (add->src[0] == mm) ? add->src[1] : add->src[0];
602
603 if (bias->type != GGML_TYPE_F32) {
604 return false;
605 }
606
607 // No broadcasting: bias shape must equal MM output shape.
608 for (int i = 0; i < GGML_MAX_DIMS; ++i) {
609 if (bias->ne[i] != mm->ne[i]) {
610 return false;
611 }
612 }
613
614 // Bias and dst must be contiguous and have identical strides - the
615 // kernel uses dst-style offset arithmetic against bias's nb[].
616 if (!ggml_is_contiguous(bias) || !ggml_is_contiguous(mm)) {
617 return false;
618 }
619 for (int i = 0; i < GGML_MAX_DIMS; ++i) {
620 if ((int64_t) bias->nb[i] != (int64_t) add->nb[i]) {
621 return false;
622 }
623 }
624 }
625
626 if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
627 const ggml_tensor * rms_norm = cgraph->nodes[node_idx];
628 const ggml_tensor * mul = cgraph->nodes[node_idx + 1];
629
630 // ET only supports F32
631 if (rms_norm->src[0]->type != GGML_TYPE_F32 || mul->type != GGML_TYPE_F32) {
632 return false;
633 }
634
635 // Identify the weights tensor (the MUL operand that isn't rms_norm output)
636 const ggml_tensor * weights = (mul->src[0] == rms_norm) ? mul->src[1] : mul->src[0];
637
638 if (weights->type != GGML_TYPE_F32) {
639 return false;
640 }
641
642 // Both inputs must be contiguous (ET hardware requirement)
643 if (!ggml_is_contiguous(rms_norm->src[0]) || !ggml_is_contiguous_rows(weights)) {
644 return false;
645 }
646
647 // ET requires cache-aligned rows (ne[0] % 16 == 0)
648 if (rms_norm->src[0]->ne[0] % 16 != 0 || weights->ne[0] % 16 != 0) {
649 return false;
650 }
651
652 // Fused kernel doesn't handle dim-0 broadcasting
653 if (weights->ne[0] != rms_norm->src[0]->ne[0]) {
654 return false;
655 }
656 }
657
658 return true;
659}
660
661static ggml_status ggml_backend_et_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
662 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) backend->device->context;
663 ggml_et_uberkernel_begin_graph(&dev_ctx->uberkernel);
664
665 for (int i = 0; i < cgraph->n_nodes; i++) {
666 ggml_tensor * node = cgraph->nodes[i];
667
668 if (node->op == GGML_OP_NONE || node->op == GGML_OP_VIEW || node->op == GGML_OP_RESHAPE ||
669 node->op == GGML_OP_PERMUTE || node->op == GGML_OP_TRANSPOSE) {
670 continue;
671 }
672
673 // --- Fusion checks (before regular dispatch) ---
674 if (ggml_et_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
675 ggml_et_op_rms_norm_mul(dev_ctx, node, cgraph->nodes[i + 1]);
676 i++; // skip the MUL node
677 continue;
678 }
679 if (ggml_et_can_fuse(cgraph, i, { GGML_OP_MUL_MAT, GGML_OP_ADD })) {
680 ggml_et_op_mul_mat(dev_ctx, node, cgraph->nodes[i + 1]);
681 i++; // skip the ADD node
682 continue;
683 }
684
685 switch (node->op) {
686 case GGML_OP_SQR:
687 ggml_et_op_sqr(dev_ctx, node);
688 break;
689
690 case GGML_OP_UNARY:
691 ggml_et_op_unary(dev_ctx, node);
692 break;
693
694 case GGML_OP_SUM_ROWS:
695 ggml_et_op_sum_rows(dev_ctx, node);
696 break;
697
698 case GGML_OP_MEAN:
699 ggml_et_op_mean(dev_ctx, node);
700 break;
701
702 case GGML_OP_CLAMP:
703 ggml_et_op_clamp(dev_ctx, node);
704 break;
705
706 case GGML_OP_MUL:
707 ggml_et_op_mul(dev_ctx, node);
708 break;
709
710 case GGML_OP_ADD:
711 ggml_et_op_add(dev_ctx, node);
712 break;
713
714 case GGML_OP_SUB:
715 ggml_et_op_sub(dev_ctx, node);
716 break;
717
718 case GGML_OP_CUMSUM:
719 ggml_et_op_cumsum(dev_ctx, node);
720 break;
721
722 case GGML_OP_MUL_MAT:
723 ggml_et_op_mul_mat(dev_ctx, node);
724 break;
725
726 case GGML_OP_MUL_MAT_ID:
727 ggml_et_op_mul_mat_id(dev_ctx, node);
728 break;
729
730 case GGML_OP_ROPE:
731 ggml_et_op_rope(dev_ctx, node);
732 break;
733
734 case GGML_OP_RMS_NORM:
735 ggml_et_op_rms_norm(dev_ctx, node);
736 break;
737
738 case GGML_OP_NORM:
739 ggml_et_op_norm(dev_ctx, node);
740 break;
741
742 case GGML_OP_L2_NORM:
743 ggml_et_op_l2_norm(dev_ctx, node);
744 break;
745
746 case GGML_OP_GROUP_NORM:
747 ggml_et_op_group_norm(dev_ctx, node);
748 break;
749
750 case GGML_OP_SCALE:
751 ggml_et_op_scale(dev_ctx, node);
752 break;
753
754 case GGML_OP_GLU:
755 ggml_et_op_glu(dev_ctx, node);
756 break;
757
758 case GGML_OP_SOFT_MAX:
759 ggml_et_op_softmax(dev_ctx, node);
760 break;
761
762 case GGML_OP_IM2COL:
763 ggml_et_op_im2col(dev_ctx, node);
764 break;
765
766 case GGML_OP_CONV_2D:
767 ggml_et_op_conv_2d(dev_ctx, node);
768 break;
769
770 case GGML_OP_FLASH_ATTN_EXT:
771 ggml_et_op_flash_attn_ext(dev_ctx, node);
772 break;
773
774 case GGML_OP_GET_ROWS:
775 ggml_et_op_get_rows(dev_ctx, node);
776 break;
777
778 case GGML_OP_CONT:
779 ggml_et_op_cont(dev_ctx, node);
780 break;
781
782 case GGML_OP_CPY:
783 ggml_et_op_cpy(dev_ctx, node);
784 break;
785
786 case GGML_OP_CONCAT:
787 ggml_et_op_concat(dev_ctx, node);
788 break;
789
790 case GGML_OP_REPEAT:
791 ggml_et_op_repeat(dev_ctx, node);
792 break;
793
794 case GGML_OP_SSM_CONV:
795 ggml_et_op_ssm_conv(dev_ctx, node);
796 break;
797
798 case GGML_OP_SSM_SCAN:
799 ggml_et_op_ssm_scan(dev_ctx, node);
800 break;
801
802 case GGML_OP_PAD:
803 ggml_et_op_pad(dev_ctx, node);
804 break;
805
806 case GGML_OP_SET_ROWS:
807 ggml_et_op_set_rows(dev_ctx, node);
808 break;
809
810 case GGML_OP_FILL:
811 ggml_et_op_fill(dev_ctx, node);
812 break;
813
814 case GGML_OP_DIAG:
815 ggml_et_op_diag(dev_ctx, node);
816 break;
817
818 case GGML_OP_TRI:
819 ggml_et_op_tri(dev_ctx, node);
820 break;
821
822 case GGML_OP_SOLVE_TRI:
823 ggml_et_op_solve_tri(dev_ctx, node);
824 break;
825
826 case GGML_OP_SET:
827 ggml_et_op_set(dev_ctx, node);
828 break;
829
830 case GGML_OP_RWKV_WKV6:
831 ggml_et_op_rwkv_wkv6(dev_ctx, node);
832 break;
833
834 case GGML_OP_RWKV_WKV7:
835 ggml_et_op_rwkv_wkv7(dev_ctx, node);
836 break;
837
838 case GGML_OP_GATED_DELTA_NET:
839 ggml_et_op_gated_delta_net(dev_ctx, node);
840 break;
841
842 default:
843 ggml_et_uberkernel_abort_graph(&dev_ctx->uberkernel);
844 GGML_LOG_ERROR("ET: Unsupported operation in graph: %s", ggml_op_name(node->op));
845 return GGML_STATUS_FAILED;
846 }
847
848 if (ggml_et_uberkernel_failed(&dev_ctx->uberkernel)) {
849 ggml_et_uberkernel_abort_graph(&dev_ctx->uberkernel);
850 return GGML_STATUS_FAILED;
851 }
852 }
853
854 if (!ggml_et_uberkernel_end_graph(dev_ctx)) {
855 ggml_et_uberkernel_abort_graph(&dev_ctx->uberkernel);
856 return GGML_STATUS_FAILED;
857 }
858
859 return GGML_STATUS_SUCCESS;
860}
861
862// Check that elements within each row are contiguous (nb[0] == type_size).
863// Higher-dim strides can be arbitrary - kernels navigate them via byte offsets.
864static bool et_ggml_is_row_contiguous(const ggml_tensor * t) {
865 return t->nb[0] == ggml_type_size(t->type);
866}
867
868static bool ggml_backend_et_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
869 GGML_UNUSED(dev);
870
871 bool supported = false;
872 switch (op->op) {
873 case GGML_OP_CUMSUM:
874 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
875 op->src[0]->nb[0] == sizeof(float) && ggml_is_contiguous(op);
876 break;
877 case GGML_OP_SQR:
878 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
879 op->ne[0] % 16 == 0 && ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]);
880 break;
881 case GGML_OP_SUM_ROWS:
882 // dst has ne[0]=1, src0 row length must be cache-aligned
883 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
884 op->src[0]->ne[0] % 16 == 0 && ggml_is_contiguous(op->src[0]);
885 break;
886 case GGML_OP_MEAN:
887 // Kernel handles arbitrary ne00 (per-row alignment guard with
888 // scalar tail), so no row-length divisibility constraint here.
889 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
890 ggml_is_contiguous(op->src[0]);
891 break;
892 case GGML_OP_CLAMP:
893 // Element-wise; kernel distributes by cache lines and handles a
894 // scalar tail, so any contiguous F32 size is fine - including the
895 // 1x1x1x1 scalar case.
896 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
897 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]);
898 break;
899 case GGML_OP_UNARY:
900 // Only require dim-0 contiguity (nb[0] == sizeof(float)). Higher
901 // dims may be arbitrarily strided views; the kernel walks per-row
902 // using all four nb[] values. See unary_f32.c entry_point.
903 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
904 ggml_nelements(op) % 16 == 0 && op->nb[0] == sizeof(float) && op->src[0]->nb[0] == sizeof(float)) {
905 switch (ggml_get_unary_op(op)) {
906 case GGML_UNARY_OP_ABS:
907 case GGML_UNARY_OP_SGN:
908 case GGML_UNARY_OP_NEG:
909 case GGML_UNARY_OP_STEP:
910 case GGML_UNARY_OP_TANH:
911 case GGML_UNARY_OP_ELU:
912 case GGML_UNARY_OP_RELU:
913 case GGML_UNARY_OP_SIGMOID:
914 case GGML_UNARY_OP_GELU:
915 case GGML_UNARY_OP_GELU_QUICK:
916 case GGML_UNARY_OP_SILU:
917 case GGML_UNARY_OP_HARDSWISH:
918 case GGML_UNARY_OP_HARDSIGMOID:
919 case GGML_UNARY_OP_EXP:
920 case GGML_UNARY_OP_EXPM1:
921 case GGML_UNARY_OP_SOFTPLUS:
922 case GGML_UNARY_OP_GELU_ERF:
923 case GGML_UNARY_OP_FLOOR:
924 case GGML_UNARY_OP_CEIL:
925 case GGML_UNARY_OP_ROUND:
926 case GGML_UNARY_OP_TRUNC:
927 supported = true;
928 break;
929 default:
930 break;
931 }
932 }
933 break;
934 case GGML_OP_MUL:
935 case GGML_OP_ADD:
936 case GGML_OP_SUB:
937 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
938 op->src[1]->type == GGML_TYPE_F32 && op->nb[0] == sizeof(float) &&
939 op->src[0]->nb[0] == sizeof(float) &&
940 (op->src[1]->nb[0] == sizeof(float) || op->src[1]->ne[0] == 1) &&
941 op->nb[1] == op->ne[0] * sizeof(float);
942 break;
943 case GGML_OP_MUL_MAT:
944 // Support Q8_0 x F32 -> F32, F16 x F32 -> F32, F16 x F16 -> F32, and F32 x F32 -> F32 matrix multiplication
945 // Stride requirements: first dimension must be contiguous for all tensors
946 if (op->type == GGML_TYPE_F32 &&
947 ((op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32) ||
948 (op->src[0]->type == GGML_TYPE_F16 && op->src[1]->type == GGML_TYPE_F16)) &&
949 op->ne[0] % 16 == 0 && // dst row length for tensor-store path
950 op->src[0]->ne[1] % 16 == 0 && // m
951 op->src[0]->ne[0] % 16 == 0 && // k
952 ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1])) {
953 // Special path for the FP32 TensorFMA kernel
954 // Limitation - generic kernels can tolerate non-cache-aligned dst rows
955 // because they publish each output element atomically. The matrix
956 // engine path still uses tiled tensor stores, so keep dst rows aligned.
957 // The m edge is difficult to do because of the 4 conseqtive load hardware limitation
958 // And the k edge is impossible because that is encoded as `stride & 0xFFFFFFFFFFC0ULL` which becomes 0 for stride 16 (4x FP32) :(
959 // FIXME: Right now this overwrites the mul_mat_f32 kernel - whatever. Fix later. Demo code
960 supported = true;
961 } else if (op->type == GGML_TYPE_F32 && op->src[0] &&
962 (op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32) && op->src[1] &&
963 (op->src[1]->type == GGML_TYPE_F16 || op->src[1]->type == GGML_TYPE_F32)) {
964 // Check first dimension contiguity requirements
965 bool src0_first_dim_contiguous = (op->src[0]->nb[0] == ggml_type_size(op->src[0]->type));
966 bool src1_first_dim_contiguous = (op->src[1]->nb[0] == ggml_type_size(op->src[1]->type));
967 bool dst_first_dim_contiguous = (op->nb[0] == sizeof(float));
968
969 // Check destination stride ordering (only for dimensions with ne > 1)
970 bool dst_properly_ordered = true;
971 for (int d = 0; d < 3; d++) {
972 if (op->ne[d] > 1 && op->ne[d + 1] > 1 && op->nb[d] > op->nb[d + 1]) {
973 dst_properly_ordered = false;
974 }
975 }
976
977 supported = src0_first_dim_contiguous && src1_first_dim_contiguous && dst_first_dim_contiguous &&
978 dst_properly_ordered;
979 } else if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_Q8_0 && op->src[1] &&
980 op->src[1]->type == GGML_TYPE_F32) {
981 // Keep the existing quantized path constraints separate from the
982 // relaxed non-quant generic fallback.
983 bool src0_first_dim_contiguous = (op->src[0]->nb[0] == ggml_type_size(op->src[0]->type));
984 bool src1_first_dim_contiguous = (op->src[1]->nb[0] == ggml_type_size(op->src[1]->type));
985 bool dst_first_dim_contiguous = (op->nb[0] == sizeof(float));
986
987 bool dst_properly_ordered = true;
988 for (int d = 0; d < 3; d++) {
989 if (op->ne[d] > 1 && op->ne[d + 1] > 1 && op->nb[d] > op->nb[d + 1]) {
990 dst_properly_ordered = false;
991 }
992 }
993
994 supported = src0_first_dim_contiguous && src1_first_dim_contiguous && dst_first_dim_contiguous &&
995 dst_properly_ordered;
996
997 } else if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_Q4_0 && op->src[1] &&
998 op->src[1]->type == GGML_TYPE_F32) {
999 // Keep the existing quantized path constraints separate from the
1000 // relaxed non-quant generic fallback.
1001 bool src0_first_dim_contiguous = (op->src[0]->nb[0] == ggml_type_size(op->src[0]->type));
1002 bool src1_first_dim_contiguous = (op->src[1]->nb[0] == ggml_type_size(op->src[1]->type));
1003 bool dst_first_dim_contiguous = (op->nb[0] == sizeof(float));
1004
1005 bool dst_properly_ordered = true;
1006 for (int d = 0; d < 3; d++) {
1007 if (op->ne[d] > 1 && op->ne[d + 1] > 1 && op->nb[d] > op->nb[d + 1]) {
1008 dst_properly_ordered = false;
1009 }
1010 }
1011
1012 supported = src0_first_dim_contiguous && src1_first_dim_contiguous && dst_first_dim_contiguous &&
1013 dst_properly_ordered;
1014 } else {
1015 supported = false;
1016 }
1017 break;
1018 case GGML_OP_MUL_MAT_ID:
1019 // Support MUL_MAT_ID for Mixture of Experts: (Q8_0/Q4_0/F16/F32) x F32 -> F32 with I32 expert indices
1020 // src0 (as): [K, M, n_expert] - expert weight matrices (can be quantized)
1021 // src1 (b): [K, n_expert_used, batch] - activations (F32)
1022 // src2 (ids): [n_expert_used, batch] - expert selection indices (I32)
1023 // dst: [M, n_expert_used, batch, 1] - output (F32)
1024 if (op->type == GGML_TYPE_F32 && op->src[0] &&
1025 (op->src[0]->type == GGML_TYPE_Q8_0 || op->src[0]->type == GGML_TYPE_Q4_0 ||
1026 op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32) &&
1027 op->src[1] && op->src[1]->type == GGML_TYPE_F32 && op->src[2] && op->src[2]->type == GGML_TYPE_I32) {
1028 // Check first dimension contiguity requirements (matching CPU backend)
1029 bool src0_first_dim_contiguous = (op->src[0]->nb[0] == ggml_type_size(op->src[0]->type));
1030 bool src1_first_dim_contiguous = (op->src[1]->nb[0] == ggml_type_size(op->src[1]->type));
1031 bool src2_first_dim_contiguous = (op->src[2]->nb[0] == ggml_type_size(op->src[2]->type));
1032 bool dst_first_dim_contiguous = (op->nb[0] == sizeof(float));
1033
1034 // Check destination stride ordering (only for dimensions with ne > 1)
1035 bool dst_properly_ordered = true;
1036 for (int d = 0; d < 3; d++) {
1037 if (op->ne[d] > 1 && op->ne[d + 1] > 1 && op->nb[d] > op->nb[d + 1]) {
1038 dst_properly_ordered = false;
1039 }
1040 }
1041
1042 // Validate tensor dimension constraints from GGML definition
1043 bool dims_valid = (op->src[0]->ne[3] == 1) && // as is 3d (one matrix per expert)
1044 (op->src[1]->ne[3] == 1) && // b is 3d
1045 (op->src[2]->ne[2] == 1 && op->src[2]->ne[3] == 1) && // ids is 2d
1046 (op->src[2]->ne[1] == op->src[1]->ne[2]) && // must have expert list per b row
1047 (op->src[0]->ne[0] == op->src[1]->ne[0]) && // K dimension must match
1048 (op->src[2]->ne[0] % op->src[1]->ne[1] == 0); // can broadcast
1049
1050 supported = src0_first_dim_contiguous && src1_first_dim_contiguous && src2_first_dim_contiguous &&
1051 dst_first_dim_contiguous && dst_properly_ordered && dims_valid;
1052 } else {
1053 supported = false;
1054 }
1055 break;
1056 case GGML_OP_ROPE:
1057 // Support F32 x I32 -> F32 RoPE for the modes implemented by rope_f32.
1058 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1059 op->src[1]->type == GGML_TYPE_I32 && ggml_is_contiguous(op) && et_ggml_is_row_contiguous(op->src[0])) {
1060 const int mode = ggml_get_op_params_i32(op, 2);
1061 const int ndims = ggml_get_op_params_i32(op, 1);
1062 const bool is_normal = mode == GGML_ROPE_TYPE_NORMAL;
1063 const bool is_neox = mode == GGML_ROPE_TYPE_NEOX;
1064 const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE;
1065 const bool zero_view_offset = op->src[0]->view_src == nullptr || op->src[0]->view_offs == 0;
1066 const bool has_sections = ggml_get_op_params_i32(op, 11) > 0 || ggml_get_op_params_i32(op, 12) > 0 ||
1067 ggml_get_op_params_i32(op, 13) > 0;
1068 // FIXME: support ggml_rope_set_offset
1069 const bool zero_rot_offset = ggml_get_op_params_i32(op, 15) == 0;
1070
1071 supported =
1072 zero_view_offset && zero_rot_offset && ndims <= 512 &&
1073 (is_normal || (is_neox && ndims % 16 == 0) || (is_imrope && ndims % 16 == 0 && has_sections));
1074 } else {
1075 supported = false;
1076 }
1077 break;
1078 case GGML_OP_RMS_NORM:
1079 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1080 op->ne[0] % 16 == 0 && ggml_is_contiguous(op) && et_ggml_is_row_contiguous(op->src[0]);
1081 break;
1082 case GGML_OP_NORM:
1083 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1084 op->ne[0] % 16 == 0 && ggml_is_contiguous(op) && et_ggml_is_row_contiguous(op->src[0]);
1085 break;
1086 case GGML_OP_L2_NORM:
1087 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1088 op->ne[0] % 16 == 0 && ggml_is_contiguous(op) && et_ggml_is_row_contiguous(op->src[0]);
1089 break;
1090 case GGML_OP_GROUP_NORM:
1091 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1092 ggml_is_contiguous(op) && et_ggml_is_row_contiguous(op->src[0]) &&
1093 ggml_get_op_params_i32(op, 0) > 0;
1094 break;
1095 case GGML_OP_IM2COL:
1096 supported = op->src[0] && op->src[1] &&
1097 ((op->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32) ||
1098 (op->type == GGML_TYPE_F16 &&
1099 (op->src[1]->type == GGML_TYPE_F16 || op->src[1]->type == GGML_TYPE_F32))) &&
1100 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[1]) &&
1101 op->nb[0] == ggml_type_size(op->type) && op->src[1]->nb[0] == ggml_type_size(op->src[1]->type);
1102 break;
1103 case GGML_OP_CONV_2D:
1104 {
1105 // First-cut conv_2d_f32_me kernel constraints. Anything outside
1106 // this falls back to CPU (it's a strict subset on purpose).
1107 if (!op->src[0] || !op->src[1]) {
1108 supported = false;
1109 break;
1110 }
1111 if (op->type != GGML_TYPE_F32 || op->src[0]->type != GGML_TYPE_F32 ||
1112 op->src[1]->type != GGML_TYPE_F32) {
1113 supported = false;
1114 break;
1115 }
1116 if (!ggml_is_contiguous(op) || !ggml_is_contiguous(op->src[0]) || !ggml_is_contiguous(op->src[1])) {
1117 supported = false;
1118 break;
1119 }
1120
1121 const ggml_tensor * flt = op->src[0]; // [Kw, Kh, Cin, Cout]
1122 const ggml_tensor * in = op->src[1]; // [W, H, Cin, N]
1123 const int32_t s0 = ggml_get_op_params_i32(op, 0);
1124 const int32_t s1 = ggml_get_op_params_i32(op, 1);
1125 const int32_t p0 = ggml_get_op_params_i32(op, 2);
1126 const int32_t p1 = ggml_get_op_params_i32(op, 3);
1127 const int32_t d0 = ggml_get_op_params_i32(op, 4);
1128 const int32_t d1 = ggml_get_op_params_i32(op, 5);
1129
1130 const int64_t Kw = flt->ne[0];
1131 const int64_t Kh = flt->ne[1];
1132 const int64_t Cin = flt->ne[2];
1133 const int64_t Cout = flt->ne[3];
1134 const int64_t H = in->ne[1];
1135 (void) in->ne[0];
1136
1137 if (s0 < 1 || s1 < 1 || !(d0 == 1 && d1 == 1) || Cin % 16 != 0 || Cout % 16 != 0 || in->ne[3] != 1) {
1138 supported = false;
1139 break;
1140 }
1141 const int64_t OW = op->ne[0];
1142 const int64_t OH = op->ne[1];
1143 if (OW <= 0 || OH <= 0) {
1144 supported = false;
1145 break;
1146 }
1147 (void) p0;
1148 (void) p1;
1149
1150 // Mirror the kernel's sizing:
1151 // if K_TILES * per_KT_bytes <= budget: 1 buffer, n_chunks=1
1152 // else: 2 buffers (double-buffer), shrink chunk_KT until
1153 // 2*chunk_KT*per_KT_bytes <= budget.
1154 const int64_t Hp = H + 2 * p1;
1155 const int64_t OW_pad = (OW + 15) & ~15;
1156 const int64_t Wp_a = OW_pad;
1157 const bool need_stage = (OW % 16 != 0);
1158 const int64_t stage_bytes = need_stage ? (Cout * OH * OW_pad * 4) : 0;
1159 const int64_t L2SCP_BUDGET = 1500 * 1024;
1160 // Per-hart partial-TenC scratch (mirrors kernel MAX_TILES_PER_HART=2):
1161 // 32 minions x 2 tiles x 1024 bytes = 64 KB per shire.
1162 const int64_t scratch_bytes = 32 * 2 * 16 * 16 * 4;
1163 const int64_t budget = L2SCP_BUDGET - stage_bytes - scratch_bytes;
1164 const int64_t per_KT_bytes = Kh * Kw * Cout * 16 * 4 + Kw * 16 * Hp * Wp_a * 4;
1165 const int64_t K_TILES = Cin / 16;
1166
1167 int64_t chunk_KT_calc;
1168 int64_t n_chunks_calc;
1169 if (K_TILES * per_KT_bytes <= budget) {
1170 chunk_KT_calc = K_TILES;
1171 n_chunks_calc = 1;
1172 } else {
1173 chunk_KT_calc = K_TILES;
1174 while (chunk_KT_calc > 1 && 2 * chunk_KT_calc * per_KT_bytes > budget) {
1175 chunk_KT_calc--;
1176 }
1177 while (chunk_KT_calc > 1 && K_TILES % chunk_KT_calc != 0) {
1178 chunk_KT_calc--;
1179 }
1180 if (chunk_KT_calc < 1) {
1181 supported = false;
1182 break;
1183 }
1184 n_chunks_calc = K_TILES / chunk_KT_calc;
1185 }
1186
1187 if (n_chunks_calc > 1) {
1188 const int64_t M_TILES = Cout / 16;
1189 const int64_t w_tiles = (OW + 15) / 16;
1190 const int64_t total_tiles = OH * w_tiles * M_TILES;
1191 // MAX_TILES_PER_HART = 2 (mirrors kernel constant).
1192 const int64_t max_workers = (need_stage ? 32 : 1024) * 2;
1193 if (total_tiles > max_workers) {
1194 supported = false;
1195 break;
1196 }
1197 }
1198
1199 supported = true;
1200 break;
1201 }
1202 case GGML_OP_SCALE:
1203 // F32 contiguous, total elements must be cache line aligned (16 floats)
1204 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1205 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]) && (ggml_nelements(op) % 16 == 0);
1206 break;
1207 case GGML_OP_GLU:
1208 // Note: we only require row-wise contiguity (ggml_is_contiguous_1) so that
1209 // strided views over a packed up_proj tensor (the common split-GLU layout)
1210 // are accepted. The kernel walks rows via nb[1] strides, so the inner
1211 // dimension just needs to be densely packed.
1212 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1213 ggml_nelements(op) % 16 == 0 && ggml_is_contiguous_1(op) && ggml_is_contiguous_1(op->src[0])) {
1214 // Check GLU variant - support SWIGLU, SWIGLU_OAI, GEGLU, GEGLU_ERF, GEGLU_QUICK, REGLU
1215 ggml_glu_op glu_type = ggml_get_glu_op(op);
1216 const bool supported_variant = glu_type == GGML_GLU_OP_SWIGLU || glu_type == GGML_GLU_OP_SWIGLU_OAI ||
1217 glu_type == GGML_GLU_OP_SWIGLU_CLAMP || glu_type == GGML_GLU_OP_GEGLU ||
1218 glu_type == GGML_GLU_OP_GEGLU_ERF ||
1219 glu_type == GGML_GLU_OP_GEGLU_QUICK || glu_type == GGML_GLU_OP_REGLU;
1220
1221 if (op->src[1]) {
1222 supported = supported_variant && op->src[1]->type == GGML_TYPE_F32 &&
1223 ggml_is_contiguous_1(op->src[1]) && op->src[0]->ne[0] == op->ne[0] &&
1224 op->src[1]->ne[0] == op->ne[0];
1225 } else {
1226 supported = supported_variant && op->src[0]->ne[0] == 2 * op->ne[0];
1227 }
1228 } else {
1229 supported = false;
1230 }
1231 break;
1232 case GGML_OP_SOFT_MAX:
1233 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1234 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]) && op->src[0]->ne[0] > 1) {
1235 // Check optional mask tensor (F32 only)
1236 if (op->src[1]) {
1237 supported = op->src[1]->type == GGML_TYPE_F32 && ggml_is_contiguous(op->src[1]);
1238 if (!supported) {
1239 break;
1240 }
1241 }
1242 // Check optional sinks tensor (F32 only)
1243 if (op->src[2]) {
1244 supported = op->src[2]->type == GGML_TYPE_F32 && ggml_is_contiguous(op->src[2]);
1245 } else {
1246 supported = true;
1247 }
1248 } else {
1249 supported = false;
1250 }
1251 break;
1252 case GGML_OP_SSM_SCAN:
1253 supported = op->type == GGML_TYPE_F32 && ggml_is_contiguous(op) && op->src[0] &&
1254 op->src[0]->type == GGML_TYPE_F32 && ggml_is_contiguous(op->src[0]) && op->src[1] &&
1255 op->src[1]->type == GGML_TYPE_F32 && op->src[2] && op->src[2]->type == GGML_TYPE_F32 &&
1256 ggml_is_contiguous(op->src[2]) && op->src[3] && op->src[3]->type == GGML_TYPE_F32 &&
1257 ggml_is_contiguous(op->src[3]) && op->src[4] && op->src[4]->type == GGML_TYPE_F32 &&
1258 op->src[5] && op->src[5]->type == GGML_TYPE_F32 && op->src[6] &&
1259 op->src[6]->type == GGML_TYPE_I32 && ggml_is_contiguous(op->src[6]) &&
1260 op->src[1]->nb[0] == sizeof(float) && op->src[4]->nb[0] == sizeof(float) &&
1261 op->src[5]->nb[0] == sizeof(float) &&
1262 op->src[1]->nb[1] == (size_t) op->src[1]->ne[0] * sizeof(float) &&
1263 op->src[4]->nb[1] == (size_t) op->src[4]->ne[0] * sizeof(float) &&
1264 op->src[5]->nb[1] == (size_t) op->src[5]->ne[0] * sizeof(float) &&
1265 op->src[0]->ne[0] == op->src[4]->ne[0] && op->src[0]->ne[1] == op->src[1]->ne[0] &&
1266 op->src[0]->ne[2] == op->src[1]->ne[1] && op->src[1]->ne[2] == op->src[2]->ne[1] &&
1267 op->src[1]->ne[3] == op->src[2]->ne[2] && op->src[4]->ne[2] == op->src[1]->ne[2] &&
1268 op->src[4]->ne[3] == op->src[1]->ne[3] && ggml_are_same_shape(op->src[4], op->src[5]) &&
1269 op->src[6]->ne[0] == op->src[1]->ne[3] && op->src[3]->ne[1] == op->src[1]->ne[1] &&
1270 (op->src[3]->ne[0] == 1 || op->src[3]->ne[0] == op->src[0]->ne[0]) &&
1271 (op->src[1]->ne[1] % op->src[4]->ne[1] == 0);
1272 break;
1273 case GGML_OP_FLASH_ATTN_EXT:
1274 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1275 (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) && op->src[2] &&
1276 (op->src[2]->type == GGML_TYPE_F32 || op->src[2]->type == GGML_TYPE_F16) && op->src[4] == nullptr && op->src[5] == nullptr &&
1277 ggml_is_contiguous_rows(op) && ggml_is_contiguous_rows(op->src[0])) {
1278 float max_bias = 0.0f;
1279 float logit_softcap = 0.0f;
1280 memcpy(&max_bias, (const float *) op->op_params + 1, sizeof(max_bias));
1281 memcpy(&logit_softcap, (const float *) op->op_params + 2, sizeof(logit_softcap));
1282
1283 const ggml_prec prec = ggml_flash_attn_ext_get_prec(op);
1284
1285 // Mask must be F16 or F32 if present
1286 bool mask_ok = (op->src[3] == nullptr) || (op->src[3]->type == GGML_TYPE_F32) ||
1287 (op->src[3]->type == GGML_TYPE_F16);
1288
1289 // GQA: n_head_q must be a multiple of n_head_kv
1290 const int64_t nhq = op->src[0]->ne[2];
1291 const int64_t nhk = op->src[1]->ne[2];
1292
1293 // K/V row stride must match element size
1294 const size_t k_elem = op->src[1]->type == GGML_TYPE_F16 ? 2 : 4;
1295 const size_t v_elem = op->src[2]->type == GGML_TYPE_F16 ? 2 : 4;
1296
1297 // Only support matrix engine path (F16 K/V, dk%32==0);
1298 // mask scalar F32 fallback to get baseline perf readings
1299 const bool me_eligible = op->src[1]->type == GGML_TYPE_F16 && op->src[2]->type == GGML_TYPE_F16 &&
1300 (op->src[0]->ne[0] % 32) == 0;
1301
1302 supported = me_eligible && mask_ok && (prec == GGML_PREC_F32 || prec == GGML_PREC_DEFAULT) &&
1303 max_bias == 0.0f && logit_softcap == 0.0f && op->src[0]->nb[0] == sizeof(float) &&
1304 op->src[1]->nb[0] == k_elem && op->src[2]->nb[0] == v_elem && op->nb[0] == sizeof(float) &&
1305 op->src[0]->ne[0] == op->src[1]->ne[0] && // dk matches
1306 op->src[2]->ne[0] == op->ne[0] && // dv matches
1307 op->src[2]->ne[0] <= 512 && // dv limit
1308 op->src[0]->ne[0] <= 512 && // dk limit
1309 nhq % nhk == 0 && // GQA ratio is integer
1310 op->src[0]->ne[1] == op->ne[2] && op->src[0]->ne[2] == op->ne[1] &&
1311 op->src[0]->ne[3] == op->ne[3] && op->src[1]->ne[1] == op->src[2]->ne[1] &&
1312 op->src[1]->ne[2] == op->src[2]->ne[2] && op->src[1]->ne[3] == op->src[2]->ne[3] &&
1313 op->src[0]->ne[3] == op->src[1]->ne[3];
1314 } else {
1315 supported = false;
1316 }
1317 break;
1318 case GGML_OP_GET_ROWS:
1319 // Support F32/F16/Q4_0/Q8_0/Q4_K data with I32 indices -> F32 output
1320 if (op->type == GGML_TYPE_F32 && op->src[0] &&
1321 (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 ||
1322 op->src[0]->type == GGML_TYPE_Q4_0 || op->src[0]->type == GGML_TYPE_Q8_0 ||
1323 op->src[0]->type == GGML_TYPE_Q4_K) &&
1324 op->src[1] && op->src[1]->type == GGML_TYPE_I32 && ggml_is_contiguous(op) &&
1325 ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1])) {
1326 // Validate dimension constraints from ggml implementation
1327 supported = (op->src[0]->ne[2] == op->src[1]->ne[1]) && (op->src[1]->ne[3] == 1);
1328 } else {
1329 supported = false;
1330 }
1331 break;
1332 case GGML_OP_CONT:
1333 // Support F32->F32 and F16->F16 CONT operations (rearrange non-contiguous to contiguous)
1334 if ((op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && op->src[0] &&
1335 op->src[0]->type == op->type && ggml_is_contiguous(op)) {
1336 // Defensive check: ensure dst and src0 are not aliased (separate buffers)
1337 // While GGML design currently guarantees this, check for future robustness
1338 if (op->data && op->src[0]->data && op->data == op->src[0]->data) {
1339 GGML_LOG_WARN("ET: CONT operation detected aliased tensors (dst == src0), unsupported");
1340 supported = false;
1341 } else {
1342 supported = true;
1343 }
1344 } else {
1345 supported = false;
1346 }
1347 break;
1348 case GGML_OP_CPY:
1349 // CPY copies src[0] data into dst layout (same as CONT for same-type)
1350 // Special path: zero-element tensors (scalars) are accepted as no-ops
1351 if (op->src[0]) {
1352 const int64_t nelements = op->ne[0] * op->ne[1] * op->ne[2] * op->ne[3];
1353 if (nelements == 0) {
1354 // Zero-element / scalar no-op case - always supported
1355 supported = true;
1356 } else if ((op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && op->src[0]->type == op->type &&
1357 ggml_is_contiguous(op)) {
1358 // Same-type with contiguous dst - reuse CONT kernel
1359 if (op->data && op->src[0]->data && op->data == op->src[0]->data) {
1360 GGML_LOG_WARN("ET: CPY operation detected aliased tensors, unsupported");
1361 supported = false;
1362 } else {
1363 supported = true;
1364 }
1365 } else if (op->type == GGML_TYPE_F16 && op->src[0]->type == GGML_TYPE_F32 && ggml_is_contiguous(op)) {
1366 // F32 -> F16 conversion copy
1367 supported = true;
1368 } else {
1369 supported = false;
1370 }
1371 } else {
1372 supported = false;
1373 }
1374 break;
1375 case GGML_OP_CONCAT:
1376 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1377 op->src[1]->type == GGML_TYPE_F32 && ggml_is_contiguous(op)) {
1378 const int32_t dim = ((const int32_t *) op->op_params)[0];
1379 if (dim == 0 && op->src[0]->ne[0] % 16 == 0 && op->src[1]->ne[0] % 16 == 0 &&
1380 ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1])) {
1381 // Fast dim==0 path: both source row segments are cacheline-aligned
1382 // and contiguous, so the kernel can use vector row copies.
1383 supported = true;
1384 } else if (dim == 0 && ((op->src[0]->nb[0] % sizeof(float) == 0) || op->src[0]->ne[0] == 1) &&
1385 ((op->src[1]->nb[0] % sizeof(float) == 0) || op->src[1]->ne[0] == 1)) {
1386 // Slow dim==0 path: scalar, stride-aware copies for non-contiguous
1387 // or non-aligned source row segments. Destination remains contiguous.
1388 supported = true;
1389 } else if (op->ne[0] % 16 == 0 && op->src[0]->ne[0] % 16 == 0 && op->src[1]->ne[0] % 16 == 0 &&
1390 ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1])) {
1391 // Dim >= 1 path: full aligned row copies from one source or the other.
1392 supported = true;
1393 }
1394 }
1395 break;
1396 case GGML_OP_SSM_CONV:
1397 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1398 op->src[1]->type == GGML_TYPE_F32 && op->src[0]->nb[0] == sizeof(float) &&
1399 op->src[1]->nb[0] == sizeof(float) && op->src[0]->nb[1] == op->src[0]->ne[0] * sizeof(float) &&
1400 op->src[1]->nb[1] == op->src[1]->ne[0] * sizeof(float) && ggml_is_contiguous(op) &&
1401 op->src[1]->ne[1] == op->src[0]->ne[1] && op->ne[0] == op->src[0]->ne[1] &&
1402 op->ne[1] == op->src[0]->ne[0] - op->src[1]->ne[0] + 1 && op->ne[2] == op->src[0]->ne[2];
1403 break;
1404 case GGML_OP_PAD:
1405 // F32 zero-pad only, no dim0 padding, dst contiguous
1406 // ne[0] must be CL-aligned (% 16 == 0) or evenly divide a CL (16 % ne[0] == 0)
1407 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1408 ggml_is_contiguous(op) && (op->ne[0] % 16 == 0 || 16 % op->ne[0] == 0) &&
1409 op->src[0]->nb[0] == sizeof(float)) {
1410 const int32_t lp0 = ((const int32_t *) op->op_params)[0];
1411 const int32_t rp0 = ((const int32_t *) op->op_params)[1];
1412 const bool circular = (bool) ((const int32_t *) op->op_params)[8];
1413 if (lp0 == 0 && rp0 == 0 && !circular) {
1414 supported = true;
1415 } else {
1416 supported = false;
1417 }
1418 } else {
1419 supported = false;
1420 }
1421 break;
1422 case GGML_OP_REPEAT:
1423 // Two acceptable shapes:
1424 // 1. No-op REPEAT (src and dst have identical shape): dispatched
1425 // to cont_f32, which handles arbitrary contiguous sizes.
1426 // 2. Real REPEAT via repeat_f32 kernel: dst ne[0] cacheline-aligned,
1427 // src0 ne[0] cacheline-aligned or 1, dst.ne[i] % src0.ne[i] == 0.
1428 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1429 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]) && ggml_are_same_shape(op->src[0], op)) {
1430 supported = true;
1431 } else if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1432 (op->src[0]->ne[0] == 1 || op->src[0]->ne[0] % 16 == 0) && op->ne[0] % 16 == 0 &&
1433 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]) && op->ne[0] % op->src[0]->ne[0] == 0 &&
1434 op->ne[1] % op->src[0]->ne[1] == 0 && op->ne[2] % op->src[0]->ne[2] == 0 &&
1435 op->ne[3] % op->src[0]->ne[3] == 0) {
1436 supported = true;
1437 } else {
1438 supported = false;
1439 }
1440 break;
1441 case GGML_OP_FILL:
1442 // F32 contiguous, ne[0] cacheline-aligned for SIMD fill
1443 supported = op->type == GGML_TYPE_F32 && ggml_is_contiguous(op) && op->ne[0] % 16 == 0;
1444 break;
1445 case GGML_OP_DIAG:
1446 // F32 contiguous dst, src0 is 1D vector [N,1,...], dst is [N,N,...]
1447 // ne[0] must be cacheline-aligned for SIMD zeroing
1448 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1449 op->ne[0] % 16 == 0 && op->ne[0] == op->ne[1] && op->src[0]->ne[0] == op->ne[0] &&
1450 op->src[0]->ne[1] == 1 && ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]);
1451 break;
1452 case GGML_OP_TRI:
1453 // F32 contiguous, same shape in/out
1454 // Kernel handles arbitrary ne[0] with aligned fast path + scalar fallback
1455 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 &&
1456 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]);
1457 break;
1458 case GGML_OP_SOLVE_TRI:
1459 // F32 contiguous, A square, shapes compatible
1460 // Only lower-triangular left-side non-unit variant
1461 // Require k % 16 == 0 for cache-line-safe column parallelism
1462 supported = op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1463 op->src[1]->type == GGML_TYPE_F32 && op->src[0]->ne[0] == op->src[0]->ne[1] &&
1464 op->src[0]->ne[1] == op->src[1]->ne[1] && op->src[1]->ne[0] % 16 == 0 &&
1465 ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]);
1466 break;
1467 case GGML_OP_SET:
1468 // Minimal useful support: inplace F32 SET of a contiguous src1 view into
1469 // a contiguous dst/base tensor using explicit destination view strides.
1470 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1471 op->src[1]->type == GGML_TYPE_F32 && ggml_is_contiguous(op) && ggml_is_contiguous(op->src[0]) &&
1472 ggml_is_contiguous(op->src[1]) && ggml_are_same_shape(op, op->src[0]) && op->src[1]->ne[0] % 16 == 0) {
1473 const bool inplace = (bool) ((const int32_t *) op->op_params)[4];
1474 const size_t nb1 = ((const int32_t *) op->op_params)[0];
1475 const size_t nb2 = ((const int32_t *) op->op_params)[1];
1476 const size_t nb3 = ((const int32_t *) op->op_params)[2];
1477 const size_t offset = ((const int32_t *) op->op_params)[3];
1478 const size_t nb0 = ggml_element_size(op);
1479 const size_t im0 = op->src[1]->ne[0] == 0 ? 0 : op->src[1]->ne[0] - 1;
1480 const size_t im1 = op->src[1]->ne[1] == 0 ? 0 : op->src[1]->ne[1] - 1;
1481 const size_t im2 = op->src[1]->ne[2] == 0 ? 0 : op->src[1]->ne[2] - 1;
1482 const size_t im3 = op->src[1]->ne[3] == 0 ? 0 : op->src[1]->ne[3] - 1;
1483
1484 const bool view_bounds_ok = offset + im0 * nb0 + im1 * nb1 + im2 * nb2 + im3 * nb3 <= ggml_nbytes(op);
1485
1486 const bool cacheline_aligned =
1487 (nb1 % 64 == 0) && (nb2 % 64 == 0) && (nb3 % 64 == 0) && (offset % 64 == 0);
1488
1489 supported = inplace && view_bounds_ok && cacheline_aligned;
1490 }
1491 break;
1492 case GGML_OP_RWKV_WKV6:
1493 // F32 contiguous, head_size must be multiple of 8 for vectorization
1494 // 6 sources: k, v, r, tf, td, state
1495 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1496 op->src[1]->type == GGML_TYPE_F32 && op->src[2] && op->src[2]->type == GGML_TYPE_F32 && op->src[3] &&
1497 op->src[3]->type == GGML_TYPE_F32 && op->src[4] && op->src[4]->type == GGML_TYPE_F32 && op->src[5] &&
1498 op->src[5]->type == GGML_TYPE_F32 && op->src[0]->ne[0] % 8 == 0 && // head_size multiple of 8
1499 ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]) && ggml_is_contiguous(op->src[2]) &&
1500 ggml_is_contiguous(op->src[3]) && ggml_is_contiguous(op->src[4]) && ggml_is_contiguous(op->src[5])) {
1501 supported = true;
1502 } else {
1503 supported = false;
1504 }
1505 break;
1506 case GGML_OP_RWKV_WKV7:
1507 // F32 contiguous, head_size must be multiple of 8 for vectorization
1508 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
1509 op->src[1]->type == GGML_TYPE_F32 && op->src[2] && op->src[2]->type == GGML_TYPE_F32 && op->src[3] &&
1510 op->src[3]->type == GGML_TYPE_F32 && op->src[4] && op->src[4]->type == GGML_TYPE_F32 && op->src[5] &&
1511 op->src[5]->type == GGML_TYPE_F32 && op->src[6] && op->src[6]->type == GGML_TYPE_F32 &&
1512 op->src[2]->ne[0] % 8 == 0 && // head_size multiple of 8
1513 ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]) && ggml_is_contiguous(op->src[2]) &&
1514 ggml_is_contiguous(op->src[3]) && ggml_is_contiguous(op->src[4]) && ggml_is_contiguous(op->src[5]) &&
1515 ggml_is_contiguous(op->src[6])) {
1516 supported = true;
1517 } else {
1518 supported = false;
1519 }
1520 break;
1521 case GGML_OP_GATED_DELTA_NET:
1522 // F32, S_v must be multiple of 8 for vectorization
1523 // q, k, v may be row-contiguous with strided higher dimensions.
1524 // g, beta, state stay contiguous.
1525 if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && // q
1526 op->src[1] && op->src[1]->type == GGML_TYPE_F32 && // k
1527 op->src[2] && op->src[2]->type == GGML_TYPE_F32 && // v
1528 op->src[3] && op->src[3]->type == GGML_TYPE_F32 && // g
1529 op->src[4] && op->src[4]->type == GGML_TYPE_F32 && // beta
1530 op->src[5] && op->src[5]->type == GGML_TYPE_F32 && // state
1531 op->src[2]->ne[0] % 8 == 0 && // S_v multiple of 8
1532 (op->src[3]->ne[0] == 1 || op->src[3]->ne[0] == op->src[2]->ne[0]) && // g is scalar or per-element
1533 op->src[4]->ne[0] == 1 && // beta is scalar per position
1534 et_ggml_is_row_contiguous(op->src[0]) && et_ggml_is_row_contiguous(op->src[1]) &&
1535 et_ggml_is_row_contiguous(op->src[2]) && ggml_is_contiguous(op->src[3]) &&
1536 ggml_is_contiguous(op->src[4]) && ggml_is_contiguous(op->src[5])) {
1537 supported = true;
1538 } else {
1539 supported = false;
1540 }
1541 break;
1542 case GGML_OP_VIEW:
1543 case GGML_OP_PERMUTE:
1544 case GGML_OP_TRANSPOSE:
1545 case GGML_OP_RESHAPE:
1546 // Metadata-only no-ops, accept any type
1547 supported = true;
1548 break;
1549 case GGML_OP_SET_ROWS:
1550 // Support F32 data with I64 indices -> F16/F32 output (scatter operation)
1551 if (op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] && op->src[1]->type == GGML_TYPE_I64 &&
1552 (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && ggml_is_contiguous_rows(op) &&
1553 ggml_is_contiguous_rows(op->src[0]) && ggml_is_contiguous(op->src[1])) {
1554 // Validate dimension constraints from ggml implementation
1555 supported = (op->ne[0] == op->src[0]->ne[0]) && // same number of columns
1556 (op->ne[2] == op->src[0]->ne[2]) && // same batch size
1557 (op->ne[3] == op->src[0]->ne[3]) && // same outer dimension
1558 (op->src[0]->ne[1] == op->src[1]->ne[0]) && // src rows = index count
1559 (op->src[0]->ne[2] % op->src[1]->ne[1] == 0) && // batch constraint
1560 (op->src[0]->ne[3] % op->src[1]->ne[2] == 0) && // outer constraint
1561 (op->src[1]->ne[3] == 1); // indices tensor constraint
1562 } else {
1563 supported = false;
1564 }
1565 break;
1566 case GGML_OP_NONE:
1567 // Always support NONE operations - they represent leaf nodes (parameters, inputs, constants)
1568 // No computation needed, just memory management
1569 supported = true;
1570 break;
1571 default:
1572 supported = false;
1573 break;
1574 }
1575 // if(!supported) {
1576 // ggml_et_dump_operator_metadata(op);
1577 // }
1578 return supported;
1579}
1580
1581static bool ggml_backend_et_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
1582 GGML_UNUSED(dev);
1583 return buft->iface.get_name == ggml_backend_et_buffer_type_get_name;
1584}
1585
1586static bool ggml_backend_et_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
1587 // GET_ROWS (embedding lookup) uses a large weight (tok_embd) that lives on CPU (dev_input).
1588 // The scheduler has no mechanism to cache cross-backend weight copies - it re-copies split
1589 // inputs every graph_compute call. For GET_ROWS this means copying the entire embedding table
1590 // (e.g. 266MB for Llama 3.1 1B) from host to device on every token, just to look up a few rows.
1591 // Keep GET_ROWS on CPU and let the scheduler copy only the small result to the device.
1592 // The other backends either only offload if the tensor lives on device or is large enough to
1593 // justify the copy cost.
1594 if (op->op == GGML_OP_GET_ROWS) {
1595 return false;
1596 }
1597 return true;
1598
1599 GGML_UNUSED(dev);
1600}
1601
1602static const struct ggml_backend_i ggml_backend_et_i = {
1603 /* .get_name = */ ggml_backend_et_get_name,
1604 /* .free = */ ggml_backend_et_free,
1605 /* .set_tensor_async = */ ggml_backend_et_set_tensor_async,
1606 /* .get_tensor_async = */ ggml_backend_et_get_tensor_async,
1607 /* .set_tensor_2d_async = */ NULL,
1608 /* .get_tensor_2d_async = */ NULL,
1609 /* .cpy_tensor_async = */ NULL,
1610 /* .synchronize = */ ggml_backend_et_synchronize,
1611 /* .graph_plan_create = */ NULL,
1612 /* .graph_plan_free = */ NULL,
1613 /* .graph_plan_update = */ NULL,
1614 /* .graph_plan_compute = */ NULL,
1615 /* .graph_compute = */ ggml_backend_et_graph_compute,
1616 /* .event_record = */ NULL,
1617 /* .event_wait = */ NULL,
1618 /* .graph_optimize = */ NULL,
1619};
1620
1621static const char * ggml_backend_et_device_get_name(ggml_backend_dev_t dev) {
1622 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
1623 return dev_ctx->name.c_str();
1624}
1625
1626static const char * ggml_backend_et_device_get_description(ggml_backend_dev_t dev) {
1627 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
1628 return dev_ctx->desc.c_str();
1629}
1630
1631static void ggml_backend_et_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
1632 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
1633 // Currently getFreeMemory is not available on a runtime without server.
1634 // For now, report total memory as free.
1635 *free = dev_ctx->total_mem;
1636 *total = dev_ctx->total_mem;
1637}
1638
1639static enum ggml_backend_dev_type ggml_backend_et_device_get_type(ggml_backend_dev_t dev) {
1640 GGML_UNUSED(dev);
1641 return GGML_BACKEND_DEVICE_TYPE_GPU;
1642}
1643
1644static void ggml_backend_et_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
1645 GGML_UNUSED(dev);
1646 props->name = ggml_backend_et_device_get_name(dev);
1647 props->description = ggml_backend_et_device_get_description(dev);
1648 props->type = ggml_backend_et_device_get_type(dev);
1649 ggml_backend_et_device_get_memory(dev, &props->memory_free, &props->memory_total);
1650 props->device_id = NULL; // No PCI device ID available
1651 props->caps = {
1652 /* .async = */ true,
1653 /* .host_buffer = */ false,
1654 /* .buffer_from_host_ptr = */ false,
1655 /* .events = */ false,
1656 /* .mmap_support = */ true,
1657 };
1658}
1659
1660static ggml_backend_t ggml_backend_et_device_init_backend(ggml_backend_dev_t dev, const char * params) {
1661 GGML_UNUSED(params);
1662 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
1663 return ggml_backend_et_init(dev_ctx->devidx);
1664}
1665
1666static ggml_backend_buffer_type_t ggml_backend_et_device_get_buffer_type(ggml_backend_dev_t dev) {
1667 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
1668 return dev_ctx->buftype;
1669}
1670
1671static ggml_backend_buffer_type_t ggml_backend_et_device_get_host_buffer_type(ggml_backend_dev_t dev) {
1672 GGML_UNUSED(dev);
1673 return ggml_backend_cpu_buffer_type();
1674}
1675
1676static const struct ggml_backend_device_i ggml_backend_et_device_i = {
1677 /* .get_name = */ ggml_backend_et_device_get_name,
1678 /* .get_description = */ ggml_backend_et_device_get_description,
1679 /* .get_memory = */ ggml_backend_et_device_get_memory,
1680 /* .get_type = */ ggml_backend_et_device_get_type,
1681 /* .get_props = */ ggml_backend_et_device_get_props,
1682 /* .init_backend = */ ggml_backend_et_device_init_backend,
1683 /* .get_buffer_type = */ ggml_backend_et_device_get_buffer_type,
1684 /* .get_host_buffer_type = */ ggml_backend_et_device_get_host_buffer_type,
1685 /* .buffer_from_host_ptr = */ NULL,
1686 /* .supports_op = */ ggml_backend_et_device_supports_op,
1687 /* .supports_buft = */ ggml_backend_et_device_supports_buft,
1688 /* .offload_op = */ ggml_backend_et_device_offload_op,
1689 /* .event_new = */ NULL,
1690 /* .event_free = */ NULL,
1691 /* .event_synchronize = */ NULL,
1692};
1693
1694/*
1695 Backend Registry.
1696*/
1697
1698static const char * ggml_backend_et_reg_get_name(ggml_backend_reg_t reg) {
1699 GGML_UNUSED(reg);
1700 return GGML_ET_NAME;
1701}
1702
1703static size_t ggml_backend_et_reg_get_device_count(ggml_backend_reg_t reg) {
1704 ggml_backend_et_reg_ctx * ctx = (ggml_backend_et_reg_ctx *) reg->context;
1705 return ctx->devices.size();
1706}
1707
1708static ggml_backend_dev_t ggml_backend_et_reg_get_device(ggml_backend_reg_t reg, size_t devidx) {
1709 ggml_backend_et_reg_ctx * ctx = (ggml_backend_et_reg_ctx *) reg->context;
1710 if (devidx >= ctx->devices.size()) {
1711 return nullptr;
1712 }
1713 return ctx->devices[devidx];
1714}
1715
1716static void * ggml_backend_et_get_proc_address(ggml_backend_reg_t reg, const char * name) {
1717 GGML_UNUSED(reg);
1718 GGML_UNUSED(name);
1719 return nullptr;
1720}
1721
1722static const struct ggml_backend_reg_i ggml_backend_et_reg_i = {
1723 /* .get_name = */ ggml_backend_et_reg_get_name,
1724 /* .get_device_count = */ ggml_backend_et_reg_get_device_count,
1725 /* .get_device = */ ggml_backend_et_reg_get_device,
1726 /* .get_proc_address = */ ggml_backend_et_get_proc_address,
1727};
1728
1729ggml_backend_reg_t ggml_backend_et_reg(void) {
1730 static ggml_backend_reg_t _reg = []() -> ggml_backend_reg_t {
1731 ggml_backend_et_reg_ctx * ctx = new ggml_backend_et_reg_ctx;
1732
1733 if (!ggml_et_driver_init()) {
1734 return nullptr;
1735 }
1736
1737 ggml_backend_reg_t r = new ggml_backend_reg{
1738 /* .api_version = */ GGML_BACKEND_API_VERSION,
1739 /* .iface = */ ggml_backend_et_reg_i,
1740 /* .context = */ nullptr, // Set later
1741 };
1742
1743 std::vector<rt::DeviceId> rtids = ggml_et_runtime()->getDevices();
1744
1745 for (int i = 0; i < ggml_et_devicelayer()->getDevicesCount(); i++) {
1746 ggml_backend_dev_t dev = new ggml_backend_device{
1747 /* .iface = */ ggml_backend_et_device_i,
1748 /* .reg = */ r,
1749 /* .context = */ nullptr // Set later
1750 };
1751
1752 rt::DeviceId rtid = rtids[i];
1753 rt::DeviceProperties prop = ggml_et_runtime()->getDeviceProperties(rtid);
1754
1755 // Create device context.
1756 ggml_backend_et_device_context * dev_ctx = new ggml_backend_et_device_context;
1757 dev_ctx->devidx = i;
1758 dev_ctx->rtid = rtid;
1759 dev_ctx->name = GGML_ET_NAME + std::to_string(i);
1760 dev_ctx->desc = "ET device " + std::to_string(i);
1761 dev_ctx->total_mem = static_cast<size_t>(prop.memorySize_);
1762 {
1763 const char * env = getenv("GGML_ET_UBERKERNEL");
1764 dev_ctx->uberkernel_enabled = env && env[0] != '\0' && strcmp(env, "0") != 0;
1765 }
1766 // Add buffer type for device to device context.
1767 ggml_backend_et_buffer_type_context * bufty_ctx = new ggml_backend_et_buffer_type_context;
1768 bufty_ctx->devidx = i;
1769 bufty_ctx->name = GGML_ET_NAME + std::to_string(i);
1770 dev_ctx->buftype = new ggml_backend_buffer_type{ /* .iface = */ ggml_backend_et_buffer_type_i,
1771 /* .device = */ dev,
1772 /* .context = */ bufty_ctx };
1773
1774 // Create default stream for ordered execution on this device
1775 dev_ctx->default_stream = ggml_et_runtime()->createStream(rtid);
1776
1777 dev_ctx->trace_buffer = ggml_et_runtime()->mallocDevice(rtid, ET_TRACE_BUFFER_SIZE);
1778 // Pre-size each slot's host buffers and device-side scratch so the
1779 // first few graph_compute calls don't pay a malloc/grow penalty.
1780 for (auto & slot : dev_ctx->uberkernel.slots) {
1781 slot.insts.reserve(256);
1782 slot.params_blob.reserve(1 << 20);
1783 slot.device_insts_capacity = 256 * sizeof(ggml_et_uberkernel_inst);
1784 slot.device_params_capacity = 1 << 20;
1785 slot.device_insts = ggml_et_runtime()->mallocDevice(rtid, slot.device_insts_capacity);
1786 slot.device_params = ggml_et_runtime()->mallocDevice(rtid, slot.device_params_capacity);
1787 if (slot.device_insts == nullptr) {
1788 slot.device_insts_capacity = 0;
1789 }
1790 if (slot.device_params == nullptr) {
1791 slot.device_params_capacity = 0;
1792 }
1793 }
1794
1795 dev->context = dev_ctx;
1796
1797 ctx->devices.push_back(dev);
1798 }
1799
1800 r->context = ctx;
1801 return r;
1802 }();
1803
1804 return _reg;
1805}
1806
1807ggml_guid_t ggml_backend_et_guid(void) {
1808 static ggml_guid guid = { 0x4b, 0xe0, 0x72, 0x88, 0xc0, 0xf6, 0x29, 0xb4,
1809 0x79, 0x9f, 0x70, 0x68, 0x71, 0x0f, 0x6d, 0xc8 };
1810 return &guid;
1811}
1812
1813ggml_backend_t ggml_backend_et_init(size_t devidx) {
1814 if (!ggml_et_driver_init()) {
1815 return nullptr;
1816 }
1817
1818 if (devidx >= (size_t) ggml_backend_et_get_device_count()) {
1819 return nullptr;
1820 }
1821
1822 ggml_backend_et_context * ctx = new ggml_backend_et_context;
1823 ctx->devidx = (int) devidx;
1824
1825 ggml_backend_t backend = new ggml_backend{
1826 /* .guid = */ ggml_backend_et_guid(),
1827 /* .iface = */ ggml_backend_et_i,
1828 /* .device = */ ggml_backend_et_reg_get_device(ggml_backend_et_reg(), devidx),
1829 /* .context = */ ctx,
1830 };
1831
1832 return backend;
1833}
1834
1835bool ggml_backend_is_et(ggml_backend_t backend) {
1836 return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_et_guid());
1837}
1838
1839int ggml_backend_et_get_device_count(void) {
1840 return ggml_backend_et_reg_get_device_count(ggml_backend_et_reg());
1841}
1842
1843void ggml_backend_et_get_device_description(int devidx, char * description, size_t description_size) {
1844 if (devidx < 0 || devidx >= ggml_backend_et_get_device_count()) {
1845 snprintf(description, description_size, "ET Device %d (invalid)", devidx);
1846 return;
1847 }
1848
1849 ggml_backend_dev_t dev = ggml_backend_et_reg_get_device(ggml_backend_et_reg(), devidx);
1850 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
1851 snprintf(description, description_size, "%s", dev_ctx->desc.c_str());
1852}
1853
1854void ggml_backend_et_get_device_memory(int devidx, size_t * free, size_t * total) {
1855 if (devidx < 0 || devidx >= ggml_backend_et_get_device_count()) {
1856 *free = 0;
1857 *total = 0;
1858 return;
1859 }
1860
1861 ggml_backend_dev_t dev = ggml_backend_et_reg_get_device(ggml_backend_et_reg(), devidx);
1862 ggml_backend_et_device_get_memory(dev, free, total);
1863}
1864
1865ggml_backend_buffer_type_t ggml_backend_et_buffer_type(size_t dev_num) {
1866 if (dev_num >= (size_t) ggml_backend_et_get_device_count()) {
1867 return nullptr;
1868 }
1869
1870 ggml_backend_dev_t dev = ggml_backend_et_reg_get_device(ggml_backend_et_reg(), dev_num);
1871 ggml_backend_et_device_context * dev_ctx = (ggml_backend_et_device_context *) dev->context;
1872 return dev_ctx->buftype;
1873}
1874
1875ggml_backend_buffer_type_t ggml_backend_et_host_buffer_type(void) {
1876 static ggml_backend_buffer_type host_buffer_type = {
1877 /* .iface = */ ggml_backend_et_buffer_type_i,
1878 /* .device = */ nullptr,
1879 /* .context = */ nullptr,
1880 };
1881 return &host_buffer_type;
1882}
1883
1884GGML_BACKEND_DL_IMPL(ggml_backend_et_reg)