| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  ShapeUnpack.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/01/10.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | #include "shape/SizeComputer.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "core/Macro.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UnpackComputer : public SizeComputer { | 
					
						
							|  |  |  |     virtual bool onComputeSize(const MNN::Op *op, const std::vector<Tensor *> &inputs, | 
					
						
							|  |  |  |                                const std::vector<Tensor *> &outputs) const override { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         if (nullptr == op || inputs.empty() || outputs.empty()) { | 
					
						
							| 
									
										
										
										
											2020-04-11 20:37:20 +08:00
										 |  |  |             // Avoid crash for special model
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         auto unpack    = op->main_as_Axis(); | 
					
						
							| 
									
										
										
										
											2020-05-07 18:19:02 +08:00
										 |  |  |         int axis = unpack->axis(); | 
					
						
							|  |  |  |         if (axis < 0) { | 
					
						
							|  |  |  |             axis += inputs[0]->dimensions(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         auto &input = inputs[0]->buffer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const int inputDimensions = input.dimensions; | 
					
						
							|  |  |  |         MNN_ASSERT(1 <= inputDimensions); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         int32_t outDims[MNN_MAX_TENSOR_DIM]; | 
					
						
							|  |  |  |         if (outputs.size() > input.dim[axis].extent) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         for (int i = 0; i < axis; i++) { | 
					
						
							|  |  |  |             outDims[i] = input.dim[i].extent; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for (int i = axis + 1; i < inputDimensions; i++) { | 
					
						
							|  |  |  |             outDims[i - 1] = input.dim[i].extent; | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         const int outputDimensions = inputDimensions - 1; | 
					
						
							|  |  |  |         for (int i = 0; i < outputs.size(); i++) { | 
					
						
							|  |  |  |             auto &output      = outputs[i]->buffer(); | 
					
						
							|  |  |  |             output.dimensions = outputDimensions; | 
					
						
							|  |  |  |             output.type       = input.type; | 
					
						
							|  |  |  |             for (int j = 0; j < outputDimensions; j++) { | 
					
						
							|  |  |  |                 output.dim[j].extent = outDims[j]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-08-22 20:13:46 +08:00
										 |  |  |             TensorUtils::getDescribe(outputs[i])->dimensionFormat = TensorUtils::getDescribe(inputs[0])->dimensionFormat; | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | REGISTER_SHAPE(UnpackComputer, OpType_Unpack); | 
					
						
							|  |  |  | } // namespace MNN
 |