| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  ShapeOneHot.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/11/29.
 | 
					
						
							|  |  |  | //  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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | class ShapeOneHot : public SizeComputer { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual bool onComputeSize(const MNN::Op* op, const std::vector<Tensor*>& inputs, | 
					
						
							|  |  |  |                                const std::vector<Tensor*>& outputs) const override { | 
					
						
							|  |  |  |         MNN_ASSERT(4 == inputs.size()); | 
					
						
							|  |  |  |         auto indices     = inputs[0]; | 
					
						
							|  |  |  |         auto depthTensor = inputs[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const int depth = depthTensor->host<int>()[0]; | 
					
						
							|  |  |  |         if (depth < 0) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const int indicesDimension = indices->dimensions(); | 
					
						
							|  |  |  |         const int outputDimension  = indicesDimension + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto param = op->main_as_OneHotParam(); | 
					
						
							|  |  |  |         int axis = param->axis(); | 
					
						
							| 
									
										
										
										
											2021-04-28 18:02:10 +08:00
										 |  |  |         if (axis < 0) { | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  |             axis = outputDimension + axis; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         auto output                 = outputs[0]; | 
					
						
							|  |  |  |         output->buffer().dimensions = outputDimension; | 
					
						
							| 
									
										
										
										
											2020-01-15 13:33:47 +08:00
										 |  |  |         output->buffer().type = inputs[2]->buffer().type; | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  |         for (int i = 0; i < outputDimension; ++i) { | 
					
						
							|  |  |  |             if (i < axis) { | 
					
						
							|  |  |  |                 output->setLength(i, indices->length(i)); | 
					
						
							|  |  |  |             } else if (i == axis) { | 
					
						
							|  |  |  |                 output->setLength(i, depth); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 output->setLength(i, indices->length(i - 1)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-12 09:36:34 +08:00
										 |  |  |         TensorUtils::getDescribe(output)->dimensionFormat = TensorUtils::getDescribe(inputs[0])->dimensionFormat; | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | REGISTER_SHAPE_INPUTS(ShapeOneHot, OpType_OneHot, (std::vector<int>{1})); | 
					
						
							|  |  |  | } // namespace MNN
 |