MCPcopy Create free account
hub / github.com/alibaba/MNN / _gradForCommandMatMul

Method _gradForCommandMatMul

tools/train/source/grad/LoopGrad.cpp:21–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19 return gradValue;
20 }
21 static void _gradForCommandMatMul(const RegionCommandT* command, const std::map<int, int>& backwardMap, int tensorNumber, std::map<int, VARP>& extraInputs, std::map<int, VARP>& extraOutputs, std::vector<std::unique_ptr<RegionCommandT>>& dstCommands) {
22 auto AIndex = command->indexes[1];
23 auto BIndex = command->indexes[2];
24 auto CIndex = command->indexes[0];
25 auto transA = command->op->main.AsMatMul()->transposeA;
26 auto transB = command->op->main.AsMatMul()->transposeB;
27 int e = command->size[0];
28 int l = command->size[1];
29 int h = command->size[2];
30
31 if (backwardMap.find(CIndex) == backwardMap.end()) {
32 return;
33 }
34 std::map<int, int> originIndexMap;
35 for (int i=0; i<command->indexes.size(); ++i) {
36 auto index = command->indexes[i];
37 originIndexMap.insert(std::make_pair(index, i));
38 auto bkIter = backwardMap.find(index);
39 if (bkIter != backwardMap.end()) {
40 originIndexMap.insert(std::make_pair(bkIter->second, i));
41 }
42 }
43 auto CDiffIndex = backwardMap.find(CIndex)->second;
44 if (backwardMap.find(AIndex) != backwardMap.end()) {
45 auto ADiffIndex = backwardMap.find(AIndex)->second;
46 // Compute A Diff
47 std::unique_ptr<RegionCommandT> currentCommand(new RegionCommandT);
48 currentCommand->op.reset(new OpT);
49 currentCommand->op->type = OpType_MatMul;
50 currentCommand->op->main.value = new MatMulT;
51 currentCommand->op->main.type = OpParameter_MatMul;
52 currentCommand->indexes = {ADiffIndex, CDiffIndex, BIndex};
53 currentCommand->view.resize(3);
54 for (int j=0; j<currentCommand->view.size(); ++j) {
55 currentCommand->view[j].reset(new ViewT);
56 }
57 currentCommand->iterIndexes.resize(3);
58 currentCommand->steps.resize(3);
59 for (int j=0; j<currentCommand->indexes.size(); ++j) {
60 // Compute output info
61 auto index = currentCommand->indexes[j];
62 currentCommand->iterIndexes[j] = command->iterIndexes[originIndexMap[index]];
63 currentCommand->steps[j] = command->steps[originIndexMap[index]];
64 *currentCommand->view[j] = *command->view[originIndexMap[index]];
65 }
66 // TODO: Optimize fuse option
67 currentCommand->fuse = BinaryOpOperation_ADD;
68
69 // Reorder the view's stride by size order change to e, h, l
70 std::vector<int> order = {0, 2, 1};
71 currentCommand->size = {e, h, l};
72 for (int j=0; j<currentCommand->indexes.size(); ++j) {
73 auto view = currentCommand->view[j].get();
74 auto originStride = view->stride;
75 for (int k=0; k<3; ++k) {
76 view->stride[k] = originStride[order[k]];
77 }
78 }

Callers

nothing calls this directly

Calls 8

AsMatMulMethod · 0.80
findMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45
insertMethod · 0.45
resetMethod · 0.45
resizeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected