printf
Supported Products
Product |
Supported/Unsupported |
|---|---|
√ |
|
√ |
|
√ |
|
√ |
|
x |
|
x |
Functions
This API provides the formatted output function in the CPU domain or NPU domain debugging scenario.
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 debugging phase. You can disable the printing function by setting ASCENDC_DUMP to 0.
Prototype
1 2 3 4 | template <class... Args> __aicore__ inline void printf(__gm__ const char* fmt, Args&&... args) template <class... Args> __aicore__ inline 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.
Note:
|
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. |
Returns
None
Restrictions
- 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.
- For a single call, the total amount of data printed by this API cannot exceed 1 MB (including the header and tail information required by the framework, which can be ignored). If the data volume exceeds the limit, the data will not be printed. When using the custom operator project for operator development, the total amount of data dumped by all APIs that use the dump function of an operator on each core cannot exceed 1 MB. You need to control the amount of data to be printed. If the limit is exceeded, no content will be printed.
- 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.
- 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 dump file paths are sorted by priority as follows:
- In the operator graph construction scenario, if a dynamic-shape model contains operators that can be offloaded, the framework splits the model into two parts: dynamic scheduling and offload scheduling (static subgraph). Operators in the static subgraph do not support the printf feature.
Examples
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 *b; AscendC::printf("TEST %p\n", b); AscendC::PRINTF("TEST %p\n", b); |
In NPU mode, the program prints the following information during execution (CANN Version and TimeStamp are printed only when the custom operator project is used):
1 2 3 4 5 6 7 | CANN Version: XXX.XX, TimeStamp: 202408 fmt string 291 fmt string 291 fmt string 3.140000 fmt string 3.140000 TEST 0x12c08001a000 TEST 0x12c08001a000 |