3535 relationship ,
3636 selectinload ,
3737)
38+ from sqlalchemy .orm .attributes import instance_state
3839
3940from ai .backend .common .config import model_definition_iv
4041from ai .backend .common .types import (
@@ -779,14 +780,16 @@ def to_deployment_info(self) -> DeploymentInfo:
779780 if self .deployment_policy is not None :
780781 policy_data = self .deployment_policy .to_data ()
781782
782- # Try to use current revision if available
783- if self .current_revision and hasattr (self , "revisions" ) and self .revisions :
784- current_rev = next (
785- (r for r in self .revisions if r .id == self .current_revision ),
786- None ,
787- )
788- if current_rev :
789- info = self ._to_deployment_info_from_revision (current_rev )
783+ # Build model_revisions list from loaded revision rows
784+ if "revisions" in instance_state (self ).dict and self .revisions :
785+ model_revisions : list [ModelRevisionSpec ] = []
786+ for rev_row in self .revisions :
787+ if rev_row .image_row is None :
788+ continue
789+ if rev_row .id == self .current_revision or rev_row .id == self .deploying_revision :
790+ model_revisions .append (self ._build_revision_spec (rev_row ))
791+ if model_revisions :
792+ info = self ._to_deployment_info_with_revisions (model_revisions )
790793 info .policy = policy_data
791794 return info
792795
@@ -795,16 +798,44 @@ def to_deployment_info(self) -> DeploymentInfo:
795798 info .policy = policy_data
796799 return info
797800
798- def _to_deployment_info_from_revision (
801+ def _build_revision_spec (
799802 self ,
800803 revision : DeploymentRevisionRow ,
801- ) -> DeploymentInfo :
802- """Build DeploymentInfo using revision data."""
803- # Get image identifier from revision's image_row
804+ ) -> ModelRevisionSpec :
805+ """Build a ModelRevisionSpec from a revision row."""
804806 image_identifier = ImageIdentifier (
805807 canonical = revision .image_row .name ,
806808 architecture = revision .image_row .architecture ,
807809 )
810+ return ModelRevisionSpec (
811+ revision_id = revision .id ,
812+ image_identifier = image_identifier ,
813+ resource_spec = ResourceSpec (
814+ cluster_mode = ClusterMode (revision .cluster_mode ),
815+ cluster_size = revision .cluster_size ,
816+ resource_slots = revision .resource_slots ,
817+ resource_opts = revision .resource_opts ,
818+ ),
819+ mounts = MountMetadata (
820+ model_vfolder_id = revision .model or uuid .UUID (int = 0 ),
821+ model_definition_path = revision .model_definition_path ,
822+ model_mount_destination = revision .model_mount_destination ,
823+ extra_mounts = revision .extra_mounts or [],
824+ ),
825+ execution = ExecutionSpec (
826+ startup_command = revision .startup_command ,
827+ bootstrap_script = revision .bootstrap_script ,
828+ environ = revision .environ ,
829+ runtime_variant = revision .runtime_variant ,
830+ callback_url = yarl .URL (revision .callback_url ) if revision .callback_url else None ,
831+ ),
832+ )
833+
834+ def _to_deployment_info_with_revisions (
835+ self ,
836+ model_revisions : Sequence [ModelRevisionSpec ],
837+ ) -> DeploymentInfo :
838+ """Build DeploymentInfo with pre-built model_revisions dict."""
808839 return DeploymentInfo (
809840 id = self .id ,
810841 metadata = DeploymentMetadata (
@@ -830,32 +861,7 @@ def _to_deployment_info_from_revision(
830861 open_to_public = self .open_to_public if self .open_to_public is not None else False ,
831862 url = self .url ,
832863 ),
833- model_revisions = [
834- ModelRevisionSpec (
835- image_identifier = image_identifier ,
836- resource_spec = ResourceSpec (
837- cluster_mode = ClusterMode (revision .cluster_mode ),
838- cluster_size = revision .cluster_size ,
839- resource_slots = revision .resource_slots ,
840- resource_opts = revision .resource_opts ,
841- ),
842- mounts = MountMetadata (
843- model_vfolder_id = revision .model or uuid .UUID (int = 0 ),
844- model_definition_path = revision .model_definition_path ,
845- model_mount_destination = revision .model_mount_destination ,
846- extra_mounts = revision .extra_mounts or [],
847- ),
848- execution = ExecutionSpec (
849- startup_command = revision .startup_command ,
850- bootstrap_script = revision .bootstrap_script ,
851- environ = revision .environ ,
852- runtime_variant = revision .runtime_variant ,
853- callback_url = yarl .URL (revision .callback_url )
854- if revision .callback_url
855- else None ,
856- ),
857- ),
858- ],
864+ model_revisions = list (model_revisions ),
859865 current_revision_id = self .current_revision ,
860866 deploying_revision_id = self .deploying_revision ,
861867 sub_step = self .sub_step ,
0 commit comments