aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/generate/generate_utils.cc
blob: a8b472a3c52a6e8094c7e86b4fc390d81cf2240e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2023-2024, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#include "generate_utils.h"

#include <nlohmann/json.hpp>

#include <algorithm>

namespace tosa
{

NLOHMANN_JSON_SERIALIZE_ENUM(DType,
                             {
                                 { DType::DType_UNKNOWN, "UNKNOWN" },
                                 { DType::DType_BOOL, "BOOL" },
                                 { DType::DType_INT4, "INT4" },
                                 { DType::DType_INT8, "INT8" },
                                 { DType::DType_INT16, "INT16" },
                                 { DType::DType_INT32, "INT32" },
                                 { DType::DType_INT48, "INT48" },
                                 { DType::DType_FP16, "FP16" },
                                 { DType::DType_BF16, "BF16" },
                                 { DType::DType_FP32, "FP32" },
                                 { DType::DType_SHAPE, "SHAPE" },
                             })

NLOHMANN_JSON_SERIALIZE_ENUM(Op,
                             {
                                 { Op::Op_UNKNOWN, "UNKNOWN" },
                                 { Op::Op_ABS, "ABS" },
                                 { Op::Op_ADD, "ADD" },
                                 { Op::Op_ARGMAX, "ARGMAX" },
                                 { Op::Op_AVG_POOL2D, "AVG_POOL2D" },
                                 { Op::Op_CAST, "CAST" },
                                 { Op::Op_CEIL, "CEIL" },
                                 { Op::Op_CLAMP, "CLAMP" },
                                 { Op::Op_CONCAT, "CONCAT" },
                                 { Op::Op_CONST, "CONST" },
                                 { Op::Op_CONV2D, "CONV2D" },
                                 { Op::Op_DEPTHWISE_CONV2D, "DEPTHWISE_CONV2D" },
                                 { Op::Op_EQUAL, "EQUAL" },
                                 { Op::Op_ERF, "ERF" },
                                 { Op::Op_EXP, "EXP" },
                                 { Op::Op_FLOOR, "FLOOR" },
                                 { Op::Op_FULLY_CONNECTED, "FULLY_CONNECTED" },
                                 { Op::Op_GATHER, "GATHER" },
                                 { Op::Op_GREATER, "GREATER" },
                                 { Op::Op_GREATER_EQUAL, "GREATER_EQUAL" },
                                 { Op::Op_IDENTITY, "IDENTITY" },
                                 { Op::Op_LOG, "LOG" },
                                 { Op::Op_MATMUL, "MATMUL" },
                                 { Op::Op_MAXIMUM, "MAXIMUM" },
                                 { Op::Op_MAX_POOL2D, "MAX_POOL2D" },
                                 { Op::Op_MINIMUM, "MINIMUM" },
                                 { Op::Op_MUL, "MUL" },
                                 { Op::Op_NEGATE, "NEGATE" },
                                 { Op::Op_PAD, "PAD" },
                                 { Op::Op_POW, "POW" },
                                 { Op::Op_RECIPROCAL, "RECIPROCAL" },
                                 { Op::Op_RESHAPE, "RESHAPE" },
                                 { Op::Op_RSQRT, "RSQRT" },
                                 { Op::Op_REDUCE_MAX, "REDUCE_MAX" },
                                 { Op::Op_REDUCE_MIN, "REDUCE_MIN" },
                                 { Op::Op_REDUCE_PRODUCT, "REDUCE_PRODUCT" },
                                 { Op::Op_REDUCE_SUM, "REDUCE_SUM" },
                                 { Op::Op_REVERSE, "REVERSE" },
                                 { Op::Op_SCATTER, "SCATTER" },
                                 { Op::Op_SELECT, "SELECT" },
                                 { Op::Op_SIGMOID, "SIGMOID" },
                                 { Op::Op_SLICE, "SLICE" },
                                 { Op::Op_SUB, "SUB" },
                                 { Op::Op_TANH, "TANH" },
                                 { Op::Op_TILE, "TILE" },
                                 { Op::Op_TRANSPOSE, "TRANSPOSE" },
                                 { Op::Op_TRANSPOSE_CONV2D, "TRANSPOSE_CONV2D" },
                             })

}    // namespace tosa

