| 
									
										
										
										
											2019-06-17 20:10:35 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  CPUSelect.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/5/22.
 | 
					
						
							|  |  |  | //  Copyright © 2018 Alibaba. All rights reserved.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "backend/cpu/CPUSelect.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-06-17 20:10:35 +08:00
										 |  |  | namespace MNN { | 
					
						
							| 
									
										
										
										
											2020-07-04 01:21:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 20:10:35 +08:00
										 |  |  | ErrorCode CPUSelect::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     auto inSize1 = inputs[1]->elementSize(); | 
					
						
							|  |  |  |     auto inSize2 = inputs[2]->elementSize(); | 
					
						
							|  |  |  |     auto outSize = outputs[0]->elementSize(); | 
					
						
							| 
									
										
										
										
											2020-12-15 14:12:35 +08:00
										 |  |  |     MNN_ASSERT(inputs[0]->elementSize() == outSize); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     MNN_ASSERT(inSize1 == 1 || inSize1 == outSize); | 
					
						
							|  |  |  |     MNN_ASSERT(inSize2 == 1 || inSize2 == outSize); | 
					
						
							|  |  |  |     auto output = outputs[0]->host<float>(); | 
					
						
							|  |  |  |     auto select = inputs[0]->host<int32_t>(); | 
					
						
							|  |  |  |     auto input0 = inputs[1]->host<float>(); | 
					
						
							|  |  |  |     auto input1 = inputs[2]->host<float>(); | 
					
						
							|  |  |  |     for (int i = 0; i < outSize; i++) { | 
					
						
							|  |  |  |         if (select[i]) { | 
					
						
							|  |  |  |             if (inSize1 == 1) { | 
					
						
							|  |  |  |                 output[i] = input0[0]; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 output[i] = input0[i]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-04 01:21:30 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |             if (inSize2 == 1) { | 
					
						
							|  |  |  |                 output[i] = input1[0]; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 output[i] = input1[i]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-04 01:21:30 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-17 20:10:35 +08:00
										 |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-07-04 01:21:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 20:10:35 +08:00
										 |  |  | class CPUSelectCreator : public CPUBackend::Creator { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs, | 
					
						
							|  |  |  |                                 const MNN::Op *op, Backend *backend) const { | 
					
						
							|  |  |  |         return new CPUSelect(backend); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | REGISTER_CPU_OP_CREATOR(CPUSelectCreator, OpType_Select); | 
					
						
							|  |  |  | } // namespace MNN
 |