diff --git a/src/onnx/onnx_parser.cpp b/src/onnx/onnx_parser.cpp index d7fd8367f2f..cd01b76e7ed 100644 --- a/src/onnx/onnx_parser.cpp +++ b/src/onnx/onnx_parser.cpp @@ -687,7 +687,7 @@ shape::type_t get_type(int dtype) case 22: return shape::int8_type; case 14: case 15: - case 16: + case 16: return shape::bf16_type; case 17: case 19: case 20: @@ -700,7 +700,7 @@ shape::type_t get_type(int dtype) bool is_type_float(shape::type_t dtype) { bool r = false; - if(dtype == shape::float_type or dtype == shape::double_type or dtype == shape::half_type) + if(dtype == shape::float_type or dtype == shape::double_type or dtype == shape::half_type or dtype == shape::bf16_type) { r = true; } diff --git a/src/onnx/parse_instancenorm.cpp b/src/onnx/parse_instancenorm.cpp index 6c2f79134e0..d1c7156aeb1 100644 --- a/src/onnx/parse_instancenorm.cpp +++ b/src/onnx/parse_instancenorm.cpp @@ -36,7 +36,7 @@ namespace onnx { struct parse_instancenorm : op_parser { - std::set valid_types = {shape::float_type, shape::half_type, shape::double_type}; + std::set valid_types = {shape::float_type, shape::half_type, shape::double_type, shape::bf16_type}; std::vector operators() const { return {{"InstanceNormalization"}}; } diff --git a/test/onnx/add_bf16_test.onnx b/test/onnx/add_bf16_test.onnx new file mode 100644 index 00000000000..36e4efefd46 --- /dev/null +++ b/test/onnx/add_bf16_test.onnx @@ -0,0 +1,16 @@ +  add_bf16_test:R + +0 +12"Add add_bf16_testZ +0 + + +Z +1 + + +b +2 + + +B \ No newline at end of file diff --git a/test/onnx/eyelike_bf16_test.onnx b/test/onnx/eyelike_bf16_test.onnx new file mode 100644 index 00000000000..9cdef15cb63 --- /dev/null +++ b/test/onnx/eyelike_bf16_test.onnx @@ -0,0 +1,11 @@ + eyelike_bf16_test:R + +T1T2"EyeLikeeyelike_bf16_testZ +T1 +  + +b +T2 +  + +B \ No newline at end of file diff --git a/test/onnx/gemm_bf16_test.onnx b/test/onnx/gemm_bf16_test.onnx new file mode 100644 index 00000000000..83e839eeec5 Binary files /dev/null and b/test/onnx/gemm_bf16_test.onnx differ diff --git a/test/onnx/gen_onnx.py b/test/onnx/gen_onnx.py index 05713398294..0a5452d1440 100644 --- a/test/onnx/gen_onnx.py +++ b/test/onnx/gen_onnx.py @@ -138,6 +138,20 @@ def add_fp8_test(): return ([node], [x, y], [z]) +@onnx_test() +def add_bf16_test(): + x = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [1]) + y = helper.make_tensor_value_info('1', TensorProto.BFLOAT16, [1]) + z = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [1]) + + node = onnx.helper.make_node( + 'Add', + inputs=['0', '1'], + outputs=['2'], + ) + + return ([node], [x, y], [z]) + @onnx_test() def add_scalar_test(): @@ -3554,6 +3568,17 @@ def eyelike_half_test(): ) return ([node], [T1], [T2]) +@onnx_test() +def eyelike_bf16_test(): + T1 = helper.make_tensor_value_info('T1', TensorProto.BFLOAT16, [8, 8]) + T2 = helper.make_tensor_value_info('T2', TensorProto.BFLOAT16, [8, 8]) + + node = onnx.helper.make_node( + 'EyeLike', + inputs=['T1'], + outputs=['T2'], + ) + return ([node], [T1], [T2]) @onnx_test() def eyelike_k_test(): @@ -3834,6 +3859,15 @@ def gelu_default_half_test(): return ([node], [x], [y]) +# @onnx_test() +# def gelu_default_bf16_test(): +# x = helper.make_tensor_value_info('x', TensorProto.BFLOAT16, [3, 3]) +# y = helper.make_tensor_value_info('y', TensorProto.BFLOAT16, [3, 3]) + +# node = onnx.helper.make_node("Gelu", inputs=["x"], outputs=["y"]) + +# return ([node], [x], [y]) + @onnx_test() def gelu_tanh_test(): @@ -4004,6 +4038,21 @@ def gemm_half_test(): return ([node], [A, B, C], [Y]) +@onnx_test() +def gemm_bf16_test(): + A = helper.make_tensor_value_info('A', TensorProto.BFLOAT16, [8, 6]) + B = helper.make_tensor_value_info('B', TensorProto.BFLOAT16, [8, 7]) + C = helper.make_tensor_value_info('C', TensorProto.BFLOAT16, [6, 1]) + Y = helper.make_tensor_value_info('Y', TensorProto.BFLOAT16, [6, 7]) + + node = onnx.helper.make_node('Gemm', + inputs=['A', 'B', 'C'], + outputs=['Y'], + alpha=0.5, + beta=0.8, + transA=1) + + return ([node], [A, B, C], [Y]) @onnx_test() def gemm_fp8_test(): @@ -4298,6 +4347,24 @@ def gridsample_half_test(): return ([node], [x, grid], [y]) +@onnx_test() +def gridsample_bf16_test(): + x = helper.make_tensor_value_info('x', TensorProto.BFLOAT16, [1, 1, 4, 4]) + grid = helper.make_tensor_value_info('grid', TensorProto.FLOAT, + [1, 6, 6, 2]) + y = helper.make_tensor_value_info('y', TensorProto.BFLOAT16, [1, 1, 6, 6]) + + node = onnx.helper.make_node( + "GridSample", + inputs=["x", "grid"], + outputs=["y"], + mode="linear", + padding_mode="zeros", + align_corners=0, + ) + + return ([node], [x, grid], [y]) + @onnx_test() def gridsample_int_test(): x = helper.make_tensor_value_info('x', TensorProto.INT32, [1, 1, 4, 4]) @@ -4661,6 +4728,11 @@ def group_norm_3d_half_test(): 2, dtype=TensorProto.FLOAT16) +@onnx_test() +def group_norm_3d_bf16_test(): + return group_norm_test([1, 4, 2], [2], [2], [1, 4, 2], + 2, + dtype=TensorProto.BFLOAT16) @onnx_test() def group_norm_4d_test(): @@ -4673,6 +4745,11 @@ def group_norm_4d_half_test(): 2, dtype=TensorProto.FLOAT16) +@onnx_test() +def group_norm_4d_bf16_test(): + return group_norm_test([1, 4, 3, 3], [2], [2], [1, 4, 3, 3], + 2, + dtype=TensorProto.BFLOAT16) @onnx_test() def group_norm_5d_test(): @@ -4685,6 +4762,12 @@ def group_norm_5d_half_test(): 1, dtype=TensorProto.FLOAT16) +@onnx_test() +def group_norm_5d_bf16_test(): + return group_norm_test([3, 3, 3, 3, 3], [1], [1], [3, 3, 3, 3, 3], + 1, + dtype=TensorProto.BFLOAT16) + @onnx_test() def group_norm_small_eps_half_test(): @@ -4693,6 +4776,12 @@ def group_norm_small_eps_half_test(): eps_value=1e-12, dtype=TensorProto.FLOAT16) +@onnx_test() +def group_norm_small_eps_bf16_test(): + return group_norm_test([1, 4, 2], [2], [2], [1, 4, 2], + 2, + eps_value=1e-7, + dtype=TensorProto.BFLOAT16) @onnx_test() def group_norm_invalid_num_groups_error_test(): @@ -5145,6 +5234,15 @@ def hardsigmoid_half_test(): return ([node], [x], [y]) +@onnx_test() +def hardsigmoid_bf16_test(): + x = helper.make_tensor_value_info('x', TensorProto.BFLOAT16, [1, 3, 4, 5]) + y = helper.make_tensor_value_info('y', TensorProto.BFLOAT16, [1, 3, 4, 5]) + + node = onnx.helper.make_node('HardSigmoid', inputs=['x'], outputs=['y']) + + return ([node], [x], [y]) + @onnx_test() def hardsigmoid_verify_test(): x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [2, 5]) @@ -5875,6 +5973,21 @@ def imagescaler_half_test(): return ([node], [x], [y]) + +@onnx_test() +def imagescaler_bf16_test(): + x = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [1, 3, 16, 16]) + y = helper.make_tensor_value_info('1', TensorProto.BFLOAT16, [1, 3, 16, 16]) + + node = onnx.helper.make_node('ImageScaler', + inputs=['0'], + outputs=['1'], + bias=[0.01, 0.02, 0.03], + scale=0.5) + + return ([node], [x], [y]) + + @onnx_test() def implicit_add_bcast_test(): x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5]) @@ -5970,6 +6083,20 @@ def instance_norm_half_test(): return ([node], [x, scale, bias], [y]) +@onnx_test() +def instance_norm_bf16_test(): + x = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [1, 2, 3, 3]) + scale = helper.make_tensor_value_info('1', TensorProto.BFLOAT16, [2]) + bias = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [2]) + y = helper.make_tensor_value_info('3', TensorProto.BFLOAT16, [1, 2, 3, 3]) + + node = onnx.helper.make_node('InstanceNormalization', + inputs=['0', '1', '2'], + outputs=['3']) + + return ([node], [x, scale, bias], [y]) + + @onnx_test() def instance_norm_type_mismatch_test(): x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 2, 3, 3]) @@ -6016,6 +6143,22 @@ def instance_norm_dyn_batch_half_test(): return ([node], [x, scale, bias], [y]) +@onnx_test() +def instance_norm_dyn_batch_bf16_test(): + # the batch size is a dynamic dimension + x = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, + [None, 2, 3, 3]) + scale = helper.make_tensor_value_info('1', TensorProto.BFLOAT16, [2]) + bias = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [2]) + y = helper.make_tensor_value_info('3', TensorProto.BFLOAT16, + [None, 2, 3, 3]) + + node = onnx.helper.make_node('InstanceNormalization', + inputs=['0', '1', '2'], + outputs=['3']) + + return ([node], [x, scale, bias], [y]) + @onnx_test() def instance_norm_invalid_type_test(): x = helper.make_tensor_value_info('0', TensorProto.INT32, [1, 2, 3, 3]) @@ -6301,6 +6444,19 @@ def isinf_half_test(): return ([node], [t1], [t2]) +@onnx_test() +def isinf_bf16_test(): + t1 = helper.make_tensor_value_info('t1', TensorProto.BFLOAT16, [2, 3]) + t2 = helper.make_tensor_value_info('t2', TensorProto.BOOL, [2, 3]) + + node = onnx.helper.make_node( + 'IsInf', + inputs=['t1'], + outputs=['t2'], + ) + return ([node], [t1], [t2]) + + @onnx_test() def isinf_neg_test(): t1 = helper.make_tensor_value_info('t1', TensorProto.FLOAT, [2, 3]) @@ -6371,6 +6527,17 @@ def isnan_half_test(): ) return ([node], [t1], [t2]) +@onnx_test() +def isnan_bf16_test(): + t1 = helper.make_tensor_value_info('t1', TensorProto.BFLOAT16, [2, 3]) + t2 = helper.make_tensor_value_info('t2', TensorProto.BFLOAT16, [2, 3]) + + node = onnx.helper.make_node( + 'IsNaN', + inputs=['t1'], + outputs=['t2'], + ) + return ([node], [t1], [t2]) @onnx_test() def layernorm_test(): @@ -6479,6 +6646,9 @@ def layer_norm_3d_test(): def layer_norm_3d_half_test(): return make_layer_norm([1, 4, 2], -1, TensorProto.FLOAT16) +@onnx_test() +def layer_norm_3d_bf16_test(): + return make_layer_norm([1, 4, 2], -1, TensorProto.BFLOAT16) @onnx_test() def layer_norm_4d_test(): @@ -6489,6 +6659,9 @@ def layer_norm_4d_test(): def layer_norm_4d_half_test(): return make_layer_norm([3, 3, 3, 3], -1, TensorProto.FLOAT16) +@onnx_test() +def layer_norm_4d_bf16_test(): + return make_layer_norm([3, 3, 3, 3], -1, TensorProto.BFLOAT16) @onnx_test() def layer_norm_invalid_axis_error_test(): @@ -6538,6 +6711,18 @@ def layer_norm_small_eps_half_test(): return ([node], [x, scale], [y]) +@onnx_test() +def layer_norm_small_eps_bf16_test(): + x = helper.make_tensor_value_info('x', TensorProto.BFLOAT16, [1, 2]) + scale = helper.make_tensor_value_info('scale', TensorProto.BFLOAT16, [2]) + y = helper.make_tensor_value_info('y', TensorProto.BFLOAT16, [1, 2]) + + node = onnx.helper.make_node('LayerNormalization', + inputs=['x', 'scale'], + outputs=['y'], + epsilon=1e-7) + + return ([node], [x, scale], [y]) @onnx_test() def leaky_relu_test(): @@ -7562,6 +7747,22 @@ def mean_fp16_test(): return ([node], [data_0, data_1, data_2], [mean]) +@onnx_test() +def mean_bf16_test(): + data_0 = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [1, 2, 3]) + data_1 = helper.make_tensor_value_info('1', TensorProto.BFLOAT16, [1, 2, 3]) + data_2 = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [1, 2, 3]) + + mean = helper.make_tensor_value_info('mean', TensorProto.BFLOAT16, + [1, 2, 3]) + + node = onnx.helper.make_node("Mean", + inputs=["0", "1", "2"], + outputs=["mean"]) + + return ([node], [data_0, data_1, data_2], [mean]) + + @onnx_test() def mean_invalid_broadcast_test(): data_0 = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 2, 3]) @@ -7634,6 +7835,9 @@ def mvn_default_axes_test(): def mvn_default_axes_fp16_test(): return mvn_default_axes_test_base([2, 2, 2, 2], TensorProto.FLOAT16) +@onnx_test() +def mvn_default_axes_bf16_test(): + return mvn_default_axes_test_base([2, 2, 2, 2], TensorProto.BFLOAT16) @onnx_test() def mvn_default_axes_rank_too_small_test(): @@ -7665,6 +7869,9 @@ def mvn_rank_2_test(): def mvn_rank_2_fp16_test(): return mvn_n_rank_test_base([1], [2, 2], TensorProto.FLOAT16) +@onnx_test() +def mvn_rank_2_bf16_test(): + return mvn_n_rank_test_base([1], [2, 2], TensorProto.BFLOAT16) @onnx_test() def mvn_rank_3_test(): @@ -7675,6 +7882,9 @@ def mvn_rank_3_test(): def mvn_rank_3_fp16_test(): return mvn_n_rank_test_base([0, 1], [2, 2, 2], TensorProto.FLOAT16) +@onnx_test() +def mvn_rank_3_bf16_test(): + return mvn_n_rank_test_base([0, 1], [2, 2, 2], TensorProto.BFLOAT16) @onnx_test() def mvn_axes_rank_too_small_test(): @@ -7723,6 +7933,15 @@ def mod_test_half(): return ([node], [a, b], [y]) +# @onnx_test() +# def mod_test_bf16(): +# a = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [3, 3, 3]) +# b = helper.make_tensor_value_info('1', TensorProto.BFLOAT16, [3, 3, 3]) +# y = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [3, 3, 3]) + +# node = onnx.helper.make_node('Mod', inputs=['0', '1'], outputs=['2']) + +# return ([node], [a, b], [y]) @onnx_test() def mod_test_different_dtypes(): @@ -7768,6 +7987,18 @@ def mod_test_fmod_half(): return ([node], [a, b], [y]) +@onnx_test() +def mod_test_fmod_bf16(): + a = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [3, 3, 3]) + b = helper.make_tensor_value_info('1', TensorProto.BFLOAT16, [3, 3, 3]) + y = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [3, 3, 3]) + + node = onnx.helper.make_node('Mod', + inputs=['0', '1'], + outputs=['2'], + fmod=1) + + return ([node], [a, b], [y]) @onnx_test() def mod_test_fmod_different_dtypes(): @@ -10953,6 +11184,14 @@ def round_half_test(): return ([node], [x], [y]) +@onnx_test() +def round_bf16_test(): + x = helper.make_tensor_value_info('x', TensorProto.BFLOAT16, [4, 4]) + y = helper.make_tensor_value_info('y', TensorProto.BFLOAT16, [4, 4]) + + node = onnx.helper.make_node('Round', inputs=['x'], outputs=['y']) + + return ([node], [x], [y]) def make_scatter_elements_test(reduction="none"): x = helper.make_tensor_value_info('data', TensorProto.FLOAT, [3, 4, 5, 6]) @@ -11480,6 +11719,16 @@ def size_half_test(): ) return ([node], [x], [y]) +@onnx_test() +def size_bf16_test(): + x = helper.make_tensor_value_info('x', TensorProto.BFLOAT16, [3, 1]) + y = helper.make_tensor_value_info('y', TensorProto.INT64, [1]) + node = onnx.helper.make_node( + 'Size', + inputs=['x'], + outputs=['y'], + ) + return ([node], [x], [y]) @onnx_test() def size_int_test(): @@ -12345,6 +12594,24 @@ def softmaxcrossentropyloss_2d_no_reduction_half_test(): return ([node], [scores, labels], [loss]) +@onnx_test() +def softmaxcrossentropyloss_2d_no_reduction_bf16_test(): + scores = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [4, 4]) + labels = helper.make_tensor_value_info('1', TensorProto.INT32, [4]) + loss = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [4]) + + node = onnx.helper.make_node( + "SoftmaxCrossEntropyLoss", + inputs=[ + "0", + "1", + ], + outputs=["2"], + reduction="none", + ) + + return ([node], [scores, labels], [loss]) + @onnx_test() def softmaxcrossentropyloss_2d_sum_reduction_test(): scores = helper.make_tensor_value_info('0', TensorProto.FLOAT, [4, 4]) @@ -12402,6 +12669,25 @@ def softmaxcrossentropyloss_2d_sum_reduction_half_test(): return ([node], [scores, labels], [loss]) +@onnx_test() +def softmaxcrossentropyloss_2d_sum_reduction_bf16_test(): + scores = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [4, 4]) + labels = helper.make_tensor_value_info('1', TensorProto.INT32, [4]) + loss = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [4]) + + node = onnx.helper.make_node( + "SoftmaxCrossEntropyLoss", + inputs=[ + "0", + "1", + ], + outputs=["2"], + reduction="sum", + ) + + return ([node], [scores, labels], [loss]) + + @onnx_test() def softmaxcrossentropyloss_2d_mean_reduction_test(): scores = helper.make_tensor_value_info('0', TensorProto.FLOAT, [4, 4]) @@ -12458,6 +12744,23 @@ def softmaxcrossentropyloss_2d_mean_reduction_half_test(): return ([node], [scores, labels], [loss]) +@onnx_test() +def softmaxcrossentropyloss_2d_mean_reduction_bf16_test(): + scores = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [4, 4]) + labels = helper.make_tensor_value_info('1', TensorProto.INT32, [4]) + loss = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [4]) + + node = onnx.helper.make_node( + "SoftmaxCrossEntropyLoss", + inputs=[ + "0", + "1", + ], + outputs=["2"], + reduction="mean", + ) + + return ([node], [scores, labels], [loss]) @onnx_test() def softmaxcrossentropyloss_2d_no_reduction_weighted_test(): @@ -12719,7 +13022,7 @@ def softmaxcrossentropyloss_2d_sum_reduction_double_weighted_test(): @onnx_test() -def softmaxcrossentropyloss_2d_sum_reduction_half_weighted_test(): +def softmaxcrossentropyloss_2d_sum_reduction_bf16_weighted_tes(): scores = helper.make_tensor_value_info('0', TensorProto.FLOAT16, [4, 4]) labels = helper.make_tensor_value_info('1', TensorProto.INT32, [4]) weights = helper.make_tensor_value_info('2', TensorProto.FLOAT16, [4]) @@ -12846,6 +13149,28 @@ def softmaxcrossentropyloss_kd_mean_reduction_half_weighted_test(): return ([node], [scores, labels, weights], [loss]) +@onnx_test() +def softmaxcrossentropyloss_kd_mean_reduction_bf16_weighted_test(): + scores = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, + [4, 4, 2, 2]) + labels = helper.make_tensor_value_info('1', TensorProto.INT32, [4, 2, 2]) + weights = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [4]) + loss = helper.make_tensor_value_info('3', TensorProto.BFLOAT16, [4]) + + node = onnx.helper.make_node( + "SoftmaxCrossEntropyLoss", + inputs=[ + "0", + "1", + "2", + ], + outputs=["3"], + reduction="mean", + ) + + return ([node], [scores, labels, weights], [loss]) + + @onnx_test() def softmaxcrossentropyloss_kd_sum_reduction_double_weighted_test(): scores = helper.make_tensor_value_info('0', TensorProto.DOUBLE, @@ -12912,6 +13237,28 @@ def negativeloglikelihoodloss_kd_mean_reduction_half_weighted_test(): return ([node], [scores, labels, weights], [loss]) +@onnx_test() +def negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test(): + scores = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, + [4, 4, 2, 2]) + labels = helper.make_tensor_value_info('1', TensorProto.INT32, [4, 2, 2]) + weights = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [4]) + loss = helper.make_tensor_value_info('3', TensorProto.BFLOAT16, [4]) + + node = onnx.helper.make_node( + "NegativeLogLikelihoodLoss", + inputs=[ + "0", + "1", + "2", + ], + outputs=["3"], + reduction="mean", + ) + + return ([node], [scores, labels, weights], [loss]) + + @onnx_test() def negativeloglikelihoodloss_kd_mean_reduction_half_weighted_test2(): scores = helper.make_tensor_value_info('0', TensorProto.FLOAT16, [2, 3, 2]) @@ -12933,6 +13280,28 @@ def negativeloglikelihoodloss_kd_mean_reduction_half_weighted_test2(): return ([node], [scores, labels, weights], [loss]) +@onnx_test() +def negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test2(): + scores = helper.make_tensor_value_info('0', TensorProto.BFLOAT16, [2, 3, 2]) + labels = helper.make_tensor_value_info('1', TensorProto.INT32, [2, 2]) + weights = helper.make_tensor_value_info('2', TensorProto.BFLOAT16, [3]) + loss = helper.make_tensor_value_info('3', TensorProto.BFLOAT16, [2]) + + node = onnx.helper.make_node( + "NegativeLogLikelihoodLoss", + inputs=[ + "0", + "1", + "2", + ], + outputs=["3"], + reduction="mean", + ) + + return ([node], [scores, labels, weights], [loss]) + + + @onnx_test() def negativeloglikelihoodloss_kd_sum_reduction_double_weighted_test(): scores = helper.make_tensor_value_info('0', TensorProto.DOUBLE, diff --git a/test/onnx/gridsample_bf16_test.onnx b/test/onnx/gridsample_bf16_test.onnx new file mode 100644 index 00000000000..5771a97ac83 Binary files /dev/null and b/test/onnx/gridsample_bf16_test.onnx differ diff --git a/test/onnx/group_norm_3d_bf16_test.onnx b/test/onnx/group_norm_3d_bf16_test.onnx new file mode 100644 index 00000000000..4c8b2087a11 --- /dev/null +++ b/test/onnx/group_norm_3d_bf16_test.onnx @@ -0,0 +1,26 @@ + group_norm_3d_bf16_test:à +M +x +scale +biasy"GroupNormalization* +epsilon¬Å'7 * + +num_groups group_norm_3d_bf16_testZ +x + + + +Z +scale + + +Z +bias + + +b +y + + + +B \ No newline at end of file diff --git a/test/onnx/group_norm_4d_bf16_test.onnx b/test/onnx/group_norm_4d_bf16_test.onnx new file mode 100644 index 00000000000..afc14b97abe --- /dev/null +++ b/test/onnx/group_norm_4d_bf16_test.onnx @@ -0,0 +1,28 @@ + group_norm_4d_bf16_test:Ë +M +x +scale +biasy"GroupNormalization* +epsilon¬Å'7 * + +num_groups group_norm_4d_bf16_testZ +x + + + + +Z +scale + + +Z +bias + + +b +y + + + + +B \ No newline at end of file diff --git a/test/onnx/group_norm_5d_bf16_test.onnx b/test/onnx/group_norm_5d_bf16_test.onnx new file mode 100644 index 00000000000..67848f3dad5 --- /dev/null +++ b/test/onnx/group_norm_5d_bf16_test.onnx @@ -0,0 +1,30 @@ + group_norm_5d_bf16_test:Ó +M +x +scale +biasy"GroupNormalization* +epsilon¬Å'7 * + +num_groups group_norm_5d_bf16_testZ +x + + + + + +Z +scale + + +Z +bias + + +b +y + + + + + +B \ No newline at end of file diff --git a/test/onnx/group_norm_small_eps_bf16_test.onnx b/test/onnx/group_norm_small_eps_bf16_test.onnx new file mode 100644 index 00000000000..21da27b93d6 --- /dev/null +++ b/test/onnx/group_norm_small_eps_bf16_test.onnx @@ -0,0 +1,26 @@ + group_norm_small_eps_bf16_test:Ê +M +x +scale +biasy"GroupNormalization* +epsilon•¿Ö3 * + +num_groups group_norm_small_eps_bf16_testZ +x + + + +Z +scale + + +Z +bias + + +b +y + + + +B \ No newline at end of file diff --git a/test/onnx/hardsigmoid_bf16_test.onnx b/test/onnx/hardsigmoid_bf16_test.onnx new file mode 100644 index 00000000000..bb5c4313c2d --- /dev/null +++ b/test/onnx/hardsigmoid_bf16_test.onnx @@ -0,0 +1,15 @@ + hardsigmoid_bf16_test:f + +xy" HardSigmoidhardsigmoid_bf16_testZ +x + + + + +b +y + + + + +B \ No newline at end of file diff --git a/test/onnx/imagescaler_bf16_test.onnx b/test/onnx/imagescaler_bf16_test.onnx new file mode 100644 index 00000000000..ae06ec39988 Binary files /dev/null and b/test/onnx/imagescaler_bf16_test.onnx differ diff --git a/test/onnx/instance_norm_bf16_test.onnx b/test/onnx/instance_norm_bf16_test.onnx new file mode 100644 index 00000000000..48129ac901e --- /dev/null +++ b/test/onnx/instance_norm_bf16_test.onnx @@ -0,0 +1,25 @@ + instance_norm_bf16_test:š +# +0 +1 +23"InstanceNormalizationinstance_norm_bf16_testZ +0 + + + + +Z +1 + + +Z +2 + + +b +3 + + + + +B \ No newline at end of file diff --git a/test/onnx/instance_norm_dyn_batch_bf16_test.onnx b/test/onnx/instance_norm_dyn_batch_bf16_test.onnx new file mode 100644 index 00000000000..a21cc6c0445 Binary files /dev/null and b/test/onnx/instance_norm_dyn_batch_bf16_test.onnx differ diff --git a/test/onnx/isinf_bf16_test.onnx b/test/onnx/isinf_bf16_test.onnx new file mode 100644 index 00000000000..f0feb41cc3f --- /dev/null +++ b/test/onnx/isinf_bf16_test.onnx @@ -0,0 +1,11 @@ + isinf_bf16_test:N + +t1t2"IsInfisinf_bf16_testZ +t1 +  + +b +t2 +   + +B \ No newline at end of file diff --git a/test/onnx/isnan_bf16_test.onnx b/test/onnx/isnan_bf16_test.onnx new file mode 100644 index 00000000000..bbbd17e139d --- /dev/null +++ b/test/onnx/isnan_bf16_test.onnx @@ -0,0 +1,11 @@ + isnan_bf16_test:N + +t1t2"IsNaNisnan_bf16_testZ +t1 +  + +b +t2 +  + +B \ No newline at end of file diff --git a/test/onnx/layer_norm_3d_bf16_test.onnx b/test/onnx/layer_norm_3d_bf16_test.onnx new file mode 100644 index 00000000000..87c1fce7e2e --- /dev/null +++ b/test/onnx/layer_norm_3d_bf16_test.onnx @@ -0,0 +1,24 @@ + layer_norm_3d_bf16_test:³ += +x +scale +biasy"LayerNormalization* +axisÿÿÿÿÿÿÿÿÿ layer_norm_3d_bf16_testZ +x + + + +Z +scale + + +Z +bias + + +b +y + + + +B \ No newline at end of file diff --git a/test/onnx/layer_norm_4d_bf16_test.onnx b/test/onnx/layer_norm_4d_bf16_test.onnx new file mode 100644 index 00000000000..7dca94f82b9 --- /dev/null +++ b/test/onnx/layer_norm_4d_bf16_test.onnx @@ -0,0 +1,26 @@ + layer_norm_4d_bf16_test:» += +x +scale +biasy"LayerNormalization* +axisÿÿÿÿÿÿÿÿÿ layer_norm_4d_bf16_testZ +x + + + + +Z +scale + + +Z +bias + + +b +y + + + + +B \ No newline at end of file diff --git a/test/onnx/layer_norm_small_eps_bf16_test.onnx b/test/onnx/layer_norm_small_eps_bf16_test.onnx new file mode 100644 index 00000000000..a844828bad3 --- /dev/null +++ b/test/onnx/layer_norm_small_eps_bf16_test.onnx @@ -0,0 +1,17 @@ + layer_norm_small_eps_bf16_test:• +4 +x +scaley"LayerNormalization* +epsilon•¿Ö3 layer_norm_small_eps_bf16_testZ +x +  + +Z +scale + + +b +y +  + +B \ No newline at end of file diff --git a/test/onnx/mean_bf16_test.onnx b/test/onnx/mean_bf16_test.onnx new file mode 100644 index 00000000000..7bb6a71dc16 --- /dev/null +++ b/test/onnx/mean_bf16_test.onnx @@ -0,0 +1,25 @@ + mean_bf16_test:Ž + +0 +1 +2mean"Meanmean_bf16_testZ +0 + + + +Z +1 + + + +Z +2 + + + +b +mean + + + +B \ No newline at end of file diff --git a/test/onnx/mod_test_fmod_bf16.onnx b/test/onnx/mod_test_fmod_bf16.onnx new file mode 100644 index 00000000000..2bd7005d3b3 --- /dev/null +++ b/test/onnx/mod_test_fmod_bf16.onnx @@ -0,0 +1,20 @@ + mod_test_fmod_bf16:| + +0 +12"Mod* +fmod mod_test_fmod_bf16Z +0 + + + +Z +1 + + + +b +2 + + + +B \ No newline at end of file diff --git a/test/onnx/mvn_default_axes_bf16_test.onnx b/test/onnx/mvn_default_axes_bf16_test.onnx new file mode 100644 index 00000000000..3f590bf2e65 --- /dev/null +++ b/test/onnx/mvn_default_axes_bf16_test.onnx @@ -0,0 +1,15 @@ + mvn_default_axes_bf16_test:ƒ +& +dataout"MeanVarianceNormalizationmvn_default_axes_bf16_testZ +data + + + + +b +out + + + + +B \ No newline at end of file diff --git a/test/onnx/mvn_rank_2_bf16_test.onnx b/test/onnx/mvn_rank_2_bf16_test.onnx new file mode 100644 index 00000000000..e39c31cc56a --- /dev/null +++ b/test/onnx/mvn_rank_2_bf16_test.onnx @@ -0,0 +1,12 @@ + mvn_rank_2_bf16_test:z +3 +dataout"MeanVarianceNormalization* +axes@ mvn_rank_2_bf16_testZ +data +  + +b +out +  + +B \ No newline at end of file diff --git a/test/onnx/mvn_rank_3_bf16_test.onnx b/test/onnx/mvn_rank_3_bf16_test.onnx new file mode 100644 index 00000000000..f2df467ccbf Binary files /dev/null and b/test/onnx/mvn_rank_3_bf16_test.onnx differ diff --git a/test/onnx/negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test.onnx b/test/onnx/negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test.onnx new file mode 100644 index 00000000000..2c7fc3da657 --- /dev/null +++ b/test/onnx/negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test.onnx @@ -0,0 +1,25 @@ + >negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test:× += +0 +1 +23"NegativeLogLikelihoodLoss* + reduction"mean >negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_testZ +0 + + + + +Z +1 + + + +Z +2 + + +b +3 + + +B \ No newline at end of file diff --git a/test/onnx/negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test2.onnx b/test/onnx/negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test2.onnx new file mode 100644 index 00000000000..7e5cffccc16 --- /dev/null +++ b/test/onnx/negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test2.onnx @@ -0,0 +1,23 @@ + ?negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test2:Ð += +0 +1 +23"NegativeLogLikelihoodLoss* + reduction"mean ?negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test2Z +0 + + + +Z +1 +  + +Z +2 + + +b +3 + + +B \ No newline at end of file diff --git a/test/onnx/parse/add_bf16_test.cpp b/test/onnx/parse/add_bf16_test.cpp new file mode 100644 index 00000000000..79bf5e2c4b3 --- /dev/null +++ b/test/onnx/parse/add_bf16_test.cpp @@ -0,0 +1,39 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(add_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + auto p0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::bf16_type, {1}}); + auto p1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::bf16_type, {1}}); + + mm->add_instruction(migraphx::make_op("add"), p0, p1); + + auto prog = optimize_onnx("add_bf16_test.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/eyelike_bf16_test.cpp b/test/onnx/parse/eyelike_bf16_test.cpp new file mode 100644 index 00000000000..fb0b1c99158 --- /dev/null +++ b/test/onnx/parse/eyelike_bf16_test.cpp @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(eyelike_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + std::vector input_lens{8, 8}; + const size_t k = 0; + auto num_rows = input_lens.front(); + auto num_cols = input_lens.back(); + auto input_type = migraphx::shape::bf16_type; + auto output_type = migraphx::shape::bf16_type; + migraphx::shape s{input_type, input_lens}; + mm->add_parameter("T1", s); + + auto eyelike_mat = make_r_eyelike(num_rows, num_cols, k); + mm->add_literal(migraphx::literal{migraphx::shape{output_type, input_lens}, eyelike_mat}); + + auto prog = optimize_onnx("eyelike_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/gemm_bf16_test.cpp b/test/onnx/parse/gemm_bf16_test.cpp new file mode 100644 index 00000000000..23afe5b7936 --- /dev/null +++ b/test/onnx/parse/gemm_bf16_test.cpp @@ -0,0 +1,56 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(gemm_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + auto l0 = mm->add_parameter("A", migraphx::shape{migraphx::shape::bf16_type, {8, 6}}); + auto l1 = mm->add_parameter("B", migraphx::shape{migraphx::shape::bf16_type, {8, 7}}); + auto l2 = mm->add_parameter("C", migraphx::shape{migraphx::shape::bf16_type, {6, 1}}); + auto alpha = 0.5f; + auto beta = 0.8f; + auto a_l = mm->add_literal(alpha); + auto t_a = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0}); + t_a = mm->add_instruction( + migraphx::make_op("convert", {{"target_type", migraphx::shape::bf16_type}}), t_a); + t_a = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a); + std::vector lens = {6, 7}; + auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, l1}, migraphx::make_op("dot"), 1.0f, 0.0f); + l2 = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), l2); + l2 = mm->add_instruction( + migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l2); + auto b_l = mm->add_literal(beta); + auto b_b = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), b_l); + auto l2_b = mm->add_instruction(migraphx::make_op("mul"), l2, b_b); + l2_b = mm->add_instruction( + migraphx::make_op("convert", {{"target_type", migraphx::shape::bf16_type}}), l2_b); + mm->add_instruction(migraphx::make_op("add"), dot, l2_b); + + auto prog = optimize_onnx("gemm_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/group_norm_3d_bf16_test.cpp b/test/onnx/parse/group_norm_3d_bf16_test.cpp new file mode 100644 index 00000000000..3d4d7579814 --- /dev/null +++ b/test/onnx/parse/group_norm_3d_bf16_test.cpp @@ -0,0 +1,34 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(group_norm_3d_bf16_test) +{ + migraphx::program p = make_group_norm( + {1, 4, 2}, {2}, {2}, {1, 2, 2, 2}, {2, 3}, 1e-5f, migraphx::shape::bf16_type); + auto prog = optimize_onnx("group_norm_3d_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/group_norm_4d_bf16_test.cpp b/test/onnx/parse/group_norm_4d_bf16_test.cpp new file mode 100644 index 00000000000..2ba0124d7ff --- /dev/null +++ b/test/onnx/parse/group_norm_4d_bf16_test.cpp @@ -0,0 +1,34 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(group_norm_4d_bf16_test) +{ + migraphx::program p = make_group_norm( + {1, 4, 3, 3}, {2}, {2}, {1, 2, 2, 3, 3}, {2, 3, 4}, 1e-5f, migraphx::shape::bf16_type); + auto prog = optimize_onnx("group_norm_4d_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/group_norm_5d_bf16_test.cpp b/test/onnx/parse/group_norm_5d_bf16_test.cpp new file mode 100644 index 00000000000..c7ec4c8632a --- /dev/null +++ b/test/onnx/parse/group_norm_5d_bf16_test.cpp @@ -0,0 +1,39 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(group_norm_5d_bf16_test) +{ + migraphx::program p = make_group_norm({3, 3, 3, 3, 3}, + {1}, + {1}, + {3, 1, 3, 3, 3, 3}, + {2, 3, 4, 5}, + 1e-5f, + migraphx::shape::bf16_type); + auto prog = optimize_onnx("group_norm_5d_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/group_norm_small_eps_bf16_test.cpp b/test/onnx/parse/group_norm_small_eps_bf16_test.cpp new file mode 100644 index 00000000000..fa871a7d237 --- /dev/null +++ b/test/onnx/parse/group_norm_small_eps_bf16_test.cpp @@ -0,0 +1,34 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(group_norm_small_eps_bf16_test) +{ + migraphx::program p = make_group_norm( + {1, 4, 2}, {2}, {2}, {1, 2, 2, 2}, {2, 3}, 1e-7f, migraphx::shape::bf16_type); + auto prog = optimize_onnx("group_norm_small_eps_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/hardsigmoid_bf16_test.cpp b/test/onnx/parse/hardsigmoid_bf16_test.cpp new file mode 100644 index 00000000000..23f8ac9a550 --- /dev/null +++ b/test/onnx/parse/hardsigmoid_bf16_test.cpp @@ -0,0 +1,58 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(hardsigmoid_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + std::vector input_lens{1, 3, 4, 5}; + auto input_type = migraphx::shape::bf16_type; + migraphx::shape s{input_type, input_lens}; + auto x = mm->add_parameter("x", s); + + float alpha = 0.2; + float beta = 0.5; + + auto mb_alpha = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}), + mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {alpha}})); + auto mb_beta = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}), + mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {beta}})); + auto mb_zero = + mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}), + mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {0}})); + auto mb_one = + mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}), + mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}})); + + auto mul = mm->add_instruction(migraphx::make_op("mul"), mb_alpha, x); + auto add = mm->add_instruction(migraphx::make_op("add"), mb_beta, mul); + mm->add_instruction(migraphx::make_op("clip"), add, mb_zero, mb_one); + + auto prog = optimize_onnx("hardsigmoid_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/imagescaler_bf16_test.cpp b/test/onnx/parse/imagescaler_bf16_test.cpp new file mode 100644 index 00000000000..8a7b34d77d0 --- /dev/null +++ b/test/onnx/parse/imagescaler_bf16_test.cpp @@ -0,0 +1,47 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(imagescaler_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + migraphx::shape s{migraphx::shape::bf16_type, {1, 3, 16, 16}}; + auto l0 = mm->add_parameter("0", s); + auto scale_val = + mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::bf16_type}, {0.5f}}); + auto bias_vals = mm->add_literal( + migraphx::literal{migraphx::shape{migraphx::shape::bf16_type, {3}}, {0.01, 0.02, 0.03}}); + auto scaled_tensor = mm->add_instruction( + migraphx::make_op("scalar", {{"scalar_bcst_dims", s.lens()}}), scale_val); + auto img_scaled = mm->add_instruction(migraphx::make_op("mul"), l0, scaled_tensor); + auto bias_bcast = mm->add_instruction( + migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", s.lens()}}), bias_vals); + mm->add_instruction(migraphx::make_op("add"), img_scaled, bias_bcast); + + auto prog = optimize_onnx("imagescaler_bf16_test.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/instance_norm_bf16_test.cpp b/test/onnx/parse/instance_norm_bf16_test.cpp new file mode 100644 index 00000000000..7d5aa9e4119 --- /dev/null +++ b/test/onnx/parse/instance_norm_bf16_test.cpp @@ -0,0 +1,63 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(instance_norm_bf16_test) +{ + std::vector dims{1, 2, 3, 3}; + migraphx::shape s1{migraphx::shape::bf16_type, dims}; + migraphx::shape s2{migraphx::shape::bf16_type, {2}}; + + migraphx::program p; + auto* mm = p.get_main_module(); + auto x = mm->add_parameter("0", s1); + auto scale = mm->add_parameter("1", s2); + auto bias = mm->add_parameter("2", s2); + + auto mean = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x); + auto l0 = add_common_op(*mm, migraphx::make_op("sub"), {x, mean}); + auto l1 = add_common_op(*mm, migraphx::make_op("sqdiff"), {x, mean}); + + auto variance = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l1); + // type of epsilon_literal is same as 0'th input; convert instruction will be added by + // add_common_op + auto epsilon_literal = + mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::bf16_type}, {1e-5}}); + auto l2 = add_common_op(*mm, migraphx::make_op("add"), {variance, epsilon_literal}); + + auto l3 = mm->add_instruction(migraphx::make_op("rsqrt"), l2); + auto l4 = add_common_op(*mm, migraphx::make_op("mul"), {l0, l3}); + + auto scale_bcast = mm->add_instruction( + migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), scale); + auto bias_bcast = mm->add_instruction( + migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), bias); + auto l5 = mm->add_instruction(migraphx::make_op("mul"), l4, scale_bcast); + mm->add_instruction(migraphx::make_op("add"), l5, bias_bcast); + + auto prog = optimize_onnx("instance_norm_bf16_test.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/instance_norm_dyn_batch_bf16_test.cpp b/test/onnx/parse/instance_norm_dyn_batch_bf16_test.cpp new file mode 100644 index 00000000000..50e1085d2e5 --- /dev/null +++ b/test/onnx/parse/instance_norm_dyn_batch_bf16_test.cpp @@ -0,0 +1,64 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(instance_norm_dyn_batch_bf16_test) +{ + // instancenorm with bf16 type, dynamic input in the 0'th (batch) dimension + migraphx::shape s1{migraphx::shape::bf16_type, {{1, 2, {2}}, {2, 2}, {3, 3}, {3, 3}}}; + migraphx::shape s2{migraphx::shape::bf16_type, {2}}; + + migraphx::program p; + auto* mm = p.get_main_module(); + auto x = mm->add_parameter("0", s1); + auto scale = mm->add_parameter("1", s2); + auto bias = mm->add_parameter("2", s2); + + auto mean = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x); + auto l0 = add_common_op(*mm, migraphx::make_op("sub"), {x, mean}); + auto l1 = add_common_op(*mm, migraphx::make_op("sqdiff"), {x, mean}); + + auto variance = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l1); + // type of epsilon_literal is same as 0'th input; convert instruction will be added by + // add_common_op + auto epsilon_literal = + mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::bf16_type}, {1e-5}}); + auto l2 = add_common_op(*mm, migraphx::make_op("add"), {variance, epsilon_literal}); + + auto l3 = mm->add_instruction(migraphx::make_op("rsqrt"), l2); + auto l4 = add_common_op(*mm, migraphx::make_op("mul"), {l0, l3}); + + auto scale_bcast = mm->add_instruction(migraphx::make_op("broadcast", {{"axis", 1}}), scale, x); + auto bias_bcast = mm->add_instruction(migraphx::make_op("broadcast", {{"axis", 1}}), bias, x); + auto l5 = mm->add_instruction(migraphx::make_op("mul"), l4, scale_bcast); + auto instance_norm_bf16 = mm->add_instruction(migraphx::make_op("add"), l5, bias_bcast); + + mm->add_return({instance_norm_bf16}); + + migraphx::onnx_options options; + options.default_dyn_dim_value = {1, 2, {2}}; + auto prog = read_onnx("instance_norm_dyn_batch_bf16_test.onnx", options); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/isinf_bf16_test.cpp b/test/onnx/parse/isinf_bf16_test.cpp new file mode 100644 index 00000000000..e8c2104312e --- /dev/null +++ b/test/onnx/parse/isinf_bf16_test.cpp @@ -0,0 +1,38 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(isinf_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + migraphx::shape s{migraphx::shape::bf16_type, {2, 3}}; + auto t1 = mm->add_parameter("t1", s); + auto ret = mm->add_instruction(migraphx::make_op("isinf"), t1); + mm->add_return({ret}); + + auto prog = read_onnx("isinf_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/isnan_bf16_test.cpp b/test/onnx/parse/isnan_bf16_test.cpp new file mode 100644 index 00000000000..b60dc4aefb5 --- /dev/null +++ b/test/onnx/parse/isnan_bf16_test.cpp @@ -0,0 +1,38 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(isnan_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + migraphx::shape s{migraphx::shape::bf16_type, {2, 3}}; + auto t1 = mm->add_parameter("t1", s); + auto ret = mm->add_instruction(migraphx::make_op("isnan"), t1); + mm->add_return({ret}); + + auto prog = read_onnx("isnan_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/layer_norm_3d_bf16_test.cpp b/test/onnx/parse/layer_norm_3d_bf16_test.cpp new file mode 100644 index 00000000000..ac54419b3a2 --- /dev/null +++ b/test/onnx/parse/layer_norm_3d_bf16_test.cpp @@ -0,0 +1,35 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(layer_norm_3d_bf16_test) +{ + migraphx::program p = + make_layer_norm({1, 4, 2}, {2}, {2}, 2, false, 1e-5f, migraphx::shape::bf16_type); + + auto prog = optimize_onnx("layer_norm_3d_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/layer_norm_4d_bf16_test.cpp b/test/onnx/parse/layer_norm_4d_bf16_test.cpp new file mode 100644 index 00000000000..c1e18a4f3f2 --- /dev/null +++ b/test/onnx/parse/layer_norm_4d_bf16_test.cpp @@ -0,0 +1,35 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(layer_norm_4d_bf16_test) +{ + migraphx::program p = + make_layer_norm({3, 3, 3, 3}, {3}, {3}, 3, false, 1e-5f, migraphx::shape::bf16_type); + + auto prog = optimize_onnx("layer_norm_4d_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/layer_norm_small_eps_bf16_test.cpp b/test/onnx/parse/layer_norm_small_eps_bf16_test.cpp new file mode 100644 index 00000000000..b43551f5a16 --- /dev/null +++ b/test/onnx/parse/layer_norm_small_eps_bf16_test.cpp @@ -0,0 +1,35 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +TEST_CASE(layer_norm_small_eps_bf16_test) +{ + migraphx::program p = + make_layer_norm({1, 2}, {2}, {1}, 1, true, 1e-7, migraphx::shape::bf16_type); + + auto prog = optimize_onnx("layer_norm_small_eps_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/mean_bf16_test.cpp b/test/onnx/parse/mean_bf16_test.cpp new file mode 100644 index 00000000000..b87f69c7b61 --- /dev/null +++ b/test/onnx/parse/mean_bf16_test.cpp @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(mean_bf16_test) +{ + const std::size_t num_data = 3; + migraphx::program p; + auto* mm = p.get_main_module(); + migraphx::shape s{migraphx::shape::bf16_type, {1, 2, 3}}; + auto data0 = mm->add_parameter("0", s); + auto data1 = mm->add_parameter("1", s); + auto data2 = mm->add_parameter("2", s); + auto add1 = mm->add_instruction(migraphx::make_op("add"), data0, data1); + auto mean = mm->add_instruction(migraphx::make_op("add"), add1, data2); + auto div_lit = mm->add_literal(migraphx::literal{migraphx::shape{s.type()}, {num_data}}); + auto divisor = + mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}), div_lit); + mean = mm->add_instruction(migraphx::make_op("div"), mean, divisor); + + auto prog = optimize_onnx("mean_bf16_test.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/mod_test_fmod_bf16.cpp b/test/onnx/parse/mod_test_fmod_bf16.cpp new file mode 100644 index 00000000000..e18cf45e6b5 --- /dev/null +++ b/test/onnx/parse/mod_test_fmod_bf16.cpp @@ -0,0 +1,38 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(mod_test_fmod_bf16) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::bf16_type, {3, 3, 3}}); + auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::bf16_type, {3, 3, 3}}); + mm->add_instruction(migraphx::make_op("fmod"), input0, input1); + + auto prog = optimize_onnx("mod_test_fmod_bf16.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/negativeloglikelihoodloss_kd_all_reduction_weighted_test.cpp b/test/onnx/parse/negativeloglikelihoodloss_kd_all_reduction_weighted_test.cpp index 0bbf82dae9e..a8510d40e1b 100644 --- a/test/onnx/parse/negativeloglikelihoodloss_kd_all_reduction_weighted_test.cpp +++ b/test/onnx/parse/negativeloglikelihoodloss_kd_all_reduction_weighted_test.cpp @@ -263,3 +263,88 @@ TEST_CASE(negativeloglikelihoodloss_kd_mean_reduction_half_weighted_test) optimize_onnx("negativeloglikelihoodloss_kd_mean_reduction_half_weighted_test.onnx"); EXPECT(p == prog); } + +TEST_CASE(negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + size_t batch_size = 4; + size_t class_size = 4; + + auto scores = mm->add_parameter( + "0", migraphx::shape{migraphx::shape::bf16_type, {batch_size, class_size, 2, 2}}); + auto labels = + mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {class_size, 2, 2}}); + auto weights = + mm->add_parameter("2", migraphx::shape{migraphx::shape::bf16_type, {class_size}}); + + auto weights_dflt = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::bf16_type, {1}, {0}), {1})); + auto labels_idx = mm->add_literal(migraphx::literal( + migraphx::shape(migraphx::shape::int32_type, {class_size}, {1}), {0, 1, 2, 3})); + + // Index variables used for gather on k dimensions that span their dimension + auto kd_1 = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::int32_type, {2}, {1}), {0, 1})); + auto kd_2 = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::int32_type, {2}, {1}), {0, 1})); + + mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {class_size}}}), + weights_dflt); + + weights = mm->add_instruction(migraphx::make_op("neg"), weights); + auto unsq_labels = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {-1}}}), labels); + + auto unsq_labels_idx = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2, 3}}}), labels_idx); + auto bc_unsq_labels_idx = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx); + + auto unsq_labels_idx2 = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0, 2, 3}}}), kd_1); + auto bc_unsq_labels_idx2 = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx2); + + auto unsq_labels_idx3 = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0, 1, 3}}}), kd_2); + auto bc_unsq_labels_idx3 = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx3); + + auto concat = mm->add_instruction(migraphx::make_op("concat", {{"axis", -1}}), + bc_unsq_labels_idx, + bc_unsq_labels_idx2, + bc_unsq_labels_idx3, + unsq_labels); + + auto transpose = mm->add_instruction( + migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), scores); + + auto gathernd = mm->add_instruction(migraphx::make_op("gathernd"), transpose, concat); + auto unsq_mb_weights = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0, 2, 3}}}), weights); + auto unsq_mb = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", scores->get_shape().lens()}}), + unsq_mb_weights); + auto transpose2 = mm->add_instruction( + migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), unsq_mb); + auto gathernd2 = mm->add_instruction(migraphx::make_op("gathernd"), transpose2, concat); + + auto weighted_loss = mm->add_instruction(migraphx::make_op("mul"), gathernd, gathernd2); + + auto loss_x = + mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1, 2}}}), weighted_loss); + auto loss_w = + mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1, 2}}}), gathernd2); + + loss_w = mm->add_instruction(migraphx::make_op("neg"), loss_w); + + mm->add_instruction(migraphx::make_op("div"), loss_x, loss_w); + + auto prog = + optimize_onnx("negativeloglikelihoodloss_kd_mean_reduction_bf16_weighted_test.onnx"); + EXPECT(p == prog); +} \ No newline at end of file diff --git a/test/onnx/parse/size_bf16_test.cpp b/test/onnx/parse/size_bf16_test.cpp new file mode 100644 index 00000000000..83056e8686f --- /dev/null +++ b/test/onnx/parse/size_bf16_test.cpp @@ -0,0 +1,36 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +TEST_CASE(size_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + auto s = migraphx::shape{migraphx::shape::bf16_type, {3, 1}}; + mm->add_parameter("x", s); + mm->add_literal(migraphx::literal{migraphx::shape::int64_type, {s.elements()}}); + auto prog = optimize_onnx("size_bf16_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/parse/softmaxcrossentropyloss_2d_mean_reduction_test.cpp b/test/onnx/parse/softmaxcrossentropyloss_2d_mean_reduction_test.cpp index e7abca8b0c6..c7a3204946d 100644 --- a/test/onnx/parse/softmaxcrossentropyloss_2d_mean_reduction_test.cpp +++ b/test/onnx/parse/softmaxcrossentropyloss_2d_mean_reduction_test.cpp @@ -158,3 +158,47 @@ TEST_CASE(softmaxcrossentropyloss_2d_mean_reduction_half_test) EXPECT(p == prog); } + +TEST_CASE(softmaxcrossentropyloss_2d_mean_reduction_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + + auto scores = mm->add_parameter("0", migraphx::shape{migraphx::shape::bf16_type, {4, 4}}); + auto labels = mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {4}}); + auto weights = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::bf16_type, {1}, {0}), {1})); + auto labels_idx = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::int32_type, {4}, {1}), {0, 1, 2, 3})); + + auto mb_weights = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", labels->get_shape().lens()}}), weights); + + mb_weights = mm->add_instruction(migraphx::make_op("neg"), mb_weights); + + auto softmax = mm->add_instruction(migraphx::make_op("softmax"), scores); + auto unsq_labels = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {-1}}}), labels); + auto unsq_labels_idx = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), labels_idx); + auto bc_unsq_labels_idx = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx); + auto concat = mm->add_instruction( + migraphx::make_op("concat", {{"axis", -1}}), bc_unsq_labels_idx, unsq_labels); + auto gathernd = mm->add_instruction(migraphx::make_op("gathernd"), softmax, concat); + auto unsq_mb_weights = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), mb_weights); + auto unsq_mb = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", scores->get_shape().lens()}}), + unsq_mb_weights); + auto gathernd2 = mm->add_instruction(migraphx::make_op("gathernd"), unsq_mb, concat); + + auto logsoftmax = mm->add_instruction(migraphx::make_op("log"), gathernd); + auto weighted_loss = mm->add_instruction(migraphx::make_op("mul"), logsoftmax, gathernd2); + mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0}}}), weighted_loss); + + auto prog = optimize_onnx("softmaxcrossentropyloss_2d_mean_reduction_bf16_test.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/softmaxcrossentropyloss_2d_no_reduction_test.cpp b/test/onnx/parse/softmaxcrossentropyloss_2d_no_reduction_test.cpp index f1da5e0d0e8..1ed0f5272da 100644 --- a/test/onnx/parse/softmaxcrossentropyloss_2d_no_reduction_test.cpp +++ b/test/onnx/parse/softmaxcrossentropyloss_2d_no_reduction_test.cpp @@ -216,3 +216,46 @@ TEST_CASE(softmaxcrossentropyloss_2d_no_reduction_half_test) EXPECT(p == prog); } + +TEST_CASE(softmaxcrossentropyloss_2d_no_reduction_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + + auto scores = mm->add_parameter("0", migraphx::shape{migraphx::shape::bf16_type, {4, 4}}); + auto labels = mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {4}}); + auto weights = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::bf16_type, {1}, {0}), {1})); + auto labels_idx = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::int32_type, {4}, {1}), {0, 1, 2, 3})); + + auto mb_weights = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", labels->get_shape().lens()}}), weights); + mb_weights = mm->add_instruction(migraphx::make_op("neg"), mb_weights); + + auto softmax = mm->add_instruction(migraphx::make_op("softmax"), scores); + auto unsq_labels = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {-1}}}), labels); + auto unsq_labels_idx = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), labels_idx); + auto bc_unsq_labels_idx = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx); + auto concat = mm->add_instruction( + migraphx::make_op("concat", {{"axis", -1}}), bc_unsq_labels_idx, unsq_labels); + auto gathernd = mm->add_instruction(migraphx::make_op("gathernd"), softmax, concat); + auto unsq_mb_weights = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), mb_weights); + auto unsq_mb = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", scores->get_shape().lens()}}), + unsq_mb_weights); + auto gathernd2 = mm->add_instruction(migraphx::make_op("gathernd"), unsq_mb, concat); + + auto logsoftmax = mm->add_instruction(migraphx::make_op("log"), gathernd); + + mm->add_instruction(migraphx::make_op("mul"), logsoftmax, gathernd2); + + auto prog = optimize_onnx("softmaxcrossentropyloss_2d_no_reduction_bf16_test.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/softmaxcrossentropyloss_2d_sum_reduction_test.cpp b/test/onnx/parse/softmaxcrossentropyloss_2d_sum_reduction_test.cpp index 3fe7df558a7..092c4fe09ef 100644 --- a/test/onnx/parse/softmaxcrossentropyloss_2d_sum_reduction_test.cpp +++ b/test/onnx/parse/softmaxcrossentropyloss_2d_sum_reduction_test.cpp @@ -157,3 +157,47 @@ TEST_CASE(softmaxcrossentropyloss_2d_sum_reduction_half_test) EXPECT(p == prog); } + +TEST_CASE(softmaxcrossentropyloss_2d_sum_reduction_bf16_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + + auto scores = mm->add_parameter("0", migraphx::shape{migraphx::shape::bf16_type, {4, 4}}); + auto labels = mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {4}}); + auto weights = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::bf16_type, {1}, {0}), {1})); + auto labels_idx = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::int32_type, {4}, {1}), {0, 1, 2, 3})); + + auto mb_weights = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", labels->get_shape().lens()}}), weights); + mb_weights = mm->add_instruction(migraphx::make_op("neg"), mb_weights); + + auto softmax = mm->add_instruction(migraphx::make_op("softmax"), scores); + auto unsq_labels = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {-1}}}), labels); + auto unsq_labels_idx = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), labels_idx); + auto bc_unsq_labels_idx = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx); + auto concat = mm->add_instruction( + migraphx::make_op("concat", {{"axis", -1}}), bc_unsq_labels_idx, unsq_labels); + auto gathernd = mm->add_instruction(migraphx::make_op("gathernd"), softmax, concat); + auto unsq_mb_weights = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), mb_weights); + auto unsq_mb = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", scores->get_shape().lens()}}), + unsq_mb_weights); + auto gathernd2 = mm->add_instruction(migraphx::make_op("gathernd"), unsq_mb, concat); + + auto logsoftmax = mm->add_instruction(migraphx::make_op("log"), gathernd); + + auto weighted_loss = mm->add_instruction(migraphx::make_op("mul"), logsoftmax, gathernd2); + mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0}}}), weighted_loss); + + auto prog = optimize_onnx("softmaxcrossentropyloss_2d_sum_reduction_bf16_test.onnx"); + + EXPECT(p == prog); +} diff --git a/test/onnx/parse/softmaxcrossentropyloss_kd_all_reduction_weighted_test.cpp b/test/onnx/parse/softmaxcrossentropyloss_kd_all_reduction_weighted_test.cpp index 42b9f341336..2ad877797b7 100644 --- a/test/onnx/parse/softmaxcrossentropyloss_kd_all_reduction_weighted_test.cpp +++ b/test/onnx/parse/softmaxcrossentropyloss_kd_all_reduction_weighted_test.cpp @@ -266,3 +266,89 @@ TEST_CASE(softmaxcrossentropyloss_kd_mean_reduction_half_weighted_test) auto prog = optimize_onnx("softmaxcrossentropyloss_kd_mean_reduction_half_weighted_test.onnx"); EXPECT(p == prog); } + +TEST_CASE(softmaxcrossentropyloss_kd_mean_reduction_bf16_weighted_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + size_t batch_size = 4; + size_t class_size = 4; + + auto scores = mm->add_parameter( + "0", migraphx::shape{migraphx::shape::bf16_type, {batch_size, class_size, 2, 2}}); + auto labels = + mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {class_size, 2, 2}}); + auto weights = + mm->add_parameter("2", migraphx::shape{migraphx::shape::bf16_type, {class_size}}); + + auto weights_dflt = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::bf16_type, {1}, {0}), {1})); + auto labels_idx = mm->add_literal(migraphx::literal( + migraphx::shape(migraphx::shape::int32_type, {class_size}, {1}), {0, 1, 2, 3})); + + // Index variables used for gather on k dimensions that span their dimension + auto kd_1 = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::int32_type, {2}, {1}), {0, 1})); + auto kd_2 = mm->add_literal( + migraphx::literal(migraphx::shape(migraphx::shape::int32_type, {2}, {1}), {0, 1})); + + mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {class_size}}}), + weights_dflt); + weights = mm->add_instruction(migraphx::make_op("neg"), weights); + + auto softmax = mm->add_instruction(migraphx::make_op("softmax"), scores); + auto unsq_labels = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {-1}}}), labels); + + auto unsq_labels_idx = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2, 3}}}), labels_idx); + auto bc_unsq_labels_idx = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx); + + auto unsq_labels_idx2 = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0, 2, 3}}}), kd_1); + auto bc_unsq_labels_idx2 = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx2); + + auto unsq_labels_idx3 = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0, 1, 3}}}), kd_2); + auto bc_unsq_labels_idx3 = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", unsq_labels->get_shape().lens()}}), + unsq_labels_idx3); + + auto concat = mm->add_instruction(migraphx::make_op("concat", {{"axis", -1}}), + bc_unsq_labels_idx, + bc_unsq_labels_idx2, + bc_unsq_labels_idx3, + unsq_labels); + + auto transpose = mm->add_instruction( + migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), softmax); + + auto gathernd = mm->add_instruction(migraphx::make_op("gathernd"), transpose, concat); + auto unsq_mb_weights = + mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0, 2, 3}}}), weights); + auto unsq_mb = mm->add_instruction( + migraphx::make_op("multibroadcast", {{"out_lens", scores->get_shape().lens()}}), + unsq_mb_weights); + auto transpose2 = mm->add_instruction( + migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), unsq_mb); + auto gathernd2 = mm->add_instruction(migraphx::make_op("gathernd"), transpose2, concat); + + auto logsoftmax = mm->add_instruction(migraphx::make_op("log"), gathernd); + + auto weighted_loss = mm->add_instruction(migraphx::make_op("mul"), logsoftmax, gathernd2); + + auto loss_x = + mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1, 2}}}), weighted_loss); + auto loss_w = + mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1, 2}}}), gathernd2); + loss_w = mm->add_instruction(migraphx::make_op("neg"), loss_w); + + mm->add_instruction(migraphx::make_op("div"), loss_x, loss_w); + + auto prog = optimize_onnx("softmaxcrossentropyloss_kd_mean_reduction_bf16_weighted_test.onnx"); + EXPECT(p == prog); +} diff --git a/test/onnx/round_bf16_test.onnx b/test/onnx/round_bf16_test.onnx new file mode 100644 index 00000000000..d5f5bcad121 --- /dev/null +++ b/test/onnx/round_bf16_test.onnx @@ -0,0 +1,11 @@ + round_bf16_test:J + +xy"Roundround_bf16_testZ +x +  + +b +y +  + +B \ No newline at end of file diff --git a/test/onnx/size_bf16_test.onnx b/test/onnx/size_bf16_test.onnx new file mode 100644 index 00000000000..85c42208cf3 --- /dev/null +++ b/test/onnx/size_bf16_test.onnx @@ -0,0 +1,11 @@ + size_bf16_test:D + +xy"Sizesize_bf16_testZ +x +  + +b +y + + +B \ No newline at end of file diff --git a/test/onnx/softmaxcrossentropyloss_2d_mean_reduction_bf16_test.onnx b/test/onnx/softmaxcrossentropyloss_2d_mean_reduction_bf16_test.onnx new file mode 100644 index 00000000000..3c9a4b6ebd2 --- /dev/null +++ b/test/onnx/softmaxcrossentropyloss_2d_mean_reduction_bf16_test.onnx @@ -0,0 +1,17 @@ + 3softmaxcrossentropyloss_2d_mean_reduction_bf16_test:¦ +8 +0 +12"SoftmaxCrossEntropyLoss* + reduction"mean 3softmaxcrossentropyloss_2d_mean_reduction_bf16_testZ +0 +  + +Z +1 + + +b +2 + + +B \ No newline at end of file diff --git a/test/onnx/softmaxcrossentropyloss_2d_no_reduction_bf16_test.onnx b/test/onnx/softmaxcrossentropyloss_2d_no_reduction_bf16_test.onnx new file mode 100644 index 00000000000..b714484bf32 --- /dev/null +++ b/test/onnx/softmaxcrossentropyloss_2d_no_reduction_bf16_test.onnx @@ -0,0 +1,17 @@ + 1softmaxcrossentropyloss_2d_no_reduction_bf16_test:¤ +8 +0 +12"SoftmaxCrossEntropyLoss* + reduction"none 1softmaxcrossentropyloss_2d_no_reduction_bf16_testZ +0 +  + +Z +1 + + +b +2 + + +B \ No newline at end of file diff --git a/test/onnx/softmaxcrossentropyloss_2d_sum_reduction_bf16_test.onnx b/test/onnx/softmaxcrossentropyloss_2d_sum_reduction_bf16_test.onnx new file mode 100644 index 00000000000..b9ebaf16c00 --- /dev/null +++ b/test/onnx/softmaxcrossentropyloss_2d_sum_reduction_bf16_test.onnx @@ -0,0 +1,17 @@ + 2softmaxcrossentropyloss_2d_sum_reduction_bf16_test:¤ +7 +0 +12"SoftmaxCrossEntropyLoss* + reduction"sum 2softmaxcrossentropyloss_2d_sum_reduction_bf16_testZ +0 +  + +Z +1 + + +b +2 + + +B \ No newline at end of file diff --git a/test/onnx/softmaxcrossentropyloss_kd_mean_reduction_bf16_weighted_test.onnx b/test/onnx/softmaxcrossentropyloss_kd_mean_reduction_bf16_weighted_test.onnx new file mode 100644 index 00000000000..38a94071f29 --- /dev/null +++ b/test/onnx/softmaxcrossentropyloss_kd_mean_reduction_bf16_weighted_test.onnx @@ -0,0 +1,25 @@ +  +#include +#include +#include + +TEST_CASE(add_bf16_test) +{ + auto p = optimize_onnx("add_bf16_test.onnx"); + p.compile(migraphx::make_target("ref")); + + migraphx::shape s{migraphx::shape::bf16_type, {1}}; + + migraphx::parameter_map pp; + migraphx::literal l1{s, {3.25}}; + migraphx::literal l2{s, {2.25}}; + pp["0"] = l1.get_argument(); + pp["1"] = l2.get_argument(); + + auto result = p.eval(pp).back(); + std::vector result_vector; + result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); }); + std::vector gold{static_cast(5.5)}; + + EXPECT(migraphx::verify::verify_rms_range(result_vector, gold)); +} diff --git a/test/onnx/verify/gemm_bf16_test.cpp b/test/onnx/verify/gemm_bf16_test.cpp new file mode 100644 index 00000000000..2dc1f9aaec0 --- /dev/null +++ b/test/onnx/verify/gemm_bf16_test.cpp @@ -0,0 +1,71 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +TEST_CASE(gemm_bf16_test) +{ + migraphx::program p = read_onnx("gemm_bf16_test.onnx"); + p.compile(migraphx::make_target("ref")); + + migraphx::shape a_shape{migraphx::shape::bf16_type, {8, 6}}; + std::vector tmp = {0.2646, 0.8525, 0.4192, 0.1415, 0.4321, 0.675, 0.4248, 0.8203, + 0.978, 0.5796, 0.6626, 0.479, 0.924, 0.734, 0.674, 0.8716, + 0.3733, 0.3328, 0.4272, 0.0247, 0.7583, 0.4873, 0.5835, 0.694, + 0.4375, 0.2406, 0.269, 0.6763, 0.542, 0.8994, 0.657, 0.5425, + 0.1412, 0.8994, 0.2183, 0.812, 0.937, 0.3438, 0.712, 0.9033, + 0.266, 0.8013, 0.803, 0.4993, 0.07196, 0.635, 0.7344, 0.3213}; + std::vector a_data{tmp.cbegin(), tmp.cend()}; + + migraphx::shape b_shape{migraphx::shape::bf16_type, {8, 7}}; + tmp = {0.7095, 0.612, 0.741, 0.02121, 0.3872, 0.4482, 0.6235, 0.02249, 0.2332, 0.7656, + 0.8955, 0.8154, 0.2239, 0.9277, 0.4622, 0.708, 0.566, 0.0736, 0.138, 0.8574, + 0.4055, 0.382, 0.6206, 0.424, 0.3674, 0.435, 0.998, 0.3594, 0.701, 0.6216, + 0.01826, 0.6313, 0.514, 0.1095, 0.3203, 0.01636, 0.537, 0.01952, 0.4502, 0.8965, + 0.5415, 0.7456, 0.793, 0.756, 0.9, 0.5264, 0.05368, 0.4214, 0.276, 0.1517, + 0.08453, 0.83, 0.417, 0.1682, 0.845, 0.1729}; + std::vector b_data{tmp.cbegin(), tmp.cend()}; + + migraphx::shape c_shape{migraphx::shape::bf16_type, {6, 1}}; + tmp = {0.10846, 0.672, 0.527, 0.94, 0.429, 0.2291}; + std::vector c_data{tmp.cbegin(), tmp.cend()}; + + migraphx::parameter_map params; + params["A"] = migraphx::argument(a_shape, a_data.data()); + params["B"] = migraphx::argument(b_shape, b_data.data()); + params["C"] = migraphx::argument(c_shape, c_data.data()); + + auto result = p.eval(params).back(); + std::vector result_vector; + result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); }); + + tmp = {1.071, 1.378, 1.465, 1.093, 0.968, 1.542, 1.145, 1.287, 1.533, 1.75, 1.338, + 1.449, 1.592, 1.668, 1.265, 1.531, 1.656, 1.348, 1.2705, 1.525, 1.479, 1.754, + 2.143, 2.062, 1.921, 1.836, 2.203, 1.952, 1.055, 1.225, 1.418, 1.209, 1.155, + 1.42, 1.234, 1.302, 1.593, 1.368, 1.289, 1.327, 1.451, 1.394}; + std::vector gold{tmp.cbegin(), tmp.cend()}; + EXPECT(migraphx::verify::verify_rms_range(result_vector, gold)); +} diff --git a/test/onnx/verify/gridsample_bf16_test.cpp b/test/onnx/verify/gridsample_bf16_test.cpp new file mode 100644 index 00000000000..d9c4f455844 --- /dev/null +++ b/test/onnx/verify/gridsample_bf16_test.cpp @@ -0,0 +1,61 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +TEST_CASE(gridsample_bf16_test) +{ + migraphx::program p = read_onnx("gridsample_bf16_test.onnx"); + migraphx::compile_options options; + p.compile(migraphx::make_target("ref")); + + migraphx::shape data_shape{migraphx::shape::bf16_type, {1, 1, 4, 4}}; + migraphx::shape grid_shape{migraphx::shape::float_type, {1, 6, 6, 2}}; + std::vector tmp = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15.}; + std::vector data = {tmp.begin(), tmp.end()}; + std::vector grid = {-1., -1., -0.6, -1., -0.2, -1., 0.2, -1., 0.6, -1., 1., -1., + -1., -0.6, -0.6, -0.6, -0.2, -0.6, 0.2, -0.6, 0.6, -0.6, 1., -0.6, + -1., -0.2, -0.6, -0.2, -0.2, -0.2, 0.2, -0.2, 0.6, -0.2, 1., -0.2, + -1., 0.2, -0.6, 0.2, -0.2, 0.2, 0.2, 0.2, 0.6, 0.2, 1., 0.2, + -1., 0.6, -0.6, 0.6, -0.2, 0.6, 0.2, 0.6, 0.6, 0.6, 1., 0.6, + -1., 1., -0.6, 1., -0.2, 1., 0.2, 1., 0.6, 1., 1., 1.}; + + migraphx::parameter_map pp; + pp["x"] = migraphx::argument(data_shape, data.data()); + pp["grid"] = migraphx::argument(grid_shape, grid.data()); + + auto result = p.eval(pp).back(); + std::vector result_vector; + result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); }); + + tmp = {0., 0.15, 0.55, 0.95, 1.35, 0.75, 0.6, 1.5, 2.3, 3.1, 3.9, 2.1, + 2.2, 4.7, 5.5, 6.3, 7.1, 3.7, 3.8, 7.9, 8.7, 9.5, 10.3, 5.3, + 5.4, 11.1, 11.9, 12.7, 13.5, 6.9, 3., 6.15, 6.55, 6.95, 7.35, 3.75}; + + std::vector gold = {tmp.begin(), tmp.end()}; + + EXPECT(migraphx::verify::verify_rms_range(result_vector, gold)); +} diff --git a/test/onnx/verify/group_norm_3d_bf16_test.cpp b/test/onnx/verify/group_norm_3d_bf16_test.cpp new file mode 100644 index 00000000000..4cd3b750d9d --- /dev/null +++ b/test/onnx/verify/group_norm_3d_bf16_test.cpp @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +TEST_CASE(group_norm_bf16_test) +{ + using migraphx::bf16; + std::vector scale{bf16{1.2}, bf16{0.8}}; + std::vector bias{bf16{0.5}, bf16{0.2}}; + std::vector result_vector = + norm_test({1, 4, 2}, scale, bias, read_onnx("group_norm_3d_bf16_test.onnx")); + std::vector gold = {bf16{-1.10996256}, + bf16{-0.0366542}, + bf16{1.0366542}, + bf16{2.10996256}, + bf16{-0.87330837}, + bf16{-0.15776947}, + bf16{0.55776947}, + bf16{1.27330837}}; + EXPECT(migraphx::verify::verify_rms_range(result_vector, gold)); +} diff --git a/test/onnx/verify/isinf_bf16_test.cpp b/test/onnx/verify/isinf_bf16_test.cpp new file mode 100644 index 00000000000..8d8f99458c7 --- /dev/null +++ b/test/onnx/verify/isinf_bf16_test.cpp @@ -0,0 +1,50 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +TEST_CASE(isinf_bf16_test) +{ + migraphx::program p = read_onnx("isinf_bf16_test.onnx"); + p.compile(migraphx::make_target("ref")); + + migraphx::shape s{migraphx::shape::bf16_type, {2, 3}}; + migraphx::parameter_map pp; + migraphx::bf16 nan = std::numeric_limits::quiet_NaN(); + migraphx::bf16 infinity = std::numeric_limits::infinity(); + migraphx::bf16 max = std::numeric_limits::max(); + migraphx::bf16 min = std::numeric_limits::min(); + migraphx::bf16 val = migraphx::bf16(3.6); + std::vector data = {-infinity, nan, min, val, max, infinity}; + pp["t1"] = migraphx::argument(s, data.data()); + + auto result = p.eval(pp).back(); + std::vector result_vector; + result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); }); + + std::vector gold = {1, 0, 0, 0, 0, 1}; + EXPECT(migraphx::verify::verify_rms_range(result_vector, gold)); +} diff --git a/test/onnx/verify/layer_norm_3d_bf16_test.cpp b/test/onnx/verify/layer_norm_3d_bf16_test.cpp new file mode 100644 index 00000000000..1a45e393809 --- /dev/null +++ b/test/onnx/verify/layer_norm_3d_bf16_test.cpp @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +TEST_CASE(layer_norm_bf16_test) +{ + using migraphx::bf16; + std::vector scale{bf16{1.2}, bf16{0.8}}; + std::vector bias{bf16{0.5}, bf16{0.2}}; + std::vector result_vector = + norm_test({1, 4, 2}, scale, bias, read_onnx("layer_norm_3d_bf16_test.onnx")); + std::vector gold = {bf16{-0.69997597}, + bf16{0.99998398}, + bf16{-0.69997597}, + bf16{0.99998398}, + bf16{-0.69997597}, + bf16{0.99998398}, + bf16{-0.69997597}, + bf16{0.99998398}}; + EXPECT(migraphx::verify::verify_rms_range(result_vector, gold)); +} diff --git a/test/onnx/verify/mvn_default_axes_bf16_test.cpp b/test/onnx/verify/mvn_default_axes_bf16_test.cpp new file mode 100644 index 00000000000..2a76a81a6ba --- /dev/null +++ b/test/onnx/verify/mvn_default_axes_bf16_test.cpp @@ -0,0 +1,51 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +TEST_CASE(mvn_default_axes_bf16_test) +{ + using migraphx::bf16; + auto result = mvn_test({2, 2, 2, 2}, read_onnx("mvn_default_axes_bf16_test.onnx")); + std::vector gold{bf16{-1.324}, + bf16{-1.084}, + bf16{-0.843}, + bf16{-0.602}, + bf16{-1.324}, + bf16{-1.084}, + bf16{-0.843}, + bf16{-0.602}, + bf16{0.602}, + bf16{0.843}, + bf16{1.084}, + bf16{1.324}, + bf16{0.602}, + bf16{0.843}, + bf16{1.084}, + bf16{1.324}}; + EXPECT(migraphx::verify::verify_rms_range(result, gold)); +} diff --git a/test/onnx/verify/mvn_rank_2_bf16_test.cpp b/test/onnx/verify/mvn_rank_2_bf16_test.cpp new file mode 100644 index 00000000000..fca3312a810 --- /dev/null +++ b/test/onnx/verify/mvn_rank_2_bf16_test.cpp @@ -0,0 +1,36 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +TEST_CASE(mvn_rank_2_bf16_test) +{ + using migraphx::bf16; + auto result = mvn_test({2, 2}, read_onnx("mvn_rank_2_bf16_test.onnx")); + std::vector gold{bf16{-1}, bf16{1}, bf16{-1}, bf16{1}}; + EXPECT(migraphx::verify::verify_rms_range(result, gold)); +} diff --git a/test/onnx/verify/mvn_rank_3_bf16_test.cpp b/test/onnx/verify/mvn_rank_3_bf16_test.cpp new file mode 100644 index 00000000000..a785e40fff8 --- /dev/null +++ b/test/onnx/verify/mvn_rank_3_bf16_test.cpp @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +TEST_CASE(mvn_rank_3_bf16_test) +{ + using migraphx::bf16; + auto result = mvn_test({2, 2, 2}, read_onnx("mvn_rank_3_bf16_test.onnx")); + std::vector gold{bf16{-1.342}, + bf16{-1.342}, + bf16{-0.4473}, + bf16{-0.4473}, + bf16{0.4473}, + bf16{0.4473}, + bf16{1.342}, + bf16{1.342}}; + EXPECT(migraphx::verify::verify_rms_range(result, gold)); +} diff --git a/test/onnx/verify/round_bf16_test.cpp b/test/onnx/verify/round_bf16_test.cpp new file mode 100644 index 00000000000..a426bd2790e --- /dev/null +++ b/test/onnx/verify/round_bf16_test.cpp @@ -0,0 +1,64 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +TEST_CASE(round_bf16_test) +{ + migraphx::program p = read_onnx("round_bf16_test.onnx"); + p.compile(migraphx::make_target("ref")); + + migraphx::shape xs{migraphx::shape::bf16_type, {4, 4}}; + std::vector tmp = {-3.51, + -3.5, + -3.49, + -2.51, + -2.50, + -2.49, + -1.6, + -1.5, + -0.51, + -0.5, + 0.5, + 0.6, + 2.4, + 2.5, + 3.5, + 4.5}; + std::vector data{tmp.cbegin(), tmp.cend()}; + migraphx::parameter_map param_map; + param_map["x"] = migraphx::argument(xs, data.data()); + + auto result = p.eval(param_map).back(); + + std::vector result_vector; + result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); }); + + tmp = {-4.0, -4.0, -3.0, -3.0, -2.0, -2.0, -2.0, -2.0, -1.0, 0.0, 0.0, 1.0, 2.0, 2.0, 4.0, 4.0}; + std::vector gold{tmp.cbegin(), tmp.cend()}; + + EXPECT(migraphx::verify::verify_rms_range(result_vector, gold)); +}