| 385 | |
| 386 | template <typename T> |
| 387 | ValueRefList setsubtensor_rule( |
| 388 | const T& op, Span<ValueRef>& inputs, const bool& auto_convert, |
| 389 | const FormatTransformation& t) { |
| 390 | mgb_assert(inputs.size() >= 2); |
| 391 | auto& src = inputs[0].cast(t.value_type()); |
| 392 | bool is_reduce_ndim = is_reduce_ndim_idx_items( |
| 393 | op.items, {&inputs[2], &inputs[inputs.size() - 1]}); |
| 394 | if (!is_reduce_ndim) { |
| 395 | // only support NHWC2NCHW convert, otherwise maintain src's format |
| 396 | if (!(auto_convert && src.format() == FT::NHWC)) { |
| 397 | return {t.wrap_output( |
| 398 | imperative::apply(op, t.unwrap_inputs(inputs))[0], src.format())}; |
| 399 | } |
| 400 | // value has been broadcasted to src's fake NCHW shape. |
| 401 | auto& value = inputs[1].cast(t.value_type()); |
| 402 | auto& format = value.format(); |
| 403 | auto nhwc_inputs = ValueRefList(inputs.size()); |
| 404 | if (format == FT::DEFAULT || format == FT::NCHW) { |
| 405 | // value for setsubtensor should transpose to match shape. |
| 406 | auto nhwc_value = t.to(value, FT::NHWC); |
| 407 | // make new inputs for setsubtensor |
| 408 | nhwc_inputs[0] = src.value(); |
| 409 | nhwc_inputs[1] = nhwc_value->value(); |
| 410 | for (auto i = 2; i < inputs.size(); ++i) { |
| 411 | nhwc_inputs[i] = t.unwrap_input(inputs[i]); |
| 412 | } |
| 413 | } else if (format != FT::NHWC) { |
| 414 | mgb_throw( |
| 415 | MegBrainError, "Unsupported format(%s) of value for setsubtensor.", |
| 416 | format.to_string().c_str()); |
| 417 | } |
| 418 | auto nhwc_items = convert_nchw2nhwc_idx_items(op.items); |
| 419 | auto outputs = imperative::apply( |
| 420 | *T::make(std::move(nhwc_items), op.scope()), nhwc_inputs); |
| 421 | return t.wrap_outputs(outputs, FT::NHWC); |
| 422 | } |
| 423 | return t.wrap_outputs(imperative::apply(op, t.unwrap_inputs(inputs))); |
| 424 | } |
| 425 | |
| 426 | inline FT get_inputs_format(Span<ValueRef>& inputs, const FormatTransformation& t) { |
| 427 | FT format(FT::DEFAULT); |
nothing calls this directly
no test coverage detected