Implementation:Ggml org Ggml Cann acl tensor
Metadata
| Field | Value |
|---|---|
| Page Type | Implementation (Utility Header) |
| Knowledge Sources | GGML |
| Domains | ML_Infrastructure, Tensor_Computing, NPU_Computing |
| Last Updated | 2026-02-10 12:00 GMT |
Overview
Declares RAII smart pointer types and factory functions for converting GGML tensors to Huawei ACL (Ascend Computing Language) tensor objects.
Description
acl_tensor.h provides the bridge between GGML's tensor representation and the ACL tensor API required by Huawei Ascend NPU operators. It defines:
- Type mapping:
ggml_cann_type_mapping()convertsggml_typeenums to their correspondingaclDataTypevalues. Unsupported types map toACL_DT_UNDEFINED.
- RAII smart pointers: A templated
acl_deleterstruct automatically invokes the correct ACL destroy function when an object goes out of scope. Four smart pointer aliases are defined:acl_tensor_ptr-- wrapsaclTensor*(destroyed viaaclDestroyTensor)acl_int_array_ptr-- wrapsaclIntArray*(destroyed viaaclDestroyIntArray)acl_scalar_ptr-- wrapsaclScalar*(destroyed viaaclDestroyScalar)acl_tensor_list_ptr-- wrapsaclTensorList*(destroyed viaaclDestroyTensorList)
- Tensor creation: Two overloads of
ggml_cann_create_tensor():- One that takes a
ggml_tensor*and optional custom shape/stride/offset parameters for broadcast support. - A template version that takes raw data pointer, ACL data type, dimensions, and strides directly.
- One that takes a
Both overloads reverse dimension and stride ordering (GGML uses row-major innermost-first; ACL uses column-major outermost-first) and compute the ACL storage length.
Usage
Use these utilities whenever creating ACL tensors for ACLNN operator dispatch in the CANN backend. The smart pointers ensure ACL resources are automatically freed even when exceptions or early returns occur.
Code Reference
Source Location
GGML repo, file: src/ggml-cann/acl_tensor.h, 349 lines.
Signature
// Type mapping
aclDataType ggml_cann_type_mapping(ggml_type type);
// Tensor creation from ggml_tensor
acl_tensor_ptr ggml_cann_create_tensor(const ggml_tensor * tensor,
int64_t * ne = nullptr,
size_t * nb = nullptr,
int64_t dims = 0,
aclFormat format = ACL_FORMAT_ND,
size_t offset = 0);
// Template tensor creation from raw parameters
template <typename TYPE>
acl_tensor_ptr ggml_cann_create_tensor(void * data_ptr, aclDataType dtype,
TYPE type_size, int64_t * ne,
TYPE * nb, int64_t dims,
aclFormat format = ACL_FORMAT_ND,
size_t offset = 0);
Import
#include "acl_tensor.h"
Dependencies
common.h-- CANN backend shared types and macrosaclnn/aclnn_base.h-- ACL neural network base types
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
tensor |
const ggml_tensor * |
Yes | Source GGML tensor with data pointer, shape, and strides. |
ne |
int64_t * |
No | Custom dimension array for broadcast reshaping. Defaults to tensor's own dimensions. |
nb |
size_t * |
No | Custom stride array for broadcast reshaping. Defaults to tensor's own strides. |
dims |
int64_t |
No | Number of dimensions (0 means use GGML_MAX_DIMS). |
format |
aclFormat |
No | ACL tensor format (default: ACL_FORMAT_ND).
|
offset |
size_t |
No | Byte offset into the data buffer (default: 0). |
Outputs
| Output | Type | Description |
|---|---|---|
| ACL tensor | acl_tensor_ptr |
RAII-managed ACL tensor object ready for use with ACLNN operators. |
| ACL data type | aclDataType |
From ggml_cann_type_mapping(): the ACL equivalent of the given GGML type.
|
Usage Examples
Creating an ACL Tensor from a GGML Tensor
#include "acl_tensor.h"
// Simple conversion using tensor's own shape
acl_tensor_ptr acl_src = ggml_cann_create_tensor(src_tensor);
// With custom broadcast shape
int64_t bcast_ne[] = {ne00, ne01, ne12, ne13};
size_t bcast_nb[] = {nb00, nb01, nb02, nb03};
acl_tensor_ptr acl_bcast = ggml_cann_create_tensor(src_tensor,
bcast_ne, bcast_nb, 4);
Using Smart Pointers for Resource Safety
{
acl_tensor_ptr a = ggml_cann_create_tensor(src0);
acl_tensor_ptr b = ggml_cann_create_tensor(src1);
// ACL tensors are automatically destroyed when leaving scope
}