Skip to content

Commit 2111eb3

Browse files
Merge pull request #45597 from tfeher:trt_pad_dynamic
PiperOrigin-RevId: 352579080 Change-Id: I10d3baa4cac957867dffce6f9240edc9ab95e2ba
2 parents ea46ce2 + c6259fc commit 2111eb3

2 files changed

Lines changed: 192 additions & 94 deletions

File tree

‎tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc‎

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,15 +4740,22 @@ Status ConvertPad(OpConverterParams* params) {
47404740
const auto& node_def = params->node_def;
47414741
TF_RETURN_IF_ERROR(
47424742
CheckInputsWeights(*params, {{"tensor", false}, {"paddings", true}}));
4743-
TF_RETURN_IF_ERROR(
4744-
AllowDataTypes(*params, {DataType::DT_FLOAT, DataType::DT_HALF}));
4743+
TF_RETURN_IF_ERROR(AllowDataTypes(
4744+
*params, {DataType::DT_FLOAT, DataType::DT_HALF, DataType::DT_INT8}));
47454745

47464746
// Implement tensor binaryOp weight [channel wise] for now;
47474747
nvinfer1::ITensor* tensor = inputs.at(0).tensor();
47484748
const auto dims = tensor->getDimensions();
47494749
// Restore implicit batch dimension
4750-
const int nb_dims = dims.nbDims + 1;
4750+
const int nb_dims =
4751+
params->use_implicit_batch ? dims.nbDims + 1 : dims.nbDims;
47514752

4753+
// TODO(tfeher): Support nb_dims < 4 by inserting extra dimensions to the
4754+
// original input.
4755+
if (nb_dims < 4) {
4756+
return errors::InvalidArgument("Convertpad requires at least 4D input, at ",
4757+
node_def.name());
4758+
}
47524759
TRT_ShapedWeights pads = inputs.at(1).weights();
47534760

47544761
TFAttrs attrs(node_def);
@@ -4758,9 +4765,9 @@ Status ConvertPad(OpConverterParams* params) {
47584765
// TODO(jie): handle data type conversion for TRT?
47594766

47604767
if (pads.shape_.d[0] != nb_dims || pads.shape_.d[1] != 2) {
4761-
return errors::InvalidArgument(
4762-
"Pad only supports explicit padding on 4 dimensional tensor, at ",
4763-
node_def.name());
4768+
return errors::InvalidArgument("Paddings at ", node_def.name(),
4769+
" must be a weight with shape [n, 2], "
4770+
"where n is the rank of input tensor");
47644771
}
47654772

47664773
// Only expect to handle INT32 as attributes for now
@@ -4769,72 +4776,100 @@ Status ConvertPad(OpConverterParams* params) {
47694776
}
47704777
auto pad_data = static_cast<int*>(pads.GetValues());
47714778

4772-
std::vector<int32_t> pad_index;
4779+
std::vector<int32_t> tf_pad_index;
47734780
for (int i = 0; i < nb_dims; i++) {
47744781
if (pad_data[2 * i] != 0 || pad_data[2 * i + 1] != 0) {
4775-
pad_index.push_back(i);
4782+
tf_pad_index.push_back(i);
47764783
}
47774784
}
47784785

47794786
// No padding at all, we should exit
4780-
if (pad_index.empty()) {
4787+
if (tf_pad_index.empty()) {
47814788
params->outputs->push_back(inputs.at(0));
47824789
return Status::OK();
47834790
}
47844791

4785-
// Only supports padding on less than 2 axis GIE-2579
4786-
if (pad_index.size() > 2) {
4792+
// TRT pad layer can only support padding on up to 2 dimensions (TRT-2579).
4793+
// TODO(tfeher): Use multiple TRT pad layers to support padding on more than 2
4794+
// dimensions.
4795+
if (tf_pad_index.size() > 2) {
47874796
return errors::InvalidArgument(
47884797
"Padding layer does not support padding on > 2");
47894798
}
47904799

47914800
// Padding on batch dimension is not supported
4792-
if (pad_index[0] == 0) {
4801+
if (params->use_implicit_batch && tf_pad_index[0] == 0) {
47934802
return errors::InvalidArgument(
47944803
"Padding layer does not support padding on batch dimension");
47954804
}
47964805

4797-
// Not doing the legit thing here. ignoring padding on dim 1 and 3;
4798-
// TODO(jie): implement pad as uff parser
4799-
if (pad_index.size() == 2 && pad_index[0] == 0 && pad_index[1] == 3) {
4800-
return errors::Unimplemented(
4801-
"Padding layer does not support padding on dimension 1 and 3 yet");
4802-
}
48034806
if (params->validation_only) return Status::OK();
48044807

4805-
bool legit_pad = true;
4808+
// TRT can only do the padding at the last two dimensions. We transpose the
4809+
// input tensor if needed.
4810+
bool transposed_pad = false;
4811+
std::vector<int> transpose_idx(nb_dims);
4812+
std::iota(transpose_idx.begin(), transpose_idx.end(), 0);
4813+
4814+
// trt_pad_index denotes the actual idx where the padding is performed by TRT.
4815+
std::vector<int> trt_pad_index{nb_dims - 2, nb_dims - 1};
4816+
4817+
// How many zeros are padded at the last two dimensions.
48064818
nvinfer1::DimsHW pre_padding(0, 0);
48074819
nvinfer1::DimsHW post_padding(0, 0);
48084820

4809-
std::vector<int32_t> permuted_pad_index(pad_index);
4810-
if (pad_index[0] == 1) {
4811-
legit_pad = false;
4812-
TF_RETURN_IF_ERROR(params->converter->TransposeTensor(
4813-
tensor, {0, 3, 2, 1}, &tensor, node_def, "to_pad"));
4814-
permuted_pad_index[0] = 3;
4815-
}
4816-
4817-
for (size_t i = 0; i < pad_index.size(); i++) {
4818-
int index = pad_index[i];
4819-
if (permuted_pad_index[i] == 2) {
4820-
pre_padding.h() = pad_data[index * 2];
4821-
post_padding.h() = pad_data[index * 2 + 1];
4822-
} else if (permuted_pad_index[i] == 3) {
4823-
pre_padding.w() = pad_data[index * 2];
4824-
post_padding.w() = pad_data[index * 2 + 1];
4821+
// Dimension to set in the pre_padding and post_padding array.
4822+
std::vector<int> trt_pre_post_padding_index{0, 1};
4823+
4824+
// Two special cases where we can avoid permutations.
4825+
if (tf_pad_index.size() == 1 && tf_pad_index[0] == nb_dims - 1) {
4826+
// Only one dimension needs to be padded. We store its index at
4827+
// trt_pad_index[0]. We ignore trt_pad_index[1].
4828+
trt_pad_index[0] = nb_dims - 1;
4829+
trt_pre_post_padding_index[0] = 1;
4830+
}
4831+
if (tf_pad_index.size() == 2 && tf_pad_index[1] == nb_dims - 2) {
4832+
// tf_pad_index only has two values that are in ascending order. If
4833+
// tf_pad_index[1] is nb_dims-2, then swapping the two values in
4834+
// trt_pad_index here makes it possible to only swap one pair of dimensions
4835+
// (swap tf_pad_index[0] with nb_dims-1) in the input tensor. Otherwise, we
4836+
// would have to swap two pairs of dimensions in the input tensor:
4837+
// (tf_pad_index[0] with nb_dims-2) and (tf_pad_index[1], with nb_dims-1).
4838+
// Here is an example for a 4D input tensor:
4839+
// tf_pad_index = [1, 2]
4840+
// trt_pad_index = [3, 2]
4841+
// transpose_idx = [0, 3, 2, 1]
4842+
std::swap(trt_pad_index[0], trt_pad_index[1]);
4843+
std::swap(trt_pre_post_padding_index[0], trt_pre_post_padding_index[1]);
4844+
}
4845+
4846+
for (int i = 0; i < tf_pad_index.size(); i++) {
4847+
const int tf_index = tf_pad_index[i];
4848+
const int trt_index = trt_pad_index[i];
4849+
const int k = trt_pre_post_padding_index[i];
4850+
pre_padding.d[k] = pad_data[tf_index * 2];
4851+
post_padding.d[k] = pad_data[tf_index * 2 + 1];
4852+
if (tf_index != trt_index) {
4853+
transposed_pad = true;
4854+
std::swap(transpose_idx[tf_index], transpose_idx[trt_index]);
48254855
}
48264856
}
48274857

4858+
if (transposed_pad) {
4859+
TF_RETURN_IF_ERROR(params->converter->TransposeTensor(
4860+
tensor, transpose_idx, &tensor, node_def, "to_pad"));
4861+
}
4862+
48284863
nvinfer1::IPaddingLayer* layer = params->converter->network()->addPadding(
48294864
*tensor, pre_padding, post_padding);
48304865
TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name());
48314866
SetLayerName(layer, node_def);
48324867
nvinfer1::ITensor* output_tensor = layer->getOutput(0);
48334868
params->converter->MarkQuantizationRangesAsInferrable(tensor, output_tensor);
48344869

4835-
if (!legit_pad) {
4870+
if (transposed_pad) {
48364871
TF_RETURN_IF_ERROR(params->converter->TransposeTensor(
4837-
output_tensor, {0, 3, 2, 1}, &output_tensor, node_def, "from_pad"));
4872+
output_tensor, transpose_idx, &output_tensor, node_def, "from_pad"));
48384873
}
48394874

48404875
params->outputs->push_back(TRT_TensorOrWeights(output_tensor));

‎tensorflow/compiler/tf2tensorrt/convert/convert_nodes_test.cc‎

Lines changed: 120 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7027,66 +7027,22 @@ NodeDef MakePadNodeDef(std::string name, DataType dtype) {
70277027
return pad.operation.node()->def();
70287028
}
70297029

7030-
template <typename CType>
70317030
struct PadTestParams {
70327031
std::vector<int> input_dims;
70337032
std::vector<int> pad_dims;
7034-
std::vector<CType> input_values;
7033+
std::vector<int> pad_values;
7034+
std::vector<float> input_values;
70357035
std::vector<int> expected_output_dims;
7036-
std::vector<CType> expected_output_values;
7036+
std::vector<float> expected_output_values;
7037+
Status status;
70377038
};
70387039

7039-
template <DataType dtype>
7040-
void TestConvertPad(OpConverterTest* test) {
7041-
typedef typename EnumToDataType<dtype>::Type CType;
7042-
7043-
std::vector<PadTestParams<CType>> params{
7044-
{
7045-
/*input_dims=*/{1, 2, 1}, // H, W, C
7046-
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7047-
/*input_values=*/CastTestVector<float, CType>({2.0f, -1.0f}),
7048-
/*expected_output_dims=*/{2, 3, 1}, // H, W, C
7049-
/*expected_output_values=*/
7050-
CastTestVector<float, CType>({0.0, 0.0, 0.0, 2.0f, -1.0f, 0.0}),
7051-
},
7052-
};
7053-
7054-
for (int i = 0; i < params.size(); ++i) {
7055-
test->Reset();
7056-
// Create pad node.
7057-
NodeDef node_def = MakePadNodeDef("my_pad", dtype);
7058-
// Create input tensor
7059-
nvinfer1::DataType trt_type;
7060-
TF_ASSERT_OK(TfTypeToTrtType(dtype, &trt_type));
7061-
test->AddTestTensor("input", params[i].input_dims, /*batch_size=*/1,
7062-
/*trt_dtype=*/trt_type);
7063-
// Create output size.
7064-
test->AddTestWeights<int32>("padding", params[i].pad_dims,
7065-
{0, 0, 1, 0, 0, 1, 0, 0});
7066-
test->RunValidationAndConversion(node_def);
7067-
7068-
TRT_TensorOrWeights output;
7069-
TF_EXPECT_OK(test->GetTensorOrWeights("padding", &output));
7070-
7071-
// Create input data for tensors.
7072-
const DataVec input_data{
7073-
{"input", test->AsTensor<CType>(params[i].input_values)}};
7074-
DataVec output_data{
7075-
{"my_pad", test->ConstructTensor<CType>(
7076-
params[i].expected_output_values.size())}};
7077-
7078-
TF_EXPECT_OK(test->BuildAndRun(input_data, &output_data));
7079-
ExpectArrayAlmostEqual(params[i].expected_output_values,
7080-
GetSpanForData<CType>(output_data[0]), CType(1e-5));
7081-
}
7082-
}
7083-
7084-
TEST_F(OpConverterTest, ConvertPad) {
7040+
TEST_P(OpConverter_FP32_FP16_Test, ConvertPad) {
70857041
{
70867042
// First input is weight, should fail.
70877043
Reset();
7088-
NodeDef node_def = MakePadNodeDef("my_pad", DT_FLOAT);
7089-
AddTestWeights<float>("input", {1, 2}, {1, 2});
7044+
NodeDef node_def = MakePadNodeDef("my_pad", tf_type_);
7045+
AddTestWeights("input", {1, 2}, {1, 2}, tf_type_);
70907046
AddTestWeights<int>("padding", {1, 2}, {1, 2});
70917047
RunValidationAndConversion(node_def, error::UNIMPLEMENTED,
70927048
"The input \"tensor\" for Pad must be a "
@@ -7095,20 +7051,18 @@ TEST_F(OpConverterTest, ConvertPad) {
70957051
{
70967052
// padding is a tensor, should fail.
70977053
Reset();
7098-
NodeDef node_def = MakePadNodeDef("my_pad", DT_FLOAT);
7054+
NodeDef node_def = MakePadNodeDef("my_pad", tf_type_);
70997055
AddTestTensor("input", {1, 2});
71007056
AddTestTensor("padding", {1, 2});
71017057
RunValidationAndConversion(node_def, error::UNIMPLEMENTED,
71027058
"The input \"paddings\" for Pad must be a "
71037059
"constant");
71047060
}
7105-
TestConvertPad<DT_FLOAT>(this);
7106-
TestConvertPad<DT_HALF>(this);
71077061
{
71087062
// Make sure that ranges are inferred across a Pad.
71097063
Reset();
7110-
NodeDef node_def = MakePadNodeDef("my_pad", DT_FLOAT);
7111-
AddTestTensor("input", {1, 2, 1});
7064+
NodeDef node_def = MakePadNodeDef("my_pad", tf_type_);
7065+
AddTestTensor("input", {1, 1, 2, 1});
71127066
AddTestWeights<int>("padding", {4, 2}, {0, 0, 1, 0, 0, 1, 0, 0});
71137067
TRT_TensorOrWeights input;
71147068
TRT_TensorOrWeights output;
@@ -7122,8 +7076,117 @@ TEST_F(OpConverterTest, ConvertPad) {
71227076
EXPECT_EQ(5.0f, ranges[input.tensor()]);
71237077
EXPECT_EQ(5.0f, ranges[output.tensor()]);
71247078
}
7125-
}
71267079

7080+
std::vector<PadTestParams> params{
7081+
// 1 padding dim
7082+
{
7083+
/*input_dims=*/{1, 1, 3, 2}, // N, H, W, C
7084+
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7085+
/*pad_values*/ {0, 0, 0, 0, 0, 1, 0, 0},
7086+
/*input_values=*/{1, 2, 3, 4, 5, 6},
7087+
/*expected_output_dims=*/{1, 1, 4, 2}, // N, H, W, C
7088+
/*expected_output_values=*/
7089+
{1, 2, 3, 4, 5, 6, 0, 0},
7090+
},
7091+
{
7092+
/*input_dims=*/{1, 1, 3, 2}, // N, H, W, C
7093+
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7094+
/*pad_values*/ {0, 0, 0, 0, 0, 0, 0, 1},
7095+
/*input_values=*/{1, 2, 3, 4, 5, 6},
7096+
/*expected_output_dims=*/{1, 1, 3, 3}, // N, H, W, C
7097+
/*expected_output_values=*/
7098+
{1, 2, 0, 3, 4, 0, 5, 6, 0},
7099+
},
7100+
{
7101+
/*input_dims=*/{1, 1, 3, 2}, // N, H, W, C
7102+
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7103+
/*pad_values*/ {0, 0, 1, 0, 0, 0, 0, 0},
7104+
/*input_values=*/{1, 2, 3, 4, 5, 6},
7105+
/*expected_output_dims=*/{1, 2, 3, 2}, // N, H, W, C
7106+
/*expected_output_values=*/
7107+
{0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6},
7108+
},
7109+
// 2 padding dims
7110+
{
7111+
/*input_dims=*/{1, 1, 2, 1}, // N, H, W, C
7112+
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7113+
/*pad_values*/ {0, 0, 1, 0, 0, 1, 0, 0},
7114+
/*input_values=*/{2.0f, -1.0f},
7115+
/*expected_output_dims=*/{1, 2, 3, 1}, // N, H, W, C
7116+
/*expected_output_values=*/
7117+
{0.0, 0.0, 0.0, 2.0f, -1.0f, 0.0},
7118+
},
7119+
PadTestParams{
7120+
/*input_dims=*/{1, 1, 2, 2}, // N, H, W, C
7121+
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7122+
/*pad_values*/ {0, 0, 1, 0, 0, 1, 0, 0},
7123+
/*input_values=*/{2, -1, 3., 4},
7124+
/*expected_output_dims=*/{1, 2, 3, 2}, // N, H, W, C
7125+
/*expected_output_values=*/
7126+
{0, 0, 0, 0, 0, 0, 2, -1, 3, 4, 0, 0},
7127+
},
7128+
PadTestParams{
7129+
/*input_dims=*/{1, 1, 2, 1, 2}, // N, C, H, W, D
7130+
/*pad_dims=*/{5, 2}, // #dims, {pad_before, pad_after}
7131+
/*pad_values*/ {0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
7132+
/*input_values=*/{2, -1, 3., 4},
7133+
/*expected_output_dims=*/{1, 2, 3, 1, 2}, // N, H, W, C
7134+
/*expected_output_values=*/
7135+
{0, 0, 0, 0, 0, 0, 2, -1, 3, 4, 0, 0},
7136+
},
7137+
PadTestParams{
7138+
/*input_dims=*/{1, 1, 2, 1, 2}, // N, C, H, W, D
7139+
/*pad_dims=*/{5, 2}, // #dims, {pad_before, pad_after}
7140+
/*pad_values*/ {0, 0, 0, 1, 0, 0, 1, 1, 0, 0},
7141+
/*input_values=*/{2, -1, 3., 4},
7142+
/*expected_output_dims=*/{1, 2, 2, 3, 2}, // N, H, W, C
7143+
/*expected_output_values=*/
7144+
{0., 0., 2., -1., 0., 0., 0., 0., 3., 4., 0., 0.,
7145+
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0},
7146+
},
7147+
PadTestParams{
7148+
/*input_dims=*/{1, 1, 2, 1}, // N, H, W, C
7149+
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7150+
/*pad_values*/ {1, 0, 0, 0, 0, 1, 0, 0},
7151+
/*input_values=*/{2.0f, -1.0f},
7152+
/*expected_output_dims=*/{2, 1, 3, 1}, // N, H, W, C
7153+
/*expected_output_values=*/{0.0, 0.0, 0.0, 2.0f, -1.0f, 0.0},
7154+
trt_mode_ == TrtTestMode::kImplicitBatch
7155+
? errors::InvalidArgument("Padding layer does not support "
7156+
"padding on batch dimension")
7157+
: Status::OK()},
7158+
PadTestParams{
7159+
/*input_dims=*/{1, 1, 2, 1}, // N, H, W, C
7160+
/*pad_dims=*/{4, 2}, // #dims, {pad_before, pad_after}
7161+
/*pad_values*/ {0, 0, 1, 0, 0, 1, 1, 1},
7162+
/*input_values=*/{2.0f, -1.0f},
7163+
/*expected_output_dims=*/{}, // N, H, W, C
7164+
/*expected_output_values=*/{},
7165+
errors::InvalidArgument("Padding layer does not support padding on "
7166+
"> 2")},
7167+
PadTestParams{
7168+
/*input_dims=*/{1, 2, 2}, // N, H, W
7169+
/*pad_dims=*/{3, 2}, // #dims, {pad_before, pad_after}
7170+
/*pad_values*/ {0, 0, 1, 0, 0, 1},
7171+
/*input_values=*/{2, -1, 3., 4},
7172+
/*expected_output_dims=*/{1, 3, 3}, // N, H, W, C
7173+
/*expected_output_values=*/
7174+
{0., 0., 0., 2., -1., 0., 3., 4., 0.},
7175+
errors::InvalidArgument("Convertpad requires at least 4D input, at "
7176+
"my_pad")}};
7177+
7178+
for (auto p : params) {
7179+
Reset();
7180+
// Create pad node.
7181+
NodeDef node_def = MakePadNodeDef("my_pad", tf_type_);
7182+
// Create input tensor.
7183+
AddTestTensor("input", p.input_dims, p.input_values);
7184+
// Create output size.
7185+
AddTestWeights<int32>("padding", p.pad_dims, p.pad_values);
7186+
TestOpConverter("my_pad", node_def, p.expected_output_dims, p.status,
7187+
p.status, ElementsAreArray(p.expected_output_values));
7188+
}
7189+
}
71277190
} // namespace convert
71287191
} // namespace tensorrt
71297192
} // namespace tensorflow

0 commit comments

Comments
 (0)