This guide explains how to integrate the new artifact storage system into your existing AstroML workflows.
-
astroml/storage/artifact_store.py- Core artifact store implementationsArtifactStore- Abstract base classLocalArtifactStore- Local filesystem storageS3ArtifactStore- AWS S3 storageGCSArtifactStore- Google Cloud Storagecreate_artifact_store()- Factory function
-
astroml/storage/config.py- Configuration classesArtifactStorageConfig- Main configurationLocalStorageConfig- Local storage settingsS3StorageConfig- S3 settingsGCSStorageConfig- GCS settings
-
astroml/storage/__init__.py- Module exports
-
astroml/tracking/mlflow_tracker.py- Enhanced with artifact store support- New parameters:
artifact_uri,artifact_store - New methods:
save_artifact(),load_artifact() log_model_artifact()now returns artifact URI
- New parameters:
-
astroml/training/config.py- Added artifact storage configuration- New field:
artifact_storage: ArtifactStorageConfig
- New field:
configs/artifact_storage/local.yaml- Local storage configconfigs/artifact_storage/s3.yaml- S3 storage configconfigs/artifact_storage/gcs.yaml- GCS storage config
Added to requirements.txt and requirements-cpu.txt:
fsspec>=2024.2.0- Filesystem abstractions3fs>=2024.2.0- S3 supportgcsfs>=2024.2.0- GCS support
pip install -r requirements.txt # or requirements-cpu.txtBefore:
from astroml.tracking import MLflowTracker
tracker = MLflowTracker(
enabled=True,
tracking_uri="mlruns",
experiment_name="my_experiment"
)
# Models saved only to local filesystem
torch.save(model.state_dict(), "outputs/model.pth")
tracker.log_model_artifact(model, checkpoint_path="outputs/model.pth")After:
from astroml.storage import create_artifact_store
from astroml.tracking import MLflowTracker
# Create artifact store
artifact_store = create_artifact_store("s3://my-bucket/models")
# Initialize tracker with artifact store
tracker = MLflowTracker(
enabled=True,
tracking_uri="mlruns",
experiment_name="my_experiment",
artifact_store=artifact_store
)
# Models saved to both MLflow and S3
torch.save(model.state_dict(), "outputs/model.pth")
artifact_uri = tracker.log_model_artifact(
model,
checkpoint_path="outputs/model.pth"
)
print(f"Model saved to: {artifact_uri}")Before:
# configs/config.yaml
experiment:
name: "astroml_experiment"
save_dir: "outputs"
mlflow:
enabled: true
tracking_uri: "mlruns"After:
# configs/config.yaml
defaults:
- artifact_storage: local # or s3, gcs
experiment:
name: "astroml_experiment"
save_dir: "outputs"
mlflow:
enabled: true
tracking_uri: "mlruns"Then create configs/artifact_storage/local.yaml:
artifact_storage:
backend: local
local:
path: artifactsfrom hydra import compose, initialize_config_dir
from astroml.storage import create_artifact_store
from astroml.training.config import TrainingConfig
# Load config
cfg = compose(config_name="config")
# Get artifact URI from config
artifact_uri = cfg.training.artifact_storage.get_artifact_uri()
# Create store
artifact_store = create_artifact_store(artifact_uri)
# Use with tracker
tracker = MLflowTracker(artifact_store=artifact_store)import os
from astroml.storage import create_artifact_store
# Use environment variable to switch backends
artifact_uri = os.getenv(
"ARTIFACT_URI",
"file:///tmp/artifacts" # Default to local
)
artifact_store = create_artifact_store(artifact_uri)Usage:
# Development
python train.py
# Production
export ARTIFACT_URI="s3://prod-models/astroml"
python train.pyfrom astroml.storage import S3ArtifactStore
# Each experiment gets its own prefix
experiment_id = "exp_2024_05_31_001"
store = S3ArtifactStore(
bucket="ml-experiments",
prefix=f"astroml/{experiment_id}"
)
tracker = MLflowTracker(artifact_store=store)from pathlib import Path
from astroml.storage import create_artifact_store
store = create_artifact_store("s3://models/astroml")
# Save with version
version = "v1.0.0"
model_path = f"models/{version}/best_model.pth"
uri = store.save("best_model.pth", model_path)
# Later, load specific version
store.load(f"models/v1.0.0/best_model.pth", "model_v1.pth")
store.load(f"models/v1.1.0/best_model.pth", "model_v1_1.pth")from astroml.storage import create_artifact_store
store = create_artifact_store("s3://models/astroml")
# List and delete old artifacts
artifacts = store.list_artifacts("experiments/old")
for artifact in artifacts:
store.delete(artifact)
print(f"Deleted: {artifact}")The changes are fully backward compatible:
-
Existing code without artifact store still works
# This still works - no artifact store tracker = MLflowTracker(enabled=True) tracker.log_model_artifact(model, checkpoint_path="model.pth")
-
Existing config files still work
# Old config without artifact_storage still works experiment: save_dir: "outputs"
-
MLflow tracking unchanged
- Models still logged to MLflow as before
- Artifact store is optional enhancement
Run the test suite:
# Run all artifact store tests
pytest tests/test_artifact_store.py -v
# Run specific test
pytest tests/test_artifact_store.py::TestLocalArtifactStore::test_save_and_load -v
# Run with coverage
pytest tests/test_artifact_store.py --cov=astroml.storageSolution: Install dependencies
pip install -r requirements.txtSolution: Configure AWS credentials
export AWS_ACCESS_KEY_ID=your_key
export AWS_SECRET_ACCESS_KEY=your_secret
export AWS_DEFAULT_REGION=us-east-1Solution: Configure GCP credentials
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
export GOOGLE_CLOUD_PROJECT=my-projectSolution: Verify IAM permissions for your credentials
For S3:
s3:PutObjects3:GetObjects3:DeleteObjects3:ListBucket
For GCS:
storage.objects.createstorage.objects.getstorage.objects.deletestorage.buckets.get
- Use regional buckets for faster access
- Compress large models before upload
- Use multipart uploads for files >100MB (automatic with fsspec)
- Cache frequently accessed artifacts locally
- Use prefixes to organize artifacts logically
- Update your training scripts to use artifact stores
- Configure your preferred backend (local, S3, or GCS)
- Test with sample models before production use
- Monitor artifact storage costs if using cloud backends
- Set up artifact cleanup policies for old experiments
- ARTIFACT_STORAGE.md - Detailed configuration guide
- examples/train_with_artifact_store.py - Example training script
- fsspec documentation
- s3fs documentation
- gcsfs documentation