test_profiler.py 2.6 KB
Newer Older
1
import unittest
2
import os
3 4
import numpy as np
import paddle.v2.fluid as fluid
D
dangqingqing 已提交
5 6
import paddle.v2.fluid.profiler as profiler
import paddle.v2.fluid.layers as layers
7
import paddle.v2.fluid.core as core
D
dangqingqing 已提交
8 9


10 11 12 13 14 15 16 17 18
class TestProfiler(unittest.TestCase):
    def test_nvprof(self):
        if not fluid.core.is_compile_gpu():
            return
        epoc = 8
        dshape = [4, 3, 28, 28]
        data = layers.data(name='data', shape=[3, 28, 28], dtype='float32')
        conv = layers.conv2d(data, 20, 3, stride=[1, 1], padding=[1, 1])

D
dzhwinter 已提交
19
        place = fluid.CUDAPlace(0)
20 21 22
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())

23 24
        output_file = 'cuda_profiler.txt'
        with profiler.cuda_profiler(output_file, 'csv') as nvprof:
25
            for i in range(epoc):
D
dangqingqing 已提交
26
                input = np.random.random(dshape).astype('float32')
27
                exe.run(fluid.default_main_program(), feed={'data': input})
28
        os.remove(output_file)
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    def test_profiler(self):
        image = fluid.layers.data(name='x', shape=[784], dtype='float32')
        hidden1 = fluid.layers.fc(input=image, size=128, act='relu')
        hidden2 = fluid.layers.fc(input=hidden1, size=64, act='relu')
        predict = fluid.layers.fc(input=hidden2, size=10, act='softmax')
        label = fluid.layers.data(name='y', shape=[1], dtype='int64')
        cost = fluid.layers.cross_entropy(input=predict, label=label)
        avg_cost = fluid.layers.mean(x=cost)
        optimizer = fluid.optimizer.Momentum(learning_rate=0.001, momentum=0.9)
        opts = optimizer.minimize(avg_cost)
        accuracy = fluid.evaluator.Accuracy(input=predict, label=label)

        states = ['CPU', 'GPU'] if core.is_compile_gpu() else ['CPU']
        for state in states:
            place = fluid.CPUPlace() if state == 'CPU' else fluid.CUDAPlace(0)
            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())

            accuracy.reset(exe)

            with profiler.profiler(state, 'total') as prof:
                for iter in range(10):
                    if iter == 2:
                        profiler.reset_profiler()
                    x = np.random.random((32, 784)).astype("float32")
                    y = np.random.randint(0, 10, (32, 1)).astype("int64")

                    outs = exe.run(fluid.default_main_program(),
                                   feed={'x': x,
                                         'y': y},
                                   fetch_list=[avg_cost] + accuracy.metrics)
                    acc = np.array(outs[1])
                    pass_acc = accuracy.eval(exe)

D
dangqingqing 已提交
64

65 66
if __name__ == '__main__':
    unittest.main()