| 116 | |
| 117 | template <class F> |
| 118 | static void remove_contiguous(const std::string& op_name, module& m, F f) |
| 119 | { |
| 120 | auto last = std::prev(m.end()); |
| 121 | std::vector<instruction_ref> const_instructions; |
| 122 | |
| 123 | for(auto ins : iterator_for(m)) |
| 124 | { |
| 125 | // return instruction should have inputs with standard shape |
| 126 | if(ins->name() == "@return") |
| 127 | continue; |
| 128 | |
| 129 | if(ins != last and ins->outputs().empty()) |
| 130 | continue; |
| 131 | |
| 132 | if(not f(ins)) |
| 133 | continue; |
| 134 | |
| 135 | auto args = ins->inputs(); |
| 136 | auto mod_args = ins->module_inputs(); |
| 137 | |
| 138 | for(auto arg : ins->inputs()) |
| 139 | { |
| 140 | if(arg->name() != op_name) |
| 141 | continue; |
| 142 | if(enabled(MIGRAPHX_TRACE_ELIMINATE_CONTIGUOUS{})) |
| 143 | { |
| 144 | std::cout << "eliminate_contiguous: "; |
| 145 | m.debug_print(ins); |
| 146 | } |
| 147 | auto prev = arg->inputs().front(); |
| 148 | // create copy of args each time as they are modified inside the loop |
| 149 | auto new_args = ins->inputs(); |
| 150 | replace(new_args, arg, prev); |
| 151 | if(try_compute_shape(ins, new_args, mod_args)) |
| 152 | { |
| 153 | instruction::replace_argument(ins, arg, prev); |
| 154 | } |
| 155 | else if(prev->can_eval()) |
| 156 | { |
| 157 | const_instructions.push_back(arg); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Perform static contiguous evaluations in parallel |
| 163 | std::vector<argument> literals(const_instructions.size()); |
| 164 | par_for(const_instructions.size(), 1, [&](const auto i) { |
| 165 | auto c = op::contiguous{}; |
| 166 | auto prev = const_instructions[i]->inputs().front(); |
| 167 | // compute the output contiguous shape from the previous instruction shape |
| 168 | shape computed_shape = c.compute_shape({prev->get_shape()}); |
| 169 | const std::vector<argument>& prev_eval = {prev->eval()}; |
| 170 | // prev_eval should not be used in make_compute_output_shape() as computed_shape is static |
| 171 | auto co_shape = make_compute_output_shape(pack(c, computed_shape, prev_eval)); |
| 172 | literals[i] = c.compute(co_shape, prev_eval); |
| 173 | }); |
| 174 | |
| 175 | // Replace static contiguous operations with a literal |
no test coverage detected