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

Validate string values passed for enum arguments #10

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright (c) 2022-present, FriendliAI Inc. All rights reserved.
-->

<p align="center">
<img src="https://drive.google.com/uc?id=1daafphR0ABKylj_0b1wtZ6n0ZRSVq5-s" width="80%" alt="system">
<img src="https://docs.periflow.ai/img/logo.svg" width="80%" alt="system">
</p>

<h2><p align="center">Supercharge Generative AI Serving 🚀</p></h2>
Expand Down
16 changes: 6 additions & 10 deletions docs/docs/sdk/resource/checkpoint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pf.init(
# Create a checkpoint by linking an existing S3 bucket.
checkpoint = pf.Checkpoint.create(
name="my-checkpoint",
credential_id=UUID("8ab9b5cf-8737-4d1b-873a-f4ef36a57cf1"),
cloud_stroage=StorageType.S3,
credential_id="YOUR_CREDENTIAL_ID",
cloud_stroage="s3",
region="us-east-1",
storage_name="my-bucket",
storage_path="path/to/ckpt",
Expand Down Expand Up @@ -290,9 +290,7 @@ pf.init(
project_name="my-project",
)

checkpoint = pf.Checkpoint.get(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63)
)
checkpoint = pf.Checkpoint.get(id="YOUR_CHECKPOINT_ID")
```

### _def_ `delete`
Expand Down Expand Up @@ -324,9 +322,7 @@ pf.init(
project_name="my-project",
)

checkpoint = pf.Checkpoint.delete(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63)
)
checkpoint = pf.Checkpoint.delete(id="YOUR_CHECKPOINT_ID")
```

### _def_ `upload`
Expand Down Expand Up @@ -546,7 +542,7 @@ pf.init(
)

pf.Checkpoint.download(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63),
id="YOUR_CHECKPOINT_ID",
save_dir="local/save/dir",
)
```
Expand Down Expand Up @@ -592,7 +588,7 @@ pf.init(
)

pf.Checkpoint.download(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63),
id="YOUR_CHECKPOINT_ID",
save_dir="local/save/dir",
)
```
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/sdk/resource/deployment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ config = {
"max_token_count": 8146,
}
deployment = pf.Deployment.create(
checkpoint_id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63),
checkpoint_id="YOUR_CHECKPOINT_ID",
name="my-deployment",
cloud=CloudType.GCP,
cloud="gcp",
region="asia-northeast3",
vm_type="a2-highgpu-1g",
config=config,
Expand Down
2 changes: 1 addition & 1 deletion periflow/configurator/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ def validation_schema(self) -> dict:
"max_token_count": {"type": "integer"},
},
},
"required": ["orca_config"],
},
"required": ["orca_config"],
}
20 changes: 10 additions & 10 deletions periflow/sdk/resource/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from periflow.utils.validate import (
validate_checkpoint_attributes,
validate_cloud_storage_type,
validate_enums,
validate_storage_region,
)

Expand Down Expand Up @@ -93,8 +94,8 @@ def create(
# Create a checkpoint by linking an existing S3 bucket.
checkpoint = pf.Checkpoint.create(
name="my-checkpoint",
credential_id=UUID("8ab9b5cf-8737-4d1b-873a-f4ef36a57cf1"),
cloud_stroage=StorageType.S3,
credential_id="YOUR_CREDENTIAL_ID",
cloud_stroage="s3",
region="us-east-1",
storage_name="my-bucket",
storage_path="path/to/ckpt",
Expand Down Expand Up @@ -229,6 +230,7 @@ def create(
:::

"""
cloud_storage = validate_enums(cloud_storage, StorageType)
validate_cloud_storage_type(cloud_storage)
validate_storage_region(vendor=cloud_storage, region=region)

Expand Down Expand Up @@ -358,9 +360,7 @@ def get(id: UUID, *args, **kwargs) -> V1Checkpoint:
project_name="my-project",
)

checkpoint = pf.Checkpoint.get(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63)
)
checkpoint = pf.Checkpoint.get(id="YOUR_CHECKPOINT_ID")
```

"""
Expand Down Expand Up @@ -391,9 +391,7 @@ def delete(id: UUID) -> None:
project_name="my-project",
)

checkpoint = pf.Checkpoint.delete(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63)
)
checkpoint = pf.Checkpoint.delete(id="YOUR_CHECKPOINT_ID")
```

"""
Expand Down Expand Up @@ -720,7 +718,7 @@ def download(id: UUID, save_dir: Optional[str] = None) -> None:
)

pf.Checkpoint.download(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63),
id="YOUR_CHECKPOINT_ID",
save_dir="local/save/dir",
)
```
Expand Down Expand Up @@ -769,7 +767,7 @@ def restore(id: UUID) -> V1Checkpoint:
)

