Add CLTuner support
authorInki Dae <inki.dae@samsung.com>
Thu, 21 Jan 2021 07:36:37 +0000 (16:36 +0900)
committerInki Dae <inki.dae@samsung.com>
Thu, 25 Mar 2021 02:11:40 +0000 (11:11 +0900)
Added CLTuner support.

For CLTuner support, this patch adds a new internal API, SetCLTuner function.
This function passes user-given CLTuner configuration to MLAPI and ARMNN
backends before inference engine loads a given model file.

[How to use]
There are two CLTuner modes:
    READ : inference engine refers to a given tuned file for inference.
    GENERATION : inference engine generates a tuning file to a given model file.

And there are three CLTuner types:
    EXHAUSTIVE : The tuning speed is slow but aggressive optimization.
    NORMAL : The tuning speed is reasonable and reasonable optimization.
    RAPID : The tuning speed is fast but leient optimization.

- For CLTuner read mode,
    inference_engine_cltuner cltuner = {
        .active = true,
        .update = false,
        .cltuner.type = INFERENCE_ENGINE_CLTUNER_READ,
    };

- For CLTuner generation mode,
    inference_engine_cltuner cltuner = {
        .active = true,
        .update = true,
        .cltuner.type = INFERENCE_ENGINE_CLTUNER_{EXHAUSTIVE |
                                                  NORMAL |
                                                  RAPID},
    };

    inference_engine_capacity capacity;
    engine->GetBackendCapacity(&capacity);

    if (capacity.cltuner_supported)
        engine->SetCLTuner(&cltuner);

Change-Id: Id1cc9513e444dfad21b933b46535c1b810f4a4d6
Signed-off-by: Inki Dae <inki.dae@samsung.com>
include/inference_engine_common.h
include/inference_engine_common_impl.h
include/inference_engine_type.h
src/inference_engine_common_impl.cpp
test/src/inference_engine_profiler.cpp

index 3321509..d7db0d5 100644 (file)
@@ -42,6 +42,15 @@ namespace Common
                virtual int SetPrivateData(void *data) = 0;
 
                /**
+                * @brief Set CLTuner configuration.
+                * @details This callback passes a given CLTuner configuration value to inference engine if the inference engine supports for CLTuner feature.
+                *
+                * @since_tizen 6.5
+                * @param[in] cltuner This contains CLTuner configuration value. See inference_engine_cltuner structure and inference_engine_cltuner_mode_e enumeration.
+                */
+               virtual int SetCLTuner(const inference_engine_cltuner *cltuner) = 0;
+
+               /**
                 * @brief Set target devices.
                 * @details See #inference_target_type_e
                 *          This callback passes given device types - CPU, GPU, CUSTOM or combinated one if a backend engine supports hybrid inferencea - to a backend engine.
index 63cfe7b..5966557 100644 (file)
@@ -84,6 +84,15 @@ namespace Common
                int SetTargetDevices(int types);
 
                /**
+                * @brief Set CLTuner configuration.
+                * @details This callback passes a given CLTuner configuration value to inference engine if the inference engine supports for CLTuner feature.
+                *
+                * @since_tizen 6.5
+                * @param[in] cltuner This contains CLTuner configuration value. See inference_engine_cltuner structure and inference_engine_cltuner_mode_e enumeration.
+                */
+               int SetCLTuner(const inference_engine_cltuner *cltuner);
+
+               /**
                 * @brief Request to load model data with user-given model file information.
                 * @details This callback requests a backend engine to load given model files for inference.
                 *          The backend engine should load the given model files according to a given model file format.
index 0e42792..3e8a46f 100644 (file)
@@ -107,6 +107,20 @@ extern "C"
        } inference_tensor_data_type_e;
 
        /**
+        * @brief Enumeration for OPenCL Tuner type.
+        *
+        * @since_tizen 6.5
+        */
+       typedef enum {
+               INFERENCE_ENGINE_CLTUNER_MIN = -1,
+               INFERENCE_ENGINE_CLTUNER_READ, /** < CLTuner mode is read only. */
+               INFERENCE_ENGINE_CLTUNER_EXHAUSTIVE, /** < The tuning speed is slow but aggressive optimization. */
+               INFERENCE_ENGINE_CLTUNER_NORMAL, /** < The tuning speed is reasonable and reasonable optimization. */
+               INFERENCE_ENGINE_CLTUNER_RAPID, /** < The tuning speed is fast but lenient optimization. */
+               INFERENCE_ENGINE_CLTUNER_MAX
+       } inference_engine_cltuner_mode_e;
+
+       /**
         * @brief Tensor defined by the dimension and their corresponding data
         * @details @a dimInfo is the information
         *          of a tensor, which is multi-dimension matix. @a data is the data pointer
@@ -129,6 +143,13 @@ extern "C"
                std::vector<void *> data;
        } tensor_t;
 
+       typedef struct _inference_engine_cltuner {
+               bool active; /** < Indicate that CLTuner feature of a given inference engine is avaliable. */
+               bool update; /** < Find optimal LWS paramaters if update is true, otherwise it uses a pre-tuned file for inference if the pre-tuned file exists. This type is valid only in case that active is true. */
+               inference_engine_cltuner_mode_e tuning_mode; /** < Tuning mode of CLTUner. This is valid only in case that active is true. */
+               std::string file_path; /** < a full path of turned file. This path is valid only in case that active is true. */
+       } inference_engine_cltuner;
+
        /**
         * @brief Inference engine backend configuration
         *
@@ -204,6 +225,7 @@ extern "C"
         *                      - device list which is able to compute operations.
         *                      - tensor shape information a given backend engine supports for.
         *                      - neural network models a given backend engine supports for.
+        *                      - Indicate that a given backend supports CLTuner feature. @since_tizen 6.5
         *
         * @since_tizen 6.0
         */
