forked from espressif/esp-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl_module_log.hpp
107 lines (94 loc) · 3.49 KB
/
dl_module_log.hpp
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
#pragma once
#include "dl_math.hpp"
#include "dl_module_base.hpp"
#include "dl_module_lut.hpp"
namespace dl {
namespace module {
/**
* @brief: Calculates the natural log of the given input tensor, element-wise.
* Supports float, int16_t and int8_t.
*/
class Log : public Module {
public:
/**
* @brief Construct a new Log object.
*
* @param name name of module
* @param inplace inplace type.
*/
Log(const char *name = NULL,
module_inplace_t inplace = MODULE_NON_INPLACE,
quant_type_t quant_type = QUANT_TYPE_NONE) :
Module(name, inplace, quant_type)
{
}
/**
* @brief Destroy the Log object.
*/
~Log() {}
std::vector<std::vector<int>> get_output_shape(std::vector<std::vector<int>> &input_shapes)
{
std::vector<std::vector<int>> output_shapes(1, input_shapes[0]);
return output_shapes;
}
void forward(std::vector<TensorBase *> &tensors, runtime_mode_t mode = RUNTIME_MODE_AUTO)
{
DL_LOG_MODULE_LATENCY_INIT();
DL_LOG_MODULE_LATENCY_START();
if (quant_type == QUANT_TYPE_SYMM_8BIT) {
forward_template<int8_t>(tensors, mode);
} else if (quant_type == QUANT_TYPE_SYMM_16BIT) {
forward_template<int16_t>(tensors, mode);
} else if (quant_type == QUANT_TYPE_FLOAT32) {
TensorBase *input = tensors[m_inputs_index[0]];
TensorBase *output = tensors[m_outputs_index[0]];
float *input_ptr = (float *)input->get_element_ptr();
float *output_ptr = (float *)output->get_element_ptr();
for (size_t i = 0; i < input->size; i++) {
output_ptr[i] = logf(input_ptr[i]);
}
}
DL_LOG_MODULE_LATENCY_END_PRINT(this->name, "Log");
}
template <typename T>
void forward_template(std::vector<TensorBase *> &tensors, runtime_mode_t mode)
{
TensorBase *input = tensors[m_inputs_index[0]];
TensorBase *output = tensors[m_outputs_index[0]];
T *input_ptr = (T *)input->get_element_ptr();
T *output_ptr = (T *)output->get_element_ptr();
float input_scale = DL_SCALE(input->exponent);
float output_scale = DL_RESCALE(output->exponent);
for (size_t i = 0; i < input->size; i++) {
float temp = input_ptr[i] * input_scale;
temp = logf(temp);
tool::truncate(output_ptr[i], tool::round(temp * output_scale));
}
}
void forward_args(void *args) {}
/**
* @brief deserialize Log module instance by node serialization information
*/
static Module *deserialize(fbs::FbsModel *fbs_model, std::string node_name)
{
Module *op = nullptr;
quant_type_t quant_type;
fbs_model->get_operation_attribute(node_name, "quant_type", quant_type);
// Create module
if (quant_type == QUANT_TYPE_SYMM_8BIT) {
TensorBase *table = fbs_model->get_operation_lut(node_name);
if (table) {
op = new LUT(node_name.c_str(), table, MODULE_INPLACE_CHANGED_BUFFER, quant_type);
} else {
op = new Log(node_name.c_str(), MODULE_INPLACE_CHANGED_BUFFER, quant_type);
}
} else {
op = new Log(node_name.c_str(), MODULE_INPLACE_CHANGED_BUFFER, quant_type);
}
op->print();
return op;
}
void print() { ESP_LOGI("Log", "quant_type: %s.", quant_type_to_string(quant_type)); }
};
} // namespace module
} // namespace dl