4343 */
4444aclDataType ggml_cann_type_mapping (ggml_type type);
4545
46+ struct acl_tensor_deleter {
47+ void operator ()(aclTensor* ptr) const noexcept {
48+ if (ptr != nullptr ) {
49+ ACL_CHECK (aclDestroyTensor (ptr));
50+ }
51+ }
52+ };
53+
54+ using acl_tensor_ptr = std::unique_ptr<aclTensor, acl_tensor_deleter>;
55+
56+ struct acl_int_array_deleter {
57+ void operator ()(aclIntArray* ptr) const noexcept {
58+ if (ptr != nullptr ) {
59+ ACL_CHECK (aclDestroyIntArray (ptr));
60+ }
61+ }
62+ };
63+
64+ using acl_int_array_ptr = std::unique_ptr<aclIntArray, acl_int_array_deleter>;
65+
66+ struct acl_scalar_deleter {
67+ void operator ()(aclScalar* ptr) const noexcept {
68+ if (ptr != nullptr ) {
69+ ACL_CHECK (aclDestroyScalar (ptr));
70+ }
71+ }
72+ };
73+
74+ using acl_scalar_ptr = std::unique_ptr<aclScalar, acl_scalar_deleter>;
75+
76+ struct acl_tensor_list_deleter {
77+ void operator ()(aclTensorList* ptr) const noexcept {
78+ if (ptr != nullptr ) {
79+ ACL_CHECK (aclDestroyTensorList (ptr));
80+ }
81+ }
82+ };
83+
84+ using acl_tensor_list_ptr = std::unique_ptr<aclTensorList, acl_tensor_list_deleter>;
85+
4686/* *
4787 * @brief Creates an ACL tensor from a ggml_tensor with optional shape.
4888 *
@@ -62,7 +102,7 @@ aclDataType ggml_cann_type_mapping(ggml_type type);
62102 * @param offset Offset in bytes for the ACL tensor data. Defaults to 0.
63103 * @return Pointer to the created ACL tensor.
64104 */
65- aclTensor * ggml_cann_create_tensor (const ggml_tensor * tensor,
105+ acl_tensor_ptr ggml_cann_create_tensor (const ggml_tensor * tensor,
66106 int64_t * ne = nullptr ,
67107 size_t * nb = nullptr ,
68108 int64_t dims = 0 ,
@@ -90,7 +130,7 @@ aclTensor * ggml_cann_create_tensor(const ggml_tensor * tensor,
90130 * @return Pointer to the created ACL tensor.
91131 */
92132template <typename TYPE>
93- aclTensor * ggml_cann_create_tensor (void * data_ptr,
133+ acl_tensor_ptr ggml_cann_create_tensor (void * data_ptr,
94134 aclDataType dtype,
95135 TYPE type_size,
96136 int64_t * ne,
@@ -114,12 +154,27 @@ aclTensor * ggml_cann_create_tensor(void * data_ptr,
114154 std::reverse (tmp_ne, tmp_ne + dims);
115155 std::reverse (tmp_stride, tmp_stride + dims);
116156
117- aclTensor * acl_tensor =
157+ aclTensor * raw =
118158 aclCreateTensor (tmp_ne, dims, dtype, tmp_stride, offset / type_size, format, &acl_storage_len, 1 , data_ptr);
119159
120- return acl_tensor ;
160+ return acl_tensor_ptr (raw) ;
121161}
122162
163+ acl_int_array_ptr ggml_cann_create_int_array (const int64_t *value, uint64_t size);
164+ acl_scalar_ptr ggml_cann_create_scalar (void *value, aclDataType dataType);
165+
166+ template <typename ... acl_tensor_ptr>
167+ acl_tensor_list_ptr ggml_cann_create_tensor_list (acl_tensor_ptr&&... tensors) {
168+ aclTensor* raw_tensors[] = { tensors.get ()... };
169+ aclTensorList * raw = aclCreateTensorList (raw_tensors, sizeof ...(tensors));
170+ // aclTensor will release by aclTensorList, so release ownership without
171+ // destroying the tensor
172+ int dummy[] = { (tensors.release (), 0 )... };
173+ GGML_UNUSED (dummy);
174+ return acl_tensor_list_ptr (raw);
175+ }
176+
177+
123178/* *
124179 * @brief Checks if tensors require broadcasting based on their shapes.
125180 *
0 commit comments