Skip to content

Commit

Permalink
Merge branch 'main' into ensemble_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaolong0728 committed Dec 2, 2024
2 parents 6542c1d + 3d92779 commit 1a04fe1
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
from pathlib import Path
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def generate(
script_dir: Path,
script_path: Path,
deployment_type: str = "shadow",
additional_settings: Dict[str, any] = None,
) -> bool:
"""
Generates a script that defines the `get_deployment_config` function for configuring the deployment status and settings.
Parameters:
script_dir (Path): The directory where the generated deployment configuration script will be saved.
script_path (Path): The path where the generated deployment configuration script will be saved.
This should be a valid writable path.
deployment_type (str, optional):
The type of deployment. Must be one of "shadow", "deployed", "baseline", or "deprecated".
Expand Down Expand Up @@ -72,4 +70,4 @@ def get_deployment_config():
deployment_config = {deployment_config}
return deployment_config
"""
return save_script(script_dir, code)
return save_script(script_path, code)
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from views_pipeline_core.templates.utils import save_script
from pathlib import Path
import logging

logger = logging.getLogger(__name__)

def generate(script_dir: Path) -> bool:
def generate(script_path: Path) -> bool:
"""
Generates a script that defines a function for obtaining hyperparameter configurations
necessary for model training.
Parameters:
script_dir (Path):
The directory where the generated deployment configuration script will be saved.
script_path (Path):
The path where the generated deployment configuration script will be saved.
This should be a valid writable path.
Returns:
Expand All @@ -32,4 +34,4 @@ def get_hp_config():
}}
return hyperparameters
"""
return save_script(script_dir, code)
return save_script(script_path, code)
11 changes: 6 additions & 5 deletions views_pipeline_core/templates/model/template_config_meta.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from views_pipeline_core.templates.utils import save_script
from pathlib import Path
import logging
logger = logging.getLogger(__name__)


def generate(script_dir: Path, model_name: str, model_algorithm: str) -> bool:
def generate(script_path: Path, model_name: str, model_algorithm: str) -> bool:
"""
Generates a script that defines the `get_meta_config` function for model metadata.
Parameters:
script_dir (Path):
The directory where the generated deployment configuration script will be saved.
script_path (Path):
The path where the generated deployment configuration script will be saved.
This should be a valid writable path.
model_name (str):
Expand Down Expand Up @@ -41,4 +42,4 @@ def generate(script_dir: Path, model_name: str, model_algorithm: str) -> bool:
}}
return meta_config
"""
return save_script(script_dir, code)
return save_script(script_path, code)
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from views_pipeline_core.templates.utils import save_script
from pathlib import Path
import logging
logger = logging.getLogger(__name__)


def generate(script_dir: Path, model_name: str) -> bool:
def generate(script_path: Path, model_name: str) -> bool:
"""
Generates a Python script that defines the `generate` function for configuring input data for model training
using the viewser library.
Args:
script_dir (Path): The directory where the generated deployment configuration script will be saved.
script_path (Path): The path where the generated deployment configuration script will be saved.
This should be a valid writable path.
model_name (str): The name of the model for which the input data configuration is being generated.
This name is used to identify the relevant data in the viewser queryset.
Expand Down Expand Up @@ -58,4 +59,4 @@ def generate():
return queryset_base
"""
return save_script(script_dir, code)
return save_script(script_path, code)
11 changes: 6 additions & 5 deletions views_pipeline_core/templates/model/template_config_sweep.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from views_pipeline_core.templates.utils import save_script
from pathlib import Path
import logging
logger = logging.getLogger(__name__)


def generate(script_dir: Path, model_name: str, model_algorithm: str) -> bool:
def generate(script_path: Path, model_name: str, model_algorithm: str) -> bool:
"""
Generates a script that defines the `get_sweep_config` function for hyperparameter sweeps.
Parameters:
script_dir (Path):
The directory where the generated deployment configuration script will be saved.
script_path (Path):
The path where the generated deployment configuration script will be saved.
This should be a valid writable path.
model_name (str):
Expand Down Expand Up @@ -52,4 +53,4 @@ def get_sweep_config():
return sweep_config
"""
return save_script(script_dir, code)
return save_script(script_path, code)
11 changes: 6 additions & 5 deletions views_pipeline_core/templates/model/template_main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from views_pipeline_core.templates.utils import save_script
from pathlib import Path
import logging
logger = logging.getLogger(__name__)


