# Copyright (c) 2021 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 contextlib import struct import unittest import numpy as np from amp_base_models import AmpTestBase, build_add_model, build_embedding_model import paddle from paddle import fluid from paddle.fluid import core from paddle.static import amp paddle.enable_static() def copy_bits_from_float_to_uint16(f): return struct.unpack('> 16 def convert_float_to_uint16(in_list): if in_list.dtype == np.float32: new_output = [] for x in np.nditer(in_list): new_output.append(np.uint16(copy_bits_from_float_to_uint16(x))) new_output = np.reshape(new_output, in_list.shape).view(np.uint16) return new_output else: return in_list def convert_uint16_to_float(in_list): if in_list.dtype == np.uint16: in_list = np.asarray(in_list) out = np.vectorize( lambda x: struct.unpack('