|
| 1 | +/* Copyright 2026 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "xprof/convert/op_metrics_to_record.h" |
| 17 | + |
| 18 | +#include "<gtest/gtest.h>" |
| 19 | +#include "plugin/xprof/protobuf/op_metrics.pb.h" |
| 20 | + |
| 21 | +namespace tensorflow { |
| 22 | +namespace profiler { |
| 23 | +namespace { |
| 24 | + |
| 25 | +constexpr double kMaxError = 1E-10; |
| 26 | + |
| 27 | +TEST(OpMetricsToRecordTest, GigaFlopsPerSecondPerCoreNormalizedOnDvfs) { |
| 28 | + OpMetrics metrics; |
| 29 | + metrics.set_time_ps(100); |
| 30 | + metrics.set_normalized_time_ps(200); |
| 31 | + metrics.set_flops_v2(1000); |
| 32 | + metrics.set_occurrences(1); |
| 33 | + metrics.set_num_cores(1); |
| 34 | + |
| 35 | + // GigaFlopsPerSecondPerCore = (flops_v2 / (time_ps / 1000.0)) = 1000 / 0.1 = |
| 36 | + // 10000. Multiplier = time_ps / normalized_time_ps = 100 / 200 = 0.5. |
| 37 | + // Expected normalized GFLOPS = 10000 * 0.5 = 5000. |
| 38 | + EXPECT_NEAR(5000.0, GigaFlopsPerSecondPerCoreNormalizedOnDvfs(metrics), |
| 39 | + kMaxError); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(OpMetricsToRecordTest, GigaFlopsPerSecondPerCoreNormalizedOnDvfsFallback) { |
| 43 | + OpMetrics metrics; |
| 44 | + metrics.set_time_ps(100); |
| 45 | + metrics.set_normalized_time_ps(0); |
| 46 | + metrics.set_flops_v2(1000); |
| 47 | + metrics.set_occurrences(1); |
| 48 | + metrics.set_num_cores(1); |
| 49 | + |
| 50 | + EXPECT_NEAR(10000.0, GigaFlopsPerSecondPerCoreNormalizedOnDvfs(metrics), |
| 51 | + kMaxError); |
| 52 | +} |
| 53 | + |
| 54 | +} // namespace |
| 55 | +} // namespace profiler |
| 56 | +} // namespace tensorflow |
0 commit comments