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

Peft docker fix #3751

Merged
merged 3 commits into from
Dec 13, 2024
Merged
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
55 changes: 51 additions & 4 deletions community-content/vertex_model_garden/model_oss/util/commons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Common utility lib for prediction on images."""

from typing import Any, Dict, List
from typing import Any, Dict, List, Tuple

import numpy as np
from PIL import Image
Expand All @@ -10,6 +10,28 @@
from util import image_format_converter


def convert_list_to_label_map(
input_list: List[str],
) -> Tuple[Dict[str, Dict[int, str]], List[int]]:
"""Converts a list of labels to a dictionary and numerical encoding.

Args:
input_list: A list of strings representing class labels.

Returns:
A tuple containing:
label_map: A dictionary mapping unique labels to integer indices.
encoded_list: A list of integers corresponding to the labels in the input
list.
"""
unique_labels = set(input_list)
label_map_reverse = {label: idx for idx, label in enumerate(unique_labels)}
label_map = {idx: label for idx, label in enumerate(unique_labels)}
encoded_list = [label_map_reverse[label] for label in input_list]

return {"label_map": label_map}, encoded_list


def get_prediction_instances(image: Image.Image) -> List[Dict[str, Any]]:
"""Gets prediction instances.

Expand Down Expand Up @@ -40,24 +62,25 @@ def get_label_map(label_map_yaml_filepath: str) -> Dict[str, Any]:


