| 680 | } // namespace |
| 681 | |
| 682 | ValueRefList FormatTransformation::apply_transformation( |
| 683 | const Operator& op, Span<ValueRef> inputs) { |
| 684 | if (m_bypass_format_transoformation) { |
| 685 | return imperative::apply(op, unwrap_inputs(inputs)); |
| 686 | } |
| 687 | if (auto* apply_op = op.as<ApplyOp>()) { |
| 688 | // bypass SymbolValue |
| 689 | if (!check_all_format_value(inputs)) { |
| 690 | return imperative::apply(op, unwrap_inputs(inputs)); |
| 691 | } |
| 692 | // all inputs should be FormattedTensorValue |
| 693 | auto iter = format_rules.find(apply_op->op().dyn_typeinfo()); |
| 694 | if (iter != format_rules.end()) { |
| 695 | return iter->second(apply_op->op(), inputs, m_auto_convert, *this); |
| 696 | } else { |
| 697 | auto unified_inputs = unify_inputs_format( |
| 698 | inputs, FT::DEFAULT, apply_op->op().scope(), *this); |
| 699 | return wrap_outputs(imperative::apply(op, unwrap_inputs(unified_inputs))); |
| 700 | } |
| 701 | } else if (auto* create_tensor = op.as<CreateTensor>()) { |
| 702 | auto format = create_tensor->format(); |
| 703 | if (format == FT::NHWC) { |
| 704 | auto output = wrap_output(imperative::apply(op, inputs)[0]); |
| 705 | output = to(output.cast(m_value_type), FT::NHWC, ""); |
| 706 | return {output}; |
| 707 | } else { |
| 708 | return {wrap_output(imperative::apply(op, inputs)[0], format)}; |
| 709 | } |
| 710 | } else if (auto* get_attr = op.as<GetAttr>()) { |
| 711 | auto&& input = inputs.item(); |
| 712 | if (!input.is(m_value_type)) { |
| 713 | return imperative::apply(op, input); |
| 714 | } |
| 715 | auto& src = input.cast(m_value_type); |
| 716 | if (!(m_auto_convert && src.format() == FT::NHWC)) { |
| 717 | return imperative::apply(op, unwrap_inputs(inputs)); |
| 718 | } |
| 719 | switch (get_attr->attr()) { |
| 720 | case GetAttr::Shape: { |
| 721 | auto output = imperative::apply(op, unwrap_inputs(inputs))[0]; |
| 722 | auto shape = convert_nhwc2nchw_shape(output.cast<ShapeValue>()); |
| 723 | return {ShapeValue::make(shape)}; |
| 724 | } |
| 725 | case GetAttr::Value: { |
| 726 | auto nchw_src = unwrap_input(to(src, FT::DEFAULT, "")); |
| 727 | return imperative::apply(op, {nchw_src}); |
| 728 | } |
| 729 | default: |
| 730 | return imperative::apply(op, unwrap_inputs(inputs)); |
| 731 | } |
| 732 | } else if (op.is<GetFormat>()) { |
| 733 | auto&& inp_ref = inputs[0].as_ref(m_value_type); |
| 734 | if (inp_ref) { |
| 735 | return {FormatValue::make(inp_ref->format())}; |
| 736 | } else { |
| 737 | MGE_CALL_ONCE(mgb_log_warn( |
| 738 | "Not FormattedTensorValue input for GetFormat op: %s, %s", |
| 739 | op.to_string().c_str(), inputs[0].to_string().c_str())); |
nothing calls this directly
no test coverage detected