# Copyright (c) 2018 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. from __future__ import print_function import paddle import paddle.fluid as fluid import paddle.static.amp as amp import contextlib import numpy import unittest import math import sys import os import struct paddle.enable_static() def convert_uint16_to_float(in_list): in_list = numpy.asarray(in_list) out = numpy.vectorize( lambda x: struct.unpack('= 0 batch_size = 10 test_reader = paddle.batch( paddle.dataset.uci_housing.test(), batch_size=batch_size) test_data = next(test_reader()) test_feat = numpy.array( [data[0] for data in test_data]).astype("float32") test_label = numpy.array( [data[1] for data in test_data]).astype("float32") assert feed_target_names[0] == 'x' results = exe.run(inference_program, feed={feed_target_names[0]: numpy.array(test_feat)}, fetch_list=fetch_targets) if results[0].dtype == numpy.uint16: results[0] = convert_uint16_to_float(results[0]) print("infer shape: ", results[0].shape) print("infer results: ", results[0]) print("ground truth: ", test_label) def main(use_cuda, is_local=True, use_bf16=False, pure_bf16=False): if use_cuda and not fluid.core.is_compiled_with_cuda(): return if use_bf16 and not fluid.core.is_compiled_with_mkldnn(): return # Directory for saving the trained model save_dirname = "fit_a_line.inference.model" train(use_cuda, save_dirname, is_local, use_bf16, pure_bf16) infer(use_cuda, save_dirname, use_bf16) class TestFitALineBase(unittest.TestCase): @contextlib.contextmanager def program_scope_guard(self): prog = fluid.Program() startup_prog = fluid.Program() scope = fluid.core.Scope() with fluid.scope_guard(scope): with fluid.program_guard(prog, startup_prog): yield class TestFitALine(TestFitALineBase): def test_cpu(self): with self.program_scope_guard(): main(use_cuda=False) def test_cuda(self): with self.program_scope_guard(): main(use_cuda=True) @unittest.skipIf(not fluid.core.supports_bfloat16(), "place does not support BF16 evaluation") class TestFitALineBF16(TestFitALineBase): def test_bf16(self): with self.program_scope_guard(): main(use_cuda=False, use_bf16=True) def test_pure_bf16(self): with self.program_scope_guard(): main(use_cuda=False, use_bf16=True, pure_bf16=True) if __name__ == '__main__': unittest.main()