Skip to content

Commit 8d52430

Browse files
Merge pull request #329 from janhq/update-dev-from-master-2025-11-18-00-35
Sync master with upstream release b7087
2 parents f9da2bc + cb623de commit 8d52430

File tree

17 files changed

+1283
-3786
lines changed

17 files changed

+1283
-3786
lines changed

ggml/src/ggml-cann/Doxyfile

Lines changed: 0 additions & 2579 deletions
This file was deleted.

ggml/src/ggml-cann/acl_tensor.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ aclDataType ggml_cann_type_mapping(ggml_type type) {
4848
default:
4949
return ACL_DT_UNDEFINED;
5050
}
51-
return ACL_DT_UNDEFINED;
5251
}
5352

54-
aclTensor * ggml_cann_create_tensor(const ggml_tensor * tensor,
55-
int64_t * ne,
56-
size_t * nb,
57-
int64_t dims,
58-
aclFormat format,
59-
size_t offset) {
53+
acl_tensor_ptr ggml_cann_create_tensor(const ggml_tensor * tensor,
54+
int64_t * ne,
55+
size_t * nb,
56+
int64_t dims,
57+
aclFormat format,
58+
size_t offset) {
6059
// If tensor is bcasted, Up to GGML_MAX_DIMS additional dimensions will be
6160
// added.
6261
int64_t acl_ne[GGML_MAX_DIMS * 2], acl_stride[GGML_MAX_DIMS * 2];
@@ -87,10 +86,20 @@ aclTensor * ggml_cann_create_tensor(const ggml_tensor * tensor,
8786
std::reverse(acl_ne, acl_ne + final_dims);
8887
std::reverse(acl_stride, acl_stride + final_dims);
8988

90-
aclTensor * acl_tensor = aclCreateTensor(acl_ne, final_dims, ggml_cann_type_mapping(tensor->type), acl_stride,
91-
elem_offset, format, &acl_storage_len, 1, tensor->data);
89+
aclTensor * raw = aclCreateTensor(acl_ne, final_dims, ggml_cann_type_mapping(tensor->type), acl_stride, elem_offset,
90+
format, &acl_storage_len, 1, tensor->data);
9291

93-
return acl_tensor;
92+
return acl_tensor_ptr(raw);
93+
}
94+
95+
acl_int_array_ptr ggml_cann_create_int_array(const int64_t * value, uint64_t size) {
96+
aclIntArray * raw = aclCreateIntArray(value, size);
97+
return acl_int_array_ptr(raw);
98+
}
99+
100+
acl_scalar_ptr ggml_cann_create_scalar(void * value, aclDataType dataType) {
101+
aclScalar * raw = aclCreateScalar(value, dataType);
102+
return acl_scalar_ptr(raw);
94103
}
95104

96105
bool ggml_cann_need_bcast(const ggml_tensor * t0, const ggml_tensor * t1) {

ggml/src/ggml-cann/acl_tensor.h

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
#ifndef CANN_ACL_TENSOR_H
2424
#define CANN_ACL_TENSOR_H
2525

26-
#include <algorithm>
27-
#include <cstring>
26+
#include "common.h"
2827

2928
#include <aclnn/aclnn_base.h>
30-
#include "common.h"
29+
30+
#include <algorithm>
31+
#include <cstring>
3132

3233
/**
3334
* @brief Maps a ggml_type to its corresponding aclDataType.
@@ -43,6 +44,20 @@
4344
*/
4445
aclDataType ggml_cann_type_mapping(ggml_type type);
4546

47+
// Deleter for acl objects.
48+
template <typename T, aclError (*DestroyFunc)(const T *)> struct acl_deleter {
49+
void operator()(T * ptr) const noexcept {
50+
if (ptr) {
51+
ACL_CHECK(DestroyFunc(ptr));
52+
}
53+
}
54+
};
55+
56+
using acl_tensor_ptr = std::unique_ptr<aclTensor, acl_deleter<aclTensor, aclDestroyTensor>>;
57+
using acl_int_array_ptr = std::unique_ptr<aclIntArray, acl_deleter<aclIntArray, aclDestroyIntArray>>;
58+
using acl_scalar_ptr = std::unique_ptr<aclScalar, acl_deleter<aclScalar, aclDestroyScalar>>;
59+
using acl_tensor_list_ptr = std::unique_ptr<aclTensorList, acl_deleter<aclTensorList, aclDestroyTensorList>>;
60+
4661
/**
4762
* @brief Creates an ACL tensor from a ggml_tensor with optional shape.
4863
*
@@ -62,12 +77,12 @@ aclDataType ggml_cann_type_mapping(ggml_type type);
6277
* @param offset Offset in bytes for the ACL tensor data. Defaults to 0.
6378
* @return Pointer to the created ACL tensor.
6479
*/
65-
aclTensor * ggml_cann_create_tensor(const ggml_tensor * tensor,
66-
int64_t * ne = nullptr,
67-
size_t * nb = nullptr,
68-
int64_t dims = 0,
69-
aclFormat format = ACL_FORMAT_ND,
70-
size_t offset = 0);
80+
acl_tensor_ptr ggml_cann_create_tensor(const ggml_tensor * tensor,
81+
int64_t * ne = nullptr,
82+
size_t * nb = nullptr,
83+
int64_t dims = 0,
84+
aclFormat format = ACL_FORMAT_ND,
85+
size_t offset = 0);
7186

7287
/**
7388
* @brief Template for creating an ACL tensor from provided parameters. typename TYPE
@@ -90,14 +105,14 @@ aclTensor * ggml_cann_create_tensor(const ggml_tensor * tensor,
90105
* @return Pointer to the created ACL tensor.
91106
*/
92107
template <typename TYPE>
93-
aclTensor * ggml_cann_create_tensor(void * data_ptr,
94-
aclDataType dtype,
95-
TYPE type_size,
96-
int64_t * ne,
97-
TYPE * nb,
98-
int64_t dims,
99-
aclFormat format = ACL_FORMAT_ND,
100-
size_t offset = 0) {
108+
acl_tensor_ptr ggml_cann_create_tensor(void * data_ptr,
109+
aclDataType dtype,
110+
TYPE type_size,
111+
int64_t * ne,
112+
TYPE * nb,
113+
int64_t dims,
114+
aclFormat format = ACL_FORMAT_ND,
115+
size_t offset = 0) {
101116
int64_t tmp_ne[GGML_MAX_DIMS * 2];
102117
int64_t tmp_stride[GGML_MAX_DIMS * 2];
103118

@@ -114,10 +129,75 @@ aclTensor * ggml_cann_create_tensor(void * data_ptr,
114129
std::reverse(tmp_ne, tmp_ne + dims);
115130
std::reverse(tmp_stride, tmp_stride + dims);
116131

117-
aclTensor * acl_tensor =
132+
aclTensor * raw =
118133
aclCreateTensor(tmp_ne, dims, dtype, tmp_stride, offset / type_size, format, &acl_storage_len, 1, data_ptr);
119134

120-
return acl_tensor;
135+
return acl_tensor_ptr(raw);
136+
}
137+
138+
/**
139+
* @brief Create an ACL int array resource wrapped in a smart pointer.
140+
*
141+
* This function constructs an aclIntArray from the provided int64_t values
142+
* and returns it as an acl_int_array_ptr (a std::unique_ptr with a custom
143+
* deleter). The returned pointer owns the ACL resource and will automatically
144+
* destroy it via aclDestroyIntArray().
145+
*
146+
* @param value Pointer to the int64_t elements.
147+
* @param size Number of elements in value.
148+
*
149+
* @return A smart pointer managing the created ACL int array.
150+
*/
151+
acl_int_array_ptr ggml_cann_create_int_array(const int64_t * value, uint64_t size);
152+
153+
/**
154+
* @brief Create an ACL scalar resource wrapped in a smart pointer.
155+
*
156+
* This function constructs an aclScalar from the raw value pointer and ACL
157+
* data type, then returns it as an acl_scalar_ptr (a std::unique_ptr with
158+
* a custom deleter). The returned pointer owns the ACL scalar and will
159+
* automatically destroy it via aclDestroyScalar().
160+
*
161+
* @param value Pointer to the raw scalar memory.
162+
* @param dataType ACL data type of the scalar.
163+
*
164+
* @return A smart pointer managing the created ACL scalar.
165+
*/
166+
acl_scalar_ptr ggml_cann_create_scalar(void * value, aclDataType dataType);
167+
168+
/**
169+
* @brief Create an ACL tensor list from multiple tensor smart pointers.
170+
*
171+
* This function accepts a variadic list of acl_tensor_ptr (a unique_ptr with
172+
* custom deleter) and produces an aclTensorList using aclCreateTensorList().
173+
*
174+
* The lifecycle management of the tensor objects changes as follows:
175+
* - aclCreateTensorList() takes ownership of the tensors
176+
* - Each input smart pointer releases ownership using release()
177+
* - As a result, the tensors will NOT be destroyed by unique_ptr
178+
* - Instead, they will be destroyed when aclDestroyTensorList() is called
179+
*
180+
* This ensures correct ownership transfer and prevents double-free situations.
181+
*
182+
* @param acl_tensor_ptr Variadic template parameter; each argument must be
183+
* a unique_ptr-like type supporting get() and release().
184+
*
185+
* @param tensors Variadic list of acl_tensor_ptr objects. Ownership of
186+
* each tensor is transferred away from these smart pointers.
187+
*
188+
* @return A smart pointer (acl_tensor_list_ptr) owning the created ACL tensor list.
189+
*
190+
* @note This implementation is C++11 compatible. The ownership-release process is
191+
* executed using a pack expansion inside an initializer list.
192+
*/
193+
template <typename... acl_tensor_ptr> acl_tensor_list_ptr ggml_cann_create_tensor_list(acl_tensor_ptr &&... tensors) {
194+
aclTensor * raw_tensors[] = { tensors.get()... };
195+
aclTensorList * raw = aclCreateTensorList(raw_tensors, sizeof...(tensors));
196+
// aclTensor will release by aclTensorList, so release ownership without
197+
// destroying the tensor
198+
int dummy[] = { (tensors.release(), 0)... };
199+
GGML_UNUSED(dummy);
200+
return acl_tensor_list_ptr(raw);
121201
}
122202

123203
/**

0 commit comments

Comments
 (0)