2023-11-13 12:16:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// ggml-backend internal header
|
|
|
|
|
|
|
|
#include "ggml-backend.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2024-11-25 14:13:39 +00:00
|
|
|
#define GGML_BACKEND_API_VERSION 1
|
|
|
|
|
2023-11-13 12:16:23 +00:00
|
|
|
//
|
2024-10-02 23:49:47 +00:00
|
|
|
// Backend buffer type
|
2023-11-13 12:16:23 +00:00
|
|
|
//
|
|
|
|
|
2023-12-07 20:26:54 +00:00
|
|
|
struct ggml_backend_buffer_type_i {
|
2024-10-02 23:49:47 +00:00
|
|
|
const char * (*get_name) (ggml_backend_buffer_type_t buft);
|
2024-06-13 01:11:35 +00:00
|
|
|
// allocate a buffer of this type
|
2024-10-02 23:49:47 +00:00
|
|
|
ggml_backend_buffer_t (*alloc_buffer) (ggml_backend_buffer_type_t buft, size_t size);
|
2024-06-13 01:11:35 +00:00
|
|
|
// tensor alignment
|
2024-10-02 23:49:47 +00:00
|
|
|
size_t (*get_alignment) (ggml_backend_buffer_type_t buft);
|
|
|
|
// (optional) max buffer size that can be allocated (defaults to SIZE_MAX)
|
|
|
|
size_t (*get_max_size) (ggml_backend_buffer_type_t buft);
|
|
|
|
// (optional) data size needed to allocate the tensor, including padding (defaults to ggml_nbytes)
|
|
|
|
size_t (*get_alloc_size)(ggml_backend_buffer_type_t buft, const struct ggml_tensor * tensor);
|
2024-10-30 01:01:23 +00:00
|
|
|
// (optional) check if tensor data is in host memory and uses standard ggml tensor layout (defaults to false)
|
2024-10-02 23:49:47 +00:00
|
|
|
bool (*is_host) (ggml_backend_buffer_type_t buft);
|
2023-12-07 20:26:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ggml_backend_buffer_type {
|
|
|
|
struct ggml_backend_buffer_type_i iface;
|
2024-10-02 23:49:47 +00:00
|
|
|
ggml_backend_dev_t device;
|
|
|
|
void * context;
|
2023-12-07 20:26:54 +00:00
|
|
|
};
|
|
|
|
|
2024-10-02 23:49:47 +00:00
|
|
|
//
|
|
|
|
// Backend buffer
|
|
|
|
//
|
2023-11-13 12:16:23 +00:00
|
|
|
|
|
|
|
struct ggml_backend_buffer_i {
|
2024-10-02 23:49:47 +00:00
|
|
|
// (optional) free the buffer
|
|
|
|
void (*free_buffer) (ggml_backend_buffer_t buffer);
|
|
|
|
// base address of the buffer
|
|
|
|
void * (*get_base) (ggml_backend_buffer_t buffer);
|
|
|
|
// (optional) initialize a tensor in the buffer (eg. add tensor extras)
|
|
|
|
void (*init_tensor) (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor);
|
|
|
|
// tensor data access
|
|
|
|
void (*memset_tensor)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size);
|
|
|
|
void (*set_tensor) (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
|
|
|
|
void (*get_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
|
|
|
|
// (optional) tensor copy: dst is in the buffer, src may be in any buffer, including buffers from a different backend (return false if not supported)
|
|
|
|
bool (*cpy_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst);
|
|
|
|
// clear the entire buffer
|
|
|
|
void (*clear) (ggml_backend_buffer_t buffer, uint8_t value);
|
|
|
|
// (optional) reset any internal state due to tensor initialization, such as tensor extras
|
|
|
|
void (*reset) (ggml_backend_buffer_t buffer);
|
2023-11-13 12:16:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ggml_backend_buffer {
|
2023-12-07 20:26:54 +00:00
|
|
|
struct ggml_backend_buffer_i iface;
|
|
|
|
ggml_backend_buffer_type_t buft;
|
2024-10-02 23:49:47 +00:00
|
|
|
void * context;
|
2023-11-13 12:16:23 +00:00
|
|
|
size_t size;
|
2024-01-12 19:07:38 +00:00
|
|
|
enum ggml_backend_buffer_usage usage;
|
2023-11-13 12:16:23 +00:00
|
|
|
};
|
|
|
|
|
2024-11-25 14:13:39 +00:00
|
|
|
GGML_API ggml_backend_buffer_t ggml_backend_buffer_init(
|
2024-10-02 23:49:47 +00:00
|
|
|
ggml_backend_buffer_type_t buft,
|
|
|
|
struct ggml_backend_buffer_i iface,
|
|
|
|
void * context,
|
|
|
|
size_t size);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-01-12 19:07:38 +00:00
|
|
|
// do not use directly, use ggml_backend_tensor_copy instead
|
2024-11-25 14:13:39 +00:00
|
|
|
GGML_API bool ggml_backend_buffer_copy_tensor(const struct ggml_tensor * src, struct ggml_tensor * dst);
|
2023-12-07 20:26:54 +00:00
|
|
|
|
2024-10-02 23:49:47 +00:00
|
|
|
// multi-buffer
|
ggml : add Vulkan backend (#2059)
* Vulkan loader code
* Fix matmul kernel, continue implementation
* Continue implementation
* Vulkan memory management
* Vulkan development
* Matmul call
* Add aligned malloc and free for VMA
* Continue implementation
* First matmul success
* GEMM Kernel optimization
* 1D Blocktiling
* 2D Blocktiling
* Write coalescing
* Continue vulkan implementation and optimization
* First FP16 attempt, disabled for now
* Code abstraction, FP16 implementation, fix kernel, add FP16 to FP32 kernel
* Enable device extensions properly, restore fp16 matmul op
* Fix mulmat_f16
* Output FP32 in fp16 matmul shader
* Fix f16_to_f32 kernel
* dequant_q4_0 kernel
* Add VMA library
* Avoid requesting dedicated memory, VMA can decide that by itself
* Add bounds checking to matmul kernels, improve implementation, fix command buffers not freed properly
* add cmake commands
* Add 2d write operation, profiling code
* Fix 2d write
* Fix queue selection for AMD RADV
* Fix trailing whitespace in vk_mem_alloc.h
* Add WIP warp tile mat mul shaders
* Disable glslc optimization
* Disable glslc optimization for CMake
* Optimize warptile matmul shader, replace blocktile with it
* Add split-k optimization for small matrix multiplication
Use semaphores for synchronization instead of fences or waitidle
Rework async write/read for synchronization
* Fix validation errors, improve compatibility with AMD GPUs
* Rework command buffer handling
* Variable matmul kernel using specialization constants
* Fix synchronization on AMD, add barriers for buffer ownership transfer, add debug flag and prints
* Reuse semaphores
* Handle stage flags during command buffer submission properly
* Increase matmul test runs for consistent results
* Fix F32 matmul
* Add vectorized loading and zeropadding for matrix multiplication
* Use pinned memory for f16 preprocessing
* Don't force aligned matmul
* Don't free before queue done
* Replace VMA library with native Vulkan buffer management
* Basic offloading support with mul_f32 and dmmv for q4_0
* Run glslc commands in parallel
* Unroll loops in dmmv shader
* Reduce usage of waitIdle
* Reuse pinned allocation for f16 conversion
* Handle devices with only a single queue
* Fix trailing whitespace in CMakeLists.txt
* Allow parallel execution of kernels, parallelize third and fourth dimension calls
* Add fallback for devices only supporting one DescriptorSet per DescriptorPool
* Move to graph function similar to CUDA implementation
* Use F16 kernel for most things, replace q_f32 with mul_mat_q_f16 function
* Add F32 dmmv shaders
* Batch submissions
* Add .spv to gitignore
* Split off matrix vector multiplication for separate optimization
* Use single command buffer for matrix vector multiplication ops
* Reduce overhead of mul_f32 calls by using a single command buffer
* Add submission batching to mul_f32
* Fix tests
* Add missing barrier
* Add further missing barrier
* Add further ops
* Replace vk::QueueFamilyIgnored with VK_QUEUE_FAMILY_IGNORED to support more Vulkan header versions
* Remove unnecessary cblas link
* Fix descriptor set pre-allocation assert
* Add runtime shader compilation, start transferring shaders to this approach
* Transfer remaining shaders to header and compile on runtime
* Fix fp32 fallback if device doesn't support fp16, add force disable env var GGML_VULKAN_DISABLE_F16
* Add support for q4_1, q5_0, q5_1 and q8_0
* Remove unnecessary scalar layout extension
* Parse graph early to pre-record command buffers
* Add q6_k support
* Add multi-submit for command buffers
* Fix q6_k dequant shader for AMD
* Fix q6_k for GPUs without fp16 support
* Simplify q6_k fp16 fix
* Minor fixes
* Fix wg_denom of m-mulmat shaders
* Add Python-based Vulkan shader generator
* Replace shaderc dependency with precompiled shaders
Fix python script to generate shaders
* Clean up code
* Fix shader generator script Windows compatibility
Co-authored-by: Concedo <39025047+LostRuins@users.noreply.github.com>
* Close file before deletion
* Fix vulkan shader fp32 name
* Add q2_k and q3_k support
Add validation check to compare shader results to cpu results
* Add q4_k support
* Add q5_k support
* Bake SPIR-V bytecode into the library instead of loading shaders from file
* Switch to signal semaphores for flexibility
Prepare broadcasting support for mul mat
* Finish broadcasting mul mat support for GQA
* Clean up unused functions
Add repeat op
* Add further ops, not yet enabled. Improve semaphore code
* Reduce number of used semaphores by utilizing timelines more properly
* Remove queue information
* Reuse timeline semaphores, allow parallel operation with binary semaphores to work around nvidia driver limitations
* Add Vulkan to llama-bench
* Remove cblas dependency
* Fix matmul k-split bug
* Fix q4_k dmmv K_QUANTS_PER_ITERATION 1 shader
* Add RMS Norm shader, rework op_f32 shader setup, fix matmul bug
* Fix issues with float16 overflows in shaders
* Fix issues with older Vulkan headers on Ubuntu 22.04
* Allow multi-op partial offloading by parsing the graph to preallocate enough between-op buffers
* Implement further ops, rework op_f32 calls, fix bugs
* Finish full offloading support, add last remaining ops, fix bugs, remove redundant code
* Upload generated file ggml-vulkan-shaders.hpp, remove redundant shaders
* Merge upstream changes, fix conflicts, adapt soft_max op
* Fix Python and shader header format
* Free model gpu buffers on exit
* Use single queue per device to simplify code
* Add matmul shader support for running multiple calculations in parallel
* Switch from semaphore-synchronized multiple command buffers per op to single command buffer for multiple ops, whole graph if possible
* Fix missing event cast
* Replace uint64_t(-1) with UINT64_MAX, rename function for clarity
* Fix warning about empty C function parameters
* Fix compiler warnings
* Properly implement Vulkan backend buffer handling
* Fix oversized host staging buffers
* Simplify barrier synchronization calls
* Fix gcc warnings
* Implement max_size for backend buffer types to limit the size of a single allocation
* Use min of maxMemoryAllocationSize and maxBufferSize for device max allocation size
* refactor multi buf
* Disable unsupported ops to fix tests
* Check for maintenance4 support before using it
* Handle devices with only a single queue
* Fix single queue logic
* propagate buffer usage in multi buffers
* Implement rope_neox op
* Cleanup header and other files
* Simplify gpu_extras by removing events and putting staging memcpys into contexts
* Move queue into context
Add not-yet-enabled async backend ops
* Simplify context use, optimize matmul shader for warp size 64 (AMD GCN), fix split_k matmul shader optimization
* Add get_max_size to SYCL backend.
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
* llama : fix trailing whitespace
---------
Co-authored-by: Henri Vasserman <henv@hot.ee>
Co-authored-by: Concedo <39025047+LostRuins@users.noreply.github.com>
Co-authored-by: slaren <slarengh@gmail.com>
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
2024-01-28 17:03:59 +00:00
|
|
|
// buffer that contains a collection of buffers
|
2024-11-25 14:13:39 +00:00
|
|
|
GGML_API ggml_backend_buffer_t ggml_backend_multi_buffer_alloc_buffer(ggml_backend_buffer_t * buffers, size_t n_buffers);
|
|
|
|
GGML_API bool ggml_backend_buffer_is_multi_buffer(ggml_backend_buffer_t buffer);
|
|
|
|
GGML_API void ggml_backend_multi_buffer_set_usage(ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage);
|
ggml : add Vulkan backend (#2059)
* Vulkan loader code
* Fix matmul kernel, continue implementation
* Continue implementation
* Vulkan memory management
* Vulkan development
* Matmul call
* Add aligned malloc and free for VMA
* Continue implementation
* First matmul success
* GEMM Kernel optimization
* 1D Blocktiling
* 2D Blocktiling
* Write coalescing
* Continue vulkan implementation and optimization
* First FP16 attempt, disabled for now
* Code abstraction, FP16 implementation, fix kernel, add FP16 to FP32 kernel
* Enable device extensions properly, restore fp16 matmul op
* Fix mulmat_f16
* Output FP32 in fp16 matmul shader
* Fix f16_to_f32 kernel
* dequant_q4_0 kernel
* Add VMA library
* Avoid requesting dedicated memory, VMA can decide that by itself
* Add bounds checking to matmul kernels, improve implementation, fix command buffers not freed properly
* add cmake commands
* Add 2d write operation, profiling code
* Fix 2d write
* Fix queue selection for AMD RADV
* Fix trailing whitespace in vk_mem_alloc.h
* Add WIP warp tile mat mul shaders
* Disable glslc optimization
* Disable glslc optimization for CMake
* Optimize warptile matmul shader, replace blocktile with it
* Add split-k optimization for small matrix multiplication
Use semaphores for synchronization instead of fences or waitidle
Rework async write/read for synchronization
* Fix validation errors, improve compatibility with AMD GPUs
* Rework command buffer handling
* Variable matmul kernel using specialization constants
* Fix synchronization on AMD, add barriers for buffer ownership transfer, add debug flag and prints
* Reuse semaphores
* Handle stage flags during command buffer submission properly
* Increase matmul test runs for consistent results
* Fix F32 matmul
* Add vectorized loading and zeropadding for matrix multiplication
* Use pinned memory for f16 preprocessing
* Don't force aligned matmul
* Don't free before queue done
* Replace VMA library with native Vulkan buffer management
* Basic offloading support with mul_f32 and dmmv for q4_0
* Run glslc commands in parallel
* Unroll loops in dmmv shader
* Reduce usage of waitIdle
* Reuse pinned allocation for f16 conversion
* Handle devices with only a single queue
* Fix trailing whitespace in CMakeLists.txt
* Allow parallel execution of kernels, parallelize third and fourth dimension calls
* Add fallback for devices only supporting one DescriptorSet per DescriptorPool
* Move to graph function similar to CUDA implementation
* Use F16 kernel for most things, replace q_f32 with mul_mat_q_f16 function
* Add F32 dmmv shaders
* Batch submissions
* Add .spv to gitignore
* Split off matrix vector multiplication for separate optimization
* Use single command buffer for matrix vector multiplication ops
* Reduce overhead of mul_f32 calls by using a single command buffer
* Add submission batching to mul_f32
* Fix tests
* Add missing barrier
* Add further missing barrier
* Add further ops
* Replace vk::QueueFamilyIgnored with VK_QUEUE_FAMILY_IGNORED to support more Vulkan header versions
* Remove unnecessary cblas link
* Fix descriptor set pre-allocation assert
* Add runtime shader compilation, start transferring shaders to this approach
* Transfer remaining shaders to header and compile on runtime
* Fix fp32 fallback if device doesn't support fp16, add force disable env var GGML_VULKAN_DISABLE_F16
* Add support for q4_1, q5_0, q5_1 and q8_0
* Remove unnecessary scalar layout extension
* Parse graph early to pre-record command buffers
* Add q6_k support
* Add multi-submit for command buffers
* Fix q6_k dequant shader for AMD
* Fix q6_k for GPUs without fp16 support
* Simplify q6_k fp16 fix
* Minor fixes
* Fix wg_denom of m-mulmat shaders
* Add Python-based Vulkan shader generator
* Replace shaderc dependency with precompiled shaders
Fix python script to generate shaders
* Clean up code
* Fix shader generator script Windows compatibility
Co-authored-by: Concedo <39025047+LostRuins@users.noreply.github.com>
* Close file before deletion
* Fix vulkan shader fp32 name
* Add q2_k and q3_k support
Add validation check to compare shader results to cpu results
* Add q4_k support
* Add q5_k support
* Bake SPIR-V bytecode into the library instead of loading shaders from file
* Switch to signal semaphores for flexibility
Prepare broadcasting support for mul mat
* Finish broadcasting mul mat support for GQA
* Clean up unused functions
Add repeat op
* Add further ops, not yet enabled. Improve semaphore code
* Reduce number of used semaphores by utilizing timelines more properly
* Remove queue information
* Reuse timeline semaphores, allow parallel operation with binary semaphores to work around nvidia driver limitations
* Add Vulkan to llama-bench
* Remove cblas dependency
* Fix matmul k-split bug
* Fix q4_k dmmv K_QUANTS_PER_ITERATION 1 shader
* Add RMS Norm shader, rework op_f32 shader setup, fix matmul bug
* Fix issues with float16 overflows in shaders
* Fix issues with older Vulkan headers on Ubuntu 22.04
* Allow multi-op partial offloading by parsing the graph to preallocate enough between-op buffers
* Implement further ops, rework op_f32 calls, fix bugs
* Finish full offloading support, add last remaining ops, fix bugs, remove redundant code
* Upload generated file ggml-vulkan-shaders.hpp, remove redundant shaders
* Merge upstream changes, fix conflicts, adapt soft_max op
* Fix Python and shader header format
* Free model gpu buffers on exit
* Use single queue per device to simplify code
* Add matmul shader support for running multiple calculations in parallel
* Switch from semaphore-synchronized multiple command buffers per op to single command buffer for multiple ops, whole graph if possible
* Fix missing event cast
* Replace uint64_t(-1) with UINT64_MAX, rename function for clarity
* Fix warning about empty C function parameters
* Fix compiler warnings
* Properly implement Vulkan backend buffer handling
* Fix oversized host staging buffers
* Simplify barrier synchronization calls
* Fix gcc warnings
* Implement max_size for backend buffer types to limit the size of a single allocation
* Use min of maxMemoryAllocationSize and maxBufferSize for device max allocation size
* refactor multi buf
* Disable unsupported ops to fix tests
* Check for maintenance4 support before using it
* Handle devices with only a single queue
* Fix single queue logic
* propagate buffer usage in multi buffers
* Implement rope_neox op
* Cleanup header and other files
* Simplify gpu_extras by removing events and putting staging memcpys into contexts
* Move queue into context
Add not-yet-enabled async backend ops
* Simplify context use, optimize matmul shader for warp size 64 (AMD GCN), fix split_k matmul shader optimization
* Add get_max_size to SYCL backend.
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
* llama : fix trailing whitespace
---------
Co-authored-by: Henri Vasserman <henv@hot.ee>
Co-authored-by: Concedo <39025047+LostRuins@users.noreply.github.com>
Co-authored-by: slaren <slarengh@gmail.com>
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
2024-01-28 17:03:59 +00:00
|
|
|
|
2023-11-13 12:16:23 +00:00
|
|
|
//
|
2024-10-02 23:49:47 +00:00
|
|
|
// Backend (stream)
|
2023-11-13 12:16:23 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
struct ggml_backend_i {
|
2024-10-02 23:49:47 +00:00
|
|
|
const char * (*get_name)(ggml_backend_t backend);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-10-02 23:49:47 +00:00
|
|
|
void (*free)(ggml_backend_t backend);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-01-12 19:07:38 +00:00
|
|
|
// (optional) asynchronous tensor data access
|
2024-10-02 23:49:47 +00:00
|
|
|
void (*set_tensor_async)(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
|
|
|
|
void (*get_tensor_async)(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
|
|
|
|
bool (*cpy_tensor_async)(ggml_backend_t backend_src, ggml_backend_t backend_dst, const struct ggml_tensor * src, struct ggml_tensor * dst);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-10-30 01:01:23 +00:00
|
|
|
// (optional) complete all pending operations (required if the backend supports async operations)
|
2024-10-02 23:49:47 +00:00
|
|
|
void (*synchronize)(ggml_backend_t backend);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-10-30 01:01:23 +00:00
|
|
|
// (optional) graph plans (not used currently)
|
|
|
|
// compute graph with a plan
|
2024-10-02 23:49:47 +00:00
|
|
|
ggml_backend_graph_plan_t (*graph_plan_create) (ggml_backend_t backend, const struct ggml_cgraph * cgraph);
|
|
|
|
void (*graph_plan_free) (ggml_backend_t backend, ggml_backend_graph_plan_t plan);
|
2024-06-13 01:11:35 +00:00
|
|
|
// update the plan with a new graph - this should be faster than creating a new plan when the graph has the same topology
|
2024-10-02 23:49:47 +00:00
|
|
|
void (*graph_plan_update) (ggml_backend_t backend, ggml_backend_graph_plan_t plan, const struct ggml_cgraph * cgraph);
|
2024-06-13 01:11:35 +00:00
|
|
|
// compute the graph with the plan
|
2024-10-02 23:49:47 +00:00
|
|
|
enum ggml_status (*graph_plan_compute)(ggml_backend_t backend, ggml_backend_graph_plan_t plan);
|
|
|
|
|
|
|
|
// compute graph (always async if supported by the backend)
|
|
|
|
enum ggml_status (*graph_compute) (ggml_backend_t backend, struct ggml_cgraph * cgraph);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-03-13 17:54:21 +00:00
|
|
|
// (optional) event synchronization
|
2024-10-02 23:49:47 +00:00
|
|
|
// record an event on this stream
|
|
|
|
void (*event_record)(ggml_backend_t backend, ggml_backend_event_t event);
|
|
|
|
// wait for an event on on a different stream
|
|
|
|
void (*event_wait) (ggml_backend_t backend, ggml_backend_event_t event);
|
2023-11-13 12:16:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ggml_backend {
|
2024-02-24 16:27:36 +00:00
|
|
|
ggml_guid_t guid;
|
2023-11-13 12:16:23 +00:00
|
|
|
struct ggml_backend_i iface;
|
2024-10-02 23:49:47 +00:00
|
|
|
ggml_backend_dev_t device;
|
|
|
|
void * context;
|
2023-11-13 12:16:23 +00:00
|
|
|
};
|
|
|
|
|
2024-03-13 17:54:21 +00:00
|
|
|
struct ggml_backend_event {
|
2024-10-02 23:49:47 +00:00
|
|
|
struct ggml_backend_device * device;
|
2024-03-13 17:54:21 +00:00
|
|
|
void * context;
|
|
|
|
};
|
|
|
|
|
2023-12-07 20:26:54 +00:00
|
|
|
//
|
2024-10-02 23:49:47 +00:00
|
|
|
// Backend device
|
2023-12-07 20:26:54 +00:00
|
|
|
//
|
|
|
|
|
2024-10-02 23:49:47 +00:00
|
|
|
// Note: if additional properties are needed, we should add a struct with all of them
|
|
|
|
// the current functions to obtain the properties can remain, since they are more convenient for often used properties
|
|
|
|
struct ggml_backend_device_i {
|
|
|
|
// device name: short identifier for this device, such as "CPU" or "CUDA0"
|
|
|
|
const char * (*get_name)(ggml_backend_dev_t dev);
|
|
|
|
|
|
|
|
// device description: short informative description of the device, could be the model name
|
|
|
|
const char * (*get_description)(ggml_backend_dev_t dev);
|
|
|
|
|
|
|
|
// device memory in bytes
|
|
|
|
void (*get_memory)(ggml_backend_dev_t dev, size_t * free, size_t * total);
|
|
|
|
|
|
|
|
// device type
|
|
|
|
enum ggml_backend_dev_type (*get_type)(ggml_backend_dev_t dev);
|
|
|
|
|
|
|
|
// device properties
|
|
|
|
void (*get_props)(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props);
|
|
|
|
|
|
|
|
// backend (stream) initialization
|
|
|
|
ggml_backend_t (*init_backend)(ggml_backend_dev_t dev, const char * params);
|
|
|
|
|
|
|
|
// preferred buffer type
|
|
|
|
ggml_backend_buffer_type_t (*get_buffer_type)(ggml_backend_dev_t dev);
|
|
|
|
|
|
|
|
// (optional) host buffer type (in system memory, typically this is a pinned memory buffer for faster transfers between host and device)
|
|
|
|
ggml_backend_buffer_type_t (*get_host_buffer_type)(ggml_backend_dev_t dev);
|
|
|
|
|
|
|
|
// (optional) buffer from pointer: create a buffer from a host pointer (useful for memory mapped models and importing data from other libraries)
|
|
|
|
ggml_backend_buffer_t (*buffer_from_host_ptr)(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size);
|
|
|
|
|
|
|
|
// check if the backend can compute an operation
|
|
|
|
bool (*supports_op)(ggml_backend_dev_t dev, const struct ggml_tensor * op);
|
|
|
|
|
|
|
|
// check if the backend can use tensors allocated in a buffer type
|
|
|
|
bool (*supports_buft)(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft);
|
|
|
|
|
2024-10-07 19:55:08 +00:00
|
|
|
// (optional) check if the backend wants to run an operation, even if the weights are allocated in an incompatible buffer
|
|
|
|
// these should be expensive operations that may benefit from running on this backend instead of the CPU backend
|
2024-10-02 23:49:47 +00:00
|
|
|
bool (*offload_op)(ggml_backend_dev_t dev, const struct ggml_tensor * op);
|
|
|
|
|
|
|
|
// (optional) event synchronization
|
|
|
|
ggml_backend_event_t (*event_new) (ggml_backend_dev_t dev);
|
|
|
|
void (*event_free) (ggml_backend_dev_t dev, ggml_backend_event_t event);
|
|
|
|
void (*event_synchronize) (ggml_backend_dev_t dev, ggml_backend_event_t event);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ggml_backend_device {
|
|
|
|
struct ggml_backend_device_i iface;
|
|
|
|
ggml_backend_reg_t reg;
|
|
|
|
void * context;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Backend (reg)
|
|
|
|
//
|
|
|
|
|
|
|
|
struct ggml_backend_reg_i {
|
|
|
|
const char * (*get_name)(ggml_backend_reg_t reg);
|
|
|
|
|
|
|
|
// enumerate available devices
|
|
|
|
size_t (*get_device_count)(ggml_backend_reg_t reg);
|
|
|
|
ggml_backend_dev_t (*get_device)(ggml_backend_reg_t reg, size_t index);
|
|
|
|
|
|
|
|
// (optional) get a pointer to a function in the backend
|
|
|
|
// backends can add custom functions that are not part of the standard ggml-backend interface
|
|
|
|
void * (*get_proc_address)(ggml_backend_reg_t reg, const char * name);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ggml_backend_reg {
|
2024-11-25 14:13:39 +00:00
|
|
|
int api_version; // initialize to GGML_BACKEND_API_VERSION
|
2024-10-02 23:49:47 +00:00
|
|
|
struct ggml_backend_reg_i iface;
|
|
|
|
void * context;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Internal backend registry API
|
2024-11-25 14:13:39 +00:00
|
|
|
GGML_API void ggml_backend_register(ggml_backend_reg_t reg);
|
|
|
|
GGML_API void ggml_backend_device_register(ggml_backend_dev_t device);
|
|
|
|
|
|
|
|
// Add backend dynamic loading support to the backend
|
|
|
|
|
2024-12-01 15:12:41 +00:00
|
|
|
// Initialize the backend
|
|
|
|
typedef ggml_backend_reg_t (*ggml_backend_init_t)(void);
|
|
|
|
// Optional: obtain a score for the backend based on the system configuration
|
|
|
|
// Higher scores are preferred, 0 means the backend is not supported in the current system
|
|
|
|
typedef int (*ggml_backend_score_t)(void);
|
|
|
|
|
|
|
|
#ifdef GGML_BACKEND_DL
|
|
|
|
# ifdef __cplusplus
|
|
|
|
# define GGML_BACKEND_DL_IMPL(reg_fn) \
|
|
|
|
extern "C" { \
|
|
|
|
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_init(void); \
|
|
|
|
} \
|
|
|
|
ggml_backend_reg_t ggml_backend_init(void) { \
|
|
|
|
return reg_fn(); \
|
|
|
|
}
|
|
|
|
# define GGML_BACKEND_DL_SCORE_IMPL(score_fn) \
|
|
|
|
extern "C" { \
|
|
|
|
GGML_BACKEND_API int ggml_backend_score(void); \
|
|
|
|
} \
|
|
|
|
int ggml_backend_score(void) { \
|
|
|
|
return score_fn(); \
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
# define GGML_BACKEND_DL_IMPL(reg_fn) \
|
|
|
|
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_init(void); \
|
|
|
|
ggml_backend_reg_t ggml_backend_init(void) { \
|
|
|
|
return reg_fn(); \
|
|
|
|
}
|
|
|
|
# define GGML_BACKEND_DL_SCORE_IMPL(score_fn) \
|
|
|
|
GGML_BACKEND_API int ggml_backend_score(void); \
|
|
|
|
int ggml_backend_score(void) { \
|
|
|
|
return score_fn(); \
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define GGML_BACKEND_DL_IMPL(reg_fn)
|
|
|
|
# define GGML_BACKEND_DL_SCORE_IMPL(score_fn)
|
|
|
|
#endif
|
2023-12-07 20:26:54 +00:00
|
|
|
|
2023-11-13 12:16:23 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|