xref: /aosp_15_r20/external/pytorch/test/cpp/profiler/containers.cpp (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker #include <algorithm>
2*da0073e9SAndroid Build Coastguard Worker #include <cmath>
3*da0073e9SAndroid Build Coastguard Worker #include <utility>
4*da0073e9SAndroid Build Coastguard Worker #include <vector>
5*da0073e9SAndroid Build Coastguard Worker 
6*da0073e9SAndroid Build Coastguard Worker #include <gtest/gtest.h>
7*da0073e9SAndroid Build Coastguard Worker 
8*da0073e9SAndroid Build Coastguard Worker #include <c10/util/ApproximateClock.h>
9*da0073e9SAndroid Build Coastguard Worker #include <c10/util/irange.h>
10*da0073e9SAndroid Build Coastguard Worker #include <torch/csrc/profiler/containers.h>
11*da0073e9SAndroid Build Coastguard Worker #include <torch/csrc/profiler/util.h>
12*da0073e9SAndroid Build Coastguard Worker 
TEST(ProfilerTest,AppendOnlyList)13*da0073e9SAndroid Build Coastguard Worker TEST(ProfilerTest, AppendOnlyList) {
14*da0073e9SAndroid Build Coastguard Worker   const int n = 4096;
15*da0073e9SAndroid Build Coastguard Worker   torch::profiler::impl::AppendOnlyList<int, 1024> list;
16*da0073e9SAndroid Build Coastguard Worker   for (const auto i : c10::irange(n)) {
17*da0073e9SAndroid Build Coastguard Worker     list.emplace_back(i);
18*da0073e9SAndroid Build Coastguard Worker     ASSERT_EQ(list.size(), i + 1);
19*da0073e9SAndroid Build Coastguard Worker   }
20*da0073e9SAndroid Build Coastguard Worker 
21*da0073e9SAndroid Build Coastguard Worker   int expected = 0;
22*da0073e9SAndroid Build Coastguard Worker   for (const auto i : list) {
23*da0073e9SAndroid Build Coastguard Worker     ASSERT_EQ(i, expected++);
24*da0073e9SAndroid Build Coastguard Worker   }
25*da0073e9SAndroid Build Coastguard Worker   ASSERT_EQ(expected, n);
26*da0073e9SAndroid Build Coastguard Worker 
27*da0073e9SAndroid Build Coastguard Worker   list.clear();
28*da0073e9SAndroid Build Coastguard Worker   ASSERT_EQ(list.size(), 0);
29*da0073e9SAndroid Build Coastguard Worker }
30*da0073e9SAndroid Build Coastguard Worker 
TEST(ProfilerTest,AppendOnlyList_ref)31*da0073e9SAndroid Build Coastguard Worker TEST(ProfilerTest, AppendOnlyList_ref) {
32*da0073e9SAndroid Build Coastguard Worker   const int n = 512;
33*da0073e9SAndroid Build Coastguard Worker   torch::profiler::impl::AppendOnlyList<std::pair<int, int>, 64> list;
34*da0073e9SAndroid Build Coastguard Worker   std::vector<std::pair<int, int>*> refs;
35*da0073e9SAndroid Build Coastguard Worker   for (const auto _ : c10::irange(n)) {
36*da0073e9SAndroid Build Coastguard Worker     refs.push_back(list.emplace_back());
37*da0073e9SAndroid Build Coastguard Worker   }
38*da0073e9SAndroid Build Coastguard Worker 
39*da0073e9SAndroid Build Coastguard Worker   for (const auto i : c10::irange(n)) {
40*da0073e9SAndroid Build Coastguard Worker     *refs.at(i) = {i, 0};
41*da0073e9SAndroid Build Coastguard Worker   }
42*da0073e9SAndroid Build Coastguard Worker 
43*da0073e9SAndroid Build Coastguard Worker   int expected = 0;
44*da0073e9SAndroid Build Coastguard Worker   for (const auto& i : list) {
45*da0073e9SAndroid Build Coastguard Worker     ASSERT_EQ(i.first, expected++);
46*da0073e9SAndroid Build Coastguard Worker   }
47*da0073e9SAndroid Build Coastguard Worker }
48*da0073e9SAndroid Build Coastguard Worker 
49*da0073e9SAndroid Build Coastguard Worker // Test that we can convert TSC measurements back to wall clock time.
TEST(ProfilerTest,clock_converter)50*da0073e9SAndroid Build Coastguard Worker TEST(ProfilerTest, clock_converter) {
51*da0073e9SAndroid Build Coastguard Worker   const int n = 10001;
52*da0073e9SAndroid Build Coastguard Worker   c10::ApproximateClockToUnixTimeConverter converter;
53*da0073e9SAndroid Build Coastguard Worker   std::vector<
54*da0073e9SAndroid Build Coastguard Worker       c10::ApproximateClockToUnixTimeConverter::UnixAndApproximateTimePair>
55*da0073e9SAndroid Build Coastguard Worker       pairs;
56*da0073e9SAndroid Build Coastguard Worker   for (const auto i : c10::irange(n)) {
57*da0073e9SAndroid Build Coastguard Worker     pairs.push_back(c10::ApproximateClockToUnixTimeConverter::measurePair());
58*da0073e9SAndroid Build Coastguard Worker   }
59*da0073e9SAndroid Build Coastguard Worker   auto count_to_ns = converter.makeConverter();
60*da0073e9SAndroid Build Coastguard Worker   std::vector<int64_t> deltas;
61*da0073e9SAndroid Build Coastguard Worker   for (const auto& i : pairs) {
62*da0073e9SAndroid Build Coastguard Worker     deltas.push_back(i.t_ - count_to_ns(i.approx_t_));
63*da0073e9SAndroid Build Coastguard Worker   }
64*da0073e9SAndroid Build Coastguard Worker   std::sort(deltas.begin(), deltas.end());
65*da0073e9SAndroid Build Coastguard Worker 
66*da0073e9SAndroid Build Coastguard Worker   // In general it's not a good idea to put clocks in unit tests as it leads
67*da0073e9SAndroid Build Coastguard Worker   // to flakiness. We mitigate this by:
68*da0073e9SAndroid Build Coastguard Worker   //   1) Testing the clock itself. While the time to complete a task may
69*da0073e9SAndroid Build Coastguard Worker   //      vary, two clocks measuring the same time should be much more
70*da0073e9SAndroid Build Coastguard Worker   //      consistent.
71*da0073e9SAndroid Build Coastguard Worker   //   2) Only testing the interquartile range. Context switches between
72*da0073e9SAndroid Build Coastguard Worker   //      calls to the two timers do occur and can result in hundreds of
73*da0073e9SAndroid Build Coastguard Worker   //      nanoseconds of noise, but such switches are only a few percent
74*da0073e9SAndroid Build Coastguard Worker   //      of cases.
75*da0073e9SAndroid Build Coastguard Worker   //   3) We're willing to accept a somewhat large bias which can emerge from
76*da0073e9SAndroid Build Coastguard Worker   //      differences in the cost of calling each clock.
77*da0073e9SAndroid Build Coastguard Worker   EXPECT_LT(std::abs(deltas[n / 2]), 200);
78*da0073e9SAndroid Build Coastguard Worker   EXPECT_LT(deltas[n * 3 / 4] - deltas[n / 4], 50);
79*da0073e9SAndroid Build Coastguard Worker }
80*da0073e9SAndroid Build Coastguard Worker 
TEST(ProfilerTest,soft_assert)81*da0073e9SAndroid Build Coastguard Worker TEST(ProfilerTest, soft_assert) {
82*da0073e9SAndroid Build Coastguard Worker   EXPECT_TRUE(SOFT_ASSERT(true));
83*da0073e9SAndroid Build Coastguard Worker   torch::profiler::impl::setSoftAssertRaises(true);
84*da0073e9SAndroid Build Coastguard Worker   EXPECT_ANY_THROW(SOFT_ASSERT(false));
85*da0073e9SAndroid Build Coastguard Worker   torch::profiler::impl::setSoftAssertRaises(false);
86*da0073e9SAndroid Build Coastguard Worker   EXPECT_NO_THROW(SOFT_ASSERT(false));
87*da0073e9SAndroid Build Coastguard Worker   // Reset soft assert behavior to default
88*da0073e9SAndroid Build Coastguard Worker   torch::profiler::impl::setSoftAssertRaises(std::nullopt);
89*da0073e9SAndroid Build Coastguard Worker   EXPECT_NO_THROW(SOFT_ASSERT(false));
90*da0073e9SAndroid Build Coastguard Worker }
91