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
wandb/sdk/wandb_run.py ADDED
@@ -0,0 +1,4088 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import functools
5
+ import glob
6
+ import json
7
+ import logging
8
+ import numbers
9
+ import os
10
+ import pathlib
11
+ import re
12
+ import sys
13
+ import threading
14
+ import time
15
+ import traceback
16
+ from collections.abc import Mapping
17
+ from dataclasses import dataclass, field
18
+ from datetime import datetime, timedelta, timezone
19
+ from enum import IntEnum
20
+ from types import TracebackType
21
+ from typing import TYPE_CHECKING, Callable, Sequence, TextIO, TypeVar
22
+
23
+ import requests
24
+ from typing_extensions import Any, Concatenate, Literal, NamedTuple, ParamSpec
25
+
26
+ import wandb
27
+ import wandb.env
28
+ import wandb.util
29
+ from wandb import trigger
30
+ from wandb.apis import internal, public
31
+ from wandb.apis.public import Api as PublicApi
32
+ from wandb.errors import CommError, UsageError
33
+ from wandb.errors.links import url_registry
34
+ from wandb.integration.torch import wandb_torch
35
+ from wandb.plot import CustomChart, Visualize
36
+ from wandb.proto.wandb_deprecated import Deprecated
37
+ from wandb.proto.wandb_internal_pb2 import (
38
+ MetricRecord,
39
+ PollExitResponse,
40
+ Result,
41
+ RunRecord,
42
+ )
43
+ from wandb.sdk.artifacts._internal_artifact import InternalArtifact
44
+ from wandb.sdk.artifacts._validators import is_artifact_registry_project
45
+ from wandb.sdk.artifacts.artifact import Artifact
46
+ from wandb.sdk.internal import job_builder
47
+ from wandb.sdk.lib import asyncio_compat, wb_logging
48
+ from wandb.sdk.lib.import_hooks import (
49
+ register_post_import_hook,
50
+ unregister_post_import_hook,
51
+ )
52
+ from wandb.sdk.lib.paths import FilePathStr, StrPath
53
+ from wandb.util import (
54
+ _is_artifact_object,
55
+ _is_artifact_string,
56
+ _is_artifact_version_weave_dict,
57
+ _is_py_requirements_or_dockerfile,
58
+ _resolve_aliases,
59
+ add_import_hook,
60
+ parse_artifact_string,
61
+ )
62
+
63
+ from . import wandb_config, wandb_metric, wandb_summary
64
+ from .artifacts._validators import (
65
+ MAX_ARTIFACT_METADATA_KEYS,
66
+ ArtifactPath,
67
+ validate_aliases,
68
+ validate_tags,
69
+ )
70
+ from .data_types._dtypes import TypeRegistry
71
+ from .interface.interface import FilesDict, GlobStr, InterfaceBase, PolicyName
72
+ from .interface.summary_record import SummaryRecord
73
+ from .lib import (
74
+ config_util,
75
+ deprecate,
76
+ filenames,
77
+ filesystem,
78
+ interrupt,
79
+ ipython,
80
+ module,
81
+ printer,
82
+ progress,
83
+ proto_util,
84
+ redirect,
85
+ telemetry,
86
+ )
87
+ from .lib.exit_hooks import ExitHooks
88
+ from .mailbox import (
89
+ HandleAbandonedError,
90
+ MailboxClosedError,
91
+ MailboxHandle,
92
+ wait_with_progress,
93
+ )
94
+ from .wandb_alerts import AlertLevel
95
+ from .wandb_settings import Settings
96
+ from .wandb_setup import _WandbSetup
97
+
98
+ if TYPE_CHECKING:
99
+ from typing import TypedDict
100
+
101
+ import torch # type: ignore [import-not-found]
102
+
103
+ import wandb.apis.public
104
+ import wandb.sdk.backend.backend
105
+ import wandb.sdk.interface.interface_queue
106
+ from wandb.proto.wandb_internal_pb2 import (
107
+ GetSummaryResponse,
108
+ InternalMessagesResponse,
109
+ SampledHistoryResponse,
110
+ )
111
+
112
+ class GitSourceDict(TypedDict):
113
+ remote: str
114
+ commit: str
115
+ entrypoint: list[str]
116
+ args: Sequence[str]
117
+
118
+ class ArtifactSourceDict(TypedDict):
119
+ artifact: str
120
+ entrypoint: list[str]
121
+ args: Sequence[str]
122
+
123
+ class ImageSourceDict(TypedDict):
124
+ image: str
125
+ args: Sequence[str]
126
+
127
+ class JobSourceDict(TypedDict, total=False):
128
+ _version: str
129
+ source_type: str
130
+ source: GitSourceDict | ArtifactSourceDict | ImageSourceDict
131
+ input_types: dict[str, Any]
132
+ output_types: dict[str, Any]
133
+ runtime: str | None
134
+ services: dict[str, str]
135
+
136
+
137
+ logger = logging.getLogger("wandb")
138
+ EXIT_TIMEOUT = 60
139
+ RE_LABEL = re.compile(r"[a-zA-Z0-9_-]+$")
140
+
141
+
142
+ class TeardownStage(IntEnum):
143
+ EARLY = 1
144
+ LATE = 2
145
+
146
+
147
+ class TeardownHook(NamedTuple):
148
+ call: Callable[[], None]
149
+ stage: TeardownStage
150
+
151
+
152
+ class RunStatusChecker:
153
+ """Periodically polls the background process for relevant updates.
154
+
155
+ - check if the user has requested a stop.
156
+ - check the network status.
157
+ - check the run sync status.
158
+ """
159
+
160
+ _stop_status_lock: threading.Lock
161
+ _stop_status_handle: MailboxHandle[Result] | None
162
+ _network_status_lock: threading.Lock
163
+ _network_status_handle: MailboxHandle[Result] | None
164
+ _internal_messages_lock: threading.Lock
165
+ _internal_messages_handle: MailboxHandle[Result] | None
166
+
167
+ def __init__(
168
+ self,
169
+ run_id: str,
170
+ interface: InterfaceBase,
171
+ settings: Settings,
172
+ stop_polling_interval: int = 15,
173
+ retry_polling_interval: int = 5,
174
+ internal_messages_polling_interval: int = 10,
175
+ ) -> None:
176
+ self._run_id = run_id
177
+ self._interface = interface
178
+ self._stop_polling_interval = stop_polling_interval
179
+ self._retry_polling_interval = retry_polling_interval
180
+ self._internal_messages_polling_interval = internal_messages_polling_interval
181
+ self._settings = settings
182
+
183
+ self._join_event = threading.Event()
184
+
185
+ self._stop_status_lock = threading.Lock()
186
+ self._stop_status_handle = None
187
+ self._stop_thread = threading.Thread(
188
+ target=self.check_stop_status,
189
+ name="ChkStopThr",
190
+ daemon=True,
191
+ )
192
+
193
+ self._network_status_lock = threading.Lock()
194
+ self._network_status_handle = None
195
+ self._network_status_thread = threading.Thread(
196
+ target=self.check_network_status,
197
+ name="NetStatThr",
198
+ daemon=True,
199
+ )
200
+
201
+ self._internal_messages_lock = threading.Lock()
202
+ self._internal_messages_handle = None
203
+ self._internal_messages_thread = threading.Thread(
204
+ target=self.check_internal_messages,
205
+ name="IntMsgThr",
206
+ daemon=True,
207
+ )
208
+
209
+ def start(self) -> None:
210
+ self._stop_thread.start()
211
+ self._network_status_thread.start()
212
+ self._internal_messages_thread.start()
213
+
214
+ @staticmethod
215
+ def _abandon_status_check(
216
+ lock: threading.Lock,
217
+ handle: MailboxHandle[Result] | None,
218
+ ):
219
+ with lock:
220
+ if handle:
221
+ handle.abandon()
222
+
223
+ def _loop_check_status(
224
+ self,
225
+ *,
226
+ lock: threading.Lock,
227
+ set_handle: Any,
228
+ timeout: int,
229
+ request: Any,
230
+ process: Any,
231
+ ) -> None:
232
+ local_handle: MailboxHandle[Result] | None = None
233
+ join_requested = False
234
+ while not join_requested:
235
+ time_probe = time.monotonic()
236
+ if not local_handle:
237
+ try:
238
+ local_handle = request()
239
+ except MailboxClosedError:
240
+ # This can happen if the service process dies.
241
+ break
242
+ assert local_handle
243
+
244
+ with lock:
245
+ if self._join_event.is_set():
246
+ break
247
+ set_handle(local_handle)
248
+
249
+ try:
250
+ result = local_handle.wait_or(timeout=timeout)
251
+ except HandleAbandonedError:
252
+ # This can happen if the service process dies.
253
+ break
254
+ except TimeoutError:
255
+ result = None
256
+
257
+ with lock:
258
+ set_handle(None)
259
+
260
+ if result:
261
+ process(result)
262
+ local_handle = None
263
+
264
+ time_elapsed = time.monotonic() - time_probe
265
+ wait_time = max(timeout - time_elapsed, 0)
266
+ join_requested = self._join_event.wait(timeout=wait_time)
267
+
268
+ def check_network_status(self) -> None:
269
+ def _process_network_status(result: Result) -> None:
270
+ network_status = result.response.network_status_response
271
+ for hr in network_status.network_responses:
272
+ if (
273
+ hr.http_status_code == 200 or hr.http_status_code == 0
274
+ ): # we use 0 for non-http errors (eg wandb errors)
275
+ wandb.termlog(f"{hr.http_response_text}")
276
+ else:
277
+ wandb.termlog(
278
+ f"{hr.http_status_code} encountered ({hr.http_response_text.rstrip()}), retrying request"
279
+ )
280
+
281
+ with wb_logging.log_to_run(self._run_id):
282
+ try:
283
+ self._loop_check_status(
284
+ lock=self._network_status_lock,
285
+ set_handle=lambda x: setattr(self, "_network_status_handle", x),
286
+ timeout=self._retry_polling_interval,
287
+ request=self._interface.deliver_network_status,
288
+ process=_process_network_status,
289
+ )
290
+ except BrokenPipeError:
291
+ self._abandon_status_check(
292
+ self._network_status_lock,
293
+ self._network_status_handle,
294
+ )
295
+
296
+ def check_stop_status(self) -> None:
297
+ def _process_stop_status(result: Result) -> None:
298
+ stop_status = result.response.stop_status_response
299
+ if stop_status.run_should_stop:
300
+ # TODO(frz): This check is required
301
+ # until WB-3606 is resolved on server side.
302
+ if not wandb.agents.pyagent.is_running(): # type: ignore
303
+ interrupt.interrupt_main()
304
+ return
305
+
306
+ with wb_logging.log_to_run(self._run_id):
307
+ try:
308
+ self._loop_check_status(
309
+ lock=self._stop_status_lock,
310
+ set_handle=lambda x: setattr(self, "_stop_status_handle", x),
311
+ timeout=self._stop_polling_interval,
312
+ request=self._interface.deliver_stop_status,
313
+ process=_process_stop_status,
314
+ )
315
+ except BrokenPipeError:
316
+ self._abandon_status_check(
317
+ self._stop_status_lock,
318
+ self._stop_status_handle,
319
+ )
320
+
321
+ def check_internal_messages(self) -> None:
322
+ def _process_internal_messages(result: Result) -> None:
323
+ if (
324
+ not self._settings.show_warnings
325
+ or self._settings.quiet
326
+ or self._settings.silent
327
+ ):
328
+ return
329
+ internal_messages = result.response.internal_messages_response
330
+ for msg in internal_messages.messages.warning:
331
+ wandb.termwarn(msg, repeat=False)
332
+
333
+ with wb_logging.log_to_run(self._run_id):
334
+ try:
335
+ self._loop_check_status(
336
+ lock=self._internal_messages_lock,
337
+ set_handle=lambda x: setattr(self, "_internal_messages_handle", x),
338
+ timeout=self._internal_messages_polling_interval,
339
+ request=self._interface.deliver_internal_messages,
340
+ process=_process_internal_messages,
341
+ )
342
+ except BrokenPipeError:
343
+ self._abandon_status_check(
344
+ self._internal_messages_lock,
345
+ self._internal_messages_handle,
346
+ )
347
+
348
+ def stop(self) -> None:
349
+ self._join_event.set()
350
+ self._abandon_status_check(
351
+ self._stop_status_lock,
352
+ self._stop_status_handle,
353
+ )
354
+ self._abandon_status_check(
355
+ self._network_status_lock,
356
+ self._network_status_handle,
357
+ )
358
+ self._abandon_status_check(
359
+ self._internal_messages_lock,
360
+ self._internal_messages_handle,
361
+ )
362
+
363
+ def join(self) -> None:
364
+ self.stop()
365
+ self._stop_thread.join()
366
+ self._network_status_thread.join()
367
+ self._internal_messages_thread.join()
368
+
369
+
370
+ _P = ParamSpec("_P")
371
+ _T = TypeVar("_T")
372
+
373
+
374
+ def _log_to_run(
375
+ func: Callable[Concatenate[Run, _P], _T],
376
+ ) -> Callable[Concatenate[Run, _P], _T]:
377
+ """Decorate a Run method to set the run ID in the logging context.
378
+
379
+ Any logs during the execution of the method go to the run's log file
380
+ and not to other runs' log files.
381
+
382
+ This is meant for use on all public methods and some callbacks. Private
383
+ methods can be assumed to be called from some public method somewhere.
384
+ The general rule is to use it on methods that can be called from a
385
+ context that isn't specific to this run (such as all user code or
386
+ internal methods that aren't run-specific).
387
+ """
388
+
389
+ @functools.wraps(func)
390
+ def wrapper(self: Run, *args, **kwargs) -> _T:
391
+ # In "attach" usage, many properties of the Run are not initially
392
+ # populated.
393
+ if hasattr(self, "_settings"):
394
+ run_id = self._settings.run_id
395
+ else:
396
+ run_id = self._attach_id
397
+
398
+ with wb_logging.log_to_run(run_id):
399
+ return func(self, *args, **kwargs)
400
+
401
+ return wrapper
402
+
403
+
404
+ _is_attaching: str = ""
405
+
406
+
407
+ def _attach(
408
+ func: Callable[Concatenate[Run, _P], _T],
409
+ ) -> Callable[Concatenate[Run, _P], _T]:
410
+ """Decorate a Run method to auto-attach when in a new process.
411
+
412
+ When in a forked process or using a pickled Run instance, this automatically
413
+ connects to the service process to "attach" to the existing run.
414
+ """
415
+
416
+ @functools.wraps(func)
417
+ def wrapper(self: Run, *args, **kwargs) -> _T:
418
+ global _is_attaching
419
+
420
+ # The _attach_id attribute is only None when running in the "disable
421
+ # service" mode.
422
+ #
423
+ # Since it is set early in `__init__` and included in the run's pickled
424
+ # state, the attribute always exists.
425
+ is_using_service = self._attach_id is not None
426
+
427
+ # The _attach_pid attribute is not pickled, so it might not exist.
428
+ # It is set when the run is initialized.
429
+ attach_pid = getattr(self, "_attach_pid", None)
430
+
431
+ if is_using_service and attach_pid != os.getpid():
432
+ if _is_attaching:
433
+ raise RuntimeError(
434
+ f"Trying to attach `{func.__name__}`"
435
+ f" while in the middle of attaching `{_is_attaching}`"
436
+ )
437
+
438
+ _is_attaching = func.__name__
439
+ try:
440
+ wandb._attach(run=self) # type: ignore
441
+ finally:
442
+ _is_attaching = ""
443
+
444
+ return func(self, *args, **kwargs)
445
+
446
+ return wrapper
447
+
448
+
449
+ def _raise_if_finished(
450
+ func: Callable[Concatenate[Run, _P], _T],
451
+ ) -> Callable[Concatenate[Run, _P], _T]:
452
+ """Decorate a Run method to raise an error after the run is finished."""
453
+
454
+ @functools.wraps(func)
455
+ def wrapper_fn(self: Run, *args, **kwargs) -> _T:
456
+ if not getattr(self, "_is_finished", False):
457
+ return func(self, *args, **kwargs)
458
+
459
+ message = (
460
+ f"Run ({self.id}) is finished. The call to"
461
+ f" `{func.__name__}` will be ignored."
462
+ f" Please make sure that you are using an active run."
463
+ )
464
+
465
+ raise UsageError(message)
466
+
467
+ return wrapper_fn
468
+
469
+
470
+ @dataclass
471
+ class RunStatus:
472
+ sync_items_total: int = field(default=0)
473
+ sync_items_pending: int = field(default=0)
474
+ sync_time: datetime | None = field(default=None)
475
+
476
+
477
+ class Run:
478
+ """A unit of computation logged by W&B. Typically, this is an ML experiment.
479
+
480
+ Call [`wandb.init()`](https://docs.wandb.ai/ref/python/init/) to create a
481
+ new run. `wandb.init()` starts a new run and returns a `wandb.Run` object.
482
+ Each run is associated with a unique ID (run ID). W&B recommends using
483
+ a context (`with` statement) manager to automatically finish the run.
484
+
485
+ For distributed training experiments, you can either track each process
486
+ separately using one run per process or track all processes to a single run.
487
+ See [Log distributed training experiments](https://docs.wandb.ai/guides/track/log/distributed-training)
488
+ for more information.
489
+
490
+ You can log data to a run with `wandb.Run.log()`. Anything you log using
491
+ `wandb.Run.log()` is sent to that run. See
492
+ [Create an experiment](https://docs.wandb.ai/guides/track/launch) or
493
+ [`wandb.init`](https://docs.wandb.ai/ref/python/init/) API reference page
494
+ or more information.
495
+
496
+ There is a another `Run` object in the
497
+ [`wandb.apis.public`](https://docs.wandb.ai/ref/python/public-api/api/)
498
+ namespace. Use this object is to interact with runs that have already been
499
+ created.
500
+
501
+ Attributes:
502
+ summary: (Summary) A summary of the run, which is a dictionary-like
503
+ object. For more information, see
504
+ [Log summary metrics](https://docs.wandb.ai/guides/track/log/log-summary/).
505
+
506
+ Examples:
507
+ Create a run with `wandb.init()`:
508
+
509
+ ```python
510
+ import wandb
511
+
512
+ # Start a new run and log some data
513
+ # Use context manager (`with` statement) to automatically finish the run
514
+ with wandb.init(entity="entity", project="project") as run:
515
+ run.log({"accuracy": acc, "loss": loss})
516
+ ```
517
+
518
+ <!-- lazydoc-ignore-init: internal -->
519
+ """
520
+
521
+ _telemetry_obj: telemetry.TelemetryRecord
522
+ _telemetry_obj_active: bool
523
+ _telemetry_obj_dirty: bool
524
+ _telemetry_obj_flushed: bytes
525
+
526
+ _teardown_hooks: list[TeardownHook]
527
+
528
+ _backend: wandb.sdk.backend.backend.Backend | None
529
+ _internal_run_interface: wandb.sdk.interface.interface_queue.InterfaceQueue | None
530
+ _wl: _WandbSetup | None
531
+
532
+ _out_redir: redirect.RedirectBase | None
533
+ _err_redir: redirect.RedirectBase | None
534
+ _redirect_cb: Callable[[str, str], None] | None
535
+ _redirect_raw_cb: Callable[[str, str], None] | None
536
+ _output_writer: filesystem.CRDedupedFile | None
537
+
538
+ _atexit_cleanup_called: bool
539
+ _hooks: ExitHooks | None
540
+ _exit_code: int | None
541
+
542
+ _run_status_checker: RunStatusChecker | None
543
+
544
+ _sampled_history: SampledHistoryResponse | None
545
+ _final_summary: GetSummaryResponse | None
546
+ _poll_exit_handle: MailboxHandle[Result] | None
547
+ _poll_exit_response: PollExitResponse | None
548
+ _internal_messages_response: InternalMessagesResponse | None
549
+
550
+ _stdout_slave_fd: int | None
551
+ _stderr_slave_fd: int | None
552
+ _artifact_slots: list[str]
553
+
554
+ _init_pid: int
555
+ _attach_pid: int
556
+
557
+ _attach_id: str | None
558
+ _is_attached: bool
559
+ _is_finished: bool
560
+ _settings: Settings
561
+
562
+ _forked: bool
563
+
564
+ _launch_artifacts: dict[str, Any] | None
565
+ _printer: printer.Printer
566
+
567
+ summary: wandb_summary.Summary
568
+
569
+ def __init__(
570
+ self,
571
+ settings: Settings,
572
+ config: dict[str, Any] | None = None,
573
+ sweep_config: dict[str, Any] | None = None,
574
+ launch_config: dict[str, Any] | None = None,
575
+ ) -> None:
576
+ # pid is set, so we know if this run object was initialized by this process
577
+ self._init_pid = os.getpid()
578
+ self._attach_id = None
579
+
580
+ if settings._noop:
581
+ # TODO: properly handle setting for disabled mode
582
+ self._settings = settings
583
+ return
584
+
585
+ self._init(
586
+ settings=settings,
587
+ config=config,
588
+ sweep_config=sweep_config,
589
+ launch_config=launch_config,
590
+ )
591
+
592
+ def _init(
593
+ self,
594
+ settings: Settings,
595
+ config: dict[str, Any] | None = None,
596
+ sweep_config: dict[str, Any] | None = None,
597
+ launch_config: dict[str, Any] | None = None,
598
+ ) -> None:
599
+ self._settings = settings
600
+
601
+ self._config = wandb_config.Config()
602
+ self._config._set_callback(self._config_callback)
603
+ self._config._set_artifact_callback(self._config_artifact_callback)
604
+ self._config._set_settings(self._settings)
605
+
606
+ # The _wandb key is always expected on the run config.
607
+ wandb_key = "_wandb"
608
+ self._config._update({wandb_key: dict()})
609
+
610
+ # TODO: perhaps this should be a property that is a noop on a finished run
611
+ self.summary = wandb_summary.Summary(
612
+ self._summary_get_current_summary_callback,
613
+ )
614
+ self.summary._set_update_callback(self._summary_update_callback)
615
+
616
+ self._step = 0
617
+ self._starting_step = 0
618
+ self._start_runtime = 0
619
+ # TODO: eventually would be nice to make this configurable using self._settings._start_time
620
+ # need to test (jhr): if you set start time to 2 days ago and run a test for 15 minutes,
621
+ # does the total time get calculated right (not as 2 days and 15 minutes)?
622
+ self._start_time = time.time()
623
+
624
+ self._printer = printer.new_printer(settings)
625
+
626
+ self._torch_history: wandb_torch.TorchHistory | None = None # type: ignore
627
+
628
+ self._backend = None
629
+ self._internal_run_interface = None
630
+ self._wl = None
631
+
632
+ self._hooks = None
633
+ self._teardown_hooks = []
634
+
635
+ self._output_writer = None
636
+ self._out_redir = None
637
+ self._err_redir = None
638
+ self._stdout_slave_fd = None
639
+ self._stderr_slave_fd = None
640
+
641
+ self._exit_code = None
642
+ self._exit_result = None
643
+
644
+ self._used_artifact_slots: dict[str, str] = {}
645
+
646
+ # Created when the run "starts".
647
+ self._run_status_checker = None
648
+
649
+ self._sampled_history = None
650
+ self._final_summary = None
651
+ self._poll_exit_response = None
652
+ self._internal_messages_response = None
653
+ self._poll_exit_handle = None
654
+
655
+ # Initialize telemetry object
656
+ self._telemetry_obj = telemetry.TelemetryRecord()
657
+ self._telemetry_obj_active = False
658
+ self._telemetry_obj_flushed = b""
659
+ self._telemetry_obj_dirty = False
660
+
661
+ self._atexit_cleanup_called = False
662
+
663
+ # Initial scope setup for sentry.
664
+ # This might get updated when the actual run comes back.
665
+ wandb._sentry.configure_scope(
666
+ tags=dict(self._settings),
667
+ process_context="user",
668
+ )
669
+
670
+ self._launch_artifact_mapping: dict[str, Any] = {}
671
+ self._unique_launch_artifact_sequence_names: dict[str, Any] = {}
672
+
673
+ # Populate config
674
+ config = config or dict()
675
+ self._config._update(config, allow_val_change=True, ignore_locked=True)
676
+
677
+ if sweep_config:
678
+ self._config.merge_locked(
679
+ sweep_config, user="sweep", _allow_val_change=True
680
+ )
681
+
682
+ if launch_config:
683
+ self._config.merge_locked(
684
+ launch_config, user="launch", _allow_val_change=True
685
+ )
686
+
687
+ # if run is from a launch queue, add queue id to _wandb config
688
+ launch_queue_name = wandb.env.get_launch_queue_name()
689
+ if launch_queue_name:
690
+ self._config[wandb_key]["launch_queue_name"] = launch_queue_name
691
+
692
+ launch_queue_entity = wandb.env.get_launch_queue_entity()
693
+ if launch_queue_entity:
694
+ self._config[wandb_key]["launch_queue_entity"] = launch_queue_entity
695
+
696
+ launch_trace_id = wandb.env.get_launch_trace_id()
697
+ if launch_trace_id:
698
+ self._config[wandb_key]["launch_trace_id"] = launch_trace_id
699
+
700
+ self._attach_id = None
701
+ self._is_attached = False
702
+ self._is_finished = False
703
+
704
+ self._attach_pid = os.getpid()
705
+ self._forked = False
706
+ # for now, use runid as attach id, this could/should be versioned in the future
707
+ self._attach_id = self._settings.run_id
708
+
709
+ def _handle_launch_artifact_overrides(self) -> None:
710
+ if self._settings.launch and (os.environ.get("WANDB_ARTIFACTS") is not None):
711
+ try:
712
+ artifacts: dict[str, Any] = json.loads(
713
+ os.environ.get("WANDB_ARTIFACTS", "{}")
714
+ )
715
+ except (ValueError, SyntaxError):
716
+ wandb.termwarn("Malformed WANDB_ARTIFACTS, using original artifacts")
717
+ else:
718
+ self._initialize_launch_artifact_maps(artifacts)
719
+
720
+ elif (
721
+ self._settings.launch
722
+ and self._settings.launch_config_path
723
+ and os.path.exists(self._settings.launch_config_path)
724
+ ):
725
+ self.save(self._settings.launch_config_path)
726
+ with open(self._settings.launch_config_path) as fp:
727
+ launch_config = json.loads(fp.read())
728
+ if launch_config.get("overrides", {}).get("artifacts") is not None:
729
+ artifacts = launch_config.get("overrides").get("artifacts")
730
+ self._initialize_launch_artifact_maps(artifacts)
731
+
732
+ def _initialize_launch_artifact_maps(self, artifacts: dict[str, Any]) -> None:
733
+ for key, item in artifacts.items():
734
+ self._launch_artifact_mapping[key] = item
735
+ artifact_sequence_tuple_or_slot = key.split(":")
736
+
737
+ if len(artifact_sequence_tuple_or_slot) == 2:
738
+ sequence_name = artifact_sequence_tuple_or_slot[0].split("/")[-1]
739
+ if self._unique_launch_artifact_sequence_names.get(sequence_name):
740
+ self._unique_launch_artifact_sequence_names.pop(sequence_name)
741
+ else:
742
+ self._unique_launch_artifact_sequence_names[sequence_name] = item
743
+
744
+ def _telemetry_callback(self, telem_obj: telemetry.TelemetryRecord) -> None:
745
+ if not hasattr(self, "_telemetry_obj") or self._is_finished:
746
+ return
747
+
748
+ self._telemetry_obj.MergeFrom(telem_obj)
749
+ self._telemetry_obj_dirty = True
750
+ self._telemetry_flush()
751
+
752
+ def _telemetry_flush(self) -> None:
753
+ if not hasattr(self, "_telemetry_obj"):
754
+ return
755
+ if not self._telemetry_obj_active:
756
+ return
757
+ if not self._telemetry_obj_dirty:
758
+ return
759
+ if self._backend and self._backend.interface:
760
+ serialized = self._telemetry_obj.SerializeToString()
761
+ if serialized == self._telemetry_obj_flushed:
762
+ return
763
+ self._backend.interface._publish_telemetry(self._telemetry_obj)
764
+ self._telemetry_obj_flushed = serialized
765
+ self._telemetry_obj_dirty = False
766
+
767
+ def _freeze(self) -> None:
768
+ self._frozen = True
769
+
770
+ def __setattr__(self, attr: str, value: object) -> None:
771
+ if getattr(self, "_frozen", None) and not hasattr(self, attr):
772
+ raise Exception(f"Attribute {attr} is not supported on Run object.")
773
+ super().__setattr__(attr, value)
774
+
775
+ def __deepcopy__(self, memo: dict[int, Any]) -> Run:
776
+ return self
777
+
778
+ def __getstate__(self) -> Any:
779
+ """Return run state as a custom pickle."""
780
+ # We only pickle in service mode
781
+ if not self._settings:
782
+ return
783
+
784
+ _attach_id = self._attach_id
785
+ if not _attach_id:
786
+ return
787
+
788
+ return dict(
789
+ _attach_id=_attach_id,
790
+ _init_pid=self._init_pid,
791
+ _is_finished=self._is_finished,
792
+ )
793
+
794
+ def __setstate__(self, state: Any) -> None:
795
+ """Set run state from a custom pickle."""
796
+ if not state:
797
+ return
798
+
799
+ _attach_id = state.get("_attach_id")
800
+ if not _attach_id:
801
+ return
802
+
803
+ if state["_init_pid"] == os.getpid():
804
+ raise RuntimeError("attach in the same process is not supported currently")
805
+
806
+ self.__dict__.update(state)
807
+
808
+ @property
809
+ def _torch(self) -> wandb_torch.TorchHistory: # type: ignore
810
+ if self._torch_history is None:
811
+ self._torch_history = wandb_torch.TorchHistory() # type: ignore
812
+ return self._torch_history
813
+
814
+ @property
815
+ @_log_to_run
816
+ @_attach
817
+ def settings(self) -> Settings:
818
+ """A frozen copy of run's Settings object."""
819
+ return self._settings.model_copy(deep=True)
820
+
821
+ @property
822
+ @_log_to_run
823
+ @_attach
824
+ def dir(self) -> str:
825
+ """The directory where files associated with the run are saved."""
826
+ return self._settings.files_dir
827
+
828
+ @property
829
+ @_log_to_run
830
+ @_attach
831
+ def config(self) -> wandb_config.Config:
832
+ """Config object associated with this run."""
833
+ return self._config
834
+
835
+ @property
836
+ @_log_to_run
837
+ @_attach
838
+ def config_static(self) -> wandb_config.ConfigStatic:
839
+ """Static config object associated with this run."""
840
+ return wandb_config.ConfigStatic(self._config)
841
+
842
+ @property
843
+ @_log_to_run
844
+ @_attach
845
+ def name(self) -> str | None:
846
+ """Display name of the run.
847
+
848
+ Display names are not guaranteed to be unique and may be descriptive.
849
+ By default, they are randomly generated.
850
+ """
851
+ return self._settings.run_name
852
+
853
+ @name.setter
854
+ @_log_to_run
855
+ @_raise_if_finished
856
+ def name(self, name: str) -> None:
857
+ with telemetry.context(run=self) as tel:
858
+ tel.feature.set_run_name = True
859
+ self._settings.run_name = name
860
+ if self._backend and self._backend.interface:
861
+ self._backend.interface.publish_run(self)
862
+
863
+ @property
864
+ @_log_to_run
865
+ @_attach
866
+ def notes(self) -> str | None:
867
+ """Notes associated with the run, if there are any.
868
+
869
+ Notes can be a multiline string and can also use markdown and latex
870
+ equations inside `$$`, like `$x + 3$`.
871
+ """
872
+ return self._settings.run_notes
873
+
874
+ @notes.setter
875
+ @_log_to_run
876
+ @_raise_if_finished
877
+ def notes(self, notes: str) -> None:
878
+ self._settings.run_notes = notes
879
+ if self._backend and self._backend.interface:
880
+ self._backend.interface.publish_run(self)
881
+
882
+ @property
883
+ @_log_to_run
884
+ @_attach
885
+ def tags(self) -> tuple | None:
886
+ """Tags associated with the run, if there are any."""
887
+ return self._settings.run_tags or ()
888
+
889
+ @tags.setter
890
+ @_log_to_run
891
+ @_raise_if_finished
892
+ def tags(self, tags: Sequence) -> None:
893
+ with telemetry.context(run=self) as tel:
894
+ tel.feature.set_run_tags = True
895
+ self._settings.run_tags = tuple(tags)
896
+ if self._backend and self._backend.interface:
897
+ self._backend.interface.publish_run(self)
898
+
899
+ @property
900
+ @_log_to_run
901
+ @_attach
902
+ def id(self) -> str:
903
+ """Identifier for this run."""
904
+ assert self._settings.run_id is not None
905
+ return self._settings.run_id
906
+
907
+ @property
908
+ @_log_to_run
909
+ @_attach
910
+ def sweep_id(self) -> str | None:
911
+ """Identifier for the sweep associated with the run, if there is one."""
912
+ return self._settings.sweep_id
913
+
914
+ def _get_path(self) -> str:
915
+ return "/".join(
916
+ e
917
+ for e in [
918
+ self._settings.entity,
919
+ self._settings.project,
920
+ self._settings.run_id,
921
+ ]
922
+ if e is not None
923
+ )
924
+
925
+ @property
926
+ @_log_to_run
927
+ @_attach
928
+ def path(self) -> str:
929
+ """Path to the run.
930
+
931
+ Run paths include entity, project, and run ID, in the format
932
+ `entity/project/run_id`.
933
+ """
934
+ return self._get_path()
935
+
936
+ @property
937
+ @_log_to_run
938
+ @_attach
939
+ def start_time(self) -> float:
940
+ """Unix timestamp (in seconds) of when the run started."""
941
+ return self._start_time
942
+
943
+ @property
944
+ @_log_to_run
945
+ @_attach
946
+ def starting_step(self) -> int:
947
+ """The first step of the run.
948
+
949
+ <!-- lazydoc-ignore: internal -->
950
+ """
951
+ return self._starting_step
952
+
953
+ @property
954
+ @_log_to_run
955
+ @_attach
956
+ def resumed(self) -> bool:
957
+ """True if the run was resumed, False otherwise."""
958
+ return self._settings.resumed
959
+
960
+ @property
961
+ @_log_to_run
962
+ @_attach
963
+ def step(self) -> int:
964
+ """Current value of the step.
965
+
966
+ This counter is incremented by `wandb.Run.log()`.
967
+
968
+ <!-- lazydoc-ignore: internal -->
969
+ """
970
+ return self._step
971
+
972
+ @property
973
+ @_log_to_run
974
+ @_attach
975
+ def offline(self) -> bool:
976
+ """True if the run is offline, False otherwise."""
977
+ return self._settings._offline
978
+
979
+ @property
980
+ @_log_to_run
981
+ @_attach
982
+ def disabled(self) -> bool:
983
+ """True if the run is disabled, False otherwise."""
984
+ return self._settings._noop
985
+
986
+ @property
987
+ @_log_to_run
988
+ @_attach
989
+ def group(self) -> str:
990
+ """Returns the name of the group associated with this run.
991
+
992
+ Grouping runs together allows related experiments to be organized and
993
+ visualized collectively in the W&B UI. This is especially useful for
994
+ scenarios such as distributed training or cross-validation, where
995
+ multiple runs should be viewed and managed as a unified experiment.
996
+
997
+ In shared mode, where all processes share the same run object,
998
+ setting a group is usually unnecessary, since there is only one
999
+ run and no grouping is required.
1000
+ """
1001
+ return self._settings.run_group or ""
1002
+
1003
+ @property
1004
+ @_log_to_run
1005
+ @_attach
1006
+ def job_type(self) -> str:
1007
+ """Name of the job type associated with the run.
1008
+
1009
+ View a run's job type in the run's Overview page in the W&B App.
1010
+
1011
+ You can use this to categorize runs by their job type, such as
1012
+ "training", "evaluation", or "inference". This is useful for organizing
1013
+ and filtering runs in the W&B UI, especially when you have multiple
1014
+ runs with different job types in the same project. For more
1015
+ information, see [Organize runs](https://docs.wandb.ai/guides/runs/#organize-runs).
1016
+ """
1017
+ return self._settings.run_job_type or ""
1018
+
1019
+ def project_name(self) -> str:
1020
+ """This method is deprecated and will be removed in a future release. Use `run.project` instead.
1021
+
1022
+ Name of the W&B project associated with the run.
1023
+
1024
+ <!-- lazydoc-ignore: internal -->
1025
+ """
1026
+ deprecate.deprecate(
1027
+ field_name=Deprecated.run__project_name,
1028
+ warning_message=(
1029
+ "The project_name method is deprecated and will be removed in a"
1030
+ " future release. Please use `run.project` instead."
1031
+ ),
1032
+ )
1033
+ return self.project
1034
+
1035
+ @property
1036
+ @_log_to_run
1037
+ @_attach
1038
+ def project(self) -> str:
1039
+ """Name of the W&B project associated with the run."""
1040
+ assert self._settings.project is not None
1041
+ return self._settings.project
1042
+
1043
+ @_log_to_run
1044
+ def get_project_url(self) -> str | None:
1045
+ """This method is deprecated and will be removed in a future release. Use `run.project_url` instead.
1046
+
1047
+ URL of the W&B project associated with the run, if there is one.
1048
+ Offline runs do not have a project URL.
1049
+
1050
+ <!-- lazydoc-ignore: internal -->
1051
+ """
1052
+ deprecate.deprecate(
1053
+ field_name=Deprecated.run__get_project_url,
1054
+ warning_message=(
1055
+ "The get_project_url method is deprecated and will be removed in a"
1056
+ " future release. Please use `run.project_url` instead."
1057
+ ),
1058
+ )
1059
+ return self.project_url
1060
+
1061
+ @property
1062
+ @_log_to_run
1063
+ @_attach
1064
+ def project_url(self) -> str | None:
1065
+ """URL of the W&B project associated with the run, if there is one.
1066
+
1067
+ Offline runs do not have a project URL.
1068
+ """
1069
+ if self._settings._offline:
1070
+ wandb.termwarn("URL not available in offline run")
1071
+ return None
1072
+ return self._settings.project_url
1073
+
1074
+ @_raise_if_finished
1075
+ @_log_to_run
1076
+ @_attach
1077
+ def log_code(
1078
+ self,
1079
+ root: str | None = ".",
1080
+ name: str | None = None,
1081
+ include_fn: Callable[[str, str], bool]
1082
+ | Callable[[str], bool] = _is_py_requirements_or_dockerfile,
1083
+ exclude_fn: Callable[[str, str], bool]
1084
+ | Callable[[str], bool] = filenames.exclude_wandb_fn,
1085
+ ) -> Artifact | None:
1086
+ """Save the current state of your code to a W&B Artifact.
1087
+
1088
+ By default, it walks the current directory and logs all files that end with `.py`.
1089
+
1090
+ Args:
1091
+ root: The relative (to `os.getcwd()`) or absolute path to recursively find code from.
1092
+ name: (str, optional) The name of our code artifact. By default, we'll name
1093
+ the artifact `source-$PROJECT_ID-$ENTRYPOINT_RELPATH`. There may be scenarios where you want
1094
+ many runs to share the same artifact. Specifying name allows you to achieve that.
1095
+ include_fn: A callable that accepts a file path and (optionally) root path and
1096
+ returns True when it should be included and False otherwise. This
1097
+ defaults to `lambda path, root: path.endswith(".py")`.
1098
+ exclude_fn: A callable that accepts a file path and (optionally) root path and
1099
+ returns `True` when it should be excluded and `False` otherwise. This
1100
+ defaults to a function that excludes all files within `<root>/.wandb/`
1101
+ and `<root>/wandb/` directories.
1102
+
1103
+ Examples:
1104
+ Basic usage
1105
+
1106
+ ```python
1107
+ import wandb
1108
+
1109
+ with wandb.init() as run:
1110
+ run.log_code()
1111
+ ```
1112
+
1113
+ Advanced usage
1114
+
1115
+ ```python
1116
+ import wandb
1117
+
1118
+ with wandb.init() as run:
1119
+ run.log_code(
1120
+ root="../",
1121
+ include_fn=lambda path: path.endswith(".py") or path.endswith(".ipynb"),
1122
+ exclude_fn=lambda path, root: os.path.relpath(path, root).startswith(
1123
+ "cache/"
1124
+ ),
1125
+ )
1126
+ ```
1127
+
1128
+ Returns:
1129
+ An `Artifact` object if code was logged
1130
+ """
1131
+ if name is None:
1132
+ if self.settings._jupyter:
1133
+ notebook_name = None
1134
+ if self.settings.notebook_name:
1135
+ notebook_name = self.settings.notebook_name
1136
+ elif self.settings.x_jupyter_path:
1137
+ if self.settings.x_jupyter_path.startswith("fileId="):
1138
+ notebook_name = self.settings.x_jupyter_name
1139
+ else:
1140
+ notebook_name = self.settings.x_jupyter_path
1141
+ name_string = f"{self._settings.project}-{notebook_name}"
1142
+ else:
1143
+ name_string = (
1144
+ f"{self._settings.project}-{self._settings.program_relpath}"
1145
+ )
1146
+ name = wandb.util.make_artifact_name_safe(f"source-{name_string}")
1147
+ art = InternalArtifact(name, "code")
1148
+ files_added = False
1149
+ if root is not None:
1150
+ root = os.path.abspath(root)
1151
+ for file_path in filenames.filtered_dir(root, include_fn, exclude_fn):
1152
+ files_added = True
1153
+ save_name = os.path.relpath(file_path, root)
1154
+ art.add_file(file_path, name=save_name)
1155
+ # Add any manually staged files such as ipynb notebooks
1156
+ for dirpath, _, files in os.walk(self._settings._tmp_code_dir):
1157
+ for fname in files:
1158
+ file_path = os.path.join(dirpath, fname)
1159
+ save_name = os.path.relpath(file_path, self._settings._tmp_code_dir)
1160
+ files_added = True
1161
+ art.add_file(file_path, name=save_name)
1162
+ if not files_added:
1163
+ wandb.termwarn(
1164
+ "No relevant files were detected in the specified directory. No code will be logged to your run."
1165
+ )
1166
+ return None
1167
+
1168
+ artifact = self._log_artifact(art)
1169
+
1170
+ self._config.update(
1171
+ {"_wandb": {"code_path": artifact.name}},
1172
+ allow_val_change=True,
1173
+ )
1174
+
1175
+ return artifact
1176
+
1177
+ @_log_to_run
1178
+ def get_sweep_url(self) -> str | None:
1179
+ """This method is deprecated and will be removed in a future release. Use `run.sweep_url` instead.
1180
+
1181
+ The URL of the sweep associated with the run, if there is one.
1182
+ Offline runs do not have a sweep URL.
1183
+
1184
+ <!-- lazydoc-ignore: internal -->
1185
+ """
1186
+ deprecate.deprecate(
1187
+ field_name=Deprecated.run__get_sweep_url,
1188
+ warning_message=(
1189
+ "The get_sweep_url method is deprecated and will be removed in a"
1190
+ " future release. Please use `run.sweep_url` instead."
1191
+ ),
1192
+ )
1193
+ return self.sweep_url
1194
+
1195
+ @property
1196
+ @_attach
1197
+ def sweep_url(self) -> str | None:
1198
+ """URL of the sweep associated with the run, if there is one.
1199
+
1200
+ Offline runs do not have a sweep URL.
1201
+ """
1202
+ if self._settings._offline:
1203
+ wandb.termwarn("URL not available in offline run")
1204
+ return None
1205
+ return self._settings.sweep_url
1206
+
1207
+ @_log_to_run
1208
+ def get_url(self) -> str | None:
1209
+ """This method is deprecated and will be removed in a future release. Use `run.url` instead.
1210
+
1211
+ URL of the W&B run, if there is one. Offline runs do not have a URL.
1212
+
1213
+ <!-- lazydoc-ignore: internal -->
1214
+ """
1215
+ deprecate.deprecate(
1216
+ field_name=Deprecated.run__get_url,
1217
+ warning_message=(
1218
+ "The get_url method is deprecated and will be removed in a"
1219
+ " future release. Please use `run.url` instead."
1220
+ ),
1221
+ )
1222
+ return self.url
1223
+
1224
+ @property
1225
+ @_log_to_run
1226
+ @_attach
1227
+ def url(self) -> str | None:
1228
+ """The url for the W&B run, if there is one.
1229
+
1230
+ Offline runs will not have a url.
1231
+ """
1232
+ if self._settings._offline:
1233
+ wandb.termwarn("URL not available in offline run")
1234
+ return None
1235
+ return self._settings.run_url
1236
+
1237
+ @property
1238
+ @_log_to_run
1239
+ @_attach
1240
+ def entity(self) -> str:
1241
+ """The name of the W&B entity associated with the run.
1242
+
1243
+ Entity can be a username or the name of a team or organization.
1244
+ """
1245
+ return self._settings.entity or ""
1246
+
1247
+ def _label_internal(
1248
+ self,
1249
+ code: str | None = None,
1250
+ repo: str | None = None,
1251
+ code_version: str | None = None,
1252
+ ) -> None:
1253
+ with telemetry.context(run=self) as tel:
1254
+ if code and RE_LABEL.match(code):
1255
+ tel.label.code_string = code
1256
+ if repo and RE_LABEL.match(repo):
1257
+ tel.label.repo_string = repo
1258
+ if code_version and RE_LABEL.match(code_version):
1259
+ tel.label.code_version = code_version
1260
+
1261
+ def _label(
1262
+ self,
1263
+ code: str | None = None,
1264
+ repo: str | None = None,
1265
+ code_version: str | None = None,
1266
+ **kwargs: str,
1267
+ ) -> None:
1268
+ if self._settings.label_disable:
1269
+ return
1270
+ for k, v in (("code", code), ("repo", repo), ("code_version", code_version)):
1271
+ if v and not RE_LABEL.match(v):
1272
+ wandb.termwarn(
1273
+ f"Label added for '{k}' with invalid identifier '{v}' (ignored).",
1274
+ repeat=False,
1275
+ )
1276
+ for v in kwargs:
1277
+ wandb.termwarn(
1278
+ f"Label added for unsupported key {v!r} (ignored).",
1279
+ repeat=False,
1280
+ )
1281
+
1282
+ self._label_internal(code=code, repo=repo, code_version=code_version)
1283
+
1284
+ # update telemetry in the backend immediately for _label() callers
1285
+ self._telemetry_flush()
1286
+
1287
+ def _label_probe_lines(self, lines: list[str]) -> None:
1288
+ if not lines:
1289
+ return
1290
+ parsed = telemetry._parse_label_lines(lines)
1291
+ if not parsed:
1292
+ return
1293
+ label_dict = {}
1294
+ code = parsed.get("code") or parsed.get("c")
1295
+ if code:
1296
+ label_dict["code"] = code
1297
+ repo = parsed.get("repo") or parsed.get("r")
1298
+ if repo:
1299
+ label_dict["repo"] = repo
1300
+ code_ver = parsed.get("version") or parsed.get("v")
1301
+ if code_ver:
1302
+ label_dict["code_version"] = code_ver
1303
+ self._label_internal(**label_dict)
1304
+
1305
+ def _label_probe_main(self) -> None:
1306
+ m = sys.modules.get("__main__")
1307
+ if not m:
1308
+ return
1309
+ doc = getattr(m, "__doc__", None)
1310
+ if not doc:
1311
+ return
1312
+
1313
+ doclines = doc.splitlines()
1314
+ self._label_probe_lines(doclines)
1315
+
1316
+ # TODO: annotate jupyter Notebook class
1317
+ def _label_probe_notebook(self, notebook: Any) -> None:
1318
+ logger.info("probe notebook")
1319
+ lines = None
1320
+ try:
1321
+ data = notebook.probe_ipynb()
1322
+ cell0 = data.get("cells", [])[0]
1323
+ lines = cell0.get("source")
1324
+ # kaggle returns a string instead of a list
1325
+ if isinstance(lines, str):
1326
+ lines = lines.split()
1327
+ except Exception as e:
1328
+ logger.info(f"Unable to probe notebook: {e}")
1329
+ return
1330
+ if lines:
1331
+ self._label_probe_lines(lines)
1332
+
1333
+ @_log_to_run
1334
+ @_attach
1335
+ def display(self, height: int = 420, hidden: bool = False) -> bool:
1336
+ """Display this run in Jupyter."""
1337
+ if self._settings.silent:
1338
+ return False
1339
+
1340
+ if not ipython.in_jupyter():
1341
+ return False
1342
+
1343
+ try:
1344
+ from IPython import display
1345
+ except ImportError:
1346
+ wandb.termwarn(".display() only works in jupyter environments")
1347
+ return False
1348
+
1349
+ display.display(display.HTML(self.to_html(height, hidden)))
1350
+ return True
1351
+
1352
+ @_log_to_run
1353
+ @_attach
1354
+ def to_html(self, height: int = 420, hidden: bool = False) -> str:
1355
+ """Generate HTML containing an iframe displaying the current run.
1356
+
1357
+ <!-- lazydoc-ignore: internal -->
1358
+ """
1359
+ url = self._settings.run_url + "?jupyter=true"
1360
+ style = f"border:none;width:100%;height:{height}px;"
1361
+ prefix = ""
1362
+ if hidden:
1363
+ style += "display:none;"
1364
+ prefix = ipython.toggle_button()
1365
+ return prefix + f"<iframe src={url!r} style={style!r}></iframe>"
1366
+
1367
+ def _repr_mimebundle_(
1368
+ self, include: Any | None = None, exclude: Any | None = None
1369
+ ) -> dict[str, str]:
1370
+ return {"text/html": self.to_html(hidden=True)}
1371
+
1372
+ @_log_to_run
1373
+ @_raise_if_finished
1374
+ def _config_callback(
1375
+ self,
1376
+ key: tuple[str, ...] | str | None = None,
1377
+ val: Any | None = None,
1378
+ data: dict[str, object] | None = None,
1379
+ ) -> None:
1380
+ logger.info(f"config_cb {key} {val} {data}")
1381
+ if self._backend and self._backend.interface:
1382
+ self._backend.interface.publish_config(key=key, val=val, data=data)
1383
+
1384
+ @_log_to_run
1385
+ def _config_artifact_callback(
1386
+ self, key: str, val: str | Artifact | dict
1387
+ ) -> Artifact:
1388
+ # artifacts can look like dicts as they are passed into the run config
1389
+ # since the run config stores them on the backend as a dict with fields shown
1390
+ # in wandb.util.artifact_to_json
1391
+ if _is_artifact_version_weave_dict(val):
1392
+ assert isinstance(val, dict)
1393
+ public_api = self._public_api()
1394
+ artifact = Artifact._from_id(val["id"], public_api.client)
1395
+
1396
+ assert artifact
1397
+ return self.use_artifact(artifact)
1398
+ elif _is_artifact_string(val):
1399
+ # this will never fail, but is required to make mypy happy
1400
+ assert isinstance(val, str)
1401
+ artifact_string, base_url, is_id = parse_artifact_string(val)
1402
+ overrides = {}
1403
+ if base_url is not None:
1404
+ overrides = {"base_url": base_url}
1405
+ public_api = public.Api(overrides)
1406
+ else:
1407
+ public_api = self._public_api()
1408
+ if is_id:
1409
+ artifact = Artifact._from_id(artifact_string, public_api._client)
1410
+ else:
1411
+ artifact = public_api._artifact(name=artifact_string)
1412
+ # in the future we'll need to support using artifacts from
1413
+ # different instances of wandb.
1414
+
1415
+ assert artifact
1416
+ return self.use_artifact(artifact)
1417
+ elif _is_artifact_object(val):
1418
+ return self.use_artifact(val)
1419
+ else:
1420
+ raise ValueError(
1421
+ f"Cannot call _config_artifact_callback on type {type(val)}"
1422
+ )
1423
+
1424
+ def _set_config_wandb(self, key: str, val: Any) -> None:
1425
+ self._config_callback(key=("_wandb", key), val=val)
1426
+
1427
+ @_log_to_run
1428
+ @_raise_if_finished
1429
+ def _summary_update_callback(self, summary_record: SummaryRecord) -> None:
1430
+ with telemetry.context(run=self) as tel:
1431
+ tel.feature.set_summary = True
1432
+ if self._backend and self._backend.interface:
1433
+ self._backend.interface.publish_summary(self, summary_record)
1434
+
1435
+ @_log_to_run
1436
+ def _summary_get_current_summary_callback(self) -> dict[str, Any]:
1437
+ if self._is_finished:
1438
+ # TODO: WB-18420: fetch summary from backend and stage it before run is finished
1439
+ wandb.termwarn("Summary data not available in finished run")
1440
+ return {}
1441
+ if not self._backend or not self._backend.interface:
1442
+ return {}
1443
+ handle = self._backend.interface.deliver_get_summary()
1444
+
1445
+ try:
1446
+ result = handle.wait_or(timeout=self._settings.summary_timeout)
1447
+ except TimeoutError:
1448
+ return {}
1449
+
1450
+ get_summary_response = result.response.get_summary_response
1451
+ return proto_util.dict_from_proto_list(get_summary_response.item)
1452
+
1453
+ @_log_to_run
1454
+ def _metric_callback(self, metric_record: MetricRecord) -> None:
1455
+ if self._backend and self._backend.interface:
1456
+ self._backend.interface._publish_metric(metric_record)
1457
+
1458
+ @_log_to_run
1459
+ def _publish_file(self, fname: str) -> None:
1460
+ """Mark a run file to be uploaded with the run.
1461
+
1462
+ This is a W&B-internal function: it can be used by other internal
1463
+ wandb code.
1464
+
1465
+ Args:
1466
+ fname: The path to the file in the run's files directory, relative
1467
+ to the run's files directory.
1468
+ """
1469
+ if not self._backend or not self._backend.interface:
1470
+ return
1471
+ files: FilesDict = dict(files=[(GlobStr(fname), "now")])
1472
+ self._backend.interface.publish_files(files)
1473
+
1474
+ def _pop_all_charts(
1475
+ self,
1476
+ data: dict[str, Any],
1477
+ key_prefix: str | None = None,
1478
+ ) -> dict[str, Any]:
1479
+ """Pops all charts from a dictionary including nested charts.
1480
+
1481
+ This function will return a mapping of the charts and a dot-separated
1482
+ key for each chart. Indicating the path to the chart in the data dictionary.
1483
+ """
1484
+ keys_to_remove = set()
1485
+ charts: dict[str, Any] = {}
1486
+ for k, v in data.items():
1487
+ key = f"{key_prefix}.{k}" if key_prefix else k
1488
+ if isinstance(v, Visualize):
1489
+ keys_to_remove.add(k)
1490
+ charts[key] = v
1491
+ elif isinstance(v, CustomChart):
1492
+ keys_to_remove.add(k)
1493
+ charts[key] = v
1494
+ elif isinstance(v, dict):
1495
+ nested_charts = self._pop_all_charts(v, key)
1496
+ charts.update(nested_charts)
1497
+
1498
+ for k in keys_to_remove:
1499
+ data.pop(k)
1500
+
1501
+ return charts
1502
+
1503
+ def _serialize_custom_charts(
1504
+ self,
1505
+ data: dict[str, Any],
1506
+ ) -> dict[str, Any]:
1507
+ """Process and replace chart objects with their underlying table values.
1508
+
1509
+ This processes the chart objects passed to `wandb.Run.log()`, replacing their entries
1510
+ in the given dictionary (which is saved to the run's history) and adding them
1511
+ to the run's config.
1512
+
1513
+ Args:
1514
+ data: Dictionary containing data that may include plot objects
1515
+ Plot objects can be nested in dictionaries, which will be processed recursively.
1516
+
1517
+ Returns:
1518
+ The processed dictionary with custom charts transformed into tables.
1519
+ """
1520
+ if not data:
1521
+ return data
1522
+
1523
+ charts = self._pop_all_charts(data)
1524
+ for k, v in charts.items():
1525
+ v.set_key(k)
1526
+ self._config_callback(
1527
+ val=v.spec.config_value,
1528
+ key=v.spec.config_key,
1529
+ )
1530
+
1531
+ if isinstance(v, CustomChart):
1532
+ data[v.spec.table_key] = v.table
1533
+ elif isinstance(v, Visualize):
1534
+ data[k] = v.table
1535
+
1536
+ return data
1537
+
1538
+ @_log_to_run
1539
+ def _partial_history_callback(
1540
+ self,
1541
+ data: dict[str, Any],
1542
+ step: int | None = None,
1543
+ commit: bool | None = None,
1544
+ ) -> None:
1545
+ if not (self._backend and self._backend.interface):
1546
+ return
1547
+
1548
+ data = data.copy() # avoid modifying the original data
1549
+
1550
+ # Serialize custom charts before publishing
1551
+ data = self._serialize_custom_charts(data)
1552
+
1553
+ not_using_tensorboard = len(wandb.patched["tensorboard"]) == 0
1554
+ self._backend.interface.publish_partial_history(
1555
+ self,
1556
+ data,
1557
+ user_step=self._step,
1558
+ step=step,
1559
+ flush=commit,
1560
+ publish_step=not_using_tensorboard,
1561
+ )
1562
+
1563
+ @_log_to_run
1564
+ def _console_callback(self, name: str, data: str) -> None:
1565
+ # logger.info("console callback: %s, %s", name, data)
1566
+ if self._backend and self._backend.interface:
1567
+ self._backend.interface.publish_output(name, data)
1568
+
1569
+ @_log_to_run
1570
+ @_raise_if_finished
1571
+ def _console_raw_callback(self, name: str, data: str) -> None:
1572
+ # logger.info("console callback: %s, %s", name, data)
1573
+
1574
+ # NOTE: console output is only allowed on the process which installed the callback
1575
+ # this will prevent potential corruption in the socket to the service. Other methods
1576
+ # are protected by the _attach run decorator, but this callback was installed on the
1577
+ # write function of stdout and stderr streams.
1578
+ console_pid = getattr(self, "_attach_pid", 0)
1579
+ if console_pid != os.getpid():
1580
+ return
1581
+
1582
+ if self._backend and self._backend.interface:
1583
+ self._backend.interface.publish_output_raw(name, data)
1584
+
1585
+ @_log_to_run
1586
+ def _tensorboard_callback(
1587
+ self, logdir: str, save: bool = True, root_logdir: str = ""
1588
+ ) -> None:
1589
+ logger.info("tensorboard callback: %s, %s", logdir, save)
1590
+ if self._backend and self._backend.interface:
1591
+ self._backend.interface.publish_tbdata(logdir, save, root_logdir)
1592
+
1593
+ def _set_library(self, library: _WandbSetup) -> None:
1594
+ self._wl = library
1595
+
1596
+ def _set_backend(self, backend: wandb.sdk.backend.backend.Backend) -> None:
1597
+ self._backend = backend
1598
+
1599
+ def _set_internal_run_interface(
1600
+ self,
1601
+ interface: wandb.sdk.interface.interface_queue.InterfaceQueue,
1602
+ ) -> None:
1603
+ self._internal_run_interface = interface
1604
+
1605
+ def _set_teardown_hooks(self, hooks: list[TeardownHook]) -> None:
1606
+ self._teardown_hooks = hooks
1607
+
1608
+ def _set_run_obj(self, run_obj: RunRecord) -> None: # noqa: C901
1609
+ if run_obj.starting_step:
1610
+ self._starting_step = run_obj.starting_step
1611
+ self._step = run_obj.starting_step
1612
+
1613
+ if run_obj.start_time:
1614
+ self._start_time = run_obj.start_time.ToMicroseconds() / 1e6
1615
+
1616
+ if run_obj.runtime:
1617
+ self._start_runtime = run_obj.runtime
1618
+
1619
+ # Grab the config from resuming
1620
+ if run_obj.config:
1621
+ c_dict = config_util.dict_no_value_from_proto_list(run_obj.config.update)
1622
+ # We update the config object here without triggering the callback
1623
+ self._config._update(c_dict, allow_val_change=True, ignore_locked=True)
1624
+ # Update the summary, this will trigger an un-needed graphql request :(
1625
+ if run_obj.summary:
1626
+ summary_dict = {}
1627
+ for orig in run_obj.summary.update:
1628
+ summary_dict[orig.key] = json.loads(orig.value_json)
1629
+ if summary_dict:
1630
+ self.summary.update(summary_dict)
1631
+
1632
+ # update settings from run_obj
1633
+ if run_obj.run_id:
1634
+ self._settings.run_id = run_obj.run_id
1635
+ if run_obj.entity:
1636
+ self._settings.entity = run_obj.entity
1637
+ if run_obj.project:
1638
+ self._settings.project = run_obj.project
1639
+ if run_obj.run_group:
1640
+ self._settings.run_group = run_obj.run_group
1641
+ if run_obj.job_type:
1642
+ self._settings.run_job_type = run_obj.job_type
1643
+ if run_obj.display_name:
1644
+ self._settings.run_name = run_obj.display_name
1645
+ if run_obj.notes:
1646
+ self._settings.run_notes = run_obj.notes
1647
+ if run_obj.tags:
1648
+ self._settings.run_tags = tuple(run_obj.tags)
1649
+ if run_obj.sweep_id:
1650
+ self._settings.sweep_id = run_obj.sweep_id
1651
+ if run_obj.host:
1652
+ self._settings.host = run_obj.host
1653
+ if run_obj.resumed:
1654
+ self._settings.resumed = run_obj.resumed
1655
+ if run_obj.git:
1656
+ if run_obj.git.remote_url:
1657
+ self._settings.git_remote_url = run_obj.git.remote_url
1658
+ if run_obj.git.commit:
1659
+ self._settings.git_commit = run_obj.git.commit
1660
+
1661
+ if run_obj.forked:
1662
+ self._forked = run_obj.forked
1663
+
1664
+ wandb._sentry.configure_scope(
1665
+ process_context="user",
1666
+ tags=dict(self._settings),
1667
+ )
1668
+
1669
+ def _populate_git_info(self) -> None:
1670
+ from .lib.gitlib import GitRepo
1671
+
1672
+ # Use user-provided git info if available, otherwise resolve it from the environment
1673
+ try:
1674
+ repo = GitRepo(
1675
+ root=self._settings.git_root,
1676
+ remote=self._settings.git_remote,
1677
+ remote_url=self._settings.git_remote_url,
1678
+ commit=self._settings.git_commit,
1679
+ lazy=False,
1680
+ )
1681
+ self._settings.git_remote_url = repo.remote_url
1682
+ self._settings.git_commit = repo.last_commit
1683
+ except Exception:
1684
+ wandb.termwarn("Cannot find valid git repo associated with this directory.")
1685
+
1686
+ def _add_singleton(
1687
+ self, data_type: str, key: str, value: dict[int | str, str]
1688
+ ) -> None:
1689
+ """Store a singleton item to wandb config.
1690
+
1691
+ A singleton in this context is a piece of data that is continually
1692
+ logged with the same value in each history step, but represented
1693
+ as a single item in the config.
1694
+
1695
+ We do this to avoid filling up history with a lot of repeated unnecessary data
1696
+
1697
+ Add singleton can be called many times in one run, and it will only be
1698
+ updated when the value changes. The last value logged will be the one
1699
+ persisted to the server.
1700
+ """
1701
+ value_extra = {"type": data_type, "key": key, "value": value}
1702
+
1703
+ if data_type not in self._config["_wandb"]:
1704
+ self._config["_wandb"][data_type] = {}
1705
+
1706
+ if data_type in self._config["_wandb"][data_type]:
1707
+ old_value = self._config["_wandb"][data_type][key]
1708
+ else:
1709
+ old_value = None
1710
+
1711
+ if value_extra != old_value:
1712
+ self._config["_wandb"][data_type][key] = value_extra
1713
+ self._config.persist()
1714
+
1715
+ def _log(
1716
+ self,
1717
+ data: dict[str, Any],
1718
+ step: int | None = None,
1719
+ commit: bool | None = None,
1720
+ ) -> None:
1721
+ if not isinstance(data, Mapping):
1722
+ raise TypeError("wandb.log must be passed a dictionary")
1723
+
1724
+ if any(not isinstance(key, str) for key in data.keys()):
1725
+ raise TypeError("Key values passed to `wandb.log` must be strings.")
1726
+
1727
+ self._partial_history_callback(data, step, commit)
1728
+
1729
+ if step is not None:
1730
+ if os.getpid() != self._init_pid or self._is_attached:
1731
+ wandb.termwarn(
1732
+ "Note that setting step in multiprocessing can result in data loss. "
1733
+ "Please use `run.define_metric(...)` to define a custom metric "
1734
+ "to log your step values.",
1735
+ repeat=False,
1736
+ )
1737
+ # if step is passed in when tensorboard_sync is used we honor the step passed
1738
+ # to make decisions about how to close out the history record, but will strip
1739
+ # this history later on in publish_history()
1740
+ if len(wandb.patched["tensorboard"]) > 0:
1741
+ wandb.termwarn(
1742
+ "Step cannot be set when using tensorboard syncing. "
1743
+ "Please use `run.define_metric(...)` to define a custom metric "
1744
+ "to log your step values.",
1745
+ repeat=False,
1746
+ )
1747
+ if step > self._step:
1748
+ self._step = step
1749
+
1750
+ if (step is None and commit is None) or commit:
1751
+ self._step += 1
1752
+
1753
+ @_log_to_run
1754
+ @_raise_if_finished
1755
+ @_attach
1756
+ def log(
1757
+ self,
1758
+ data: dict[str, Any],
1759
+ step: int | None = None,
1760
+ commit: bool | None = None,
1761
+ ) -> None:
1762
+ """Upload run data.
1763
+
1764
+ Use `log` to log data from runs, such as scalars, images, video,
1765
+ histograms, plots, and tables. See [Log objects and media](https://docs.wandb.ai/guides/track/log) for
1766
+ code snippets, best practices, and more.
1767
+
1768
+ Basic usage:
1769
+
1770
+ ```python
1771
+ import wandb
1772
+
1773
+ with wandb.init() as run:
1774
+ run.log({"train-loss": 0.5, "accuracy": 0.9})
1775
+ ```
1776
+
1777
+ The previous code snippet saves the loss and accuracy to the run's
1778
+ history and updates the summary values for these metrics.
1779
+
1780
+ Visualize logged data in a workspace at [wandb.ai](https://wandb.ai),
1781
+ or locally on a [self-hosted instance](https://docs.wandb.ai/guides/hosting)
1782
+ of the W&B app, or export data to visualize and explore locally, such as in a
1783
+ Jupyter notebook, with the [Public API](https://docs.wandb.ai/guides/track/public-api-guide).
1784
+
1785
+ Logged values don't have to be scalars. You can log any
1786
+ [W&B supported Data Type](https://docs.wandb.ai/ref/python/data-types/)
1787
+ such as images, audio, video, and more. For example, you can use
1788
+ `wandb.Table` to log structured data. See
1789
+ [Log tables, visualize and query data](https://docs.wandb.ai/guides/models/tables/tables-walkthrough)
1790
+ tutorial for more details.
1791
+
1792
+ W&B organizes metrics with a forward slash (`/`) in their name
1793
+ into sections named using the text before the final slash. For example,
1794
+ the following results in two sections named "train" and "validate":
1795
+
1796
+ ```python
1797
+ with wandb.init() as run:
1798
+ # Log metrics in the "train" section.
1799
+ run.log(
1800
+ {
1801
+ "train/accuracy": 0.9,
1802
+ "train/loss": 30,
1803
+ "validate/accuracy": 0.8,
1804
+ "validate/loss": 20,
1805
+ }
1806
+ )
1807
+ ```
1808
+
1809
+ Only one level of nesting is supported; `run.log({"a/b/c": 1})`
1810
+ produces a section named "a/b".
1811
+
1812
+ `run.log()` is not intended to be called more than a few times per second.
1813
+ For optimal performance, limit your logging to once every N iterations,
1814
+ or collect data over multiple iterations and log it in a single step.
1815
+
1816
+ By default, each call to `log` creates a new "step".
1817
+ The step must always increase, and it is not possible to log
1818
+ to a previous step. You can use any metric as the X axis in charts.
1819
+ See [Custom log axes](https://docs.wandb.ai/guides/track/log/customize-logging-axes/)
1820
+ for more details.
1821
+
1822
+ In many cases, it is better to treat the W&B step like
1823
+ you'd treat a timestamp rather than a training step.
1824
+
1825
+ ```python
1826
+ with wandb.init() as run:
1827
+ # Example: log an "epoch" metric for use as an X axis.
1828
+ run.log({"epoch": 40, "train-loss": 0.5})
1829
+ ```
1830
+
1831
+ It is possible to use multiple `wandb.Run.log()` invocations to log to
1832
+ the same step with the `step` and `commit` parameters.
1833
+ The following are all equivalent:
1834
+
1835
+ ```python
1836
+ with wandb.init() as run:
1837
+ # Normal usage:
1838
+ run.log({"train-loss": 0.5, "accuracy": 0.8})
1839
+ run.log({"train-loss": 0.4, "accuracy": 0.9})
1840
+
1841
+ # Implicit step without auto-incrementing:
1842
+ run.log({"train-loss": 0.5}, commit=False)
1843
+ run.log({"accuracy": 0.8})
1844
+ run.log({"train-loss": 0.4}, commit=False)
1845
+ run.log({"accuracy": 0.9})
1846
+
1847
+ # Explicit step:
1848
+ run.log({"train-loss": 0.5}, step=current_step)
1849
+ run.log({"accuracy": 0.8}, step=current_step)
1850
+ current_step += 1
1851
+ run.log({"train-loss": 0.4}, step=current_step)
1852
+ run.log({"accuracy": 0.9}, step=current_step)
1853
+ ```
1854
+
1855
+ Args:
1856
+ data: A `dict` with `str` keys and values that are serializable
1857
+ Python objects including: `int`, `float` and `string`;
1858
+ any of the `wandb.data_types`; lists, tuples and NumPy arrays
1859
+ of serializable Python objects; other `dict`s of this
1860
+ structure.
1861
+ step: The step number to log. If `None`, then an implicit
1862
+ auto-incrementing step is used. See the notes in
1863
+ the description.
1864
+ commit: If true, finalize and upload the step. If false, then
1865
+ accumulate data for the step. See the notes in the description.
1866
+ If `step` is `None`, then the default is `commit=True`;
1867
+ otherwise, the default is `commit=False`.
1868
+
1869
+ Examples:
1870
+ For more and more detailed examples, see
1871
+ [our guides to logging](https://docs.wandb.com/guides/track/log).
1872
+
1873
+ Basic usage
1874
+
1875
+ ```python
1876
+ import wandb
1877
+
1878
+ with wandb.init() as run:
1879
+ run.log({"train-loss": 0.5, "accuracy": 0.9
1880
+ ```
1881
+
1882
+ Incremental logging
1883
+
1884
+ ```python
1885
+ import wandb
1886
+
1887
+ with wandb.init() as run:
1888
+ run.log({"loss": 0.2}, commit=False)
1889
+ # Somewhere else when I'm ready to report this step:
1890
+ run.log({"accuracy": 0.8})
1891
+ ```
1892
+
1893
+ Histogram
1894
+
1895
+ ```python
1896
+ import numpy as np
1897
+ import wandb
1898
+
1899
+ # sample gradients at random from normal distribution
1900
+ gradients = np.random.randn(100, 100)
1901
+ with wandb.init() as run:
1902
+ run.log({"gradients": wandb.Histogram(gradients)})
1903
+ ```
1904
+
1905
+ Image from NumPy
1906
+
1907
+ ```python
1908
+ import numpy as np
1909
+ import wandb
1910
+
1911
+ with wandb.init() as run:
1912
+ examples = []
1913
+ for i in range(3):
1914
+ pixels = np.random.randint(low=0, high=256, size=(100, 100, 3))
1915
+ image = wandb.Image(pixels, caption=f"random field {i}")
1916
+ examples.append(image)
1917
+ run.log({"examples": examples})
1918
+ ```
1919
+
1920
+ Image from PIL
1921
+
1922
+ ```python
1923
+ import numpy as np
1924
+ from PIL import Image as PILImage
1925
+ import wandb
1926
+
1927
+ with wandb.init() as run:
1928
+ examples = []
1929
+ for i in range(3):
1930
+ pixels = np.random.randint(
1931
+ low=0,
1932
+ high=256,
1933
+ size=(100, 100, 3),
1934
+ dtype=np.uint8,
1935
+ )
1936
+ pil_image = PILImage.fromarray(pixels, mode="RGB")
1937
+ image = wandb.Image(pil_image, caption=f"random field {i}")
1938
+ examples.append(image)
1939
+ run.log({"examples": examples})
1940
+ ```
1941
+
1942
+ Video from NumPy
1943
+
1944
+ ```python
1945
+ import numpy as np
1946
+ import wandb
1947
+
1948
+ with wandb.init() as run:
1949
+ # axes are (time, channel, height, width)
1950
+ frames = np.random.randint(
1951
+ low=0,
1952
+ high=256,
1953
+ size=(10, 3, 100, 100),
1954
+ dtype=np.uint8,
1955
+ )
1956
+ run.log({"video": wandb.Video(frames, fps=4)})
1957
+ ```
1958
+
1959
+ Matplotlib plot
1960
+
1961
+ ```python
1962
+ from matplotlib import pyplot as plt
1963
+ import numpy as np
1964
+ import wandb
1965
+
1966
+ with wandb.init() as run:
1967
+ fig, ax = plt.subplots()
1968
+ x = np.linspace(0, 10)
1969
+ y = x * x
1970
+ ax.plot(x, y) # plot y = x^2
1971
+ run.log({"chart": fig})
1972
+ ```
1973
+
1974
+ PR Curve
1975
+
1976
+ ```python
1977
+ import wandb
1978
+
1979
+ with wandb.init() as run:
1980
+ run.log({"pr": wandb.plot.pr_curve(y_test, y_probas, labels)})
1981
+ ```
1982
+
1983
+ 3D Object
1984
+
1985
+ ```python
1986
+ import wandb
1987
+
1988
+ with wandb.init() as run:
1989
+ run.log(
1990
+ {
1991
+ "generated_samples": [
1992
+ wandb.Object3D(open("sample.obj")),
1993
+ wandb.Object3D(open("sample.gltf")),
1994
+ wandb.Object3D(open("sample.glb")),
1995
+ ]
1996
+ }
1997
+ )
1998
+ ```
1999
+
2000
+ Raises:
2001
+ wandb.Error: If called before `wandb.init()`.
2002
+ ValueError: If invalid data is passed.
2003
+
2004
+ """
2005
+ if step is not None:
2006
+ with telemetry.context(run=self) as tel:
2007
+ tel.feature.set_step_log = True
2008
+
2009
+ if self._settings._shared and step is not None:
2010
+ wandb.termwarn(
2011
+ "In shared mode, the use of `wandb.log` with the step argument is not supported "
2012
+ f"and will be ignored. Please refer to {url_registry.url('define-metric')} "
2013
+ "on how to customize your x-axis.",
2014
+ repeat=False,
2015
+ )
2016
+ self._log(data=data, step=step, commit=commit)
2017
+
2018
+ @_log_to_run
2019
+ @_raise_if_finished
2020
+ @_attach
2021
+ def save(
2022
+ self,
2023
+ glob_str: str | os.PathLike,
2024
+ base_path: str | os.PathLike | None = None,
2025
+ policy: PolicyName = "live",
2026
+ ) -> bool | list[str]:
2027
+ """Sync one or more files to W&B.
2028
+
2029
+ Relative paths are relative to the current working directory.
2030
+
2031
+ A Unix glob, such as "myfiles/*", is expanded at the time `save` is
2032
+ called regardless of the `policy`. In particular, new files are not
2033
+ picked up automatically.
2034
+
2035
+ A `base_path` may be provided to control the directory structure of
2036
+ uploaded files. It should be a prefix of `glob_str`, and the directory
2037
+ structure beneath it is preserved.
2038
+
2039
+ When given an absolute path or glob and no `base_path`, one
2040
+ directory level is preserved as in the example above.
2041
+
2042
+ Args:
2043
+ glob_str: A relative or absolute path or Unix glob.
2044
+ base_path: A path to use to infer a directory structure; see examples.
2045
+ policy: One of `live`, `now`, or `end`.
2046
+ - live: upload the file as it changes, overwriting the previous version
2047
+ - now: upload the file once now
2048
+ - end: upload file when the run ends
2049
+
2050
+ Returns:
2051
+ Paths to the symlinks created for the matched files.
2052
+
2053
+ For historical reasons, this may return a boolean in legacy code.
2054
+
2055
+ ```python
2056
+ import wandb
2057
+
2058
+ run = wandb.init()
2059
+
2060
+ run.save("these/are/myfiles/*")
2061
+ # => Saves files in a "these/are/myfiles/" folder in the run.
2062
+
2063
+ run.save("these/are/myfiles/*", base_path="these")
2064
+ # => Saves files in an "are/myfiles/" folder in the run.
2065
+
2066
+ run.save("/User/username/Documents/run123/*.txt")
2067
+ # => Saves files in a "run123/" folder in the run. See note below.
2068
+
2069
+ run.save("/User/username/Documents/run123/*.txt", base_path="/User")
2070
+ # => Saves files in a "username/Documents/run123/" folder in the run.
2071
+
2072
+ run.save("files/*/saveme.txt")
2073
+ # => Saves each "saveme.txt" file in an appropriate subdirectory
2074
+ # of "files/".
2075
+
2076
+ # Explicitly finish the run since a context manager is not used.
2077
+ run.finish()
2078
+ ```
2079
+ """
2080
+ if isinstance(glob_str, bytes):
2081
+ # Preserved for backward compatibility: allow bytes inputs.
2082
+ glob_str = glob_str.decode("utf-8")
2083
+ if isinstance(glob_str, str) and (glob_str.startswith(("gs://", "s3://"))):
2084
+ # Provide a better error message for a common misuse.
2085
+ wandb.termlog(f"{glob_str} is a cloud storage url, can't save file to W&B.")
2086
+ return []
2087
+ # NOTE: We use PurePath instead of Path because WindowsPath doesn't
2088
+ # like asterisks and errors out in resolve(). It also makes logical
2089
+ # sense: globs aren't real paths, they're just path-like strings.
2090
+ glob_path = pathlib.PurePath(glob_str)
2091
+ resolved_glob_path = pathlib.PurePath(os.path.abspath(glob_path))
2092
+
2093
+ if base_path is not None:
2094
+ base_path = pathlib.Path(base_path)
2095
+ elif not glob_path.is_absolute():
2096
+ base_path = pathlib.Path(".")
2097
+ else:
2098
+ # Absolute glob paths with no base path get special handling.
2099
+ wandb.termwarn(
2100
+ "Saving files without folders. If you want to preserve "
2101
+ "subdirectories pass base_path to wandb.save, i.e. "
2102
+ 'wandb.save("/mnt/folder/file.h5", base_path="/mnt")',
2103
+ repeat=False,
2104
+ )
2105
+ base_path = resolved_glob_path.parent.parent
2106
+
2107
+ if policy not in ("live", "end", "now"):
2108
+ raise ValueError(
2109
+ 'Only "live", "end" and "now" policies are currently supported.'
2110
+ )
2111
+
2112
+ resolved_base_path = pathlib.PurePath(os.path.abspath(base_path))
2113
+
2114
+ return self._save(
2115
+ resolved_glob_path,
2116
+ resolved_base_path,
2117
+ policy,
2118
+ )
2119
+
2120
+ def _save(
2121
+ self,
2122
+ glob_path: pathlib.PurePath,
2123
+ base_path: pathlib.PurePath,
2124
+ policy: PolicyName,
2125
+ ) -> list[str]:
2126
+ # Can't use is_relative_to() because that's added in Python 3.9,
2127
+ # but we support down to Python 3.8.
2128
+ if not str(glob_path).startswith(str(base_path)):
2129
+ raise ValueError("Glob may not walk above the base path")
2130
+
2131
+ if glob_path == base_path:
2132
+ raise ValueError("Glob cannot be the same as the base path")
2133
+
2134
+ relative_glob = glob_path.relative_to(base_path)
2135
+ if relative_glob.parts[0] == "*":
2136
+ raise ValueError("Glob may not start with '*' relative to the base path")
2137
+ relative_glob_str = GlobStr(str(relative_glob))
2138
+
2139
+ with telemetry.context(run=self) as tel:
2140
+ tel.feature.save = True
2141
+
2142
+ # Files in the files directory matched by the glob, including old and
2143
+ # new ones.
2144
+ globbed_files = set(
2145
+ pathlib.Path(
2146
+ self._settings.files_dir,
2147
+ ).glob(relative_glob_str)
2148
+ )
2149
+
2150
+ had_symlinked_files = len(globbed_files) > 0
2151
+ is_star_glob = "*" in relative_glob_str
2152
+
2153
+ # The base_path may itself be a glob, so we can't do
2154
+ # base_path.glob(relative_glob_str)
2155
+ for path_str in glob.glob(str(base_path / relative_glob_str)):
2156
+ source_path = pathlib.Path(path_str).absolute()
2157
+
2158
+ # We can't use relative_to() because base_path may be a glob.
2159
+ relative_path = pathlib.Path(*source_path.parts[len(base_path.parts) :])
2160
+
2161
+ target_path = pathlib.Path(self._settings.files_dir, relative_path)
2162
+ globbed_files.add(target_path)
2163
+
2164
+ # If the file is already where it needs to be, don't create a symlink.
2165
+ if source_path.resolve() == target_path.resolve():
2166
+ continue
2167
+
2168
+ target_path.parent.mkdir(parents=True, exist_ok=True)
2169
+
2170
+ # Delete the symlink if it exists.
2171
+ target_path.unlink(missing_ok=True)
2172
+
2173
+ target_path.symlink_to(source_path)
2174
+
2175
+ # Inform users that new files aren't detected automatically.
2176
+ if not had_symlinked_files and is_star_glob:
2177
+ file_str = f"{len(globbed_files)} file"
2178
+ if len(globbed_files) > 1:
2179
+ file_str += "s"
2180
+ wandb.termwarn(
2181
+ f"Symlinked {file_str} into the W&B run directory, "
2182
+ "call wandb.save again to sync new files."
2183
+ )
2184
+
2185
+ files_dict: FilesDict = {
2186
+ "files": [
2187
+ (
2188
+ GlobStr(str(f.relative_to(self._settings.files_dir))),
2189
+ policy,
2190
+ )
2191
+ for f in globbed_files
2192
+ ]
2193
+ }
2194
+ if self._backend and self._backend.interface:
2195
+ self._backend.interface.publish_files(files_dict)
2196
+
2197
+ return [str(f) for f in globbed_files]
2198
+
2199
+ @_log_to_run
2200
+ @_attach
2201
+ def restore(
2202
+ self,
2203
+ name: str,
2204
+ run_path: str | None = None,
2205
+ replace: bool = False,
2206
+ root: str | None = None,
2207
+ ) -> None | TextIO:
2208
+ return restore(
2209
+ name,
2210
+ run_path or self._get_path(),
2211
+ replace,
2212
+ root or self._settings.files_dir,
2213
+ )
2214
+
2215
+ @_log_to_run
2216
+ @_attach
2217
+ def finish(
2218
+ self,
2219
+ exit_code: int | None = None,
2220
+ quiet: bool | None = None,
2221
+ ) -> None:
2222
+ """Finish a run and upload any remaining data.
2223
+
2224
+ Marks the completion of a W&B run and ensures all data is synced to the server.
2225
+ The run's final state is determined by its exit conditions and sync status.
2226
+
2227
+ Run States:
2228
+ - Running: Active run that is logging data and/or sending heartbeats.
2229
+ - Crashed: Run that stopped sending heartbeats unexpectedly.
2230
+ - Finished: Run completed successfully (`exit_code=0`) with all data synced.
2231
+ - Failed: Run completed with errors (`exit_code!=0`).
2232
+ - Killed: Run was forcibly stopped before it could finish.
2233
+
2234
+ Args:
2235
+ exit_code: Integer indicating the run's exit status. Use 0 for success,
2236
+ any other value marks the run as failed.
2237
+ quiet: Deprecated. Configure logging verbosity using `wandb.Settings(quiet=...)`.
2238
+ """
2239
+ if quiet is not None:
2240
+ deprecate.deprecate(
2241
+ field_name=Deprecated.run__finish_quiet,
2242
+ warning_message=(
2243
+ "The `quiet` argument to `wandb.run.finish()` is deprecated, "
2244
+ "use `wandb.Settings(quiet=...)` to set this instead."
2245
+ ),
2246
+ run=self,
2247
+ )
2248
+ return self._finish(exit_code)
2249
+
2250
+ @_log_to_run
2251
+ def _finish(
2252
+ self,
2253
+ exit_code: int | None = None,
2254
+ ) -> None:
2255
+ if self._is_finished:
2256
+ return
2257
+
2258
+ assert self._wl
2259
+
2260
+ logger.info(f"finishing run {self._get_path()}")
2261
+ with telemetry.context(run=self) as tel:
2262
+ tel.feature.finish = True
2263
+
2264
+ # Run hooks that need to happen before the last messages to the
2265
+ # internal service, like Jupyter hooks.
2266
+ for hook in self._teardown_hooks:
2267
+ if hook.stage == TeardownStage.EARLY:
2268
+ hook.call()
2269
+
2270
+ # Early-stage hooks may use methods that require _is_finished
2271
+ # to be False, so we set this after running those hooks.
2272
+ self._is_finished = True
2273
+ self._wl.remove_active_run(self)
2274
+
2275
+ try:
2276
+ self._atexit_cleanup(exit_code=exit_code)
2277
+
2278
+ # Run hooks that should happen after the last messages to the
2279
+ # internal service, like detaching the logger.
2280
+ for hook in self._teardown_hooks:
2281
+ if hook.stage == TeardownStage.LATE:
2282
+ hook.call()
2283
+ self._teardown_hooks = []
2284
+
2285
+ # Inform the service that we're done sending messages for this run.
2286
+ #
2287
+ # TODO: Why not do this in _atexit_cleanup()?
2288
+ if self._settings.run_id:
2289
+ service = self._wl.assert_service()
2290
+ service.inform_finish(run_id=self._settings.run_id)
2291
+
2292
+ finally:
2293
+ if wandb.run is self:
2294
+ module.unset_globals()
2295
+ wandb._sentry.end_session()
2296
+
2297
+ @_log_to_run
2298
+ @_raise_if_finished
2299
+ @_attach
2300
+ def status(
2301
+ self,
2302
+ ) -> RunStatus:
2303
+ """Get sync info from the internal backend, about the current run's sync status."""
2304
+ if not self._backend or not self._backend.interface:
2305
+ return RunStatus()
2306
+
2307
+ handle_run_status = self._backend.interface.deliver_request_run_status()
2308
+ result = handle_run_status.wait_or(timeout=None)
2309
+ sync_data = result.response.run_status_response
2310
+
2311
+ sync_time = None
2312
+ if sync_data.sync_time.seconds:
2313
+ sync_time = datetime.fromtimestamp(
2314
+ sync_data.sync_time.seconds + sync_data.sync_time.nanos / 1e9
2315
+ )
2316
+ return RunStatus(
2317
+ sync_items_total=sync_data.sync_items_total,
2318
+ sync_items_pending=sync_data.sync_items_pending,
2319
+ sync_time=sync_time,
2320
+ )
2321
+
2322
+ def _add_panel(
2323
+ self, visualize_key: str, panel_type: str, panel_config: dict
2324
+ ) -> None:
2325
+ config = {
2326
+ "panel_type": panel_type,
2327
+ "panel_config": panel_config,
2328
+ }
2329
+ self._config_callback(val=config, key=("_wandb", "visualize", visualize_key))
2330
+
2331
+ def _redirect(
2332
+ self,
2333
+ stdout_slave_fd: int | None,
2334
+ stderr_slave_fd: int | None,
2335
+ console: str | None = None,
2336
+ ) -> None:
2337
+ if console is None:
2338
+ console = self._settings.console
2339
+ # only use raw for service to minimize potential changes
2340
+ if console == "wrap":
2341
+ console = "wrap_raw"
2342
+ logger.info("redirect: %s", console)
2343
+
2344
+ out_redir: redirect.RedirectBase
2345
+ err_redir: redirect.RedirectBase
2346
+
2347
+ # raw output handles the output_log writing in the internal process
2348
+ if console in {"redirect", "wrap_emu"}:
2349
+ output_log_path = os.path.join(
2350
+ self._settings.files_dir, filenames.OUTPUT_FNAME
2351
+ )
2352
+ # output writer might have been set up, see wrap_fallback case
2353
+ if not self._output_writer:
2354
+ self._output_writer = filesystem.CRDedupedFile(
2355
+ open(output_log_path, "wb")
2356
+ )
2357
+
2358
+ if console == "redirect":
2359
+ logger.info("Redirecting console.")
2360
+ out_redir = redirect.Redirect(
2361
+ src="stdout",
2362
+ cbs=[
2363
+ lambda data: self._console_callback("stdout", data),
2364
+ self._output_writer.write, # type: ignore
2365
+ ],
2366
+ flush_periodically=(self._settings.mode == "online"),
2367
+ )
2368
+ err_redir = redirect.Redirect(
2369
+ src="stderr",
2370
+ cbs=[
2371
+ lambda data: self._console_callback("stderr", data),
2372
+ self._output_writer.write, # type: ignore
2373
+ ],
2374
+ flush_periodically=(self._settings.mode == "online"),
2375
+ )
2376
+ if os.name == "nt":
2377
+
2378
+ def wrap_fallback() -> None:
2379
+ if self._out_redir:
2380
+ self._out_redir.uninstall()
2381
+ if self._err_redir:
2382
+ self._err_redir.uninstall()
2383
+ msg = (
2384
+ "Tensorflow detected. Stream redirection is not supported "
2385
+ "on Windows when tensorflow is imported. Falling back to "
2386
+ "wrapping stdout/err."
2387
+ )
2388
+ wandb.termlog(msg)
2389
+ self._redirect(None, None, console="wrap")
2390
+
2391
+ add_import_hook("tensorflow", wrap_fallback)
2392
+ elif console == "wrap_emu":
2393
+ logger.info("Wrapping output streams.")
2394
+ out_redir = redirect.StreamWrapper(
2395
+ src="stdout",
2396
+ cbs=[
2397
+ lambda data: self._console_callback("stdout", data),
2398
+ self._output_writer.write, # type: ignore
2399
+ ],
2400
+ flush_periodically=(self._settings.mode == "online"),
2401
+ )
2402
+ err_redir = redirect.StreamWrapper(
2403
+ src="stderr",
2404
+ cbs=[
2405
+ lambda data: self._console_callback("stderr", data),
2406
+ self._output_writer.write, # type: ignore
2407
+ ],
2408
+ flush_periodically=(self._settings.mode == "online"),
2409
+ )
2410
+ elif console == "wrap_raw":
2411
+ logger.info("Wrapping output streams.")
2412
+ out_redir = redirect.StreamRawWrapper(
2413
+ src="stdout",
2414
+ cbs=[
2415
+ lambda data: self._console_raw_callback("stdout", data),
2416
+ ],
2417
+ )
2418
+ err_redir = redirect.StreamRawWrapper(
2419
+ src="stderr",
2420
+ cbs=[
2421
+ lambda data: self._console_raw_callback("stderr", data),
2422
+ ],
2423
+ )
2424
+ elif console == "off":
2425
+ return
2426
+ else:
2427
+ raise ValueError("unhandled console")
2428
+ try:
2429
+ # save stdout and stderr before installing new write functions
2430
+ out_redir.install()
2431
+ err_redir.install()
2432
+ self._out_redir = out_redir
2433
+ self._err_redir = err_redir
2434
+ logger.info("Redirects installed.")
2435
+ except Exception as e:
2436
+ wandb.termwarn(f"Failed to redirect: {e}")
2437
+ logger.exception("Failed to redirect.")
2438
+ return
2439
+
2440
+ def _restore(self) -> None:
2441
+ logger.info("restore")
2442
+ # TODO(jhr): drain and shutdown all threads
2443
+ if self._out_redir:
2444
+ self._out_redir.uninstall()
2445
+ if self._err_redir:
2446
+ self._err_redir.uninstall()
2447
+ logger.info("restore done")
2448
+
2449
+ def _atexit_cleanup(self, exit_code: int | None = None) -> None:
2450
+ if self._backend is None:
2451
+ logger.warning("process exited without backend configured")
2452
+ return
2453
+ if self._atexit_cleanup_called:
2454
+ return
2455
+ self._atexit_cleanup_called = True
2456
+
2457
+ exit_code = exit_code or (self._hooks and self._hooks.exit_code) or 0
2458
+ self._exit_code = exit_code
2459
+ logger.info(f"got exitcode: {exit_code}")
2460
+
2461
+ # Delete this run's "resume" file if the run finished successfully.
2462
+ #
2463
+ # This is used by the "auto" resume mode, which resumes from the last
2464
+ # failed (or unfinished/crashed) run. If we reach this line, then this
2465
+ # run shouldn't be a candidate for "auto" resume.
2466
+ if exit_code == 0:
2467
+ if os.path.exists(self._settings.resume_fname):
2468
+ os.remove(self._settings.resume_fname)
2469
+
2470
+ try:
2471
+ self._on_finish()
2472
+
2473
+ except KeyboardInterrupt:
2474
+ if not wandb.wandb_agent._is_running(): # type: ignore
2475
+ wandb.termerror("Control-C detected -- Run data was not synced")
2476
+ raise
2477
+
2478
+ except Exception:
2479
+ self._console_stop()
2480
+ logger.exception("Problem finishing run")
2481
+ wandb.termerror("Problem finishing run")
2482
+ raise
2483
+
2484
+ Run._footer(
2485
+ sampled_history=self._sampled_history,
2486
+ final_summary=self._final_summary,
2487
+ poll_exit_response=self._poll_exit_response,
2488
+ internal_messages_response=self._internal_messages_response,
2489
+ settings=self._settings,
2490
+ printer=self._printer,
2491
+ )
2492
+
2493
+ def _console_start(self) -> None:
2494
+ logger.info("atexit reg")
2495
+ self._hooks = ExitHooks()
2496
+
2497
+ self._redirect(self._stdout_slave_fd, self._stderr_slave_fd)
2498
+
2499
+ def _console_stop(self) -> None:
2500
+ self._restore()
2501
+ if self._output_writer:
2502
+ self._output_writer.close()
2503
+ self._output_writer = None
2504
+
2505
+ def _on_start(self) -> None:
2506
+ self._header()
2507
+
2508
+ if self._settings.save_code and self._settings.code_dir is not None:
2509
+ self.log_code(self._settings.code_dir)
2510
+
2511
+ if self._settings.x_save_requirements:
2512
+ if self._backend and self._backend.interface:
2513
+ from wandb.util import working_set
2514
+
2515
+ logger.debug(
2516
+ "Saving list of pip packages installed into the current environment"
2517
+ )
2518
+ self._backend.interface.publish_python_packages(working_set())
2519
+
2520
+ if self._backend and self._backend.interface and not self._settings._offline:
2521
+ assert self._settings.run_id
2522
+ self._run_status_checker = RunStatusChecker(
2523
+ self._settings.run_id,
2524
+ interface=self._backend.interface,
2525
+ settings=self._settings,
2526
+ )
2527
+ self._run_status_checker.start()
2528
+
2529
+ self._console_start()
2530
+ self._on_ready()
2531
+
2532
+ def _on_attach(self) -> None:
2533
+ """Event triggered when run is attached to another run."""
2534
+ with telemetry.context(run=self) as tel:
2535
+ tel.feature.attach = True
2536
+
2537
+ self._is_attached = True
2538
+ self._on_ready()
2539
+
2540
+ def _register_telemetry_import_hooks(
2541
+ self,
2542
+ ) -> None:
2543
+ def _telemetry_import_hook(
2544
+ run: Run,
2545
+ module: Any,
2546
+ ) -> None:
2547
+ with telemetry.context(run=run) as tel:
2548
+ try:
2549
+ name = getattr(module, "__name__", None)
2550
+ if name is not None:
2551
+ setattr(tel.imports_finish, name, True)
2552
+ except AttributeError:
2553
+ return
2554
+
2555
+ import_telemetry_set = telemetry.list_telemetry_imports()
2556
+ import_hook_fn = functools.partial(_telemetry_import_hook, self)
2557
+ if not self._settings.run_id:
2558
+ return
2559
+ for module_name in import_telemetry_set:
2560
+ register_post_import_hook(
2561
+ import_hook_fn,
2562
+ self._settings.run_id,
2563
+ module_name,
2564
+ )
2565
+
2566
+ def _on_ready(self) -> None:
2567
+ """Event triggered when run is ready for the user."""
2568
+ assert self._wl
2569
+ self._wl.add_active_run(self)
2570
+
2571
+ self._register_telemetry_import_hooks()
2572
+
2573
+ # start reporting any telemetry changes
2574
+ self._telemetry_obj_active = True
2575
+ self._telemetry_flush()
2576
+
2577
+ try:
2578
+ self._detect_and_apply_job_inputs()
2579
+ except Exception:
2580
+ logger.exception("Problem applying launch job inputs")
2581
+
2582
+ # object is about to be returned to the user, don't let them modify it
2583
+ self._freeze()
2584
+
2585
+ if not self._settings.resume:
2586
+ if os.path.exists(self._settings.resume_fname):
2587
+ os.remove(self._settings.resume_fname)
2588
+
2589
+ def _detect_and_apply_job_inputs(self) -> None:
2590
+ """If the user has staged launch inputs, apply them to the run."""
2591
+ from wandb.sdk.launch.inputs.internal import StagedLaunchInputs
2592
+
2593
+ StagedLaunchInputs().apply(self)
2594
+
2595
+ def _make_job_source_reqs(self) -> tuple[list[str], dict[str, Any], dict[str, Any]]:
2596
+ from wandb.util import working_set
2597
+
2598
+ installed_packages_list = sorted(f"{d.key}=={d.version}" for d in working_set())
2599
+ input_types = TypeRegistry.type_of(self.config.as_dict()).to_json()
2600
+ output_types = TypeRegistry.type_of(self.summary._as_dict()).to_json()
2601
+
2602
+ return installed_packages_list, input_types, output_types
2603
+
2604
+ def _construct_job_artifact(
2605
+ self,
2606
+ name: str,
2607
+ source_dict: JobSourceDict,
2608
+ installed_packages_list: list[str],
2609
+ patch_path: os.PathLike | None = None,
2610
+ ) -> Artifact:
2611
+ job_artifact = InternalArtifact(name, job_builder.JOB_ARTIFACT_TYPE)
2612
+ if patch_path and os.path.exists(patch_path):
2613
+ job_artifact.add_file(FilePathStr(str(patch_path)), "diff.patch")
2614
+ with job_artifact.new_file("requirements.frozen.txt") as f:
2615
+ f.write("\n".join(installed_packages_list))
2616
+ with job_artifact.new_file("wandb-job.json") as f:
2617
+ f.write(json.dumps(source_dict))
2618
+
2619
+ return job_artifact
2620
+
2621
+ def _create_image_job(
2622
+ self,
2623
+ input_types: dict[str, Any],
2624
+ output_types: dict[str, Any],
2625
+ installed_packages_list: list[str],
2626
+ docker_image_name: str | None = None,
2627
+ args: list[str] | None = None,
2628
+ ) -> Artifact | None:
2629
+ docker_image_name = docker_image_name or os.getenv("WANDB_DOCKER")
2630
+
2631
+ if not docker_image_name:
2632
+ return None
2633
+
2634
+ name = wandb.util.make_artifact_name_safe(f"job-{docker_image_name}")
2635
+ s_args: Sequence[str] = args if args is not None else self._settings._args
2636
+ source_info: JobSourceDict = {
2637
+ "_version": "v0",
2638
+ "source_type": "image",
2639
+ "source": {"image": docker_image_name, "args": s_args},
2640
+ "input_types": input_types,
2641
+ "output_types": output_types,
2642
+ "runtime": self._settings._python,
2643
+ }
2644
+ job_artifact = self._construct_job_artifact(
2645
+ name, source_info, installed_packages_list
2646
+ )
2647
+
2648
+ return job_artifact
2649
+
2650
+ def _log_job_artifact_with_image(
2651
+ self, docker_image_name: str, args: list[str] | None = None
2652
+ ) -> Artifact:
2653
+ packages, in_types, out_types = self._make_job_source_reqs()
2654
+ job_artifact = self._create_image_job(
2655
+ in_types,
2656
+ out_types,
2657
+ packages,
2658
+ args=args,
2659
+ docker_image_name=docker_image_name,
2660
+ )
2661
+
2662
+ assert job_artifact
2663
+ artifact = self.log_artifact(job_artifact)
2664
+
2665
+ if not artifact:
2666
+ raise wandb.Error(f"Job Artifact log unsuccessful: {artifact}")
2667
+ else:
2668
+ return artifact
2669
+
2670
+ async def _display_finish_stats(
2671
+ self,
2672
+ progress_printer: progress.ProgressPrinter,
2673
+ ) -> None:
2674
+ last_result: Result | None = None
2675
+
2676
+ async def loop_update_printer() -> None:
2677
+ while True:
2678
+ if last_result:
2679
+ progress_printer.update(
2680
+ [last_result.response.poll_exit_response],
2681
+ )
2682
+ await asyncio.sleep(0.1)
2683
+
2684
+ async def loop_poll_exit() -> None:
2685
+ nonlocal last_result
2686
+ assert self._backend and self._backend.interface
2687
+
2688
+ while True:
2689
+ handle = self._backend.interface.deliver_poll_exit()
2690
+
2691
+ time_start = time.monotonic()
2692
+ last_result = await handle.wait_async(timeout=None)
2693
+
2694
+ # Update at most once a second.
2695
+ time_elapsed = time.monotonic() - time_start
2696
+ if time_elapsed < 1:
2697
+ await asyncio.sleep(1 - time_elapsed)
2698
+
2699
+ async with asyncio_compat.open_task_group() as task_group:
2700
+ task_group.start_soon(loop_update_printer())
2701
+ task_group.start_soon(loop_poll_exit())
2702
+
2703
+ def _on_finish(self) -> None:
2704
+ trigger.call("on_finished")
2705
+
2706
+ if self._run_status_checker is not None:
2707
+ self._run_status_checker.stop()
2708
+
2709
+ self._console_stop() # TODO: there's a race here with jupyter console logging
2710
+
2711
+ assert self._backend and self._backend.interface
2712
+
2713
+ if self._settings.x_update_finish_state:
2714
+ exit_handle = self._backend.interface.deliver_exit(self._exit_code)
2715
+ else:
2716
+ exit_handle = self._backend.interface.deliver_finish_without_exit()
2717
+
2718
+ with progress.progress_printer(
2719
+ self._printer,
2720
+ default_text="Finishing up...",
2721
+ ) as progress_printer:
2722
+ # Wait for the run to complete.
2723
+ wait_with_progress(
2724
+ exit_handle,
2725
+ timeout=None,
2726
+ display_progress=functools.partial(
2727
+ self._display_finish_stats,
2728
+ progress_printer,
2729
+ ),
2730
+ )
2731
+
2732
+ poll_exit_handle = self._backend.interface.deliver_poll_exit()
2733
+ result = poll_exit_handle.wait_or(timeout=None)
2734
+ self._poll_exit_response = result.response.poll_exit_response
2735
+
2736
+ internal_messages_handle = self._backend.interface.deliver_internal_messages()
2737
+ result = internal_messages_handle.wait_or(timeout=None)
2738
+ self._internal_messages_response = result.response.internal_messages_response
2739
+
2740
+ # dispatch all our final requests
2741
+
2742
+ final_summary_handle = self._backend.interface.deliver_get_summary()
2743
+ sampled_history_handle = (
2744
+ self._backend.interface.deliver_request_sampled_history()
2745
+ )
2746
+
2747
+ result = sampled_history_handle.wait_or(timeout=None)
2748
+ self._sampled_history = result.response.sampled_history_response
2749
+
2750
+ result = final_summary_handle.wait_or(timeout=None)
2751
+ self._final_summary = result.response.get_summary_response
2752
+
2753
+ if self._backend:
2754
+ self._backend.cleanup()
2755
+
2756
+ if self._run_status_checker:
2757
+ self._run_status_checker.join()
2758
+
2759
+ if self._settings.run_id:
2760
+ self._unregister_telemetry_import_hooks(self._settings.run_id)
2761
+
2762
+ @staticmethod
2763
+ def _unregister_telemetry_import_hooks(run_id: str) -> None:
2764
+ import_telemetry_set = telemetry.list_telemetry_imports()
2765
+ for module_name in import_telemetry_set:
2766
+ unregister_post_import_hook(module_name, run_id)
2767
+
2768
+ @_log_to_run
2769
+ @_raise_if_finished
2770
+ @_attach
2771
+ def define_metric(
2772
+ self,
2773
+ name: str,
2774
+ step_metric: str | wandb_metric.Metric | None = None,
2775
+ step_sync: bool | None = None,
2776
+ hidden: bool | None = None,
2777
+ summary: str | None = None,
2778
+ goal: str | None = None,
2779
+ overwrite: bool | None = None,
2780
+ ) -> wandb_metric.Metric:
2781
+ """Customize metrics logged with `wandb.Run.log()`.
2782
+
2783
+ Args:
2784
+ name: The name of the metric to customize.
2785
+ step_metric: The name of another metric to serve as the X-axis
2786
+ for this metric in automatically generated charts.
2787
+ step_sync: Automatically insert the last value of step_metric into
2788
+ `wandb.Run.log()` if it is not provided explicitly. Defaults to True
2789
+ if step_metric is specified.
2790
+ hidden: Hide this metric from automatic plots.
2791
+ summary: Specify aggregate metrics added to summary.
2792
+ Supported aggregations include "min", "max", "mean", "last",
2793
+ "first", "best", "copy" and "none". "none" prevents a summary
2794
+ from being generated. "best" is used together with the goal
2795
+ parameter, "best" is deprecated and should not be used, use
2796
+ "min" or "max" instead. "copy" is deprecated and should not be
2797
+ used.
2798
+ goal: Specify how to interpret the "best" summary type.
2799
+ Supported options are "minimize" and "maximize". "goal" is
2800
+ deprecated and should not be used, use "min" or "max" instead.
2801
+ overwrite: If false, then this call is merged with previous
2802
+ `define_metric` calls for the same metric by using their
2803
+ values for any unspecified parameters. If true, then
2804
+ unspecified parameters overwrite values specified by
2805
+ previous calls.
2806
+
2807
+ Returns:
2808
+ An object that represents this call but can otherwise be discarded.
2809
+ """
2810
+ if summary and "copy" in summary:
2811
+ deprecate.deprecate(
2812
+ Deprecated.run__define_metric_copy,
2813
+ "define_metric(summary='copy') is deprecated and will be removed.",
2814
+ self,
2815
+ )
2816
+
2817
+ if (summary and "best" in summary) or goal is not None:
2818
+ deprecate.deprecate(
2819
+ Deprecated.run__define_metric_best_goal,
2820
+ "define_metric(summary='best', goal=...) is deprecated and will be removed. "
2821
+ "Use define_metric(summary='min') or define_metric(summary='max') instead.",
2822
+ self,
2823
+ )
2824
+
2825
+ return self._define_metric(
2826
+ name,
2827
+ step_metric,
2828
+ step_sync,
2829
+ hidden,
2830
+ summary,
2831
+ goal,
2832
+ overwrite,
2833
+ )
2834
+
2835
+ def _define_metric(
2836
+ self,
2837
+ name: str,
2838
+ step_metric: str | wandb_metric.Metric | None = None,
2839
+ step_sync: bool | None = None,
2840
+ hidden: bool | None = None,
2841
+ summary: str | None = None,
2842
+ goal: str | None = None,
2843
+ overwrite: bool | None = None,
2844
+ ) -> wandb_metric.Metric:
2845
+ if not name:
2846
+ raise wandb.Error("define_metric() requires non-empty name argument")
2847
+ if isinstance(step_metric, wandb_metric.Metric):
2848
+ step_metric = step_metric.name
2849
+ for arg_name, arg_val, exp_type in (
2850
+ ("name", name, str),
2851
+ ("step_metric", step_metric, str),
2852
+ ("step_sync", step_sync, bool),
2853
+ ("hidden", hidden, bool),
2854
+ ("summary", summary, str),
2855
+ ("goal", goal, str),
2856
+ ("overwrite", overwrite, bool),
2857
+ ):
2858
+ # NOTE: type checking is broken for isinstance and str
2859
+ if arg_val is not None and not isinstance(arg_val, exp_type):
2860
+ arg_type = type(arg_val).__name__
2861
+ raise wandb.Error(
2862
+ f"Unhandled define_metric() arg: {arg_name} type: {arg_type}"
2863
+ )
2864
+ stripped = name[:-1] if name.endswith("*") else name
2865
+ if "*" in stripped:
2866
+ raise wandb.Error(
2867
+ f"Unhandled define_metric() arg: name (glob suffixes only): {name}"
2868
+ )
2869
+ summary_ops: Sequence[str] | None = None
2870
+ if summary:
2871
+ summary_items = [s.lower() for s in summary.split(",")]
2872
+ summary_ops = []
2873
+ valid = {"min", "max", "mean", "best", "last", "copy", "none", "first"}
2874
+ # TODO: deprecate copy and best
2875
+ for i in summary_items:
2876
+ if i not in valid:
2877
+ raise wandb.Error(f"Unhandled define_metric() arg: summary op: {i}")
2878
+ summary_ops.append(i)
2879
+ with telemetry.context(run=self) as tel:
2880
+ tel.feature.metric_summary = True
2881
+ # TODO: deprecate goal
2882
+ goal_cleaned: str | None = None
2883
+ if goal is not None:
2884
+ goal_cleaned = goal[:3].lower()
2885
+ valid_goal = {"min", "max"}
2886
+ if goal_cleaned not in valid_goal:
2887
+ raise wandb.Error(f"Unhandled define_metric() arg: goal: {goal}")
2888
+ with telemetry.context(run=self) as tel:
2889
+ tel.feature.metric_goal = True
2890
+ if hidden:
2891
+ with telemetry.context(run=self) as tel:
2892
+ tel.feature.metric_hidden = True
2893
+ if step_sync:
2894
+ with telemetry.context(run=self) as tel:
2895
+ tel.feature.metric_step_sync = True
2896
+
2897
+ with telemetry.context(run=self) as tel:
2898
+ tel.feature.metric = True
2899
+
2900
+ m = wandb_metric.Metric(
2901
+ name=name,
2902
+ step_metric=step_metric,
2903
+ step_sync=step_sync,
2904
+ summary=summary_ops,
2905
+ hidden=hidden,
2906
+ goal=goal_cleaned,
2907
+ overwrite=overwrite,
2908
+ )
2909
+ m._set_callback(self._metric_callback)
2910
+ m._commit()
2911
+ return m
2912
+
2913
+ @_log_to_run
2914
+ @_attach
2915
+ def watch(
2916
+ self,
2917
+ models: torch.nn.Module | Sequence[torch.nn.Module],
2918
+ criterion: torch.F | None = None, # type: ignore
2919
+ log: Literal["gradients", "parameters", "all"] | None = "gradients",
2920
+ log_freq: int = 1000,
2921
+ idx: int | None = None,
2922
+ log_graph: bool = False,
2923
+ ) -> None:
2924
+ """Hook into given PyTorch model to monitor gradients and the model's computational graph.
2925
+
2926
+ This function can track parameters, gradients, or both during training.
2927
+
2928
+ Args:
2929
+ models: A single model or a sequence of models to be monitored.
2930
+ criterion: The loss function being optimized (optional).
2931
+ log: Specifies whether to log "gradients", "parameters", or "all".
2932
+ Set to None to disable logging. (default="gradients").
2933
+ log_freq: Frequency (in batches) to log gradients and parameters. (default=1000)
2934
+ idx: Index used when tracking multiple models with `wandb.watch`. (default=None)
2935
+ log_graph: Whether to log the model's computational graph. (default=False)
2936
+
2937
+ Raises:
2938
+ ValueError:
2939
+ If `wandb.init()` has not been called or if any of the models are not instances
2940
+ of `torch.nn.Module`.
2941
+ """
2942
+ wandb.sdk._watch(self, models, criterion, log, log_freq, idx, log_graph)
2943
+
2944
+ @_log_to_run
2945
+ @_attach
2946
+ def unwatch(
2947
+ self, models: torch.nn.Module | Sequence[torch.nn.Module] | None = None
2948
+ ) -> None:
2949
+ """Remove pytorch model topology, gradient and parameter hooks.
2950
+
2951
+ Args:
2952
+ models: Optional list of pytorch models that have had watch called on them.
2953
+ """
2954
+ wandb.sdk._unwatch(self, models=models)
2955
+
2956
+ @_log_to_run
2957
+ @_raise_if_finished
2958
+ @_attach
2959
+ def link_artifact(
2960
+ self,
2961
+ artifact: Artifact,
2962
+ target_path: str,
2963
+ aliases: list[str] | None = None,
2964
+ ) -> Artifact:
2965
+ """Link the given artifact to a portfolio (a promoted collection of artifacts).
2966
+
2967
+ Linked artifacts are visible in the UI for the specified portfolio.
2968
+
2969
+ Args:
2970
+ artifact: the (public or local) artifact which will be linked
2971
+ target_path: `str` - takes the following forms: `{portfolio}`, `{project}/{portfolio}`,
2972
+ or `{entity}/{project}/{portfolio}`
2973
+ aliases: `List[str]` - optional alias(es) that will only be applied on this linked artifact
2974
+ inside the portfolio.
2975
+ The alias "latest" will always be applied to the latest version of an artifact that is linked.
2976
+
2977
+ Returns:
2978
+ The linked artifact.
2979
+
2980
+ """
2981
+ if artifact.is_draft() and not artifact._is_draft_save_started():
2982
+ artifact = self._log_artifact(artifact)
2983
+
2984
+ if self._settings._offline:
2985
+ # TODO: implement offline mode + sync
2986
+ raise NotImplementedError
2987
+
2988
+ # Normalize the target "entity/project/collection" with defaults
2989
+ # inferred from this run's entity and project, if needed.
2990
+ #
2991
+ # HOWEVER, if the target path is a registry collection, avoid setting
2992
+ # the target entity to the run's entity. Instead, delegate to
2993
+ # Artifact.link() to resolve the required org entity.
2994
+ target = ArtifactPath.from_str(target_path)
2995
+ if not (target.project and is_artifact_registry_project(target.project)):
2996
+ target = target.with_defaults(prefix=self.entity, project=self.project)
2997
+
2998
+ return artifact.link(target.to_str(), aliases)
2999
+
3000
+ @_log_to_run
3001
+ @_raise_if_finished
3002
+ @_attach
3003
+ def use_artifact(
3004
+ self,
3005
+ artifact_or_name: str | Artifact,
3006
+ type: str | None = None,
3007
+ aliases: list[str] | None = None,
3008
+ use_as: str | None = None,
3009
+ ) -> Artifact:
3010
+ """Declare an artifact as an input to a run.
3011
+
3012
+ Call `download` or `file` on the returned object to get the contents locally.
3013
+
3014
+ Args:
3015
+ artifact_or_name: The name of the artifact to use. May be prefixed
3016
+ with the name of the project the artifact was logged to
3017
+ ("<entity>" or "<entity>/<project>"). If no
3018
+ entity is specified in the name, the Run or API setting's entity is used.
3019
+ Valid names can be in the following forms
3020
+ - name:version
3021
+ - name:alias
3022
+ type: The type of artifact to use.
3023
+ aliases: Aliases to apply to this artifact
3024
+ use_as: This argument is deprecated and does nothing.
3025
+
3026
+ Returns:
3027
+ An `Artifact` object.
3028
+
3029
+ Examples:
3030
+ ```python
3031
+ import wandb
3032
+
3033
+ run = wandb.init(project="<example>")
3034
+
3035
+ # Use an artifact by name and alias
3036
+ artifact_a = run.use_artifact(artifact_or_name="<name>:<alias>")
3037
+
3038
+ # Use an artifact by name and version
3039
+ artifact_b = run.use_artifact(artifact_or_name="<name>:v<version>")
3040
+
3041
+ # Use an artifact by entity/project/name:alias
3042
+ artifact_c = run.use_artifact(
3043
+ artifact_or_name="<entity>/<project>/<name>:<alias>"
3044
+ )
3045
+
3046
+ # Use an artifact by entity/project/name:version
3047
+ artifact_d = run.use_artifact(
3048
+ artifact_or_name="<entity>/<project>/<name>:v<version>"
3049
+ )
3050
+
3051
+ # Explicitly finish the run since a context manager is not used.
3052
+ run.finish()
3053
+ ```
3054
+
3055
+ """
3056
+ if self._settings._offline:
3057
+ raise TypeError("Cannot use artifact when in offline mode.")
3058
+
3059
+ api = internal.Api(
3060
+ default_settings={
3061
+ "entity": self._settings.entity,
3062
+ "project": self._settings.project,
3063
+ }
3064
+ )
3065
+ api.set_current_run_id(self._settings.run_id)
3066
+
3067
+ if use_as is not None:
3068
+ deprecate.deprecate(
3069
+ field_name=Deprecated.run__use_artifact_use_as,
3070
+ warning_message=(
3071
+ "`use_as` argument is deprecated and does not affect the behaviour of `run.use_artifact`"
3072
+ ),
3073
+ )
3074
+
3075
+ if isinstance(artifact_or_name, str):
3076
+ name = artifact_or_name
3077
+ public_api = self._public_api()
3078
+ artifact = public_api._artifact(type=type, name=name)
3079
+ if type is not None and type != artifact.type:
3080
+ raise ValueError(
3081
+ f"Supplied type {type} does not match type {artifact.type} of artifact {artifact.name}"
3082
+ )
3083
+ api.use_artifact(
3084
+ artifact.id,
3085
+ entity_name=self._settings.entity,
3086
+ project_name=self._settings.project,
3087
+ artifact_entity_name=artifact.entity,
3088
+ artifact_project_name=artifact.project,
3089
+ )
3090
+ else:
3091
+ artifact = artifact_or_name
3092
+ if aliases is None:
3093
+ aliases = []
3094
+ elif isinstance(aliases, str):
3095
+ aliases = [aliases]
3096
+ if isinstance(artifact_or_name, Artifact) and artifact.is_draft():
3097
+ if use_as is not None:
3098
+ wandb.termwarn(
3099
+ "Indicating use_as is not supported when using a draft artifact"
3100
+ )
3101
+ self._log_artifact(
3102
+ artifact,
3103
+ aliases=aliases,
3104
+ is_user_created=True,
3105
+ use_after_commit=True,
3106
+ )
3107
+ artifact.wait()
3108
+ elif isinstance(artifact, Artifact) and not artifact.is_draft():
3109
+ api.use_artifact(
3110
+ artifact.id,
3111
+ artifact_entity_name=artifact.entity,
3112
+ artifact_project_name=artifact.project,
3113
+ )
3114
+ else:
3115
+ raise ValueError(
3116
+ 'You must pass an artifact name (e.g. "pedestrian-dataset:v1"), '
3117
+ "an instance of `wandb.Artifact`, or `wandb.Api().artifact()` to `use_artifact`"
3118
+ )
3119
+ if self._backend and self._backend.interface:
3120
+ self._backend.interface.publish_use_artifact(artifact)
3121
+ return artifact
3122
+
3123
+ @_log_to_run
3124
+ @_raise_if_finished
3125
+ @_attach
3126
+ def log_artifact(
3127
+ self,
3128
+ artifact_or_path: Artifact | StrPath,
3129
+ name: str | None = None,
3130
+ type: str | None = None,
3131
+ aliases: list[str] | None = None,
3132
+ tags: list[str] | None = None,
3133
+ ) -> Artifact:
3134
+ """Declare an artifact as an output of a run.
3135
+
3136
+ Args:
3137
+ artifact_or_path: (str or Artifact) A path to the contents of this artifact,
3138
+ can be in the following forms:
3139
+ - `/local/directory`
3140
+ - `/local/directory/file.txt`
3141
+ - `s3://bucket/path`
3142
+ You can also pass an Artifact object created by calling
3143
+ `wandb.Artifact`.
3144
+ name: (str, optional) An artifact name. Valid names can be in the following forms:
3145
+ - name:version
3146
+ - name:alias
3147
+ - digest
3148
+ This will default to the basename of the path prepended with the current
3149
+ run id if not specified.
3150
+ type: (str) The type of artifact to log, examples include `dataset`, `model`
3151
+ aliases: (list, optional) Aliases to apply to this artifact,
3152
+ defaults to `["latest"]`
3153
+ tags: (list, optional) Tags to apply to this artifact, if any.
3154
+
3155
+ Returns:
3156
+ An `Artifact` object.
3157
+ """
3158
+ return self._log_artifact(
3159
+ artifact_or_path,
3160
+ name=name,
3161
+ type=type,
3162
+ aliases=aliases,
3163
+ tags=tags,
3164
+ )
3165
+
3166
+ @_log_to_run
3167
+ @_raise_if_finished
3168
+ @_attach
3169
+ def upsert_artifact(
3170
+ self,
3171
+ artifact_or_path: Artifact | str,
3172
+ name: str | None = None,
3173
+ type: str | None = None,
3174
+ aliases: list[str] | None = None,
3175
+ distributed_id: str | None = None,
3176
+ ) -> Artifact:
3177
+ """Declare (or append to) a non-finalized artifact as output of a run.
3178
+
3179
+ Note that you must call run.finish_artifact() to finalize the artifact.
3180
+ This is useful when distributed jobs need to all contribute to the same artifact.
3181
+
3182
+ Args:
3183
+ artifact_or_path: A path to the contents of this artifact,
3184
+ can be in the following forms:
3185
+ - `/local/directory`
3186
+ - `/local/directory/file.txt`
3187
+ - `s3://bucket/path`
3188
+ name: An artifact name. May be prefixed with "entity/project". Defaults
3189
+ to the basename of the path prepended with the current run ID
3190
+ if not specified. Valid names can be in the following forms:
3191
+ - name:version
3192
+ - name:alias
3193
+ - digest
3194
+ type: The type of artifact to log. Common examples include `dataset`, `model`.
3195
+ aliases: Aliases to apply to this artifact, defaults to `["latest"]`.
3196
+ distributed_id: Unique string that all distributed jobs share. If None,
3197
+ defaults to the run's group name.
3198
+
3199
+ Returns:
3200
+ An `Artifact` object.
3201
+ """
3202
+ if self._settings.run_group is None and distributed_id is None:
3203
+ raise TypeError(
3204
+ "Cannot upsert artifact unless run is in a group or distributed_id is provided"
3205
+ )
3206
+ if distributed_id is None:
3207
+ distributed_id = self._settings.run_group or ""
3208
+ return self._log_artifact(
3209
+ artifact_or_path,
3210
+ name=name,
3211
+ type=type,
3212
+ aliases=aliases,
3213
+ distributed_id=distributed_id,
3214
+ finalize=False,
3215
+ )
3216
+
3217
+ @_log_to_run
3218
+ @_raise_if_finished
3219
+ @_attach
3220
+ def finish_artifact(
3221
+ self,
3222
+ artifact_or_path: Artifact | str,
3223
+ name: str | None = None,
3224
+ type: str | None = None,
3225
+ aliases: list[str] | None = None,
3226
+ distributed_id: str | None = None,
3227
+ ) -> Artifact:
3228
+ """Finishes a non-finalized artifact as output of a run.
3229
+
3230
+ Subsequent "upserts" with the same distributed ID will result in a new version.
3231
+
3232
+ Args:
3233
+ artifact_or_path: A path to the contents of this artifact,
3234
+ can be in the following forms:
3235
+ - `/local/directory`
3236
+ - `/local/directory/file.txt`
3237
+ - `s3://bucket/path`
3238
+ You can also pass an Artifact object created by calling
3239
+ `wandb.Artifact`.
3240
+ name: An artifact name. May be prefixed with entity/project.
3241
+ Valid names can be in the following forms:
3242
+ - name:version
3243
+ - name:alias
3244
+ - digest
3245
+ This will default to the basename of the path prepended with the current
3246
+ run id if not specified.
3247
+ type: The type of artifact to log, examples include `dataset`, `model`
3248
+ aliases: Aliases to apply to this artifact,
3249
+ defaults to `["latest"]`
3250
+ distributed_id: Unique string that all distributed jobs share. If None,
3251
+ defaults to the run's group name.
3252
+
3253
+ Returns:
3254
+ An `Artifact` object.
3255
+ """
3256
+ if self._settings.run_group is None and distributed_id is None:
3257
+ raise TypeError(
3258
+ "Cannot finish artifact unless run is in a group or distributed_id is provided"
3259
+ )
3260
+ if distributed_id is None:
3261
+ distributed_id = self._settings.run_group or ""
3262
+
3263
+ return self._log_artifact(
3264
+ artifact_or_path,
3265
+ name,
3266
+ type,
3267
+ aliases,
3268
+ distributed_id=distributed_id,
3269
+ finalize=True,
3270
+ )
3271
+
3272
+ def _log_artifact(
3273
+ self,
3274
+ artifact_or_path: Artifact | StrPath,
3275
+ name: str | None = None,
3276
+ type: str | None = None,
3277
+ aliases: list[str] | None = None,
3278
+ tags: list[str] | None = None,
3279
+ distributed_id: str | None = None,
3280
+ finalize: bool = True,
3281
+ is_user_created: bool = False,
3282
+ use_after_commit: bool = False,
3283
+ ) -> Artifact:
3284
+ if self._settings.anonymous in ["allow", "must"]:
3285
+ wandb.termwarn(
3286
+ "Artifacts logged anonymously cannot be claimed and expire after 7 days."
3287
+ )
3288
+
3289
+ if not finalize and distributed_id is None:
3290
+ raise TypeError("Must provide distributed_id if artifact is not finalize")
3291
+
3292
+ if aliases is not None:
3293
+ aliases = validate_aliases(aliases)
3294
+
3295
+ # Check if artifact tags are supported
3296
+ if tags is not None:
3297
+ tags = validate_tags(tags)
3298
+
3299
+ artifact, aliases = self._prepare_artifact(
3300
+ artifact_or_path, name, type, aliases
3301
+ )
3302
+
3303
+ if len(artifact.metadata) > MAX_ARTIFACT_METADATA_KEYS:
3304
+ raise ValueError(
3305
+ f"Artifact must not have more than {MAX_ARTIFACT_METADATA_KEYS} metadata keys."
3306
+ )
3307
+
3308
+ artifact.distributed_id = distributed_id
3309
+ self._assert_can_log_artifact(artifact)
3310
+ if self._backend and self._backend.interface:
3311
+ if not self._settings._offline:
3312
+ handle = self._backend.interface.deliver_artifact(
3313
+ self,
3314
+ artifact,
3315
+ aliases,
3316
+ tags,
3317
+ self.step,
3318
+ finalize=finalize,
3319
+ is_user_created=is_user_created,
3320
+ use_after_commit=use_after_commit,
3321
+ )
3322
+ artifact._set_save_handle(handle, self._public_api().client)
3323
+ else:
3324
+ self._backend.interface.publish_artifact(
3325
+ self,
3326
+ artifact,
3327
+ aliases,
3328
+ tags,
3329
+ finalize=finalize,
3330
+ is_user_created=is_user_created,
3331
+ use_after_commit=use_after_commit,
3332
+ )
3333
+ elif self._internal_run_interface:
3334
+ self._internal_run_interface.publish_artifact(
3335
+ self,
3336
+ artifact,
3337
+ aliases,
3338
+ tags,
3339
+ finalize=finalize,
3340
+ is_user_created=is_user_created,
3341
+ use_after_commit=use_after_commit,
3342
+ )
3343
+ return artifact
3344
+
3345
+ def _public_api(self, overrides: dict[str, str] | None = None) -> PublicApi:
3346
+ overrides = {"run": self._settings.run_id} # type: ignore
3347
+ if not self._settings._offline:
3348
+ overrides["entity"] = self._settings.entity or ""
3349
+ overrides["project"] = self._settings.project or ""
3350
+ return public.Api(overrides)
3351
+
3352
+ # TODO(jhr): annotate this
3353
+ def _assert_can_log_artifact(self, artifact) -> None: # type: ignore
3354
+ if self._settings._offline:
3355
+ return
3356
+ try:
3357
+ public_api = self._public_api()
3358
+ entity = public_api.settings["entity"]
3359
+ project = public_api.settings["project"]
3360
+ expected_type = Artifact._expected_type(
3361
+ entity, project, artifact.name, public_api.client
3362
+ )
3363
+ except requests.exceptions.RequestException:
3364
+ # Just return early if there is a network error. This is
3365
+ # ok, as this function is intended to help catch an invalid
3366
+ # type early, but not a hard requirement for valid operation.
3367
+ return
3368
+ if expected_type is not None and artifact.type != expected_type:
3369
+ raise ValueError(
3370
+ f"Artifact {artifact.name} already exists with type '{expected_type}'; "
3371
+ f"cannot create another with type '{artifact.type}'"
3372
+ )
3373
+ if entity and artifact._source_entity and entity != artifact._source_entity:
3374
+ raise ValueError(
3375
+ f"Artifact {artifact.name} is owned by entity "
3376
+ f"'{artifact._source_entity}'; it can't be moved to '{entity}'"
3377
+ )
3378
+ if project and artifact._source_project and project != artifact._source_project:
3379
+ raise ValueError(
3380
+ f"Artifact {artifact.name} exists in project "
3381
+ f"'{artifact._source_project}'; it can't be moved to '{project}'"
3382
+ )
3383
+
3384
+ def _prepare_artifact(
3385
+ self,
3386
+ artifact_or_path: Artifact | StrPath,
3387
+ name: str | None = None,
3388
+ type: str | None = None,
3389
+ aliases: list[str] | None = None,
3390
+ ) -> tuple[Artifact, list[str]]:
3391
+ if isinstance(artifact_or_path, (str, os.PathLike)):
3392
+ name = (
3393
+ name
3394
+ or f"run-{self._settings.run_id}-{os.path.basename(artifact_or_path)}"
3395
+ )
3396
+ artifact = Artifact(name, type or "unspecified")
3397
+ if os.path.isfile(artifact_or_path):
3398
+ artifact.add_file(str(artifact_or_path))
3399
+ elif os.path.isdir(artifact_or_path):
3400
+ artifact.add_dir(str(artifact_or_path))
3401
+ elif "://" in str(artifact_or_path):
3402
+ artifact.add_reference(str(artifact_or_path))
3403
+ else:
3404
+ raise ValueError(
3405
+ "path must be a file, directory or external"
3406
+ "reference like s3://bucket/path"
3407
+ )
3408
+ else:
3409
+ artifact = artifact_or_path
3410
+ if not isinstance(artifact, Artifact):
3411
+ raise TypeError(
3412
+ "You must pass an instance of wandb.Artifact or a "
3413
+ "valid file path to log_artifact"
3414
+ )
3415
+
3416
+ artifact.finalize()
3417
+ return artifact, _resolve_aliases(aliases)
3418
+
3419
+ @_log_to_run
3420
+ @_raise_if_finished
3421
+ @_attach
3422
+ def log_model(
3423
+ self,
3424
+ path: StrPath,
3425
+ name: str | None = None,
3426
+ aliases: list[str] | None = None,
3427
+ ) -> None:
3428
+ """Logs a model artifact containing the contents inside the 'path' to a run and marks it as an output to this run.
3429
+
3430
+ The name of model artifact can only contain alphanumeric characters,
3431
+ underscores, and hyphens.
3432
+
3433
+ Args:
3434
+ path: (str) A path to the contents of this model,
3435
+ can be in the following forms:
3436
+ - `/local/directory`
3437
+ - `/local/directory/file.txt`
3438
+ - `s3://bucket/path`
3439
+ name: A name to assign to the model artifact that
3440
+ the file contents will be added to. This will default to the
3441
+ basename of the path prepended with the current run id if
3442
+ not specified.
3443
+ aliases: Aliases to apply to the created model artifact,
3444
+ defaults to `["latest"]`
3445
+
3446
+ Raises:
3447
+ ValueError: If name has invalid special characters.
3448
+
3449
+ Returns:
3450
+ None
3451
+ """
3452
+ self._log_artifact(
3453
+ artifact_or_path=path, name=name, type="model", aliases=aliases
3454
+ )
3455
+
3456
+ @_log_to_run
3457
+ @_raise_if_finished
3458
+ @_attach
3459
+ def use_model(self, name: str) -> FilePathStr:
3460
+ """Download the files logged in a model artifact 'name'.
3461
+
3462
+ Args:
3463
+ name: A model artifact name. 'name' must match the name of an existing logged
3464
+ model artifact. May be prefixed with `entity/project/`. Valid names
3465
+ can be in the following forms
3466
+ - model_artifact_name:version
3467
+ - model_artifact_name:alias
3468
+
3469
+ Returns:
3470
+ path (str): Path to downloaded model artifact file(s).
3471
+
3472
+ Raises:
3473
+ AssertionError: If model artifact 'name' is of a type that does
3474
+ not contain the substring 'model'.
3475
+ """
3476
+ if self._settings._offline:
3477
+ # Downloading artifacts is not supported when offline.
3478
+ raise RuntimeError("`use_model` not supported in offline mode.")
3479
+
3480
+ artifact = self.use_artifact(artifact_or_name=name)
3481
+ if "model" not in str(artifact.type.lower()):
3482
+ raise AssertionError(
3483
+ "You can only use this method for 'model' artifacts."
3484
+ " For an artifact to be a 'model' artifact, its type property"
3485
+ " must contain the substring 'model'."
3486
+ )
3487
+
3488
+ path = artifact.download()
3489
+
3490
+ # If returned directory contains only one file, return path to that file
3491
+ dir_list = os.listdir(path)
3492
+ if len(dir_list) == 1:
3493
+ return FilePathStr(os.path.join(path, dir_list[0]))
3494
+ return path
3495
+
3496
+ @_log_to_run
3497
+ @_raise_if_finished
3498
+ @_attach
3499
+ def link_model(
3500
+ self,
3501
+ path: StrPath,
3502
+ registered_model_name: str,
3503
+ name: str | None = None,
3504
+ aliases: list[str] | None = None,
3505
+ ) -> Artifact | None:
3506
+ """Log a model artifact version and link it to a registered model in the model registry.
3507
+
3508
+ Linked model versions are visible in the UI for the specified registered model.
3509
+
3510
+ This method will:
3511
+ - Check if 'name' model artifact has been logged. If so, use the artifact version that matches the files
3512
+ located at 'path' or log a new version. Otherwise log files under 'path' as a new model artifact, 'name'
3513
+ of type 'model'.
3514
+ - Check if registered model with name 'registered_model_name' exists in the 'model-registry' project.
3515
+ If not, create a new registered model with name 'registered_model_name'.
3516
+ - Link version of model artifact 'name' to registered model, 'registered_model_name'.
3517
+ - Attach aliases from 'aliases' list to the newly linked model artifact version.
3518
+
3519
+ Args:
3520
+ path: (str) A path to the contents of this model, can be in the
3521
+ following forms:
3522
+ - `/local/directory`
3523
+ - `/local/directory/file.txt`
3524
+ - `s3://bucket/path`
3525
+ registered_model_name: The name of the registered model that the
3526
+ model is to be linked to. A registered model is a collection of
3527
+ model versions linked to the model registry, typically
3528
+ representing a team's specific ML Task. The entity that this
3529
+ registered model belongs to will be derived from the run.
3530
+ name: The name of the model artifact that files in 'path' will be
3531
+ logged to. This will default to the basename of the path
3532
+ prepended with the current run id if not specified.
3533
+ aliases: Aliases that will only be applied on this linked artifact
3534
+ inside the registered model. The alias "latest" will always be
3535
+ applied to the latest version of an artifact that is linked.
3536
+
3537
+ Raises:
3538
+ AssertionError: If registered_model_name is a path or
3539
+ if model artifact 'name' is of a type that does not contain
3540
+ the substring 'model'.
3541
+ ValueError: If name has invalid special characters.
3542
+
3543
+ Returns:
3544
+ The linked artifact if linking was successful, otherwise `None`.
3545
+ """
3546
+ name_parts = registered_model_name.split("/")
3547
+ if len(name_parts) != 1:
3548
+ raise AssertionError(
3549
+ "Please provide only the name of the registered model."
3550
+ " Do not append the entity or project name."
3551
+ )
3552
+
3553
+ project = "model-registry"
3554
+ target_path = self.entity + "/" + project + "/" + registered_model_name
3555
+
3556
+ public_api = self._public_api()
3557
+ try:
3558
+ artifact = public_api._artifact(name=f"{name}:latest")
3559
+ if "model" not in str(artifact.type.lower()):
3560
+ raise AssertionError(
3561
+ "You can only use this method for 'model' artifacts."
3562
+ " For an artifact to be a 'model' artifact, its type"
3563
+ " property must contain the substring 'model'."
3564
+ )
3565
+
3566
+ artifact = self._log_artifact(
3567
+ artifact_or_path=path, name=name, type=artifact.type
3568
+ )
3569
+ except (ValueError, CommError):
3570
+ artifact = self._log_artifact(
3571
+ artifact_or_path=path, name=name, type="model"
3572
+ )
3573
+ return self.link_artifact(
3574
+ artifact=artifact, target_path=target_path, aliases=aliases
3575
+ )
3576
+
3577
+ @_log_to_run
3578
+ @_raise_if_finished
3579
+ @_attach
3580
+ def alert(
3581
+ self,
3582
+ title: str,
3583
+ text: str,
3584
+ level: str | AlertLevel | None = None,
3585
+ wait_duration: int | float | timedelta | None = None,
3586
+ ) -> None:
3587
+ """Create an alert with the given title and text.
3588
+
3589
+ Args:
3590
+ title: The title of the alert, must be less than 64 characters long.
3591
+ text: The text body of the alert.
3592
+ level: The alert level to use, either: `INFO`, `WARN`, or `ERROR`.
3593
+ wait_duration: The time to wait (in seconds) before sending another
3594
+ alert with this title.
3595
+ """
3596
+ level = level or AlertLevel.INFO
3597
+ level_str: str = level.value if isinstance(level, AlertLevel) else level
3598
+ if level_str not in {lev.value for lev in AlertLevel}:
3599
+ raise ValueError("level must be one of 'INFO', 'WARN', or 'ERROR'")
3600
+
3601
+ wait_duration = wait_duration or timedelta(minutes=1)
3602
+ if isinstance(wait_duration, int) or isinstance(wait_duration, float):
3603
+ wait_duration = timedelta(seconds=wait_duration)
3604
+ elif not callable(getattr(wait_duration, "total_seconds", None)):
3605
+ raise TypeError(
3606
+ "wait_duration must be an int, float, or datetime.timedelta"
3607
+ )
3608
+ wait_duration = int(wait_duration.total_seconds() * 1000)
3609
+
3610
+ if self._backend and self._backend.interface:
3611
+ self._backend.interface.publish_alert(title, text, level_str, wait_duration)
3612
+
3613
+ def __enter__(self) -> Run:
3614
+ return self
3615
+
3616
+ def __exit__(
3617
+ self,
3618
+ exc_type: type[BaseException],
3619
+ exc_val: BaseException,
3620
+ exc_tb: TracebackType,
3621
+ ) -> bool:
3622
+ exception_raised = exc_type is not None
3623
+ if exception_raised:
3624
+ traceback.print_exception(exc_type, exc_val, exc_tb)
3625
+ exit_code = 1 if exception_raised else 0
3626
+ self._finish(exit_code=exit_code)
3627
+ return not exception_raised
3628
+
3629
+ @_log_to_run
3630
+ @_raise_if_finished
3631
+ @_attach
3632
+ def mark_preempting(self) -> None:
3633
+ """Mark this run as preempting.
3634
+
3635
+ Also tells the internal process to immediately report this to server.
3636
+ """
3637
+ if self._backend and self._backend.interface:
3638
+ self._backend.interface.publish_preempting()
3639
+
3640
+ @property
3641
+ @_log_to_run
3642
+ @_raise_if_finished
3643
+ @_attach
3644
+ def _system_metrics(self) -> dict[str, list[tuple[datetime, float]]]:
3645
+ """Returns a dictionary of system metrics.
3646
+
3647
+ Returns:
3648
+ A dictionary of system metrics.
3649
+ """
3650
+ from wandb.proto import wandb_internal_pb2
3651
+
3652
+ def pb_to_dict(
3653
+ system_metrics_pb: wandb_internal_pb2.GetSystemMetricsResponse,
3654
+ ) -> dict[str, list[tuple[datetime, float]]]:
3655
+ res = {}
3656
+
3657
+ for metric, records in system_metrics_pb.system_metrics.items():
3658
+ measurements = []
3659
+ for record in records.record:
3660
+ # Convert timestamp to datetime
3661
+ dt = datetime.fromtimestamp(
3662
+ record.timestamp.seconds, tz=timezone.utc
3663
+ )
3664
+ dt = dt.replace(microsecond=record.timestamp.nanos // 1000)
3665
+
3666
+ measurements.append((dt, record.value))
3667
+
3668
+ res[metric] = measurements
3669
+
3670
+ return res
3671
+
3672
+ if not self._backend or not self._backend.interface:
3673
+ return {}
3674
+
3675
+ handle = self._backend.interface.deliver_get_system_metrics()
3676
+
3677
+ try:
3678
+ result = handle.wait_or(timeout=1)
3679
+ except TimeoutError:
3680
+ return {}
3681
+ else:
3682
+ try:
3683
+ response = result.response.get_system_metrics_response
3684
+ return pb_to_dict(response) if response else {}
3685
+ except Exception:
3686
+ logger.exception("Error getting system metrics.")
3687
+ return {}
3688
+
3689
+ # ------------------------------------------------------------------------------
3690
+ # HEADER
3691
+ # ------------------------------------------------------------------------------
3692
+ def _header(self) -> None:
3693
+ self._header_wandb_version_info()
3694
+ self._header_sync_info()
3695
+ self._header_run_info()
3696
+
3697
+ def _header_wandb_version_info(self) -> None:
3698
+ if self._settings.quiet or self._settings.silent:
3699
+ return
3700
+
3701
+ # TODO: add this to a higher verbosity level
3702
+ self._printer.display(f"Tracking run with wandb version {wandb.__version__}")
3703
+
3704
+ def _header_sync_info(self) -> None:
3705
+ sync_location_msg = f"Run data is saved locally in {self._printer.files(self._settings.sync_dir)}"
3706
+
3707
+ if self._settings._offline:
3708
+ offline_warning = (
3709
+ f"W&B syncing is set to {self._printer.code('`offline`')} "
3710
+ f"in this directory. Run {self._printer.code('`wandb online`')} "
3711
+ f"or set {self._printer.code('WANDB_MODE=online')} "
3712
+ "to enable cloud syncing."
3713
+ )
3714
+ self._printer.display([offline_warning, sync_location_msg])
3715
+ else:
3716
+ messages = [sync_location_msg]
3717
+
3718
+ if not self._printer.supports_html:
3719
+ disable_sync_msg = (
3720
+ f"Run {self._printer.code('`wandb offline`')} to turn off syncing."
3721
+ )
3722
+ messages.append(disable_sync_msg)
3723
+
3724
+ if not self._settings.quiet and not self._settings.silent:
3725
+ self._printer.display(messages)
3726
+
3727
+ def _header_run_info(self) -> None:
3728
+ settings, printer = self._settings, self._printer
3729
+
3730
+ if settings._offline or settings.silent:
3731
+ return
3732
+
3733
+ run_url = settings.run_url
3734
+ project_url = settings.project_url
3735
+ sweep_url = settings.sweep_url
3736
+
3737
+ run_state_str = (
3738
+ "Resuming run"
3739
+ if settings.resumed or settings.resume_from
3740
+ else "Syncing run"
3741
+ )
3742
+ run_name = settings.run_name
3743
+ if not run_name:
3744
+ return
3745
+
3746
+ if printer.supports_html:
3747
+ import wandb.jupyter
3748
+
3749
+ if not wandb.jupyter.display_if_magic_is_used(self):
3750
+ run_line = f"<strong>{printer.link(run_url, run_name)}</strong>"
3751
+ project_line, sweep_line = "", ""
3752
+
3753
+ if not settings.quiet:
3754
+ doc_html = printer.link(url_registry.url("developer-guide"), "docs")
3755
+
3756
+ project_html = printer.link(project_url, "Weights & Biases")
3757
+ project_line = f"to {project_html} ({doc_html})"
3758
+
3759
+ if sweep_url:
3760
+ sweep_line = f"Sweep page: {printer.link(sweep_url, sweep_url)}"
3761
+
3762
+ printer.display(
3763
+ [f"{run_state_str} {run_line} {project_line}", sweep_line],
3764
+ )
3765
+
3766
+ elif run_name:
3767
+ printer.display(f"{run_state_str} {printer.name(run_name)}")
3768
+
3769
+ if not settings.quiet:
3770
+ # TODO: add verbosity levels and add this to higher levels
3771
+ printer.display(
3772
+ f"{printer.emoji('star')} View project at {printer.link(project_url)}"
3773
+ )
3774
+ if sweep_url:
3775
+ printer.display(
3776
+ f"{printer.emoji('broom')} View sweep at {printer.link(sweep_url)}"
3777
+ )
3778
+ printer.display(
3779
+ f"{printer.emoji('rocket')} View run at {printer.link(run_url)}",
3780
+ )
3781
+
3782
+ if run_name and settings.anonymous in ["allow", "must"]:
3783
+ printer.display(
3784
+ (
3785
+ "Do NOT share these links with anyone."
3786
+ " They can be used to claim your runs."
3787
+ ),
3788
+ level="warn",
3789
+ )
3790
+
3791
+ # ------------------------------------------------------------------------------
3792
+ # FOOTER
3793
+ # ------------------------------------------------------------------------------
3794
+ # Note: All the footer methods are static methods since we want to share the printing logic
3795
+ # with the service execution path that doesn't have access to the run instance
3796
+ @staticmethod
3797
+ def _footer(
3798
+ sampled_history: SampledHistoryResponse | None = None,
3799
+ final_summary: GetSummaryResponse | None = None,
3800
+ poll_exit_response: PollExitResponse | None = None,
3801
+ internal_messages_response: InternalMessagesResponse | None = None,
3802
+ *,
3803
+ settings: Settings,
3804
+ printer: printer.Printer,
3805
+ ) -> None:
3806
+ Run._footer_history_summary_info(
3807
+ history=sampled_history,
3808
+ summary=final_summary,
3809
+ settings=settings,
3810
+ printer=printer,
3811
+ )
3812
+
3813
+ Run._footer_sync_info(
3814
+ poll_exit_response=poll_exit_response,
3815
+ settings=settings,
3816
+ printer=printer,
3817
+ )
3818
+ Run._footer_log_dir_info(settings=settings, printer=printer)
3819
+ Run._footer_internal_messages(
3820
+ internal_messages_response=internal_messages_response,
3821
+ settings=settings,
3822
+ printer=printer,
3823
+ )
3824
+
3825
+ @staticmethod
3826
+ def _footer_sync_info(
3827
+ poll_exit_response: PollExitResponse | None = None,
3828
+ *,
3829
+ settings: Settings,
3830
+ printer: printer.Printer,
3831
+ ) -> None:
3832
+ if settings.silent:
3833
+ return
3834
+
3835
+ if settings._offline:
3836
+ if not settings.quiet:
3837
+ printer.display(
3838
+ [
3839
+ "You can sync this run to the cloud by running:",
3840
+ printer.code(f"wandb sync {settings.sync_dir}"),
3841
+ ],
3842
+ )
3843
+ return
3844
+
3845
+ info = []
3846
+ if settings.run_name and settings.run_url:
3847
+ info.append(
3848
+ f"{printer.emoji('rocket')} View run {printer.name(settings.run_name)} at: {printer.link(settings.run_url)}"
3849
+ )
3850
+ if settings.project_url:
3851
+ info.append(
3852
+ f"{printer.emoji('star')} View project at: {printer.link(settings.project_url)}"
3853
+ )
3854
+ if poll_exit_response and poll_exit_response.file_counts:
3855
+ logger.info("logging synced files")
3856
+ file_counts = poll_exit_response.file_counts
3857
+ info.append(
3858
+ f"Synced {file_counts.wandb_count} W&B file(s), {file_counts.media_count} media file(s), "
3859
+ f"{file_counts.artifact_count} artifact file(s) and {file_counts.other_count} other file(s)",
3860
+ )
3861
+ printer.display(info)
3862
+
3863
+ @staticmethod
3864
+ def _footer_log_dir_info(
3865
+ *,
3866
+ settings: Settings,
3867
+ printer: printer.Printer,
3868
+ ) -> None:
3869
+ if settings.quiet or settings.silent:
3870
+ return
3871
+
3872
+ log_dir = settings.log_user or settings.log_internal
3873
+ if log_dir:
3874
+ log_dir = os.path.dirname(log_dir.replace(os.getcwd(), "."))
3875
+ printer.display(
3876
+ f"Find logs at: {printer.files(log_dir)}",
3877
+ )
3878
+
3879
+ @staticmethod
3880
+ def _footer_history_summary_info(
3881
+ history: SampledHistoryResponse | None = None,
3882
+ summary: GetSummaryResponse | None = None,
3883
+ *,
3884
+ settings: Settings,
3885
+ printer: printer.Printer,
3886
+ ) -> None:
3887
+ if settings.quiet or settings.silent:
3888
+ return
3889
+
3890
+ panel: list[str] = []
3891
+
3892
+ if history and (
3893
+ history_grid := Run._footer_history(history, printer, settings)
3894
+ ):
3895
+ panel.append(history_grid)
3896
+
3897
+ if summary and (
3898
+ summary_grid := Run._footer_summary(summary, printer, settings)
3899
+ ):
3900
+ panel.append(summary_grid)
3901
+
3902
+ if panel:
3903
+ printer.display(printer.panel(panel))
3904
+
3905
+ @staticmethod
3906
+ def _footer_history(
3907
+ history: SampledHistoryResponse,
3908
+ printer: printer.Printer,
3909
+ settings: Settings,
3910
+ ) -> str | None:
3911
+ """Returns the run history formatted for printing to the console."""
3912
+ sorted_history_items = sorted(
3913
+ (item for item in history.item if not item.key.startswith("_")),
3914
+ key=lambda item: item.key,
3915
+ )
3916
+
3917
+ history_rows: list[list[str]] = []
3918
+ for item in sorted_history_items:
3919
+ if len(history_rows) >= settings.max_end_of_run_history_metrics:
3920
+ break
3921
+
3922
+ values = wandb.util.downsample(
3923
+ item.values_float or item.values_int,
3924
+ 40,
3925
+ )
3926
+
3927
+ if sparkline := printer.sparklines(values):
3928
+ history_rows.append([item.key, sparkline])
3929
+
3930
+ if not history_rows:
3931
+ return None
3932
+
3933
+ if len(history_rows) < len(sorted_history_items):
3934
+ remaining = len(sorted_history_items) - len(history_rows)
3935
+ history_rows.append([f"+{remaining:,d}", "..."])
3936
+
3937
+ return printer.grid(history_rows, "Run history:")
3938
+
3939
+ @staticmethod
3940
+ def _footer_summary(
3941
+ summary: GetSummaryResponse,
3942
+ printer: printer.Printer,
3943
+ settings: Settings,
3944
+ ) -> str | None:
3945
+ """Returns the run summary formatted for printing to the console."""
3946
+ sorted_summary_items = sorted(
3947
+ (
3948
+ item
3949
+ for item in summary.item
3950
+ if not item.key.startswith("_") and not item.nested_key
3951
+ ),
3952
+ key=lambda item: item.key,
3953
+ )
3954
+
3955
+ summary_rows: list[list[str]] = []
3956
+ skipped = 0
3957
+ for item in sorted_summary_items:
3958
+ if len(summary_rows) >= settings.max_end_of_run_summary_metrics:
3959
+ break
3960
+
3961
+ try:
3962
+ value = json.loads(item.value_json)
3963
+ except json.JSONDecodeError:
3964
+ logger.exception(f"Error decoding summary[{item.key!r}]")
3965
+ skipped += 1
3966
+ continue
3967
+
3968
+ if isinstance(value, str):
3969
+ value = value[:20] + "..." * (len(value) >= 20)
3970
+ summary_rows.append([item.key, value])
3971
+ elif isinstance(value, numbers.Number):
3972
+ value = round(value, 5) if isinstance(value, float) else value
3973
+ summary_rows.append([item.key, str(value)])
3974
+ else:
3975
+ skipped += 1
3976
+
3977
+ if not summary_rows:
3978
+ return None
3979
+
3980
+ if len(summary_rows) < len(sorted_summary_items) - skipped:
3981
+ remaining = len(sorted_summary_items) - len(summary_rows) - skipped
3982
+ summary_rows.append([f"+{remaining:,d}", "..."])
3983
+
3984
+ return printer.grid(summary_rows, "Run summary:")
3985
+
3986
+ @staticmethod
3987
+ def _footer_internal_messages(
3988
+ internal_messages_response: InternalMessagesResponse | None = None,
3989
+ *,
3990
+ settings: Settings,
3991
+ printer: printer.Printer,
3992
+ ) -> None:
3993
+ if settings.quiet or settings.silent:
3994
+ return
3995
+
3996
+ if not internal_messages_response:
3997
+ return
3998
+
3999
+ for message in internal_messages_response.messages.warning:
4000
+ printer.display(message, level="warn")
4001
+
4002
+
4003
+ # We define this outside of the run context to support restoring before init
4004
+ def restore(
4005
+ name: str,
4006
+ run_path: str | None = None,
4007
+ replace: bool = False,
4008
+ root: str | None = None,
4009
+ ) -> None | TextIO:
4010
+ """Download the specified file from cloud storage.
4011
+
4012
+ File is placed into the current directory or run directory.
4013
+ By default, will only download the file if it doesn't already exist.
4014
+
4015
+ Args:
4016
+ name: The name of the file.
4017
+ run_path: Optional path to a run to pull files from, i.e. `username/project_name/run_id`
4018
+ if wandb.init has not been called, this is required.
4019
+ replace: Whether to download the file even if it already exists locally
4020
+ root: The directory to download the file to. Defaults to the current
4021
+ directory or the run directory if wandb.init was called.
4022
+
4023
+ Returns:
4024
+ None if it can't find the file, otherwise a file object open for reading.
4025
+
4026
+ Raises:
4027
+ CommError: If W&B can't connect to the W&B backend.
4028
+ ValueError: If the file is not found or can't find run_path.
4029
+ """
4030
+ is_disabled = wandb.run is not None and wandb.run.disabled
4031
+ run = None if is_disabled else wandb.run
4032
+ if run_path is None:
4033
+ if run is not None:
4034
+ run_path = run.path
4035
+ else:
4036
+ raise ValueError(
4037
+ "run_path required when calling wandb.restore before wandb.init"
4038
+ )
4039
+ if root is None:
4040
+ if run is not None:
4041
+ root = run.dir
4042
+ api = public.Api()
4043
+ api_run = api.run(run_path)
4044
+ if root is None:
4045
+ root = os.getcwd()
4046
+ path = os.path.join(root, name)
4047
+ if os.path.exists(path) and replace is False:
4048
+ return open(path)
4049
+ if is_disabled:
4050
+ return None
4051
+ files = api_run.files([name])
4052
+ if len(files) == 0:
4053
+ return None
4054
+ # if the file does not exist, the file has an md5 of 0
4055
+ if files[0].md5 == "0":
4056
+ raise ValueError(f"File {name} not found in {run_path or root}.")
4057
+ return files[0].download(root=root, replace=True)
4058
+
4059
+
4060
+ # propagate our doc string to the runs restore method
4061
+ try:
4062
+ Run.restore.__doc__ = restore.__doc__
4063
+ except AttributeError:
4064
+ pass
4065
+
4066
+
4067
+ def finish(
4068
+ exit_code: int | None = None,
4069
+ quiet: bool | None = None,
4070
+ ) -> None:
4071
+ """Finish a run and upload any remaining data.
4072
+
4073
+ Marks the completion of a W&B run and ensures all data is synced to the server.
4074
+ The run's final state is determined by its exit conditions and sync status.
4075
+
4076
+ Run States:
4077
+ - Running: Active run that is logging data and/or sending heartbeats.
4078
+ - Crashed: Run that stopped sending heartbeats unexpectedly.
4079
+ - Finished: Run completed successfully (`exit_code=0`) with all data synced.
4080
+ - Failed: Run completed with errors (`exit_code!=0`).
4081
+
4082
+ Args:
4083
+ exit_code: Integer indicating the run's exit status. Use 0 for success,
4084
+ any other value marks the run as failed.
4085
+ quiet: Deprecated. Configure logging verbosity using `wandb.Settings(quiet=...)`.
4086
+ """
4087
+ if wandb.run:
4088
+ wandb.run.finish(exit_code=exit_code, quiet=quiet)