pf.Checkpoint.download(
id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63),
id="YOUR_CHECKPOINT_ID",
save_dir="local/save/dir",
)
```
Expand Down Expand Up @@ -842,6 +840,8 @@ def convert(
"To convert the checkpoint, your must install the package with 'pip install periflow-client[mllib]'"
) from exc

data_type = validate_enums(data_type, CheckpointDataType)

try:
config = transformers.AutoConfig.from_pretrained(
model_name_or_path, cache_dir=cache_dir, trust_remote_code=True
Expand Down
13 changes: 10 additions & 3 deletions periflow/sdk/resource/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from periflow.utils.format import extract_datetime_part, extract_deployment_id_part
from periflow.utils.fs import download_file, upload_file
from periflow.utils.maps import cloud_vm_map, vm_num_gpu_map
from periflow.utils.validate import validate_enums


class Deployment(ResourceAPI[V1Deployment, str]):
Expand Down Expand Up @@ -111,9 +112,9 @@ def create(
"max_token_count": 8146,
}
deployment = pf.Deployment.create(
checkpoint_id=UUID(190c117a-30ef-4c33-aad7-16f21bca0d63),
checkpoint_id="YOUR_CHECKPOINT_ID",
name="my-deployment",
cloud=CloudType.GCP,
cloud="gcp",
region="asia-northeast3",
vm_type="a2-highgpu-1g",
config=config,
Expand Down Expand Up @@ -154,6 +155,12 @@ def create(
V1Deployment: The created deployment object.

"""
# pylint: disable=too-many-statements
cloud = validate_enums(cloud, CloudType)
vm_type = validate_enums(vm_type, VMType)
deployment_type = validate_enums(deployment_type, DeploymentType)
security_level = validate_enums(security_level, DeploymentSecurityLevel)

org_id = get_current_group_id()
if org_id is None:
raise AuthenticationError(
Expand Down Expand Up @@ -254,7 +261,7 @@ def create(
"infrequest_log": logging,
**config,
}
if description:
if description is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any reason to change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change considers the case when the value of description is "" (empty string).

request_data["description"] = description

client = DeploymentClient()
Expand Down
16 changes: 14 additions & 2 deletions periflow/utils/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from __future__ import annotations

from datetime import datetime
from typing import Any, Dict, Optional
from enum import Enum
from typing import Any, Dict, Optional, Type

import typer
from pydantic import ValidationError
Expand Down Expand Up @@ -63,7 +64,7 @@ def validate_datetime_format(datetime_str: Optional[str]) -> Optional[str]:

def validate_cloud_storage_type(val: StorageType) -> None:
"""Validate the cloud storage type."""
if val is StorageType.FAI:
if val == StorageType.FAI:
raise NotSupportedError(
"Checkpoint creation with FAI storage is not supported now."
)
Expand Down Expand Up @@ -101,3 +102,14 @@ def validate_checkpoint_attributes(attr: Dict[str, Any]) -> None:
else:
msg = "\n>>> " + "\n>>> ".join(msgs)
raise InvalidAttributesError(msg) from exc


def validate_enums(val: Any, enum_cls: Type[Enum]) -> Any:
"""Validate if the value is the proper enum."""
try:
return enum_cls(val)
except ValueError as exc:
supported_values = set([e.value for e in enum_cls])
raise InvalidConfigError(
f"Invalid value. Please provide one of {supported_values}"
) from exc
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "periflow-client"
version = "0.1.3"
version = "0.1.4"
description = "Client of PeriFlow, the fastest generative AI serving available."
license = "Apache-2.0"
authors = ["PeriFlow teams <eng@friendli.ai>"]
Expand Down