| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  CPUQuantizedSoftmax.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2018/09/29.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
											  
											
												- 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
										 |  |  | #ifdef MNN_SUPPORT_TFLITE_QUAN
 | 
					
						
							| 
									
										
										
										
											2019-06-10 21:08:55 +08:00
										 |  |  | #if defined(_MSC_VER)
 | 
					
						
							|  |  |  | #include <intrin.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "backend/cpu/CPUQuantizedSoftmax.hpp"
 | 
					
						
							|  |  |  | #include "backend/cpu/CPUBackend.hpp"
 | 
					
						
							|  |  |  | #include "backend/cpu/CPUFixedPoint.hpp"
 | 
					
						
							|  |  |  | #include "backend/cpu/CPUQuantizationUtils.hpp"
 | 
					
						
							|  |  |  | #include "core/Macro.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | CPUQuantizedSoftmax<T>::CPUQuantizedSoftmax(Backend* backend, const Op* op) : Execution(backend) { | 
					
						
							|  |  |  |     auto quantizedSoftmax_param = op->main_as_QuantizedSoftmax(); | 
					
						
							|  |  |  |     mBeta                       = quantizedSoftmax_param->beta(); | 
					
						
							|  |  |  |     mInputScale                 = quantizedSoftmax_param->inputScale(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const int kScaledDiffIntegerBits   = 5; | 
					
						
							|  |  |  | const int kAccumulationIntegerBits = 12; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | ErrorCode CPUQuantizedSoftmax<T>::onResize(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) { | 
					
						
							|  |  |  |     float beta  = mBeta; | 
					
						
							|  |  |  |     float scale = mInputScale; | 
					
						
							|  |  |  |     PreprocessSoftmaxScaling(beta, scale, kScaledDiffIntegerBits, &mInputMultiplier, &mInputLeftShift); | 
					
						
							|  |  |  |     mDiffMin = -1.0 * CalculateInputRadius(kScaledDiffIntegerBits, mInputLeftShift); | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 20:27:57 +08:00
										 |  |  |     Tensor* input       = inputs[0]; | 
					
						
							|  |  |  |     Tensor* output      = outputs[0]; | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 20:27:57 +08:00
										 |  |  |     MNN_ASSERT(2 == input->buffer().dimensions || 4 == input->buffer().dimensions); | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 20:27:57 +08:00
										 |  |  |     mInputDims.clear(); | 
					
						
							|  |  |  |     mOutputDims.clear(); | 
					
						
							|  |  |  |     if (4 == input->buffer().dimensions) { | 
					
						
							|  |  |  |         for (int i = 0; i < input->buffer().dimensions; i++) { | 
					
						
							|  |  |  |             mInputDims.push_back(input->buffer().dim[i].extent); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for (int i = 0; i < output->buffer().dimensions; i++) { | 
					
						
							|  |  |  |             mOutputDims.push_back(output->buffer().dim[i].extent); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         mInputDims.push_back(input->buffer().dim[0].extent); | 
					
						
							|  |  |  |         mInputDims.push_back(1); | 
					
						
							|  |  |  |         mInputDims.push_back(1); | 
					
						
							|  |  |  |         mInputDims.push_back(input->buffer().dim[1].extent); | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 20:27:57 +08:00
										 |  |  |         mOutputDims.push_back(input->buffer().dim[0].extent); | 
					
						
							|  |  |  |         mOutputDims.push_back(1); | 
					
						
							|  |  |  |         mOutputDims.push_back(1); | 
					
						
							|  |  |  |         mOutputDims.push_back(input->buffer().dim[1].extent); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | void CPUQuantizedSoftmax<T>::QuantizedSoftmax(const uint8_t* inputData, const std::vector<int>& inputDims, | 
					
						
							|  |  |  |                                               int32_t inputBetaMultiplier, int32_t inputBetaLeftShift, | 
					
						
							|  |  |  |                                               uint8_t* outputData, const std::vector<int>& outputDims) { | 
					
						
							|  |  |  |     using FixedPointScaledDiff = FixedPoint<int, kScaledDiffIntegerBits>; | 
					
						
							|  |  |  |     using FixedPointAccum      = FixedPoint<int, kAccumulationIntegerBits>; | 
					
						
							|  |  |  |     using FixedPoint0          = FixedPoint<int, 0>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const int outerSize = inputDims.at(0) * inputDims.at(1) * inputDims.at(2); | 
					
						
							|  |  |  |     const int depth     = inputDims.at(3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int b = 0; b < outerSize; ++b) { | 
					
						
							|  |  |  |         const uint8_t* inputDataPtr = inputData + b * depth; | 
					
						
							|  |  |  |         uint8_t* outputDataPtr      = outputData + b * depth; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Determine the largest entry in the current row
 | 
					
						
							|  |  |  |         uint8_t maxInRow = 0; | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             int c = 0; | 
					
						
							|  |  |  |             for (; c < depth; ++c) { | 
					
						
							|  |  |  |                 maxInRow = std::max(maxInRow, inputDataPtr[c]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         FixedPointAccum sumOfExps = FixedPointAccum::Zero(); | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             int c = 0; | 
					
						
							|  |  |  |             for (; c < depth; ++c) { | 
					
						
							|  |  |  |                 int32_t inputDiff = static_cast<int32_t>(inputDataPtr[c]) - maxInRow; | 
					
						
							|  |  |  |                 if (inputDiff >= mDiffMin) { | 
					
						
							|  |  |  |                     const int32_t inputDiffRescaled = | 
					
						
							|  |  |  |                         MultiplyByQuantizedMultiplierGreaterThanOne(inputDiff, inputBetaMultiplier, inputBetaLeftShift); | 
					
						
							|  |  |  |                     const FixedPointScaledDiff scaledDiffF8 = FixedPointScaledDiff::FromRaw(inputDiffRescaled); | 
					
						
							|  |  |  |                     sumOfExps = sumOfExps + Rescale<kAccumulationIntegerBits>(exp_on_negative_values(scaledDiffF8)); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         int fixedSumOfExps  = sumOfExps.raw(); | 
					
						
							| 
									
										
										
										
											2019-06-10 21:08:55 +08:00
										 |  |  | #if defined(_MSC_VER)
 | 
					
						
							|  |  |  |         int headroomPlusOne; | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             unsigned long leading_zero = 0; | 
					
						
							|  |  |  |             if (_BitScanReverse(&leading_zero, static_cast<uint32_t>(fixedSumOfExps))) { | 
					
						
							|  |  |  |                 headroomPlusOne = 31 - leading_zero; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 headroomPlusOne = 32; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         int headroomPlusOne = __builtin_clz(static_cast<uint32_t>(fixedSumOfExps)); | 
					
						
							| 
									
										
										
										
											2019-06-10 21:08:55 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         int numBitsOverUnit        = kAccumulationIntegerBits - headroomPlusOne; | 
					
						
							|  |  |  |         int32_t shiftedSumMinusOne = static_cast<int32_t>((static_cast<uint32_t>(fixedSumOfExps) << headroomPlusOne) - | 
					
						
							|  |  |  |                                                           (static_cast<uint32_t>(1) << 31)); | 
					
						
							|  |  |  |         FixedPoint0 shiftedScale   = one_over_one_plus_x_for_x_in_0_1(FixedPoint0::FromRaw(shiftedSumMinusOne)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             int c = 0; | 
					
						
							|  |  |  |             for (; c < depth; ++c) { | 
					
						
							|  |  |  |                 int32_t inputDiff = static_cast<int32_t>(inputDataPtr[c]) - maxInRow; | 
					
						
							|  |  |  |                 if (inputDiff >= mDiffMin) { | 
					
						
							|  |  |  |                     const int inputDiffRescaled = | 
					
						
							|  |  |  |                         MultiplyByQuantizedMultiplierGreaterThanOne(inputDiff, inputBetaMultiplier, inputBetaLeftShift); | 
					
						
							|  |  |  |                     const FixedPointScaledDiff scaledDiffF8 = FixedPointScaledDiff::FromRaw(inputDiffRescaled); | 
					
						
							|  |  |  |                     FixedPoint0 expIn0                      = exp_on_negative_values(scaledDiffF8); | 
					
						
							|  |  |  |                     int unsatOutput  = RoundingDivideByPOT((shiftedScale * expIn0).raw(), numBitsOverUnit + 31 - 8); | 
					
						
							|  |  |  |                     outputDataPtr[c] = std::max(std::min(unsatOutput, 255), 0); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     outputDataPtr[c] = 0; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | ErrorCode CPUQuantizedSoftmax<T>::onExecute(const std::vector<MNN::Tensor*>& inputs, | 
					
						
							|  |  |  |                                             const std::vector<MNN::Tensor*>& outputs) { | 
					
						
							|  |  |  |     Tensor* input       = inputs[0]; | 
					
						
							|  |  |  |     Tensor* output      = outputs[0]; | 
					
						
							|  |  |  |     uint8_t* inputData  = input->host<uint8_t>(); | 
					
						
							|  |  |  |     uint8_t* outputData = output->host<uint8_t>(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 20:27:57 +08:00
										 |  |  |     QuantizedSoftmax(inputData, mInputDims, mInputMultiplier, mInputLeftShift, outputData, mOutputDims); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CPUQuantizedSoftmaxCreator : 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 CPUQuantizedSoftmax<uint8_t>(backend, op); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | REGISTER_CPU_OP_CREATOR(CPUQuantizedSoftmaxCreator, OpType_QuantizedSoftmax); | 
					
						
							|  |  |  | } // 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
										 |  |  | #endif
 |