| """Amazon S3 utilities for SageMaker jobs.""" |
| from __future__ import annotations |
|
|
| import logging |
| import os |
| import shutil |
| from pathlib import Path |
| from typing import TYPE_CHECKING, Tuple |
|
|
| import boto3 |
| from botocore.config import Config |
|
|
| if TYPE_CHECKING: |
| from datasets import Dataset |
|
|
| LOGGER = logging.getLogger(__name__) |
|
|
|
|
| def get_s3_client(): |
| """Get S3 client with retry configuration.""" |
| config = Config( |
| retries={"max_attempts": 3, "mode": "standard"}, |
| max_pool_connections=50, |
| ) |
| return boto3.client("s3", config=config) |
|
|
|
|
| def parse_s3_uri(uri: str) -> Tuple[str, str]: |
| """Parse s3://bucket/key into (bucket, key).""" |
| if not uri.startswith("s3://"): |
| raise ValueError(f"Invalid S3 URI: {uri}") |
| parts = uri[5:].split("/", 1) |
| bucket = parts[0] |
| key = parts[1] if len(parts) > 1 else "" |
| return bucket, key |
|
|
|
|
| def upload_files_to_s3( |
| *, |
| output_dir: Path, |
| s3_uri: str, |
| path_prefix: str = "", |
| ) -> None: |
| """Upload local files to S3. |
| |
| Args: |
| output_dir: Local directory containing files to upload |
| s3_uri: S3 URI (s3://bucket/prefix) |
| path_prefix: Additional prefix to add to S3 keys |
| """ |
| if not s3_uri: |
| LOGGER.info("No S3 URI provided; skipping upload.") |
| return |
|
|
| bucket, base_prefix = parse_s3_uri(s3_uri) |
| |
| full_prefix = base_prefix.rstrip("/") |
| if path_prefix: |
| full_prefix = f"{full_prefix}/{path_prefix.strip('/')}" if full_prefix else path_prefix.strip("/") |
|
|
| s3 = get_s3_client() |
| base = output_dir.resolve() |
| |
| files = sorted(p for p in base.rglob("*") if p.is_file()) |
| if not files: |
| LOGGER.info("Nothing to upload from %s", output_dir) |
| return |
|
|
| LOGGER.info("Uploading %d files to s3://%s/%s", len(files), bucket, full_prefix) |
| |
| for local_path in files: |
| rel = local_path.relative_to(base).as_posix() |
| s3_key = f"{full_prefix}/{rel}" if full_prefix else rel |
| try: |
| s3.upload_file(str(local_path), bucket, s3_key) |
| except Exception as exc: |
| LOGGER.error("Failed to upload %s to s3://%s/%s: %s", local_path, bucket, s3_key, exc) |
| raise |
|
|
|
|
| def save_dataset_to_s3( |
| dataset, |
| s3_uri: str, |
| name: str = "dataset", |
| ) -> str: |
| """Save HF dataset as Parquet to S3. |
| |
| Args: |
| dataset: HuggingFace Dataset or DatasetDict to save |
| s3_uri: Base S3 URI (s3://bucket/prefix) |
| name: Name for the parquet file |
| |
| Returns: |
| S3 URI of the saved parquet file |
| """ |
| from datasets import DatasetDict |
| |
| |
| if isinstance(dataset, DatasetDict): |
| if "train" in dataset: |
| dataset = dataset["train"] |
| else: |
| |
| split_name = list(dataset.keys())[0] |
| dataset = dataset[split_name] |
| LOGGER.info("Using split '%s' from DatasetDict", split_name) |
| |
| bucket, prefix = parse_s3_uri(s3_uri) |
| full_prefix = prefix.rstrip("/") |
| |
| |
| local_dir = Path(f"/tmp/{name}_parquet_temp") |
| if local_dir.exists(): |
| shutil.rmtree(local_dir) |
| local_dir.mkdir(parents=True) |
| |
| parquet_path = local_dir / f"{name}.parquet" |
| dataset.to_parquet(str(parquet_path)) |
| |
| |
| s3_key = f"{full_prefix}/{name}.parquet" if full_prefix else f"{name}.parquet" |
| s3 = get_s3_client() |
| s3.upload_file(str(parquet_path), bucket, s3_key) |
| |
| |
| shutil.rmtree(local_dir) |
| |
| result_uri = f"s3://{bucket}/{s3_key}" |
| LOGGER.info("Saved dataset to %s", result_uri) |
| return result_uri |
|
|
|
|
| def load_dataset_from_s3(s3_uri: str, split: str = "train") -> "Dataset": |
| """Load HF dataset from S3 Parquet file. |
| |
| Downloads the parquet file(s) locally first, then loads with datasets library. |
| This avoids s3fs/aiobotocore version compatibility issues. |
| |
| Image columns are loaded without decoding to avoid issues with paths |
| that don't exist on this machine. |
| |
| Args: |
| s3_uri: S3 URI to parquet file or directory (s3://bucket/path/dataset.parquet) |
| split: Split name to assign |
| |
| Returns: |
| Loaded Dataset |
| """ |
| import pandas as pd |
| from datasets import Dataset |
| |
| bucket, prefix = parse_s3_uri(s3_uri) |
| s3 = get_s3_client() |
| |
| |
| local_dir = Path(f"/tmp/s3_dataset_{prefix.replace('/', '_')}") |
| if local_dir.exists(): |
| shutil.rmtree(local_dir) |
| local_dir.mkdir(parents=True) |
| |
| |
| paginator = s3.get_paginator("list_objects_v2") |
| parquet_files = [] |
| |
| for page in paginator.paginate(Bucket=bucket, Prefix=prefix.rstrip("/")): |
| for obj in page.get("Contents", []): |
| key = obj["Key"] |
| if key.endswith(".parquet"): |
| |
| filename = Path(key).name |
| local_path = local_dir / filename |
| LOGGER.info("Downloading s3://%s/%s", bucket, key) |
| s3.download_file(bucket, key, str(local_path)) |
| parquet_files.append(str(local_path)) |
| |
| if not parquet_files: |
| raise FileNotFoundError(f"No parquet files found at {s3_uri}") |
| |
| LOGGER.info("Loading %d parquet file(s) from %s", len(parquet_files), local_dir) |
| |
| |
| |
| dfs = [pd.read_parquet(f) for f in parquet_files] |
| df = pd.concat(dfs, ignore_index=True) if len(dfs) > 1 else dfs[0] |
| |
| return Dataset.from_pandas(df) |
|
|
|
|
| def get_sagemaker_output_dir() -> Path: |
| """Get the SageMaker output directory from environment.""" |
| return Path(os.environ.get("SM_OUTPUT_DATA_DIR", "/opt/ml/output/data")) |
|
|
|
|
| def get_sagemaker_input_dir() -> Path: |
| """Get the SageMaker input directory from environment.""" |
| return Path(os.environ.get("SM_INPUT_DIR", "/opt/ml/input")) |
|
|
|
|
| def get_sagemaker_model_dir() -> Path: |
| """Get the SageMaker model directory from environment.""" |
| return Path(os.environ.get("SM_MODEL_DIR", "/opt/ml/model")) |
|
|
|
|
| __all__ = [ |
| "upload_files_to_s3", |
| "save_dataset_to_s3", |
| "load_dataset_from_s3", |
| "parse_s3_uri", |
| "get_s3_client", |
| "get_sagemaker_output_dir", |
| "get_sagemaker_input_dir", |
| "get_sagemaker_model_dir", |
| ] |
|
|