Skip to content

Commit 54b9c67

Browse files
Fixed unreasonable type of parameter in broadcast (#505)
Change type of shape from int to uint & add ut for broadcast From github issue #376 Define opversion to avoid incompatibility with upper level software Type: Code Improvement Signed-off-by: Feiyue Chen <Feiyue.Chen@verisilicon.com>
1 parent e8dab60 commit 54b9c67

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

include/tim/vx/ops/broadcast.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#ifndef OVXLIBXX_OPERATIONS_BROADCAST_H_
2525
#define OVXLIBXX_OPERATIONS_BROADCAST_H_
2626
#include "tim/vx/builtin_op.h"
27-
27+
#define BROADCAST_OPVERSION 1
2828
namespace tim {
2929
namespace vx {
3030
namespace ops {
@@ -45,12 +45,12 @@ namespace ops {
4545

4646
class Broadcast : public BuiltinOp {
4747
public:
48-
Broadcast(Graph* graph, const std::vector<int32_t>& shape, const std::vector<int32_t>& dimensions = {});
48+
Broadcast(Graph* graph, const std::vector<uint32_t>& shape, const std::vector<int32_t>& dimensions = {});
4949

5050
std::shared_ptr<Operation> Clone(std::shared_ptr<Graph>& graph) const override;
5151

52-
protected:
53-
const std::vector<int32_t> shape_;
52+
protected:
53+
std::vector<uint32_t> shape_;
5454
std::vector<int32_t> dimensions_;
5555
};
5656

src/tim/vx/ops/broadcast.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,24 @@
3030
namespace tim {
3131
namespace vx {
3232
namespace ops {
33-
Broadcast::Broadcast(Graph* graph, const std::vector<int32_t>& shape,
33+
Broadcast::Broadcast(Graph* graph, const std::vector <uint32_t>& shape,
3434
const std::vector<int32_t>& dimensions)
3535
: BuiltinOp(graph, VSI_NN_OP_EXPAND_BROADCAST),
3636
shape_(shape),
3737
dimensions_(dimensions) {
3838
this->impl()->node()->nn_param.expand_broadcast.dim_num = shape_.size();
39-
this->impl()->node()->nn_param.expand_broadcast.shape = (uint32_t*)shape_.data();
39+
this->impl()->node()->nn_param.expand_broadcast.shape = shape_.data();
4040
#ifdef VSI_EXPAND_BROADCAST_ENABLE_DIMENSIONS
4141
this->impl()->node()->nn_param.expand_broadcast.dimensions_num = dimensions_.size();
4242
if (dimensions.size() > 0)
4343
{
44-
int dim_num = shape.size();
45-
for (uint32_t i = 0; i < dimensions.size(); ++i) {
46-
dimensions_[i] += (dimensions[i] < 0 ? dim_num : 0U);
44+
for (uint32_t i = 0; i < dimensions.size(); i++)
45+
{
46+
dimensions_[i] += (dimensions[i] < 0 ? shape_.size() : 0U );
4747
}
4848
this->impl()->node()->nn_param.expand_broadcast.dimensions = (uint32_t*)dimensions_.data();
49-
} else {
49+
} else
50+
{
5051
this->impl()->node()->nn_param.expand_broadcast.dimensions = nullptr;
5152
}
5253
#endif

src/tim/vx/ops/broadcast_test.cc

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ TEST(Broadcast, ScalarTo2D_2x3) {
6262
std::vector<float> golden = {
6363
2.25f, 2.25f, 2.25f, 2.25f, 2.25f, 2.25f,
6464
};
65-
std::vector<int32_t> shape = {3, 2};
65+
std::vector<uint32_t> shape = {3, 2};
6666

6767
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
6868
in_data.size() * sizeof(float)));
@@ -93,7 +93,7 @@ TEST(Broadcast, 1DTo2D) {
9393
std::vector<float> golden = {
9494
1.f, 2.f, 3.f, 1.f, 2.f, 3.f,
9595
};
96-
std::vector<int32_t> shape = {3, 2};
96+
std::vector<uint32_t> shape = {3, 2};
9797

9898
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
9999
in_data.size() * sizeof(float)));
@@ -125,7 +125,7 @@ TEST(Broadcast, 1DTo2D_WithDims0) {
125125
1.f, 2.f,
126126
1.f, 2.f,
127127
};
128-
std::vector<int32_t> shape = {2, 2};
128+
std::vector<uint32_t> shape = {2, 2};
129129
std::vector<int32_t> dimensions = {0};
130130

131131
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
@@ -158,7 +158,7 @@ TEST(Broadcast, 1DTo2D_WithDims1) {
158158
1.f, 1.f,
159159
2.f, 2.f,
160160
};
161-
std::vector<int32_t> shape = {2, 2};
161+
std::vector<uint32_t> shape = {2, 2};
162162
std::vector<int32_t> dimensions = {1};
163163

164164
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
@@ -170,6 +170,44 @@ TEST(Broadcast, 1DTo2D_WithDims1) {
170170
CheckResult(graph, golden, output_tensor);
171171
}
172172

173+
TEST(Broadcast, 1DTo2D_WithDimsMinus2) {
174+
auto ctx = tim::vx::Context::Create();
175+
auto graph = ctx->CreateGraph();
176+
177+
tim::vx::ShapeType input_shape({3});
178+
tim::vx::ShapeType output_shape({3, 2});
179+
tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, input_shape,
180+
tim::vx::TensorAttribute::INPUT);
181+
tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, output_shape,
182+
tim::vx::TensorAttribute::OUTPUT);
183+
184+
auto input_tensor = graph->CreateTensor(input_spec);
185+
auto output_tensor = graph->CreateTensor(output_spec);
186+
187+
std::vector<float> in_data = {
188+
1.f, 2.f, 3.f
189+
};
190+
std::vector<float> golden = {
191+
1.f, 2.f, 3.f,
192+
1.f, 2.f, 3.f
193+
};
194+
std::vector<uint32_t> shape = {3, 2};
195+
std::vector<int32_t> dimensions = {-2};
196+
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
197+
in_data.size() * sizeof(float)));
198+
199+
auto op = graph->CreateOperation<tim::vx::ops::Broadcast>(shape, dimensions);
200+
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
201+
202+
203+
EXPECT_TRUE(graph->Compile());
204+
EXPECT_TRUE(graph->Run());
205+
206+
std::vector<float> output(golden.size());
207+
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
208+
EXPECT_EQ(golden, output);
209+
}
210+
173211
TEST(Broadcast, 1DTo3D_WithDims0) {
174212
auto ctx = tim::vx::Context::Create();
175213
auto graph = ctx->CreateGraph();
@@ -190,7 +228,7 @@ TEST(Broadcast, 1DTo3D_WithDims0) {
190228
std::vector<float> golden = {
191229
1.f, 2.f, 1.f, 2.f, 1.f, 2.f, 1.f, 2.f,
192230
};
193-
std::vector<int32_t> shape = {2, 2, 2};
231+
std::vector<uint32_t> shape = {2, 2, 2};
194232
std::vector<int32_t> dimensions = {0};
195233

196234
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
@@ -222,7 +260,7 @@ TEST(Broadcast, 1DTo3D_WithDims1) {
222260
std::vector<float> golden = {
223261
1.f, 1.f, 2.f, 2.f, 1.f, 1.f, 2.f, 2.f,
224262
};
225-
std::vector<int32_t> shape = {2, 2, 2};
263+
std::vector<uint32_t> shape = {2, 2, 2};
226264
std::vector<int32_t> dimensions = {1};
227265

228266
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
@@ -254,7 +292,7 @@ TEST(Broadcast, 1DTo3D_WithDims2) {
254292
std::vector<float> golden = {
255293
1.f, 1.f, 1.f, 1.f, 2.f, 2.f, 2.f, 2.f,
256294
};
257-
std::vector<int32_t> shape = {2, 2, 2};
295+
std::vector<uint32_t> shape = {2, 2, 2};
258296
std::vector<int32_t> dimensions = {2};
259297

260298
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
@@ -286,7 +324,7 @@ TEST(Broadcast, 2DTo3D_WithDims02) {
286324
std::vector<float> golden = {
287325
1.f, 5.f, 1.f, 5.f, 2.f, 6.f, 2.f, 6.f,
288326
};
289-
std::vector<int32_t> shape = {2, 2, 2};
327+
std::vector<uint32_t> shape = {2, 2, 2};
290328
std::vector<int32_t> dimensions = {0, 2};
291329

292330
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
@@ -318,7 +356,7 @@ TEST(Broadcast, 2DTo3D_WithDims12) {
318356
std::vector<float> golden = {
319357
1.f, 1.f, 5.f, 5.f, 2.f, 2.f, 6.f, 6.f,
320358
};
321-
std::vector<int32_t> shape = {2, 2, 2};
359+
std::vector<uint32_t> shape = {2, 2, 2};
322360
std::vector<int32_t> dimensions = {1, 2};
323361

324362
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),

0 commit comments

Comments
 (0)