execute.ftl 6.73 KB
Newer Older
Bernhard Rumpe's avatar
BR-sy    
Bernhard Rumpe committed
1
<#-- (c) https://github.com/MontiCore/monticore -->
2
<#list tc.architectureInputSymbols as input>
3
4
    vector<float> ${tc.getName(input)} = CNNTranslator::translate(${input.name}<#if input.arrayAccess.isPresent()>[${input.arrayAccess.get().intValue.get()?c}]</#if>);
</#list>
5

6
7
8
9
10
11
12
<#if tc.architectureOutputSymbols?size gt 1>
<#assign outputName = tc.getNameWithoutIndex(tc.getName(tc.architectureOutputSymbols[0]))>
    vector<vector<float>> ${outputName}(${tc.architectureOutputSymbols?size});
    for (size_t i = 0; i < ${outputName}.size(); ++i) {
        ${outputName}[i].emplace_back(${tc.join(tc.architectureOutputSymbols[0].ioDeclaration.type.dimensions, " * ")});
    }
<#else>
13
<#list tc.architectureOutputSymbols as output>
14
15
16
17
    vector<float> ${tc.getName(output)}(${tc.join(tc.architectureOutputSymbols[0].ioDeclaration.type.dimensions, " * ")});<#sep>,
</#list>
</#if>

18
19
<#list tc.getLayerVariableMembers()?keys as member>
    vector<float> ${member}(${tc.join(tc.getLayerVariableMembers()[member], " * ")});
20
</#list>
21

Sebastian Nickels's avatar
Sebastian Nickels committed
22
23
24
<#list tc.architecture.constants as constant>
    vector<float> ${tc.getName(constant)}{${constant.intValue?c}};
</#list>
25

Sebastian Nickels's avatar
Sebastian Nickels committed
26
<#list tc.architecture.networkInstructions as networkInstruction>
Sebastian Nickels's avatar
Sebastian Nickels committed
27
<#if networkInstruction.isUnroll()>
28
29
30
31
    {
        int k = ${tc.getBeamSearchWidth(networkInstruction)};
<#list tc.getUnrollInputNames(networkInstruction, "1") as inputName>
<#if tc.getNameWithoutIndex(inputName) == tc.outputName>
32
        vector<pair<vector<vector<float>>, double>> sequences{make_pair(vector<vector<float>>{${inputName}}, 1.0)};
33
</#if>
Sebastian Nickels's avatar
Sebastian Nickels committed
34
</#list>
35
36
37
38

        for (size_t i = 1; i < ${tc.getBeamSearchMaxLength(networkInstruction)}; ++i) {
            vector<pair<vector<vector<float>>, double>> allCandidates;

39
40
41
            for (const pair<vector<vector<float>>, double>& p : sequences) {
                vector<vector<float>> seq = p.first;
                double score = p.second;
42
43
44
45

<#list tc.getUnrollInputNames(networkInstruction, "i") as inputName>
<#if tc.getNameWithoutIndex(inputName) == tc.outputName>
                ${inputName} = seq.back();
46
</#if>
47
48
49
50
</#list>
                _predictor_${networkInstruction?index}_.predict(${tc.join(tc.getUnrollInputNames(networkInstruction, "i"), ", ")}, ${tc.join(tc.getUnrollOutputNames(networkInstruction, "i"), ", ")});
<#list tc.getUnrollOutputNames(networkInstruction, "i") as outputName>
<#if tc.getNameWithoutIndex(outputName) == tc.outputName>
51
52
53
54
<#if tc.endsWithArgmax(networkInstruction.body)>
                std::vector<float>::iterator maxElement = std::max_element(${outputName}.begin(), ${outputName}.end());
                ${outputName} = std::vector<float>{static_cast<float>(std::distance(${outputName}.begin(), maxElement))};
</#if>
55
                vector<float> out = ${outputName};
Sebastian Nickels's avatar
Sebastian Nickels committed
56
</#if>
57
</#list>
58

59
60
61
62
63
64
65
                vector<pair<int, float>> topk;
                for (size_t i = 0; i < out.size(); ++i) {
                    topk.emplace_back(i, out[i]);
                }

                sort(topk.begin(), topk.end(), [] (const pair<int, float>& p1, const pair<int, float>& p2) {
                    return p1.second > p2.second;
66
                });
67
                topk = vector<pair<int, float>>(topk.begin(), topk.begin() + std::min<int>(k, topk.size()));
68
69
70

                for (const pair<int, float>& pair : topk) {
                    vector<vector<float>> currentSeq = seq;
71
                    currentSeq.push_back(vector<float>{(float) pair.first});
72
73
74
75
76
77
78
                    allCandidates.emplace_back(currentSeq, score * pair.second);
                }
            }

            sort(allCandidates.begin(), allCandidates.end(), [] (const pair<vector<vector<float>>, double>& p1, const pair<vector<vector<float>>, double>& p2) {
                return p1.second > p2.second;
            });
79
            sequences = vector<pair<vector<vector<float>>, double>>(allCandidates.begin(), allCandidates.begin() + std::min<int>(k, allCandidates.size()));
80
81
82
83
84
        }

        for (size_t i = 1; i < ${tc.getBeamSearchMaxLength(networkInstruction)}; ++i) {
<#list tc.getUnrollOutputNames(networkInstruction, "i") as outputName>
<#if tc.getNameWithoutIndex(outputName) == tc.outputName>
Sebastian Nickels's avatar
Sebastian Nickels committed
85
            ${outputName} = sequences[0].first[i];
86
87
</#if>
</#list>
88
89
90
91
        }
    }
<#else>
    _predictor_${networkInstruction?index}_.predict(${tc.join(tc.getStreamInputNames(networkInstruction.body, true), ", ")}, ${tc.join(tc.getStreamOutputNames(networkInstruction.body, true), ", ")});
92
93
94
95
96
97
98
99
<#list tc.getStreamOutputNames(networkInstruction.body, true) as outputName>
<#if tc.getNameWithoutIndex(outputName) == tc.outputName>
<#if tc.endsWithArgmax(networkInstruction.body)>
    std::vector<float>::iterator maxElement = std::max_element(${outputName}.begin(), ${outputName}.end());
    ${outputName} = std::vector<float>{static_cast<float>(std::distance(${outputName}.begin(), maxElement))};
</#if>
</#if>
</#list>
100
</#if>
101

102
</#list>
103
<#list tc.architectureOutputSymbols as output>
104
<#assign shape = output.ioDeclaration.type.dimensions>
105
<#if shape?size == 1>
106
<#if (output.ioDeclaration.type.domain.isNaturalNumber() || output.ioDeclaration.type.domain.isWholeNumber())>
107
    ${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToIntCol(${tc.getNameAsArray(tc.getName(output))}, std::vector<size_t> {${shape[0]?c}});
108
<#else>
109
    ${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToCol(${tc.getNameAsArray(tc.getName(output))}, std::vector<size_t> {${shape[0]?c}});
110
</#if>
111
</#if>
112
<#if shape?size == 2>
113
<#if (output.ioDeclaration.type.domain.isNaturalNumber() || output.ioDeclaration.type.domain.isWholeNumber())>
114
    ${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToIntMat(${tc.getNameAsArray(tc.getName(output))}, std::vector<size_t> {${shape[0]?c}, ${shape[1]?c}});
115
<#else>
116
    ${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToMat(${tc.getNameAsArray(tc.getName(output))}, std::vector<size_t> {${shape[0]?c}, ${shape[1]?c}});
117
</#if>
118
</#if>
119
<#if shape?size == 3>
120
<#if (output.ioDeclaration.type.domain.isNaturalNumber() || output.ioDeclaration.type.domain.isWholeNumber())>
121
    ${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToIntCube(${tc.getNameAsArray(tc.getName(output))}, std::vector<size_t> {${shape[0]?c}, ${shape[1]?c}, ${shape[2]?c}});
122
<#else>
123
    ${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToCube(${tc.getNameAsArray(tc.getName(output))}, std::vector<size_t> {${shape[0]?c}, ${shape[1]?c}, ${shape[2]?c}});
124
</#if>
125
126
</#if>
</#list>