Skip to content

Commit

Permalink
[moco] Use must_cast (Samsung#798)
Browse files Browse the repository at this point in the history
* [moco] Use must_cast

This will revise moco to use must_cast() to resolve static analysis warnings

ONE-DCO-1.0-Signed-off-by: seongwoo <[email protected]>

* remove unnecessary assert

* remove unnecessary assert
  • Loading branch information
mhs4670go authored May 14, 2020
1 parent 4224eea commit ae48497
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 30 deletions.
3 changes: 1 addition & 2 deletions compiler/moco/import/src/Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ void convert_graph(const moco::GraphBuilderSource &source, const moco::ModelSign

auto graph_input = graph->inputs()->create();

auto placeholder_node = dynamic_cast<moco::TFPlaceholder *>(node);
assert(placeholder_node != nullptr);
auto placeholder_node = loco::must_cast<moco::TFPlaceholder *>(node);

graph_input->name(input.nodeName());

Expand Down
4 changes: 2 additions & 2 deletions compiler/moco/import/src/Nodes/BiasAdd.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST(TensorFlowImport, bias_add_01)
tester.output("out");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFBiasAdd *>(tester.output());
auto test_node = loco::must_cast<moco::TFBiasAdd *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_TRUE(test_node->data_layout() == "NHWC");
}
Expand Down Expand Up @@ -106,7 +106,7 @@ TEST(TensorFlowImport, bias_add_NCHW_axis)
tester.output("out");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFBiasAdd *>(tester.output());
auto test_node = loco::must_cast<moco::TFBiasAdd *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_TRUE(test_node->data_layout() == "NCHW");
}
4 changes: 2 additions & 2 deletions compiler/moco/import/src/Nodes/Concat.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ TEST(TensorFlowImport, concat_01)
tester.output("Concat");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConcatV2 *>(tester.output());
auto test_node = loco::must_cast<moco::TFConcatV2 *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->num_values(), 2);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ TEST(TensorFlowImport, concat_02)
tester.output("Concat");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConcatV2 *>(tester.output());
auto test_node = loco::must_cast<moco::TFConcatV2 *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->num_values(), 3);
}
14 changes: 7 additions & 7 deletions compiler/moco/import/src/Nodes/Const.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ TEST(TensorFlowImport, const_float_01)
tester.output("const/float");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConst *>(tester.output());
auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
Expand Down Expand Up @@ -142,7 +142,7 @@ TEST(TensorFlowImport, const_float_02)
tester.output("const/float");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConst *>(tester.output());
auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
Expand Down Expand Up @@ -206,7 +206,7 @@ TEST(TensorFlowImport, const_float_03)
tester.output("const/float");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConst *>(tester.output());
auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
Expand Down Expand Up @@ -270,7 +270,7 @@ TEST(TensorFlowImport, const_float_04)
tester.output("const/float");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConst *>(tester.output());
auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
Expand Down Expand Up @@ -334,7 +334,7 @@ TEST(TensorFlowImport, const_int32_04)
tester.output("const/int");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConst *>(tester.output());
auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->size<loco::DataType::S32>(), 6);
ASSERT_EQ(test_node->at<loco::DataType::S32>(0), 1);
Expand Down Expand Up @@ -391,7 +391,7 @@ TEST(TensorFlowImport, const_int32_scalar)
tester.output("const/int");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConst *>(tester.output());
auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->size<loco::DataType::S32>(), 1);
ASSERT_EQ(test_node->at<loco::DataType::S32>(0), 3);
Expand Down Expand Up @@ -453,7 +453,7 @@ TEST(TensorFlowImport, const_int8_01)
tester.output("const/int8");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFConst *>(tester.output());
auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->size<loco::DataType::S8>(), 6);
ASSERT_EQ(test_node->at<loco::DataType::S8>(0), 0);
Expand Down
4 changes: 2 additions & 2 deletions compiler/moco/import/src/Nodes/Mean.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TEST(TensorFlowImport, mean_true)
tester.output("Mean");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFMean *>(tester.output());
auto test_node = loco::must_cast<moco::TFMean *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->keep_dims(), true);
}
Expand Down Expand Up @@ -114,7 +114,7 @@ TEST(TensorFlowImport, mean_false)
tester.output("Mean");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFMean *>(tester.output());
auto test_node = loco::must_cast<moco::TFMean *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->keep_dims(), false);
}
2 changes: 1 addition & 1 deletion compiler/moco/import/src/Nodes/Pack.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ TEST(TensorFlowImport, tf_pack_basic)
tester.output("Pack");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFPack *>(tester.output());
auto test_node = loco::must_cast<moco::TFPack *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->N(), 4);
ASSERT_NE(test_node->values(0), nullptr);
Expand Down
2 changes: 1 addition & 1 deletion compiler/moco/import/src/Nodes/StridedSlice.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ TEST(TensorFlowImport, tf_stridedslice_basic)
tester.output("StridedSlice");
tester.run(nodedef, graphbuilder);

