nvfuser-cu121-torch25 0.2.25.dev20250201__cp312-cp312-manylinux_2_28_x86_64.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. nvfuser/_C.cpython-312-x86_64-linux-gnu.so +0 -0
  2. nvfuser/__init__.py +618 -0
  3. nvfuser/__init__.pyi +4 -0
  4. nvfuser/contrib/__init__.py +9 -0
  5. nvfuser/contrib/nn/__init__.py +13 -0
  6. nvfuser/contrib/nn/normalization.py +725 -0
  7. nvfuser/include/nvfuser/alias_analysis.h +116 -0
  8. nvfuser/include/nvfuser/bfs.h +929 -0
  9. nvfuser/include/nvfuser/codegen.h +26 -0
  10. nvfuser/include/nvfuser/compute_at.h +28 -0
  11. nvfuser/include/nvfuser/compute_at_map.h +394 -0
  12. nvfuser/include/nvfuser/contiguity.h +351 -0
  13. nvfuser/include/nvfuser/cuda_utils.h +50 -0
  14. nvfuser/include/nvfuser/debug.h +50 -0
  15. nvfuser/include/nvfuser/device_lower/analysis/bank_conflict.h +53 -0
  16. nvfuser/include/nvfuser/device_lower/analysis/circular_buffer.h +109 -0
  17. nvfuser/include/nvfuser/device_lower/analysis/device_version.h +65 -0
  18. nvfuser/include/nvfuser/device_lower/analysis/divisible_split.h +28 -0
  19. nvfuser/include/nvfuser/device_lower/analysis/fused_reduction.h +36 -0
  20. nvfuser/include/nvfuser/device_lower/analysis/index_compute.h +322 -0
  21. nvfuser/include/nvfuser/device_lower/analysis/predicate_elimination.h +71 -0
  22. nvfuser/include/nvfuser/device_lower/analysis/sync_information.h +47 -0
  23. nvfuser/include/nvfuser/device_lower/analysis/tensor_memory.h +65 -0
  24. nvfuser/include/nvfuser/device_lower/analysis/thread_predicate.h +158 -0
  25. nvfuser/include/nvfuser/device_lower/analysis/tma.h +93 -0
  26. nvfuser/include/nvfuser/device_lower/analysis/trivial_broadcast.h +75 -0
  27. nvfuser/include/nvfuser/device_lower/id_model_options.h +135 -0
  28. nvfuser/include/nvfuser/device_lower/lower2device.h +391 -0
  29. nvfuser/include/nvfuser/device_lower/pass/alias_memory.h +37 -0
  30. nvfuser/include/nvfuser/device_lower/pass/allocation.h +32 -0
  31. nvfuser/include/nvfuser/device_lower/pass/circular_buffer.h +191 -0
  32. nvfuser/include/nvfuser/device_lower/pass/expr_sort.h +17 -0
  33. nvfuser/include/nvfuser/device_lower/pass/fusion_simplifier.h +21 -0
  34. nvfuser/include/nvfuser/device_lower/pass/grid_serialization.h +26 -0
  35. nvfuser/include/nvfuser/device_lower/pass/index.h +200 -0
  36. nvfuser/include/nvfuser/device_lower/pass/inline_ptx.h +16 -0
  37. nvfuser/include/nvfuser/device_lower/pass/insert_syncs.h +39 -0
  38. nvfuser/include/nvfuser/device_lower/pass/instrument.h +24 -0
  39. nvfuser/include/nvfuser/device_lower/pass/loop_rotation.h +150 -0
  40. nvfuser/include/nvfuser/device_lower/pass/loops.h +68 -0
  41. nvfuser/include/nvfuser/device_lower/pass/magic_zero.h +86 -0
  42. nvfuser/include/nvfuser/device_lower/pass/misaligned_vectorization.h +118 -0
  43. nvfuser/include/nvfuser/device_lower/pass/predicate.h +23 -0
  44. nvfuser/include/nvfuser/device_lower/pass/replace_size.h +24 -0
  45. nvfuser/include/nvfuser/device_lower/pass/scalar_hoist.h +115 -0
  46. nvfuser/include/nvfuser/device_lower/pass/unroll.h +98 -0
  47. nvfuser/include/nvfuser/device_lower/pass/vectorize_welford.h +45 -0
  48. nvfuser/include/nvfuser/device_lower/pass/warp_reduce.h +23 -0
  49. nvfuser/include/nvfuser/device_lower/utils.h +382 -0
  50. nvfuser/include/nvfuser/device_lower/validation.h +74 -0
  51. nvfuser/include/nvfuser/disjoint_set.h +556 -0
  52. nvfuser/include/nvfuser/dispatch.h +334 -0
  53. nvfuser/include/nvfuser/driver_api.h +49 -0
  54. nvfuser/include/nvfuser/dynamic_transform.h +316 -0
  55. nvfuser/include/nvfuser/dynamic_type/C++20/type_traits +37 -0
  56. nvfuser/include/nvfuser/dynamic_type/dynamic_type.h +969 -0
  57. nvfuser/include/nvfuser/dynamic_type/error.h +24 -0
  58. nvfuser/include/nvfuser/dynamic_type/type_traits.h +703 -0
  59. nvfuser/include/nvfuser/evaluator_common.h +295 -0
  60. nvfuser/include/nvfuser/exceptions.h +283 -0
  61. nvfuser/include/nvfuser/expr_evaluator.h +125 -0
  62. nvfuser/include/nvfuser/expr_simplifier.h +218 -0
  63. nvfuser/include/nvfuser/flatbuffers/allocator.h +68 -0
  64. nvfuser/include/nvfuser/flatbuffers/array.h +253 -0
  65. nvfuser/include/nvfuser/flatbuffers/base.h +486 -0
  66. nvfuser/include/nvfuser/flatbuffers/buffer.h +154 -0
  67. nvfuser/include/nvfuser/flatbuffers/buffer_ref.h +53 -0
  68. nvfuser/include/nvfuser/flatbuffers/code_generator.h +80 -0
  69. nvfuser/include/nvfuser/flatbuffers/code_generators.h +234 -0
  70. nvfuser/include/nvfuser/flatbuffers/default_allocator.h +64 -0
  71. nvfuser/include/nvfuser/flatbuffers/detached_buffer.h +114 -0
  72. nvfuser/include/nvfuser/flatbuffers/flatbuffer_builder.h +1225 -0
  73. nvfuser/include/nvfuser/flatbuffers/flatbuffers.h +272 -0
  74. nvfuser/include/nvfuser/flatbuffers/flatc.h +130 -0
  75. nvfuser/include/nvfuser/flatbuffers/flex_flat_util.h +36 -0
  76. nvfuser/include/nvfuser/flatbuffers/flexbuffers.h +1889 -0
  77. nvfuser/include/nvfuser/flatbuffers/grpc.h +300 -0
  78. nvfuser/include/nvfuser/flatbuffers/hash.h +127 -0
  79. nvfuser/include/nvfuser/flatbuffers/idl.h +1359 -0
  80. nvfuser/include/nvfuser/flatbuffers/minireflect.h +420 -0
  81. nvfuser/include/nvfuser/flatbuffers/reflection.h +522 -0
  82. nvfuser/include/nvfuser/flatbuffers/reflection_generated.h +1471 -0
  83. nvfuser/include/nvfuser/flatbuffers/registry.h +128 -0
  84. nvfuser/include/nvfuser/flatbuffers/stl_emulation.h +513 -0
  85. nvfuser/include/nvfuser/flatbuffers/string.h +64 -0
  86. nvfuser/include/nvfuser/flatbuffers/struct.h +53 -0
  87. nvfuser/include/nvfuser/flatbuffers/table.h +168 -0
  88. nvfuser/include/nvfuser/flatbuffers/util.h +731 -0
  89. nvfuser/include/nvfuser/flatbuffers/vector.h +393 -0
  90. nvfuser/include/nvfuser/flatbuffers/vector_downward.h +273 -0
  91. nvfuser/include/nvfuser/flatbuffers/verifier.h +317 -0
  92. nvfuser/include/nvfuser/fusion.h +511 -0
  93. nvfuser/include/nvfuser/fusion_guard.h +37 -0
  94. nvfuser/include/nvfuser/fusion_profiler.h +311 -0
  95. nvfuser/include/nvfuser/fusion_segmenter.h +751 -0
  96. nvfuser/include/nvfuser/global_allocator.h +27 -0
  97. nvfuser/include/nvfuser/grouped_reduction.h +47 -0
  98. nvfuser/include/nvfuser/host_ir/container.h +60 -0
  99. nvfuser/include/nvfuser/host_ir/executor.h +152 -0
  100. nvfuser/include/nvfuser/host_ir/host_ir.h +320 -0
  101. nvfuser/include/nvfuser/host_ir/lower.h +35 -0
  102. nvfuser/include/nvfuser/id_model/circular_buffer_indexing.h +56 -0
  103. nvfuser/include/nvfuser/id_model/contiguity.h +166 -0
  104. nvfuser/include/nvfuser/id_model/id_model.h +359 -0
  105. nvfuser/include/nvfuser/id_model/id_model_index_compute.h +81 -0
  106. nvfuser/include/nvfuser/id_model/indexing.h +208 -0
  107. nvfuser/include/nvfuser/id_model/indexing_traversal.h +72 -0
  108. nvfuser/include/nvfuser/id_model/indexing_utils.h +62 -0
  109. nvfuser/include/nvfuser/id_model/loop_promotion.h +180 -0
  110. nvfuser/include/nvfuser/id_model/predicate_indexing.h +104 -0
  111. nvfuser/include/nvfuser/id_model/schedule.h +54 -0
  112. nvfuser/include/nvfuser/id_model/to_string.h +87 -0
  113. nvfuser/include/nvfuser/id_model/transform_replay.h +58 -0
  114. nvfuser/include/nvfuser/id_model/utils.h +176 -0
  115. nvfuser/include/nvfuser/id_model/validation_utils.h +55 -0
  116. nvfuser/include/nvfuser/index_compute.h +651 -0
  117. nvfuser/include/nvfuser/instrumentation.h +107 -0
  118. nvfuser/include/nvfuser/ir/all_nodes.h +14 -0
  119. nvfuser/include/nvfuser/ir/base_nodes.h +687 -0
  120. nvfuser/include/nvfuser/ir/builder.h +215 -0
  121. nvfuser/include/nvfuser/ir/builder_passkey.h +29 -0
  122. nvfuser/include/nvfuser/ir/cloner.h +185 -0
  123. nvfuser/include/nvfuser/ir/container.h +226 -0
  124. nvfuser/include/nvfuser/ir/graphviz.h +119 -0
  125. nvfuser/include/nvfuser/ir/interface_nodes.h +957 -0
  126. nvfuser/include/nvfuser/ir/internal_base_nodes.h +744 -0
  127. nvfuser/include/nvfuser/ir/internal_nodes.h +2792 -0
  128. nvfuser/include/nvfuser/ir/iostream.h +98 -0
  129. nvfuser/include/nvfuser/ir/printer.h +57 -0
  130. nvfuser/include/nvfuser/ir/utils.h +801 -0
  131. nvfuser/include/nvfuser/iter_visitor.h +661 -0
  132. nvfuser/include/nvfuser/kernel.h +299 -0
  133. nvfuser/include/nvfuser/kernel_db/kernel_db.h +109 -0
  134. nvfuser/include/nvfuser/kernel_db/utils.h +37 -0
  135. nvfuser/include/nvfuser/kernel_ir.h +1457 -0
  136. nvfuser/include/nvfuser/kernel_ir_dispatch.h +147 -0
  137. nvfuser/include/nvfuser/linked_hash_map.h +97 -0
  138. nvfuser/include/nvfuser/logical_domain_map.h +577 -0
  139. nvfuser/include/nvfuser/macros.h +23 -0
  140. nvfuser/include/nvfuser/mma_type.h +257 -0
  141. nvfuser/include/nvfuser/multidevice/c10d_mock.h +175 -0
  142. nvfuser/include/nvfuser/multidevice/communication.h +232 -0
  143. nvfuser/include/nvfuser/multidevice/communicator.h +179 -0
  144. nvfuser/include/nvfuser/multidevice/device_mesh.h +95 -0
  145. nvfuser/include/nvfuser/multidevice/executor.h +107 -0
  146. nvfuser/include/nvfuser/multidevice/multidevice.h +18 -0
  147. nvfuser/include/nvfuser/multidevice/utils.h +187 -0
  148. nvfuser/include/nvfuser/non_divisible_split.h +86 -0
  149. nvfuser/include/nvfuser/opaque_type.h +129 -0
  150. nvfuser/include/nvfuser/ops/alias.h +192 -0
  151. nvfuser/include/nvfuser/ops/all_ops.h +13 -0
  152. nvfuser/include/nvfuser/ops/arith.h +712 -0
  153. nvfuser/include/nvfuser/ops/composite.h +130 -0
  154. nvfuser/include/nvfuser/ops/indexing.h +55 -0
  155. nvfuser/include/nvfuser/ops/normalization.h +263 -0
  156. nvfuser/include/nvfuser/ops/utils.h +127 -0
  157. nvfuser/include/nvfuser/options.h +313 -0
  158. nvfuser/include/nvfuser/parallel_dimension_map.h +95 -0
  159. nvfuser/include/nvfuser/parallel_type_bitmap.h +365 -0
  160. nvfuser/include/nvfuser/polymorphic_value.h +432 -0
  161. nvfuser/include/nvfuser/predicate_compute.h +213 -0
  162. nvfuser/include/nvfuser/python_frontend/distributed_tensor.h +50 -0
  163. nvfuser/include/nvfuser/python_frontend/fusion_cache.h +298 -0
  164. nvfuser/include/nvfuser/python_frontend/fusion_definition.h +372 -0
  165. nvfuser/include/nvfuser/python_frontend/fusion_record.h +3124 -0
  166. nvfuser/include/nvfuser/python_frontend/fusion_state.h +143 -0
  167. nvfuser/include/nvfuser/python_frontend/python_bindings.h +27 -0
  168. nvfuser/include/nvfuser/python_frontend/segmentation.h +246 -0
  169. nvfuser/include/nvfuser/python_frontend/translation.h +20 -0
  170. nvfuser/include/nvfuser/python_frontend/translation_utils.h +308 -0
  171. nvfuser/include/nvfuser/scheduler/all_schedulers.h +17 -0
  172. nvfuser/include/nvfuser/scheduler/ampere_multi_matmul.h +206 -0
  173. nvfuser/include/nvfuser/scheduler/cache_policy_refiner.h +19 -0
  174. nvfuser/include/nvfuser/scheduler/compile_time_info.h +322 -0
  175. nvfuser/include/nvfuser/scheduler/debug_utils.h +68 -0
  176. nvfuser/include/nvfuser/scheduler/expr_eval_sched.h +45 -0
  177. nvfuser/include/nvfuser/scheduler/heuristic.h +113 -0
  178. nvfuser/include/nvfuser/scheduler/hopper_multi_matmul.h +204 -0
  179. nvfuser/include/nvfuser/scheduler/mark_aliases.h +19 -0
  180. nvfuser/include/nvfuser/scheduler/matmul.h +40 -0
  181. nvfuser/include/nvfuser/scheduler/matmul_heuristic.h +293 -0
  182. nvfuser/include/nvfuser/scheduler/matmul_heuristic_plugin.h +65 -0
  183. nvfuser/include/nvfuser/scheduler/matmul_heuristic_plugin_api.h +99 -0
  184. nvfuser/include/nvfuser/scheduler/matmul_utils.h +54 -0
  185. nvfuser/include/nvfuser/scheduler/mma_utils.h +500 -0
  186. nvfuser/include/nvfuser/scheduler/multi_matmul.h +74 -0
  187. nvfuser/include/nvfuser/scheduler/no_op.h +48 -0
  188. nvfuser/include/nvfuser/scheduler/normalization_inner.h +49 -0
  189. nvfuser/include/nvfuser/scheduler/normalization_inner_outer.h +51 -0
  190. nvfuser/include/nvfuser/scheduler/normalization_outer.h +48 -0
  191. nvfuser/include/nvfuser/scheduler/normalization_utils.h +379 -0
  192. nvfuser/include/nvfuser/scheduler/pointwise.h +183 -0
  193. nvfuser/include/nvfuser/scheduler/pointwise_heuristic.h +118 -0
  194. nvfuser/include/nvfuser/scheduler/pointwise_utils.h +24 -0
  195. nvfuser/include/nvfuser/scheduler/reduction.h +43 -0
  196. nvfuser/include/nvfuser/scheduler/reduction_heuristic.h +339 -0
  197. nvfuser/include/nvfuser/scheduler/reduction_utils.h +159 -0
  198. nvfuser/include/nvfuser/scheduler/registry.h +97 -0
  199. nvfuser/include/nvfuser/scheduler/registry_utils.h +111 -0
  200. nvfuser/include/nvfuser/scheduler/resize.h +41 -0
  201. nvfuser/include/nvfuser/scheduler/resize_heuristic.h +67 -0
  202. nvfuser/include/nvfuser/scheduler/runtime_info.h +166 -0
  203. nvfuser/include/nvfuser/scheduler/scheduler_types.h +80 -0
  204. nvfuser/include/nvfuser/scheduler/transpose.h +114 -0
  205. nvfuser/include/nvfuser/scheduler/transpose_heuristic.h +164 -0
  206. nvfuser/include/nvfuser/scheduler/utils.h +771 -0
  207. nvfuser/include/nvfuser/scheduler/vectorize_helper.h +349 -0
  208. nvfuser/include/nvfuser/serde/factory.h +55 -0
  209. nvfuser/include/nvfuser/serde/fusion_cache_generated.h +4319 -0
  210. nvfuser/include/nvfuser/serde/fusion_record.h +124 -0
  211. nvfuser/include/nvfuser/serde/polymorphic_value.h +52 -0
  212. nvfuser/include/nvfuser/serde/utils.h +34 -0
  213. nvfuser/include/nvfuser/struct.inl +127 -0
  214. nvfuser/include/nvfuser/swizzle.h +54 -0
  215. nvfuser/include/nvfuser/sys_utils.h +40 -0
  216. nvfuser/include/nvfuser/tensor_metadata.h +118 -0
  217. nvfuser/include/nvfuser/tma.h +124 -0
  218. nvfuser/include/nvfuser/transform_iter.h +522 -0
  219. nvfuser/include/nvfuser/transform_replay.h +297 -0
  220. nvfuser/include/nvfuser/transform_rfactor.h +33 -0
  221. nvfuser/include/nvfuser/transform_view.h +136 -0
  222. nvfuser/include/nvfuser/type.h +1125 -0
  223. nvfuser/include/nvfuser/type_promotion.h +61 -0
  224. nvfuser/include/nvfuser/utils.h +619 -0
  225. nvfuser/include/nvfuser/val_graph.h +446 -0
  226. nvfuser/include/nvfuser/val_graph_visitor.h +259 -0
  227. nvfuser/include/nvfuser/validator_utils.h +92 -0
  228. nvfuser/include/nvfuser/vectorization_info.h +31 -0
  229. nvfuser/include/nvfuser/visibility.h +21 -0
  230. nvfuser/lib/libnvfuser_codegen.so +0 -0
  231. nvfuser/nvfuser_version.py +69 -0
  232. nvfuser/pytorch_utils.py +184 -0
  233. nvfuser/share/cmake/nvfuser/NvfuserConfig-release.cmake +20 -0
  234. nvfuser/share/cmake/nvfuser/NvfuserConfig.cmake +106 -0
  235. nvfuser/utils.py +18 -0
  236. nvfuser/version.py +1 -0
  237. nvfuser_cu121_torch25-0.2.25.dev20250201.dist-info/LICENSE +976 -0
  238. nvfuser_cu121_torch25-0.2.25.dev20250201.dist-info/METADATA +16 -0
  239. nvfuser_cu121_torch25-0.2.25.dev20250201.dist-info/RECORD +242 -0
  240. nvfuser_cu121_torch25-0.2.25.dev20250201.dist-info/WHEEL +5 -0
  241. nvfuser_cu121_torch25-0.2.25.dev20250201.dist-info/top_level.txt +1 -0
  242. nvfuser_cu121_torch25.libs/libnvToolsExt-847d78f2.so.1.0.0 +0 -0