namespace TosaReference
{

NLOHMANN_JSON_SERIALIZE_ENUM(GeneratorType,
                             {
                                 { GeneratorType::Unknown, "UNKNOWN" },
                                 { GeneratorType::PseudoRandom, "PSEUDO_RANDOM" },
                                 { GeneratorType::DotProduct, "DOT_PRODUCT" },
                                 { GeneratorType::OpFullRange, "OP_FULL_RANGE" },
                                 { GeneratorType::OpBoundary, "OP_BOUNDARY" },
                                 { GeneratorType::OpSpecial, "OP_SPECIAL" },
                                 { GeneratorType::FixedData, "FIXED_DATA" },
                             })

// NOTE: This assumes it's VARIABLE if the InputType is not recognized
NLOHMANN_JSON_SERIALIZE_ENUM(InputType,
                             {
                                 { InputType::Variable, "VARIABLE" },
                                 { InputType::Constant, "CONSTANT" },
                             })

void from_json(const nlohmann::json& j, DotProductInfo& dotProductInfo)
{
    j.at("s").get_to(dotProductInfo.s);
    j.at("ks").get_to(dotProductInfo.ks);
    j.at("acc_type").get_to(dotProductInfo.accType);
    if (j.contains("kernel"))
    {
        j.at("kernel").get_to(dotProductInfo.kernel);
    }
    if (j.contains("axis"))
    {
        j.at("axis").get_to(dotProductInfo.axis);
    }
}

void from_json(const nlohmann::json& j, PseudoRandomInfo& pseudoRandomInfo)
{
    j.at("rng_seed").get_to(pseudoRandomInfo.rngSeed);
    if (j.contains("range"))
    {
        j.at("range").get_to(pseudoRandomInfo.range);
    }
    if (j.contains("round"))
    {
        j.at("round").get_to(pseudoRandomInfo.round);
    }
}

void from_json(const nlohmann::json& j, FixedDataInfo& fixedDataInfo)
{
    j.at("data").get_to(fixedDataInfo.data);
}

void from_json(const nlohmann::json& j, GenerateConfig& cfg)
{
    j.at("data_type").get_to(cfg.dataType);
    j.at("input_type").get_to(cfg.inputType);
    j.at("shape").get_to(cfg.shape);
    j.at("input_pos").get_to(cfg.inputPos);
    j.at("op").get_to(cfg.opType);
    j.at("generator").get_to(cfg.generatorType);

    // Set up defaults for dotProductInfo
    cfg.dotProductInfo.s       = -1;
    cfg.dotProductInfo.ks      = -1;
    cfg.dotProductInfo.accType = DType_UNKNOWN;
    cfg.dotProductInfo.kernel  = std::vector<int32_t>();
    cfg.dotProductInfo.axis    = -1;
    if (j.contains("dot_product_info"))
    {
        j.at("dot_product_info").get_to(cfg.dotProductInfo);
    }

    // Set up defaults for pseudoRandomInfo
    cfg.pseudoRandomInfo.rngSeed = -1;
    cfg.pseudoRandomInfo.range   = std::vector<std::string>();
    cfg.pseudoRandomInfo.round   = false;
    if (j.contains("pseudo_random_info"))
    {
        j.at("pseudo_random_info").get_to(cfg.pseudoRandomInfo);
    }

    // Set up defaults for fixedDataInfo
    cfg.fixedDataInfo.data = std::vector<int32_t>();
    if (j.contains("fixed_data_info"))
    {
        j.at("fixed_data_info").get_to(cfg.fixedDataInfo);
    }
}

std::optional<GenerateConfig> parseGenerateConfig(const char* json, const char* tensorName)
{
    if (!tensorName)
        return std::nullopt;

    auto jsonCfg = nlohmann::json::parse(json, nullptr, /* allow exceptions */ false);

    if (jsonCfg.is_discarded())
    {
        WARNING("[Generator] Invalid json config.");
        return std::nullopt;
    }
    if (!jsonCfg.contains("tensors"))
    {
        WARNING("[Generator] Missing tensors in json config.");
        return std::nullopt;
    }
    const auto& tensors = jsonCfg["tensors"];
    if (!tensors.contains(tensorName))
    {
        WARNING("[Generator] Missing tensor %s in json config.", tensorName);
        return std::nullopt;
    }
    const auto& namedTensor = tensors[tensorName];
    return namedTensor.get<GenerateConfig>();
}

int64_t numElementsFromShape(const std::vector<int32_t>& shape)
{
    // Rank 0 shapes have no entries and so this will return 1
    // Other ranked shapes will return the product of their dimensions
    return std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int64_t>());
}

size_t elementSizeFromType(DType type)
{
    switch (type)
    {
        case DType::DType_BOOL:
        case DType::DType_UINT8:
        case DType::DType_INT8:
            return 1;
        case DType::DType_UINT16:
        case DType::DType_INT16:
        case DType::DType_FP16:
        case DType::DType_BF16:
            return 2;
        case DType::DType_INT32:
        case DType::DType_FP32:
        case DType::DType_SHAPE:
            return 4;
        default:
            return 0;
    }
    return 0;
}
}    // namespace TosaReference