目录

printf

Function Description

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. Example:

#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 runtime performance of the operator. Therefore, this function is usually used in the debugging 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.

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

The following is an example of the printf result:

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 direct tuning scenario, the content to be output will be 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.

Function Prototype

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 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 supported conversion types are as follows:

- %d / %i: outputs decimal numbers. Supported data types: bool, int8_t, int16_t, int32_t, and int64_t.

- %f: outputs real numbers. Supported data types: float, half, and bfloat16_t.

- %x: outputs hexadecimal integers. Supported data types: int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, and uint64_t.

- %s: outputs a string.

- %u: outputs unsigned data. Supported data types: bool, uint8_t, uint16_t, uint32_t, and uint64_t.

- %p: outputs the pointer address.

args Input Additional arguments (an output list with variable quantities and types). Depending on the fmt string, the function may require a series of additional arguments. Each argument 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.

Return Value

None

Supported Models

Kirin 9020 series processors

Kirin 9030 series processors

Kirin X90 series processors

Precautions

Calling Example

#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:

fmt string 291
fmt string 291
TEST 0x12c08001a000
TEST 0x12c08001a000