| 39 | #include <migraphx/verify.hpp> |
| 40 | |
| 41 | TEST_CASE(bias_gelu) |
| 42 | { |
| 43 | migraphx::shape s1{migraphx::shape::half_type, {2, 4, 8}}; |
| 44 | migraphx::shape s2{migraphx::shape::half_type}; |
| 45 | migraphx::module m1; |
| 46 | { |
| 47 | auto a = m1.add_parameter("a", s1); |
| 48 | auto b = m1.add_parameter("b", s1); |
| 49 | auto add1 = m1.add_instruction(migraphx::make_op("add"), a, b); |
| 50 | auto l1 = m1.add_literal(migraphx::literal{s2, {1.4140625f}}); |
| 51 | auto div = add_common_op(m1, migraphx::make_op("div"), {add1, l1}); |
| 52 | auto erf = m1.add_instruction(migraphx::make_op("erf"), div); |
| 53 | auto l2 = m1.add_literal(migraphx::literal{s2, {1.0f}}); |
| 54 | auto add2 = add_common_op(m1, migraphx::make_op("add"), {erf, l2}); |
| 55 | auto mul = m1.add_instruction(migraphx::make_op("mul"), add1, add2); |
| 56 | auto l3 = m1.add_literal(migraphx::literal{s2, {0.5f}}); |
| 57 | mul = add_common_op(m1, migraphx::make_op("mul"), {mul, l3}); |
| 58 | m1.add_return({mul}); |
| 59 | } |
| 60 | migraphx::rewrite_gelu pass; |
| 61 | pass.apply(m1); |
| 62 | migraphx::dead_code_elimination dce; |
| 63 | dce.apply(m1); |
| 64 | |
| 65 | migraphx::module m2; |
| 66 | { |
| 67 | using migraphx::literal; |
| 68 | using migraphx::make_op; |
| 69 | using migraphx::shape; |
| 70 | auto x_param = m2.add_parameter("a", s1); |
| 71 | auto bias_param = m2.add_parameter("b", s1); |
| 72 | auto bias_add = m2.add_instruction(migraphx::make_op("add"), x_param, bias_param); |
| 73 | double const0 = -2. * sqrt(M_2_PI); |
| 74 | double const1 = 0.044715 * const0; |
| 75 | auto lit0 = m2.add_literal(literal{shape{s2.type()}, {const0}}); |
| 76 | auto lit1 = m2.add_literal(literal{shape{s2.type()}, {const1}}); |
| 77 | auto one = m2.add_literal(literal{shape{s2.type()}, {1.0}}); |
| 78 | auto xb = add_common_op(m2, make_op("mul"), {bias_add, lit1}); |
| 79 | auto a = m2.add_instruction(make_op("mul"), bias_add, xb); |
| 80 | auto b = add_common_op(m2, make_op("add"), {a, lit0}); |
| 81 | auto u = m2.add_instruction(make_op("mul"), bias_add, b); |
| 82 | auto emu = m2.add_instruction(make_op("exp"), u); |
| 83 | auto c = add_common_op(m2, make_op("add"), {one, emu}); |
| 84 | auto y = m2.add_instruction(make_op("div"), bias_add, c); |
| 85 | m2.add_return({y}); |
| 86 | } |
| 87 | |
| 88 | EXPECT(m1 == m2); |
| 89 | } |
| 90 | |
| 91 | TEST_CASE(non_bias_gelu) |
| 92 | { |
nothing calls this directly
no test coverage detected