@@ -211,6 +233,7 @@ extern "C"
                int supported_accel_devices;
                inference_tensor_shape_type_e supported_tensor_shape_type;
                std::vector<std::string> supported_nn_models;
+               bool cltuner_supported;
                // TODO.
        } inference_engine_capacity;
 
index 4ef465f..4bc8222 100644 (file)
@@ -467,6 +467,23 @@ namespace Common
                return ret;
        }
 
+       int InferenceEngineCommon::SetCLTuner(const inference_engine_cltuner *cltuner)
+       {
+               CHECK_ENGINE_INSTANCE(mBackendHandle);
+
+               if (cltuner->active && (cltuner->tuning_mode <= INFERENCE_ENGINE_CLTUNER_MIN ||
+                                                               cltuner->tuning_mode >= INFERENCE_ENGINE_CLTUNER_MAX)) {
+                       LOGE("Invalid tuning mode of CLTuner.(%d)", cltuner->tuning_mode);
+                       return INFERENCE_ENGINE_ERROR_INVALID_PARAMETER;
+               }
+
+               int ret = mBackendHandle->SetCLTuner(cltuner);
+               if (ret != INFERENCE_ENGINE_ERROR_NONE)
+                       LOGE("Fail to SetCLTuner");
+
+               return ret;
+       }
+
        int InferenceEngineCommon::Load(std::vector<std::string> model_paths,
                                                                        inference_model_format_e model_format)
        {
index 81581f0..c97dfa1 100644 (file)
@@ -161,6 +161,16 @@ TEST_P(InferenceEngineTfliteTest, Inference)
        ret = engine->GetBackendCapacity(&capacity);
        EXPECT_EQ(ret, INFERENCE_ENGINE_ERROR_NONE);
 
+       if (capacity.cltuner_supported) {
+               inference_engine_cltuner cltuner;
+                       cltuner.active = true;
+                       cltuner.update = false;
+                       cltuner.tuning_mode = static_cast<inference_engine_cltuner_mode_e>(INFERENCE_ENGINE_CLTUNER_READ);
+
+               ret = engine->SetCLTuner(&cltuner);
+               EXPECT_EQ(ret, INFERENCE_ENGINE_ERROR_NONE);
+       }
+
        ret = engine->SetTargetDevices(target_devices);
        EXPECT_EQ(ret, INFERENCE_ENGINE_ERROR_NONE);