| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  CPUBatchMatMul.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/03/25.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "backend/cpu/CPUBatchMatMul.hpp"
 | 
					
						
							|  |  |  | #include "backend/cpu/CPUBackend.hpp"
 | 
					
						
							|  |  |  | #include "math/Matrix.hpp"
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | #include "core/TensorUtils.hpp"
 | 
					
						
							|  |  |  | #include "core/BufferAllocator.hpp"
 | 
					
						
							|  |  |  | #include "core/Concurrency.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												- build:
	- unify schema building in core and converter;
	- add more build script for android;
	- add linux build script for python;
- ops impl:
	- add floor mod support in binary;
	- use eltwise impl in add/max/sub/mul binary for optimization;
	- remove fake double support in cast;
	- fix 5d support for concat;
	- add adjX and adjY support for batch matmul;
	- optimize conv2d back prop filter;
	- add pad mode support for conv3d;
	- fix bug in conv2d & conv depthwise with very small feature map;
	- optimize binary without broacast;
	- add data types support for gather;
	- add gather ND support;
	- use uint8 data type in gather v2;
	- add transpose support for matmul;
	- add matrix band part;
	- add dim != 4 support for padding, reshape & tensor convert;
	- add pad type support for pool3d;
	- make ops based on TensorFlow Lite quantization optional;
	- add all & any support for reduction;
	- use type in parameter as output type in reduction;
	- add int support for unary;
	- add variable weight support for conv2d;
	- fix conv2d depthwise weights initialization;
	- fix type support for transpose;
	- fix grad outputs count for  reduce grad and reshape grad;
	- fix priorbox & detection output;
	- fix metal softmax error;
- python:
	- add runSessionWithCallBackInfo interface;
	- add max nodes limit (1400) for visualization tool;
	- fix save error in python3;
	- align default dim;
- convert:
	- add extra design for optimization;
	- add more post converting optimizers;
	- add caffe v1 weights blob support;
	- add cast, unary, conv transpose support for onnx model;
	- optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model;
	- add cos/sin/atan/tan support for unary for tensorflow model;
	- add any/all support for reduction for tensorflow model;
	- add elu, conv3d, pool3d support for tensorflow model;
	- optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model;
