wandb 0.21.2__py3-none-macosx_12_0_arm64.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 (904) hide show
  1. package_readme.md +97 -0
  2. wandb/__init__.py +248 -0
  3. wandb/__init__.pyi +1230 -0
  4. wandb/__main__.py +3 -0
  5. wandb/_iterutils.py +65 -0
  6. wandb/_pydantic/__init__.py +30 -0
  7. wandb/_pydantic/base.py +128 -0
  8. wandb/_pydantic/utils.py +80 -0
  9. wandb/_pydantic/v1_compat.py +284 -0
  10. wandb/agents/__init__.py +0 -0
  11. wandb/agents/pyagent.py +386 -0
  12. wandb/analytics/__init__.py +3 -0
  13. wandb/analytics/sentry.py +267 -0
  14. wandb/apis/__init__.py +48 -0
  15. wandb/apis/attrs.py +50 -0
  16. wandb/apis/importers/__init__.py +1 -0
  17. wandb/apis/importers/internals/internal.py +382 -0
  18. wandb/apis/importers/internals/protocols.py +103 -0
  19. wandb/apis/importers/internals/util.py +78 -0
  20. wandb/apis/importers/mlflow.py +254 -0
  21. wandb/apis/importers/validation.py +108 -0
  22. wandb/apis/importers/wandb.py +1608 -0
  23. wandb/apis/internal.py +239 -0
  24. wandb/apis/normalize.py +81 -0
  25. wandb/apis/paginator.py +138 -0
  26. wandb/apis/public/__init__.py +35 -0
  27. wandb/apis/public/api.py +2449 -0
  28. wandb/apis/public/artifacts.py +1046 -0
  29. wandb/apis/public/automations.py +85 -0
  30. wandb/apis/public/const.py +4 -0
  31. wandb/apis/public/files.py +402 -0
  32. wandb/apis/public/history.py +201 -0
  33. wandb/apis/public/integrations.py +203 -0
  34. wandb/apis/public/jobs.py +742 -0
  35. wandb/apis/public/projects.py +276 -0
  36. wandb/apis/public/query_generator.py +176 -0
  37. wandb/apis/public/registries/__init__.py +0 -0
  38. wandb/apis/public/registries/_freezable_list.py +179 -0
  39. wandb/apis/public/registries/_utils.py +138 -0
  40. wandb/apis/public/registries/registries_search.py +347 -0
  41. wandb/apis/public/registries/registry.py +358 -0
  42. wandb/apis/public/reports.py +595 -0
  43. wandb/apis/public/runs.py +1216 -0
  44. wandb/apis/public/sweeps.py +440 -0
  45. wandb/apis/public/teams.py +235 -0
  46. wandb/apis/public/users.py +177 -0
  47. wandb/apis/public/utils.py +210 -0
  48. wandb/apis/reports/__init__.py +1 -0
  49. wandb/apis/reports/v1/__init__.py +8 -0
  50. wandb/apis/reports/v2/__init__.py +8 -0
  51. wandb/apis/workspaces/__init__.py +8 -0
  52. wandb/automations/__init__.py +73 -0
  53. wandb/automations/_filters/__init__.py +40 -0
  54. wandb/automations/_filters/expressions.py +181 -0
  55. wandb/automations/_filters/operators.py +258 -0
  56. wandb/automations/_filters/run_metrics.py +330 -0
  57. wandb/automations/_generated/__init__.py +177 -0
  58. wandb/automations/_generated/create_automation.py +17 -0
  59. wandb/automations/_generated/create_generic_webhook_integration.py +43 -0
  60. wandb/automations/_generated/delete_automation.py +15 -0
  61. wandb/automations/_generated/enums.py +35 -0
  62. wandb/automations/_generated/fragments.py +358 -0
  63. wandb/automations/_generated/generic_webhook_integrations_by_entity.py +22 -0
  64. wandb/automations/_generated/get_automations.py +24 -0
  65. wandb/automations/_generated/get_automations_by_entity.py +26 -0
  66. wandb/automations/_generated/input_types.py +104 -0
  67. wandb/automations/_generated/integrations_by_entity.py +22 -0
  68. wandb/automations/_generated/operations.py +647 -0
  69. wandb/automations/_generated/slack_integrations_by_entity.py +22 -0
  70. wandb/automations/_generated/update_automation.py +17 -0
  71. wandb/automations/_utils.py +235 -0
  72. wandb/automations/_validators.py +165 -0
  73. wandb/automations/actions.py +218 -0
  74. wandb/automations/automations.py +85 -0
  75. wandb/automations/events.py +285 -0
  76. wandb/automations/integrations.py +45 -0
  77. wandb/automations/scopes.py +78 -0
  78. wandb/beta/workflows.py +324 -0
  79. wandb/bin/gpu_stats +0 -0
  80. wandb/bin/wandb-core +0 -0
  81. wandb/cli/__init__.py +0 -0
  82. wandb/cli/beta.py +175 -0
  83. wandb/cli/cli.py +2883 -0
  84. wandb/data_types.py +66 -0
  85. wandb/docker/__init__.py +290 -0
  86. wandb/docker/names.py +40 -0
  87. wandb/docker/wandb-entrypoint.sh +33 -0
  88. wandb/env.py +535 -0
  89. wandb/errors/__init__.py +17 -0
  90. wandb/errors/errors.py +40 -0
  91. wandb/errors/links.py +73 -0
  92. wandb/errors/term.py +415 -0
  93. wandb/errors/util.py +57 -0
  94. wandb/errors/warnings.py +2 -0
  95. wandb/filesync/__init__.py +0 -0
  96. wandb/filesync/dir_watcher.py +404 -0
  97. wandb/filesync/stats.py +100 -0
  98. wandb/filesync/step_checksum.py +142 -0
  99. wandb/filesync/step_prepare.py +179 -0
  100. wandb/filesync/step_upload.py +287 -0
  101. wandb/filesync/upload_job.py +142 -0
  102. wandb/integration/__init__.py +0 -0
  103. wandb/integration/catboost/__init__.py +5 -0
  104. wandb/integration/catboost/catboost.py +182 -0
  105. wandb/integration/cohere/__init__.py +3 -0
  106. wandb/integration/cohere/cohere.py +21 -0
  107. wandb/integration/cohere/resolver.py +347 -0
  108. wandb/integration/diffusers/__init__.py +3 -0
  109. wandb/integration/diffusers/autologger.py +76 -0
  110. wandb/integration/diffusers/pipeline_resolver.py +50 -0
  111. wandb/integration/diffusers/resolvers/__init__.py +9 -0
  112. wandb/integration/diffusers/resolvers/multimodal.py +881 -0
  113. wandb/integration/diffusers/resolvers/utils.py +102 -0
  114. wandb/integration/fastai/__init__.py +243 -0
  115. wandb/integration/gym/__init__.py +98 -0
  116. wandb/integration/huggingface/__init__.py +3 -0
  117. wandb/integration/huggingface/huggingface.py +18 -0
  118. wandb/integration/huggingface/resolver.py +213 -0
  119. wandb/integration/keras/__init__.py +11 -0
  120. wandb/integration/keras/callbacks/__init__.py +5 -0
  121. wandb/integration/keras/callbacks/metrics_logger.py +129 -0
  122. wandb/integration/keras/callbacks/model_checkpoint.py +188 -0
  123. wandb/integration/keras/callbacks/tables_builder.py +228 -0
  124. wandb/integration/keras/keras.py +1086 -0
  125. wandb/integration/kfp/__init__.py +6 -0
  126. wandb/integration/kfp/helpers.py +28 -0
  127. wandb/integration/kfp/kfp_patch.py +335 -0
  128. wandb/integration/kfp/wandb_logging.py +182 -0
  129. wandb/integration/langchain/__init__.py +3 -0
  130. wandb/integration/langchain/wandb_tracer.py +49 -0
  131. wandb/integration/lightgbm/__init__.py +239 -0
  132. wandb/integration/lightning/__init__.py +0 -0
  133. wandb/integration/lightning/fabric/__init__.py +3 -0
  134. wandb/integration/lightning/fabric/logger.py +763 -0
  135. wandb/integration/metaflow/__init__.py +9 -0
  136. wandb/integration/metaflow/data_pandas.py +74 -0
  137. wandb/integration/metaflow/data_pytorch.py +75 -0
  138. wandb/integration/metaflow/data_sklearn.py +76 -0
  139. wandb/integration/metaflow/errors.py +13 -0
  140. wandb/integration/metaflow/metaflow.py +327 -0
  141. wandb/integration/openai/__init__.py +3 -0
  142. wandb/integration/openai/fine_tuning.py +480 -0
  143. wandb/integration/openai/openai.py +22 -0
  144. wandb/integration/openai/resolver.py +240 -0
  145. wandb/integration/prodigy/__init__.py +3 -0
  146. wandb/integration/prodigy/prodigy.py +291 -0
  147. wandb/integration/sacred/__init__.py +117 -0
  148. wandb/integration/sagemaker/__init__.py +14 -0
  149. wandb/integration/sagemaker/auth.py +29 -0
  150. wandb/integration/sagemaker/config.py +58 -0
  151. wandb/integration/sagemaker/files.py +2 -0
  152. wandb/integration/sagemaker/resources.py +63 -0
  153. wandb/integration/sb3/__init__.py +3 -0
  154. wandb/integration/sb3/sb3.py +147 -0
  155. wandb/integration/sklearn/__init__.py +37 -0
  156. wandb/integration/sklearn/calculate/__init__.py +32 -0
  157. wandb/integration/sklearn/calculate/calibration_curves.py +125 -0
  158. wandb/integration/sklearn/calculate/class_proportions.py +68 -0
  159. wandb/integration/sklearn/calculate/confusion_matrix.py +93 -0
  160. wandb/integration/sklearn/calculate/decision_boundaries.py +40 -0
  161. wandb/integration/sklearn/calculate/elbow_curve.py +55 -0
  162. wandb/integration/sklearn/calculate/feature_importances.py +67 -0
  163. wandb/integration/sklearn/calculate/learning_curve.py +64 -0
  164. wandb/integration/sklearn/calculate/outlier_candidates.py +69 -0
  165. wandb/integration/sklearn/calculate/residuals.py +86 -0
  166. wandb/integration/sklearn/calculate/silhouette.py +118 -0
  167. wandb/integration/sklearn/calculate/summary_metrics.py +62 -0
  168. wandb/integration/sklearn/plot/__init__.py +35 -0
  169. wandb/integration/sklearn/plot/classifier.py +329 -0
  170. wandb/integration/sklearn/plot/clusterer.py +146 -0
  171. wandb/integration/sklearn/plot/regressor.py +121 -0
  172. wandb/integration/sklearn/plot/shared.py +91 -0
  173. wandb/integration/sklearn/utils.py +184 -0
  174. wandb/integration/tensorboard/__init__.py +10 -0
  175. wandb/integration/tensorboard/log.py +351 -0
  176. wandb/integration/tensorboard/monkeypatch.py +186 -0
  177. wandb/integration/tensorflow/__init__.py +5 -0
  178. wandb/integration/tensorflow/estimator_hook.py +54 -0
  179. wandb/integration/torch/__init__.py +0 -0
  180. wandb/integration/torch/wandb_torch.py +554 -0
  181. wandb/integration/ultralytics/__init__.py +11 -0
  182. wandb/integration/ultralytics/bbox_utils.py +215 -0
  183. wandb/integration/ultralytics/callback.py +528 -0
  184. wandb/integration/ultralytics/classification_utils.py +83 -0
  185. wandb/integration/ultralytics/mask_utils.py +202 -0
  186. wandb/integration/ultralytics/pose_utils.py +103 -0
  187. wandb/integration/weave/__init__.py +6 -0
  188. wandb/integration/weave/interface.py +49 -0
  189. wandb/integration/weave/weave.py +63 -0
  190. wandb/integration/xgboost/__init__.py +11 -0
  191. wandb/integration/xgboost/xgboost.py +189 -0
  192. wandb/integration/yolov8/__init__.py +0 -0
  193. wandb/integration/yolov8/yolov8.py +284 -0
  194. wandb/jupyter.py +538 -0
  195. wandb/mpmain/__init__.py +0 -0
  196. wandb/mpmain/__main__.py +1 -0
  197. wandb/old/__init__.py +0 -0
  198. wandb/old/core.py +53 -0
  199. wandb/old/settings.py +176 -0
  200. wandb/old/summary.py +438 -0
  201. wandb/plot/__init__.py +30 -0
  202. wandb/plot/bar.py +71 -0
  203. wandb/plot/confusion_matrix.py +185 -0
  204. wandb/plot/custom_chart.py +147 -0
  205. wandb/plot/histogram.py +66 -0
  206. wandb/plot/line.py +75 -0
  207. wandb/plot/line_series.py +173 -0
  208. wandb/plot/pr_curve.py +186 -0
  209. wandb/plot/roc_curve.py +163 -0
  210. wandb/plot/scatter.py +66 -0
  211. wandb/plot/utils.py +184 -0
  212. wandb/plot/viz.py +41 -0
  213. wandb/proto/__init__.py +0 -0
  214. wandb/proto/v3/__init__.py +0 -0
  215. wandb/proto/v3/wandb_base_pb2.py +55 -0
  216. wandb/proto/v3/wandb_internal_pb2.py +1728 -0
  217. wandb/proto/v3/wandb_server_pb2.py +228 -0
  218. wandb/proto/v3/wandb_settings_pb2.py +122 -0
  219. wandb/proto/v3/wandb_telemetry_pb2.py +106 -0
  220. wandb/proto/v4/__init__.py +0 -0
  221. wandb/proto/v4/wandb_base_pb2.py +30 -0
  222. wandb/proto/v4/wandb_internal_pb2.py +382 -0
  223. wandb/proto/v4/wandb_server_pb2.py +67 -0
  224. wandb/proto/v4/wandb_settings_pb2.py +47 -0
  225. wandb/proto/v4/wandb_telemetry_pb2.py +41 -0
  226. wandb/proto/v5/wandb_base_pb2.py +31 -0
  227. wandb/proto/v5/wandb_internal_pb2.py +383 -0
  228. wandb/proto/v5/wandb_server_pb2.py +68 -0
  229. wandb/proto/v5/wandb_settings_pb2.py +48 -0
  230. wandb/proto/v5/wandb_telemetry_pb2.py +42 -0
  231. wandb/proto/v6/wandb_base_pb2.py +41 -0
  232. wandb/proto/v6/wandb_internal_pb2.py +393 -0
  233. wandb/proto/v6/wandb_server_pb2.py +78 -0
  234. wandb/proto/v6/wandb_settings_pb2.py +58 -0
  235. wandb/proto/v6/wandb_telemetry_pb2.py +52 -0
  236. wandb/proto/wandb_base_pb2.py +12 -0
  237. wandb/proto/wandb_deprecated.py +59 -0
  238. wandb/proto/wandb_generate_deprecated.py +30 -0
  239. wandb/proto/wandb_generate_proto.py +49 -0
  240. wandb/proto/wandb_internal_pb2.py +18 -0
  241. wandb/proto/wandb_server_pb2.py +12 -0
  242. wandb/proto/wandb_settings_pb2.py +12 -0
  243. wandb/proto/wandb_telemetry_pb2.py +12 -0
  244. wandb/py.typed +0 -0
  245. wandb/sdk/__init__.py +37 -0
  246. wandb/sdk/artifacts/__init__.py +0 -0
  247. wandb/sdk/artifacts/_factories.py +17 -0
  248. wandb/sdk/artifacts/_generated/__init__.py +508 -0
  249. wandb/sdk/artifacts/_generated/add_aliases.py +21 -0
  250. wandb/sdk/artifacts/_generated/artifact_by_id.py +17 -0
  251. wandb/sdk/artifacts/_generated/artifact_by_name.py +22 -0
  252. wandb/sdk/artifacts/_generated/artifact_collection_membership_file_urls.py +43 -0
  253. wandb/sdk/artifacts/_generated/artifact_collection_membership_files.py +43 -0
  254. wandb/sdk/artifacts/_generated/artifact_created_by.py +47 -0
  255. wandb/sdk/artifacts/_generated/artifact_file_urls.py +22 -0
  256. wandb/sdk/artifacts/_generated/artifact_type.py +31 -0
  257. wandb/sdk/artifacts/_generated/artifact_used_by.py +43 -0
  258. wandb/sdk/artifacts/_generated/artifact_version_files.py +36 -0
  259. wandb/sdk/artifacts/_generated/artifact_via_membership_by_name.py +26 -0
  260. wandb/sdk/artifacts/_generated/create_artifact_collection_tag_assignments.py +36 -0
  261. wandb/sdk/artifacts/_generated/delete_aliases.py +21 -0
  262. wandb/sdk/artifacts/_generated/delete_artifact.py +28 -0
  263. wandb/sdk/artifacts/_generated/delete_artifact_collection_tag_assignments.py +25 -0
  264. wandb/sdk/artifacts/_generated/delete_artifact_portfolio.py +35 -0
  265. wandb/sdk/artifacts/_generated/delete_artifact_sequence.py +35 -0
  266. wandb/sdk/artifacts/_generated/enums.py +22 -0
  267. wandb/sdk/artifacts/_generated/fetch_artifact_manifest.py +38 -0
  268. wandb/sdk/artifacts/_generated/fetch_linked_artifacts.py +67 -0
  269. wandb/sdk/artifacts/_generated/fetch_registries.py +32 -0
  270. wandb/sdk/artifacts/_generated/fragments.py +459 -0
  271. wandb/sdk/artifacts/_generated/input_types.py +46 -0
  272. wandb/sdk/artifacts/_generated/link_artifact.py +27 -0
  273. wandb/sdk/artifacts/_generated/move_artifact_collection.py +35 -0
  274. wandb/sdk/artifacts/_generated/operations.py +1223 -0
  275. wandb/sdk/artifacts/_generated/project_artifact_collection.py +101 -0
  276. wandb/sdk/artifacts/_generated/project_artifact_collections.py +33 -0
  277. wandb/sdk/artifacts/_generated/project_artifact_type.py +24 -0
  278. wandb/sdk/artifacts/_generated/project_artifact_types.py +24 -0
  279. wandb/sdk/artifacts/_generated/project_artifacts.py +42 -0
  280. wandb/sdk/artifacts/_generated/registry_collections.py +34 -0
  281. wandb/sdk/artifacts/_generated/registry_versions.py +34 -0
  282. wandb/sdk/artifacts/_generated/run_input_artifacts.py +51 -0
  283. wandb/sdk/artifacts/_generated/run_output_artifacts.py +51 -0
  284. wandb/sdk/artifacts/_generated/unlink_artifact.py +25 -0
  285. wandb/sdk/artifacts/_generated/update_artifact.py +26 -0
  286. wandb/sdk/artifacts/_generated/update_artifact_portfolio.py +35 -0
  287. wandb/sdk/artifacts/_generated/update_artifact_sequence.py +35 -0
  288. wandb/sdk/artifacts/_graphql_fragments.py +19 -0
  289. wandb/sdk/artifacts/_internal_artifact.py +54 -0
  290. wandb/sdk/artifacts/_validators.py +309 -0
  291. wandb/sdk/artifacts/artifact.py +2702 -0
  292. wandb/sdk/artifacts/artifact_download_logger.py +45 -0
  293. wandb/sdk/artifacts/artifact_file_cache.py +251 -0
  294. wandb/sdk/artifacts/artifact_instance_cache.py +17 -0
  295. wandb/sdk/artifacts/artifact_manifest.py +76 -0
  296. wandb/sdk/artifacts/artifact_manifest_entry.py +258 -0
  297. wandb/sdk/artifacts/artifact_manifests/__init__.py +0 -0
  298. wandb/sdk/artifacts/artifact_manifests/artifact_manifest_v1.py +94 -0
  299. wandb/sdk/artifacts/artifact_saver.py +277 -0
  300. wandb/sdk/artifacts/artifact_state.py +13 -0
  301. wandb/sdk/artifacts/artifact_ttl.py +9 -0
  302. wandb/sdk/artifacts/exceptions.py +71 -0
  303. wandb/sdk/artifacts/staging.py +27 -0
  304. wandb/sdk/artifacts/storage_handler.py +62 -0
  305. wandb/sdk/artifacts/storage_handlers/__init__.py +0 -0
  306. wandb/sdk/artifacts/storage_handlers/azure_handler.py +214 -0
  307. wandb/sdk/artifacts/storage_handlers/gcs_handler.py +224 -0
  308. wandb/sdk/artifacts/storage_handlers/http_handler.py +114 -0
  309. wandb/sdk/artifacts/storage_handlers/local_file_handler.py +142 -0
  310. wandb/sdk/artifacts/storage_handlers/multi_handler.py +56 -0
  311. wandb/sdk/artifacts/storage_handlers/s3_handler.py +339 -0
  312. wandb/sdk/artifacts/storage_handlers/tracking_handler.py +68 -0
  313. wandb/sdk/artifacts/storage_handlers/wb_artifact_handler.py +131 -0
  314. wandb/sdk/artifacts/storage_handlers/wb_local_artifact_handler.py +74 -0
  315. wandb/sdk/artifacts/storage_layout.py +8 -0
  316. wandb/sdk/artifacts/storage_policies/__init__.py +4 -0
  317. wandb/sdk/artifacts/storage_policies/register.py +1 -0
  318. wandb/sdk/artifacts/storage_policies/wandb_storage_policy.py +580 -0
  319. wandb/sdk/artifacts/storage_policy.py +75 -0
  320. wandb/sdk/backend/__init__.py +0 -0
  321. wandb/sdk/backend/backend.py +57 -0
  322. wandb/sdk/data_types/__init__.py +0 -0
  323. wandb/sdk/data_types/_dtypes.py +914 -0
  324. wandb/sdk/data_types/_private.py +10 -0
  325. wandb/sdk/data_types/audio.py +208 -0
  326. wandb/sdk/data_types/base_types/__init__.py +0 -0
  327. wandb/sdk/data_types/base_types/json_metadata.py +55 -0
  328. wandb/sdk/data_types/base_types/media.py +339 -0
  329. wandb/sdk/data_types/base_types/wb_value.py +295 -0
  330. wandb/sdk/data_types/bokeh.py +87 -0
  331. wandb/sdk/data_types/graph.py +439 -0
  332. wandb/sdk/data_types/helper_types/__init__.py +0 -0
  333. wandb/sdk/data_types/helper_types/bounding_boxes_2d.py +327 -0
  334. wandb/sdk/data_types/helper_types/classes.py +159 -0
  335. wandb/sdk/data_types/helper_types/image_mask.py +251 -0
  336. wandb/sdk/data_types/histogram.py +107 -0
  337. wandb/sdk/data_types/html.py +165 -0
  338. wandb/sdk/data_types/image.py +974 -0
  339. wandb/sdk/data_types/molecule.py +250 -0
  340. wandb/sdk/data_types/object_3d.py +495 -0
  341. wandb/sdk/data_types/plotly.py +95 -0
  342. wandb/sdk/data_types/saved_model.py +435 -0
  343. wandb/sdk/data_types/table.py +1468 -0
  344. wandb/sdk/data_types/table_decorators.py +108 -0
  345. wandb/sdk/data_types/trace_tree.py +440 -0
  346. wandb/sdk/data_types/utils.py +260 -0
  347. wandb/sdk/data_types/video.py +303 -0
  348. wandb/sdk/integration_utils/__init__.py +0 -0
  349. wandb/sdk/integration_utils/auto_logging.py +232 -0
  350. wandb/sdk/integration_utils/data_logging.py +475 -0
  351. wandb/sdk/interface/__init__.py +0 -0
  352. wandb/sdk/interface/constants.py +4 -0
  353. wandb/sdk/interface/interface.py +1056 -0
  354. wandb/sdk/interface/interface_queue.py +40 -0
  355. wandb/sdk/interface/interface_shared.py +471 -0
  356. wandb/sdk/interface/interface_sock.py +49 -0
  357. wandb/sdk/interface/summary_record.py +67 -0
  358. wandb/sdk/internal/__init__.py +0 -0
  359. wandb/sdk/internal/_generated/__init__.py +15 -0
  360. wandb/sdk/internal/_generated/enums.py +4 -0
  361. wandb/sdk/internal/_generated/input_types.py +4 -0
  362. wandb/sdk/internal/_generated/operations.py +15 -0
  363. wandb/sdk/internal/_generated/server_features_query.py +27 -0
  364. wandb/sdk/internal/context.py +89 -0
  365. wandb/sdk/internal/datastore.py +293 -0
  366. wandb/sdk/internal/file_pusher.py +177 -0
  367. wandb/sdk/internal/file_stream.py +686 -0
  368. wandb/sdk/internal/handler.py +854 -0
  369. wandb/sdk/internal/incremental_table_util.py +53 -0
  370. wandb/sdk/internal/internal_api.py +4723 -0
  371. wandb/sdk/internal/job_builder.py +639 -0
  372. wandb/sdk/internal/profiler.py +79 -0
  373. wandb/sdk/internal/progress.py +77 -0
  374. wandb/sdk/internal/run.py +27 -0
  375. wandb/sdk/internal/sample.py +70 -0
  376. wandb/sdk/internal/sender.py +1692 -0
  377. wandb/sdk/internal/sender_config.py +203 -0
  378. wandb/sdk/internal/settings_static.py +120 -0
  379. wandb/sdk/internal/tb_watcher.py +519 -0
  380. wandb/sdk/internal/thread_local_settings.py +18 -0
  381. wandb/sdk/launch/__init__.py +15 -0
  382. wandb/sdk/launch/_launch.py +331 -0
  383. wandb/sdk/launch/_launch_add.py +255 -0
  384. wandb/sdk/launch/_project_spec.py +565 -0
  385. wandb/sdk/launch/agent/__init__.py +5 -0
  386. wandb/sdk/launch/agent/agent.py +931 -0
  387. wandb/sdk/launch/agent/config.py +296 -0
  388. wandb/sdk/launch/agent/job_status_tracker.py +55 -0
  389. wandb/sdk/launch/agent/run_queue_item_file_saver.py +39 -0
  390. wandb/sdk/launch/builder/__init__.py +0 -0
  391. wandb/sdk/launch/builder/abstract.py +156 -0
  392. wandb/sdk/launch/builder/build.py +296 -0
  393. wandb/sdk/launch/builder/context_manager.py +235 -0
  394. wandb/sdk/launch/builder/docker_builder.py +177 -0
  395. wandb/sdk/launch/builder/kaniko_builder.py +595 -0
  396. wandb/sdk/launch/builder/noop.py +58 -0
  397. wandb/sdk/launch/builder/templates/_wandb_bootstrap.py +188 -0
  398. wandb/sdk/launch/builder/templates/dockerfile.py +92 -0
  399. wandb/sdk/launch/create_job.py +541 -0
  400. wandb/sdk/launch/environment/abstract.py +29 -0
  401. wandb/sdk/launch/environment/aws_environment.py +322 -0
  402. wandb/sdk/launch/environment/azure_environment.py +105 -0
  403. wandb/sdk/launch/environment/gcp_environment.py +334 -0
  404. wandb/sdk/launch/environment/local_environment.py +65 -0
  405. wandb/sdk/launch/errors.py +13 -0
  406. wandb/sdk/launch/git_reference.py +109 -0
  407. wandb/sdk/launch/inputs/files.py +148 -0
  408. wandb/sdk/launch/inputs/internal.py +314 -0
  409. wandb/sdk/launch/inputs/manage.py +113 -0
  410. wandb/sdk/launch/inputs/schema.py +40 -0
  411. wandb/sdk/launch/loader.py +249 -0
  412. wandb/sdk/launch/registry/abstract.py +48 -0
  413. wandb/sdk/launch/registry/anon.py +29 -0
  414. wandb/sdk/launch/registry/azure_container_registry.py +124 -0
  415. wandb/sdk/launch/registry/elastic_container_registry.py +192 -0
  416. wandb/sdk/launch/registry/google_artifact_registry.py +219 -0
  417. wandb/sdk/launch/registry/local_registry.py +65 -0
  418. wandb/sdk/launch/runner/__init__.py +0 -0
  419. wandb/sdk/launch/runner/abstract.py +185 -0
  420. wandb/sdk/launch/runner/kubernetes_monitor.py +473 -0
  421. wandb/sdk/launch/runner/kubernetes_runner.py +1285 -0
  422. wandb/sdk/launch/runner/local_container.py +301 -0
  423. wandb/sdk/launch/runner/local_process.py +78 -0
  424. wandb/sdk/launch/runner/sagemaker_runner.py +424 -0
  425. wandb/sdk/launch/runner/vertex_runner.py +225 -0
  426. wandb/sdk/launch/sweeps/__init__.py +37 -0
  427. wandb/sdk/launch/sweeps/scheduler.py +739 -0
  428. wandb/sdk/launch/sweeps/scheduler_sweep.py +90 -0
  429. wandb/sdk/launch/sweeps/utils.py +324 -0
  430. wandb/sdk/launch/utils.py +746 -0
  431. wandb/sdk/launch/wandb_reference.py +138 -0
  432. wandb/sdk/lib/__init__.py +5 -0
  433. wandb/sdk/lib/apikey.py +334 -0
  434. wandb/sdk/lib/asyncio_compat.py +213 -0
  435. wandb/sdk/lib/asyncio_manager.py +252 -0
  436. wandb/sdk/lib/capped_dict.py +26 -0
  437. wandb/sdk/lib/config_util.py +101 -0
  438. wandb/sdk/lib/console_capture.py +219 -0
  439. wandb/sdk/lib/credentials.py +141 -0
  440. wandb/sdk/lib/deprecate.py +27 -0
  441. wandb/sdk/lib/disabled.py +30 -0
  442. wandb/sdk/lib/exit_hooks.py +54 -0
  443. wandb/sdk/lib/file_stream_utils.py +118 -0
  444. wandb/sdk/lib/filenames.py +64 -0
  445. wandb/sdk/lib/filesystem.py +372 -0
  446. wandb/sdk/lib/fsm.py +165 -0
  447. wandb/sdk/lib/gitlib.py +240 -0
  448. wandb/sdk/lib/gql_request.py +65 -0
  449. wandb/sdk/lib/handler_util.py +21 -0
  450. wandb/sdk/lib/hashutil.py +106 -0
  451. wandb/sdk/lib/import_hooks.py +275 -0
  452. wandb/sdk/lib/interrupt.py +37 -0
  453. wandb/sdk/lib/ipython.py +126 -0
  454. wandb/sdk/lib/json_util.py +75 -0
  455. wandb/sdk/lib/lazyloader.py +63 -0
  456. wandb/sdk/lib/module.py +72 -0
  457. wandb/sdk/lib/paths.py +106 -0
  458. wandb/sdk/lib/preinit.py +42 -0
  459. wandb/sdk/lib/printer.py +571 -0
  460. wandb/sdk/lib/printer_asyncio.py +48 -0
  461. wandb/sdk/lib/progress.py +320 -0
  462. wandb/sdk/lib/proto_util.py +90 -0
  463. wandb/sdk/lib/redirect.py +876 -0
  464. wandb/sdk/lib/retry.py +395 -0
  465. wandb/sdk/lib/run_moment.py +82 -0
  466. wandb/sdk/lib/runid.py +12 -0
  467. wandb/sdk/lib/server.py +58 -0
  468. wandb/sdk/lib/service/ipc_support.py +13 -0
  469. wandb/sdk/lib/service/service_client.py +106 -0
  470. wandb/sdk/lib/service/service_connection.py +192 -0
  471. wandb/sdk/lib/service/service_port_file.py +105 -0
  472. wandb/sdk/lib/service/service_process.py +111 -0
  473. wandb/sdk/lib/service/service_token.py +181 -0
  474. wandb/sdk/lib/sparkline.py +44 -0
  475. wandb/sdk/lib/telemetry.py +100 -0
  476. wandb/sdk/lib/timed_input.py +133 -0
  477. wandb/sdk/lib/timer.py +19 -0
  478. wandb/sdk/lib/wb_logging.py +161 -0
  479. wandb/sdk/mailbox/__init__.py +23 -0
  480. wandb/sdk/mailbox/mailbox.py +143 -0
  481. wandb/sdk/mailbox/mailbox_handle.py +132 -0
  482. wandb/sdk/mailbox/response_handle.py +99 -0
  483. wandb/sdk/mailbox/wait_with_progress.py +100 -0
  484. wandb/sdk/projects/_generated/__init__.py +47 -0
  485. wandb/sdk/projects/_generated/delete_project.py +22 -0
  486. wandb/sdk/projects/_generated/enums.py +4 -0
  487. wandb/sdk/projects/_generated/fetch_registry.py +22 -0
  488. wandb/sdk/projects/_generated/fragments.py +41 -0
  489. wandb/sdk/projects/_generated/input_types.py +13 -0
  490. wandb/sdk/projects/_generated/operations.py +88 -0
  491. wandb/sdk/projects/_generated/rename_project.py +27 -0
  492. wandb/sdk/projects/_generated/upsert_registry_project.py +27 -0
  493. wandb/sdk/verify/__init__.py +0 -0
  494. wandb/sdk/verify/verify.py +555 -0
  495. wandb/sdk/wandb_alerts.py +12 -0
  496. wandb/sdk/wandb_config.py +323 -0
  497. wandb/sdk/wandb_helper.py +54 -0
  498. wandb/sdk/wandb_init.py +1581 -0
  499. wandb/sdk/wandb_login.py +332 -0
  500. wandb/sdk/wandb_metric.py +112 -0
  501. wandb/sdk/wandb_require.py +88 -0
  502. wandb/sdk/wandb_require_helpers.py +44 -0
  503. wandb/sdk/wandb_run.py +4088 -0
  504. wandb/sdk/wandb_settings.py +2105 -0
  505. wandb/sdk/wandb_setup.py +560 -0
  506. wandb/sdk/wandb_summary.py +150 -0
  507. wandb/sdk/wandb_sweep.py +120 -0
  508. wandb/sdk/wandb_sync.py +71 -0
  509. wandb/sdk/wandb_watch.py +146 -0
  510. wandb/sklearn.py +35 -0
  511. wandb/sync/__init__.py +3 -0
  512. wandb/sync/sync.py +452 -0
  513. wandb/trigger.py +29 -0
  514. wandb/util.py +2040 -0
  515. wandb/vendor/__init__.py +0 -0
  516. wandb/vendor/gql-0.2.0/setup.py +40 -0
  517. wandb/vendor/gql-0.2.0/tests/__init__.py +0 -0
  518. wandb/vendor/gql-0.2.0/tests/starwars/__init__.py +0 -0
  519. wandb/vendor/gql-0.2.0/tests/starwars/fixtures.py +96 -0
  520. wandb/vendor/gql-0.2.0/tests/starwars/schema.py +146 -0
  521. wandb/vendor/gql-0.2.0/tests/starwars/test_dsl.py +293 -0
  522. wandb/vendor/gql-0.2.0/tests/starwars/test_query.py +355 -0
  523. wandb/vendor/gql-0.2.0/tests/starwars/test_validation.py +171 -0
  524. wandb/vendor/gql-0.2.0/tests/test_client.py +31 -0
  525. wandb/vendor/gql-0.2.0/tests/test_transport.py +89 -0
  526. wandb/vendor/gql-0.2.0/wandb_gql/__init__.py +4 -0
  527. wandb/vendor/gql-0.2.0/wandb_gql/client.py +75 -0
  528. wandb/vendor/gql-0.2.0/wandb_gql/dsl.py +152 -0
  529. wandb/vendor/gql-0.2.0/wandb_gql/gql.py +10 -0
  530. wandb/vendor/gql-0.2.0/wandb_gql/transport/__init__.py +0 -0
  531. wandb/vendor/gql-0.2.0/wandb_gql/transport/http.py +6 -0
  532. wandb/vendor/gql-0.2.0/wandb_gql/transport/local_schema.py +15 -0
  533. wandb/vendor/gql-0.2.0/wandb_gql/transport/requests.py +46 -0
  534. wandb/vendor/gql-0.2.0/wandb_gql/utils.py +21 -0
  535. wandb/vendor/graphql-core-1.1/setup.py +86 -0
  536. wandb/vendor/graphql-core-1.1/wandb_graphql/__init__.py +287 -0
  537. wandb/vendor/graphql-core-1.1/wandb_graphql/error/__init__.py +6 -0
  538. wandb/vendor/graphql-core-1.1/wandb_graphql/error/base.py +42 -0
  539. wandb/vendor/graphql-core-1.1/wandb_graphql/error/format_error.py +11 -0
  540. wandb/vendor/graphql-core-1.1/wandb_graphql/error/located_error.py +29 -0
  541. wandb/vendor/graphql-core-1.1/wandb_graphql/error/syntax_error.py +36 -0
  542. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/__init__.py +26 -0
  543. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/base.py +311 -0
  544. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executor.py +398 -0
  545. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executors/__init__.py +0 -0
  546. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executors/asyncio.py +53 -0
  547. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executors/gevent.py +22 -0
  548. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executors/process.py +32 -0
  549. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executors/sync.py +7 -0
  550. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executors/thread.py +35 -0
  551. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/executors/utils.py +6 -0
  552. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/experimental/__init__.py +0 -0
  553. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/experimental/executor.py +66 -0
  554. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/experimental/fragment.py +252 -0
  555. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/experimental/resolver.py +151 -0
  556. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/experimental/utils.py +7 -0
  557. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/middleware.py +57 -0
  558. wandb/vendor/graphql-core-1.1/wandb_graphql/execution/values.py +145 -0
  559. wandb/vendor/graphql-core-1.1/wandb_graphql/graphql.py +60 -0
  560. wandb/vendor/graphql-core-1.1/wandb_graphql/language/__init__.py +0 -0
  561. wandb/vendor/graphql-core-1.1/wandb_graphql/language/ast.py +1349 -0
  562. wandb/vendor/graphql-core-1.1/wandb_graphql/language/base.py +19 -0
  563. wandb/vendor/graphql-core-1.1/wandb_graphql/language/lexer.py +435 -0
  564. wandb/vendor/graphql-core-1.1/wandb_graphql/language/location.py +30 -0
  565. wandb/vendor/graphql-core-1.1/wandb_graphql/language/parser.py +779 -0
  566. wandb/vendor/graphql-core-1.1/wandb_graphql/language/printer.py +193 -0
  567. wandb/vendor/graphql-core-1.1/wandb_graphql/language/source.py +18 -0
  568. wandb/vendor/graphql-core-1.1/wandb_graphql/language/visitor.py +222 -0
  569. wandb/vendor/graphql-core-1.1/wandb_graphql/language/visitor_meta.py +82 -0
  570. wandb/vendor/graphql-core-1.1/wandb_graphql/pyutils/__init__.py +0 -0
  571. wandb/vendor/graphql-core-1.1/wandb_graphql/pyutils/cached_property.py +17 -0
  572. wandb/vendor/graphql-core-1.1/wandb_graphql/pyutils/contain_subset.py +28 -0
  573. wandb/vendor/graphql-core-1.1/wandb_graphql/pyutils/default_ordered_dict.py +40 -0
  574. wandb/vendor/graphql-core-1.1/wandb_graphql/pyutils/ordereddict.py +8 -0
  575. wandb/vendor/graphql-core-1.1/wandb_graphql/pyutils/pair_set.py +43 -0
  576. wandb/vendor/graphql-core-1.1/wandb_graphql/pyutils/version.py +78 -0
  577. wandb/vendor/graphql-core-1.1/wandb_graphql/type/__init__.py +67 -0
  578. wandb/vendor/graphql-core-1.1/wandb_graphql/type/definition.py +619 -0
  579. wandb/vendor/graphql-core-1.1/wandb_graphql/type/directives.py +132 -0
  580. wandb/vendor/graphql-core-1.1/wandb_graphql/type/introspection.py +440 -0
  581. wandb/vendor/graphql-core-1.1/wandb_graphql/type/scalars.py +131 -0
  582. wandb/vendor/graphql-core-1.1/wandb_graphql/type/schema.py +100 -0
  583. wandb/vendor/graphql-core-1.1/wandb_graphql/type/typemap.py +145 -0
  584. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/__init__.py +0 -0
  585. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/assert_valid_name.py +9 -0
  586. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/ast_from_value.py +65 -0
  587. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/ast_to_code.py +49 -0
  588. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/ast_to_dict.py +24 -0
  589. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/base.py +75 -0
  590. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/build_ast_schema.py +291 -0
  591. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/build_client_schema.py +250 -0
  592. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/concat_ast.py +9 -0
  593. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/extend_schema.py +357 -0
  594. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/get_field_def.py +27 -0
  595. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/get_operation_ast.py +21 -0
  596. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/introspection_query.py +90 -0
  597. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/is_valid_literal_value.py +67 -0
  598. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/is_valid_value.py +66 -0
  599. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/quoted_or_list.py +21 -0
  600. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/schema_printer.py +168 -0
  601. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/suggestion_list.py +56 -0
  602. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/type_comparators.py +69 -0
  603. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/type_from_ast.py +21 -0
  604. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/type_info.py +149 -0
  605. wandb/vendor/graphql-core-1.1/wandb_graphql/utils/value_from_ast.py +69 -0
  606. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/__init__.py +4 -0
  607. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/__init__.py +79 -0
  608. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/arguments_of_correct_type.py +24 -0
  609. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/base.py +8 -0
  610. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/default_values_of_correct_type.py +44 -0
  611. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/fields_on_correct_type.py +113 -0
  612. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/fragments_on_composite_types.py +33 -0
  613. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/known_argument_names.py +70 -0
  614. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/known_directives.py +97 -0
  615. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/known_fragment_names.py +19 -0
  616. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/known_type_names.py +43 -0
  617. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/lone_anonymous_operation.py +23 -0
  618. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/no_fragment_cycles.py +59 -0
  619. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/no_undefined_variables.py +36 -0
  620. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/no_unused_fragments.py +38 -0
  621. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/no_unused_variables.py +37 -0
  622. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/overlapping_fields_can_be_merged.py +529 -0
  623. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/possible_fragment_spreads.py +44 -0
  624. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/provided_non_null_arguments.py +46 -0
  625. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/scalar_leafs.py +33 -0
  626. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/unique_argument_names.py +32 -0
  627. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/unique_fragment_names.py +28 -0
  628. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/unique_input_field_names.py +33 -0
  629. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/unique_operation_names.py +31 -0
  630. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/unique_variable_names.py +27 -0
  631. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/variables_are_input_types.py +21 -0
  632. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/rules/variables_in_allowed_position.py +53 -0
  633. wandb/vendor/graphql-core-1.1/wandb_graphql/validation/validation.py +158 -0
  634. wandb/vendor/promise-2.3.0/conftest.py +30 -0
  635. wandb/vendor/promise-2.3.0/setup.py +64 -0
  636. wandb/vendor/promise-2.3.0/tests/__init__.py +0 -0
  637. wandb/vendor/promise-2.3.0/tests/conftest.py +8 -0
  638. wandb/vendor/promise-2.3.0/tests/test_awaitable.py +32 -0
  639. wandb/vendor/promise-2.3.0/tests/test_awaitable_35.py +47 -0
  640. wandb/vendor/promise-2.3.0/tests/test_benchmark.py +116 -0
  641. wandb/vendor/promise-2.3.0/tests/test_complex_threads.py +23 -0
  642. wandb/vendor/promise-2.3.0/tests/test_dataloader.py +452 -0
  643. wandb/vendor/promise-2.3.0/tests/test_dataloader_awaitable_35.py +99 -0
  644. wandb/vendor/promise-2.3.0/tests/test_dataloader_extra.py +65 -0
  645. wandb/vendor/promise-2.3.0/tests/test_extra.py +670 -0
  646. wandb/vendor/promise-2.3.0/tests/test_issues.py +132 -0
  647. wandb/vendor/promise-2.3.0/tests/test_promise_list.py +70 -0
  648. wandb/vendor/promise-2.3.0/tests/test_spec.py +584 -0
  649. wandb/vendor/promise-2.3.0/tests/test_thread_safety.py +115 -0
  650. wandb/vendor/promise-2.3.0/tests/utils.py +3 -0
  651. wandb/vendor/promise-2.3.0/wandb_promise/__init__.py +38 -0
  652. wandb/vendor/promise-2.3.0/wandb_promise/async_.py +135 -0
  653. wandb/vendor/promise-2.3.0/wandb_promise/compat.py +32 -0
  654. wandb/vendor/promise-2.3.0/wandb_promise/dataloader.py +326 -0
  655. wandb/vendor/promise-2.3.0/wandb_promise/iterate_promise.py +12 -0
  656. wandb/vendor/promise-2.3.0/wandb_promise/promise.py +848 -0
  657. wandb/vendor/promise-2.3.0/wandb_promise/promise_list.py +151 -0
  658. wandb/vendor/promise-2.3.0/wandb_promise/pyutils/__init__.py +0 -0
  659. wandb/vendor/promise-2.3.0/wandb_promise/pyutils/version.py +83 -0
  660. wandb/vendor/promise-2.3.0/wandb_promise/schedulers/__init__.py +0 -0
  661. wandb/vendor/promise-2.3.0/wandb_promise/schedulers/asyncio.py +22 -0
  662. wandb/vendor/promise-2.3.0/wandb_promise/schedulers/gevent.py +21 -0
  663. wandb/vendor/promise-2.3.0/wandb_promise/schedulers/immediate.py +27 -0
  664. wandb/vendor/promise-2.3.0/wandb_promise/schedulers/thread.py +18 -0
  665. wandb/vendor/promise-2.3.0/wandb_promise/utils.py +56 -0
  666. wandb/vendor/pygments/__init__.py +90 -0
  667. wandb/vendor/pygments/cmdline.py +568 -0
  668. wandb/vendor/pygments/console.py +74 -0
  669. wandb/vendor/pygments/filter.py +74 -0
  670. wandb/vendor/pygments/filters/__init__.py +350 -0
  671. wandb/vendor/pygments/formatter.py +95 -0
  672. wandb/vendor/pygments/formatters/__init__.py +153 -0
  673. wandb/vendor/pygments/formatters/_mapping.py +85 -0
  674. wandb/vendor/pygments/formatters/bbcode.py +109 -0
  675. wandb/vendor/pygments/formatters/html.py +851 -0
  676. wandb/vendor/pygments/formatters/img.py +600 -0
  677. wandb/vendor/pygments/formatters/irc.py +182 -0
  678. wandb/vendor/pygments/formatters/latex.py +482 -0
  679. wandb/vendor/pygments/formatters/other.py +160 -0
  680. wandb/vendor/pygments/formatters/rtf.py +147 -0
  681. wandb/vendor/pygments/formatters/svg.py +153 -0
  682. wandb/vendor/pygments/formatters/terminal.py +136 -0
  683. wandb/vendor/pygments/formatters/terminal256.py +309 -0
  684. wandb/vendor/pygments/lexer.py +871 -0
  685. wandb/vendor/pygments/lexers/__init__.py +329 -0
  686. wandb/vendor/pygments/lexers/_asy_builtins.py +1645 -0
  687. wandb/vendor/pygments/lexers/_cl_builtins.py +232 -0
  688. wandb/vendor/pygments/lexers/_cocoa_builtins.py +72 -0
  689. wandb/vendor/pygments/lexers/_csound_builtins.py +1346 -0
  690. wandb/vendor/pygments/lexers/_lasso_builtins.py +5327 -0
  691. wandb/vendor/pygments/lexers/_lua_builtins.py +295 -0
  692. wandb/vendor/pygments/lexers/_mapping.py +500 -0
  693. wandb/vendor/pygments/lexers/_mql_builtins.py +1172 -0
  694. wandb/vendor/pygments/lexers/_openedge_builtins.py +2547 -0
  695. wandb/vendor/pygments/lexers/_php_builtins.py +4756 -0
  696. wandb/vendor/pygments/lexers/_postgres_builtins.py +621 -0
  697. wandb/vendor/pygments/lexers/_scilab_builtins.py +3094 -0
  698. wandb/vendor/pygments/lexers/_sourcemod_builtins.py +1163 -0
  699. wandb/vendor/pygments/lexers/_stan_builtins.py +532 -0
  700. wandb/vendor/pygments/lexers/_stata_builtins.py +419 -0
  701. wandb/vendor/pygments/lexers/_tsql_builtins.py +1004 -0
  702. wandb/vendor/pygments/lexers/_vim_builtins.py +1939 -0
  703. wandb/vendor/pygments/lexers/actionscript.py +240 -0
  704. wandb/vendor/pygments/lexers/agile.py +24 -0
  705. wandb/vendor/pygments/lexers/algebra.py +221 -0
  706. wandb/vendor/pygments/lexers/ambient.py +76 -0
  707. wandb/vendor/pygments/lexers/ampl.py +87 -0
  708. wandb/vendor/pygments/lexers/apl.py +101 -0
  709. wandb/vendor/pygments/lexers/archetype.py +318 -0
  710. wandb/vendor/pygments/lexers/asm.py +641 -0
  711. wandb/vendor/pygments/lexers/automation.py +374 -0
  712. wandb/vendor/pygments/lexers/basic.py +500 -0
  713. wandb/vendor/pygments/lexers/bibtex.py +160 -0
  714. wandb/vendor/pygments/lexers/business.py +612 -0
  715. wandb/vendor/pygments/lexers/c_cpp.py +252 -0
  716. wandb/vendor/pygments/lexers/c_like.py +541 -0
  717. wandb/vendor/pygments/lexers/capnproto.py +78 -0
  718. wandb/vendor/pygments/lexers/chapel.py +102 -0
  719. wandb/vendor/pygments/lexers/clean.py +288 -0
  720. wandb/vendor/pygments/lexers/compiled.py +34 -0
  721. wandb/vendor/pygments/lexers/configs.py +833 -0
  722. wandb/vendor/pygments/lexers/console.py +114 -0
  723. wandb/vendor/pygments/lexers/crystal.py +393 -0
  724. wandb/vendor/pygments/lexers/csound.py +366 -0
  725. wandb/vendor/pygments/lexers/css.py +689 -0
  726. wandb/vendor/pygments/lexers/d.py +251 -0
  727. wandb/vendor/pygments/lexers/dalvik.py +125 -0
  728. wandb/vendor/pygments/lexers/data.py +555 -0
  729. wandb/vendor/pygments/lexers/diff.py +165 -0
  730. wandb/vendor/pygments/lexers/dotnet.py +691 -0
  731. wandb/vendor/pygments/lexers/dsls.py +878 -0
  732. wandb/vendor/pygments/lexers/dylan.py +289 -0
  733. wandb/vendor/pygments/lexers/ecl.py +125 -0
  734. wandb/vendor/pygments/lexers/eiffel.py +65 -0
  735. wandb/vendor/pygments/lexers/elm.py +121 -0
  736. wandb/vendor/pygments/lexers/erlang.py +533 -0
  737. wandb/vendor/pygments/lexers/esoteric.py +277 -0
  738. wandb/vendor/pygments/lexers/ezhil.py +69 -0
  739. wandb/vendor/pygments/lexers/factor.py +344 -0
  740. wandb/vendor/pygments/lexers/fantom.py +250 -0
  741. wandb/vendor/pygments/lexers/felix.py +273 -0
  742. wandb/vendor/pygments/lexers/forth.py +177 -0
  743. wandb/vendor/pygments/lexers/fortran.py +205 -0
  744. wandb/vendor/pygments/lexers/foxpro.py +428 -0
  745. wandb/vendor/pygments/lexers/functional.py +21 -0
  746. wandb/vendor/pygments/lexers/go.py +101 -0
  747. wandb/vendor/pygments/lexers/grammar_notation.py +213 -0
  748. wandb/vendor/pygments/lexers/graph.py +80 -0
  749. wandb/vendor/pygments/lexers/graphics.py +553 -0
  750. wandb/vendor/pygments/lexers/haskell.py +843 -0
  751. wandb/vendor/pygments/lexers/haxe.py +936 -0
  752. wandb/vendor/pygments/lexers/hdl.py +382 -0
  753. wandb/vendor/pygments/lexers/hexdump.py +103 -0
  754. wandb/vendor/pygments/lexers/html.py +602 -0
  755. wandb/vendor/pygments/lexers/idl.py +270 -0
  756. wandb/vendor/pygments/lexers/igor.py +288 -0
  757. wandb/vendor/pygments/lexers/inferno.py +96 -0
  758. wandb/vendor/pygments/lexers/installers.py +322 -0
  759. wandb/vendor/pygments/lexers/int_fiction.py +1343 -0
  760. wandb/vendor/pygments/lexers/iolang.py +63 -0
  761. wandb/vendor/pygments/lexers/j.py +146 -0
  762. wandb/vendor/pygments/lexers/javascript.py +1525 -0
  763. wandb/vendor/pygments/lexers/julia.py +333 -0
  764. wandb/vendor/pygments/lexers/jvm.py +1573 -0
  765. wandb/vendor/pygments/lexers/lisp.py +2621 -0
  766. wandb/vendor/pygments/lexers/make.py +202 -0
  767. wandb/vendor/pygments/lexers/markup.py +595 -0
  768. wandb/vendor/pygments/lexers/math.py +21 -0
  769. wandb/vendor/pygments/lexers/matlab.py +663 -0
  770. wandb/vendor/pygments/lexers/ml.py +769 -0
  771. wandb/vendor/pygments/lexers/modeling.py +358 -0
  772. wandb/vendor/pygments/lexers/modula2.py +1561 -0
  773. wandb/vendor/pygments/lexers/monte.py +204 -0
  774. wandb/vendor/pygments/lexers/ncl.py +894 -0
  775. wandb/vendor/pygments/lexers/nimrod.py +159 -0
  776. wandb/vendor/pygments/lexers/nit.py +64 -0
  777. wandb/vendor/pygments/lexers/nix.py +136 -0
  778. wandb/vendor/pygments/lexers/oberon.py +105 -0
  779. wandb/vendor/pygments/lexers/objective.py +504 -0
  780. wandb/vendor/pygments/lexers/ooc.py +85 -0
  781. wandb/vendor/pygments/lexers/other.py +41 -0
  782. wandb/vendor/pygments/lexers/parasail.py +79 -0
  783. wandb/vendor/pygments/lexers/parsers.py +835 -0
  784. wandb/vendor/pygments/lexers/pascal.py +644 -0
  785. wandb/vendor/pygments/lexers/pawn.py +199 -0
  786. wandb/vendor/pygments/lexers/perl.py +620 -0
  787. wandb/vendor/pygments/lexers/php.py +267 -0
  788. wandb/vendor/pygments/lexers/praat.py +294 -0
  789. wandb/vendor/pygments/lexers/prolog.py +306 -0
  790. wandb/vendor/pygments/lexers/python.py +939 -0
  791. wandb/vendor/pygments/lexers/qvt.py +152 -0
  792. wandb/vendor/pygments/lexers/r.py +453 -0
  793. wandb/vendor/pygments/lexers/rdf.py +270 -0
  794. wandb/vendor/pygments/lexers/rebol.py +431 -0
  795. wandb/vendor/pygments/lexers/resource.py +85 -0
  796. wandb/vendor/pygments/lexers/rnc.py +67 -0
  797. wandb/vendor/pygments/lexers/roboconf.py +82 -0
  798. wandb/vendor/pygments/lexers/robotframework.py +560 -0
  799. wandb/vendor/pygments/lexers/ruby.py +519 -0
  800. wandb/vendor/pygments/lexers/rust.py +220 -0
  801. wandb/vendor/pygments/lexers/sas.py +228 -0
  802. wandb/vendor/pygments/lexers/scripting.py +1222 -0
  803. wandb/vendor/pygments/lexers/shell.py +794 -0
  804. wandb/vendor/pygments/lexers/smalltalk.py +195 -0
  805. wandb/vendor/pygments/lexers/smv.py +79 -0
  806. wandb/vendor/pygments/lexers/snobol.py +83 -0
  807. wandb/vendor/pygments/lexers/special.py +103 -0
  808. wandb/vendor/pygments/lexers/sql.py +681 -0
  809. wandb/vendor/pygments/lexers/stata.py +108 -0
  810. wandb/vendor/pygments/lexers/supercollider.py +90 -0
  811. wandb/vendor/pygments/lexers/tcl.py +145 -0
  812. wandb/vendor/pygments/lexers/templates.py +2283 -0
  813. wandb/vendor/pygments/lexers/testing.py +207 -0
  814. wandb/vendor/pygments/lexers/text.py +25 -0
  815. wandb/vendor/pygments/lexers/textedit.py +169 -0
  816. wandb/vendor/pygments/lexers/textfmts.py +297 -0
  817. wandb/vendor/pygments/lexers/theorem.py +458 -0
  818. wandb/vendor/pygments/lexers/trafficscript.py +54 -0
  819. wandb/vendor/pygments/lexers/typoscript.py +226 -0
  820. wandb/vendor/pygments/lexers/urbi.py +133 -0
  821. wandb/vendor/pygments/lexers/varnish.py +190 -0
  822. wandb/vendor/pygments/lexers/verification.py +111 -0
  823. wandb/vendor/pygments/lexers/web.py +24 -0
  824. wandb/vendor/pygments/lexers/webmisc.py +988 -0
  825. wandb/vendor/pygments/lexers/whiley.py +116 -0
  826. wandb/vendor/pygments/lexers/x10.py +69 -0
  827. wandb/vendor/pygments/modeline.py +44 -0
  828. wandb/vendor/pygments/plugin.py +68 -0
  829. wandb/vendor/pygments/regexopt.py +92 -0
  830. wandb/vendor/pygments/scanner.py +105 -0
  831. wandb/vendor/pygments/sphinxext.py +158 -0
  832. wandb/vendor/pygments/style.py +155 -0
  833. wandb/vendor/pygments/styles/__init__.py +80 -0
  834. wandb/vendor/pygments/styles/abap.py +29 -0
  835. wandb/vendor/pygments/styles/algol.py +63 -0
  836. wandb/vendor/pygments/styles/algol_nu.py +63 -0
  837. wandb/vendor/pygments/styles/arduino.py +98 -0
  838. wandb/vendor/pygments/styles/autumn.py +65 -0
  839. wandb/vendor/pygments/styles/borland.py +51 -0
  840. wandb/vendor/pygments/styles/bw.py +49 -0
  841. wandb/vendor/pygments/styles/colorful.py +81 -0
  842. wandb/vendor/pygments/styles/default.py +73 -0
  843. wandb/vendor/pygments/styles/emacs.py +72 -0
  844. wandb/vendor/pygments/styles/friendly.py +72 -0
  845. wandb/vendor/pygments/styles/fruity.py +42 -0
  846. wandb/vendor/pygments/styles/igor.py +29 -0
  847. wandb/vendor/pygments/styles/lovelace.py +97 -0
  848. wandb/vendor/pygments/styles/manni.py +75 -0
  849. wandb/vendor/pygments/styles/monokai.py +106 -0
  850. wandb/vendor/pygments/styles/murphy.py +80 -0
  851. wandb/vendor/pygments/styles/native.py +65 -0
  852. wandb/vendor/pygments/styles/paraiso_dark.py +125 -0
  853. wandb/vendor/pygments/styles/paraiso_light.py +125 -0
  854. wandb/vendor/pygments/styles/pastie.py +75 -0
  855. wandb/vendor/pygments/styles/perldoc.py +69 -0
  856. wandb/vendor/pygments/styles/rainbow_dash.py +89 -0
  857. wandb/vendor/pygments/styles/rrt.py +33 -0
  858. wandb/vendor/pygments/styles/sas.py +44 -0
  859. wandb/vendor/pygments/styles/stata.py +40 -0
  860. wandb/vendor/pygments/styles/tango.py +141 -0
  861. wandb/vendor/pygments/styles/trac.py +63 -0
  862. wandb/vendor/pygments/styles/vim.py +63 -0
  863. wandb/vendor/pygments/styles/vs.py +38 -0
  864. wandb/vendor/pygments/styles/xcode.py +51 -0
  865. wandb/vendor/pygments/token.py +213 -0
  866. wandb/vendor/pygments/unistring.py +217 -0
  867. wandb/vendor/pygments/util.py +388 -0
  868. wandb/vendor/watchdog_0_9_0/wandb_watchdog/__init__.py +17 -0
  869. wandb/vendor/watchdog_0_9_0/wandb_watchdog/events.py +615 -0
  870. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/__init__.py +98 -0
  871. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/api.py +369 -0
  872. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/fsevents.py +172 -0
  873. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/fsevents2.py +239 -0
  874. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/inotify.py +218 -0
  875. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/inotify_buffer.py +81 -0
  876. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/inotify_c.py +575 -0
  877. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/kqueue.py +730 -0
  878. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/polling.py +145 -0
  879. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/read_directory_changes.py +133 -0
  880. wandb/vendor/watchdog_0_9_0/wandb_watchdog/observers/winapi.py +348 -0
  881. wandb/vendor/watchdog_0_9_0/wandb_watchdog/patterns.py +265 -0
  882. wandb/vendor/watchdog_0_9_0/wandb_watchdog/tricks/__init__.py +174 -0
  883. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/__init__.py +151 -0
  884. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/bricks.py +249 -0
  885. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/compat.py +29 -0
  886. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/decorators.py +198 -0
  887. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/delayed_queue.py +88 -0
  888. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/dirsnapshot.py +293 -0
  889. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/echo.py +157 -0
  890. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/event_backport.py +41 -0
  891. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/importlib2.py +40 -0
  892. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/platform.py +57 -0
  893. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/unicode_paths.py +64 -0
  894. wandb/vendor/watchdog_0_9_0/wandb_watchdog/utils/win32stat.py +123 -0
  895. wandb/vendor/watchdog_0_9_0/wandb_watchdog/version.py +28 -0
  896. wandb/vendor/watchdog_0_9_0/wandb_watchdog/watchmedo.py +577 -0
  897. wandb/wandb_agent.py +580 -0
  898. wandb/wandb_controller.py +719 -0
  899. wandb/wandb_run.py +8 -0
  900. wandb-0.21.2.dist-info/METADATA +223 -0
  901. wandb-0.21.2.dist-info/RECORD +904 -0
  902. wandb-0.21.2.dist-info/WHEEL +4 -0
  903. wandb-0.21.2.dist-info/entry_points.txt +3 -0
  904. wandb-0.21.2.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,4723 @@
