fluid.profiler

cuda_profiler

paddle.fluid.profiler.cuda_profiler(output_file, output_mode=None, config=None)[source]

The CUDA profiler. This fuctions is used to profile CUDA program by CUDA runtime application programming interface. The profiling result will be written into output_file with Key-Value pair format or Comma separated values format. The user can set the output mode by output_mode argument and set the counters/options for profiling by config argument. The default config is [‘gpustarttimestamp’, ‘gpustarttimestamp’, ‘gridsize3d’, ‘threadblocksize’, ‘streamid’, ‘enableonstart 0’, ‘conckerneltrace’]. Then users can use NVIDIA Visual Profiler (https://developer.nvidia.com/nvidia-visual-profiler) tools to load this this output file to visualize results.

Parameters
  • output_file (string) – The output file name, the result will be written into this file.

  • output_mode (string) – The output mode has Key-Value pair format and Comma separated values format. It should be ‘kvp’ or ‘csv’.

  • config (list of string) – The profiler options and counters can refer to “Compute Command Line Profiler User Guide”.

Raises

ValueError – If output_mode is not in [‘kvp’, ‘csv’].

Examples

import paddle.fluid as fluid
import paddle.fluid.profiler as profiler
import numpy as np

epoc = 8
dshape = [4, 3, 28, 28]
data = fluid.layers.data(name='data', shape=[3, 28, 28], dtype='float32')
conv = fluid.layers.conv2d(data, 20, 3, stride=[1, 1], padding=[1, 1])

place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

output_file = 'cuda_profiler.txt'
with profiler.cuda_profiler(output_file, 'csv') as nvprof:
    for i in range(epoc):
        input = np.random.random(dshape).astype('float32')
        exe.run(fluid.default_main_program(), feed={'data': input})
# then use  NVIDIA Visual Profiler (nvvp) to load this output file
# to visualize results.

profiler

paddle.fluid.profiler.profiler(state, sorted_key=None, profile_path='/tmp/profile')[source]

The profiler interface. Different from cuda_profiler, this profiler can be used to profile both CPU and GPU program. By default, it records the CPU and GPU operator kernels, if you want to profile other program, you can refer the profiling tutorial to add more records in C++ code.

If the state == ‘All’, a profile proto file will be written to profile_path. This file records timeline information during the execution. Then users can visualize this file to see the timeline, please refer https://github.com/PaddlePaddle/Paddle/blob/develop/doc/fluid/howto/optimization/timeline.md

Parameters
  • state (string) – The profiling state, which should be ‘CPU’ or ‘GPU’, telling the profiler to use CPU timer or GPU timer for profiling. Although users may have already specified the execution place (CPUPlace/CUDAPlace) in the beginning, for flexibility the profiler would not inherit this place.

  • sorted_key (string) – If None, the profiling results will be printed in the order of first end time of events. Otherwise, the profiling results will be sorted by the this flag. This flag should be one of ‘calls’, ‘total’, ‘max’, ‘min’ or ‘ave’. The calls means sorting by the number of calls. The total means sorting by the total execution time. The max means sorting by the maximum execution time. The min means sorting by the minimum execution time. The ave means sorting by the average execution time.

  • profile_path (string) – If state == ‘All’, it will write a profile proto output file.

Raises

ValueError – If state is not in [‘CPU’, ‘GPU’, ‘All’]. If sorted_key is not in [‘calls’, ‘total’, ‘max’, ‘min’, ‘ave’].

Examples

import paddle.fluid as fluid
import paddle.fluid.profiler as profiler
import numpy as np

epoc = 8
dshape = [4, 3, 28, 28]
data = fluid.layers.data(name='data', shape=[3, 28, 28], dtype='float32')
conv = fluid.layers.conv2d(data, 20, 3, stride=[1, 1], padding=[1, 1])

place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

with profiler.profiler('CPU', 'total', '/tmp/profile') as prof:
    for i in range(epoc):
        input = np.random.random(dshape).astype('float32')
        exe.run(fluid.default_main_program(), feed={'data': input})

reset_profiler

paddle.fluid.profiler.reset_profiler()[source]

Clear the previous time record. This interface does not work for fluid.profiler.cuda_profiler, it only works for fluid.profiler.start_profiler, fluid.profiler.stop_profiler, and fluid.profiler.profiler.

Examples

import paddle.fluid as fluid
import paddle.fluid.profiler as profiler
with profiler.profiler('CPU', 'total', '/tmp/profile'):
    for iter in range(10):
        if iter == 2:
            profiler.reset_profiler()
        # ...

start_profiler

paddle.fluid.profiler.start_profiler(state)[source]

Enable the profiler. Uers can use fluid.profiler.start_profiler and fluid.profiler.stop_profiler to insert the code, except the usage of fluid.profiler.profiler interface.

Parameters

state (string) – The profiling state, which should be ‘CPU’, ‘GPU’ or ‘All’. ‘CPU’ means only profile CPU. ‘GPU’ means profiling GPU as well. ‘All’ also generates timeline.

Raises

ValueError – If state is not in [‘CPU’, ‘GPU’, ‘All’].

Examples

import paddle.fluid as fluid
import paddle.fluid.profiler as profiler

profiler.start_profiler('GPU')
for iter in range(10):
    if iter == 2:
        profiler.reset_profiler()
    # except each iteration
profiler.stop_profiler('total', '/tmp/profile')

stop_profiler

paddle.fluid.profiler.stop_profiler(sorted_key=None, profile_path='/tmp/profile')[source]

Stop the profiler. Uers can use fluid.profiler.start_profiler and fluid.profiler.stop_profiler to insert the code, except the usage of fluid.profiler.profiler interface.

Parameters
  • sorted_key (string) – If None, the profiling results will be printed in the order of first end time of events. Otherwise, the profiling results will be sorted by the this flag. This flag should be one of ‘calls’, ‘total’, ‘max’, ‘min’ or ‘ave’. The calls means sorting by the number of calls. The total means sorting by the total execution time. The max means sorting by the maximum execution time. The min means sorting by the minimum execution time. The ave means sorting by the average execution time.

  • profile_path (string) – If state == ‘All’, it will write a profile proto output file.

Raises

ValueError – If sorted_key is not in [‘calls’, ‘total’, ‘max’, ‘min’, ‘ave’].

Examples

import paddle.fluid as fluid
import paddle.fluid.profiler as profiler

profiler.start_profiler('GPU')
for iter in range(10):
    if iter == 2:
        profiler.reset_profiler()
    # except each iteration
profiler.stop_profiler('total', '/tmp/profile')