| 47 | std::vector<op_desc> operators() const { return {{"Scan"}}; } |
| 48 | |
| 49 | std::vector<instruction_ref> parse(const op_desc& /*opd*/, |
| 50 | onnx_parser& parser, |
| 51 | onnx_parser::node_info info, |
| 52 | std::vector<instruction_ref> args) const |
| 53 | { |
| 54 | if(parser.opset_version == 8) |
| 55 | MIGRAPHX_THROW("Scan: Opset 8 version not supported"); |
| 56 | |
| 57 | check_for_required_attributes(info, {"body", "num_scan_inputs"}); |
| 58 | |
| 59 | const auto& body_graph = info.attributes["body"].g(); |
| 60 | auto* body = parser.prog.create_module(info.name + "_scan"); |
| 61 | parser.parse_graph(body, body_graph); |
| 62 | |
| 63 | // Scan has: |
| 64 | // N + M inputs (N state variables, M scan inputs) |
| 65 | // N + K outputs (N state variables, K scan outputs) |
| 66 | // Same input and output counts apply for body |
| 67 | auto body_outs = body->get_returns(); |
| 68 | const auto m = info.attributes["num_scan_inputs"].i(); |
| 69 | const auto n = args.size() - m; |
| 70 | const auto k = body_outs.size() - n; |
| 71 | |
| 72 | std::vector<instruction_ref> body_params; |
| 73 | transform(body->get_parameter_names(), |
| 74 | std::back_inserter(body_params), |
| 75 | [&](const auto& name) { return body->get_parameter(name); }); |
| 76 | |
| 77 | if(auto num_body_params = body_params.size(); num_body_params != n + m) |
| 78 | MIGRAPHX_THROW("Scan: Number of inputs to body {" + std::to_string(num_body_params) + |
| 79 | "} does not match number of inputs to Scan {" + std::to_string(n + m) + |
| 80 | "}"); |
| 81 | |
| 82 | const auto scan_input_axes = parse_axes(info, "scan_input_axes", m, args.begin() + n, 0); |
| 83 | const auto scan_input_directions = parse_dirs(info, "scan_input_directions", m); |
| 84 | const auto scan_output_axes = |
| 85 | parse_axes(info, "scan_output_axes", k, body_outs.begin() + n, 1); |
| 86 | const auto scan_output_directions = parse_dirs(info, "scan_output_directions", k); |
| 87 | |
| 88 | // Check that scan axes lens are the same across all scan inputs |
| 89 | size_t num_iters = args[n]->get_shape().lens()[scan_input_axes[0]]; |
| 90 | for(auto i = 1; i < m; ++i) |
| 91 | if(args[n + i]->get_shape().lens()[scan_input_axes[i]] != num_iters) |
| 92 | MIGRAPHX_THROW( |
| 93 | "Scan: Lengths of scan_input_axes do not match across all scan inputs.\n" |
| 94 | "Scan input shapes: " + |
| 95 | to_string_range( |
| 96 | to_shapes(std::vector<instruction_ref>(args.begin() + n, args.end()))) + |
| 97 | "\nScan input axes: " + to_string_range(scan_input_axes)); |
| 98 | |
| 99 | if(num_iters > parser.max_loop_iterations) |
| 100 | MIGRAPHX_THROW("Scan: Number of required iterations {" + std::to_string(num_iters) + |
| 101 | "} would exceed the maximum iteration limit {" + |
| 102 | std::to_string(parser.max_loop_iterations) + "}"); |
| 103 | |
| 104 | // Check that state variable shapes match between the Scan node and its body attribute |
| 105 | for(auto i = 0; i < n; ++i) |
| 106 | if(args[i]->get_shape() != body_params[i]->get_shape()) |
nothing calls this directly
no test coverage detected