1
0

ggml-cann.h 4.4 KB

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