| 49 | namespace concatenate { |
| 50 | |
| 51 | TensorLayout concat_layout_deduce( |
| 52 | const SmallVector<const TensorLayout*> inputs, int axis) { |
| 53 | // if we use megdnn::Concat::deduce_layout directly, we need construct |
| 54 | // TensorLayoutArray, which will result in much memory copy |
| 55 | auto shape_equal_but_specific_axis = [](const TensorShape& lhs, |
| 56 | const TensorShape& rhs, int axis) -> bool { |
| 57 | if (lhs.ndim != rhs.ndim) { |
| 58 | return false; |
| 59 | } |
| 60 | for (size_t i = 0; i < lhs.ndim; ++i) { |
| 61 | if (i == axis) |
| 62 | continue; |
| 63 | if (lhs.shape[i] != rhs.shape[i]) |
| 64 | return false; |
| 65 | } |
| 66 | return true; |
| 67 | }; |
| 68 | |
| 69 | TensorLayout oup_layout = *inputs[0]; |
| 70 | for (size_t i = 1; i < inputs.size(); ++i) { |
| 71 | mgb_assert( |
| 72 | shape_equal_but_specific_axis(oup_layout, *inputs[i], axis), |
| 73 | "Concat input shape mismatch: %s vs %s", inputs[0]->to_string().c_str(), |
| 74 | inputs[i]->to_string().c_str()); |
| 75 | oup_layout.shape[axis] += inputs[i]->shape[axis]; |
| 76 | } |
| 77 | oup_layout.init_contiguous_stride(); |
| 78 | return oup_layout; |
| 79 | } |
| 80 | |
| 81 | auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) { |
| 82 | auto&& op = static_cast<const Concat&>(def); |
no test coverage detected