HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inlined_containers_fwd.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <memory>
7 #include <utility>
8 
9 #ifndef DISABLE_ABSEIL
10 #ifdef _MSC_VER
11 #pragma warning(push)
12 // C4127: conditional expression is constant
13 #pragma warning(disable : 4127)
14 // C4324: structure was padded due to alignment specifier
15 // Usage of alignas causes some internal padding in places.
16 #pragma warning(disable : 4324)
17 #else
18 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102329#c2
19 #if !defined(__clang__) && defined(__GNUC__)
20 #pragma GCC diagnostic push
21 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
22 #endif
23 #endif // _MSC_VER
24 
25 #include <absl/container/inlined_vector.h>
26 
27 #ifdef _MSC_VER
28 #pragma warning(pop)
29 #else
30 #if !defined(__clang__) && defined(__GNUC__)
31 #pragma GCC diagnostic pop
32 #endif
33 #endif // _MSC_VER
34 
35 #else
36 
37 #include <vector>
38 
39 #endif // DISABLE_ABSEIL
40 
41 // Forward declarations for contexts where abseil can not be compiled and
42 // not really needed but we want to have it in the headers that are included
43 // e.g. CUDA 10 and .CU files
44 // InlinedVector seems to be fine with old CUDA
45 
46 //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
47 //
48 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49 // See https://llvm.org/LICENSE.txt for license information.
50 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
51 //
52 // This file contains code and comments derived from llvm/ADT/SmallVector.h
53 //
54 // Specifically CalculateInlinedVectorDefaultInlinedElements<T>() template is derived from
55 // CalculateSmallVectorDefaultInlinedElements<T>() and its comments.
56 
57 namespace onnxruntime {
58 #ifndef DISABLE_ABSEIL
59 /// Inspired by LLVM SmallVector with ONNX Runtime adjustments for abseil.
60 /// https://github.com/llvm/llvm-project/blob/a85b37d0ca819776c6034c2dbda2b21e54e3393a/llvm/include/llvm/ADT/SmallVector.h#L1128-L1179
61 ///
62 /// Helper class for calculating the default number of inline elements for
63 /// `InlinedVector<T>`.
64 /// This produces the following on MSVC x64
65 /// int8_t -> 41
66 // int16_t -> 21
67 // int32_t -> 11
68 // int64_t -> 6
69 // std::string 40 -> 1
70 template <typename T>
72  // Parameter controlling the default number of inlined elements
73  // for `InlinedVector<T>`.
74  //
75  // The default number of inlined elements ensures that
76  // 1. There is at least one inlined element.
77  // 2. `sizeof(InlinedVector<T>) <= kPreferredInlinedVectorSizeof` unless
78  // it contradicts 1.
79  static constexpr size_t kPreferredInlinedVectorSizeof = 64;
80 
81  // Largest allowed element size for default element count calculation.
82  static constexpr size_t kElementSizeCutoff = 256;
83 
84  // static_assert that sizeof(T) is not "too big".
85  //
86  // Because the InlinedVector must have at least one inlined element, it is possible
87  // for an arbitrarily large inlined element to allocate an arbitrarily large
88  // amount of inline storage. So we want to call attention to these cases and
89  // make sure that users are making an intentional decision if they request a lot of inline storage.
90  //
91  // We want this assertion to trigger in pathological cases, but otherwise
92  // not be too easy to hit. To accomplish that, the cutoff is actually somewhat
93  // larger than kPreferredInlinedVectorSizeof (otherwise,
94  // `InlinedVector<InlinedVector<T>>` would be one easy way to trip it, and that
95  // pattern seems useful in practice).
96  //
97  // One wrinkle is that this assertion is in theory non-portable, since
98  // sizeof(absl::InlinedVector<T, 1>) is in general platform-dependent. However, we don't expect this
99  // to be much of an issue, because most LLVM development happens on 64-bit
100  // hosts, and therefore sizeof(T) is expected to *decrease* when compiled for
101  // 32-bit hosts, dodging the issue. The reverse situation, where development
102  // happens on a 32-bit host and then fails due to sizeof(T) *increasing* on a
103  // 64-bit host, is expected to be very rare.
104  static_assert(
105  sizeof(T) <= kElementSizeCutoff,
106  "You are trying to use a default number of inlined elements for "
107  "`InlinedVector<T>` but `sizeof(T)` is really big! Please use an "
108  "explicit number of inlined elements with `InlinedVector<T, N>` to make "
109  "sure you really want that much inline storage.");
110 
111  // Discount the size of the header itself when calculating the maximum inline
112  // bytes.
113  static constexpr size_t InlinedVectorHeaderSize = sizeof(absl::InlinedVector<T, 1>) - sizeof(T);
115  static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
116  static constexpr size_t value =
118 };
119 
120 // Use InlinedVector for small arrays that can fit on a stack with a default
121 // value pre-calculated.
122 // Use TensorShapeVector for shapes.
123 template <typename T,
125  typename Allocator = std::allocator<T>>
126 using InlinedVector = absl::InlinedVector<T, N, Allocator>;
127 
128 #else
129 
130 template <typename T,
131  size_t N = 0,
132  typename Allocator = std::allocator<T>>
133 using InlinedVector = std::vector<T, Allocator>;
134 
135 #endif // DISABLE_ABSEIL
136 
137 template <typename T,
138  typename Allocator = std::allocator<T>>
139 class InlinedHashSet;
140 
141 template <typename Key, typename Value,
142  typename Allocator = std::allocator<std::pair<const Key, Value>>>
143 class InlinedHashMap;
144 
145 template <typename T, typename Allocator = std::allocator<T>>
146 class NodeHashSet;
147 
148 template <typename Key, typename Value,
149  typename Allocator = std::allocator<std::pair<const Key, Value>>>
150 class NodeHashMap;
151 } // namespace onnxruntime
A generic, discriminated value, whose type may be queried dynamically.
Definition: Value.h:44
absl::InlinedVector< T, N, Allocator > InlinedVector
GA_API const UT_StringHolder N
Definition: core.h:1131