Skip to content

Commit e173ee4

Browse files
committed
user unique ptr
1 parent 145aa2b commit e173ee4

File tree

5 files changed

+479
-555
lines changed

5 files changed

+479
-555
lines changed

ggml/src/ggml-cann/acl_tensor.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ aclDataType ggml_cann_type_mapping(ggml_type type) {
5050
}
5151
}
5252

53-
aclTensor * ggml_cann_create_tensor(const ggml_tensor * tensor,
53+
acl_tensor_ptr ggml_cann_create_tensor(const ggml_tensor * tensor,
5454
int64_t * ne,
5555
size_t * nb,
5656
int64_t dims,
@@ -86,10 +86,20 @@ aclTensor * ggml_cann_create_tensor(const ggml_tensor * tensor,
8686
std::reverse(acl_ne, acl_ne + final_dims);
8787
std::reverse(acl_stride, acl_stride + final_dims);
8888

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

92-
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);
93103
}
94104

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

ggml/src/ggml-cann/acl_tensor.h

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@
4343
*/
4444
aclDataType 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
*/
92132
template <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

Comments
 (0)