def generate(script_dir: Path) -> bool:
def generate(script_path: Path) -> bool:
"""
Generates a Python script that sets up and executes model runs with Weights & Biases (WandB) integration.
Expand All @@ -11,8 +12,8 @@ def generate(script_dir: Path) -> bool:
generated script includes command-line argument parsing, validation, and runtime logging.
Parameters:
script_dir (Path):
The directory where the generated Python script will be saved. This should be a valid writable
script_path (Path):
The path where the generated Python script will be saved. This should be a valid writable
path that exists within the project structure.
Returns:
Expand Down Expand Up @@ -62,4 +63,4 @@ def generate(script_dir: Path) -> bool:
else:
# YourModelManager(model_path=model_path).execute_single_run(args)
'''
return save_script(script_dir, code)
return save_script(script_path, code)
87 changes: 87 additions & 0 deletions views_pipeline_core/templates/model/template_run_sh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from pathlib import Path

def generate(script_path: Path, package_name: str) -> bool:
"""
Generates a shell script to set up the environment and run a Python script.
This function creates a shell script that:
- Checks if the operating system is macOS (Darwin) and updates the user's `.zshrc` file with necessary environment variables for `libomp`.
- Determines the script's directory and the project's root directory.
- Sets up the path for the Conda environment specific to the project.
- Activates the Conda environment if it exists, or creates a new one if it doesn't.
- Installs any missing or outdated Python packages listed in `requirements.txt`.
- Runs the main Python script with any provided command-line arguments.
Parameters:
script_path (Path):
The path where the generated shell script will be saved. This should be a valid writable path.
package_name (str):
The name of the package to be used in the script.
Returns:
bool:
True if the script was successfully written to the specified directory, False otherwise.
The generated shell script includes the following steps:
- Checks if the operating system is macOS and updates the `.zshrc` file with necessary environment variables for `libomp`.
- Determines the script's directory and the project's root directory.
- Sets up the path for the Conda environment specific to the project.
- Activates the Conda environment if it exists, or creates a new one if it doesn't.
- Installs any missing or outdated Python packages listed in `requirements.txt`.
- Runs the main Python script with any provided command-line arguments.
Note:
- Ensure that the `requirements.txt` file is present in the same directory as the generated shell script.
- The generated shell script is designed to be executed in a zsh shell.
"""
code = f"""#!/bin/zsh
if [[ "$OSTYPE" == "darwin"* ]]; then
if ! grep -q 'export LDFLAGS="-L/opt/homebrew/opt/libomp/lib"' ~/.zshrc; then
echo 'export LDFLAGS="-L/opt/homebrew/opt/libomp/lib"' >> ~/.zshrc
fi
if ! grep -q 'export CPPFLAGS="-I/opt/homebrew/opt/libomp/include"' ~/.zshrc; then
echo 'export CPPFLAGS="-I/opt/homebrew/opt/libomp/include"' >> ~/.zshrc
fi
if ! grep -q 'export DYLD_LIBRARY_PATH="/opt/homebrew/opt/libomp/lib:$DYLD_LIBRARY_PATH"' ~/.zshrc; then
echo 'export DYLD_LIBRARY_PATH="/opt/homebrew/opt/libomp/lib:$DYLD_LIBRARY_PATH"' >> ~/.zshrc
fi
source ~/.zshrc
fi
script_path=$(dirname "$(realpath $0)")
project_path="$( cd "$script_path/../../" >/dev/null 2>&1 && pwd )"
env_path="$project_path/envs/{package_name}"
eval "$(conda shell.bash hook)"
if [ -d "$env_path" ]; then
echo "Conda environment already exists at $env_path. Checking dependencies..."
conda activate "$env_path"
echo "$env_path is activated"
missing_packages=$(pip install --dry-run -r $script_path/requirements.txt 2>&1 | grep -v "Requirement already satisfied" | wc -l)
if [ "$missing_packages" -gt 0 ]; then
echo "Installing missing or outdated packages..."
pip install -r $script_path/requirements.txt
else
echo "All packages are up-to-date."
fi
else
echo "Creating new Conda environment at $env_path..."
conda create --prefix "$env_path" python=3.11 -y
conda activate "$env_path"
pip install -r $script_path/requirements.txt
fi
echo "Running $script_path/main.py "
python $script_path/main.py "$@"
"""

try:
with open(script_path, "w", encoding="utf-8") as script_file:
script_file.write(code)
return True
except IOError as e:
print(f"An error occurred while writing to {script_path}: {e}")
return False

0 comments on commit 1a04fe1

Please sign in to comment.