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 ///
61 /// Helper class for calculating the default number of inline elements for
62 /// `InlinedVector<T>`.
63 /// This produces the following on MSVC x64
64 /// int8_t -> 41
65 // int16_t -> 21
66 // int32_t -> 11
67 // int64_t -> 6
68 // std::string 40 -> 1
69 template <typename T>
71  // Parameter controlling the default number of inlined elements
72  // for `InlinedVector<T>`.
73  //
74  // The default number of inlined elements ensures that
75  // 1. There is at least one inlined element.
76  // 2. `sizeof(InlinedVector<T>) <= kPreferredInlinedVectorSizeof` unless
77  // it contradicts 1.
78  static constexpr size_t kPreferredInlinedVectorSizeof = 64;
79 
80  // static_assert that sizeof(T) is not "too big".
81  //
82  // Because the InlinedVector must have at least one inlined element, it is possible
83  // for an arbitrarily large inlined element to allocate an arbitrarily large
84  // amount of inline storage. So we want to call attention to these cases and
85  // make sure that users are making an intentional decision if they request a lot of inline storage.
86  //
87  // We want this assertion to trigger in pathological cases, but otherwise
88  // not be too easy to hit. To accomplish that, the cutoff is actually somewhat
89  // larger than kPreferredInlinedVectorSizeof (otherwise,
90  // `InlinedVector<InlinedVector<T>>` would be one easy way to trip it, and that
91  // pattern seems useful in practice).
92  //
93  // One wrinkle is that this assertion is in theory non-portable, since
94  // sizeof(absl::InlinedVector<T, 1>) is in general platform-dependent. However, we don't expect this
95  // to be much of an issue, because most LLVM development happens on 64-bit
96  // hosts, and therefore sizeof(T) is expected to *decrease* when compiled for
97  // 32-bit hosts, dodging the issue. The reverse situation, where development
98  // happens on a 32-bit host and then fails due to sizeof(T) *increasing* on a
99  // 64-bit host, is expected to be very rare.
100  static_assert(
101  sizeof(absl::InlinedVector<T, 1>) <= kPreferredInlinedVectorSizeof,
102  "You are trying to use a default number of inlined elements for "
103  "`InlinedVector<T>` but `sizeof(T)` is really big! Please use an "
104  "explicit number of inlined elements with `InlinedVector<T, N>` to make "
105  "sure you really want that much inline storage.");
106 
107  // Discount the size of the header itself when calculating the maximum inline
108  // bytes.
109  static constexpr size_t PreferredInlineBytes =
110  kPreferredInlinedVectorSizeof - (sizeof(absl::InlinedVector<T, 1>) - sizeof(T));
111  static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
112  static constexpr size_t value =
114 };
115 
116 // Use InlinedVector for small arrays that can fit on a stack with a default
117 // value pre-calculated.
118 // Use TensorShapeVector for shapes.
119 template <typename T,
121  typename Allocator = std::allocator<T>>
122 using InlinedVector = absl::InlinedVector<T, N, Allocator>;
123 
124 #else
125 
126 template <typename T,
127  size_t N = 0,
128  typename Allocator = std::allocator<T>>
129 using InlinedVector = std::vector<T, Allocator>;
130 
131 #endif // DISABLE_ABSEIL
132 
133 template <typename T,
134  typename Allocator = std::allocator<T>>
135 class InlinedHashSet;
136 
137 template <typename Key, typename Value,
138  typename Allocator = std::allocator<std::pair<const Key, Value>>>
139 class InlinedHashMap;
140 
141 template <typename T, typename Allocator = std::allocator<T>>
142 class NodeHashSet;
143 
144 template <typename Key, typename Value,
145  typename Allocator = std::allocator<std::pair<const Key, Value>>>
146 class NodeHashMap;
147 } // 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