|
| 1 | +# Copyright 2023–2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +This module provides utility functions for custom argument parsing |
| 17 | +and defines a comprehensive set of command-line arguments for configuring a machine learning workload. |
| 18 | +""" |
| 19 | + |
| 20 | +import argparse |
| 21 | + |
| 22 | + |
| 23 | +def parse_int_list(arg): |
| 24 | + """Parses a string with comma-separated values into a list of integers.""" |
| 25 | + return [int(x) for x in arg.split(",")] |
| 26 | + |
| 27 | + |
| 28 | +def parse_str_list(arg): |
| 29 | + """Parses a string with space-separated values into a list of strings.""" |
| 30 | + return [s.strip() for s in arg.split(",")] |
| 31 | + |
| 32 | + |
| 33 | +def str2bool(v): |
| 34 | + """Parses a string representation of a boolean value into a Python boolean.""" |
| 35 | + if isinstance(v, bool): |
| 36 | + return v |
| 37 | + if v.lower() in ("true"): |
| 38 | + return True |
| 39 | + elif v.lower() in ("false"): |
| 40 | + return False |
| 41 | + else: |
| 42 | + raise argparse.ArgumentTypeError("Boolean value expected (e.g., True or False).") |
| 43 | + |
| 44 | + |
| 45 | +def add_arguments(parser: argparse.ArgumentParser): |
| 46 | + """Add arguments to arg parsers that need it. |
| 47 | +
|
| 48 | + Args: |
| 49 | + parser: parser to add shared arguments to. |
| 50 | + """ |
| 51 | + # Add the arguments for each parser. |
| 52 | + # GCP Configuration |
| 53 | + parser.add_argument("--user", type=str, default="user_name", help="GCP user name.") |
| 54 | + parser.add_argument( |
| 55 | + "--cluster_name", |
| 56 | + type=str, |
| 57 | + default="test-v5e-32-cluster", |
| 58 | + help="Name of the TPU cluster.", |
| 59 | + ) |
| 60 | + parser.add_argument("--project", type=str, default="cloud-tpu-cluster", help="GCP project ID.") |
| 61 | + parser.add_argument("--zone", type=str, default="us-south1-a", help="GCP zone for the cluster.") |
| 62 | + parser.add_argument( |
| 63 | + "--device_type", |
| 64 | + type=str, |
| 65 | + default="v5litepod-32", |
| 66 | + help="Type of TPU device (e.g., v5litepod-32).", |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + "--priority", |
| 70 | + type=str, |
| 71 | + choices=["low", "medium", "high", "very high"], |
| 72 | + default="medium", |
| 73 | + help="Priority of the job.", |
| 74 | + ) |
| 75 | + |
| 76 | + # Image Configuration |
| 77 | + parser.add_argument( |
| 78 | + "--server_image", |
| 79 | + type=str, |
| 80 | + default="us-docker.pkg.dev/cloud-tpu-v2-images/pathways/proxy_server", |
| 81 | + help="Docker image for the proxy server.", |
| 82 | + ) |
| 83 | + parser.add_argument( |
| 84 | + "--proxy_image", |
| 85 | + type=str, |
| 86 | + default="us-docker.pkg.dev/cloud-tpu-v2-images/pathways/server", |
| 87 | + help="Docker image for the server.", |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + "--runner", |
| 91 | + type=str, |
| 92 | + default="us-docker.pkg.dev/path/to/maxtext_runner", |
| 93 | + help="Docker image for the runner.", |
| 94 | + ) |
| 95 | + parser.add_argument( |
| 96 | + "--colocated_python_image", |
| 97 | + type=str, |
| 98 | + default=None, |
| 99 | + help="Colocated Python image.", |
| 100 | + ) |
| 101 | + parser.add_argument("--worker_flags", type=str, default="", help="Worker flags.") |
| 102 | + parser.add_argument("--proxy_flags", type=str, default="", help="Proxy flags.") |
| 103 | + parser.add_argument("--server_flags", type=str, default="", help="Server flags.") |
| 104 | + |
| 105 | + # Model Configuration |
| 106 | + parser.add_argument("--benchmark_steps", type=int, default=20, help="Number of benchmark steps.") |
| 107 | + parser.add_argument( |
| 108 | + "--headless", |
| 109 | + action=argparse.BooleanOptionalAction, |
| 110 | + default=False, |
| 111 | + help="Run in headless mode.", |
| 112 | + ) |
| 113 | + parser.add_argument( |
| 114 | + "--selected_model_framework", |
| 115 | + type=parse_str_list, |
| 116 | + default=["pathways"], |
| 117 | + help="List of model frameworks (e.g., pathways, mcjax", |
| 118 | + ) |
| 119 | + parser.add_argument( |
| 120 | + "--selected_model_names", |
| 121 | + type=parse_str_list, |
| 122 | + default=["llama3_1_8b_8192_v5e_256"], |
| 123 | + help="List of model names (e.g., llama3_1_8b_8192_v5e_256, llama2-7b-v5e-256", |
| 124 | + ) |
| 125 | + parser.add_argument( |
| 126 | + "--num_slices_list", |
| 127 | + type=parse_int_list, |
| 128 | + default=[2], |
| 129 | + help="List of number of slices.", |
| 130 | + ) |
| 131 | + |
| 132 | + # BigQuery configuration |
| 133 | + parser.add_argument( |
| 134 | + "--bq_enable", |
| 135 | + type=str2bool, |
| 136 | + default=False, |
| 137 | + help="Enable BigQuery logging. Must be True or False. Defaults to False.", |
| 138 | + ) |
| 139 | + |
| 140 | + parser.add_argument( |
| 141 | + "--bq_db_project", |
| 142 | + type=str, |
| 143 | + default="", |
| 144 | + help="BigQuery project ID where the logging dataset resides.", |
| 145 | + ) |
| 146 | + |
| 147 | + parser.add_argument( |
| 148 | + "--bq_db_dataset", |
| 149 | + type=str, |
| 150 | + default="", |
| 151 | + help="BigQuery dataset name where metrics will be written.", |
| 152 | + ) |
| 153 | + |
| 154 | + # Other configurations |
| 155 | + parser.add_argument("--xpk_path", type=str, default="~/xpk", help="Path to xpk.") |
| 156 | + parser.add_argument("--delete", action="store_true", help="Delete the cluster workload") |
| 157 | + parser.add_argument("--max_restarts", type=int, default=0, help="Maximum number of restarts") |
| 158 | + parser.add_argument("--temp_key", type=str, default=None, help="Temporary placeholder code") |
0 commit comments