MNN/source/backend/cpu/CPUCast.cpp

187 lines
7.1 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// CPUCast.cpp
// MNN
//
// Created by MNN on 2018/08/05.
// Copyright © 2018, Alibaba Group Holding Limited
//
2019-12-27 22:16:57 +08:00
#include "backend/cpu/CPUCast.hpp"
2021-04-08 15:34:23 +08:00
#include "core/TensorUtils.hpp"
2019-12-27 22:16:57 +08:00
#include "core/Macro.h"
2021-04-08 15:34:23 +08:00
#include "backend/cpu/compute/Int8FunctionsOpt.h"
#include <cmath>
2019-04-17 10:49:11 +08:00
namespace MNN {
2021-04-08 15:34:23 +08:00
ErrorCode CPUCastCreator::cast(void* const inputRaw, void* outputRaw, halide_type_t inputType, halide_type_t outputType,
int number, float scale, float zero, float min, float max) {
int c4Size = number / 4;
int remain = c4Size * 4;
std::vector<float> scales(4, scale);
if (inputType == halide_type_of<float>() && outputType == halide_type_of<int8_t>()) {
std::for_each(scales.begin(), scales.end(), [](float& x){ x = x == 0.f ? 0.f : 1 / x; });
MNNFloat2Int8(static_cast<float*>(inputRaw), static_cast<int8_t*>(outputRaw), c4Size, scales.data(), min, max, zero);
for (int i = remain; i < number; i++) {
float x = std::round(static_cast<float* const>(inputRaw)[i] * scale + zero);
static_cast<int8_t*>(outputRaw)[i] = static_cast<int8_t>(std::max(std::min(x, max), min));
2021-04-08 15:34:23 +08:00
}
return NO_ERROR;
}
if (inputType == halide_type_of<int8_t>() && outputType == halide_type_of<float>()) {
MNNInt8ScaleToFloat(static_cast<float*>(outputRaw), static_cast<int8_t*>(inputRaw), scales.data(), c4Size, zero);
for (int i = remain; i < number; i++) {
static_cast<float*>(outputRaw)[i] = (static_cast<int8_t* const>(inputRaw)[i] - zero) * scale;
2021-04-08 15:34:23 +08:00
}
return NO_ERROR;
}
MNN_ERROR("Don't support cast type \n");
return NOT_SUPPORT;
}
ErrorCode CPUCastCreator::cast(const Tensor* input, const Tensor* output) {
auto srcT = input->getType();
auto dstT = output->getType();
auto ib = input->buffer();
auto ob = output->buffer();
if (srcT == dstT) {
::memcpy(ib.host, ob.host, input->size());
return NO_ERROR;
}
auto& quantAttr = TensorUtils::getDescribe(input)->quantAttr;
if (quantAttr == nullptr) {
MNN_ERROR("No quant info for Cast\n");
return INVALID_VALUE;
}
int totalSize = input->elementSize();
auto code = cast(ib.host, ob.host, srcT, dstT, totalSize, quantAttr->scale, quantAttr->zero, quantAttr->min, quantAttr->max);
if (NO_ERROR != code) {
MNN_ERROR("Error in CPUCast\n");
return code;
}
return NO_ERROR;
}
2019-04-17 10:49:11 +08:00
template <typename srcT, typename dstT>
class CastDataType : public Execution {
public:
CastDataType(Backend *b) : Execution(b) {
// nothing to do
}
virtual ~CastDataType() = default;
virtual ErrorCode onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) override {
auto input = inputs[0];
auto output = outputs[0];
auto srcData = input->host<srcT>();
auto dstData = output->host<dstT>();
const auto inputDataSize = input->elementSize();
2020-12-15 14:12:35 +08:00
MNN_ASSERT(inputDataSize == output->elementSize());
2019-04-17 10:49:11 +08:00
for (int i = 0; i < inputDataSize; i++) {
dstData[i] = static_cast<dstT>(srcData[i]);
}
return NO_ERROR;
}
};
class Bit32ToBool : public Execution {
public:
Bit32ToBool(Backend *b) : Execution(b) {
// nothing to do
}
virtual ~Bit32ToBool() = default;
2019-04-17 10:49:11 +08:00
virtual ErrorCode onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) override {
auto input = inputs[0];
auto output = outputs[0];
auto srcData = input->host<int>();
auto dstData = output->host<int>();
const auto inputDataSize = input->elementSize();
2020-12-15 14:12:35 +08:00
MNN_ASSERT(inputDataSize == output->elementSize());
for (int i = 0; i < inputDataSize; i++) {
int value = srcData[i] == 0 ? 0 : 1;
dstData[i] = value;
}
return NO_ERROR;
}
};
2019-04-17 10:49:11 +08:00
class CopyExecution : public Execution {
public:
CopyExecution(Backend *b) : Execution(b) {
// nothing to do
}
virtual ~CopyExecution() = default;
virtual ErrorCode onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) override {
auto input = inputs[0];
auto output = outputs[0];
auto srcData = input->host<char>();
auto dstData = output->host<char>();
const auto inputDataSize = input->size();
const auto outputDataSize = output->size();
if (inputDataSize != outputDataSize) {
return INPUT_DATA_ERROR;
}
::memcpy(dstData, srcData, inputDataSize);
return NO_ERROR;
}
};
static DataType _mapDataType(DataType src) {
if (DataType_DT_BOOL == src) {
return DataType_DT_INT32;
}
2019-07-04 19:38:23 +08:00
if (DataType_DT_INT64 == src) {
return DataType_DT_INT32;
}
if (DataType_DT_DOUBLE == src) {
return DataType_DT_FLOAT;
}
2019-04-17 10:49:11 +08:00
return src;
}
Execution *CPUCastCreator::onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const {
auto cast = op->main_as_CastParam();
- 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
// cast param srcT is invalid
// auto srcT = _mapDataType(cast->srcT());
2019-04-17 10:49:11 +08:00
auto dstT = _mapDataType(cast->dstT());
- 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
const auto &inputDataType = inputs[0]->getType();
if (inputDataType.bytes() == 4 && cast->dstT() == MNN::DataType_DT_BOOL) {
return new Bit32ToBool(backend);
}
2019-07-04 19:38:23 +08:00
if (inputs[0]->buffer().type == outputs[0]->buffer().type) {
2019-04-17 10:49:11 +08:00
return new CopyExecution(backend);
}
- 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
if (dstT == MNN::DataType_DT_INT32 && halide_type_of<float>() == inputDataType) {
2019-04-17 10:49:11 +08:00
return new CastDataType<float, int>(backend);
}
- 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
if (dstT == MNN::DataType_DT_FLOAT && halide_type_of<int32_t>() == inputDataType) {
2019-04-17 10:49:11 +08:00
return new CastDataType<int, float>(backend);
}
- 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
if (dstT == MNN::DataType_DT_FLOAT && halide_type_of<uint8_t>() == inputDataType) {
2019-04-17 10:49:11 +08:00
return new CastDataType<uint8_t, float>(backend);
}
if (dstT == MNN::DataType_DT_FLOAT && halide_type_of<int8_t>() == inputDataType) {
return new CastDataType<int8_t, float>(backend);
}
if (dstT == MNN::DataType_DT_INT8 && halide_type_of<float>() == inputDataType) {
return new CastDataType<float, int8_t>(backend);
}
2020-04-29 10:12:16 +08:00
if (dstT == MNN::DataType_DT_UINT8 && halide_type_of<float>() == inputDataType) {
return new CastDataType<float, uint8_t>(backend);
}
if (dstT == MNN::DataType_DT_UINT8 && halide_type_of<int32_t>() == inputDataType) {
return new CastDataType<int32_t, uint8_t>(backend);
}
2019-12-27 22:16:57 +08:00
if (dstT == MNN::DataType_DT_INT32 && halide_type_of<uint8_t>() == inputDataType) {
return new CastDataType<uint8_t, int32_t>(backend);
}
2020-04-29 10:12:16 +08:00
if (dstT == MNN::DataType_DT_INT32 && halide_type_of<int8_t>() == inputDataType) {
return new CastDataType<int8_t, int32_t>(backend);
}
2019-04-17 10:49:11 +08:00
MNN_PRINT("Don't support cast form %d to %d\n", cast->srcT(), cast->dstT());
return nullptr;
}
REGISTER_CPU_OP_CREATOR(CPUCastCreator, OpType_Cast);
} // namespace MNN