1
+ import base64
2
+ import datetime
3
+ import functools
4
+ import http.client
5
+ import json
6
+ import logging
7
+ import os
8
+ import re
9
+ import socket
10
+ import sys
11
+ import threading
12
+ from copy import deepcopy
13
+ from pathlib import Path
14
+ from typing import (
15
+ IO,
16
+ TYPE_CHECKING,
17
+ Any,
18
+ Callable,
19
+ Dict,
20
+ Iterable,
21
+ List,
22
+ Literal,
23
+ Mapping,
24
+ MutableMapping,
25
+ NamedTuple,
26
+ Optional,
27
+ Sequence,
28
+ TextIO,
29
+ Tuple,
30
+ Union,
31
+ )
32
+
33
+ import click
34
+ import requests
35
+ import yaml
36
+ from wandb_gql import Client, gql
37
+ from wandb_gql.client import RetryError
38
+ from wandb_graphql.language.ast import Document
39
+
40
+ import wandb
41
+ from wandb import env, util
42
+ from wandb.apis.normalize import normalize_exceptions, parse_backend_error_messages
43
+ from wandb.errors import AuthenticationError, CommError, UnsupportedError, UsageError
44
+ from wandb.integration.sagemaker import parse_sm_secrets
45
+ from wandb.old.settings import Settings
46
+ from wandb.proto.wandb_internal_pb2 import ServerFeature
47
+ from wandb.sdk.artifacts._validators import is_artifact_registry_project
48
+ from wandb.sdk.internal._generated import SERVER_FEATURES_QUERY_GQL, ServerFeaturesQuery
49
+ from wandb.sdk.internal.thread_local_settings import _thread_local_api_settings
50
+ from wandb.sdk.lib.gql_request import GraphQLSession
51
+ from wandb.sdk.lib.hashutil import B64MD5, md5_file_b64
52
+
53
+ from ..lib import credentials, retry
54
+ from ..lib.filenames import DIFF_FNAME, METADATA_FNAME
55
+ from ..lib.gitlib import GitRepo
56
+ from . import context
57
+ from .progress import Progress
58
+
59
+ logger = logging.getLogger(__name__)
60
+
61
+ LAUNCH_DEFAULT_PROJECT = "model-registry"
62
+
63
+ if TYPE_CHECKING:
64
+ from typing import Literal, TypedDict
65
+
66
+ from .progress import ProgressFn
67
+
68
+ class CreateArtifactFileSpecInput(TypedDict, total=False):
69
+ """Corresponds to `type CreateArtifactFileSpecInput` in schema.graphql."""
70
+
71
+ artifactID: str
72
+ name: str
73
+ md5: str
74
+ mimetype: Optional[str]
75
+ artifactManifestID: Optional[str]
76
+ uploadPartsInput: Optional[List[Dict[str, object]]]
77
+
78
+ class CreateArtifactFilesResponseFile(TypedDict):
79
+ id: str
80
+ name: str
81
+ displayName: str
82
+ uploadUrl: Optional[str]
83
+ uploadHeaders: Sequence[str]
84
+ uploadMultipartUrls: "UploadPartsResponse"
85
+ storagePath: str
86
+ artifact: "CreateArtifactFilesResponseFileNode"
87
+
88
+ class CreateArtifactFilesResponseFileNode(TypedDict):
89
+ id: str
90
+
91
+ class UploadPartsResponse(TypedDict):
92
+ uploadUrlParts: List["UploadUrlParts"]
93
+ uploadID: str
94
+
95
+ class UploadUrlParts(TypedDict):
96
+ partNumber: int
97
+ uploadUrl: str
98
+
99
+ class CompleteMultipartUploadArtifactInput(TypedDict):
100
+ """Corresponds to `type CompleteMultipartUploadArtifactInput` in schema.graphql."""
101
+
102
+ completeMultipartAction: str
103
+ completedParts: Dict[int, str]
104
+ artifactID: str
105
+ storagePath: str
106
+ uploadID: str
107
+ md5: str
108
+
109
+ class CompleteMultipartUploadArtifactResponse(TypedDict):
110
+ digest: str
111
+
112
+ class DefaultSettings(TypedDict):
113
+ section: str
114
+ git_remote: str
115
+ ignore_globs: Optional[List[str]]
116
+ base_url: Optional[str]
117
+ root_dir: Optional[str]
118
+ api_key: Optional[str]
119
+ entity: Optional[str]
120
+ organization: Optional[str]
121
+ project: Optional[str]
122
+ _extra_http_headers: Optional[Mapping[str, str]]
123
+ _proxies: Optional[Mapping[str, str]]
124
+
125
+ _Response = MutableMapping
126
+ SweepState = Literal["RUNNING", "PAUSED", "CANCELED", "FINISHED"]
127
+ Number = Union[int, float]
128
+
129
+ # class _MappingSupportsCopy(Protocol):
130
+ # def copy(self) -> "_MappingSupportsCopy": ...
131
+ # def keys(self) -> Iterable: ...
132
+ # def __getitem__(self, name: str) -> Any: ...
133
+
134
+ httpclient_logger = logging.getLogger("http.client")
135
+ if os.environ.get("WANDB_DEBUG"):
136
+ httpclient_logger.setLevel(logging.DEBUG)
137
+
138
+
139
+ def check_httpclient_logger_handler() -> None:
140
+ # Only enable http.client logging if WANDB_DEBUG is set
141
+ if not os.environ.get("WANDB_DEBUG"):
142
+ return
143
+ if httpclient_logger.handlers:
144
+ return
145
+
146
+ # Enable HTTPConnection debug logging to the logging framework
147
+ level = logging.DEBUG
148
+
149
+ def httpclient_log(*args: Any) -> None:
150
+ httpclient_logger.log(level, " ".join(args))
151
+
152
+ # mask the print() built-in in the http.client module to use logging instead
153
+ http.client.print = httpclient_log # type: ignore[attr-defined]
154
+ # enable debugging
155
+ http.client.HTTPConnection.debuglevel = 1
156
+
157
+ root_logger = logging.getLogger("wandb")
158
+ if root_logger.handlers:
159
+ httpclient_logger.addHandler(root_logger.handlers[0])
160
+
161
+
162
+ class _ThreadLocalData(threading.local):
163
+ context: Optional[context.Context]
164
+
165
+ def __init__(self) -> None:
166
+ self.context = None
167
+
168
+
169
+ class _OrgNames(NamedTuple):
170
+ entity_name: str
171
+ display_name: str
172
+
173
+
174
+ def _match_org_with_fetched_org_entities(
175
+ organization: str, orgs: Sequence[_OrgNames]
176
+ ) -> str:
177
+ """Match the organization provided in the path with the org entity or org name of the input entity.
178
+
179
+ Args:
180
+ organization: The organization name to match
181
+ orgs: List of tuples containing (org_entity_name, org_display_name)
182
+
183
+ Returns:
184
+ str: The matched org entity name
185
+
186
+ Raises:
187
+ ValueError: If no matching organization is found or if multiple orgs exist without a match
188
+ """
189
+ for org_names in orgs:
190
+ if organization in org_names:
191
+ return org_names.entity_name
192
+
193
+ if len(orgs) == 1:
194
+ raise ValueError(
195
+ f"Expecting the organization name or entity name to match {orgs[0].display_name!r} "
196
+ f"and cannot be linked/fetched with {organization!r}. "
197
+ "Please update the target path with the correct organization name."
198
+ )
199
+
200
+ raise ValueError(
201
+ "Personal entity belongs to multiple organizations "
202
+ f"and cannot be linked/fetched with {organization!r}. "
203
+ "Please update the target path with the correct organization name "
204
+ "or use a team entity in the entity settings."
205
+ )
206
+
207
+
208
+ class Api:
209
+ """W&B Internal Api wrapper.
210
+
211
+ Note:
212
+ Settings are automatically overridden by looking for
213
+ a `wandb/settings` file in the current working directory or its parent
214
+ directory. If none can be found, we look in the current user's home
215
+ directory.
216
+
217
+ Args:
218
+ default_settings(dict, optional): If you aren't using a settings
219
+ file, or you wish to override the section to use in the settings file
220
+ Override the settings here.
221
+ """
222
+
223
+ HTTP_TIMEOUT = env.get_http_timeout(20)
224
+ FILE_PUSHER_TIMEOUT = env.get_file_pusher_timeout()
225
+ _global_context: context.Context
226
+ _local_data: _ThreadLocalData
227
+
228
+ def __init__(
229
+ self,
230
+ default_settings: Optional[
231
+ Union[
232
+ "wandb.sdk.wandb_settings.Settings",
233
+ "wandb.sdk.internal.settings_static.SettingsStatic",
234
+ Settings,
235
+ dict,
236
+ ]
237
+ ] = None,
238
+ load_settings: bool = True,
239
+ retry_timedelta: datetime.timedelta = datetime.timedelta( # okay because it's immutable
240
+ days=7
241
+ ),
242
+ environ: MutableMapping = os.environ,
243
+ retry_callback: Optional[Callable[[int, str], Any]] = None,
244
+ api_key: Optional[str] = None,
245
+ ) -> None:
246
+ self._environ = environ
247
+ self._global_context = context.Context()
248
+ self._local_data = _ThreadLocalData()
249
+ self.default_settings: DefaultSettings = {
250
+ "section": "default",
251
+ "git_remote": "origin",
252
+ "ignore_globs": [],
253
+ "base_url": "https://api.wandb.ai",
254
+ "root_dir": None,
255
+ "api_key": None,
256
+ "entity": None,
257
+ "organization": None,
258
+ "project": None,
259
+ "_extra_http_headers": None,
260
+ "_proxies": None,
261
+ }
262
+ self.retry_timedelta = retry_timedelta
263
+ # todo: Old Settings do not follow the SupportsKeysAndGetItem Protocol
264
+ default_settings = default_settings or {}
265
+ self.default_settings.update(default_settings) # type: ignore
266
+ self.retry_uploads = 10
267
+ self._settings = Settings(
268
+ load_settings=load_settings,
269
+ root_dir=self.default_settings.get("root_dir"),
270
+ )
271
+ self.git = GitRepo(remote=self.settings("git_remote"))
272
+ # Mutable settings set by the _file_stream_api
273
+ self.dynamic_settings = {
274
+ "system_sample_seconds": 2,
275
+ "system_samples": 15,
276
+ "heartbeat_seconds": 30,
277
+ }
278
+
279
+ # todo: remove these hacky hacks after settings refactor is complete
280
+ # keeping this code here to limit scope and so that it is easy to remove later
281
+ self._extra_http_headers = self.settings("_extra_http_headers") or json.loads(
282
+ self._environ.get("WANDB__EXTRA_HTTP_HEADERS", "{}")
283
+ )
284
+ self._extra_http_headers.update(_thread_local_api_settings.headers or {})
285
+
286
+ auth = None
287
+ api_key = api_key or self.default_settings.get("api_key")
288
+ if api_key:
289
+ auth = ("api", api_key)
290
+ elif self.access_token is not None:
291
+ self._extra_http_headers["Authorization"] = f"Bearer {self.access_token}"
292
+ elif _thread_local_api_settings.cookies is None:
293
+ auth = ("api", self.api_key or "")
294
+
295
+ proxies = self.settings("_proxies") or json.loads(
296
+ self._environ.get("WANDB__PROXIES", "{}")
297
+ )
298
+
299
+ self.client = Client(
300
+ transport=GraphQLSession(
301
+ headers={
302
+ "User-Agent": self.user_agent,
303
+ "X-WANDB-USERNAME": env.get_username(env=self._environ),
304
+ "X-WANDB-USER-EMAIL": env.get_user_email(env=self._environ),
305
+ **self._extra_http_headers,
306
+ },
307
+ use_json=True,
308
+ # this timeout won't apply when the DNS lookup fails. in that case, it will be 60s
309
+ # https://bugs.python.org/issue22889
310
+ timeout=self.HTTP_TIMEOUT,
311
+ auth=auth,
312
+ url=f"{self.settings('base_url')}/graphql",
313
+ cookies=_thread_local_api_settings.cookies,
314
+ proxies=proxies,
315
+ )
316
+ )
317
+
318
+ self.retry_callback = retry_callback
319
+ self._retry_gql = retry.Retry(
320
+ self.execute,
321
+ retry_timedelta=retry_timedelta,
322
+ check_retry_fn=util.no_retry_auth,
323
+ retryable_exceptions=(RetryError, requests.RequestException),
324
+ retry_callback=retry_callback,
325
+ )
326
+ self._current_run_id: Optional[str] = None
327
+ self._file_stream_api = None
328
+ self._upload_file_session = requests.Session()
329
+ if self.FILE_PUSHER_TIMEOUT:
330
+ self._upload_file_session.put = functools.partial( # type: ignore
331
+ self._upload_file_session.put,
332
+ timeout=self.FILE_PUSHER_TIMEOUT,
333
+ )
334
+ if proxies:
335
+ self._upload_file_session.proxies.update(proxies)
336
+ # This Retry class is initialized once for each Api instance, so this
337
+ # defaults to retrying 1 million times per process or 7 days
338
+ self.upload_file_retry = normalize_exceptions(
339
+ retry.retriable(retry_timedelta=retry_timedelta)(self.upload_file)
340
+ )
341
+ self.upload_multipart_file_chunk_retry = normalize_exceptions(
342
+ retry.retriable(retry_timedelta=retry_timedelta)(
343
+ self.upload_multipart_file_chunk
344
+ )
345
+ )
346
+ self._client_id_mapping: Dict[str, str] = {}
347
+ # Large file uploads to azure can optionally use their SDK
348
+ self._azure_blob_module = util.get_module("azure.storage.blob")
349
+
350
+ self.query_types: Optional[List[str]] = None
351
+ self.mutation_types: Optional[List[str]] = None
352
+ self.server_info_types: Optional[List[str]] = None
353
+ self.server_use_artifact_input_info: Optional[List[str]] = None
354
+ self.server_create_artifact_input_info: Optional[List[str]] = None
355
+ self.server_artifact_fields_info: Optional[List[str]] = None
356
+ self.server_organization_type_fields_info: Optional[List[str]] = None
357
+ self.server_supports_enabling_artifact_usage_tracking: Optional[bool] = None
358
+ self._max_cli_version: Optional[str] = None
359
+ self._server_settings_type: Optional[List[str]] = None
360
+ self.fail_run_queue_item_input_info: Optional[List[str]] = None
361
+ self.create_launch_agent_input_info: Optional[List[str]] = None
362
+ self.server_create_run_queue_supports_drc: Optional[bool] = None
363
+ self.server_create_run_queue_supports_priority: Optional[bool] = None
364
+ self.server_supports_template_variables: Optional[bool] = None
365
+ self.server_push_to_run_queue_supports_priority: Optional[bool] = None
366
+
367
+ self._server_features_cache: Optional[Dict[str, bool]] = None
368
+
369
+ def gql(self, *args: Any, **kwargs: Any) -> Any:
370
+ ret = self._retry_gql(
371
+ *args,
372
+ retry_cancel_event=self.context.cancel_event,
373
+ **kwargs,
374
+ )
375
+ return ret
376
+
377
+ def set_local_context(self, api_context: Optional[context.Context]) -> None:
378
+ self._local_data.context = api_context
379
+
380
+ def clear_local_context(self) -> None:
381
+ self._local_data.context = None
382
+
383
+ @property
384
+ def context(self) -> context.Context:
385
+ return self._local_data.context or self._global_context
386
+
387
+ def reauth(self) -> None:
388
+ """Ensure the current api key is set in the transport."""
389
+ self.client.transport.session.auth = ("api", self.api_key or "")
390
+
391
+ def relocate(self) -> None:
392
+ """Ensure the current api points to the right server."""
393
+ self.client.transport.url = "{}/graphql".format(self.settings("base_url"))
394
+
395
+ def execute(self, *args: Any, **kwargs: Any) -> "_Response":
396
+ """Wrapper around execute that logs in cases of failure."""
397
+ try:
398
+ return self.client.execute(*args, **kwargs) # type: ignore
399
+ except requests.exceptions.HTTPError as err:
400
+ response = err.response
401
+ assert response is not None
402
+ logger.exception("Error executing GraphQL.")
403
+ for error in parse_backend_error_messages(response):
404
+ wandb.termerror(f"Error while calling W&B API: {error} ({response})")
405
+ raise
406
+
407
+ def validate_api_key(self) -> bool:
408
+ """Returns whether the API key stored on initialization is valid."""
409
+ res = self.execute(gql("query { viewer { id } }"))
410
+ return res is not None and res["viewer"] is not None
411
+
412
+ def set_current_run_id(self, run_id: str) -> None:
413
+ self._current_run_id = run_id
414
+
415
+ @property
416
+ def current_run_id(self) -> Optional[str]:
417
+ return self._current_run_id
418
+
419
+ @property
420
+ def user_agent(self) -> str:
421
+ return f"W&B Internal Client {wandb.__version__}"
422
+
423
+ @property
424
+ def api_key(self) -> Optional[str]:
425
+ if _thread_local_api_settings.api_key:
426
+ return _thread_local_api_settings.api_key
427
+ auth = requests.utils.get_netrc_auth(self.api_url)
428
+ key = None
429
+ if auth:
430
+ key = auth[-1]
431
+
432
+ # Environment should take precedence
433
+ env_key: Optional[str] = self._environ.get(env.API_KEY)
434
+ sagemaker_key: Optional[str] = parse_sm_secrets().get(env.API_KEY)
435
+ default_key: Optional[str] = self.default_settings.get("api_key")
436
+ return env_key or key or sagemaker_key or default_key
437
+
438
+ @property
439
+ def access_token(self) -> Optional[str]:
440
+ """Retrieves an access token for authentication.
441
+
442
+ This function attempts to exchange an identity token for a temporary
443
+ access token from the server, and save it to the credentials file.
444
+ It uses the path to the identity token as defined in the environment
445
+ variables. If the environment variable is not set, it returns None.
446
+
447
+ Returns:
448
+ Optional[str]: The access token if available, otherwise None if
449
+ no identity token is supplied.
450
+ Raises:
451
+ AuthenticationError: If the path to the identity token is not found.
452
+ """
453
+ token_file_str = self._environ.get(env.IDENTITY_TOKEN_FILE)
454
+ if not token_file_str:
455
+ return None
456
+
457
+ token_file = Path(token_file_str)
458
+ if not token_file.exists():
459
+ raise AuthenticationError(f"Identity token file not found: {token_file}")
460
+
461
+ base_url = self.settings("base_url")
462
+ credentials_file = env.get_credentials_file(
463
+ str(credentials.DEFAULT_WANDB_CREDENTIALS_FILE), self._environ
464
+ )
465
+ return credentials.access_token(base_url, token_file, credentials_file)
466
+
467
+ @property
468
+ def api_url(self) -> str:
469
+ return self.settings("base_url") # type: ignore
470
+
471
+ @property
472
+ def app_url(self) -> str:
473
+ return wandb.util.app_url(self.api_url)
474
+
475
+ @property
476
+ def default_entity(self) -> str:
477
+ return self.viewer().get("entity") # type: ignore
478
+
479
+ def settings(self, key: Optional[str] = None, section: Optional[str] = None) -> Any:
480
+ """The settings overridden from the wandb/settings file.
481
+
482
+ Args:
483
+ key (str, optional): If provided only this setting is returned
484
+ section (str, optional): If provided this section of the setting file is
485
+ used, defaults to "default"
486
+
487
+ Returns:
488
+ A dict with the current settings
489
+
490
+ {
491
+ "entity": "models",
492
+ "base_url": "https://api.wandb.ai",
493
+ "project": None,
494
+ "organization": "my-org",
495
+ }
496
+ """
497
+ result = self.default_settings.copy()
498
+ result.update(self._settings.items(section=section)) # type: ignore
499
+ result.update(
500
+ {
501
+ "entity": env.get_entity(
502
+ self._settings.get(
503
+ Settings.DEFAULT_SECTION,
504
+ "entity",
505
+ fallback=result.get("entity"),
506
+ ),
507
+ env=self._environ,
508
+ ),
509
+ "organization": env.get_organization(
510
+ self._settings.get(
511
+ Settings.DEFAULT_SECTION,
512
+ "organization",
513
+ fallback=result.get("organization"),
514
+ ),
515
+ env=self._environ,
516
+ ),
517
+ "project": env.get_project(
518
+ self._settings.get(
519
+ Settings.DEFAULT_SECTION,
520
+ "project",
521
+ fallback=result.get("project"),
522
+ ),
523
+ env=self._environ,
524
+ ),
525
+ "base_url": env.get_base_url(
526
+ self._settings.get(
527
+ Settings.DEFAULT_SECTION,
528
+ "base_url",
529
+ fallback=result.get("base_url"),
530
+ ),
531
+ env=self._environ,
532
+ ),
533
+ "ignore_globs": env.get_ignore(
534
+ self._settings.get(
535
+ Settings.DEFAULT_SECTION,
536
+ "ignore_globs",
537
+ fallback=result.get("ignore_globs"),
538
+ ),
539
+ env=self._environ,
540
+ ),
541
+ }
542
+ )
543
+
544
+ return result if key is None else result[key] # type: ignore
545
+
546
+ def clear_setting(
547
+ self, key: str, globally: bool = False, persist: bool = False
548
+ ) -> None:
549
+ self._settings.clear(
550
+ Settings.DEFAULT_SECTION, key, globally=globally, persist=persist
551
+ )
552
+
553
+ def set_setting(
554
+ self, key: str, value: Any, globally: bool = False, persist: bool = False
555
+ ) -> None:
556
+ self._settings.set(
557
+ Settings.DEFAULT_SECTION, key, value, globally=globally, persist=persist
558
+ )
559
+ if key == "entity":
560
+ env.set_entity(value, env=self._environ)
561
+ elif key == "project":
562
+ env.set_project(value, env=self._environ)
563
+ elif key == "base_url":
564
+ self.relocate()
565
+
566
+ def parse_slug(
567
+ self, slug: str, project: Optional[str] = None, run: Optional[str] = None
568
+ ) -> Tuple[str, str]:
569
+ """Parse a slug into a project and run.
570
+
571
+ Args:
572
+ slug (str): The slug to parse
573
+ project (str, optional): The project to use, if not provided it will be
574
+ inferred from the slug
575
+ run (str, optional): The run to use, if not provided it will be inferred
576
+ from the slug
577
+
578
+ Returns:
579
+ A dict with the project and run
580
+ """
581
+ if slug and "/" in slug:
582
+ parts = slug.split("/")
583
+ project = parts[0]
584
+ run = parts[1]
585
+ else:
586
+ project = project or self.settings().get("project")
587
+ if project is None:
588
+ raise CommError("No default project configured.")
589
+ run = run or slug or self.current_run_id or env.get_run(env=self._environ)
590
+ assert run, "run must be specified"
591
+ return project, run
592
+
593
+ @normalize_exceptions
594
+ def server_info_introspection(self) -> Tuple[List[str], List[str], List[str]]:
595
+ query_string = """
596
+ query ProbeServerCapabilities {
597
+ QueryType: __type(name: "Query") {
598
+ ...fieldData
599
+ }
600
+ MutationType: __type(name: "Mutation") {
601
+ ...fieldData
602
+ }
603
+ ServerInfoType: __type(name: "ServerInfo") {
604
+ ...fieldData
605
+ }
606
+ }
607
+
608
+ fragment fieldData on __Type {
609
+ fields {
610
+ name
611
+ }
612
+ }
613
+ """
614
+ if (
615
+ self.query_types is None
616
+ or self.mutation_types is None
617
+ or self.server_info_types is None
618
+ ):
619
+ query = gql(query_string)
620
+ res = self.gql(query)
621
+
622
+ self.query_types = [
623
+ field.get("name", "")
624
+ for field in res.get("QueryType", {}).get("fields", [{}])
625
+ ]
626
+ self.mutation_types = [
627
+ field.get("name", "")
628
+ for field in res.get("MutationType", {}).get("fields", [{}])
629
+ ]
630
+ self.server_info_types = [
631
+ field.get("name", "")
632
+ for field in res.get("ServerInfoType", {}).get("fields", [{}])
633
+ ]
634
+ return self.query_types, self.server_info_types, self.mutation_types
635
+
636
+ @normalize_exceptions
637
+ def server_settings_introspection(self) -> None:
638
+ query_string = """
639
+ query ProbeServerSettings {
640
+ ServerSettingsType: __type(name: "ServerSettings") {
641
+ ...fieldData
642
+ }
643
+ }
644
+
645
+ fragment fieldData on __Type {
646
+ fields {
647
+ name
648
+ }
649
+ }
650
+ """
651
+ if self._server_settings_type is None:
652
+ query = gql(query_string)
653
+ res = self.gql(query)
654
+ self._server_settings_type = (
655
+ [
656
+ field.get("name", "")
657
+ for field in res.get("ServerSettingsType", {}).get("fields", [{}])
658
+ ]
659
+ if res
660
+ else []
661
+ )
662
+
663
+ def server_use_artifact_input_introspection(self) -> List:
664
+ query_string = """
665
+ query ProbeServerUseArtifactInput {
666
+ UseArtifactInputInfoType: __type(name: "UseArtifactInput") {
667
+ name
668
+ inputFields {
669
+ name
670
+ }
671
+ }
672
+ }
673
+ """
674
+
675
+ if self.server_use_artifact_input_info is None:
676
+ query = gql(query_string)
677
+ res = self.gql(query)
678
+ self.server_use_artifact_input_info = [
679
+ field.get("name", "")
680
+ for field in res.get("UseArtifactInputInfoType", {}).get(
681
+ "inputFields", [{}]
682
+ )
683
+ ]
684
+ return self.server_use_artifact_input_info
685
+
686
+ @normalize_exceptions
687
+ def launch_agent_introspection(self) -> Optional[str]:
688
+ query = gql(
689
+ """
690
+ query LaunchAgentIntrospection {
691
+ LaunchAgentType: __type(name: "LaunchAgent") {
692
+ name
693
+ }
694
+ }
695
+ """
696
+ )
697
+
698
+ res = self.gql(query)
699
+ return res.get("LaunchAgentType") or None
700
+
701
+ @normalize_exceptions
702
+ def create_run_queue_introspection(self) -> Tuple[bool, bool, bool]:
703
+ _, _, mutations = self.server_info_introspection()
704
+ query_string = """
705
+ query ProbeCreateRunQueueInput {
706
+ CreateRunQueueInputType: __type(name: "CreateRunQueueInput") {
707
+ name
708
+ inputFields {
709
+ name
710
+ }
711
+ }
712
+ }
713
+ """
714
+ if (
715
+ self.server_create_run_queue_supports_drc is None
716
+ or self.server_create_run_queue_supports_priority is None
717
+ ):
718
+ query = gql(query_string)
719
+ res = self.gql(query)
720
+ if res is None:
721
+ raise CommError("Could not get CreateRunQueue input from GQL.")
722
+ self.server_create_run_queue_supports_drc = "defaultResourceConfigID" in [
723
+ x["name"]
724
+ for x in (
725
+ res.get("CreateRunQueueInputType", {}).get("inputFields", [{}])
726
+ )
727
+ ]
728
+ self.server_create_run_queue_supports_priority = "prioritizationMode" in [
729
+ x["name"]
730
+ for x in (
731
+ res.get("CreateRunQueueInputType", {}).get("inputFields", [{}])
732
+ )
733
+ ]
734
+ return (
735
+ "createRunQueue" in mutations,
736
+ self.server_create_run_queue_supports_drc,
737
+ self.server_create_run_queue_supports_priority,
738
+ )
739
+
740
+ @normalize_exceptions
741
+ def upsert_run_queue_introspection(self) -> bool:
742
+ _, _, mutations = self.server_info_introspection()
743
+ return "upsertRunQueue" in mutations
744
+
745
+ @normalize_exceptions
746
+ def push_to_run_queue_introspection(self) -> Tuple[bool, bool]:
747
+ query_string = """
748
+ query ProbePushToRunQueueInput {
749
+ PushToRunQueueInputType: __type(name: "PushToRunQueueInput") {
750
+ name
751
+ inputFields {
752
+ name
753
+ }
754
+ }
755
+ }
756
+ """
757
+
758
+ if (
759
+ self.server_supports_template_variables is None
760
+ or self.server_push_to_run_queue_supports_priority is None
761
+ ):
762
+ query = gql(query_string)
763
+ res = self.gql(query)
764
+ self.server_supports_template_variables = "templateVariableValues" in [
765
+ x["name"]
766
+ for x in (
767
+ res.get("PushToRunQueueInputType", {}).get("inputFields", [{}])
768
+ )
769
+ ]
770
+ self.server_push_to_run_queue_supports_priority = "priority" in [
771
+ x["name"]
772
+ for x in (
773
+ res.get("PushToRunQueueInputType", {}).get("inputFields", [{}])
774
+ )
775
+ ]
776
+
777
+ return (
778
+ self.server_supports_template_variables,
779
+ self.server_push_to_run_queue_supports_priority,
780
+ )
781
+
782
+ @normalize_exceptions
783
+ def create_default_resource_config_introspection(self) -> bool:
784
+ _, _, mutations = self.server_info_introspection()
785
+ return "createDefaultResourceConfig" in mutations
786
+
787
+ @normalize_exceptions
788
+ def fail_run_queue_item_introspection(self) -> bool:
789
+ _, _, mutations = self.server_info_introspection()
790
+ return "failRunQueueItem" in mutations
791
+
792
+ @normalize_exceptions
793
+ def fail_run_queue_item_fields_introspection(self) -> List:
794
+ if self.fail_run_queue_item_input_info:
795
+ return self.fail_run_queue_item_input_info
796
+ query_string = """
797
+ query ProbeServerFailRunQueueItemInput {
798
+ FailRunQueueItemInputInfoType: __type(name:"FailRunQueueItemInput") {
799
+ inputFields{
800
+ name
801
+ }
802
+ }
803
+ }
804
+ """
805
+
806
+ query = gql(query_string)
807
+ res = self.gql(query)
808
+
809
+ self.fail_run_queue_item_input_info = [
810
+ field.get("name", "")
811
+ for field in res.get("FailRunQueueItemInputInfoType", {}).get(
812
+ "inputFields", [{}]
813
+ )
814
+ ]
815
+ return self.fail_run_queue_item_input_info
816
+
817
+ @normalize_exceptions
818
+ def fail_run_queue_item(
819
+ self,
820
+ run_queue_item_id: str,
821
+ message: str,
822
+ stage: str,
823
+ file_paths: Optional[List[str]] = None,
824
+ ) -> bool:
825
+ if not self.fail_run_queue_item_introspection():
826
+ return False
827
+ variable_values: Dict[str, Union[str, Optional[List[str]]]] = {
828
+ "runQueueItemId": run_queue_item_id,
829
+ }
830
+ if "message" in self.fail_run_queue_item_fields_introspection():
831
+ variable_values.update({"message": message, "stage": stage})
832
+ if file_paths is not None:
833
+ variable_values["filePaths"] = file_paths
834
+ mutation_string = """
835
+ mutation failRunQueueItem($runQueueItemId: ID!, $message: String!, $stage: String!, $filePaths: [String!]) {
836
+ failRunQueueItem(
837
+ input: {
838
+ runQueueItemId: $runQueueItemId
839
+ message: $message
840
+ stage: $stage
841
+ filePaths: $filePaths
842
+ }
843
+ ) {
844
+ success
845
+ }
846
+ }
847
+ """
848
+ else:
849
+ mutation_string = """
850
+ mutation failRunQueueItem($runQueueItemId: ID!) {
851
+ failRunQueueItem(
852
+ input: {
853
+ runQueueItemId: $runQueueItemId
854
+ }
855
+ ) {
856
+ success
857
+ }
858
+ }
859
+ """
860
+
861
+ mutation = gql(mutation_string)
862
+ response = self.gql(mutation, variable_values=variable_values)
863
+ result: bool = response["failRunQueueItem"]["success"]
864
+ return result
865
+
866
+ @normalize_exceptions
867
+ def update_run_queue_item_warning_introspection(self) -> bool:
868
+ _, _, mutations = self.server_info_introspection()
869
+ return "updateRunQueueItemWarning" in mutations
870
+
871
+ def _server_features(self) -> Dict[str, bool]:
872
+ # NOTE: Avoid caching via `@cached_property`, due to undocumented
873
+ # locking behavior before Python 3.12.
874
+ # See: https://github.com/python/cpython/issues/87634
875
+ query = gql(SERVER_FEATURES_QUERY_GQL)
876
+ try:
877
+ response = self.gql(query)
878
+ except Exception as e:
879
+ # Unfortunately we currently have to match on the text of the error message,
880
+ # as the `gql` client raises `Exception` rather than a more specific error.
881
+ if 'Cannot query field "features" on type "ServerInfo".' in str(e):
882
+ self._server_features_cache = {}
883
+ else:
884
+ raise
885
+ else:
886
+ info = ServerFeaturesQuery.model_validate(response).server_info
887
+ if info and (feats := info.features):
888
+ self._server_features_cache = {f.name: f.is_enabled for f in feats if f}
889
+ else:
890
+ self._server_features_cache = {}
891
+ return self._server_features_cache
892
+
893
+ def _server_supports(self, feature: Union[int, str]) -> bool:
894
+ """Return whether the current server supports the given feature.
895
+
896
+ This also caches the underlying lookup of server feature flags,
897
+ and it maps {feature_name (str) -> is_enabled (bool)}.
898
+
899
+ Good to use for features that have a fallback mechanism for older servers.
900
+ """
901
+ # If we're given the protobuf enum value, convert to a string name.
902
+ # NOTE: We deliberately use names (str) instead of enum values (int)
903
+ # as the keys here, since:
904
+ # - the server identifies features by their name, rather than (client-side) enum value
905
+ # - the defined list of client-side flags may be behind the server-side list of flags
906
+ key = ServerFeature.Name(feature) if isinstance(feature, int) else feature
907
+ return self._server_features().get(key) or False
908
+
909
+ @normalize_exceptions
910
+ def update_run_queue_item_warning(
911
+ self,
912
+ run_queue_item_id: str,
913
+ message: str,
914
+ stage: str,
915
+ file_paths: Optional[List[str]] = None,
916
+ ) -> bool:
917
+ if not self.update_run_queue_item_warning_introspection():
918
+ return False
919
+ mutation = gql(
920
+ """
921
+ mutation updateRunQueueItemWarning($runQueueItemId: ID!, $message: String!, $stage: String!, $filePaths: [String!]) {
922
+ updateRunQueueItemWarning(
923
+ input: {
924
+ runQueueItemId: $runQueueItemId
925
+ message: $message
926
+ stage: $stage
927
+ filePaths: $filePaths
928
+ }
929
+ ) {
930
+ success
931
+ }
932
+ }
933
+ """
934
+ )
935
+ response = self.gql(
936
+ mutation,
937
+ variable_values={
938
+ "runQueueItemId": run_queue_item_id,
939
+ "message": message,
940
+ "stage": stage,
941
+ "filePaths": file_paths,
942
+ },
943
+ )
944
+ result: bool = response["updateRunQueueItemWarning"]["success"]
945
+ return result
946
+
947
+ @normalize_exceptions
948
+ def viewer(self) -> Dict[str, Any]:
949
+ query = gql(
950
+ """
951
+ query Viewer{
952
+ viewer {
953
+ id
954
+ entity
955
+ username
956
+ flags
957
+ teams {
958
+ edges {
959
+ node {
960
+ name
961
+ }
962
+ }
963
+ }
964
+ }
965
+ }
966
+ """
967
+ )
968
+ res = self.gql(query)
969
+ return res.get("viewer") or {}
970
+
971
+ @normalize_exceptions
972
+ def max_cli_version(self) -> Optional[str]:
973
+ if self._max_cli_version is not None:
974
+ return self._max_cli_version
975
+
976
+ query_types, server_info_types, _ = self.server_info_introspection()
977
+ cli_version_exists = (
978
+ "serverInfo" in query_types and "cliVersionInfo" in server_info_types
979
+ )
980
+ if not cli_version_exists:
981
+ return None
982
+
983
+ _, server_info = self.viewer_server_info()
984
+ self._max_cli_version = server_info.get("cliVersionInfo", {}).get(
985
+ "max_cli_version"
986
+ )
987
+ return self._max_cli_version
988
+
989
+ @normalize_exceptions
990
+ def viewer_server_info(self) -> Tuple[Dict[str, Any], Dict[str, Any]]:
991
+ local_query = """
992
+ latestLocalVersionInfo {
993
+ outOfDate
994
+ latestVersionString
995
+ versionOnThisInstanceString
996
+ }
997
+ """
998
+ cli_query = """
999
+ serverInfo {
1000
+ cliVersionInfo
1001
+ _LOCAL_QUERY_
1002
+ }
1003
+ """
1004
+ query_template = """
1005
+ query Viewer{
1006
+ viewer {
1007
+ id
1008
+ entity
1009
+ username
1010
+ email
1011
+ flags
1012
+ teams {
1013
+ edges {
1014
+ node {
1015
+ name
1016
+ }
1017
+ }
1018
+ }
1019
+ }
1020
+ _CLI_QUERY_
1021
+ }
1022
+ """
1023
+ query_types, server_info_types, _ = self.server_info_introspection()
1024
+
1025
+ cli_version_exists = (
1026
+ "serverInfo" in query_types and "cliVersionInfo" in server_info_types
1027
+ )
1028
+
1029
+ local_version_exists = (
1030
+ "serverInfo" in query_types
1031
+ and "latestLocalVersionInfo" in server_info_types
1032
+ )
1033
+
1034
+ cli_query_string = "" if not cli_version_exists else cli_query
1035
+ local_query_string = "" if not local_version_exists else local_query
1036
+
1037
+ query_string = query_template.replace("_CLI_QUERY_", cli_query_string).replace(
1038
+ "_LOCAL_QUERY_", local_query_string
1039
+ )
1040
+ query = gql(query_string)
1041
+ res = self.gql(query)
1042
+ return res.get("viewer") or {}, res.get("serverInfo") or {}
1043
+
1044
+ @normalize_exceptions
1045
+ def list_projects(self, entity: Optional[str] = None) -> List[Dict[str, str]]:
1046
+ """List projects in W&B scoped by entity.
1047
+
1048
+ Args:
1049
+ entity (str, optional): The entity to scope this project to.
1050
+
1051
+ Returns:
1052
+ [{"id","name","description"}]
1053
+ """
1054
+ query = gql(
1055
+ """
1056
+ query EntityProjects($entity: String) {
1057
+ models(first: 10, entityName: $entity) {
1058
+ edges {
1059
+ node {
1060
+ id
1061
+ name
1062
+ description
1063
+ }
1064
+ }
1065
+ }
1066
+ }
1067
+ """
1068
+ )
1069
+ project_list: List[Dict[str, str]] = self._flatten_edges(
1070
+ self.gql(
1071
+ query, variable_values={"entity": entity or self.settings("entity")}
1072
+ )["models"]
1073
+ )
1074
+ return project_list
1075
+
1076
+ @normalize_exceptions
1077
+ def project(self, project: str, entity: Optional[str] = None) -> "_Response":
1078
+ """Retrieve project.
1079
+
1080
+ Args:
1081
+ project (str): The project to get details for
1082
+ entity (str, optional): The entity to scope this project to.
1083
+
1084
+ Returns:
1085
+ [{"id","name","repo","dockerImage","description"}]
1086
+ """
1087
+ query = gql(
1088
+ """
1089
+ query ProjectDetails($entity: String, $project: String) {
1090
+ model(name: $project, entityName: $entity) {
1091
+ id
1092
+ name
1093
+ repo
1094
+ dockerImage
1095
+ description
1096
+ }
1097
+ }
1098
+ """
1099
+ )
1100
+ response: _Response = self.gql(
1101
+ query, variable_values={"entity": entity, "project": project}
1102
+ )["model"]
1103
+ return response
1104
+
1105
+ @normalize_exceptions
1106
+ def sweep(
1107
+ self,
1108
+ sweep: str,
1109
+ specs: str,
1110
+ project: Optional[str] = None,
1111
+ entity: Optional[str] = None,
1112
+ ) -> Dict[str, Any]:
1113
+ """Retrieve sweep.
1114
+
1115
+ Args:
1116
+ sweep (str): The sweep to get details for
1117
+ specs (str): history specs
1118
+ project (str, optional): The project to scope this sweep to.
1119
+ entity (str, optional): The entity to scope this sweep to.
1120
+
1121
+ Returns:
1122
+ [{"id","name","repo","dockerImage","description"}]
1123
+ """
1124
+ query = gql(
1125
+ """
1126
+ query SweepWithRuns($entity: String, $project: String, $sweep: String!, $specs: [JSONString!]!) {
1127
+ project(name: $project, entityName: $entity) {
1128
+ sweep(sweepName: $sweep) {
1129
+ id
1130
+ name
1131
+ method
1132
+ state
1133
+ description
1134
+ config
1135
+ createdAt
1136
+ heartbeatAt
1137
+ updatedAt
1138
+ earlyStopJobRunning
1139
+ bestLoss
1140
+ controller
1141
+ scheduler
1142
+ runs {
1143
+ edges {
1144
+ node {
1145
+ name
1146
+ state
1147
+ config
1148
+ exitcode
1149
+ heartbeatAt
1150
+ shouldStop
1151
+ failed
1152
+ stopped
1153
+ running
1154
+ summaryMetrics
1155
+ sampledHistory(specs: $specs)
1156
+ }
1157
+ }
1158
+ }
1159
+ }
1160
+ }
1161
+ }
1162
+ """
1163
+ )
1164
+ entity = entity or self.settings("entity")
1165
+ project = project or self.settings("project")
1166
+ response = self.gql(
1167
+ query,
1168
+ variable_values={
1169
+ "entity": entity,
1170
+ "project": project,
1171
+ "sweep": sweep,
1172
+ "specs": specs,
1173
+ },
1174
+ )
1175
+ if response["project"] is None or response["project"]["sweep"] is None:
1176
+ raise ValueError(f"Sweep {entity}/{project}/{sweep} not found")
1177
+ data: Dict[str, Any] = response["project"]["sweep"]
1178
+ if data:
1179
+ data["runs"] = self._flatten_edges(data["runs"])
1180
+ return data
1181
+
1182
+ @normalize_exceptions
1183
+ def list_runs(
1184
+ self, project: str, entity: Optional[str] = None
1185
+ ) -> List[Dict[str, str]]:
1186
+ """List runs in W&B scoped by project.
1187
+
1188
+ Args:
1189
+ project (str): The project to scope the runs to
1190
+ entity (str, optional): The entity to scope this project to. Defaults to public models
1191
+
1192
+ Returns:
1193
+ [{"id","name","description"}]
1194
+ """
1195
+ query = gql(
1196
+ """
1197
+ query ProjectRuns($model: String!, $entity: String) {
1198
+ model(name: $model, entityName: $entity) {
1199
+ buckets(first: 10) {
1200
+ edges {
1201
+ node {
1202
+ id
1203
+ name
1204
+ displayName
1205
+ description
1206
+ }
1207
+ }
1208
+ }
1209
+ }
1210
+ }
1211
+ """
1212
+ )
1213
+ return self._flatten_edges(
1214
+ self.gql(
1215
+ query,
1216
+ variable_values={
1217
+ "entity": entity or self.settings("entity"),
1218
+ "model": project or self.settings("project"),
1219
+ },
1220
+ )["model"]["buckets"]
1221
+ )
1222
+
1223
+ @normalize_exceptions
1224
+ def run_config(
1225
+ self, project: str, run: Optional[str] = None, entity: Optional[str] = None
1226
+ ) -> Tuple[str, Dict[str, Any], Optional[str], Dict[str, Any]]:
1227
+ """Get the relevant configs for a run.
1228
+
1229
+ Args:
1230
+ project (str): The project to download, (can include bucket)
1231
+ run (str, optional): The run to download
1232
+ entity (str, optional): The entity to scope this project to.
1233
+ """
1234
+ check_httpclient_logger_handler()
1235
+
1236
+ query = gql(
1237
+ """
1238
+ query RunConfigs(
1239
+ $name: String!,
1240
+ $entity: String,
1241
+ $run: String!,
1242
+ $pattern: String!,
1243
+ $includeConfig: Boolean!,
1244
+ ) {
1245
+ model(name: $name, entityName: $entity) {
1246
+ bucket(name: $run) {
1247
+ config @include(if: $includeConfig)
1248
+ commit @include(if: $includeConfig)
1249
+ files(pattern: $pattern) {
1250
+ pageInfo {
1251
+ hasNextPage
1252
+ endCursor
1253
+ }
1254
+ edges {
1255
+ node {
1256
+ name
1257
+ directUrl
1258
+ }
1259
+ }
1260
+ }
1261
+ }
1262
+ }
1263
+ }
1264
+ """
1265
+ )
1266
+
1267
+ variable_values = {
1268
+ "name": project,
1269
+ "run": run,
1270
+ "entity": entity,
1271
+ "includeConfig": True,
1272
+ }
1273
+
1274
+ commit: str = ""
1275
+ config: Dict[str, Any] = {}
1276
+ patch: Optional[str] = None
1277
+ metadata: Dict[str, Any] = {}
1278
+
1279
+ # If we use the `names` parameter on the `files` node, then the server
1280
+ # will helpfully give us and 'open' file handle to the files that don't
1281
+ # exist. This is so that we can upload data to it. However, in this
1282
+ # case, we just want to download that file and not upload to it, so
1283
+ # let's instead query for the files that do exist using `pattern`
1284
+ # (with no wildcards).
1285
+ #
1286
+ # Unfortunately we're unable to construct a single pattern that matches
1287
+ # our 2 files, we would need something like regex for that.
1288
+ for filename in [DIFF_FNAME, METADATA_FNAME]:
1289
+ variable_values["pattern"] = filename
1290
+ response = self.gql(query, variable_values=variable_values)
1291
+ if response["model"] is None:
1292
+ raise CommError(f"Run {entity}/{project}/{run} not found")
1293
+ run_obj: Dict = response["model"]["bucket"]
1294
+ # we only need to fetch this config once
1295
+ if variable_values["includeConfig"]:
1296
+ commit = run_obj["commit"]
1297
+ config = json.loads(run_obj["config"] or "{}")
1298
+ variable_values["includeConfig"] = False
1299
+ if run_obj["files"] is not None:
1300
+ for file_edge in run_obj["files"]["edges"]:
1301
+ name = file_edge["node"]["name"]
1302
+ url = file_edge["node"]["directUrl"]
1303
+ res = requests.get(url)
1304
+ res.raise_for_status()
1305
+ if name == METADATA_FNAME:
1306
+ metadata = res.json()
1307
+ elif name == DIFF_FNAME:
1308
+ patch = res.text
1309
+
1310
+ return commit, config, patch, metadata
1311
+
1312
+ @normalize_exceptions
1313
+ def run_resume_status(
1314
+ self, entity: str, project_name: str, name: str
1315
+ ) -> Optional[Dict[str, Any]]:
1316
+ """Check if a run exists and get resume information.
1317
+
1318
+ Args:
1319
+ entity (str): The entity to scope this project to.
1320
+ project_name (str): The project to download, (can include bucket)
1321
+ name (str): The run to download
1322
+ """
1323
+ # Pulling wandbConfig.start_time is required so that we can determine if a run has actually started
1324
+ query = gql(
1325
+ """
1326
+ query RunResumeStatus($project: String, $entity: String, $name: String!) {
1327
+ model(name: $project, entityName: $entity) {
1328
+ id
1329
+ name
1330
+ entity {
1331
+ id
1332
+ name
1333
+ }
1334
+
1335
+ bucket(name: $name, missingOk: true) {
1336
+ id
1337
+ name
1338
+ summaryMetrics
1339
+ displayName
1340
+ logLineCount
1341
+ historyLineCount
1342
+ eventsLineCount
1343
+ historyTail
1344
+ eventsTail
1345
+ config
1346
+ tags
1347
+ wandbConfig(keys: ["t"])
1348
+ }
1349
+ }
1350
+ }
1351
+ """
1352
+ )
1353
+
1354
+ response = self.gql(
1355
+ query,
1356
+ variable_values={
1357
+ "entity": entity,
1358
+ "project": project_name,
1359
+ "name": name,
1360
+ },
1361
+ )
1362
+
1363
+ if "model" not in response or "bucket" not in (response["model"] or {}):
1364
+ return None
1365
+
1366
+ project = response["model"]
1367
+ self.set_setting("project", project_name)
1368
+ if "entity" in project:
1369
+ self.set_setting("entity", project["entity"]["name"])
1370
+
1371
+ result: Dict[str, Any] = project["bucket"]
1372
+
1373
+ return result
1374
+
1375
+ @normalize_exceptions
1376
+ def check_stop_requested(
1377
+ self, project_name: str, entity_name: str, run_id: str
1378
+ ) -> bool:
1379
+ query = gql(
1380
+ """
1381
+ query RunStoppedStatus($projectName: String, $entityName: String, $runId: String!) {
1382
+ project(name:$projectName, entityName:$entityName) {
1383
+ run(name:$runId) {
1384
+ stopped
1385
+ }
1386
+ }
1387
+ }
1388
+ """
1389
+ )
1390
+
1391
+ response = self.gql(
1392
+ query,
1393
+ variable_values={
1394
+ "projectName": project_name,
1395
+ "entityName": entity_name,
1396
+ "runId": run_id,
1397
+ },
1398
+ )
1399
+
1400
+ project = response.get("project", None)
1401
+ if not project:
1402
+ return False
1403
+ run = project.get("run", None)
1404
+ if not run:
1405
+ return False
1406
+
1407
+ status: bool = run["stopped"]
1408
+ return status
1409
+
1410
+ def format_project(self, project: str) -> str:
1411
+ return re.sub(r"\W+", "-", project.lower()).strip("-_")
1412
+
1413
+ @normalize_exceptions
1414
+ def upsert_project(
1415
+ self,
1416
+ project: str,
1417
+ id: Optional[str] = None,
1418
+ description: Optional[str] = None,
1419
+ entity: Optional[str] = None,
1420
+ ) -> Dict[str, Any]:
1421
+ """Create a new project.
1422
+
1423
+ Args:
1424
+ project (str): The project to create
1425
+ description (str, optional): A description of this project
1426
+ entity (str, optional): The entity to scope this project to.
1427
+ """
1428
+ mutation = gql(
1429
+ """
1430
+ mutation UpsertModel($name: String!, $id: String, $entity: String!, $description: String, $repo: String) {
1431
+ upsertModel(input: { id: $id, name: $name, entityName: $entity, description: $description, repo: $repo }) {
1432
+ model {
1433
+ name
1434
+ description
1435
+ }
1436
+ }
1437
+ }
1438
+ """
1439
+ )
1440
+ response = self.gql(
1441
+ mutation,
1442
+ variable_values={
1443
+ "name": self.format_project(project),
1444
+ "entity": entity or self.settings("entity"),
1445
+ "description": description,
1446
+ "id": id,
1447
+ },
1448
+ )
1449
+ # TODO(jhr): Commenting out 'repo' field for cling, add back
1450
+ # 'description': description, 'repo': self.git.remote_url, 'id': id})
1451
+ result: Dict[str, Any] = response["upsertModel"]["model"]
1452
+ return result
1453
+
1454
+ @normalize_exceptions
1455
+ def entity_is_team(self, entity: str) -> bool:
1456
+ query = gql(
1457
+ """
1458
+ query EntityIsTeam($entity: String!) {
1459
+ entity(name: $entity) {
1460
+ id
1461
+ isTeam
1462
+ }
1463
+ }
1464
+ """
1465
+ )
1466
+ variable_values = {
1467
+ "entity": entity,
1468
+ }
1469
+
1470
+ res = self.gql(query, variable_values)
1471
+ if res.get("entity") is None:
1472
+ raise Exception(
1473
+ f"Error fetching entity {entity} "
1474
+ "check that you have access to this entity"
1475
+ )
1476
+
1477
+ is_team: bool = res["entity"]["isTeam"]
1478
+ return is_team
1479
+
1480
+ @normalize_exceptions
1481
+ def get_project_run_queues(self, entity: str, project: str) -> List[Dict[str, str]]:
1482
+ query = gql(
1483
+ """
1484
+ query ProjectRunQueues($entity: String!, $projectName: String!){
1485
+ project(entityName: $entity, name: $projectName) {
1486
+ runQueues {
1487
+ id
1488
+ name
1489
+ createdBy
1490
+ access
1491
+ }
1492
+ }
1493
+ }
1494
+ """
1495
+ )
1496
+ variable_values = {
1497
+ "projectName": project,
1498
+ "entity": entity,
1499
+ }
1500
+
1501
+ res = self.gql(query, variable_values)
1502
+ if res.get("project") is None:
1503
+ # circular dependency: (LAUNCH_DEFAULT_PROJECT = model-registry)
1504
+ if project == "model-registry":
1505
+ msg = (
1506
+ f"Error fetching run queues for {entity} "
1507
+ "check that you have access to this entity and project"
1508
+ )
1509
+ else:
1510
+ msg = (
1511
+ f"Error fetching run queues for {entity}/{project} "
1512
+ "check that you have access to this entity and project"
1513
+ )
1514
+
1515
+ raise Exception(msg)
1516
+
1517
+ project_run_queues: List[Dict[str, str]] = res["project"]["runQueues"]
1518
+ return project_run_queues
1519
+
1520
+ @normalize_exceptions
1521
+ def create_default_resource_config(
1522
+ self,
1523
+ entity: str,
1524
+ resource: str,
1525
+ config: str,
1526
+ template_variables: Optional[Dict[str, Union[float, int, str]]],
1527
+ ) -> Optional[Dict[str, Any]]:
1528
+ if not self.create_default_resource_config_introspection():
1529
+ raise Exception()
1530
+ supports_template_vars, _ = self.push_to_run_queue_introspection()
1531
+
1532
+ mutation_params = """
1533
+ $entityName: String!,
1534
+ $resource: String!,
1535
+ $config: JSONString!
1536
+ """
1537
+ mutation_inputs = """
1538
+ entityName: $entityName,
1539
+ resource: $resource,
1540
+ config: $config
1541
+ """
1542
+
1543
+ if supports_template_vars:
1544
+ mutation_params += ", $templateVariables: JSONString"
1545
+ mutation_inputs += ", templateVariables: $templateVariables"
1546
+ else:
1547
+ if template_variables is not None:
1548
+ raise UnsupportedError(
1549
+ "server does not support template variables, please update server instance to >=0.46"
1550
+ )
1551
+
1552
+ variable_values = {
1553
+ "entityName": entity,
1554
+ "resource": resource,
1555
+ "config": config,
1556
+ }
1557
+ if supports_template_vars:
1558
+ if template_variables is not None:
1559
+ variable_values["templateVariables"] = json.dumps(template_variables)
1560
+ else:
1561
+ variable_values["templateVariables"] = "{}"
1562
+
1563
+ query = gql(
1564
+ f"""
1565
+ mutation createDefaultResourceConfig(
1566
+ {mutation_params}
1567
+ ) {{
1568
+ createDefaultResourceConfig(
1569
+ input: {{
1570
+ {mutation_inputs}
1571
+ }}
1572
+ ) {{
1573
+ defaultResourceConfigID
1574
+ success
1575
+ }}
1576
+ }}
1577
+ """
1578
+ )
1579
+
1580
+ result: Optional[Dict[str, Any]] = self.gql(query, variable_values)[
1581
+ "createDefaultResourceConfig"
1582
+ ]
1583
+ return result
1584
+
1585
+ @normalize_exceptions
1586
+ def create_run_queue(
1587
+ self,
1588
+ entity: str,
1589
+ project: str,
1590
+ queue_name: str,
1591
+ access: str,
1592
+ prioritization_mode: Optional[str] = None,
1593
+ config_id: Optional[str] = None,
1594
+ ) -> Optional[Dict[str, Any]]:
1595
+ (
1596
+ create_run_queue,
1597
+ supports_drc,
1598
+ supports_prioritization,
1599
+ ) = self.create_run_queue_introspection()
1600
+ if not create_run_queue:
1601
+ raise UnsupportedError(
1602
+ "run queue creation is not supported by this version of "
1603
+ "wandb server. Consider updating to the latest version."
1604
+ )
1605
+ if not supports_drc and config_id is not None:
1606
+ raise UnsupportedError(
1607
+ "default resource configurations are not supported by this version "
1608
+ "of wandb server. Consider updating to the latest version."
1609
+ )
1610
+ if not supports_prioritization and prioritization_mode is not None:
1611
+ raise UnsupportedError(
1612
+ "launch prioritization is not supported by this version of "
1613
+ "wandb server. Consider updating to the latest version."
1614
+ )
1615
+
1616
+ if supports_prioritization:
1617
+ query = gql(
1618
+ """
1619
+ mutation createRunQueue(
1620
+ $entity: String!,
1621
+ $project: String!,
1622
+ $queueName: String!,
1623
+ $access: RunQueueAccessType!,
1624
+ $prioritizationMode: RunQueuePrioritizationMode,
1625
+ $defaultResourceConfigID: ID,
1626
+ ) {
1627
+ createRunQueue(
1628
+ input: {
1629
+ entityName: $entity,
1630
+ projectName: $project,
1631
+ queueName: $queueName,
1632
+ access: $access,
1633
+ prioritizationMode: $prioritizationMode
1634
+ defaultResourceConfigID: $defaultResourceConfigID
1635
+ }
1636
+ ) {
1637
+ success
1638
+ queueID
1639
+ }
1640
+ }
1641
+ """
1642
+ )
1643
+ variable_values = {
1644
+ "entity": entity,
1645
+ "project": project,
1646
+ "queueName": queue_name,
1647
+ "access": access,
1648
+ "prioritizationMode": prioritization_mode,
1649
+ "defaultResourceConfigID": config_id,
1650
+ }
1651
+ else:
1652
+ query = gql(
1653
+ """
1654
+ mutation createRunQueue(
1655
+ $entity: String!,
1656
+ $project: String!,
1657
+ $queueName: String!,
1658
+ $access: RunQueueAccessType!,
1659
+ $defaultResourceConfigID: ID,
1660
+ ) {
1661
+ createRunQueue(
1662
+ input: {
1663
+ entityName: $entity,
1664
+ projectName: $project,
1665
+ queueName: $queueName,
1666
+ access: $access,
1667
+ defaultResourceConfigID: $defaultResourceConfigID
1668
+ }
1669
+ ) {
1670
+ success
1671
+ queueID
1672
+ }
1673
+ }
1674
+ """
1675
+ )
1676
+ variable_values = {
1677
+ "entity": entity,
1678
+ "project": project,
1679
+ "queueName": queue_name,
1680
+ "access": access,
1681
+ "defaultResourceConfigID": config_id,
1682
+ }
1683
+
1684
+ result: Optional[Dict[str, Any]] = self.gql(query, variable_values)[
1685
+ "createRunQueue"
1686
+ ]
1687
+ return result
1688
+
1689
+ @normalize_exceptions
1690
+ def upsert_run_queue(
1691
+ self,
1692
+ queue_name: str,
1693
+ entity: str,
1694
+ resource_type: str,
1695
+ resource_config: dict,
1696
+ project: str = LAUNCH_DEFAULT_PROJECT,
1697
+ prioritization_mode: Optional[str] = None,
1698
+ template_variables: Optional[dict] = None,
1699
+ external_links: Optional[dict] = None,
1700
+ ) -> Optional[Dict[str, Any]]:
1701
+ if not self.upsert_run_queue_introspection():
1702
+ raise UnsupportedError(
1703
+ "upserting run queues is not supported by this version of "
1704
+ "wandb server. Consider updating to the latest version."
1705
+ )
1706
+ query = gql(
1707
+ """
1708
+ mutation upsertRunQueue(
1709
+ $entityName: String!
1710
+ $projectName: String!
1711
+ $queueName: String!
1712
+ $resourceType: String!
1713
+ $resourceConfig: JSONString!
1714
+ $templateVariables: JSONString
1715
+ $prioritizationMode: RunQueuePrioritizationMode
1716
+ $externalLinks: JSONString
1717
+ $clientMutationId: String
1718
+ ) {
1719
+ upsertRunQueue(
1720
+ input: {
1721
+ entityName: $entityName
1722
+ projectName: $projectName
1723
+ queueName: $queueName
1724
+ resourceType: $resourceType
1725
+ resourceConfig: $resourceConfig
1726
+ templateVariables: $templateVariables
1727
+ prioritizationMode: $prioritizationMode
1728
+ externalLinks: $externalLinks
1729
+ clientMutationId: $clientMutationId
1730
+ }
1731
+ ) {
1732
+ success
1733
+ configSchemaValidationErrors
1734
+ }
1735
+ }
1736
+ """
1737
+ )
1738
+ variable_values = {
1739
+ "entityName": entity,
1740
+ "projectName": project,
1741
+ "queueName": queue_name,
1742
+ "resourceType": resource_type,
1743
+ "resourceConfig": json.dumps(resource_config),
1744
+ "templateVariables": (
1745
+ json.dumps(template_variables) if template_variables else None
1746
+ ),
1747
+ "prioritizationMode": prioritization_mode,
1748
+ "externalLinks": json.dumps(external_links) if external_links else None,
1749
+ }
1750
+ result: Dict[str, Any] = self.gql(query, variable_values)
1751
+ return result["upsertRunQueue"]
1752
+
1753
+ @normalize_exceptions
1754
+ def push_to_run_queue_by_name(
1755
+ self,
1756
+ entity: str,
1757
+ project: str,
1758
+ queue_name: str,
1759
+ run_spec: str,
1760
+ template_variables: Optional[Dict[str, Union[int, float, str]]],
1761
+ priority: Optional[int] = None,
1762
+ ) -> Optional[Dict[str, Any]]:
1763
+ self.push_to_run_queue_introspection()
1764
+ """Queryless mutation, should be used before legacy fallback method."""
1765
+
1766
+ mutation_params = """
1767
+ $entityName: String!,
1768
+ $projectName: String!,
1769
+ $queueName: String!,
1770
+ $runSpec: JSONString!
1771
+ """
1772
+
1773
+ mutation_input = """
1774
+ entityName: $entityName,
1775
+ projectName: $projectName,
1776
+ queueName: $queueName,
1777
+ runSpec: $runSpec
1778
+ """
1779
+
1780
+ variables: Dict[str, Any] = {
1781
+ "entityName": entity,
1782
+ "projectName": project,
1783
+ "queueName": queue_name,
1784
+ "runSpec": run_spec,
1785
+ }
1786
+ if self.server_push_to_run_queue_supports_priority:
1787
+ if priority is not None:
1788
+ variables["priority"] = priority
1789
+ mutation_params += ", $priority: Int"
1790
+ mutation_input += ", priority: $priority"
1791
+ else:
1792
+ if priority is not None:
1793
+ raise UnsupportedError(
1794
+ "server does not support priority, please update server instance to >=0.46"
1795
+ )
1796
+
1797
+ if self.server_supports_template_variables:
1798
+ if template_variables is not None:
1799
+ variables.update(
1800
+ {"templateVariableValues": json.dumps(template_variables)}
1801
+ )
1802
+ mutation_params += ", $templateVariableValues: JSONString"
1803
+ mutation_input += ", templateVariableValues: $templateVariableValues"
1804
+ else:
1805
+ if template_variables is not None:
1806
+ raise UnsupportedError(
1807
+ "server does not support template variables, please update server instance to >=0.46"
1808
+ )
1809
+
1810
+ mutation = gql(
1811
+ f"""
1812
+ mutation pushToRunQueueByName(
1813
+ {mutation_params}
1814
+ ) {{
1815
+ pushToRunQueueByName(
1816
+ input: {{
1817
+ {mutation_input}
1818
+ }}
1819
+ ) {{
1820
+ runQueueItemId
1821
+ runSpec
1822
+ }}
1823
+ }}
1824
+ """
1825
+ )
1826
+
1827
+ try:
1828
+ result: Optional[Dict[str, Any]] = self.gql(
1829
+ mutation, variables, check_retry_fn=util.no_retry_4xx
1830
+ ).get("pushToRunQueueByName")
1831
+ if not result:
1832
+ return None
1833
+
1834
+ if result.get("runSpec"):
1835
+ run_spec = json.loads(str(result["runSpec"]))
1836
+ result["runSpec"] = run_spec
1837
+
1838
+ return result
1839
+ except Exception as e:
1840
+ if (
1841
+ 'Cannot query field "runSpec" on type "PushToRunQueueByNamePayload"'
1842
+ not in str(e)
1843
+ ):
1844
+ return None
1845
+
1846
+ mutation_no_runspec = gql(
1847
+ """
1848
+ mutation pushToRunQueueByName(
1849
+ $entityName: String!,
1850
+ $projectName: String!,
1851
+ $queueName: String!,
1852
+ $runSpec: JSONString!,
1853
+ ) {
1854
+ pushToRunQueueByName(
1855
+ input: {
1856
+ entityName: $entityName,
1857
+ projectName: $projectName,
1858
+ queueName: $queueName,
1859
+ runSpec: $runSpec
1860
+ }
1861
+ ) {
1862
+ runQueueItemId
1863
+ }
1864
+ }
1865
+ """
1866
+ )
1867
+
1868
+ try:
1869
+ result = self.gql(
1870
+ mutation_no_runspec, variables, check_retry_fn=util.no_retry_4xx
1871
+ ).get("pushToRunQueueByName")
1872
+ except Exception:
1873
+ result = None
1874
+
1875
+ return result
1876
+
1877
+ @normalize_exceptions
1878
+ def push_to_run_queue(
1879
+ self,
1880
+ queue_name: str,
1881
+ launch_spec: Dict[str, str],
1882
+ template_variables: Optional[dict],
1883
+ project_queue: str,
1884
+ priority: Optional[int] = None,
1885
+ ) -> Optional[Dict[str, Any]]:
1886
+ self.push_to_run_queue_introspection()
1887
+ entity = launch_spec.get("queue_entity") or launch_spec["entity"]
1888
+ run_spec = json.dumps(launch_spec)
1889
+
1890
+ push_result = self.push_to_run_queue_by_name(
1891
+ entity, project_queue, queue_name, run_spec, template_variables, priority
1892
+ )
1893
+
1894
+ if push_result:
1895
+ return push_result
1896
+
1897
+ if priority is not None:
1898
+ # Cannot proceed with legacy method if priority is set
1899
+ return None
1900
+
1901
+ """ Legacy Method """
1902
+ queues_found = self.get_project_run_queues(entity, project_queue)
1903
+ matching_queues = [
1904
+ q
1905
+ for q in queues_found
1906
+ if q["name"] == queue_name
1907
+ # ensure user has access to queue
1908
+ and (
1909
+ # TODO: User created queues in the UI have USER access
1910
+ q["access"] in ["PROJECT", "USER"]
1911
+ or q["createdBy"] == self.default_entity
1912
+ )
1913
+ ]
1914
+ if not matching_queues:
1915
+ # in the case of a missing default queue. create it
1916
+ if queue_name == "default":
1917
+ wandb.termlog(
1918
+ f"No default queue existing for entity: {entity} in project: {project_queue}, creating one."
1919
+ )
1920
+ res = self.create_run_queue(
1921
+ launch_spec["entity"],
1922
+ project_queue,
1923
+ queue_name,
1924
+ access="PROJECT",
1925
+ )
1926
+
1927
+ if res is None or res.get("queueID") is None:
1928
+ wandb.termerror(
1929
+ f"Unable to create default queue for entity: {entity} on project: {project_queue}. Run could not be added to a queue"
1930
+ )
1931
+ return None
1932
+ queue_id = res["queueID"]
1933
+
1934
+ else:
1935
+ if project_queue == "model-registry":
1936
+ _msg = f"Unable to push to run queue {queue_name}. Queue not found."
1937
+ else:
1938
+ _msg = f"Unable to push to run queue {project_queue}/{queue_name}. Queue not found."
1939
+ wandb.termwarn(_msg)
1940
+ return None
1941
+ elif len(matching_queues) > 1:
1942
+ wandb.termerror(
1943
+ f"Unable to push to run queue {queue_name}. More than one queue found with this name."
1944
+ )
1945
+ return None
1946
+ else:
1947
+ queue_id = matching_queues[0]["id"]
1948
+ spec_json = json.dumps(launch_spec)
1949
+ variables = {"queueID": queue_id, "runSpec": spec_json}
1950
+
1951
+ mutation_params = """
1952
+ $queueID: ID!,
1953
+ $runSpec: JSONString!
1954
+ """
1955
+ mutation_input = """
1956
+ queueID: $queueID,
1957
+ runSpec: $runSpec
1958
+ """
1959
+ if self.server_supports_template_variables:
1960
+ if template_variables is not None:
1961
+ mutation_params += ", $templateVariableValues: JSONString"
1962
+ mutation_input += ", templateVariableValues: $templateVariableValues"
1963
+ variables.update(
1964
+ {"templateVariableValues": json.dumps(template_variables)}
1965
+ )
1966
+ else:
1967
+ if template_variables is not None:
1968
+ raise UnsupportedError(
1969
+ "server does not support template variables, please update server instance to >=0.46"
1970
+ )
1971
+
1972
+ mutation = gql(
1973
+ f"""
1974
+ mutation pushToRunQueue(
1975
+ {mutation_params}
1976
+ ) {{
1977
+ pushToRunQueue(
1978
+ input: {{{mutation_input}}}
1979
+ ) {{
1980
+ runQueueItemId
1981
+ }}
1982
+ }}
1983
+ """
1984
+ )
1985
+
1986
+ response = self.gql(mutation, variable_values=variables)
1987
+ if not response.get("pushToRunQueue"):
1988
+ raise CommError(f"Error pushing run queue item to queue {queue_name}.")
1989
+
1990
+ result: Optional[Dict[str, Any]] = response["pushToRunQueue"]
1991
+ return result
1992
+
1993
+ @normalize_exceptions
1994
+ def pop_from_run_queue(
1995
+ self,
1996
+ queue_name: str,
1997
+ entity: Optional[str] = None,
1998
+ project: Optional[str] = None,
1999
+ agent_id: Optional[str] = None,
2000
+ ) -> Optional[Dict[str, Any]]:
2001
+ mutation = gql(
2002
+ """
2003
+ mutation popFromRunQueue($entity: String!, $project: String!, $queueName: String!, $launchAgentId: ID) {
2004
+ popFromRunQueue(input: {
2005
+ entityName: $entity,
2006
+ projectName: $project,
2007
+ queueName: $queueName,
2008
+ launchAgentId: $launchAgentId
2009
+ }) {
2010
+ runQueueItemId
2011
+ runSpec
2012
+ }
2013
+ }
2014
+ """
2015
+ )
2016
+ response = self.gql(
2017
+ mutation,
2018
+ variable_values={
2019
+ "entity": entity,
2020
+ "project": project,
2021
+ "queueName": queue_name,
2022
+ "launchAgentId": agent_id,
2023
+ },
2024
+ )
2025
+ result: Optional[Dict[str, Any]] = response["popFromRunQueue"]
2026
+ return result
2027
+
2028
+ @normalize_exceptions
2029
+ def ack_run_queue_item(self, item_id: str, run_id: Optional[str] = None) -> bool:
2030
+ mutation = gql(
2031
+ """
2032
+ mutation ackRunQueueItem($itemId: ID!, $runId: String!) {
2033
+ ackRunQueueItem(input: { runQueueItemId: $itemId, runName: $runId }) {
2034
+ success
2035
+ }
2036
+ }
2037
+ """
2038
+ )
2039
+ response = self.gql(
2040
+ mutation, variable_values={"itemId": item_id, "runId": str(run_id)}
2041
+ )
2042
+ if not response["ackRunQueueItem"]["success"]:
2043
+ raise CommError(
2044
+ "Error acking run queue item. Item may have already been acknowledged by another process"
2045
+ )
2046
+ result: bool = response["ackRunQueueItem"]["success"]
2047
+ return result
2048
+
2049
+ @normalize_exceptions
2050
+ def create_launch_agent_fields_introspection(self) -> List:
2051
+ if self.create_launch_agent_input_info:
2052
+ return self.create_launch_agent_input_info
2053
+ query_string = """
2054
+ query ProbeServerCreateLaunchAgentInput {
2055
+ CreateLaunchAgentInputInfoType: __type(name:"CreateLaunchAgentInput") {
2056
+ inputFields{
2057
+ name
2058
+ }
2059
+ }
2060
+ }
2061
+ """
2062
+
2063
+ query = gql(query_string)
2064
+ res = self.gql(query)
2065
+
2066
+ self.create_launch_agent_input_info = [
2067
+ field.get("name", "")
2068
+ for field in res.get("CreateLaunchAgentInputInfoType", {}).get(
2069
+ "inputFields", [{}]
2070
+ )
2071
+ ]
2072
+ return self.create_launch_agent_input_info
2073
+
2074
+ @normalize_exceptions
2075
+ def create_launch_agent(
2076
+ self,
2077
+ entity: str,
2078
+ project: str,
2079
+ queues: List[str],
2080
+ agent_config: Dict[str, Any],
2081
+ version: str,
2082
+ gorilla_agent_support: bool,
2083
+ ) -> dict:
2084
+ project_queues = self.get_project_run_queues(entity, project)
2085
+ if not project_queues:
2086
+ # create default queue if it doesn't already exist
2087
+ default = self.create_run_queue(
2088
+ entity, project, "default", access="PROJECT"
2089
+ )
2090
+ if default is None or default.get("queueID") is None:
2091
+ raise CommError(
2092
+ f"Unable to create default queue for {entity}/{project}. No queues for agent to poll"
2093
+ )
2094
+ project_queues = [{"id": default["queueID"], "name": "default"}]
2095
+ polling_queue_ids = [
2096
+ q["id"] for q in project_queues if q["name"] in queues
2097
+ ] # filter to poll specified queues
2098
+ if len(polling_queue_ids) != len(queues):
2099
+ raise CommError(
2100
+ f"Could not start launch agent: Not all of requested queues ({', '.join(queues)}) found. "
2101
+ f"Available queues for this project: {','.join([q['name'] for q in project_queues])}"
2102
+ )
2103
+
2104
+ if not gorilla_agent_support:
2105
+ # if gorilla doesn't support launch agents, return a client-generated id
2106
+ return {
2107
+ "success": True,
2108
+ "launchAgentId": None,
2109
+ }
2110
+
2111
+ hostname = socket.gethostname()
2112
+
2113
+ variable_values = {
2114
+ "entity": entity,
2115
+ "project": project,
2116
+ "queues": polling_queue_ids,
2117
+ "hostname": hostname,
2118
+ }
2119
+
2120
+ mutation_params = """
2121
+ $entity: String!,
2122
+ $project: String!,
2123
+ $queues: [ID!]!,
2124
+ $hostname: String!
2125
+ """
2126
+
2127
+ mutation_input = """
2128
+ entityName: $entity,
2129
+ projectName: $project,
2130
+ runQueues: $queues,
2131
+ hostname: $hostname
2132
+ """
2133
+
2134
+ if "agentConfig" in self.create_launch_agent_fields_introspection():
2135
+ variable_values["agentConfig"] = json.dumps(agent_config)
2136
+ mutation_params += ", $agentConfig: JSONString"
2137
+ mutation_input += ", agentConfig: $agentConfig"
2138
+ if "version" in self.create_launch_agent_fields_introspection():
2139
+ variable_values["version"] = version
2140
+ mutation_params += ", $version: String"
2141
+ mutation_input += ", version: $version"
2142
+
2143
+ mutation = gql(
2144
+ f"""
2145
+ mutation createLaunchAgent(
2146
+ {mutation_params}
2147
+ ) {{
2148
+ createLaunchAgent(
2149
+ input: {{
2150
+ {mutation_input}
2151
+ }}
2152
+ ) {{
2153
+ launchAgentId
2154
+ }}
2155
+ }}
2156
+ """
2157
+ )
2158
+ result: dict = self.gql(mutation, variable_values)["createLaunchAgent"]
2159
+ return result
2160
+
2161
+ @normalize_exceptions
2162
+ def update_launch_agent_status(
2163
+ self,
2164
+ agent_id: str,
2165
+ status: str,
2166
+ gorilla_agent_support: bool,
2167
+ ) -> dict:
2168
+ if not gorilla_agent_support:
2169
+ # if gorilla doesn't support launch agents, this is a no-op
2170
+ return {
2171
+ "success": True,
2172
+ }
2173
+
2174
+ mutation = gql(
2175
+ """
2176
+ mutation updateLaunchAgent($agentId: ID!, $agentStatus: String){
2177
+ updateLaunchAgent(
2178
+ input: {
2179
+ launchAgentId: $agentId
2180
+ agentStatus: $agentStatus
2181
+ }
2182
+ ) {
2183
+ success
2184
+ }
2185
+ }
2186
+ """
2187
+ )
2188
+ variable_values = {
2189
+ "agentId": agent_id,
2190
+ "agentStatus": status,
2191
+ }
2192
+ result: dict = self.gql(mutation, variable_values)["updateLaunchAgent"]
2193
+ return result
2194
+
2195
+ @normalize_exceptions
2196
+ def get_launch_agent(self, agent_id: str, gorilla_agent_support: bool) -> dict:
2197
+ if not gorilla_agent_support:
2198
+ return {
2199
+ "id": None,
2200
+ "name": "",
2201
+ "stopPolling": False,
2202
+ }
2203
+ query = gql(
2204
+ """
2205
+ query LaunchAgent($agentId: ID!) {
2206
+ launchAgent(id: $agentId) {
2207
+ id
2208
+ name
2209
+ runQueues
2210
+ hostname
2211
+ agentStatus
2212
+ stopPolling
2213
+ heartbeatAt
2214
+ }
2215
+ }
2216
+ """
2217
+ )
2218
+ variable_values = {
2219
+ "agentId": agent_id,
2220
+ }
2221
+ result: dict = self.gql(query, variable_values)["launchAgent"]
2222
+ return result
2223
+
2224
+ @normalize_exceptions
2225
+ def upsert_run(
2226
+ self,
2227
+ id: Optional[str] = None,
2228
+ name: Optional[str] = None,
2229
+ project: Optional[str] = None,
2230
+ host: Optional[str] = None,
2231
+ group: Optional[str] = None,
2232
+ tags: Optional[List[str]] = None,
2233
+ config: Optional[dict] = None,
2234
+ description: Optional[str] = None,
2235
+ entity: Optional[str] = None,
2236
+ state: Optional[str] = None,
2237
+ display_name: Optional[str] = None,
2238
+ notes: Optional[str] = None,
2239
+ repo: Optional[str] = None,
2240
+ job_type: Optional[str] = None,
2241
+ program_path: Optional[str] = None,
2242
+ commit: Optional[str] = None,
2243
+ sweep_name: Optional[str] = None,
2244
+ summary_metrics: Optional[str] = None,
2245
+ num_retries: Optional[int] = None,
2246
+ ) -> Tuple[dict, bool, Optional[List]]:
2247
+ """Update a run.
2248
+
2249
+ Args:
2250
+ id (str, optional): The existing run to update
2251
+ name (str, optional): The name of the run to create
2252
+ group (str, optional): Name of the group this run is a part of
2253
+ project (str, optional): The name of the project
2254
+ host (str, optional): The name of the host
2255
+ tags (list, optional): A list of tags to apply to the run
2256
+ config (dict, optional): The latest config params
2257
+ description (str, optional): A description of this project
2258
+ entity (str, optional): The entity to scope this project to.
2259
+ display_name (str, optional): The display name of this project
2260
+ notes (str, optional): Notes about this run
2261
+ repo (str, optional): Url of the program's repository.
2262
+ state (str, optional): State of the program.
2263
+ job_type (str, optional): Type of job, e.g 'train'.
2264
+ program_path (str, optional): Path to the program.
2265
+ commit (str, optional): The Git SHA to associate the run with
2266
+ sweep_name (str, optional): The name of the sweep this run is a part of
2267
+ summary_metrics (str, optional): The JSON summary metrics
2268
+ num_retries (int, optional): Number of retries
2269
+ """
2270
+ query_string = """
2271
+ mutation UpsertBucket(
2272
+ $id: String,
2273
+ $name: String,
2274
+ $project: String,
2275
+ $entity: String,
2276
+ $groupName: String,
2277
+ $description: String,
2278
+ $displayName: String,
2279
+ $notes: String,
2280
+ $commit: String,
2281
+ $config: JSONString,
2282
+ $host: String,
2283
+ $debug: Boolean,
2284
+ $program: String,
2285
+ $repo: String,
2286
+ $jobType: String,
2287
+ $state: String,
2288
+ $sweep: String,
2289
+ $tags: [String!],
2290
+ $summaryMetrics: JSONString,
2291
+ ) {
2292
+ upsertBucket(input: {
2293
+ id: $id,
2294
+ name: $name,
2295
+ groupName: $groupName,
2296
+ modelName: $project,
2297
+ entityName: $entity,
2298
+ description: $description,
2299
+ displayName: $displayName,
2300
+ notes: $notes,
2301
+ config: $config,
2302
+ commit: $commit,
2303
+ host: $host,
2304
+ debug: $debug,
2305
+ jobProgram: $program,
2306
+ jobRepo: $repo,
2307
+ jobType: $jobType,
2308
+ state: $state,
2309
+ sweep: $sweep,
2310
+ tags: $tags,
2311
+ summaryMetrics: $summaryMetrics,
2312
+ }) {
2313
+ bucket {
2314
+ id
2315
+ name
2316
+ displayName
2317
+ description
2318
+ config
2319
+ sweepName
2320
+ project {
2321
+ id
2322
+ name
2323
+ entity {
2324
+ id
2325
+ name
2326
+ }
2327
+ }
2328
+ historyLineCount
2329
+ }
2330
+ inserted
2331
+ _Server_Settings_
2332
+ }
2333
+ }
2334
+ """
2335
+ self.server_settings_introspection()
2336
+
2337
+ server_settings_string = (
2338
+ """
2339
+ serverSettings {
2340
+ serverMessages{
2341
+ utfText
2342
+ plainText
2343
+ htmlText
2344
+ messageType
2345
+ messageLevel
2346
+ }
2347
+ }
2348
+ """
2349
+ if self._server_settings_type
2350
+ else ""
2351
+ )
2352
+
2353
+ query_string = query_string.replace("_Server_Settings_", server_settings_string)
2354
+ mutation = gql(query_string)
2355
+ config_str = json.dumps(config) if config else None
2356
+ if not description or description.isspace():
2357
+ description = None
2358
+
2359
+ kwargs = {}
2360
+ if num_retries is not None:
2361
+ kwargs["num_retries"] = num_retries
2362
+
2363
+ variable_values = {
2364
+ "id": id,
2365
+ "entity": entity or self.settings("entity"),
2366
+ "name": name,
2367
+ "project": project or util.auto_project_name(program_path),
2368
+ "groupName": group,
2369
+ "tags": tags,
2370
+ "description": description,
2371
+ "config": config_str,
2372
+ "commit": commit,
2373
+ "displayName": display_name,
2374
+ "notes": notes,
2375
+ "host": None
2376
+ if self.settings().get("anonymous") in ["allow", "must"]
2377
+ else host,
2378
+ "debug": env.is_debug(env=self._environ),
2379
+ "repo": repo,
2380
+ "program": program_path,
2381
+ "jobType": job_type,
2382
+ "state": state,
2383
+ "sweep": sweep_name,
2384
+ "summaryMetrics": summary_metrics,
2385
+ }
2386
+
2387
+ # retry conflict errors for 2 minutes, default to no_auth_retry
2388
+ check_retry_fn = util.make_check_retry_fn(
2389
+ check_fn=util.check_retry_conflict_or_gone,
2390
+ check_timedelta=datetime.timedelta(minutes=2),
2391
+ fallback_retry_fn=util.no_retry_auth,
2392
+ )
2393
+
2394
+ response = self.gql(
2395
+ mutation,
2396
+ variable_values=variable_values,
2397
+ check_retry_fn=check_retry_fn,
2398
+ **kwargs,
2399
+ )
2400
+
2401
+ run_obj: Dict[str, Dict[str, Dict[str, str]]] = response["upsertBucket"][
2402
+ "bucket"
2403
+ ]
2404
+ project_obj: Dict[str, Dict[str, str]] = run_obj.get("project", {})
2405
+ if project_obj:
2406
+ self.set_setting("project", project_obj["name"])
2407
+ entity_obj = project_obj.get("entity", {})
2408
+ if entity_obj:
2409
+ self.set_setting("entity", entity_obj["name"])
2410
+
2411
+ server_messages = None
2412
+ if self._server_settings_type:
2413
+ server_messages = (
2414
+ response["upsertBucket"]
2415
+ .get("serverSettings", {})
2416
+ .get("serverMessages", [])
2417
+ )
2418
+
2419
+ return (
2420
+ response["upsertBucket"]["bucket"],
2421
+ response["upsertBucket"]["inserted"],
2422
+ server_messages,
2423
+ )
2424
+
2425
+ @normalize_exceptions
2426
+ def rewind_run(
2427
+ self,
2428
+ run_name: str,
2429
+ metric_name: str,
2430
+ metric_value: float,
2431
+ program_path: Optional[str] = None,
2432
+ entity: Optional[str] = None,
2433
+ project: Optional[str] = None,
2434
+ num_retries: Optional[int] = None,
2435
+ ) -> dict:
2436
+ """Rewinds a run to a previous state.
2437
+
2438
+ Args:
2439
+ run_name (str): The name of the run to rewind
2440
+ metric_name (str): The name of the metric to rewind to
2441
+ metric_value (float): The value of the metric to rewind to
2442
+ program_path (str, optional): Path to the program
2443
+ entity (str, optional): The entity to scope this project to
2444
+ project (str, optional): The name of the project
2445
+ num_retries (int, optional): Number of retries
2446
+
2447
+ Returns:
2448
+ A dict with the rewound run
2449
+
2450
+ {
2451
+ "id": "run_id",
2452
+ "name": "run_name",
2453
+ "displayName": "run_display_name",
2454
+ "description": "run_description",
2455
+ "config": "stringified_run_config_json",
2456
+ "sweepName": "run_sweep_name",
2457
+ "project": {
2458
+ "id": "project_id",
2459
+ "name": "project_name",
2460
+ "entity": {
2461
+ "id": "entity_id",
2462
+ "name": "entity_name"
2463
+ }
2464
+ },
2465
+ "historyLineCount": 100,
2466
+ }
2467
+ """
2468
+ query_string = """
2469
+ mutation RewindRun($runName: String!, $entity: String, $project: String, $metricName: String!, $metricValue: Float!) {
2470
+ rewindRun(input: {runName: $runName, entityName: $entity, projectName: $project, metricName: $metricName, metricValue: $metricValue}) {
2471
+ rewoundRun {
2472
+ id
2473
+ name
2474
+ displayName
2475
+ description
2476
+ config
2477
+ sweepName
2478
+ project {
2479
+ id
2480
+ name
2481
+ entity {
2482
+ id
2483
+ name
2484
+ }
2485
+ }
2486
+ historyLineCount
2487
+ }
2488
+ }
2489
+ }
2490
+ """
2491
+
2492
+ mutation = gql(query_string)
2493
+
2494
+ kwargs = {}
2495
+ if num_retries is not None:
2496
+ kwargs["num_retries"] = num_retries
2497
+
2498
+ variable_values = {
2499
+ "runName": run_name,
2500
+ "entity": entity or self.settings("entity"),
2501
+ "project": project or util.auto_project_name(program_path),
2502
+ "metricName": metric_name,
2503
+ "metricValue": metric_value,
2504
+ }
2505
+
2506
+ # retry conflict errors for 2 minutes, default to no_auth_retry
2507
+ check_retry_fn = util.make_check_retry_fn(
2508
+ check_fn=util.check_retry_conflict_or_gone,
2509
+ check_timedelta=datetime.timedelta(minutes=2),
2510
+ fallback_retry_fn=util.no_retry_auth,
2511
+ )
2512
+
2513
+ response = self.gql(
2514
+ mutation,
2515
+ variable_values=variable_values,
2516
+ check_retry_fn=check_retry_fn,
2517
+ **kwargs,
2518
+ )
2519
+
2520
+ run_obj: Dict[str, Dict[str, Dict[str, str]]] = response.get(
2521
+ "rewindRun", {}
2522
+ ).get("rewoundRun", {})
2523
+ project_obj: Dict[str, Dict[str, str]] = run_obj.get("project", {})
2524
+ if project_obj:
2525
+ self.set_setting("project", project_obj["name"])
2526
+ entity_obj = project_obj.get("entity", {})
2527
+ if entity_obj:
2528
+ self.set_setting("entity", entity_obj["name"])
2529
+
2530
+ return run_obj
2531
+
2532
+ @normalize_exceptions
2533
+ def get_run_info(
2534
+ self,
2535
+ entity: str,
2536
+ project: str,
2537
+ name: str,
2538
+ ) -> dict:
2539
+ query = gql(
2540
+ """
2541
+ query RunInfo($project: String!, $entity: String!, $name: String!) {
2542
+ project(name: $project, entityName: $entity) {
2543
+ run(name: $name) {
2544
+ runInfo {
2545
+ program
2546
+ args
2547
+ os
2548
+ python
2549
+ colab
2550
+ executable
2551
+ codeSaved
2552
+ cpuCount
2553
+ gpuCount
2554
+ gpu
2555
+ git {
2556
+ remote
2557
+ commit
2558
+ }
2559
+ }
2560
+ }
2561
+ }
2562
+ }
2563
+ """
2564
+ )
2565
+ variable_values = {"project": project, "entity": entity, "name": name}
2566
+ res = self.gql(query, variable_values)
2567
+ if res.get("project") is None:
2568
+ raise CommError(
2569
+ f"Error fetching run info for {entity}/{project}/{name}. Check that this project exists and you have access to this entity and project"
2570
+ )
2571
+ elif res["project"].get("run") is None:
2572
+ raise CommError(
2573
+ f"Error fetching run info for {entity}/{project}/{name}. Check that this run id exists"
2574
+ )
2575
+ run_info: dict = res["project"]["run"]["runInfo"]
2576
+ return run_info
2577
+
2578
+ @normalize_exceptions
2579
+ def get_run_state(self, entity: str, project: str, name: str) -> str:
2580
+ query = gql(
2581
+ """
2582
+ query RunState(
2583
+ $project: String!,
2584
+ $entity: String!,
2585
+ $name: String!) {
2586
+ project(name: $project, entityName: $entity) {
2587
+ run(name: $name) {
2588
+ state
2589
+ }
2590
+ }
2591
+ }
2592
+ """
2593
+ )
2594
+ variable_values = {
2595
+ "project": project,
2596
+ "entity": entity,
2597
+ "name": name,
2598
+ }
2599
+ res = self.gql(query, variable_values)
2600
+ if res.get("project") is None or res["project"].get("run") is None:
2601
+ raise CommError(f"Error fetching run state for {entity}/{project}/{name}.")
2602
+ run_state: str = res["project"]["run"]["state"]
2603
+ return run_state
2604
+
2605
+ @normalize_exceptions
2606
+ def create_run_files_introspection(self) -> bool:
2607
+ _, _, mutations = self.server_info_introspection()
2608
+ return "createRunFiles" in mutations
2609
+
2610
+ @normalize_exceptions
2611
+ def upload_urls(
2612
+ self,
2613
+ project: str,
2614
+ files: Union[List[str], Dict[str, IO]],
2615
+ run: Optional[str] = None,
2616
+ entity: Optional[str] = None,
2617
+ description: Optional[str] = None,
2618
+ ) -> Tuple[str, List[str], Dict[str, Dict[str, Any]]]:
2619
+ """Generate temporary resumable upload urls.
2620
+
2621
+ Args:
2622
+ project (str): The project to download
2623
+ files (list or dict): The filenames to upload
2624
+ run (str, optional): The run to upload to
2625
+ entity (str, optional): The entity to scope this project to.
2626
+ description (str, optional): description
2627
+
2628
+ Returns:
2629
+ (run_id, upload_headers, file_info)
2630
+ run_id: id of run we uploaded files to
2631
+ upload_headers: A list of headers to use when uploading files.
2632
+ file_info: A dict of filenames and urls.
2633
+ {
2634
+ "run_id": "run_id",
2635
+ "upload_headers": [""],
2636
+ "file_info": [
2637
+ { "weights.h5": { "uploadUrl": "https://weights.url" } },
2638
+ { "model.json": { "uploadUrl": "https://model.json" } }
2639
+ ]
2640
+ }
2641
+ """
2642
+ run_name = run or self.current_run_id
2643
+ assert run_name, "run must be specified"
2644
+ entity = entity or self.settings("entity")
2645
+ assert entity, "entity must be specified"
2646
+
2647
+ has_create_run_files_mutation = self.create_run_files_introspection()
2648
+ if not has_create_run_files_mutation:
2649
+ return self.legacy_upload_urls(project, files, run, entity, description)
2650
+
2651
+ query = gql(
2652
+ """
2653
+ mutation CreateRunFiles($entity: String!, $project: String!, $run: String!, $files: [String!]!) {
2654
+ createRunFiles(input: {entityName: $entity, projectName: $project, runName: $run, files: $files}) {
2655
+ runID
2656
+ uploadHeaders
2657
+ files {
2658
+ name
2659
+ uploadUrl
2660
+ }
2661
+ }
2662
+ }
2663
+ """
2664
+ )
2665
+
2666
+ query_result = self.gql(
2667
+ query,
2668
+ variable_values={
2669
+ "project": project,
2670
+ "run": run_name,
2671
+ "entity": entity,
2672
+ "files": [file for file in files],
2673
+ },
2674
+ )
2675
+
2676
+ result = query_result["createRunFiles"]
2677
+ run_id = result["runID"]
2678
+ if not run_id:
2679
+ raise CommError(
2680
+ f"Error uploading files to {entity}/{project}/{run_name}. Check that this project exists and you have access to this entity and project"
2681
+ )
2682
+ file_name_urls = {file["name"]: file for file in result["files"]}
2683
+ return run_id, result["uploadHeaders"], file_name_urls
2684
+
2685
+ def legacy_upload_urls(
2686
+ self,
2687
+ project: str,
2688
+ files: Union[List[str], Dict[str, IO]],
2689
+ run: Optional[str] = None,
2690
+ entity: Optional[str] = None,
2691
+ description: Optional[str] = None,
2692
+ ) -> Tuple[str, List[str], Dict[str, Dict[str, Any]]]:
2693
+ """Generate temporary resumable upload urls.
2694
+
2695
+ A new mutation createRunFiles was introduced after 0.15.4.
2696
+ This function is used to support older versions.
2697
+ """
2698
+ query = gql(
2699
+ """
2700
+ query RunUploadUrls($name: String!, $files: [String]!, $entity: String, $run: String!, $description: String) {
2701
+ model(name: $name, entityName: $entity) {
2702
+ bucket(name: $run, desc: $description) {
2703
+ id
2704
+ files(names: $files) {
2705
+ uploadHeaders
2706
+ edges {
2707
+ node {
2708
+ name
2709
+ url(upload: true)
2710
+ updatedAt
2711
+ }
2712
+ }
2713
+ }
2714
+ }
2715
+ }
2716
+ }
2717
+ """
2718
+ )
2719
+ run_id = run or self.current_run_id
2720
+ assert run_id, "run must be specified"
2721
+ entity = entity or self.settings("entity")
2722
+ query_result = self.gql(
2723
+ query,
2724
+ variable_values={
2725
+ "name": project,
2726
+ "run": run_id,
2727
+ "entity": entity,
2728
+ "files": [file for file in files],
2729
+ "description": description,
2730
+ },
2731
+ )
2732
+
2733
+ run_obj = query_result["model"]["bucket"]
2734
+ if run_obj:
2735
+ for file_node in run_obj["files"]["edges"]:
2736
+ file = file_node["node"]
2737
+ # we previously used "url" field but now use "uploadUrl"
2738
+ # replace the "url" field with "uploadUrl for downstream compatibility
2739
+ if "url" in file and "uploadUrl" not in file:
2740
+ file["uploadUrl"] = file.pop("url")
2741
+
2742
+ result = {
2743
+ file["name"]: file for file in self._flatten_edges(run_obj["files"])
2744
+ }
2745
+ return run_obj["id"], run_obj["files"]["uploadHeaders"], result
2746
+ else:
2747
+ raise CommError(f"Run does not exist {entity}/{project}/{run_id}.")
2748
+
2749
+ @normalize_exceptions
2750
+ def download_urls(
2751
+ self,
2752
+ project: str,
2753
+ run: Optional[str] = None,
2754
+ entity: Optional[str] = None,
2755
+ ) -> Dict[str, Dict[str, str]]:
2756
+ """Generate download urls.
2757
+
2758
+ Args:
2759
+ project (str): The project to download
2760
+ run (str): The run to upload to
2761
+ entity (str, optional): The entity to scope this project to. Defaults to wandb models
2762
+
2763
+ Returns:
2764
+ A dict of extensions and urls
2765
+
2766
+ {
2767
+ 'weights.h5': { "url": "https://weights.url", "updatedAt": '2013-04-26T22:22:23.832Z', 'md5': 'mZFLkyvTelC5g8XnyQrpOw==' },
2768
+ 'model.json': { "url": "https://model.url", "updatedAt": '2013-04-26T22:22:23.832Z', 'md5': 'mZFLkyvTelC5g8XnyQrpOw==' }
2769
+ }
2770
+ """
2771
+ query = gql(
2772
+ """
2773
+ query RunDownloadUrls($name: String!, $entity: String, $run: String!) {
2774
+ model(name: $name, entityName: $entity) {
2775
+ bucket(name: $run) {
2776
+ files {
2777
+ edges {
2778
+ node {
2779
+ name
2780
+ url
2781
+ md5
2782
+ updatedAt
2783
+ }
2784
+ }
2785
+ }
2786
+ }
2787
+ }
2788
+ }
2789
+ """
2790
+ )
2791
+ run = run or self.current_run_id
2792
+ assert run, "run must be specified"
2793
+ entity = entity or self.settings("entity")
2794
+ query_result = self.gql(
2795
+ query,
2796
+ variable_values={
2797
+ "name": project,
2798
+ "run": run,
2799
+ "entity": entity,
2800
+ },
2801
+ )
2802
+ if query_result["model"] is None:
2803
+ raise CommError(f"Run does not exist {entity}/{project}/{run}.")
2804
+ files = self._flatten_edges(query_result["model"]["bucket"]["files"])
2805
+ return {file["name"]: file for file in files if file}
2806
+
2807
+ @normalize_exceptions
2808
+ def download_url(
2809
+ self,
2810
+ project: str,
2811
+ file_name: str,
2812
+ run: Optional[str] = None,
2813
+ entity: Optional[str] = None,
2814
+ ) -> Optional[Dict[str, str]]:
2815
+ """Generate download urls.
2816
+
2817
+ Args:
2818
+ project (str): The project to download
2819
+ file_name (str): The name of the file to download
2820
+ run (str): The run to upload to
2821
+ entity (str, optional): The entity to scope this project to. Defaults to wandb models
2822
+
2823
+ Returns:
2824
+ A dict of extensions and urls
2825
+
2826
+ { "url": "https://weights.url", "updatedAt": '2013-04-26T22:22:23.832Z', 'md5': 'mZFLkyvTelC5g8XnyQrpOw==' }
2827
+
2828
+ """
2829
+ query = gql(
2830
+ """
2831
+ query RunDownloadUrl($name: String!, $fileName: String!, $entity: String, $run: String!) {
2832
+ model(name: $name, entityName: $entity) {
2833
+ bucket(name: $run) {
2834
+ files(names: [$fileName]) {
2835
+ edges {
2836
+ node {
2837
+ name
2838
+ url
2839
+ md5
2840
+ updatedAt
2841
+ }
2842
+ }
2843
+ }
2844
+ }
2845
+ }
2846
+ }
2847
+ """
2848
+ )
2849
+ run = run or self.current_run_id
2850
+ assert run, "run must be specified"
2851
+ query_result = self.gql(
2852
+ query,
2853
+ variable_values={
2854
+ "name": project,
2855
+ "run": run,
2856
+ "fileName": file_name,
2857
+ "entity": entity or self.settings("entity"),
2858
+ },
2859
+ )
2860
+ if query_result["model"]:
2861
+ files = self._flatten_edges(query_result["model"]["bucket"]["files"])
2862
+ return files[0] if len(files) > 0 and files[0].get("updatedAt") else None
2863
+ else:
2864
+ return None
2865
+
2866
+ @normalize_exceptions
2867
+ def download_file(self, url: str) -> Tuple[int, requests.Response]:
2868
+ """Initiate a streaming download.
2869
+
2870
+ Args:
2871
+ url (str): The url to download
2872
+
2873
+ Returns:
2874
+ A tuple of the content length and the streaming response
2875
+ """
2876
+ check_httpclient_logger_handler()
2877
+
2878
+ http_headers = _thread_local_api_settings.headers or {}
2879
+
2880
+ auth = None
2881
+ if self.access_token is not None:
2882
+ http_headers["Authorization"] = f"Bearer {self.access_token}"
2883
+ elif _thread_local_api_settings.cookies is None:
2884
+ auth = ("api", self.api_key or "")
2885
+
2886
+ response = requests.get(
2887
+ url,
2888
+ auth=auth,
2889
+ cookies=_thread_local_api_settings.cookies or {},
2890
+ headers=http_headers,
2891
+ stream=True,
2892
+ )
2893
+ response.raise_for_status()
2894
+ return int(response.headers.get("content-length", 0)), response
2895
+
2896
+ @normalize_exceptions
2897
+ def download_write_file(
2898
+ self,
2899
+ metadata: Dict[str, str],
2900
+ out_dir: Optional[str] = None,
2901
+ ) -> Tuple[str, Optional[requests.Response]]:
2902
+ """Download a file from a run and write it to wandb/.
2903
+
2904
+ Args:
2905
+ metadata (obj): The metadata object for the file to download. Comes from Api.download_urls().
2906
+ out_dir (str, optional): The directory to write the file to. Defaults to wandb/
2907
+
2908
+ Returns:
2909
+ A tuple of the file's local path and the streaming response. The streaming response is None if the file
2910
+ already existed and was up-to-date.
2911
+ """
2912
+ filename = metadata["name"]
2913
+ path = os.path.join(out_dir or self.settings("wandb_dir"), filename)
2914
+ if self.file_current(filename, B64MD5(metadata["md5"])):
2915
+ return path, None
2916
+
2917
+ size, response = self.download_file(metadata["url"])
2918
+
2919
+ with util.fsync_open(path, "wb") as file:
2920
+ for data in response.iter_content(chunk_size=1024):
2921
+ file.write(data)
2922
+
2923
+ return path, response
2924
+
2925
+ def upload_file_azure(
2926
+ self, url: str, file: Any, extra_headers: Dict[str, str]
2927
+ ) -> None:
2928
+ """Upload a file to azure."""
2929
+ from azure.core.exceptions import AzureError # type: ignore
2930
+
2931
+ # Configure the client without retries so our existing logic can handle them
2932
+ client = self._azure_blob_module.BlobClient.from_blob_url(
2933
+ url, retry_policy=self._azure_blob_module.LinearRetry(retry_total=0)
2934
+ )
2935
+ try:
2936
+ if extra_headers.get("Content-MD5") is not None:
2937
+ md5: Optional[bytes] = base64.b64decode(extra_headers["Content-MD5"])
2938
+ else:
2939
+ md5 = None
2940
+ content_settings = self._azure_blob_module.ContentSettings(
2941
+ content_md5=md5,
2942
+ content_type=extra_headers.get("Content-Type"),
2943
+ )
2944
+ client.upload_blob(
2945
+ file,
2946
+ max_concurrency=4,
2947
+ length=len(file),
2948
+ overwrite=True,
2949
+ content_settings=content_settings,
2950
+ )
2951
+ except AzureError as e:
2952
+ if hasattr(e, "response"):
2953
+ response = requests.models.Response()
2954
+ response.status_code = e.response.status_code
2955
+ response.headers = e.response.headers
2956
+ raise requests.exceptions.RequestException(e.message, response=response)
2957
+ else:
2958
+ raise requests.exceptions.ConnectionError(e.message)
2959
+
2960
+ def upload_multipart_file_chunk(
2961
+ self,
2962
+ url: str,
2963
+ upload_chunk: bytes,
2964
+ extra_headers: Optional[Dict[str, str]] = None,
2965
+ ) -> Optional[requests.Response]:
2966
+ """Upload a file chunk to S3 with failure resumption.
2967
+
2968
+ Args:
2969
+ url: The url to download
2970
+ upload_chunk: The path to the file you want to upload
2971
+ extra_headers: A dictionary of extra headers to send with the request
2972
+
2973
+ Returns:
2974
+ The `requests` library response object
2975
+ """
2976
+ check_httpclient_logger_handler()
2977
+ try:
2978
+ if env.is_debug(env=self._environ):
2979
+ logger.debug("upload_file: %s", url)
2980
+ response = self._upload_file_session.put(
2981
+ url, data=upload_chunk, headers=extra_headers
2982
+ )
2983
+ if env.is_debug(env=self._environ):
2984
+ logger.debug("upload_file: %s complete", url)
2985
+ response.raise_for_status()
2986
+ except requests.exceptions.RequestException as e:
2987
+ logger.exception(f"upload_file exception for {url=}")
2988
+ response_content = e.response.content if e.response is not None else ""
2989
+ status_code = e.response.status_code if e.response is not None else 0
2990
+ # S3 reports retryable request timeouts out-of-band
2991
+ is_aws_retryable = status_code == 400 and "RequestTimeout" in str(
2992
+ response_content
2993
+ )
2994
+ # Retry errors from cloud storage or local network issues
2995
+ if (
2996
+ status_code in (308, 408, 409, 429, 500, 502, 503, 504)
2997
+ or isinstance(
2998
+ e,
2999
+ (requests.exceptions.Timeout, requests.exceptions.ConnectionError),
3000
+ )
3001
+ or is_aws_retryable
3002
+ ):
3003
+ _e = retry.TransientError(exc=e)
3004
+ raise _e.with_traceback(sys.exc_info()[2])
3005
+ else:
3006
+ wandb._sentry.reraise(e)
3007
+ return response
3008
+
3009
+ def upload_file(
3010
+ self,
3011
+ url: str,
3012
+ file: IO[bytes],
3013
+ callback: Optional["ProgressFn"] = None,
3014
+ extra_headers: Optional[Dict[str, str]] = None,
3015
+ ) -> Optional[requests.Response]:
3016
+ """Upload a file to W&B with failure resumption.
3017
+
3018
+ Args:
3019
+ url: The url to download
3020
+ file: The path to the file you want to upload
3021
+ callback: A callback which is passed the number of
3022
+ bytes uploaded since the last time it was called, used to report progress
3023
+ extra_headers: A dictionary of extra headers to send with the request
3024
+
3025
+ Returns:
3026
+ The `requests` library response object
3027
+ """
3028
+ check_httpclient_logger_handler()
3029
+ extra_headers = extra_headers.copy() if extra_headers else {}
3030
+ response: Optional[requests.Response] = None
3031
+ progress = Progress(file, callback=callback)
3032
+ try:
3033
+ if "x-ms-blob-type" in extra_headers and self._azure_blob_module:
3034
+ self.upload_file_azure(url, progress, extra_headers)
3035
+ else:
3036
+ if "x-ms-blob-type" in extra_headers:
3037
+ wandb.termwarn(
3038
+ "Azure uploads over 256MB require the azure SDK, install with pip install wandb[azure]",
3039
+ repeat=False,
3040
+ )
3041
+ if env.is_debug(env=self._environ):
3042
+ logger.debug("upload_file: %s", url)
3043
+ response = self._upload_file_session.put(
3044
+ url, data=progress, headers=extra_headers
3045
+ )
3046
+ if env.is_debug(env=self._environ):
3047
+ logger.debug("upload_file: %s complete", url)
3048
+ response.raise_for_status()
3049
+ except requests.exceptions.RequestException as e:
3050
+ logger.exception(f"upload_file exception for {url=}")
3051
+ response_content = e.response.content if e.response is not None else ""
3052
+ status_code = e.response.status_code if e.response is not None else 0
3053
+ # S3 reports retryable request timeouts out-of-band
3054
+ is_aws_retryable = (
3055
+ "x-amz-meta-md5" in extra_headers
3056
+ and status_code == 400
3057
+ and "RequestTimeout" in str(response_content)
3058
+ )
3059
+ # We need to rewind the file for the next retry (the file passed in is `seek`'ed to 0)
3060
+ progress.rewind()
3061
+ # Retry errors from cloud storage or local network issues
3062
+ if (
3063
+ status_code in (308, 408, 409, 429, 500, 502, 503, 504)
3064
+ or isinstance(
3065
+ e,
3066
+ (requests.exceptions.Timeout, requests.exceptions.ConnectionError),
3067
+ )
3068
+ or is_aws_retryable
3069
+ ):
3070
+ _e = retry.TransientError(exc=e)
3071
+ raise _e.with_traceback(sys.exc_info()[2])
3072
+ else:
3073
+ wandb._sentry.reraise(e)
3074
+
3075
+ return response
3076
+
3077
+ @normalize_exceptions
3078
+ def register_agent(
3079
+ self,
3080
+ host: str,
3081
+ sweep_id: Optional[str] = None,
3082
+ project_name: Optional[str] = None,
3083
+ entity: Optional[str] = None,
3084
+ ) -> dict:
3085
+ """Register a new agent.
3086
+
3087
+ Args:
3088
+ host (str): hostname
3089
+ sweep_id (str): sweep id
3090
+ project_name: (str): model that contains sweep
3091
+ entity: (str): entity that contains sweep
3092
+ """
3093
+ mutation = gql(
3094
+ """
3095
+ mutation CreateAgent(
3096
+ $host: String!
3097
+ $projectName: String,
3098
+ $entityName: String,
3099
+ $sweep: String!
3100
+ ) {
3101
+ createAgent(input: {
3102
+ host: $host,
3103
+ projectName: $projectName,
3104
+ entityName: $entityName,
3105
+ sweep: $sweep,
3106
+ }) {
3107
+ agent {
3108
+ id
3109
+ }
3110
+ }
3111
+ }
3112
+ """
3113
+ )
3114
+ if entity is None:
3115
+ entity = self.settings("entity")
3116
+ if project_name is None:
3117
+ project_name = self.settings("project")
3118
+
3119
+ response = self.gql(
3120
+ mutation,
3121
+ variable_values={
3122
+ "host": host,
3123
+ "entityName": entity,
3124
+ "projectName": project_name,
3125
+ "sweep": sweep_id,
3126
+ },
3127
+ check_retry_fn=util.no_retry_4xx,
3128
+ )
3129
+ result: dict = response["createAgent"]["agent"]
3130
+ return result
3131
+
3132
+ def agent_heartbeat(
3133
+ self, agent_id: str, metrics: dict, run_states: dict
3134
+ ) -> List[Dict[str, Any]]:
3135
+ """Notify server about agent state, receive commands.
3136
+
3137
+ Args:
3138
+ agent_id (str): agent_id
3139
+ metrics (dict): system metrics
3140
+ run_states (dict): run_id: state mapping
3141
+ Returns:
3142
+ List of commands to execute.
3143
+ """
3144
+ mutation = gql(
3145
+ """
3146
+ mutation Heartbeat(
3147
+ $id: ID!,
3148
+ $metrics: JSONString,
3149
+ $runState: JSONString
3150
+ ) {
3151
+ agentHeartbeat(input: {
3152
+ id: $id,
3153
+ metrics: $metrics,
3154
+ runState: $runState
3155
+ }) {
3156
+ agent {
3157
+ id
3158
+ }
3159
+ commands
3160
+ }
3161
+ }
3162
+ """
3163
+ )
3164
+
3165
+ if agent_id is None:
3166
+ raise ValueError("Cannot call heartbeat with an unregistered agent.")
3167
+
3168
+ try:
3169
+ response = self.gql(
3170
+ mutation,
3171
+ variable_values={
3172
+ "id": agent_id,
3173
+ "metrics": json.dumps(metrics),
3174
+ "runState": json.dumps(run_states),
3175
+ },
3176
+ timeout=60,
3177
+ )
3178
+ except Exception:
3179
+ logger.exception("Error communicating with W&B.")
3180
+ return []
3181
+ else:
3182
+ result: List[Dict[str, Any]] = json.loads(
3183
+ response["agentHeartbeat"]["commands"]
3184
+ )
3185
+ return result
3186
+
3187
+ @staticmethod
3188
+ def _validate_config_and_fill_distribution(config: dict) -> dict:
3189
+ # verify that parameters are well specified.
3190
+ # TODO(dag): deprecate this in favor of jsonschema validation once
3191
+ # apiVersion 2 is released and local controller is integrated with
3192
+ # wandb/client.
3193
+
3194
+ # avoid modifying the original config dict in
3195
+ # case it is reused outside the calling func
3196
+ config = deepcopy(config)
3197
+
3198
+ # explicitly cast to dict in case config was passed as a sweepconfig
3199
+ # sweepconfig does not serialize cleanly to yaml and breaks graphql,
3200
+ # but it is a subclass of dict, so this conversion is clean
3201
+ config = dict(config)
3202
+
3203
+ if "parameters" not in config:
3204
+ # still shows an anaconda warning, but doesn't error
3205
+ return config
3206
+
3207
+ for parameter_name in config["parameters"]:
3208
+ parameter = config["parameters"][parameter_name]
3209
+ if "min" in parameter and "max" in parameter:
3210
+ if "distribution" not in parameter:
3211
+ if isinstance(parameter["min"], int) and isinstance(
3212
+ parameter["max"], int
3213
+ ):
3214
+ parameter["distribution"] = "int_uniform"
3215
+ elif isinstance(parameter["min"], float) and isinstance(
3216
+ parameter["max"], float
3217
+ ):
3218
+ parameter["distribution"] = "uniform"
3219
+ else:
3220
+ raise ValueError(
3221
+ f"Parameter {parameter_name} is ambiguous, please specify bounds as both floats (for a float_"
3222
+ "uniform distribution) or ints (for an int_uniform distribution)."
3223
+ )
3224
+ return config
3225
+
3226
+ @normalize_exceptions
3227
+ def upsert_sweep(
3228
+ self,
3229
+ config: dict,
3230
+ controller: Optional[str] = None,
3231
+ launch_scheduler: Optional[str] = None,
3232
+ scheduler: Optional[str] = None,
3233
+ obj_id: Optional[str] = None,
3234
+ project: Optional[str] = None,
3235
+ entity: Optional[str] = None,
3236
+ state: Optional[str] = None,
3237
+ prior_runs: Optional[List[str]] = None,
3238
+ display_name: Optional[str] = None,
3239
+ template_variable_values: Optional[Dict[str, Any]] = None,
3240
+ ) -> Tuple[str, List[str]]:
3241
+ """Upsert a sweep object.
3242
+
3243
+ Args:
3244
+ config (dict): sweep config (will be converted to yaml)
3245
+ controller (str): controller to use
3246
+ launch_scheduler (str): launch scheduler to use
3247
+ scheduler (str): scheduler to use
3248
+ obj_id (str): object id
3249
+ project (str): project to use
3250
+ entity (str): entity to use
3251
+ state (str): state
3252
+ prior_runs (list): IDs of existing runs to add to the sweep
3253
+ display_name (str): display name for the sweep
3254
+ template_variable_values (dict): template variable values
3255
+ """
3256
+ project_query = """
3257
+ project {
3258
+ id
3259
+ name
3260
+ entity {
3261
+ id
3262
+ name
3263
+ }
3264
+ }
3265
+ """
3266
+ mutation_str = """
3267
+ mutation UpsertSweep(
3268
+ $id: ID,
3269
+ $config: String,
3270
+ $description: String,
3271
+ $entityName: String,
3272
+ $projectName: String,
3273
+ $controller: JSONString,
3274
+ $scheduler: JSONString,
3275
+ $state: String,
3276
+ $priorRunsFilters: JSONString,
3277
+ $displayName: String,
3278
+ ) {
3279
+ upsertSweep(input: {
3280
+ id: $id,
3281
+ config: $config,
3282
+ description: $description,
3283
+ entityName: $entityName,
3284
+ projectName: $projectName,
3285
+ controller: $controller,
3286
+ scheduler: $scheduler,
3287
+ state: $state,
3288
+ priorRunsFilters: $priorRunsFilters,
3289
+ displayName: $displayName,
3290
+ }) {
3291
+ sweep {
3292
+ name
3293
+ _PROJECT_QUERY_
3294
+ }
3295
+ configValidationWarnings
3296
+ }
3297
+ }
3298
+ """
3299
+ # TODO(jhr): we need protocol versioning to know schema is not supported
3300
+ # for now we will just try both new and old query
3301
+ mutation_5 = gql(
3302
+ mutation_str.replace(
3303
+ "$controller: JSONString,",
3304
+ "$controller: JSONString,$launchScheduler: JSONString, $templateVariableValues: JSONString,",
3305
+ )
3306
+ .replace(
3307
+ "controller: $controller,",
3308
+ "controller: $controller,launchScheduler: $launchScheduler,templateVariableValues: $templateVariableValues,",
3309
+ )
3310
+ .replace("_PROJECT_QUERY_", project_query)
3311
+ )
3312
+ # launchScheduler was introduced in core v0.14.0
3313
+ mutation_4 = gql(
3314
+ mutation_str.replace(
3315
+ "$controller: JSONString,",
3316
+ "$controller: JSONString,$launchScheduler: JSONString,",
3317
+ )
3318
+ .replace(
3319
+ "controller: $controller,",
3320
+ "controller: $controller,launchScheduler: $launchScheduler",
3321
+ )
3322
+ .replace("_PROJECT_QUERY_", project_query)
3323
+ )
3324
+
3325
+ # mutation 3 maps to backend that can support CLI version of at least 0.10.31
3326
+ mutation_3 = gql(mutation_str.replace("_PROJECT_QUERY_", project_query))
3327
+ mutation_2 = gql(
3328
+ mutation_str.replace("_PROJECT_QUERY_", project_query).replace(
3329
+ "configValidationWarnings", ""
3330
+ )
3331
+ )
3332
+ mutation_1 = gql(
3333
+ mutation_str.replace("_PROJECT_QUERY_", "").replace(
3334
+ "configValidationWarnings", ""
3335
+ )
3336
+ )
3337
+
3338
+ # TODO(dag): replace this with a query for protocol versioning
3339
+ mutations = [mutation_5, mutation_4, mutation_3, mutation_2, mutation_1]
3340
+
3341
+ config = self._validate_config_and_fill_distribution(config)
3342
+
3343
+ # Silly, but attr-dicts like EasyDicts don't serialize correctly to yaml.
3344
+ # This sanitizes them with a round trip pass through json to get a regular dict.
3345
+ config_str = yaml.dump(
3346
+ json.loads(json.dumps(config)), Dumper=util.NonOctalStringDumper
3347
+ )
3348
+ filters = None
3349
+ if prior_runs:
3350
+ filters = json.dumps({"$or": [{"name": r} for r in prior_runs]})
3351
+
3352
+ err: Optional[Exception] = None
3353
+ for mutation in mutations:
3354
+ try:
3355
+ variables = {
3356
+ "id": obj_id,
3357
+ "config": config_str,
3358
+ "description": config.get("description"),
3359
+ "entityName": entity or self.settings("entity"),
3360
+ "projectName": project or self.settings("project"),
3361
+ "controller": controller,
3362
+ "launchScheduler": launch_scheduler,
3363
+ "templateVariableValues": json.dumps(template_variable_values),
3364
+ "scheduler": scheduler,
3365
+ "priorRunsFilters": filters,
3366
+ "displayName": display_name,
3367
+ }
3368
+ if state:
3369
+ variables["state"] = state
3370
+
3371
+ response = self.gql(
3372
+ mutation,
3373
+ variable_values=variables,
3374
+ check_retry_fn=util.no_retry_4xx,
3375
+ )
3376
+ except UsageError:
3377
+ raise
3378
+ except Exception as e:
3379
+ # graphql schema exception is generic
3380
+ err = e
3381
+ continue
3382
+ err = None
3383
+ break
3384
+ if err:
3385
+ raise err
3386
+
3387
+ sweep: Dict[str, Dict[str, Dict]] = response["upsertSweep"]["sweep"]
3388
+ project_obj: Dict[str, Dict] = sweep.get("project", {})
3389
+ if project_obj:
3390
+ self.set_setting("project", project_obj["name"])
3391
+ entity_obj: dict = project_obj.get("entity", {})
3392
+ if entity_obj:
3393
+ self.set_setting("entity", entity_obj["name"])
3394
+
3395
+ warnings = response["upsertSweep"].get("configValidationWarnings", [])
3396
+ return response["upsertSweep"]["sweep"]["name"], warnings
3397
+
3398
+ @normalize_exceptions
3399
+ def create_anonymous_api_key(self) -> str:
3400
+ """Create a new API key belonging to a new anonymous user."""
3401
+ mutation = gql(
3402
+ """
3403
+ mutation CreateAnonymousApiKey {
3404
+ createAnonymousEntity(input: {}) {
3405
+ apiKey {
3406
+ name
3407
+ }
3408
+ }
3409
+ }
3410
+ """
3411
+ )
3412
+
3413
+ response = self.gql(mutation, variable_values={})
3414
+ key: str = str(response["createAnonymousEntity"]["apiKey"]["name"])
3415
+ return key
3416
+
3417
+ @staticmethod
3418
+ def file_current(fname: str, md5: B64MD5) -> bool:
3419
+ """Checksum a file and compare the md5 with the known md5."""
3420
+ return os.path.isfile(fname) and md5_file_b64(fname) == md5
3421
+
3422
+ @normalize_exceptions
3423
+ def pull(
3424
+ self, project: str, run: Optional[str] = None, entity: Optional[str] = None
3425
+ ) -> "List[requests.Response]":
3426
+ """Download files from W&B.
3427
+
3428
+ Args:
3429
+ project (str): The project to download
3430
+ run (str, optional): The run to upload to
3431
+ entity (str, optional): The entity to scope this project to. Defaults to wandb models
3432
+
3433
+ Returns:
3434
+ The `requests` library response object
3435
+ """
3436
+ project, run = self.parse_slug(project, run=run)
3437
+ urls = self.download_urls(project, run, entity)
3438
+ responses = []
3439
+ for filename in urls:
3440
+ _, response = self.download_write_file(urls[filename])
3441
+ if response:
3442
+ responses.append(response)
3443
+
3444
+ return responses
3445
+
3446
+ def get_project(self) -> str:
3447
+ project: str = self.default_settings.get("project") or self.settings("project")
3448
+ return project
3449
+
3450
+ @normalize_exceptions
3451
+ def push(
3452
+ self,
3453
+ files: Union[List[str], Dict[str, IO]],
3454
+ run: Optional[str] = None,
3455
+ entity: Optional[str] = None,
3456
+ project: Optional[str] = None,
3457
+ description: Optional[str] = None,
3458
+ force: bool = True,
3459
+ progress: Union[TextIO, Literal[False]] = False,
3460
+ ) -> "List[Optional[requests.Response]]":
3461
+ """Uploads multiple files to W&B.
3462
+
3463
+ Args:
3464
+ files (list or dict): The filenames to upload, when dict the values are open files
3465
+ run (str, optional): The run to upload to
3466
+ entity (str, optional): The entity to scope this project to. Defaults to wandb models
3467
+ project (str, optional): The name of the project to upload to. Defaults to the one in settings.
3468
+ description (str, optional): The description of the changes
3469
+ force (bool, optional): Whether to prevent push if git has uncommitted changes
3470
+ progress (callable, or stream): If callable, will be called with (chunk_bytes,
3471
+ total_bytes) as argument. If TextIO, renders a progress bar to it.
3472
+
3473
+ Returns:
3474
+ A list of `requests.Response` objects
3475
+ """
3476
+ if project is None:
3477
+ project = self.get_project()
3478
+ if project is None:
3479
+ raise CommError("No project configured.")
3480
+ if run is None:
3481
+ run = self.current_run_id
3482
+
3483
+ # TODO(adrian): we use a retriable version of self.upload_file() so
3484
+ # will never retry self.upload_urls() here. Instead, maybe we should
3485
+ # make push itself retriable.
3486
+ _, upload_headers, result = self.upload_urls(
3487
+ project,
3488
+ files,
3489
+ run,
3490
+ entity,
3491
+ )
3492
+ extra_headers = {}
3493
+ for upload_header in upload_headers:
3494
+ key, val = upload_header.split(":", 1)
3495
+ extra_headers[key] = val
3496
+ responses = []
3497
+ for file_name, file_info in result.items():
3498
+ file_url = file_info["uploadUrl"]
3499
+
3500
+ # If the upload URL is relative, fill it in with the base URL,
3501
+ # since it's a proxied file store like the on-prem VM.
3502
+ if file_url.startswith("/"):
3503
+ file_url = f"{self.api_url}{file_url}"
3504
+
3505
+ try:
3506
+ # To handle Windows paths
3507
+ # TODO: this doesn't handle absolute paths...
3508
+ normal_name = os.path.join(*file_name.split("/"))
3509
+ open_file = (
3510
+ files[file_name]
3511
+ if isinstance(files, dict)
3512
+ else open(normal_name, "rb")
3513
+ )
3514
+ except OSError:
3515
+ print(f"{file_name} does not exist") # noqa: T201
3516
+ continue
3517
+ if progress is False:
3518
+ responses.append(
3519
+ self.upload_file_retry(
3520
+ file_info["uploadUrl"], open_file, extra_headers=extra_headers
3521
+ )
3522
+ )
3523
+ else:
3524
+ if callable(progress):
3525
+ responses.append( # type: ignore
3526
+ self.upload_file_retry(
3527
+ file_url, open_file, progress, extra_headers=extra_headers
3528
+ )
3529
+ )
3530
+ else:
3531
+ length = os.fstat(open_file.fileno()).st_size
3532
+ with click.progressbar( # type: ignore
3533
+ file=progress,
3534
+ length=length,
3535
+ label=f"Uploading file: {file_name}",
3536
+ fill_char=click.style("&", fg="green"),
3537
+ ) as bar:
3538
+ responses.append(
3539
+ self.upload_file_retry(
3540
+ file_url,
3541
+ open_file,
3542
+ lambda bites, _: bar.update(bites),
3543
+ extra_headers=extra_headers,
3544
+ )
3545
+ )
3546
+ open_file.close()
3547
+ return responses
3548
+
3549
+ def link_artifact(
3550
+ self,
3551
+ client_id: str,
3552
+ server_id: str,
3553
+ portfolio_name: str,
3554
+ entity: str,
3555
+ project: str,
3556
+ aliases: Sequence[str],
3557
+ organization: str,
3558
+ ) -> Dict[str, Any]:
3559
+ template = """
3560
+ mutation LinkArtifact(
3561
+ $artifactPortfolioName: String!,
3562
+ $entityName: String!,
3563
+ $projectName: String!,
3564
+ $aliases: [ArtifactAliasInput!],
3565
+ ID_TYPE
3566
+ ) {
3567
+ linkArtifact(input: {
3568
+ artifactPortfolioName: $artifactPortfolioName,
3569
+ entityName: $entityName,
3570
+ projectName: $projectName,
3571
+ aliases: $aliases,
3572
+ ID_VALUE
3573
+ }) {
3574
+ versionIndex
3575
+ }
3576
+ }
3577
+ """
3578
+
3579
+ org_entity = ""
3580
+ if is_artifact_registry_project(project):
3581
+ try:
3582
+ org_entity = self._resolve_org_entity_name(
3583
+ entity=entity, organization=organization
3584
+ )
3585
+ except ValueError as e:
3586
+ wandb.termerror(str(e))
3587
+ raise
3588
+
3589
+ def replace(a: str, b: str) -> None:
3590
+ nonlocal template
3591
+ template = template.replace(a, b)
3592
+
3593
+ if server_id:
3594
+ replace("ID_TYPE", "$artifactID: ID")
3595
+ replace("ID_VALUE", "artifactID: $artifactID")
3596
+ elif client_id:
3597
+ replace("ID_TYPE", "$clientID: ID")
3598
+ replace("ID_VALUE", "clientID: $clientID")
3599
+
3600
+ variable_values = {
3601
+ "clientID": client_id,
3602
+ "artifactID": server_id,
3603
+ "artifactPortfolioName": portfolio_name,
3604
+ "entityName": org_entity or entity,
3605
+ "projectName": project,
3606
+ "aliases": [
3607
+ {"alias": alias, "artifactCollectionName": portfolio_name}
3608
+ for alias in aliases
3609
+ ],
3610
+ }
3611
+
3612
+ mutation = gql(template)
3613
+ response = self.gql(mutation, variable_values=variable_values)
3614
+ link_artifact: Dict[str, Any] = response["linkArtifact"]
3615
+ return link_artifact
3616
+
3617
+ def _resolve_org_entity_name(self, entity: str, organization: str = "") -> str:
3618
+ # resolveOrgEntityName fetches the portfolio's org entity's name.
3619
+ #
3620
+ # The organization parameter may be empty, an org's display name, or an org entity name.
3621
+ #
3622
+ # If the server doesn't support fetching the org name of a portfolio, then this returns
3623
+ # the organization parameter, or an error if it is empty. Otherwise, this returns the
3624
+ # fetched value after validating that the given organization, if not empty, matches
3625
+ # either the org's display or entity name.
3626
+
3627
+ if not entity:
3628
+ raise ValueError("Entity name is required to resolve org entity name.")
3629
+
3630
+ org_fields = self.server_organization_type_introspection()
3631
+ can_shorthand_org_entity = "orgEntity" in org_fields
3632
+ if not organization and not can_shorthand_org_entity:
3633
+ raise ValueError(
3634
+ "Fetching Registry artifacts without inputting an organization "
3635
+ "is unavailable for your server version. "
3636
+ "Please upgrade your server to 0.50.0 or later."
3637
+ )
3638
+ if not can_shorthand_org_entity:
3639
+ # Server doesn't support fetching org entity to validate,
3640
+ # assume org entity is correctly inputted
3641
+ return organization
3642
+
3643
+ orgs_from_entity = self._fetch_orgs_and_org_entities_from_entity(entity)
3644
+ if organization:
3645
+ return _match_org_with_fetched_org_entities(organization, orgs_from_entity)
3646
+
3647
+ # If no input organization provided, error if entity belongs to multiple orgs because we
3648
+ # cannot determine which one to use.
3649
+ if len(orgs_from_entity) > 1:
3650
+ raise ValueError(
3651
+ f"Personal entity {entity!r} belongs to multiple organizations "
3652
+ "and cannot be used without specifying the organization name. "
3653
+ "Please specify the organization in the Registry path or use a team entity in the entity settings."
3654
+ )
3655
+ return orgs_from_entity[0].entity_name
3656
+
3657
+ def _fetch_orgs_and_org_entities_from_entity(self, entity: str) -> List[_OrgNames]:
3658
+ """Fetches organization entity names and display names for a given entity.
3659
+
3660
+ Args:
3661
+ entity (str): Entity name to lookup. Can be either a personal or team entity.
3662
+
3663
+ Returns:
3664
+ List[_OrgNames]: List of _OrgNames tuples. (_OrgNames(entity_name, display_name))
3665
+
3666
+ Raises:
3667
+ ValueError: If entity is not found, has no organizations, or other validation errors.
3668
+ """
3669
+ query = gql(
3670
+ """
3671
+ query FetchOrgEntityFromEntity($entityName: String!) {
3672
+ entity(name: $entityName) {
3673
+ organization {
3674
+ name
3675
+ orgEntity {
3676
+ name
3677
+ }
3678
+ }
3679
+ user {
3680
+ organizations {
3681
+ name
3682
+ orgEntity {
3683
+ name
3684
+ }
3685
+ }
3686
+ }
3687
+ }
3688
+ }
3689
+ """
3690
+ )
3691
+ response = self.gql(
3692
+ query,
3693
+ variable_values={
3694
+ "entityName": entity,
3695
+ },
3696
+ )
3697
+
3698
+ # Parse organization from response
3699
+ entity_resp = response["entity"]["organization"]
3700
+ user_resp = response["entity"]["user"]
3701
+ # Check for organization under team/org entity type
3702
+ if entity_resp:
3703
+ org_name = entity_resp.get("name")
3704
+ org_entity_name = entity_resp.get("orgEntity") and entity_resp[
3705
+ "orgEntity"
3706
+ ].get("name")
3707
+ if not org_name or not org_entity_name:
3708
+ raise ValueError(
3709
+ f"Unable to find an organization under entity {entity!r}."
3710
+ )
3711
+ return [_OrgNames(entity_name=org_entity_name, display_name=org_name)]
3712
+ # Check for organization under personal entity type, where a user can belong to multiple orgs
3713
+ elif user_resp:
3714
+ orgs = user_resp.get("organizations", [])
3715
+ org_entities_return = [
3716
+ _OrgNames(
3717
+ entity_name=org["orgEntity"]["name"], display_name=org["name"]
3718
+ )
3719
+ for org in orgs
3720
+ if org.get("orgEntity") and org.get("name")
3721
+ ]
3722
+ if not org_entities_return:
3723
+ raise ValueError(
3724
+ f"Unable to resolve an organization associated with personal entity: {entity!r}. "
3725
+ "This could be because its a personal entity that doesn't belong to any organizations. "
3726
+ "Please specify the organization in the Registry path or use a team entity in the entity settings."
3727
+ )
3728
+ return org_entities_return
3729
+ else:
3730
+ raise ValueError(f"Unable to find an organization under entity {entity!r}.")
3731
+
3732
+ def _construct_use_artifact_query(
3733
+ self,
3734
+ artifact_id: str,
3735
+ entity_name: Optional[str] = None,
3736
+ project_name: Optional[str] = None,
3737
+ run_name: Optional[str] = None,
3738
+ use_as: Optional[str] = None,
3739
+ artifact_entity_name: Optional[str] = None,
3740
+ artifact_project_name: Optional[str] = None,
3741
+ ) -> Tuple[Document, Dict[str, Any]]:
3742
+ query_vars = [
3743
+ "$entityName: String!",
3744
+ "$projectName: String!",
3745
+ "$runName: String!",
3746
+ "$artifactID: ID!",
3747
+ ]
3748
+ query_args = [
3749
+ "entityName: $entityName",
3750
+ "projectName: $projectName",
3751
+ "runName: $runName",
3752
+ "artifactID: $artifactID",
3753
+ ]
3754
+
3755
+ artifact_types = self.server_use_artifact_input_introspection()
3756
+ if "usedAs" in artifact_types and use_as:
3757
+ query_vars.append("$usedAs: String")
3758
+ query_args.append("usedAs: $usedAs")
3759
+
3760
+ entity_name = entity_name or self.settings("entity")
3761
+ project_name = project_name or self.settings("project")
3762
+ run_name = run_name or self.current_run_id
3763
+
3764
+ variable_values: Dict[str, Any] = {
3765
+ "entityName": entity_name,
3766
+ "projectName": project_name,
3767
+ "runName": run_name,
3768
+ "artifactID": artifact_id,
3769
+ "usedAs": use_as,
3770
+ }
3771
+
3772
+ server_allows_entity_project_information = self._server_supports(
3773
+ ServerFeature.USE_ARTIFACT_WITH_ENTITY_AND_PROJECT_INFORMATION
3774
+ )
3775
+ if server_allows_entity_project_information:
3776
+ query_vars.extend(
3777
+ [
3778
+ "$artifactEntityName: String",
3779
+ "$artifactProjectName: String",
3780
+ ]
3781
+ )
3782
+ query_args.extend(
3783
+ [
3784
+ "artifactEntityName: $artifactEntityName",
3785
+ "artifactProjectName: $artifactProjectName",
3786
+ ]
3787
+ )
3788
+ variable_values["artifactEntityName"] = artifact_entity_name
3789
+ variable_values["artifactProjectName"] = artifact_project_name
3790
+
3791
+ vars_str = ", ".join(query_vars)
3792
+ args_str = ", ".join(query_args)
3793
+
3794
+ query = gql(
3795
+ f"""
3796
+ mutation UseArtifact({vars_str}) {{
3797
+ useArtifact(input: {{{args_str}}}) {{
3798
+ artifact {{
3799
+ id
3800
+ digest
3801
+ description
3802
+ state
3803
+ createdAt
3804
+ metadata
3805
+ }}
3806
+ }}
3807
+ }}
3808
+ """
3809
+ )
3810
+ return query, variable_values
3811
+
3812
+ def use_artifact(
3813
+ self,
3814
+ artifact_id: str,
3815
+ entity_name: Optional[str] = None,
3816
+ project_name: Optional[str] = None,
3817
+ run_name: Optional[str] = None,
3818
+ artifact_entity_name: Optional[str] = None,
3819
+ artifact_project_name: Optional[str] = None,
3820
+ use_as: Optional[str] = None,
3821
+ ) -> Optional[Dict[str, Any]]:
3822
+ query, variable_values = self._construct_use_artifact_query(
3823
+ artifact_id,
3824
+ entity_name,
3825
+ project_name,
3826
+ run_name,
3827
+ use_as,
3828
+ artifact_entity_name,
3829
+ artifact_project_name,
3830
+ )
3831
+ response = self.gql(query, variable_values)
3832
+
3833
+ if response["useArtifact"]["artifact"]:
3834
+ artifact: Dict[str, Any] = response["useArtifact"]["artifact"]
3835
+ return artifact
3836
+ return None
3837
+
3838
+ # Fetch fields available in backend of Organization type
3839
+ def server_organization_type_introspection(self) -> List[str]:
3840
+ query_string = """
3841
+ query ProbeServerOrganization {
3842
+ OrganizationInfoType: __type(name:"Organization") {
3843
+ fields {
3844
+ name
3845
+ }
3846
+ }
3847
+ }
3848
+ """
3849
+
3850
+ if self.server_organization_type_fields_info is None:
3851
+ query = gql(query_string)
3852
+ res = self.gql(query)
3853
+ input_fields = res.get("OrganizationInfoType", {}).get("fields", [{}])
3854
+ self.server_organization_type_fields_info = [
3855
+ field["name"] for field in input_fields if "name" in field
3856
+ ]
3857
+
3858
+ return self.server_organization_type_fields_info
3859
+
3860
+ # Fetch input arguments for the "artifact" endpoint on the "Project" type
3861
+ def server_project_type_introspection(self) -> bool:
3862
+ if self.server_supports_enabling_artifact_usage_tracking is not None:
3863
+ return self.server_supports_enabling_artifact_usage_tracking
3864
+
3865
+ query_string = """
3866
+ query ProbeServerProjectInfo {
3867
+ ProjectInfoType: __type(name:"Project") {
3868
+ fields {
3869
+ name
3870
+ args {
3871
+ name
3872
+ }
3873
+ }
3874
+ }
3875
+ }
3876
+ """
3877
+
3878
+ query = gql(query_string)
3879
+ res = self.gql(query)
3880
+ input_fields = res.get("ProjectInfoType", {}).get("fields", [{}])
3881
+ artifact_args: List[Dict[str, str]] = next(
3882
+ (
3883
+ field.get("args", [])
3884
+ for field in input_fields
3885
+ if field.get("name") == "artifact"
3886
+ ),
3887
+ [],
3888
+ )
3889
+ self.server_supports_enabling_artifact_usage_tracking = any(
3890
+ arg.get("name") == "enableTracking" for arg in artifact_args
3891
+ )
3892
+
3893
+ return self.server_supports_enabling_artifact_usage_tracking
3894
+
3895
+ def create_artifact_type(
3896
+ self,
3897
+ artifact_type_name: str,
3898
+ entity_name: Optional[str] = None,
3899
+ project_name: Optional[str] = None,
3900
+ description: Optional[str] = None,
3901
+ ) -> Optional[str]:
3902
+ mutation = gql(
3903
+ """
3904
+ mutation CreateArtifactType(
3905
+ $entityName: String!,
3906
+ $projectName: String!,
3907
+ $artifactTypeName: String!,
3908
+ $description: String
3909
+ ) {
3910
+ createArtifactType(input: {
3911
+ entityName: $entityName,
3912
+ projectName: $projectName,
3913
+ name: $artifactTypeName,
3914
+ description: $description
3915
+ }) {
3916
+ artifactType {
3917
+ id
3918
+ }
3919
+ }
3920
+ }
3921
+ """
3922
+ )
3923
+ entity_name = entity_name or self.settings("entity")
3924
+ project_name = project_name or self.settings("project")
3925
+ response = self.gql(
3926
+ mutation,
3927
+ variable_values={
3928
+ "entityName": entity_name,
3929
+ "projectName": project_name,
3930
+ "artifactTypeName": artifact_type_name,
3931
+ "description": description,
3932
+ },
3933
+ )
3934
+ _id: Optional[str] = response["createArtifactType"]["artifactType"]["id"]
3935
+ return _id
3936
+
3937
+ def server_artifact_introspection(self) -> List[str]:
3938
+ query_string = """
3939
+ query ProbeServerArtifact {
3940
+ ArtifactInfoType: __type(name:"Artifact") {
3941
+ fields {
3942
+ name
3943
+ }
3944
+ }
3945
+ }
3946
+ """
3947
+
3948
+ if self.server_artifact_fields_info is None:
3949
+ query = gql(query_string)
3950
+ res = self.gql(query)
3951
+ input_fields = res.get("ArtifactInfoType", {}).get("fields", [{}])
3952
+ self.server_artifact_fields_info = [
3953
+ field["name"] for field in input_fields if "name" in field
3954
+ ]
3955
+
3956
+ return self.server_artifact_fields_info
3957
+
3958
+ def server_create_artifact_introspection(self) -> List[str]:
3959
+ query_string = """
3960
+ query ProbeServerCreateArtifactInput {
3961
+ CreateArtifactInputInfoType: __type(name:"CreateArtifactInput") {
3962
+ inputFields{
3963
+ name
3964
+ }
3965
+ }
3966
+ }
3967
+ """
3968
+
3969
+ if self.server_create_artifact_input_info is None:
3970
+ query = gql(query_string)
3971
+ res = self.gql(query)
3972
+ input_fields = res.get("CreateArtifactInputInfoType", {}).get(
3973
+ "inputFields", [{}]
3974
+ )
3975
+ self.server_create_artifact_input_info = [
3976
+ field["name"] for field in input_fields if "name" in field
3977
+ ]
3978
+
3979
+ return self.server_create_artifact_input_info
3980
+
3981
+ def _get_create_artifact_mutation(
3982
+ self,
3983
+ fields: List,
3984
+ history_step: Optional[int],
3985
+ distributed_id: Optional[str],
3986
+ ) -> str:
3987
+ types = ""
3988
+ values = ""
3989
+
3990
+ if "historyStep" in fields and history_step not in [0, None]:
3991
+ types += "$historyStep: Int64!,"
3992
+ values += "historyStep: $historyStep,"
3993
+
3994
+ if distributed_id:
3995
+ types += "$distributedID: String,"
3996
+ values += "distributedID: $distributedID,"
3997
+
3998
+ if "clientID" in fields:
3999
+ types += "$clientID: ID,"
4000
+ values += "clientID: $clientID,"
4001
+
4002
+ if "sequenceClientID" in fields:
4003
+ types += "$sequenceClientID: ID,"
4004
+ values += "sequenceClientID: $sequenceClientID,"
4005
+
4006
+ if "enableDigestDeduplication" in fields:
4007
+ values += "enableDigestDeduplication: true,"
4008
+
4009
+ if "ttlDurationSeconds" in fields:
4010
+ types += "$ttlDurationSeconds: Int64,"
4011
+ values += "ttlDurationSeconds: $ttlDurationSeconds,"
4012
+
4013
+ if "tags" in fields:
4014
+ types += "$tags: [TagInput!],"
4015
+ values += "tags: $tags,"
4016
+
4017
+ query_template = """
4018
+ mutation CreateArtifact(
4019
+ $artifactTypeName: String!,
4020
+ $artifactCollectionNames: [String!],
4021
+ $entityName: String!,
4022
+ $projectName: String!,
4023
+ $runName: String,
4024
+ $description: String,
4025
+ $digest: String!,
4026
+ $aliases: [ArtifactAliasInput!],
4027
+ $metadata: JSONString,
4028
+ _CREATE_ARTIFACT_ADDITIONAL_TYPE_
4029
+ ) {
4030
+ createArtifact(input: {
4031
+ artifactTypeName: $artifactTypeName,
4032
+ artifactCollectionNames: $artifactCollectionNames,
4033
+ entityName: $entityName,
4034
+ projectName: $projectName,
4035
+ runName: $runName,
4036
+ description: $description,
4037
+ digest: $digest,
4038
+ digestAlgorithm: MANIFEST_MD5,
4039
+ aliases: $aliases,
4040
+ metadata: $metadata,
4041
+ _CREATE_ARTIFACT_ADDITIONAL_VALUE_
4042
+ }) {
4043
+ artifact {
4044
+ id
4045
+ state
4046
+ artifactSequence {
4047
+ id
4048
+ latestArtifact {
4049
+ id
4050
+ versionIndex
4051
+ }
4052
+ }
4053
+ }
4054
+ }
4055
+ }
4056
+ """
4057
+
4058
+ return query_template.replace(
4059
+ "_CREATE_ARTIFACT_ADDITIONAL_TYPE_", types
4060
+ ).replace("_CREATE_ARTIFACT_ADDITIONAL_VALUE_", values)
4061
+
4062
+ def create_artifact(
4063
+ self,
4064
+ artifact_type_name: str,
4065
+ artifact_collection_name: str,
4066
+ digest: str,
4067
+ client_id: Optional[str] = None,
4068
+ sequence_client_id: Optional[str] = None,
4069
+ entity_name: Optional[str] = None,
4070
+ project_name: Optional[str] = None,
4071
+ run_name: Optional[str] = None,
4072
+ description: Optional[str] = None,
4073
+ metadata: Optional[Dict] = None,
4074
+ ttl_duration_seconds: Optional[int] = None,
4075
+ aliases: Optional[List[Dict[str, str]]] = None,
4076
+ tags: Optional[List[Dict[str, str]]] = None,
4077
+ distributed_id: Optional[str] = None,
4078
+ is_user_created: Optional[bool] = False,
4079
+ history_step: Optional[int] = None,
4080
+ ) -> Tuple[Dict, Dict]:
4081
+ fields = self.server_create_artifact_introspection()
4082
+ artifact_fields = self.server_artifact_introspection()
4083
+ if ("ttlIsInherited" not in artifact_fields) and ttl_duration_seconds:
4084
+ wandb.termwarn(
4085
+ "Server not compatible with setting Artifact TTLs, please upgrade the server to use Artifact TTL"
4086
+ )
4087
+ # ttlDurationSeconds is only usable if ttlIsInherited is also present
4088
+ ttl_duration_seconds = None
4089
+ if ("tags" not in artifact_fields) and tags:
4090
+ wandb.termwarn(
4091
+ "Server not compatible with Artifact tags. "
4092
+ "To use Artifact tags, please upgrade the server to v0.85 or higher."
4093
+ )
4094
+
4095
+ query_template = self._get_create_artifact_mutation(
4096
+ fields, history_step, distributed_id
4097
+ )
4098
+
4099
+ entity_name = entity_name or self.settings("entity")
4100
+ project_name = project_name or self.settings("project")
4101
+ if not is_user_created:
4102
+ run_name = run_name or self.current_run_id
4103
+
4104
+ mutation = gql(query_template)
4105
+ response = self.gql(
4106
+ mutation,
4107
+ variable_values={
4108
+ "entityName": entity_name,
4109
+ "projectName": project_name,
4110
+ "runName": run_name,
4111
+ "artifactTypeName": artifact_type_name,
4112
+ "artifactCollectionNames": [artifact_collection_name],
4113
+ "clientID": client_id,
4114
+ "sequenceClientID": sequence_client_id,
4115
+ "digest": digest,
4116
+ "description": description,
4117
+ "aliases": list(aliases or []),
4118
+ "tags": list(tags or []),
4119
+ "metadata": json.dumps(util.make_safe_for_json(metadata))
4120
+ if metadata
4121
+ else None,
4122
+ "ttlDurationSeconds": ttl_duration_seconds,
4123
+ "distributedID": distributed_id,
4124
+ "historyStep": history_step,
4125
+ },
4126
+ )
4127
+ av = response["createArtifact"]["artifact"]
4128
+ latest = response["createArtifact"]["artifact"]["artifactSequence"].get(
4129
+ "latestArtifact"
4130
+ )
4131
+ return av, latest
4132
+
4133
+ def commit_artifact(self, artifact_id: str) -> "_Response":
4134
+ mutation = gql(
4135
+ """
4136
+ mutation CommitArtifact(
4137
+ $artifactID: ID!,
4138
+ ) {
4139
+ commitArtifact(input: {
4140
+ artifactID: $artifactID,
4141
+ }) {
4142
+ artifact {
4143
+ id
4144
+ digest
4145
+ }
4146
+ }
4147
+ }
4148
+ """
4149
+ )
4150
+
4151
+ response: _Response = self.gql(
4152
+ mutation,
4153
+ variable_values={"artifactID": artifact_id},
4154
+ timeout=60,
4155
+ )
4156
+ return response
4157
+
4158
+ def complete_multipart_upload_artifact(
4159
+ self,
4160
+ artifact_id: str,
4161
+ storage_path: str,
4162
+ completed_parts: List[Dict[str, Any]],
4163
+ upload_id: Optional[str],
4164
+ complete_multipart_action: str = "Complete",
4165
+ ) -> Optional[str]:
4166
+ mutation = gql(
4167
+ """
4168
+ mutation CompleteMultipartUploadArtifact(
4169
+ $completeMultipartAction: CompleteMultipartAction!,
4170
+ $completedParts: [UploadPartsInput!]!,
4171
+ $artifactID: ID!
4172
+ $storagePath: String!
4173
+ $uploadID: String!
4174
+ ) {
4175
+ completeMultipartUploadArtifact(
4176
+ input: {
4177
+ completeMultipartAction: $completeMultipartAction,
4178
+ completedParts: $completedParts,
4179
+ artifactID: $artifactID,
4180
+ storagePath: $storagePath
4181
+ uploadID: $uploadID
4182
+ }
4183
+ ) {
4184
+ digest
4185
+ }
4186
+ }
4187
+ """
4188
+ )
4189
+ response = self.gql(
4190
+ mutation,
4191
+ variable_values={
4192
+ "completeMultipartAction": complete_multipart_action,
4193
+ "artifactID": artifact_id,
4194
+ "storagePath": storage_path,
4195
+ "completedParts": completed_parts,
4196
+ "uploadID": upload_id,
4197
+ },
4198
+ )
4199
+ digest: Optional[str] = response["completeMultipartUploadArtifact"]["digest"]
4200
+ return digest
4201
+
4202
+ def create_artifact_manifest(
4203
+ self,
4204
+ name: str,
4205
+ digest: str,
4206
+ artifact_id: Optional[str],
4207
+ base_artifact_id: Optional[str] = None,
4208
+ entity: Optional[str] = None,
4209
+ project: Optional[str] = None,
4210
+ run: Optional[str] = None,
4211
+ include_upload: bool = True,
4212
+ type: str = "FULL",
4213
+ ) -> Tuple[str, Dict[str, Any]]:
4214
+ mutation = gql(
4215
+ """
4216
+ mutation CreateArtifactManifest(
4217
+ $name: String!,
4218
+ $digest: String!,
4219
+ $artifactID: ID!,
4220
+ $baseArtifactID: ID,
4221
+ $entityName: String!,
4222
+ $projectName: String!,
4223
+ $runName: String!,
4224
+ $includeUpload: Boolean!,
4225
+ {}
4226
+ ) {{
4227
+ createArtifactManifest(input: {{
4228
+ name: $name,
4229
+ digest: $digest,
4230
+ artifactID: $artifactID,
4231
+ baseArtifactID: $baseArtifactID,
4232
+ entityName: $entityName,
4233
+ projectName: $projectName,
4234
+ runName: $runName,
4235
+ {}
4236
+ }}) {{
4237
+ artifactManifest {{
4238
+ id
4239
+ file {{
4240
+ id
4241
+ name
4242
+ displayName
4243
+ uploadUrl @include(if: $includeUpload)
4244
+ uploadHeaders @include(if: $includeUpload)
4245
+ }}
4246
+ }}
4247
+ }}
4248
+ }}
4249
+ """.format(
4250
+ "$type: ArtifactManifestType = FULL" if type != "FULL" else "",
4251
+ "type: $type" if type != "FULL" else "",
4252
+ )
4253
+ )
4254
+
4255
+ entity_name = entity or self.settings("entity")
4256
+ project_name = project or self.settings("project")
4257
+ run_name = run or self.current_run_id
4258
+
4259
+ response = self.gql(
4260
+ mutation,
4261
+ variable_values={
4262
+ "name": name,
4263
+ "digest": digest,
4264
+ "artifactID": artifact_id,
4265
+ "baseArtifactID": base_artifact_id,
4266
+ "entityName": entity_name,
4267
+ "projectName": project_name,
4268
+ "runName": run_name,
4269
+ "includeUpload": include_upload,
4270
+ "type": type,
4271
+ },
4272
+ )
4273
+ return (
4274
+ response["createArtifactManifest"]["artifactManifest"]["id"],
4275
+ response["createArtifactManifest"]["artifactManifest"]["file"],
4276
+ )
4277
+
4278
+ def update_artifact_manifest(
4279
+ self,
4280
+ artifact_manifest_id: str,
4281
+ base_artifact_id: Optional[str] = None,
4282
+ digest: Optional[str] = None,
4283
+ include_upload: Optional[bool] = True,
4284
+ ) -> Tuple[str, Dict[str, Any]]:
4285
+ mutation = gql(
4286
+ """
4287
+ mutation UpdateArtifactManifest(
4288
+ $artifactManifestID: ID!,
4289
+ $digest: String,
4290
+ $baseArtifactID: ID,
4291
+ $includeUpload: Boolean!,
4292
+ ) {
4293
+ updateArtifactManifest(input: {
4294
+ artifactManifestID: $artifactManifestID,
4295
+ digest: $digest,
4296
+ baseArtifactID: $baseArtifactID,
4297
+ }) {
4298
+ artifactManifest {
4299
+ id
4300
+ file {
4301
+ id
4302
+ name
4303
+ displayName
4304
+ uploadUrl @include(if: $includeUpload)
4305
+ uploadHeaders @include(if: $includeUpload)
4306
+ }
4307
+ }
4308
+ }
4309
+ }
4310
+ """
4311
+ )
4312
+
4313
+ response = self.gql(
4314
+ mutation,
4315
+ variable_values={
4316
+ "artifactManifestID": artifact_manifest_id,
4317
+ "digest": digest,
4318
+ "baseArtifactID": base_artifact_id,
4319
+ "includeUpload": include_upload,
4320
+ },
4321
+ )
4322
+
4323
+ return (
4324
+ response["updateArtifactManifest"]["artifactManifest"]["id"],
4325
+ response["updateArtifactManifest"]["artifactManifest"]["file"],
4326
+ )
4327
+
4328
+ def update_artifact_metadata(
4329
+ self, artifact_id: str, metadata: Dict[str, Any]
4330
+ ) -> Dict[str, Any]:
4331
+ """Set the metadata of the given artifact version."""
4332
+ mutation = gql(
4333
+ """
4334
+ mutation UpdateArtifact(
4335
+ $artifactID: ID!,
4336
+ $metadata: JSONString,
4337
+ ) {
4338
+ updateArtifact(input: {
4339
+ artifactID: $artifactID,
4340
+ metadata: $metadata,
4341
+ }) {
4342
+ artifact {
4343
+ id
4344
+ }
4345
+ }
4346
+ }
4347
+ """
4348
+ )
4349
+ response = self.gql(
4350
+ mutation,
4351
+ variable_values={
4352
+ "artifactID": artifact_id,
4353
+ "metadata": json.dumps(metadata),
4354
+ },
4355
+ )
4356
+ return response["updateArtifact"]["artifact"]
4357
+
4358
+ def _resolve_client_id(
4359
+ self,
4360
+ client_id: str,
4361
+ ) -> Optional[str]:
4362
+ if client_id in self._client_id_mapping:
4363
+ return self._client_id_mapping[client_id]
4364
+
4365
+ query = gql(
4366
+ """
4367
+ query ClientIDMapping($clientID: ID!) {
4368
+ clientIDMapping(clientID: $clientID) {
4369
+ serverID
4370
+ }
4371
+ }
4372
+ """
4373
+ )
4374
+ response = self.gql(
4375
+ query,
4376
+ variable_values={
4377
+ "clientID": client_id,
4378
+ },
4379
+ )
4380
+ server_id = None
4381
+ if response is not None:
4382
+ client_id_mapping = response.get("clientIDMapping")
4383
+ if client_id_mapping is not None:
4384
+ server_id = client_id_mapping.get("serverID")
4385
+ if server_id is not None:
4386
+ self._client_id_mapping[client_id] = server_id
4387
+ return server_id
4388
+
4389
+ def server_create_artifact_file_spec_input_introspection(self) -> List:
4390
+ query_string = """
4391
+ query ProbeServerCreateArtifactFileSpecInput {
4392
+ CreateArtifactFileSpecInputInfoType: __type(name:"CreateArtifactFileSpecInput") {
4393
+ inputFields{
4394
+ name
4395
+ }
4396
+ }
4397
+ }
4398
+ """
4399
+
4400
+ query = gql(query_string)
4401
+ res = self.gql(query)
4402
+ create_artifact_file_spec_input_info = [
4403
+ field.get("name", "")
4404
+ for field in res.get("CreateArtifactFileSpecInputInfoType", {}).get(
4405
+ "inputFields", [{}]
4406
+ )
4407
+ ]
4408
+ return create_artifact_file_spec_input_info
4409
+
4410
+ @normalize_exceptions
4411
+ def create_artifact_files(
4412
+ self, artifact_files: Iterable["CreateArtifactFileSpecInput"]
4413
+ ) -> Mapping[str, "CreateArtifactFilesResponseFile"]:
4414
+ query_template = """
4415
+ mutation CreateArtifactFiles(
4416
+ $storageLayout: ArtifactStorageLayout!
4417
+ $artifactFiles: [CreateArtifactFileSpecInput!]!
4418
+ ) {
4419
+ createArtifactFiles(input: {
4420
+ artifactFiles: $artifactFiles,
4421
+ storageLayout: $storageLayout,
4422
+ }) {
4423
+ files {
4424
+ edges {
4425
+ node {
4426
+ id
4427
+ name
4428
+ displayName
4429
+ uploadUrl
4430
+ uploadHeaders
4431
+ _MULTIPART_UPLOAD_FIELDS_
4432
+ artifact {
4433
+ id
4434
+ }
4435
+ }
4436
+ }
4437
+ }
4438
+ }
4439
+ }
4440
+ """
4441
+ multipart_upload_url_query = """
4442
+ storagePath
4443
+ uploadMultipartUrls {
4444
+ uploadID
4445
+ uploadUrlParts {
4446
+ partNumber
4447
+ uploadUrl
4448
+ }
4449
+ }
4450
+ """
4451
+
4452
+ # TODO: we should use constants here from interface/artifacts.py
4453
+ # but probably don't want the dependency. We're going to remove
4454
+ # this setting in a future release, so I'm just hard-coding the strings.
4455
+ storage_layout = "V2"
4456
+ if env.get_use_v1_artifacts():
4457
+ storage_layout = "V1"
4458
+
4459
+ create_artifact_file_spec_input_fields = (
4460
+ self.server_create_artifact_file_spec_input_introspection()
4461
+ )
4462
+ if "uploadPartsInput" in create_artifact_file_spec_input_fields:
4463
+ query_template = query_template.replace(
4464
+ "_MULTIPART_UPLOAD_FIELDS_", multipart_upload_url_query
4465
+ )
4466
+ else:
4467
+ query_template = query_template.replace("_MULTIPART_UPLOAD_FIELDS_", "")
4468
+
4469
+ mutation = gql(query_template)
4470
+ response = self.gql(
4471
+ mutation,
4472
+ variable_values={
4473
+ "storageLayout": storage_layout,
4474
+ "artifactFiles": [af for af in artifact_files],
4475
+ },
4476
+ )
4477
+
4478
+ result = {}
4479
+ for edge in response["createArtifactFiles"]["files"]["edges"]:
4480
+ node = edge["node"]
4481
+ result[node["displayName"]] = node
4482
+ return result
4483
+
4484
+ @normalize_exceptions
4485
+ def notify_scriptable_run_alert(
4486
+ self,
4487
+ title: str,
4488
+ text: str,
4489
+ level: Optional[str] = None,
4490
+ wait_duration: Optional["Number"] = None,
4491
+ ) -> bool:
4492
+ mutation = gql(
4493
+ """
4494
+ mutation NotifyScriptableRunAlert(
4495
+ $entityName: String!,
4496
+ $projectName: String!,
4497
+ $runName: String!,
4498
+ $title: String!,
4499
+ $text: String!,
4500
+ $severity: AlertSeverity = INFO,
4501
+ $waitDuration: Duration
4502
+ ) {
4503
+ notifyScriptableRunAlert(input: {
4504
+ entityName: $entityName,
4505
+ projectName: $projectName,
4506
+ runName: $runName,
4507
+ title: $title,
4508
+ text: $text,
4509
+ severity: $severity,
4510
+ waitDuration: $waitDuration
4511
+ }) {
4512
+ success
4513
+ }
4514
+ }
4515
+ """
4516
+ )
4517
+
4518
+ response = self.gql(
4519
+ mutation,
4520
+ variable_values={
4521
+ "entityName": self.settings("entity"),
4522
+ "projectName": self.settings("project"),
4523
+ "runName": self.current_run_id,
4524
+ "title": title,
4525
+ "text": text,
4526
+ "severity": level,
4527
+ "waitDuration": wait_duration,
4528
+ },
4529
+ )
4530
+ success: bool = response["notifyScriptableRunAlert"]["success"]
4531
+ return success
4532
+
4533
+ def get_sweep_state(
4534
+ self, sweep: str, entity: Optional[str] = None, project: Optional[str] = None
4535
+ ) -> "SweepState":
4536
+ state: SweepState = self.sweep(
4537
+ sweep=sweep, entity=entity, project=project, specs="{}"
4538
+ )["state"]
4539
+ return state
4540
+
4541
+ def set_sweep_state(
4542
+ self,
4543
+ sweep: str,
4544
+ state: "SweepState",
4545
+ entity: Optional[str] = None,
4546
+ project: Optional[str] = None,
4547
+ ) -> None:
4548
+ assert state in ("RUNNING", "PAUSED", "CANCELED", "FINISHED")
4549
+ s = self.sweep(sweep=sweep, entity=entity, project=project, specs="{}")
4550
+ curr_state = s["state"].upper()
4551
+ if state == "PAUSED" and curr_state not in ("PAUSED", "RUNNING"):
4552
+ raise Exception(f"Cannot pause {curr_state.lower()} sweep.")
4553
+ elif state != "RUNNING" and curr_state not in ("RUNNING", "PAUSED", "PENDING"):
4554
+ raise Exception(f"Sweep already {curr_state.lower()}.")
4555
+ sweep_id = s["id"]
4556
+ mutation = gql(
4557
+ """
4558
+ mutation UpsertSweep(
4559
+ $id: ID,
4560
+ $state: String,
4561
+ $entityName: String,
4562
+ $projectName: String
4563
+ ) {
4564
+ upsertSweep(input: {
4565
+ id: $id,
4566
+ state: $state,
4567
+ entityName: $entityName,
4568
+ projectName: $projectName
4569
+ }){
4570
+ sweep {
4571
+ name
4572
+ }
4573
+ }
4574
+ }
4575
+ """
4576
+ )
4577
+ self.gql(
4578
+ mutation,
4579
+ variable_values={
4580
+ "id": sweep_id,
4581
+ "state": state,
4582
+ "entityName": entity or self.settings("entity"),
4583
+ "projectName": project or self.settings("project"),
4584
+ },
4585
+ )
4586
+
4587
+ def stop_sweep(
4588
+ self,
4589
+ sweep: str,
4590
+ entity: Optional[str] = None,
4591
+ project: Optional[str] = None,
4592
+ ) -> None:
4593
+ """Finish the sweep to stop running new runs and let currently running runs finish."""
4594
+ self.set_sweep_state(
4595
+ sweep=sweep, state="FINISHED", entity=entity, project=project
4596
+ )
4597
+
4598
+ def cancel_sweep(
4599
+ self,
4600
+ sweep: str,
4601
+ entity: Optional[str] = None,
4602
+ project: Optional[str] = None,
4603
+ ) -> None:
4604
+ """Cancel the sweep to kill all running runs and stop running new runs."""
4605
+ self.set_sweep_state(
4606
+ sweep=sweep, state="CANCELED", entity=entity, project=project
4607
+ )
4608
+
4609
+ def pause_sweep(
4610
+ self,
4611
+ sweep: str,
4612
+ entity: Optional[str] = None,
4613
+ project: Optional[str] = None,
4614
+ ) -> None:
4615
+ """Pause the sweep to temporarily stop running new runs."""
4616
+ self.set_sweep_state(
4617
+ sweep=sweep, state="PAUSED", entity=entity, project=project
4618
+ )
4619
+
4620
+ def resume_sweep(
4621
+ self,
4622
+ sweep: str,
4623
+ entity: Optional[str] = None,
4624
+ project: Optional[str] = None,
4625
+ ) -> None:
4626
+ """Resume the sweep to continue running new runs."""
4627
+ self.set_sweep_state(
4628
+ sweep=sweep, state="RUNNING", entity=entity, project=project
4629
+ )
4630
+
4631
+ def _status_request(self, url: str, length: int) -> requests.Response:
4632
+ """Ask google how much we've uploaded."""
4633
+ check_httpclient_logger_handler()
4634
+ return requests.put(
4635
+ url=url,
4636
+ headers={"Content-Length": "0", "Content-Range": f"bytes */{length}"},
4637
+ )
4638
+
4639
+ def _flatten_edges(self, response: "_Response") -> List[Dict]:
4640
+ """Return an array from the nested graphql relay structure."""
4641
+ return [node["node"] for node in response["edges"]]
4642
+
4643
+ @normalize_exceptions
4644
+ def stop_run(
4645
+ self,
4646
+ run_id: str,
4647
+ ) -> bool:
4648
+ mutation = gql(
4649
+ """
4650
+ mutation stopRun($id: ID!) {
4651
+ stopRun(input: {
4652
+ id: $id
4653
+ }) {
4654
+ clientMutationId
4655
+ success
4656
+ }
4657
+ }
4658
+ """
4659
+ )
4660
+
4661
+ response = self.gql(
4662
+ mutation,
4663
+ variable_values={
4664
+ "id": run_id,
4665
+ },
4666
+ )
4667
+
4668
+ success: bool = response["stopRun"].get("success")
4669
+
4670
+ return success
4671
+
4672
+ @normalize_exceptions
4673
+ def create_custom_chart(
4674
+ self,
4675
+ entity: str,
4676
+ name: str,
4677
+ display_name: str,
4678
+ spec_type: str,
4679
+ access: str,
4680
+ spec: Union[str, Mapping[str, Any]],
4681
+ ) -> Optional[Dict[str, Any]]:
4682
+ if not isinstance(spec, str):
4683
+ spec = json.dumps(spec)
4684
+
4685
+ mutation = gql(
4686
+ """
4687
+ mutation CreateCustomChart(
4688
+ $entity: String!
4689
+ $name: String!
4690
+ $displayName: String!
4691
+ $type: String!
4692
+ $access: String!
4693
+ $spec: JSONString!
4694
+ ) {
4695
+ createCustomChart(
4696
+ input: {
4697
+ entity: $entity
4698
+ name: $name
4699
+ displayName: $displayName
4700
+ type: $type
4701
+ access: $access
4702
+ spec: $spec
4703
+ }
4704
+ ) {
4705
+ chart { id }
4706
+ }
4707
+ }
4708
+ """
4709
+ )
4710
+
4711
+ variable_values = {
4712
+ "entity": entity,
4713
+ "name": name,
4714
+ "displayName": display_name,
4715
+ "type": spec_type,
4716
+ "access": access,
4717
+ "spec": spec,
4718
+ }
4719
+
4720
+ result: Optional[Dict[str, Any]] = self.gql(mutation, variable_values)[
4721
+ "createCustomChart"
4722
+ ]
4723
+ return result