• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "pipeline/jit/pi/graph_guard/perf.h"
17 
18 namespace mindspore {
19 namespace pijit {
20 
21 class PerfStatisticsImpl : public PerfStatistics {
22  public:
23   PerfStatisticsImpl() = default;
24   virtual ~PerfStatisticsImpl() = default;
GetAverageDuration()25   virtual double GetAverageDuration() { return avg_duration_; }
GetMaxDuration()26   virtual double GetMaxDuration() { return max_duration_; }
GetMinDuration()27   virtual double GetMinDuration() { return min_duration_; }
GetTotalCount()28   virtual int GetTotalCount() { return total_count_; }
29 
30  protected:
31   friend class OptPerf;
32   double avg_duration_ = 0.f;
33   double max_duration_ = std::numeric_limits<double>::min();
34   double min_duration_ = std::numeric_limits<double>::max();
35   int total_count_ = 0;
36 };
37 
OptPerf()38 OptPerf::OptPerf() : stat_(std::make_shared<PerfStatisticsImpl>()) {}
39 
AddDuration(double duration)40 void OptPerf::AddDuration(double duration) {
41   PerfStatisticsImpl *stat = static_cast<PerfStatisticsImpl *>(stat_.get());
42   if (stat->max_duration_ < duration) {
43     stat->max_duration_ = duration;
44   }
45   if (stat->min_duration_ > duration) {
46     stat->min_duration_ = duration;
47   }
48   stat->avg_duration_ =
49     (stat->avg_duration_ * stat->total_count_ + duration) / static_cast<double>(stat->total_count_ + 1);
50   stat->total_count_ += 1;
51 }
52 
GetStatistics()53 PerfStatisticsPtr OptPerf::GetStatistics() { return stat_; }
54 
55 }  // namespace pijit
56 }  // namespace mindspore
57