- others:
	- fix size computer lock;
	- fix thread pool deadlock;
	- add express & parameters in express;
	- rewrite blitter chooser without static map;
	- add tests for expr;
											
										 
											2019-10-29 13:37:26 +08:00
										 |  |  | CPUBatchMatMul::CPUBatchMatMul(Backend* backend, bool adjX, bool adjY) : Execution(backend) { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     auto threadNumber = static_cast<CPUBackend*>(backend)->threadNumber(); | 
					
						
							|  |  |  |     for (int i = 0; i < threadNumber; ++i) { | 
					
						
							|  |  |  |         Unit unit; | 
					
						
							|  |  |  |         unit.mMatrixA.reset(new Tensor); | 
					
						
							|  |  |  |         unit.mMatrixB.reset(new Tensor); | 
					
						
							|  |  |  |         unit.mMatrixC.reset(new Tensor); | 
					
						
							|  |  |  |         unit.mMatMul.reset(new CPUMatMul(backend, adjX, adjY, false)); | 
					
						
							|  |  |  |         unit.mMatrixB->buffer().dimensions = 2; | 
					
						
							|  |  |  |         unit.mMatrixA->buffer().dimensions = 2; | 
					
						
							|  |  |  |         unit.mMatrixC->buffer().dimensions = 2; | 
					
						
							|  |  |  |         unit.mTempInputs = {unit.mMatrixA.get(), unit.mMatrixB.get()}; | 
					
						
							|  |  |  |         unit.mTempOutputs = {unit.mMatrixC.get()}; | 
					
						
							|  |  |  |         mUnits.emplace_back(std::move(unit)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode CPUBatchMatMul::onResize(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) { | 
					
						
							|  |  |  |     auto input0          = inputs[0]; | 
					
						
							|  |  |  |     auto input1          = inputs[1]; | 
					
						
							|  |  |  |     auto output          = outputs[0]; | 
					
						
							| 
									
										
										
										
											2020-07-04 01:21:30 +08:00
										 |  |  |     // Fill output by zero if one of inputs is empty.
 | 
					
						
							|  |  |  |     if (input0->elementSize() == 0 || input1->elementSize() == 0) { | 
					
						
							|  |  |  |         return NO_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
											  
											
												- build:
	- unify schema building in core and converter;
	- add more build script for android;
	- add linux build script for python;
- ops impl:
	- add floor mod support in binary;
	- use eltwise impl in add/max/sub/mul binary for optimization;
	- remove fake double support in cast;
	- fix 5d support for concat;
	- add adjX and adjY support for batch matmul;
	- optimize conv2d back prop filter;
	- add pad mode support for conv3d;
	- fix bug in conv2d & conv depthwise with very small feature map;
	- optimize binary without broacast;
	- add data types support for gather;
	- add gather ND support;
	- use uint8 data type in gather v2;
	- add transpose support for matmul;
	- add matrix band part;
	- add dim != 4 support for padding, reshape & tensor convert;
	- add pad type support for pool3d;
	- make ops based on TensorFlow Lite quantization optional;
	- add all & any support for reduction;
	- use type in parameter as output type in reduction;
	- add int support for unary;
	- add variable weight support for conv2d;
	- fix conv2d depthwise weights initialization;
	- fix type support for transpose;
	- fix grad outputs count for  reduce grad and reshape grad;
	- fix priorbox & detection output;
	- fix metal softmax error;
- python:
	- add runSessionWithCallBackInfo interface;
	- add max nodes limit (1400) for visualization tool;
	- fix save error in python3;
	- align default dim;
- convert:
	- add extra design for optimization;
	- add more post converting optimizers;
	- add caffe v1 weights blob support;
	- add cast, unary, conv transpose support for onnx model;
	- optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model;
	- add cos/sin/atan/tan support for unary for tensorflow model;
	- add any/all support for reduction for tensorflow model;
	- add elu, conv3d, pool3d support for tensorflow model;
	- optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model;
- others:
	- fix size computer lock;
	- fix thread pool deadlock;
	- add express & parameters in express;
	- rewrite blitter chooser without static map;
	- add tests for expr;
											
										 
											2019-10-29 13:37:26 +08:00
										 |  |  |     auto dimensions = input0->dimensions(); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     int threadNumber = static_cast<CPUBackend*>(backend())->threadNumber(); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     int batch = 1; | 
					
						
							|  |  |  |     for (int i = 0; i < dimensions - 2; ++i) { | 
					
						
							|  |  |  |         batch *= input0->length(i); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     mBatch = batch; | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     if (threadNumber > batch) { | 
					
						
							|  |  |  |         threadNumber = batch; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto memoryPool = static_cast<CPUBackend*>(backend())->getBufferAllocator(); | 
					
						
							|  |  |  |     memoryPool->barrierBegin(); | 
					
						
							|  |  |  |     std::shared_ptr<void> __a(nullptr, [memoryPool](void *) { memoryPool->barrierEnd(); }); | 
					
						
							|  |  |  |     for (int i = 0; i < threadNumber; ++i) { | 
					
						
							|  |  |  |         memoryPool->beginGroup(); | 
					
						
							|  |  |  |         std::shared_ptr<void> __b(nullptr, [memoryPool](void *) { memoryPool->endGroup(); }); | 
					
						
							|  |  |  |         auto& unit = mUnits[i]; | 
					
						
							|  |  |  |         unit.mMatrixA->setLength(0, input0->length(input0->dimensions()-2)); | 
					
						
							|  |  |  |         unit.mMatrixA->setLength(1, input0->length(input0->dimensions()-1)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         unit.mMatrixB->setLength(0, input1->length(input1->dimensions()-2)); | 
					
						
							|  |  |  |         unit.mMatrixB->setLength(1, input1->length(input1->dimensions()-1)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         unit.mMatrixC->setLength(0, output->length(output->dimensions()-2)); | 
					
						
							|  |  |  |         unit.mMatrixC->setLength(1, output->length(output->dimensions()-1)); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         TensorUtils::setLinearLayout(unit.mMatrixA.get()); | 
					
						
							|  |  |  |         TensorUtils::setLinearLayout(unit.mMatrixB.get()); | 
					
						
							|  |  |  |         TensorUtils::setLinearLayout(unit.mMatrixC.get()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto code = unit.mMatMul->onResize(unit.mTempInputs, unit.mTempOutputs); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return NO_ERROR; | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode CPUBatchMatMul::onExecute(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) { | 
					
						
							|  |  |  |     auto input0          = inputs[0]; | 
					
						
							|  |  |  |     auto input1          = inputs[1]; | 
					
						
							|  |  |  |     auto output          = outputs[0]; | 
					
						
							| 
									
										
										
										
											2020-07-04 01:21:30 +08:00
										 |  |  |     // Fill output by zero if one of inputs is empty.
 | 
					
						
							|  |  |  |     if (input0->elementSize() == 0 || input1->elementSize() == 0) { | 
					
						
							|  |  |  |         ::memset(output->host<float>(), 0, output->size()); | 
					
						
							|  |  |  |         return NO_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     const int dimensions = input0->dimensions(); | 
					
						
							|  |  |  |     MNN_ASSERT(dimensions >= 3); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     const int input0Stride = input0->length(dimensions - 1) * input0->length(dimensions - 2); | 
					
						
							|  |  |  |     const int input1Stride = input1->length(dimensions - 1) * input1->length(dimensions - 2); | 
					
						
							|  |  |  |     const int outputStride = output->length(dimensions - 1) * output->length(dimensions - 2); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     const auto input0Ptr   = input0->host<float>(); | 
					
						
							|  |  |  |     const auto input1Ptr   = input1->host<float>(); | 
					
						
							|  |  |  |     float* const outputPtr = output->host<float>(); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     int threadNumber = static_cast<CPUBackend*>(backend())->threadNumber(); | 
					
						
							|  |  |  |     if (threadNumber > mBatch) { | 
					
						
							|  |  |  |         threadNumber = mBatch; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     MNN_CONCURRENCY_BEGIN(tId, threadNumber) { | 
					
						
							|  |  |  |         auto& unit = mUnits[tId]; | 
					
						
							|  |  |  |         for (int i = (int)tId; i < mBatch; i+=threadNumber) { | 
					
						
							| 
									
										
										
										
											2020-12-15 14:12:35 +08:00
										 |  |  |             unit.mMatrixA->buffer().host = (uint8_t*)(input0Ptr + i * input0Stride); | 
					
						
							|  |  |  |             unit.mMatrixB->buffer().host = (uint8_t*)(input1Ptr + i * input1Stride); | 
					
						
							|  |  |  |             unit.mMatrixC->buffer().host = (uint8_t*)(outputPtr + i * outputStride); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |             unit.mMatMul->onExecute(unit.mTempInputs, unit.mTempOutputs); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     MNN_CONCURRENCY_END(); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CPUBatchMatMulCreator : public CPUBackend::Creator { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual Execution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs, | 
					
						
							|  |  |  |                                 const MNN::Op* op, Backend* backend) const override { | 
					
						
							| 
									
										
											  
											
												- build:
	- unify schema building in core and converter;
	- add more build script for android;
	- add linux build script for python;
- ops impl:
	- add floor mod support in binary;
	- use eltwise impl in add/max/sub/mul binary for optimization;
	- remove fake double support in cast;
	- fix 5d support for concat;
	- add adjX and adjY support for batch matmul;
	- optimize conv2d back prop filter;
	- add pad mode support for conv3d;
	- fix bug in conv2d & conv depthwise with very small feature map;
	- optimize binary without broacast;
	- add data types support for gather;
	- add gather ND support;
	- use uint8 data type in gather v2;
	- add transpose support for matmul;
	- add matrix band part;
	- add dim != 4 support for padding, reshape & tensor convert;
	- add pad type support for pool3d;
	- make ops based on TensorFlow Lite quantization optional;
	- add all & any support for reduction;
	- use type in parameter as output type in reduction;
	- add int support for unary;
	- add variable weight support for conv2d;
	- fix conv2d depthwise weights initialization;
	- fix type support for transpose;
	- fix grad outputs count for  reduce grad and reshape grad;
	- fix priorbox & detection output;
	- fix metal softmax error;
- python:
	- add runSessionWithCallBackInfo interface;
	- add max nodes limit (1400) for visualization tool;
	- fix save error in python3;
	- align default dim;
- convert:
	- add extra design for optimization;
	- add more post converting optimizers;
	- add caffe v1 weights blob support;
	- add cast, unary, conv transpose support for onnx model;
	- optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model;
	- add cos/sin/atan/tan support for unary for tensorflow model;
	- add any/all support for reduction for tensorflow model;
	- add elu, conv3d, pool3d support for tensorflow model;
	- optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model;
- others:
	- fix size computer lock;
	- fix thread pool deadlock;
	- add express & parameters in express;
	- rewrite blitter chooser without static map;
	- add tests for expr;
											
										 
											2019-10-29 13:37:26 +08:00
										 |  |  |         return new CPUBatchMatMul(backend, op->main_as_BatchMatMulParam()->adjX(), op->main_as_BatchMatMulParam()->adjY()); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | REGISTER_CPU_OP_CREATOR(CPUBatchMatMulCreator, OpType_BatchMatMul); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace MNN
 |