| 118 | }; |
| 119 | |
| 120 | TEST_F(TRTEngineOpTestBase, DynamicShapes) { |
| 121 | TRTEngineOpTestBase::AddSimpleTrtOp(DT_FLOAT, /*max_cached_engines_count=*/4); |
| 122 | |
| 123 | // Execute the op with batch size > 1. |
| 124 | TRTEngineOpTestBase::AddSimpleInput<float>(TensorShape({2, 2})); |
| 125 | TF_ASSERT_OK(OpsTestBase::RunOpKernel()); |
| 126 | |
| 127 | // Get the engine cache. |
| 128 | TRTEngineCacheResource* cache_resource = nullptr; |
| 129 | TF_ASSERT_OK( |
| 130 | device_->resource_manager()->Lookup("TF-TRT", "myop", &cache_resource)); |
| 131 | core::ScopedUnref sc(cache_resource); |
| 132 | |
| 133 | // It should contain only one engine. |
| 134 | auto cache = &cache_resource->cache_; |
| 135 | EXPECT_EQ(1, cache->size()); |
| 136 | EXPECT_EQ(1, cache->count({TensorShape({2, 2})})); |
| 137 | |
| 138 | // Execute the op with batch size 1. It should reuse existing engine to |
| 139 | // execute. |
| 140 | ResetInputs(); |
| 141 | TRTEngineOpTestBase::AddSimpleInput<float>(TensorShape({1, 2})); |
| 142 | TF_ASSERT_OK(OpsTestBase::RunOpKernel()); |
| 143 | EXPECT_EQ(1, cache->size()); |
| 144 | EXPECT_EQ(1, cache->count({TensorShape({2, 2})})); |
| 145 | |
| 146 | // Execute the op with a larger batch size. |
| 147 | ResetInputs(); |
| 148 | TRTEngineOpTestBase::AddSimpleInput<float>(TensorShape({3, 2})); |
| 149 | TF_ASSERT_OK(OpsTestBase::RunOpKernel()); |
| 150 | EXPECT_EQ(2, cache->size()); |
| 151 | EXPECT_EQ(1, cache->count({TensorShape({2, 2})})); |
| 152 | EXPECT_EQ(1, cache->count({TensorShape({3, 2})})); |
| 153 | |
| 154 | // Execute the op with an input that has different non-batch dimension. |
| 155 | ResetInputs(); |
| 156 | TRTEngineOpTestBase::AddSimpleInput<float>(TensorShape({10, 10})); |
| 157 | TF_ASSERT_OK(OpsTestBase::RunOpKernel()); |
| 158 | // Execute it again with an input that has the same non-batch dimension but |
| 159 | // smallest batch size. It should find the correct engine to use. |
| 160 | ResetInputs(); |
| 161 | TRTEngineOpTestBase::AddSimpleInput<float>(TensorShape({1, 10})); |
| 162 | TF_ASSERT_OK(OpsTestBase::RunOpKernel()); |
| 163 | EXPECT_EQ(3, cache->size()); // Should only create 3 engines in total. |
| 164 | EXPECT_EQ(1, cache->count({TensorShape({2, 2})})); |
| 165 | EXPECT_EQ(1, cache->count({TensorShape({3, 2})})); |
| 166 | EXPECT_EQ(1, cache->count({TensorShape({10, 10})})); |
| 167 | } |
| 168 | |
| 169 | template <typename T> |
| 170 | class TRTEngineOpTest : public TRTEngineOpTestBase {}; |
nothing calls this directly
no test coverage detected