Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
hipudding committed Jun 20, 2024
1 parent ab9d928 commit a5abdf7
Show file tree
Hide file tree
Showing 19 changed files with 458 additions and 535 deletions.
4 changes: 2 additions & 2 deletions ggml-cann.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ GGML_CALL static void ggml_backend_cann_transform_back_q4_0(
uint8_t* quant_offset = (uint8_t*)src;
uint16_t* scale_offset = (uint16_t*)((char*)src + quant_bytes);

for (;quant_offset < (uint8_t*)src + quant_bytes; quant_offset++) {
for (; quant_offset < (uint8_t*)src + quant_bytes; quant_offset++) {
(*quant_offset) ^= 0x88;
}
quant_offset = (uint8_t*)src;
Expand Down Expand Up @@ -895,7 +895,7 @@ GGML_CALL static enum ggml_status ggml_backend_cann_graph_compute(
fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__,
node->name, ggml_op_name(node->op));
}
// if not synchronize, aclrtSynchronizeStream in
// if not synchronize, aclrtSynchronizeStream in
// ggml_backend_cann_synchronize() will raise error.
ACL_CHECK(aclrtSynchronizeStream(cann_ctx->stream()));
GGML_ASSERT(ok);
Expand Down
9 changes: 4 additions & 5 deletions ggml-cann.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ extern "C" {

#define QK4_0 32
typedef struct {
uint16_t d; // delta
uint8_t qs[QK4_0 / 2]; // nibbles / quants
uint16_t d; // delta
uint8_t qs[QK4_0 / 2]; // nibbles / quants
} block_q4_0;


#define QK8_0 32
typedef struct {
uint16_t d; // delta
int8_t qs[QK8_0]; // quants
uint16_t d; // delta
int8_t qs[QK8_0]; // quants
} block_q8_0;

// backend API
Expand Down
4 changes: 2 additions & 2 deletions ggml-cann/acl_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct OpCaller {
OpCaller& attr(float value, const char* name);

template <typename T>
OpCaller& input(ggml_backend_cann_context& ctx, ggml_tensor *dst, T* values,
OpCaller& input(ggml_backend_cann_context& ctx, ggml_tensor* dst, T* values,
aclDataType dtype, size_t dims, int64_t* dim,
const char* name, aclrtStream stream = nullptr) {
size_t n_elem = 1;
Expand Down Expand Up @@ -69,4 +69,4 @@ struct OpCaller {
OpCaller& run(aclrtStream stream = nullptr);
};

#endif // CANN_ACL_OPS
#endif // CANN_ACL_OPS
52 changes: 25 additions & 27 deletions ggml-cann/acl_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ aclDataType type_mapping(ggml_type type) {

bool nb3_is_valid(const ggml_tensor* tensor) {
// check tensor->nb[3] is contiguous by ne.
if (tensor->nb[3] == tensor->ne[0] * tensor->ne[1] * tensor->ne[2]
* ggml_element_size(tensor)) {
if (tensor->nb[3] == tensor->ne[0] * tensor->ne[1] * tensor->ne[2] *
ggml_element_size(tensor)) {
return true;
}
else {
} else {
return false;
}
}
Expand Down Expand Up @@ -71,30 +70,29 @@ aclTensor* create_acl_tensor(const ggml_tensor* tensor, int64_t* bcast_ne,
acl_storage_ne[i] = acl_ne[i];
}
if (!nb3_is_valid(tensor)) {
if (tensor->ne[GGML_MAX_DIMS-1] == 1) {
if (tensor->nb[2] == tensor->nb[0]*tensor->ne[0] &&
tensor->nb[1] == tensor->nb[2]*tensor->ne[2]) {
// nb[3] not valid, tensor is not contiguous by permuted to
// (0,2,1,3), still use tensor->ne.
if (tensor->ne[GGML_MAX_DIMS - 1] == 1) {
if (tensor->nb[2] == tensor->nb[0] * tensor->ne[0] &&
tensor->nb[1] == tensor->nb[2] * tensor->ne[2]) {
// nb[3] not valid, tensor is not contiguous by permuted to
// (0,2,1,3), still use tensor->ne.
// @see https://github.com/ggerganov/llama.cpp/issues/7930.
for (int i = 0; i < GGML_MAX_DIMS; i++) {
for (int i = 0; i < GGML_MAX_DIMS; i++) {
acl_storage_ne[i] = acl_ne[i];
}
}
else {
} else {
// nb[3] is valid but tensor is not contiguous.
// e.g. nb=(2,1024,121072,1048576), ne=(32,128,8,1) with
// e.g. nb=(2,1024,121072,1048576), ne=(32,128,8,1) with
// fp16, 1024/2 not equal to 32.
// acl_storage_ne should be decided by tensor->nb.
for (int i = 0; i < GGML_MAX_DIMS-1; i++) {
acl_storage_ne[i] = std::max(static_cast<int64_t>(1),
acl_stride[i+1] / acl_stride[i]);
for (int i = 0; i < GGML_MAX_DIMS - 1; i++) {
acl_storage_ne[i] =
std::max(static_cast<int64_t>(1),
acl_stride[i + 1] / acl_stride[i]);
}
acl_storage_ne[GGML_MAX_DIMS-1] =
tensor->ne[GGML_MAX_DIMS-1];
acl_storage_ne[GGML_MAX_DIMS - 1] =
tensor->ne[GGML_MAX_DIMS - 1];
}
}
else {
} else {
// not impl
GGML_ASSERT(false);
}
Expand All @@ -113,10 +111,10 @@ aclTensor* create_acl_tensor(const ggml_tensor* tensor, int64_t* bcast_ne,
std::reverse(acl_stride, acl_stride + dims);
std::reverse(acl_storage_ne, acl_storage_ne + dims);

aclTensor* acl_tensor = aclCreateTensor(
acl_ne, dims, type_mapping(tensor->type), acl_stride,
offset / ggml_element_size(tensor), format, acl_storage_ne, dims,
deviceAddr);
aclTensor* acl_tensor =
aclCreateTensor(acl_ne, dims, type_mapping(tensor->type), acl_stride,
offset / ggml_element_size(tensor), format,
acl_storage_ne, dims, deviceAddr);

return acl_tensor;
}
Expand All @@ -135,9 +133,9 @@ aclTensor* create_acl_tensor(void* data_ptr, aclDataType dtype,
std::reverse(tmp_ne, tmp_ne + dims);
std::reverse(tmp_stride, tmp_stride + dims);

aclTensor* acl_tensor = aclCreateTensor(tmp_ne, dims, dtype, tmp_stride,
offset / type_size, format, tmp_ne,
dims, data_ptr);
aclTensor* acl_tensor =
aclCreateTensor(tmp_ne, dims, dtype, tmp_stride, offset / type_size,
format, tmp_ne, dims, data_ptr);

return acl_tensor;
}
Expand Down
8 changes: 5 additions & 3 deletions ggml-cann/acl_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ aclDataType type_mapping(ggml_type type);
aclTensor* create_acl_tensor(const ggml_tensor* tensor,
int64_t* bcast_ne = nullptr,
size_t* bcast_nb = nullptr, int64_t bcast_dims = 0,
aclFormat format = ACL_FORMAT_ND, size_t offset = 0);
aclFormat format = ACL_FORMAT_ND,
size_t offset = 0);

aclTensor* create_acl_tensor(void* data_ptr, aclDataType dtype,
size_t type_size, int64_t* ne, size_t* nb,
int64_t dims, aclFormat format = ACL_FORMAT_ND, size_t offset = 0);
int64_t dims, aclFormat format = ACL_FORMAT_ND,
size_t offset = 0);

bool need_bcast(const ggml_tensor* t0, const ggml_tensor* t1);

Expand All @@ -35,4 +37,4 @@ int64_t get_bcast_shape(const ggml_tensor* src0, const ggml_tensor* src1,

#define BCAST_PARAM(src) bcast_ne_##src, bcast_nb_##src, bcast_dims

#endif // CANN_ACL_TENSOR_H
#endif // CANN_ACL_TENSOR_H
Loading

0 comments on commit a5abdf7

Please sign in to comment.