ggml-cann.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #pragma once
  23. #include "ggml-backend.h"
  24. #include "ggml.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * @brief Maximum number of CANN devices supported.
  30. */
  31. #define GGML_CANN_MAX_DEVICES 16
  32. /**
  33. * @brief Initializes the CANN backend for a specified device.
  34. *
  35. * This function initializes the CANN backend for the given device.
  36. * It verifies the device index, allocates a context, and creates a backend
  37. * instance.
  38. *
  39. * @param device The index of the device to initialize.
  40. * @return A pointer to the initialized backend instance, or nullptr on failure.
  41. */
  42. GGML_API GGML_CALL ggml_backend_t ggml_backend_cann_init(int32_t device);
  43. /**
  44. * @brief Checks if a given backend is a CANN backend.
  45. *
  46. * This function verifies if the provided backend is a CANN backend by comparing
  47. * its GUID with the CANN backend's GUID.
  48. *
  49. * @param backend The backend instance to check.
  50. * @return True if the backend is a CANN backend, false otherwise.
  51. */
  52. GGML_API GGML_CALL bool ggml_backend_is_cann(ggml_backend_t backend);
  53. /**
  54. * @brief Retrieves the CANN buffer type for a specified device.
  55. *
  56. * This function initializes and returns the buffer type interface associated
  57. * with the given device. It ensures thread-safe access using a mutex.
  58. *
  59. * @param device The device index for which to retrieve the buffer type.
  60. * @return A pointer to the buffer type interface for the specified device, or
  61. * nullptr if the device index is out of range.
  62. */
  63. GGML_API GGML_CALL ggml_backend_buffer_type_t
  64. ggml_backend_cann_buffer_type(int32_t device);
  65. /**
  66. * @brief Retrieves the number of CANN devices available.
  67. *
  68. * This function returns the number of CANN devices available based on
  69. * information obtained from `ggml_cann_info()`.
  70. *
  71. * @return The number of CANN devices available.
  72. */
  73. GGML_API GGML_CALL int32_t ggml_backend_cann_get_device_count(void);
  74. /**
  75. * @brief pinned host buffer for use with the CPU backend for faster copies between CPU and NPU.
  76. *
  77. * @return A pointer to the host buffer type interface.
  78. */
  79. GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cann_host_buffer_type(void);
  80. /**
  81. * @brief Retrieves the description of a specific CANN device.
  82. *
  83. * This function sets the specified device, retrieves the SoC name,
  84. * and writes it into the provided description buffer.
  85. *
  86. * @param device The device index to retrieve the description for.
  87. * @param description Pointer to a buffer where the description will be written.
  88. * @param description_size Size of the description buffer.
  89. */
  90. GGML_API GGML_CALL void ggml_backend_cann_get_device_description(
  91. int32_t device, char* description, size_t description_size);
  92. /**
  93. * @brief Retrieves the memory information of a specific CANN device.
  94. *
  95. * This function sets the specified device, retrieves the free and total
  96. * memory information of the specified type (ACL_HBM_MEM), and stores them
  97. * in the provided pointers.
  98. *
  99. * @param device The device index to retrieve memory information for.
  100. * @param free Pointer to a variable where the free memory size will be stored.
  101. * @param total Pointer to a variable where the total memory size will be
  102. * stored.
  103. */
  104. GGML_API GGML_CALL void ggml_backend_cann_get_device_memory(int32_t device,
  105. size_t* free,
  106. size_t* total);
  107. /**
  108. * @brief Set the logging callback for GGML.
  109. *
  110. * This function sets the logging callback and user data for logging.
  111. *
  112. * @param log_callback The logging callback to set.
  113. * @param user_data User data to pass to the logging callback.
  114. */
  115. GGML_API void ggml_backend_cann_log_set_callback(ggml_log_callback log_callback,
  116. void* user_data);
  117. #ifdef __cplusplus
  118. }
  119. #endif