jasonfan commited on
Commit
38af1f7
·
verified ·
1 Parent(s): 6d94f95

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/StringUtil.h +262 -0
  2. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Synchronized.h +62 -0
  3. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/ThreadLocal.h +156 -0
  4. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/ThreadLocalDebugInfo.h +85 -0
  5. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Type.h +30 -0
  6. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeCast.h +210 -0
  7. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeIndex.h +127 -0
  8. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeList.h +515 -0
  9. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeSafeSignMath.h +140 -0
  10. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeTraits.h +151 -0
  11. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Unicode.h +14 -0
  12. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/UniqueVoidPtr.h +140 -0
  13. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Unroll.h +30 -0
  14. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/WaitCounter.h +97 -0
  15. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/WaitCounterDynamicBackend.h +21 -0
  16. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/accumulate.h +124 -0
  17. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/bit_cast.h +44 -0
  18. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/bits.h +61 -0
  19. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/complex.h +668 -0
  20. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/complex_math.h +406 -0
  21. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/complex_utils.h +46 -0
  22. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/copysign.h +27 -0
  23. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/env.h +31 -0
  24. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/error.h +11 -0
  25. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/flat_hash_map.h +2102 -0
  26. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/floating_point_utils.h +33 -0
  27. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/generic_math.h +102 -0
  28. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/hash.h +379 -0
  29. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/int128.h +398 -0
  30. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/intrusive_ptr.h +1076 -0
  31. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/irange.h +123 -0
  32. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/llvmMathExtras.h +905 -0
  33. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/logging_is_google_glog.h +109 -0
  34. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/logging_is_not_google_glog.h +258 -0
  35. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/numa.h +41 -0
  36. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/order_preserving_flat_hash_map.h +2217 -0
  37. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/overflows.h +100 -0
  38. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/overloaded.h +31 -0
  39. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/python_stub.h +4 -0
  40. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/qint32.h +18 -0
  41. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/qint8.h +20 -0
  42. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/quint2x4.h +19 -0
  43. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/quint4x2.h +19 -0
  44. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/quint8.h +18 -0
  45. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/safe_numerics.h +90 -0
  46. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/signal_handler.h +119 -0
  47. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/sparse_bitset.h +893 -0
  48. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/ssize.h +46 -0
  49. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/static_tracepoint.h +34 -0
  50. code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/static_tracepoint_elfx86.h +144 -0
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/StringUtil.h ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef C10_UTIL_STRINGUTIL_H_
2
+ #define C10_UTIL_STRINGUTIL_H_
3
+
4
+ #include <c10/macros/Macros.h>
5
+ #include <c10/util/string_utils.h>
6
+
7
+ #include <cstddef>
8
+ #include <optional>
9
+ #include <ostream>
10
+ #include <sstream>
11
+ #include <string>
12
+ #include <string_view>
13
+ #include <type_traits>
14
+ #include <vector>
15
+
16
+ C10_CLANG_DIAGNOSTIC_PUSH()
17
+ #if C10_CLANG_HAS_WARNING("-Wshorten-64-to-32")
18
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wshorten-64-to-32")
19
+ #endif
20
+
21
+ namespace c10 {
22
+
23
+ namespace detail {
24
+
25
+ // Obtains the base name from a full path.
26
+ C10_API std::string StripBasename(const std::string& full_path);
27
+
28
+ C10_API std::string ExcludeFileExtension(const std::string& full_path);
29
+
30
+ struct CompileTimeEmptyString {
31
+ operator const std::string&() const {
32
+ static const std::string empty_string_literal;
33
+ return empty_string_literal;
34
+ }
35
+ operator const char*() const {
36
+ return "";
37
+ }
38
+ };
39
+
40
+ template <typename T>
41
+ struct CanonicalizeStrTypes {
42
+ using type = const T&;
43
+ };
44
+
45
+ template <size_t N>
46
+ // NOLINTNEXTLINE(*c-arrays*)
47
+ struct CanonicalizeStrTypes<char[N]> {
48
+ using type = const char*;
49
+ };
50
+
51
+ inline std::ostream& _str(std::ostream& ss) {
52
+ return ss;
53
+ }
54
+
55
+ template <class T, class = std::ostream&>
56
+ struct Streamable : std::false_type {};
57
+
58
+ template <class T>
59
+ struct Streamable<T, decltype(std::declval<std::ostream&>() << T{})>
60
+ : std::true_type {};
61
+
62
+ template <typename T>
63
+ inline std::ostream& _str(std::ostream& ss, const T& t) {
64
+ if constexpr (std::is_enum_v<T> && !Streamable<T>::value) {
65
+ // NOLINTNEXTLINE(modernize-type-traits)
66
+ return _str(ss, static_cast<typename std::underlying_type<T>::type>(t));
67
+ } else {
68
+ // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
69
+ ss << t;
70
+ return ss;
71
+ }
72
+ }
73
+
74
+ template <typename T>
75
+ inline std::ostream& _str(std::ostream& ss, const std::optional<T>& t) {
76
+ if (t.has_value()) {
77
+ return _str(ss, t.value());
78
+ }
79
+ ss << "std::nullopt";
80
+ return ss;
81
+ }
82
+ // Overloads of _str for wide types; forces narrowing.
83
+ C10_API std::ostream& _str(std::ostream& ss, const wchar_t* wCStr);
84
+ C10_API std::ostream& _str(std::ostream& ss, const wchar_t& wChar);
85
+ C10_API std::ostream& _str(std::ostream& ss, const std::wstring& wString);
86
+
87
+ template <>
88
+ inline std::ostream& _str<CompileTimeEmptyString>(
89
+ std::ostream& ss,
90
+ const CompileTimeEmptyString&) {
91
+ return ss;
92
+ }
93
+
94
+ template <typename T, typename... Args>
95
+ inline std::ostream& _str(std::ostream& ss, const T& t, const Args&... args) {
96
+ return _str(_str(ss, t), args...);
97
+ }
98
+
99
+ template <typename... Args>
100
+ struct _str_wrapper final {
101
+ static std::string call(const Args&... args) {
102
+ std::ostringstream ss;
103
+ _str(ss, args...);
104
+ return ss.str();
105
+ }
106
+ };
107
+
108
+ // Specializations for already-a-string types.
109
+ template <>
110
+ struct _str_wrapper<std::string> final {
111
+ // return by reference to avoid the binary size of a string copy
112
+ static const std::string& call(const std::string& str) {
113
+ return str;
114
+ }
115
+ };
116
+
117
+ template <>
118
+ struct _str_wrapper<const char*> final {
119
+ static const char* call(const char* str) {
120
+ return str;
121
+ }
122
+ };
123
+
124
+ // For c10::str() with an empty argument list (which is common in our assert
125
+ // macros), we don't want to pay the binary size for constructing and
126
+ // destructing a stringstream or even constructing a string.
127
+ template <>
128
+ struct _str_wrapper<> final {
129
+ static CompileTimeEmptyString call() {
130
+ return CompileTimeEmptyString();
131
+ }
132
+ };
133
+
134
+ } // namespace detail
135
+
136
+ // Convert a list of string-like arguments into a single string.
137
+ template <typename... Args>
138
+ inline decltype(auto) str(const Args&... args) {
139
+ return detail::_str_wrapper<
140
+ typename detail::CanonicalizeStrTypes<Args>::type...>::call(args...);
141
+ }
142
+
143
+ template <class Container>
144
+ inline std::string Join(const std::string& delimiter, const Container& v) {
145
+ std::stringstream s;
146
+ int cnt = static_cast<int64_t>(v.size()) - 1;
147
+ for (auto i = v.begin(); i != v.end(); ++i, --cnt) {
148
+ s << (*i) << (cnt ? delimiter : "");
149
+ }
150
+ return std::move(s).str();
151
+ }
152
+
153
+ // Replace all occurrences of "from" substring to "to" string.
154
+ // Returns number of replacements
155
+ size_t C10_API
156
+ ReplaceAll(std::string& s, std::string_view from, std::string_view to);
157
+
158
+ /// Represents a location in source code (for debugging).
159
+ struct C10_API SourceLocation {
160
+ const char* function;
161
+ const char* file;
162
+ uint32_t line;
163
+ };
164
+
165
+ std::ostream& operator<<(std::ostream& out, const SourceLocation& loc);
166
+
167
+ // unix isprint but insensitive to locale
168
+ inline bool isPrint(char s) {
169
+ return s > 0x1f && s < 0x7f;
170
+ }
171
+
172
+ inline void printQuotedString(std::ostream& stmt, const std::string_view str) {
173
+ stmt << "\"";
174
+ for (auto s : str) {
175
+ switch (s) {
176
+ case '\\':
177
+ stmt << "\\\\";
178
+ break;
179
+ case '\'':
180
+ stmt << "\\'";
181
+ break;
182
+ case '\"':
183
+ stmt << "\\\"";
184
+ break;
185
+ case '\a':
186
+ stmt << "\\a";
187
+ break;
188
+ case '\b':
189
+ stmt << "\\b";
190
+ break;
191
+ case '\f':
192
+ stmt << "\\f";
193
+ break;
194
+ case '\n':
195
+ stmt << "\\n";
196
+ break;
197
+ case '\r':
198
+ stmt << "\\r";
199
+ break;
200
+ case '\t':
201
+ stmt << "\\t";
202
+ break;
203
+ case '\v':
204
+ stmt << "\\v";
205
+ break;
206
+ default:
207
+ if (isPrint(s)) {
208
+ stmt << s;
209
+ } else {
210
+ // C++ io has stateful formatting settings. Messing with
211
+ // them is probably worse than doing this manually.
212
+ // NOLINTNEXTLINE(*c-arrays*)
213
+ char buf[4] = "000";
214
+ // NOLINTNEXTLINE(*narrowing-conversions)
215
+ buf[2] += s % 8;
216
+ s /= 8;
217
+ // NOLINTNEXTLINE(*narrowing-conversions)
218
+ buf[1] += s % 8;
219
+ s /= 8;
220
+ // NOLINTNEXTLINE(*narrowing-conversions)
221
+ buf[0] += s;
222
+ stmt << "\\" << buf;
223
+ }
224
+ break;
225
+ }
226
+ }
227
+ stmt << "\"";
228
+ }
229
+
230
+ template <typename T>
231
+ std::optional<T> tryToNumber(const char* symbol) = delete;
232
+ template <typename T>
233
+ std::optional<T> tryToNumber(const std::string& symbol) = delete;
234
+
235
+ /*
236
+ * Convert a string to a 64 bit integer. Trailing whitespaces are not supported.
237
+ * Similarly, integer string with trailing characters like "123abc" will be
238
+ * rejected.
239
+ */
240
+ template <>
241
+ C10_API std::optional<int64_t> tryToNumber<int64_t>(const char* symbol);
242
+ template <>
243
+ C10_API std::optional<int64_t> tryToNumber<int64_t>(const std::string& symbol);
244
+
245
+ /*
246
+ * Convert a string to a double. Trailing whitespaces are not supported.
247
+ * Similarly, integer string with trailing characters like "123abc" will
248
+ * be rejected.
249
+ */
250
+ template <>
251
+ C10_API std::optional<double> tryToNumber<double>(const char* symbol);
252
+ template <>
253
+ C10_API std::optional<double> tryToNumber<double>(const std::string& symbol);
254
+
255
+ C10_API std::vector<std::string_view> split(
256
+ std::string_view target,
257
+ char delimiter);
258
+ } // namespace c10
259
+
260
+ C10_CLANG_DIAGNOSTIC_POP()
261
+
262
+ #endif // C10_UTIL_STRINGUTIL_H_
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Synchronized.h ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <mutex>
4
+
5
+ namespace c10 {
6
+
7
+ /**
8
+ * A very simple Synchronization class for error-free use of data
9
+ * in a multi-threaded context. See folly/docs/Synchronized.md for
10
+ * the inspiration of this class.
11
+ *
12
+ * Full URL:
13
+ * https://github.com/facebook/folly/blob/main/folly/docs/Synchronized.md
14
+ *
15
+ * This class implements a small subset of the generic functionality
16
+ * implemented by folly:Synchronized<T>. Specifically, only withLock<T>
17
+ * is implemented here since it's the smallest possible API that is
18
+ * able to cover a large surface area of functionality offered by
19
+ * folly::Synchronized<T>.
20
+ */
21
+ template <typename T>
22
+ class Synchronized final {
23
+ mutable std::mutex mutex_;
24
+ T data_;
25
+
26
+ public:
27
+ Synchronized() = default;
28
+ Synchronized(T const& data) : data_(data) {}
29
+ Synchronized(T&& data) : data_(std::move(data)) {}
30
+
31
+ // Don't permit copy construction, move, assignment, or
32
+ // move assignment, since the underlying std::mutex
33
+ // isn't necessarily copyable/moveable.
34
+ Synchronized(Synchronized const&) = delete;
35
+ Synchronized(Synchronized&&) = delete;
36
+ Synchronized operator=(Synchronized const&) = delete;
37
+ Synchronized operator=(Synchronized&&) = delete;
38
+ ~Synchronized() = default;
39
+
40
+ /**
41
+ * To use, call withLock<T> with a callback that accepts T either
42
+ * by copy or by reference. Use the protected variable in the
43
+ * provided callback safely.
44
+ */
45
+ template <typename CB>
46
+ auto withLock(CB&& cb) {
47
+ std::lock_guard<std::mutex> guard(this->mutex_);
48
+ return std::forward<CB>(cb)(this->data_);
49
+ }
50
+
51
+ /**
52
+ * To use, call withLock<T> with a callback that accepts T either
53
+ * by copy or by const reference. Use the protected variable in
54
+ * the provided callback safely.
55
+ */
56
+ template <typename CB>
57
+ auto withLock(CB&& cb) const {
58
+ std::lock_guard<std::mutex> guard(this->mutex_);
59
+ return std::forward<CB>(cb)(this->data_);
60
+ }
61
+ };
62
+ } // end namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/ThreadLocal.h ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Macros.h>
4
+
5
+ /**
6
+ * Android versions with libgnustl incorrectly handle thread_local C++
7
+ * qualifier with composite types. NDK up to r17 version is affected.
8
+ *
9
+ * (A fix landed on Jun 4 2018:
10
+ * https://android-review.googlesource.com/c/toolchain/gcc/+/683601)
11
+ *
12
+ * In such cases, use c10::ThreadLocal<T> wrapper
13
+ * which is `pthread_*` based with smart pointer semantics.
14
+ *
15
+ * In addition, convenient macro C10_DEFINE_TLS_static is available.
16
+ * To define static TLS variable of type std::string, do the following
17
+ * ```
18
+ * C10_DEFINE_TLS_static(std::string, str_tls_);
19
+ * ///////
20
+ * {
21
+ * *str_tls_ = "abc";
22
+ * assert(str_tls_->length(), 3);
23
+ * }
24
+ * ```
25
+ *
26
+ * (see c10/test/util/ThreadLocal_test.cpp for more examples)
27
+ */
28
+ #if !defined(C10_PREFER_CUSTOM_THREAD_LOCAL_STORAGE)
29
+
30
+ #if defined(C10_ANDROID) && defined(__GLIBCXX__) && __GLIBCXX__ < 20180604
31
+ #define C10_PREFER_CUSTOM_THREAD_LOCAL_STORAGE
32
+ #endif // defined(C10_ANDROID) && defined(__GLIBCXX__) && __GLIBCXX__ < 20180604
33
+
34
+ #endif // !defined(C10_PREFER_CUSTOM_THREAD_LOCAL_STORAGE)
35
+
36
+ #if defined(C10_PREFER_CUSTOM_THREAD_LOCAL_STORAGE)
37
+ #include <c10/util/Exception.h>
38
+ #include <errno.h>
39
+ #include <pthread.h>
40
+ #include <memory>
41
+ namespace c10 {
42
+
43
+ /**
44
+ * @brief Temporary thread_local C++ qualifier replacement for Android
45
+ * based on `pthread_*`.
46
+ * To be used with composite types that provide default ctor.
47
+ */
48
+ template <typename Type>
49
+ class ThreadLocal {
50
+ public:
51
+ ThreadLocal() {
52
+ pthread_key_create(
53
+ &key_, [](void* buf) { delete static_cast<Type*>(buf); });
54
+ }
55
+
56
+ ~ThreadLocal() {
57
+ if (void* current = pthread_getspecific(key_)) {
58
+ delete static_cast<Type*>(current);
59
+ }
60
+
61
+ pthread_key_delete(key_);
62
+ }
63
+
64
+ ThreadLocal(const ThreadLocal&) = delete;
65
+ ThreadLocal& operator=(const ThreadLocal&) = delete;
66
+
67
+ Type& get() {
68
+ if (void* current = pthread_getspecific(key_)) {
69
+ return *static_cast<Type*>(current);
70
+ }
71
+
72
+ std::unique_ptr<Type> ptr = std::make_unique<Type>();
73
+ if (0 == pthread_setspecific(key_, ptr.get())) {
74
+ return *ptr.release();
75
+ }
76
+
77
+ int err = errno;
78
+ TORCH_INTERNAL_ASSERT(false, "pthread_setspecific() failed, errno = ", err);
79
+ }
80
+
81
+ Type& operator*() {
82
+ return get();
83
+ }
84
+
85
+ Type* operator->() {
86
+ return &get();
87
+ }
88
+
89
+ private:
90
+ pthread_key_t key_;
91
+ };
92
+
93
+ } // namespace c10
94
+
95
+ #define C10_DEFINE_TLS_static(Type, Name) static ::c10::ThreadLocal<Type> Name
96
+
97
+ #define C10_DECLARE_TLS_class_static(Class, Type, Name) \
98
+ static ::c10::ThreadLocal<Type> Name
99
+
100
+ #define C10_DEFINE_TLS_class_static(Class, Type, Name) \
101
+ ::c10::ThreadLocal<Type> Class::Name
102
+
103
+ #else // defined(C10_PREFER_CUSTOM_THREAD_LOCAL_STORAGE)
104
+
105
+ namespace c10 {
106
+
107
+ /**
108
+ * @brief Default thread_local implementation for non-Android cases.
109
+ * To be used with composite types that provide default ctor.
110
+ */
111
+ template <typename Type>
112
+ class ThreadLocal {
113
+ public:
114
+ using Accessor = Type* (*)();
115
+ explicit ThreadLocal(Accessor accessor) : accessor_(accessor) {}
116
+
117
+ ThreadLocal(const ThreadLocal&) = delete;
118
+ ThreadLocal(ThreadLocal&&) noexcept = default;
119
+ ThreadLocal& operator=(const ThreadLocal&) = delete;
120
+ ThreadLocal& operator=(ThreadLocal&&) noexcept = default;
121
+ ~ThreadLocal() = default;
122
+
123
+ Type& get() {
124
+ return *accessor_();
125
+ }
126
+
127
+ Type& operator*() {
128
+ return get();
129
+ }
130
+
131
+ Type* operator->() {
132
+ return &get();
133
+ }
134
+
135
+ private:
136
+ Accessor accessor_;
137
+ };
138
+
139
+ } // namespace c10
140
+
141
+ #define C10_DEFINE_TLS_static(Type, Name) \
142
+ static ::c10::ThreadLocal<Type> Name([]() { \
143
+ static thread_local Type var; \
144
+ return &var; \
145
+ })
146
+
147
+ #define C10_DECLARE_TLS_class_static(Class, Type, Name) \
148
+ static ::c10::ThreadLocal<Type> Name
149
+
150
+ #define C10_DEFINE_TLS_class_static(Class, Type, Name) \
151
+ ::c10::ThreadLocal<Type> Class::Name([]() { \
152
+ static thread_local Type var; \
153
+ return &var; \
154
+ })
155
+
156
+ #endif // defined(C10_PREFER_CUSTOM_THREAD_LOCAL_STORAGE)
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/ThreadLocalDebugInfo.h ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Export.h>
4
+
5
+ #include <cstdint>
6
+ #include <memory>
7
+
8
+ namespace c10 {
9
+
10
+ enum class C10_API_ENUM DebugInfoKind : uint8_t {
11
+ PRODUCER_INFO = 0,
12
+ MOBILE_RUNTIME_INFO,
13
+ PROFILER_STATE,
14
+ INFERENCE_CONTEXT, // for inference usage
15
+ PARAM_COMMS_INFO,
16
+
17
+ TEST_INFO, // used only in tests
18
+ TEST_INFO_2, // used only in tests
19
+ };
20
+
21
+ class C10_API DebugInfoBase {
22
+ public:
23
+ DebugInfoBase() = default;
24
+ virtual ~DebugInfoBase() = default;
25
+ };
26
+
27
+ // Thread local debug information is propagated across the forward
28
+ // (including async fork tasks) and backward passes and is supposed
29
+ // to be utilized by the user's code to pass extra information from
30
+ // the higher layers (e.g. model id) down to the lower levels
31
+ // (e.g. to the operator observers used for debugging, logging,
32
+ // profiling, etc)
33
+ class C10_API ThreadLocalDebugInfo {
34
+ public:
35
+ static DebugInfoBase* get(DebugInfoKind kind);
36
+
37
+ // Get current ThreadLocalDebugInfo
38
+ static std::shared_ptr<ThreadLocalDebugInfo> current();
39
+
40
+ // Internal, use DebugInfoGuard/ThreadLocalStateGuard
41
+ static void _forceCurrentDebugInfo(
42
+ std::shared_ptr<ThreadLocalDebugInfo> info);
43
+
44
+ // Push debug info struct of a given kind
45
+ static void _push(DebugInfoKind kind, std::shared_ptr<DebugInfoBase> info);
46
+ // Pop debug info, throws in case the last pushed
47
+ // debug info is not of a given kind
48
+ static std::shared_ptr<DebugInfoBase> _pop(DebugInfoKind kind);
49
+ // Peek debug info, throws in case the last pushed debug info is not of the
50
+ // given kind
51
+ static std::shared_ptr<DebugInfoBase> _peek(DebugInfoKind kind);
52
+
53
+ private:
54
+ std::shared_ptr<DebugInfoBase> info_;
55
+ DebugInfoKind kind_;
56
+ std::shared_ptr<ThreadLocalDebugInfo> parent_info_;
57
+
58
+ friend class DebugInfoGuard;
59
+ };
60
+
61
+ // DebugInfoGuard is used to set debug information,
62
+ // ThreadLocalDebugInfo is semantically immutable, the values are set
63
+ // through the scope-based guard object.
64
+ // Nested DebugInfoGuard adds/overrides existing values in the scope,
65
+ // restoring the original values after exiting the scope.
66
+ // Users can access the values through the ThreadLocalDebugInfo::get() call;
67
+ class C10_API DebugInfoGuard {
68
+ public:
69
+ DebugInfoGuard(DebugInfoKind kind, std::shared_ptr<DebugInfoBase> info);
70
+
71
+ explicit DebugInfoGuard(std::shared_ptr<ThreadLocalDebugInfo> info);
72
+
73
+ ~DebugInfoGuard();
74
+
75
+ DebugInfoGuard(const DebugInfoGuard&) = delete;
76
+ DebugInfoGuard(DebugInfoGuard&&) = delete;
77
+ DebugInfoGuard& operator=(const DebugInfoGuard&) = delete;
78
+ DebugInfoGuard& operator=(DebugInfoGuard&&) = delete;
79
+
80
+ private:
81
+ bool active_ = false;
82
+ std::shared_ptr<ThreadLocalDebugInfo> prev_info_ = nullptr;
83
+ };
84
+
85
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Type.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef C10_UTIL_TYPE_H_
2
+ #define C10_UTIL_TYPE_H_
3
+
4
+ #include <cstddef>
5
+ #include <string>
6
+ #ifdef __GXX_RTTI
7
+ #include <typeinfo>
8
+ #endif // __GXX_RTTI
9
+
10
+ #include <c10/macros/Macros.h>
11
+
12
+ namespace c10 {
13
+
14
+ /// Utility to demangle a C++ symbol name.
15
+ C10_API std::string demangle(const char* name);
16
+
17
+ /// Returns the printable name of the type.
18
+ template <typename T>
19
+ inline const char* demangle_type() {
20
+ #ifdef __GXX_RTTI
21
+ static const auto& name = *(new std::string(demangle(typeid(T).name())));
22
+ return name.c_str();
23
+ #else // __GXX_RTTI
24
+ return "(RTTI disabled, cannot show name)";
25
+ #endif // __GXX_RTTI
26
+ }
27
+
28
+ } // namespace c10
29
+
30
+ #endif // C10_UTIL_TYPE_H_
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeCast.h ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <c10/macros/Macros.h>
3
+ #include <c10/util/BFloat16.h>
4
+ #include <c10/util/Float8_e4m3fn.h>
5
+ #include <c10/util/Float8_e4m3fnuz.h>
6
+ #include <c10/util/Float8_e5m2.h>
7
+ #include <c10/util/Float8_e5m2fnuz.h>
8
+ #include <c10/util/Float8_e8m0fnu.h>
9
+ #include <c10/util/Half.h>
10
+ #include <c10/util/complex.h>
11
+ #include <c10/util/overflows.h>
12
+
13
+ #include <type_traits>
14
+
15
+ C10_CLANG_DIAGNOSTIC_PUSH()
16
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
17
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
18
+ #endif
19
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
20
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
21
+ #endif
22
+
23
+ namespace c10 {
24
+
25
+ template <typename dest_t, typename src_t>
26
+ struct needs_real {
27
+ constexpr static bool value =
28
+ (is_complex<src_t>::value && !is_complex<dest_t>::value);
29
+ };
30
+
31
+ template <bool, typename src_t>
32
+ struct maybe_real {
33
+ C10_HOST_DEVICE static inline src_t apply(src_t src) {
34
+ return src;
35
+ }
36
+ };
37
+
38
+ template <typename src_t>
39
+ struct maybe_real<true, src_t> {
40
+ C10_HOST_DEVICE static inline decltype(auto) apply(src_t src) {
41
+ return src.real();
42
+ }
43
+ };
44
+
45
+ template <bool, typename src_t>
46
+ struct maybe_bool {
47
+ C10_HOST_DEVICE static inline src_t apply(src_t src) {
48
+ return src;
49
+ }
50
+ };
51
+
52
+ template <typename src_t>
53
+ struct maybe_bool<true, src_t> {
54
+ C10_HOST_DEVICE static inline decltype(auto) apply(src_t src) {
55
+ // Don't use bool operator so as to to also compile for ComplexHalf.
56
+ return src.real() || src.imag();
57
+ }
58
+ };
59
+
60
+ // Note: deliberately ignores undefined behavior, consistent with NumPy.
61
+ // PyTorch's type conversions can cause a variety of undefined behavior,
62
+ // including float to integral overflow and signed to unsigned integer overflow.
63
+ // Some of this undefined behavior is addressed below.
64
+ template <typename dest_t, typename src_t>
65
+ struct static_cast_with_inter_type {
66
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline dest_t apply(
67
+ src_t src) {
68
+ constexpr bool real = needs_real<dest_t, src_t>::value;
69
+ auto r = maybe_real<real, src_t>::apply(src);
70
+ return static_cast<dest_t>(r);
71
+ }
72
+ };
73
+
74
+ // Partial template specialization for casting to bool.
75
+ // Need to handle complex types separately, as we don't
76
+ // simply want to cast the real part to bool.
77
+ template <typename src_t>
78
+ struct static_cast_with_inter_type<bool, src_t> {
79
+ C10_HOST_DEVICE static inline bool apply(src_t src) {
80
+ constexpr bool complex = needs_real<bool, src_t>::value;
81
+ return static_cast<bool>(maybe_bool<complex, src_t>::apply(src));
82
+ }
83
+ };
84
+
85
+ // Partial template instantiation for casting to uint8.
86
+ // Note: Converting from negative float values to unsigned integer types is
87
+ // undefined behavior in C++, and current CPU and GPU compilers exhibit
88
+ // divergent behavior. Casting from negative float values to signed
89
+ // integer types and then to unsigned integer types is not undefined,
90
+ // however, so this cast improves the consistency of type conversions
91
+ // to uint8 across compilers.
92
+ // Further note: Type conversions across compilers still have other undefined
93
+ // and divergent behavior.
94
+ template <typename src_t>
95
+ struct static_cast_with_inter_type<uint8_t, src_t> {
96
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline uint8_t apply(
97
+ src_t src) {
98
+ constexpr bool real = needs_real<uint8_t, src_t>::value;
99
+ return static_cast<uint8_t>(
100
+ static_cast<int64_t>(maybe_real<real, src_t>::apply(src)));
101
+ }
102
+ };
103
+
104
+ template <>
105
+ struct static_cast_with_inter_type<c10::complex<c10::Half>, c10::BFloat16> {
106
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
107
+ c10::Half>
108
+ apply(c10::BFloat16 src) {
109
+ return static_cast<c10::complex<c10::Half>>(c10::complex<float>{src});
110
+ }
111
+ };
112
+
113
+ template <>
114
+ struct static_cast_with_inter_type<c10::complex<c10::Half>, c10::Float8_e5m2> {
115
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
116
+ c10::Half>
117
+ apply(c10::Float8_e5m2 src) {
118
+ return static_cast<c10::complex<c10::Half>>(c10::complex<float>{src});
119
+ }
120
+ };
121
+
122
+ template <>
123
+ struct static_cast_with_inter_type<
124
+ c10::complex<c10::Half>,
125
+ c10::Float8_e5m2fnuz> {
126
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
127
+ c10::Half>
128
+ apply(c10::Float8_e5m2fnuz src) {
129
+ return static_cast<c10::complex<c10::Half>>(c10::complex<float>{src});
130
+ }
131
+ };
132
+
133
+ template <>
134
+ struct static_cast_with_inter_type<
135
+ c10::complex<c10::Half>,
136
+ c10::Float8_e4m3fn> {
137
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
138
+ c10::Half>
139
+ apply(c10::Float8_e4m3fn src) {
140
+ return static_cast<c10::complex<c10::Half>>(c10::complex<float>{src});
141
+ }
142
+ };
143
+
144
+ template <>
145
+ struct static_cast_with_inter_type<
146
+ c10::complex<c10::Half>,
147
+ c10::Float8_e4m3fnuz> {
148
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
149
+ c10::Half>
150
+ apply(c10::Float8_e4m3fnuz src) {
151
+ return static_cast<c10::complex<c10::Half>>(c10::complex<float>{src});
152
+ }
153
+ };
154
+
155
+ // TODO(#146647): Can we make all these template specialization happen
156
+ // based off our apply macros?
157
+ template <>
158
+ struct static_cast_with_inter_type<
159
+ c10::complex<c10::Half>,
160
+ c10::Float8_e8m0fnu> {
161
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
162
+ c10::Half>
163
+ apply(c10::Float8_e8m0fnu src) {
164
+ return static_cast<c10::complex<c10::Half>>(c10::complex<float>{src});
165
+ }
166
+ };
167
+
168
+ template <>
169
+ struct static_cast_with_inter_type<c10::complex<c10::Half>, c10::Half> {
170
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
171
+ c10::Half>
172
+ apply(c10::Half src) {
173
+ return static_cast<c10::complex<c10::Half>>(c10::complex<float>{src});
174
+ }
175
+ };
176
+
177
+ template <>
178
+ struct static_cast_with_inter_type<
179
+ c10::complex<c10::Half>,
180
+ c10::complex<double>> {
181
+ C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline c10::complex<
182
+ c10::Half>
183
+ apply(c10::complex<double> src) {
184
+ return static_cast<c10::complex<c10::Half>>(
185
+ static_cast<c10::complex<float>>(src));
186
+ }
187
+ };
188
+
189
+ template <typename To, typename From>
190
+ C10_HOST_DEVICE To convert(From f) {
191
+ return static_cast_with_inter_type<To, From>::apply(f);
192
+ }
193
+
194
+ // Define separately to avoid being inlined and prevent code-size bloat
195
+ [[noreturn]] C10_API void report_overflow(const char* name);
196
+
197
+ template <typename To, typename From>
198
+ To checked_convert(From f, const char* name) {
199
+ // Converting to bool can't overflow so we exclude this case from checking.
200
+ if (!std::is_same_v<To, bool> && overflows<To, From>(f)) {
201
+ report_overflow(name);
202
+ }
203
+ return convert<To, From>(f);
204
+ }
205
+
206
+ } // namespace c10
207
+
208
+ C10_CLANG_DIAGNOSTIC_POP()
209
+
210
+ // Trigger tests for D25440771. TODO: Remove this line any time you want.
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeIndex.h ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/util/ConstexprCrc.h>
4
+ #include <c10/util/IdWrapper.h>
5
+ #include <c10/util/string_view.h>
6
+ #include <cstdint>
7
+ #include <ostream>
8
+ #include <string>
9
+ #include <type_traits>
10
+
11
+ #if !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
12
+ #define C10_TYPENAME_SUPPORTS_CONSTEXPR 1
13
+ #define C10_TYPENAME_CONSTEXPR constexpr
14
+ #endif
15
+
16
+ namespace c10::util {
17
+
18
+ struct type_index final : IdWrapper<type_index, uint64_t> {
19
+ constexpr explicit type_index(uint64_t checksum) : IdWrapper(checksum) {}
20
+
21
+ // Allow usage in std::map / std::set
22
+ // TODO Disallow this and rather use std::unordered_map/set everywhere
23
+ friend constexpr bool operator<(type_index lhs, type_index rhs) noexcept {
24
+ return lhs.underlyingId() < rhs.underlyingId();
25
+ }
26
+
27
+ friend std::ostream& operator<<(std::ostream& stream, type_index typeId) {
28
+ return stream << typeId.underlyingId();
29
+ }
30
+ };
31
+
32
+ namespace detail {
33
+
34
+ template <typename T>
35
+ inline constexpr c10::c10_string_view fully_qualified_type_name_impl() {
36
+ #if defined(_MSC_VER) && !defined(__clang__)
37
+ constexpr std::string_view fun_sig = __FUNCSIG__;
38
+ #if defined(__NVCC__)
39
+ constexpr std::string_view prefix =
40
+ "c10::basic_string_view<char> c10::util::detail::fully_qualified_type_name_impl<";
41
+ constexpr std::string_view suffix = ">()";
42
+ #else
43
+ constexpr std::string_view prefix =
44
+ "class c10::basic_string_view<char> __cdecl c10::util::detail::fully_qualified_type_name_impl<";
45
+ constexpr std::string_view suffix = ">(void)";
46
+ #endif
47
+ #elif defined(__clang__)
48
+ constexpr std::string_view fun_sig = __PRETTY_FUNCTION__;
49
+ constexpr std::string_view prefix =
50
+ "c10::c10_string_view c10::util::detail::fully_qualified_type_name_impl() [T = ";
51
+ constexpr std::string_view suffix = "]";
52
+ #elif defined(__GNUC__)
53
+ constexpr std::string_view fun_sig = __PRETTY_FUNCTION__;
54
+ constexpr std::string_view prefix =
55
+ "constexpr c10::c10_string_view c10::util::detail::fully_qualified_type_name_impl() [with T = ";
56
+ constexpr std::string_view suffix =
57
+ "; c10::c10_string_view = c10::basic_string_view<char>]";
58
+ #endif
59
+ #if !defined(__CUDA_ARCH__) && !defined(__CUDA_ARCH_LIST__)
60
+ static_assert(c10::starts_with(
61
+ static_cast<std::string_view>(fun_sig),
62
+ static_cast<std::string_view>(prefix)));
63
+ static_assert(c10::ends_with(
64
+ static_cast<std::string_view>(fun_sig),
65
+ static_cast<std::string_view>(suffix)));
66
+ #endif
67
+ return fun_sig.substr(
68
+ prefix.size(), fun_sig.size() - prefix.size() - suffix.size());
69
+ }
70
+
71
+ #if !defined(__CUDA_ARCH__) && !defined(__CUDA_ARCH_LIST__)
72
+ template <typename T>
73
+ inline constexpr uint64_t type_index_impl() {
74
+ // Idea: __PRETTY_FUNCTION__ (or __FUNCSIG__ on msvc) contains a qualified name
75
+ // of this function, including its template parameter, i.e. including the
76
+ // type we want an id for. We use this name and run crc64 on it to get a type
77
+ // id.
78
+ #if defined(_MSC_VER) && !defined(__clang__)
79
+ return crc64(__FUNCSIG__, sizeof(__FUNCSIG__)).checksum();
80
+ #elif defined(__clang__)
81
+ return crc64(__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__)).checksum();
82
+ #elif defined(__GNUC__)
83
+ return crc64(__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__)).checksum();
84
+ #endif
85
+ }
86
+ #endif
87
+
88
+ } // namespace detail
89
+
90
+ template <typename T>
91
+ inline constexpr type_index get_type_index() {
92
+ #if !defined(__CUDA_ARCH__) && !defined(__CUDA_ARCH_LIST__)
93
+ // To enforce that this is really computed at compile time, we pass the
94
+ // type index through std::integral_constant.
95
+ return type_index{std::integral_constant<
96
+ uint64_t,
97
+ detail::type_index_impl<std::decay_t<T>>()>::value};
98
+ #else
99
+ // There's nothing in theory preventing us from running this on device code
100
+ // except for nvcc throwing a compiler error if we enable it.
101
+ return (abort(), type_index(0));
102
+ #endif
103
+ }
104
+
105
+ #if !defined(TORCH_PEDANTIC)
106
+ // Use precomputed hashsum for std::string
107
+ // Needed to workaround ambiguity in class name resolution
108
+ // into __PRETTY_FUNCTION__ when abovementioned class is defined in inlined
109
+ // namespace. In multi-ABI C++ library, `std::string` is an alias to
110
+ // `std::__cxx11::basic_string<char>` which depending on compiler flags can be
111
+ // resolved to `basic_string<char>` either in `std` namespace or in
112
+ // `std::__cxx11` one (`__cxx11` is an inline namespace)
113
+ template <>
114
+ inline constexpr type_index get_type_index<std::string>() {
115
+ // hashsum for std::basic_string<char>
116
+ return type_index{4193213214807308375ULL};
117
+ }
118
+ #endif
119
+
120
+ template <typename T>
121
+ inline constexpr std::string_view get_fully_qualified_type_name() noexcept {
122
+ return static_cast<std::string_view>(
123
+ detail::fully_qualified_type_name_impl<T>());
124
+ }
125
+ } // namespace c10::util
126
+
127
+ C10_DEFINE_HASH_FOR_IDWRAPPER(c10::util::type_index)
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeList.h ADDED
@@ -0,0 +1,515 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/util/TypeTraits.h>
4
+ #include <algorithm>
5
+ #include <cstddef>
6
+ #include <tuple>
7
+ #include <type_traits>
8
+ #include <utility>
9
+
10
+ namespace c10::guts {
11
+
12
+ template <class... T>
13
+ struct false_t : std::false_type {};
14
+ template <template <class> class... T>
15
+ struct false_higher_t : std::false_type {};
16
+
17
+ namespace typelist {
18
+
19
+ /**
20
+ * Type holding a list of types for compile time type computations
21
+ */
22
+ template <class... Items>
23
+ struct typelist final {
24
+ public:
25
+ typelist() = delete; // not for instantiation
26
+ };
27
+
28
+ /**
29
+ * Returns the number of types in a typelist
30
+ * Example:
31
+ * 3 == size<typelist<int, int, double>>::value
32
+ */
33
+ template <class TypeList>
34
+ struct size final {
35
+ static_assert(
36
+ false_t<TypeList>::value,
37
+ "In typelist::size<T>, T must be typelist<...>.");
38
+ };
39
+ template <class... Types>
40
+ struct size<typelist<Types...>> final {
41
+ static constexpr size_t value = sizeof...(Types);
42
+ };
43
+
44
+ /**
45
+ * Transforms a list of types into a tuple holding these types.
46
+ * Example:
47
+ * std::tuple<int, string> == to_tuple_t<typelist<int, string>>
48
+ */
49
+ template <class TypeList>
50
+ struct to_tuple final {
51
+ static_assert(
52
+ false_t<TypeList>::value,
53
+ "In typelist::to_tuple<T>, T must be typelist<...>.");
54
+ };
55
+ template <class... Types>
56
+ struct to_tuple<typelist<Types...>> final {
57
+ using type = std::tuple<Types...>;
58
+ };
59
+ template <class TypeList>
60
+ using to_tuple_t = typename to_tuple<TypeList>::type;
61
+
62
+ /**
63
+ * Creates a typelist containing the types of a given tuple.
64
+ * Example:
65
+ * typelist<int, string> == from_tuple_t<std::tuple<int, string>>
66
+ */
67
+ template <class Tuple>
68
+ struct from_tuple final {
69
+ static_assert(
70
+ false_t<Tuple>::value,
71
+ "In typelist::from_tuple<T>, T must be std::tuple<...>.");
72
+ };
73
+ template <class... Types>
74
+ struct from_tuple<std::tuple<Types...>> final {
75
+ using type = typelist<Types...>;
76
+ };
77
+ template <class Tuple>
78
+ using from_tuple_t = typename from_tuple<Tuple>::type;
79
+
80
+ /**
81
+ * Concatenates multiple type lists.
82
+ * Example:
83
+ * typelist<int, string, int> == concat_t<typelist<int, string>,
84
+ * typelist<int>>
85
+ */
86
+ template <class... TypeLists>
87
+ struct concat final {
88
+ static_assert(
89
+ false_t<TypeLists...>::value,
90
+ "In typelist::concat<T1, ...>, the T arguments each must be typelist<...>.");
91
+ };
92
+ template <class... Head1Types, class... Head2Types, class... TailLists>
93
+ struct concat<typelist<Head1Types...>, typelist<Head2Types...>, TailLists...>
94
+ final {
95
+ using type =
96
+ typename concat<typelist<Head1Types..., Head2Types...>, TailLists...>::
97
+ type;
98
+ };
99
+ template <class... HeadTypes>
100
+ struct concat<typelist<HeadTypes...>> final {
101
+ using type = typelist<HeadTypes...>;
102
+ };
103
+ template <>
104
+ struct concat<> final {
105
+ using type = typelist<>;
106
+ };
107
+ template <class... TypeLists>
108
+ using concat_t = typename concat<TypeLists...>::type;
109
+
110
+ /**
111
+ * Filters the types in a type list by a type trait.
112
+ * Examples:
113
+ * typelist<int&, const string&&> == filter_t<std::is_reference,
114
+ * typelist<void, string, int&, bool, const string&&, int>>
115
+ */
116
+ template <template <class> class Condition, class TypeList>
117
+ struct filter final {
118
+ static_assert(
119
+ false_t<TypeList>::value,
120
+ "In typelist::filter<Condition, TypeList>, the TypeList argument must be typelist<...>.");
121
+ };
122
+ template <template <class> class Condition, class Head, class... Tail>
123
+ struct filter<Condition, typelist<Head, Tail...>> final {
124
+ static_assert(
125
+ is_type_condition<Condition>::value,
126
+ "In typelist::filter<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
127
+ using type = std::conditional_t<
128
+ Condition<Head>::value,
129
+ concat_t<
130
+ typelist<Head>,
131
+ typename filter<Condition, typelist<Tail...>>::type>,
132
+ typename filter<Condition, typelist<Tail...>>::type>;
133
+ };
134
+ template <template <class> class Condition>
135
+ struct filter<Condition, typelist<>> final {
136
+ static_assert(
137
+ is_type_condition<Condition>::value,
138
+ "In typelist::filter<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
139
+ using type = typelist<>;
140
+ };
141
+ template <template <class> class Condition, class TypeList>
142
+ using filter_t = typename filter<Condition, TypeList>::type;
143
+
144
+ /**
145
+ * Counts how many types in the list fulfill a type trait
146
+ * Examples:
147
+ * 2 == count_if<std::is_reference, typelist<void, string, int&, bool, const
148
+ * string&&, int>>
149
+ */
150
+ template <template <class> class Condition, class TypeList>
151
+ struct count_if final {
152
+ static_assert(
153
+ is_type_condition<Condition>::value,
154
+ "In typelist::count_if<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
155
+ static_assert(
156
+ is_instantiation_of<typelist, TypeList>::value,
157
+ "In typelist::count_if<Condition, TypeList>, the TypeList argument must be typelist<...>.");
158
+ // TODO Direct implementation might be faster
159
+ static constexpr size_t value = size<filter_t<Condition, TypeList>>::value;
160
+ };
161
+
162
+ /**
163
+ * Checks if a typelist contains a certain type.
164
+ * Examples:
165
+ * contains<typelist<int, string>, string> == true_type
166
+ * contains<typelist<int, string>, double> == false_type
167
+ */
168
+ namespace detail {
169
+ template <class TypeList, class Type, class Enable = void>
170
+ struct contains {};
171
+ template <class Type>
172
+ struct contains<typelist<>, Type, void> : std::false_type {};
173
+ template <class Type, class Head, class... Tail>
174
+ struct contains<
175
+ typelist<Head, Tail...>,
176
+ Type,
177
+ std::enable_if_t<std::is_same_v<Head, Type>>> : std::true_type {};
178
+ template <class Type, class Head, class... Tail>
179
+ struct contains<
180
+ typelist<Head, Tail...>,
181
+ Type,
182
+ std::enable_if_t<!std::is_same_v<Head, Type>>>
183
+ : contains<typelist<Tail...>, Type> {};
184
+ } // namespace detail
185
+ template <class TypeList, class Type>
186
+ using contains = typename detail::contains<TypeList, Type>::type;
187
+
188
+ /**
189
+ * Returns true iff the type trait is true for all types in the type list
190
+ * Examples:
191
+ * true == all<std::is_reference, typelist<int&, const float&&, const
192
+ * MyClass&>>::value false == all<std::is_reference, typelist<int&, const
193
+ * float&&, MyClass>>::value
194
+ */
195
+ template <template <class> class Condition, class TypeList>
196
+ struct all {
197
+ static_assert(
198
+ false_t<TypeList>::value,
199
+ "In typelist::all<Condition, TypeList>, the TypeList argument must be typelist<...>.");
200
+ };
201
+ template <template <class> class Condition, class... Types>
202
+ struct all<Condition, typelist<Types...>>
203
+ : std::conjunction<Condition<Types>...> {
204
+ static_assert(
205
+ is_type_condition<Condition>::value,
206
+ "In typelist::all<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
207
+ };
208
+
209
+ /**
210
+ * Returns true iff the type trait is true for any type in the type list
211
+ * Examples:
212
+ * true == true_for_any_type<std::is_reference, typelist<int, const
213
+ * float&&, const MyClass>>::value false ==
214
+ * true_for_any_type<std::is_reference, typelist<int, const float,
215
+ * MyClass>>::value
216
+ */
217
+ template <template <class> class Condition, class TypeList>
218
+ struct true_for_any_type final {
219
+ static_assert(
220
+ false_t<TypeList>::value,
221
+ "In typelist::true_for_any_type<Condition, TypeList>, the TypeList argument must be typelist<...>.");
222
+ };
223
+ template <template <class> class Condition, class... Types>
224
+ struct true_for_any_type<Condition, typelist<Types...>> final
225
+ : std::disjunction<Condition<Types>...> {
226
+ static_assert(
227
+ is_type_condition<Condition>::value,
228
+ "In typelist::true_for_any_type<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
229
+ };
230
+
231
+ /**
232
+ * Maps types of a type list using a type trait
233
+ * Example:
234
+ * typelist<int&, double&, string&> == map_t<std::add_lvalue_reference_t,
235
+ * typelist<int, double, string>>
236
+ */
237
+ template <template <class> class Mapper, class TypeList>
238
+ struct map final {
239
+ static_assert(
240
+ false_t<TypeList>::value,
241
+ "In typelist::map<Mapper, TypeList>, the TypeList argument must be typelist<...>.");
242
+ };
243
+ template <template <class> class Mapper, class... Types>
244
+ struct map<Mapper, typelist<Types...>> final {
245
+ using type = typelist<Mapper<Types>...>;
246
+ };
247
+ template <template <class> class Mapper, class TypeList>
248
+ using map_t = typename map<Mapper, TypeList>::type;
249
+
250
+ /**
251
+ * Returns the first element of a type list.
252
+ * Example:
253
+ * int == head_t<typelist<int, string>>
254
+ */
255
+ template <class TypeList>
256
+ struct head final {
257
+ static_assert(
258
+ false_t<TypeList>::value,
259
+ "In typelist::head<T>, the T argument must be typelist<...>.");
260
+ };
261
+ template <class Head, class... Tail>
262
+ struct head<typelist<Head, Tail...>> final {
263
+ using type = Head;
264
+ };
265
+ template <class TypeList>
266
+ using head_t = typename head<TypeList>::type;
267
+
268
+ /**
269
+ * Returns the first element of a type list, or the specified default if the
270
+ * type list is empty. Example: int == head_t<bool, typelist<int, string>>
271
+ * bool == head_t<bool, typelist<>>
272
+ */
273
+ template <class Default, class TypeList>
274
+ struct head_with_default final {
275
+ using type = Default;
276
+ };
277
+ template <class Default, class Head, class... Tail>
278
+ struct head_with_default<Default, typelist<Head, Tail...>> final {
279
+ using type = Head;
280
+ };
281
+ template <class Default, class TypeList>
282
+ using head_with_default_t = typename head_with_default<Default, TypeList>::type;
283
+
284
+ /**
285
+ * Returns the N-th element of a type list.
286
+ * Example:
287
+ * int == element_t<1, typelist<float, int, char>>
288
+ */
289
+
290
+ /// Base template.
291
+ template <size_t Index, class TypeList>
292
+ struct element final {
293
+ static_assert(
294
+ false_t<TypeList>::value,
295
+ "In typelist::element<T>, the T argument must be typelist<...>.");
296
+ };
297
+
298
+ /// Successful case, we have reached the zero index and can "return" the head
299
+ /// type.
300
+ template <class Head, class... Tail>
301
+ struct element<0, typelist<Head, Tail...>> {
302
+ using type = Head;
303
+ };
304
+
305
+ /// Error case, we have an index but ran out of types! It will only be selected
306
+ /// if `Ts...` is actually empty!
307
+ template <size_t Index, class... Ts>
308
+ struct element<Index, typelist<Ts...>> {
309
+ static_assert(
310
+ Index < sizeof...(Ts),
311
+ "Index is out of bounds in typelist::element");
312
+ };
313
+
314
+ /// Shave off types until we hit the <0, Head, Tail...> or <Index> case.
315
+ template <size_t Index, class Head, class... Tail>
316
+ struct element<Index, typelist<Head, Tail...>>
317
+ : element<Index - 1, typelist<Tail...>> {};
318
+
319
+ /// Convenience alias.
320
+ template <size_t Index, class TypeList>
321
+ using element_t = typename element<Index, TypeList>::type;
322
+
323
+ /**
324
+ * Returns the last element of a type list.
325
+ * Example:
326
+ * int == last_t<typelist<int, string>>
327
+ */
328
+ template <class TypeList>
329
+ struct last final {
330
+ static_assert(
331
+ false_t<TypeList>::value,
332
+ "In typelist::last<T>, the T argument must be typelist<...>.");
333
+ };
334
+ template <class Head, class... Tail>
335
+ struct last<typelist<Head, Tail...>> final {
336
+ using type = typename last<typelist<Tail...>>::type;
337
+ };
338
+ template <class Head>
339
+ struct last<typelist<Head>> final {
340
+ using type = Head;
341
+ };
342
+ template <class TypeList>
343
+ using last_t = typename last<TypeList>::type;
344
+ static_assert(std::is_same_v<int, last_t<typelist<double, float, int>>>);
345
+
346
+ /**
347
+ * Take/drop a number of arguments from a typelist.
348
+ * Example:
349
+ * typelist<int, string> == take_t<typelist<int, string, bool>, 2>
350
+ * typelist<bool> == drop_t<typelist<int, string, bool>, 2>
351
+ */
352
+ namespace detail {
353
+ template <class TypeList, size_t offset, class IndexSequence>
354
+ struct take_elements final {};
355
+
356
+ template <class TypeList, size_t offset, size_t... Indices>
357
+ struct take_elements<TypeList, offset, std::index_sequence<Indices...>> final {
358
+ using type = typelist<typename element<offset + Indices, TypeList>::type...>;
359
+ };
360
+ } // namespace detail
361
+
362
+ template <class TypeList, size_t num>
363
+ struct take final {
364
+ static_assert(
365
+ is_instantiation_of<typelist, TypeList>::value,
366
+ "In typelist::take<T, num>, the T argument must be typelist<...>.");
367
+ static_assert(
368
+ num <= size<TypeList>::value,
369
+ "Tried to typelist::take more elements than there are in the list");
370
+ using type = typename detail::
371
+ take_elements<TypeList, 0, std::make_index_sequence<num>>::type;
372
+ };
373
+ template <class TypeList, size_t num>
374
+ using take_t = typename take<TypeList, num>::type;
375
+
376
+ template <class TypeList, size_t num>
377
+ struct drop final {
378
+ static_assert(
379
+ is_instantiation_of<typelist, TypeList>::value,
380
+ "In typelist::drop<T, num>, the T argument must be typelist<...>.");
381
+ static_assert(
382
+ num <= size<TypeList>::value,
383
+ "Tried to typelist::drop more elements than there are in the list");
384
+ using type = typename detail::take_elements<
385
+ TypeList,
386
+ num,
387
+ std::make_index_sequence<size<TypeList>::value - num>>::type;
388
+ };
389
+ template <class TypeList, size_t num>
390
+ using drop_t = typename drop<TypeList, num>::type;
391
+
392
+ /**
393
+ * Like drop, but returns an empty list rather than an assertion error if `num`
394
+ * is larger than the size of the TypeList.
395
+ * Example:
396
+ * typelist<> == drop_if_nonempty_t<typelist<string, bool>, 2>
397
+ * typelist<> == drop_if_nonempty_t<typelist<int, string, bool>, 3>
398
+ */
399
+ template <class TypeList, size_t num>
400
+ struct drop_if_nonempty final {
401
+ static_assert(
402
+ is_instantiation_of<typelist, TypeList>::value,
403
+ "In typelist::drop<T, num>, the T argument must be typelist<...>.");
404
+ using type = typename detail::take_elements<
405
+ TypeList,
406
+ std::min(num, size<TypeList>::value),
407
+ std::make_index_sequence<
408
+ size<TypeList>::value - std::min(num, size<TypeList>::value)>>::type;
409
+ };
410
+ template <class TypeList, size_t num>
411
+ using drop_if_nonempty_t = typename drop_if_nonempty<TypeList, num>::type;
412
+
413
+ /**
414
+ * Reverses a typelist.
415
+ * Example:
416
+ * typelist<int, string> == reverse_t<typelist<string, int>>
417
+ */
418
+ template <class TypeList>
419
+ struct reverse final {
420
+ static_assert(
421
+ false_t<TypeList>::value,
422
+ "In typelist::reverse<T>, the T argument must be typelist<...>.");
423
+ };
424
+ template <class Head, class... Tail>
425
+ struct reverse<typelist<Head, Tail...>> final {
426
+ using type =
427
+ concat_t<typename reverse<typelist<Tail...>>::type, typelist<Head>>;
428
+ };
429
+ template <>
430
+ struct reverse<typelist<>> final {
431
+ using type = typelist<>;
432
+ };
433
+ template <class TypeList>
434
+ using reverse_t = typename reverse<TypeList>::type;
435
+
436
+ /**
437
+ * Find the index of the first type in a typelist fulfilling a type trait
438
+ * condition. Example:
439
+ *
440
+ * 2 == find_if<typelist<char, int, char&, int&>, std::is_reference>::value
441
+ */
442
+ template <class TypeList, template <class> class Condition, class Enable = void>
443
+ struct find_if final {
444
+ static_assert(
445
+ false_t<TypeList>::value,
446
+ "In typelist::find_if<TypeList, Condition>, the TypeList argument must be typelist<...>.");
447
+ };
448
+ template <template <class> class Condition>
449
+ struct find_if<typelist<>, Condition, void> final {
450
+ static_assert(
451
+ false_higher_t<Condition>::value,
452
+ "In typelist::find_if<Type/List, Condition>, didn't find any type fulfilling the Condition.");
453
+ };
454
+ template <class Head, class... Tail, template <class> class Condition>
455
+ struct find_if<
456
+ typelist<Head, Tail...>,
457
+ Condition,
458
+ std::enable_if_t<Condition<Head>::value>>
459
+ final {
460
+ static constexpr size_t value = 0;
461
+ };
462
+ template <class Head, class... Tail, template <class> class Condition>
463
+ struct find_if<
464
+ typelist<Head, Tail...>,
465
+ Condition,
466
+ std::enable_if_t<!Condition<Head>::value>>
467
+ final {
468
+ static constexpr size_t value =
469
+ 1 + find_if<typelist<Tail...>, Condition>::value;
470
+ };
471
+
472
+ /**
473
+ * Maps a list of types into a list of values.
474
+ * Examples:
475
+ * // Example 1
476
+ * auto sizes =
477
+ * map_types_to_values<typelist<int64_t, bool, uint32_t>>(
478
+ * [] (auto t) { return sizeof(decltype(t)::type); }
479
+ * );
480
+ * // sizes == std::tuple<size_t, size_t, size_t>{8, 1, 4}
481
+ *
482
+ * // Example 2
483
+ * auto shared_ptrs =
484
+ * map_types_to_values<typelist<int, double>>(
485
+ * [] (auto t) { return make_shared<typename decltype(t)::type>(); }
486
+ * );
487
+ * // shared_ptrs == std::tuple<shared_ptr<int>, shared_ptr<double>>()
488
+ */
489
+ namespace detail {
490
+ template <class T>
491
+ struct type_ final {
492
+ using type = T;
493
+ };
494
+ template <class TypeList>
495
+ struct map_types_to_values final {
496
+ static_assert(
497
+ false_t<TypeList>::value,
498
+ "In typelist::map_types_to_values<T>, the T argument must be typelist<...>.");
499
+ };
500
+ template <class... Types>
501
+ struct map_types_to_values<typelist<Types...>> final {
502
+ template <class Func>
503
+ static auto call(Func&& func) {
504
+ return std::tuple{std::forward<Func>(func)(type_<Types>())...};
505
+ }
506
+ };
507
+ } // namespace detail
508
+
509
+ template <class TypeList, class Func>
510
+ decltype(auto) map_types_to_values(Func&& func) {
511
+ return detail::map_types_to_values<TypeList>::call(std::forward<Func>(func));
512
+ }
513
+
514
+ } // namespace typelist
515
+ } // namespace c10::guts
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeSafeSignMath.h ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Macros.h>
4
+ #include <limits>
5
+ #include <type_traits>
6
+
7
+ C10_CLANG_DIAGNOSTIC_PUSH()
8
+ #if C10_CLANG_HAS_WARNING("-Wstring-conversion")
9
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wstring-conversion")
10
+ #endif
11
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
12
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
13
+ #endif
14
+
15
+ namespace c10 {
16
+
17
+ /// Returns false since we cannot have x < 0 if x is unsigned.
18
+ template <typename T>
19
+ inline constexpr bool is_negative(
20
+ const T& /*x*/,
21
+ std::true_type /*is_unsigned*/) {
22
+ return false;
23
+ }
24
+
25
+ /// Returns true if a signed variable x < 0
26
+ template <typename T>
27
+ inline constexpr bool is_negative(const T& x, std::false_type /*is_unsigned*/) {
28
+ return x < T(0);
29
+ }
30
+
31
+ /// Returns true if x < 0
32
+ /// NOTE: Will fail on an unsigned custom type
33
+ /// For the most part it's possible to fix this if
34
+ /// the custom type has a constexpr constructor.
35
+ /// However, notably, c10::Half does not :-(
36
+ template <typename T>
37
+ inline constexpr bool is_negative(const T& x) {
38
+ return is_negative(x, std::is_unsigned<T>());
39
+ }
40
+
41
+ /// Returns the sign of an unsigned variable x as 0, 1
42
+ template <typename T>
43
+ inline constexpr int signum(const T& x, std::true_type /*is_unsigned*/) {
44
+ return T(0) < x;
45
+ }
46
+
47
+ /// Returns the sign of a signed variable x as -1, 0, 1
48
+ template <typename T>
49
+ inline constexpr int signum(const T& x, std::false_type /*is_unsigned*/) {
50
+ return (T(0) < x) - (x < T(0));
51
+ }
52
+
53
+ /// Returns the sign of x as -1, 0, 1
54
+ /// NOTE: Will fail on an unsigned custom type
55
+ /// For the most part it's possible to fix this if
56
+ /// the custom type has a constexpr constructor.
57
+ /// However, notably, c10::Half does not :-(
58
+ template <typename T>
59
+ inline constexpr int signum(const T& x) {
60
+ return signum(x, std::is_unsigned<T>());
61
+ }
62
+
63
+ /// Returns true if a and b are not both negative
64
+ template <typename T, typename U>
65
+ inline constexpr bool signs_differ(const T& a, const U& b) {
66
+ return is_negative(a) != is_negative(b);
67
+ }
68
+
69
+ // Suppress sign compare warning when compiling with GCC
70
+ // as later does not account for short-circuit rule before
71
+ // raising the warning, see https://godbolt.org/z/Tr3Msnz99
72
+ #ifdef __GNUC__
73
+ #pragma GCC diagnostic push
74
+ #pragma GCC diagnostic ignored "-Wsign-compare"
75
+ #endif
76
+
77
+ /// Returns true if x is greater than the greatest value of the type Limit
78
+ template <typename Limit, typename T>
79
+ inline constexpr bool greater_than_max(const T& x) {
80
+ constexpr bool can_overflow =
81
+ std::numeric_limits<T>::digits > std::numeric_limits<Limit>::digits;
82
+ return can_overflow && x > std::numeric_limits<Limit>::max();
83
+ }
84
+
85
+ #ifdef __GNUC__
86
+ #pragma GCC diagnostic pop
87
+ #endif
88
+
89
+ /// Returns true if x < lowest(Limit). Standard comparison
90
+ template <typename Limit, typename T>
91
+ inline constexpr bool less_than_lowest(
92
+ const T& x,
93
+ std::false_type /*limit_is_unsigned*/,
94
+ std::false_type /*x_is_unsigned*/) {
95
+ return x < std::numeric_limits<Limit>::lowest();
96
+ }
97
+
98
+ /// Returns false since all the limit is signed and therefore includes
99
+ /// negative values but x cannot be negative because it is unsigned
100
+ template <typename Limit, typename T>
101
+ inline constexpr bool less_than_lowest(
102
+ const T& /*x*/,
103
+ std::false_type /*limit_is_unsigned*/,
104
+ std::true_type /*x_is_unsigned*/) {
105
+ return false;
106
+ }
107
+
108
+ /// Returns true if x < 0, where 0 is constructed from T.
109
+ /// Limit is not signed, so its lower value is zero
110
+ template <typename Limit, typename T>
111
+ inline constexpr bool less_than_lowest(
112
+ const T& x,
113
+ std::true_type /*limit_is_unsigned*/,
114
+ std::false_type /*x_is_unsigned*/) {
115
+ return x < T(0);
116
+ }
117
+
118
+ /// Returns false sign both types are unsigned
119
+ template <typename Limit, typename T>
120
+ inline constexpr bool less_than_lowest(
121
+ const T& /*x*/,
122
+ std::true_type /*limit_is_unsigned*/,
123
+ std::true_type /*x_is_unsigned*/) {
124
+ return false;
125
+ }
126
+
127
+ /// Returns true if x is less than the lowest value of type T
128
+ /// NOTE: Will fail on an unsigned custom type
129
+ /// For the most part it's possible to fix this if
130
+ /// the custom type has a constexpr constructor.
131
+ /// However, notably, c10::Half does not :
132
+ template <typename Limit, typename T>
133
+ inline constexpr bool less_than_lowest(const T& x) {
134
+ return less_than_lowest<Limit>(
135
+ x, std::is_unsigned<Limit>(), std::is_unsigned<T>());
136
+ }
137
+
138
+ } // namespace c10
139
+
140
+ C10_CLANG_DIAGNOSTIC_POP()
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/TypeTraits.h ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <functional>
4
+ #include <type_traits>
5
+
6
+ namespace c10::guts {
7
+
8
+ /**
9
+ * is_equality_comparable<T> is true_type iff the equality operator is defined
10
+ * for T.
11
+ */
12
+ template <class T, class Enable = void>
13
+ struct is_equality_comparable : std::false_type {};
14
+ template <class T>
15
+ struct is_equality_comparable<
16
+ T,
17
+ std::void_t<decltype(std::declval<T&>() == std::declval<T&>())>>
18
+ : std::true_type {};
19
+ template <class T>
20
+ using is_equality_comparable_t = typename is_equality_comparable<T>::type;
21
+
22
+ /**
23
+ * is_hashable<T> is true_type iff std::hash is defined for T
24
+ */
25
+ template <class T, class Enable = void>
26
+ struct is_hashable : std::false_type {};
27
+ template <class T>
28
+ struct is_hashable<T, std::void_t<decltype(std::hash<T>()(std::declval<T&>()))>>
29
+ : std::true_type {};
30
+ template <class T>
31
+ using is_hashable_t = typename is_hashable<T>::type;
32
+
33
+ /**
34
+ * is_function_type<T> is true_type iff T is a plain function type (i.e.
35
+ * "Result(Args...)")
36
+ */
37
+ template <class T>
38
+ struct is_function_type : std::false_type {};
39
+ template <class Result, class... Args>
40
+ struct is_function_type<Result(Args...)> : std::true_type {};
41
+ template <class T>
42
+ using is_function_type_t = typename is_function_type<T>::type;
43
+
44
+ /**
45
+ * is_instantiation_of<T, I> is true_type iff I is a template instantiation of T
46
+ * (e.g. vector<int> is an instantiation of vector) Example:
47
+ * is_instantiation_of_t<vector, vector<int>> // true
48
+ * is_instantiation_of_t<pair, pair<int, string>> // true
49
+ * is_instantiation_of_t<vector, pair<int, string>> // false
50
+ */
51
+ template <template <class...> class Template, class T>
52
+ struct is_instantiation_of : std::false_type {};
53
+ template <template <class...> class Template, class... Args>
54
+ struct is_instantiation_of<Template, Template<Args...>> : std::true_type {};
55
+ template <template <class...> class Template, class T>
56
+ using is_instantiation_of_t = typename is_instantiation_of<Template, T>::type;
57
+
58
+ namespace detail {
59
+ /**
60
+ * strip_class: helper to remove the class type from pointers to `operator()`.
61
+ */
62
+
63
+ template <typename T>
64
+ struct strip_class {};
65
+ template <typename Class, typename Result, typename... Args>
66
+ struct strip_class<Result (Class::*)(Args...)> {
67
+ using type = Result(Args...);
68
+ };
69
+ template <typename Class, typename Result, typename... Args>
70
+ struct strip_class<Result (Class::*)(Args...) const> {
71
+ using type = Result(Args...);
72
+ };
73
+ template <typename T>
74
+ using strip_class_t = typename strip_class<T>::type;
75
+ } // namespace detail
76
+
77
+ /**
78
+ * Evaluates to true_type, iff the given class is a Functor
79
+ * (i.e. has a call operator with some set of arguments)
80
+ */
81
+
82
+ template <class Functor, class Enable = void>
83
+ struct is_functor : std::false_type {};
84
+ template <class Functor>
85
+ struct is_functor<
86
+ Functor,
87
+ std::enable_if_t<is_function_type<
88
+ detail::strip_class_t<decltype(&Functor::operator())>>::value>>
89
+ : std::true_type {};
90
+
91
+ /**
92
+ * lambda_is_stateless<T> is true iff the lambda type T is stateless
93
+ * (i.e. does not have a closure).
94
+ * Example:
95
+ * auto stateless_lambda = [] (int a) {return a;};
96
+ * lambda_is_stateless<decltype(stateless_lambda)> // true
97
+ * auto stateful_lambda = [&] (int a) {return a;};
98
+ * lambda_is_stateless<decltype(stateful_lambda)> // false
99
+ */
100
+ namespace detail {
101
+ template <class LambdaType, class FuncType>
102
+ struct is_stateless_lambda__ final {
103
+ static_assert(
104
+ !std::is_same_v<LambdaType, LambdaType>,
105
+ "Base case shouldn't be hit");
106
+ };
107
+ // implementation idea: According to the C++ standard, stateless lambdas are
108
+ // convertible to function pointers
109
+ template <class LambdaType, class C, class Result, class... Args>
110
+ struct is_stateless_lambda__<LambdaType, Result (C::*)(Args...) const>
111
+ : std::is_convertible<LambdaType, Result (*)(Args...)> {};
112
+ template <class LambdaType, class C, class Result, class... Args>
113
+ struct is_stateless_lambda__<LambdaType, Result (C::*)(Args...)>
114
+ : std::is_convertible<LambdaType, Result (*)(Args...)> {};
115
+
116
+ // case where LambdaType is not even a functor
117
+ template <class LambdaType, class Enable = void>
118
+ struct is_stateless_lambda_ final : std::false_type {};
119
+ // case where LambdaType is a functor
120
+ template <class LambdaType>
121
+ struct is_stateless_lambda_<
122
+ LambdaType,
123
+ std::enable_if_t<is_functor<LambdaType>::value>>
124
+ : is_stateless_lambda__<LambdaType, decltype(&LambdaType::operator())> {};
125
+ } // namespace detail
126
+ template <class T>
127
+ using is_stateless_lambda = detail::is_stateless_lambda_<std::decay_t<T>>;
128
+
129
+ /**
130
+ * is_type_condition<C> is true_type iff C<...> is a type trait representing a
131
+ * condition (i.e. has a constexpr static bool ::value member) Example:
132
+ * is_type_condition<std::is_reference> // true
133
+ */
134
+ template <template <class> class C, class Enable = void>
135
+ struct is_type_condition : std::false_type {};
136
+ template <template <class> class C>
137
+ struct is_type_condition<
138
+ C,
139
+ std::enable_if_t<
140
+ std::is_same_v<bool, std::remove_cv_t<decltype(C<int>::value)>>>>
141
+ : std::true_type {};
142
+
143
+ /**
144
+ * is_fundamental<T> is true_type iff the lambda type T is a fundamental type
145
+ * (that is, arithmetic type, void, or nullptr_t). Example: is_fundamental<int>
146
+ * // true We define it here to resolve a MSVC bug. See
147
+ * https://github.com/pytorch/pytorch/issues/30932 for details.
148
+ */
149
+ template <class T>
150
+ struct is_fundamental : std::is_fundamental<T> {};
151
+ } // namespace c10::guts
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Unicode.h ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #if defined(_WIN32)
4
+ #include <c10/util/Exception.h>
5
+ #include <c10/util/win32-headers.h>
6
+ #include <string>
7
+ #endif
8
+
9
+ namespace c10 {
10
+ #if defined(_WIN32)
11
+ C10_API std::wstring u8u16(const std::string& str);
12
+ C10_API std::string u16u8(const std::wstring& wstr);
13
+ #endif
14
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/UniqueVoidPtr.h ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstddef>
3
+ #include <memory>
4
+ #include <utility>
5
+
6
+ #include <c10/macros/Export.h>
7
+ #include <c10/macros/Macros.h>
8
+
9
+ namespace c10 {
10
+
11
+ using DeleterFnPtr = void (*)(void*);
12
+
13
+ namespace detail {
14
+
15
+ // Does not delete anything
16
+ C10_API void deleteNothing(void*);
17
+
18
+ // A detail::UniqueVoidPtr is an owning smart pointer like unique_ptr, but
19
+ // with three major differences:
20
+ //
21
+ // 1) It is specialized to void
22
+ //
23
+ // 2) It is specialized for a function pointer deleter
24
+ // void(void* ctx); i.e., the deleter doesn't take a
25
+ // reference to the data, just to a context pointer
26
+ // (erased as void*). In fact, internally, this pointer
27
+ // is implemented as having an owning reference to
28
+ // context, and a non-owning reference to data; this is why
29
+ // you release_context(), not release() (the conventional
30
+ // API for release() wouldn't give you enough information
31
+ // to properly dispose of the object later.)
32
+ //
33
+ // 3) The deleter is guaranteed to be called when the unique
34
+ // pointer is destructed and the context is non-null; this is different
35
+ // from std::unique_ptr where the deleter is not called if the
36
+ // data pointer is null.
37
+ //
38
+ // Some of the methods have slightly different types than std::unique_ptr
39
+ // to reflect this.
40
+ //
41
+ class UniqueVoidPtr {
42
+ private:
43
+ // Lifetime tied to ctx_
44
+ void* data_;
45
+ std::unique_ptr<void, DeleterFnPtr> ctx_;
46
+
47
+ public:
48
+ UniqueVoidPtr() : data_(nullptr), ctx_(nullptr, &deleteNothing) {}
49
+ explicit UniqueVoidPtr(void* data)
50
+ : data_(data), ctx_(nullptr, &deleteNothing) {}
51
+ UniqueVoidPtr(void* data, void* ctx, DeleterFnPtr ctx_deleter)
52
+ : data_(data), ctx_(ctx, ctx_deleter ? ctx_deleter : &deleteNothing) {}
53
+ void* operator->() const {
54
+ return data_;
55
+ }
56
+ void clear() {
57
+ ctx_ = nullptr;
58
+ data_ = nullptr;
59
+ }
60
+ void* get() const {
61
+ return data_;
62
+ }
63
+
64
+ bool /* success */ unsafe_reset_data_and_ctx(void* new_data_and_ctx) {
65
+ if (C10_UNLIKELY(ctx_.get_deleter() != &deleteNothing)) {
66
+ return false;
67
+ }
68
+ // seems quicker than calling the no-op deleter when we reset
69
+ // NOLINTNEXTLINE(bugprone-unused-return-value)
70
+ ctx_.release();
71
+ ctx_.reset(new_data_and_ctx);
72
+ data_ = new_data_and_ctx;
73
+ return true;
74
+ }
75
+
76
+ void* get_context() const {
77
+ return ctx_.get();
78
+ }
79
+ void* release_context() {
80
+ return ctx_.release();
81
+ }
82
+ std::unique_ptr<void, DeleterFnPtr>&& move_context() {
83
+ return std::move(ctx_);
84
+ }
85
+ [[nodiscard]] bool compare_exchange_deleter(
86
+ DeleterFnPtr expected_deleter,
87
+ DeleterFnPtr new_deleter) {
88
+ if (get_deleter() != expected_deleter)
89
+ return false;
90
+ ctx_ = std::unique_ptr<void, DeleterFnPtr>(ctx_.release(), new_deleter);
91
+ return true;
92
+ }
93
+
94
+ template <typename T>
95
+ T* cast_context(DeleterFnPtr expected_deleter) const {
96
+ if (get_deleter() != expected_deleter)
97
+ return nullptr;
98
+ return static_cast<T*>(get_context());
99
+ }
100
+ operator bool() const {
101
+ return data_ || ctx_;
102
+ }
103
+ DeleterFnPtr get_deleter() const {
104
+ return ctx_.get_deleter();
105
+ }
106
+ };
107
+
108
+ // Note [How UniqueVoidPtr is implemented]
109
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
+ // UniqueVoidPtr solves a common problem for allocators of tensor data, which
111
+ // is that the data pointer (e.g., float*) which you are interested in, is not
112
+ // the same as the context pointer (e.g., DLManagedTensor) which you need
113
+ // to actually deallocate the data. Under a conventional deleter design, you
114
+ // have to store extra context in the deleter itself so that you can actually
115
+ // delete the right thing. Implementing this with standard C++ is somewhat
116
+ // error-prone: if you use a std::unique_ptr to manage tensors, the deleter will
117
+ // not be called if the data pointer is nullptr, which can cause a leak if the
118
+ // context pointer is non-null (and the deleter is responsible for freeing both
119
+ // the data pointer and the context pointer).
120
+ //
121
+ // So, in our reimplementation of unique_ptr, which just store the context
122
+ // directly in the unique pointer, and attach the deleter to the context
123
+ // pointer itself. In simple cases, the context pointer is just the pointer
124
+ // itself.
125
+
126
+ inline bool operator==(const UniqueVoidPtr& sp, std::nullptr_t) noexcept {
127
+ return !sp;
128
+ }
129
+ inline bool operator==(std::nullptr_t, const UniqueVoidPtr& sp) noexcept {
130
+ return !sp;
131
+ }
132
+ inline bool operator!=(const UniqueVoidPtr& sp, std::nullptr_t) noexcept {
133
+ return sp;
134
+ }
135
+ inline bool operator!=(std::nullptr_t, const UniqueVoidPtr& sp) noexcept {
136
+ return sp;
137
+ }
138
+
139
+ } // namespace detail
140
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/Unroll.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <c10/macros/Macros.h>
3
+ #include <type_traits>
4
+
5
+ // Utility to guarantee complete unrolling of a loop where the bounds are known
6
+ // at compile time. Various pragmas achieve similar effects, but are not as
7
+ // portable across compilers.
8
+
9
+ // Example: c10::ForcedUnroll<4>{}(f); is equivalent to f(0); f(1); f(2); f(3);
10
+
11
+ namespace c10 {
12
+
13
+ template <int n>
14
+ struct ForcedUnroll {
15
+ template <typename Func, typename... Args>
16
+ C10_ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
17
+ ForcedUnroll<n - 1>{}(f, args...);
18
+ f(std::integral_constant<int, n - 1>{}, args...);
19
+ }
20
+ };
21
+
22
+ template <>
23
+ struct ForcedUnroll<1> {
24
+ template <typename Func, typename... Args>
25
+ C10_ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
26
+ f(std::integral_constant<int, 0>{}, args...);
27
+ }
28
+ };
29
+
30
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/WaitCounter.h ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <chrono>
4
+ #include <memory>
5
+ #include <string_view>
6
+
7
+ #include <c10/macros/Macros.h>
8
+ #include <c10/util/ScopeExit.h>
9
+ #include <c10/util/SmallVector.h>
10
+
11
+ namespace c10::monitor {
12
+ namespace detail {
13
+ class WaitCounterImpl;
14
+
15
+ class WaitCounterBackendIf {
16
+ public:
17
+ virtual ~WaitCounterBackendIf() = default;
18
+
19
+ virtual intptr_t start(
20
+ std::chrono::steady_clock::time_point now) noexcept = 0;
21
+ virtual void stop(
22
+ std::chrono::steady_clock::time_point now,
23
+ intptr_t ctx) noexcept = 0;
24
+ };
25
+
26
+ class WaitCounterBackendFactoryIf {
27
+ public:
28
+ virtual ~WaitCounterBackendFactoryIf() = default;
29
+
30
+ // May return nullptr.
31
+ // In this case the counter will be ignored by the given backend.
32
+ virtual std::unique_ptr<WaitCounterBackendIf> create(
33
+ std::string_view key) noexcept = 0;
34
+ };
35
+
36
+ C10_API void registerWaitCounterBackend(
37
+ std::unique_ptr<WaitCounterBackendFactoryIf>);
38
+
39
+ C10_API std::vector<std::shared_ptr<WaitCounterBackendFactoryIf>>
40
+ getRegisteredWaitCounterBackends();
41
+ } // namespace detail
42
+
43
+ // A handle to a wait counter.
44
+ class C10_API WaitCounterHandle {
45
+ public:
46
+ explicit WaitCounterHandle(std::string_view key);
47
+
48
+ class WaitGuard {
49
+ public:
50
+ WaitGuard(WaitGuard&& other) noexcept
51
+ : handle_{std::exchange(other.handle_, {})},
52
+ ctxs_{std::move(other.ctxs_)} {}
53
+ WaitGuard(const WaitGuard&) = delete;
54
+ WaitGuard& operator=(const WaitGuard&) = delete;
55
+ WaitGuard& operator=(WaitGuard&&) = delete;
56
+
57
+ ~WaitGuard() {
58
+ stop();
59
+ }
60
+
61
+ void stop() {
62
+ if (auto handle = std::exchange(handle_, nullptr)) {
63
+ handle->stop(ctxs_);
64
+ }
65
+ }
66
+
67
+ private:
68
+ WaitGuard(WaitCounterHandle& handle, SmallVector<intptr_t>&& ctxs)
69
+ : handle_{&handle}, ctxs_{std::move(ctxs)} {}
70
+
71
+ friend class WaitCounterHandle;
72
+
73
+ WaitCounterHandle* handle_;
74
+ SmallVector<intptr_t> ctxs_;
75
+ };
76
+
77
+ // Starts a waiter
78
+ WaitGuard start();
79
+
80
+ private:
81
+ // Stops the waiter. Each start() call should be matched by exactly one stop()
82
+ // call.
83
+ void stop(const SmallVector<intptr_t>& ctxs);
84
+
85
+ // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
86
+ detail::WaitCounterImpl& impl_;
87
+ };
88
+ } // namespace c10::monitor
89
+
90
+ #define STATIC_WAIT_COUNTER(_key) \
91
+ []() -> ::c10::monitor::WaitCounterHandle& { \
92
+ static ::c10::monitor::WaitCounterHandle handle(#_key); \
93
+ return handle; \
94
+ }()
95
+
96
+ #define STATIC_SCOPED_WAIT_COUNTER(_name) \
97
+ auto C10_ANONYMOUS_VARIABLE(SCOPE_GUARD) = STATIC_WAIT_COUNTER(_name).start();
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/WaitCounterDynamicBackend.h ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <cstdint>
4
+ #include <string_view>
5
+
6
+ namespace c10::monitor::detail {
7
+
8
+ struct WaitCounterDynamicBackend {
9
+ void* self{nullptr};
10
+ intptr_t (*start)(void* self, int64_t nowUs){nullptr};
11
+ void (*stop)(void* self, int64_t nowUs, intptr_t ctx){nullptr};
12
+ void (*destroy)(void* self){nullptr};
13
+ };
14
+
15
+ using WaitCounterDynamicBackendInit =
16
+ void (*)(WaitCounterDynamicBackend*, const char* key, std::size_t keyLen);
17
+
18
+ // This name needs to be updated if anything in the API above is changed.
19
+ constexpr std::string_view kWaitCounterDynamicBackendInitFn =
20
+ "c10_monitor_wait_counter_dynamic_backend_init_v1";
21
+ } // namespace c10::monitor::detail
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/accumulate.h ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2004-present Facebook. All Rights Reserved.
2
+
3
+ #pragma once
4
+
5
+ #include <c10/util/Exception.h>
6
+ #include <cstdint>
7
+ #include <functional>
8
+ #include <iterator>
9
+ #include <numeric>
10
+ #include <type_traits>
11
+ #include <utility>
12
+
13
+ namespace c10 {
14
+
15
+ /// Sum of a list of integers; accumulates into the int64_t datatype
16
+ template <
17
+ typename C,
18
+ std::enable_if_t<std::is_integral_v<typename C::value_type>, int> = 0>
19
+ inline int64_t sum_integers(const C& container) {
20
+ // std::accumulate infers return type from `init` type, so if the `init` type
21
+ // is not large enough to hold the result, computation can overflow. We use
22
+ // `int64_t` here to avoid this.
23
+ return std::accumulate(
24
+ container.begin(), container.end(), static_cast<int64_t>(0));
25
+ }
26
+
27
+ /// Sum of integer elements referred to by iterators; accumulates into the
28
+ /// int64_t datatype
29
+ template <
30
+ typename Iter,
31
+ std::enable_if_t<
32
+ std::is_integral_v<typename std::iterator_traits<Iter>::value_type>,
33
+ int> = 0>
34
+ inline int64_t sum_integers(Iter begin, Iter end) {
35
+ // std::accumulate infers return type from `init` type, so if the `init` type
36
+ // is not large enough to hold the result, computation can overflow. We use
37
+ // `int64_t` here to avoid this.
38
+ return std::accumulate(begin, end, static_cast<int64_t>(0));
39
+ }
40
+
41
+ /// Product of a list of integers; accumulates into the int64_t datatype
42
+ template <
43
+ typename C,
44
+ std::enable_if_t<std::is_integral_v<typename C::value_type>, int> = 0>
45
+ inline int64_t multiply_integers(const C& container) {
46
+ // std::accumulate infers return type from `init` type, so if the `init` type
47
+ // is not large enough to hold the result, computation can overflow. We use
48
+ // `int64_t` here to avoid this.
49
+ return std::accumulate(
50
+ container.begin(),
51
+ container.end(),
52
+ static_cast<int64_t>(1),
53
+ std::multiplies<>());
54
+ }
55
+
56
+ /// Product of integer elements referred to by iterators; accumulates into the
57
+ /// int64_t datatype
58
+ template <
59
+ typename Iter,
60
+ std::enable_if_t<
61
+ std::is_integral_v<typename std::iterator_traits<Iter>::value_type>,
62
+ int> = 0>
63
+ inline int64_t multiply_integers(Iter begin, Iter end) {
64
+ // std::accumulate infers return type from `init` type, so if the `init` type
65
+ // is not large enough to hold the result, computation can overflow. We use
66
+ // `int64_t` here to avoid this.
67
+ return std::accumulate(
68
+ begin, end, static_cast<int64_t>(1), std::multiplies<>());
69
+ }
70
+
71
+ /// Return product of all dimensions starting from k
72
+ /// Returns 1 if k>=dims.size()
73
+ template <
74
+ typename C,
75
+ std::enable_if_t<std::is_integral_v<typename C::value_type>, int> = 0>
76
+ inline int64_t numelements_from_dim(const int k, const C& dims) {
77
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(k >= 0);
78
+
79
+ if (k > static_cast<int>(dims.size())) {
80
+ return 1;
81
+ } else {
82
+ auto cbegin = dims.cbegin();
83
+ std::advance(cbegin, k);
84
+ return multiply_integers(cbegin, dims.cend());
85
+ }
86
+ }
87
+
88
+ /// Product of all dims up to k (not including dims[k])
89
+ /// Throws an error if k>dims.size()
90
+ template <
91
+ typename C,
92
+ std::enable_if_t<std::is_integral_v<typename C::value_type>, int> = 0>
93
+ inline int64_t numelements_to_dim(const int k, const C& dims) {
94
+ TORCH_INTERNAL_ASSERT(0 <= k);
95
+ TORCH_INTERNAL_ASSERT((unsigned)k <= dims.size());
96
+
97
+ auto cend = dims.cbegin();
98
+ std::advance(cend, k);
99
+ return multiply_integers(dims.cbegin(), cend);
100
+ }
101
+
102
+ /// Product of all dims between k and l (including dims[k] and excluding
103
+ /// dims[l]) k and l may be supplied in either order
104
+ template <
105
+ typename C,
106
+ std::enable_if_t<std::is_integral_v<typename C::value_type>, int> = 0>
107
+ inline int64_t numelements_between_dim(int k, int l, const C& dims) {
108
+ TORCH_INTERNAL_ASSERT(0 <= k);
109
+ TORCH_INTERNAL_ASSERT(0 <= l);
110
+
111
+ if (k > l) {
112
+ std::swap(k, l);
113
+ }
114
+
115
+ TORCH_INTERNAL_ASSERT((unsigned)l < dims.size());
116
+
117
+ auto cbegin = dims.cbegin();
118
+ auto cend = dims.cbegin();
119
+ std::advance(cbegin, k);
120
+ std::advance(cend, l);
121
+ return multiply_integers(cbegin, cend);
122
+ }
123
+
124
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/bit_cast.h ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <cstring>
4
+ #include <type_traits>
5
+
6
+ #if __has_include(<bit>) && (defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L)
7
+ #include <bit>
8
+ #define C10_HAVE_STD_BIT_CAST 1
9
+ #else
10
+ #define C10_HAVE_STD_BIT_CAST 0
11
+ #endif // __has_include(<bit>) && (__cplusplus >= 202002L ||
12
+ // (defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L))
13
+
14
+ namespace c10 {
15
+
16
+ #if C10_HAVE_STD_BIT_CAST
17
+ using std::bit_cast;
18
+ #else
19
+ // Implementations of std::bit_cast() from C++ 20.
20
+ //
21
+ // This is a less sketchy version of reinterpret_cast.
22
+ //
23
+ // See https://en.cppreference.com/w/cpp/numeric/bit_cast for more
24
+ // information as well as the source of our implementations.
25
+ template <class To, class From>
26
+ std::enable_if_t<
27
+ sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> &&
28
+ std::is_trivially_copyable_v<To>,
29
+ To>
30
+ // constexpr support needs compiler magic
31
+ bit_cast(const From& src) noexcept {
32
+ static_assert(
33
+ std::is_trivially_constructible_v<To>,
34
+ "This implementation additionally requires "
35
+ "destination type to be trivially constructible");
36
+
37
+ To dst;
38
+ std::memcpy(&dst, &src, sizeof(To));
39
+ return dst;
40
+ }
41
+ #endif // C10_HAVE_STD_BIT_CAST
42
+ #undef C10_HAVE_STD_BIT_CAST
43
+
44
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/bits.h ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <c10/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * bits1x8 is an uninterpreted dtype of a tensor with 1 bit (packed to byte
10
+ * boundary), without any semantics defined.
11
+ */
12
+ struct alignas(1) bits1x8 {
13
+ using underlying = uint8_t;
14
+ uint8_t val_;
15
+ bits1x8() = default;
16
+ C10_HOST_DEVICE explicit bits1x8(uint8_t val) : val_(val) {}
17
+ };
18
+
19
+ /**
20
+ * bits2x4 is an uninterpreted dtype of a tensor with 2 bits (packed to byte
21
+ * boundary), without any semantics defined.
22
+ */
23
+ struct alignas(1) bits2x4 {
24
+ using underlying = uint8_t;
25
+ uint8_t val_;
26
+ bits2x4() = default;
27
+ C10_HOST_DEVICE explicit bits2x4(uint8_t val) : val_(val) {}
28
+ };
29
+
30
+ /**
31
+ * bits4x2 is an uninterpreted dtype of a tensor with 4 bits (packed to byte
32
+ * boundary), without any semantics defined.
33
+ */
34
+ struct alignas(1) bits4x2 {
35
+ using underlying = uint8_t;
36
+ uint8_t val_;
37
+ bits4x2() = default;
38
+ C10_HOST_DEVICE explicit bits4x2(uint8_t val) : val_(val) {}
39
+ };
40
+
41
+ /**
42
+ * bits8 is an uninterpreted dtype of a tensor with 8 bits, without any
43
+ * semantics defined.
44
+ */
45
+ struct alignas(1) bits8 {
46
+ uint8_t val_;
47
+ bits8() = default;
48
+ C10_HOST_DEVICE explicit bits8(uint8_t val) : val_(val) {}
49
+ };
50
+
51
+ /**
52
+ * bits16 is an uninterpreted dtype of a tensor with 16 bits, without any
53
+ * semantics defined.
54
+ */
55
+ struct alignas(2) bits16 {
56
+ uint16_t val_;
57
+ bits16() = default;
58
+ C10_HOST_DEVICE explicit bits16(uint16_t val) : val_(val) {}
59
+ };
60
+
61
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/complex.h ADDED
@@ -0,0 +1,668 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <complex>
4
+
5
+ #include <c10/macros/Macros.h>
6
+ #include <c10/util/Half.h>
7
+
8
+ #if defined(__CUDACC__) || defined(__HIPCC__)
9
+ #include <thrust/complex.h>
10
+ #endif
11
+
12
+ C10_CLANG_DIAGNOSTIC_PUSH()
13
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
14
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
15
+ #endif
16
+ #if C10_CLANG_HAS_WARNING("-Wfloat-conversion")
17
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wfloat-conversion")
18
+ #endif
19
+
20
+ namespace c10 {
21
+
22
+ // c10::complex is an implementation of complex numbers that aims
23
+ // to work on all devices supported by PyTorch
24
+ //
25
+ // Most of the APIs duplicates std::complex
26
+ // Reference: https://en.cppreference.com/w/cpp/numeric/complex
27
+ //
28
+ // [NOTE: Complex Operator Unification]
29
+ // Operators currently use a mix of std::complex, thrust::complex, and
30
+ // c10::complex internally. The end state is that all operators will use
31
+ // c10::complex internally. Until then, there may be some hacks to support all
32
+ // variants.
33
+ //
34
+ //
35
+ // [Note on Constructors]
36
+ //
37
+ // The APIs of constructors are mostly copied from C++ standard:
38
+ // https://en.cppreference.com/w/cpp/numeric/complex/complex
39
+ //
40
+ // Since C++14, all constructors are constexpr in std::complex
41
+ //
42
+ // There are three types of constructors:
43
+ // - initializing from real and imag:
44
+ // `constexpr complex( const T& re = T(), const T& im = T() );`
45
+ // - implicitly-declared copy constructor
46
+ // - converting constructors
47
+ //
48
+ // Converting constructors:
49
+ // - std::complex defines converting constructor between float/double/long
50
+ // double,
51
+ // while we define converting constructor between float/double.
52
+ // - For these converting constructors, upcasting is implicit, downcasting is
53
+ // explicit.
54
+ // - We also define explicit casting from std::complex/thrust::complex
55
+ // - Note that the conversion from thrust is not constexpr, because
56
+ // thrust does not define them as constexpr ????
57
+ //
58
+ //
59
+ // [Operator =]
60
+ //
61
+ // The APIs of operator = are mostly copied from C++ standard:
62
+ // https://en.cppreference.com/w/cpp/numeric/complex/operator%3D
63
+ //
64
+ // Since C++20, all operator= are constexpr. Although we are not building with
65
+ // C++20, we also obey this behavior.
66
+ //
67
+ // There are three types of assign operator:
68
+ // - Assign a real value from the same scalar type
69
+ // - In std, this is templated as complex& operator=(const T& x)
70
+ // with specialization `complex& operator=(T x)` for float/double/long
71
+ // double Since we only support float and double, on will use `complex&
72
+ // operator=(T x)`
73
+ // - Copy assignment operator and converting assignment operator
74
+ // - There is no specialization of converting assignment operators, which type
75
+ // is
76
+ // convertible is solely dependent on whether the scalar type is convertible
77
+ //
78
+ // In addition to the standard assignment, we also provide assignment operators
79
+ // with std and thrust
80
+ //
81
+ //
82
+ // [Casting operators]
83
+ //
84
+ // std::complex does not have casting operators. We define casting operators
85
+ // casting to std::complex and thrust::complex
86
+ //
87
+ //
88
+ // [Operator ""]
89
+ //
90
+ // std::complex has custom literals `i`, `if` and `il` defined in namespace
91
+ // `std::literals::complex_literals`. We define our own custom literals in the
92
+ // namespace `c10::complex_literals`. Our custom literals does not follow the
93
+ // same behavior as in std::complex, instead, we define _if, _id to construct
94
+ // float/double complex literals.
95
+ //
96
+ //
97
+ // [real() and imag()]
98
+ //
99
+ // In C++20, there are two overload of these functions, one it to return the
100
+ // real/imag, another is to set real/imag, they are both constexpr. We follow
101
+ // this design.
102
+ //
103
+ //
104
+ // [Operator +=,-=,*=,/=]
105
+ //
106
+ // Since C++20, these operators become constexpr. In our implementation, they
107
+ // are also constexpr.
108
+ //
109
+ // There are two types of such operators: operating with a real number, or
110
+ // operating with another complex number. For the operating with a real number,
111
+ // the generic template form has argument type `const T &`, while the overload
112
+ // for float/double/long double has `T`. We will follow the same type as
113
+ // float/double/long double in std.
114
+ //
115
+ // [Unary operator +-]
116
+ //
117
+ // Since C++20, they are constexpr. We also make them expr
118
+ //
119
+ // [Binary operators +-*/]
120
+ //
121
+ // Each operator has three versions (taking + as example):
122
+ // - complex + complex
123
+ // - complex + real
124
+ // - real + complex
125
+ //
126
+ // [Operator ==, !=]
127
+ //
128
+ // Each operator has three versions (taking == as example):
129
+ // - complex == complex
130
+ // - complex == real
131
+ // - real == complex
132
+ //
133
+ // Some of them are removed on C++20, but we decide to keep them
134
+ //
135
+ // [Operator <<, >>]
136
+ //
137
+ // These are implemented by casting to std::complex
138
+ //
139
+ //
140
+ //
141
+ // TODO(@zasdfgbnm): c10::complex<c10::Half> is not currently supported,
142
+ // because:
143
+ // - lots of members and functions of c10::Half are not constexpr
144
+ // - thrust::complex only support float and double
145
+
146
+ template <typename T>
147
+ struct alignas(sizeof(T) * 2) complex {
148
+ using value_type = T;
149
+
150
+ T real_ = T(0);
151
+ T imag_ = T(0);
152
+
153
+ constexpr complex() = default;
154
+ C10_HOST_DEVICE constexpr complex(const T& re, const T& im = T())
155
+ : real_(re), imag_(im) {}
156
+ template <typename U>
157
+ explicit constexpr complex(const std::complex<U>& other)
158
+ : complex(other.real(), other.imag()) {}
159
+ #if defined(__CUDACC__) || defined(__HIPCC__)
160
+ template <typename U>
161
+ explicit C10_HOST_DEVICE complex(const thrust::complex<U>& other)
162
+ : real_(other.real()), imag_(other.imag()) {}
163
+ // NOTE can not be implemented as follow due to ROCm bug:
164
+ // explicit C10_HOST_DEVICE complex(const thrust::complex<U> &other):
165
+ // complex(other.real(), other.imag()) {}
166
+ #endif
167
+
168
+ // Use SFINAE to specialize casting constructor for c10::complex<float> and
169
+ // c10::complex<double>
170
+ template <typename U = T>
171
+ C10_HOST_DEVICE explicit constexpr complex(
172
+ const std::enable_if_t<std::is_same_v<U, float>, complex<double>>& other)
173
+ : real_(other.real_), imag_(other.imag_) {}
174
+ template <typename U = T>
175
+ C10_HOST_DEVICE constexpr complex(
176
+ const std::enable_if_t<std::is_same_v<U, double>, complex<float>>& other)
177
+ : real_(other.real_), imag_(other.imag_) {}
178
+
179
+ constexpr complex<T>& operator=(T re) {
180
+ real_ = re;
181
+ imag_ = 0;
182
+ return *this;
183
+ }
184
+
185
+ constexpr complex<T>& operator+=(T re) {
186
+ real_ += re;
187
+ return *this;
188
+ }
189
+
190
+ constexpr complex<T>& operator-=(T re) {
191
+ real_ -= re;
192
+ return *this;
193
+ }
194
+
195
+ constexpr complex<T>& operator*=(T re) {
196
+ real_ *= re;
197
+ imag_ *= re;
198
+ return *this;
199
+ }
200
+
201
+ constexpr complex<T>& operator/=(T re) {
202
+ real_ /= re;
203
+ imag_ /= re;
204
+ return *this;
205
+ }
206
+
207
+ template <typename U>
208
+ constexpr complex<T>& operator=(const complex<U>& rhs) {
209
+ real_ = rhs.real();
210
+ imag_ = rhs.imag();
211
+ return *this;
212
+ }
213
+
214
+ template <typename U>
215
+ constexpr complex<T>& operator+=(const complex<U>& rhs) {
216
+ real_ += rhs.real();
217
+ imag_ += rhs.imag();
218
+ return *this;
219
+ }
220
+
221
+ template <typename U>
222
+ constexpr complex<T>& operator-=(const complex<U>& rhs) {
223
+ real_ -= rhs.real();
224
+ imag_ -= rhs.imag();
225
+ return *this;
226
+ }
227
+
228
+ template <typename U>
229
+ constexpr complex<T>& operator*=(const complex<U>& rhs) {
230
+ // (a + bi) * (c + di) = (a*c - b*d) + (a * d + b * c) i
231
+ T a = real_;
232
+ T b = imag_;
233
+ U c = rhs.real();
234
+ U d = rhs.imag();
235
+ real_ = a * c - b * d;
236
+ imag_ = a * d + b * c;
237
+ return *this;
238
+ }
239
+
240
+ #ifdef __APPLE__
241
+ #define FORCE_INLINE_APPLE __attribute__((always_inline))
242
+ #else
243
+ #define FORCE_INLINE_APPLE
244
+ #endif
245
+ template <typename U>
246
+ constexpr FORCE_INLINE_APPLE complex<T>& operator/=(const complex<U>& rhs)
247
+ __ubsan_ignore_float_divide_by_zero__ {
248
+ // (a + bi) / (c + di) = (ac + bd)/(c^2 + d^2) + (bc - ad)/(c^2 + d^2) i
249
+ // the calculation below follows numpy's complex division
250
+ T a = real_;
251
+ T b = imag_;
252
+ U c = rhs.real();
253
+ U d = rhs.imag();
254
+
255
+ #if defined(__GNUC__) && !defined(__clang__)
256
+ // std::abs is already constexpr by gcc
257
+ auto abs_c = std::abs(c);
258
+ auto abs_d = std::abs(d);
259
+ #else
260
+ auto abs_c = c < 0 ? -c : c;
261
+ auto abs_d = d < 0 ? -d : d;
262
+ #endif
263
+
264
+ if (abs_c >= abs_d) {
265
+ if (abs_c == U(0) && abs_d == U(0)) {
266
+ /* divide by zeros should yield a complex inf or nan */
267
+ real_ = a / abs_c;
268
+ imag_ = b / abs_d;
269
+ } else {
270
+ auto rat = d / c;
271
+ auto scl = U(1.0) / (c + d * rat);
272
+ real_ = (a + b * rat) * scl;
273
+ imag_ = (b - a * rat) * scl;
274
+ }
275
+ } else {
276
+ auto rat = c / d;
277
+ auto scl = U(1.0) / (d + c * rat);
278
+ real_ = (a * rat + b) * scl;
279
+ imag_ = (b * rat - a) * scl;
280
+ }
281
+ return *this;
282
+ }
283
+ #undef FORCE_INLINE_APPLE
284
+
285
+ template <typename U>
286
+ constexpr complex<T>& operator=(const std::complex<U>& rhs) {
287
+ real_ = rhs.real();
288
+ imag_ = rhs.imag();
289
+ return *this;
290
+ }
291
+
292
+ #if defined(__CUDACC__) || defined(__HIPCC__)
293
+ template <typename U>
294
+ C10_HOST_DEVICE complex<T>& operator=(const thrust::complex<U>& rhs) {
295
+ real_ = rhs.real();
296
+ imag_ = rhs.imag();
297
+ return *this;
298
+ }
299
+ #endif
300
+
301
+ template <typename U>
302
+ explicit constexpr operator std::complex<U>() const {
303
+ return std::complex<U>(std::complex<T>(real(), imag()));
304
+ }
305
+
306
+ #if defined(__CUDACC__) || defined(__HIPCC__)
307
+ template <typename U>
308
+ C10_HOST_DEVICE explicit operator thrust::complex<U>() const {
309
+ return static_cast<thrust::complex<U>>(thrust::complex<T>(real(), imag()));
310
+ }
311
+ #endif
312
+
313
+ // consistent with NumPy behavior
314
+ explicit constexpr operator bool() const {
315
+ return real() || imag();
316
+ }
317
+
318
+ C10_HOST_DEVICE constexpr T real() const {
319
+ return real_;
320
+ }
321
+ constexpr void real(T value) {
322
+ real_ = value;
323
+ }
324
+ C10_HOST_DEVICE constexpr T imag() const {
325
+ return imag_;
326
+ }
327
+ constexpr void imag(T value) {
328
+ imag_ = value;
329
+ }
330
+ };
331
+
332
+ namespace complex_literals {
333
+
334
+ constexpr complex<float> operator""_if(long double imag) {
335
+ return complex<float>(0.0f, static_cast<float>(imag));
336
+ }
337
+
338
+ constexpr complex<double> operator""_id(long double imag) {
339
+ return complex<double>(0.0, static_cast<double>(imag));
340
+ }
341
+
342
+ constexpr complex<float> operator""_if(unsigned long long imag) {
343
+ return complex<float>(0.0f, static_cast<float>(imag));
344
+ }
345
+
346
+ constexpr complex<double> operator""_id(unsigned long long imag) {
347
+ return complex<double>(0.0, static_cast<double>(imag));
348
+ }
349
+
350
+ } // namespace complex_literals
351
+
352
+ template <typename T>
353
+ constexpr complex<T> operator+(const complex<T>& val) {
354
+ return val;
355
+ }
356
+
357
+ template <typename T>
358
+ constexpr complex<T> operator-(const complex<T>& val) {
359
+ return complex<T>(-val.real(), -val.imag());
360
+ }
361
+
362
+ template <typename T>
363
+ constexpr complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs) {
364
+ complex<T> result = lhs;
365
+ return result += rhs;
366
+ }
367
+
368
+ template <typename T>
369
+ constexpr complex<T> operator+(const complex<T>& lhs, const T& rhs) {
370
+ complex<T> result = lhs;
371
+ return result += rhs;
372
+ }
373
+
374
+ template <typename T>
375
+ constexpr complex<T> operator+(const T& lhs, const complex<T>& rhs) {
376
+ return complex<T>(lhs + rhs.real(), rhs.imag());
377
+ }
378
+
379
+ template <typename T>
380
+ constexpr complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs) {
381
+ complex<T> result = lhs;
382
+ return result -= rhs;
383
+ }
384
+
385
+ template <typename T>
386
+ constexpr complex<T> operator-(const complex<T>& lhs, const T& rhs) {
387
+ complex<T> result = lhs;
388
+ return result -= rhs;
389
+ }
390
+
391
+ template <typename T>
392
+ constexpr complex<T> operator-(const T& lhs, const complex<T>& rhs) {
393
+ complex<T> result = -rhs;
394
+ return result += lhs;
395
+ }
396
+
397
+ template <typename T>
398
+ constexpr complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs) {
399
+ complex<T> result = lhs;
400
+ return result *= rhs;
401
+ }
402
+
403
+ template <typename T>
404
+ constexpr complex<T> operator*(const complex<T>& lhs, const T& rhs) {
405
+ complex<T> result = lhs;
406
+ return result *= rhs;
407
+ }
408
+
409
+ template <typename T>
410
+ constexpr complex<T> operator*(const T& lhs, const complex<T>& rhs) {
411
+ complex<T> result = rhs;
412
+ return result *= lhs;
413
+ }
414
+
415
+ template <typename T>
416
+ constexpr complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs) {
417
+ complex<T> result = lhs;
418
+ return result /= rhs;
419
+ }
420
+
421
+ template <typename T>
422
+ constexpr complex<T> operator/(const complex<T>& lhs, const T& rhs) {
423
+ complex<T> result = lhs;
424
+ return result /= rhs;
425
+ }
426
+
427
+ template <typename T>
428
+ constexpr complex<T> operator/(const T& lhs, const complex<T>& rhs) {
429
+ complex<T> result(lhs, T());
430
+ return result /= rhs;
431
+ }
432
+
433
+ // Define operators between integral scalars and c10::complex. std::complex does
434
+ // not support this when T is a floating-point number. This is useful because it
435
+ // saves a lot of "static_cast" when operate a complex and an integer. This
436
+ // makes the code both less verbose and potentially more efficient.
437
+ #define COMPLEX_INTEGER_OP_TEMPLATE_CONDITION \
438
+ typename std::enable_if_t< \
439
+ std::is_floating_point_v<fT> && std::is_integral_v<iT>, \
440
+ int> = 0
441
+
442
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
443
+ constexpr c10::complex<fT> operator+(const c10::complex<fT>& a, const iT& b) {
444
+ return a + static_cast<fT>(b);
445
+ }
446
+
447
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
448
+ constexpr c10::complex<fT> operator+(const iT& a, const c10::complex<fT>& b) {
449
+ return static_cast<fT>(a) + b;
450
+ }
451
+
452
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
453
+ constexpr c10::complex<fT> operator-(const c10::complex<fT>& a, const iT& b) {
454
+ return a - static_cast<fT>(b);
455
+ }
456
+
457
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
458
+ constexpr c10::complex<fT> operator-(const iT& a, const c10::complex<fT>& b) {
459
+ return static_cast<fT>(a) - b;
460
+ }
461
+
462
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
463
+ constexpr c10::complex<fT> operator*(const c10::complex<fT>& a, const iT& b) {
464
+ return a * static_cast<fT>(b);
465
+ }
466
+
467
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
468
+ constexpr c10::complex<fT> operator*(const iT& a, const c10::complex<fT>& b) {
469
+ return static_cast<fT>(a) * b;
470
+ }
471
+
472
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
473
+ constexpr c10::complex<fT> operator/(const c10::complex<fT>& a, const iT& b) {
474
+ return a / static_cast<fT>(b);
475
+ }
476
+
477
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
478
+ constexpr c10::complex<fT> operator/(const iT& a, const c10::complex<fT>& b) {
479
+ return static_cast<fT>(a) / b;
480
+ }
481
+
482
+ #undef COMPLEX_INTEGER_OP_TEMPLATE_CONDITION
483
+
484
+ template <typename T>
485
+ constexpr bool operator==(const complex<T>& lhs, const complex<T>& rhs) {
486
+ return (lhs.real() == rhs.real()) && (lhs.imag() == rhs.imag());
487
+ }
488
+
489
+ template <typename T>
490
+ constexpr bool operator==(const complex<T>& lhs, const T& rhs) {
491
+ return (lhs.real() == rhs) && (lhs.imag() == T());
492
+ }
493
+
494
+ template <typename T>
495
+ constexpr bool operator==(const T& lhs, const complex<T>& rhs) {
496
+ return (lhs == rhs.real()) && (T() == rhs.imag());
497
+ }
498
+
499
+ template <typename T>
500
+ constexpr bool operator!=(const complex<T>& lhs, const complex<T>& rhs) {
501
+ return !(lhs == rhs);
502
+ }
503
+
504
+ template <typename T>
505
+ constexpr bool operator!=(const complex<T>& lhs, const T& rhs) {
506
+ return !(lhs == rhs);
507
+ }
508
+
509
+ template <typename T>
510
+ constexpr bool operator!=(const T& lhs, const complex<T>& rhs) {
511
+ return !(lhs == rhs);
512
+ }
513
+
514
+ template <typename T, typename CharT, typename Traits>
515
+ std::basic_ostream<CharT, Traits>& operator<<(
516
+ std::basic_ostream<CharT, Traits>& os,
517
+ const complex<T>& x) {
518
+ return (os << static_cast<std::complex<T>>(x));
519
+ }
520
+
521
+ template <typename T, typename CharT, typename Traits>
522
+ std::basic_istream<CharT, Traits>& operator>>(
523
+ std::basic_istream<CharT, Traits>& is,
524
+ complex<T>& x) {
525
+ std::complex<T> tmp;
526
+ is >> tmp;
527
+ x = tmp;
528
+ return is;
529
+ }
530
+
531
+ } // namespace c10
532
+
533
+ // std functions
534
+ //
535
+ // The implementation of these functions also follow the design of C++20
536
+
537
+ namespace std {
538
+
539
+ template <typename T>
540
+ constexpr T real(const c10::complex<T>& z) {
541
+ return z.real();
542
+ }
543
+
544
+ template <typename T>
545
+ constexpr T imag(const c10::complex<T>& z) {
546
+ return z.imag();
547
+ }
548
+
549
+ template <typename T>
550
+ C10_HOST_DEVICE T abs(const c10::complex<T>& z) {
551
+ #if defined(__CUDACC__) || defined(__HIPCC__)
552
+ return thrust::abs(static_cast<thrust::complex<T>>(z));
553
+ #else
554
+ return std::abs(static_cast<std::complex<T>>(z));
555
+ #endif
556
+ }
557
+
558
+ #if defined(USE_ROCM)
559
+ #define ROCm_Bug(x)
560
+ #else
561
+ #define ROCm_Bug(x) x
562
+ #endif
563
+
564
+ template <typename T>
565
+ C10_HOST_DEVICE T arg(const c10::complex<T>& z) {
566
+ return ROCm_Bug(std)::atan2(std::imag(z), std::real(z));
567
+ }
568
+
569
+ #undef ROCm_Bug
570
+
571
+ template <typename T>
572
+ constexpr T norm(const c10::complex<T>& z) {
573
+ return z.real() * z.real() + z.imag() * z.imag();
574
+ }
575
+
576
+ // For std::conj, there are other versions of it:
577
+ // constexpr std::complex<float> conj( float z );
578
+ // template< class DoubleOrInteger >
579
+ // constexpr std::complex<double> conj( DoubleOrInteger z );
580
+ // constexpr std::complex<long double> conj( long double z );
581
+ // These are not implemented
582
+ // TODO(@zasdfgbnm): implement them as c10::conj
583
+ template <typename T>
584
+ constexpr c10::complex<T> conj(const c10::complex<T>& z) {
585
+ return c10::complex<T>(z.real(), -z.imag());
586
+ }
587
+
588
+ // Thrust does not have complex --> complex version of thrust::proj,
589
+ // so this function is not implemented at c10 right now.
590
+ // TODO(@zasdfgbnm): implement it by ourselves
591
+
592
+ // There is no c10 version of std::polar, because std::polar always
593
+ // returns std::complex. Use c10::polar instead;
594
+
595
+ } // namespace std
596
+
597
+ namespace c10 {
598
+
599
+ template <typename T>
600
+ C10_HOST_DEVICE complex<T> polar(const T& r, const T& theta = T()) {
601
+ #if defined(__CUDACC__) || defined(__HIPCC__)
602
+ return static_cast<complex<T>>(thrust::polar(r, theta));
603
+ #else
604
+ // std::polar() requires r >= 0, so spell out the explicit implementation to
605
+ // avoid a branch.
606
+ return complex<T>(r * std::cos(theta), r * std::sin(theta));
607
+ #endif
608
+ }
609
+
610
+ template <>
611
+ struct alignas(4) complex<Half> {
612
+ Half real_;
613
+ Half imag_;
614
+
615
+ // Constructors
616
+ complex() = default;
617
+ // Half constructor is not constexpr so the following constructor can't
618
+ // be constexpr
619
+ C10_HOST_DEVICE explicit inline complex(const Half& real, const Half& imag)
620
+ : real_(real), imag_(imag) {}
621
+ C10_HOST_DEVICE inline complex(const c10::complex<float>& value)
622
+ : real_(value.real()), imag_(value.imag()) {}
623
+
624
+ // Conversion operator
625
+ inline C10_HOST_DEVICE operator c10::complex<float>() const {
626
+ return {real_, imag_};
627
+ }
628
+
629
+ constexpr C10_HOST_DEVICE Half real() const {
630
+ return real_;
631
+ }
632
+ constexpr C10_HOST_DEVICE Half imag() const {
633
+ return imag_;
634
+ }
635
+
636
+ C10_HOST_DEVICE complex<Half>& operator+=(const complex<Half>& other) {
637
+ real_ = static_cast<float>(real_) + static_cast<float>(other.real_);
638
+ imag_ = static_cast<float>(imag_) + static_cast<float>(other.imag_);
639
+ return *this;
640
+ }
641
+
642
+ C10_HOST_DEVICE complex<Half>& operator-=(const complex<Half>& other) {
643
+ real_ = static_cast<float>(real_) - static_cast<float>(other.real_);
644
+ imag_ = static_cast<float>(imag_) - static_cast<float>(other.imag_);
645
+ return *this;
646
+ }
647
+
648
+ C10_HOST_DEVICE complex<Half>& operator*=(const complex<Half>& other) {
649
+ auto a = static_cast<float>(real_);
650
+ auto b = static_cast<float>(imag_);
651
+ auto c = static_cast<float>(other.real());
652
+ auto d = static_cast<float>(other.imag());
653
+ real_ = a * c - b * d;
654
+ imag_ = a * d + b * c;
655
+ return *this;
656
+ }
657
+ };
658
+
659
+ } // namespace c10
660
+
661
+ C10_CLANG_DIAGNOSTIC_POP()
662
+
663
+ #define C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H
664
+ // math functions are included in a separate file
665
+ #include <c10/util/complex_math.h> // IWYU pragma: keep
666
+ // utilities for complex types
667
+ #include <c10/util/complex_utils.h> // IWYU pragma: keep
668
+ #undef C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/complex_math.h ADDED
@@ -0,0 +1,406 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H)
2
+ #error \
3
+ "c10/util/complex_math.h is not meant to be individually included. Include c10/util/complex.h instead."
4
+ #endif
5
+
6
+ namespace c10_complex_math {
7
+
8
+ // Exponential functions
9
+
10
+ template <typename T>
11
+ C10_HOST_DEVICE inline c10::complex<T> exp(const c10::complex<T>& x) {
12
+ #if defined(__CUDACC__) || defined(__HIPCC__)
13
+ return static_cast<c10::complex<T>>(
14
+ thrust::exp(static_cast<thrust::complex<T>>(x)));
15
+ #else
16
+ return static_cast<c10::complex<T>>(
17
+ std::exp(static_cast<std::complex<T>>(x)));
18
+ #endif
19
+ }
20
+
21
+ template <typename T>
22
+ C10_HOST_DEVICE inline c10::complex<T> log(const c10::complex<T>& x) {
23
+ #if defined(__CUDACC__) || defined(__HIPCC__)
24
+ return static_cast<c10::complex<T>>(
25
+ thrust::log(static_cast<thrust::complex<T>>(x)));
26
+ #else
27
+ return static_cast<c10::complex<T>>(
28
+ std::log(static_cast<std::complex<T>>(x)));
29
+ #endif
30
+ }
31
+
32
+ template <typename T>
33
+ C10_HOST_DEVICE inline c10::complex<T> log10(const c10::complex<T>& x) {
34
+ #if defined(__CUDACC__) || defined(__HIPCC__)
35
+ return static_cast<c10::complex<T>>(
36
+ thrust::log10(static_cast<thrust::complex<T>>(x)));
37
+ #else
38
+ return static_cast<c10::complex<T>>(
39
+ std::log10(static_cast<std::complex<T>>(x)));
40
+ #endif
41
+ }
42
+
43
+ template <typename T>
44
+ C10_HOST_DEVICE inline c10::complex<T> log2(const c10::complex<T>& x) {
45
+ const c10::complex<T> log2 = c10::complex<T>(::log(2.0), 0.0);
46
+ return c10_complex_math::log(x) / log2;
47
+ }
48
+
49
+ // Power functions
50
+ //
51
+ #if defined(_LIBCPP_VERSION) || \
52
+ (defined(__GLIBCXX__) && !defined(_GLIBCXX11_USE_C99_COMPLEX))
53
+ namespace _detail {
54
+ C10_API c10::complex<float> sqrt(const c10::complex<float>& in);
55
+ C10_API c10::complex<double> sqrt(const c10::complex<double>& in);
56
+ C10_API c10::complex<float> acos(const c10::complex<float>& in);
57
+ C10_API c10::complex<double> acos(const c10::complex<double>& in);
58
+ } // namespace _detail
59
+ #endif
60
+
61
+ template <typename T>
62
+ C10_HOST_DEVICE inline c10::complex<T> sqrt(const c10::complex<T>& x) {
63
+ #if defined(__CUDACC__) || defined(__HIPCC__)
64
+ return static_cast<c10::complex<T>>(
65
+ thrust::sqrt(static_cast<thrust::complex<T>>(x)));
66
+ #elif !( \
67
+ defined(_LIBCPP_VERSION) || \
68
+ (defined(__GLIBCXX__) && !defined(_GLIBCXX11_USE_C99_COMPLEX)))
69
+ return static_cast<c10::complex<T>>(
70
+ std::sqrt(static_cast<std::complex<T>>(x)));
71
+ #else
72
+ return _detail::sqrt(x);
73
+ #endif
74
+ }
75
+
76
+ template <typename T>
77
+ C10_HOST_DEVICE inline c10::complex<T> pow(
78
+ const c10::complex<T>& x,
79
+ const c10::complex<T>& y) {
80
+ #if defined(__CUDACC__) || defined(__HIPCC__)
81
+ return static_cast<c10::complex<T>>(thrust::pow(
82
+ static_cast<thrust::complex<T>>(x), static_cast<thrust::complex<T>>(y)));
83
+ #else
84
+ return static_cast<c10::complex<T>>(std::pow(
85
+ static_cast<std::complex<T>>(x), static_cast<std::complex<T>>(y)));
86
+ #endif
87
+ }
88
+
89
+ template <typename T>
90
+ C10_HOST_DEVICE inline c10::complex<T> pow(
91
+ const c10::complex<T>& x,
92
+ const T& y) {
93
+ #if defined(__CUDACC__) || defined(__HIPCC__)
94
+ return static_cast<c10::complex<T>>(
95
+ thrust::pow(static_cast<thrust::complex<T>>(x), y));
96
+ #else
97
+ return static_cast<c10::complex<T>>(
98
+ std::pow(static_cast<std::complex<T>>(x), y));
99
+ #endif
100
+ }
101
+
102
+ template <typename T>
103
+ C10_HOST_DEVICE inline c10::complex<T> pow(
104
+ const T& x,
105
+ const c10::complex<T>& y) {
106
+ #if defined(__CUDACC__) || defined(__HIPCC__)
107
+ return static_cast<c10::complex<T>>(
108
+ thrust::pow(x, static_cast<thrust::complex<T>>(y)));
109
+ #else
110
+ return static_cast<c10::complex<T>>(
111
+ std::pow(x, static_cast<std::complex<T>>(y)));
112
+ #endif
113
+ }
114
+
115
+ template <typename T, typename U>
116
+ C10_HOST_DEVICE inline c10::complex<decltype(T() * U())> pow(
117
+ const c10::complex<T>& x,
118
+ const c10::complex<U>& y) {
119
+ #if defined(__CUDACC__) || defined(__HIPCC__)
120
+ return static_cast<c10::complex<T>>(thrust::pow(
121
+ static_cast<thrust::complex<T>>(x), static_cast<thrust::complex<T>>(y)));
122
+ #else
123
+ return static_cast<c10::complex<T>>(std::pow(
124
+ static_cast<std::complex<T>>(x), static_cast<std::complex<T>>(y)));
125
+ #endif
126
+ }
127
+
128
+ template <typename T, typename U>
129
+ C10_HOST_DEVICE inline c10::complex<decltype(T() * U())> pow(
130
+ const c10::complex<T>& x,
131
+ const U& y) {
132
+ #if defined(__CUDACC__) || defined(__HIPCC__)
133
+ return static_cast<c10::complex<T>>(
134
+ thrust::pow(static_cast<thrust::complex<T>>(x), y));
135
+ #else
136
+ return static_cast<c10::complex<T>>(
137
+ std::pow(static_cast<std::complex<T>>(x), y));
138
+ #endif
139
+ }
140
+
141
+ template <typename T, typename U>
142
+ C10_HOST_DEVICE inline c10::complex<decltype(T() * U())> pow(
143
+ const T& x,
144
+ const c10::complex<U>& y) {
145
+ #if defined(__CUDACC__) || defined(__HIPCC__)
146
+ return static_cast<c10::complex<T>>(
147
+ thrust::pow(x, static_cast<thrust::complex<T>>(y)));
148
+ #else
149
+ return static_cast<c10::complex<T>>(
150
+ std::pow(x, static_cast<std::complex<T>>(y)));
151
+ #endif
152
+ }
153
+
154
+ // Trigonometric functions
155
+
156
+ template <typename T>
157
+ C10_HOST_DEVICE inline c10::complex<T> sin(const c10::complex<T>& x) {
158
+ #if defined(__CUDACC__) || defined(__HIPCC__)
159
+ return static_cast<c10::complex<T>>(
160
+ thrust::sin(static_cast<thrust::complex<T>>(x)));
161
+ #else
162
+ return static_cast<c10::complex<T>>(
163
+ std::sin(static_cast<std::complex<T>>(x)));
164
+ #endif
165
+ }
166
+
167
+ template <typename T>
168
+ C10_HOST_DEVICE inline c10::complex<T> cos(const c10::complex<T>& x) {
169
+ #if defined(__CUDACC__) || defined(__HIPCC__)
170
+ return static_cast<c10::complex<T>>(
171
+ thrust::cos(static_cast<thrust::complex<T>>(x)));
172
+ #else
173
+ return static_cast<c10::complex<T>>(
174
+ std::cos(static_cast<std::complex<T>>(x)));
175
+ #endif
176
+ }
177
+
178
+ template <typename T>
179
+ C10_HOST_DEVICE inline c10::complex<T> tan(const c10::complex<T>& x) {
180
+ #if defined(__CUDACC__) || defined(__HIPCC__)
181
+ return static_cast<c10::complex<T>>(
182
+ thrust::tan(static_cast<thrust::complex<T>>(x)));
183
+ #else
184
+ return static_cast<c10::complex<T>>(
185
+ std::tan(static_cast<std::complex<T>>(x)));
186
+ #endif
187
+ }
188
+
189
+ template <typename T>
190
+ C10_HOST_DEVICE inline c10::complex<T> asin(const c10::complex<T>& x) {
191
+ #if defined(__CUDACC__) || defined(__HIPCC__)
192
+ return static_cast<c10::complex<T>>(
193
+ thrust::asin(static_cast<thrust::complex<T>>(x)));
194
+ #else
195
+ return static_cast<c10::complex<T>>(
196
+ std::asin(static_cast<std::complex<T>>(x)));
197
+ #endif
198
+ }
199
+
200
+ template <typename T>
201
+ C10_HOST_DEVICE inline c10::complex<T> acos(const c10::complex<T>& x) {
202
+ #if defined(__CUDACC__) || defined(__HIPCC__)
203
+ return static_cast<c10::complex<T>>(
204
+ thrust::acos(static_cast<thrust::complex<T>>(x)));
205
+ #elif !defined(_LIBCPP_VERSION)
206
+ return static_cast<c10::complex<T>>(
207
+ std::acos(static_cast<std::complex<T>>(x)));
208
+ #else
209
+ return _detail::acos(x);
210
+ #endif
211
+ }
212
+
213
+ template <typename T>
214
+ C10_HOST_DEVICE inline c10::complex<T> atan(const c10::complex<T>& x) {
215
+ #if defined(__CUDACC__) || defined(__HIPCC__)
216
+ return static_cast<c10::complex<T>>(
217
+ thrust::atan(static_cast<thrust::complex<T>>(x)));
218
+ #else
219
+ return static_cast<c10::complex<T>>(
220
+ std::atan(static_cast<std::complex<T>>(x)));
221
+ #endif
222
+ }
223
+
224
+ // Hyperbolic functions
225
+
226
+ template <typename T>
227
+ C10_HOST_DEVICE inline c10::complex<T> sinh(const c10::complex<T>& x) {
228
+ #if defined(__CUDACC__) || defined(__HIPCC__)
229
+ return static_cast<c10::complex<T>>(
230
+ thrust::sinh(static_cast<thrust::complex<T>>(x)));
231
+ #else
232
+ return static_cast<c10::complex<T>>(
233
+ std::sinh(static_cast<std::complex<T>>(x)));
234
+ #endif
235
+ }
236
+
237
+ template <typename T>
238
+ C10_HOST_DEVICE inline c10::complex<T> cosh(const c10::complex<T>& x) {
239
+ #if defined(__CUDACC__) || defined(__HIPCC__)
240
+ return static_cast<c10::complex<T>>(
241
+ thrust::cosh(static_cast<thrust::complex<T>>(x)));
242
+ #else
243
+ return static_cast<c10::complex<T>>(
244
+ std::cosh(static_cast<std::complex<T>>(x)));
245
+ #endif
246
+ }
247
+
248
+ template <typename T>
249
+ C10_HOST_DEVICE inline c10::complex<T> tanh(const c10::complex<T>& x) {
250
+ #if defined(__CUDACC__) || defined(__HIPCC__)
251
+ return static_cast<c10::complex<T>>(
252
+ thrust::tanh(static_cast<thrust::complex<T>>(x)));
253
+ #else
254
+ return static_cast<c10::complex<T>>(
255
+ std::tanh(static_cast<std::complex<T>>(x)));
256
+ #endif
257
+ }
258
+
259
+ template <typename T>
260
+ C10_HOST_DEVICE inline c10::complex<T> asinh(const c10::complex<T>& x) {
261
+ #if defined(__CUDACC__) || defined(__HIPCC__)
262
+ return static_cast<c10::complex<T>>(
263
+ thrust::asinh(static_cast<thrust::complex<T>>(x)));
264
+ #else
265
+ return static_cast<c10::complex<T>>(
266
+ std::asinh(static_cast<std::complex<T>>(x)));
267
+ #endif
268
+ }
269
+
270
+ template <typename T>
271
+ C10_HOST_DEVICE inline c10::complex<T> acosh(const c10::complex<T>& x) {
272
+ #if defined(__CUDACC__) || defined(__HIPCC__)
273
+ return static_cast<c10::complex<T>>(
274
+ thrust::acosh(static_cast<thrust::complex<T>>(x)));
275
+ #else
276
+ return static_cast<c10::complex<T>>(
277
+ std::acosh(static_cast<std::complex<T>>(x)));
278
+ #endif
279
+ }
280
+
281
+ template <typename T>
282
+ C10_HOST_DEVICE inline c10::complex<T> atanh(const c10::complex<T>& x) {
283
+ #if defined(__CUDACC__) || defined(__HIPCC__)
284
+ return static_cast<c10::complex<T>>(
285
+ thrust::atanh(static_cast<thrust::complex<T>>(x)));
286
+ #else
287
+ return static_cast<c10::complex<T>>(
288
+ std::atanh(static_cast<std::complex<T>>(x)));
289
+ #endif
290
+ }
291
+
292
+ template <typename T>
293
+ C10_HOST_DEVICE inline c10::complex<T> log1p(const c10::complex<T>& z) {
294
+ #if defined(__APPLE__) || defined(__MACOSX) || defined(__CUDACC__) || \
295
+ defined(__HIPCC__)
296
+ // For Mac, the new implementation yielded a high relative error. Falling back
297
+ // to the old version for now.
298
+ // See https://github.com/numpy/numpy/pull/22611#issuecomment-1667945354
299
+ // For CUDA we also use this one, as thrust::log(thrust::complex) takes
300
+ // *forever* to compile
301
+
302
+ // log1p(z) = log(1 + z)
303
+ // Let's define 1 + z = r * e ^ (i * a), then we have
304
+ // log(r * e ^ (i * a)) = log(r) + i * a
305
+ // With z = x + iy, the term r can be written as
306
+ // r = ((1 + x) ^ 2 + y ^ 2) ^ 0.5
307
+ // = (1 + x ^ 2 + 2 * x + y ^ 2) ^ 0.5
308
+ // So, log(r) is
309
+ // log(r) = 0.5 * log(1 + x ^ 2 + 2 * x + y ^ 2)
310
+ // = 0.5 * log1p(x * (x + 2) + y ^ 2)
311
+ // we need to use the expression only on certain condition to avoid overflow
312
+ // and underflow from `(x * (x + 2) + y ^ 2)`
313
+ T x = z.real();
314
+ T y = z.imag();
315
+ T zabs = std::abs(z);
316
+ T theta = std::atan2(y, x + T(1));
317
+ if (zabs < 0.5) {
318
+ T r = x * (T(2) + x) + y * y;
319
+ if (r == 0) { // handle underflow
320
+ return {x, theta};
321
+ }
322
+ return {T(0.5) * std::log1p(r), theta};
323
+ } else {
324
+ T z0 = std::hypot(x + 1, y);
325
+ return {std::log(z0), theta};
326
+ }
327
+ #else
328
+ // CPU path
329
+ // Based on https://github.com/numpy/numpy/pull/22611#issuecomment-1667945354
330
+ c10::complex<T> u = z + T(1);
331
+ if (u == T(1)) {
332
+ return z;
333
+ } else {
334
+ auto log_u = log(u);
335
+ if (u - T(1) == z) {
336
+ return log_u;
337
+ }
338
+ return log_u * (z / (u - T(1)));
339
+ }
340
+ #endif
341
+ }
342
+
343
+ template <typename T>
344
+ C10_HOST_DEVICE inline c10::complex<T> expm1(const c10::complex<T>& z) {
345
+ // expm1(z) = exp(z) - 1
346
+ // Define z = x + i * y
347
+ // f = e ^ (x + i * y) - 1
348
+ // = e ^ x * e ^ (i * y) - 1
349
+ // = (e ^ x * cos(y) - 1) + i * (e ^ x * sin(y))
350
+ // = (e ^ x - 1) * cos(y) - (1 - cos(y)) + i * e ^ x * sin(y)
351
+ // = expm1(x) * cos(y) - 2 * sin(y / 2) ^ 2 + i * e ^ x * sin(y)
352
+ T x = z.real();
353
+ T y = z.imag();
354
+ T a = std::sin(y / 2);
355
+ T er = std::expm1(x) * std::cos(y) - T(2) * a * a;
356
+ T ei = std::exp(x) * std::sin(y);
357
+ return {er, ei};
358
+ }
359
+
360
+ } // namespace c10_complex_math
361
+
362
+ using c10_complex_math::acos;
363
+ using c10_complex_math::acosh;
364
+ using c10_complex_math::asin;
365
+ using c10_complex_math::asinh;
366
+ using c10_complex_math::atan;
367
+ using c10_complex_math::atanh;
368
+ using c10_complex_math::cos;
369
+ using c10_complex_math::cosh;
370
+ using c10_complex_math::exp;
371
+ using c10_complex_math::expm1;
372
+ using c10_complex_math::log;
373
+ using c10_complex_math::log10;
374
+ using c10_complex_math::log1p;
375
+ using c10_complex_math::log2;
376
+ using c10_complex_math::pow;
377
+ using c10_complex_math::sin;
378
+ using c10_complex_math::sinh;
379
+ using c10_complex_math::sqrt;
380
+ using c10_complex_math::tan;
381
+ using c10_complex_math::tanh;
382
+
383
+ namespace std {
384
+
385
+ using c10_complex_math::acos;
386
+ using c10_complex_math::acosh;
387
+ using c10_complex_math::asin;
388
+ using c10_complex_math::asinh;
389
+ using c10_complex_math::atan;
390
+ using c10_complex_math::atanh;
391
+ using c10_complex_math::cos;
392
+ using c10_complex_math::cosh;
393
+ using c10_complex_math::exp;
394
+ using c10_complex_math::expm1;
395
+ using c10_complex_math::log;
396
+ using c10_complex_math::log10;
397
+ using c10_complex_math::log1p;
398
+ using c10_complex_math::log2;
399
+ using c10_complex_math::pow;
400
+ using c10_complex_math::sin;
401
+ using c10_complex_math::sinh;
402
+ using c10_complex_math::sqrt;
403
+ using c10_complex_math::tan;
404
+ using c10_complex_math::tanh;
405
+
406
+ } // namespace std
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/complex_utils.h ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H)
2
+ #error \
3
+ "c10/util/complex_utils.h is not meant to be individually included. Include c10/util/complex.h instead."
4
+ #endif
5
+
6
+ #include <limits>
7
+
8
+ namespace c10 {
9
+
10
+ template <typename T>
11
+ struct is_complex : public std::false_type {};
12
+
13
+ template <typename T>
14
+ struct is_complex<std::complex<T>> : public std::true_type {};
15
+
16
+ template <typename T>
17
+ struct is_complex<c10::complex<T>> : public std::true_type {};
18
+
19
+ // Extract double from std::complex<double>; is identity otherwise
20
+ // TODO: Write in more idiomatic C++17
21
+ template <typename T>
22
+ struct scalar_value_type {
23
+ using type = T;
24
+ };
25
+ template <typename T>
26
+ struct scalar_value_type<std::complex<T>> {
27
+ using type = T;
28
+ };
29
+ template <typename T>
30
+ struct scalar_value_type<c10::complex<T>> {
31
+ using type = T;
32
+ };
33
+
34
+ } // namespace c10
35
+
36
+ namespace std {
37
+
38
+ template <typename T>
39
+ class numeric_limits<c10::complex<T>> : public numeric_limits<T> {};
40
+
41
+ template <typename T>
42
+ bool isnan(const c10::complex<T>& v) {
43
+ return std::isnan(v.real()) || std::isnan(v.imag());
44
+ }
45
+
46
+ } // namespace std
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/copysign.h ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/util/BFloat16.h>
4
+ #include <c10/util/Half.h>
5
+
6
+ namespace c10 {
7
+
8
+ // Note: Explicit implementation of copysign for Half and BFloat16
9
+ // is needed to workaround g++-7/8 crash on aarch64, but also makes
10
+ // copysign faster for the half-precision types
11
+ template <typename T, typename U>
12
+ inline auto copysign(const T& a, const U& b) {
13
+ return std::copysign(a, b);
14
+ }
15
+
16
+ // Implement copysign for half precision floats using bit ops
17
+ // Sign is the most significant bit for both half and bfloat16 types
18
+ inline c10::Half copysign(c10::Half a, c10::Half b) {
19
+ return c10::Half((a.x & 0x7fff) | (b.x & 0x8000), c10::Half::from_bits());
20
+ }
21
+
22
+ inline c10::BFloat16 copysign(c10::BFloat16 a, c10::BFloat16 b) {
23
+ return c10::BFloat16(
24
+ (a.x & 0x7fff) | (b.x & 0x8000), c10::BFloat16::from_bits());
25
+ }
26
+
27
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/env.h ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Export.h>
4
+ #include <optional>
5
+ #include <string>
6
+
7
+ namespace c10::utils {
8
+
9
+ // Set an environment variable.
10
+ C10_API void set_env(
11
+ const char* name,
12
+ const char* value,
13
+ bool overwrite = true);
14
+
15
+ // Checks an environment variable is set.
16
+ C10_API bool has_env(const char* name) noexcept;
17
+
18
+ // Reads an environment variable and returns
19
+ // - std::optional<true>, if set equal to "1"
20
+ // - std::optional<false>, if set equal to "0"
21
+ // - nullopt, otherwise
22
+ //
23
+ // NB:
24
+ // Issues a warning if the value of the environment variable is not 0 or 1.
25
+ C10_API std::optional<bool> check_env(const char* name);
26
+
27
+ // Reads the value of an environment variable if it is set.
28
+ // However, check_env should be used if the value is assumed to be a flag.
29
+ C10_API std::optional<std::string> get_env(const char* name) noexcept;
30
+
31
+ } // namespace c10::utils
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/error.h ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Export.h>
4
+ #include <string>
5
+
6
+ namespace c10::utils {
7
+
8
+ // Get an error string in the thread-safe way.
9
+ C10_API std::string str_error(int errnum);
10
+
11
+ } // namespace c10::utils
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/flat_hash_map.h ADDED
@@ -0,0 +1,2102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Taken from
2
+ // https://github.com/skarupke/flat_hash_map/blob/2c4687431f978f02a3780e24b8b701d22aa32d9c/flat_hash_map.hpp
3
+ // with fixes applied:
4
+ // - https://github.com/skarupke/flat_hash_map/pull/25
5
+ // - https://github.com/skarupke/flat_hash_map/pull/26
6
+ // - replace size_t with uint64_t to fix it for 32bit
7
+ // - add "GCC diagnostic" pragma to ignore -Wshadow
8
+ // - make sherwood_v3_table::convertible_to_iterator public because GCC5 seems
9
+ // to have issues with it otherwise
10
+ // - fix compiler warnings in operator templated_iterator<const value_type>
11
+ // - make use of 'if constexpr' and eliminate AssignIfTrue template
12
+
13
+ // Copyright Malte Skarupke 2017.
14
+ // Distributed under the Boost Software License, Version 1.0.
15
+ // (See http://www.boost.org/LICENSE_1_0.txt)
16
+
17
+ #pragma once
18
+
19
+ #include <c10/macros/Macros.h>
20
+ #include <algorithm>
21
+ #include <cmath>
22
+ #include <cstddef>
23
+ #include <cstdint>
24
+ #include <functional>
25
+ #include <iterator>
26
+ #include <stdexcept>
27
+ #include <type_traits>
28
+ #include <utility>
29
+
30
+ C10_CLANG_DIAGNOSTIC_PUSH()
31
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
32
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
33
+ #endif
34
+
35
+ #if defined(_MSC_VER) && !defined(__clang__)
36
+ #pragma warning(push)
37
+ #pragma warning(disable : 4624) // destructor was implicitly defined as deleted
38
+ #endif
39
+
40
+ #ifdef _MSC_VER
41
+ #define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
42
+ #else
43
+ #define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
44
+ #endif
45
+
46
+ namespace ska {
47
+ struct prime_number_hash_policy;
48
+ struct power_of_two_hash_policy;
49
+ struct fibonacci_hash_policy;
50
+
51
+ namespace detailv3 {
52
+ template <typename Result, typename Functor>
53
+ struct functor_storage : Functor {
54
+ functor_storage() = default;
55
+ functor_storage(const Functor& functor) : Functor(functor) {}
56
+ template <typename... Args>
57
+ Result operator()(Args&&... args) {
58
+ return static_cast<Functor&>(*this)(std::forward<Args>(args)...);
59
+ }
60
+ template <typename... Args>
61
+ Result operator()(Args&&... args) const {
62
+ return static_cast<const Functor&>(*this)(std::forward<Args>(args)...);
63
+ }
64
+ };
65
+ template <typename Result, typename... Args>
66
+ struct functor_storage<Result, Result (*)(Args...)> {
67
+ typedef Result (*function_ptr)(Args...);
68
+ function_ptr function;
69
+ functor_storage(function_ptr function) : function(function) {}
70
+ Result operator()(Args... args) const {
71
+ return function(std::forward<Args>(args)...);
72
+ }
73
+ operator function_ptr&() {
74
+ return function;
75
+ }
76
+ operator const function_ptr&() {
77
+ return function;
78
+ }
79
+ };
80
+ template <typename key_type, typename value_type, typename hasher>
81
+ struct KeyOrValueHasher : functor_storage<uint64_t, hasher> {
82
+ typedef functor_storage<uint64_t, hasher> hasher_storage;
83
+ KeyOrValueHasher() = default;
84
+ KeyOrValueHasher(const hasher& hash) : hasher_storage(hash) {}
85
+ uint64_t operator()(const key_type& key) {
86
+ return static_cast<hasher_storage&>(*this)(key);
87
+ }
88
+ uint64_t operator()(const key_type& key) const {
89
+ return static_cast<const hasher_storage&>(*this)(key);
90
+ }
91
+ uint64_t operator()(const value_type& value) {
92
+ return static_cast<hasher_storage&>(*this)(value.first);
93
+ }
94
+ uint64_t operator()(const value_type& value) const {
95
+ return static_cast<const hasher_storage&>(*this)(value.first);
96
+ }
97
+ template <typename F, typename S>
98
+ uint64_t operator()(const std::pair<F, S>& value) {
99
+ return static_cast<hasher_storage&>(*this)(value.first);
100
+ }
101
+ template <typename F, typename S>
102
+ uint64_t operator()(const std::pair<F, S>& value) const {
103
+ return static_cast<const hasher_storage&>(*this)(value.first);
104
+ }
105
+ };
106
+ template <typename key_type, typename value_type, typename key_equal>
107
+ struct KeyOrValueEquality : functor_storage<bool, key_equal> {
108
+ typedef functor_storage<bool, key_equal> equality_storage;
109
+ KeyOrValueEquality() = default;
110
+ KeyOrValueEquality(const key_equal& equality) : equality_storage(equality) {}
111
+ bool operator()(const key_type& lhs, const key_type& rhs) {
112
+ return static_cast<equality_storage&>(*this)(lhs, rhs);
113
+ }
114
+ bool operator()(const key_type& lhs, const value_type& rhs) {
115
+ return static_cast<equality_storage&>(*this)(lhs, rhs.first);
116
+ }
117
+ bool operator()(const value_type& lhs, const key_type& rhs) {
118
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs);
119
+ }
120
+ bool operator()(const value_type& lhs, const value_type& rhs) {
121
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
122
+ }
123
+ template <typename F, typename S>
124
+ bool operator()(const key_type& lhs, const std::pair<F, S>& rhs) {
125
+ return static_cast<equality_storage&>(*this)(lhs, rhs.first);
126
+ }
127
+ template <typename F, typename S>
128
+ bool operator()(const std::pair<F, S>& lhs, const key_type& rhs) {
129
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs);
130
+ }
131
+ template <typename F, typename S>
132
+ bool operator()(const value_type& lhs, const std::pair<F, S>& rhs) {
133
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
134
+ }
135
+ template <typename F, typename S>
136
+ bool operator()(const std::pair<F, S>& lhs, const value_type& rhs) {
137
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
138
+ }
139
+ template <typename FL, typename SL, typename FR, typename SR>
140
+ bool operator()(const std::pair<FL, SL>& lhs, const std::pair<FR, SR>& rhs) {
141
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
142
+ }
143
+ };
144
+ static constexpr int8_t min_lookups = 4;
145
+ template <typename T>
146
+ struct sherwood_v3_entry {
147
+ sherwood_v3_entry() = default;
148
+ sherwood_v3_entry(int8_t distance_from_desired)
149
+ : distance_from_desired(distance_from_desired) {}
150
+ ~sherwood_v3_entry() = default;
151
+
152
+ bool has_value() const {
153
+ return distance_from_desired >= 0;
154
+ }
155
+ bool is_empty() const {
156
+ return distance_from_desired < 0;
157
+ }
158
+ bool is_at_desired_position() const {
159
+ return distance_from_desired <= 0;
160
+ }
161
+ template <typename... Args>
162
+ void emplace(int8_t distance, Args&&... args) {
163
+ new (std::addressof(value)) T(std::forward<Args>(args)...);
164
+ distance_from_desired = distance;
165
+ }
166
+
167
+ void destroy_value() {
168
+ value.~T();
169
+ distance_from_desired = -1;
170
+ }
171
+
172
+ int8_t distance_from_desired = -1;
173
+ static constexpr int8_t special_end_value = 0;
174
+ union {
175
+ T value;
176
+ };
177
+ };
178
+
179
+ inline int8_t log2(uint64_t value) {
180
+ // NOLINTNEXTLINE(*c-arrays*)
181
+ static constexpr int8_t table[64] = {
182
+ 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3,
183
+ 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4,
184
+ 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
185
+ 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5};
186
+ value |= value >> 1;
187
+ value |= value >> 2;
188
+ value |= value >> 4;
189
+ value |= value >> 8;
190
+ value |= value >> 16;
191
+ value |= value >> 32;
192
+ return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
193
+ }
194
+
195
+ inline uint64_t next_power_of_two(uint64_t i) {
196
+ --i;
197
+ i |= i >> 1;
198
+ i |= i >> 2;
199
+ i |= i >> 4;
200
+ i |= i >> 8;
201
+ i |= i >> 16;
202
+ i |= i >> 32;
203
+ ++i;
204
+ return i;
205
+ }
206
+
207
+ // Implementation taken from http://en.cppreference.com/w/cpp/types/void_t
208
+ // (it takes CWG1558 into account and also works for older compilers)
209
+ template <typename... Ts>
210
+ struct make_void {
211
+ typedef void type;
212
+ };
213
+ template <typename... Ts>
214
+ using void_t = typename make_void<Ts...>::type;
215
+
216
+ template <typename T, typename = void>
217
+ struct HashPolicySelector {
218
+ typedef fibonacci_hash_policy type;
219
+ };
220
+ template <typename T>
221
+ struct HashPolicySelector<T, void_t<typename T::hash_policy>> {
222
+ typedef typename T::hash_policy type;
223
+ };
224
+
225
+ template <
226
+ typename T,
227
+ typename FindKey,
228
+ typename ArgumentHash,
229
+ typename DetailHasher,
230
+ typename ArgumentEqual,
231
+ typename Equal,
232
+ typename ArgumentAlloc,
233
+ typename EntryAlloc>
234
+ class sherwood_v3_table : private EntryAlloc,
235
+ private DetailHasher,
236
+ private Equal {
237
+ using Entry = detailv3::sherwood_v3_entry<T>;
238
+ using AllocatorTraits = std::allocator_traits<EntryAlloc>;
239
+ using EntryPointer = typename AllocatorTraits::pointer;
240
+
241
+ public:
242
+ struct convertible_to_iterator;
243
+
244
+ using value_type = T;
245
+ using size_type = uint64_t;
246
+ using difference_type = std::ptrdiff_t;
247
+ using hasher = ArgumentHash;
248
+ using key_equal = ArgumentEqual;
249
+ using allocator_type = EntryAlloc;
250
+ using reference = value_type&;
251
+ using const_reference = const value_type&;
252
+ using pointer = value_type*;
253
+ using const_pointer = const value_type*;
254
+
255
+ sherwood_v3_table() = default;
256
+ explicit sherwood_v3_table(
257
+ size_type bucket_count,
258
+ const ArgumentHash& hash = ArgumentHash(),
259
+ const ArgumentEqual& equal = ArgumentEqual(),
260
+ const ArgumentAlloc& alloc = ArgumentAlloc())
261
+ : EntryAlloc(alloc), DetailHasher(hash), Equal(equal) {
262
+ rehash(bucket_count);
263
+ }
264
+ sherwood_v3_table(size_type bucket_count, const ArgumentAlloc& alloc)
265
+ : sherwood_v3_table(
266
+ bucket_count,
267
+ ArgumentHash(),
268
+ ArgumentEqual(),
269
+ alloc) {}
270
+ sherwood_v3_table(
271
+ size_type bucket_count,
272
+ const ArgumentHash& hash,
273
+ const ArgumentAlloc& alloc)
274
+ : sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc) {}
275
+ explicit sherwood_v3_table(const ArgumentAlloc& alloc) : EntryAlloc(alloc) {}
276
+ template <typename It>
277
+ sherwood_v3_table(
278
+ It first,
279
+ It last,
280
+ size_type bucket_count = 0,
281
+ const ArgumentHash& hash = ArgumentHash(),
282
+ const ArgumentEqual& equal = ArgumentEqual(),
283
+ const ArgumentAlloc& alloc = ArgumentAlloc())
284
+ : sherwood_v3_table(bucket_count, hash, equal, alloc) {
285
+ insert(first, last);
286
+ }
287
+ template <typename It>
288
+ sherwood_v3_table(
289
+ It first,
290
+ It last,
291
+ size_type bucket_count,
292
+ const ArgumentAlloc& alloc)
293
+ : sherwood_v3_table(
294
+ first,
295
+ last,
296
+ bucket_count,
297
+ ArgumentHash(),
298
+ ArgumentEqual(),
299
+ alloc) {}
300
+ template <typename It>
301
+ sherwood_v3_table(
302
+ It first,
303
+ It last,
304
+ size_type bucket_count,
305
+ const ArgumentHash& hash,
306
+ const ArgumentAlloc& alloc)
307
+ : sherwood_v3_table(
308
+ first,
309
+ last,
310
+ bucket_count,
311
+ hash,
312
+ ArgumentEqual(),
313
+ alloc) {}
314
+ sherwood_v3_table(
315
+ std::initializer_list<T> il,
316
+ size_type bucket_count = 0,
317
+ const ArgumentHash& hash = ArgumentHash(),
318
+ const ArgumentEqual& equal = ArgumentEqual(),
319
+ const ArgumentAlloc& alloc = ArgumentAlloc())
320
+ : sherwood_v3_table(bucket_count, hash, equal, alloc) {
321
+ if (bucket_count == 0)
322
+ rehash(il.size());
323
+ insert(il.begin(), il.end());
324
+ }
325
+ sherwood_v3_table(
326
+ std::initializer_list<T> il,
327
+ size_type bucket_count,
328
+ const ArgumentAlloc& alloc)
329
+ : sherwood_v3_table(
330
+ il,
331
+ bucket_count,
332
+ ArgumentHash(),
333
+ ArgumentEqual(),
334
+ alloc) {}
335
+ sherwood_v3_table(
336
+ std::initializer_list<T> il,
337
+ size_type bucket_count,
338
+ const ArgumentHash& hash,
339
+ const ArgumentAlloc& alloc)
340
+ : sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc) {}
341
+ sherwood_v3_table(const sherwood_v3_table& other)
342
+ : sherwood_v3_table(
343
+ other,
344
+ AllocatorTraits::select_on_container_copy_construction(
345
+ other.get_allocator())) {}
346
+ sherwood_v3_table(const sherwood_v3_table& other, const ArgumentAlloc& alloc)
347
+ : EntryAlloc(alloc),
348
+ DetailHasher(other),
349
+ Equal(other),
350
+ _max_load_factor(other._max_load_factor) {
351
+ rehash_for_other_container(other);
352
+ try {
353
+ insert(other.begin(), other.end());
354
+ } catch (...) {
355
+ clear();
356
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
357
+ throw;
358
+ }
359
+ }
360
+ sherwood_v3_table(sherwood_v3_table&& other) noexcept
361
+ : EntryAlloc(std::move(other)),
362
+ DetailHasher(std::move(other)),
363
+ Equal(std::move(other)) {
364
+ swap_pointers(other);
365
+ }
366
+ sherwood_v3_table(
367
+ sherwood_v3_table&& other,
368
+ const ArgumentAlloc& alloc) noexcept
369
+ : EntryAlloc(alloc),
370
+ DetailHasher(std::move(other)),
371
+ Equal(std::move(other)) {
372
+ swap_pointers(other);
373
+ }
374
+ sherwood_v3_table& operator=(const sherwood_v3_table& other) {
375
+ if (this == std::addressof(other))
376
+ return *this;
377
+
378
+ clear();
379
+ if constexpr (AllocatorTraits::propagate_on_container_copy_assignment::
380
+ value) {
381
+ if (static_cast<EntryAlloc&>(*this) !=
382
+ static_cast<const EntryAlloc&>(other)) {
383
+ reset_to_empty_state();
384
+ }
385
+ static_cast<EntryAlloc&>(*this) = other;
386
+ }
387
+ _max_load_factor = other._max_load_factor;
388
+ static_cast<DetailHasher&>(*this) = other;
389
+ static_cast<Equal&>(*this) = other;
390
+ rehash_for_other_container(other);
391
+ insert(other.begin(), other.end());
392
+ return *this;
393
+ }
394
+ sherwood_v3_table& operator=(sherwood_v3_table&& other) noexcept {
395
+ if (this == std::addressof(other))
396
+ return *this;
397
+ else if constexpr (AllocatorTraits::propagate_on_container_move_assignment::
398
+ value) {
399
+ clear();
400
+ reset_to_empty_state();
401
+ static_cast<EntryAlloc&>(*this) = std::move(other);
402
+ swap_pointers(other);
403
+ } else if (
404
+ static_cast<EntryAlloc&>(*this) == static_cast<EntryAlloc&>(other)) {
405
+ swap_pointers(other);
406
+ } else {
407
+ clear();
408
+ _max_load_factor = other._max_load_factor;
409
+ rehash_for_other_container(other);
410
+ for (T& elem : other)
411
+ emplace(std::move(elem));
412
+ other.clear();
413
+ }
414
+ static_cast<DetailHasher&>(*this) = std::move(other);
415
+ static_cast<Equal&>(*this) = std::move(other);
416
+ return *this;
417
+ }
418
+ ~sherwood_v3_table() {
419
+ clear();
420
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
421
+ }
422
+
423
+ const allocator_type& get_allocator() const {
424
+ return static_cast<const allocator_type&>(*this);
425
+ }
426
+ const ArgumentEqual& key_eq() const {
427
+ return static_cast<const ArgumentEqual&>(*this);
428
+ }
429
+ const ArgumentHash& hash_function() const {
430
+ return static_cast<const ArgumentHash&>(*this);
431
+ }
432
+
433
+ template <typename ValueType>
434
+ struct templated_iterator {
435
+ templated_iterator() = default;
436
+ templated_iterator(EntryPointer current) : current(current) {}
437
+ EntryPointer current = EntryPointer();
438
+
439
+ using iterator_category = std::forward_iterator_tag;
440
+ using value_type = ValueType;
441
+ using difference_type = ptrdiff_t;
442
+ using pointer = ValueType*;
443
+ using reference = ValueType&;
444
+
445
+ friend bool operator==(
446
+ const templated_iterator& lhs,
447
+ const templated_iterator& rhs) {
448
+ return lhs.current == rhs.current;
449
+ }
450
+ friend bool operator!=(
451
+ const templated_iterator& lhs,
452
+ const templated_iterator& rhs) {
453
+ return !(lhs == rhs);
454
+ }
455
+
456
+ templated_iterator& operator++() {
457
+ do {
458
+ ++current;
459
+ } while (current->is_empty());
460
+ return *this;
461
+ }
462
+ templated_iterator operator++(int) {
463
+ templated_iterator copy(*this);
464
+ ++*this;
465
+ return copy;
466
+ }
467
+
468
+ ValueType& operator*() const {
469
+ return current->value;
470
+ }
471
+ ValueType* operator->() const {
472
+ return std::addressof(current->value);
473
+ }
474
+
475
+ // the template automatically disables the operator when value_type is
476
+ // already const, because that would cause a lot of compiler warnings
477
+ // otherwise.
478
+ template <
479
+ class target_type = const value_type,
480
+ class = std::enable_if_t<
481
+ std::is_same_v<target_type, const value_type> &&
482
+ !std::is_same_v<target_type, value_type>>>
483
+ operator templated_iterator<target_type>() const {
484
+ return {current};
485
+ }
486
+ };
487
+ using iterator = templated_iterator<value_type>;
488
+ using const_iterator = templated_iterator<const value_type>;
489
+
490
+ iterator begin() {
491
+ for (EntryPointer it = entries;; ++it) {
492
+ if (it->has_value())
493
+ return {it};
494
+ }
495
+ }
496
+ const_iterator begin() const {
497
+ for (EntryPointer it = entries;; ++it) {
498
+ if (it->has_value())
499
+ return {it};
500
+ }
501
+ }
502
+ const_iterator cbegin() const {
503
+ return begin();
504
+ }
505
+ iterator end() {
506
+ return {
507
+ entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups)};
508
+ }
509
+ const_iterator end() const {
510
+ return {
511
+ entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups)};
512
+ }
513
+ const_iterator cend() const {
514
+ return end();
515
+ }
516
+
517
+ iterator find(const FindKey& key) {
518
+ uint64_t index =
519
+ hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
520
+ EntryPointer it = entries + ptrdiff_t(index);
521
+ for (int8_t distance = 0; it->distance_from_desired >= distance;
522
+ ++distance, ++it) {
523
+ if (compares_equal(key, it->value))
524
+ return {it};
525
+ }
526
+ return end();
527
+ }
528
+ const_iterator find(const FindKey& key) const {
529
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
530
+ return const_cast<sherwood_v3_table*>(this)->find(key);
531
+ }
532
+ uint64_t count(const FindKey& key) const {
533
+ return find(key) == end() ? 0 : 1;
534
+ }
535
+ std::pair<iterator, iterator> equal_range(const FindKey& key) {
536
+ iterator found = find(key);
537
+ if (found == end())
538
+ return {found, found};
539
+ else
540
+ return {found, std::next(found)};
541
+ }
542
+ std::pair<const_iterator, const_iterator> equal_range(
543
+ const FindKey& key) const {
544
+ const_iterator found = find(key);
545
+ if (found == end())
546
+ return {found, found};
547
+ else
548
+ return {found, std::next(found)};
549
+ }
550
+
551
+ template <typename Key, typename... Args>
552
+ std::pair<iterator, bool> emplace(Key&& key, Args&&... args) {
553
+ uint64_t index =
554
+ hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
555
+ EntryPointer current_entry = entries + ptrdiff_t(index);
556
+ int8_t distance_from_desired = 0;
557
+ for (; current_entry->distance_from_desired >= distance_from_desired;
558
+ ++current_entry, ++distance_from_desired) {
559
+ if (compares_equal(key, current_entry->value))
560
+ return {{current_entry}, false};
561
+ }
562
+ return emplace_new_key(
563
+ distance_from_desired,
564
+ current_entry,
565
+ std::forward<Key>(key),
566
+ std::forward<Args>(args)...);
567
+ }
568
+
569
+ std::pair<iterator, bool> insert(const value_type& value) {
570
+ return emplace(value);
571
+ }
572
+ std::pair<iterator, bool> insert(value_type&& value) {
573
+ return emplace(std::move(value));
574
+ }
575
+ template <typename... Args>
576
+ iterator emplace_hint(const_iterator, Args&&... args) {
577
+ return emplace(std::forward<Args>(args)...).first;
578
+ }
579
+ iterator insert(const_iterator, const value_type& value) {
580
+ return emplace(value).first;
581
+ }
582
+ iterator insert(const_iterator, value_type&& value) {
583
+ return emplace(std::move(value)).first;
584
+ }
585
+
586
+ template <typename It>
587
+ void insert(It begin, It end) {
588
+ for (; begin != end; ++begin) {
589
+ emplace(*begin);
590
+ }
591
+ }
592
+ void insert(std::initializer_list<value_type> il) {
593
+ insert(il.begin(), il.end());
594
+ }
595
+
596
+ void rehash(uint64_t num_buckets) {
597
+ num_buckets = std::max(
598
+ num_buckets,
599
+ static_cast<uint64_t>(
600
+ std::ceil(num_elements / static_cast<double>(_max_load_factor))));
601
+ if (num_buckets == 0) {
602
+ reset_to_empty_state();
603
+ return;
604
+ }
605
+ auto new_prime_index = hash_policy.next_size_over(num_buckets);
606
+ if (num_buckets == bucket_count())
607
+ return;
608
+ int8_t new_max_lookups = compute_max_lookups(num_buckets);
609
+ EntryPointer new_buckets(
610
+ AllocatorTraits::allocate(*this, num_buckets + new_max_lookups));
611
+ EntryPointer special_end_item =
612
+ new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1);
613
+ for (EntryPointer it = new_buckets; it != special_end_item; ++it)
614
+ it->distance_from_desired = -1;
615
+ special_end_item->distance_from_desired = Entry::special_end_value;
616
+ std::swap(entries, new_buckets);
617
+ std::swap(num_slots_minus_one, num_buckets);
618
+ --num_slots_minus_one;
619
+ hash_policy.commit(new_prime_index);
620
+ int8_t old_max_lookups = max_lookups;
621
+ max_lookups = new_max_lookups;
622
+ num_elements = 0;
623
+ for (EntryPointer
624
+ it = new_buckets,
625
+ end = it + static_cast<ptrdiff_t>(num_buckets + old_max_lookups);
626
+ it != end;
627
+ ++it) {
628
+ if (it->has_value()) {
629
+ emplace(std::move(it->value));
630
+ it->destroy_value();
631
+ }
632
+ }
633
+ deallocate_data(new_buckets, num_buckets, old_max_lookups);
634
+ }
635
+
636
+ void reserve(uint64_t num_elements_) {
637
+ uint64_t required_buckets = num_buckets_for_reserve(num_elements_);
638
+ if (required_buckets > bucket_count())
639
+ rehash(required_buckets);
640
+ }
641
+
642
+ // the return value is a type that can be converted to an iterator
643
+ // the reason for doing this is that it's not free to find the
644
+ // iterator pointing at the next element. if you care about the
645
+ // next iterator, turn the return value into an iterator
646
+ convertible_to_iterator erase(const_iterator to_erase) {
647
+ EntryPointer current = to_erase.current;
648
+ current->destroy_value();
649
+ --num_elements;
650
+ for (EntryPointer next = current + ptrdiff_t(1);
651
+ !next->is_at_desired_position();
652
+ ++current, ++next) {
653
+ current->emplace(next->distance_from_desired - 1, std::move(next->value));
654
+ next->destroy_value();
655
+ }
656
+ return {to_erase.current};
657
+ }
658
+
659
+ iterator erase(const_iterator begin_it, const_iterator end_it) {
660
+ if (begin_it == end_it)
661
+ return {begin_it.current};
662
+ for (EntryPointer it = begin_it.current, end = end_it.current; it != end;
663
+ ++it) {
664
+ if (it->has_value()) {
665
+ it->destroy_value();
666
+ --num_elements;
667
+ }
668
+ }
669
+ if (end_it == this->end())
670
+ return this->end();
671
+ ptrdiff_t num_to_move = std::min(
672
+ static_cast<ptrdiff_t>(end_it.current->distance_from_desired),
673
+ end_it.current - begin_it.current);
674
+ EntryPointer to_return = end_it.current - num_to_move;
675
+ for (EntryPointer it = end_it.current; !it->is_at_desired_position();) {
676
+ EntryPointer target = it - num_to_move;
677
+ target->emplace(
678
+ it->distance_from_desired - num_to_move, std::move(it->value));
679
+ it->destroy_value();
680
+ ++it;
681
+ num_to_move = std::min(
682
+ static_cast<ptrdiff_t>(it->distance_from_desired), num_to_move);
683
+ }
684
+ return {to_return};
685
+ }
686
+
687
+ uint64_t erase(const FindKey& key) {
688
+ auto found = find(key);
689
+ if (found == end())
690
+ return 0;
691
+ else {
692
+ erase(found);
693
+ return 1;
694
+ }
695
+ }
696
+
697
+ void clear() {
698
+ for (EntryPointer it = entries,
699
+ end = it +
700
+ static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups);
701
+ it != end;
702
+ ++it) {
703
+ if (it->has_value())
704
+ it->destroy_value();
705
+ }
706
+ num_elements = 0;
707
+ }
708
+
709
+ void shrink_to_fit() {
710
+ rehash_for_other_container(*this);
711
+ }
712
+
713
+ void swap(sherwood_v3_table& other) noexcept {
714
+ using std::swap;
715
+ swap_pointers(other);
716
+ swap(static_cast<ArgumentHash&>(*this), static_cast<ArgumentHash&>(other));
717
+ swap(
718
+ static_cast<ArgumentEqual&>(*this), static_cast<ArgumentEqual&>(other));
719
+ if (AllocatorTraits::propagate_on_container_swap::value)
720
+ swap(static_cast<EntryAlloc&>(*this), static_cast<EntryAlloc&>(other));
721
+ }
722
+
723
+ uint64_t size() const {
724
+ return num_elements;
725
+ }
726
+ uint64_t max_size() const {
727
+ return (AllocatorTraits::max_size(*this)) / sizeof(Entry);
728
+ }
729
+ uint64_t bucket_count() const {
730
+ return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
731
+ }
732
+ size_type max_bucket_count() const {
733
+ return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry);
734
+ }
735
+ uint64_t bucket(const FindKey& key) const {
736
+ return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
737
+ }
738
+ float load_factor() const {
739
+ uint64_t buckets = bucket_count();
740
+ if (buckets)
741
+ return static_cast<float>(num_elements) / bucket_count();
742
+ else
743
+ return 0;
744
+ }
745
+ void max_load_factor(float value) {
746
+ _max_load_factor = value;
747
+ }
748
+ float max_load_factor() const {
749
+ return _max_load_factor;
750
+ }
751
+
752
+ bool empty() const {
753
+ return num_elements == 0;
754
+ }
755
+
756
+ private:
757
+ EntryPointer entries = empty_default_table();
758
+ uint64_t num_slots_minus_one = 0;
759
+ typename HashPolicySelector<ArgumentHash>::type hash_policy;
760
+ int8_t max_lookups = detailv3::min_lookups - 1;
761
+ float _max_load_factor = 0.5f;
762
+ uint64_t num_elements = 0;
763
+
764
+ EntryPointer empty_default_table() {
765
+ EntryPointer result =
766
+ AllocatorTraits::allocate(*this, detailv3::min_lookups);
767
+ EntryPointer special_end_item =
768
+ result + static_cast<ptrdiff_t>(detailv3::min_lookups - 1);
769
+ for (EntryPointer it = result; it != special_end_item; ++it)
770
+ it->distance_from_desired = -1;
771
+ special_end_item->distance_from_desired = Entry::special_end_value;
772
+ return result;
773
+ }
774
+
775
+ static int8_t compute_max_lookups(uint64_t num_buckets) {
776
+ int8_t desired = detailv3::log2(num_buckets);
777
+ return std::max(detailv3::min_lookups, desired);
778
+ }
779
+
780
+ uint64_t num_buckets_for_reserve(uint64_t num_elements_) const {
781
+ return static_cast<uint64_t>(std::ceil(
782
+ static_cast<double>(num_elements_) /
783
+ std::min(0.5, static_cast<double>(_max_load_factor))));
784
+ }
785
+ void rehash_for_other_container(const sherwood_v3_table& other) {
786
+ rehash(
787
+ std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
788
+ }
789
+
790
+ void swap_pointers(sherwood_v3_table& other) {
791
+ using std::swap;
792
+ swap(hash_policy, other.hash_policy);
793
+ swap(entries, other.entries);
794
+ swap(num_slots_minus_one, other.num_slots_minus_one);
795
+ swap(num_elements, other.num_elements);
796
+ swap(max_lookups, other.max_lookups);
797
+ swap(_max_load_factor, other._max_load_factor);
798
+ }
799
+
800
+ template <typename Key, typename... Args>
801
+ SKA_NOINLINE(std::pair<iterator, bool>)
802
+ emplace_new_key(
803
+ int8_t distance_from_desired,
804
+ EntryPointer current_entry,
805
+ Key&& key,
806
+ Args&&... args) {
807
+ using std::swap;
808
+ if (num_slots_minus_one == 0 || distance_from_desired == max_lookups ||
809
+ num_elements + 1 >
810
+ (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor)) {
811
+ grow();
812
+ return emplace(std::forward<Key>(key), std::forward<Args>(args)...);
813
+ } else if (current_entry->is_empty()) {
814
+ current_entry->emplace(
815
+ distance_from_desired,
816
+ std::forward<Key>(key),
817
+ std::forward<Args>(args)...);
818
+ ++num_elements;
819
+ return {{current_entry}, true};
820
+ }
821
+ value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...);
822
+ swap(distance_from_desired, current_entry->distance_from_desired);
823
+ swap(to_insert, current_entry->value);
824
+ iterator result = {current_entry};
825
+ for (++distance_from_desired, ++current_entry;; ++current_entry) {
826
+ if (current_entry->is_empty()) {
827
+ current_entry->emplace(distance_from_desired, std::move(to_insert));
828
+ ++num_elements;
829
+ return {result, true};
830
+ } else if (current_entry->distance_from_desired < distance_from_desired) {
831
+ swap(distance_from_desired, current_entry->distance_from_desired);
832
+ swap(to_insert, current_entry->value);
833
+ ++distance_from_desired;
834
+ } else {
835
+ ++distance_from_desired;
836
+ if (distance_from_desired == max_lookups) {
837
+ swap(to_insert, result.current->value);
838
+ grow();
839
+ return emplace(std::move(to_insert));
840
+ }
841
+ }
842
+ }
843
+ }
844
+
845
+ void grow() {
846
+ rehash(std::max(uint64_t(4), 2 * bucket_count()));
847
+ }
848
+
849
+ void deallocate_data(
850
+ EntryPointer begin,
851
+ uint64_t num_slots_minus_one_,
852
+ int8_t max_lookups_) {
853
+ AllocatorTraits::deallocate(
854
+ *this, begin, num_slots_minus_one_ + max_lookups_ + 1);
855
+ }
856
+
857
+ void reset_to_empty_state() {
858
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
859
+ entries = empty_default_table();
860
+ num_slots_minus_one = 0;
861
+ hash_policy.reset();
862
+ max_lookups = detailv3::min_lookups - 1;
863
+ }
864
+
865
+ template <typename U>
866
+ uint64_t hash_object(const U& key) {
867
+ return static_cast<DetailHasher&>(*this)(key);
868
+ }
869
+ template <typename U>
870
+ uint64_t hash_object(const U& key) const {
871
+ return static_cast<const DetailHasher&>(*this)(key);
872
+ }
873
+ template <typename L, typename R>
874
+ bool compares_equal(const L& lhs, const R& rhs) {
875
+ return static_cast<Equal&>(*this)(lhs, rhs);
876
+ }
877
+
878
+ public:
879
+ struct convertible_to_iterator {
880
+ EntryPointer it;
881
+
882
+ operator iterator() {
883
+ if (it->has_value())
884
+ return {it};
885
+ else
886
+ return ++iterator{it};
887
+ }
888
+ operator const_iterator() {
889
+ if (it->has_value())
890
+ return {it};
891
+ else
892
+ return ++const_iterator{it};
893
+ }
894
+ };
895
+ };
896
+ } // namespace detailv3
897
+
898
+ struct prime_number_hash_policy {
899
+ static uint64_t mod0(uint64_t) {
900
+ return 0llu;
901
+ }
902
+ static uint64_t mod2(uint64_t hash) {
903
+ return hash % 2llu;
904
+ }
905
+ static uint64_t mod3(uint64_t hash) {
906
+ return hash % 3llu;
907
+ }
908
+ static uint64_t mod5(uint64_t hash) {
909
+ return hash % 5llu;
910
+ }
911
+ static uint64_t mod7(uint64_t hash) {
912
+ return hash % 7llu;
913
+ }
914
+ static uint64_t mod11(uint64_t hash) {
915
+ return hash % 11llu;
916
+ }
917
+ static uint64_t mod13(uint64_t hash) {
918
+ return hash % 13llu;
919
+ }
920
+ static uint64_t mod17(uint64_t hash) {
921
+ return hash % 17llu;
922
+ }
923
+ static uint64_t mod23(uint64_t hash) {
924
+ return hash % 23llu;
925
+ }
926
+ static uint64_t mod29(uint64_t hash) {
927
+ return hash % 29llu;
928
+ }
929
+ static uint64_t mod37(uint64_t hash) {
930
+ return hash % 37llu;
931
+ }
932
+ static uint64_t mod47(uint64_t hash) {
933
+ return hash % 47llu;
934
+ }
935
+ static uint64_t mod59(uint64_t hash) {
936
+ return hash % 59llu;
937
+ }
938
+ static uint64_t mod73(uint64_t hash) {
939
+ return hash % 73llu;
940
+ }
941
+ static uint64_t mod97(uint64_t hash) {
942
+ return hash % 97llu;
943
+ }
944
+ static uint64_t mod127(uint64_t hash) {
945
+ return hash % 127llu;
946
+ }
947
+ static uint64_t mod151(uint64_t hash) {
948
+ return hash % 151llu;
949
+ }
950
+ static uint64_t mod197(uint64_t hash) {
951
+ return hash % 197llu;
952
+ }
953
+ static uint64_t mod251(uint64_t hash) {
954
+ return hash % 251llu;
955
+ }
956
+ static uint64_t mod313(uint64_t hash) {
957
+ return hash % 313llu;
958
+ }
959
+ static uint64_t mod397(uint64_t hash) {
960
+ return hash % 397llu;
961
+ }
962
+ static uint64_t mod499(uint64_t hash) {
963
+ return hash % 499llu;
964
+ }
965
+ static uint64_t mod631(uint64_t hash) {
966
+ return hash % 631llu;
967
+ }
968
+ static uint64_t mod797(uint64_t hash) {
969
+ return hash % 797llu;
970
+ }
971
+ static uint64_t mod1009(uint64_t hash) {
972
+ return hash % 1009llu;
973
+ }
974
+ static uint64_t mod1259(uint64_t hash) {
975
+ return hash % 1259llu;
976
+ }
977
+ static uint64_t mod1597(uint64_t hash) {
978
+ return hash % 1597llu;
979
+ }
980
+ static uint64_t mod2011(uint64_t hash) {
981
+ return hash % 2011llu;
982
+ }
983
+ static uint64_t mod2539(uint64_t hash) {
984
+ return hash % 2539llu;
985
+ }
986
+ static uint64_t mod3203(uint64_t hash) {
987
+ return hash % 3203llu;
988
+ }
989
+ static uint64_t mod4027(uint64_t hash) {
990
+ return hash % 4027llu;
991
+ }
992
+ static uint64_t mod5087(uint64_t hash) {
993
+ return hash % 5087llu;
994
+ }
995
+ static uint64_t mod6421(uint64_t hash) {
996
+ return hash % 6421llu;
997
+ }
998
+ static uint64_t mod8089(uint64_t hash) {
999
+ return hash % 8089llu;
1000
+ }
1001
+ static uint64_t mod10193(uint64_t hash) {
1002
+ return hash % 10193llu;
1003
+ }
1004
+ static uint64_t mod12853(uint64_t hash) {
1005
+ return hash % 12853llu;
1006
+ }
1007
+ static uint64_t mod16193(uint64_t hash) {
1008
+ return hash % 16193llu;
1009
+ }
1010
+ static uint64_t mod20399(uint64_t hash) {
1011
+ return hash % 20399llu;
1012
+ }
1013
+ static uint64_t mod25717(uint64_t hash) {
1014
+ return hash % 25717llu;
1015
+ }
1016
+ static uint64_t mod32401(uint64_t hash) {
1017
+ return hash % 32401llu;
1018
+ }
1019
+ static uint64_t mod40823(uint64_t hash) {
1020
+ return hash % 40823llu;
1021
+ }
1022
+ static uint64_t mod51437(uint64_t hash) {
1023
+ return hash % 51437llu;
1024
+ }
1025
+ static uint64_t mod64811(uint64_t hash) {
1026
+ return hash % 64811llu;
1027
+ }
1028
+ static uint64_t mod81649(uint64_t hash) {
1029
+ return hash % 81649llu;
1030
+ }
1031
+ static uint64_t mod102877(uint64_t hash) {
1032
+ return hash % 102877llu;
1033
+ }
1034
+ static uint64_t mod129607(uint64_t hash) {
1035
+ return hash % 129607llu;
1036
+ }
1037
+ static uint64_t mod163307(uint64_t hash) {
1038
+ return hash % 163307llu;
1039
+ }
1040
+ static uint64_t mod205759(uint64_t hash) {
1041
+ return hash % 205759llu;
1042
+ }
1043
+ static uint64_t mod259229(uint64_t hash) {
1044
+ return hash % 259229llu;
1045
+ }
1046
+ static uint64_t mod326617(uint64_t hash) {
1047
+ return hash % 326617llu;
1048
+ }
1049
+ static uint64_t mod411527(uint64_t hash) {
1050
+ return hash % 411527llu;
1051
+ }
1052
+ static uint64_t mod518509(uint64_t hash) {
1053
+ return hash % 518509llu;
1054
+ }
1055
+ static uint64_t mod653267(uint64_t hash) {
1056
+ return hash % 653267llu;
1057
+ }
1058
+ static uint64_t mod823117(uint64_t hash) {
1059
+ return hash % 823117llu;
1060
+ }
1061
+ static uint64_t mod1037059(uint64_t hash) {
1062
+ return hash % 1037059llu;
1063
+ }
1064
+ static uint64_t mod1306601(uint64_t hash) {
1065
+ return hash % 1306601llu;
1066
+ }
1067
+ static uint64_t mod1646237(uint64_t hash) {
1068
+ return hash % 1646237llu;
1069
+ }
1070
+ static uint64_t mod2074129(uint64_t hash) {
1071
+ return hash % 2074129llu;
1072
+ }
1073
+ static uint64_t mod2613229(uint64_t hash) {
1074
+ return hash % 2613229llu;
1075
+ }
1076
+ static uint64_t mod3292489(uint64_t hash) {
1077
+ return hash % 3292489llu;
1078
+ }
1079
+ static uint64_t mod4148279(uint64_t hash) {
1080
+ return hash % 4148279llu;
1081
+ }
1082
+ static uint64_t mod5226491(uint64_t hash) {
1083
+ return hash % 5226491llu;
1084
+ }
1085
+ static uint64_t mod6584983(uint64_t hash) {
1086
+ return hash % 6584983llu;
1087
+ }
1088
+ static uint64_t mod8296553(uint64_t hash) {
1089
+ return hash % 8296553llu;
1090
+ }
1091
+ static uint64_t mod10453007(uint64_t hash) {
1092
+ return hash % 10453007llu;
1093
+ }
1094
+ static uint64_t mod13169977(uint64_t hash) {
1095
+ return hash % 13169977llu;
1096
+ }
1097
+ static uint64_t mod16593127(uint64_t hash) {
1098
+ return hash % 16593127llu;
1099
+ }
1100
+ static uint64_t mod20906033(uint64_t hash) {
1101
+ return hash % 20906033llu;
1102
+ }
1103
+ static uint64_t mod26339969(uint64_t hash) {
1104
+ return hash % 26339969llu;
1105
+ }
1106
+ static uint64_t mod33186281(uint64_t hash) {
1107
+ return hash % 33186281llu;
1108
+ }
1109
+ static uint64_t mod41812097(uint64_t hash) {
1110
+ return hash % 41812097llu;
1111
+ }
1112
+ static uint64_t mod52679969(uint64_t hash) {
1113
+ return hash % 52679969llu;
1114
+ }
1115
+ static uint64_t mod66372617(uint64_t hash) {
1116
+ return hash % 66372617llu;
1117
+ }
1118
+ static uint64_t mod83624237(uint64_t hash) {
1119
+ return hash % 83624237llu;
1120
+ }
1121
+ static uint64_t mod105359939(uint64_t hash) {
1122
+ return hash % 105359939llu;
1123
+ }
1124
+ static uint64_t mod132745199(uint64_t hash) {
1125
+ return hash % 132745199llu;
1126
+ }
1127
+ static uint64_t mod167248483(uint64_t hash) {
1128
+ return hash % 167248483llu;
1129
+ }
1130
+ static uint64_t mod210719881(uint64_t hash) {
1131
+ return hash % 210719881llu;
1132
+ }
1133
+ static uint64_t mod265490441(uint64_t hash) {
1134
+ return hash % 265490441llu;
1135
+ }
1136
+ static uint64_t mod334496971(uint64_t hash) {
1137
+ return hash % 334496971llu;
1138
+ }
1139
+ static uint64_t mod421439783(uint64_t hash) {
1140
+ return hash % 421439783llu;
1141
+ }
1142
+ static uint64_t mod530980861(uint64_t hash) {
1143
+ return hash % 530980861llu;
1144
+ }
1145
+ static uint64_t mod668993977(uint64_t hash) {
1146
+ return hash % 668993977llu;
1147
+ }
1148
+ static uint64_t mod842879579(uint64_t hash) {
1149
+ return hash % 842879579llu;
1150
+ }
1151
+ static uint64_t mod1061961721(uint64_t hash) {
1152
+ return hash % 1061961721llu;
1153
+ }
1154
+ static uint64_t mod1337987929(uint64_t hash) {
1155
+ return hash % 1337987929llu;
1156
+ }
1157
+ static uint64_t mod1685759167(uint64_t hash) {
1158
+ return hash % 1685759167llu;
1159
+ }
1160
+ static uint64_t mod2123923447(uint64_t hash) {
1161
+ return hash % 2123923447llu;
1162
+ }
1163
+ static uint64_t mod2675975881(uint64_t hash) {
1164
+ return hash % 2675975881llu;
1165
+ }
1166
+ static uint64_t mod3371518343(uint64_t hash) {
1167
+ return hash % 3371518343llu;
1168
+ }
1169
+ static uint64_t mod4247846927(uint64_t hash) {
1170
+ return hash % 4247846927llu;
1171
+ }
1172
+ static uint64_t mod5351951779(uint64_t hash) {
1173
+ return hash % 5351951779llu;
1174
+ }
1175
+ static uint64_t mod6743036717(uint64_t hash) {
1176
+ return hash % 6743036717llu;
1177
+ }
1178
+ static uint64_t mod8495693897(uint64_t hash) {
1179
+ return hash % 8495693897llu;
1180
+ }
1181
+ static uint64_t mod10703903591(uint64_t hash) {
1182
+ return hash % 10703903591llu;
1183
+ }
1184
+ static uint64_t mod13486073473(uint64_t hash) {
1185
+ return hash % 13486073473llu;
1186
+ }
1187
+ static uint64_t mod16991387857(uint64_t hash) {
1188
+ return hash % 16991387857llu;
1189
+ }
1190
+ static uint64_t mod21407807219(uint64_t hash) {
1191
+ return hash % 21407807219llu;
1192
+ }
1193
+ static uint64_t mod26972146961(uint64_t hash) {
1194
+ return hash % 26972146961llu;
1195
+ }
1196
+ static uint64_t mod33982775741(uint64_t hash) {
1197
+ return hash % 33982775741llu;
1198
+ }
1199
+ static uint64_t mod42815614441(uint64_t hash) {
1200
+ return hash % 42815614441llu;
1201
+ }
1202
+ static uint64_t mod53944293929(uint64_t hash) {
1203
+ return hash % 53944293929llu;
1204
+ }
1205
+ static uint64_t mod67965551447(uint64_t hash) {
1206
+ return hash % 67965551447llu;
1207
+ }
1208
+ static uint64_t mod85631228929(uint64_t hash) {
1209
+ return hash % 85631228929llu;
1210
+ }
1211
+ static uint64_t mod107888587883(uint64_t hash) {
1212
+ return hash % 107888587883llu;
1213
+ }
1214
+ static uint64_t mod135931102921(uint64_t hash) {
1215
+ return hash % 135931102921llu;
1216
+ }
1217
+ static uint64_t mod171262457903(uint64_t hash) {
1218
+ return hash % 171262457903llu;
1219
+ }
1220
+ static uint64_t mod215777175787(uint64_t hash) {
1221
+ return hash % 215777175787llu;
1222
+ }
1223
+ static uint64_t mod271862205833(uint64_t hash) {
1224
+ return hash % 271862205833llu;
1225
+ }
1226
+ static uint64_t mod342524915839(uint64_t hash) {
1227
+ return hash % 342524915839llu;
1228
+ }
1229
+ static uint64_t mod431554351609(uint64_t hash) {
1230
+ return hash % 431554351609llu;
1231
+ }
1232
+ static uint64_t mod543724411781(uint64_t hash) {
1233
+ return hash % 543724411781llu;
1234
+ }
1235
+ static uint64_t mod685049831731(uint64_t hash) {
1236
+ return hash % 685049831731llu;
1237
+ }
1238
+ static uint64_t mod863108703229(uint64_t hash) {
1239
+ return hash % 863108703229llu;
1240
+ }
1241
+ static uint64_t mod1087448823553(uint64_t hash) {
1242
+ return hash % 1087448823553llu;
1243
+ }
1244
+ static uint64_t mod1370099663459(uint64_t hash) {
1245
+ return hash % 1370099663459llu;
1246
+ }
1247
+ static uint64_t mod1726217406467(uint64_t hash) {
1248
+ return hash % 1726217406467llu;
1249
+ }
1250
+ static uint64_t mod2174897647073(uint64_t hash) {
1251
+ return hash % 2174897647073llu;
1252
+ }
1253
+ static uint64_t mod2740199326961(uint64_t hash) {
1254
+ return hash % 2740199326961llu;
1255
+ }
1256
+ static uint64_t mod3452434812973(uint64_t hash) {
1257
+ return hash % 3452434812973llu;
1258
+ }
1259
+ static uint64_t mod4349795294267(uint64_t hash) {
1260
+ return hash % 4349795294267llu;
1261
+ }
1262
+ static uint64_t mod5480398654009(uint64_t hash) {
1263
+ return hash % 5480398654009llu;
1264
+ }
1265
+ static uint64_t mod6904869625999(uint64_t hash) {
1266
+ return hash % 6904869625999llu;
1267
+ }
1268
+ static uint64_t mod8699590588571(uint64_t hash) {
1269
+ return hash % 8699590588571llu;
1270
+ }
1271
+ static uint64_t mod10960797308051(uint64_t hash) {
1272
+ return hash % 10960797308051llu;
1273
+ }
1274
+ static uint64_t mod13809739252051(uint64_t hash) {
1275
+ return hash % 13809739252051llu;
1276
+ }
1277
+ static uint64_t mod17399181177241(uint64_t hash) {
1278
+ return hash % 17399181177241llu;
1279
+ }
1280
+ static uint64_t mod21921594616111(uint64_t hash) {
1281
+ return hash % 21921594616111llu;
1282
+ }
1283
+ static uint64_t mod27619478504183(uint64_t hash) {
1284
+ return hash % 27619478504183llu;
1285
+ }
1286
+ static uint64_t mod34798362354533(uint64_t hash) {
1287
+ return hash % 34798362354533llu;
1288
+ }
1289
+ static uint64_t mod43843189232363(uint64_t hash) {
1290
+ return hash % 43843189232363llu;
1291
+ }
1292
+ static uint64_t mod55238957008387(uint64_t hash) {
1293
+ return hash % 55238957008387llu;
1294
+ }
1295
+ static uint64_t mod69596724709081(uint64_t hash) {
1296
+ return hash % 69596724709081llu;
1297
+ }
1298
+ static uint64_t mod87686378464759(uint64_t hash) {
1299
+ return hash % 87686378464759llu;
1300
+ }
1301
+ static uint64_t mod110477914016779(uint64_t hash) {
1302
+ return hash % 110477914016779llu;
1303
+ }
1304
+ static uint64_t mod139193449418173(uint64_t hash) {
1305
+ return hash % 139193449418173llu;
1306
+ }
1307
+ static uint64_t mod175372756929481(uint64_t hash) {
1308
+ return hash % 175372756929481llu;
1309
+ }
1310
+ static uint64_t mod220955828033581(uint64_t hash) {
1311
+ return hash % 220955828033581llu;
1312
+ }
1313
+ static uint64_t mod278386898836457(uint64_t hash) {
1314
+ return hash % 278386898836457llu;
1315
+ }
1316
+ static uint64_t mod350745513859007(uint64_t hash) {
1317
+ return hash % 350745513859007llu;
1318
+ }
1319
+ static uint64_t mod441911656067171(uint64_t hash) {
1320
+ return hash % 441911656067171llu;
1321
+ }
1322
+ static uint64_t mod556773797672909(uint64_t hash) {
1323
+ return hash % 556773797672909llu;
1324
+ }
1325
+ static uint64_t mod701491027718027(uint64_t hash) {
1326
+ return hash % 701491027718027llu;
1327
+ }
1328
+ static uint64_t mod883823312134381(uint64_t hash) {
1329
+ return hash % 883823312134381llu;
1330
+ }
1331
+ static uint64_t mod1113547595345903(uint64_t hash) {
1332
+ return hash % 1113547595345903llu;
1333
+ }
1334
+ static uint64_t mod1402982055436147(uint64_t hash) {
1335
+ return hash % 1402982055436147llu;
1336
+ }
1337
+ static uint64_t mod1767646624268779(uint64_t hash) {
1338
+ return hash % 1767646624268779llu;
1339
+ }
1340
+ static uint64_t mod2227095190691797(uint64_t hash) {
1341
+ return hash % 2227095190691797llu;
1342
+ }
1343
+ static uint64_t mod2805964110872297(uint64_t hash) {
1344
+ return hash % 2805964110872297llu;
1345
+ }
1346
+ static uint64_t mod3535293248537579(uint64_t hash) {
1347
+ return hash % 3535293248537579llu;
1348
+ }
1349
+ static uint64_t mod4454190381383713(uint64_t hash) {
1350
+ return hash % 4454190381383713llu;
1351
+ }
1352
+ static uint64_t mod5611928221744609(uint64_t hash) {
1353
+ return hash % 5611928221744609llu;
1354
+ }
1355
+ static uint64_t mod7070586497075177(uint64_t hash) {
1356
+ return hash % 7070586497075177llu;
1357
+ }
1358
+ static uint64_t mod8908380762767489(uint64_t hash) {
1359
+ return hash % 8908380762767489llu;
1360
+ }
1361
+ static uint64_t mod11223856443489329(uint64_t hash) {
1362
+ return hash % 11223856443489329llu;
1363
+ }
1364
+ static uint64_t mod14141172994150357(uint64_t hash) {
1365
+ return hash % 14141172994150357llu;
1366
+ }
1367
+ static uint64_t mod17816761525534927(uint64_t hash) {
1368
+ return hash % 17816761525534927llu;
1369
+ }
1370
+ static uint64_t mod22447712886978529(uint64_t hash) {
1371
+ return hash % 22447712886978529llu;
1372
+ }
1373
+ static uint64_t mod28282345988300791(uint64_t hash) {
1374
+ return hash % 28282345988300791llu;
1375
+ }
1376
+ static uint64_t mod35633523051069991(uint64_t hash) {
1377
+ return hash % 35633523051069991llu;
1378
+ }
1379
+ static uint64_t mod44895425773957261(uint64_t hash) {
1380
+ return hash % 44895425773957261llu;
1381
+ }
1382
+ static uint64_t mod56564691976601587(uint64_t hash) {
1383
+ return hash % 56564691976601587llu;
1384
+ }
1385
+ static uint64_t mod71267046102139967(uint64_t hash) {
1386
+ return hash % 71267046102139967llu;
1387
+ }
1388
+ static uint64_t mod89790851547914507(uint64_t hash) {
1389
+ return hash % 89790851547914507llu;
1390
+ }
1391
+ static uint64_t mod113129383953203213(uint64_t hash) {
1392
+ return hash % 113129383953203213llu;
1393
+ }
1394
+ static uint64_t mod142534092204280003(uint64_t hash) {
1395
+ return hash % 142534092204280003llu;
1396
+ }
1397
+ static uint64_t mod179581703095829107(uint64_t hash) {
1398
+ return hash % 179581703095829107llu;
1399
+ }
1400
+ static uint64_t mod226258767906406483(uint64_t hash) {
1401
+ return hash % 226258767906406483llu;
1402
+ }
1403
+ static uint64_t mod285068184408560057(uint64_t hash) {
1404
+ return hash % 285068184408560057llu;
1405
+ }
1406
+ static uint64_t mod359163406191658253(uint64_t hash) {
1407
+ return hash % 359163406191658253llu;
1408
+ }
1409
+ static uint64_t mod452517535812813007(uint64_t hash) {
1410
+ return hash % 452517535812813007llu;
1411
+ }
1412
+ static uint64_t mod570136368817120201(uint64_t hash) {
1413
+ return hash % 570136368817120201llu;
1414
+ }
1415
+ static uint64_t mod718326812383316683(uint64_t hash) {
1416
+ return hash % 718326812383316683llu;
1417
+ }
1418
+ static uint64_t mod905035071625626043(uint64_t hash) {
1419
+ return hash % 905035071625626043llu;
1420
+ }
1421
+ static uint64_t mod1140272737634240411(uint64_t hash) {
1422
+ return hash % 1140272737634240411llu;
1423
+ }
1424
+ static uint64_t mod1436653624766633509(uint64_t hash) {
1425
+ return hash % 1436653624766633509llu;
1426
+ }
1427
+ static uint64_t mod1810070143251252131(uint64_t hash) {
1428
+ return hash % 1810070143251252131llu;
1429
+ }
1430
+ static uint64_t mod2280545475268481167(uint64_t hash) {
1431
+ return hash % 2280545475268481167llu;
1432
+ }
1433
+ static uint64_t mod2873307249533267101(uint64_t hash) {
1434
+ return hash % 2873307249533267101llu;
1435
+ }
1436
+ static uint64_t mod3620140286502504283(uint64_t hash) {
1437
+ return hash % 3620140286502504283llu;
1438
+ }
1439
+ static uint64_t mod4561090950536962147(uint64_t hash) {
1440
+ return hash % 4561090950536962147llu;
1441
+ }
1442
+ static uint64_t mod5746614499066534157(uint64_t hash) {
1443
+ return hash % 5746614499066534157llu;
1444
+ }
1445
+ static uint64_t mod7240280573005008577(uint64_t hash) {
1446
+ return hash % 7240280573005008577llu;
1447
+ }
1448
+ static uint64_t mod9122181901073924329(uint64_t hash) {
1449
+ return hash % 9122181901073924329llu;
1450
+ }
1451
+ static uint64_t mod11493228998133068689(uint64_t hash) {
1452
+ return hash % 11493228998133068689llu;
1453
+ }
1454
+ static uint64_t mod14480561146010017169(uint64_t hash) {
1455
+ return hash % 14480561146010017169llu;
1456
+ }
1457
+ static uint64_t mod18446744073709551557(uint64_t hash) {
1458
+ return hash % 18446744073709551557llu;
1459
+ }
1460
+
1461
+ using mod_function = uint64_t (*)(uint64_t);
1462
+
1463
+ mod_function next_size_over(uint64_t& size) const {
1464
+ // prime numbers generated by the following method:
1465
+ // 1. start with a prime p = 2
1466
+ // 2. go to wolfram alpha and get p = NextPrime(2 * p)
1467
+ // 3. repeat 2. until you overflow 64 bits
1468
+ // you now have large gaps which you would hit if somebody called reserve()
1469
+ // with an unlucky number.
1470
+ // 4. to fill the gaps for every prime p go to wolfram alpha and get
1471
+ // ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in
1472
+ // the gaps
1473
+ // 5. get PrevPrime(2^64) and put it at the end
1474
+ // NOLINTNEXTLINE(*c-arrays*)
1475
+ static constexpr const uint64_t prime_list[] = {
1476
+ 2llu,
1477
+ 3llu,
1478
+ 5llu,
1479
+ 7llu,
1480
+ 11llu,
1481
+ 13llu,
1482
+ 17llu,
1483
+ 23llu,
1484
+ 29llu,
1485
+ 37llu,
1486
+ 47llu,
1487
+ 59llu,
1488
+ 73llu,
1489
+ 97llu,
1490
+ 127llu,
1491
+ 151llu,
1492
+ 197llu,
1493
+ 251llu,
1494
+ 313llu,
1495
+ 397llu,
1496
+ 499llu,
1497
+ 631llu,
1498
+ 797llu,
1499
+ 1009llu,
1500
+ 1259llu,
1501
+ 1597llu,
1502
+ 2011llu,
1503
+ 2539llu,
1504
+ 3203llu,
1505
+ 4027llu,
1506
+ 5087llu,
1507
+ 6421llu,
1508
+ 8089llu,
1509
+ 10193llu,
1510
+ 12853llu,
1511
+ 16193llu,
1512
+ 20399llu,
1513
+ 25717llu,
1514
+ 32401llu,
1515
+ 40823llu,
1516
+ 51437llu,
1517
+ 64811llu,
1518
+ 81649llu,
1519
+ 102877llu,
1520
+ 129607llu,
1521
+ 163307llu,
1522
+ 205759llu,
1523
+ 259229llu,
1524
+ 326617llu,
1525
+ 411527llu,
1526
+ 518509llu,
1527
+ 653267llu,
1528
+ 823117llu,
1529
+ 1037059llu,
1530
+ 1306601llu,
1531
+ 1646237llu,
1532
+ 2074129llu,
1533
+ 2613229llu,
1534
+ 3292489llu,
1535
+ 4148279llu,
1536
+ 5226491llu,
1537
+ 6584983llu,
1538
+ 8296553llu,
1539
+ 10453007llu,
1540
+ 13169977llu,
1541
+ 16593127llu,
1542
+ 20906033llu,
1543
+ 26339969llu,
1544
+ 33186281llu,
1545
+ 41812097llu,
1546
+ 52679969llu,
1547
+ 66372617llu,
1548
+ 83624237llu,
1549
+ 105359939llu,
1550
+ 132745199llu,
1551
+ 167248483llu,
1552
+ 210719881llu,
1553
+ 265490441llu,
1554
+ 334496971llu,
1555
+ 421439783llu,
1556
+ 530980861llu,
1557
+ 668993977llu,
1558
+ 842879579llu,
1559
+ 1061961721llu,
1560
+ 1337987929llu,
1561
+ 1685759167llu,
1562
+ 2123923447llu,
1563
+ 2675975881llu,
1564
+ 3371518343llu,
1565
+ 4247846927llu,
1566
+ 5351951779llu,
1567
+ 6743036717llu,
1568
+ 8495693897llu,
1569
+ 10703903591llu,
1570
+ 13486073473llu,
1571
+ 16991387857llu,
1572
+ 21407807219llu,
1573
+ 26972146961llu,
1574
+ 33982775741llu,
1575
+ 42815614441llu,
1576
+ 53944293929llu,
1577
+ 67965551447llu,
1578
+ 85631228929llu,
1579
+ 107888587883llu,
1580
+ 135931102921llu,
1581
+ 171262457903llu,
1582
+ 215777175787llu,
1583
+ 271862205833llu,
1584
+ 342524915839llu,
1585
+ 431554351609llu,
1586
+ 543724411781llu,
1587
+ 685049831731llu,
1588
+ 863108703229llu,
1589
+ 1087448823553llu,
1590
+ 1370099663459llu,
1591
+ 1726217406467llu,
1592
+ 2174897647073llu,
1593
+ 2740199326961llu,
1594
+ 3452434812973llu,
1595
+ 4349795294267llu,
1596
+ 5480398654009llu,
1597
+ 6904869625999llu,
1598
+ 8699590588571llu,
1599
+ 10960797308051llu,
1600
+ 13809739252051llu,
1601
+ 17399181177241llu,
1602
+ 21921594616111llu,
1603
+ 27619478504183llu,
1604
+ 34798362354533llu,
1605
+ 43843189232363llu,
1606
+ 55238957008387llu,
1607
+ 69596724709081llu,
1608
+ 87686378464759llu,
1609
+ 110477914016779llu,
1610
+ 139193449418173llu,
1611
+ 175372756929481llu,
1612
+ 220955828033581llu,
1613
+ 278386898836457llu,
1614
+ 350745513859007llu,
1615
+ 441911656067171llu,
1616
+ 556773797672909llu,
1617
+ 701491027718027llu,
1618
+ 883823312134381llu,
1619
+ 1113547595345903llu,
1620
+ 1402982055436147llu,
1621
+ 1767646624268779llu,
1622
+ 2227095190691797llu,
1623
+ 2805964110872297llu,
1624
+ 3535293248537579llu,
1625
+ 4454190381383713llu,
1626
+ 5611928221744609llu,
1627
+ 7070586497075177llu,
1628
+ 8908380762767489llu,
1629
+ 11223856443489329llu,
1630
+ 14141172994150357llu,
1631
+ 17816761525534927llu,
1632
+ 22447712886978529llu,
1633
+ 28282345988300791llu,
1634
+ 35633523051069991llu,
1635
+ 44895425773957261llu,
1636
+ 56564691976601587llu,
1637
+ 71267046102139967llu,
1638
+ 89790851547914507llu,
1639
+ 113129383953203213llu,
1640
+ 142534092204280003llu,
1641
+ 179581703095829107llu,
1642
+ 226258767906406483llu,
1643
+ 285068184408560057llu,
1644
+ 359163406191658253llu,
1645
+ 452517535812813007llu,
1646
+ 570136368817120201llu,
1647
+ 718326812383316683llu,
1648
+ 905035071625626043llu,
1649
+ 1140272737634240411llu,
1650
+ 1436653624766633509llu,
1651
+ 1810070143251252131llu,
1652
+ 2280545475268481167llu,
1653
+ 2873307249533267101llu,
1654
+ 3620140286502504283llu,
1655
+ 4561090950536962147llu,
1656
+ 5746614499066534157llu,
1657
+ 7240280573005008577llu,
1658
+ 9122181901073924329llu,
1659
+ 11493228998133068689llu,
1660
+ 14480561146010017169llu,
1661
+ 18446744073709551557llu};
1662
+ // NOLINTNEXTLINE(*c-arrays*)
1663
+ static constexpr uint64_t (*const mod_functions[])(uint64_t) = {
1664
+ &mod0,
1665
+ &mod2,
1666
+ &mod3,
1667
+ &mod5,
1668
+ &mod7,
1669
+ &mod11,
1670
+ &mod13,
1671
+ &mod17,
1672
+ &mod23,
1673
+ &mod29,
1674
+ &mod37,
1675
+ &mod47,
1676
+ &mod59,
1677
+ &mod73,
1678
+ &mod97,
1679
+ &mod127,
1680
+ &mod151,
1681
+ &mod197,
1682
+ &mod251,
1683
+ &mod313,
1684
+ &mod397,
1685
+ &mod499,
1686
+ &mod631,
1687
+ &mod797,
1688
+ &mod1009,
1689
+ &mod1259,
1690
+ &mod1597,
1691
+ &mod2011,
1692
+ &mod2539,
1693
+ &mod3203,
1694
+ &mod4027,
1695
+ &mod5087,
1696
+ &mod6421,
1697
+ &mod8089,
1698
+ &mod10193,
1699
+ &mod12853,
1700
+ &mod16193,
1701
+ &mod20399,
1702
+ &mod25717,
1703
+ &mod32401,
1704
+ &mod40823,
1705
+ &mod51437,
1706
+ &mod64811,
1707
+ &mod81649,
1708
+ &mod102877,
1709
+ &mod129607,
1710
+ &mod163307,
1711
+ &mod205759,
1712
+ &mod259229,
1713
+ &mod326617,
1714
+ &mod411527,
1715
+ &mod518509,
1716
+ &mod653267,
1717
+ &mod823117,
1718
+ &mod1037059,
1719
+ &mod1306601,
1720
+ &mod1646237,
1721
+ &mod2074129,
1722
+ &mod2613229,
1723
+ &mod3292489,
1724
+ &mod4148279,
1725
+ &mod5226491,
1726
+ &mod6584983,
1727
+ &mod8296553,
1728
+ &mod10453007,
1729
+ &mod13169977,
1730
+ &mod16593127,
1731
+ &mod20906033,
1732
+ &mod26339969,
1733
+ &mod33186281,
1734
+ &mod41812097,
1735
+ &mod52679969,
1736
+ &mod66372617,
1737
+ &mod83624237,
1738
+ &mod105359939,
1739
+ &mod132745199,
1740
+ &mod167248483,
1741
+ &mod210719881,
1742
+ &mod265490441,
1743
+ &mod334496971,
1744
+ &mod421439783,
1745
+ &mod530980861,
1746
+ &mod668993977,
1747
+ &mod842879579,
1748
+ &mod1061961721,
1749
+ &mod1337987929,
1750
+ &mod1685759167,
1751
+ &mod2123923447,
1752
+ &mod2675975881,
1753
+ &mod3371518343,
1754
+ &mod4247846927,
1755
+ &mod5351951779,
1756
+ &mod6743036717,
1757
+ &mod8495693897,
1758
+ &mod10703903591,
1759
+ &mod13486073473,
1760
+ &mod16991387857,
1761
+ &mod21407807219,
1762
+ &mod26972146961,
1763
+ &mod33982775741,
1764
+ &mod42815614441,
1765
+ &mod53944293929,
1766
+ &mod67965551447,
1767
+ &mod85631228929,
1768
+ &mod107888587883,
1769
+ &mod135931102921,
1770
+ &mod171262457903,
1771
+ &mod215777175787,
1772
+ &mod271862205833,
1773
+ &mod342524915839,
1774
+ &mod431554351609,
1775
+ &mod543724411781,
1776
+ &mod685049831731,
1777
+ &mod863108703229,
1778
+ &mod1087448823553,
1779
+ &mod1370099663459,
1780
+ &mod1726217406467,
1781
+ &mod2174897647073,
1782
+ &mod2740199326961,
1783
+ &mod3452434812973,
1784
+ &mod4349795294267,
1785
+ &mod5480398654009,
1786
+ &mod6904869625999,
1787
+ &mod8699590588571,
1788
+ &mod10960797308051,
1789
+ &mod13809739252051,
1790
+ &mod17399181177241,
1791
+ &mod21921594616111,
1792
+ &mod27619478504183,
1793
+ &mod34798362354533,
1794
+ &mod43843189232363,
1795
+ &mod55238957008387,
1796
+ &mod69596724709081,
1797
+ &mod87686378464759,
1798
+ &mod110477914016779,
1799
+ &mod139193449418173,
1800
+ &mod175372756929481,
1801
+ &mod220955828033581,
1802
+ &mod278386898836457,
1803
+ &mod350745513859007,
1804
+ &mod441911656067171,
1805
+ &mod556773797672909,
1806
+ &mod701491027718027,
1807
+ &mod883823312134381,
1808
+ &mod1113547595345903,
1809
+ &mod1402982055436147,
1810
+ &mod1767646624268779,
1811
+ &mod2227095190691797,
1812
+ &mod2805964110872297,
1813
+ &mod3535293248537579,
1814
+ &mod4454190381383713,
1815
+ &mod5611928221744609,
1816
+ &mod7070586497075177,
1817
+ &mod8908380762767489,
1818
+ &mod11223856443489329,
1819
+ &mod14141172994150357,
1820
+ &mod17816761525534927,
1821
+ &mod22447712886978529,
1822
+ &mod28282345988300791,
1823
+ &mod35633523051069991,
1824
+ &mod44895425773957261,
1825
+ &mod56564691976601587,
1826
+ &mod71267046102139967,
1827
+ &mod89790851547914507,
1828
+ &mod113129383953203213,
1829
+ &mod142534092204280003,
1830
+ &mod179581703095829107,
1831
+ &mod226258767906406483,
1832
+ &mod285068184408560057,
1833
+ &mod359163406191658253,
1834
+ &mod452517535812813007,
1835
+ &mod570136368817120201,
1836
+ &mod718326812383316683,
1837
+ &mod905035071625626043,
1838
+ &mod1140272737634240411,
1839
+ &mod1436653624766633509,
1840
+ &mod1810070143251252131,
1841
+ &mod2280545475268481167,
1842
+ &mod2873307249533267101,
1843
+ &mod3620140286502504283,
1844
+ &mod4561090950536962147,
1845
+ &mod5746614499066534157,
1846
+ &mod7240280573005008577,
1847
+ &mod9122181901073924329,
1848
+ &mod11493228998133068689,
1849
+ &mod14480561146010017169,
1850
+ &mod18446744073709551557};
1851
+ const uint64_t* found = std::lower_bound(
1852
+ std::begin(prime_list), std::end(prime_list) - 1, size);
1853
+ size = *found;
1854
+ return mod_functions[1 + found - prime_list];
1855
+ }
1856
+ void commit(mod_function new_mod_function) {
1857
+ current_mod_function = new_mod_function;
1858
+ }
1859
+ void reset() {
1860
+ current_mod_function = &mod0;
1861
+ }
1862
+
1863
+ uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
1864
+ const {
1865
+ return current_mod_function(hash);
1866
+ }
1867
+ uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
1868
+ return index > num_slots_minus_one ? current_mod_function(index) : index;
1869
+ }
1870
+
1871
+ private:
1872
+ mod_function current_mod_function = &mod0;
1873
+ };
1874
+
1875
+ struct power_of_two_hash_policy {
1876
+ uint64_t index_for_hash(uint64_t hash, uint64_t num_slots_minus_one) const {
1877
+ return hash & num_slots_minus_one;
1878
+ }
1879
+ uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
1880
+ return index_for_hash(index, num_slots_minus_one);
1881
+ }
1882
+ int8_t next_size_over(uint64_t& size) const {
1883
+ size = detailv3::next_power_of_two(size);
1884
+ return 0;
1885
+ }
1886
+ void commit(int8_t) {}
1887
+ void reset() {}
1888
+ };
1889
+
1890
+ struct fibonacci_hash_policy {
1891
+ uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
1892
+ const {
1893
+ return (11400714819323198485ull * hash) >> shift;
1894
+ }
1895
+ uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
1896
+ return index & num_slots_minus_one;
1897
+ }
1898
+
1899
+ int8_t next_size_over(uint64_t& size) const {
1900
+ size = std::max(uint64_t(2), detailv3::next_power_of_two(size));
1901
+ return static_cast<int8_t>(64 - detailv3::log2(size));
1902
+ }
1903
+ void commit(int8_t shift_) {
1904
+ shift = shift_;
1905
+ }
1906
+ void reset() {
1907
+ shift = 63;
1908
+ }
1909
+
1910
+ private:
1911
+ int8_t shift = 63;
1912
+ };
1913
+
1914
+ template <
1915
+ typename K,
1916
+ typename V,
1917
+ typename H = std::hash<K>,
1918
+ typename E = std::equal_to<K>,
1919
+ typename A = std::allocator<std::pair<K, V>>>
1920
+ class flat_hash_map
1921
+ : public detailv3::sherwood_v3_table<
1922
+ std::pair<K, V>,
1923
+ K,
1924
+ H,
1925
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
1926
+ E,
1927
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
1928
+ A,
1929
+ typename std::allocator_traits<A>::template rebind_alloc<
1930
+ detailv3::sherwood_v3_entry<std::pair<K, V>>>> {
1931
+ using Table = detailv3::sherwood_v3_table<
1932
+ std::pair<K, V>,
1933
+ K,
1934
+ H,
1935
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
1936
+ E,
1937
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
1938
+ A,
1939
+ typename std::allocator_traits<A>::template rebind_alloc<
1940
+ detailv3::sherwood_v3_entry<std::pair<K, V>>>>;
1941
+
1942
+ public:
1943
+ using key_type = K;
1944
+ using mapped_type = V;
1945
+
1946
+ using Table::Table;
1947
+ flat_hash_map() = default;
1948
+
1949
+ inline V& operator[](const K& key) {
1950
+ return emplace(key, convertible_to_value()).first->second;
1951
+ }
1952
+ inline V& operator[](K&& key) {
1953
+ return emplace(std::move(key), convertible_to_value()).first->second;
1954
+ }
1955
+ V& at(const K& key) {
1956
+ auto found = this->find(key);
1957
+ if (found == this->end())
1958
+ throw std::out_of_range("Argument passed to at() was not in the map.");
1959
+ return found->second;
1960
+ }
1961
+ const V& at(const K& key) const {
1962
+ auto found = this->find(key);
1963
+ if (found == this->end())
1964
+ throw std::out_of_range("Argument passed to at() was not in the map.");
1965
+ return found->second;
1966
+ }
1967
+
1968
+ using Table::emplace;
1969
+ std::pair<typename Table::iterator, bool> emplace() {
1970
+ return emplace(key_type(), convertible_to_value());
1971
+ }
1972
+ template <typename M>
1973
+ std::pair<typename Table::iterator, bool> insert_or_assign(
1974
+ const key_type& key,
1975
+ M&& m) {
1976
+ auto emplace_result = emplace(key, std::forward<M>(m));
1977
+ if (!emplace_result.second)
1978
+ emplace_result.first->second = std::forward<M>(m);
1979
+ return emplace_result;
1980
+ }
1981
+ template <typename M>
1982
+ std::pair<typename Table::iterator, bool> insert_or_assign(
1983
+ key_type&& key,
1984
+ M&& m) {
1985
+ auto emplace_result = emplace(std::move(key), std::forward<M>(m));
1986
+ if (!emplace_result.second)
1987
+ emplace_result.first->second = std::forward<M>(m);
1988
+ return emplace_result;
1989
+ }
1990
+ template <typename M>
1991
+ typename Table::iterator insert_or_assign(
1992
+ typename Table::const_iterator,
1993
+ const key_type& key,
1994
+ M&& m) {
1995
+ return insert_or_assign(key, std::forward<M>(m)).first;
1996
+ }
1997
+ template <typename M>
1998
+ typename Table::iterator insert_or_assign(
1999
+ typename Table::const_iterator,
2000
+ key_type&& key,
2001
+ M&& m) {
2002
+ return insert_or_assign(std::move(key), std::forward<M>(m)).first;
2003
+ }
2004
+
2005
+ friend bool operator==(const flat_hash_map& lhs, const flat_hash_map& rhs) {
2006
+ if (lhs.size() != rhs.size())
2007
+ return false;
2008
+ for (const typename Table::value_type& value : lhs) {
2009
+ auto found = rhs.find(value.first);
2010
+ if (found == rhs.end() || value.second != found->second)
2011
+ return false;
2012
+ }
2013
+ return true;
2014
+ }
2015
+ friend bool operator!=(const flat_hash_map& lhs, const flat_hash_map& rhs) {
2016
+ return !(lhs == rhs);
2017
+ }
2018
+
2019
+ private:
2020
+ struct convertible_to_value {
2021
+ operator V() const {
2022
+ return V();
2023
+ }
2024
+ };
2025
+ };
2026
+
2027
+ template <
2028
+ typename T,
2029
+ typename H = std::hash<T>,
2030
+ typename E = std::equal_to<T>,
2031
+ typename A = std::allocator<T>>
2032
+ class flat_hash_set
2033
+ : public detailv3::sherwood_v3_table<
2034
+ T,
2035
+ T,
2036
+ H,
2037
+ detailv3::functor_storage<uint64_t, H>,
2038
+ E,
2039
+ detailv3::functor_storage<bool, E>,
2040
+ A,
2041
+ typename std::allocator_traits<A>::template rebind_alloc<
2042
+ detailv3::sherwood_v3_entry<T>>> {
2043
+ using Table = detailv3::sherwood_v3_table<
2044
+ T,
2045
+ T,
2046
+ H,
2047
+ detailv3::functor_storage<uint64_t, H>,
2048
+ E,
2049
+ detailv3::functor_storage<bool, E>,
2050
+ A,
2051
+ typename std::allocator_traits<A>::template rebind_alloc<
2052
+ detailv3::sherwood_v3_entry<T>>>;
2053
+
2054
+ public:
2055
+ using key_type = T;
2056
+
2057
+ using Table::Table;
2058
+ flat_hash_set() = default;
2059
+
2060
+ template <typename... Args>
2061
+ std::pair<typename Table::iterator, bool> emplace(Args&&... args) {
2062
+ return Table::emplace(T(std::forward<Args>(args)...));
2063
+ }
2064
+ std::pair<typename Table::iterator, bool> emplace(const key_type& arg) {
2065
+ return Table::emplace(arg);
2066
+ }
2067
+ std::pair<typename Table::iterator, bool> emplace(key_type& arg) {
2068
+ return Table::emplace(arg);
2069
+ }
2070
+ std::pair<typename Table::iterator, bool> emplace(const key_type&& arg) {
2071
+ return Table::emplace(std::move(arg));
2072
+ }
2073
+ std::pair<typename Table::iterator, bool> emplace(key_type&& arg) {
2074
+ return Table::emplace(std::move(arg));
2075
+ }
2076
+
2077
+ friend bool operator==(const flat_hash_set& lhs, const flat_hash_set& rhs) {
2078
+ if (lhs.size() != rhs.size())
2079
+ return false;
2080
+ for (const T& value : lhs) {
2081
+ if (rhs.find(value) == rhs.end())
2082
+ return false;
2083
+ }
2084
+ return true;
2085
+ }
2086
+ friend bool operator!=(const flat_hash_set& lhs, const flat_hash_set& rhs) {
2087
+ return !(lhs == rhs);
2088
+ }
2089
+ };
2090
+
2091
+ template <typename T>
2092
+ struct power_of_two_std_hash : std::hash<T> {
2093
+ typedef ska::power_of_two_hash_policy hash_policy;
2094
+ };
2095
+
2096
+ } // end namespace ska
2097
+
2098
+ C10_CLANG_DIAGNOSTIC_POP()
2099
+
2100
+ #if defined(_MSC_VER) && !defined(__clang__)
2101
+ #pragma warning(pop)
2102
+ #endif
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/floating_point_utils.h ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Macros.h>
4
+ #include <c10/util/bit_cast.h>
5
+ #include <cstdint>
6
+
7
+ namespace c10::detail {
8
+
9
+ C10_HOST_DEVICE inline float fp32_from_bits(uint32_t w) {
10
+ #if defined(__OPENCL_VERSION__)
11
+ return as_float(w);
12
+ #elif defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
13
+ return __uint_as_float((unsigned int)w);
14
+ #elif defined(__INTEL_COMPILER)
15
+ return _castu32_f32(w);
16
+ #else
17
+ return c10::bit_cast<float>(w);
18
+ #endif
19
+ }
20
+
21
+ C10_HOST_DEVICE inline uint32_t fp32_to_bits(float f) {
22
+ #if defined(__OPENCL_VERSION__)
23
+ return as_uint(f);
24
+ #elif defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
25
+ return (uint32_t)__float_as_uint(f);
26
+ #elif defined(__INTEL_COMPILER)
27
+ return _castf32_u32(f);
28
+ #else
29
+ return c10::bit_cast<uint32_t>(f);
30
+ #endif
31
+ }
32
+
33
+ } // namespace c10::detail
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/generic_math.h ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Macros.h>
4
+ #include <c10/util/TypeSafeSignMath.h>
5
+ #include <cmath>
6
+
7
+ #if defined(__CUDA_ARCH__)
8
+ #include <c10/cuda/CUDAMathCompat.h>
9
+ #define C10_COMPAT_COPYSIGN c10::cuda::compat::copysign
10
+ #elif defined(__HIPCC__)
11
+ #include <c10/hip/HIPMathCompat.h>
12
+ #define C10_COMPAT_COPYSIGN c10::hip::compat::copysign
13
+ #else
14
+ #include <c10/util/copysign.h>
15
+ #define C10_COMPAT_COPYSIGN c10::copysign
16
+ #endif
17
+
18
+ // The functions in this file should be header-only as it is used under
19
+ // ABI-compatibility mode.
20
+
21
+ namespace c10 {
22
+
23
+ // NOTE: [Floor Division in Python]
24
+ // Python's __floordiv__ operator is more complicated than just floor(a / b).
25
+ // It aims to maintain the property: a == (a // b) * b + remainder(a, b)
26
+ // which can otherwise fail due to rounding errors in the remainder.
27
+ // So, instead it is calculated as: a // b = (a - remainder(a, b)) / b
28
+ // With some additional fix-ups added to the result.
29
+ //
30
+ // For reference, see CPython's implementation:
31
+ // https://github.com/python/cpython/blob/ace008c531dd685a30c1dd68f9b5ba35f20171cf/Objects/floatobject.c#L636
32
+
33
+ template <typename scalar_t>
34
+ inline C10_HOST_DEVICE scalar_t div_floor_floating(scalar_t a, scalar_t b)
35
+ __ubsan_ignore_float_divide_by_zero__ {
36
+ if (C10_UNLIKELY(b == 0)) {
37
+ // Divide by zero: return standard IEEE result
38
+ return a / b;
39
+ }
40
+
41
+ auto mod = std::fmod(a, b);
42
+ auto div = (a - mod) / b;
43
+ if ((mod != 0) && (b < 0) != (mod < 0)) {
44
+ div -= scalar_t(1);
45
+ }
46
+
47
+ scalar_t floordiv;
48
+ if (div != 0) {
49
+ floordiv = std::floor(div);
50
+ if (div - floordiv > scalar_t(0.5)) {
51
+ floordiv += scalar_t(1.0);
52
+ }
53
+ } else {
54
+ floordiv = C10_COMPAT_COPYSIGN(scalar_t(0), a / b);
55
+ }
56
+ return floordiv;
57
+ }
58
+
59
+ template <typename scalar_t>
60
+ inline C10_HOST_DEVICE scalar_t div_floor_integer(scalar_t a, scalar_t b) {
61
+ if (c10::signs_differ(a, b)) {
62
+ // Subtracts one from the results of truncation division if the
63
+ // divisor and dividend have different sign(bit)s and the remainder of
64
+ // the division is nonzero
65
+ const auto quot = a / b;
66
+ const auto rem = a % b;
67
+ return rem ? quot - 1 : quot;
68
+ }
69
+ return a / b;
70
+ }
71
+
72
+ template <
73
+ typename scalar_t,
74
+ std::enable_if_t<std::is_floating_point_v<scalar_t>, int> = 0>
75
+ inline C10_HOST_DEVICE scalar_t div_mod(scalar_t a, scalar_t b)
76
+ __ubsan_ignore_float_divide_by_zero__ {
77
+ if (C10_UNLIKELY(b == 0)) {
78
+ // Divide by zero: return standard IEEE result
79
+ return std::fmod(a, b);
80
+ }
81
+
82
+ auto mod = std::fmod(a, b);
83
+ if (mod == 0) {
84
+ mod = C10_COMPAT_COPYSIGN(scalar_t(0), b);
85
+ } else if ((b < 0) != (mod < 0)) {
86
+ mod += b;
87
+ }
88
+ return mod;
89
+ }
90
+
91
+ template <
92
+ typename scalar_t,
93
+ std::enable_if_t<std::is_integral_v<scalar_t>, int> = 0>
94
+ inline C10_HOST_DEVICE scalar_t div_mod(scalar_t a, scalar_t b) {
95
+ auto mod = a % b;
96
+ if ((b < 0) != (mod < 0)) {
97
+ mod += b;
98
+ }
99
+ return mod;
100
+ }
101
+
102
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/hash.h ADDED
@@ -0,0 +1,379 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/util/Exception.h>
4
+ #include <cstddef>
5
+ #include <functional>
6
+ #include <iomanip>
7
+ #include <ios>
8
+ #include <sstream>
9
+ #include <string>
10
+ #include <tuple>
11
+ #include <type_traits>
12
+ #include <utility>
13
+ #include <vector>
14
+
15
+ #include <c10/util/ArrayRef.h>
16
+ #include <c10/util/complex.h>
17
+
18
+ namespace c10 {
19
+
20
+ // NOTE: hash_combine and SHA1 hashing is based on implementation from Boost
21
+ //
22
+ // Boost Software License - Version 1.0 - August 17th, 2003
23
+ //
24
+ // Permission is hereby granted, free of charge, to any person or organization
25
+ // obtaining a copy of the software and accompanying documentation covered by
26
+ // this license (the "Software") to use, reproduce, display, distribute,
27
+ // execute, and transmit the Software, and to prepare derivative works of the
28
+ // Software, and to permit third-parties to whom the Software is furnished to
29
+ // do so, all subject to the following:
30
+ //
31
+ // The copyright notices in the Software and this entire statement, including
32
+ // the above license grant, this restriction and the following disclaimer,
33
+ // must be included in all copies of the Software, in whole or in part, and
34
+ // all derivative works of the Software, unless such copies or derivative
35
+ // works are solely in the form of machine-executable object code generated by
36
+ // a source language processor.
37
+ //
38
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40
+ // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
41
+ // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
42
+ // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
43
+ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
44
+ // DEALINGS IN THE SOFTWARE.
45
+
46
+ inline size_t hash_combine(size_t seed, size_t value) {
47
+ return seed ^ (value + 0x9e3779b9 + (seed << 6u) + (seed >> 2u));
48
+ }
49
+
50
+ // Creates the SHA1 hash of a string. A 160-bit hash.
51
+ // Based on the implementation in Boost (see notice above).
52
+ // Note that SHA1 hashes are no longer considered cryptographically
53
+ // secure, but are the standard hash for generating unique ids.
54
+ // Usage:
55
+ // // Let 'code' be a std::string
56
+ // c10::sha1 sha1_hash{code};
57
+ // const auto hash_code = sha1_hash.str();
58
+ // TODO: Compare vs OpenSSL and/or CryptoPP implementations
59
+ struct sha1 {
60
+ typedef unsigned int(digest_type)[5];
61
+
62
+ sha1(const std::string& s = "") {
63
+ if (!s.empty()) {
64
+ reset();
65
+ process_bytes(s.c_str(), s.size());
66
+ }
67
+ }
68
+
69
+ void reset() {
70
+ h_[0] = 0x67452301;
71
+ h_[1] = 0xEFCDAB89;
72
+ h_[2] = 0x98BADCFE;
73
+ h_[3] = 0x10325476;
74
+ h_[4] = 0xC3D2E1F0;
75
+
76
+ block_byte_index_ = 0;
77
+ bit_count_low = 0;
78
+ bit_count_high = 0;
79
+ }
80
+
81
+ std::string str() {
82
+ unsigned int digest[5];
83
+ get_digest(digest);
84
+
85
+ std::ostringstream buf;
86
+ for (unsigned int i : digest) {
87
+ buf << std::hex << std::setfill('0') << std::setw(8) << i;
88
+ }
89
+
90
+ return buf.str();
91
+ }
92
+
93
+ private:
94
+ unsigned int left_rotate(unsigned int x, std::size_t n) {
95
+ return (x << n) ^ (x >> (32 - n));
96
+ }
97
+
98
+ void process_block_impl() {
99
+ unsigned int w[80];
100
+
101
+ for (std::size_t i = 0; i < 16; ++i) {
102
+ w[i] = (block_[i * 4 + 0] << 24);
103
+ w[i] |= (block_[i * 4 + 1] << 16);
104
+ w[i] |= (block_[i * 4 + 2] << 8);
105
+ w[i] |= (block_[i * 4 + 3]);
106
+ }
107
+
108
+ for (std::size_t i = 16; i < 80; ++i) {
109
+ w[i] = left_rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1);
110
+ }
111
+
112
+ unsigned int a = h_[0];
113
+ unsigned int b = h_[1];
114
+ unsigned int c = h_[2];
115
+ unsigned int d = h_[3];
116
+ unsigned int e = h_[4];
117
+
118
+ for (std::size_t i = 0; i < 80; ++i) {
119
+ unsigned int f = 0;
120
+ unsigned int k = 0;
121
+
122
+ if (i < 20) {
123
+ f = (b & c) | (~b & d);
124
+ k = 0x5A827999;
125
+ } else if (i < 40) {
126
+ f = b ^ c ^ d;
127
+ k = 0x6ED9EBA1;
128
+ } else if (i < 60) {
129
+ f = (b & c) | (b & d) | (c & d);
130
+ k = 0x8F1BBCDC;
131
+ } else {
132
+ f = b ^ c ^ d;
133
+ k = 0xCA62C1D6;
134
+ }
135
+
136
+ unsigned temp = left_rotate(a, 5) + f + e + k + w[i];
137
+ e = d;
138
+ d = c;
139
+ c = left_rotate(b, 30);
140
+ b = a;
141
+ a = temp;
142
+ }
143
+
144
+ h_[0] += a;
145
+ h_[1] += b;
146
+ h_[2] += c;
147
+ h_[3] += d;
148
+ h_[4] += e;
149
+ }
150
+
151
+ void process_byte_impl(unsigned char byte) {
152
+ block_[block_byte_index_++] = byte;
153
+
154
+ if (block_byte_index_ == 64) {
155
+ block_byte_index_ = 0;
156
+ process_block_impl();
157
+ }
158
+ }
159
+
160
+ void process_byte(unsigned char byte) {
161
+ process_byte_impl(byte);
162
+
163
+ // size_t max value = 0xFFFFFFFF
164
+ // if (bit_count_low + 8 >= 0x100000000) { // would overflow
165
+ // if (bit_count_low >= 0x100000000-8) {
166
+ if (bit_count_low < 0xFFFFFFF8) {
167
+ bit_count_low += 8;
168
+ } else {
169
+ bit_count_low = 0;
170
+
171
+ if (bit_count_high <= 0xFFFFFFFE) {
172
+ ++bit_count_high;
173
+ } else {
174
+ TORCH_CHECK(false, "sha1 too many bytes");
175
+ }
176
+ }
177
+ }
178
+
179
+ void process_block(void const* bytes_begin, void const* bytes_end) {
180
+ unsigned char const* begin = static_cast<unsigned char const*>(bytes_begin);
181
+ unsigned char const* end = static_cast<unsigned char const*>(bytes_end);
182
+ for (; begin != end; ++begin) {
183
+ process_byte(*begin);
184
+ }
185
+ }
186
+
187
+ void process_bytes(void const* buffer, std::size_t byte_count) {
188
+ unsigned char const* b = static_cast<unsigned char const*>(buffer);
189
+ process_block(b, b + byte_count);
190
+ }
191
+
192
+ void get_digest(digest_type& digest) {
193
+ // append the bit '1' to the message
194
+ process_byte_impl(0x80);
195
+
196
+ // append k bits '0', where k is the minimum number >= 0
197
+ // such that the resulting message length is congruent to 56 (mod 64)
198
+ // check if there is enough space for padding and bit_count
199
+ if (block_byte_index_ > 56) {
200
+ // finish this block
201
+ while (block_byte_index_ != 0) {
202
+ process_byte_impl(0);
203
+ }
204
+
205
+ // one more block
206
+ while (block_byte_index_ < 56) {
207
+ process_byte_impl(0);
208
+ }
209
+ } else {
210
+ while (block_byte_index_ < 56) {
211
+ process_byte_impl(0);
212
+ }
213
+ }
214
+
215
+ // append length of message (before pre-processing)
216
+ // as a 64-bit big-endian integer
217
+ process_byte_impl(
218
+ static_cast<unsigned char>((bit_count_high >> 24) & 0xFF));
219
+ process_byte_impl(
220
+ static_cast<unsigned char>((bit_count_high >> 16) & 0xFF));
221
+ process_byte_impl(static_cast<unsigned char>((bit_count_high >> 8) & 0xFF));
222
+ process_byte_impl(static_cast<unsigned char>((bit_count_high) & 0xFF));
223
+ process_byte_impl(static_cast<unsigned char>((bit_count_low >> 24) & 0xFF));
224
+ process_byte_impl(static_cast<unsigned char>((bit_count_low >> 16) & 0xFF));
225
+ process_byte_impl(static_cast<unsigned char>((bit_count_low >> 8) & 0xFF));
226
+ process_byte_impl(static_cast<unsigned char>((bit_count_low) & 0xFF));
227
+
228
+ // get final digest
229
+ digest[0] = h_[0];
230
+ digest[1] = h_[1];
231
+ digest[2] = h_[2];
232
+ digest[3] = h_[3];
233
+ digest[4] = h_[4];
234
+ }
235
+
236
+ unsigned int h_[5]{};
237
+ unsigned char block_[64]{};
238
+ std::size_t block_byte_index_{};
239
+ std::size_t bit_count_low{};
240
+ std::size_t bit_count_high{};
241
+ };
242
+
243
+ constexpr uint64_t twang_mix64(uint64_t key) noexcept {
244
+ key = (~key) + (key << 21); // key *= (1 << 21) - 1; key -= 1;
245
+ key = key ^ (key >> 24);
246
+ key = key + (key << 3) + (key << 8); // key *= 1 + (1 << 3) + (1 << 8)
247
+ key = key ^ (key >> 14);
248
+ key = key + (key << 2) + (key << 4); // key *= 1 + (1 << 2) + (1 << 4)
249
+ key = key ^ (key >> 28);
250
+ key = key + (key << 31); // key *= 1 + (1 << 31)
251
+ return key;
252
+ }
253
+
254
+ ////////////////////////////////////////////////////////////////////////////////
255
+ // c10::hash implementation
256
+ ////////////////////////////////////////////////////////////////////////////////
257
+
258
+ namespace _hash_detail {
259
+
260
+ // Use template argument deduction to shorten calls to c10::hash
261
+ template <typename T>
262
+ size_t simple_get_hash(const T& o);
263
+
264
+ template <typename T, typename V>
265
+ using type_if_not_enum = std::enable_if_t<!std::is_enum_v<T>, V>;
266
+
267
+ // Use SFINAE to dispatch to std::hash if possible, cast enum types to int
268
+ // automatically, and fall back to T::hash otherwise. NOTE: C++14 added support
269
+ // for hashing enum types to the standard, and some compilers implement it even
270
+ // when C++14 flags aren't specified. This is why we have to disable this
271
+ // overload if T is an enum type (and use the one below in this case).
272
+ template <typename T>
273
+ auto dispatch_hash(const T& o)
274
+ -> decltype(std::hash<T>()(o), type_if_not_enum<T, size_t>()) {
275
+ return std::hash<T>()(o);
276
+ }
277
+
278
+ template <typename T>
279
+ std::enable_if_t<std::is_enum_v<T>, size_t> dispatch_hash(const T& o) {
280
+ using R = std::underlying_type_t<T>;
281
+ return std::hash<R>()(static_cast<R>(o));
282
+ }
283
+
284
+ template <typename T>
285
+ auto dispatch_hash(const T& o) -> decltype(T::hash(o), size_t()) {
286
+ return T::hash(o);
287
+ }
288
+
289
+ } // namespace _hash_detail
290
+
291
+ // Hasher struct
292
+ template <typename T>
293
+ struct hash {
294
+ size_t operator()(const T& o) const {
295
+ return _hash_detail::dispatch_hash(o);
296
+ }
297
+ };
298
+
299
+ // Specialization for std::tuple
300
+ template <typename... Types>
301
+ struct hash<std::tuple<Types...>> {
302
+ template <size_t idx, typename... Ts>
303
+ struct tuple_hash {
304
+ size_t operator()(const std::tuple<Ts...>& t) const {
305
+ return hash_combine(
306
+ _hash_detail::simple_get_hash(std::get<idx>(t)),
307
+ tuple_hash<idx - 1, Ts...>()(t));
308
+ }
309
+ };
310
+
311
+ template <typename... Ts>
312
+ struct tuple_hash<0, Ts...> {
313
+ size_t operator()(const std::tuple<Ts...>& t) const {
314
+ return _hash_detail::simple_get_hash(std::get<0>(t));
315
+ }
316
+ };
317
+
318
+ size_t operator()(const std::tuple<Types...>& t) const {
319
+ return tuple_hash<sizeof...(Types) - 1, Types...>()(t);
320
+ }
321
+ };
322
+
323
+ template <typename T1, typename T2>
324
+ struct hash<std::pair<T1, T2>> {
325
+ size_t operator()(const std::pair<T1, T2>& pair) const {
326
+ std::tuple<T1, T2> tuple = std::make_tuple(pair.first, pair.second);
327
+ return _hash_detail::simple_get_hash(tuple);
328
+ }
329
+ };
330
+
331
+ template <typename T>
332
+ struct hash<c10::ArrayRef<T>> {
333
+ size_t operator()(c10::ArrayRef<T> v) const {
334
+ size_t seed = 0;
335
+ for (const auto& elem : v) {
336
+ seed = hash_combine(seed, _hash_detail::simple_get_hash(elem));
337
+ }
338
+ return seed;
339
+ }
340
+ };
341
+
342
+ // Specialization for std::vector
343
+ template <typename T>
344
+ struct hash<std::vector<T>> {
345
+ size_t operator()(const std::vector<T>& v) const {
346
+ return hash<c10::ArrayRef<T>>()(v);
347
+ }
348
+ };
349
+
350
+ namespace _hash_detail {
351
+
352
+ template <typename T>
353
+ size_t simple_get_hash(const T& o) {
354
+ return c10::hash<T>()(o);
355
+ }
356
+
357
+ } // namespace _hash_detail
358
+
359
+ // Use this function to actually hash multiple things in one line.
360
+ // Dispatches to c10::hash, so it can hash containers.
361
+ // Example:
362
+ //
363
+ // static size_t hash(const MyStruct& s) {
364
+ // return get_hash(s.member1, s.member2, s.member3);
365
+ // }
366
+ template <typename... Types>
367
+ size_t get_hash(const Types&... args) {
368
+ return c10::hash<decltype(std::tie(args...))>()(std::tie(args...));
369
+ }
370
+
371
+ // Specialization for c10::complex
372
+ template <typename T>
373
+ struct hash<c10::complex<T>> {
374
+ size_t operator()(const c10::complex<T>& c) const {
375
+ return get_hash(c.real(), c.imag());
376
+ }
377
+ };
378
+
379
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/int128.h ADDED
@@ -0,0 +1,398 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is based on the uint128 implementation of protobuf at
2
+ // https://github.com/protocolbuffers/protobuf/blob/1e88936fce10cf773cb72b44c6a7f48b38c7578b/src/google/protobuf/stubs/int128.h
3
+ //
4
+ // Protocol Buffers - Google's data interchange format
5
+ // Copyright 2008 Google Inc. All rights reserved.
6
+ // https://developers.google.com/protocol-buffers/
7
+ //
8
+ // Redistribution and use in source and binary forms, with or without
9
+ // modification, are permitted provided that the following conditions are
10
+ // met:
11
+ //
12
+ // * Redistributions of source code must retain the above copyright
13
+ // notice, this list of conditions and the following disclaimer.
14
+ // * Redistributions in binary form must reproduce the above
15
+ // copyright notice, this list of conditions and the following disclaimer
16
+ // in the documentation and/or other materials provided with the
17
+ // distribution.
18
+ // * Neither the name of Google Inc. nor the names of its
19
+ // contributors may be used to endorse or promote products derived from
20
+ // this software without specific prior written permission.
21
+ //
22
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ #pragma once
34
+
35
+ #include <c10/macros/Export.h>
36
+ #include <cstdint>
37
+ #include <iosfwd>
38
+
39
+ namespace c10 {
40
+
41
+ struct uint128_pod;
42
+
43
+ // TODO(xiaofeng): Define GOOGLE_PROTOBUF_HAS_CONSTEXPR when constexpr is
44
+ // available.
45
+ #ifdef GOOGLE_PROTOBUF_HAS_CONSTEXPR
46
+ #define UINT128_CONSTEXPR constexpr
47
+ #else
48
+ #define UINT128_CONSTEXPR
49
+ #endif
50
+
51
+ class uint128;
52
+ inline uint128& operator<<=(uint128& self, int amount);
53
+
54
+ // An unsigned 128-bit integer type. Thread-compatible.
55
+ class C10_API uint128 {
56
+ public:
57
+ UINT128_CONSTEXPR uint128(); // Sets to 0, but don't trust on this behavior.
58
+ UINT128_CONSTEXPR uint128(uint64_t top, uint64_t bottom);
59
+ #ifndef SWIG
60
+ UINT128_CONSTEXPR uint128(int bottom);
61
+ UINT128_CONSTEXPR uint128(uint32_t bottom); // Top 96 bits = 0
62
+ #endif
63
+ UINT128_CONSTEXPR uint128(uint64_t bottom); // hi_ = 0
64
+ UINT128_CONSTEXPR uint128(const uint128_pod& val);
65
+
66
+ // Trivial copy constructor, assignment operator and destructor.
67
+
68
+ void Initialize(uint64_t top, uint64_t bottom);
69
+
70
+ // Arithmetic operators.
71
+ uint128& operator+=(const uint128& b);
72
+ uint128& operator-=(const uint128& b);
73
+ uint128& operator*=(const uint128& b);
74
+ // Long division/modulo for uint128.
75
+ uint128& operator/=(const uint128& b);
76
+ uint128& operator%=(const uint128& b);
77
+ uint128 operator++(int);
78
+ uint128 operator--(int);
79
+ // Make msvc happy with using operator<<= from DivModImpl
80
+ // which is a static function, and linker complained about missing
81
+ // static version of this overload
82
+ friend uint128& operator<<=(uint128&, int);
83
+ uint128& operator>>=(int);
84
+ uint128& operator&=(const uint128& b);
85
+ uint128& operator|=(const uint128& b);
86
+ uint128& operator^=(const uint128& b);
87
+ uint128& operator++();
88
+ uint128& operator--();
89
+
90
+ friend uint64_t Uint128Low64(const uint128& v);
91
+ friend uint64_t Uint128High64(const uint128& v);
92
+
93
+ // We add "std::" to avoid including all of port.h.
94
+ C10_API friend std::ostream& operator<<(std::ostream& o, const uint128& b);
95
+
96
+ private:
97
+ static void DivModImpl(
98
+ uint128 dividend,
99
+ uint128 divisor,
100
+ uint128* quotient_ret,
101
+ uint128* remainder_ret);
102
+
103
+ // Little-endian memory order optimizations can benefit from
104
+ // having lo_ first, hi_ last.
105
+ // See util/endian/endian.h and Load128/Store128 for storing a uint128.
106
+ uint64_t lo_;
107
+ uint64_t hi_;
108
+
109
+ // Not implemented, just declared for catching automatic type conversions.
110
+ uint128(uint8_t);
111
+ uint128(uint16_t);
112
+ uint128(float v);
113
+ uint128(double v);
114
+ };
115
+
116
+ // This is a POD form of uint128 which can be used for static variables which
117
+ // need to be operated on as uint128.
118
+ struct uint128_pod {
119
+ // Note: The ordering of fields is different than 'class uint128' but the
120
+ // same as its 2-arg constructor. This enables more obvious initialization
121
+ // of static instances, which is the primary reason for this struct in the
122
+ // first place. This does not seem to defeat any optimizations wrt
123
+ // operations involving this struct.
124
+ uint64_t hi;
125
+ uint64_t lo;
126
+ };
127
+
128
+ C10_API extern const uint128_pod kuint128max;
129
+
130
+ // allow uint128 to be logged
131
+ C10_API extern std::ostream& operator<<(std::ostream& o, const uint128& b);
132
+
133
+ // Methods to access low and high pieces of 128-bit value.
134
+ // Defined externally from uint128 to facilitate conversion
135
+ // to native 128-bit types when compilers support them.
136
+ inline uint64_t Uint128Low64(const uint128& v) {
137
+ return v.lo_;
138
+ }
139
+ inline uint64_t Uint128High64(const uint128& v) {
140
+ return v.hi_;
141
+ }
142
+
143
+ // TODO: perhaps it would be nice to have int128, a signed 128-bit type?
144
+
145
+ // --------------------------------------------------------------------------
146
+ // Implementation details follow
147
+ // --------------------------------------------------------------------------
148
+ inline bool operator==(const uint128& lhs, const uint128& rhs) {
149
+ return (
150
+ Uint128Low64(lhs) == Uint128Low64(rhs) &&
151
+ Uint128High64(lhs) == Uint128High64(rhs));
152
+ }
153
+ inline bool operator!=(const uint128& lhs, const uint128& rhs) {
154
+ return !(lhs == rhs);
155
+ }
156
+
157
+ C10_API inline UINT128_CONSTEXPR uint128::uint128() : lo_(0), hi_(0) {}
158
+ C10_API inline UINT128_CONSTEXPR uint128::uint128(uint64_t top, uint64_t bottom)
159
+ : lo_(bottom), hi_(top) {}
160
+ C10_API inline UINT128_CONSTEXPR uint128::uint128(const uint128_pod& v)
161
+ : lo_(v.lo), hi_(v.hi) {}
162
+ C10_API inline UINT128_CONSTEXPR uint128::uint128(uint64_t bottom)
163
+ : lo_(bottom), hi_(0) {}
164
+ #ifndef SWIG
165
+ C10_API inline UINT128_CONSTEXPR uint128::uint128(uint32_t bottom)
166
+ : lo_(bottom), hi_(0) {}
167
+ C10_API inline UINT128_CONSTEXPR uint128::uint128(int bottom)
168
+ : lo_(bottom), hi_(static_cast<int64_t>((bottom < 0) ? -1 : 0)) {}
169
+ #endif
170
+
171
+ #undef UINT128_CONSTEXPR
172
+
173
+ C10_API inline void uint128::Initialize(uint64_t top, uint64_t bottom) {
174
+ hi_ = top;
175
+ lo_ = bottom;
176
+ }
177
+
178
+ // Comparison operators.
179
+
180
+ #define CMP128(op) \
181
+ inline bool operator op(const uint128& lhs, const uint128& rhs) { \
182
+ return (Uint128High64(lhs) == Uint128High64(rhs)) \
183
+ ? (Uint128Low64(lhs) op Uint128Low64(rhs)) \
184
+ : (Uint128High64(lhs) op Uint128High64(rhs)); \
185
+ }
186
+
187
+ CMP128(<)
188
+ CMP128(>)
189
+ CMP128(>=)
190
+ CMP128(<=)
191
+
192
+ #undef CMP128
193
+
194
+ // Unary operators
195
+
196
+ inline uint128 operator-(const uint128& val) {
197
+ const uint64_t hi_flip = ~Uint128High64(val);
198
+ const uint64_t lo_flip = ~Uint128Low64(val);
199
+ const uint64_t lo_add = lo_flip + 1;
200
+ if (lo_add < lo_flip) {
201
+ return uint128(hi_flip + 1, lo_add);
202
+ }
203
+ return uint128(hi_flip, lo_add);
204
+ }
205
+
206
+ inline bool operator!(const uint128& val) {
207
+ return !Uint128High64(val) && !Uint128Low64(val);
208
+ }
209
+
210
+ // Logical operators.
211
+
212
+ inline uint128 operator~(const uint128& val) {
213
+ return uint128(~Uint128High64(val), ~Uint128Low64(val));
214
+ }
215
+
216
+ #define LOGIC128(op) \
217
+ inline uint128 operator op(const uint128& lhs, const uint128& rhs) { \
218
+ return uint128( \
219
+ Uint128High64(lhs) op Uint128High64(rhs), \
220
+ Uint128Low64(lhs) op Uint128Low64(rhs)); \
221
+ }
222
+
223
+ LOGIC128(|)
224
+ LOGIC128(&)
225
+ LOGIC128(^)
226
+
227
+ #undef LOGIC128
228
+
229
+ #define LOGICASSIGN128(op) \
230
+ C10_API inline uint128& uint128::operator op(const uint128 & other) { \
231
+ hi_ op other.hi_; \
232
+ lo_ op other.lo_; \
233
+ return *this; \
234
+ }
235
+
236
+ LOGICASSIGN128(|=)
237
+ LOGICASSIGN128(&=)
238
+ LOGICASSIGN128(^=)
239
+
240
+ #undef LOGICASSIGN128
241
+
242
+ // Shift operators.
243
+
244
+ inline uint128 operator<<(const uint128& val, int amount) {
245
+ // uint64_t shifts of >= 64 are undefined, so we will need some
246
+ // special-casing.
247
+ if (amount < 64) {
248
+ if (amount == 0) {
249
+ return val;
250
+ }
251
+ uint64_t new_hi =
252
+ (Uint128High64(val) << amount) | (Uint128Low64(val) >> (64 - amount));
253
+ uint64_t new_lo = Uint128Low64(val) << amount;
254
+ return uint128(new_hi, new_lo);
255
+ } else if (amount < 128) {
256
+ return uint128(Uint128Low64(val) << (amount - 64), 0);
257
+ } else {
258
+ return uint128(0, 0);
259
+ }
260
+ }
261
+
262
+ inline uint128 operator>>(const uint128& val, int amount) {
263
+ // uint64_t shifts of >= 64 are undefined, so we will need some
264
+ // special-casing.
265
+ if (amount < 64) {
266
+ if (amount == 0) {
267
+ return val;
268
+ }
269
+ uint64_t new_hi = Uint128High64(val) >> amount;
270
+ uint64_t new_lo =
271
+ (Uint128Low64(val) >> amount) | (Uint128High64(val) << (64 - amount));
272
+ return uint128(new_hi, new_lo);
273
+ } else if (amount < 128) {
274
+ return uint128(0, Uint128High64(val) >> (amount - 64));
275
+ } else {
276
+ return uint128(0, 0);
277
+ }
278
+ }
279
+
280
+ inline uint128& operator<<=(uint128& self, int amount) {
281
+ // uint64_t shifts of >= 64 are undefined, so we will need some
282
+ // special-casing.
283
+ if (amount < 64) {
284
+ if (amount != 0) {
285
+ self.hi_ = (self.hi_ << amount) | (self.lo_ >> (64 - amount));
286
+ self.lo_ = self.lo_ << amount;
287
+ }
288
+ } else if (amount < 128) {
289
+ self.hi_ = self.lo_ << (amount - 64);
290
+ self.lo_ = 0;
291
+ } else {
292
+ self.hi_ = 0;
293
+ self.lo_ = 0;
294
+ }
295
+ return self;
296
+ }
297
+
298
+ C10_API inline uint128& uint128::operator>>=(int amount) {
299
+ // uint64_t shifts of >= 64 are undefined, so we will need some
300
+ // special-casing.
301
+ if (amount < 64) {
302
+ if (amount != 0) {
303
+ lo_ = (lo_ >> amount) | (hi_ << (64 - amount));
304
+ hi_ = hi_ >> amount;
305
+ }
306
+ } else if (amount < 128) {
307
+ lo_ = hi_ >> (amount - 64);
308
+ hi_ = 0;
309
+ } else {
310
+ lo_ = 0;
311
+ hi_ = 0;
312
+ }
313
+ return *this;
314
+ }
315
+
316
+ inline uint128 operator+(const uint128& lhs, const uint128& rhs) {
317
+ return uint128(lhs) += rhs;
318
+ }
319
+
320
+ inline uint128 operator-(const uint128& lhs, const uint128& rhs) {
321
+ return uint128(lhs) -= rhs;
322
+ }
323
+
324
+ inline uint128 operator*(const uint128& lhs, const uint128& rhs) {
325
+ return uint128(lhs) *= rhs;
326
+ }
327
+
328
+ inline uint128 operator/(const uint128& lhs, const uint128& rhs) {
329
+ return uint128(lhs) /= rhs;
330
+ }
331
+
332
+ inline uint128 operator%(const uint128& lhs, const uint128& rhs) {
333
+ return uint128(lhs) %= rhs;
334
+ }
335
+
336
+ C10_API inline uint128& uint128::operator+=(const uint128& b) {
337
+ hi_ += b.hi_;
338
+ uint64_t lolo = lo_ + b.lo_;
339
+ if (lolo < lo_)
340
+ ++hi_;
341
+ lo_ = lolo;
342
+ return *this;
343
+ }
344
+
345
+ C10_API inline uint128& uint128::operator-=(const uint128& b) {
346
+ hi_ -= b.hi_;
347
+ if (b.lo_ > lo_)
348
+ --hi_;
349
+ lo_ -= b.lo_;
350
+ return *this;
351
+ }
352
+
353
+ C10_API inline uint128& uint128::operator*=(const uint128& b) {
354
+ uint64_t a96 = hi_ >> 32;
355
+ uint64_t a64 = hi_ & 0xffffffffu;
356
+ uint64_t a32 = lo_ >> 32;
357
+ uint64_t a00 = lo_ & 0xffffffffu;
358
+ uint64_t b96 = b.hi_ >> 32;
359
+ uint64_t b64 = b.hi_ & 0xffffffffu;
360
+ uint64_t b32 = b.lo_ >> 32;
361
+ uint64_t b00 = b.lo_ & 0xffffffffu;
362
+ // multiply [a96 .. a00] x [b96 .. b00]
363
+ // terms higher than c96 disappear off the high side
364
+ // terms c96 and c64 are safe to ignore carry bit
365
+ uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
366
+ uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
367
+ this->hi_ = (c96 << 32) + c64;
368
+ this->lo_ = 0;
369
+ // add terms after this one at a time to capture carry
370
+ *this += uint128(a32 * b00) << 32;
371
+ *this += uint128(a00 * b32) << 32;
372
+ *this += a00 * b00;
373
+ return *this;
374
+ }
375
+
376
+ C10_API inline uint128 uint128::operator++(int) {
377
+ uint128 tmp(*this);
378
+ *this += 1;
379
+ return tmp;
380
+ }
381
+
382
+ C10_API inline uint128 uint128::operator--(int) {
383
+ uint128 tmp(*this);
384
+ *this -= 1;
385
+ return tmp;
386
+ }
387
+
388
+ C10_API inline uint128& uint128::operator++() {
389
+ *this += 1;
390
+ return *this;
391
+ }
392
+
393
+ C10_API inline uint128& uint128::operator--() {
394
+ *this -= 1;
395
+ return *this;
396
+ }
397
+
398
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/intrusive_ptr.h ADDED
@@ -0,0 +1,1076 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/util/Exception.h>
4
+ #include <c10/util/MaybeOwned.h>
5
+ #include <atomic>
6
+ #include <climits>
7
+ #include <memory>
8
+ #include <type_traits>
9
+
10
+ namespace pybind11 {
11
+ template <typename, typename...>
12
+ class class_;
13
+ }
14
+
15
+ namespace c10 {
16
+ class intrusive_ptr_target;
17
+ namespace raw {
18
+ namespace weak_intrusive_ptr {
19
+ inline void incref(intrusive_ptr_target* self);
20
+ }
21
+ namespace intrusive_ptr {
22
+ inline void incref(intrusive_ptr_target* self);
23
+ }
24
+
25
+ // constructor tag used by intrusive_ptr constructors
26
+ struct DontIncreaseRefcount {};
27
+ } // namespace raw
28
+
29
+ namespace detail {
30
+ constexpr uint32_t kImpracticallyHugeReferenceCount = 0x0FFFFFFF;
31
+ } // namespace detail
32
+
33
+ /**
34
+ * intrusive_ptr<T> is an alternative to shared_ptr<T> that has better
35
+ * performance because it does the refcounting intrusively
36
+ * (i.e. in a member of the object itself).
37
+ * Your class T needs to inherit from intrusive_ptr_target to allow it to be
38
+ * used in an intrusive_ptr<T>. Your class's constructor should not allow
39
+ *`this` to escape to other threads or create an intrusive_ptr from `this`.
40
+ */
41
+
42
+ // Note [Stack allocated intrusive_ptr_target safety]
43
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
+ // A well known problem with std::enable_shared_from_this is that it
45
+ // allows you to create a std::shared_ptr from a stack allocated object,
46
+ // which is totally bogus because the object will die once you return
47
+ // from the stack. In intrusive_ptr, we can detect that this has occurred,
48
+ // because we set the refcount/weakcount of objects which inherit from
49
+ // intrusive_ptr_target to zero, *unless* we can prove that the object
50
+ // was dynamically allocated (e.g., via make_intrusive).
51
+ //
52
+ // Thus, whenever you transmute a T* into a intrusive_ptr<T>, we check
53
+ // and make sure that the refcount isn't zero (or, a more subtle
54
+ // test for weak_intrusive_ptr<T>, for which the refcount may validly
55
+ // be zero, but the weak refcount better not be zero), because that
56
+ // tells us if the object was allocated by us. If it wasn't, no
57
+ // intrusive_ptr for you!
58
+
59
+ // NOLINTNEXTLINE(cppcoreguidelines-virtual-class-destructor)
60
+ class C10_API intrusive_ptr_target {
61
+ // Note [Weak references for intrusive refcounting]
62
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63
+ // Here's the scheme:
64
+ //
65
+ // - refcount == number of strong references to the object
66
+ // weakcount == number of weak references to the object,
67
+ // plus one more if refcount > 0
68
+ // An invariant: refcount > 0 => weakcount > 0
69
+ //
70
+ // - c10::StorageImpl stays live as long as there are any strong
71
+ // or weak pointers to it (weakcount > 0, since strong
72
+ // references count as a +1 to weakcount)
73
+ //
74
+ // - finalizers are called and data_ptr is deallocated when refcount == 0
75
+ //
76
+ // - Once refcount == 0, it can never again be > 0 (the transition
77
+ // from > 0 to == 0 is monotonic)
78
+ //
79
+ // - When you access c10::StorageImpl via a weak pointer, you must
80
+ // atomically increment the use count, if it is greater than 0.
81
+ // If it is not, you must report that the storage is dead.
82
+ //
83
+ mutable std::atomic<uint32_t> refcount_;
84
+ mutable std::atomic<uint32_t> weakcount_;
85
+
86
+ template <typename T, typename NullType>
87
+ friend class intrusive_ptr;
88
+ friend inline void raw::intrusive_ptr::incref(intrusive_ptr_target* self);
89
+
90
+ template <typename T, typename NullType>
91
+ friend class weak_intrusive_ptr;
92
+ friend inline void raw::weak_intrusive_ptr::incref(
93
+ intrusive_ptr_target* self);
94
+
95
+ template <typename T>
96
+ friend struct ExclusivelyOwnedTensorTraits;
97
+
98
+ protected:
99
+ // protected destructor. We never want to destruct intrusive_ptr_target*
100
+ // directly.
101
+ virtual ~intrusive_ptr_target() {
102
+ // Disable -Wterminate and -Wexceptions so we're allowed to use assertions
103
+ // (i.e. throw exceptions) in a destructor.
104
+ // We also have to disable -Wunknown-warning-option and -Wpragmas, because
105
+ // some other compilers don't know about -Wterminate or -Wexceptions and
106
+ // will show a warning about unknown warning options otherwise.
107
+ #if defined(_MSC_VER) && !defined(__clang__)
108
+ #pragma warning(push)
109
+ #pragma warning( \
110
+ disable : 4297) // function assumed not to throw an exception but does
111
+ #else
112
+ #pragma GCC diagnostic push
113
+ #pragma GCC diagnostic ignored "-Wpragmas"
114
+ #pragma GCC diagnostic ignored "-Wunknown-warning-option"
115
+ #pragma GCC diagnostic ignored "-Wterminate"
116
+ #pragma GCC diagnostic ignored "-Wexceptions"
117
+ #endif
118
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
119
+ // Second condition is there to accommodate
120
+ // unsafe_adapt_non_heap_allocated: since we are doing our own
121
+ // deallocation in that case, it is correct for each
122
+ // expected_decref to have happened (some user code tried to
123
+ // decref and thus free the object, but it didn't happen right
124
+ // away) or not (no user code tried to free the object, and
125
+ // now it's getting destroyed through whatever mechanism the
126
+ // caller of unsafe_adapt_non_heap_allocated wanted to
127
+ // use). We choose our reference count such that the count
128
+ // will not dip below kImpracticallyHugeReferenceCount regardless.
129
+ refcount_.load() == 0 ||
130
+ refcount_.load() >= detail::kImpracticallyHugeReferenceCount,
131
+ "Tried to destruct an intrusive_ptr_target that still has intrusive_ptr to it; refcount was ",
132
+ refcount_.load());
133
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
134
+ // See ~intrusive_ptr for optimization that will frequently result in 1
135
+ // at destruction time.
136
+ weakcount_.load() == 1 || weakcount_.load() == 0 ||
137
+ weakcount_.load() == detail::kImpracticallyHugeReferenceCount - 1 ||
138
+ weakcount_.load() == detail::kImpracticallyHugeReferenceCount,
139
+ "Tried to destruct an intrusive_ptr_target that still has weak_intrusive_ptr to it");
140
+ #if defined(_MSC_VER) && !defined(__clang__)
141
+ #pragma warning(pop)
142
+ #else
143
+ #pragma GCC diagnostic pop
144
+ #endif
145
+ }
146
+
147
+ constexpr intrusive_ptr_target() noexcept : refcount_(0), weakcount_(0) {}
148
+
149
+ // intrusive_ptr_target supports copy and move: but refcount and weakcount
150
+ // don't participate (since they are intrinsic properties of the memory
151
+ // location)
152
+ intrusive_ptr_target(intrusive_ptr_target&& /*other*/) noexcept
153
+ : intrusive_ptr_target() {}
154
+
155
+ intrusive_ptr_target& operator=(intrusive_ptr_target&& /*other*/) noexcept {
156
+ return *this;
157
+ }
158
+
159
+ intrusive_ptr_target(const intrusive_ptr_target& /*other*/) noexcept
160
+ : intrusive_ptr_target() {}
161
+
162
+ intrusive_ptr_target& operator=(
163
+ const intrusive_ptr_target& /*other*/) noexcept {
164
+ return *this;
165
+ }
166
+
167
+ private:
168
+ /**
169
+ * This is called when refcount reaches zero.
170
+ * You can override this to release expensive resources.
171
+ * There might still be weak references, so your object might not get
172
+ * destructed yet, but you can assume the object isn't used anymore,
173
+ * i.e. no more calls to methods or accesses to members (we just can't
174
+ * destruct it yet because we need the weakcount accessible).
175
+ *
176
+ * If there are no weak references (i.e. your class is about to be
177
+ * destructed), this function WILL NOT be called.
178
+ */
179
+ virtual void release_resources() {}
180
+ };
181
+
182
+ namespace detail {
183
+ template <class TTarget>
184
+ struct intrusive_target_default_null_type final {
185
+ static constexpr TTarget* singleton() noexcept {
186
+ return nullptr;
187
+ }
188
+ };
189
+
190
+ template <class TTarget, class ToNullType, class FromNullType>
191
+ TTarget* assign_ptr_(TTarget* rhs) {
192
+ if (FromNullType::singleton() == rhs) {
193
+ return ToNullType::singleton();
194
+ } else {
195
+ return rhs;
196
+ }
197
+ }
198
+
199
+ // Increment needs to be acquire-release to make use_count() and
200
+ // unique() reliable.
201
+ inline uint32_t atomic_refcount_increment(std::atomic<uint32_t>& refcount) {
202
+ return refcount.fetch_add(1, std::memory_order_acq_rel) + 1;
203
+ }
204
+
205
+ // weak_use_count() is only used for testing, so we don't need it to
206
+ // be reliable. Relaxed should be fine.
207
+ inline uint32_t atomic_weakcount_increment(std::atomic<uint32_t>& weakcount) {
208
+ return weakcount.fetch_add(1, std::memory_order_relaxed) + 1;
209
+ }
210
+
211
+ // Both decrements need to be acquire-release for correctness. See
212
+ // e.g. std::shared_ptr implementation.
213
+ inline uint32_t atomic_refcount_decrement(std::atomic<uint32_t>& refcount) {
214
+ return refcount.fetch_sub(1, std::memory_order_acq_rel) - 1;
215
+ }
216
+
217
+ inline uint32_t atomic_weakcount_decrement(std::atomic<uint32_t>& weakcount) {
218
+ return weakcount.fetch_sub(1, std::memory_order_acq_rel) - 1;
219
+ }
220
+
221
+ } // namespace detail
222
+
223
+ template <class TTarget, class NullType>
224
+ class weak_intrusive_ptr;
225
+
226
+ template <
227
+ class TTarget,
228
+ class NullType = detail::intrusive_target_default_null_type<TTarget>>
229
+ class intrusive_ptr final {
230
+ private:
231
+ // the following static assert would be nice to have but it requires
232
+ // the target class T to be fully defined when intrusive_ptr<T> is instantiated
233
+ // this is a problem for classes that contain pointers to themselves
234
+ // static_assert(
235
+ // std::is_base_of_v<intrusive_ptr_target, TTarget>,
236
+ // "intrusive_ptr can only be used for classes that inherit from
237
+ // intrusive_ptr_target.");
238
+ #ifndef _WIN32
239
+ // This static_assert triggers on MSVC
240
+ // error C2131: expression did not evaluate to a constant
241
+ static_assert(
242
+ // NOLINTNEXTLINE(misc-redundant-expression)
243
+ NullType::singleton() == NullType::singleton(),
244
+ "NullType must have a constexpr singleton() method");
245
+ #endif
246
+ static_assert(
247
+ std::is_base_of_v<
248
+ TTarget,
249
+ std::remove_pointer_t<decltype(NullType::singleton())>>,
250
+ "NullType::singleton() must return a element_type* pointer");
251
+
252
+ TTarget* target_;
253
+
254
+ template <typename T>
255
+ friend struct ExclusivelyOwnedTensorTraits;
256
+ template <class TTarget2, class NullType2>
257
+ friend class intrusive_ptr;
258
+ friend class weak_intrusive_ptr<TTarget, NullType>;
259
+
260
+ // Make pybind11::class_ be a friend class of intrusive_ptr, so that custom
261
+ // smart holder in pybind11 could access the private constructor of
262
+ // intrusive_ptr(T*) which took the ownership of the object. This is required
263
+ // by customer holder macro PYBIND11_DECLARE_HOLDER_TYPE, where it uses
264
+ // intrusive_ptr(TTarget*) to initialize and take ownership of the object. For
265
+ // details, see
266
+ // https://pybind11.readthedocs.io/en/stable/advanced/smart_ptrs.html#custom-smart-pointers
267
+ template <typename, typename...>
268
+ friend class pybind11::class_;
269
+
270
+ void retain_() {
271
+ if (target_ != NullType::singleton()) {
272
+ uint32_t new_refcount =
273
+ detail::atomic_refcount_increment(target_->refcount_);
274
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
275
+ new_refcount != 1,
276
+ "intrusive_ptr: Cannot increase refcount after it reached zero.");
277
+ }
278
+ }
279
+
280
+ void reset_() noexcept {
281
+ if (target_ != NullType::singleton() &&
282
+ detail::atomic_refcount_decrement(target_->refcount_) == 0) {
283
+ // See comment above about weakcount. As long as refcount>0,
284
+ // weakcount is one larger than the actual number of weak references.
285
+ // So we need to decrement it here.
286
+ bool should_delete =
287
+ target_->weakcount_.load(std::memory_order_acquire) == 1;
288
+ if (!should_delete) {
289
+ // justification for const_cast: release_resources is basically a
290
+ // destructor and a destructor always mutates the object, even for const
291
+ // objects. NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
292
+ const_cast<std::remove_const_t<TTarget>*>(target_)->release_resources();
293
+ should_delete =
294
+ detail::atomic_weakcount_decrement(target_->weakcount_) == 0;
295
+ }
296
+ if (should_delete) {
297
+ delete target_;
298
+ }
299
+ }
300
+ }
301
+
302
+ // raw pointer constructors are not public because we shouldn't make
303
+ // intrusive_ptr out of raw pointers except from inside the make_intrusive(),
304
+ // reclaim() and weak_intrusive_ptr::lock() implementations.
305
+
306
+ // This constructor will increase the ref counter for you.
307
+ // This constructor will be used by the make_intrusive(), and also pybind11,
308
+ // which wrap the intrusive_ptr holder around the raw pointer and incref
309
+ // correspondingly (pybind11 requires raw pointer constructor to incref by
310
+ // default).
311
+ explicit intrusive_ptr(TTarget* target)
312
+ : intrusive_ptr(target, raw::DontIncreaseRefcount{}) {
313
+ if (target_ != NullType::singleton()) {
314
+ // We just created result.target_, so we know no other thread has
315
+ // access to it, so we know we needn't care about memory ordering.
316
+ // (On x86_64, a store with memory_order_relaxed generates a plain old
317
+ // `mov`, whereas an atomic increment does a lock-prefixed `add`, which is
318
+ // much more expensive: https://godbolt.org/z/eKPzj8.)
319
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
320
+ target_->refcount_ == 0 && target_->weakcount_ == 0,
321
+ "intrusive_ptr: Newly-created target had non-zero refcounts. Does its "
322
+ "constructor do something strange like incref or create an "
323
+ "intrusive_ptr from `this`?");
324
+ target_->refcount_.store(1, std::memory_order_relaxed);
325
+ target_->weakcount_.store(1, std::memory_order_relaxed);
326
+ }
327
+ }
328
+
329
+ public:
330
+ using element_type = TTarget;
331
+
332
+ intrusive_ptr() noexcept
333
+ : intrusive_ptr(NullType::singleton(), raw::DontIncreaseRefcount{}) {}
334
+
335
+ intrusive_ptr(std::nullptr_t) noexcept
336
+ : intrusive_ptr(NullType::singleton(), raw::DontIncreaseRefcount{}) {}
337
+
338
+ // This constructor will not increase the ref counter for you.
339
+ // We use the tagged dispatch mechanism to explicitly mark this constructor
340
+ // to not increase the refcount
341
+ explicit intrusive_ptr(TTarget* target, raw::DontIncreaseRefcount) noexcept
342
+ : target_(target) {}
343
+
344
+ explicit intrusive_ptr(std::unique_ptr<TTarget> rhs) noexcept
345
+ : intrusive_ptr(rhs.release()) {}
346
+
347
+ intrusive_ptr(intrusive_ptr&& rhs) noexcept : target_(rhs.target_) {
348
+ rhs.target_ = NullType::singleton();
349
+ }
350
+
351
+ template <class From, class FromNullType>
352
+ // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
353
+ /* implicit */ intrusive_ptr(intrusive_ptr<From, FromNullType>&& rhs) noexcept
354
+ : target_(
355
+ detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {
356
+ static_assert(
357
+ std::is_convertible_v<From*, TTarget*>,
358
+ "Type mismatch. intrusive_ptr move constructor got pointer of wrong type.");
359
+ rhs.target_ = FromNullType::singleton();
360
+ }
361
+
362
+ intrusive_ptr(const intrusive_ptr& rhs) : target_(rhs.target_) {
363
+ retain_();
364
+ }
365
+
366
+ template <class From, class FromNullType>
367
+ /* implicit */ intrusive_ptr(const intrusive_ptr<From, FromNullType>& rhs)
368
+ : target_(
369
+ detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {
370
+ static_assert(
371
+ std::is_convertible_v<From*, TTarget*>,
372
+ "Type mismatch. intrusive_ptr copy constructor got pointer of wrong type.");
373
+ retain_();
374
+ }
375
+
376
+ ~intrusive_ptr() noexcept {
377
+ reset_();
378
+ }
379
+
380
+ intrusive_ptr& operator=(intrusive_ptr&& rhs) & noexcept {
381
+ // NOLINTNEXTLINE(*assign*)
382
+ return this->template operator= <TTarget, NullType>(std::move(rhs));
383
+ }
384
+
385
+ template <class From, class FromNullType>
386
+ intrusive_ptr& operator=(intrusive_ptr<From, FromNullType>&& rhs) & noexcept {
387
+ static_assert(
388
+ std::is_convertible_v<From*, TTarget*>,
389
+ "Type mismatch. intrusive_ptr move assignment got pointer of wrong type.");
390
+ intrusive_ptr tmp = std::move(rhs);
391
+ swap(tmp);
392
+ return *this;
393
+ }
394
+
395
+ // Assignment is implemented using copy and swap. That's safe for self
396
+ // assignment.
397
+ // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
398
+ intrusive_ptr& operator=(const intrusive_ptr& rhs) & noexcept {
399
+ // NOLINTNEXTLINE(*assign-operator, *assignment-signature)
400
+ return this->template operator= <TTarget, NullType>(rhs);
401
+ }
402
+
403
+ template <class From, class FromNullType>
404
+ intrusive_ptr& operator=(
405
+ const intrusive_ptr<From, NullType>& rhs) & noexcept {
406
+ static_assert(
407
+ std::is_convertible_v<From*, TTarget*>,
408
+ "Type mismatch. intrusive_ptr copy assignment got pointer of wrong type.");
409
+ intrusive_ptr tmp = rhs;
410
+ swap(tmp);
411
+ return *this;
412
+ }
413
+
414
+ TTarget* get() const noexcept {
415
+ return target_;
416
+ }
417
+
418
+ TTarget& operator*() const noexcept {
419
+ return *target_;
420
+ }
421
+
422
+ TTarget* operator->() const noexcept {
423
+ return target_;
424
+ }
425
+
426
+ operator bool() const noexcept {
427
+ return target_ != NullType::singleton();
428
+ }
429
+
430
+ void reset() noexcept {
431
+ reset_();
432
+ target_ = NullType::singleton();
433
+ }
434
+
435
+ void swap(intrusive_ptr& rhs) noexcept {
436
+ std::swap(target_, rhs.target_);
437
+ }
438
+
439
+ // We do a lot of null-pointer checks in our code, good to have this be cheap.
440
+ bool defined() const noexcept {
441
+ return target_ != NullType::singleton();
442
+ }
443
+
444
+ uint32_t use_count() const noexcept {
445
+ if (target_ == NullType::singleton()) {
446
+ return 0;
447
+ }
448
+ return target_->refcount_.load(std::memory_order_acquire);
449
+ }
450
+
451
+ uint32_t weak_use_count() const noexcept {
452
+ if (target_ == NullType::singleton()) {
453
+ return 0;
454
+ }
455
+ return target_->weakcount_.load(std::memory_order_acquire);
456
+ }
457
+
458
+ bool unique() const noexcept {
459
+ return use_count() == 1;
460
+ }
461
+
462
+ /**
463
+ * Returns an owning (!) pointer to the underlying object and makes the
464
+ * intrusive_ptr instance invalid. That means the refcount is not decreased.
465
+ * You *must* put the returned pointer back into a intrusive_ptr using
466
+ * intrusive_ptr::reclaim(ptr) to properly destruct it.
467
+ * This is helpful for C APIs.
468
+ */
469
+ TTarget* release() noexcept {
470
+ // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.Assign)
471
+ TTarget* result = target_;
472
+ target_ = NullType::singleton();
473
+ return result;
474
+ }
475
+
476
+ /**
477
+ * Takes an owning pointer to TTarget* and creates an intrusive_ptr that takes
478
+ * over ownership. That means the refcount is not increased.
479
+ * This is the counter-part to intrusive_ptr::release() and the pointer
480
+ * passed in *must* have been created using intrusive_ptr::release().
481
+ */
482
+ static intrusive_ptr reclaim(TTarget* owning_ptr) {
483
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
484
+ owning_ptr == NullType::singleton() ||
485
+ owning_ptr->refcount_.load() == 0 || owning_ptr->weakcount_.load(),
486
+ "TTarget violates the invariant that refcount > 0 => weakcount > 0");
487
+ return intrusive_ptr(owning_ptr, raw::DontIncreaseRefcount{});
488
+ }
489
+
490
+ /**
491
+ * Takes an owning pointer to TTarget* and creates an intrusive_ptr
492
+ * representing a new reference, i.e. the raw pointer retains
493
+ * ownership.
494
+ */
495
+ static intrusive_ptr reclaim_copy(TTarget* owning_ptr) {
496
+ auto ret = reclaim(owning_ptr);
497
+ ret.retain_();
498
+ return ret;
499
+ }
500
+
501
+ /**
502
+ * Allocate a heap object with args and wrap it inside a intrusive_ptr and
503
+ * incref. This is a helper function to let make_intrusive() access private
504
+ * intrusive_ptr constructors.
505
+ */
506
+ template <class... Args>
507
+ static intrusive_ptr make(Args&&... args) {
508
+ return intrusive_ptr(new TTarget(std::forward<Args>(args)...));
509
+ }
510
+
511
+ /**
512
+ * Turn a new instance of TTarget (e.g., literally allocated
513
+ * using new TTarget(...) into an intrusive_ptr. If possible,
514
+ * use intrusive_ptr::make instead which statically guarantees
515
+ * that the allocation was done properly.
516
+ *
517
+ * At the moment, the only reason this method exists is because
518
+ * pybind11 holder types expect to be able to allocate in
519
+ * this way (because pybind11 handles the new allocation itself).
520
+ */
521
+ static intrusive_ptr unsafe_steal_from_new(TTarget* raw_ptr) {
522
+ return intrusive_ptr(raw_ptr);
523
+ }
524
+
525
+ /**
526
+ * Turn an instance of TTarget that should not be reference counted
527
+ * (e.g., allocated into an arena with placement new) into an
528
+ * intrusive_ptr. This is gratuitously unsafe and should only be
529
+ * used if you can guarantee that the pointer will not escape and be
530
+ * refcounted as normal.
531
+ *
532
+ * `expected_decrefs` is a debugging parameter: it indicates the
533
+ * number of strong owners the intrusive_ptr_target in question is
534
+ * expected to get. In most use cases, this will likely be 1.
535
+ *
536
+ * The reason this method exists is for manually sharing
537
+ * StorageImpls across Tensors in the static runtime. It needs
538
+ * access to private intrusive_ptr members so that the refcounts can
539
+ * be initialized to custom values.
540
+ */
541
+ static intrusive_ptr unsafe_adapt_non_heap_allocated(
542
+ TTarget* raw_ptr,
543
+ uint32_t expected_decrefs) {
544
+ intrusive_ptr result(raw_ptr, raw::DontIncreaseRefcount{});
545
+ // kImpracticallyHugeReferenceCount is impractically huge for a reference
546
+ // count, while being in no danger of overflowing uint32_t. We actually only
547
+ // need to initialize the refcount to 2 -- we are just doing an unbalanced
548
+ // incref to prevent the non-heap-allocated target from being
549
+ // freed, and we are optimizing that incref by directly
550
+ // initializing the refcounts rather than doing an expensive
551
+ // atomic increment. The reason to use kImpracticallyHugeReferenceCount is
552
+ // to accommodate the debug assertions in ~intrusive_ptr_target.
553
+ #ifdef NDEBUG
554
+ expected_decrefs = 0;
555
+ #endif
556
+ result.target_->refcount_.store(
557
+ detail::kImpracticallyHugeReferenceCount + expected_decrefs,
558
+ std::memory_order_relaxed);
559
+ result.target_->weakcount_.store(
560
+ detail::kImpracticallyHugeReferenceCount, std::memory_order_relaxed);
561
+ return result;
562
+ }
563
+
564
+ /**
565
+ * Turn a **non-owning raw pointer** to an intrusive_ptr. It is
566
+ * the moral equivalent of enable_shared_from_this on a shared pointer.
567
+ *
568
+ * This method is only valid for objects that are already live. If
569
+ * you are looking for the moral equivalent of unique_ptr<T>(T*)
570
+ * constructor, see steal_from_new.
571
+ *
572
+ * TODO: https://github.com/pytorch/pytorch/issues/56482
573
+ */
574
+ static intrusive_ptr unsafe_reclaim_from_nonowning(TTarget* raw_ptr) {
575
+ // See Note [Stack allocated intrusive_ptr_target safety]
576
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
577
+ raw_ptr == NullType::singleton() || raw_ptr->refcount_.load() > 0,
578
+ "intrusive_ptr: Can only reclaim pointers that are owned by someone");
579
+ auto ptr = reclaim(raw_ptr); // doesn't increase refcount
580
+ ptr.retain_();
581
+ return ptr;
582
+ }
583
+ };
584
+
585
+ template <
586
+ class TTarget,
587
+ class NullType = detail::intrusive_target_default_null_type<TTarget>,
588
+ class... Args>
589
+ inline intrusive_ptr<TTarget, NullType> make_intrusive(Args&&... args) {
590
+ return intrusive_ptr<TTarget, NullType>::make(std::forward<Args>(args)...);
591
+ }
592
+
593
+ template <class TTarget, class NullType>
594
+ inline void swap(
595
+ intrusive_ptr<TTarget, NullType>& lhs,
596
+ intrusive_ptr<TTarget, NullType>& rhs) noexcept {
597
+ lhs.swap(rhs);
598
+ }
599
+
600
+ // To allow intrusive_ptr inside std::map or std::set, we need operator<
601
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
602
+ inline bool operator<(
603
+ const intrusive_ptr<TTarget1, NullType1>& lhs,
604
+ const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
605
+ return lhs.get() < rhs.get();
606
+ }
607
+
608
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
609
+ inline bool operator==(
610
+ const intrusive_ptr<TTarget1, NullType1>& lhs,
611
+ const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
612
+ return lhs.get() == rhs.get();
613
+ }
614
+
615
+ template <class TTarget1, class NullType1>
616
+ inline bool operator==(
617
+ const intrusive_ptr<TTarget1, NullType1>& lhs,
618
+ std::nullptr_t) noexcept {
619
+ return lhs.get() == nullptr;
620
+ }
621
+
622
+ template <class TTarget2, class NullType2>
623
+ inline bool operator==(
624
+ std::nullptr_t,
625
+ const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
626
+ return nullptr == rhs.get();
627
+ }
628
+
629
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
630
+ inline bool operator!=(
631
+ const intrusive_ptr<TTarget1, NullType1>& lhs,
632
+ const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
633
+ return !operator==(lhs, rhs);
634
+ }
635
+
636
+ template <class TTarget1, class NullType1>
637
+ inline bool operator!=(
638
+ const intrusive_ptr<TTarget1, NullType1>& lhs,
639
+ std::nullptr_t) noexcept {
640
+ return !operator==(lhs, nullptr);
641
+ }
642
+
643
+ template <class TTarget2, class NullType2>
644
+ inline bool operator!=(
645
+ std::nullptr_t,
646
+ const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
647
+ return !operator==(nullptr, rhs);
648
+ }
649
+ template <typename T>
650
+ struct MaybeOwnedTraits<c10::intrusive_ptr<T>> {
651
+ using owned_type = c10::intrusive_ptr<T>;
652
+ using borrow_type = c10::intrusive_ptr<T>;
653
+
654
+ static borrow_type createBorrow(const owned_type& from) {
655
+ return borrow_type::reclaim(from.get());
656
+ }
657
+
658
+ static void assignBorrow(borrow_type& lhs, const borrow_type& rhs) {
659
+ lhs.release();
660
+ lhs = borrow_type::reclaim(rhs.get());
661
+ }
662
+
663
+ static void destroyBorrow(borrow_type& toDestroy) {
664
+ toDestroy.release();
665
+ }
666
+
667
+ static const owned_type& referenceFromBorrow(
668
+ const borrow_type& borrow) noexcept {
669
+ return borrow;
670
+ }
671
+
672
+ static const owned_type* pointerFromBorrow(
673
+ const borrow_type& borrow) noexcept {
674
+ return &borrow;
675
+ }
676
+
677
+ static bool debugBorrowIsValid(const borrow_type& /*borrow*/) noexcept {
678
+ return true;
679
+ }
680
+ };
681
+
682
+ template <
683
+ typename TTarget,
684
+ class NullType = detail::intrusive_target_default_null_type<TTarget>>
685
+ class weak_intrusive_ptr final {
686
+ private:
687
+ static_assert(
688
+ std::is_base_of_v<intrusive_ptr_target, TTarget>,
689
+ "intrusive_ptr can only be used for classes that inherit from intrusive_ptr_target.");
690
+ #ifndef _WIN32
691
+ // This static_assert triggers on MSVC
692
+ // error C2131: expression did not evaluate to a constant
693
+ static_assert(
694
+ NullType::singleton() == NullType::singleton(),
695
+ "NullType must have a constexpr singleton() method");
696
+ #endif
697
+ static_assert(
698
+ std::is_base_of_v<
699
+ TTarget,
700
+ std::remove_pointer_t<decltype(NullType::singleton())>>,
701
+ "NullType::singleton() must return a element_type* pointer");
702
+
703
+ TTarget* target_;
704
+
705
+ template <class TTarget2, class NullType2>
706
+ friend class weak_intrusive_ptr;
707
+
708
+ void retain_() {
709
+ if (target_ != NullType::singleton()) {
710
+ uint32_t new_weakcount =
711
+ detail::atomic_weakcount_increment(target_->weakcount_);
712
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
713
+ new_weakcount != 1,
714
+ "weak_intrusive_ptr: Cannot increase weakcount after it reached zero.");
715
+ }
716
+ }
717
+
718
+ void reset_() noexcept {
719
+ if (target_ != NullType::singleton() &&
720
+ detail::atomic_weakcount_decrement(target_->weakcount_) == 0) {
721
+ // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete)
722
+ delete target_;
723
+ }
724
+ target_ = NullType::singleton();
725
+ }
726
+
727
+ constexpr explicit weak_intrusive_ptr(TTarget* target) : target_(target) {}
728
+
729
+ public:
730
+ using element_type = TTarget;
731
+
732
+ explicit weak_intrusive_ptr(const intrusive_ptr<TTarget, NullType>& ptr)
733
+ : weak_intrusive_ptr(ptr.get()) {
734
+ retain_();
735
+ }
736
+
737
+ weak_intrusive_ptr(weak_intrusive_ptr&& rhs) noexcept : target_(rhs.target_) {
738
+ rhs.target_ = NullType::singleton();
739
+ }
740
+
741
+ template <class From, class FromNullType>
742
+ /* implicit */ weak_intrusive_ptr(
743
+ // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
744
+ weak_intrusive_ptr<From, FromNullType>&& rhs) noexcept
745
+ : target_(
746
+ detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {
747
+ static_assert(
748
+ std::is_convertible_v<From*, TTarget*>,
749
+ "Type mismatch. weak_intrusive_ptr move constructor got pointer of wrong type.");
750
+ rhs.target_ = FromNullType::singleton();
751
+ }
752
+
753
+ weak_intrusive_ptr(const weak_intrusive_ptr& rhs) : target_(rhs.target_) {
754
+ retain_();
755
+ }
756
+
757
+ template <class From, class FromNullType>
758
+ /* implicit */ weak_intrusive_ptr(
759
+ const weak_intrusive_ptr<From, FromNullType>& rhs)
760
+ : target_(
761
+ detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {
762
+ static_assert(
763
+ std::is_convertible_v<From*, TTarget*>,
764
+ "Type mismatch. weak_intrusive_ptr copy constructor got pointer of wrong type.");
765
+ retain_();
766
+ }
767
+
768
+ ~weak_intrusive_ptr() noexcept {
769
+ reset_();
770
+ }
771
+
772
+ weak_intrusive_ptr& operator=(weak_intrusive_ptr&& rhs) & noexcept {
773
+ // NOLINTNEXTLINE(*assign*)
774
+ return this->template operator= <TTarget, NullType>(std::move(rhs));
775
+ }
776
+
777
+ template <class From, class FromNullType>
778
+ weak_intrusive_ptr& operator=(
779
+ weak_intrusive_ptr<From, FromNullType>&& rhs) & noexcept {
780
+ static_assert(
781
+ std::is_convertible_v<From*, TTarget*>,
782
+ "Type mismatch. weak_intrusive_ptr move assignment got pointer of wrong type.");
783
+ weak_intrusive_ptr tmp = std::move(rhs);
784
+ swap(tmp);
785
+ return *this;
786
+ }
787
+
788
+ weak_intrusive_ptr& operator=(const weak_intrusive_ptr& rhs) & noexcept {
789
+ if (this == &rhs) {
790
+ return *this;
791
+ }
792
+ // NOLINTNEXTLINE(*assign*)
793
+ return this->template operator= <TTarget, NullType>(rhs);
794
+ }
795
+
796
+ weak_intrusive_ptr& operator=(
797
+ const intrusive_ptr<TTarget, NullType>& rhs) & noexcept {
798
+ weak_intrusive_ptr tmp(rhs);
799
+ swap(tmp);
800
+ return *this;
801
+ }
802
+
803
+ template <class From, class FromNullType>
804
+ weak_intrusive_ptr& operator=(
805
+ const weak_intrusive_ptr<From, NullType>& rhs) & noexcept {
806
+ static_assert(
807
+ std::is_convertible_v<From*, TTarget*>,
808
+ "Type mismatch. weak_intrusive_ptr copy assignment got pointer of wrong type.");
809
+ weak_intrusive_ptr tmp = rhs;
810
+ swap(tmp);
811
+ return *this;
812
+ }
813
+
814
+ void reset() noexcept {
815
+ reset_();
816
+ }
817
+
818
+ void swap(weak_intrusive_ptr& rhs) noexcept {
819
+ TTarget* tmp = target_;
820
+ target_ = rhs.target_;
821
+ rhs.target_ = tmp;
822
+ }
823
+
824
+ // NB: This should ONLY be used by the std::hash implementation
825
+ // for weak_intrusive_ptr. Another way you could do this is
826
+ // friend std::hash<weak_intrusive_ptr>, but this triggers two
827
+ // bugs:
828
+ //
829
+ // (1) It triggers an nvcc bug, where std::hash in a friend class
830
+ // declaration gets preprocessed into hash, which then cannot
831
+ // actually be found. The error in this case looks like:
832
+ //
833
+ // error: no template named 'hash'; did you mean 'std::hash'?
834
+ //
835
+ // (2) On OS X, std::hash is declared as a struct, not a class.
836
+ // This twings:
837
+ //
838
+ // error: class 'hash' was previously declared as a struct
839
+ // [-Werror,-Wmismatched-tags]
840
+ //
841
+ // Both of these are work-aroundable, but on the whole, I decided
842
+ // it would be simpler and easier to make work if we just expose
843
+ // an unsafe getter for target_
844
+ //
845
+ TTarget* _unsafe_get_target() const noexcept {
846
+ return target_;
847
+ }
848
+
849
+ uint32_t use_count() const noexcept {
850
+ if (target_ == NullType::singleton()) {
851
+ return 0;
852
+ }
853
+ return target_->refcount_.load(
854
+ std::memory_order_acquire); // refcount, not weakcount!
855
+ }
856
+
857
+ uint32_t weak_use_count() const noexcept {
858
+ if (target_ == NullType::singleton()) {
859
+ return 0;
860
+ }
861
+ return target_->weakcount_.load(std::memory_order_acquire);
862
+ }
863
+
864
+ bool expired() const noexcept {
865
+ return use_count() == 0;
866
+ }
867
+
868
+ intrusive_ptr<TTarget, NullType> lock() const noexcept {
869
+ if (expired()) {
870
+ return intrusive_ptr<TTarget, NullType>();
871
+ } else {
872
+ auto refcount = target_->refcount_.load(std::memory_order_seq_cst);
873
+ do {
874
+ if (refcount == 0) {
875
+ // Object already destructed, no strong references left anymore.
876
+ // Return nullptr.
877
+ return intrusive_ptr<TTarget, NullType>();
878
+ }
879
+ } while (
880
+ !target_->refcount_.compare_exchange_weak(refcount, refcount + 1));
881
+ return intrusive_ptr<TTarget, NullType>(
882
+ target_, raw::DontIncreaseRefcount{});
883
+ }
884
+ }
885
+
886
+ /**
887
+ * Returns an owning (but still only weakly referenced) pointer to the
888
+ * underlying object and makes the weak_intrusive_ptr instance invalid.
889
+ * That means the weakcount is not decreased.
890
+ * You *must* put the returned pointer back into a weak_intrusive_ptr using
891
+ * weak_intrusive_ptr::reclaim(ptr) to properly destruct it.
892
+ * This is helpful for C APIs.
893
+ */
894
+ TTarget* release() noexcept {
895
+ TTarget* result = target_;
896
+ target_ = NullType::singleton();
897
+ return result;
898
+ }
899
+
900
+ /**
901
+ * Takes an owning (but must be weakly referenced) pointer to TTarget* and
902
+ * creates a weak_intrusive_ptr that takes over ownership.
903
+ * This means that the weakcount is not increased.
904
+ * This is the counter-part to weak_intrusive_ptr::release() and the pointer
905
+ * passed in *must* have been created using weak_intrusive_ptr::release().
906
+ */
907
+ static weak_intrusive_ptr reclaim(TTarget* owning_weak_ptr) {
908
+ // See Note [Stack allocated intrusive_ptr_target safety]
909
+ // if refcount > 0, weakcount must be >1 for weak references to exist.
910
+ // see weak counting explanation at top of this file.
911
+ // if refcount == 0, weakcount only must be >0.
912
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
913
+ owning_weak_ptr == NullType::singleton() ||
914
+ owning_weak_ptr->weakcount_.load() > 1 ||
915
+ (owning_weak_ptr->refcount_.load() == 0 &&
916
+ owning_weak_ptr->weakcount_.load() > 0),
917
+ "weak_intrusive_ptr: Can only weak_intrusive_ptr::reclaim() owning pointers that were created using weak_intrusive_ptr::release().");
918
+ return weak_intrusive_ptr(owning_weak_ptr);
919
+ }
920
+
921
+ /**
922
+ * Takes a pointer to TTarget* (may be weak or strong) and creates a
923
+ * new weak_intrusive_ptr representing a new weak reference, i.e.
924
+ * the raw pointer retains ownership.
925
+ */
926
+ static weak_intrusive_ptr reclaim_copy(TTarget* owning_ptr) {
927
+ auto ret = reclaim(owning_ptr);
928
+ ret.retain_();
929
+ return ret;
930
+ }
931
+
932
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
933
+ friend bool operator<(
934
+ const weak_intrusive_ptr<TTarget1, NullType1>& lhs,
935
+ const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept;
936
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
937
+ friend bool operator==(
938
+ const weak_intrusive_ptr<TTarget1, NullType1>& lhs,
939
+ const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept;
940
+ };
941
+
942
+ template <class TTarget, class NullType>
943
+ inline void swap(
944
+ weak_intrusive_ptr<TTarget, NullType>& lhs,
945
+ weak_intrusive_ptr<TTarget, NullType>& rhs) noexcept {
946
+ lhs.swap(rhs);
947
+ }
948
+
949
+ // To allow weak_intrusive_ptr inside std::map or std::set, we need operator<
950
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
951
+ inline bool operator<(
952
+ const weak_intrusive_ptr<TTarget1, NullType1>& lhs,
953
+ const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
954
+ return lhs.target_ < rhs.target_;
955
+ }
956
+
957
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
958
+ inline bool operator==(
959
+ const weak_intrusive_ptr<TTarget1, NullType1>& lhs,
960
+ const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
961
+ return lhs.target_ == rhs.target_;
962
+ }
963
+
964
+ template <class TTarget1, class NullType1, class TTarget2, class NullType2>
965
+ inline bool operator!=(
966
+ const weak_intrusive_ptr<TTarget1, NullType1>& lhs,
967
+ const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {
968
+ return !operator==(lhs, rhs);
969
+ }
970
+
971
+ // Alias for documentary purposes, to more easily distinguish
972
+ // weak raw intrusive pointers from intrusive pointers.
973
+ using weak_intrusive_ptr_target = intrusive_ptr_target;
974
+
975
+ // This namespace provides some methods for working with
976
+ // raw pointers that subclass intrusive_ptr_target. They are not provided
977
+ // as methods on intrusive_ptr_target, because ideally you would not need these
978
+ // methods at all (use smart pointers), but if you are dealing with legacy code
979
+ // that still needs to pass around raw pointers, you may find these quite
980
+ // useful.
981
+ //
982
+ // An important usage note: some functions are only valid if you have a
983
+ // strong raw pointer to the object, while others are only valid if you
984
+ // have a weak raw pointer to the object. ONLY call intrusive_ptr namespace
985
+ // functions on strong pointers, and weak_intrusive_ptr namespace functions
986
+ // on weak pointers. If you mix it up, you may get an assert failure.
987
+ namespace raw {
988
+
989
+ namespace intrusive_ptr {
990
+
991
+ // WARNING: Unlike the reclaim() API, it is NOT valid to pass
992
+ // NullType::singleton to this function
993
+ inline void incref(intrusive_ptr_target* self) {
994
+ if (self) {
995
+ detail::atomic_refcount_increment(self->refcount_);
996
+ }
997
+ }
998
+
999
+ // WARNING: Unlike the reclaim() API, it is NOT valid to pass
1000
+ // NullType::singleton to this function
1001
+ inline void decref(intrusive_ptr_target* self) {
1002
+ // Let it die
1003
+ c10::intrusive_ptr<intrusive_ptr_target>::reclaim(self);
1004
+ // NB: Caller still has 'self' pointer, but it's now invalid.
1005
+ // If you want more safety, used the actual c10::intrusive_ptr class
1006
+ }
1007
+
1008
+ template <typename T>
1009
+ inline T* make_weak(T* self) {
1010
+ // NB: 'this' is a strong pointer, but we return a weak pointer
1011
+ auto ptr = c10::intrusive_ptr<T>::reclaim(self);
1012
+ c10::weak_intrusive_ptr<T> wptr(ptr);
1013
+ ptr.release();
1014
+ return wptr.release();
1015
+ }
1016
+
1017
+ inline uint32_t use_count(intrusive_ptr_target* self) {
1018
+ auto ptr = c10::intrusive_ptr<intrusive_ptr_target>::reclaim(self);
1019
+ auto r = ptr.use_count();
1020
+ ptr.release();
1021
+ return r;
1022
+ }
1023
+
1024
+ } // namespace intrusive_ptr
1025
+
1026
+ namespace weak_intrusive_ptr {
1027
+
1028
+ inline void incref(weak_intrusive_ptr_target* self) {
1029
+ detail::atomic_weakcount_increment(self->weakcount_);
1030
+ }
1031
+
1032
+ inline void decref(weak_intrusive_ptr_target* self) {
1033
+ // Let it die
1034
+ c10::weak_intrusive_ptr<intrusive_ptr_target>::reclaim(self);
1035
+ // NB: You still "have" the 'self' pointer, but it's now invalid.
1036
+ // If you want more safety, used the actual c10::weak_intrusive_ptr class
1037
+ }
1038
+
1039
+ template <typename T>
1040
+ inline T* lock(T* self) {
1041
+ auto wptr = c10::weak_intrusive_ptr<T>::reclaim(self);
1042
+ auto ptr = wptr.lock();
1043
+ wptr.release();
1044
+ return ptr.release();
1045
+ }
1046
+
1047
+ // This gives the STRONG refcount of a WEAK pointer
1048
+ inline uint32_t use_count(weak_intrusive_ptr_target* self) {
1049
+ auto wptr = c10::weak_intrusive_ptr<intrusive_ptr_target>::reclaim(self);
1050
+ auto r = wptr.use_count();
1051
+ wptr.release();
1052
+ return r;
1053
+ }
1054
+
1055
+ } // namespace weak_intrusive_ptr
1056
+
1057
+ } // namespace raw
1058
+
1059
+ } // namespace c10
1060
+
1061
+ namespace std {
1062
+ // To allow intrusive_ptr and weak_intrusive_ptr inside std::unordered_map or
1063
+ // std::unordered_set, we need std::hash
1064
+ template <class TTarget, class NullType>
1065
+ struct hash<c10::intrusive_ptr<TTarget, NullType>> {
1066
+ size_t operator()(const c10::intrusive_ptr<TTarget, NullType>& x) const {
1067
+ return std::hash<TTarget*>()(x.get());
1068
+ }
1069
+ };
1070
+ template <class TTarget, class NullType>
1071
+ struct hash<c10::weak_intrusive_ptr<TTarget, NullType>> {
1072
+ size_t operator()(const c10::weak_intrusive_ptr<TTarget, NullType>& x) const {
1073
+ return std::hash<TTarget*>()(x._unsafe_get_target());
1074
+ }
1075
+ };
1076
+ } // namespace std
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/irange.h ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2004-present Facebook. All Rights Reserved.
2
+
3
+ #pragma once
4
+
5
+ #include <c10/util/TypeSafeSignMath.h>
6
+
7
+ #include <algorithm>
8
+ #include <cstddef>
9
+ #include <iterator>
10
+ #include <type_traits>
11
+
12
+ namespace c10 {
13
+
14
+ namespace detail {
15
+
16
+ template <
17
+ typename I,
18
+ bool one_sided = false,
19
+ std::enable_if_t<std::is_integral_v<I>, int> = 0>
20
+ struct integer_iterator {
21
+ using iterator_category = std::input_iterator_tag;
22
+ using value_type = I;
23
+ using difference_type = std::ptrdiff_t;
24
+ using pointer = I*;
25
+ using reference = I&;
26
+
27
+ explicit constexpr integer_iterator(I value) : value(value) {}
28
+
29
+ constexpr I operator*() const {
30
+ return value;
31
+ }
32
+
33
+ constexpr I const* operator->() const {
34
+ return &value;
35
+ }
36
+
37
+ constexpr integer_iterator& operator++() {
38
+ ++value;
39
+ return *this;
40
+ }
41
+
42
+ constexpr integer_iterator operator++(int) {
43
+ const auto copy = *this;
44
+ ++*this;
45
+ return copy;
46
+ }
47
+
48
+ constexpr bool operator==(const integer_iterator& other) const {
49
+ if constexpr (one_sided) {
50
+ // Range-for loops' end test is `begin != end`, not `begin <
51
+ // end`. To handle `c10::irange(n)` where n < 0 (which should be
52
+ // empty), we just make `begin != end` fail whenever `end` is
53
+ // negative.
54
+ return is_negative(other.value) || value == other.value;
55
+ } else {
56
+ return value == other.value;
57
+ }
58
+ // Suppress "warning: missing return statement at end of non-void function"
59
+ // which Nvidia's Robert Crovella confirms is an NVCC compiler error
60
+ // here https://stackoverflow.com/a/64561686/752843 on 2020-10-27
61
+ // `__builtin_unreachable();` would be best here, but it's not
62
+ // available with all compilers. So we instead return an arbitrary
63
+ // value trusting that this line will, in fact, never be reached.
64
+ return false; // Horrible hack
65
+ }
66
+
67
+ constexpr bool operator!=(const integer_iterator& other) const {
68
+ return !(*this == other);
69
+ }
70
+
71
+ protected:
72
+ I value;
73
+ };
74
+
75
+ } // namespace detail
76
+
77
+ template <
78
+ typename I,
79
+ bool one_sided = false,
80
+ std::enable_if_t<std::is_integral_v<I>, bool> = true>
81
+ struct integer_range {
82
+ public:
83
+ constexpr integer_range(I begin, I end) : begin_(begin), end_(end) {}
84
+ using iterator = detail::integer_iterator<I, one_sided>;
85
+ constexpr iterator begin() const {
86
+ return begin_;
87
+ }
88
+ constexpr iterator end() const {
89
+ return end_;
90
+ }
91
+
92
+ private:
93
+ iterator begin_;
94
+ iterator end_;
95
+ };
96
+
97
+ /// Creates an integer range for the half-open interval [begin, end)
98
+ /// If end<=begin, then the range is empty.
99
+ /// The range has the type of the `end` integer; `begin` integer is
100
+ /// cast to this type.
101
+ template <
102
+ typename Integer1,
103
+ typename Integer2,
104
+ std::enable_if_t<std::is_integral_v<Integer1>, bool> = true,
105
+ std::enable_if_t<std::is_integral_v<Integer2>, bool> = true>
106
+ constexpr integer_range<Integer2> irange(Integer1 begin, Integer2 end) {
107
+ // If end<=begin then the range is empty; we can achieve this effect by
108
+ // choosing the larger of {begin, end} as the loop terminator
109
+ return {
110
+ static_cast<Integer2>(begin),
111
+ std::max(static_cast<Integer2>(begin), end)};
112
+ }
113
+
114
+ /// Creates an integer range for the half-open interval [0, end)
115
+ /// If end<=begin, then the range is empty
116
+ template <
117
+ typename Integer,
118
+ std::enable_if_t<std::is_integral_v<Integer>, bool> = true>
119
+ constexpr integer_range<Integer, true> irange(Integer end) {
120
+ return {Integer(), end};
121
+ }
122
+
123
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/llvmMathExtras.h ADDED
@@ -0,0 +1,905 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2
+ //
3
+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
+ // See https://llvm.org/LICENSE.txt for license information.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
+ //
7
+ //===----------------------------------------------------------------------===//
8
+ //
9
+ // This file contains some functions that are useful for math stuff.
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ #pragma once
14
+
15
+ #include <c10/util/bit_cast.h>
16
+
17
+ #include <algorithm>
18
+ #include <cassert>
19
+ #include <climits>
20
+ #include <cmath>
21
+ #include <cstdint>
22
+ #include <cstring>
23
+ #include <limits>
24
+ #include <type_traits>
25
+
26
+ #ifdef __ANDROID_NDK__
27
+ #include <android/api-level.h>
28
+ #endif
29
+
30
+ #ifndef __has_builtin
31
+ #define __has_builtin(x) 0
32
+ #endif
33
+
34
+ #ifndef LLVM_GNUC_PREREQ
35
+ #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
36
+ #define LLVM_GNUC_PREREQ(maj, min, patch) \
37
+ ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
38
+ ((maj) << 20) + ((min) << 10) + (patch))
39
+ #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
40
+ #define LLVM_GNUC_PREREQ(maj, min, patch) \
41
+ ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
42
+ #else
43
+ #define LLVM_GNUC_PREREQ(maj, min, patch) 0
44
+ #endif
45
+ #endif
46
+
47
+ #ifdef _MSC_VER
48
+ // Declare these intrinsics manually rather including intrin.h. It's very
49
+ // expensive, and MathExtras.h is popular.
50
+ // #include <intrin.h>
51
+ extern "C" {
52
+ unsigned char _BitScanForward(unsigned long* _Index, unsigned long _Mask);
53
+ unsigned char _BitScanForward64(unsigned long* _Index, unsigned __int64 _Mask);
54
+ unsigned char _BitScanReverse(unsigned long* _Index, unsigned long _Mask);
55
+ unsigned char _BitScanReverse64(unsigned long* _Index, unsigned __int64 _Mask);
56
+ }
57
+ #endif
58
+
59
+ namespace c10::llvm {
60
+ /// The behavior an operation has on an input of 0.
61
+ enum ZeroBehavior {
62
+ /// The returned value is undefined.
63
+ ZB_Undefined,
64
+ /// The returned value is numeric_limits<T>::max()
65
+ ZB_Max,
66
+ /// The returned value is numeric_limits<T>::digits
67
+ ZB_Width
68
+ };
69
+
70
+ namespace detail {
71
+ template <typename T, std::size_t SizeOfT>
72
+ struct TrailingZerosCounter {
73
+ static std::size_t count(T Val, ZeroBehavior) {
74
+ if (!Val)
75
+ return std::numeric_limits<T>::digits;
76
+ if (Val & 0x1)
77
+ return 0;
78
+
79
+ // Bisection method.
80
+ std::size_t ZeroBits = 0;
81
+ T Shift = std::numeric_limits<T>::digits >> 1;
82
+ T Mask = std::numeric_limits<T>::max() >> Shift;
83
+ while (Shift) {
84
+ if ((Val & Mask) == 0) {
85
+ Val >>= Shift;
86
+ ZeroBits |= Shift;
87
+ }
88
+ Shift >>= 1;
89
+ Mask >>= Shift;
90
+ }
91
+ return ZeroBits;
92
+ }
93
+ };
94
+
95
+ #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(_MSC_VER)
96
+ template <typename T>
97
+ struct TrailingZerosCounter<T, 4> {
98
+ static std::size_t count(T Val, ZeroBehavior ZB) {
99
+ if (ZB != ZB_Undefined && Val == 0)
100
+ return 32;
101
+
102
+ #if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0)
103
+ return __builtin_ctz(Val);
104
+ #elif defined(_MSC_VER)
105
+ unsigned long Index;
106
+ _BitScanForward(&Index, Val);
107
+ return Index;
108
+ #endif
109
+ }
110
+ };
111
+
112
+ #if !defined(_MSC_VER) || defined(_M_X64)
113
+ template <typename T>
114
+ struct TrailingZerosCounter<T, 8> {
115
+ static std::size_t count(T Val, ZeroBehavior ZB) {
116
+ if (ZB != ZB_Undefined && Val == 0)
117
+ return 64;
118
+
119
+ #if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0)
120
+ return __builtin_ctzll(Val);
121
+ #elif defined(_MSC_VER)
122
+ unsigned long Index;
123
+ _BitScanForward64(&Index, Val);
124
+ return Index;
125
+ #endif
126
+ }
127
+ };
128
+ #endif
129
+ #endif
130
+ } // namespace detail
131
+
132
+ /// Count number of 0's from the least significant bit to the most
133
+ /// stopping at the first 1.
134
+ ///
135
+ /// Only unsigned integral types are allowed.
136
+ ///
137
+ /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
138
+ /// valid arguments.
139
+ template <typename T>
140
+ std::size_t countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
141
+ static_assert(
142
+ std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
143
+ "Only unsigned integral types are allowed.");
144
+ return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB);
145
+ }
146
+
147
+ namespace detail {
148
+ template <typename T, std::size_t SizeOfT>
149
+ struct LeadingZerosCounter {
150
+ static std::size_t count(T Val, ZeroBehavior) {
151
+ if (!Val)
152
+ return std::numeric_limits<T>::digits;
153
+
154
+ // Bisection method.
155
+ std::size_t ZeroBits = 0;
156
+ for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
157
+ T Tmp = Val >> Shift;
158
+ if (Tmp)
159
+ Val = Tmp;
160
+ else
161
+ ZeroBits |= Shift;
162
+ }
163
+ return ZeroBits;
164
+ }
165
+ };
166
+
167
+ #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(_MSC_VER)
168
+ template <typename T>
169
+ struct LeadingZerosCounter<T, 4> {
170
+ static std::size_t count(T Val, ZeroBehavior ZB) {
171
+ if (ZB != ZB_Undefined && Val == 0)
172
+ return 32;
173
+
174
+ #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
175
+ return __builtin_clz(Val);
176
+ #elif defined(_MSC_VER)
177
+ unsigned long Index;
178
+ _BitScanReverse(&Index, Val);
179
+ return Index ^ 31;
180
+ #endif
181
+ }
182
+ };
183
+
184
+ #if !defined(_MSC_VER) || defined(_M_X64)
185
+ template <typename T>
186
+ struct LeadingZerosCounter<T, 8> {
187
+ static std::size_t count(T Val, ZeroBehavior ZB) {
188
+ if (ZB != ZB_Undefined && Val == 0)
189
+ return 64;
190
+
191
+ #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
192
+ return __builtin_clzll(Val);
193
+ #elif defined(_MSC_VER)
194
+ unsigned long Index;
195
+ _BitScanReverse64(&Index, Val);
196
+ return Index ^ 63;
197
+ #endif
198
+ }
199
+ };
200
+ #endif
201
+ #endif
202
+ } // namespace detail
203
+
204
+ /// Count number of 0's from the most significant bit to the least
205
+ /// stopping at the first 1.
206
+ ///
207
+ /// Only unsigned integral types are allowed.
208
+ ///
209
+ /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
210
+ /// valid arguments.
211
+ template <typename T>
212
+ std::size_t countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
213
+ static_assert(
214
+ std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
215
+ "Only unsigned integral types are allowed.");
216
+ return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
217
+ }
218
+
219
+ /// Get the index of the first set bit starting from the least
220
+ /// significant bit.
221
+ ///
222
+ /// Only unsigned integral types are allowed.
223
+ ///
224
+ /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
225
+ /// valid arguments.
226
+ template <typename T>
227
+ T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
228
+ if (ZB == ZB_Max && Val == 0)
229
+ return std::numeric_limits<T>::max();
230
+
231
+ return countTrailingZeros(Val, ZB_Undefined);
232
+ }
233
+
234
+ /// Create a bitmask with the N right-most bits set to 1, and all other
235
+ /// bits set to 0. Only unsigned types are allowed.
236
+ template <typename T>
237
+ T maskTrailingOnes(unsigned N) {
238
+ static_assert(std::is_unsigned_v<T>, "Invalid type!");
239
+ const unsigned Bits = CHAR_BIT * sizeof(T);
240
+ assert(N <= Bits && "Invalid bit index");
241
+ return N == 0 ? 0 : (T(-1) >> (Bits - N));
242
+ }
243
+
244
+ /// Create a bitmask with the N left-most bits set to 1, and all other
245
+ /// bits set to 0. Only unsigned types are allowed.
246
+ template <typename T>
247
+ T maskLeadingOnes(unsigned N) {
248
+ return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
249
+ }
250
+
251
+ /// Create a bitmask with the N right-most bits set to 0, and all other
252
+ /// bits set to 1. Only unsigned types are allowed.
253
+ template <typename T>
254
+ T maskTrailingZeros(unsigned N) {
255
+ return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
256
+ }
257
+
258
+ /// Create a bitmask with the N left-most bits set to 0, and all other
259
+ /// bits set to 1. Only unsigned types are allowed.
260
+ template <typename T>
261
+ T maskLeadingZeros(unsigned N) {
262
+ return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
263
+ }
264
+
265
+ /// Get the index of the last set bit starting from the least
266
+ /// significant bit.
267
+ ///
268
+ /// Only unsigned integral types are allowed.
269
+ ///
270
+ /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
271
+ /// valid arguments.
272
+ template <typename T>
273
+ T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
274
+ if (ZB == ZB_Max && Val == 0)
275
+ return std::numeric_limits<T>::max();
276
+
277
+ // Use ^ instead of - because both gcc and llvm can remove the associated ^
278
+ // in the __builtin_clz intrinsic on x86.
279
+ return countLeadingZeros(Val, ZB_Undefined) ^
280
+ (std::numeric_limits<T>::digits - 1);
281
+ }
282
+
283
+ /// Macro compressed bit reversal table for 256 bits.
284
+ ///
285
+ /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
286
+ /// NOLINTNEXTLINE(*c-arrays*)
287
+ static constexpr unsigned char BitReverseTable256[256] = {
288
+ #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
289
+ #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
290
+ #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
291
+ R6(0),
292
+ R6(2),
293
+ R6(1),
294
+ R6(3)
295
+ #undef R2
296
+ #undef R4
297
+ #undef R6
298
+ };
299
+
300
+ /// Reverse the bits in \p Val.
301
+ template <typename T>
302
+ T reverseBits(T Val) {
303
+ // NOLINTNEXTLINE(*c-arrays*)
304
+ unsigned char in[sizeof(Val)];
305
+ // NOLINTNEXTLINE(*c-arrays*)
306
+ unsigned char out[sizeof(Val)];
307
+ std::memcpy(in, &Val, sizeof(Val));
308
+ for (unsigned i = 0; i < sizeof(Val); ++i)
309
+ out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
310
+ std::memcpy(&Val, out, sizeof(Val));
311
+ return Val;
312
+ }
313
+
314
+ // NOTE: The following support functions use the _32/_64 extensions instead of
315
+ // type overloading so that signed and unsigned integers can be used without
316
+ // ambiguity.
317
+
318
+ /// Return the high 32 bits of a 64 bit value.
319
+ constexpr inline uint32_t Hi_32(uint64_t Value) {
320
+ return static_cast<uint32_t>(Value >> 32);
321
+ }
322
+
323
+ /// Return the low 32 bits of a 64 bit value.
324
+ constexpr inline uint32_t Lo_32(uint64_t Value) {
325
+ return static_cast<uint32_t>(Value);
326
+ }
327
+
328
+ /// Make a 64-bit integer from a high / low pair of 32-bit integers.
329
+ constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
330
+ return ((uint64_t)High << 32) | (uint64_t)Low;
331
+ }
332
+
333
+ /// Checks if an integer fits into the given bit width.
334
+ template <unsigned N>
335
+ constexpr inline bool isInt(int64_t x) {
336
+ return N >= 64 ||
337
+ (-(INT64_C(1) << (N - 1)) <= x && x < (INT64_C(1) << (N - 1)));
338
+ }
339
+ // Template specializations to get better code for common cases.
340
+ template <>
341
+ constexpr inline bool isInt<8>(int64_t x) {
342
+ return static_cast<int8_t>(x) == x;
343
+ }
344
+ template <>
345
+ constexpr inline bool isInt<16>(int64_t x) {
346
+ return static_cast<int16_t>(x) == x;
347
+ }
348
+ template <>
349
+ constexpr inline bool isInt<32>(int64_t x) {
350
+ return static_cast<int32_t>(x) == x;
351
+ }
352
+
353
+ /// Checks if a signed integer is an N bit number shifted left by S.
354
+ template <unsigned N, unsigned S>
355
+ constexpr inline bool isShiftedInt(int64_t x) {
356
+ static_assert(
357
+ N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
358
+ static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide.");
359
+ return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
360
+ }
361
+
362
+ /// Checks if an unsigned integer fits into the given bit width.
363
+ ///
364
+ /// This is written as two functions rather than as simply
365
+ ///
366
+ /// return N >= 64 || X < (UINT64_C(1) << N);
367
+ ///
368
+ /// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting
369
+ /// left too many places.
370
+ template <unsigned N>
371
+ constexpr inline std::enable_if_t<(N < 64), bool> isUInt(uint64_t X) {
372
+ static_assert(N > 0, "isUInt<0> doesn't make sense");
373
+ return X < (UINT64_C(1) << (N));
374
+ }
375
+ template <unsigned N>
376
+ constexpr inline std::enable_if_t<N >= 64, bool> isUInt(uint64_t /*X*/) {
377
+ return true;
378
+ }
379
+
380
+ // Template specializations to get better code for common cases.
381
+ template <>
382
+ constexpr inline bool isUInt<8>(uint64_t x) {
383
+ return static_cast<uint8_t>(x) == x;
384
+ }
385
+ template <>
386
+ constexpr inline bool isUInt<16>(uint64_t x) {
387
+ return static_cast<uint16_t>(x) == x;
388
+ }
389
+ template <>
390
+ constexpr inline bool isUInt<32>(uint64_t x) {
391
+ return static_cast<uint32_t>(x) == x;
392
+ }
393
+
394
+ /// Checks if a unsigned integer is an N bit number shifted left by S.
395
+ template <unsigned N, unsigned S>
396
+ constexpr inline bool isShiftedUInt(uint64_t x) {
397
+ static_assert(
398
+ N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
399
+ static_assert(
400
+ N + S <= 64, "isShiftedUInt<N, S> with N + S > 64 is too wide.");
401
+ // Per the two static_asserts above, S must be strictly less than 64. So
402
+ // 1 << S is not undefined behavior.
403
+ return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
404
+ }
405
+
406
+ /// Gets the maximum value for a N-bit unsigned integer.
407
+ inline uint64_t maxUIntN(uint64_t N) {
408
+ assert(N > 0 && N <= 64 && "integer width out of range");
409
+
410
+ // uint64_t(1) << 64 is undefined behavior, so we can't do
411
+ // (uint64_t(1) << N) - 1
412
+ // without checking first that N != 64. But this works and doesn't have a
413
+ // branch.
414
+ return UINT64_MAX >> (64 - N);
415
+ }
416
+
417
+ // Ignore the false warning "Arithmetic overflow" for MSVC
418
+ #ifdef _MSC_VER
419
+ #pragma warning(push)
420
+ #pragma warning(disable : 4146)
421
+ #endif
422
+
423
+ /// Gets the minimum value for a N-bit signed integer.
424
+ inline int64_t minIntN(int64_t N) {
425
+ assert(N > 0 && N <= 64 && "integer width out of range");
426
+ // NOLINTNEXTLINE(*-narrowing-conversions)
427
+ return -(UINT64_C(1) << (N - 1));
428
+ }
429
+
430
+ #ifdef _MSC_VER
431
+ #pragma warning(pop)
432
+ #endif
433
+
434
+ /// Gets the maximum value for a N-bit signed integer.
435
+ inline int64_t maxIntN(int64_t N) {
436
+ assert(N > 0 && N <= 64 && "integer width out of range");
437
+
438
+ // This relies on two's complement wraparound when N == 64, so we convert to
439
+ // int64_t only at the very end to avoid UB.
440
+ // NOLINTNEXTLINE(*-narrowing-conversions)
441
+ return (UINT64_C(1) << (N - 1)) - 1;
442
+ }
443
+
444
+ /// Checks if an unsigned integer fits into the given (dynamic) bit width.
445
+ inline bool isUIntN(unsigned N, uint64_t x) {
446
+ return N >= 64 || x <= maxUIntN(N);
447
+ }
448
+
449
+ /// Checks if an signed integer fits into the given (dynamic) bit width.
450
+ inline bool isIntN(unsigned N, int64_t x) {
451
+ return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
452
+ }
453
+
454
+ /// Return true if the argument is a non-empty sequence of ones starting at the
455
+ /// least significant bit with the remainder zero (32 bit version).
456
+ /// Ex. isMask_32(0x0000FFFFU) == true.
457
+ constexpr inline bool isMask_32(uint32_t Value) {
458
+ return Value && ((Value + 1) & Value) == 0;
459
+ }
460
+
461
+ /// Return true if the argument is a non-empty sequence of ones starting at the
462
+ /// least significant bit with the remainder zero (64 bit version).
463
+ constexpr inline bool isMask_64(uint64_t Value) {
464
+ return Value && ((Value + 1) & Value) == 0;
465
+ }
466
+
467
+ /// Return true if the argument contains a non-empty sequence of ones with the
468
+ /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
469
+ constexpr inline bool isShiftedMask_32(uint32_t Value) {
470
+ return Value && isMask_32((Value - 1) | Value);
471
+ }
472
+
473
+ /// Return true if the argument contains a non-empty sequence of ones with the
474
+ /// remainder zero (64 bit version.)
475
+ constexpr inline bool isShiftedMask_64(uint64_t Value) {
476
+ return Value && isMask_64((Value - 1) | Value);
477
+ }
478
+
479
+ /// Return true if the argument is a power of two > 0.
480
+ /// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
481
+ constexpr inline bool isPowerOf2_32(uint32_t Value) {
482
+ return Value && !(Value & (Value - 1));
483
+ }
484
+
485
+ /// Return true if the argument is a power of two > 0 (64 bit edition.)
486
+ constexpr inline bool isPowerOf2_64(uint64_t Value) {
487
+ return Value && !(Value & (Value - 1));
488
+ }
489
+
490
+ /// Count the number of ones from the most significant bit to the first
491
+ /// zero bit.
492
+ ///
493
+ /// Ex. countLeadingOnes(0xFF0FFF00) == 8.
494
+ /// Only unsigned integral types are allowed.
495
+ ///
496
+ /// \param ZB the behavior on an input of all ones. Only ZB_Width and
497
+ /// ZB_Undefined are valid arguments.
498
+ template <typename T>
499
+ std::size_t countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
500
+ static_assert(
501
+ std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
502
+ "Only unsigned integral types are allowed.");
503
+ return countLeadingZeros<T>(~Value, ZB);
504
+ }
505
+
506
+ /// Count the number of ones from the least significant bit to the first
507
+ /// zero bit.
508
+ ///
509
+ /// Ex. countTrailingOnes(0x00FF00FF) == 8.
510
+ /// Only unsigned integral types are allowed.
511
+ ///
512
+ /// \param ZB the behavior on an input of all ones. Only ZB_Width and
513
+ /// ZB_Undefined are valid arguments.
514
+ template <typename T>
515
+ std::size_t countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
516
+ static_assert(
517
+ std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
518
+ "Only unsigned integral types are allowed.");
519
+ return countTrailingZeros<T>(~Value, ZB);
520
+ }
521
+
522
+ namespace detail {
523
+ template <typename T, std::size_t SizeOfT>
524
+ struct PopulationCounter {
525
+ static unsigned count(T Value) {
526
+ // Generic version, forward to 32 bits.
527
+ static_assert(SizeOfT <= 4, "Not implemented!");
528
+ #if defined(__GNUC__) && __GNUC__ >= 4
529
+ return __builtin_popcount(Value);
530
+ #else
531
+ uint32_t v = Value;
532
+ v = v - ((v >> 1) & 0x55555555);
533
+ v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
534
+ return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
535
+ #endif
536
+ }
537
+ };
538
+
539
+ template <typename T>
540
+ struct PopulationCounter<T, 8> {
541
+ static unsigned count(T Value) {
542
+ #if defined(__GNUC__) && __GNUC__ >= 4
543
+ return __builtin_popcountll(Value);
544
+ #else
545
+ uint64_t v = Value;
546
+ v = v - ((v >> 1) & 0x5555555555555555ULL);
547
+ v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
548
+ v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
549
+ return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
550
+ #endif
551
+ }
552
+ };
553
+ } // namespace detail
554
+
555
+ /// Count the number of set bits in a value.
556
+ /// Ex. countPopulation(0xF000F000) = 8
557
+ /// Returns 0 if the word is zero.
558
+ template <typename T>
559
+ inline unsigned countPopulation(T Value) {
560
+ static_assert(
561
+ std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
562
+ "Only unsigned integral types are allowed.");
563
+ return detail::PopulationCounter<T, sizeof(T)>::count(Value);
564
+ }
565
+
566
+ /// Return the log base 2 of the specified value.
567
+ inline double Log2(double Value) {
568
+ #if defined(__ANDROID_API__) && __ANDROID_API__ < 18
569
+ return __builtin_log(Value) / __builtin_log(2.0);
570
+ #else
571
+ return log2(Value);
572
+ #endif
573
+ }
574
+
575
+ /// Return the floor log base 2 of the specified value, -1 if the value is zero.
576
+ /// (32 bit edition.)
577
+ /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
578
+ inline unsigned Log2_32(uint32_t Value) {
579
+ return static_cast<unsigned>(31 - countLeadingZeros(Value));
580
+ }
581
+
582
+ /// Return the floor log base 2 of the specified value, -1 if the value is zero.
583
+ /// (64 bit edition.)
584
+ inline unsigned Log2_64(uint64_t Value) {
585
+ return static_cast<unsigned>(63 - countLeadingZeros(Value));
586
+ }
587
+
588
+ /// Return the ceil log base 2 of the specified value, 32 if the value is zero.
589
+ /// (32 bit edition).
590
+ /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
591
+ inline unsigned Log2_32_Ceil(uint32_t Value) {
592
+ return static_cast<unsigned>(32 - countLeadingZeros(Value - 1));
593
+ }
594
+
595
+ /// Return the ceil log base 2 of the specified value, 64 if the value is zero.
596
+ /// (64 bit edition.)
597
+ inline unsigned Log2_64_Ceil(uint64_t Value) {
598
+ return static_cast<unsigned>(64 - countLeadingZeros(Value - 1));
599
+ }
600
+
601
+ /// Return the greatest common divisor of the values using Euclid's algorithm.
602
+ inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
603
+ while (B) {
604
+ uint64_t T = B;
605
+ B = A % B;
606
+ A = T;
607
+ }
608
+ return A;
609
+ }
610
+
611
+ /// This function takes a 64-bit integer and returns the bit equivalent double.
612
+ inline double BitsToDouble(uint64_t Bits) {
613
+ double D = 0;
614
+ static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
615
+ memcpy(&D, &Bits, sizeof(Bits));
616
+ return D;
617
+ }
618
+
619
+ /// This function takes a 32-bit integer and returns the bit equivalent float.
620
+ inline float BitsToFloat(uint32_t Bits) {
621
+ // TODO: Use std::bit_cast once C++20 becomes available.
622
+ return c10::bit_cast<float>(Bits);
623
+ }
624
+
625
+ /// This function takes a double and returns the bit equivalent 64-bit integer.
626
+ /// Note that copying doubles around changes the bits of NaNs on some hosts,
627
+ /// notably x86, so this routine cannot be used if these bits are needed.
628
+ inline uint64_t DoubleToBits(double Double) {
629
+ // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
630
+ uint64_t Bits;
631
+ static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
632
+ memcpy(&Bits, &Double, sizeof(Double));
633
+ return Bits;
634
+ }
635
+
636
+ /// This function takes a float and returns the bit equivalent 32-bit integer.
637
+ /// Note that copying floats around changes the bits of NaNs on some hosts,
638
+ /// notably x86, so this routine cannot be used if these bits are needed.
639
+ inline uint32_t FloatToBits(float Float) {
640
+ // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
641
+ uint32_t Bits;
642
+ static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
643
+ memcpy(&Bits, &Float, sizeof(Float));
644
+ return Bits;
645
+ }
646
+
647
+ /// A and B are either alignments or offsets. Return the minimum alignment that
648
+ /// may be assumed after adding the two together.
649
+ constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
650
+ // The largest power of 2 that divides both A and B.
651
+ //
652
+ // Replace "-Value" by "1+~Value" in the following commented code to avoid
653
+ // MSVC warning C4146
654
+ // return (A | B) & -(A | B);
655
+ return (A | B) & (1 + ~(A | B));
656
+ }
657
+
658
+ /// Aligns \c Addr to \c Alignment bytes, rounding up.
659
+ ///
660
+ /// Alignment should be a power of two. This method rounds up, so
661
+ /// alignAddr(7, 4) == 8 and alignAddr(8, 4) == 8.
662
+ inline uintptr_t alignAddr(const void* Addr, size_t Alignment) {
663
+ assert(
664
+ Alignment && isPowerOf2_64((uint64_t)Alignment) &&
665
+ "Alignment is not a power of two!");
666
+
667
+ assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
668
+
669
+ return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
670
+ }
671
+
672
+ /// Returns the necessary adjustment for aligning \c Ptr to \c Alignment
673
+ /// bytes, rounding up.
674
+ inline size_t alignmentAdjustment(const void* Ptr, size_t Alignment) {
675
+ return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
676
+ }
677
+
678
+ /// Returns the next power of two (in 64-bits) that is strictly greater than A.
679
+ /// Returns zero on overflow.
680
+ inline uint64_t NextPowerOf2(uint64_t A) {
681
+ A |= (A >> 1);
682
+ A |= (A >> 2);
683
+ A |= (A >> 4);
684
+ A |= (A >> 8);
685
+ A |= (A >> 16);
686
+ A |= (A >> 32);
687
+ return A + 1;
688
+ }
689
+
690
+ /// Returns the power of two which is less than or equal to the given value.
691
+ /// Essentially, it is a floor operation across the domain of powers of two.
692
+ inline uint64_t PowerOf2Floor(uint64_t A) {
693
+ if (!A)
694
+ return 0;
695
+ return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
696
+ }
697
+
698
+ /// Returns the power of two which is greater than or equal to the given value.
699
+ /// Essentially, it is a ceil operation across the domain of powers of two.
700
+ inline uint64_t PowerOf2Ceil(uint64_t A) {
701
+ if (!A)
702
+ return 0;
703
+ return NextPowerOf2(A - 1);
704
+ }
705
+
706
+ /// Returns the next integer (mod 2**64) that is greater than or equal to
707
+ /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
708
+ ///
709
+ /// If non-zero \p Skew is specified, the return value will be a minimal
710
+ /// integer that is greater than or equal to \p Value and equal to
711
+ /// \p Align * N + \p Skew for some integer N. If \p Skew is larger than
712
+ /// \p Align, its value is adjusted to '\p Skew mod \p Align'.
713
+ ///
714
+ /// Examples:
715
+ /// \code
716
+ /// alignTo(5, 8) = 8
717
+ /// alignTo(17, 8) = 24
718
+ /// alignTo(~0LL, 8) = 0
719
+ /// alignTo(321, 255) = 510
720
+ ///
721
+ /// alignTo(5, 8, 7) = 7
722
+ /// alignTo(17, 8, 1) = 17
723
+ /// alignTo(~0LL, 8, 3) = 3
724
+ /// alignTo(321, 255, 42) = 552
725
+ /// \endcode
726
+ inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
727
+ assert(Align != 0u && "Align can't be 0.");
728
+ Skew %= Align;
729
+ return (Value + Align - 1 - Skew) / Align * Align + Skew;
730
+ }
731
+
732
+ /// Returns the next integer (mod 2**64) that is greater than or equal to
733
+ /// \p Value and is a multiple of \c Align. \c Align must be non-zero.
734
+ template <uint64_t Align>
735
+ constexpr inline uint64_t alignTo(uint64_t Value) {
736
+ static_assert(Align != 0u, "Align must be non-zero");
737
+ return (Value + Align - 1) / Align * Align;
738
+ }
739
+
740
+ /// Returns the integer ceil(Numerator / Denominator).
741
+ inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
742
+ return alignTo(Numerator, Denominator) / Denominator;
743
+ }
744
+
745
+ /// \c alignTo for contexts where a constant expression is required.
746
+ /// \sa alignTo
747
+ ///
748
+ /// \todo FIXME: remove when \c constexpr becomes really \c constexpr
749
+ template <uint64_t Align>
750
+ struct AlignTo {
751
+ static_assert(Align != 0u, "Align must be non-zero");
752
+ template <uint64_t Value>
753
+ struct from_value {
754
+ static const uint64_t value = (Value + Align - 1) / Align * Align;
755
+ };
756
+ };
757
+
758
+ /// Returns the largest uint64_t less than or equal to \p Value and is
759
+ /// \p Skew mod \p Align. \p Align must be non-zero
760
+ inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
761
+ assert(Align != 0u && "Align can't be 0.");
762
+ Skew %= Align;
763
+ return (Value - Skew) / Align * Align + Skew;
764
+ }
765
+
766
+ /// Returns the offset to the next integer (mod 2**64) that is greater than
767
+ /// or equal to \p Value and is a multiple of \p Align. \p Align must be
768
+ /// non-zero.
769
+ inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
770
+ return alignTo(Value, Align) - Value;
771
+ }
772
+
773
+ /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
774
+ /// Requires 0 < B <= 32.
775
+ template <unsigned B>
776
+ constexpr inline int32_t SignExtend32(uint32_t X) {
777
+ static_assert(B > 0, "Bit width can't be 0.");
778
+ static_assert(B <= 32, "Bit width out of range.");
779
+ return int32_t(X << (32 - B)) >> (32 - B);
780
+ }
781
+
782
+ /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
783
+ /// Requires 0 < B < 32.
784
+ inline int32_t SignExtend32(uint32_t X, unsigned B) {
785
+ assert(B > 0 && "Bit width can't be 0.");
786
+ assert(B <= 32 && "Bit width out of range.");
787
+ return int32_t(X << (32 - B)) >> (32 - B);
788
+ }
789
+
790
+ /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
791
+ /// Requires 0 < B < 64.
792
+ template <unsigned B>
793
+ constexpr inline int64_t SignExtend64(uint64_t x) {
794
+ static_assert(B > 0, "Bit width can't be 0.");
795
+ static_assert(B <= 64, "Bit width out of range.");
796
+ return int64_t(x << (64 - B)) >> (64 - B);
797
+ }
798
+
799
+ /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
800
+ /// Requires 0 < B < 64.
801
+ inline int64_t SignExtend64(uint64_t X, unsigned B) {
802
+ assert(B > 0 && "Bit width can't be 0.");
803
+ assert(B <= 64 && "Bit width out of range.");
804
+ return int64_t(X << (64 - B)) >> (64 - B);
805
+ }
806
+
807
+ /// Subtract two unsigned integers, X and Y, of type T and return the absolute
808
+ /// value of the result.
809
+ template <typename T>
810
+ std::enable_if_t<std::is_unsigned_v<T>, T> AbsoluteDifference(T X, T Y) {
811
+ return std::max(X, Y) - std::min(X, Y);
812
+ }
813
+
814
+ /// Add two unsigned integers, X and Y, of type T. Clamp the result to the
815
+ /// maximum representable value of T on overflow. ResultOverflowed indicates if
816
+ /// the result is larger than the maximum representable value of type T.
817
+ template <typename T>
818
+ std::enable_if_t<std::is_unsigned_v<T>, T> SaturatingAdd(
819
+ T X,
820
+ T Y,
821
+ bool* ResultOverflowed = nullptr) {
822
+ // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
823
+ bool Dummy;
824
+ bool& Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
825
+ // Hacker's Delight, p. 29
826
+ T Z = X + Y;
827
+ Overflowed = (Z < X || Z < Y);
828
+ if (Overflowed)
829
+ return std::numeric_limits<T>::max();
830
+ else
831
+ return Z;
832
+ }
833
+
834
+ /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the
835
+ /// maximum representable value of T on overflow. ResultOverflowed indicates if
836
+ /// the result is larger than the maximum representable value of type T.
837
+ template <typename T>
838
+ std::enable_if_t<std::is_unsigned_v<T>, T> SaturatingMultiply(
839
+ T X,
840
+ T Y,
841
+ bool* ResultOverflowed = nullptr) {
842
+ // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
843
+ bool Dummy;
844
+ bool& Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
845
+
846
+ // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
847
+ // because it fails for uint16_t (where multiplication can have undefined
848
+ // behavior due to promotion to int), and requires a division in addition
849
+ // to the multiplication.
850
+
851
+ Overflowed = false;
852
+
853
+ // Log2(Z) would be either Log2Z or Log2Z + 1.
854
+ // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
855
+ // will necessarily be less than Log2Max as desired.
856
+ int Log2Z = Log2_64(X) + Log2_64(Y);
857
+ const T Max = std::numeric_limits<T>::max();
858
+ int Log2Max = Log2_64(Max);
859
+ if (Log2Z < Log2Max) {
860
+ return X * Y;
861
+ }
862
+ if (Log2Z > Log2Max) {
863
+ Overflowed = true;
864
+ return Max;
865
+ }
866
+
867
+ // We're going to use the top bit, and maybe overflow one
868
+ // bit past it. Multiply all but the bottom bit then add
869
+ // that on at the end.
870
+ T Z = (X >> 1) * Y;
871
+ if (Z & ~(Max >> 1)) {
872
+ Overflowed = true;
873
+ return Max;
874
+ }
875
+ Z <<= 1;
876
+ if (X & 1)
877
+ return SaturatingAdd(Z, Y, ResultOverflowed);
878
+
879
+ return Z;
880
+ }
881
+
882
+ /// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to
883
+ /// the product. Clamp the result to the maximum representable value of T on
884
+ /// overflow. ResultOverflowed indicates if the result is larger than the
885
+ /// maximum representable value of type T.
886
+ template <typename T>
887
+ std::enable_if_t<std::is_unsigned_v<T>, T> SaturatingMultiplyAdd(
888
+ T X,
889
+ T Y,
890
+ T A,
891
+ bool* ResultOverflowed = nullptr) {
892
+ // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
893
+ bool Dummy;
894
+ bool& Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
895
+
896
+ T Product = SaturatingMultiply(X, Y, &Overflowed);
897
+ if (Overflowed)
898
+ return Product;
899
+
900
+ return SaturatingAdd(A, Product, &Overflowed);
901
+ }
902
+
903
+ /// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
904
+ extern const float huge_valf;
905
+ } // namespace c10::llvm
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/logging_is_google_glog.h ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef C10_UTIL_LOGGING_IS_GOOGLE_GLOG_H_
2
+ #define C10_UTIL_LOGGING_IS_GOOGLE_GLOG_H_
3
+
4
+ #include <map>
5
+ #include <set>
6
+ #include <vector>
7
+
8
+ #include <iomanip> // because some of the caffe2 code uses e.g. std::setw
9
+ // Using google glog. For glog 0.3.2 versions, stl_logging.h needs to be before
10
+ // logging.h to actually use stl_logging. Because template magic.
11
+ // In addition, we do not do stl logging in .cu files because nvcc does not like
12
+ // it. Some mobile platforms do not like stl_logging, so we add an
13
+ // overload in that case as well.
14
+
15
+ #ifdef __CUDACC__
16
+ #include <cuda.h>
17
+ #endif
18
+
19
+ #if !defined(__CUDACC__) && !defined(C10_USE_MINIMAL_GLOG)
20
+ #include <glog/stl_logging.h>
21
+
22
+ // Old versions of glog don't declare this using declaration, so help
23
+ // them out. Fortunately, C++ won't complain if you declare the same
24
+ // using declaration multiple times.
25
+ namespace std {
26
+ using ::operator<<;
27
+ }
28
+
29
+ #else // !defined(__CUDACC__) && !defined(C10_USE_MINIMAL_GLOG)
30
+
31
+ // In the cudacc compiler scenario, we will simply ignore the container
32
+ // printout feature. Basically we need to register a fake overload for
33
+ // vector/string - here, we just ignore the entries in the logs.
34
+
35
+ namespace std {
36
+ #define INSTANTIATE_FOR_CONTAINER(container) \
37
+ template <class... Types> \
38
+ ostream& operator<<(ostream& out, const container<Types...>&) { \
39
+ return out; \
40
+ }
41
+
42
+ INSTANTIATE_FOR_CONTAINER(vector)
43
+ INSTANTIATE_FOR_CONTAINER(map)
44
+ INSTANTIATE_FOR_CONTAINER(set)
45
+ #undef INSTANTIATE_FOR_CONTAINER
46
+ } // namespace std
47
+
48
+ #endif
49
+
50
+ #include <glog/logging.h>
51
+
52
+ // Additional macros on top of glog
53
+ #define TORCH_CHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
54
+ #define TORCH_CHECK_NE(val1, val2) CHECK_NE(val1, val2)
55
+ #define TORCH_CHECK_LE(val1, val2) CHECK_LE(val1, val2)
56
+ #define TORCH_CHECK_LT(val1, val2) CHECK_LT(val1, val2)
57
+ #define TORCH_CHECK_GE(val1, val2) CHECK_GE(val1, val2)
58
+ #define TORCH_CHECK_GT(val1, val2) CHECK_GT(val1, val2)
59
+
60
+ #ifndef NDEBUG
61
+ #define TORCH_DCHECK_EQ(val1, val2) DCHECK_EQ(val1, val2)
62
+ #define TORCH_DCHECK_NE(val1, val2) DCHECK_NE(val1, val2)
63
+ #define TORCH_DCHECK_LE(val1, val2) DCHECK_LE(val1, val2)
64
+ #define TORCH_DCHECK_LT(val1, val2) DCHECK_LT(val1, val2)
65
+ #define TORCH_DCHECK_GE(val1, val2) DCHECK_GE(val1, val2)
66
+ #define TORCH_DCHECK_GT(val1, val2) DCHECK_GT(val1, val2)
67
+ #else // !NDEBUG
68
+ // These versions generate no code in optimized mode.
69
+ #define TORCH_DCHECK_EQ(val1, val2) \
70
+ while (false) \
71
+ DCHECK_EQ(val1, val2)
72
+ #define TORCH_DCHECK_NE(val1, val2) \
73
+ while (false) \
74
+ DCHECK_NE(val1, val2)
75
+ #define TORCH_DCHECK_LE(val1, val2) \
76
+ while (false) \
77
+ DCHECK_LE(val1, val2)
78
+ #define TORCH_DCHECK_LT(val1, val2) \
79
+ while (false) \
80
+ DCHECK_LT(val1, val2)
81
+ #define TORCH_DCHECK_GE(val1, val2) \
82
+ while (false) \
83
+ DCHECK_GE(val1, val2)
84
+ #define TORCH_DCHECK_GT(val1, val2) \
85
+ while (false) \
86
+ DCHECK_GT(val1, val2)
87
+ #endif // NDEBUG
88
+
89
+ // Check that a pointer is not null.
90
+ #define TORCH_CHECK_NOTNULL(val) CHECK_NOTNULL(val)
91
+
92
+ #ifndef NDEBUG
93
+ // Debug only version of TORCH_CHECK_NOTNULL
94
+ #define TORCH_DCHECK_NOTNULL(val) DCHECK_NOTNULL(val)
95
+ #else // !NDEBUG
96
+ // Optimized version - generates no code.
97
+ #define TORCH_DCHECK_NOTNULL(val) \
98
+ while (false) \
99
+ DCHECK_NOTNULL(val)
100
+ #endif // NDEBUG
101
+
102
+ // Log with source location information override (to be used in generic
103
+ // warning/error handlers implemented as functions, not macros)
104
+ //
105
+ // Note, we don't respect GOOGLE_STRIP_LOG here for simplicity
106
+ #define LOG_AT_FILE_LINE(n, file, line) \
107
+ ::google::LogMessage(file, line, ::google::GLOG_##n).stream()
108
+
109
+ #endif // C10_UTIL_LOGGING_IS_GOOGLE_GLOG_H_
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/logging_is_not_google_glog.h ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef C10_UTIL_LOGGING_IS_NOT_GOOGLE_GLOG_H_
2
+ #define C10_UTIL_LOGGING_IS_NOT_GOOGLE_GLOG_H_
3
+
4
+ #include <chrono>
5
+ #include <climits>
6
+ #include <ctime>
7
+ #include <iomanip>
8
+ #include <map>
9
+ #include <ostream>
10
+ #include <set>
11
+ #include <sstream>
12
+ #include <string>
13
+ #include <vector>
14
+
15
+ #include <c10/util/Flags.h>
16
+
17
+ const char CAFFE2_SEVERITY_PREFIX[] = "FEWIV";
18
+
19
+ namespace c10 {
20
+
21
+ // Log severity level constants.
22
+ const int GLOG_FATAL = 3;
23
+ const int GLOG_ERROR = 2;
24
+ const int GLOG_WARNING = 1;
25
+ const int GLOG_INFO = 0;
26
+
27
+ class C10_API MessageLogger {
28
+ public:
29
+ MessageLogger(const char* file, int line, int severity);
30
+ ~MessageLogger();
31
+ // Return the stream associated with the logger object.
32
+ std::stringstream& stream() {
33
+ return stream_;
34
+ }
35
+
36
+ private:
37
+ // When there is a fatal log, we simply abort.
38
+ void DealWithFatal() {
39
+ abort();
40
+ }
41
+
42
+ const char* tag_;
43
+ std::stringstream stream_;
44
+ int severity_;
45
+ };
46
+
47
+ // This class is used to explicitly ignore values in the conditional
48
+ // logging macros. This avoids compiler warnings like "value computed
49
+ // is not used" and "statement has no effect".
50
+ class C10_API LoggerVoidify {
51
+ public:
52
+ LoggerVoidify() = default;
53
+ // This has to be an operator with a precedence lower than << but
54
+ // higher than ?:
55
+ void operator&(const std::ostream& s [[maybe_unused]]) {}
56
+ };
57
+
58
+ // Log a message and terminate.
59
+ template <class T>
60
+ void LogMessageFatal(const char* file, int line, const T& message) {
61
+ MessageLogger(file, line, GLOG_FATAL).stream() << message;
62
+ }
63
+
64
+ // Helpers for TORCH_CHECK_NOTNULL(). Two are necessary to support both raw
65
+ // pointers and smart pointers.
66
+ template <typename T>
67
+ T& CheckNotNullCommon(const char* file, int line, const char* names, T& t) {
68
+ if (t == nullptr) {
69
+ LogMessageFatal(file, line, std::string(names));
70
+ }
71
+ return t;
72
+ }
73
+
74
+ template <typename T>
75
+ T* CheckNotNull(const char* file, int line, const char* names, T* t) {
76
+ return CheckNotNullCommon(file, line, names, t);
77
+ }
78
+
79
+ template <typename T>
80
+ T& CheckNotNull(const char* file, int line, const char* names, T& t) {
81
+ return CheckNotNullCommon(file, line, names, t);
82
+ }
83
+ } // namespace c10
84
+
85
+ // ---------------------- Logging Macro definitions --------------------------
86
+
87
+ static_assert(
88
+ CAFFE2_LOG_THRESHOLD <= ::c10::GLOG_FATAL,
89
+ "CAFFE2_LOG_THRESHOLD should at most be GLOG_FATAL.");
90
+ // If n is under the compile time caffe log threshold, The _CAFFE_LOG(n)
91
+ // should not generate anything in optimized code.
92
+ #define LOG(n) \
93
+ if (::c10::GLOG_##n >= CAFFE2_LOG_THRESHOLD) \
94
+ ::c10::MessageLogger(__FILE__, __LINE__, ::c10::GLOG_##n).stream()
95
+ #define VLOG(n) \
96
+ if (-n >= CAFFE2_LOG_THRESHOLD) \
97
+ ::c10::MessageLogger(__FILE__, __LINE__, -n).stream()
98
+
99
+ #define LOG_IF(n, condition) \
100
+ if (::c10::GLOG_##n >= CAFFE2_LOG_THRESHOLD && (condition)) \
101
+ ::c10::MessageLogger(__FILE__, __LINE__, ::c10::GLOG_##n).stream()
102
+ #define VLOG_IF(n, condition) \
103
+ if (-n >= CAFFE2_LOG_THRESHOLD && (condition)) \
104
+ ::c10::MessageLogger(__FILE__, __LINE__, -n).stream()
105
+
106
+ #define VLOG_IS_ON(verboselevel) (CAFFE2_LOG_THRESHOLD <= -(verboselevel))
107
+
108
+ // Log with source location information override (to be used in generic
109
+ // warning/error handlers implemented as functions, not macros)
110
+ #define LOG_AT_FILE_LINE(n, file, line) \
111
+ if (::c10::GLOG_##n >= CAFFE2_LOG_THRESHOLD) \
112
+ ::c10::MessageLogger(file, line, ::c10::GLOG_##n).stream()
113
+
114
+ // Log only if condition is met. Otherwise evaluates to void.
115
+ #define FATAL_IF(condition) \
116
+ condition ? (void)0 \
117
+ : ::c10::LoggerVoidify() & \
118
+ ::c10::MessageLogger(__FILE__, __LINE__, ::c10::GLOG_FATAL).stream()
119
+
120
+ // Check for a given boolean condition.
121
+ #define CHECK(condition) FATAL_IF(condition) << "Check failed: " #condition " "
122
+
123
+ #ifndef NDEBUG
124
+ // Debug only version of CHECK
125
+ #define DCHECK(condition) FATAL_IF(condition) << "Check failed: " #condition " "
126
+ #define DLOG(severity) LOG(severity)
127
+ #else // NDEBUG
128
+ // Optimized version - generates no code.
129
+ #define DCHECK(condition) \
130
+ while (false) \
131
+ CHECK(condition)
132
+
133
+ #define DLOG(n) \
134
+ true ? (void)0 \
135
+ : ::c10::LoggerVoidify() & \
136
+ ::c10::MessageLogger(__FILE__, __LINE__, ::c10::GLOG_##n).stream()
137
+ #endif // NDEBUG
138
+
139
+ #define TORCH_CHECK_OP(val1, val2, op) \
140
+ FATAL_IF(((val1)op(val2))) << "Check failed: " #val1 " " #op " " #val2 " (" \
141
+ << (val1) << " vs. " << (val2) << ") "
142
+
143
+ // TORCH_CHECK_OP macro definitions
144
+ #define TORCH_CHECK_EQ(val1, val2) TORCH_CHECK_OP(val1, val2, ==)
145
+ #define TORCH_CHECK_NE(val1, val2) TORCH_CHECK_OP(val1, val2, !=)
146
+ #define TORCH_CHECK_LE(val1, val2) TORCH_CHECK_OP(val1, val2, <=)
147
+ #define TORCH_CHECK_LT(val1, val2) TORCH_CHECK_OP(val1, val2, <)
148
+ #define TORCH_CHECK_GE(val1, val2) TORCH_CHECK_OP(val1, val2, >=)
149
+ #define TORCH_CHECK_GT(val1, val2) TORCH_CHECK_OP(val1, val2, >)
150
+
151
+ #ifndef NDEBUG
152
+ // Debug only versions of TORCH_CHECK_OP macros.
153
+ #define TORCH_DCHECK_EQ(val1, val2) TORCH_CHECK_OP(val1, val2, ==)
154
+ #define TORCH_DCHECK_NE(val1, val2) TORCH_CHECK_OP(val1, val2, !=)
155
+ #define TORCH_DCHECK_LE(val1, val2) TORCH_CHECK_OP(val1, val2, <=)
156
+ #define TORCH_DCHECK_LT(val1, val2) TORCH_CHECK_OP(val1, val2, <)
157
+ #define TORCH_DCHECK_GE(val1, val2) TORCH_CHECK_OP(val1, val2, >=)
158
+ #define TORCH_DCHECK_GT(val1, val2) TORCH_CHECK_OP(val1, val2, >)
159
+ #else // !NDEBUG
160
+ // These versions generate no code in optimized mode.
161
+ #define TORCH_DCHECK_EQ(val1, val2) \
162
+ while (false) \
163
+ TORCH_CHECK_OP(val1, val2, ==)
164
+ #define TORCH_DCHECK_NE(val1, val2) \
165
+ while (false) \
166
+ TORCH_CHECK_OP(val1, val2, !=)
167
+ #define TORCH_DCHECK_LE(val1, val2) \
168
+ while (false) \
169
+ TORCH_CHECK_OP(val1, val2, <=)
170
+ #define TORCH_DCHECK_LT(val1, val2) \
171
+ while (false) \
172
+ TORCH_CHECK_OP(val1, val2, <)
173
+ #define TORCH_DCHECK_GE(val1, val2) \
174
+ while (false) \
175
+ TORCH_CHECK_OP(val1, val2, >=)
176
+ #define TORCH_DCHECK_GT(val1, val2) \
177
+ while (false) \
178
+ TORCH_CHECK_OP(val1, val2, >)
179
+ #endif // NDEBUG
180
+
181
+ // Check that a pointer is not null.
182
+ #define TORCH_CHECK_NOTNULL(val) \
183
+ ::c10::CheckNotNull( \
184
+ __FILE__, __LINE__, "Check failed: '" #val "' Must be non NULL", (val))
185
+
186
+ #ifndef NDEBUG
187
+ // Debug only version of TORCH_CHECK_NOTNULL
188
+ #define TORCH_DCHECK_NOTNULL(val) \
189
+ ::c10::CheckNotNull( \
190
+ __FILE__, __LINE__, "Check failed: '" #val "' Must be non NULL", (val))
191
+ #else // !NDEBUG
192
+ // Optimized version - generates no code.
193
+ #define TORCH_DCHECK_NOTNULL(val) \
194
+ while (false) \
195
+ TORCH_CHECK_NOTNULL(val)
196
+ #endif // NDEBUG
197
+
198
+ // ---------------------- Support for std objects --------------------------
199
+ // These are adapted from glog to support a limited set of logging capability
200
+ // for STL objects.
201
+
202
+ namespace std {
203
+ // Forward declare these two, and define them after all the container streams
204
+ // operators so that we can recurse from pair -> container -> container -> pair
205
+ // properly.
206
+ template <class First, class Second>
207
+ std::ostream& operator<<(std::ostream& out, const std::pair<First, Second>& p);
208
+ } // namespace std
209
+
210
+ namespace c10 {
211
+ template <class Iter>
212
+ void PrintSequence(std::ostream& ss, Iter begin, Iter end);
213
+ } // namespace c10
214
+
215
+ namespace std {
216
+ #define INSTANTIATE_FOR_CONTAINER(container) \
217
+ template <class... Types> \
218
+ std::ostream& operator<<( \
219
+ std::ostream& out, const container<Types...>& seq) { \
220
+ c10::PrintSequence(out, seq.begin(), seq.end()); \
221
+ return out; \
222
+ }
223
+
224
+ INSTANTIATE_FOR_CONTAINER(std::vector)
225
+ INSTANTIATE_FOR_CONTAINER(std::map)
226
+ INSTANTIATE_FOR_CONTAINER(std::set)
227
+ #undef INSTANTIATE_FOR_CONTAINER
228
+
229
+ template <class First, class Second>
230
+ inline std::ostream& operator<<(
231
+ std::ostream& out,
232
+ const std::pair<First, Second>& p) {
233
+ out << '(' << p.first << ", " << p.second << ')';
234
+ return out;
235
+ }
236
+
237
+ inline std::ostream& operator<<(std::ostream& out, const std::nullptr_t&) {
238
+ out << "(null)";
239
+ return out;
240
+ }
241
+ } // namespace std
242
+
243
+ namespace c10 {
244
+ template <class Iter>
245
+ inline void PrintSequence(std::ostream& out, Iter begin, Iter end) {
246
+ // Output at most 100 elements -- appropriate if used for logging.
247
+ for (int i = 0; begin != end && i < 100; ++i, ++begin) {
248
+ if (i > 0)
249
+ out << ' ';
250
+ out << *begin;
251
+ }
252
+ if (begin != end) {
253
+ out << " ...";
254
+ }
255
+ }
256
+ } // namespace c10
257
+
258
+ #endif // C10_UTIL_LOGGING_IS_NOT_GOOGLE_GLOG_H_
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/numa.h ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Export.h>
4
+ #include <c10/util/Flags.h>
5
+ #include <cstddef>
6
+
7
+ C10_DECLARE_bool(caffe2_cpu_numa_enabled);
8
+
9
+ namespace c10 {
10
+
11
+ /**
12
+ * Check whether NUMA is enabled
13
+ */
14
+ C10_API bool IsNUMAEnabled();
15
+
16
+ /**
17
+ * Bind to a given NUMA node
18
+ */
19
+ C10_API void NUMABind(int numa_node_id);
20
+
21
+ /**
22
+ * Get the NUMA id for a given pointer `ptr`
23
+ */
24
+ C10_API int GetNUMANode(const void* ptr);
25
+
26
+ /**
27
+ * Get number of NUMA nodes
28
+ */
29
+ C10_API int GetNumNUMANodes();
30
+
31
+ /**
32
+ * Move the memory pointed to by `ptr` of a given size to another NUMA node
33
+ */
34
+ C10_API void NUMAMove(void* ptr, size_t size, int numa_node_id);
35
+
36
+ /**
37
+ * Get the current NUMA node id
38
+ */
39
+ C10_API int GetCurrentNUMANode();
40
+
41
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/order_preserving_flat_hash_map.h ADDED
@@ -0,0 +1,2217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Taken from
2
+ // https://github.com/skarupke/flat_hash_map/blob/2c4687431f978f02a3780e24b8b701d22aa32d9c/flat_hash_map.hpp
3
+ // with fixes applied:
4
+ // - https://github.com/skarupke/flat_hash_map/pull/25
5
+ // - https://github.com/skarupke/flat_hash_map/pull/26
6
+ // - replace size_t with uint64_t to fix it for 32bit
7
+ // - add "GCC diagnostic" pragma to ignore -Wshadow
8
+ // - make sherwood_v3_table::convertible_to_iterator public because GCC5 seems
9
+ // to have issues with it otherwise
10
+ // - fix compiler warnings in operator templated_iterator<const value_type>
11
+ // - make use of 'if constexpr' and eliminate AssignIfTrue template
12
+
13
+ // Copyright Malte Skarupke 2017.
14
+ // Distributed under the Boost Software License, Version 1.0.
15
+ // (See http://www.boost.org/LICENSE_1_0.txt)
16
+
17
+ // Modified to maintain insertion and deletion order through a doubly-linked
18
+ // list
19
+
20
+ #pragma once
21
+
22
+ #include <algorithm>
23
+ #include <array>
24
+ #include <cmath>
25
+ #include <cstddef>
26
+ #include <cstdint>
27
+ #include <functional>
28
+ #include <initializer_list>
29
+ #include <iterator>
30
+ #include <memory>
31
+ #include <stdexcept>
32
+ #include <type_traits>
33
+ #include <utility>
34
+
35
+ #ifdef _MSC_VER
36
+ #define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
37
+ #else
38
+ #define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
39
+ #endif
40
+
41
+ namespace ska_ordered {
42
+
43
+ struct prime_number_hash_policy;
44
+ struct power_of_two_hash_policy;
45
+ struct fibonacci_hash_policy;
46
+
47
+ namespace detailv3 {
48
+ template <typename Result, typename Functor>
49
+ struct functor_storage : Functor {
50
+ functor_storage() = default;
51
+ functor_storage(const Functor& functor) : Functor(functor) {}
52
+ template <typename... Args>
53
+ Result operator()(Args&&... args) {
54
+ return static_cast<Functor&>(*this)(std::forward<Args>(args)...);
55
+ }
56
+ template <typename... Args>
57
+ Result operator()(Args&&... args) const {
58
+ return static_cast<const Functor&>(*this)(std::forward<Args>(args)...);
59
+ }
60
+ };
61
+ template <typename Result, typename... Args>
62
+ struct functor_storage<Result, Result (*)(Args...)> {
63
+ typedef Result (*function_ptr)(Args...);
64
+ function_ptr function;
65
+ functor_storage(function_ptr function) : function(function) {}
66
+ Result operator()(Args... args) const {
67
+ return function(std::forward<Args>(args)...);
68
+ }
69
+ operator function_ptr&() {
70
+ return function;
71
+ }
72
+ operator const function_ptr&() {
73
+ return function;
74
+ }
75
+ };
76
+ template <typename key_type, typename value_type, typename hasher>
77
+ struct KeyOrValueHasher : functor_storage<uint64_t, hasher> {
78
+ typedef functor_storage<uint64_t, hasher> hasher_storage;
79
+ KeyOrValueHasher() = default;
80
+ KeyOrValueHasher(const hasher& hash) : hasher_storage(hash) {}
81
+ uint64_t operator()(const key_type& key) {
82
+ return static_cast<hasher_storage&>(*this)(key);
83
+ }
84
+ uint64_t operator()(const key_type& key) const {
85
+ return static_cast<const hasher_storage&>(*this)(key);
86
+ }
87
+ uint64_t operator()(const value_type& value) {
88
+ return static_cast<hasher_storage&>(*this)(value.first);
89
+ }
90
+ uint64_t operator()(const value_type& value) const {
91
+ return static_cast<const hasher_storage&>(*this)(value.first);
92
+ }
93
+ template <typename F, typename S>
94
+ uint64_t operator()(const std::pair<F, S>& value) {
95
+ return static_cast<hasher_storage&>(*this)(value.first);
96
+ }
97
+ template <typename F, typename S>
98
+ uint64_t operator()(const std::pair<F, S>& value) const {
99
+ return static_cast<const hasher_storage&>(*this)(value.first);
100
+ }
101
+ };
102
+ template <typename key_type, typename value_type, typename key_equal>
103
+ struct KeyOrValueEquality : functor_storage<bool, key_equal> {
104
+ typedef functor_storage<bool, key_equal> equality_storage;
105
+ KeyOrValueEquality() = default;
106
+ KeyOrValueEquality(const key_equal& equality) : equality_storage(equality) {}
107
+ bool operator()(const key_type& lhs, const key_type& rhs) {
108
+ return static_cast<equality_storage&>(*this)(lhs, rhs);
109
+ }
110
+ bool operator()(const key_type& lhs, const value_type& rhs) {
111
+ return static_cast<equality_storage&>(*this)(lhs, rhs.first);
112
+ }
113
+ bool operator()(const value_type& lhs, const key_type& rhs) {
114
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs);
115
+ }
116
+ bool operator()(const value_type& lhs, const value_type& rhs) {
117
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
118
+ }
119
+ template <typename F, typename S>
120
+ bool operator()(const key_type& lhs, const std::pair<F, S>& rhs) {
121
+ return static_cast<equality_storage&>(*this)(lhs, rhs.first);
122
+ }
123
+ template <typename F, typename S>
124
+ bool operator()(const std::pair<F, S>& lhs, const key_type& rhs) {
125
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs);
126
+ }
127
+ template <typename F, typename S>
128
+ bool operator()(const value_type& lhs, const std::pair<F, S>& rhs) {
129
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
130
+ }
131
+ template <typename F, typename S>
132
+ bool operator()(const std::pair<F, S>& lhs, const value_type& rhs) {
133
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
134
+ }
135
+ template <typename FL, typename SL, typename FR, typename SR>
136
+ bool operator()(const std::pair<FL, SL>& lhs, const std::pair<FR, SR>& rhs) {
137
+ return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
138
+ }
139
+ };
140
+ static constexpr int8_t min_lookups = 4;
141
+ template <typename T>
142
+ // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
143
+ struct sherwood_v3_entry {
144
+ // NOLINTNEXTLINE(modernize-use-equals-default)
145
+ sherwood_v3_entry() {}
146
+ sherwood_v3_entry(int8_t distance_from_desired)
147
+ : distance_from_desired(distance_from_desired) {}
148
+ // NOLINTNEXTLINE(modernize-use-equals-default)
149
+ ~sherwood_v3_entry() {}
150
+
151
+ bool has_value() const {
152
+ return distance_from_desired >= 0;
153
+ }
154
+ bool is_empty() const {
155
+ return distance_from_desired < 0;
156
+ }
157
+ bool is_at_desired_position() const {
158
+ return distance_from_desired <= 0;
159
+ }
160
+ template <typename... Args>
161
+ void emplace(int8_t distance, Args&&... args) {
162
+ new (std::addressof(value)) T(std::forward<Args>(args)...);
163
+ distance_from_desired = distance;
164
+ }
165
+
166
+ void destroy_value() {
167
+ value.~T();
168
+ distance_from_desired = -1;
169
+ }
170
+
171
+ sherwood_v3_entry<T>* prev = nullptr;
172
+ sherwood_v3_entry<T>* next = nullptr;
173
+ int8_t distance_from_desired = -1;
174
+ static constexpr int8_t special_end_value = 0;
175
+ union {
176
+ T value;
177
+ };
178
+ };
179
+
180
+ inline int8_t log2(uint64_t value) {
181
+ static constexpr std::array<int8_t, 64> table = {
182
+ 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3,
183
+ 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4,
184
+ 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
185
+ 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5};
186
+ value |= value >> 1;
187
+ value |= value >> 2;
188
+ value |= value >> 4;
189
+ value |= value >> 8;
190
+ value |= value >> 16;
191
+ value |= value >> 32;
192
+ return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
193
+ }
194
+
195
+ inline uint64_t next_power_of_two(uint64_t i) {
196
+ --i;
197
+ i |= i >> 1;
198
+ i |= i >> 2;
199
+ i |= i >> 4;
200
+ i |= i >> 8;
201
+ i |= i >> 16;
202
+ i |= i >> 32;
203
+ ++i;
204
+ return i;
205
+ }
206
+
207
+ // Implementation taken from http://en.cppreference.com/w/cpp/types/void_t
208
+ // (it takes CWG1558 into account and also works for older compilers)
209
+ template <typename... Ts>
210
+ struct make_void {
211
+ typedef void type;
212
+ };
213
+ template <typename... Ts>
214
+ using void_t = typename make_void<Ts...>::type;
215
+
216
+ template <typename T, typename = void>
217
+ struct HashPolicySelector {
218
+ typedef fibonacci_hash_policy type;
219
+ };
220
+ template <typename T>
221
+ struct HashPolicySelector<T, void_t<typename T::hash_policy>> {
222
+ typedef typename T::hash_policy type;
223
+ };
224
+
225
+ template <
226
+ typename T,
227
+ typename FindKey,
228
+ typename ArgumentHash,
229
+ typename Hasher,
230
+ typename ArgumentEqual,
231
+ typename Equal,
232
+ typename ArgumentAlloc,
233
+ typename EntryAlloc>
234
+ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal {
235
+ using Entry = detailv3::sherwood_v3_entry<T>;
236
+ using AllocatorTraits = std::allocator_traits<EntryAlloc>;
237
+ using EntryPointer = typename AllocatorTraits::pointer;
238
+
239
+ public:
240
+ struct convertible_to_iterator;
241
+
242
+ using value_type = T;
243
+ using size_type = uint64_t;
244
+ using difference_type = std::ptrdiff_t;
245
+ using hasher = ArgumentHash;
246
+ using key_equal = ArgumentEqual;
247
+ using allocator_type = EntryAlloc;
248
+ using reference = value_type&;
249
+ using const_reference = const value_type&;
250
+ using pointer = value_type*;
251
+ using const_pointer = const value_type*;
252
+
253
+ sherwood_v3_table() = default;
254
+ explicit sherwood_v3_table(
255
+ size_type bucket_count,
256
+ const ArgumentHash& hash = ArgumentHash(),
257
+ const ArgumentEqual& equal = ArgumentEqual(),
258
+ const ArgumentAlloc& alloc = ArgumentAlloc())
259
+ : EntryAlloc(alloc), Hasher(hash), Equal(equal) {
260
+ rehash(bucket_count);
261
+ }
262
+ sherwood_v3_table(size_type bucket_count, const ArgumentAlloc& alloc)
263
+ : sherwood_v3_table(
264
+ bucket_count,
265
+ ArgumentHash(),
266
+ ArgumentEqual(),
267
+ alloc) {}
268
+ sherwood_v3_table(
269
+ size_type bucket_count,
270
+ const ArgumentHash& hash,
271
+ const ArgumentAlloc& alloc)
272
+ : sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc) {}
273
+ explicit sherwood_v3_table(const ArgumentAlloc& alloc) : EntryAlloc(alloc) {}
274
+ template <typename It>
275
+ sherwood_v3_table(
276
+ It first,
277
+ It last,
278
+ size_type bucket_count = 0,
279
+ const ArgumentHash& hash = ArgumentHash(),
280
+ const ArgumentEqual& equal = ArgumentEqual(),
281
+ const ArgumentAlloc& alloc = ArgumentAlloc())
282
+ : sherwood_v3_table(bucket_count, hash, equal, alloc) {
283
+ insert(first, last);
284
+ }
285
+ template <typename It>
286
+ sherwood_v3_table(
287
+ It first,
288
+ It last,
289
+ size_type bucket_count,
290
+ const ArgumentAlloc& alloc)
291
+ : sherwood_v3_table(
292
+ first,
293
+ last,
294
+ bucket_count,
295
+ ArgumentHash(),
296
+ ArgumentEqual(),
297
+ alloc) {}
298
+ template <typename It>
299
+ sherwood_v3_table(
300
+ It first,
301
+ It last,
302
+ size_type bucket_count,
303
+ const ArgumentHash& hash,
304
+ const ArgumentAlloc& alloc)
305
+ : sherwood_v3_table(
306
+ first,
307
+ last,
308
+ bucket_count,
309
+ hash,
310
+ ArgumentEqual(),
311
+ alloc) {}
312
+ sherwood_v3_table(
313
+ std::initializer_list<T> il,
314
+ size_type bucket_count = 0,
315
+ const ArgumentHash& hash = ArgumentHash(),
316
+ const ArgumentEqual& equal = ArgumentEqual(),
317
+ const ArgumentAlloc& alloc = ArgumentAlloc())
318
+ : sherwood_v3_table(bucket_count, hash, equal, alloc) {
319
+ if (bucket_count == 0)
320
+ rehash(il.size());
321
+ insert(il.begin(), il.end());
322
+ }
323
+ sherwood_v3_table(
324
+ std::initializer_list<T> il,
325
+ size_type bucket_count,
326
+ const ArgumentAlloc& alloc)
327
+ : sherwood_v3_table(
328
+ il,
329
+ bucket_count,
330
+ ArgumentHash(),
331
+ ArgumentEqual(),
332
+ alloc) {}
333
+ sherwood_v3_table(
334
+ std::initializer_list<T> il,
335
+ size_type bucket_count,
336
+ const ArgumentHash& hash,
337
+ const ArgumentAlloc& alloc)
338
+ : sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc) {}
339
+ sherwood_v3_table(const sherwood_v3_table& other)
340
+ : sherwood_v3_table(
341
+ other,
342
+ AllocatorTraits::select_on_container_copy_construction(
343
+ other.get_allocator())) {}
344
+ sherwood_v3_table(const sherwood_v3_table& other, const ArgumentAlloc& alloc)
345
+ : EntryAlloc(alloc),
346
+ Hasher(other),
347
+ Equal(other),
348
+ _max_load_factor(other._max_load_factor) {
349
+ rehash_for_other_container(other);
350
+ try {
351
+ insert(other.begin(), other.end());
352
+ } catch (...) {
353
+ clear();
354
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
355
+ throw;
356
+ }
357
+ }
358
+ sherwood_v3_table(sherwood_v3_table&& other) noexcept
359
+ : EntryAlloc(std::move(other)),
360
+ Hasher(std::move(other)),
361
+ Equal(std::move(other)) {
362
+ swap_pointers(other);
363
+ }
364
+ sherwood_v3_table(
365
+ sherwood_v3_table&& other,
366
+ const ArgumentAlloc& alloc) noexcept
367
+ : EntryAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other)) {
368
+ swap_pointers(other);
369
+ }
370
+ sherwood_v3_table& operator=(const sherwood_v3_table& other) {
371
+ if (this == std::addressof(other))
372
+ return *this;
373
+
374
+ clear();
375
+ if constexpr (AllocatorTraits::propagate_on_container_copy_assignment::
376
+ value) {
377
+ if (static_cast<EntryAlloc&>(*this) !=
378
+ static_cast<const EntryAlloc&>(other)) {
379
+ reset_to_empty_state();
380
+ }
381
+ static_cast<EntryAlloc&>(*this) = other;
382
+ }
383
+ _max_load_factor = other._max_load_factor;
384
+ static_cast<Hasher&>(*this) = other;
385
+ static_cast<Equal&>(*this) = other;
386
+ rehash_for_other_container(other);
387
+ insert(other.begin(), other.end());
388
+ return *this;
389
+ }
390
+ sherwood_v3_table& operator=(sherwood_v3_table&& other) noexcept {
391
+ if (this == std::addressof(other))
392
+ return *this;
393
+ else if constexpr (AllocatorTraits::propagate_on_container_move_assignment::
394
+ value) {
395
+ clear();
396
+ reset_to_empty_state();
397
+ static_cast<EntryAlloc&>(*this) = std::move(other);
398
+ swap_pointers(other);
399
+ } else if (
400
+ static_cast<EntryAlloc&>(*this) == static_cast<EntryAlloc&>(other)) {
401
+ swap_pointers(other);
402
+ } else {
403
+ clear();
404
+ _max_load_factor = other._max_load_factor;
405
+ rehash_for_other_container(other);
406
+ for (T& elem : other)
407
+ emplace(std::move(elem));
408
+ other.clear();
409
+ }
410
+ static_cast<Hasher&>(*this) = std::move(other);
411
+ static_cast<Equal&>(*this) = std::move(other);
412
+ return *this;
413
+ }
414
+ ~sherwood_v3_table() {
415
+ clear();
416
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
417
+ }
418
+
419
+ const allocator_type& get_allocator() const {
420
+ return static_cast<const allocator_type&>(*this);
421
+ }
422
+ const ArgumentEqual& key_eq() const {
423
+ return static_cast<const ArgumentEqual&>(*this);
424
+ }
425
+ const ArgumentHash& hash_function() const {
426
+ return static_cast<const ArgumentHash&>(*this);
427
+ }
428
+
429
+ template <typename ValueType>
430
+ struct templated_iterator {
431
+ templated_iterator() = default;
432
+ templated_iterator(EntryPointer current) : current(current) {}
433
+ EntryPointer current = EntryPointer();
434
+
435
+ using iterator_category = std::forward_iterator_tag;
436
+ using value_type = ValueType;
437
+ using difference_type = ptrdiff_t;
438
+ using pointer = ValueType*;
439
+ using reference = ValueType&;
440
+
441
+ friend bool operator==(
442
+ const templated_iterator& lhs,
443
+ const templated_iterator& rhs) {
444
+ return lhs.current == rhs.current;
445
+ }
446
+ friend bool operator!=(
447
+ const templated_iterator& lhs,
448
+ const templated_iterator& rhs) {
449
+ return !(lhs == rhs);
450
+ }
451
+
452
+ templated_iterator& operator++() {
453
+ current = current->next;
454
+ return *this;
455
+ }
456
+ templated_iterator operator++(int) {
457
+ templated_iterator copy(*this);
458
+ ++*this;
459
+ return copy;
460
+ }
461
+
462
+ ValueType& operator*() const {
463
+ return current->value;
464
+ }
465
+ ValueType* operator->() const {
466
+ return std::addressof(current->value);
467
+ }
468
+
469
+ // the template automatically disables the operator when value_type is
470
+ // already const, because that would cause a lot of compiler warnings
471
+ // otherwise.
472
+ template <
473
+ class target_type = const value_type,
474
+ class = std::enable_if_t<
475
+ std::is_same_v<target_type, const value_type> &&
476
+ !std::is_same_v<target_type, value_type>>>
477
+ operator templated_iterator<target_type>() const {
478
+ return {current};
479
+ }
480
+ };
481
+ using iterator = templated_iterator<value_type>;
482
+ using const_iterator = templated_iterator<const value_type>;
483
+
484
+ iterator begin() {
485
+ return sentinel->next;
486
+ }
487
+ const_iterator begin() const {
488
+ return sentinel->next;
489
+ }
490
+ const_iterator cbegin() const {
491
+ return begin();
492
+ }
493
+ iterator end() {
494
+ return sentinel;
495
+ }
496
+ const_iterator end() const {
497
+ return sentinel;
498
+ }
499
+ const_iterator cend() const {
500
+ return end();
501
+ }
502
+
503
+ iterator find(const FindKey& key) {
504
+ uint64_t index =
505
+ hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
506
+ EntryPointer it = entries + ptrdiff_t(index);
507
+ for (int8_t distance = 0; it->distance_from_desired >= distance;
508
+ ++distance, ++it) {
509
+ if (compares_equal(key, it->value))
510
+ return {it};
511
+ }
512
+ return end();
513
+ }
514
+ const_iterator find(const FindKey& key) const {
515
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
516
+ return const_cast<sherwood_v3_table*>(this)->find(key);
517
+ }
518
+ uint64_t count(const FindKey& key) const {
519
+ return find(key) == end() ? 0 : 1;
520
+ }
521
+ std::pair<iterator, iterator> equal_range(const FindKey& key) {
522
+ iterator found = find(key);
523
+ if (found == end())
524
+ return {found, found};
525
+ else
526
+ return {found, std::next(found)};
527
+ }
528
+ std::pair<const_iterator, const_iterator> equal_range(
529
+ const FindKey& key) const {
530
+ const_iterator found = find(key);
531
+ if (found == end())
532
+ return {found, found};
533
+ else
534
+ return {found, std::next(found)};
535
+ }
536
+
537
+ template <typename Key, typename... Args>
538
+ std::pair<iterator, bool> emplace(Key&& key, Args&&... args) {
539
+ uint64_t index =
540
+ hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
541
+ EntryPointer current_entry = entries + ptrdiff_t(index);
542
+ int8_t distance_from_desired = 0;
543
+ for (; current_entry->distance_from_desired >= distance_from_desired;
544
+ ++current_entry, ++distance_from_desired) {
545
+ // insertion of an existing key does not change ordering
546
+ if (compares_equal(key, current_entry->value))
547
+ return {{current_entry}, false};
548
+ }
549
+ return emplace_new_key(
550
+ distance_from_desired,
551
+ current_entry,
552
+ std::forward<Key>(key),
553
+ std::forward<Args>(args)...);
554
+ }
555
+
556
+ std::pair<iterator, bool> insert(const value_type& value) {
557
+ return emplace(value);
558
+ }
559
+ std::pair<iterator, bool> insert(value_type&& value) {
560
+ return emplace(std::move(value));
561
+ }
562
+ template <typename... Args>
563
+ iterator emplace_hint(const_iterator, Args&&... args) {
564
+ return emplace(std::forward<Args>(args)...).first;
565
+ }
566
+ iterator insert(const_iterator, const value_type& value) {
567
+ return emplace(value).first;
568
+ }
569
+ iterator insert(const_iterator, value_type&& value) {
570
+ return emplace(std::move(value)).first;
571
+ }
572
+
573
+ template <typename It>
574
+ void insert(It begin, It end) {
575
+ for (; begin != end; ++begin) {
576
+ emplace(*begin);
577
+ }
578
+ }
579
+ void insert(std::initializer_list<value_type> il) {
580
+ insert(il.begin(), il.end());
581
+ }
582
+
583
+ void rehash(uint64_t num_buckets) {
584
+ num_buckets = std::max(
585
+ num_buckets,
586
+ static_cast<uint64_t>(std::ceil(
587
+ static_cast<double>(num_elements) /
588
+ static_cast<double>(_max_load_factor))));
589
+ if (num_buckets == 0) {
590
+ reset_to_empty_state();
591
+ return;
592
+ }
593
+ auto new_prime_index = hash_policy.next_size_over(num_buckets);
594
+ if (num_buckets == bucket_count())
595
+ return;
596
+ int8_t new_max_lookups = compute_max_lookups(num_buckets);
597
+ EntryPointer new_buckets(
598
+ AllocatorTraits::allocate(*this, num_buckets + new_max_lookups));
599
+ EntryPointer special_end_item =
600
+ new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1);
601
+ for (EntryPointer it = new_buckets; it != special_end_item; ++it)
602
+ it->distance_from_desired = -1;
603
+ special_end_item->distance_from_desired = Entry::special_end_value;
604
+ std::swap(entries, new_buckets);
605
+ std::swap(num_slots_minus_one, num_buckets);
606
+ --num_slots_minus_one;
607
+ hash_policy.commit(new_prime_index);
608
+ int8_t old_max_lookups = max_lookups;
609
+ max_lookups = new_max_lookups;
610
+ num_elements = 0;
611
+
612
+ auto start = sentinel->next;
613
+ // point sentinel to itself;
614
+ reset_list();
615
+ // reinsert list
616
+ for (EntryPointer it = start; it != sentinel;) {
617
+ auto next = it->next;
618
+ emplace(std::move(it->value));
619
+ it->destroy_value();
620
+ it = next;
621
+ }
622
+
623
+ deallocate_data(new_buckets, num_buckets, old_max_lookups);
624
+ }
625
+
626
+ void reserve(uint64_t num_elements_) {
627
+ uint64_t required_buckets = num_buckets_for_reserve(num_elements_);
628
+ if (required_buckets > bucket_count())
629
+ rehash(required_buckets);
630
+ }
631
+
632
+ void replace_linked_list_position(
633
+ EntryPointer to_be_replaced,
634
+ EntryPointer new_node) {
635
+ remove_from_list(new_node);
636
+ insert_after(new_node, to_be_replaced->prev);
637
+ remove_from_list(to_be_replaced);
638
+ }
639
+
640
+ // the return value is a type that can be converted to an iterator
641
+ // the reason for doing this is that it's not free to find the
642
+ // iterator pointing at the next element. if you care about the
643
+ // next iterator, turn the return value into an iterator
644
+ convertible_to_iterator erase(const_iterator to_erase) {
645
+ EntryPointer current = to_erase.current;
646
+ remove_from_list(current);
647
+ current->destroy_value();
648
+ --num_elements;
649
+
650
+ for (EntryPointer next = current + ptrdiff_t(1);
651
+ !next->is_at_desired_position();
652
+ ++current, ++next) {
653
+ // if an entry is being removed, and there are other entries with the
654
+ // same hash, the other entries get moved to their desired position by
655
+ // reinserting.
656
+ current->emplace(next->distance_from_desired - 1, std::move(next->value));
657
+ replace_linked_list_position(next, current);
658
+ next->destroy_value();
659
+ }
660
+ return {to_erase.current};
661
+ }
662
+
663
+ iterator erase(const_iterator begin_it, const_iterator end_it) {
664
+ // whenever an entry is removed and there are other entries with the same
665
+ // hash, the other entries must get moved to their desired position.
666
+ // any reference to a moved entry is invalidated.
667
+ // here, we iterate through the range, and make sure that we update
668
+ // the pointer to our next entry in the list or the end of the iterator
669
+ // when it is invalidated.
670
+
671
+ auto curr_iter = begin_it.current;
672
+ auto next_iter = curr_iter->next;
673
+ auto end_iter = end_it.current;
674
+
675
+ while (curr_iter != end_iter) {
676
+ remove_from_list(curr_iter);
677
+ curr_iter->destroy_value();
678
+ --num_elements;
679
+
680
+ for (EntryPointer next_hash_slot = curr_iter + ptrdiff_t(1);
681
+ !next_hash_slot->is_at_desired_position();
682
+ ++curr_iter, ++next_hash_slot) {
683
+ curr_iter->emplace(
684
+ next_hash_slot->distance_from_desired - 1,
685
+ std::move(next_hash_slot->value));
686
+ replace_linked_list_position(next_hash_slot, curr_iter);
687
+ next_hash_slot->destroy_value();
688
+
689
+ // we are invalidating next_iter or end_iter
690
+ if (next_hash_slot == end_iter) {
691
+ end_iter = curr_iter;
692
+ } else if (next_hash_slot == next_iter) {
693
+ next_iter = curr_iter;
694
+ }
695
+ }
696
+ curr_iter = next_iter;
697
+ next_iter = curr_iter->next;
698
+ }
699
+
700
+ return {end_iter};
701
+ }
702
+
703
+ uint64_t erase(const FindKey& key) {
704
+ auto found = find(key);
705
+ if (found == end())
706
+ return 0;
707
+ else {
708
+ erase(found);
709
+ return 1;
710
+ }
711
+ }
712
+
713
+ void clear() {
714
+ for (EntryPointer it = entries,
715
+ end = it +
716
+ static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups);
717
+ it != end;
718
+ ++it) {
719
+ if (it->has_value())
720
+ it->destroy_value();
721
+ }
722
+ reset_list();
723
+ num_elements = 0;
724
+ }
725
+
726
+ void shrink_to_fit() {
727
+ rehash_for_other_container(*this);
728
+ }
729
+
730
+ void swap(sherwood_v3_table& other) noexcept {
731
+ using std::swap;
732
+ swap_pointers(other);
733
+ swap(static_cast<ArgumentHash&>(*this), static_cast<ArgumentHash&>(other));
734
+ swap(
735
+ static_cast<ArgumentEqual&>(*this), static_cast<ArgumentEqual&>(other));
736
+ if (AllocatorTraits::propagate_on_container_swap::value)
737
+ swap(static_cast<EntryAlloc&>(*this), static_cast<EntryAlloc&>(other));
738
+ }
739
+
740
+ uint64_t size() const {
741
+ return num_elements;
742
+ }
743
+ uint64_t max_size() const {
744
+ return (AllocatorTraits::max_size(*this)) / sizeof(Entry);
745
+ }
746
+ uint64_t bucket_count() const {
747
+ return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
748
+ }
749
+ size_type max_bucket_count() const {
750
+ return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry);
751
+ }
752
+ uint64_t bucket(const FindKey& key) const {
753
+ return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
754
+ }
755
+ float load_factor() const {
756
+ uint64_t buckets = bucket_count();
757
+ if (buckets)
758
+ return static_cast<float>(num_elements) / bucket_count();
759
+ else
760
+ return 0;
761
+ }
762
+ void max_load_factor(float value) {
763
+ _max_load_factor = value;
764
+ }
765
+ float max_load_factor() const {
766
+ return _max_load_factor;
767
+ }
768
+
769
+ bool empty() const {
770
+ return num_elements == 0;
771
+ }
772
+
773
+ private:
774
+ EntryPointer entries = empty_default_table();
775
+ uint64_t num_slots_minus_one = 0;
776
+ typename HashPolicySelector<ArgumentHash>::type hash_policy;
777
+ int8_t max_lookups = detailv3::min_lookups - 1;
778
+ float _max_load_factor = 0.5f;
779
+ uint64_t num_elements = 0;
780
+ std::unique_ptr<sherwood_v3_entry<T>> sentinel_val;
781
+
782
+ // head of doubly linked list
783
+ EntryPointer sentinel = initSentinel();
784
+
785
+ EntryPointer initSentinel() {
786
+ // needs to be a pointer so that hash map can be used with forward declared
787
+ // types
788
+ sentinel_val = std::make_unique<sherwood_v3_entry<T>>();
789
+ sentinel = sentinel_val.get();
790
+ reset_list();
791
+ return sentinel;
792
+ }
793
+
794
+ EntryPointer empty_default_table() {
795
+ EntryPointer result =
796
+ AllocatorTraits::allocate(*this, detailv3::min_lookups);
797
+ EntryPointer special_end_item =
798
+ result + static_cast<ptrdiff_t>(detailv3::min_lookups - 1);
799
+ for (EntryPointer it = result; it != special_end_item; ++it)
800
+ it->distance_from_desired = -1;
801
+ special_end_item->distance_from_desired = Entry::special_end_value;
802
+ return result;
803
+ }
804
+
805
+ static int8_t compute_max_lookups(uint64_t num_buckets) {
806
+ int8_t desired = detailv3::log2(num_buckets);
807
+ return std::max(detailv3::min_lookups, desired);
808
+ }
809
+
810
+ uint64_t num_buckets_for_reserve(uint64_t num_elements_) const {
811
+ return static_cast<uint64_t>(std::ceil(
812
+ static_cast<double>(num_elements_) /
813
+ std::min(0.5, static_cast<double>(_max_load_factor))));
814
+ }
815
+ void rehash_for_other_container(const sherwood_v3_table& other) {
816
+ rehash(
817
+ std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
818
+ }
819
+
820
+ void swap_pointers(sherwood_v3_table& other) {
821
+ using std::swap;
822
+ swap(hash_policy, other.hash_policy);
823
+ swap(entries, other.entries);
824
+ swap(num_slots_minus_one, other.num_slots_minus_one);
825
+ swap(num_elements, other.num_elements);
826
+ swap(max_lookups, other.max_lookups);
827
+ swap(_max_load_factor, other._max_load_factor);
828
+ swap(sentinel, other.sentinel);
829
+ swap(sentinel_val, other.sentinel_val);
830
+ }
831
+
832
+ void reset_list() {
833
+ sentinel->next = sentinel;
834
+ sentinel->prev = sentinel;
835
+ }
836
+
837
+ void remove_from_list(EntryPointer elem) {
838
+ elem->prev->next = elem->next;
839
+ elem->next->prev = elem->prev;
840
+ }
841
+
842
+ void insert_after(EntryPointer new_elem, EntryPointer prev) {
843
+ auto next = prev->next;
844
+
845
+ prev->next = new_elem;
846
+ new_elem->prev = prev;
847
+
848
+ new_elem->next = next;
849
+ next->prev = new_elem;
850
+ }
851
+
852
+ void swap_adjacent_nodes(EntryPointer before, EntryPointer after) {
853
+ // sentinel stays constant, so before->prev cannot equal after
854
+ auto before_prev = before->prev;
855
+ auto after_next = after->next;
856
+
857
+ before_prev->next = after;
858
+ after->prev = before_prev;
859
+
860
+ after_next->prev = before;
861
+ before->next = after_next;
862
+
863
+ before->prev = after;
864
+ after->next = before;
865
+ }
866
+
867
+ void swap_positions(EntryPointer p1, EntryPointer p2) {
868
+ if (p1 == p2) {
869
+ return;
870
+ }
871
+ if (p1->next == p2) {
872
+ return swap_adjacent_nodes(p1, p2);
873
+ } else if (p2->next == p1) {
874
+ return swap_adjacent_nodes(p2, p1);
875
+ }
876
+
877
+ auto p1_prev = p1->prev;
878
+ auto p1_next = p1->next;
879
+
880
+ auto p2_prev = p2->prev;
881
+ auto p2_next = p2->next;
882
+
883
+ p1_prev->next = p2;
884
+ p2->prev = p1_prev;
885
+
886
+ p1_next->prev = p2;
887
+ p2->next = p1_next;
888
+
889
+ p2_prev->next = p1;
890
+ p1->prev = p2_prev;
891
+
892
+ p2_next->prev = p1;
893
+ p1->next = p2_next;
894
+ }
895
+
896
+ void append_to_list(EntryPointer new_tail) {
897
+ insert_after(new_tail, sentinel->prev);
898
+ }
899
+
900
+ template <typename Key, typename... Args>
901
+ SKA_NOINLINE(std::pair<iterator, bool>)
902
+ emplace_new_key(
903
+ int8_t distance_from_desired,
904
+ EntryPointer current_entry,
905
+ Key&& key,
906
+ Args&&... args) {
907
+ using std::swap;
908
+ if (num_slots_minus_one == 0 || distance_from_desired == max_lookups ||
909
+ static_cast<double>(num_elements + 1) >
910
+ static_cast<double>(num_slots_minus_one + 1) *
911
+ static_cast<double>(_max_load_factor)) {
912
+ grow();
913
+ return emplace(std::forward<Key>(key), std::forward<Args>(args)...);
914
+ } else if (current_entry->is_empty()) {
915
+ current_entry->emplace(
916
+ distance_from_desired,
917
+ std::forward<Key>(key),
918
+ std::forward<Args>(args)...);
919
+ ++num_elements;
920
+ append_to_list(current_entry);
921
+ return {{current_entry}, true};
922
+ }
923
+ value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...);
924
+ swap(distance_from_desired, current_entry->distance_from_desired);
925
+ // We maintain the invariant that:
926
+ // - result.current_entry contains the new value we're inserting
927
+ // and is in the LinkedList position of to_insert
928
+ // - to_insert contains the value that represents the position of
929
+ // result.current_entry
930
+ swap(to_insert, current_entry->value);
931
+ iterator result = {current_entry};
932
+ for (++distance_from_desired, ++current_entry;; ++current_entry) {
933
+ if (current_entry->is_empty()) {
934
+ current_entry->emplace(distance_from_desired, std::move(to_insert));
935
+ append_to_list(current_entry);
936
+ // now we can swap back the displaced value to its correct position,
937
+ // putting the new value we're inserting to the front of the list
938
+ swap_positions(current_entry, result.current);
939
+ ++num_elements;
940
+ return {result, true};
941
+ } else if (current_entry->distance_from_desired < distance_from_desired) {
942
+ swap(distance_from_desired, current_entry->distance_from_desired);
943
+ swap(to_insert, current_entry->value);
944
+ // to maintain our invariants we need to swap positions
945
+ // of result.current & current_entry:
946
+ swap_positions(result.current, current_entry);
947
+ ++distance_from_desired;
948
+ } else {
949
+ ++distance_from_desired;
950
+ if (distance_from_desired == max_lookups) {
951
+ // the displaced element gets put back into its correct position
952
+ // we grow the hash table, and then try again to reinsert the new
953
+ // element
954
+ swap(to_insert, result.current->value);
955
+ grow();
956
+ return emplace(std::move(to_insert));
957
+ }
958
+ }
959
+ }
960
+ }
961
+
962
+ void grow() {
963
+ rehash(std::max(uint64_t(4), 2 * bucket_count()));
964
+ }
965
+
966
+ void deallocate_data(
967
+ EntryPointer begin,
968
+ uint64_t num_slots_minus_one_,
969
+ int8_t max_lookups_) {
970
+ AllocatorTraits::deallocate(
971
+ *this, begin, num_slots_minus_one_ + max_lookups_ + 1);
972
+ }
973
+
974
+ void reset_to_empty_state() {
975
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
976
+ entries = empty_default_table();
977
+ num_slots_minus_one = 0;
978
+ hash_policy.reset();
979
+ max_lookups = detailv3::min_lookups - 1;
980
+ }
981
+
982
+ template <typename U>
983
+ uint64_t hash_object(const U& key) {
984
+ return static_cast<Hasher&>(*this)(key);
985
+ }
986
+ template <typename U>
987
+ uint64_t hash_object(const U& key) const {
988
+ return static_cast<const Hasher&>(*this)(key);
989
+ }
990
+ template <typename L, typename R>
991
+ bool compares_equal(const L& lhs, const R& rhs) {
992
+ return static_cast<Equal&>(*this)(lhs, rhs);
993
+ }
994
+
995
+ public:
996
+ struct convertible_to_iterator {
997
+ EntryPointer it;
998
+
999
+ operator iterator() {
1000
+ if (it->has_value())
1001
+ return {it};
1002
+ else
1003
+ return ++iterator{it};
1004
+ }
1005
+ operator const_iterator() {
1006
+ if (it->has_value())
1007
+ return {it};
1008
+ else
1009
+ return ++const_iterator{it};
1010
+ }
1011
+ };
1012
+ };
1013
+ } // namespace detailv3
1014
+
1015
+ struct prime_number_hash_policy {
1016
+ static uint64_t mod0(uint64_t) {
1017
+ return 0llu;
1018
+ }
1019
+ static uint64_t mod2(uint64_t hash) {
1020
+ return hash % 2llu;
1021
+ }
1022
+ static uint64_t mod3(uint64_t hash) {
1023
+ return hash % 3llu;
1024
+ }
1025
+ static uint64_t mod5(uint64_t hash) {
1026
+ return hash % 5llu;
1027
+ }
1028
+ static uint64_t mod7(uint64_t hash) {
1029
+ return hash % 7llu;
1030
+ }
1031
+ static uint64_t mod11(uint64_t hash) {
1032
+ return hash % 11llu;
1033
+ }
1034
+ static uint64_t mod13(uint64_t hash) {
1035
+ return hash % 13llu;
1036
+ }
1037
+ static uint64_t mod17(uint64_t hash) {
1038
+ return hash % 17llu;
1039
+ }
1040
+ static uint64_t mod23(uint64_t hash) {
1041
+ return hash % 23llu;
1042
+ }
1043
+ static uint64_t mod29(uint64_t hash) {
1044
+ return hash % 29llu;
1045
+ }
1046
+ static uint64_t mod37(uint64_t hash) {
1047
+ return hash % 37llu;
1048
+ }
1049
+ static uint64_t mod47(uint64_t hash) {
1050
+ return hash % 47llu;
1051
+ }
1052
+ static uint64_t mod59(uint64_t hash) {
1053
+ return hash % 59llu;
1054
+ }
1055
+ static uint64_t mod73(uint64_t hash) {
1056
+ return hash % 73llu;
1057
+ }
1058
+ static uint64_t mod97(uint64_t hash) {
1059
+ return hash % 97llu;
1060
+ }
1061
+ static uint64_t mod127(uint64_t hash) {
1062
+ return hash % 127llu;
1063
+ }
1064
+ static uint64_t mod151(uint64_t hash) {
1065
+ return hash % 151llu;
1066
+ }
1067
+ static uint64_t mod197(uint64_t hash) {
1068
+ return hash % 197llu;
1069
+ }
1070
+ static uint64_t mod251(uint64_t hash) {
1071
+ return hash % 251llu;
1072
+ }
1073
+ static uint64_t mod313(uint64_t hash) {
1074
+ return hash % 313llu;
1075
+ }
1076
+ static uint64_t mod397(uint64_t hash) {
1077
+ return hash % 397llu;
1078
+ }
1079
+ static uint64_t mod499(uint64_t hash) {
1080
+ return hash % 499llu;
1081
+ }
1082
+ static uint64_t mod631(uint64_t hash) {
1083
+ return hash % 631llu;
1084
+ }
1085
+ static uint64_t mod797(uint64_t hash) {
1086
+ return hash % 797llu;
1087
+ }
1088
+ static uint64_t mod1009(uint64_t hash) {
1089
+ return hash % 1009llu;
1090
+ }
1091
+ static uint64_t mod1259(uint64_t hash) {
1092
+ return hash % 1259llu;
1093
+ }
1094
+ static uint64_t mod1597(uint64_t hash) {
1095
+ return hash % 1597llu;
1096
+ }
1097
+ static uint64_t mod2011(uint64_t hash) {
1098
+ return hash % 2011llu;
1099
+ }
1100
+ static uint64_t mod2539(uint64_t hash) {
1101
+ return hash % 2539llu;
1102
+ }
1103
+ static uint64_t mod3203(uint64_t hash) {
1104
+ return hash % 3203llu;
1105
+ }
1106
+ static uint64_t mod4027(uint64_t hash) {
1107
+ return hash % 4027llu;
1108
+ }
1109
+ static uint64_t mod5087(uint64_t hash) {
1110
+ return hash % 5087llu;
1111
+ }
1112
+ static uint64_t mod6421(uint64_t hash) {
1113
+ return hash % 6421llu;
1114
+ }
1115
+ static uint64_t mod8089(uint64_t hash) {
1116
+ return hash % 8089llu;
1117
+ }
1118
+ static uint64_t mod10193(uint64_t hash) {
1119
+ return hash % 10193llu;
1120
+ }
1121
+ static uint64_t mod12853(uint64_t hash) {
1122
+ return hash % 12853llu;
1123
+ }
1124
+ static uint64_t mod16193(uint64_t hash) {
1125
+ return hash % 16193llu;
1126
+ }
1127
+ static uint64_t mod20399(uint64_t hash) {
1128
+ return hash % 20399llu;
1129
+ }
1130
+ static uint64_t mod25717(uint64_t hash) {
1131
+ return hash % 25717llu;
1132
+ }
1133
+ static uint64_t mod32401(uint64_t hash) {
1134
+ return hash % 32401llu;
1135
+ }
1136
+ static uint64_t mod40823(uint64_t hash) {
1137
+ return hash % 40823llu;
1138
+ }
1139
+ static uint64_t mod51437(uint64_t hash) {
1140
+ return hash % 51437llu;
1141
+ }
1142
+ static uint64_t mod64811(uint64_t hash) {
1143
+ return hash % 64811llu;
1144
+ }
1145
+ static uint64_t mod81649(uint64_t hash) {
1146
+ return hash % 81649llu;
1147
+ }
1148
+ static uint64_t mod102877(uint64_t hash) {
1149
+ return hash % 102877llu;
1150
+ }
1151
+ static uint64_t mod129607(uint64_t hash) {
1152
+ return hash % 129607llu;
1153
+ }
1154
+ static uint64_t mod163307(uint64_t hash) {
1155
+ return hash % 163307llu;
1156
+ }
1157
+ static uint64_t mod205759(uint64_t hash) {
1158
+ return hash % 205759llu;
1159
+ }
1160
+ static uint64_t mod259229(uint64_t hash) {
1161
+ return hash % 259229llu;
1162
+ }
1163
+ static uint64_t mod326617(uint64_t hash) {
1164
+ return hash % 326617llu;
1165
+ }
1166
+ static uint64_t mod411527(uint64_t hash) {
1167
+ return hash % 411527llu;
1168
+ }
1169
+ static uint64_t mod518509(uint64_t hash) {
1170
+ return hash % 518509llu;
1171
+ }
1172
+ static uint64_t mod653267(uint64_t hash) {
1173
+ return hash % 653267llu;
1174
+ }
1175
+ static uint64_t mod823117(uint64_t hash) {
1176
+ return hash % 823117llu;
1177
+ }
1178
+ static uint64_t mod1037059(uint64_t hash) {
1179
+ return hash % 1037059llu;
1180
+ }
1181
+ static uint64_t mod1306601(uint64_t hash) {
1182
+ return hash % 1306601llu;
1183
+ }
1184
+ static uint64_t mod1646237(uint64_t hash) {
1185
+ return hash % 1646237llu;
1186
+ }
1187
+ static uint64_t mod2074129(uint64_t hash) {
1188
+ return hash % 2074129llu;
1189
+ }
1190
+ static uint64_t mod2613229(uint64_t hash) {
1191
+ return hash % 2613229llu;
1192
+ }
1193
+ static uint64_t mod3292489(uint64_t hash) {
1194
+ return hash % 3292489llu;
1195
+ }
1196
+ static uint64_t mod4148279(uint64_t hash) {
1197
+ return hash % 4148279llu;
1198
+ }
1199
+ static uint64_t mod5226491(uint64_t hash) {
1200
+ return hash % 5226491llu;
1201
+ }
1202
+ static uint64_t mod6584983(uint64_t hash) {
1203
+ return hash % 6584983llu;
1204
+ }
1205
+ static uint64_t mod8296553(uint64_t hash) {
1206
+ return hash % 8296553llu;
1207
+ }
1208
+ static uint64_t mod10453007(uint64_t hash) {
1209
+ return hash % 10453007llu;
1210
+ }
1211
+ static uint64_t mod13169977(uint64_t hash) {
1212
+ return hash % 13169977llu;
1213
+ }
1214
+ static uint64_t mod16593127(uint64_t hash) {
1215
+ return hash % 16593127llu;
1216
+ }
1217
+ static uint64_t mod20906033(uint64_t hash) {
1218
+ return hash % 20906033llu;
1219
+ }
1220
+ static uint64_t mod26339969(uint64_t hash) {
1221
+ return hash % 26339969llu;
1222
+ }
1223
+ static uint64_t mod33186281(uint64_t hash) {
1224
+ return hash % 33186281llu;
1225
+ }
1226
+ static uint64_t mod41812097(uint64_t hash) {
1227
+ return hash % 41812097llu;
1228
+ }
1229
+ static uint64_t mod52679969(uint64_t hash) {
1230
+ return hash % 52679969llu;
1231
+ }
1232
+ static uint64_t mod66372617(uint64_t hash) {
1233
+ return hash % 66372617llu;
1234
+ }
1235
+ static uint64_t mod83624237(uint64_t hash) {
1236
+ return hash % 83624237llu;
1237
+ }
1238
+ static uint64_t mod105359939(uint64_t hash) {
1239
+ return hash % 105359939llu;
1240
+ }
1241
+ static uint64_t mod132745199(uint64_t hash) {
1242
+ return hash % 132745199llu;
1243
+ }
1244
+ static uint64_t mod167248483(uint64_t hash) {
1245
+ return hash % 167248483llu;
1246
+ }
1247
+ static uint64_t mod210719881(uint64_t hash) {
1248
+ return hash % 210719881llu;
1249
+ }
1250
+ static uint64_t mod265490441(uint64_t hash) {
1251
+ return hash % 265490441llu;
1252
+ }
1253
+ static uint64_t mod334496971(uint64_t hash) {
1254
+ return hash % 334496971llu;
1255
+ }
1256
+ static uint64_t mod421439783(uint64_t hash) {
1257
+ return hash % 421439783llu;
1258
+ }
1259
+ static uint64_t mod530980861(uint64_t hash) {
1260
+ return hash % 530980861llu;
1261
+ }
1262
+ static uint64_t mod668993977(uint64_t hash) {
1263
+ return hash % 668993977llu;
1264
+ }
1265
+ static uint64_t mod842879579(uint64_t hash) {
1266
+ return hash % 842879579llu;
1267
+ }
1268
+ static uint64_t mod1061961721(uint64_t hash) {
1269
+ return hash % 1061961721llu;
1270
+ }
1271
+ static uint64_t mod1337987929(uint64_t hash) {
1272
+ return hash % 1337987929llu;
1273
+ }
1274
+ static uint64_t mod1685759167(uint64_t hash) {
1275
+ return hash % 1685759167llu;
1276
+ }
1277
+ static uint64_t mod2123923447(uint64_t hash) {
1278
+ return hash % 2123923447llu;
1279
+ }
1280
+ static uint64_t mod2675975881(uint64_t hash) {
1281
+ return hash % 2675975881llu;
1282
+ }
1283
+ static uint64_t mod3371518343(uint64_t hash) {
1284
+ return hash % 3371518343llu;
1285
+ }
1286
+ static uint64_t mod4247846927(uint64_t hash) {
1287
+ return hash % 4247846927llu;
1288
+ }
1289
+ static uint64_t mod5351951779(uint64_t hash) {
1290
+ return hash % 5351951779llu;
1291
+ }
1292
+ static uint64_t mod6743036717(uint64_t hash) {
1293
+ return hash % 6743036717llu;
1294
+ }
1295
+ static uint64_t mod8495693897(uint64_t hash) {
1296
+ return hash % 8495693897llu;
1297
+ }
1298
+ static uint64_t mod10703903591(uint64_t hash) {
1299
+ return hash % 10703903591llu;
1300
+ }
1301
+ static uint64_t mod13486073473(uint64_t hash) {
1302
+ return hash % 13486073473llu;
1303
+ }
1304
+ static uint64_t mod16991387857(uint64_t hash) {
1305
+ return hash % 16991387857llu;
1306
+ }
1307
+ static uint64_t mod21407807219(uint64_t hash) {
1308
+ return hash % 21407807219llu;
1309
+ }
1310
+ static uint64_t mod26972146961(uint64_t hash) {
1311
+ return hash % 26972146961llu;
1312
+ }
1313
+ static uint64_t mod33982775741(uint64_t hash) {
1314
+ return hash % 33982775741llu;
1315
+ }
1316
+ static uint64_t mod42815614441(uint64_t hash) {
1317
+ return hash % 42815614441llu;
1318
+ }
1319
+ static uint64_t mod53944293929(uint64_t hash) {
1320
+ return hash % 53944293929llu;
1321
+ }
1322
+ static uint64_t mod67965551447(uint64_t hash) {
1323
+ return hash % 67965551447llu;
1324
+ }
1325
+ static uint64_t mod85631228929(uint64_t hash) {
1326
+ return hash % 85631228929llu;
1327
+ }
1328
+ static uint64_t mod107888587883(uint64_t hash) {
1329
+ return hash % 107888587883llu;
1330
+ }
1331
+ static uint64_t mod135931102921(uint64_t hash) {
1332
+ return hash % 135931102921llu;
1333
+ }
1334
+ static uint64_t mod171262457903(uint64_t hash) {
1335
+ return hash % 171262457903llu;
1336
+ }
1337
+ static uint64_t mod215777175787(uint64_t hash) {
1338
+ return hash % 215777175787llu;
1339
+ }
1340
+ static uint64_t mod271862205833(uint64_t hash) {
1341
+ return hash % 271862205833llu;
1342
+ }
1343
+ static uint64_t mod342524915839(uint64_t hash) {
1344
+ return hash % 342524915839llu;
1345
+ }
1346
+ static uint64_t mod431554351609(uint64_t hash) {
1347
+ return hash % 431554351609llu;
1348
+ }
1349
+ static uint64_t mod543724411781(uint64_t hash) {
1350
+ return hash % 543724411781llu;
1351
+ }
1352
+ static uint64_t mod685049831731(uint64_t hash) {
1353
+ return hash % 685049831731llu;
1354
+ }
1355
+ static uint64_t mod863108703229(uint64_t hash) {
1356
+ return hash % 863108703229llu;
1357
+ }
1358
+ static uint64_t mod1087448823553(uint64_t hash) {
1359
+ return hash % 1087448823553llu;
1360
+ }
1361
+ static uint64_t mod1370099663459(uint64_t hash) {
1362
+ return hash % 1370099663459llu;
1363
+ }
1364
+ static uint64_t mod1726217406467(uint64_t hash) {
1365
+ return hash % 1726217406467llu;
1366
+ }
1367
+ static uint64_t mod2174897647073(uint64_t hash) {
1368
+ return hash % 2174897647073llu;
1369
+ }
1370
+ static uint64_t mod2740199326961(uint64_t hash) {
1371
+ return hash % 2740199326961llu;
1372
+ }
1373
+ static uint64_t mod3452434812973(uint64_t hash) {
1374
+ return hash % 3452434812973llu;
1375
+ }
1376
+ static uint64_t mod4349795294267(uint64_t hash) {
1377
+ return hash % 4349795294267llu;
1378
+ }
1379
+ static uint64_t mod5480398654009(uint64_t hash) {
1380
+ return hash % 5480398654009llu;
1381
+ }
1382
+ static uint64_t mod6904869625999(uint64_t hash) {
1383
+ return hash % 6904869625999llu;
1384
+ }
1385
+ static uint64_t mod8699590588571(uint64_t hash) {
1386
+ return hash % 8699590588571llu;
1387
+ }
1388
+ static uint64_t mod10960797308051(uint64_t hash) {
1389
+ return hash % 10960797308051llu;
1390
+ }
1391
+ static uint64_t mod13809739252051(uint64_t hash) {
1392
+ return hash % 13809739252051llu;
1393
+ }
1394
+ static uint64_t mod17399181177241(uint64_t hash) {
1395
+ return hash % 17399181177241llu;
1396
+ }
1397
+ static uint64_t mod21921594616111(uint64_t hash) {
1398
+ return hash % 21921594616111llu;
1399
+ }
1400
+ static uint64_t mod27619478504183(uint64_t hash) {
1401
+ return hash % 27619478504183llu;
1402
+ }
1403
+ static uint64_t mod34798362354533(uint64_t hash) {
1404
+ return hash % 34798362354533llu;
1405
+ }
1406
+ static uint64_t mod43843189232363(uint64_t hash) {
1407
+ return hash % 43843189232363llu;
1408
+ }
1409
+ static uint64_t mod55238957008387(uint64_t hash) {
1410
+ return hash % 55238957008387llu;
1411
+ }
1412
+ static uint64_t mod69596724709081(uint64_t hash) {
1413
+ return hash % 69596724709081llu;
1414
+ }
1415
+ static uint64_t mod87686378464759(uint64_t hash) {
1416
+ return hash % 87686378464759llu;
1417
+ }
1418
+ static uint64_t mod110477914016779(uint64_t hash) {
1419
+ return hash % 110477914016779llu;
1420
+ }
1421
+ static uint64_t mod139193449418173(uint64_t hash) {
1422
+ return hash % 139193449418173llu;
1423
+ }
1424
+ static uint64_t mod175372756929481(uint64_t hash) {
1425
+ return hash % 175372756929481llu;
1426
+ }
1427
+ static uint64_t mod220955828033581(uint64_t hash) {
1428
+ return hash % 220955828033581llu;
1429
+ }
1430
+ static uint64_t mod278386898836457(uint64_t hash) {
1431
+ return hash % 278386898836457llu;
1432
+ }
1433
+ static uint64_t mod350745513859007(uint64_t hash) {
1434
+ return hash % 350745513859007llu;
1435
+ }
1436
+ static uint64_t mod441911656067171(uint64_t hash) {
1437
+ return hash % 441911656067171llu;
1438
+ }
1439
+ static uint64_t mod556773797672909(uint64_t hash) {
1440
+ return hash % 556773797672909llu;
1441
+ }
1442
+ static uint64_t mod701491027718027(uint64_t hash) {
1443
+ return hash % 701491027718027llu;
1444
+ }
1445
+ static uint64_t mod883823312134381(uint64_t hash) {
1446
+ return hash % 883823312134381llu;
1447
+ }
1448
+ static uint64_t mod1113547595345903(uint64_t hash) {
1449
+ return hash % 1113547595345903llu;
1450
+ }
1451
+ static uint64_t mod1402982055436147(uint64_t hash) {
1452
+ return hash % 1402982055436147llu;
1453
+ }
1454
+ static uint64_t mod1767646624268779(uint64_t hash) {
1455
+ return hash % 1767646624268779llu;
1456
+ }
1457
+ static uint64_t mod2227095190691797(uint64_t hash) {
1458
+ return hash % 2227095190691797llu;
1459
+ }
1460
+ static uint64_t mod2805964110872297(uint64_t hash) {
1461
+ return hash % 2805964110872297llu;
1462
+ }
1463
+ static uint64_t mod3535293248537579(uint64_t hash) {
1464
+ return hash % 3535293248537579llu;
1465
+ }
1466
+ static uint64_t mod4454190381383713(uint64_t hash) {
1467
+ return hash % 4454190381383713llu;
1468
+ }
1469
+ static uint64_t mod5611928221744609(uint64_t hash) {
1470
+ return hash % 5611928221744609llu;
1471
+ }
1472
+ static uint64_t mod7070586497075177(uint64_t hash) {
1473
+ return hash % 7070586497075177llu;
1474
+ }
1475
+ static uint64_t mod8908380762767489(uint64_t hash) {
1476
+ return hash % 8908380762767489llu;
1477
+ }
1478
+ static uint64_t mod11223856443489329(uint64_t hash) {
1479
+ return hash % 11223856443489329llu;
1480
+ }
1481
+ static uint64_t mod14141172994150357(uint64_t hash) {
1482
+ return hash % 14141172994150357llu;
1483
+ }
1484
+ static uint64_t mod17816761525534927(uint64_t hash) {
1485
+ return hash % 17816761525534927llu;
1486
+ }
1487
+ static uint64_t mod22447712886978529(uint64_t hash) {
1488
+ return hash % 22447712886978529llu;
1489
+ }
1490
+ static uint64_t mod28282345988300791(uint64_t hash) {
1491
+ return hash % 28282345988300791llu;
1492
+ }
1493
+ static uint64_t mod35633523051069991(uint64_t hash) {
1494
+ return hash % 35633523051069991llu;
1495
+ }
1496
+ static uint64_t mod44895425773957261(uint64_t hash) {
1497
+ return hash % 44895425773957261llu;
1498
+ }
1499
+ static uint64_t mod56564691976601587(uint64_t hash) {
1500
+ return hash % 56564691976601587llu;
1501
+ }
1502
+ static uint64_t mod71267046102139967(uint64_t hash) {
1503
+ return hash % 71267046102139967llu;
1504
+ }
1505
+ static uint64_t mod89790851547914507(uint64_t hash) {
1506
+ return hash % 89790851547914507llu;
1507
+ }
1508
+ static uint64_t mod113129383953203213(uint64_t hash) {
1509
+ return hash % 113129383953203213llu;
1510
+ }
1511
+ static uint64_t mod142534092204280003(uint64_t hash) {
1512
+ return hash % 142534092204280003llu;
1513
+ }
1514
+ static uint64_t mod179581703095829107(uint64_t hash) {
1515
+ return hash % 179581703095829107llu;
1516
+ }
1517
+ static uint64_t mod226258767906406483(uint64_t hash) {
1518
+ return hash % 226258767906406483llu;
1519
+ }
1520
+ static uint64_t mod285068184408560057(uint64_t hash) {
1521
+ return hash % 285068184408560057llu;
1522
+ }
1523
+ static uint64_t mod359163406191658253(uint64_t hash) {
1524
+ return hash % 359163406191658253llu;
1525
+ }
1526
+ static uint64_t mod452517535812813007(uint64_t hash) {
1527
+ return hash % 452517535812813007llu;
1528
+ }
1529
+ static uint64_t mod570136368817120201(uint64_t hash) {
1530
+ return hash % 570136368817120201llu;
1531
+ }
1532
+ static uint64_t mod718326812383316683(uint64_t hash) {
1533
+ return hash % 718326812383316683llu;
1534
+ }
1535
+ static uint64_t mod905035071625626043(uint64_t hash) {
1536
+ return hash % 905035071625626043llu;
1537
+ }
1538
+ static uint64_t mod1140272737634240411(uint64_t hash) {
1539
+ return hash % 1140272737634240411llu;
1540
+ }
1541
+ static uint64_t mod1436653624766633509(uint64_t hash) {
1542
+ return hash % 1436653624766633509llu;
1543
+ }
1544
+ static uint64_t mod1810070143251252131(uint64_t hash) {
1545
+ return hash % 1810070143251252131llu;
1546
+ }
1547
+ static uint64_t mod2280545475268481167(uint64_t hash) {
1548
+ return hash % 2280545475268481167llu;
1549
+ }
1550
+ static uint64_t mod2873307249533267101(uint64_t hash) {
1551
+ return hash % 2873307249533267101llu;
1552
+ }
1553
+ static uint64_t mod3620140286502504283(uint64_t hash) {
1554
+ return hash % 3620140286502504283llu;
1555
+ }
1556
+ static uint64_t mod4561090950536962147(uint64_t hash) {
1557
+ return hash % 4561090950536962147llu;
1558
+ }
1559
+ static uint64_t mod5746614499066534157(uint64_t hash) {
1560
+ return hash % 5746614499066534157llu;
1561
+ }
1562
+ static uint64_t mod7240280573005008577(uint64_t hash) {
1563
+ return hash % 7240280573005008577llu;
1564
+ }
1565
+ static uint64_t mod9122181901073924329(uint64_t hash) {
1566
+ return hash % 9122181901073924329llu;
1567
+ }
1568
+ static uint64_t mod11493228998133068689(uint64_t hash) {
1569
+ return hash % 11493228998133068689llu;
1570
+ }
1571
+ static uint64_t mod14480561146010017169(uint64_t hash) {
1572
+ return hash % 14480561146010017169llu;
1573
+ }
1574
+ static uint64_t mod18446744073709551557(uint64_t hash) {
1575
+ return hash % 18446744073709551557llu;
1576
+ }
1577
+
1578
+ using mod_function = uint64_t (*)(uint64_t);
1579
+
1580
+ mod_function next_size_over(uint64_t& size) const {
1581
+ // prime numbers generated by the following method:
1582
+ // 1. start with a prime p = 2
1583
+ // 2. go to wolfram alpha and get p = NextPrime(2 * p)
1584
+ // 3. repeat 2. until you overflow 64 bits
1585
+ // you now have large gaps which you would hit if somebody called reserve()
1586
+ // with an unlucky number.
1587
+ // 4. to fill the gaps for every prime p go to wolfram alpha and get
1588
+ // ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in
1589
+ // the gaps
1590
+ // 5. get PrevPrime(2^64) and put it at the end
1591
+ // NOLINTNEXTLINE(*c-array*)
1592
+ static constexpr const uint64_t prime_list[] = {
1593
+ 2llu,
1594
+ 3llu,
1595
+ 5llu,
1596
+ 7llu,
1597
+ 11llu,
1598
+ 13llu,
1599
+ 17llu,
1600
+ 23llu,
1601
+ 29llu,
1602
+ 37llu,
1603
+ 47llu,
1604
+ 59llu,
1605
+ 73llu,
1606
+ 97llu,
1607
+ 127llu,
1608
+ 151llu,
1609
+ 197llu,
1610
+ 251llu,
1611
+ 313llu,
1612
+ 397llu,
1613
+ 499llu,
1614
+ 631llu,
1615
+ 797llu,
1616
+ 1009llu,
1617
+ 1259llu,
1618
+ 1597llu,
1619
+ 2011llu,
1620
+ 2539llu,
1621
+ 3203llu,
1622
+ 4027llu,
1623
+ 5087llu,
1624
+ 6421llu,
1625
+ 8089llu,
1626
+ 10193llu,
1627
+ 12853llu,
1628
+ 16193llu,
1629
+ 20399llu,
1630
+ 25717llu,
1631
+ 32401llu,
1632
+ 40823llu,
1633
+ 51437llu,
1634
+ 64811llu,
1635
+ 81649llu,
1636
+ 102877llu,
1637
+ 129607llu,
1638
+ 163307llu,
1639
+ 205759llu,
1640
+ 259229llu,
1641
+ 326617llu,
1642
+ 411527llu,
1643
+ 518509llu,
1644
+ 653267llu,
1645
+ 823117llu,
1646
+ 1037059llu,
1647
+ 1306601llu,
1648
+ 1646237llu,
1649
+ 2074129llu,
1650
+ 2613229llu,
1651
+ 3292489llu,
1652
+ 4148279llu,
1653
+ 5226491llu,
1654
+ 6584983llu,
1655
+ 8296553llu,
1656
+ 10453007llu,
1657
+ 13169977llu,
1658
+ 16593127llu,
1659
+ 20906033llu,
1660
+ 26339969llu,
1661
+ 33186281llu,
1662
+ 41812097llu,
1663
+ 52679969llu,
1664
+ 66372617llu,
1665
+ 83624237llu,
1666
+ 105359939llu,
1667
+ 132745199llu,
1668
+ 167248483llu,
1669
+ 210719881llu,
1670
+ 265490441llu,
1671
+ 334496971llu,
1672
+ 421439783llu,
1673
+ 530980861llu,
1674
+ 668993977llu,
1675
+ 842879579llu,
1676
+ 1061961721llu,
1677
+ 1337987929llu,
1678
+ 1685759167llu,
1679
+ 2123923447llu,
1680
+ 2675975881llu,
1681
+ 3371518343llu,
1682
+ 4247846927llu,
1683
+ 5351951779llu,
1684
+ 6743036717llu,
1685
+ 8495693897llu,
1686
+ 10703903591llu,
1687
+ 13486073473llu,
1688
+ 16991387857llu,
1689
+ 21407807219llu,
1690
+ 26972146961llu,
1691
+ 33982775741llu,
1692
+ 42815614441llu,
1693
+ 53944293929llu,
1694
+ 67965551447llu,
1695
+ 85631228929llu,
1696
+ 107888587883llu,
1697
+ 135931102921llu,
1698
+ 171262457903llu,
1699
+ 215777175787llu,
1700
+ 271862205833llu,
1701
+ 342524915839llu,
1702
+ 431554351609llu,
1703
+ 543724411781llu,
1704
+ 685049831731llu,
1705
+ 863108703229llu,
1706
+ 1087448823553llu,
1707
+ 1370099663459llu,
1708
+ 1726217406467llu,
1709
+ 2174897647073llu,
1710
+ 2740199326961llu,
1711
+ 3452434812973llu,
1712
+ 4349795294267llu,
1713
+ 5480398654009llu,
1714
+ 6904869625999llu,
1715
+ 8699590588571llu,
1716
+ 10960797308051llu,
1717
+ 13809739252051llu,
1718
+ 17399181177241llu,
1719
+ 21921594616111llu,
1720
+ 27619478504183llu,
1721
+ 34798362354533llu,
1722
+ 43843189232363llu,
1723
+ 55238957008387llu,
1724
+ 69596724709081llu,
1725
+ 87686378464759llu,
1726
+ 110477914016779llu,
1727
+ 139193449418173llu,
1728
+ 175372756929481llu,
1729
+ 220955828033581llu,
1730
+ 278386898836457llu,
1731
+ 350745513859007llu,
1732
+ 441911656067171llu,
1733
+ 556773797672909llu,
1734
+ 701491027718027llu,
1735
+ 883823312134381llu,
1736
+ 1113547595345903llu,
1737
+ 1402982055436147llu,
1738
+ 1767646624268779llu,
1739
+ 2227095190691797llu,
1740
+ 2805964110872297llu,
1741
+ 3535293248537579llu,
1742
+ 4454190381383713llu,
1743
+ 5611928221744609llu,
1744
+ 7070586497075177llu,
1745
+ 8908380762767489llu,
1746
+ 11223856443489329llu,
1747
+ 14141172994150357llu,
1748
+ 17816761525534927llu,
1749
+ 22447712886978529llu,
1750
+ 28282345988300791llu,
1751
+ 35633523051069991llu,
1752
+ 44895425773957261llu,
1753
+ 56564691976601587llu,
1754
+ 71267046102139967llu,
1755
+ 89790851547914507llu,
1756
+ 113129383953203213llu,
1757
+ 142534092204280003llu,
1758
+ 179581703095829107llu,
1759
+ 226258767906406483llu,
1760
+ 285068184408560057llu,
1761
+ 359163406191658253llu,
1762
+ 452517535812813007llu,
1763
+ 570136368817120201llu,
1764
+ 718326812383316683llu,
1765
+ 905035071625626043llu,
1766
+ 1140272737634240411llu,
1767
+ 1436653624766633509llu,
1768
+ 1810070143251252131llu,
1769
+ 2280545475268481167llu,
1770
+ 2873307249533267101llu,
1771
+ 3620140286502504283llu,
1772
+ 4561090950536962147llu,
1773
+ 5746614499066534157llu,
1774
+ 7240280573005008577llu,
1775
+ 9122181901073924329llu,
1776
+ 11493228998133068689llu,
1777
+ 14480561146010017169llu,
1778
+ 18446744073709551557llu};
1779
+ // NOLINTNEXTLINE(*c-array*)
1780
+ static constexpr uint64_t (*const mod_functions[])(uint64_t) = {
1781
+ &mod0,
1782
+ &mod2,
1783
+ &mod3,
1784
+ &mod5,
1785
+ &mod7,
1786
+ &mod11,
1787
+ &mod13,
1788
+ &mod17,
1789
+ &mod23,
1790
+ &mod29,
1791
+ &mod37,
1792
+ &mod47,
1793
+ &mod59,
1794
+ &mod73,
1795
+ &mod97,
1796
+ &mod127,
1797
+ &mod151,
1798
+ &mod197,
1799
+ &mod251,
1800
+ &mod313,
1801
+ &mod397,
1802
+ &mod499,
1803
+ &mod631,
1804
+ &mod797,
1805
+ &mod1009,
1806
+ &mod1259,
1807
+ &mod1597,
1808
+ &mod2011,
1809
+ &mod2539,
1810
+ &mod3203,
1811
+ &mod4027,
1812
+ &mod5087,
1813
+ &mod6421,
1814
+ &mod8089,
1815
+ &mod10193,
1816
+ &mod12853,
1817
+ &mod16193,
1818
+ &mod20399,
1819
+ &mod25717,
1820
+ &mod32401,
1821
+ &mod40823,
1822
+ &mod51437,
1823
+ &mod64811,
1824
+ &mod81649,
1825
+ &mod102877,
1826
+ &mod129607,
1827
+ &mod163307,
1828
+ &mod205759,
1829
+ &mod259229,
1830
+ &mod326617,
1831
+ &mod411527,
1832
+ &mod518509,
1833
+ &mod653267,
1834
+ &mod823117,
1835
+ &mod1037059,
1836
+ &mod1306601,
1837
+ &mod1646237,
1838
+ &mod2074129,
1839
+ &mod2613229,
1840
+ &mod3292489,
1841
+ &mod4148279,
1842
+ &mod5226491,
1843
+ &mod6584983,
1844
+ &mod8296553,
1845
+ &mod10453007,
1846
+ &mod13169977,
1847
+ &mod16593127,
1848
+ &mod20906033,
1849
+ &mod26339969,
1850
+ &mod33186281,
1851
+ &mod41812097,
1852
+ &mod52679969,
1853
+ &mod66372617,
1854
+ &mod83624237,
1855
+ &mod105359939,
1856
+ &mod132745199,
1857
+ &mod167248483,
1858
+ &mod210719881,
1859
+ &mod265490441,
1860
+ &mod334496971,
1861
+ &mod421439783,
1862
+ &mod530980861,
1863
+ &mod668993977,
1864
+ &mod842879579,
1865
+ &mod1061961721,
1866
+ &mod1337987929,
1867
+ &mod1685759167,
1868
+ &mod2123923447,
1869
+ &mod2675975881,
1870
+ &mod3371518343,
1871
+ &mod4247846927,
1872
+ &mod5351951779,
1873
+ &mod6743036717,
1874
+ &mod8495693897,
1875
+ &mod10703903591,
1876
+ &mod13486073473,
1877
+ &mod16991387857,
1878
+ &mod21407807219,
1879
+ &mod26972146961,
1880
+ &mod33982775741,
1881
+ &mod42815614441,
1882
+ &mod53944293929,
1883
+ &mod67965551447,
1884
+ &mod85631228929,
1885
+ &mod107888587883,
1886
+ &mod135931102921,
1887
+ &mod171262457903,
1888
+ &mod215777175787,
1889
+ &mod271862205833,
1890
+ &mod342524915839,
1891
+ &mod431554351609,
1892
+ &mod543724411781,
1893
+ &mod685049831731,
1894
+ &mod863108703229,
1895
+ &mod1087448823553,
1896
+ &mod1370099663459,
1897
+ &mod1726217406467,
1898
+ &mod2174897647073,
1899
+ &mod2740199326961,
1900
+ &mod3452434812973,
1901
+ &mod4349795294267,
1902
+ &mod5480398654009,
1903
+ &mod6904869625999,
1904
+ &mod8699590588571,
1905
+ &mod10960797308051,
1906
+ &mod13809739252051,
1907
+ &mod17399181177241,
1908
+ &mod21921594616111,
1909
+ &mod27619478504183,
1910
+ &mod34798362354533,
1911
+ &mod43843189232363,
1912
+ &mod55238957008387,
1913
+ &mod69596724709081,
1914
+ &mod87686378464759,
1915
+ &mod110477914016779,
1916
+ &mod139193449418173,
1917
+ &mod175372756929481,
1918
+ &mod220955828033581,
1919
+ &mod278386898836457,
1920
+ &mod350745513859007,
1921
+ &mod441911656067171,
1922
+ &mod556773797672909,
1923
+ &mod701491027718027,
1924
+ &mod883823312134381,
1925
+ &mod1113547595345903,
1926
+ &mod1402982055436147,
1927
+ &mod1767646624268779,
1928
+ &mod2227095190691797,
1929
+ &mod2805964110872297,
1930
+ &mod3535293248537579,
1931
+ &mod4454190381383713,
1932
+ &mod5611928221744609,
1933
+ &mod7070586497075177,
1934
+ &mod8908380762767489,
1935
+ &mod11223856443489329,
1936
+ &mod14141172994150357,
1937
+ &mod17816761525534927,
1938
+ &mod22447712886978529,
1939
+ &mod28282345988300791,
1940
+ &mod35633523051069991,
1941
+ &mod44895425773957261,
1942
+ &mod56564691976601587,
1943
+ &mod71267046102139967,
1944
+ &mod89790851547914507,
1945
+ &mod113129383953203213,
1946
+ &mod142534092204280003,
1947
+ &mod179581703095829107,
1948
+ &mod226258767906406483,
1949
+ &mod285068184408560057,
1950
+ &mod359163406191658253,
1951
+ &mod452517535812813007,
1952
+ &mod570136368817120201,
1953
+ &mod718326812383316683,
1954
+ &mod905035071625626043,
1955
+ &mod1140272737634240411,
1956
+ &mod1436653624766633509,
1957
+ &mod1810070143251252131,
1958
+ &mod2280545475268481167,
1959
+ &mod2873307249533267101,
1960
+ &mod3620140286502504283,
1961
+ &mod4561090950536962147,
1962
+ &mod5746614499066534157,
1963
+ &mod7240280573005008577,
1964
+ &mod9122181901073924329,
1965
+ &mod11493228998133068689,
1966
+ &mod14480561146010017169,
1967
+ &mod18446744073709551557};
1968
+ const uint64_t* found = std::lower_bound(
1969
+ std::begin(prime_list), std::end(prime_list) - 1, size);
1970
+ size = *found;
1971
+ return mod_functions[1 + found - prime_list];
1972
+ }
1973
+ void commit(mod_function new_mod_function) {
1974
+ current_mod_function = new_mod_function;
1975
+ }
1976
+ void reset() {
1977
+ current_mod_function = &mod0;
1978
+ }
1979
+
1980
+ uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
1981
+ const {
1982
+ return current_mod_function(hash);
1983
+ }
1984
+ uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
1985
+ return index > num_slots_minus_one ? current_mod_function(index) : index;
1986
+ }
1987
+
1988
+ private:
1989
+ mod_function current_mod_function = &mod0;
1990
+ };
1991
+
1992
+ struct power_of_two_hash_policy {
1993
+ uint64_t index_for_hash(uint64_t hash, uint64_t num_slots_minus_one) const {
1994
+ return hash & num_slots_minus_one;
1995
+ }
1996
+ uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
1997
+ return index_for_hash(index, num_slots_minus_one);
1998
+ }
1999
+ int8_t next_size_over(uint64_t& size) const {
2000
+ size = detailv3::next_power_of_two(size);
2001
+ return 0;
2002
+ }
2003
+ void commit(int8_t) {}
2004
+ void reset() {}
2005
+ };
2006
+
2007
+ struct fibonacci_hash_policy {
2008
+ uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
2009
+ const {
2010
+ return (11400714819323198485ull * hash) >> shift;
2011
+ }
2012
+ uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
2013
+ return index & num_slots_minus_one;
2014
+ }
2015
+
2016
+ int8_t next_size_over(uint64_t& size) const {
2017
+ size = std::max(uint64_t(2), detailv3::next_power_of_two(size));
2018
+ return static_cast<int8_t>(64 - detailv3::log2(size));
2019
+ }
2020
+ void commit(int8_t shift_) {
2021
+ shift = shift_;
2022
+ }
2023
+ void reset() {
2024
+ shift = 63;
2025
+ }
2026
+
2027
+ private:
2028
+ int8_t shift = 63;
2029
+ };
2030
+
2031
+ template <
2032
+ typename K,
2033
+ typename V,
2034
+ typename H = std::hash<K>,
2035
+ typename E = std::equal_to<K>,
2036
+ typename A = std::allocator<std::pair<K, V>>>
2037
+ class order_preserving_flat_hash_map
2038
+ : public detailv3::sherwood_v3_table<
2039
+ std::pair<K, V>,
2040
+ K,
2041
+ H,
2042
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
2043
+ E,
2044
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
2045
+ A,
2046
+ typename std::allocator_traits<A>::template rebind_alloc<
2047
+ detailv3::sherwood_v3_entry<std::pair<K, V>>>> {
2048
+ using Table = detailv3::sherwood_v3_table<
2049
+ std::pair<K, V>,
2050
+ K,
2051
+ H,
2052
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
2053
+ E,
2054
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
2055
+ A,
2056
+ typename std::allocator_traits<A>::template rebind_alloc<
2057
+ detailv3::sherwood_v3_entry<std::pair<K, V>>>>;
2058
+
2059
+ public:
2060
+ using key_type = K;
2061
+ using mapped_type = V;
2062
+
2063
+ using Table::Table;
2064
+ order_preserving_flat_hash_map() = default;
2065
+
2066
+ inline V& operator[](const K& key) {
2067
+ return emplace(key, convertible_to_value()).first->second;
2068
+ }
2069
+ inline V& operator[](K&& key) {
2070
+ return emplace(std::move(key), convertible_to_value()).first->second;
2071
+ }
2072
+ V& at(const K& key) {
2073
+ auto found = this->find(key);
2074
+ if (found == this->end())
2075
+ throw std::out_of_range("Argument passed to at() was not in the map.");
2076
+ return found->second;
2077
+ }
2078
+ const V& at(const K& key) const {
2079
+ auto found = this->find(key);
2080
+ if (found == this->end())
2081
+ throw std::out_of_range("Argument passed to at() was not in the map.");
2082
+ return found->second;
2083
+ }
2084
+
2085
+ using Table::emplace;
2086
+ std::pair<typename Table::iterator, bool> emplace() {
2087
+ return emplace(key_type(), convertible_to_value());
2088
+ }
2089
+ template <typename M>
2090
+ std::pair<typename Table::iterator, bool> insert_or_assign(
2091
+ const key_type& key,
2092
+ M&& m) {
2093
+ auto emplace_result = emplace(key, std::forward<M>(m));
2094
+ if (!emplace_result.second)
2095
+ emplace_result.first->second = std::forward<M>(m);
2096
+ return emplace_result;
2097
+ }
2098
+ template <typename M>
2099
+ std::pair<typename Table::iterator, bool> insert_or_assign(
2100
+ key_type&& key,
2101
+ M&& m) {
2102
+ auto emplace_result = emplace(std::move(key), std::forward<M>(m));
2103
+ if (!emplace_result.second)
2104
+ emplace_result.first->second = std::forward<M>(m);
2105
+ return emplace_result;
2106
+ }
2107
+ template <typename M>
2108
+ typename Table::iterator insert_or_assign(
2109
+ typename Table::const_iterator,
2110
+ const key_type& key,
2111
+ M&& m) {
2112
+ return insert_or_assign(key, std::forward<M>(m)).first;
2113
+ }
2114
+ template <typename M>
2115
+ typename Table::iterator insert_or_assign(
2116
+ typename Table::const_iterator,
2117
+ key_type&& key,
2118
+ M&& m) {
2119
+ return insert_or_assign(std::move(key), std::forward<M>(m)).first;
2120
+ }
2121
+
2122
+ friend bool operator==(
2123
+ const order_preserving_flat_hash_map& lhs,
2124
+ const order_preserving_flat_hash_map& rhs) {
2125
+ if (lhs.size() != rhs.size())
2126
+ return false;
2127
+ for (const typename Table::value_type& value : lhs) {
2128
+ auto found = rhs.find(value.first);
2129
+ if (found == rhs.end() || value.second != found->second)
2130
+ return false;
2131
+ }
2132
+ return true;
2133
+ }
2134
+ friend bool operator!=(
2135
+ const order_preserving_flat_hash_map& lhs,
2136
+ const order_preserving_flat_hash_map& rhs) {
2137
+ return !(lhs == rhs);
2138
+ }
2139
+
2140
+ private:
2141
+ struct convertible_to_value {
2142
+ operator V() const {
2143
+ return V();
2144
+ }
2145
+ };
2146
+ };
2147
+
2148
+ template <
2149
+ typename T,
2150
+ typename H = std::hash<T>,
2151
+ typename E = std::equal_to<T>,
2152
+ typename A = std::allocator<T>>
2153
+ class flat_hash_set
2154
+ : public detailv3::sherwood_v3_table<
2155
+ T,
2156
+ T,
2157
+ H,
2158
+ detailv3::functor_storage<uint64_t, H>,
2159
+ E,
2160
+ detailv3::functor_storage<bool, E>,
2161
+ A,
2162
+ typename std::allocator_traits<A>::template rebind_alloc<
2163
+ detailv3::sherwood_v3_entry<T>>> {
2164
+ using Table = detailv3::sherwood_v3_table<
2165
+ T,
2166
+ T,
2167
+ H,
2168
+ detailv3::functor_storage<uint64_t, H>,
2169
+ E,
2170
+ detailv3::functor_storage<bool, E>,
2171
+ A,
2172
+ typename std::allocator_traits<A>::template rebind_alloc<
2173
+ detailv3::sherwood_v3_entry<T>>>;
2174
+
2175
+ public:
2176
+ using key_type = T;
2177
+
2178
+ using Table::Table;
2179
+ flat_hash_set() = default;
2180
+
2181
+ template <typename... Args>
2182
+ std::pair<typename Table::iterator, bool> emplace(Args&&... args) {
2183
+ return Table::emplace(T(std::forward<Args>(args)...));
2184
+ }
2185
+ std::pair<typename Table::iterator, bool> emplace(const key_type& arg) {
2186
+ return Table::emplace(arg);
2187
+ }
2188
+ std::pair<typename Table::iterator, bool> emplace(key_type& arg) {
2189
+ return Table::emplace(arg);
2190
+ }
2191
+ std::pair<typename Table::iterator, bool> emplace(const key_type&& arg) {
2192
+ return Table::emplace(std::move(arg));
2193
+ }
2194
+ std::pair<typename Table::iterator, bool> emplace(key_type&& arg) {
2195
+ return Table::emplace(std::move(arg));
2196
+ }
2197
+
2198
+ friend bool operator==(const flat_hash_set& lhs, const flat_hash_set& rhs) {
2199
+ if (lhs.size() != rhs.size())
2200
+ return false;
2201
+ for (const T& value : lhs) {
2202
+ if (rhs.find(value) == rhs.end())
2203
+ return false;
2204
+ }
2205
+ return true;
2206
+ }
2207
+ friend bool operator!=(const flat_hash_set& lhs, const flat_hash_set& rhs) {
2208
+ return !(lhs == rhs);
2209
+ }
2210
+ };
2211
+
2212
+ template <typename T>
2213
+ struct power_of_two_std_hash : std::hash<T> {
2214
+ typedef ska_ordered::power_of_two_hash_policy hash_policy;
2215
+ };
2216
+
2217
+ } // namespace ska_ordered
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/overflows.h ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Macros.h>
4
+ #include <c10/util/TypeSafeSignMath.h>
5
+ #include <c10/util/complex.h>
6
+
7
+ #include <cmath>
8
+ #include <limits>
9
+ #include <type_traits>
10
+
11
+ namespace c10 {
12
+ // In some versions of MSVC, there will be a compiler error when building.
13
+ // C4146: unary minus operator applied to unsigned type, result still unsigned
14
+ // C4804: unsafe use of type 'bool' in operation
15
+ // It can be addressed by disabling the following warning.
16
+ #ifdef _MSC_VER
17
+ #pragma warning(push)
18
+ #pragma warning(disable : 4146)
19
+ #pragma warning(disable : 4804)
20
+ #pragma warning(disable : 4018)
21
+ #endif
22
+
23
+ // The overflow checks may involve float to int conversion which may
24
+ // trigger precision loss warning. Re-enable the warning once the code
25
+ // is fixed. See T58053069.
26
+ C10_CLANG_DIAGNOSTIC_PUSH()
27
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
28
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
29
+ #endif
30
+
31
+ // bool can be converted to any type.
32
+ // Without specializing on bool, in pytorch_linux_trusty_py2_7_9_build:
33
+ // `error: comparison of constant '255' with boolean expression is always false`
34
+ // for `f > limit::max()` below
35
+ template <typename To, typename From>
36
+ std::enable_if_t<std::is_same_v<From, bool>, bool> overflows(
37
+ From /*f*/,
38
+ bool strict_unsigned [[maybe_unused]] = false) {
39
+ return false;
40
+ }
41
+
42
+ // skip isnan and isinf check for integral types
43
+ template <typename To, typename From>
44
+ std::enable_if_t<std::is_integral_v<From> && !std::is_same_v<From, bool>, bool>
45
+ overflows(From f, bool strict_unsigned = false) {
46
+ using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
47
+ if constexpr (!limit::is_signed && std::numeric_limits<From>::is_signed) {
48
+ // allow for negative numbers to wrap using two's complement arithmetic.
49
+ // For example, with uint8, this allows for `a - b` to be treated as
50
+ // `a + 255 * b`.
51
+ if (!strict_unsigned) {
52
+ return greater_than_max<To>(f) ||
53
+ (c10::is_negative(f) &&
54
+ -static_cast<uint64_t>(f) > static_cast<uint64_t>(limit::max()));
55
+ }
56
+ }
57
+ return c10::less_than_lowest<To>(f) || greater_than_max<To>(f);
58
+ }
59
+
60
+ template <typename To, typename From>
61
+ std::enable_if_t<std::is_floating_point_v<From>, bool> overflows(
62
+ From f,
63
+ bool strict_unsigned [[maybe_unused]] = false) {
64
+ using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
65
+ if (limit::has_infinity && std::isinf(static_cast<double>(f))) {
66
+ return false;
67
+ }
68
+ if (!limit::has_quiet_NaN && (f != f)) {
69
+ return true;
70
+ }
71
+ return f < limit::lowest() || f > limit::max();
72
+ }
73
+
74
+ C10_CLANG_DIAGNOSTIC_POP()
75
+
76
+ #ifdef _MSC_VER
77
+ #pragma warning(pop)
78
+ #endif
79
+
80
+ template <typename To, typename From>
81
+ std::enable_if_t<is_complex<From>::value, bool> overflows(
82
+ From f,
83
+ bool strict_unsigned = false) {
84
+ // casts from complex to real are considered to overflow if the
85
+ // imaginary component is non-zero
86
+ if (!is_complex<To>::value && f.imag() != 0) {
87
+ return true;
88
+ }
89
+ // Check for overflow componentwise
90
+ // (Technically, the imag overflow check is guaranteed to be false
91
+ // when !is_complex<To>, but any optimizer worth its salt will be
92
+ // able to figure it out.)
93
+ return overflows<
94
+ typename scalar_value_type<To>::type,
95
+ typename From::value_type>(f.real(), strict_unsigned) ||
96
+ overflows<
97
+ typename scalar_value_type<To>::type,
98
+ typename From::value_type>(f.imag(), strict_unsigned);
99
+ }
100
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/overloaded.h ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ namespace c10 {
5
+ namespace detail {
6
+
7
+ template <class... Ts>
8
+ struct overloaded_t {};
9
+
10
+ template <class T0>
11
+ struct overloaded_t<T0> : T0 {
12
+ using T0::operator();
13
+ overloaded_t(T0 t0) : T0(std::move(t0)) {}
14
+ };
15
+ template <class T0, class... Ts>
16
+ struct overloaded_t<T0, Ts...> : T0, overloaded_t<Ts...> {
17
+ using T0::operator();
18
+ using overloaded_t<Ts...>::operator();
19
+ overloaded_t(T0 t0, Ts... ts)
20
+ : T0(std::move(t0)), overloaded_t<Ts...>(std::move(ts)...) {}
21
+ };
22
+
23
+ } // namespace detail
24
+
25
+ // Construct an overloaded callable combining multiple callables, e.g. lambdas
26
+ template <class... Ts>
27
+ detail::overloaded_t<Ts...> overloaded(Ts... ts) {
28
+ return {std::move(ts)...};
29
+ }
30
+
31
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/python_stub.h ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ #pragma once
2
+
3
+ struct _object;
4
+ using PyObject = _object;
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/qint32.h ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <c10/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * qint32 is for signed 32 bit quantized Tensors
10
+ */
11
+ struct alignas(4) qint32 {
12
+ using underlying = int32_t;
13
+ int32_t val_;
14
+ qint32() = default;
15
+ C10_HOST_DEVICE explicit qint32(int32_t val) : val_(val) {}
16
+ };
17
+
18
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/qint8.h ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <c10/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * This is the data type for quantized Tensors. Right now we only have
10
+ * qint8 which is for 8 bit Tensors, and qint32 for 32 bit int Tensors,
11
+ * we might have 4 bit, 2 bit or 1 bit data types in the future.
12
+ */
13
+ struct alignas(1) qint8 {
14
+ using underlying = int8_t;
15
+ int8_t val_;
16
+ qint8() = default;
17
+ C10_HOST_DEVICE explicit qint8(int8_t val) : val_(val) {}
18
+ };
19
+
20
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/quint2x4.h ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <c10/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * quint2x4 is for un-signed 2 bit quantized Tensors that are packed to byte
10
+ * boundary.
11
+ */
12
+ struct alignas(1) quint2x4 {
13
+ using underlying = uint8_t;
14
+ uint8_t val_;
15
+ quint2x4() = default;
16
+ C10_HOST_DEVICE explicit quint2x4(uint8_t val) : val_(val) {}
17
+ };
18
+
19
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/quint4x2.h ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <c10/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * quint4x2 is for un-signed 4 bit quantized Tensors that are packed to byte
10
+ * boundary.
11
+ */
12
+ struct alignas(1) quint4x2 {
13
+ using underlying = uint8_t;
14
+ uint8_t val_;
15
+ quint4x2() = default;
16
+ C10_HOST_DEVICE explicit quint4x2(uint8_t val) : val_(val) {}
17
+ };
18
+
19
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/quint8.h ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <c10/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * quint8 is for unsigned 8 bit quantized Tensors
10
+ */
11
+ struct alignas(1) quint8 {
12
+ using underlying = uint8_t;
13
+ uint8_t val_;
14
+ quint8() = default;
15
+ C10_HOST_DEVICE explicit quint8(uint8_t val) : val_(val) {}
16
+ };
17
+
18
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/safe_numerics.h ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <c10/macros/Macros.h>
3
+
4
+ #include <cstdint>
5
+
6
+ // GCC has __builtin_mul_overflow from before it supported __has_builtin
7
+ #ifdef _MSC_VER
8
+ #define C10_HAS_BUILTIN_OVERFLOW() (0)
9
+ #include <c10/util/llvmMathExtras.h>
10
+ #include <intrin.h>
11
+ #else
12
+ #define C10_HAS_BUILTIN_OVERFLOW() (1)
13
+ #endif
14
+
15
+ namespace c10 {
16
+
17
+ C10_ALWAYS_INLINE bool add_overflows(uint64_t a, uint64_t b, uint64_t* out) {
18
+ #if C10_HAS_BUILTIN_OVERFLOW()
19
+ return __builtin_add_overflow(a, b, out);
20
+ #else
21
+ unsigned long long tmp;
22
+ #if defined(_M_IX86) || defined(_M_X64)
23
+ auto carry = _addcarry_u64(0, a, b, &tmp);
24
+ #else
25
+ tmp = a + b;
26
+ unsigned long long vector = (a & b) ^ ((a ^ b) & ~tmp);
27
+ auto carry = vector >> 63;
28
+ #endif
29
+ *out = tmp;
30
+ return carry;
31
+ #endif
32
+ }
33
+
34
+ C10_ALWAYS_INLINE bool mul_overflows(uint64_t a, uint64_t b, uint64_t* out) {
35
+ #if C10_HAS_BUILTIN_OVERFLOW()
36
+ return __builtin_mul_overflow(a, b, out);
37
+ #else
38
+ *out = a * b;
39
+ // This test isn't exact, but avoids doing integer division
40
+ return (
41
+ (c10::llvm::countLeadingZeros(a) + c10::llvm::countLeadingZeros(b)) < 64);
42
+ #endif
43
+ }
44
+
45
+ C10_ALWAYS_INLINE bool mul_overflows(int64_t a, int64_t b, int64_t* out) {
46
+ #if C10_HAS_BUILTIN_OVERFLOW()
47
+ return __builtin_mul_overflow(a, b, out);
48
+ #else
49
+ volatile int64_t tmp = a * b;
50
+ *out = tmp;
51
+ if (a == 0 || b == 0) {
52
+ return false;
53
+ }
54
+ return !(a == tmp / b);
55
+ #endif
56
+ }
57
+
58
+ template <typename It>
59
+ bool safe_multiplies_u64(It first, It last, uint64_t* out) {
60
+ #if C10_HAS_BUILTIN_OVERFLOW()
61
+ uint64_t prod = 1;
62
+ bool overflow = false;
63
+ for (; first != last; ++first) {
64
+ overflow |= c10::mul_overflows(prod, *first, &prod);
65
+ }
66
+ *out = prod;
67
+ return overflow;
68
+ #else
69
+ uint64_t prod = 1;
70
+ uint64_t prod_log2 = 0;
71
+ bool is_zero = false;
72
+ for (; first != last; ++first) {
73
+ auto x = static_cast<uint64_t>(*first);
74
+ prod *= x;
75
+ // log2(0) isn't valid, so need to track it specially
76
+ is_zero |= (x == 0);
77
+ prod_log2 += c10::llvm::Log2_64_Ceil(x);
78
+ }
79
+ *out = prod;
80
+ // This test isn't exact, but avoids doing integer division
81
+ return !is_zero && (prod_log2 >= 64);
82
+ #endif
83
+ }
84
+
85
+ template <typename Container>
86
+ bool safe_multiplies_u64(const Container& c, uint64_t* out) {
87
+ return safe_multiplies_u64(c.begin(), c.end(), out);
88
+ }
89
+
90
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/signal_handler.h ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <atomic>
4
+ #include <condition_variable>
5
+ #include <csignal>
6
+ #include <cstdint>
7
+ #include <mutex>
8
+
9
+ #include <c10/macros/Export.h>
10
+
11
+ #if defined(__APPLE__)
12
+ #define C10_SUPPORTS_SIGNAL_HANDLER
13
+ #elif defined(__linux__) && !defined(C10_DISABLE_SIGNAL_HANDLERS)
14
+ #define C10_SUPPORTS_FATAL_SIGNAL_HANDLERS
15
+ #define C10_SUPPORTS_SIGNAL_HANDLER
16
+ #endif
17
+
18
+ #if defined(C10_SUPPORTS_FATAL_SIGNAL_HANDLERS)
19
+ #include <pthread.h>
20
+ #endif
21
+
22
+ namespace c10 {
23
+
24
+ class C10_API SignalHandler {
25
+ public:
26
+ enum class Action { NONE, STOP };
27
+
28
+ // Constructor. Specify what action to take when a signal is received.
29
+ SignalHandler(Action SIGINT_action, Action SIGHUP_action);
30
+
31
+ SignalHandler(const SignalHandler&) = delete;
32
+ SignalHandler(SignalHandler&&) = delete;
33
+ SignalHandler& operator=(const SignalHandler&) = delete;
34
+ SignalHandler& operator=(SignalHandler&&) = delete;
35
+ ~SignalHandler();
36
+
37
+ Action CheckForSignals();
38
+
39
+ bool GotSIGINT();
40
+ bool GotSIGHUP();
41
+
42
+ Action SIGINT_action_;
43
+ Action SIGHUP_action_;
44
+ std::atomic<uint64_t> my_sigint_count_;
45
+ std::atomic<uint64_t> my_sighup_count_;
46
+ };
47
+
48
+ #if defined(C10_SUPPORTS_FATAL_SIGNAL_HANDLERS)
49
+ class C10_API FatalSignalHandler {
50
+ // This works by setting up certain fatal signal handlers. Previous fatal
51
+ // signal handlers will still be called when the signal is raised. Defaults
52
+ // to being off.
53
+ public:
54
+ C10_API void setPrintStackTracesOnFatalSignal(bool print);
55
+ C10_API bool printStackTracesOnFatalSignal();
56
+ static FatalSignalHandler& getInstance();
57
+ FatalSignalHandler(const FatalSignalHandler&) = delete;
58
+ FatalSignalHandler(FatalSignalHandler&&) = delete;
59
+ FatalSignalHandler& operator=(const FatalSignalHandler&) = delete;
60
+ FatalSignalHandler& operator=(FatalSignalHandler&&) = delete;
61
+ virtual ~FatalSignalHandler() = default;
62
+
63
+ protected:
64
+ explicit FatalSignalHandler();
65
+
66
+ private:
67
+ void installFatalSignalHandlers();
68
+ void uninstallFatalSignalHandlers();
69
+ static void fatalSignalHandlerStatic(int signum);
70
+ void fatalSignalHandler(int signum);
71
+ virtual void fatalSignalHandlerPostProcess();
72
+ struct sigaction* getPreviousSigaction(int signum);
73
+ const char* getSignalName(int signum);
74
+ void callPreviousSignalHandler(
75
+ struct sigaction* action,
76
+ int signum,
77
+ siginfo_t* info,
78
+ void* ctx);
79
+ void stacktraceSignalHandler(bool needsLock);
80
+ static void stacktraceSignalHandlerStatic(
81
+ int signum,
82
+ siginfo_t* info,
83
+ void* ctx);
84
+ void stacktraceSignalHandler(int signum, siginfo_t* info, void* ctx);
85
+
86
+ // The mutex protects the bool.
87
+ std::mutex fatalSignalHandlersInstallationMutex;
88
+ bool fatalSignalHandlersInstalled;
89
+ // We need to hold a reference to call the previous SIGUSR2 handler in case
90
+ // we didn't signal it
91
+ struct sigaction previousSigusr2{};
92
+ // Flag dictating whether the SIGUSR2 handler falls back to previous handlers
93
+ // or is intercepted in order to print a stack trace.
94
+ std::atomic<bool> fatalSignalReceived;
95
+ // Global state set when a fatal signal is received so that backtracing
96
+ // threads know why they're printing a stacktrace.
97
+ const char* fatalSignalName;
98
+ int fatalSignum = -1;
99
+ // This wait condition is used to wait for other threads to finish writing
100
+ // their stack trace when in fatal sig handler (we can't use pthread_join
101
+ // because there's no way to convert from a tid to a pthread_t).
102
+ std::condition_variable writingCond;
103
+ std::mutex writingMutex;
104
+ // used to indicate if the other thread responded to the signal
105
+ bool signalReceived;
106
+
107
+ struct signal_handler {
108
+ const char* name;
109
+ int signum;
110
+ struct sigaction previous;
111
+ };
112
+
113
+ // NOLINTNEXTLINE(*c-arrays*)
114
+ static signal_handler kSignalHandlers[];
115
+ };
116
+
117
+ #endif // defined(C10_SUPPORTS_SIGNAL_HANDLER)
118
+
119
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/sparse_bitset.h ADDED
@@ -0,0 +1,893 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //===- llvm/ADT/SparseBitVector.h - Efficient Sparse BitVector --*- C++ -*-===//
2
+ //
3
+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
+ // See https://llvm.org/LICENSE.txt for license information.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
+ //
7
+ //===----------------------------------------------------------------------===//
8
+ //
9
+ // This file defines the SparseBitVector class. See the doxygen comment for
10
+ // SparseBitVector for more details on the algorithm used.
11
+ //
12
+ //===----------------------------------------------------------------------===//
13
+
14
+ #pragma once
15
+ #include <c10/macros/Macros.h>
16
+ #include <c10/util/llvmMathExtras.h>
17
+ #include <array>
18
+ #include <cassert>
19
+ #include <climits>
20
+ #include <iterator>
21
+ #include <list>
22
+ #include <ostream>
23
+
24
+ namespace c10 {
25
+
26
+ /// SparseBitVector is an implementation of a bitvector that is sparse by only
27
+ /// storing the elements that have non-zero bits set. In order to make this
28
+ /// fast for the most common cases, SparseBitVector is implemented as a linked
29
+ /// list of SparseBitVectorElements. We maintain a pointer to the last
30
+ /// SparseBitVectorElement accessed (in the form of a list iterator), in order
31
+ /// to make multiple in-order test/set constant time after the first one is
32
+ /// executed. Note that using vectors to store SparseBitVectorElement's does
33
+ /// not work out very well because it causes insertion in the middle to take
34
+ /// enormous amounts of time with a large amount of bits. Other structures that
35
+ /// have better worst cases for insertion in the middle (various balanced trees,
36
+ /// etc) do not perform as well in practice as a linked list with this iterator
37
+ /// kept up to date. They are also significantly more memory intensive.
38
+
39
+ template <unsigned ElementSize = 128>
40
+ struct SparseBitVectorElement {
41
+ public:
42
+ using BitWord = unsigned long;
43
+ using size_type = unsigned;
44
+ enum {
45
+ BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
46
+ BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
47
+ BITS_PER_ELEMENT = ElementSize
48
+ };
49
+
50
+ private:
51
+ // Index of Element in terms of where first bit starts.
52
+ unsigned ElementIndex;
53
+ std::array<BitWord, BITWORDS_PER_ELEMENT> Bits{};
54
+
55
+ SparseBitVectorElement() : ElementIndex(~0U) {}
56
+
57
+ public:
58
+ explicit SparseBitVectorElement(unsigned Idx) : ElementIndex(Idx) {}
59
+
60
+ // Comparison.
61
+ bool operator==(const SparseBitVectorElement& RHS) const {
62
+ if (ElementIndex != RHS.ElementIndex)
63
+ return false;
64
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
65
+ if (Bits[i] != RHS.Bits[i])
66
+ return false;
67
+ return true;
68
+ }
69
+
70
+ bool operator!=(const SparseBitVectorElement& RHS) const {
71
+ return !(*this == RHS);
72
+ }
73
+
74
+ // Return the bits that make up word Idx in our element.
75
+ BitWord word(unsigned Idx) const {
76
+ assert(Idx < BITWORDS_PER_ELEMENT);
77
+ return Bits[Idx];
78
+ }
79
+
80
+ unsigned index() const {
81
+ return ElementIndex;
82
+ }
83
+
84
+ bool empty() const {
85
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
86
+ if (Bits[i])
87
+ return false;
88
+ return true;
89
+ }
90
+
91
+ void set(unsigned Idx) {
92
+ Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
93
+ }
94
+
95
+ bool test_and_set(unsigned Idx) {
96
+ bool old = test(Idx);
97
+ if (!old) {
98
+ set(Idx);
99
+ return true;
100
+ }
101
+ return false;
102
+ }
103
+
104
+ void reset(unsigned Idx) {
105
+ Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
106
+ }
107
+
108
+ bool test(unsigned Idx) const {
109
+ return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
110
+ }
111
+
112
+ size_type count() const {
113
+ unsigned NumBits = 0;
114
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
115
+ NumBits += llvm::countPopulation(Bits[i]);
116
+ return NumBits;
117
+ }
118
+
119
+ /// find_first - Returns the index of the first set bit.
120
+ int find_first() const {
121
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
122
+ if (Bits[i] != 0)
123
+ return i * BITWORD_SIZE + llvm::countTrailingZeros(Bits[i]);
124
+ throw std::runtime_error("Illegal empty element");
125
+ }
126
+
127
+ /// find_last - Returns the index of the last set bit.
128
+ int find_last() const {
129
+ for (unsigned I = 0; I < BITWORDS_PER_ELEMENT; ++I) {
130
+ unsigned Idx = BITWORDS_PER_ELEMENT - I - 1;
131
+ if (Bits[Idx] != 0)
132
+ return Idx * BITWORD_SIZE + BITWORD_SIZE -
133
+ llvm::countLeadingZeros(Bits[Idx]);
134
+ }
135
+ throw std::runtime_error("Illegal empty element");
136
+ }
137
+
138
+ /// find_next - Returns the index of the next set bit starting from the
139
+ /// "Curr" bit. Returns -1 if the next set bit is not found.
140
+ int find_next(unsigned Curr) const {
141
+ if (Curr >= BITS_PER_ELEMENT)
142
+ return -1;
143
+
144
+ unsigned WordPos = Curr / BITWORD_SIZE;
145
+ unsigned BitPos = Curr % BITWORD_SIZE;
146
+ BitWord Copy = Bits[WordPos];
147
+ assert(
148
+ WordPos <= BITWORDS_PER_ELEMENT && "Word Position outside of element");
149
+
150
+ // Mask off previous bits.
151
+ Copy &= ~0UL << BitPos;
152
+
153
+ if (Copy != 0)
154
+ return WordPos * BITWORD_SIZE + llvm::countTrailingZeros(Copy);
155
+
156
+ // Check subsequent words.
157
+ for (unsigned i = WordPos + 1; i < BITWORDS_PER_ELEMENT; ++i)
158
+ if (Bits[i] != 0)
159
+ return i * BITWORD_SIZE + llvm::countTrailingZeros(Bits[i]);
160
+ return -1;
161
+ }
162
+
163
+ // Union this element with RHS and return true if this one changed.
164
+ bool unionWith(const SparseBitVectorElement& RHS) {
165
+ bool changed = false;
166
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
167
+ BitWord old = changed ? 0 : Bits[i];
168
+
169
+ Bits[i] |= RHS.Bits[i];
170
+ if (!changed && old != Bits[i])
171
+ changed = true;
172
+ }
173
+ return changed;
174
+ }
175
+
176
+ // Return true if we have any bits in common with RHS
177
+ bool intersects(const SparseBitVectorElement& RHS) const {
178
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
179
+ if (RHS.Bits[i] & Bits[i])
180
+ return true;
181
+ }
182
+ return false;
183
+ }
184
+
185
+ // Intersect this Element with RHS and return true if this one changed.
186
+ // BecameZero is set to true if this element became all-zero bits.
187
+ bool intersectWith(const SparseBitVectorElement& RHS, bool& BecameZero) {
188
+ bool changed = false;
189
+ bool allzero = true;
190
+
191
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
192
+ BitWord old = changed ? 0 : Bits[i];
193
+
194
+ Bits[i] &= RHS.Bits[i];
195
+ if (Bits[i] != 0)
196
+ allzero = false;
197
+
198
+ if (!changed && old != Bits[i])
199
+ changed = true;
200
+ }
201
+ BecameZero = allzero;
202
+ return changed;
203
+ }
204
+
205
+ // Intersect this Element with the complement of RHS and return true if this
206
+ // one changed. BecameZero is set to true if this element became all-zero
207
+ // bits.
208
+ bool intersectWithComplement(
209
+ const SparseBitVectorElement& RHS,
210
+ bool& BecameZero) {
211
+ bool changed = false;
212
+ bool allzero = true;
213
+
214
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
215
+ BitWord old = changed ? 0 : Bits[i];
216
+
217
+ Bits[i] &= ~RHS.Bits[i];
218
+ if (Bits[i] != 0)
219
+ allzero = false;
220
+
221
+ if (!changed && old != Bits[i])
222
+ changed = true;
223
+ }
224
+ BecameZero = allzero;
225
+ return changed;
226
+ }
227
+
228
+ // Three argument version of intersectWithComplement that intersects
229
+ // RHS1 & ~RHS2 into this element
230
+ void intersectWithComplement(
231
+ const SparseBitVectorElement& RHS1,
232
+ const SparseBitVectorElement& RHS2,
233
+ bool& BecameZero) {
234
+ bool allzero = true;
235
+
236
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
237
+ Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
238
+ if (Bits[i] != 0)
239
+ allzero = false;
240
+ }
241
+ BecameZero = allzero;
242
+ }
243
+ };
244
+
245
+ template <unsigned ElementSize = 128>
246
+ class SparseBitVector {
247
+ using ElementList = std::list<SparseBitVectorElement<ElementSize>>;
248
+ using ElementListIter = typename ElementList::iterator;
249
+ using ElementListConstIter = typename ElementList::const_iterator;
250
+ enum { BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE };
251
+
252
+ ElementList Elements;
253
+ // Pointer to our current Element. This has no visible effect on the external
254
+ // state of a SparseBitVector, it's just used to improve performance in the
255
+ // common case of testing/modifying bits with similar indices.
256
+ mutable ElementListIter CurrElementIter;
257
+
258
+ // This is like std::lower_bound, except we do linear searching from the
259
+ // current position.
260
+ ElementListIter FindLowerBoundImpl(unsigned ElementIndex) const {
261
+ // We cache a non-const iterator so we're forced to resort to const_cast to
262
+ // get the begin/end in the case where 'this' is const. To avoid duplication
263
+ // of code with the only difference being whether the const cast is present
264
+ // 'this' is always const in this particular function and we sort out the
265
+ // difference in FindLowerBound and FindLowerBoundConst.
266
+ ElementListIter Begin =
267
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
268
+ const_cast<SparseBitVector<ElementSize>*>(this)->Elements.begin();
269
+ ElementListIter End =
270
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
271
+ const_cast<SparseBitVector<ElementSize>*>(this)->Elements.end();
272
+
273
+ if (Elements.empty()) {
274
+ CurrElementIter = Begin;
275
+ return CurrElementIter;
276
+ }
277
+
278
+ // Make sure our current iterator is valid.
279
+ if (CurrElementIter == End)
280
+ --CurrElementIter;
281
+
282
+ // Search from our current iterator, either backwards or forwards,
283
+ // depending on what element we are looking for.
284
+ ElementListIter ElementIter = CurrElementIter;
285
+ if (CurrElementIter->index() == ElementIndex) {
286
+ return ElementIter;
287
+ } else if (CurrElementIter->index() > ElementIndex) {
288
+ while (ElementIter != Begin && ElementIter->index() > ElementIndex)
289
+ --ElementIter;
290
+ } else {
291
+ while (ElementIter != End && ElementIter->index() < ElementIndex)
292
+ ++ElementIter;
293
+ }
294
+ CurrElementIter = ElementIter;
295
+ return ElementIter;
296
+ }
297
+ ElementListConstIter FindLowerBoundConst(unsigned ElementIndex) const {
298
+ return FindLowerBoundImpl(ElementIndex);
299
+ }
300
+ ElementListIter FindLowerBound(unsigned ElementIndex) {
301
+ return FindLowerBoundImpl(ElementIndex);
302
+ }
303
+
304
+ // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
305
+ // than it would be, in order to be efficient.
306
+ class SparseBitVectorIterator {
307
+ private:
308
+ bool AtEnd{false};
309
+
310
+ const SparseBitVector<ElementSize>* BitVector = nullptr;
311
+
312
+ // Current element inside of bitmap.
313
+ ElementListConstIter Iter;
314
+
315
+ // Current bit number inside of our bitmap.
316
+ unsigned BitNumber{0};
317
+
318
+ // Current word number inside of our element.
319
+ unsigned WordNumber{0};
320
+
321
+ // Current bits from the element.
322
+ typename SparseBitVectorElement<ElementSize>::BitWord Bits{0};
323
+
324
+ // Move our iterator to the first non-zero bit in the bitmap.
325
+ void AdvanceToFirstNonZero() {
326
+ if (AtEnd)
327
+ return;
328
+ if (BitVector->Elements.empty()) {
329
+ AtEnd = true;
330
+ return;
331
+ }
332
+ Iter = BitVector->Elements.begin();
333
+ BitNumber = Iter->index() * ElementSize;
334
+ unsigned BitPos = Iter->find_first();
335
+ BitNumber += BitPos;
336
+ WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
337
+ Bits = Iter->word(WordNumber);
338
+ Bits >>= BitPos % BITWORD_SIZE;
339
+ }
340
+
341
+ // Move our iterator to the next non-zero bit.
342
+ void AdvanceToNextNonZero() {
343
+ if (AtEnd)
344
+ return;
345
+
346
+ while (Bits && !(Bits & 1)) {
347
+ Bits >>= 1;
348
+ BitNumber += 1;
349
+ }
350
+
351
+ // See if we ran out of Bits in this word.
352
+ if (!Bits) {
353
+ int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize);
354
+ // If we ran out of set bits in this element, move to next element.
355
+ if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
356
+ ++Iter;
357
+ WordNumber = 0;
358
+
359
+ // We may run out of elements in the bitmap.
360
+ if (Iter == BitVector->Elements.end()) {
361
+ AtEnd = true;
362
+ return;
363
+ }
364
+ // Set up for next non-zero word in bitmap.
365
+ BitNumber = Iter->index() * ElementSize;
366
+ NextSetBitNumber = Iter->find_first();
367
+ BitNumber += NextSetBitNumber;
368
+ WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
369
+ Bits = Iter->word(WordNumber);
370
+ Bits >>= NextSetBitNumber % BITWORD_SIZE;
371
+ } else {
372
+ WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
373
+ Bits = Iter->word(WordNumber);
374
+ Bits >>= NextSetBitNumber % BITWORD_SIZE;
375
+ BitNumber = Iter->index() * ElementSize;
376
+ BitNumber += NextSetBitNumber;
377
+ }
378
+ }
379
+ }
380
+
381
+ public:
382
+ SparseBitVectorIterator() = default;
383
+
384
+ SparseBitVectorIterator(
385
+ const SparseBitVector<ElementSize>* RHS,
386
+ bool end = false)
387
+ : AtEnd(end),
388
+ BitVector(RHS),
389
+ Iter(BitVector->Elements.begin()),
390
+ WordNumber(~0) {
391
+ AdvanceToFirstNonZero();
392
+ }
393
+
394
+ // Preincrement.
395
+ inline SparseBitVectorIterator& operator++() {
396
+ ++BitNumber;
397
+ Bits >>= 1;
398
+ AdvanceToNextNonZero();
399
+ return *this;
400
+ }
401
+
402
+ // Postincrement.
403
+ inline SparseBitVectorIterator operator++(int) {
404
+ SparseBitVectorIterator tmp = *this;
405
+ ++*this;
406
+ return tmp;
407
+ }
408
+
409
+ // Return the current set bit number.
410
+ unsigned operator*() const {
411
+ return BitNumber;
412
+ }
413
+
414
+ bool operator==(const SparseBitVectorIterator& RHS) const {
415
+ // If they are both at the end, ignore the rest of the fields.
416
+ if (AtEnd && RHS.AtEnd)
417
+ return true;
418
+ // Otherwise they are the same if they have the same bit number and
419
+ // bitmap.
420
+ return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
421
+ }
422
+
423
+ bool operator!=(const SparseBitVectorIterator& RHS) const {
424
+ return !(*this == RHS);
425
+ }
426
+ };
427
+
428
+ public:
429
+ using iterator = SparseBitVectorIterator;
430
+
431
+ SparseBitVector() : Elements(), CurrElementIter(Elements.begin()) {}
432
+
433
+ SparseBitVector(const SparseBitVector& RHS)
434
+ : Elements(RHS.Elements), CurrElementIter(Elements.begin()) {}
435
+ SparseBitVector(SparseBitVector&& RHS) noexcept
436
+ : Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {}
437
+ ~SparseBitVector() = default;
438
+
439
+ // Clear.
440
+ void clear() {
441
+ Elements.clear();
442
+ }
443
+
444
+ // Assignment
445
+ SparseBitVector& operator=(const SparseBitVector& RHS) {
446
+ if (this == &RHS)
447
+ return *this;
448
+
449
+ Elements = RHS.Elements;
450
+ CurrElementIter = Elements.begin();
451
+ return *this;
452
+ }
453
+ SparseBitVector& operator=(SparseBitVector&& RHS) noexcept {
454
+ Elements = std::move(RHS.Elements);
455
+ CurrElementIter = Elements.begin();
456
+ return *this;
457
+ }
458
+
459
+ // Test, Reset, and Set a bit in the bitmap.
460
+ bool test(unsigned Idx) const {
461
+ if (Elements.empty())
462
+ return false;
463
+
464
+ unsigned ElementIndex = Idx / ElementSize;
465
+ ElementListConstIter ElementIter = FindLowerBoundConst(ElementIndex);
466
+
467
+ // If we can't find an element that is supposed to contain this bit, there
468
+ // is nothing more to do.
469
+ if (ElementIter == Elements.end() || ElementIter->index() != ElementIndex)
470
+ return false;
471
+ return ElementIter->test(Idx % ElementSize);
472
+ }
473
+
474
+ void reset(unsigned Idx) {
475
+ if (Elements.empty())
476
+ return;
477
+
478
+ unsigned ElementIndex = Idx / ElementSize;
479
+ ElementListIter ElementIter = FindLowerBound(ElementIndex);
480
+
481
+ // If we can't find an element that is supposed to contain this bit, there
482
+ // is nothing more to do.
483
+ if (ElementIter == Elements.end() || ElementIter->index() != ElementIndex)
484
+ return;
485
+ ElementIter->reset(Idx % ElementSize);
486
+
487
+ // When the element is zeroed out, delete it.
488
+ if (ElementIter->empty()) {
489
+ ++CurrElementIter;
490
+ Elements.erase(ElementIter);
491
+ }
492
+ }
493
+
494
+ void set(unsigned Idx) {
495
+ unsigned ElementIndex = Idx / ElementSize;
496
+ ElementListIter ElementIter;
497
+ if (Elements.empty()) {
498
+ ElementIter = Elements.emplace(Elements.end(), ElementIndex);
499
+ } else {
500
+ ElementIter = FindLowerBound(ElementIndex);
501
+
502
+ if (ElementIter == Elements.end() ||
503
+ ElementIter->index() != ElementIndex) {
504
+ // We may have hit the beginning of our SparseBitVector, in which case,
505
+ // we may need to insert right after this element, which requires moving
506
+ // the current iterator forward one, because insert does insert before.
507
+ if (ElementIter != Elements.end() &&
508
+ ElementIter->index() < ElementIndex)
509
+ ++ElementIter;
510
+ ElementIter = Elements.emplace(ElementIter, ElementIndex);
511
+ }
512
+ }
513
+ CurrElementIter = ElementIter;
514
+
515
+ ElementIter->set(Idx % ElementSize);
516
+ }
517
+
518
+ bool test_and_set(unsigned Idx) {
519
+ bool old = test(Idx);
520
+ if (!old) {
521
+ set(Idx);
522
+ return true;
523
+ }
524
+ return false;
525
+ }
526
+
527
+ bool operator!=(const SparseBitVector& RHS) const {
528
+ return !(*this == RHS);
529
+ }
530
+
531
+ bool operator==(const SparseBitVector& RHS) const {
532
+ ElementListConstIter Iter1 = Elements.begin();
533
+ ElementListConstIter Iter2 = RHS.Elements.begin();
534
+
535
+ for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
536
+ ++Iter1, ++Iter2) {
537
+ if (*Iter1 != *Iter2)
538
+ return false;
539
+ }
540
+ return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
541
+ }
542
+
543
+ // Union our bitmap with the RHS and return true if we changed.
544
+ bool operator|=(const SparseBitVector& RHS) {
545
+ if (this == &RHS)
546
+ return false;
547
+
548
+ if (empty()) {
549
+ *this = RHS;
550
+ return true;
551
+ }
552
+
553
+ bool changed = false;
554
+ ElementListIter Iter1 = Elements.begin();
555
+ ElementListConstIter Iter2 = RHS.Elements.begin();
556
+
557
+ // If RHS is empty, we are done
558
+ if (RHS.Elements.empty())
559
+ return false;
560
+
561
+ while (Iter2 != RHS.Elements.end()) {
562
+ if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
563
+ Elements.insert(Iter1, *Iter2);
564
+ ++Iter2;
565
+ changed = true;
566
+ } else if (Iter1->index() == Iter2->index()) {
567
+ changed |= Iter1->unionWith(*Iter2);
568
+ ++Iter1;
569
+ ++Iter2;
570
+ } else {
571
+ ++Iter1;
572
+ }
573
+ }
574
+ CurrElementIter = Elements.begin();
575
+ return changed;
576
+ }
577
+
578
+ // Intersect our bitmap with the RHS and return true if ours changed.
579
+ bool operator-=(const SparseBitVector& RHS) {
580
+ return intersectWithComplement(RHS);
581
+ }
582
+
583
+ // Intersect our bitmap with the RHS and return true if ours changed.
584
+ bool operator&=(const SparseBitVector& RHS) {
585
+ if (this == &RHS)
586
+ return false;
587
+
588
+ bool changed = false;
589
+ ElementListIter Iter1 = Elements.begin();
590
+ ElementListConstIter Iter2 = RHS.Elements.begin();
591
+
592
+ // Check if both bitmaps are empty.
593
+ if (Elements.empty() && RHS.Elements.empty())
594
+ return false;
595
+
596
+ // Loop through, intersecting as we go, erasing elements when necessary.
597
+ while (Iter2 != RHS.Elements.end()) {
598
+ if (Iter1 == Elements.end()) {
599
+ CurrElementIter = Elements.begin();
600
+ return changed;
601
+ }
602
+
603
+ if (Iter1->index() > Iter2->index()) {
604
+ ++Iter2;
605
+ } else if (Iter1->index() == Iter2->index()) {
606
+ bool BecameZero = false;
607
+ changed |= Iter1->intersectWith(*Iter2, BecameZero);
608
+ if (BecameZero) {
609
+ ElementListIter IterTmp = Iter1;
610
+ ++Iter1;
611
+ Elements.erase(IterTmp);
612
+ } else {
613
+ ++Iter1;
614
+ }
615
+ ++Iter2;
616
+ } else {
617
+ ElementListIter IterTmp = Iter1;
618
+ ++Iter1;
619
+ Elements.erase(IterTmp);
620
+ changed = true;
621
+ }
622
+ }
623
+ if (Iter1 != Elements.end()) {
624
+ Elements.erase(Iter1, Elements.end());
625
+ changed = true;
626
+ }
627
+ CurrElementIter = Elements.begin();
628
+ return changed;
629
+ }
630
+
631
+ // Intersect our bitmap with the complement of the RHS and return true
632
+ // if ours changed.
633
+ bool intersectWithComplement(const SparseBitVector& RHS) {
634
+ if (this == &RHS) {
635
+ if (!empty()) {
636
+ clear();
637
+ return true;
638
+ }
639
+ return false;
640
+ }
641
+
642
+ bool changed = false;
643
+ ElementListIter Iter1 = Elements.begin();
644
+ ElementListConstIter Iter2 = RHS.Elements.begin();
645
+
646
+ // If either our bitmap or RHS is empty, we are done
647
+ if (Elements.empty() || RHS.Elements.empty())
648
+ return false;
649
+
650
+ // Loop through, intersecting as we go, erasing elements when necessary.
651
+ while (Iter2 != RHS.Elements.end()) {
652
+ if (Iter1 == Elements.end()) {
653
+ CurrElementIter = Elements.begin();
654
+ return changed;
655
+ }
656
+
657
+ if (Iter1->index() > Iter2->index()) {
658
+ ++Iter2;
659
+ } else if (Iter1->index() == Iter2->index()) {
660
+ bool BecameZero = false;
661
+ changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
662
+ if (BecameZero) {
663
+ ElementListIter IterTmp = Iter1;
664
+ ++Iter1;
665
+ Elements.erase(IterTmp);
666
+ } else {
667
+ ++Iter1;
668
+ }
669
+ ++Iter2;
670
+ } else {
671
+ ++Iter1;
672
+ }
673
+ }
674
+ CurrElementIter = Elements.begin();
675
+ return changed;
676
+ }
677
+
678
+ bool intersectWithComplement(const SparseBitVector<ElementSize>* RHS) const {
679
+ return intersectWithComplement(*RHS);
680
+ }
681
+
682
+ // Three argument version of intersectWithComplement.
683
+ // Result of RHS1 & ~RHS2 is stored into this bitmap.
684
+ void intersectWithComplement(
685
+ const SparseBitVector<ElementSize>& RHS1,
686
+ const SparseBitVector<ElementSize>& RHS2) {
687
+ if (this == &RHS1) {
688
+ intersectWithComplement(RHS2);
689
+ return;
690
+ } else if (this == &RHS2) {
691
+ SparseBitVector RHS2Copy(RHS2);
692
+ intersectWithComplement(RHS1, RHS2Copy);
693
+ return;
694
+ }
695
+
696
+ Elements.clear();
697
+ CurrElementIter = Elements.begin();
698
+ ElementListConstIter Iter1 = RHS1.Elements.begin();
699
+ ElementListConstIter Iter2 = RHS2.Elements.begin();
700
+
701
+ // If RHS1 is empty, we are done
702
+ // If RHS2 is empty, we still have to copy RHS1
703
+ if (RHS1.Elements.empty())
704
+ return;
705
+
706
+ // Loop through, intersecting as we go, erasing elements when necessary.
707
+ while (Iter2 != RHS2.Elements.end()) {
708
+ if (Iter1 == RHS1.Elements.end())
709
+ return;
710
+
711
+ if (Iter1->index() > Iter2->index()) {
712
+ ++Iter2;
713
+ } else if (Iter1->index() == Iter2->index()) {
714
+ bool BecameZero = false;
715
+ Elements.emplace_back(Iter1->index());
716
+ Elements.back().intersectWithComplement(*Iter1, *Iter2, BecameZero);
717
+ if (BecameZero)
718
+ Elements.pop_back();
719
+ ++Iter1;
720
+ ++Iter2;
721
+ } else {
722
+ Elements.push_back(*Iter1++);
723
+ }
724
+ }
725
+
726
+ // copy the remaining elements
727
+ std::copy(Iter1, RHS1.Elements.end(), std::back_inserter(Elements));
728
+ }
729
+
730
+ void intersectWithComplement(
731
+ const SparseBitVector<ElementSize>* RHS1,
732
+ const SparseBitVector<ElementSize>* RHS2) {
733
+ intersectWithComplement(*RHS1, *RHS2);
734
+ }
735
+
736
+ bool intersects(const SparseBitVector<ElementSize>* RHS) const {
737
+ return intersects(*RHS);
738
+ }
739
+
740
+ // Return true if we share any bits in common with RHS
741
+ bool intersects(const SparseBitVector<ElementSize>& RHS) const {
742
+ ElementListConstIter Iter1 = Elements.begin();
743
+ ElementListConstIter Iter2 = RHS.Elements.begin();
744
+
745
+ // Check if both bitmaps are empty.
746
+ if (Elements.empty() && RHS.Elements.empty())
747
+ return false;
748
+
749
+ // Loop through, intersecting stopping when we hit bits in common.
750
+ while (Iter2 != RHS.Elements.end()) {
751
+ if (Iter1 == Elements.end())
752
+ return false;
753
+
754
+ if (Iter1->index() > Iter2->index()) {
755
+ ++Iter2;
756
+ } else if (Iter1->index() == Iter2->index()) {
757
+ if (Iter1->intersects(*Iter2))
758
+ return true;
759
+ ++Iter1;
760
+ ++Iter2;
761
+ } else {
762
+ ++Iter1;
763
+ }
764
+ }
765
+ return false;
766
+ }
767
+
768
+ // Return true iff all bits set in this SparseBitVector are
769
+ // also set in RHS.
770
+ bool contains(const SparseBitVector<ElementSize>& RHS) const {
771
+ SparseBitVector<ElementSize> Result(*this);
772
+ Result &= RHS;
773
+ return (Result == RHS);
774
+ }
775
+
776
+ // Return the first set bit in the bitmap. Return -1 if no bits are set.
777
+ int find_first() const {
778
+ if (Elements.empty())
779
+ return -1;
780
+ const SparseBitVectorElement<ElementSize>& First = *(Elements.begin());
781
+ return (First.index() * ElementSize) + First.find_first();
782
+ }
783
+
784
+ // Return the last set bit in the bitmap. Return -1 if no bits are set.
785
+ int find_last() const {
786
+ if (Elements.empty())
787
+ return -1;
788
+ const SparseBitVectorElement<ElementSize>& Last = *(Elements.rbegin());
789
+ return (Last.index() * ElementSize) + Last.find_last();
790
+ }
791
+
792
+ // Return true if the SparseBitVector is empty
793
+ bool empty() const {
794
+ return Elements.empty();
795
+ }
796
+
797
+ unsigned count() const {
798
+ unsigned BitCount = 0;
799
+ for (ElementListConstIter Iter = Elements.begin(); Iter != Elements.end();
800
+ ++Iter)
801
+ BitCount += Iter->count();
802
+
803
+ return BitCount;
804
+ }
805
+
806
+ iterator begin() const {
807
+ return iterator(this);
808
+ }
809
+
810
+ iterator end() const {
811
+ return iterator(this, true);
812
+ }
813
+ };
814
+
815
+ // Convenience functions to allow Or and And without dereferencing in the user
816
+ // code.
817
+
818
+ template <unsigned ElementSize>
819
+ inline bool operator|=(
820
+ SparseBitVector<ElementSize>& LHS,
821
+ const SparseBitVector<ElementSize>* RHS) {
822
+ return LHS |= *RHS;
823
+ }
824
+
825
+ template <unsigned ElementSize>
826
+ inline bool operator|=(
827
+ SparseBitVector<ElementSize>* LHS,
828
+ const SparseBitVector<ElementSize>& RHS) {
829
+ return LHS->operator|=(RHS);
830
+ }
831
+
832
+ template <unsigned ElementSize>
833
+ inline bool operator&=(
834
+ SparseBitVector<ElementSize>* LHS,
835
+ const SparseBitVector<ElementSize>& RHS) {
836
+ return LHS->operator&=(RHS);
837
+ }
838
+
839
+ template <unsigned ElementSize>
840
+ inline bool operator&=(
841
+ SparseBitVector<ElementSize>& LHS,
842
+ const SparseBitVector<ElementSize>* RHS) {
843
+ return LHS &= *RHS;
844
+ }
845
+
846
+ // Convenience functions for infix union, intersection, difference operators.
847
+
848
+ template <unsigned ElementSize>
849
+ inline SparseBitVector<ElementSize> operator|(
850
+ const SparseBitVector<ElementSize>& LHS,
851
+ const SparseBitVector<ElementSize>& RHS) {
852
+ SparseBitVector<ElementSize> Result(LHS);
853
+ Result |= RHS;
854
+ return Result;
855
+ }
856
+
857
+ template <unsigned ElementSize>
858
+ inline SparseBitVector<ElementSize> operator&(
859
+ const SparseBitVector<ElementSize>& LHS,
860
+ const SparseBitVector<ElementSize>& RHS) {
861
+ SparseBitVector<ElementSize> Result(LHS);
862
+ Result &= RHS;
863
+ return Result;
864
+ }
865
+
866
+ template <unsigned ElementSize>
867
+ inline SparseBitVector<ElementSize> operator-(
868
+ const SparseBitVector<ElementSize>& LHS,
869
+ const SparseBitVector<ElementSize>& RHS) {
870
+ SparseBitVector<ElementSize> Result;
871
+ Result.intersectWithComplement(LHS, RHS);
872
+ return Result;
873
+ }
874
+
875
+ template <unsigned ElementSize>
876
+ std::ostream& operator<<(
877
+ std::ostream& stream,
878
+ const SparseBitVector<ElementSize>& vec) {
879
+ bool first = true;
880
+ stream << "{";
881
+ for (auto el : vec) {
882
+ if (first) {
883
+ first = false;
884
+ } else {
885
+ stream << ", ";
886
+ }
887
+ stream << el;
888
+ }
889
+ stream << "}";
890
+ return stream;
891
+ }
892
+
893
+ } // end namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/ssize.h ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/util/Exception.h>
4
+ #include <c10/util/TypeSafeSignMath.h>
5
+
6
+ #include <cstddef>
7
+ #include <type_traits>
8
+
9
+ namespace c10 {
10
+
11
+ // Implementations of std::ssize() from C++ 20.
12
+ //
13
+ // This is useful in particular for avoiding -Werror=sign-compare
14
+ // issues.
15
+ //
16
+ // Use this with argument-dependent lookup, e.g.:
17
+ // use c10::ssize;
18
+ // auto size = ssize(container);
19
+ //
20
+ // As with the standard library version, containers are permitted to
21
+ // specialize this with a free function defined in the same namespace.
22
+ //
23
+ // See https://en.cppreference.com/w/cpp/iterator/size for more
24
+ // information as well as the source of our implementations.
25
+ //
26
+ // We augment the implementation by adding an assert() if an overflow
27
+ // would occur.
28
+
29
+ template <typename C>
30
+ constexpr auto ssize(const C& c) -> std::
31
+ common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>> {
32
+ using R = std::
33
+ common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>;
34
+ // We expect this to be exceedingly rare to fire and don't wish to
35
+ // pay a performance hit in release mode.
36
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!greater_than_max<R>(c.size()));
37
+ return static_cast<R>(c.size());
38
+ }
39
+
40
+ template <typename T, std::ptrdiff_t N>
41
+ // NOLINTNEXTLINE(*-c-arrays)
42
+ constexpr auto ssize(const T (&array)[N]) noexcept -> std::ptrdiff_t {
43
+ return N;
44
+ }
45
+
46
+ } // namespace c10
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/static_tracepoint.h ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) && \
4
+ !(defined(TORCH_DISABLE_SDT) && TORCH_DISABLE_SDT)
5
+
6
+ #define TORCH_HAVE_SDT 1
7
+
8
+ #include <c10/util/static_tracepoint_elfx86.h>
9
+
10
+ #define TORCH_SDT(name, ...) \
11
+ TORCH_SDT_PROBE_N( \
12
+ pytorch, name, 0, TORCH_SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__)
13
+ // Use TORCH_SDT_DEFINE_SEMAPHORE(name) to define the semaphore
14
+ // as global variable before using the TORCH_SDT_WITH_SEMAPHORE macro
15
+ #define TORCH_SDT_WITH_SEMAPHORE(name, ...) \
16
+ TORCH_SDT_PROBE_N( \
17
+ pytorch, name, 1, TORCH_SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__)
18
+ #define TORCH_SDT_IS_ENABLED(name) (TORCH_SDT_SEMAPHORE(pytorch, name) > 0)
19
+
20
+ #else
21
+
22
+ #define TORCH_HAVE_SDT 0
23
+
24
+ #define TORCH_SDT(name, ...) \
25
+ do { \
26
+ } while (0)
27
+ #define TORCH_SDT_WITH_SEMAPHORE(name, ...) \
28
+ do { \
29
+ } while (0)
30
+ #define TORCH_SDT_IS_ENABLED(name) (false)
31
+ #define TORCH_SDT_DEFINE_SEMAPHORE(name)
32
+ #define TORCH_SDT_DECLARE_SEMAPHORE(name)
33
+
34
+ #endif
code/LaDi-RL-old-qwen-cod/LaDi-RL-old-qwen-cod/venv/lib/python3.10/site-packages/torch/include/c10/util/static_tracepoint_elfx86.h ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // clang-format off
4
+
5
+ // Default constraint for the probe arguments as operands.
6
+ #ifndef TORCH_SDT_ARG_CONSTRAINT
7
+ #define TORCH_SDT_ARG_CONSTRAINT "nor"
8
+ #endif
9
+
10
+ // Instruction to emit for the probe.
11
+ #define TORCH_SDT_NOP nop
12
+
13
+ // Note section properties.
14
+ #define TORCH_SDT_NOTE_NAME "stapsdt"
15
+ #define TORCH_SDT_NOTE_TYPE 3
16
+
17
+ // Semaphore variables are put in this section
18
+ #define TORCH_SDT_SEMAPHORE_SECTION ".probes"
19
+
20
+ // Size of address depending on platform.
21
+ #ifdef __LP64__
22
+ #define TORCH_SDT_ASM_ADDR .8byte
23
+ #else
24
+ #define TORCH_SDT_ASM_ADDR .4byte
25
+ #endif
26
+
27
+ // Assembler helper Macros.
28
+ #define TORCH_SDT_S(x) #x
29
+ #define TORCH_SDT_ASM_1(x) TORCH_SDT_S(x) "\n"
30
+ #define TORCH_SDT_ASM_2(a, b) TORCH_SDT_S(a) "," TORCH_SDT_S(b) "\n"
31
+ #define TORCH_SDT_ASM_3(a, b, c) TORCH_SDT_S(a) "," TORCH_SDT_S(b) "," \
32
+ TORCH_SDT_S(c) "\n"
33
+ #define TORCH_SDT_ASM_STRING(x) TORCH_SDT_ASM_1(.asciz TORCH_SDT_S(x))
34
+
35
+ // Helper to determine the size of an argument.
36
+ #define TORCH_SDT_IS_ARRAY_POINTER(x) ((__builtin_classify_type(x) == 14) || \
37
+ (__builtin_classify_type(x) == 5))
38
+ #define TORCH_SDT_ARGSIZE(x) (TORCH_SDT_IS_ARRAY_POINTER(x) \
39
+ ? sizeof(void*) \
40
+ : sizeof(x))
41
+
42
+ // Format of each probe arguments as operand.
43
+ // Size of the argument tagged with TORCH_SDT_Sn, with "n" constraint.
44
+ // Value of the argument tagged with TORCH_SDT_An, with configured constraint.
45
+ #define TORCH_SDT_ARG(n, x) \
46
+ [TORCH_SDT_S##n] "n" ((size_t)TORCH_SDT_ARGSIZE(x)), \
47
+ [TORCH_SDT_A##n] TORCH_SDT_ARG_CONSTRAINT (x)
48
+
49
+ // Templates to append arguments as operands.
50
+ #define TORCH_SDT_OPERANDS_0() [__sdt_dummy] "g" (0)
51
+ #define TORCH_SDT_OPERANDS_1(_1) TORCH_SDT_ARG(1, _1)
52
+ #define TORCH_SDT_OPERANDS_2(_1, _2) \
53
+ TORCH_SDT_OPERANDS_1(_1), TORCH_SDT_ARG(2, _2)
54
+ #define TORCH_SDT_OPERANDS_3(_1, _2, _3) \
55
+ TORCH_SDT_OPERANDS_2(_1, _2), TORCH_SDT_ARG(3, _3)
56
+ #define TORCH_SDT_OPERANDS_4(_1, _2, _3, _4) \
57
+ TORCH_SDT_OPERANDS_3(_1, _2, _3), TORCH_SDT_ARG(4, _4)
58
+ #define TORCH_SDT_OPERANDS_5(_1, _2, _3, _4, _5) \
59
+ TORCH_SDT_OPERANDS_4(_1, _2, _3, _4), TORCH_SDT_ARG(5, _5)
60
+ #define TORCH_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6) \
61
+ TORCH_SDT_OPERANDS_5(_1, _2, _3, _4, _5), TORCH_SDT_ARG(6, _6)
62
+ #define TORCH_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7) \
63
+ TORCH_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6), TORCH_SDT_ARG(7, _7)
64
+ #define TORCH_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8) \
65
+ TORCH_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7), TORCH_SDT_ARG(8, _8)
66
+ #define TORCH_SDT_OPERANDS_9(_1, _2, _3, _4, _5, _6, _7, _8, _9) \
67
+ TORCH_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8), TORCH_SDT_ARG(9, _9)
68
+
69
+ // Templates to reference the arguments from operands in note section.
70
+ #define TORCH_SDT_ARGFMT(no) %n[TORCH_SDT_S##no]@%[TORCH_SDT_A##no]
71
+ #define TORCH_SDT_ARG_TEMPLATE_0 /*No arguments*/
72
+ #define TORCH_SDT_ARG_TEMPLATE_1 TORCH_SDT_ARGFMT(1)
73
+ #define TORCH_SDT_ARG_TEMPLATE_2 TORCH_SDT_ARG_TEMPLATE_1 TORCH_SDT_ARGFMT(2)
74
+ #define TORCH_SDT_ARG_TEMPLATE_3 TORCH_SDT_ARG_TEMPLATE_2 TORCH_SDT_ARGFMT(3)
75
+ #define TORCH_SDT_ARG_TEMPLATE_4 TORCH_SDT_ARG_TEMPLATE_3 TORCH_SDT_ARGFMT(4)
76
+ #define TORCH_SDT_ARG_TEMPLATE_5 TORCH_SDT_ARG_TEMPLATE_4 TORCH_SDT_ARGFMT(5)
77
+ #define TORCH_SDT_ARG_TEMPLATE_6 TORCH_SDT_ARG_TEMPLATE_5 TORCH_SDT_ARGFMT(6)
78
+ #define TORCH_SDT_ARG_TEMPLATE_7 TORCH_SDT_ARG_TEMPLATE_6 TORCH_SDT_ARGFMT(7)
79
+ #define TORCH_SDT_ARG_TEMPLATE_8 TORCH_SDT_ARG_TEMPLATE_7 TORCH_SDT_ARGFMT(8)
80
+ #define TORCH_SDT_ARG_TEMPLATE_9 TORCH_SDT_ARG_TEMPLATE_8 TORCH_SDT_ARGFMT(9)
81
+
82
+ // Resolvable by name macros
83
+ // An attribute that marks a function or variable as needing to be resolvable
84
+ // by name. This generally is needed if inline assembly refers to the variable
85
+ // by string name.
86
+ #ifdef __roar__
87
+ #define TORCH_NAME_RESOLVABLE __attribute__((roar_resolvable_by_name))
88
+ #else
89
+ #define TORCH_NAME_RESOLVABLE
90
+ #endif
91
+
92
+ // Semaphore define, declare and probe note format
93
+
94
+ #define TORCH_SDT_SEMAPHORE(provider, name) \
95
+ torch_sdt_semaphore_##provider##_##name
96
+
97
+ #define TORCH_SDT_DEFINE_SEMAPHORE(name) \
98
+ extern "C" { \
99
+ TORCH_NAME_RESOLVABLE \
100
+ volatile unsigned short TORCH_SDT_SEMAPHORE(pytorch, name) \
101
+ __attribute__((section(TORCH_SDT_SEMAPHORE_SECTION), used)) = 0; \
102
+ }
103
+
104
+ #define TORCH_SDT_DECLARE_SEMAPHORE(name) \
105
+ extern "C" TORCH_NAME_RESOLVABLE volatile unsigned short \
106
+ TORCH_SDT_SEMAPHORE(pytorch, name)
107
+
108
+ #define TORCH_SDT_SEMAPHORE_NOTE_0(provider, name) \
109
+ TORCH_SDT_ASM_1( TORCH_SDT_ASM_ADDR 0) /*No Semaphore*/ \
110
+
111
+ #define TORCH_SDT_SEMAPHORE_NOTE_1(provider, name) \
112
+ TORCH_SDT_ASM_1(TORCH_SDT_ASM_ADDR TORCH_SDT_SEMAPHORE(provider, name))
113
+
114
+ // Structure of note section for the probe.
115
+ #define TORCH_SDT_NOTE_CONTENT(provider, name, has_semaphore, arg_template) \
116
+ TORCH_SDT_ASM_1(990: TORCH_SDT_NOP) \
117
+ TORCH_SDT_ASM_3( .pushsection .note.stapsdt,"","note") \
118
+ TORCH_SDT_ASM_1( .balign 4) \
119
+ TORCH_SDT_ASM_3( .4byte 992f-991f, 994f-993f, TORCH_SDT_NOTE_TYPE) \
120
+ TORCH_SDT_ASM_1(991: .asciz TORCH_SDT_NOTE_NAME) \
121
+ TORCH_SDT_ASM_1(992: .balign 4) \
122
+ TORCH_SDT_ASM_1(993: TORCH_SDT_ASM_ADDR 990b) \
123
+ TORCH_SDT_ASM_1( TORCH_SDT_ASM_ADDR 0) /*Reserved for Base Address*/ \
124
+ TORCH_SDT_SEMAPHORE_NOTE_##has_semaphore(provider, name) \
125
+ TORCH_SDT_ASM_STRING(provider) \
126
+ TORCH_SDT_ASM_STRING(name) \
127
+ TORCH_SDT_ASM_STRING(arg_template) \
128
+ TORCH_SDT_ASM_1(994: .balign 4) \
129
+ TORCH_SDT_ASM_1( .popsection)
130
+
131
+ // Main probe Macro.
132
+ #define TORCH_SDT_PROBE(provider, name, has_semaphore, n, arglist) \
133
+ __asm__ __volatile__ ( \
134
+ TORCH_SDT_NOTE_CONTENT( \
135
+ provider, name, has_semaphore, TORCH_SDT_ARG_TEMPLATE_##n) \
136
+ :: TORCH_SDT_OPERANDS_##n arglist \
137
+ ) \
138
+
139
+ // Helper Macros to handle variadic arguments.
140
+ #define TORCH_SDT_NARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
141
+ #define TORCH_SDT_NARG(...) \
142
+ TORCH_SDT_NARG_(__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
143
+ #define TORCH_SDT_PROBE_N(provider, name, has_semaphore, N, ...) \
144
+ TORCH_SDT_PROBE(provider, name, has_semaphore, N, (__VA_ARGS__))