| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  GLReshape.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/01/31.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 13:33:47 +08:00
										 |  |  | #include "backend/opengl/GLReshape.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | #include <sstream>
 | 
					
						
							| 
									
										
										
										
											2020-01-15 13:33:47 +08:00
										 |  |  | #include "AllShader.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "backend/opengl/GLBackend.hpp"
 | 
					
						
							|  |  |  | #include "core/Macro.h"
 | 
					
						
							|  |  |  | #include "core/TensorUtils.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | namespace OpenGL { | 
					
						
							|  |  |  | GLReshape::GLReshape(const std::vector<Tensor *> &inputs, const Op *op, Backend *bn) : Execution(bn) { | 
					
						
							| 
									
										
										
											
												- dynamic computation graph (beta)
	- add supports (/express)
	- add tests
	- add benchmarks with it (/benchmark/exprModels)
- Python
	- MNN engine and tools were submitted to pip
	- available on Windows/macOS/Linux
- Engine/Converter
	- add supports for each op benchmarking
	- refactor optimizer by separating steps
- CPU
	- add supports for Conv3D, Pool3D, ELU, ReverseSequence
	- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
	- add half transform in CPU
	- add broadcast supports for binary
	- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
	- add sub, real div supports for binary
	- add supports for unary
	- optimize Conv2D, Reshape
- Vulkan
	- add max supports for eltwise
- Metal
	- fix metallib missing problem
- Train/Quantization
	- use express to refactor training codes
											
										 
											2019-09-26 21:02:07 +08:00
										 |  |  |     mDimType = op->main_as_Reshape()->dimType(); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLReshape::~GLReshape() { | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | ErrorCode GLReshape::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) { | 
					
						
							|  |  |  |     std::vector<std::string> prefix; | 
					
						
							|  |  |  |     setLocalSize(prefix, mLocalSize, 8, 8, 1); | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     mTempBuffer.reset(new GLSSBOBuffer(inputs[0]->size())); | 
					
						
							| 
									
										
										
											
												- dynamic computation graph (beta)
	- add supports (/express)
	- add tests
	- add benchmarks with it (/benchmark/exprModels)
- Python
	- MNN engine and tools were submitted to pip
	- available on Windows/macOS/Linux
- Engine/Converter
	- add supports for each op benchmarking
	- refactor optimizer by separating steps
- CPU
	- add supports for Conv3D, Pool3D, ELU, ReverseSequence
	- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
	- add half transform in CPU
	- add broadcast supports for binary
	- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
	- add sub, real div supports for binary
	- add supports for unary
	- optimize Conv2D, Reshape
- Vulkan
	- add max supports for eltwise
- Metal
	- fix metallib missing problem
- Train/Quantization
	- use express to refactor training codes
											
										 
											2019-09-26 21:02:07 +08:00
										 |  |  |     auto input = inputs[0]; | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												- dynamic computation graph (beta)
	- add supports (/express)
	- add tests
	- add benchmarks with it (/benchmark/exprModels)
- Python
	- MNN engine and tools were submitted to pip
	- available on Windows/macOS/Linux
- Engine/Converter
	- add supports for each op benchmarking
	- refactor optimizer by separating steps
- CPU
	- add supports for Conv3D, Pool3D, ELU, ReverseSequence
	- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
	- add half transform in CPU
	- add broadcast supports for binary
	- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
	- add sub, real div supports for binary
	- add supports for unary
	- optimize Conv2D, Reshape
- Vulkan
	- add max supports for eltwise
- Metal
	- fix metallib missing problem
- Train/Quantization
	- use express to refactor training codes
											
										 
											2019-09-26 21:02:07 +08:00
										 |  |  |     if (mDimType == MNN_DATA_FORMAT_NCHW) { | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |         mSrcProgram = ((GLBackend *)backend())->getProgram("src", glsl_image_to_nchw_buffer_glsl, prefix); | 
					
						
							|  |  |  |         mDstProgram = ((GLBackend *)backend())->getProgram("dst", glsl_nchw_buffer_to_image_glsl, prefix); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     }else{ | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |         mSrcProgram = ((GLBackend *)backend())->getProgram("src", glsl_image_to_nhwc_buffer_glsl, prefix); | 
					
						
							|  |  |  |         mDstProgram = ((GLBackend *)backend())->getProgram("dst", glsl_nhwc_buffer_to_image_glsl, prefix); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode GLReshape::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) { | 
					
						
							|  |  |  |     auto input = inputs[0]; | 
					
						
							|  |  |  |     auto output = outputs[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     std::vector<int> inputShape  = tensorShapeFormat(input); | 
					
						
							|  |  |  |     std::vector<int> outputShape = tensorShapeFormat(output); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int ib = inputShape.at(0); | 
					
						
							|  |  |  |     int ih = inputShape.at(1); | 
					
						
							|  |  |  |     int iw = inputShape.at(2); | 
					
						
							|  |  |  |     int ic = inputShape.at(3); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     int ic_4 = UP_DIV(ic, 4); | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     int ob = outputShape.at(0); | 
					
						
							|  |  |  |     int oh = outputShape.at(1); | 
					
						
							|  |  |  |     int ow = outputShape.at(2); | 
					
						
							|  |  |  |     int oc = outputShape.at(3); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     int oc_4 = UP_DIV(oc, 4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												- dynamic computation graph (beta)
	- add supports (/express)
	- add tests
	- add benchmarks with it (/benchmark/exprModels)
- Python
	- MNN engine and tools were submitted to pip
	- available on Windows/macOS/Linux
- Engine/Converter
	- add supports for each op benchmarking
	- refactor optimizer by separating steps
- CPU
	- add supports for Conv3D, Pool3D, ELU, ReverseSequence
	- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
	- add half transform in CPU
	- add broadcast supports for binary
	- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
	- add sub, real div supports for binary
	- add supports for unary
	- optimize Conv2D, Reshape
- Vulkan
	- add max supports for eltwise
- Metal
	- fix metallib missing problem
- Train/Quantization
	- use express to refactor training codes
											
										 
											2019-09-26 21:02:07 +08:00
										 |  |  |     if (mDimType == MNN_DATA_FORMAT_NCHW) { | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |         //image -> buffer(nchw)
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             mSrcProgram->useProgram(); | 
					
						
							| 
									
										
										
										
											2019-07-25 13:36:35 +08:00
										 |  |  |             glBindImageTexture(0, input->deviceId(), 0, GL_TRUE, 0, GL_READ_ONLY, ((GLBackend *)backend())->getTextrueFormat()); | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |             glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId()); | 
					
						
							|  |  |  |             glUniform1i(2, iw); | 
					
						
							|  |  |  |             glUniform1i(3, ih); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |             ((GLBackend *)backend())->compute(UP_DIV(iw, mLocalSize[0]), UP_DIV(ih, mLocalSize[1]), UP_DIV(ic_4, mLocalSize[2])); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //buffer(nchw) -> image
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             mDstProgram->useProgram(); | 
					
						
							| 
									
										
										
										
											2019-07-25 13:36:35 +08:00
										 |  |  |             glBindImageTexture(0, output->deviceId(), 0, GL_TRUE, 0, GL_WRITE_ONLY, ((GLBackend *)backend())->getTextrueFormat()); | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |             glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId()); | 
					
						
							|  |  |  |             glUniform1i(2, ow); | 
					
						
							|  |  |  |             glUniform1i(3, oh); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |             ((GLBackend *)backend())->compute(UP_DIV(ow, mLocalSize[0]), UP_DIV(oh, mLocalSize[1]), UP_DIV(oc_4, mLocalSize[2])); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }else{ | 
					
						
							|  |  |  |         //image -> buffer(nhwc)
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             mSrcProgram->useProgram(); | 
					
						
							| 
									
										
										
										
											2019-07-25 13:36:35 +08:00
										 |  |  |             glBindImageTexture(0, input->deviceId(), 0, GL_TRUE, 0, GL_READ_ONLY, ((GLBackend *)backend())->getTextrueFormat()); | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |             glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId()); | 
					
						
							|  |  |  |             glUniform1i(2, iw); | 
					
						
							|  |  |  |             glUniform1i(3, ih); | 
					
						
							|  |  |  |             glUniform1i(4, ic); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |             ((GLBackend *)backend())->compute(UP_DIV(iw, mLocalSize[0]), UP_DIV(ih, mLocalSize[1]), UP_DIV(ic_4, mLocalSize[2])); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //buffer -> image
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             mDstProgram->useProgram(); | 
					
						
							| 
									
										
										
										
											2019-07-25 13:36:35 +08:00
										 |  |  |             glBindImageTexture(0, output->deviceId(), 0, GL_TRUE, 0, GL_WRITE_ONLY, ((GLBackend *)backend())->getTextrueFormat()); | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |             glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId()); | 
					
						
							|  |  |  |             glUniform1i(2, ow); | 
					
						
							|  |  |  |             glUniform1i(3, oh); | 
					
						
							|  |  |  |             glUniform1i(4, oc); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |             ((GLBackend *)backend())->compute(UP_DIV(ow, mLocalSize[0]), UP_DIV(oh, mLocalSize[1]), UP_DIV(oc_4, mLocalSize[2])); | 
					
						
							|  |  |  |             OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  | class ReshapeCreator : public GLBackend::Creator { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual ~ReshapeCreator() = default; | 
					
						
							|  |  |  |     virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs, | 
					
						
							|  |  |  |                                 const MNN::Op *op, Backend *backend) const override { | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |         if(inputs[0]->dimensions() == 3 || outputs[0]->dimensions() == 3){ | 
					
						
							|  |  |  |             MNN_PRINT("reshape not support dimensions == 3 \n"); | 
					
						
							|  |  |  |             return nullptr; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return new GLReshape(inputs, op, backend); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | GLCreatorRegister<ReshapeCreator> __reshape_op(OpType_Reshape); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | } // namespace OpenGL
 | 
					
						
							|  |  |  | } // namespace MNN
 |