Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5889] feat(client-python): Add model management Python API #6009

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clients/client-python/gravitino/api/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ def as_topic_catalog(self) -> "TopicCatalog":
"""
raise UnsupportedOperationException("Catalog does not support topic operations")

def as_model_catalog(self) -> "ModelCatalog":
"""
Returns:
the {@link ModelCatalog} if the catalog supports model operations.
Raises:
UnsupportedOperationException if the catalog does not support model operations.
"""
raise UnsupportedOperationException("Catalog does not support model operations")


class UnsupportedOperationException(Exception):
pass
74 changes: 74 additions & 0 deletions clients/client-python/gravitino/api/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from typing import Dict, Optional
from abc import abstractmethod

from gravitino.api.auditable import Auditable


class Model(Auditable):
"""An interface representing an ML model under a schema `Namespace`. A model is a metadata
object that represents the model artifact in ML. Users can register a model object in Gravitino
to manage the model metadata. The typical use case is to manage the model in ML lifecycle with a
unified way in Gravitino, and access the model artifact with a unified identifier. Also, with
the model registered in Gravitino, users can govern the model with Gravitino's unified audit,
tag, and role management.

The difference of Model and tabular data is that the model is schema-free, and the main
property of the model is the model artifact URL. The difference compared to the fileset is that
the model is versioned, and the model object contains the version information.
"""

@abstractmethod
def name(self) -> str:
"""
Returns:
Name of the model object.
"""
pass

@abstractmethod
def comment(self) -> Optional[str]:
"""The comment of the model object. This is the general description of the model object.
User can still add more detailed information in the model version.

Returns:
The comment of the model object. None is returned if no comment is set.
"""
pass

def properties(self) -> Dict[str, str]:
"""The properties of the model object. The properties are key-value pairs that can be used
to store additional information of the model object. The properties are optional.

Users can still specify the properties in the model version for different information.

Returns:
The properties of the model object. An empty dictionary is returned if no properties are set.
"""
pass

@abstractmethod
def latest_version(self) -> int:
"""The latest version of the model object. The latest version is the version number of the
latest model checkpoint / snapshot that is linked to the registered model.

Returns:
The latest version of the model object.
"""
pass
85 changes: 85 additions & 0 deletions clients/client-python/gravitino/api/model_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.


from abc import abstractmethod
from typing import Optional, Dict, List
from gravitino.api.auditable import Auditable


class ModelVersion(Auditable):
"""
An interface representing a single model checkpoint under a model `Model`. A model version
is a snapshot at a point of time of a model artifact in ML. Users can link a model version to a
registered model.
"""

@abstractmethod
def version(self) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to use string as version number for generic scenarios.
If this means an internal representation, maybe we want to call it something like revision.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current design, version is a self-incremented integer version number, in the meantime, we also have "aliases" for the convenience of use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

"""
The version of this model object. The version number is an integer number starts from 0. Each
time the model checkpoint / snapshot is linked to the registered, the version number will be
increased by 1.

Returns:
The version of the model object.
"""
pass

@abstractmethod
def comment(self) -> Optional[str]:
"""
The comment of this model version. This comment can be different from the comment of the model
to provide more detailed information about this version.

Returns:
The comment of the model version. None is returned if no comment is set.
"""
pass

@abstractmethod
def aliases(self) -> List[str]:
"""
The aliases of this model version. The aliases are the alternative names of the model version.
The aliases are optional. The aliases are unique for a model version. If the alias is already
set to one model version, it cannot be set to another model version.

Returns:
The aliases of the model version.
"""
pass

@abstractmethod
def uri(self) -> str:
"""
The URI of the model artifact. The URI is the location of the model artifact. The URI can be a
file path or a remote URI.

Returns:
The URI of the model artifact.
"""
pass

def properties(self) -> Dict[str, str]:
"""
The properties of the model version. The properties are key-value pairs that can be used to
store additional information of the model version. The properties are optional.

Returns:
The properties of the model version. An empty dictionary is returned if no properties are set.
"""
pass
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

from gravitino.api.catalog import Catalog
from gravitino.api.catalog_change import CatalogChange
from gravitino.catalog.fileset_catalog import FilesetCatalog
from gravitino.client.fileset_catalog import FilesetCatalog
from gravitino.client.generic_model_catalog import GenericModelCatalog
from gravitino.dto.catalog_dto import CatalogDTO
from gravitino.dto.requests.catalog_update_request import CatalogUpdateRequest
from gravitino.dto.requests.metalake_update_request import MetalakeUpdateRequest
Expand Down Expand Up @@ -64,6 +65,18 @@ def to_catalog(metalake: str, catalog: CatalogDTO, client: HTTPClient):
rest_client=client,
)

if catalog.type() == Catalog.Type.MODEL:
return GenericModelCatalog(
namespace=namespace,
name=catalog.name(),
catalog_type=catalog.type(),
provider=catalog.provider(),
comment=catalog.comment(),
properties=catalog.properties(),
audit=catalog.audit_info(),
rest_client=client,
)

raise NotImplementedError("Unsupported catalog type: " + str(catalog.type()))

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from gravitino.api.fileset import Fileset
from gravitino.api.fileset_change import FilesetChange
from gravitino.audit.caller_context import CallerContextHolder, CallerContext
from gravitino.catalog.base_schema_catalog import BaseSchemaCatalog
from gravitino.client.base_schema_catalog import BaseSchemaCatalog
from gravitino.client.generic_fileset import GenericFileset
from gravitino.dto.audit_dto import AuditDTO
from gravitino.dto.requests.fileset_create_request import FilesetCreateRequest
Expand Down Expand Up @@ -289,9 +289,9 @@ def check_fileset_name_identifier(ident: NameIdentifier):
)
FilesetCatalog.check_fileset_namespace(ident.namespace())

def _get_fileset_full_namespace(self, table_namespace: Namespace) -> Namespace:
def _get_fileset_full_namespace(self, fileset_namespace: Namespace) -> Namespace:
return Namespace.of(
self._catalog_namespace.level(0), self.name(), table_namespace.level(0)
self._catalog_namespace.level(0), self.name(), fileset_namespace.level(0)
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,32 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Optional

from gravitino.api.model import Model
from gravitino.dto.audit_dto import AuditDTO
from gravitino.dto.model_dto import ModelDTO


class GenericModel(Model):

_model_dto: ModelDTO
"""The model DTO object."""

def __init__(self, model_dto: ModelDTO):
self._model_dto = model_dto

def name(self) -> str:
return self._model_dto.name()

def comment(self) -> Optional[str]:
return self._model_dto.comment()

def properties(self) -> dict:
return self._model_dto.properties()

def latest_version(self) -> int:
return self._model_dto.latest_version()

def audit_info(self) -> AuditDTO:
return self._model_dto.audit_info()
Loading
Loading