| 152 | } |
| 153 | |
| 154 | std::vector<instruction_ref> insert_common_args(module& m, |
| 155 | instruction_ref ins, |
| 156 | std::vector<instruction_ref> inputs, |
| 157 | common_options options) |
| 158 | { |
| 159 | if(std::any_of( |
| 160 | inputs.cbegin(), inputs.cend(), [](auto input) { return input->get_shape().dynamic(); })) |
| 161 | { |
| 162 | auto input_shapes = to_shapes(inputs); |
| 163 | if(options.common_lens) |
| 164 | { |
| 165 | auto c_dyn_dims = compute_common_dyn_dims(input_shapes); |
| 166 | |
| 167 | auto s0 = inputs[0]->get_shape(); |
| 168 | // always add both multibroadcast instructions for dynamic shapes |
| 169 | inputs[0] = m.insert_instruction( |
| 170 | ins, make_op("multibroadcast", {{"out_dyn_dims", to_value(c_dyn_dims)}}), inputs); |
| 171 | std::transform(inputs.begin() + 1, inputs.end(), inputs.begin() + 1, [&](auto input) { |
| 172 | // uses previous input to avoid recalculating the common shape from the |
| 173 | // full set of input shapes at runtime |
| 174 | auto s = input->get_shape(); |
| 175 | return m.insert_instruction( |
| 176 | ins, |
| 177 | make_op("multibroadcast", {{"out_dyn_dims", to_value(c_dyn_dims)}}), |
| 178 | input, |
| 179 | inputs[0]); |
| 180 | }); |
| 181 | } |
| 182 | if(options.common_type) |
| 183 | { |
| 184 | auto c_type = compute_common_types(input_shapes); |
| 185 | std::transform(inputs.begin(), inputs.end(), inputs.begin(), [&](auto input) { |
| 186 | if(input->get_shape().type() != c_type) |
| 187 | { |
| 188 | input = m.insert_instruction( |
| 189 | ins, make_op("convert", {{"target_type", c_type}}), input); |
| 190 | } |
| 191 | return input; |
| 192 | }); |
| 193 | } |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | auto common = common_shape(to_shapes(inputs)); |
| 198 | std::transform(inputs.begin(), inputs.end(), inputs.begin(), [&](auto input) { |
| 199 | if(options.common_lens and input->get_shape().lens() != common.lens()) |
| 200 | { |
| 201 | input = m.insert_instruction( |
| 202 | ins, make_op("multibroadcast", {{"out_lens", common.lens()}}), input); |
| 203 | } |
| 204 | if(options.common_type and input->get_shape().type() != common.type()) |
| 205 | { |
| 206 | input = m.insert_instruction( |
| 207 | ins, make_op("convert", {{"target_type", common.type()}}), input); |
| 208 | } |
| 209 | return input; |
| 210 | }); |
| 211 | } |
no test coverage detected