|
13 | 13 | # permissions and limitations under the License. |
14 | 14 | """Utility functions to handle metadata for ZenML entities.""" |
15 | 15 |
|
16 | | -from typing import Dict, List, Optional, Union, overload |
| 16 | +from typing import Dict, List, Optional, Set, Union, overload |
17 | 17 | from uuid import UUID |
18 | 18 |
|
19 | 19 | from zenml.client import Client |
20 | 20 | from zenml.enums import MetadataResourceTypes, ModelStages |
21 | 21 | from zenml.logger import get_logger |
22 | 22 | from zenml.metadata.metadata_types import MetadataType |
23 | | -from zenml.models import RunMetadataResource |
| 23 | +from zenml.models import ( |
| 24 | + PipelineRunIdentifier, |
| 25 | + RunMetadataResource, |
| 26 | + StepRunIdentifier, |
| 27 | + VersionedIdentifier, |
| 28 | +) |
24 | 29 | from zenml.steps.step_context import get_step_context |
25 | 30 |
|
26 | 31 | logger = get_logger(__name__) |
@@ -366,3 +371,161 @@ def log_metadata( |
366 | 371 | resources=resources, |
367 | 372 | publisher_step_id=publisher_step_id, |
368 | 373 | ) |
| 374 | + |
| 375 | + |
| 376 | +def bulk_log_metadata( |
| 377 | + metadata: Dict[str, MetadataType], |
| 378 | + pipeline_runs: list[PipelineRunIdentifier] | None = None, |
| 379 | + step_runs: list[StepRunIdentifier] | None = None, |
| 380 | + artifact_versions: list[VersionedIdentifier] | None = None, |
| 381 | + model_versions: list[VersionedIdentifier] | None = None, |
| 382 | + infer_models: bool = False, |
| 383 | + infer_artifacts: bool = False, |
| 384 | +) -> None: |
| 385 | + """Logs metadata for multiple entities in a single invocation. |
| 386 | +
|
| 387 | + Args: |
| 388 | + metadata: The metadata to log. |
| 389 | + pipeline_runs: A list of pipeline runs to log metadata for. |
| 390 | + step_runs: A list of step runs to log metadata for. |
| 391 | + artifact_versions: A list of artifact versions to log metadata for. |
| 392 | + model_versions: A list of model versions to log metadata for. |
| 393 | + infer_models: Flag - when enabled infer model to log metadata for from step context. |
| 394 | + infer_artifacts: Flag - when enabled infer artifact to log metadata for from step context. |
| 395 | +
|
| 396 | + Raises: |
| 397 | + ValueError: If options are not passed correctly (infer options with explicit declarations) or |
| 398 | + invocation with `infer` options is done outside of a step context. |
| 399 | + """ |
| 400 | + client = Client() |
| 401 | + |
| 402 | + resources: Set[RunMetadataResource] = set() |
| 403 | + |
| 404 | + if not metadata: |
| 405 | + raise ValueError("You must provide metadata to log.") |
| 406 | + |
| 407 | + if not any( |
| 408 | + bool(v) |
| 409 | + for v in [ |
| 410 | + pipeline_runs, |
| 411 | + step_runs, |
| 412 | + artifact_versions, |
| 413 | + model_versions, |
| 414 | + infer_models, |
| 415 | + infer_artifacts, |
| 416 | + ] |
| 417 | + ): |
| 418 | + raise ValueError( |
| 419 | + "You must select at least one pipeline/step/artifact/model to log metadata to." |
| 420 | + ) |
| 421 | + |
| 422 | + if infer_models and model_versions: |
| 423 | + raise ValueError( |
| 424 | + "You can either specify model versions or use the infer option." |
| 425 | + ) |
| 426 | + |
| 427 | + if infer_artifacts and artifact_versions: |
| 428 | + raise ValueError( |
| 429 | + "You can either specify artifact versions or use the infer option." |
| 430 | + ) |
| 431 | + |
| 432 | + try: |
| 433 | + step_context = get_step_context() |
| 434 | + except RuntimeError: |
| 435 | + step_context = None |
| 436 | + |
| 437 | + if (infer_models or infer_artifacts) and step_context is None: |
| 438 | + raise ValueError( |
| 439 | + "Infer options can be used only within a step function code." |
| 440 | + ) |
| 441 | + |
| 442 | + # resolve pipeline runs and add metadata resources |
| 443 | + |
| 444 | + for pipeline in pipeline_runs or []: |
| 445 | + if not pipeline.id: |
| 446 | + pipeline.id = client.get_pipeline_run( |
| 447 | + name_id_or_prefix=pipeline.value |
| 448 | + ).id |
| 449 | + resources.add( |
| 450 | + RunMetadataResource( |
| 451 | + id=pipeline.id, type=MetadataResourceTypes.PIPELINE_RUN |
| 452 | + ) |
| 453 | + ) |
| 454 | + |
| 455 | + # resolve step runs and add metadata resources |
| 456 | + |
| 457 | + for step in step_runs or []: |
| 458 | + if not step.id: |
| 459 | + step.id = ( |
| 460 | + client.get_pipeline_run(name_id_or_prefix=step.pipeline.value) |
| 461 | + .steps[step.name] |
| 462 | + .id |
| 463 | + ) |
| 464 | + |
| 465 | + resources.add( |
| 466 | + RunMetadataResource( |
| 467 | + id=step.id, type=MetadataResourceTypes.STEP_RUN |
| 468 | + ) |
| 469 | + ) |
| 470 | + |
| 471 | + # resolve artifacts and add metadata resources |
| 472 | + |
| 473 | + for artifact_version in artifact_versions or []: |
| 474 | + if not artifact_version.id: |
| 475 | + artifact_version.id = client.get_artifact_version( |
| 476 | + name_id_or_prefix=artifact_version.name, |
| 477 | + version=artifact_version.version, |
| 478 | + ).id |
| 479 | + resources.add( |
| 480 | + RunMetadataResource( |
| 481 | + id=artifact_version.id, |
| 482 | + type=MetadataResourceTypes.ARTIFACT_VERSION, |
| 483 | + ) |
| 484 | + ) |
| 485 | + |
| 486 | + # resolve models and add metadata resources |
| 487 | + |
| 488 | + for model_version in model_versions or []: |
| 489 | + if not model_version.id: |
| 490 | + model_version.id = client.get_model_version( |
| 491 | + model_name_or_id=model_version.name, |
| 492 | + model_version_name_or_number_or_id=model_version.version, |
| 493 | + ).id |
| 494 | + resources.add( |
| 495 | + RunMetadataResource( |
| 496 | + id=model_version.id, type=MetadataResourceTypes.MODEL_VERSION |
| 497 | + ) |
| 498 | + ) |
| 499 | + |
| 500 | + # infer models - resolve from step context |
| 501 | + |
| 502 | + if infer_models and not step_context.model_version: |
| 503 | + raise ValueError( |
| 504 | + "The step context does not feature any model versions." |
| 505 | + ) |
| 506 | + elif infer_models: |
| 507 | + resources.add( |
| 508 | + RunMetadataResource( |
| 509 | + id=step_context.model_version.id, |
| 510 | + type=MetadataResourceTypes.MODEL_VERSION, |
| 511 | + ) |
| 512 | + ) |
| 513 | + |
| 514 | + # infer artifacts - resolve from step context |
| 515 | + |
| 516 | + if infer_artifacts: |
| 517 | + step_output_names = list(step_context._outputs.keys()) |
| 518 | + |
| 519 | + for artifact_name in step_output_names: |
| 520 | + step_context.add_output_metadata( |
| 521 | + metadata=metadata, output_name=artifact_name |
| 522 | + ) |
| 523 | + |
| 524 | + if not resources: |
| 525 | + return |
| 526 | + |
| 527 | + client.create_run_metadata( |
| 528 | + metadata=metadata, |
| 529 | + resources=list(resources), |
| 530 | + publisher_step_id=None, |
| 531 | + ) |
0 commit comments