-
Notifications
You must be signed in to change notification settings - Fork 0
/
unaryop.cpp
81 lines (68 loc) · 1.59 KB
/
unaryop.cpp
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
#include "unaryop.h"
#include "layer_register.h"
#include "utils.h"
#include "math.h"
UnaryOp::UnaryOp()
{
_op_type = -1;
}
UnaryOp::~UnaryOp()
{
}
int UnaryOp::load_model(const vector<string> ¶ms, FILE* fp)
{
vector<string> op_type_param = split(params[6], "=");
if(op_type_param[1] == "Square")
{
_op_type = 0;
}
if(op_type_param[1] == "Rsq")
{
_op_type = 1;
}
return 0;
}
void UnaryOp::forward(vector<Tensor*> &input, vector<Tensor*> &output)
{
Tensor* result;
if(output[0] == nullptr)
{
result = new Tensor();
}
else
{
result = output[0];
}
result->set_shape(input[0]->get_shape());
vector<float>* data = result->get_data();
switch(_op_type)
{
case 0:
{
//#pragma omp parallel for
for(int i=0; i<input[0]->get_data()->size(); i++)
{
data->data()[i] = input[0]->get_data()->data()[i] * input[0]->get_data()->data()[i];
}
}
break;
case 1:
{
//#pragma omp parallel for
for(int i=0; i<input[0]->get_data()->size(); i++)
{
data->data()[i] = 1.0f / powf(input[0]->get_data()->data()[i], 0.5f);
}
}
break;
default:
break;
}
output[0] = result;
}
int UnaryOp::CreateInstance(Layer* &layer)
{
layer = new UnaryOp();
return 0;
}
LayerRegistererWrapper unaryOpCreateInstance("UnaryOp_t", UnaryOp::CreateInstance);