@@ -0,0 +1,522 @@
1
+ /*
2
+ * Copyright 2015 Google Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #ifndef FLATBUFFERS_REFLECTION_H_
18
+ #define FLATBUFFERS_REFLECTION_H_
19
+
20
+ // This is somewhat of a circular dependency because flatc (and thus this
21
+ // file) is needed to generate this header in the first place.
22
+ // Should normally not be a problem since it can be generated by the
23
+ // previous version of flatc whenever this code needs to change.
24
+ // See scripts/generate_code.py for generation.
25
+ #include "flatbuffers/reflection_generated.h"
26
+
27
+ // Helper functionality for reflection.
28
+
29
+ namespace flatbuffers {
30
+
31
+ // ------------------------- GETTERS -------------------------
32
+
33
+ inline bool IsScalar(reflection::BaseType t) {
34
+ return t >= reflection::UType && t <= reflection::Double;
35
+ }
36
+ inline bool IsInteger(reflection::BaseType t) {
37
+ return t >= reflection::UType && t <= reflection::ULong;
38
+ }
39
+ inline bool IsFloat(reflection::BaseType t) {
40
+ return t == reflection::Float || t == reflection::Double;
41
+ }
42
+ inline bool IsLong(reflection::BaseType t) {
43
+ return t == reflection::Long || t == reflection::ULong;
44
+ }
45
+
46
+ // Size of a basic type, don't use with structs.
47
+ inline size_t GetTypeSize(reflection::BaseType base_type) {
48
+ // This needs to correspond to the BaseType enum.
49
+ static size_t sizes[] = {
50
+ 0, // None
51
+ 1, // UType
52
+ 1, // Bool
53
+ 1, // Byte
54
+ 1, // UByte
55
+ 2, // Short
56
+ 2, // UShort
57
+ 4, // Int
58
+ 4, // UInt
59
+ 8, // Long
60
+ 8, // ULong
61
+ 4, // Float
62
+ 8, // Double
63
+ 4, // String
64
+ 4, // Vector
65
+ 4, // Obj
66
+ 4, // Union
67
+ 0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds
68
+ // errors.
69
+
70
+ 0 // MaxBaseType. This must be kept the last entry in this array.
71
+ };
72
+ static_assert(sizeof(sizes) / sizeof(size_t) == reflection::MaxBaseType + 1,
73
+ "Size of sizes[] array does not match the count of BaseType "
74
+ "enum values.");
75
+ return sizes[base_type];
76
+ }
77
+
78
+ // Same as above, but now correctly returns the size of a struct if
79
+ // the field (or vector element) is a struct.
80
+ inline size_t GetTypeSizeInline(reflection::BaseType base_type, int type_index,
81
+ const reflection::Schema &schema) {
82
+ if (base_type == reflection::Obj &&
83
+ schema.objects()->Get(type_index)->is_struct()) {
84
+ return schema.objects()->Get(type_index)->bytesize();
85
+ } else {
86
+ return GetTypeSize(base_type);
87
+ }
88
+ }
89
+
90
+ // Get the root, regardless of what type it is.
91
+ inline Table *GetAnyRoot(uint8_t *const flatbuf) {
92
+ return GetMutableRoot<Table>(flatbuf);
93
+ }
94
+
95
+ inline const Table *GetAnyRoot(const uint8_t *const flatbuf) {
96
+ return GetRoot<Table>(flatbuf);
97
+ }
98
+
99
+ inline Table *GetAnySizePrefixedRoot(uint8_t *const flatbuf) {
100
+ return GetMutableSizePrefixedRoot<Table>(flatbuf);
101
+ }
102
+
103
+ inline const Table *GetAnySizePrefixedRoot(const uint8_t *const flatbuf) {
104
+ return GetSizePrefixedRoot<Table>(flatbuf);
105
+ }
106
+
107
+ // Get a field's default, if you know it's an integer, and its exact type.
108
+ template<typename T> T GetFieldDefaultI(const reflection::Field &field) {
109
+ FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
110
+ return static_cast<T>(field.default_integer());
111
+ }
112
+
113
+ // Get a field's default, if you know it's floating point and its exact type.
114
+ template<typename T> T GetFieldDefaultF(const reflection::Field &field) {
115
+ FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
116
+ return static_cast<T>(field.default_real());
117
+ }
118
+
119
+ // Get a field, if you know it's an integer, and its exact type.
120
+ template<typename T>
121
+ T GetFieldI(const Table &table, const reflection::Field &field) {
122
+ FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
123
+ return table.GetField<T>(field.offset(),
124
+ static_cast<T>(field.default_integer()));
125
+ }
126
+
127
+ // Get a field, if you know it's floating point and its exact type.
128
+ template<typename T>
129
+ T GetFieldF(const Table &table, const reflection::Field &field) {
130
+ FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
131
+ return table.GetField<T>(field.offset(),
132
+ static_cast<T>(field.default_real()));
133
+ }
134
+
135
+ // Get a field, if you know it's a string.
136
+ inline const String *GetFieldS(const Table &table,
137
+ const reflection::Field &field) {
138
+ FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::String);
139
+ return table.GetPointer<const String *>(field.offset());
140
+ }
141
+
142
+ // Get a field, if you know it's a vector.
143
+ template<typename T>
144
+ Vector<T> *GetFieldV(const Table &table, const reflection::Field &field) {
145
+ FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Vector &&
146
+ sizeof(T) == GetTypeSize(field.type()->element()));
147
+ return table.GetPointer<Vector<T> *>(field.offset());
148
+ }
149
+
150
+ // Get a field, if you know it's a vector, generically.
151
+ // To actually access elements, use the return value together with
152
+ // field.type()->element() in any of GetAnyVectorElemI below etc.
153
+ inline VectorOfAny *GetFieldAnyV(const Table &table,
154
+ const reflection::Field &field) {
155
+ return table.GetPointer<VectorOfAny *>(field.offset());
156
+ }
157
+
158
+ // Get a field, if you know it's a table.
159
+ inline Table *GetFieldT(const Table &table, const reflection::Field &field) {
160
+ FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj ||
161
+ field.type()->base_type() == reflection::Union);
162
+ return table.GetPointer<Table *>(field.offset());
163
+ }
164
+
165
+ // Get a field, if you know it's a struct.
166
+ inline const Struct *GetFieldStruct(const Table &table,
167
+ const reflection::Field &field) {
168
+ // TODO: This does NOT check if the field is a table or struct, but we'd need
169
+ // access to the schema to check the is_struct flag.
170
+ FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj);
171
+ return table.GetStruct<const Struct *>(field.offset());
172
+ }
173
+
174
+ // Get a structure's field, if you know it's a struct.
175
+ inline const Struct *GetFieldStruct(const Struct &structure,
176
+ const reflection::Field &field) {
177
+ FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj);
178
+ return structure.GetStruct<const Struct *>(field.offset());
179
+ }
180
+
181
+ // Raw helper functions used below: get any value in memory as a 64bit int, a
182
+ // double or a string.
183
+ // All scalars get static_cast to an int64_t, strings use strtoull, every other
184
+ // data type returns 0.
185
+ int64_t GetAnyValueI(reflection::BaseType type, const uint8_t *data);
186
+ // All scalars static cast to double, strings use strtod, every other data
187
+ // type is 0.0.
188
+ double GetAnyValueF(reflection::BaseType type, const uint8_t *data);
189
+ // All scalars converted using stringstream, strings as-is, and all other
190
+ // data types provide some level of debug-pretty-printing.
191
+ std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
192
+ const reflection::Schema *schema, int type_index);
193
+
194
+ // Get any table field as a 64bit int, regardless of what type it is.
195
+ inline int64_t GetAnyFieldI(const Table &table,
196
+ const reflection::Field &field) {
197
+ auto field_ptr = table.GetAddressOf(field.offset());
198
+ return field_ptr ? GetAnyValueI(field.type()->base_type(), field_ptr)
199
+ : field.default_integer();
200
+ }
201
+
202
+ // Get any table field as a double, regardless of what type it is.
203
+ inline double GetAnyFieldF(const Table &table, const reflection::Field &field) {
204
+ auto field_ptr = table.GetAddressOf(field.offset());
205
+ return field_ptr ? GetAnyValueF(field.type()->base_type(), field_ptr)
206
+ : field.default_real();
207
+ }
208
+
209
+ // Get any table field as a string, regardless of what type it is.
210
+ // You may pass nullptr for the schema if you don't care to have fields that
211
+ // are of table type pretty-printed.
212
+ inline std::string GetAnyFieldS(const Table &table,
213
+ const reflection::Field &field,
214
+ const reflection::Schema *schema) {
215
+ auto field_ptr = table.GetAddressOf(field.offset());
216
+ return field_ptr ? GetAnyValueS(field.type()->base_type(), field_ptr, schema,
217
+ field.type()->index())
218
+ : "";
219
+ }
220
+
221
+ // Get any struct field as a 64bit int, regardless of what type it is.
222
+ inline int64_t GetAnyFieldI(const Struct &st, const reflection::Field &field) {
223
+ return GetAnyValueI(field.type()->base_type(),
224
+ st.GetAddressOf(field.offset()));
225
+ }
226
+
227
+ // Get any struct field as a double, regardless of what type it is.
228
+ inline double GetAnyFieldF(const Struct &st, const reflection::Field &field) {
229
+ return GetAnyValueF(field.type()->base_type(),
230
+ st.GetAddressOf(field.offset()));
231
+ }
232
+
233
+ // Get any struct field as a string, regardless of what type it is.
234
+ inline std::string GetAnyFieldS(const Struct &st,
235
+ const reflection::Field &field) {
236
+ return GetAnyValueS(field.type()->base_type(),
237
+ st.GetAddressOf(field.offset()), nullptr, -1);
238
+ }
239
+
240
+ // Get any vector element as a 64bit int, regardless of what type it is.
241
+ inline int64_t GetAnyVectorElemI(const VectorOfAny *vec,
242
+ reflection::BaseType elem_type, size_t i) {
243
+ return GetAnyValueI(elem_type, vec->Data() + GetTypeSize(elem_type) * i);
244
+ }
245
+
246
+ // Get any vector element as a double, regardless of what type it is.
247
+ inline double GetAnyVectorElemF(const VectorOfAny *vec,
248
+ reflection::BaseType elem_type, size_t i) {
249
+ return GetAnyValueF(elem_type, vec->Data() + GetTypeSize(elem_type) * i);
250
+ }
251
+
252
+ // Get any vector element as a string, regardless of what type it is.
253
+ inline std::string GetAnyVectorElemS(const VectorOfAny *vec,
254
+ reflection::BaseType elem_type, size_t i) {
255
+ return GetAnyValueS(elem_type, vec->Data() + GetTypeSize(elem_type) * i,
256
+ nullptr, -1);
257
+ }
258
+
259
+ // Get a vector element that's a table/string/vector from a generic vector.
260
+ // Pass Table/String/VectorOfAny as template parameter.
261
+ // Warning: does no typechecking.
262
+ template<typename T>
263
+ T *GetAnyVectorElemPointer(const VectorOfAny *vec, size_t i) {
264
+ auto elem_ptr = vec->Data() + sizeof(uoffset_t) * i;
265
+ return reinterpret_cast<T *>(elem_ptr + ReadScalar<uoffset_t>(elem_ptr));
266
+ }
267
+
268
+ // Get the inline-address of a vector element. Useful for Structs (pass Struct
269
+ // as template arg), or being able to address a range of scalars in-line.
270
+ // Get elem_size from GetTypeSizeInline().
271
+ // Note: little-endian data on all platforms, use EndianScalar() instead of
272
+ // raw pointer access with scalars).
273
+ template<typename T>
274
+ T *GetAnyVectorElemAddressOf(const VectorOfAny *vec, size_t i,
275
+ size_t elem_size) {
276
+ return reinterpret_cast<T *>(vec->Data() + elem_size * i);
277
+ }
278
+
279
+ // Similarly, for elements of tables.
280
+ template<typename T>
281
+ T *GetAnyFieldAddressOf(const Table &table, const reflection::Field &field) {
282
+ return reinterpret_cast<T *>(table.GetAddressOf(field.offset()));
283
+ }
284
+
285
+ // Similarly, for elements of structs.
286
+ template<typename T>
287
+ T *GetAnyFieldAddressOf(const Struct &st, const reflection::Field &field) {
288
+ return reinterpret_cast<T *>(st.GetAddressOf(field.offset()));
289
+ }
290
+
291
+ // Loop over all the fields of the provided `object` and call `func` on each one
292
+ // in increasing order by their field->id(). If `reverse` is true, `func` is
293
+ // called in descending order
294
+ void ForAllFields(const reflection::Object *object, bool reverse,
295
+ std::function<void(const reflection::Field *)> func);
296
+
297
+ // ------------------------- SETTERS -------------------------
298
+
299
+ // Set any scalar field, if you know its exact type.
300
+ template<typename T>
301
+ bool SetField(Table *table, const reflection::Field &field, T val) {
302
+ reflection::BaseType type = field.type()->base_type();
303
+ if (!IsScalar(type)) { return false; }
304
+ FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(type));
305
+ T def;
306
+ if (IsInteger(type)) {
307
+ def = GetFieldDefaultI<T>(field);
308
+ } else {
309
+ FLATBUFFERS_ASSERT(IsFloat(type));
310
+ def = GetFieldDefaultF<T>(field);
311
+ }
312
+ return table->SetField(field.offset(), val, def);
313
+ }
314
+
315
+ // Raw helper functions used below: set any value in memory as a 64bit int, a
316
+ // double or a string.
317
+ // These work for all scalar values, but do nothing for other data types.
318
+ // To set a string, see SetString below.
319
+ void SetAnyValueI(reflection::BaseType type, uint8_t *data, int64_t val);
320
+ void SetAnyValueF(reflection::BaseType type, uint8_t *data, double val);
321
+ void SetAnyValueS(reflection::BaseType type, uint8_t *data, const char *val);
322
+
323
+ // Set any table field as a 64bit int, regardless of type what it is.
324
+ inline bool SetAnyFieldI(Table *table, const reflection::Field &field,
325
+ int64_t val) {
326
+ auto field_ptr = table->GetAddressOf(field.offset());
327
+ if (!field_ptr) return val == GetFieldDefaultI<int64_t>(field);
328
+ SetAnyValueI(field.type()->base_type(), field_ptr, val);
329
+ return true;
330
+ }
331
+
332
+ // Set any table field as a double, regardless of what type it is.
333
+ inline bool SetAnyFieldF(Table *table, const reflection::Field &field,
334
+ double val) {
335
+ auto field_ptr = table->GetAddressOf(field.offset());
336
+ if (!field_ptr) return val == GetFieldDefaultF<double>(field);
337
+ SetAnyValueF(field.type()->base_type(), field_ptr, val);
338
+ return true;
339
+ }
340
+
341
+ // Set any table field as a string, regardless of what type it is.
342
+ inline bool SetAnyFieldS(Table *table, const reflection::Field &field,
343
+ const char *val) {
344
+ auto field_ptr = table->GetAddressOf(field.offset());
345
+ if (!field_ptr) return false;
346
+ SetAnyValueS(field.type()->base_type(), field_ptr, val);
347
+ return true;
348
+ }
349
+
350
+ // Set any struct field as a 64bit int, regardless of type what it is.
351
+ inline void SetAnyFieldI(Struct *st, const reflection::Field &field,
352
+ int64_t val) {
353
+ SetAnyValueI(field.type()->base_type(), st->GetAddressOf(field.offset()),
354
+ val);
355
+ }
356
+
357
+ // Set any struct field as a double, regardless of type what it is.
358
+ inline void SetAnyFieldF(Struct *st, const reflection::Field &field,
359
+ double val) {
360
+ SetAnyValueF(field.type()->base_type(), st->GetAddressOf(field.offset()),
361
+ val);
362
+ }
363
+
364
+ // Set any struct field as a string, regardless of type what it is.
365
+ inline void SetAnyFieldS(Struct *st, const reflection::Field &field,
366
+ const char *val) {
367
+ SetAnyValueS(field.type()->base_type(), st->GetAddressOf(field.offset()),
368
+ val);
369
+ }
370
+
371
+ // Set any vector element as a 64bit int, regardless of type what it is.
372
+ inline void SetAnyVectorElemI(VectorOfAny *vec, reflection::BaseType elem_type,
373
+ size_t i, int64_t val) {
374
+ SetAnyValueI(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
375
+ }
376
+
377
+ // Set any vector element as a double, regardless of type what it is.
378
+ inline void SetAnyVectorElemF(VectorOfAny *vec, reflection::BaseType elem_type,
379
+ size_t i, double val) {
380
+ SetAnyValueF(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
381
+ }
382
+
383
+ // Set any vector element as a string, regardless of type what it is.
384
+ inline void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type,
385
+ size_t i, const char *val) {
386
+ SetAnyValueS(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
387
+ }
388
+
389
+ // ------------------------- RESIZING SETTERS -------------------------
390
+
391
+ // "smart" pointer for use with resizing vectors: turns a pointer inside
392
+ // a vector into a relative offset, such that it is not affected by resizes.
393
+ template<typename T, typename U> class pointer_inside_vector {
394
+ public:
395
+ pointer_inside_vector(T *ptr, std::vector<U> &vec)
396
+ : offset_(reinterpret_cast<uint8_t *>(ptr) -
397
+ reinterpret_cast<uint8_t *>(vec.data())),
398
+ vec_(vec) {}
399
+
400
+ T *operator*() const {
401
+ return reinterpret_cast<T *>(reinterpret_cast<uint8_t *>(vec_.data()) +
402
+ offset_);
403
+ }
404
+ T *operator->() const { return operator*(); }
405
+
406
+ private:
407
+ size_t offset_;
408
+ std::vector<U> &vec_;
409
+ };
410
+
411
+ // Helper to create the above easily without specifying template args.
412
+ template<typename T, typename U>
413
+ pointer_inside_vector<T, U> piv(T *ptr, std::vector<U> &vec) {
414
+ return pointer_inside_vector<T, U>(ptr, vec);
415
+ }
416
+
417
+ inline const char *UnionTypeFieldSuffix() { return "_type"; }
418
+
419
+ // Helper to figure out the actual table type a union refers to.
420
+ inline const reflection::Object &GetUnionType(
421
+ const reflection::Schema &schema, const reflection::Object &parent,
422
+ const reflection::Field &unionfield, const Table &table) {
423
+ auto enumdef = schema.enums()->Get(unionfield.type()->index());
424
+ // TODO: this is clumsy and slow, but no other way to find it?
425
+ auto type_field = parent.fields()->LookupByKey(
426
+ (unionfield.name()->str() + UnionTypeFieldSuffix()).c_str());
427
+ FLATBUFFERS_ASSERT(type_field);
428
+ auto union_type = GetFieldI<uint8_t>(table, *type_field);
429
+ auto enumval = enumdef->values()->LookupByKey(union_type);
430
+ return *schema.objects()->Get(enumval->union_type()->index());
431
+ }
432
+
433
+ // Changes the contents of a string inside a FlatBuffer. FlatBuffer must
434
+ // live inside a std::vector so we can resize the buffer if needed.
435
+ // "str" must live inside "flatbuf" and may be invalidated after this call.
436
+ // If your FlatBuffer's root table is not the schema's root table, you should
437
+ // pass in your root_table type as well.
438
+ void SetString(const reflection::Schema &schema, const std::string &val,
439
+ const String *str, std::vector<uint8_t> *flatbuf,
440
+ const reflection::Object *root_table = nullptr);
441
+
442
+ // Resizes a flatbuffers::Vector inside a FlatBuffer. FlatBuffer must
443
+ // live inside a std::vector so we can resize the buffer if needed.
444
+ // "vec" must live inside "flatbuf" and may be invalidated after this call.
445
+ // If your FlatBuffer's root table is not the schema's root table, you should
446
+ // pass in your root_table type as well.
447
+ uint8_t *ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize,
448
+ const VectorOfAny *vec, uoffset_t num_elems,
449
+ uoffset_t elem_size, std::vector<uint8_t> *flatbuf,
450
+ const reflection::Object *root_table = nullptr);
451
+
452
+ template<typename T>
453
+ void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val,
454
+ const Vector<T> *vec, std::vector<uint8_t> *flatbuf,
455
+ const reflection::Object *root_table = nullptr) {
456
+ auto delta_elem = static_cast<int>(newsize) - static_cast<int>(vec->size());
457
+ auto newelems = ResizeAnyVector(
458
+ schema, newsize, reinterpret_cast<const VectorOfAny *>(vec), vec->size(),
459
+ static_cast<uoffset_t>(sizeof(T)), flatbuf, root_table);
460
+ // Set new elements to "val".
461
+ for (int i = 0; i < delta_elem; i++) {
462
+ auto loc = newelems + i * sizeof(T);
463
+ auto is_scalar = flatbuffers::is_scalar<T>::value;
464
+ if (is_scalar) {
465
+ WriteScalar(loc, val);
466
+ } else { // struct
467
+ *reinterpret_cast<T *>(loc) = val;
468
+ }
469
+ }
470
+ }
471
+
472
+ // Adds any new data (in the form of a new FlatBuffer) to an existing
473
+ // FlatBuffer. This can be used when any of the above methods are not
474
+ // sufficient, in particular for adding new tables and new fields.
475
+ // This is potentially slightly less efficient than a FlatBuffer constructed
476
+ // in one piece, since the new FlatBuffer doesn't share any vtables with the
477
+ // existing one.
478
+ // The return value can now be set using Vector::MutateOffset or SetFieldT
479
+ // below.
480
+ const uint8_t *AddFlatBuffer(std::vector<uint8_t> &flatbuf,
481
+ const uint8_t *newbuf, size_t newlen);
482
+
483
+ inline bool SetFieldT(Table *table, const reflection::Field &field,
484
+ const uint8_t *val) {
485
+ FLATBUFFERS_ASSERT(sizeof(uoffset_t) ==
486
+ GetTypeSize(field.type()->base_type()));
487
+ return table->SetPointer(field.offset(), val);
488
+ }
489
+
490
+ // ------------------------- COPYING -------------------------
491
+
492
+ // Generic copying of tables from a FlatBuffer into a FlatBuffer builder.
493
+ // Can be used to do any kind of merging/selecting you may want to do out
494
+ // of existing buffers. Also useful to reconstruct a whole buffer if the
495
+ // above resizing functionality has introduced garbage in a buffer you want
496
+ // to remove.
497
+ // Note: this does not deal with DAGs correctly. If the table passed forms a
498
+ // DAG, the copy will be a tree instead (with duplicates). Strings can be
499
+ // shared however, by passing true for use_string_pooling.
500
+
501
+ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
502
+ const reflection::Schema &schema,
503
+ const reflection::Object &objectdef,
504
+ const Table &table,
505
+ bool use_string_pooling = false);
506
+
507
+ // Verifies the provided flatbuffer using reflection.
508
+ // root should point to the root type for this flatbuffer.
509
+ // buf should point to the start of flatbuffer data.
510
+ // length specifies the size of the flatbuffer data.
511
+ bool Verify(const reflection::Schema &schema, const reflection::Object &root,
512
+ const uint8_t *buf, size_t length, uoffset_t max_depth = 64,
513
+ uoffset_t max_tables = 1000000);
514
+
515
+ bool VerifySizePrefixed(const reflection::Schema &schema,
516
+ const reflection::Object &root, const uint8_t *buf,
517
+ size_t length, uoffset_t max_depth = 64,
518
+ uoffset_t max_tables = 1000000);
519
+
520
+ } // namespace flatbuffers
521
+
522
+ #endif // FLATBUFFERS_REFLECTION_H_