Introduction

Document Positioning

This document aims to help you quickly and correctly call built-in operators of CANN (Compute Architecture for Neural Networks).

These operators are highly optimized and work well with diverse types of hardware, speeding up network computations on AI processors.

The source code for built-in CANN operators is available in the CANN > Operator Library project on GitCode. This helps you learn about the community ecosystem and how to build or contribute operators.

Operator Library Architecture

Operator libraries are classified by function. Figure 1 shows where these libraries fit in the CANN architecture and how they depend on each other.

  • Math: numerical calculation operators, like Add and Abs, for use cases such as tensor shape transformation, basic mathematical operations, and random number generation.
  • NN: neural network operators, common in deep learning models, for use cases such as convolution, matrix multiplication, activation functions, and normalization.
  • CV: computer vision operators, such as GridSample, for image processing and object detection.
  • Transformer: foundation model computation operators in major classes such as Attention, LayerNorm, and merged compute and communication (MC2).
  • RAS: reliability, availability, and serviceability (RAS) operators.
  • Opbase: basic framework library on which all other operator libraries depend. It realizes basic scheduling (such as aclTensor creation/release and workspace reuse) and common dependencies.
Figure 1 Operator library architecture

Operators and Operator APIs

An operator is a basic functional unit in AI computing, much like a math function. For instance, the Add operator performs vector addition, while the Matmul operator handles matrix multiplication. You combine these operators to create complex neural networks.

In practice, you call operators through their APIs.

  • Operator: the underlying computation logic (for example, addition and matrix multiplication).
  • Operator API: the programming interface that an operator provides. When you write code on the host (CPU), you are calling these APIs, such as aclnn APIs and PyTorch APIs.

One operator may have multiple APIs. Using aclnn APIs as an example:

  • The Add operator offers the aclnnAdd and aclnnAdds APIs.
  • The MatMulV3 operator offers the aclnnMatmul and aclnnMatmulWeightNz APIs.

Calling Operators

You can call CANN operators in the following ways:

  • aclnn APIs: Provide a set of standard C APIs for all operators, making it easy to call them on the host. These single-operator APIs (or operator host APIs) are called as shown in Figure 2.
  • PyTorch APIs: Provide a set of torch_extension APIs that match the native PyTorch style for common but non-PyTorch native operators (such as those for neural networks and foundation models). These APIs use the Just-In-Time (JIT) (torch.utils.cpp_extension.load) mechanism to compile the C++ Kernel Wrapper in real time upon the first call, bridging PyTorch functions to aclnn APIs. In addition, they support the TorchAir graph mode through GE Converter.
  • Ascend IR (Intermediate Representation): Calls operators by constructing graphs. The Graph Engine (GE) converts operators from major AI frameworks into Ascend IRs uniformly, enabling faster computation on AI processors. For details about how the graph mode works, see Programming Guide in Graph Development.
Figure 2 aclnn API calling process

Instructions

This section guides you through the operator call process. Read this document in the following order if you are new to operators.

  1. Environment preparation: Refer to Header Files and Library Files to learn how to install the CANN package and the header files or library files required for calling operators.
  2. Concept understanding: Familiarize yourself with the terminology and key features (such as determinism and quantization modes) of operators by referring to Basic Concepts.
  3. API calling: Select a proper way as described in Calling Operators. For details about operators or operator APIs, see the following table.
    Table 1 Operator list

    API Category

    Guide

    Description

    Common APIs

    Common APIs

    Common meta APIs on which the aclnn APIs depend, such as those for creating aclTensor, aclScalar, and aclIntArray.

    Operator APIs (aclnn)

    Math APIs

    C APIs corresponding to mathematical operators such as Add and Abs.

    NN APIs

    C APIs corresponding to neural network operators such as Matmul.

    Currently, these operators account for the largest proportion in the operator library.

    CV APIs

    C APIs corresponding to computer vision operators such as GridSample.

    Transformer APIsTransformer APIs

    C APIs corresponding to foundation model computation operators such as FlashAttention, MC2, and MoE (Mixture of Experts).

    RAS APIs

    C APIs corresponding to RAS operators.

    Operator APIs (torch_extension)

    NN APIs (torch)

    PyTorch APIs for common but non-PyTorch native neural network operators such as Matmul.

    Transformer APIs (torch)

    PyTorch APIs for common but non-PyTorch native foundation model operators such as FlashAttention, MC2, and MoE.

    Ascend IR operator prototype

    Ascend IR Operator Specifications

    Operator information defined based on the Ascend IR.

  4. Related resources:
    • Appendix: FAQ about operator calls, performance tuning guide, and advanced API description.
      • nnopbase APIs on which the aclnn API development depends
      • op_common APIs on which operator development and calls depend
      • Operator execution performance tuning using static kernels
      • FAQ and cases for operator calls
    • : information about native IR operators of major AI frameworks (such as TensorFlow and Caffe).

Instructions

  • An operator or its API can have several versions. Always choose the latest one—newer versions work with older ones by default.
  • For unsupported use cases—like specific product models, data types, formats, or dimensions not listed here—we advise against using them. Skip because CANN cannot guarantee how they will work.
  • Ascend virtual instances: Fused operators made up of several independent basic operators—like vector or cube operations—are not supported. Although these complex operators, such as FlashAttention, perform the same tasks as multiple basic operators combined, they typically offer better performance.