Skip to content

Commit b576bc9

Browse files
committed
build dens,resn, yolo, google/ res&dens - perfect
1 parent 58ad832 commit b576bc9

File tree

4 files changed

+153
-27
lines changed

4 files changed

+153
-27
lines changed

app/Graph/build.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
289289
it_lab_ai::ImplType impl2 = parallel ? it_lab_ai::kSTL : it_lab_ai::kDefault;
290290

291291
std::unordered_map<std::string, std::vector<int64_t>> layer_parameters;
292+
std::string last_constant_name;
293+
std::vector<int64_t> last_constant_value;
292294

293295
std::unordered_map<std::string, std::shared_ptr<it_lab_ai::SplitLayer>>
294296
split_layers;
@@ -814,7 +816,6 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
814816
if (comments) {
815817
std::cout << "ReduceSum layer: " << layer_name << std::endl;
816818
}
817-
818819
int64_t keepdims = 0;
819820
if (layer_data.contains("attributes")) {
820821
const auto& attributes = layer_data["attributes"];
@@ -830,12 +831,14 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
830831
std::string constant_name = inputs[1].get<std::string>();
831832
constant_name = get_base_layer_name(constant_name);
832833

833-
if (layer_parameters.count(constant_name)) {
834+
if (layer_parameters.count(constant_name)){
834835
axes = layer_parameters[constant_name];
836+
} else if (constant_name.find("onnx::") != constant_name.npos) {
837+
axes = last_constant_value;
838+
layer_parameters[constant_name] = last_constant_value;
835839
}
836840
}
837841
}
838-
839842
auto reduce_layer = std::make_shared<it_lab_ai::ReduceLayer>(
840843
it_lab_ai::ReduceLayer::Operation::kSum, keepdims, axes);
841844
reduce_layer->setName(it_lab_ai::kReduce);
@@ -854,6 +857,8 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
854857
data.push_back(val.get<int64_t>());
855858
}
856859
layer_parameters[layer_name] = data;
860+
last_constant_name = layer_name;
861+
last_constant_value = data;
857862
}
858863
}
859864

