| From b41a43386c39f21e09656d0cbbc1a271874feaf2 Mon Sep 17 00:00:00 2001 |
| From: "A. Unique TensorFlower" <gardener@tensorflow.org> |
| Date: Wed, 23 Oct 2024 21:44:50 -0700 |
| Subject: [PATCH] Construct RE2 in ConfigurationEntry constructor rather than |
| on every Matches() |
| |
| Originally, ConfigurationEntry::Matches() constructs a RE2 of test_id_rex_, |
| which takes about 0.15ms. Since the Matches() is called number of regex |
| multiplied by number of tests times, this takes extra ~2 min in total to run all |
| the tests in stable_delegate_test_suite with a 300 lines acceleration config. |
| |
| We could construct the RE2 in advance on construction of ConfigurationEntry. |
| This could save us lots of time when using acceleration config. |
| |
| PiperOrigin-RevId: 689229679 |
| Signed-off-by: TFLite Patcher <tflite@localhost> |
| |
| PATCH_NAME=accel-config-optimization |
| --- |
| .../kernels/acceleration_test_util_internal.h | 21 +++++++++++-------- |
| 1 file changed, 12 insertions(+), 9 deletions(-) |
| |
| diff --git a/tensorflow/lite/kernels/acceleration_test_util_internal.h b/tensorflow/lite/kernels/acceleration_test_util_internal.h |
| index 3b5a5166..afb928dc 100644 |
| --- a/tensorflow/lite/kernels/acceleration_test_util_internal.h |
| +++ b/tensorflow/lite/kernels/acceleration_test_util_internal.h |
| @@ -19,6 +19,7 @@ limitations under the License. |
| #include <atomic> |
| #include <functional> |
| #include <iterator> |
| +#include <memory> |
| #include <optional> |
| #include <string> |
| #include <vector> |
| @@ -41,20 +42,20 @@ class ConfigurationEntry { |
| public: |
| ConfigurationEntry(const std::string& test_id_rex, T test_config, |
| bool is_denylist) |
| - : test_id_rex_(test_id_rex), |
| + : test_id_rex_(new RE2(test_id_rex)), |
| test_config_(test_config), |
| is_denylist_(is_denylist) {} |
| |
| - bool Matches(const std::string& test_id) { |
| - return RE2::FullMatch(test_id, test_id_rex_); |
| + bool Matches(const std::string& test_id) const { |
| + return RE2::FullMatch(test_id, *test_id_rex_); |
| } |
| bool IsDenylistEntry() const { return is_denylist_; } |
| const T& TestConfig() const { return test_config_; } |
| |
| - const std::string& TestIdRex() const { return test_id_rex_; } |
| + const std::string& TestIdRex() const { return test_id_rex_->pattern(); } |
| |
| private: |
| - std::string test_id_rex_; |
| + std::unique_ptr<RE2> test_id_rex_; |
| T test_config_; |
| bool is_denylist_; |
| }; |
| @@ -74,7 +75,7 @@ std::optional<T> GetAccelerationTestParam(std::string test_id) { |
| auto consumer = [&config](std::string key, std::string value_str, |
| bool is_denylist) mutable { |
| T value = T::ParseConfigurationLine(value_str); |
| - config->push_back(ConfigurationEntry<T>(key, value, is_denylist)); |
| + config->emplace_back(key, value, is_denylist); |
| }; |
| |
| ReadAccelerationConfig(T::AccelerationTestConfig(), consumer); |
| @@ -88,9 +89,11 @@ std::optional<T> GetAccelerationTestParam(std::string test_id) { |
| const std::vector<ConfigurationEntry<T>>* test_config = |
| test_config_ptr.load(); |
| |
| - const auto test_config_iter = std::find_if( |
| - test_config->begin(), test_config->end(), |
| - [&test_id](ConfigurationEntry<T> elem) { return elem.Matches(test_id); }); |
| + const auto test_config_iter = |
| + std::find_if(test_config->begin(), test_config->end(), |
| + [&test_id](const ConfigurationEntry<T>& elem) { |
| + return elem.Matches(test_id); |
| + }); |
| if (test_config_iter != test_config->end() && |
| !test_config_iter->IsDenylistEntry()) { |
| return std::optional<T>(test_config_iter->TestConfig()); |