auto test_node = dynamic_cast<moco::TFStridedSlice *>(tester.output());
auto test_node = loco::must_cast<moco::TFStridedSlice *>(tester.output());
ASSERT_NE(test_node, nullptr);
ASSERT_EQ(test_node->begin_mask(), 0);
ASSERT_EQ(test_node->end_mask(), 0);
Expand Down
7 changes: 3 additions & 4 deletions compiler/moco/pass/src/Passes/ConstantFoldStridedSlice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ namespace

loco::TensorShape calc_output_shape(moco::TFStridedSlice *node)
{
auto const_input = dynamic_cast<moco::TFConst *>(node->input());
auto const_begin = dynamic_cast<moco::TFConst *>(node->begin());
auto const_end = dynamic_cast<moco::TFConst *>(node->end());
auto const_strides = dynamic_cast<moco::TFConst *>(node->strides());
auto const_input = loco::must_cast<moco::TFConst *>(node->input());
auto const_begin = loco::must_cast<moco::TFConst *>(node->begin());
auto const_end = loco::must_cast<moco::TFConst *>(node->end());
auto input_rank = const_input->rank();
auto output_rank = input_rank;
loco::TensorShape output_shape_range;
Expand Down
3 changes: 1 addition & 2 deletions compiler/moco/pass/src/Passes/FuseBinaryIntoPreceding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,7 @@ bool fuse_to_preceding(loco::Graph *graph, moco::TFAdd *node)
}

// Let's fuse addparam into biasadd bias
auto biasadd_bias = dynamic_cast<moco::TFConst *>(biasadd->bias());
assert(biasadd_bias != nullptr);
auto biasadd_bias = loco::must_cast<moco::TFConst *>(biasadd->bias());
if (!shape_match(biasadd_bias, addparam))
{
// INFO(l) << "TFBiasAdd bias and TFAdd input shape mismatch";
Expand Down
11 changes: 5 additions & 6 deletions compiler/moco/service/src/Service/TFShapeInferenceRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,7 @@ class ShapeInferenceAlgorithm final : public moco::TFNodeVisitor<loco::NodeShape
auto input_shape = node_shape(node->input());
assert(input_shape.domain() == loco::Domain::Tensor);

auto const_paddings = dynamic_cast<moco::TFConst *>(node->paddings());
assert(const_paddings);
auto const_paddings = loco::must_cast<moco::TFConst *>(node->paddings());
assert(const_paddings->dtype() == loco::DataType::S32);
assert(const_paddings->rank() == 2);

Expand Down Expand Up @@ -707,9 +706,9 @@ class ShapeInferenceAlgorithm final : public moco::TFNodeVisitor<loco::NodeShape
assert(node->ellipsis_mask() == 0);
assert(node->shrink_axis_mask() == 1);

auto const_begin = dynamic_cast<moco::TFConst *>(node->begin());
auto const_end = dynamic_cast<moco::TFConst *>(node->end());
auto const_strides = dynamic_cast<moco::TFConst *>(node->strides());
auto const_begin = loco::must_cast<moco::TFConst *>(node->begin());
auto const_end = loco::must_cast<moco::TFConst *>(node->end());
auto const_strides = loco::must_cast<moco::TFConst *>(node->strides());

assert(dynamic_cast<moco::TFConst *>(node->input()) != nullptr);
assert(const_begin != nullptr);
Expand Down Expand Up @@ -880,7 +879,7 @@ void TFShapeInferenceRule::infer(const Context *ctx, const loco::Node *node, Sin
assert(dynamic_cast<const TFNode *>(node) != nullptr);

ShapeInferenceAlgorithm alg{ctx};
auto shape = dynamic_cast<const TFNode *>(node)->accept(&alg);
auto shape = loco::must_cast<const TFNode *>(node)->accept(&alg);

if (shape.domain() == loco::Domain::Unknown)
sink->fail();
Expand Down
2 changes: 1 addition & 1 deletion compiler/moco/service/src/Service/TFTypeInferenceRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ bool TFTypeInferenceRule::infer(const loco::Node *node, loco::DataType &dtype) c
#define TENSORFLOW_NODE(OPCODE,CLASS) \
if (dynamic_cast<const moco::CLASS *>(node)) \
{ \
auto tfnode = dynamic_cast<const moco::CLASS *>(node); \
auto tfnode = loco::must_cast<const moco::CLASS *>(node); \
dtype = tfnode->accept(&alg); \
assert(dtype != loco::DataType::Unknown); \
return true; \
Expand Down

0 comments on commit ae48497

Please sign in to comment.