printf

Function Usage

Implements the formatted output function in CPU- or NPU-side debugging for operators developed based on operator projects.

Call the printf API to print required log information at the target position in the operator kernel implementation code. An example is as follows.
1
2
3
#include "kernel_operator.h"
AscendC::printf("fmt string %d\n", 0x123);
AscendC::PRINTF("fmt string %d\n", 0x123);
The printing function of printf (PRINTF) affects the actual running performance of the operator. Therefore, this function is usually used in the commissioning phase. Developers can disable the printing function in either of the following ways:
  • Custom operator project
    Modify the CMakeLists.txt file in the op_kernel directory of the operator project. Add the compilation option -DASCENDC_DUMP=0 to the first line to disable ASCENDC_DUMP. The following is an example.
    1
    2
    // Disable the printf printing function of all operators.
    add_ops_compile_options(ALL OPTIONS -DASCENDC_DUMP=0)
    
  • Kernel launch project
    • Modify the npu_lib.cmake file in the cmake directory. Add the -DASCENDC_DUMP=0 macro definition to the ascendc_compile_definitions command to disable the ASCENDC_DUMP function on the NPU. The following is an example.
      1
      2
      3
      4
      // Disable the printf printing function of all operators.
      ascendc_compile_definitions(ascendc_kernels_${RUN_MODE} PRIVATE
          -DASCENDC_DUMP=0
      )
      
    • Modify the cpu_lib.cmake file in the cmake directory. Add the -DASCENDC_DUMP=0 macro definition to the target_compile_definitions command to disable the ASCENDC_DUMP switch on the CPU. The following is an example.
      1
      2
      3
      target_compile_definitions(ascendc_kernels_${RUN_MODE} PRIVATE
          -DASCENDC_DUMP=0
      )
      

      Note that disabling the printing function on the CPU takes effect only for PRINTF and does not take effect for printf.

The values of CANN_VERSION_STR and CANN_TIMESTAMP are automatically printed at the beginning of the printf result. CANN_VERSION_STR and CANN_TIMESTAMP are macro definitions. CANN_VERSION_STR indicates the version number of the CANN package in the form of a string. CANN_TIMESTAMP indicates the timestamp when the CANN package is released, the value is in the format of uint64_t. You can directly use the two macros in the code. The following is an example of the printf result:
1
2
3
CANN Version: XXX.XX, TimeStamp: 20240807140556417
fmt string 291
fmt string 291

The way of outputting the printf result varies according to the operator execution mode. In the dynamic graph or single-operator scenario, the content to be output is parsed and printed on the screen. In the static graph scenario, all operators of the graph need to be offloaded to the NPU for execution, and the information about a single operator cannot be printed by directly calling the API. In this case, after the model is executed, the content to be output needs to be flushed to the dump file, which is further parsed into readable content by using a tool.

  • The dump file paths are sorted by priority as follows:
    • If the data dump function is enabled, the dump file is stored in dump_path configured by the developer. Dump function enabling depends on the network run mode. The following uses TensorFlow online inference as an example to describe how to configure parameters such as enable_dump, dump_path, and dump_mode. For details about how to configure them, see API Reference > TF Adapter API (2.x) > npu.global_options > Configuration Options in TensorFlow 2.6.5 Model Porting Guide.
    • If the data dump function is disabled but the ASCEND_WORK_PATH environment variable is configured, the dump file is flushed to the printf directory in ASCEND_WORK_PATH. For details about how to configure the ASCEND_WORK_PATH environment variable, see ASCEND_WORK_PATH.
    • If the data dump function is disabled and the ASCEND_WORK_PATH environment variable is not configured, the dump file is flushed to the printf directory in the current execution directory.
  • The dump file needs to be parsed into readable content by using a tool.
    The show_kernel_debug_data tool can be used to parse a dump binary file into readable content. The command format is as follows: For details about how to use show_kernel_debug_data, see show_kernel_debug_data.
    show_kernel_debug_data  bin_file  output_dir

Prototype

1
2
void printf(__gm__ const char* fmt, Args&&... args)
void PRINTF(__gm__ const char* fmt, Args&&... args)

Parameters

Parameter

Input/Output

Description

fmt

Input

Format control character string, which contains two types of objects: common characters and conversion descriptions.

  • Common characters are printed as they are.
  • Conversion descriptions are not directly output. Instead, they are used to control the conversion and printing of parameters in printf. Each conversion description starts with a percent (%) character and ends with the specific conversion description, indicating the type of the output data.
    The following conversion types are supported:
    • %d / %i: outputs decimal numbers. The data types that can be printed are bool/int8_t/int16_t/int32_t/int64_t.
    • %f: outputs real numbers. The data types that can be printed are float/half/bfloat16_t.
    • %x: outputs hexadecimal integers. The data types that can be printed are int8_t/int16_t/int32_t/int64_t/uint8_t/uint16_t/uint32_t/uint64_t.
    • %s: outputs character strings.
    • %u: outputs unsigned data. The data types that can be printed are bool/uint8_t/uint16_t/uint32_t/uint64_t.
    • %p: outputs pointer addresses.

args

Input

Additional parameters (an output list with variable quantities and types). Depending on the fmt string, the function may require a series of additional parameters. Each parameter contains a value to be inserted and replaces each % tag specified in the fmt parameter. The number of parameters must be the same as the number of % tags.

Returns

None

Availability

Precautions

  • This API does not allow the printing of escape characters, except for newline characters.
  • If developers need to include the standard library header files stdio.h and cstdio, include them before the kernel_operator.h header file to avoid printf symbol conflicts.
  • The sum size of the space used by the printf/PRINTF call, DumpTensor call, DumpAccChkPoint call, assert call, and framework dump function cannot exceed 1 MB on each core. Developers need to control the amount of data to be printed. If the limit is exceeded, no content will be printed.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include "kernel_operator.h"

// Integer printing:
AscendC::printf("fmt string %d\n", 0x123);
AscendC::PRINTF("fmt string %d\n", 0x123);

// Floating-point printing:
float a = 3.14;
AscendC::printf("fmt string %f\n", a);
AscendC::PRINTF("fmt string %f\n", a);

// Pointer printing:
int *a;
AscendC::printf("TEST %p\n", a);
AscendC::PRINTF("TEST %p\n", a);

When the program is running, you will see the following print effect:

1
2
3
4
5
6
fmt string 291
fmt string 291
fmt string 3.14
fmt string 3.14
TEST 0x12c08001a000
TEST 0x12c08001a000