| 51 | } |
| 52 | |
| 53 | std::vector<instruction_ref> |
| 54 | insert(module& m, instruction_ref ins, const std::vector<instruction_ref>& args) const |
| 55 | { |
| 56 | auto a_arg = args[0]; |
| 57 | auto b_arg = args[1]; |
| 58 | if(a_arg->get_shape().ndim() != 2 or b_arg->get_shape().ndim() != 2) |
| 59 | { |
| 60 | MIGRAPHX_THROW("gemm op_builder: A and B should be rank 2, A is rank " + |
| 61 | std::to_string(a_arg->get_shape().ndim()) + ", B is rank " + |
| 62 | std::to_string(b_arg->get_shape().ndim())); |
| 63 | } |
| 64 | |
| 65 | std::vector<int64_t> perm = {1, 0}; |
| 66 | auto dot_type = a_arg->get_shape().type(); |
| 67 | if(alpha != 1.0f) |
| 68 | { |
| 69 | auto alpha_literal = m.add_literal(alpha); |
| 70 | a_arg = insert_common_op(m, ins, "mul", alpha_literal, a_arg); |
| 71 | |
| 72 | if(a_arg->get_shape().type() != dot_type) |
| 73 | { |
| 74 | a_arg = m.insert_instruction( |
| 75 | ins, make_op("convert", {{"target_type", dot_type}}), a_arg); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | a_arg = |
| 80 | (trans_a) |
| 81 | ? m.insert_instruction(ins, make_op("transpose", {{"permutation", perm}}), a_arg) |
| 82 | : a_arg; |
| 83 | b_arg = |
| 84 | (trans_b) |
| 85 | ? m.insert_instruction(ins, make_op("transpose", {{"permutation", perm}}), args[1]) |
| 86 | : args[1]; |
| 87 | |
| 88 | auto dot_ins = m.insert_instruction(ins, make_op("dot"), a_arg, b_arg); |
| 89 | |
| 90 | if(args.size() == 3) |
| 91 | { |
| 92 | if(not float_equal(beta, 0.0f)) |
| 93 | { |
| 94 | auto c_arg = args[2]; |
| 95 | if(dot_ins->get_shape().dynamic()) |
| 96 | { |
| 97 | c_arg = m.insert_instruction(ins, make_op("multibroadcast"), args[2], dot_ins); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | auto out_lens = a_arg->get_shape().lens(); |
| 102 | out_lens.back() = b_arg->get_shape().lens().back(); |
| 103 | auto c_lens = c_arg->get_shape().lens(); |
| 104 | if(not std::equal( |
| 105 | out_lens.begin(), out_lens.end(), c_lens.begin(), c_lens.end())) |
| 106 | { |
| 107 | c_arg = m.insert_instruction( |
| 108 | ins, make_op("multibroadcast", {{"out_lens", out_lens}}), args[2]); |
| 109 | } |
| 110 | } |
no test coverage detected