switch_autotune.h 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <cmath>
#include "paddle/phi/kernels/autotune/cache.h"

namespace phi {
namespace autotune {

class AutoTuneStatus {
 public:
  static AutoTuneStatus& Instance() {
    static AutoTuneStatus switch_autotune;
    return switch_autotune;
  }

  bool UseAutoTune() { return use_autotune_; }

32 33 34
  // EnableAutoTune and DisableAutoTune should be used for debug only.
  void EnableAutoTune();
  void DisableAutoTune();
35

36
  void Update();
37 38 39 40 41 42 43 44 45 46 47 48 49 50

  int64_t StepID() { return current_steps_id_; }

  float RecentHitRate() {
    int recent_step_nums = std::ceil(step_hit_rates_.size() * 0.3);
    float sum = std::accumulate(step_hit_rates_.rbegin(),
                                step_hit_rates_.rbegin() + recent_step_nums,
                                0.0);
    float mean = sum / recent_step_nums;
    return mean;
  }

  // Hit Rate of Current Step
  float StepHitRate() {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    static int64_t last_step_id = -2;

    if (last_step_id != current_steps_id_) {
      int64_t current_hits = AutoTuneCache::Instance().CacheHits();
      int64_t current_misses = AutoTuneCache::Instance().CacheMisses();
      int64_t step_hits_ = current_hits - previous_hits_;
      int64_t step_misses_ = current_misses - previous_misses_;
      float step_hit_rate = 0.;
      int64_t step_num_accesses = step_hits_ + step_misses_;
      if (step_num_accesses != 0) {
        step_hit_rate = static_cast<float>(step_hits_) /
                        static_cast<float>(step_num_accesses);
      }
      previous_hits_ = current_hits;
      previous_misses_ = current_misses;
      current_step_hit_rate_ = step_hit_rate;
      last_step_id = current_steps_id_;
68
    }
69
    return current_step_hit_rate_;
70 71 72 73 74 75 76 77 78 79 80
  }

  void SetAutoTuneRange(int64_t start, int64_t stop) {
    start_step_id_ = start;
    stop_step_id_ = stop;
  }

 private:
  AutoTuneStatus() = default;

  void Init() {
81
    use_autotune_ = false;
82 83 84 85
    current_steps_id_ = -1;
    previous_hits_ = 0;
    previous_misses_ = 0;
    step_hit_rates_.clear();
86
    AutoTuneCache::Instance().Clean();
87 88
  }

89 90 91 92 93 94 95
  bool use_autotune_{false};
  int64_t start_step_id_{1};
  int64_t stop_step_id_{10};
  int64_t current_steps_id_{-1};
  int64_t previous_hits_{0};
  int64_t previous_misses_{0};
  float current_step_hit_rate_{0.f};
96 97 98 99 100
  std::vector<float> step_hit_rates_;
};

}  // namespace autotune
}  // namespace phi