Skip to content

Commit be88ee1

Browse files
hipuddingggerganov
authored andcommitted
ggml : add CANN backend (llama/0)
ggml-ci
1 parent 3ab19c7 commit be88ee1

18 files changed

+10830
-0
lines changed

ggml/include/ggml-cann.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2023-2024 The ggml authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
#pragma once
24+
25+
#include "ggml-backend.h"
26+
#include "ggml.h"
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
/**
33+
* @brief Maximum number of CANN devices supported.
34+
*/
35+
#define GGML_CANN_MAX_DEVICES 16
36+
37+
/**
38+
* @brief Initializes the CANN backend for a specified device.
39+
*
40+
* This function initializes the CANN backend for the given device.
41+
* It verifies the device index, allocates a context, and creates a backend
42+
* instance.
43+
*
44+
* @param device The index of the device to initialize.
45+
* @return A pointer to the initialized backend instance, or nullptr on failure.
46+
*/
47+
GGML_API GGML_CALL ggml_backend_t ggml_backend_cann_init(int32_t device);
48+
49+
/**
50+
* @brief Checks if a given backend is a CANN backend.
51+
*
52+
* This function verifies if the provided backend is a CANN backend by comparing
53+
* its GUID with the CANN backend's GUID.
54+
*
55+
* @param backend The backend instance to check.
56+
* @return True if the backend is a CANN backend, false otherwise.
57+
*/
58+
GGML_API GGML_CALL bool ggml_backend_is_cann(ggml_backend_t backend);
59+
60+
/**
61+
* @brief Retrieves the CANN buffer type for a specified device.
62+
*
63+
* This function initializes and returns the buffer type interface associated
64+
* with the given device. It ensures thread-safe access using a mutex.
65+
*
66+
* @param device The device index for which to retrieve the buffer type.
67+
* @return A pointer to the buffer type interface for the specified device, or
68+
* nullptr if the device index is out of range.
69+
*/
70+
GGML_API GGML_CALL ggml_backend_buffer_type_t
71+
ggml_backend_cann_buffer_type(int32_t device);
72+
73+
/**
74+
* @brief Retrieves the number of CANN devices available.
75+
*
76+
* This function returns the number of CANN devices available based on
77+
* information obtained from `ggml_cann_info()`.
78+
*
79+
* @return The number of CANN devices available.
80+
*/
81+
GGML_API GGML_CALL int32_t ggml_backend_cann_get_device_count(void);
82+
83+
/**
84+
* @brief Retrieves the description of a specific CANN device.
85+
*
86+
* This function sets the specified device, retrieves the SoC name,
87+
* and writes it into the provided description buffer.
88+
*
89+
* @param device The device index to retrieve the description for.
90+
* @param description Pointer to a buffer where the description will be written.
91+
* @param description_size Size of the description buffer.
92+
*/
93+
GGML_API GGML_CALL void ggml_backend_cann_get_device_description(
94+
int32_t device, char* description, size_t description_size);
95+
96+
/**
97+
* @brief Retrieves the memory information of a specific CANN device.
98+
*
99+
* This function sets the specified device, retrieves the free and total
100+
* memory information of the specified type (ACL_HBM_MEM), and stores them
101+
* in the provided pointers.
102+
*
103+
* @param device The device index to retrieve memory information for.
104+
* @param free Pointer to a variable where the free memory size will be stored.
105+
* @param total Pointer to a variable where the total memory size will be
106+
* stored.
107+
*/
108+
GGML_API GGML_CALL void ggml_backend_cann_get_device_memory(int32_t device,
109+
size_t* free,
110+
size_t* total);
111+
112+
/**
113+
* @brief Set the logging callback for GGML.
114+
*
115+
* This function sets the logging callback and user data for logging.
116+
*
117+
* @param log_callback The logging callback to set.
118+
* @param user_data User data to pass to the logging callback.
119+
*/
120+
GGML_API void ggml_backend_cann_log_set_callback(ggml_log_callback log_callback,
121+
void* user_data);
122+
123+
#ifdef __cplusplus
124+
}
125+
#endif

0 commit comments

Comments
 (0)