Skip to content

Commit 9efc21c

Browse files
committed
Add comment
1 parent 7acd077 commit 9efc21c

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

ggml/src/ggml-cann/acl_tensor.h

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*/
4545
aclDataType ggml_cann_type_mapping(ggml_type type);
4646

47+
// Deleter for aclTensor.
4748
struct acl_tensor_deleter {
4849
void operator()(aclTensor * ptr) const noexcept {
4950
if (ptr != nullptr) {
@@ -52,8 +53,7 @@ struct acl_tensor_deleter {
5253
}
5354
};
5455

55-
using acl_tensor_ptr = std::unique_ptr<aclTensor, acl_tensor_deleter>;
56-
56+
// Deleter for aclIntArray.
5757
struct acl_int_array_deleter {
5858
void operator()(aclIntArray * ptr) const noexcept {
5959
if (ptr != nullptr) {
@@ -62,8 +62,7 @@ struct acl_int_array_deleter {
6262
}
6363
};
6464

65-
using acl_int_array_ptr = std::unique_ptr<aclIntArray, acl_int_array_deleter>;
66-
65+
// Deleter for aclScalar.
6766
struct acl_scalar_deleter {
6867
void operator()(aclScalar * ptr) const noexcept {
6968
if (ptr != nullptr) {
@@ -72,8 +71,7 @@ struct acl_scalar_deleter {
7271
}
7372
};
7473

75-
using acl_scalar_ptr = std::unique_ptr<aclScalar, acl_scalar_deleter>;
76-
74+
// Deleter for aclTensorList.
7775
struct acl_tensor_list_deleter {
7876
void operator()(aclTensorList * ptr) const noexcept {
7977
if (ptr != nullptr) {
@@ -82,6 +80,9 @@ struct acl_tensor_list_deleter {
8280
}
8381
};
8482

83+
using acl_tensor_ptr = std::unique_ptr<aclTensor, acl_tensor_deleter>;
84+
using acl_int_array_ptr = std::unique_ptr<aclIntArray, acl_int_array_deleter>;
85+
using acl_scalar_ptr = std::unique_ptr<aclScalar, acl_scalar_deleter>;
8586
using acl_tensor_list_ptr = std::unique_ptr<aclTensorList, acl_tensor_list_deleter>;
8687

8788
/**
@@ -161,9 +162,61 @@ acl_tensor_ptr ggml_cann_create_tensor(void * data_ptr,
161162
return acl_tensor_ptr(raw);
162163
}
163164

165+
/**
166+
* @brief Create an ACL int array resource wrapped in a smart pointer.
167+
*
168+
* This function constructs an aclIntArray from the provided int64_t values
169+
* and returns it as an acl_int_array_ptr (a std::unique_ptr with a custom
170+
* deleter). The returned pointer owns the ACL resource and will automatically
171+
* destroy it via aclDestroyIntArray().
172+
*
173+
* @param value Pointer to the int64_t elements.
174+
* @param size Number of elements in value.
175+
*
176+
* @return A smart pointer managing the created ACL int array.
177+
*/
164178
acl_int_array_ptr ggml_cann_create_int_array(const int64_t * value, uint64_t size);
165-
acl_scalar_ptr ggml_cann_create_scalar(void * value, aclDataType dataType);
166179

180+
/**
181+
* @brief Create an ACL scalar resource wrapped in a smart pointer.
182+
*
183+
* This function constructs an aclScalar from the raw value pointer and ACL
184+
* data type, then returns it as an acl_scalar_ptr (a std::unique_ptr with
185+
* a custom deleter). The returned pointer owns the ACL scalar and will
186+
* automatically destroy it via aclDestroyScalar().
187+
*
188+
* @param value Pointer to the raw scalar memory.
189+
* @param dataType ACL data type of the scalar.
190+
*
191+
* @return A smart pointer managing the created ACL scalar.
192+
*/
193+
acl_scalar_ptr ggml_cann_create_scalar(void * value, aclDataType dataType);
194+
195+
/**
196+
* @brief Create an ACL tensor list from multiple tensor smart pointers.
197+
*
198+
* This function accepts a variadic list of acl_tensor_ptr (a unique_ptr with
199+
* custom deleter) and produces an aclTensorList using aclCreateTensorList().
200+
*
201+
* The lifecycle management of the tensor objects changes as follows:
202+
* - aclCreateTensorList() takes ownership of the tensors
203+
* - Each input smart pointer releases ownership using release()
204+
* - As a result, the tensors will NOT be destroyed by unique_ptr
205+
* - Instead, they will be destroyed when aclDestroyTensorList() is called
206+
*
207+
* This ensures correct ownership transfer and prevents double-free situations.
208+
*
209+
* @param acl_tensor_ptr Variadic template parameter; each argument must be
210+
* a unique_ptr-like type supporting get() and release().
211+
*
212+
* @param tensors Variadic list of acl_tensor_ptr objects. Ownership of
213+
* each tensor is transferred away from these smart pointers.
214+
*
215+
* @return A smart pointer (acl_tensor_list_ptr) owning the created ACL tensor list.
216+
*
217+
* @note This implementation is C++11 compatible. The ownership-release process is
218+
* executed using a pack expansion inside an initializer list.
219+
*/
167220
template <typename... acl_tensor_ptr> acl_tensor_list_ptr ggml_cann_create_tensor_list(acl_tensor_ptr &&... tensors) {
168221
aclTensor * raw_tensors[] = { tensors.get()... };
169222
aclTensorList * raw = aclCreateTensorList(raw_tensors, sizeof...(tensors));

ggml/src/ggml-cann/aclnn_ops.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ void ggml_cann_dup(ggml_backend_cann_context & ctx, ggml_tensor * dst) {
900900
* @param dims The number of dimensions of the tensor.
901901
* @param type The data type of the tensor.
902902
* @param type_size The size of each element in the tensor data type.
903-
* @return An ACL tensor initialized with zeros.
903+
* @return A tensor smart pointer initialized with zeros.
904904
*/
905905
static acl_tensor_ptr aclnn_zero(ggml_backend_cann_context & ctx,
906906
void * buffer,
@@ -937,7 +937,7 @@ static acl_tensor_ptr aclnn_zero(ggml_backend_cann_context & ctx,
937937
* @param type_size The size of each element in the tensor data type.
938938
* @param value The value to be used for initializing the tensor (default
939939
* is 1.0).
940-
* @return An ACL tensor initialized with value.
940+
* @return A tensor smart pointer initialized with value.
941941
*/
942942
static acl_tensor_ptr aclnn_values(ggml_backend_cann_context & ctx,
943943
void * buffer,
@@ -990,7 +990,7 @@ static void aclnn_fill_scalar(ggml_backend_cann_context & ctx, float scalar, acl
990990
* @param dims The number of tensor dimensions.
991991
* @param value The scalar value used to fill the tensor (supports zero
992992
* initialization via memset or arbitrary values via fill_scalar).
993-
* @return An aclTensor pointer created from the cached buffer.
993+
* @return A tensor smart pointer created from the cached buffer.
994994
*/
995995
static acl_tensor_ptr get_cache_acl_tensor(ggml_backend_cann_context & ctx,
996996
void ** buffer,

ggml/src/ggml-cann/aclnn_ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ template <auto binary_op> void ggml_cann_binary_op(ggml_backend_cann_context & c
993993
* and stores the result in the destination tensor.
994994
*
995995
* @tparam unary_op A callable with the signature:
996-
* void(ggml_backend_cann_context&, acl_tensor_ptr, acl_tensor_ptr)
996+
* void(ggml_backend_cann_context&, aclTensor *, aclTensor *)
997997
* where the first aclTensor is the source and the second is the destination.
998998
* @param ctx The CANN backend context for managing resources and execution.
999999
* @param dst The destination tensor. Its src[0] is treated as the input tensor.

0 commit comments

Comments
 (0)