|
1 | 1 | #pragma once |
2 | | - |
| 2 | +#include <chrono> |
| 3 | +#include <mutex> |
| 4 | +#include <atomic> |
3 | 5 | #include "llama.h" |
4 | 6 | #include "llama-cparams.h" |
5 | 7 | #include "llama-graph.h" |
|
11 | 13 | #include <map> |
12 | 14 | #include <vector> |
13 | 15 |
|
| 16 | + |
| 17 | +namespace test { |
| 18 | + |
| 19 | +// from |
| 20 | +// https://stackoverflow.com/questions/16337610/how-to-know-if-a-type-is-a-specialization-of-stdvector |
| 21 | +template <typename, template <typename...> typename> constexpr bool is_specialization_v = false; |
| 22 | + |
| 23 | +template <template <typename...> typename value_type, typename... arg_types> |
| 24 | +constexpr bool is_specialization_v<value_type<arg_types...>, value_type> = true; |
| 25 | + |
| 26 | +template <typename value_type = std::chrono::nanoseconds> class stop_watch { |
| 27 | + public: |
| 28 | + using hr_clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady, |
| 29 | + std::chrono::high_resolution_clock, std::chrono::steady_clock>; |
| 30 | + static constexpr bool lock_free{ std::atomic<value_type>::is_always_lock_free }; |
| 31 | + using time_type = std::conditional_t<lock_free, value_type, uint64_t>; |
| 32 | + |
| 33 | + stop_watch(uint64_t newTime) noexcept { total_time_units.store(time_type{ newTime }, std::memory_order_release); } |
| 34 | + |
| 35 | + stop_watch & operator=(stop_watch && other) noexcept { |
| 36 | + if (this != &other) { |
| 37 | + total_time_units.store(other.total_time_units.load(std::memory_order_acquire), std::memory_order_release); |
| 38 | + start_time_units.store(other.start_time_units.load(std::memory_order_acquire), std::memory_order_release); |
| 39 | + } |
| 40 | + return *this; |
| 41 | + } |
| 42 | + |
| 43 | + stop_watch(stop_watch && other) noexcept { *this = std::move(other); } |
| 44 | + |
| 45 | + stop_watch & operator=(const stop_watch & other) noexcept { |
| 46 | + if (this != &other) { |
| 47 | + total_time_units.store(other.total_time_units.load(std::memory_order_acquire), std::memory_order_release); |
| 48 | + start_time_units.store(other.start_time_units.load(std::memory_order_acquire), std::memory_order_release); |
| 49 | + } |
| 50 | + return *this; |
| 51 | + } |
| 52 | + |
| 53 | + stop_watch(const stop_watch & other) noexcept { *this = other; } |
| 54 | + |
| 55 | + bool has_time_elapsed() noexcept { |
| 56 | + return ((get_current_time() - start_time_units.load(std::memory_order_acquire)) >= |
| 57 | + total_time_units.load(std::memory_order_acquire)); |
| 58 | + } |
| 59 | + |
| 60 | + void add_time() noexcept { |
| 61 | + //std::unique_lock lock{ mutex }; |
| 62 | + values.emplace_back(total_time_elapsed()); |
| 63 | + //lock.release(); |
| 64 | + reset(); |
| 65 | + } |
| 66 | + |
| 67 | + uint64_t get_count() noexcept { return values.size(); } |
| 68 | + |
| 69 | + uint64_t get_average(time_type newTimeValue = time_type{}) noexcept { |
| 70 | + std::unique_lock lock{ mutex }; |
| 71 | + uint64_t total_time{}; |
| 72 | + for (auto & value : values) { |
| 73 | + total_time += get_value_as_uint(value); |
| 74 | + } |
| 75 | + return total_time / ((values.size() > 0) ? values.size() : 1); |
| 76 | + } |
| 77 | + |
| 78 | + void reset(time_type newTimeValue = time_type{}) noexcept { |
| 79 | + if (newTimeValue != time_type{}) { |
| 80 | + total_time_units.store(newTimeValue, std::memory_order_release); |
| 81 | + } |
| 82 | + start_time_units.store(get_current_time(), std::memory_order_release); |
| 83 | + } |
| 84 | + |
| 85 | + uint64_t get_total_wait_time() const noexcept { |
| 86 | + return get_value_as_uint(total_time_units.load(std::memory_order_acquire)); |
| 87 | + } |
| 88 | + |
| 89 | + time_type total_time_elapsed() noexcept { |
| 90 | + return get_current_time() - start_time_units.load(std::memory_order_acquire); |
| 91 | + } |
| 92 | + |
| 93 | + uint64_t total_time_elapsed_uint64() noexcept { |
| 94 | + return get_value_as_uint(get_current_time()) - |
| 95 | + get_value_as_uint(start_time_units.load(std::memory_order_acquire)); |
| 96 | + } |
| 97 | + |
| 98 | + protected: |
| 99 | + std::atomic<time_type> total_time_units{}; |
| 100 | + std::atomic<time_type> start_time_units{}; |
| 101 | + std::vector<time_type> values{}; |
| 102 | + std::mutex mutex{}; |
| 103 | + |
| 104 | + time_type get_current_time() { |
| 105 | + if constexpr (lock_free) { |
| 106 | + return std::chrono::duration_cast<value_type>(hr_clock::now().time_since_epoch()); |
| 107 | + } else { |
| 108 | + return std::chrono::duration_cast<value_type>(hr_clock::now().time_since_epoch()).count(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + uint64_t get_value_as_uint(time_type time) { |
| 113 | + if constexpr (lock_free) { |
| 114 | + return time.count(); |
| 115 | + } else { |
| 116 | + return time; |
| 117 | + } |
| 118 | + } |
| 119 | +}; |
| 120 | +} // namespace test |
| 121 | + |
| 122 | +inline test::stop_watch stop_watch_val{ 0 }; |
| 123 | + |
| 124 | + |
14 | 125 | struct llama_model; |
15 | 126 | class llama_batch_allocr; |
16 | 127 |
|
|
0 commit comments