def get_object_detection_endpoint_predictions(
detection_endpoint: ...,
detector_endpoint: ...,
input_image: np.ndarray,
detection_thresh: float = 0.2,
) -> np.ndarray:
"""Gets endpoint predictions.

Args:
detection_endpoint: image object detection endpoint.
detector_endpoint: image object detection endpoint.
input_image: Input image.
detection_thresh: Detection threshold.

Returns:
Object detection predictions from endpoints.
"""
height, width, _ = input_image.shape
predictions = detection_endpoint.predict(
predictions = detector_endpoint.predict(
get_prediction_instances(Image.fromarray(input_image))
).predictions

detection_scores = np.array(predictions[0]["detection_scores"])
detection_classes = np.array(predictions[0]["detection_classes"])
detection_boxes = np.array(
Expand All @@ -66,6 +89,29 @@ def get_object_detection_endpoint_predictions(
for b in predictions[0]["detection_boxes"]
]
)
return merge_boxes_and_classes(
detection_scores, detection_boxes, detection_classes, detection_thresh
)


def merge_boxes_and_classes(
detection_scores: np.ndarray,
detection_boxes: np.ndarray,
detection_classes: np.ndarray,
detection_thresh: float = 0.2,
) -> np.ndarray:
"""Merges prediction boxes and classes.

Args:
detection_scores: array of detection scores.
detection_boxes: array of detection boxes.
detection_classes: array of detection classes.
detection_thresh: float indicating the detection threshold.

Returns:
preds_merge_cls: a numpy array containing the detection boxes, scores and
classes.
"""
thresh_indices = [
x for x, val in enumerate(detection_scores) if val > detection_thresh
]
Expand All @@ -76,4 +122,5 @@ def get_object_detection_endpoint_predictions(
preds_merge_cls = np.column_stack(
(preds_merge_conf, detection_classes[thresh_indices])
)

return preds_merge_cls
84 changes: 71 additions & 13 deletions community-content/vertex_model_garden/model_oss/util/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@

# Reported hyperparameter tuning metric tag.
HP_METRIC_TAG = 'model_performance'
HP_LOSS_TAG = 'model_loss'

# Reported places.
REPORT_TO_NONE = 'none'
REPORT_TO_WANDB = 'wandb'
REPORT_TO_TENSORBOARD = 'tensorboard'

# HPT trial prefix.
TRIAL_PREFIX = 'trial_'
Expand All @@ -45,7 +51,7 @@
ML_USE_VALIDATION = 'validation'
ML_USE_TEST = 'test'

# COCO json keys
# COCO json keys.
COCO_JSON_ANNOTATIONS = 'annotations'
COCO_JSON_ANNOTATION_IMAGE_ID = 'image_id'
COCO_JSON_ANNOTATION_CATEGORY_ID = 'category_id'
Expand All @@ -60,36 +66,88 @@
COCO_JSON_IMAGE_COCO_URL = 'coco_url'
COCO_ANNOTATION_BBOX = 'bbox'

# GCS prefixes
# GCS prefixes.
GCS_URI_PREFIX = 'gs://'
GCSFUSE_URI_PREFIX = '/gcs/'

LOCAL_EVALUATION_RESULT_DIR = '/tmp/evaluation_result_dir'
LOCAL_MODEL_DIR = '/tmp/model_dir'
LOCAL_LORA_DIR = '/tmp/lora_dir'
LOCAL_BASE_MODEL_DIR = '/tmp/base_model_dir'
LOCAL_DATA_DIR = '/tmp/data'
LOCAL_OUTPUT_DIR = '/tmp/output_dir'
LOCAL_PREDICTION_RESULT_DIR = '/tmp/prediction_result_dir'
SHARED_MEM_DIR = '/dev/shm'

# Huggingface files.
HF_MODEL_WEIGHTS_SUFFIX = '.bin'

# PEFT finetuning constants.
TEXT_TO_IMAGE = 'text-to-image'
TEXT_TO_IMAGE_LORA = 'text-to-image-lora'
TEXT_TO_IMAGE_DREAMBOOTH = 'text-to-image-dreambooth'
TEXT_TO_IMAGE_DREAMBOOTH_LORA = 'text-to-image-dreambooth-lora'
TEXT_TO_IMAGE_DREAMBOOTH_LORA_SDXL = 'text-to-image-dreambooth-lora-sdxl'
SEQUENCE_CLASSIFICATION_LORA = 'sequence-classification-lora'
CAUSAL_LANGUAGE_MODELING_LORA = 'causal-language-modeling-lora'
MERGE_CAUSAL_LANGUAGE_MODEL_LORA = 'merge-causal-language-model-lora'
QUANTIZE_MODEL = 'quantize-model'
INSTRUCT_LORA = 'instruct-lora'
CAUSAL_LANGUAGE_MODELING_LORA_TARGET_MODULES = [
"q_proj",
"v_proj",
]
INSTRUCT_LORA_TARGET_MODULES = [
"query_key_value",
"dense",
"dense_h_to_4h",
"dense_4h_to_h",
]
VALIDATE_DATASET_WITH_TEMPLATE = 'validate-dataset-with-template'
DEFAULT_TEXT_COLUMN_IN_DATASET = 'quote'
DEFAULT_TEXT_COLUMN_IN_QUANTIZATION_DATASET = 'text'
DEFAULT_INSTRUCT_COLUMN_IN_DATASET = 'text'

FINAL_CHECKPOINT_DIRNAME = 'checkpoint-final'

# ImageBind inference constants.
FEATURE_EMBEDDING_GENERATION = 'feature-embedding-generation'
ZERO_SHOT_CLASSIFICATION = 'zero-shot-classification'

# Precision modes for loading model weights.
PRECISION_MODE_2 = '2bit'
PRECISION_MODE_3 = '3bit'
PRECISION_MODE_4 = '4bit'
PRECISION_MODE_8 = '8bit'
PRECISION_MODE_FP8 = 'float8' # to use fbgemm_fp8 quantization
PRECISION_MODE_16 = 'float16'
PRECISION_MODE_16B = 'bfloat16'
PRECISION_MODE_32 = 'float32'

# Quantization modes.
GPTQ = 'gptq'
AWQ = 'awq'

# AWQ versions.
GEMM = 'GEMM'
GEMV = 'GEMV'

# Environment variable keys.
PRIVATE_BUCKET_ENV_KEY = 'AIP_PRIVATE_BUCKET_NAME'

# Kfp pipeline constants.
TFVISION_TRAIN_OUTPUT_ARTIFACT_NAME = 'checkpoint_dir'

# Vertex IOD type.
AUTOML = 'AUTOML'
MODEL_GARDEN = 'MODEL_GARDEN'

# LRU Disk Cache constants.
MD5_HASHMAP_FILENAME = 'md5_hashmap.json'

# Prediction request keys.
PREDICT_INSTANCE_KEY = 'instances'
PREDICT_INSTANCE_IMAGE_KEY = 'image'
PREDICT_INSTANCE_POSE_IMAGE_KEY = 'pose_image'
PREDICT_INSTANCE_TEXT_KEY = 'text'
PREDICT_INSTANCE_PROMPT_KEY = 'prompt'

PREDICT_PARAMETERS_KEY = 'parameters'
PREDICT_PARAMETERS_NUM_INFERENCE_STEPS_KEY = 'num_inference_steps'
PREDICT_PARAMETERS_HEIGHT_KEY = 'height'
PREDICT_PARAMETERS_WIDTH_KEY = 'width'
PREDICT_PARAMETERS_GUIDANCE_SCALE_KEY = 'guidance_scale'
PREDICT_PARAMETERS_NEGATIVE_PROMPT_KEY = 'negative_prompt'
PREDICT_PARAMETERS_LORA_ID_KEY = 'lora_id'
PREDICT_PARAMETERS_IGNORE_LORA_CACHE_KEY = 'ignore_lora_cache'

PREDICT_OUTPUT_KEY = 'output'
Loading
Loading