| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  ShapeConcat.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/01/10.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Macro.h"
 | 
					
						
							|  |  |  | #include "SizeComputer.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-06-05 10:45:59 +08:00
										 |  |  | #include "TensorUtils.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | class ConcatSizeComputer : public SizeComputer { | 
					
						
							|  |  |  |     virtual bool onComputeSize(const MNN::Op* op, const std::vector<Tensor*>& inputs, | 
					
						
							|  |  |  |                                const std::vector<Tensor*>& outputs) const override { | 
					
						
							|  |  |  |         MNN_ASSERT(1 == outputs.size()); | 
					
						
							|  |  |  |         MNN_ASSERT(inputs.size() >= 2); | 
					
						
							| 
									
										
										
										
											2019-06-05 10:45:59 +08:00
										 |  |  |         auto& ob      = outputs[0]->buffer(); | 
					
						
							|  |  |  |         int basicAxis = 0; | 
					
						
							|  |  |  |         if (op->type() == OpType_Concat) { | 
					
						
							|  |  |  |             basicAxis = op->main_as_Axis()->axis(); | 
					
						
							|  |  |  |         } else if (op->type() == OpType_QuantizedConcat) { | 
					
						
							|  |  |  |             basicAxis = op->main_as_QuantizedConcat()->axis(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 10:45:59 +08:00
										 |  |  |         int axis = basicAxis; | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         // Concat-inputs may have scalar which should be delete
 | 
					
						
							|  |  |  |         for (const auto& input : inputs) { | 
					
						
							|  |  |  |             if (0 == input->buffer().dimensions) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 auto inputDimensions = input->buffer().dimensions; | 
					
						
							|  |  |  |                 ::memcpy(ob.dim, input->buffer().dim, sizeof(halide_dimension_t) * inputDimensions); | 
					
						
							|  |  |  |                 ob.dimensions = inputDimensions; | 
					
						
							|  |  |  |                 ob.type       = input->buffer().type; | 
					
						
							| 
									
										
										
										
											2019-06-05 10:45:59 +08:00
										 |  |  |                 if (axis < 0) { | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |                     axis = inputDimensions + axis; | 
					
						
							| 
									
										
										
										
											2019-06-05 10:45:59 +08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         int sum = 0; | 
					
						
							|  |  |  |         for (auto t : inputs) { | 
					
						
							|  |  |  |             sum += t->buffer().dim[axis].extent; | 
					
						
							|  |  |  |             for (int i = 0; i < t->dimensions(); ++i) { | 
					
						
							|  |  |  |                 if (axis == i) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (t->length(i) != outputs[0]->length(i)) { | 
					
						
							|  |  |  |                     MNN_PRINT("Error for concat size of op %s, %d input not match output\n", op->name()->c_str(), i); | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-05 10:45:59 +08:00
										 |  |  |         ob.dim[axis].extent                                   = sum; | 
					
						
							|  |  |  |         ob.type                                               = inputs[0]->buffer().type; | 
					
						
							|  |  |  |         TensorUtils::getDescribe(outputs[0])->dimensionFormat = TensorUtils::getDescribe(inputs[0])->dimensionFormat; | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | REGISTER_SHAPE(ConcatSizeComputer, OpType_Concat); | 
					
						
							| 
									
										
										
										
											2019-06-05 10:45:59 +08:00
										 |  |  | REGISTER_SHAPE(ConcatSizeComputer, OpType_QuantizedConcat); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | } // namespace MNN
 |