test_switch_autotune.py 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle
import unittest
import numpy


class SimpleNet(paddle.nn.Layer):
    def __init__(self):
        super(SimpleNet, self).__init__()
        self.conv = paddle.nn.Conv2D(1, 2, (3, 3))

    def forward(self, image, label=None):
        return self.conv(image)


def train_dygraph(net, data):
    out = net(data)
    loss = paddle.mean(out)
    adam = paddle.optimizer.Adam(parameters=net.parameters())
    out.backward()
    adam.step()
    adam.clear_grad()


def static_program(net, data):
    out = net(data)
    loss = paddle.mean(out)
    adam = paddle.optimizer.Adam()
    adam.minimize(loss)
    return loss


46 47 48 49 50 51 52 53 54 55
def set_flags(enable_autotune):
    if paddle.is_compiled_with_cuda():
        if enable_autotune:
            paddle.set_flags({'FLAGS_conv_workspace_size_limit': -1})
            paddle.set_flags({'FLAGS_cudnn_exhaustive_search': 1})
        else:
            paddle.set_flags({'FLAGS_conv_workspace_size_limit': 512})
            paddle.set_flags({'FLAGS_cudnn_exhaustive_search': 0})


56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
class TestAutoTune(unittest.TestCase):
    def test_autotune(self):
        paddle.fluid.core.disable_autotune()
        status = paddle.fluid.core.autotune_status()
        self.assertEqual(status["use_autotune"], False)

        paddle.fluid.core.enable_autotune()
        status = paddle.fluid.core.autotune_status()
        self.assertEqual(status["use_autotune"], True)

    def check_status(self, expected_res):
        status = paddle.fluid.core.autotune_status()
        for key in status.keys():
            self.assertEqual(status[key], expected_res[key])


class TestDygraphAutoTuneStatus(TestAutoTune):
    def run_program(self, enable_autotune):
74
        set_flags(enable_autotune)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        if enable_autotune:
            paddle.fluid.core.enable_autotune()
        else:
            paddle.fluid.core.disable_autotune()
        paddle.fluid.core.autotune_range(1, 2)
        x_var = paddle.uniform((1, 1, 8, 8), dtype='float32', min=-1., max=1.)
        net = SimpleNet()
        for i in range(3):
            train_dygraph(net, x_var)
            if i >= 1 and i < 2:
                expected_res = {
                    "step_id": i,
                    "use_autotune": enable_autotune,
                    "cache_size": 0,
                    "cache_hit_rate": 0
                }
                self.check_status(expected_res)
            else:
                expected_res = {
                    "step_id": i,
                    "use_autotune": False,
                    "cache_size": 0,
                    "cache_hit_rate": 0
                }
                self.check_status(expected_res)

J
Jiabin Yang 已提交
101
    def func_enable_autotune(self):
102 103
        self.run_program(enable_autotune=True)

J
Jiabin Yang 已提交
104 105 106 107 108 109
    def test_enable_autotune(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_enable_autotune()
        self.func_enable_autotune()

    def func_disable_autotune(self):
110 111
        self.run_program(enable_autotune=False)

J
Jiabin Yang 已提交
112 113 114 115 116
    def test_disable_autotune(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_disable_autotune()
        self.func_disable_autotune()

117 118 119 120

class TestStaticAutoTuneStatus(TestAutoTune):
    def run_program(self, enable_autotune):
        paddle.enable_static()
121
        set_flags(enable_autotune)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        if enable_autotune:
            paddle.fluid.core.enable_autotune()
        else:
            paddle.fluid.core.disable_autotune()
        paddle.fluid.core.autotune_range(1, 2)

        data_shape = [1, 1, 8, 8]
        data = paddle.static.data(name='X', shape=data_shape, dtype='float32')
        net = SimpleNet()
        loss = static_program(net, data)
        place = paddle.CUDAPlace(0) if paddle.fluid.core.is_compiled_with_cuda(
        ) else paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        exe.run(paddle.static.default_startup_program())
        x = numpy.random.random(size=data_shape).astype('float32')

        for i in range(3):
            exe.run(feed={'X': x}, fetch_list=[loss])
            status = paddle.fluid.core.autotune_status()
            # In static mode, the startup_program will run at first.
            # The expected step_id will be increased by 1.
            if i >= 0 and i < 1:
                expected_res = {
                    "step_id": i + 1,
                    "use_autotune": enable_autotune,
                    "cache_size": 0,
                    "cache_hit_rate": 0
                }
                self.check_status(expected_res)
            else:
                expected_res = {
                    "step_id": i + 1,
                    "use_autotune": False,
                    "cache_size": 0,
                    "cache_hit_rate": 0
                }
                self.check_status(expected_res)
        paddle.disable_static()

J
Jiabin Yang 已提交
161
    def func_enable_autotune(self):
162 163
        self.run_program(enable_autotune=True)

J
Jiabin Yang 已提交
164 165 166 167 168 169
    def test_enable_autotune(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_enable_autotune()
        self.func_enable_autotune()

    def func_disable_autotune(self):
170 171
        self.run_program(enable_autotune=False)

J
Jiabin Yang 已提交
172 173 174 175 176
    def test_disable_autotune(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_disable_autotune()
        self.func_disable_autotune()

177 178 179

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