| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  GeometryComputer.hpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2020/04/01.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef GeometryComputer_hpp
 | 
					
						
							|  |  |  | #define GeometryComputer_hpp
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include "MNN_generated.h"
 | 
					
						
							|  |  |  | #include "core/Command.hpp"
 | 
					
						
							|  |  |  | #include "core/TensorUtils.hpp"
 | 
					
						
							| 
									
										
										
										
											2021-06-11 17:17:13 +08:00
										 |  |  | #include "core/Backend.hpp"
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | class GeometryComputer { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual ~GeometryComputer() { | 
					
						
							|  |  |  |         // Do nothing
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     class MNN_PUBLIC Context { | 
					
						
							|  |  |  |     public: | 
					
						
							| 
									
										
										
										
											2024-06-15 15:39:59 +08:00
										 |  |  |         Context(int mask, std::shared_ptr<Backend> allocBackend, MNNForwardType type = MNN_FORWARD_CPU, BackendConfig::PrecisionMode precision = BackendConfig::Precision_Normal); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         ~Context(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void clear(); | 
					
						
							|  |  |  |         void setBackend(Backend* backend); | 
					
						
							| 
									
										
										
										
											2022-06-10 10:39:50 +08:00
										 |  |  |         void getRasterCacheCreateRecursive(Tensor* src, CommandBuffer& cmd); | 
					
						
							| 
									
										
										
										
											2021-06-11 17:17:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // If has cache, return. Otherwise create cache
 | 
					
						
							|  |  |  |         const std::vector<std::shared_ptr<Tensor>>& searchConst(const Op* op); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         std::shared_ptr<Tensor> allocConst(const Op* key, const std::vector<int>& shape, halide_type_t type, | 
					
						
							|  |  |  |                                            Tensor::DimensionType dimType = Tensor::TENSORFLOW); | 
					
						
							| 
									
										
										
										
											2021-04-08 15:34:23 +08:00
										 |  |  |         bool allocTensor(Tensor* tenosr); | 
					
						
							| 
									
										
										
										
											2021-06-11 17:17:13 +08:00
										 |  |  |         inline MNNForwardType forwardType() const { | 
					
						
							|  |  |  |             return mForwardType; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-07-18 09:36:26 +08:00
										 |  |  |         inline BackendConfig::PrecisionMode precisionType() const { | 
					
						
							|  |  |  |             return mPrecision; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-06-15 15:39:59 +08:00
										 |  |  |         inline bool support(int option) const { | 
					
						
							|  |  |  |             return mMask & option; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-04 10:50:40 +08:00
										 |  |  |         std::shared_ptr<BufferStorage> mRasterOp; | 
					
						
							| 
									
										
										
										
											2024-08-24 15:46:21 +08:00
										 |  |  |         bool mNeedRelease = true; | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     private: | 
					
						
							| 
									
										
										
										
											2021-04-08 15:34:23 +08:00
										 |  |  |         void getRasterCacheCreate(Tensor* src, CommandBuffer& cmd); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         std::map<const Op*, std::vector<std::shared_ptr<Tensor>>> mConstTensors; | 
					
						
							|  |  |  |         std::vector<std::shared_ptr<Tensor>> mEmpty; | 
					
						
							| 
									
										
										
										
											2021-06-11 17:17:13 +08:00
										 |  |  |         std::vector<std::shared_ptr<Tensor>> mTempConstTensors; | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         std::shared_ptr<Backend> mBackend; | 
					
						
							| 
									
										
										
										
											2021-06-11 17:17:13 +08:00
										 |  |  |         MNNForwardType mForwardType; | 
					
						
							| 
									
										
										
										
											2023-07-18 09:36:26 +08:00
										 |  |  |         BackendConfig::PrecisionMode mPrecision; | 
					
						
							| 
									
										
										
										
											2024-06-15 15:39:59 +08:00
										 |  |  |         TensorUtils::FuseWrap mFuseUtils; | 
					
						
							|  |  |  |         const int mMask; | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  |     static void init(); | 
					
						
							| 
									
										
										
										
											2021-06-11 17:17:13 +08:00
										 |  |  |     MNN_PUBLIC static const GeometryComputer* search(int opType, Runtime::CompilerType compType); | 
					
						
							|  |  |  |     static void registerGeometryComputer(std::shared_ptr<GeometryComputer> comp, std::vector<int> type, Runtime::CompilerType compType = Runtime::Compiler_Geometry); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual bool onCompute(const Op* op, const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs, | 
					
						
							|  |  |  |                            Context& context, CommandBuffer& cmd) const = 0; | 
					
						
							| 
									
										
										
										
											2021-11-30 10:10:53 +08:00
										 |  |  |     virtual bool onRecompute(const Op* op, const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs, | 
					
						
							|  |  |  |                              Context& context, CommandBuffer& cmd) const { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-05-11 19:17:02 +08:00
										 |  |  |     static bool ComputePermuteRegion(Tensor* input, Tensor* output, int* newshape, int shapeDim); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DefaultGeometryComputer : public GeometryComputer { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     DefaultGeometryComputer() { | 
					
						
							|  |  |  |         // Do nothing
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-30 10:10:53 +08:00
										 |  |  |     virtual bool onRecompute(const Op* op, const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs, | 
					
						
							|  |  |  |                              Context& context, CommandBuffer& cmd) const override; | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     virtual bool onCompute(const Op* op, const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs, | 
					
						
							|  |  |  |                            Context& context, CommandBuffer& cmd) const override; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | void registerGeometryOps(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define REGISTER_GEOMETRY(f, c)       \
 | 
					
						
							|  |  |  |     extern void ___##f##__##c##__() { \ | 
					
						
							|  |  |  |         c();                          \ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace MNN
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |