inference-models 0.18.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. inference_models/__init__.py +36 -0
  2. inference_models/configuration.py +72 -0
  3. inference_models/constants.py +2 -0
  4. inference_models/entities.py +5 -0
  5. inference_models/errors.py +137 -0
  6. inference_models/logger.py +52 -0
  7. inference_models/model_pipelines/__init__.py +0 -0
  8. inference_models/model_pipelines/auto_loaders/__init__.py +0 -0
  9. inference_models/model_pipelines/auto_loaders/core.py +120 -0
  10. inference_models/model_pipelines/auto_loaders/pipelines_registry.py +36 -0
  11. inference_models/model_pipelines/face_and_gaze_detection/__init__.py +0 -0
  12. inference_models/model_pipelines/face_and_gaze_detection/mediapipe_l2cs.py +200 -0
  13. inference_models/models/__init__.py +0 -0
  14. inference_models/models/auto_loaders/__init__.py +0 -0
  15. inference_models/models/auto_loaders/access_manager.py +168 -0
  16. inference_models/models/auto_loaders/auto_negotiation.py +1329 -0
  17. inference_models/models/auto_loaders/auto_resolution_cache.py +129 -0
  18. inference_models/models/auto_loaders/constants.py +7 -0
  19. inference_models/models/auto_loaders/core.py +1341 -0
  20. inference_models/models/auto_loaders/dependency_models.py +52 -0
  21. inference_models/models/auto_loaders/entities.py +57 -0
  22. inference_models/models/auto_loaders/models_registry.py +497 -0
  23. inference_models/models/auto_loaders/presentation_utils.py +333 -0
  24. inference_models/models/auto_loaders/ranking.py +413 -0
  25. inference_models/models/auto_loaders/utils.py +31 -0
  26. inference_models/models/base/__init__.py +0 -0
  27. inference_models/models/base/classification.py +123 -0
  28. inference_models/models/base/depth_estimation.py +62 -0
  29. inference_models/models/base/documents_parsing.py +111 -0
  30. inference_models/models/base/embeddings.py +66 -0
  31. inference_models/models/base/instance_segmentation.py +87 -0
  32. inference_models/models/base/keypoints_detection.py +93 -0
  33. inference_models/models/base/object_detection.py +143 -0
  34. inference_models/models/base/semantic_segmentation.py +74 -0
  35. inference_models/models/base/types.py +5 -0
  36. inference_models/models/clip/__init__.py +0 -0
  37. inference_models/models/clip/clip_onnx.py +148 -0
  38. inference_models/models/clip/clip_pytorch.py +104 -0
  39. inference_models/models/clip/preprocessing.py +162 -0
  40. inference_models/models/common/__init__.py +0 -0
  41. inference_models/models/common/cuda.py +30 -0
  42. inference_models/models/common/model_packages.py +25 -0
  43. inference_models/models/common/onnx.py +379 -0
  44. inference_models/models/common/roboflow/__init__.py +0 -0
  45. inference_models/models/common/roboflow/model_packages.py +361 -0
  46. inference_models/models/common/roboflow/post_processing.py +436 -0
  47. inference_models/models/common/roboflow/pre_processing.py +1332 -0
  48. inference_models/models/common/torch.py +20 -0
  49. inference_models/models/common/trt.py +266 -0
  50. inference_models/models/deep_lab_v3_plus/__init__.py +0 -0
  51. inference_models/models/deep_lab_v3_plus/deep_lab_v3_plus_segmentation_onnx.py +282 -0
  52. inference_models/models/deep_lab_v3_plus/deep_lab_v3_plus_segmentation_torch.py +264 -0
  53. inference_models/models/deep_lab_v3_plus/deep_lab_v3_plus_segmentation_trt.py +313 -0
  54. inference_models/models/depth_anything_v2/__init__.py +0 -0
  55. inference_models/models/depth_anything_v2/depth_anything_v2_hf.py +77 -0
  56. inference_models/models/dinov3/__init__.py +0 -0
  57. inference_models/models/dinov3/dinov3_classification_onnx.py +348 -0
  58. inference_models/models/dinov3/dinov3_classification_torch.py +323 -0
  59. inference_models/models/doctr/__init__.py +0 -0
  60. inference_models/models/doctr/doctr_torch.py +304 -0
  61. inference_models/models/easy_ocr/__init__.py +0 -0
  62. inference_models/models/easy_ocr/easy_ocr_torch.py +222 -0
  63. inference_models/models/florence2/__init__.py +0 -0
  64. inference_models/models/florence2/florence2_hf.py +897 -0
  65. inference_models/models/grounding_dino/__init__.py +0 -0
  66. inference_models/models/grounding_dino/grounding_dino_torch.py +227 -0
  67. inference_models/models/l2cs/__init__.py +0 -0
  68. inference_models/models/l2cs/l2cs_onnx.py +216 -0
  69. inference_models/models/mediapipe_face_detection/__init__.py +0 -0
  70. inference_models/models/mediapipe_face_detection/face_detection.py +203 -0
  71. inference_models/models/moondream2/__init__.py +0 -0
  72. inference_models/models/moondream2/moondream2_hf.py +281 -0
  73. inference_models/models/owlv2/__init__.py +0 -0
  74. inference_models/models/owlv2/cache.py +182 -0
  75. inference_models/models/owlv2/entities.py +112 -0
  76. inference_models/models/owlv2/owlv2_hf.py +695 -0
  77. inference_models/models/owlv2/reference_dataset.py +291 -0
  78. inference_models/models/paligemma/__init__.py +0 -0
  79. inference_models/models/paligemma/paligemma_hf.py +209 -0
  80. inference_models/models/perception_encoder/__init__.py +0 -0
  81. inference_models/models/perception_encoder/perception_encoder_pytorch.py +197 -0
  82. inference_models/models/perception_encoder/vision_encoder/__init__.py +0 -0
  83. inference_models/models/perception_encoder/vision_encoder/config.py +160 -0
  84. inference_models/models/perception_encoder/vision_encoder/pe.py +742 -0
  85. inference_models/models/perception_encoder/vision_encoder/rope.py +344 -0
  86. inference_models/models/perception_encoder/vision_encoder/tokenizer.py +342 -0
  87. inference_models/models/perception_encoder/vision_encoder/transforms.py +33 -0
  88. inference_models/models/qwen25vl/__init__.py +1 -0
  89. inference_models/models/qwen25vl/qwen25vl_hf.py +285 -0
  90. inference_models/models/resnet/__init__.py +0 -0
  91. inference_models/models/resnet/resnet_classification_onnx.py +330 -0
  92. inference_models/models/resnet/resnet_classification_torch.py +305 -0
  93. inference_models/models/resnet/resnet_classification_trt.py +369 -0
  94. inference_models/models/rfdetr/__init__.py +0 -0
  95. inference_models/models/rfdetr/backbone_builder.py +101 -0
  96. inference_models/models/rfdetr/class_remapping.py +41 -0
  97. inference_models/models/rfdetr/common.py +115 -0
  98. inference_models/models/rfdetr/default_labels.py +108 -0
  99. inference_models/models/rfdetr/dinov2_with_windowed_attn.py +1330 -0
  100. inference_models/models/rfdetr/misc.py +26 -0
  101. inference_models/models/rfdetr/ms_deform_attn.py +180 -0
  102. inference_models/models/rfdetr/ms_deform_attn_func.py +60 -0
  103. inference_models/models/rfdetr/position_encoding.py +166 -0
  104. inference_models/models/rfdetr/post_processor.py +83 -0
  105. inference_models/models/rfdetr/projector.py +373 -0
  106. inference_models/models/rfdetr/rfdetr_backbone_pytorch.py +394 -0
  107. inference_models/models/rfdetr/rfdetr_base_pytorch.py +807 -0
  108. inference_models/models/rfdetr/rfdetr_instance_segmentation_onnx.py +206 -0
  109. inference_models/models/rfdetr/rfdetr_instance_segmentation_pytorch.py +373 -0
  110. inference_models/models/rfdetr/rfdetr_instance_segmentation_trt.py +227 -0
  111. inference_models/models/rfdetr/rfdetr_object_detection_onnx.py +244 -0
  112. inference_models/models/rfdetr/rfdetr_object_detection_pytorch.py +470 -0
  113. inference_models/models/rfdetr/rfdetr_object_detection_trt.py +270 -0
  114. inference_models/models/rfdetr/segmentation_head.py +273 -0
  115. inference_models/models/rfdetr/transformer.py +767 -0
  116. inference_models/models/roboflow_instant/__init__.py +0 -0
  117. inference_models/models/roboflow_instant/roboflow_instant_hf.py +141 -0
  118. inference_models/models/sam/__init__.py +0 -0
  119. inference_models/models/sam/cache.py +147 -0
  120. inference_models/models/sam/entities.py +25 -0
  121. inference_models/models/sam/sam_torch.py +675 -0
  122. inference_models/models/sam2/__init__.py +0 -0
  123. inference_models/models/sam2/cache.py +162 -0
  124. inference_models/models/sam2/entities.py +43 -0
  125. inference_models/models/sam2/sam2_torch.py +905 -0
  126. inference_models/models/sam2_rt/__init__.py +0 -0
  127. inference_models/models/sam2_rt/sam2_pytorch.py +119 -0
  128. inference_models/models/smolvlm/__init__.py +0 -0
  129. inference_models/models/smolvlm/smolvlm_hf.py +245 -0
  130. inference_models/models/trocr/__init__.py +0 -0
  131. inference_models/models/trocr/trocr_hf.py +53 -0
  132. inference_models/models/vit/__init__.py +0 -0
  133. inference_models/models/vit/vit_classification_huggingface.py +319 -0
  134. inference_models/models/vit/vit_classification_onnx.py +326 -0
  135. inference_models/models/vit/vit_classification_trt.py +365 -0
  136. inference_models/models/yolact/__init__.py +1 -0
  137. inference_models/models/yolact/yolact_instance_segmentation_onnx.py +336 -0
  138. inference_models/models/yolact/yolact_instance_segmentation_trt.py +361 -0
  139. inference_models/models/yolo_world/__init__.py +1 -0
  140. inference_models/models/yolonas/__init__.py +0 -0
  141. inference_models/models/yolonas/nms.py +44 -0
  142. inference_models/models/yolonas/yolonas_object_detection_onnx.py +204 -0
  143. inference_models/models/yolonas/yolonas_object_detection_trt.py +230 -0
  144. inference_models/models/yolov10/__init__.py +0 -0
  145. inference_models/models/yolov10/yolov10_object_detection_onnx.py +187 -0
  146. inference_models/models/yolov10/yolov10_object_detection_trt.py +215 -0
  147. inference_models/models/yolov11/__init__.py +0 -0
  148. inference_models/models/yolov11/yolov11_onnx.py +28 -0
  149. inference_models/models/yolov11/yolov11_torch_script.py +25 -0
  150. inference_models/models/yolov11/yolov11_trt.py +21 -0
  151. inference_models/models/yolov12/__init__.py +0 -0
  152. inference_models/models/yolov12/yolov12_onnx.py +7 -0
  153. inference_models/models/yolov12/yolov12_torch_script.py +7 -0
  154. inference_models/models/yolov12/yolov12_trt.py +7 -0
  155. inference_models/models/yolov5/__init__.py +0 -0
  156. inference_models/models/yolov5/nms.py +99 -0
  157. inference_models/models/yolov5/yolov5_instance_segmentation_onnx.py +225 -0
  158. inference_models/models/yolov5/yolov5_instance_segmentation_trt.py +255 -0
  159. inference_models/models/yolov5/yolov5_object_detection_onnx.py +192 -0
  160. inference_models/models/yolov5/yolov5_object_detection_trt.py +218 -0
  161. inference_models/models/yolov7/__init__.py +0 -0
  162. inference_models/models/yolov7/yolov7_instance_segmentation_onnx.py +226 -0
  163. inference_models/models/yolov7/yolov7_instance_segmentation_trt.py +253 -0
  164. inference_models/models/yolov8/__init__.py +0 -0
  165. inference_models/models/yolov8/yolov8_classification_onnx.py +181 -0
  166. inference_models/models/yolov8/yolov8_instance_segmentation_onnx.py +239 -0
  167. inference_models/models/yolov8/yolov8_instance_segmentation_torch_script.py +201 -0
  168. inference_models/models/yolov8/yolov8_instance_segmentation_trt.py +268 -0
  169. inference_models/models/yolov8/yolov8_key_points_detection_onnx.py +263 -0
  170. inference_models/models/yolov8/yolov8_key_points_detection_torch_script.py +218 -0
  171. inference_models/models/yolov8/yolov8_key_points_detection_trt.py +287 -0
  172. inference_models/models/yolov8/yolov8_object_detection_onnx.py +213 -0
  173. inference_models/models/yolov8/yolov8_object_detection_torch_script.py +166 -0
  174. inference_models/models/yolov8/yolov8_object_detection_trt.py +231 -0
  175. inference_models/models/yolov9/__init__.py +0 -0
  176. inference_models/models/yolov9/yolov9_onnx.py +7 -0
  177. inference_models/models/yolov9/yolov9_torch_script.py +7 -0
  178. inference_models/models/yolov9/yolov9_trt.py +7 -0
  179. inference_models/runtime_introspection/__init__.py +0 -0
  180. inference_models/runtime_introspection/core.py +410 -0
  181. inference_models/utils/__init__.py +0 -0
  182. inference_models/utils/download.py +608 -0
  183. inference_models/utils/environment.py +28 -0
  184. inference_models/utils/file_system.py +51 -0
  185. inference_models/utils/hashing.py +7 -0
  186. inference_models/utils/imports.py +48 -0
  187. inference_models/utils/onnx_introspection.py +17 -0
  188. inference_models/weights_providers/__init__.py +0 -0
  189. inference_models/weights_providers/core.py +20 -0
  190. inference_models/weights_providers/entities.py +159 -0
  191. inference_models/weights_providers/roboflow.py +601 -0
  192. inference_models-0.18.3.dist-info/METADATA +466 -0
  193. inference_models-0.18.3.dist-info/RECORD +195 -0
  194. inference_models-0.18.3.dist-info/WHEEL +5 -0
  195. inference_models-0.18.3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,231 @@
1
+ import threading
2
+ from typing import List, Optional, Tuple, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ from inference_models import Detections, ObjectDetectionModel
8
+ from inference_models.configuration import DEFAULT_DEVICE
9
+ from inference_models.entities import ColorFormat
10
+ from inference_models.errors import (
11
+ CorruptedModelPackageError,
12
+ MissingDependencyError,
13
+ ModelRuntimeError,
14
+ )
15
+ from inference_models.models.common.cuda import (
16
+ use_cuda_context,
17
+ use_primary_cuda_context,
18
+ )
19
+ from inference_models.models.common.model_packages import get_model_package_contents
20
+ from inference_models.models.common.roboflow.model_packages import (
21
+ InferenceConfig,
22
+ PreProcessingMetadata,
23
+ ResizeMode,
24
+ TRTConfig,
25
+ parse_class_names_file,
26
+ parse_inference_config,
27
+ parse_trt_config,
28
+ )
29
+ from inference_models.models.common.roboflow.post_processing import (
30
+ post_process_nms_fused_model_output,
31
+ rescale_detections,
32
+ run_nms_for_object_detection,
33
+ )
34
+ from inference_models.models.common.roboflow.pre_processing import (
35
+ pre_process_network_input,
36
+ )
37
+ from inference_models.models.common.trt import (
38
+ get_engine_inputs_and_outputs,
39
+ infer_from_trt_engine,
40
+ load_model,
41
+ )
42
+
43
+ try:
44
+ import tensorrt as trt
45
+ except ImportError as import_error:
46
+ raise MissingDependencyError(
47
+ message=f"Could not import YOLOv8 model with TRT backend - this error means that some additional dependencies "
48
+ f"are not installed in the environment. If you run the `inference-models` library directly in your Python "
49
+ f"program, make sure the following extras of the package are installed: `trt10` - installation can only "
50
+ f"succeed for Linux and Windows machines with Cuda 12 installed. Jetson devices, should have TRT 10.x "
51
+ f"installed for all builds with Jetpack 6. "
52
+ f"If you see this error using Roboflow infrastructure, make sure the service you use does support the model. "
53
+ f"You can also contact Roboflow to get support.",
54
+ help_url="https://todo",
55
+ ) from import_error
56
+
57
+ try:
58
+ import pycuda.driver as cuda
59
+ except ImportError as import_error:
60
+ raise MissingDependencyError(
61
+ message="TODO", help_url="https://todo"
62
+ ) from import_error
63
+
64
+
65
+ class YOLOv8ForObjectDetectionTRT(
66
+ ObjectDetectionModel[torch.Tensor, PreProcessingMetadata, torch.Tensor]
67
+ ):
68
+
69
+ @classmethod
70
+ def from_pretrained(
71
+ cls,
72
+ model_name_or_path: str,
73
+ device: torch.device = DEFAULT_DEVICE,
74
+ engine_host_code_allowed: bool = False,
75
+ **kwargs,
76
+ ) -> "YOLOv8ForObjectDetectionTRT":
77
+ if device.type != "cuda":
78
+ raise ModelRuntimeError(
79
+ message=f"TRT engine only runs on CUDA device - {device} device detected.",
80
+ help_url="https://todo",
81
+ )
82
+ model_package_content = get_model_package_contents(
83
+ model_package_dir=model_name_or_path,
84
+ elements=[
85
+ "class_names.txt",
86
+ "inference_config.json",
87
+ "trt_config.json",
88
+ "engine.plan",
89
+ ],
90
+ )
91
+ class_names = parse_class_names_file(
92
+ class_names_path=model_package_content["class_names.txt"]
93
+ )
94
+ inference_config = parse_inference_config(
95
+ config_path=model_package_content["inference_config.json"],
96
+ allowed_resize_modes={
97
+ ResizeMode.STRETCH_TO,
98
+ ResizeMode.LETTERBOX,
99
+ ResizeMode.CENTER_CROP,
100
+ ResizeMode.LETTERBOX_REFLECT_EDGES,
101
+ },
102
+ )
103
+ if inference_config.post_processing.type != "nms":
104
+ raise CorruptedModelPackageError(
105
+ message="Expected NMS to be the post-processing",
106
+ help_url="https://todo",
107
+ )
108
+ trt_config = parse_trt_config(
109
+ config_path=model_package_content["trt_config.json"]
110
+ )
111
+ cuda.init()
112
+ cuda_device = cuda.Device(device.index or 0)
113
+ with use_primary_cuda_context(cuda_device=cuda_device) as cuda_context:
114
+ engine = load_model(
115
+ model_path=model_package_content["engine.plan"],
116
+ engine_host_code_allowed=engine_host_code_allowed,
117
+ )
118
+ execution_context = engine.create_execution_context()
119
+ inputs, outputs = get_engine_inputs_and_outputs(engine=engine)
120
+ if len(inputs) != 1:
121
+ raise CorruptedModelPackageError(
122
+ message=f"Implementation assume single model input, found: {len(inputs)}.",
123
+ help_url="https://todo",
124
+ )
125
+ if len(outputs) != 1:
126
+ raise CorruptedModelPackageError(
127
+ message=f"Implementation assume single model output, found: {len(outputs)}.",
128
+ help_url="https://todo",
129
+ )
130
+ return cls(
131
+ engine=engine,
132
+ input_name=inputs[0],
133
+ output_name=outputs[0],
134
+ class_names=class_names,
135
+ inference_config=inference_config,
136
+ trt_config=trt_config,
137
+ device=device,
138
+ cuda_context=cuda_context,
139
+ execution_context=execution_context,
140
+ )
141
+
142
+ def __init__(
143
+ self,
144
+ engine: trt.ICudaEngine,
145
+ input_name: str,
146
+ output_name: str,
147
+ class_names: List[str],
148
+ inference_config: InferenceConfig,
149
+ trt_config: TRTConfig,
150
+ device: torch.device,
151
+ cuda_context: cuda.Context,
152
+ execution_context: trt.IExecutionContext,
153
+ ):
154
+ self._engine = engine
155
+ self._input_name = input_name
156
+ self._output_names = [output_name]
157
+ self._class_names = class_names
158
+ self._inference_config = inference_config
159
+ self._trt_config = trt_config
160
+ self._device = device
161
+ self._cuda_context = cuda_context
162
+ self._execution_context = execution_context
163
+ self._lock = threading.Lock()
164
+
165
+ @property
166
+ def class_names(self) -> List[str]:
167
+ return self._class_names
168
+
169
+ def pre_process(
170
+ self,
171
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
172
+ input_color_format: Optional[ColorFormat] = None,
173
+ **kwargs,
174
+ ) -> Tuple[torch.Tensor, List[PreProcessingMetadata]]:
175
+ return pre_process_network_input(
176
+ images=images,
177
+ image_pre_processing=self._inference_config.image_pre_processing,
178
+ network_input=self._inference_config.network_input,
179
+ target_device=self._device,
180
+ input_color_format=input_color_format,
181
+ )
182
+
183
+ def forward(self, pre_processed_images: torch.Tensor, **kwargs) -> torch.Tensor:
184
+ with self._lock:
185
+ with use_cuda_context(context=self._cuda_context):
186
+ return infer_from_trt_engine(
187
+ pre_processed_images=pre_processed_images,
188
+ trt_config=self._trt_config,
189
+ engine=self._engine,
190
+ context=self._execution_context,
191
+ device=self._device,
192
+ input_name=self._input_name,
193
+ outputs=self._output_names,
194
+ )[0]
195
+
196
+ def post_process(
197
+ self,
198
+ model_results: torch.Tensor,
199
+ pre_processing_meta: List[PreProcessingMetadata],
200
+ conf_thresh: float = 0.25,
201
+ iou_thresh: float = 0.45,
202
+ max_detections: int = 100,
203
+ class_agnostic: bool = False,
204
+ **kwargs,
205
+ ) -> List[Detections]:
206
+ if self._inference_config.post_processing.fused:
207
+ nms_results = post_process_nms_fused_model_output(
208
+ output=model_results, conf_thresh=conf_thresh
209
+ )
210
+ else:
211
+ nms_results = run_nms_for_object_detection(
212
+ output=model_results,
213
+ conf_thresh=conf_thresh,
214
+ iou_thresh=iou_thresh,
215
+ max_detections=max_detections,
216
+ class_agnostic=class_agnostic,
217
+ )
218
+ rescaled_results = rescale_detections(
219
+ detections=nms_results,
220
+ images_metadata=pre_processing_meta,
221
+ )
222
+ results = []
223
+ for result in rescaled_results:
224
+ results.append(
225
+ Detections(
226
+ xyxy=result[:, :4].round().int(),
227
+ class_id=result[:, 5].int(),
228
+ confidence=result[:, 4],
229
+ )
230
+ )
231
+ return results
File without changes
@@ -0,0 +1,7 @@
1
+ from inference_models.models.yolov8.yolov8_object_detection_onnx import (
2
+ YOLOv8ForObjectDetectionOnnx,
3
+ )
4
+
5
+
6
+ class YOLOv9ForObjectDetectionOnnx(YOLOv8ForObjectDetectionOnnx):
7
+ pass
@@ -0,0 +1,7 @@
1
+ from inference_models.models.yolov8.yolov8_object_detection_torch_script import (
2
+ YOLOv8ForObjectDetectionTorchScript,
3
+ )
4
+
5
+
6
+ class YOLOv9ForObjectDetectionTorchScript(YOLOv8ForObjectDetectionTorchScript):
7
+ pass
@@ -0,0 +1,7 @@
1
+ from inference_models.models.yolov8.yolov8_object_detection_trt import (
2
+ YOLOv8ForObjectDetectionTRT,
3
+ )
4
+
5
+
6
+ class YOLOv9ForObjectDetectionTRT(YOLOv8ForObjectDetectionTRT):
7
+ pass
File without changes
@@ -0,0 +1,410 @@
1
+ import os
2
+ import platform
3
+ import re
4
+ import subprocess
5
+ from dataclasses import dataclass
6
+ from functools import cache
7
+ from typing import List, Optional, Set, Tuple
8
+
9
+ import torch
10
+ from packaging.version import InvalidVersion, Version
11
+
12
+ from inference_models.configuration import L4T_VERSION, RUNNING_ON_JETSON
13
+ from inference_models.errors import JetsonTypeResolutionError, RuntimeIntrospectionError
14
+ from inference_models.logger import LOGGER
15
+ from inference_models.utils.environment import str2bool
16
+
17
+ JETSON_DEVICES_TABLE = [
18
+ "NVIDIA Jetson Orin Nano",
19
+ "NVIDIA Jetson Orin NX",
20
+ "NVIDIA Jetson AGX Orin",
21
+ "NVIDIA Jetson IGX Orin",
22
+ "NVIDIA Jetson Xavier NX",
23
+ "NVIDIA Jetson AGX Xavier Industrial",
24
+ "NVIDIA Jetson AGX Xavier",
25
+ "NVIDIA Jetson Nano",
26
+ "NVIDIA Jetson TX2",
27
+ ]
28
+
29
+
30
+ @dataclass(frozen=True)
31
+ class RuntimeXRayResult:
32
+ gpu_available: bool
33
+ gpu_devices: List[str]
34
+ gpu_devices_cc: List[Version]
35
+ driver_version: Optional[Version]
36
+ cuda_version: Optional[Version]
37
+ trt_version: Optional[Version]
38
+ jetson_type: Optional[str]
39
+ l4t_version: Optional[Version]
40
+ os_version: Optional[str]
41
+ torch_available: bool
42
+ torch_version: Optional[Version]
43
+ torchvision_version: Optional[Version]
44
+ onnxruntime_version: Optional[Version]
45
+ available_onnx_execution_providers: Optional[Set[str]]
46
+ hf_transformers_available: bool
47
+ ultralytics_available: bool
48
+ trt_python_package_available: bool
49
+ mediapipe_available: bool
50
+
51
+ def __str__(self) -> str:
52
+ gpu_devices_str = ", ".join(self.gpu_devices)
53
+ return (
54
+ f"RuntimeXRayResult(gpu_available={self.gpu_available}, gpu_devices=[{gpu_devices_str}], "
55
+ f"gpu_devices_cc={self.gpu_devices_cc}, gpu_driver={self.driver_version}, "
56
+ f"cuda_version={self.cuda_version}, trt_version={self.trt_version}, "
57
+ f"jetson_type={self.jetson_type}, l4t_version={self.l4t_version}, os_version={self.os_version}, "
58
+ f"torch_available={self.torch_available}, onnxruntime_version={self.onnxruntime_version}, "
59
+ f"available_onnx_execution_providers={self.available_onnx_execution_providers}, hf_transformers_available={self.hf_transformers_available}, "
60
+ f"ultralytics_available={self.ultralytics_available}, "
61
+ f"trt_python_package_available={self.trt_python_package_available}, torch_version={self.torch_version}, "
62
+ f"torchvision_version={self.torchvision_version}, mediapipe_available={self.mediapipe_available})"
63
+ )
64
+
65
+
66
+ @cache
67
+ def x_ray_runtime_environment() -> RuntimeXRayResult:
68
+ trt_version = get_trt_version()
69
+ cuda_version = get_cuda_version()
70
+ jetson_type, l4t_version, os_version, driver_version = None, None, None, None
71
+ if is_running_on_jetson():
72
+ jetson_type = get_jetson_type()
73
+ l4t_version = get_l4t_version()
74
+ else:
75
+ os_version = get_os_version()
76
+ driver_version = get_driver_version()
77
+ gpu_devices = get_available_gpu_devices()
78
+ ensure_jetson_l4t_declared_for_jetson_hardware(
79
+ gpu_devices=gpu_devices,
80
+ l4t_version=l4t_version,
81
+ )
82
+ gpu_devices_cc = get_available_gpu_devices_cc()
83
+ torch_available = is_torch_available()
84
+ torch_version = get_torch_version()
85
+ torchvision_version = get_torchvision_version()
86
+ onnx_info = get_onnxruntime_info()
87
+ if onnx_info:
88
+ onnxruntime_version, available_onnx_execution_providers = onnx_info
89
+ else:
90
+ onnxruntime_version, available_onnx_execution_providers = None, None
91
+ hf_transformers_available = is_hf_transformers_available()
92
+ ultralytics_available = is_ultralytics_available()
93
+ trt_python_package_available = is_trt_python_package_available()
94
+ mediapipe_available = is_mediapipe_available()
95
+ return RuntimeXRayResult(
96
+ gpu_available=len(gpu_devices) > 0,
97
+ gpu_devices=gpu_devices,
98
+ gpu_devices_cc=gpu_devices_cc,
99
+ driver_version=driver_version,
100
+ cuda_version=cuda_version,
101
+ trt_version=trt_version,
102
+ jetson_type=jetson_type,
103
+ l4t_version=l4t_version,
104
+ os_version=os_version,
105
+ torch_available=torch_available,
106
+ torch_version=torch_version,
107
+ torchvision_version=torchvision_version,
108
+ onnxruntime_version=onnxruntime_version,
109
+ available_onnx_execution_providers=available_onnx_execution_providers,
110
+ hf_transformers_available=hf_transformers_available,
111
+ ultralytics_available=ultralytics_available,
112
+ trt_python_package_available=trt_python_package_available,
113
+ mediapipe_available=mediapipe_available,
114
+ )
115
+
116
+
117
+ @cache
118
+ def is_running_on_jetson() -> bool:
119
+ if RUNNING_ON_JETSON is not None:
120
+ return str2bool(value=RUNNING_ON_JETSON)
121
+ return get_jetson_type() is not None
122
+
123
+
124
+ @cache
125
+ def get_available_gpu_devices() -> List[str]:
126
+ num_devices = torch.cuda.device_count()
127
+ result = []
128
+ for i in range(num_devices):
129
+ device_name = torch.cuda.get_device_name(i)
130
+ result.append(device_name.replace(" ", "-").lower())
131
+ return result
132
+
133
+
134
+ @cache
135
+ def get_available_gpu_devices_cc() -> List[Version]:
136
+ num_devices = torch.cuda.device_count()
137
+ result = []
138
+ for i in range(num_devices):
139
+ device_cc_raw = torch.cuda.get_device_capability(i)
140
+ result.append(Version(f"{device_cc_raw[0]}.{device_cc_raw[1]}"))
141
+ return result
142
+
143
+
144
+ @cache
145
+ def get_cuda_version() -> Optional[Version]:
146
+ try:
147
+ result = subprocess.run(
148
+ "dpkg -l | grep cuda-cudart", shell=True, capture_output=True, text=True
149
+ )
150
+ if result.returncode != 0:
151
+ return None
152
+ result_chunks = result.stdout.strip().split(os.linesep)[0].split()
153
+ return Version(result_chunks[2])
154
+ except Exception:
155
+ return None
156
+
157
+
158
+ @cache
159
+ def get_trt_version() -> Optional[Version]:
160
+ trt_version = get_trt_version_from_libnvinfer()
161
+ if trt_version is not None:
162
+ return trt_version
163
+ try:
164
+ import tensorrt as trt
165
+
166
+ return Version(trt.__version__)
167
+ except Exception:
168
+ return None
169
+
170
+
171
+ @cache
172
+ def get_trt_version_from_libnvinfer() -> Optional[Version]:
173
+ try:
174
+ result = subprocess.run(
175
+ "dpkg -l | grep libnvinfer-bin", shell=True, capture_output=True, text=True
176
+ )
177
+ if result.returncode != 0:
178
+ return None
179
+ result_chunks = result.stdout.strip().split()
180
+ libraries_versions = result_chunks[2].split("+cuda")
181
+ return Version(libraries_versions[0])
182
+ except Exception:
183
+ return None
184
+
185
+
186
+ @cache
187
+ def get_jetson_type() -> Optional[str]:
188
+ declared_json_module = os.getenv("JETSON_MODULE")
189
+ if declared_json_module:
190
+ return resolve_jetson_type(jetson_module_name=declared_json_module)
191
+ jetson_type_from_hardware_inspection = get_jetson_type_from_hardware_inspection()
192
+ if jetson_type_from_hardware_inspection:
193
+ return jetson_type_from_hardware_inspection
194
+ return get_jetson_type_from_device_tree()
195
+
196
+
197
+ def get_jetson_type_from_hardware_inspection() -> Optional[str]:
198
+ try:
199
+ result = subprocess.run(
200
+ "lshw | grep 'product: NVIDIA Jetson'",
201
+ shell=True,
202
+ capture_output=True,
203
+ text=True,
204
+ )
205
+ if result.returncode != 0:
206
+ return None
207
+ for result_line in result.stdout.strip().split("\n"):
208
+ start_idx = result_line.find("NVIDIA")
209
+ end_idx = result_line.find("HDA")
210
+ if start_idx < 0 or end_idx < 0:
211
+ continue
212
+ jetson_type = result_line[start_idx:end_idx].strip()
213
+ return resolve_jetson_type(jetson_module_name=jetson_type)
214
+ return None
215
+ except Exception:
216
+ return None
217
+
218
+
219
+ def get_jetson_type_from_device_tree() -> Optional[str]:
220
+ try:
221
+ with open("/proc/device-tree/model") as f:
222
+ model_headline = f.read().strip().split("\n")[0]
223
+ return resolve_jetson_type(jetson_module_name=model_headline)
224
+ except Exception:
225
+ return None
226
+
227
+
228
+ def resolve_jetson_type(jetson_module_name: str) -> str:
229
+ for jetson_device in JETSON_DEVICES_TABLE:
230
+ if jetson_module_name.startswith(jetson_device):
231
+ return jetson_device.replace(" ", "-").lower()
232
+ raise JetsonTypeResolutionError(
233
+ message=f"Could not resolve jetson type. Value found in environment: {jetson_module_name}",
234
+ help_url="https://todo",
235
+ )
236
+
237
+
238
+ @cache
239
+ def get_l4t_version() -> Optional[Version]:
240
+ if L4T_VERSION:
241
+ return Version(L4T_VERSION)
242
+ return get_l4t_version_from_tegra_release()
243
+
244
+
245
+ def get_l4t_version_from_tegra_release() -> Optional[Version]:
246
+ try:
247
+ with open("/etc/nv_tegra_release") as f:
248
+ file_header = f.readline()
249
+ match = re.search(r"R(\d+).*REVISION:\s*([\d.]+)", file_header)
250
+ if match:
251
+ major = match.group(1)
252
+ minor_patch = match.group(2)
253
+ return Version(f"{major}.{minor_patch}")
254
+ return None
255
+ except Exception:
256
+ return None
257
+
258
+
259
+ @cache
260
+ def get_os_version() -> Optional[str]:
261
+ system = platform.system().lower()
262
+ if system == "linux":
263
+ return get_linux_os_version()
264
+ elif system == "darwin":
265
+ return platform.platform().lower()
266
+ elif system == "windows":
267
+ return platform.platform().lower()
268
+ return None
269
+
270
+
271
+ def get_linux_os_version() -> Optional[str]:
272
+ os_version_from_os_release = get_linux_os_from_os_release()
273
+ if os_version_from_os_release:
274
+ return os_version_from_os_release
275
+ return platform.platform().lower()
276
+
277
+
278
+ def get_linux_os_from_os_release() -> Optional[str]:
279
+ try:
280
+ with open("/etc/os-release") as f:
281
+ data = dict(line.strip().split("=", 1) for line in f if "=" in line)
282
+ distro_name = data["NAME"].strip('"')
283
+ version_id = data["VERSION_ID"].strip('"')
284
+ return f"{distro_name}-{version_id}".lower()
285
+ except Exception:
286
+ return None
287
+
288
+
289
+ @cache
290
+ def get_driver_version() -> Optional[Version]:
291
+ try:
292
+ with open("/proc/driver/nvidia/version") as f:
293
+ head_line = f.readline()
294
+ match = re.search(r"\b(\d+(\.\d+){1,2})\b", head_line)
295
+ if match:
296
+ return Version(match.group(1))
297
+ except Exception:
298
+ return None
299
+
300
+
301
+ @cache
302
+ def is_trt_python_package_available() -> bool:
303
+ try:
304
+ import tensorrt
305
+
306
+ return True
307
+ except ImportError:
308
+ return False
309
+
310
+
311
+ @cache
312
+ def is_mediapipe_available() -> bool:
313
+ try:
314
+ import mediapipe
315
+
316
+ return True
317
+ except ImportError:
318
+ return False
319
+
320
+
321
+ @cache
322
+ def is_torch_available() -> bool:
323
+ try:
324
+ import torch
325
+
326
+ return True
327
+ except ImportError:
328
+ return False
329
+
330
+
331
+ @cache
332
+ def get_torch_version() -> Optional[Version]:
333
+ try:
334
+ import torch
335
+
336
+ version_str = torch.__version__
337
+ if "+" in version_str:
338
+ version_str = version_str.split("+")[0]
339
+ return Version(version_str)
340
+ except ImportError:
341
+ return None
342
+ except InvalidVersion as e:
343
+ LOGGER.warning(f"Could not parse torch version: {e}")
344
+ return None
345
+
346
+
347
+ @cache
348
+ def get_torchvision_version() -> Optional[Version]:
349
+ try:
350
+ import torchvision
351
+
352
+ version_str = torchvision.__version__
353
+ if "+" in version_str:
354
+ version_str = version_str.split("+")[0]
355
+ return Version(version_str)
356
+ except ImportError:
357
+ return None
358
+ except InvalidVersion as e:
359
+ LOGGER.warning(f"Could not parse torch version: {e}")
360
+ return None
361
+
362
+
363
+ @cache
364
+ def get_onnxruntime_info() -> Optional[Tuple[Version, Set[str]]]:
365
+ try:
366
+ import onnxruntime
367
+
368
+ available_providers = onnxruntime.get_available_providers()
369
+ return Version(onnxruntime.__version__), available_providers
370
+ except ImportError:
371
+ return None
372
+
373
+
374
+ @cache
375
+ def is_hf_transformers_available() -> bool:
376
+ try:
377
+ import transformers
378
+
379
+ return True
380
+ except ImportError:
381
+ return False
382
+
383
+
384
+ @cache
385
+ def is_ultralytics_available() -> bool:
386
+ try:
387
+ import ultralytics
388
+
389
+ return True
390
+ except ImportError:
391
+ return False
392
+
393
+
394
+ def ensure_jetson_l4t_declared_for_jetson_hardware(
395
+ gpu_devices: List[str],
396
+ l4t_version: Optional[Version],
397
+ ) -> None:
398
+ # this function is needed to increase likelihood that in cases when
399
+ # runtime x-ray rely on env variables (like when on Jetson in container we
400
+ # cannot inspect hardware in a reliable way) - the xray is correct
401
+ if any(device == "orin" for device in gpu_devices) and l4t_version is None:
402
+ raise RuntimeIntrospectionError(
403
+ message="Inconsistency in environment setup detected - GPU device name indicate that you run the code"
404
+ "on Jetson device, but L4T version cannot be established, which is important for `inference-models` "
405
+ "to work correctly. Please set environment variable `RUNNING_ON_JETSON=True`, as well as "
406
+ "`L4T_VERSION=<your-l4t-version>`. Optionally you may also set `JETSON_MODULE` variable "
407
+ f"to one of the values: {JETSON_DEVICES_TABLE}. If that does not solve your issue - contact "
408
+ f"Roboflow immediately.",
409
+ help_url="https://todo",
410
+ )
File without changes