2019-06-17 20:10:35 +08:00
|
|
|
//
|
|
|
|
|
// SoftmaxGrad.cpp
|
|
|
|
|
// MNN
|
|
|
|
|
//
|
|
|
|
|
// Created by MNN on 2019/04/22.
|
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "SoftmaxGrad.hpp"
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Macro.h"
|
2020-02-26 09:57:17 +08:00
|
|
|
#include <MNN/expr/ExprCreator.hpp>
|
2019-06-17 20:10:35 +08:00
|
|
|
using namespace std;
|
|
|
|
|
using namespace MNN;
|
2020-02-26 09:57:17 +08:00
|
|
|
using namespace MNN::Express;
|
2019-06-17 20:10:35 +08:00
|
|
|
|
|
|
|
|
class SoftmaxGrad : public OpGrad {
|
|
|
|
|
public:
|
|
|
|
|
SoftmaxGrad() {
|
|
|
|
|
mType = NO_LINEAR;
|
|
|
|
|
}
|
2020-01-15 13:33:47 +08:00
|
|
|
virtual std::vector<Express::VARP> onGrad(Express::EXPRP expr,
|
2019-12-27 22:16:57 +08:00
|
|
|
const std::vector<Express::VARP>& backwardOutput) override {
|
2020-02-26 09:57:17 +08:00
|
|
|
MNN_ASSERT(expr->inputs().size() == 1 && backwardOutput.size() == 1);
|
|
|
|
|
auto input = expr->inputs()[0];
|
|
|
|
|
auto info = input->getInfo();
|
|
|
|
|
auto outputDiff = backwardOutput[0];
|
|
|
|
|
if (nullptr == info) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
auto axis = expr->get()->main_as_Axis()->axis();
|
|
|
|
|
if (axis < 0) {
|
|
|
|
|
axis = axis + info->dim.size();
|
|
|
|
|
}
|
2019-06-17 20:10:35 +08:00
|
|
|
unique_ptr<OpT> newOp(new OpT);
|
2019-12-27 22:16:57 +08:00
|
|
|
newOp->type = OpType_SoftmaxGrad;
|
|
|
|
|
newOp->main.type = OpParameter_Axis;
|
|
|
|
|
newOp->main.value = new AxisT;
|
2020-02-26 09:57:17 +08:00
|
|
|
newOp->main.AsAxis()->axis = 1;
|
|
|
|
|
auto originOrder = info->order;
|
|
|
|
|
auto output = Express::Variable::create(expr, 0);
|
|
|
|
|
if (axis != 1) {
|
|
|
|
|
if (originOrder == NC4HW4) {
|
|
|
|
|
outputDiff = _Convert(outputDiff, NCHW);
|
|
|
|
|
output = _Convert(output, NCHW);
|
|
|
|
|
}
|
|
|
|
|
std::vector<int> permuteDims(info->dim.size());
|
|
|
|
|
for (int i=0; i<info->dim.size(); ++i) {
|
|
|
|
|
permuteDims[i] = i;
|
|
|
|
|
}
|
|
|
|
|
permuteDims[1] = axis;
|
|
|
|
|
permuteDims[axis] = 1;
|
|
|
|
|
auto res = Express::Variable::create(Express::Expr::create(std::move(newOp), {_Transpose(output, permuteDims), _Transpose(outputDiff, permuteDims)}));
|
|
|
|
|
res = _Transpose(res, permuteDims);
|
|
|
|
|
if (originOrder == NC4HW4) {
|
|
|
|
|
res = _Convert(res, originOrder);
|
|
|
|
|
}
|
|
|
|
|
return {res};
|
|
|
|
|
}
|
|
|
|
|
std::vector<Express::VARP> result(1, nullptr);
|
2020-01-15 13:33:47 +08:00
|
|
|
result[0] = Express::Variable::create(
|
|
|
|
|
Express::Expr::create(std::move(newOp), {Express::Variable::create(expr, 0), backwardOutput[0]}));
|
2019-06-17 20:10:35 +08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
static const auto gRegister = []() {
|
- 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
|
|
|
static SoftmaxGrad _c;
|
2019-06-17 20:10:35 +08:00
|
|
|
OpGrad::insert(OpType_Softmax, &_c);
|
|
|
|
|
return true;
|
|
|
|
|
}();
|