include/layers/FCLayer.hpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,39 @@ template <typename ValueType>
3030
std::vector<ValueType> mat_vec_mul(const std::vector<ValueType>& mat,
3131
const Shape& mat_shape,
3232
const std::vector<ValueType>& vec) {
33-
size_t c = vec.size() / mat_shape[1];
33+
std::cout << " mat_vec_mul DEBUG:" << std::endl;
34+
std::cout << " Matrix size: " << mat.size() << std::endl;
35+
std::cout << " Matrix shape: [" << mat_shape[0] << ", " << mat_shape[1]
36+
<< "]" << std::endl;
37+
std::cout << " Vector size: " << vec.size() << std::endl;
38+
3439
if (mat_shape.dims() != 2) {
3540
throw std::invalid_argument("Not a matrix in argument");
3641
}
42+
43+
size_t batch_size = vec.size() / mat_shape[0];
44+
std::cout << " Batch size: " << batch_size << std::endl;
45+
46+
if (vec.size() % mat_shape[0] != 0) {
47+
throw std::invalid_argument("Vector size not divisible by matrix rows");
48+
}
49+
3750
Shape res_shape(1);
38-
res_shape[0] = mat_shape[0] * c;
51+
res_shape[0] = mat_shape[1] * batch_size;
3952
std::vector<ValueType> res(res_shape[0]);
53+
std::cout << " Result size: " << res.size() << std::endl;
54+
4055
ValueType elem;
41-
for (size_t count = 0; count < c; count++) {
42-
for (size_t i = 0; i < mat_shape[0]; i++) {
56+
for (size_t batch = 0; batch < batch_size; batch++) {
57+
for (size_t j = 0; j < mat_shape[1]; j++) {
4358
elem = ValueType(0);
44-
for (size_t j = 0; j < mat_shape[1]; j++) {
45-
// due to 1d indexing
46-
elem += mat[i * mat_shape[1] + j] * vec[count * mat_shape[1] + j];
59+
for (size_t i = 0; i < mat_shape[0]; i++) {
60+
elem += mat[i * mat_shape[1] + j] * vec[batch * mat_shape[0] + i];
4761
}
48-
res[count * mat_shape[0] + i] = elem;
62+
res[batch * mat_shape[1] + j] = elem;
4963
}
5064
}
65+
5166
return res;
5267
}
5368

@@ -103,30 +118,40 @@ FCLayerImpl<ValueType>::FCLayerImpl(const std::vector<ValueType>& input_weights,
103118
if (input_weights.empty()) {
104119
throw std::invalid_argument("Empty weights for FCLayer");
105120
}
106-
/*if (input_weights_shape.dims() != 2 ||
107-
input_weights_shape[0] != input_bias.size()) {
108-
throw std::invalid_argument("Invalid weights shape");
109-
}*/
110-
this->inputShape_[0] = input_weights_shape[1];
111-
this->outputShape_[0] = input_bias.size();
121+
122+
// Äëÿ òðàíñïîíèðîâàííîé ìàòðèöû [input_size, output_size] = [2048, 1000]
123+
this->inputShape_[0] = input_weights_shape[0]; // input size = 2048
124+
this->outputShape_[0] = input_weights_shape[1]; // output size = 1000
125+
112126
if (this->inputShape_[0] == 0 || this->outputShape_[0] == 0) {
113127
throw std::invalid_argument("Invalid weights/bias size for FCLayer");
114128
}
129+
130+
// Ïðîâåðÿåì ñîîòâåòñòâèå bias è output size
131+
if (input_bias.size() != this->outputShape_[0]) {
132+
throw std::invalid_argument("Bias size doesn't match output size");
133+
}
134+
115135
weights_.resize(input_weights_shape.count(), ValueType(0));
116136
}
117137

118138
template <typename ValueType>
119139
std::vector<ValueType> FCLayerImpl<ValueType>::run(
120140
const std::vector<ValueType>& input) const {
121-
Shape cur_w_shape({this->outputShape_[0], this->inputShape_[0]});
141+
// Äëÿ òðàíñïîíèðîâàííîé ìàòðèöû: [input_size, output_size] = [2048, 1000]
142+
Shape cur_w_shape({this->inputShape_[0], this->outputShape_[0]});
143+
122144
std::vector<ValueType> output_values =
123145
mat_vec_mul(weights_, cur_w_shape, input);
124-
for (size_t p = 0; p < output_values.size() / bias_.size(); ++p) {
146+
147+
// Äîáàâëÿåì bias ê êàæäîìó ýëåìåíòó âûõîäíîãî âåêòîðà
148+
size_t batch_size = output_values.size() / this->outputShape_[0];
149+
for (size_t batch = 0; batch < batch_size; ++batch) {
125150
for (size_t i = 0; i < bias_.size(); ++i) {
126-
output_values[p * bias_.size() + i] += bias_[i];
151+
output_values[batch * this->outputShape_[0] + i] += bias_[i];
127152
}
128153
}
154+
129155
return output_values;
130156
}
131-
132157
} // namespace it_lab_ai

src/layers/FCLayer.cpp

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,90 @@ void FCLayer::run(const std::vector<Tensor>& input,
1616

1717
// Ïîëó÷àåì batch_size è output_size
1818
size_t batch_size = input[0].get_shape()[0];
19-
size_t output_size =
20-
bias_.get_shape()[0]; // Èñïîëüçóåì ðàçìåð bias äëÿ output_size
19+
size_t output_size = bias_.get_shape()[0];
20+
21+
// Äîáàâëÿåì îòëàäî÷íûå âûâîäû
22+
std::cout << "FCLayer DEBUG:" << std::endl;
23+
std::cout << " Input shape: ";
24+
for (size_t d = 0; d < input[0].get_shape().dims(); ++d) {
25+
std::cout << input[0].get_shape()[d] << " ";
26+
}
27+
std::cout << std::endl;
28+
29+
std::cout << " Weights shape: ";
30+
for (size_t d = 0; d < weights_.get_shape().dims(); ++d) {
31+
std::cout << weights_.get_shape()[d] << " ";
32+
}
33+
std::cout << std::endl;
34+
35+
std::cout << " Bias shape: ";
36+
for (size_t d = 0; d < bias_.get_shape().dims(); ++d) {
37+
std::cout << bias_.get_shape()[d] << " ";
38+
}
39+
std::cout << std::endl;
40+
41+
std::cout << " Batch size: " << batch_size << std::endl;
42+
std::cout << " Output size: " << output_size << std::endl;
2143

2244
switch (input[0].get_type()) {
2345
case Type::kInt: {
2446
FCLayerImpl<int> used_impl(*weights_.as<int>(), weights_.get_shape(),
2547
*bias_.as<int>());
26-
output[0] = make_tensor(used_impl.run(*input[0].as<int>()),
27-
{batch_size, output_size});
48+
49+
// Äîáàâëÿåì îòëàäî÷íûé âûâîä ïåðåä âûçîâîì run
50+
std::cout << " Running INT implementation" << std::endl;
51+
52+
auto result = used_impl.run(*input[0].as<int>());
53+
54+
// Äîáàâëÿåì îòëàäî÷íûé âûâîä ïîñëå âû÷èñëåíèé
55+
std::cout << " Result vector size: " << result.size() << std::endl;
56+
std::cout << " Expected output shape: [" << batch_size << ", "
57+
<< output_size << "]" << std::endl;
58+
std::cout << " Expected total elements: " << batch_size * output_size
59+
<< std::endl;
60+
61+
// Ïðîâåðÿåì ðàçìåð ðåçóëüòàòà
62+
if (result.size() != batch_size * output_size) {
63+
throw std::runtime_error("Result size mismatch: got " +
64+
std::to_string(result.size()) + ", expected " +
65+
std::to_string(batch_size * output_size));
66+
}
67+
68+
output[0] = make_tensor(result, {batch_size, output_size});
2869
break;
2970
}
3071
case Type::kFloat: {
3172
FCLayerImpl<float> used_impl(*weights_.as<float>(), weights_.get_shape(),
3273
*bias_.as<float>());
33-
output[0] = make_tensor(used_impl.run(*input[0].as<float>()),
34-
{batch_size, output_size});
74+
75+
// Äîáàâëÿåì îòëàäî÷íûé âûâîä ïåðåä âûçîâîì run
76+
std::cout << " Running FLOAT implementation" << std::endl;
77+
78+
auto result = used_impl.run(*input[0].as<float>());
79+
80+
// Äîáàâëÿåì îòëàäî÷íûé âûâîä ïîñëå âû÷èñëåíèé
81+
std::cout << " Result vector size: " << result.size() << std::endl;
82+
std::cout << " Expected output shape: [" << batch_size << ", "
83+
<< output_size << "]" << std::endl;
84+
std::cout << " Expected total elements: " << batch_size * output_size
85+
<< std::endl;
86+
87+
// Ïðîâåðÿåì ðàçìåð ðåçóëüòàòà
88+
if (result.size() != batch_size * output_size) {
89+
throw std::runtime_error("Result size mismatch: got " +
90+
std::to_string(result.size()) + ", expected " +
91+
std::to_string(batch_size * output_size));
92+
}
93+
94+
output[0] = make_tensor(result, {batch_size, output_size});
3595
break;
3696
}
3797
default: {
3898
throw std::runtime_error("No such type");
3999
}
40100
}
101+
102+
std::cout << " FCLayer completed successfully" << std::endl;
41103
}
42104

43105
} // namespace it_lab_ai

src/layers/ReduceLayer.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,51 @@ void ReduceLayer::run(const std::vector<Tensor>& input,
173173
throw std::runtime_error("ReduceLayer: Input tensors not 1");
174174
}
175175

176+
// ÎÒËÀÄÎ×ÍÛÉ ÂÛÂÎÄ
177+
std::cout << "=== REDUCE LAYER DEBUG ===" << std::endl;
178+
std::cout << "Input shape: [";
179+
for (size_t i = 0; i < input[0].get_shape().dims(); ++i) {
180+
std::cout << input[0].get_shape()[i];
181+
if (i < input[0].get_shape().dims() - 1) std::cout << ", ";
182+
}
183+
std::cout << "]" << std::endl;
184+
std::cout << "Keep dims: " << keepdims_ << std::endl;
185+
std::cout << "Axes: [";
186+
for (size_t i = 0; i < axes_.size(); ++i) {
187+
std::cout << axes_[i];
188+
if (i < axes_.size() - 1) std::cout << ", ";
189+
}
190+
std::cout << "]" << std::endl;
191+
176192
if (input[0].get_shape().count() == 0) {
193+
std::cout << "Empty input tensor detected" << std::endl;
177194
output[0] = make_tensor<float>({0.0F}, {});
178195
return;
179196
}
180197

181198
// Ïðîñòî èñïîëüçóåì ñîõðàíåííûå axes
182199
std::vector<int64_t> axes_indices = axes_;
183200
normalize_axes(input[0].get_shape(), axes_indices);
201+
202+
// Îòëàäî÷íûé âûâîä ïîñëå íîðìàëèçàöèè
203+
std::cout << "Normalized axes: [";
204+
for (size_t i = 0; i < axes_indices.size(); ++i) {
205+
std::cout << axes_indices[i];
206+
if (i < axes_indices.size() - 1) std::cout << ", ";
207+
}
208+
std::cout << "]" << std::endl;
209+
184210
Shape output_shape =
185211
calculate_output_shape(input[0].get_shape(), axes_indices);
186212

213+
std::cout << "Output shape: [";
214+
for (size_t i = 0; i < output_shape.dims(); ++i) {
215+
std::cout << output_shape[i];
216+
if (i < output_shape.dims() - 1) std::cout << ", ";
217+
}
218+
std::cout << "]" << std::endl;
219+
std::cout << "==========================" << std::endl;
220+
187221
switch (input[0].get_type()) {
188222
case Type::kFloat:
189223
compute<float>(input[0], output_shape, axes_indices, output[0]);

0 commit comments

Comments
 (0)