Merge pull request #26631 from trevor-m:tmorris_tftrt_dont_settype_trt_5_1_3

PiperOrigin-RevId: 238142352
This commit is contained in:
TensorFlower Gardener 2019-03-12 18:42:55 -07:00
commit c2ebe3986d
3 changed files with 19 additions and 13 deletions

View File

@ -367,11 +367,13 @@ nvinfer1::ITensor* Converter::CreateConstantLayer(
if (!layer) return nullptr; if (!layer) return nullptr;
const nvinfer1::DataType trt_dtype = trt_weights.type; const nvinfer1::DataType trt_dtype = trt_weights.type;
nvinfer1::ITensor* trt_tensor = layer->getOutput(0); nvinfer1::ITensor* trt_tensor = layer->getOutput(0);
#if !IS_TRT_VERSION_GE(5, 1, 3)
// TODO(laigd): there is a bug in TensorRT 5.0 library that, if we don't set // TODO(laigd): there is a bug in TensorRT 5.0 library that, if we don't set
// the data type below, it will always be kFLOAT regardless what the data type // the data type below, it will always be kFLOAT regardless what the data type
// of the weights is. Once NVIDIA fixes this bug, we should remove the data // of the weights is. Once NVIDIA fixes this bug, we should remove the data
// type setting logic below and test should still pass. // type setting logic below and test should still pass.
trt_tensor->setType(trt_dtype); trt_tensor->setType(trt_dtype);
#endif
return trt_tensor; return trt_tensor;
} }
@ -574,13 +576,13 @@ class TRT_TensorOrWeights::SimpleITensor : public nvinfer1::ITensor {
void setLocation(nvinfer1::TensorLocation location) override {} void setLocation(nvinfer1::TensorLocation location) override {}
#if NV_TENSORRT_MAJOR >= 5 #if IS_TRT_VERSION_GE(5, 0, 0)
bool setDynamicRange(float min, float max) override { return true; } bool setDynamicRange(float min, float max) override { return true; }
float getDynamicRange() const override { return 0; } float getDynamicRange() const override { return 0; }
#endif #endif
#if NV_TENSORRT_MAJOR > 5 || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) #if IS_TRT_VERSION_GE(5, 1, 0)
bool dynamicRangeIsSet() const override { return true; } bool dynamicRangeIsSet() const override { return true; }
void resetDynamicRange() override {} void resetDynamicRange() override {}
@ -1281,7 +1283,7 @@ void Converter::MaybeApplyQuantizationRanges() {
// Infer ranges across marked ops. // Infer ranges across marked ops.
PropagateQuantizationRanges(); PropagateQuantizationRanges();
// Apply ranges. // Apply ranges.
#if NV_TENSORRT_MAJOR >= 5 #if IS_TRT_VERSION_GE(5, 0, 0)
for (auto pair : quantization_ranges_) { for (auto pair : quantization_ranges_) {
nvinfer1::ITensor* tensor = pair.first; nvinfer1::ITensor* tensor = pair.first;
const float range = pair.second; const float range = pair.second;
@ -2297,9 +2299,7 @@ Status ConvertStridedSliceHelper(OpConverterParams* params,
} }
// TRT 5.1 adds a slice layer. For older versions, we attempt to use the // TRT 5.1 adds a slice layer. For older versions, we attempt to use the
// padding layer with negative padding. // padding layer with negative padding.
#if (NV_TENSORRT_MAJOR > 5 || \ #if IS_TRT_VERSION_GE(5, 1, 0) && 0
(NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1)) && \
0
// TODO(laigd): TRT 5.1 RC has a bug when ISliceLayer is used along with // TODO(laigd): TRT 5.1 RC has a bug when ISliceLayer is used along with
// IConcatenationLayer, so disable ISliceLayer for now until it's fixed. // IConcatenationLayer, so disable ISliceLayer for now until it's fixed.
// Use ISliceLayer. // Use ISliceLayer.
@ -3220,7 +3220,7 @@ UnaryOperationMap() {
{"Sqrt", nvinfer1::UnaryOperation::kSQRT}, {"Sqrt", nvinfer1::UnaryOperation::kSQRT},
{"Abs", nvinfer1::UnaryOperation::kABS}, {"Abs", nvinfer1::UnaryOperation::kABS},
{"Reciprocal", nvinfer1::UnaryOperation::kRECIP}, {"Reciprocal", nvinfer1::UnaryOperation::kRECIP},
#if NV_TENSORRT_MAJOR > 5 || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) #if IS_TRT_VERSION_GE(5, 1, 0)
{"Sin", nvinfer1::UnaryOperation::kSIN}, {"Sin", nvinfer1::UnaryOperation::kSIN},
{"Cos", nvinfer1::UnaryOperation::kCOS}, {"Cos", nvinfer1::UnaryOperation::kCOS},
{"Tan", nvinfer1::UnaryOperation::kTAN}, {"Tan", nvinfer1::UnaryOperation::kTAN},

View File

@ -43,6 +43,12 @@ extern const char* const kOutputPHName;
namespace convert { namespace convert {
#define IS_TRT_VERSION_GE(major, minor, patch) \
((NV_TENSORRT_MAJOR > major) || \
(NV_TENSORRT_MAJOR == major && NV_TENSORRT_MINOR > minor) || \
(NV_TENSORRT_MAJOR == major && NV_TENSORRT_MINOR == minor && \
NV_TENSORRT_PATCH >= patch))
struct EngineConnection { struct EngineConnection {
// Constructs a non-control edge. // Constructs a non-control edge.
EngineConnection(const string& outside, int out_id, int out_port, EngineConnection(const string& outside, int out_id, int out_port,

View File

@ -233,7 +233,7 @@ class FakeITensor : public nvinfer1::ITensor {
location_ = location; location_ = location;
} }
#if NV_TENSORRT_MAJOR >= 5 #if IS_TRT_VERSION_GE(5, 0, 0)
bool setDynamicRange(float min, float max) override { bool setDynamicRange(float min, float max) override {
dynamic_range_ = std::max(std::abs(min), std::abs(max)); dynamic_range_ = std::max(std::abs(min), std::abs(max));
return true; return true;
@ -242,7 +242,7 @@ class FakeITensor : public nvinfer1::ITensor {
float getDynamicRange() const override { return dynamic_range_; } float getDynamicRange() const override { return dynamic_range_; }
#endif #endif
#if NV_TENSORRT_MAJOR > 5 || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) #if IS_TRT_VERSION_GE(5, 1, 0)
bool dynamicRangeIsSet() const override { return true; } bool dynamicRangeIsSet() const override { return true; }
void resetDynamicRange() override {} void resetDynamicRange() override {}
@ -845,7 +845,7 @@ TEST_F(ConverterTest, MaybeApplyQuantizationRanges) {
// Input range should be inferred along the chain and applied to tensors. // Input range should be inferred along the chain and applied to tensors.
int8_converter.MaybeApplyQuantizationRanges(); int8_converter.MaybeApplyQuantizationRanges();
#if NV_TENSORRT_MAJOR >= 5 #if IS_TRT_VERSION_GE(5, 0, 0)
EXPECT_EQ(input.getDynamicRange(), 5.0f); EXPECT_EQ(input.getDynamicRange(), 5.0f);
EXPECT_EQ(infer_1.getDynamicRange(), 5.0f); EXPECT_EQ(infer_1.getDynamicRange(), 5.0f);
EXPECT_EQ(infer_2.getDynamicRange(), 5.0f); EXPECT_EQ(infer_2.getDynamicRange(), 5.0f);
@ -2672,7 +2672,7 @@ TEST_F(OpConverterTest, ConvertStridedSlice) {
RunValidationAndConversion(node_def); RunValidationAndConversion(node_def);
} }
// TRT 5.1+ supports strides // TRT 5.1+ supports strides
#if NV_TENSORRT_MAJOR > 5 || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) #if IS_TRT_VERSION_GE(5, 1, 0)
{ {
// Negative strides, should fail. // Negative strides, should fail.
Reset(); Reset();
@ -2735,7 +2735,7 @@ TEST_F(OpConverterTest, ConvertStridedSlice) {
// Same input is used for all tests. // Same input is used for all tests.
const std::vector<float> ok_input = {1, 2, 3, 4, 5, 6}; const std::vector<float> ok_input = {1, 2, 3, 4, 5, 6};
#if NV_TENSORRT_MAJOR > 5 || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) #if IS_TRT_VERSION_GE(5, 1, 0)
const int kStridedSliceOKCases = 23; const int kStridedSliceOKCases = 23;
#else #else
const int kStridedSliceOKCases = 19; const int kStridedSliceOKCases = 19;
@ -2862,7 +2862,7 @@ TEST_F(OpConverterTest, ConvertStridedSlice) {
/*end_mask=*/get_mask({1, 0, 0, 0}), /*end_mask=*/get_mask({1, 0, 0, 0}),
/*expected_output_dims=*/{1, 2, 3}, /*expected_output_dims=*/{1, 2, 3},
/*expected_output=*/{1, 2, 3, 4, 5, 6}}, /*expected_output=*/{1, 2, 3, 4, 5, 6}},
#if NV_TENSORRT_MAJOR > 5 || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) #if IS_TRT_VERSION_GE(5, 1, 0)
// Strides // Strides
TestParams{/*input_dims=*/{6}, TestParams{/*input_dims=*/{6},
/*begin=*/{0, 0}, /*end=*/{0, 5}, /*strides=*/{1, 2}, /*begin=*/{0, 0}, /*end=*/{0, 5}, /*strides=*/{1, 2},