| 38 | MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_ELIMINATE_CONTIGUOUS) |
| 39 | |
| 40 | static bool try_compute_shape(instruction_ref ins, |
| 41 | const std::vector<shape>& inputs, |
| 42 | const std::vector<module_ref>& mods) |
| 43 | { |
| 44 | try |
| 45 | { |
| 46 | shape new_shape = ins->get_operator().compute_shape(inputs, mods); |
| 47 | |
| 48 | // Cannot tell if a dynamic shape will need to be made contiguous |
| 49 | if(new_shape.dynamic()) |
| 50 | { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | // If the output shape is a standard shape, no need to try its output |
| 55 | if(new_shape.standard()) |
| 56 | { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | // if no changes for the shape, the contiguous can also be removed |
| 61 | if(new_shape == ins->get_shape()) |
| 62 | { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | auto outputs = ins->outputs(); |
| 67 | // If the current instruction has no output, it means it is the last |
| 68 | // instruction and generates a non-standard output shape, and the last |
| 69 | // output shape is different from the case with the contiguous operator |
| 70 | if(outputs.empty()) |
| 71 | { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | for(auto output : outputs) |
| 76 | { |
| 77 | auto args = output->inputs(); |
| 78 | std::vector<shape> input_shapes(args.size()); |
| 79 | std::transform(args.begin(), args.end(), input_shapes.begin(), [&](auto& arg) { |
| 80 | return (arg == ins) ? new_shape : arg->get_shape(); |
| 81 | }); |
| 82 | |
| 83 | if(not try_compute_shape(output, input_shapes, output->module_inputs())) |
| 84 | { |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | catch(const std::exception& e) |
| 90 | { |
| 91 | if(enabled(MIGRAPHX_TRACE_ELIMINATE_CONTIGUOUS{})) |
| 92 | { |
| 93 | std::cout << "Exception: " << e.what() << std::endl; |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | catch(...) |
no test coverage detected