# Copyright (c) 2023 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 copy import struct import unittest import numpy as np import paddle from paddle import nn from paddle.fluid import core from paddle.framework import in_dynamic_mode 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(' is expected to be {expected_value}, but recieved {actual_value}.", ) for op_type, expected_value in expected_fp16_calls.items(): # print(f"[FP16] op_type={op_type}, value={value}") if isinstance(op_stats_dict[op_type], str): actual_value = _extract_op_call(op_stats_dict[op_type], 0) else: actual_value = op_stats_dict[op_type].fp16_calls self.assertEqual( actual_value, expected_value, f"[debug_info] The number of fp16 calls of operator < {op_type} > is expected to be {expected_value}, but recieved {actual_value}.", ) def run_program( self, main_program, startup_program, optimizer, feed_vars, fetch_vars, place, exe, x_np, max_iters, dtype, level, ): losses = [] scope = paddle.static.Scope() with paddle.static.scope_guard(scope): exe.run(startup_program) if level == 'O2': optimizer.amp_init(place) for iter_id in range(max_iters): results = exe.run( program=main_program, feed={feed_vars[0].name: x_np}, fetch_list=fetch_vars, ) print( f"-- [AMP {dtype} {level}] iter={iter_id}, loss={results[0]}" ) losses.append(results[0]) return losses