Skip to content

Commit 920a377

Browse files
committed
analisys, add int
1 parent d00b0d4 commit 920a377

4 files changed

Lines changed: 159 additions & 66 deletions

File tree

app/Graph/build.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
8282
if (layer_type.find("relu") != std::string::npos) {
8383
std::shared_ptr<it_lab_ai::Layer> ew_layer;
8484
if (onednn) {
85-
ew_layer = std::make_shared<it_lab_ai::EWLayer_oneDNN>("relu");
85+
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>("relu");
8686
} else {
8787
ew_layer = std::make_shared<it_lab_ai::EWLayer>("relu");
8888
}
@@ -496,15 +496,15 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
496496
layer_type.find("relu") != std::string::npos) {
497497
std::shared_ptr<it_lab_ai::Layer> ew_layer;
498498
if (onednn) {
499-
ew_layer = std::make_shared<it_lab_ai::EWLayer_oneDNN>("relu");
499+
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>("relu");
500500
} else {
501501
ew_layer = std::make_shared<it_lab_ai::EWLayer>("relu");
502502
}
503503
layer = ew_layer;
504504
} else if (layer_type.find("Sigmoid") != std::string::npos) {
505505
std::shared_ptr<it_lab_ai::Layer> ew_layer;
506506
if (onednn) {
507-
ew_layer = std::make_shared<it_lab_ai::EWLayer_oneDNN>("sigmoid");
507+
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>("sigmoid");
508508
} else {
509509
ew_layer = std::make_shared<it_lab_ai::EWLayer>("sigmoid");
510510
}
@@ -734,7 +734,7 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
734734
ew_operation = "linear";
735735
std::shared_ptr<it_lab_ai::Layer> ew_layer;
736736
if (onednn) {
737-
ew_layer = std::make_shared<it_lab_ai::EWLayer_oneDNN>(
737+
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>(
738738
ew_operation, value, 0.0F);
739739
} else {
740740
ew_layer = std::make_shared<it_lab_ai::EWLayer>(ew_operation,
@@ -745,8 +745,8 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
745745
ew_operation = "linear";
746746
std::shared_ptr<it_lab_ai::Layer> ew_layer;
747747
if (onednn &&
748-
it_lab_ai::EWLayer_oneDNN::is_function_supported("linear")) {
749-
ew_layer = std::make_shared<it_lab_ai::EWLayer_oneDNN>(
748+
it_lab_ai::EwLayerOneDnn::is_function_supported("linear")) {
749+
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>(
750750
ew_operation, 1.0F, value);
751751
} else {
752752
ew_layer = std::make_shared<it_lab_ai::EWLayer>(ew_operation,
@@ -757,8 +757,8 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
757757
ew_operation = "linear";
758758
std::shared_ptr<it_lab_ai::Layer> ew_layer;
759759
if (onednn &&
760-
it_lab_ai::EWLayer_oneDNN::is_function_supported("linear")) {
761-
ew_layer = std::make_shared<it_lab_ai::EWLayer_oneDNN>(
760+
it_lab_ai::EwLayerOneDnn::is_function_supported("linear")) {
761+
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>(
762762
ew_operation, 1.0F, -value);
763763
} else {
764764
ew_layer = std::make_shared<it_lab_ai::EWLayer>(ew_operation,

include/layers_oneDNN/EWLayer_oneDNN.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
namespace it_lab_ai {
1111

12-
class EWLayer_oneDNN : public Layer {
12+
class EwLayerOneDnn : public Layer {
1313
public:
14-
EWLayer_oneDNN()
14+
EwLayerOneDnn()
1515
: Layer(kElementWise), func_("none"), alpha_(0.0F), beta_(0.0F) {}
1616

17-
EWLayer_oneDNN(std::string function, float alpha = 0.0F, float beta = 0.0F)
17+
EwLayerOneDnn(std::string function, float alpha = 0.0F, float beta = 0.0F)
1818
: Layer(kElementWise),
1919
func_(std::move(function)),
2020
alpha_(alpha),
@@ -33,7 +33,7 @@ class EWLayer_oneDNN : public Layer {
3333
#endif
3434

3535
private:
36-
void initialize_onednn(const Shape& shape);
36+
void initialize_onednn(const Shape& shape, Type data_type);
3737
dnnl::algorithm get_algorithm() const;
3838
void validate_input(const std::vector<Tensor>& input) const;
3939

src/layers_oneDNN/EWLayer_oneDNN.cpp

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,84 @@
1-
#include "layers_oneDNN/EWLayer_oneDNN.hpp"
1+
#include "layers_oneDNN/EwLayer_oneDnn.hpp"
22

33
#include <iostream>
44
#include <stdexcept>
55

66
namespace it_lab_ai {
77

8-
void EWLayer_oneDNN::run(const std::vector<Tensor>& input,
9-
std::vector<Tensor>& output) {
8+
void EwLayerOneDnn::run(const std::vector<Tensor>& input,
9+
std::vector<Tensor>& output) {
1010
validate_input(input);
1111

1212
const Tensor& input_tensor = input[0];
13+
Type data_type = input_tensor.get_type();
1314

1415
if (!initialized_) {
15-
initialize_onednn(input_tensor.get_shape());
16-
}
17-
if (input_tensor.get_type() != Type::kFloat) {
18-
throw std::runtime_error("oneDNN EWLayer supports only float tensors");
16+
initialize_onednn(input_tensor.get_shape(), data_type);
1917
}
2018

2119
try {
22-
const std::vector<float>& input_data = *input_tensor.as<float>();
23-
std::vector<float> output_data(input_data.size());
24-
dnnl::memory src_mem = dnnl::memory(memory_desc_, *engine_,
25-
const_cast<float*>(input_data.data()));
26-
dnnl::memory dst_mem =
27-
dnnl::memory(memory_desc_, *engine_, output_data.data());
28-
eltwise_prim_->execute(*stream_,
29-
{{DNNL_ARG_SRC, src_mem}, {DNNL_ARG_DST, dst_mem}});
30-
stream_->wait();
31-
output[0] = make_tensor(output_data, input_tensor.get_shape());
20+
if (data_type == Type::kFloat) {
21+
const std::vector<float>& input_data = *input_tensor.as<float>();
22+
std::vector<float> output_data(input_data.size());
23+
dnnl::memory src_mem = dnnl::memory(
24+
memory_desc_, *engine_, const_cast<float*>(input_data.data()));
25+
dnnl::memory dst_mem =
26+
dnnl::memory(memory_desc_, *engine_, output_data.data());
27+
eltwise_prim_->execute(
28+
*stream_, {{DNNL_ARG_SRC, src_mem}, {DNNL_ARG_DST, dst_mem}});
29+
stream_->wait();
30+
output[0] = make_tensor(output_data, input_tensor.get_shape());
31+
} else if (data_type == Type::kInt) {
32+
const std::vector<int>& input_data = *input_tensor.as<int>();
33+
std::vector<int> output_data(input_data.size());
34+
35+
std::vector<float> float_input;
36+
float_input.reserve(input_data.size());
37+
for (int val : input_data) {
38+
float_input.push_back(static_cast<float>(val));
39+
}
40+
41+
std::vector<float> float_output(input_data.size());
42+
43+
dnnl::memory src_mem =
44+
dnnl::memory(memory_desc_, *engine_, float_input.data());
45+
dnnl::memory dst_mem =
46+
dnnl::memory(memory_desc_, *engine_, float_output.data());
47+
eltwise_prim_->execute(
48+
*stream_, {{DNNL_ARG_SRC, src_mem}, {DNNL_ARG_DST, dst_mem}});
49+
stream_->wait();
50+
51+
for (size_t i = 0; i < float_output.size(); ++i) {
52+
output_data[i] = static_cast<int>(std::round(float_output[i]));
53+
}
54+
output[0] = make_tensor(output_data, input_tensor.get_shape());
55+
} else {
56+
throw std::runtime_error("EwLayerOneDnn: Unsupported data type");
57+
}
3258

3359
} catch (const std::exception& e) {
3460
std::cerr << "oneDNN execution failed: " << e.what() << std::endl;
3561
throw;
3662
}
3763
}
3864

39-
void EWLayer_oneDNN::validate_input(const std::vector<Tensor>& input) const {
65+
void EwLayerOneDnn::validate_input(const std::vector<Tensor>& input) const {
4066
if (input.size() != 1) {
41-
throw std::runtime_error("EWLayer_oneDNN: Expected exactly 1 input tensor");
67+
throw std::runtime_error("EwLayerOneDnn: Expected exactly 1 input tensor");
4268
}
4369

4470
if (!is_function_supported(func_)) {
4571
throw std::invalid_argument("Unsupported function for oneDNN: " + func_);
4672
}
73+
74+
Type data_type = input[0].get_type();
75+
if (data_type != Type::kFloat && data_type != Type::kInt) {
76+
throw std::runtime_error(
77+
"EwLayerOneDnn supports only float and int tensors");
78+
}
4779
}
4880

49-
void EWLayer_oneDNN::initialize_onednn(const Shape& shape) {
81+
void EwLayerOneDnn::initialize_onednn(const Shape& shape, Type data_type) {
5082
try {
5183
engine_ = std::make_unique<dnnl::engine>(dnnl::engine::kind::cpu, 0);
5284
stream_ = std::make_unique<dnnl::stream>(*engine_);
@@ -55,6 +87,7 @@ void EWLayer_oneDNN::initialize_onednn(const Shape& shape) {
5587
for (size_t i = 0; i < shape.dims(); i++) {
5688
dims.push_back(static_cast<dnnl::memory::dim>(shape.at(i)));
5789
}
90+
5891
dnnl::memory::format_tag format;
5992
switch (dims.size()) {
6093
case 1:
@@ -77,16 +110,22 @@ void EWLayer_oneDNN::initialize_onednn(const Shape& shape) {
77110
std::to_string(dims.size()));
78111
}
79112

80-
memory_desc_ =
81-
dnnl::memory::desc(dims, dnnl::memory::data_type::f32, format);
113+
dnnl::memory::data_type dnnl_data_type;
114+
if (data_type == Type::kFloat) {
115+
dnnl_data_type = dnnl::memory::data_type::f32;
116+
} else {
117+
dnnl_data_type = dnnl::memory::data_type::f32;
118+
}
119+
120+
memory_desc_ = dnnl::memory::desc(dims, dnnl_data_type, format);
82121

83122
dnnl::algorithm algo = get_algorithm();
84123

85-
float primitive_alpha = 0.0f;
86-
float primitive_beta = 0.0f;
124+
float primitive_alpha = 0.0F;
125+
float primitive_beta = 0.0F;
87126

88127
if (func_ == "relu") {
89-
primitive_alpha = 0.0f;
128+
primitive_alpha = 0.0F;
90129
} else if (func_ == "linear") {
91130
primitive_alpha = alpha_;
92131
primitive_beta = beta_;
@@ -100,34 +139,31 @@ void EWLayer_oneDNN::initialize_onednn(const Shape& shape) {
100139

101140
initialized_ = true;
102141

103-
for (size_t i = 0; i < dims.size(); ++i) {
104-
std::cout << dims[i];
105-
if (i < dims.size() - 1) std::cout << ", ";
106-
}
107-
std::cout << "]" << std::endl;
108-
109142
} catch (const std::exception& e) {
110143
std::cerr << "oneDNN initialization failed for function '" << func_
111144
<< "': " << e.what() << std::endl;
112145
throw;
113146
}
114147
}
115148

116-
dnnl::algorithm EWLayer_oneDNN::get_algorithm() const {
149+
dnnl::algorithm EwLayerOneDnn::get_algorithm() const {
117150
if (func_ == "relu") {
118151
return dnnl::algorithm::eltwise_relu;
119-
} else if (func_ == "tanh") {
152+
}
153+
if (func_ == "tanh") {
120154
return dnnl::algorithm::eltwise_tanh;
121-
} else if (func_ == "sigmoid") {
155+
}
156+
if (func_ == "sigmoid") {
122157
return dnnl::algorithm::eltwise_logistic;
123-
} else if (func_ == "linear") {
158+
}
159+
if (func_ == "linear") {
124160
return dnnl::algorithm::eltwise_linear;
125-
} else {
126-
throw std::invalid_argument("Unsupported function for oneDNN: " + func_);
127161
}
162+
163+
throw std::invalid_argument("Unsupported function for oneDNN: " + func_);
128164
}
129165

130-
bool EWLayer_oneDNN::is_function_supported(const std::string& function) {
166+
bool EwLayerOneDnn::is_function_supported(const std::string& function) {
131167
return (function == "relu" || function == "tanh" || function == "sigmoid" ||
132168
function == "linear");
133169
}

0 commit comments

Comments
 (0)