[TFTRT] Add Dynamic Shape Tests for ConvertSquare
Co-authored-by: Tamas Feher <tfeher@nvidia.com> - Modify ConvertSquare tests to use newer TFTRT testing API - Add INT32 as a supported dtype for TFTRT ConvertSquare
This commit is contained in:
parent
67487368bb
commit
cb61c42938
@ -4424,8 +4424,13 @@ Status ConvertSquare(OpConverterParams* params) {
|
|||||||
const auto& inputs = params->inputs;
|
const auto& inputs = params->inputs;
|
||||||
const auto& node_def = params->node_def;
|
const auto& node_def = params->node_def;
|
||||||
TF_RETURN_IF_ERROR(CheckInputsWeights(*params, {{"x", false}}));
|
TF_RETURN_IF_ERROR(CheckInputsWeights(*params, {{"x", false}}));
|
||||||
|
#if IS_TRT_VERSION_GE(6, 0, 1, 0)
|
||||||
|
TF_RETURN_IF_ERROR(AllowDataTypes(
|
||||||
|
*params, {DataType::DT_FLOAT, DataType::DT_HALF, DataType::DT_INT32}));
|
||||||
|
#else
|
||||||
TF_RETURN_IF_ERROR(
|
TF_RETURN_IF_ERROR(
|
||||||
AllowDataTypes(*params, {DataType::DT_FLOAT, DataType::DT_HALF}));
|
AllowDataTypes(*params, {DataType::DT_FLOAT, DataType::DT_HALF}));
|
||||||
|
#endif
|
||||||
if (params->validation_only) return Status::OK();
|
if (params->validation_only) return Status::OK();
|
||||||
|
|
||||||
// Constant 2 with same rank as input
|
// Constant 2 with same rank as input
|
||||||
|
@ -2754,58 +2754,40 @@ TEST_F(OpConverterTest, ConvertQuantize) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <DataType dtype>
|
TEST_P(OpConverterTest2, ConvertSquare) {
|
||||||
void TestConvertSquare(OpConverterTest* test) {
|
|
||||||
test->Reset();
|
|
||||||
typedef typename EnumToDataType<dtype>::Type CType;
|
|
||||||
|
|
||||||
Scope s = Scope::NewRootScope();
|
|
||||||
auto input = ops::Placeholder(s.WithOpName("input"), dtype);
|
|
||||||
auto square = ops::Square(s.WithOpName("my_square"), input);
|
|
||||||
NodeDef node_def = square.operation.node()->def();
|
|
||||||
|
|
||||||
test->AddTestTensor("input", {1, 20}, /*batch_size=*/1,
|
|
||||||
TfDataTypeToTrt(dtype));
|
|
||||||
test->RunValidationAndConversion(node_def);
|
|
||||||
TRT_TensorOrWeights output;
|
|
||||||
TF_EXPECT_OK(test->GetTensorOrWeights("my_square", &output));
|
|
||||||
ASSERT_TRUE(output.is_tensor());
|
|
||||||
ExpectTrtDimsEqualsArray({1, 20}, output.tensor()->getDimensions());
|
|
||||||
|
|
||||||
const int num_inputs = 20;
|
|
||||||
std::vector<CType> inputs(num_inputs);
|
|
||||||
std::vector<CType> expected_outputs(num_inputs);
|
|
||||||
for (int i = 0; i < num_inputs; ++i) {
|
|
||||||
const CType value = CType(i - 9);
|
|
||||||
inputs[i] = value;
|
|
||||||
expected_outputs[i] = value * value;
|
|
||||||
}
|
|
||||||
const DataVec input_data{{"input", test->AsTensor<CType>(inputs)}};
|
|
||||||
// Engine outputs are converted to FP16 automatically if we set FP16 mode in
|
|
||||||
// the builder.
|
|
||||||
DataVec output_data{{"my_square", test->ConstructTensor<CType>(num_inputs)}};
|
|
||||||
TF_EXPECT_OK(test->BuildAndRun(input_data, &output_data));
|
|
||||||
ExpectArrayNear(expected_outputs, GetSpanForData<CType>(output_data[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(OpConverterTest, ConvertSquare) {
|
|
||||||
{
|
{
|
||||||
// Input is weights, should fail.
|
// Input is weights, should fail.
|
||||||
Reset();
|
Reset();
|
||||||
Scope s = Scope::NewRootScope();
|
Scope s = Scope::NewRootScope();
|
||||||
auto input = ops::Placeholder(s.WithOpName("input"), DT_FLOAT);
|
auto input = ops::Placeholder(s.WithOpName("input"), tf_type);
|
||||||
auto square = ops::Square(s.WithOpName("my_square"), input);
|
auto square = ops::Square(s.WithOpName("my_square"), input);
|
||||||
NodeDef node_def = square.operation.node()->def();
|
NodeDef node_def = square.operation.node()->def();
|
||||||
AddTestWeights<float>("input", {1, 2, 3}, {1, 2, 3, 4, -5, 6});
|
AddTestWeights("input", {1, 2, 3}, {1, 2, 3, 4, -5, 6}, tf_type);
|
||||||
RunValidationAndConversion(
|
RunValidationAndConversion(
|
||||||
node_def, error::UNIMPLEMENTED,
|
node_def, error::UNIMPLEMENTED,
|
||||||
"The input \"x\" for Square must be a tensor, at my_square");
|
"The input \"x\" for Square must be a tensor, at my_square");
|
||||||
}
|
}
|
||||||
|
|
||||||
// OK. Note that kINT32 is not supported by IElementWiseLayer, so we don't
|
Reset();
|
||||||
// test DT_INT32 type here.
|
|
||||||
TestConvertSquare<DT_FLOAT>(this);
|
Scope s = Scope::NewRootScope();
|
||||||
TestConvertSquare<DT_HALF>(this);
|
auto input = ops::Placeholder(s.WithOpName("input"), tf_type);
|
||||||
|
auto square = ops::Square(s.WithOpName("my_square"), input);
|
||||||
|
NodeDef node_def = square.operation.node()->def();
|
||||||
|
|
||||||
|
const int num_inputs = 20;
|
||||||
|
std::vector<float> inputs(num_inputs);
|
||||||
|
std::vector<float> expected_outputs(num_inputs);
|
||||||
|
|
||||||
|
for (int i = 0; i < num_inputs; ++i) {
|
||||||
|
const float value = (i - 9);
|
||||||
|
inputs[i] = value;
|
||||||
|
expected_outputs[i] = value * value;
|
||||||
|
}
|
||||||
|
AddTestTensor("input", {1, 1, 20}, tf_type, inputs);
|
||||||
|
|
||||||
|
TestOpConverter("my_square", node_def, {1, 1, 20}, Status::OK(), Status::OK(),
|
||||||
|
ArrayFloatNear(expected_outputs, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if IS_TRT_VERSION_GE(5, 1, 0, 0)
|
#if IS_TRT_VERSION_GE(5, 1, 0, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user