|
| 1 | +import functools |
| 2 | + |
| 3 | +import click |
| 4 | + |
| 5 | +from paperspace import client, config |
| 6 | +from paperspace.cli import common |
| 7 | +from paperspace.cli.cli import cli |
| 8 | +from paperspace.cli.common import ClickGroup |
| 9 | +from paperspace.commands import hyperparameters as hyperparameters_commands |
| 10 | + |
| 11 | + |
| 12 | +@cli.group("hyperparameters", help="Manage hyperparameters", cls=ClickGroup) |
| 13 | +def hyperparameters_group(): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +def common_hyperparameter_create_options(f): |
| 18 | + options = [ |
| 19 | + click.option( |
| 20 | + "--name", |
| 21 | + "name", |
| 22 | + required=True, |
| 23 | + ), |
| 24 | + click.option( |
| 25 | + "--projectId", |
| 26 | + "projectHandle", |
| 27 | + required=True, |
| 28 | + ), |
| 29 | + click.option( |
| 30 | + "--tuningCommand", |
| 31 | + "tuningCommand", |
| 32 | + required=True, |
| 33 | + ), |
| 34 | + click.option( |
| 35 | + "--workerContainer", |
| 36 | + "workerContainer", |
| 37 | + required=True, |
| 38 | + ), |
| 39 | + click.option( |
| 40 | + "--workerMachineType", |
| 41 | + "workerMachineType", |
| 42 | + required=True, |
| 43 | + ), |
| 44 | + click.option( |
| 45 | + "--workerCommand", |
| 46 | + "workerCommand", |
| 47 | + required=True, |
| 48 | + ), |
| 49 | + click.option( |
| 50 | + "--workerCount", |
| 51 | + "workerCount", |
| 52 | + required=True, |
| 53 | + type=int, |
| 54 | + ), |
| 55 | + click.option( |
| 56 | + "--serverRegistryUsername", |
| 57 | + "hyperparameterServerRegistryUsername", |
| 58 | + ), |
| 59 | + click.option( |
| 60 | + "--serverRegistryPassword", |
| 61 | + "hyperparameterServerRegistryPassword", |
| 62 | + ), |
| 63 | + click.option( |
| 64 | + "--serverContainerUser", |
| 65 | + "hyperparameterServerContainerUser", |
| 66 | + ), |
| 67 | + ] |
| 68 | + return functools.reduce(lambda x, opt: opt(x), reversed(options), f) |
| 69 | + |
| 70 | + |
| 71 | +@hyperparameters_group.command("create", help="Create hyperparameter") |
| 72 | +@common_hyperparameter_create_options |
| 73 | +@common.api_key_option |
| 74 | +def create_hyperparameter(api_key, **hyperparameter): |
| 75 | + common.del_if_value_is_none(hyperparameter) |
| 76 | + hyperparameters_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key) |
| 77 | + command = hyperparameters_commands.CreateHyperparameterCommand(api=hyperparameters_api) |
| 78 | + command.execute(hyperparameter) |
| 79 | + |
| 80 | + |
| 81 | +@hyperparameters_group.command("createAndStart", help="Create hyperparameter") |
| 82 | +@common_hyperparameter_create_options |
| 83 | +@common.api_key_option |
| 84 | +def create_and_start_hyperparameter(api_key, **hyperparameter): |
| 85 | + common.del_if_value_is_none(hyperparameter) |
| 86 | + hyperparameters_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key) |
| 87 | + command = hyperparameters_commands.CreateAndStartHyperparameterCommand(api=hyperparameters_api) |
| 88 | + command.execute(hyperparameter) |
| 89 | + |
| 90 | + |
| 91 | +@hyperparameters_group.command("list", help="List hyperparameters") |
| 92 | +@common.api_key_option |
| 93 | +def list_hyperparameters(api_key): |
| 94 | + hyperparameters_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key) |
| 95 | + command = hyperparameters_commands.ListHyperparametersCommand(api=hyperparameters_api) |
| 96 | + command.execute() |
| 97 | + |
| 98 | + |
| 99 | +# TODO: 'unhidden' command and test it when api is updated to support deleting hyperparameters |
| 100 | +@hyperparameters_group.command("delete", help="Delete hyperparameter", hidden=True) |
| 101 | +@click.option( |
| 102 | + "--id", |
| 103 | + "id_", |
| 104 | + required=True, |
| 105 | +) |
| 106 | +@common.api_key_option |
| 107 | +def delete_hyperparameter(api_key, id_): |
| 108 | + hyperparameters_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key) |
| 109 | + command = hyperparameters_commands.DeleteHyperparameterCommand(api=hyperparameters_api) |
| 110 | + command.execute(id_) |
| 111 | + |
| 112 | + |
| 113 | +@hyperparameters_group.command("details", help="Show details of hyperparameter") |
| 114 | +@click.option( |
| 115 | + "--id", |
| 116 | + "id_", |
| 117 | + required=True, |
| 118 | +) |
| 119 | +@common.api_key_option |
| 120 | +def get_hyperparameter_details(api_key, id_): |
| 121 | + hyperparameters_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key) |
| 122 | + command = hyperparameters_commands.HyperparameterDetailsCommand(api=hyperparameters_api) |
| 123 | + command.execute(id_) |
| 124 | + |
| 125 | + |
| 126 | +@hyperparameters_group.command("start", help="Start hyperparameter tuning") |
| 127 | +@click.option( |
| 128 | + "--id", |
| 129 | + "id_", |
| 130 | + required=True, |
| 131 | +) |
| 132 | +@common.api_key_option |
| 133 | +def start_hyperparameter_tuning(api_key, id_): |
| 134 | + hyperparameters_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key) |
| 135 | + command = hyperparameters_commands.HyperparameterStartCommand(api=hyperparameters_api) |
| 136 | + command.execute(id_) |
0 commit comments