|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "Xgqk7eHswDpB" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "[](https://colab.research.google.com/github/google/vizier/blob/main/docs/guides/user/distributed.ipynb)\n", |
| 10 | + "\n", |
| 11 | + "# Distributed Vizier\n", |
| 12 | + "This documentation shows how to perform distributed optimization over multiple clients." |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "metadata": { |
| 18 | + "id": "O5RnMytPR8Aw" |
| 19 | + }, |
| 20 | + "source": [ |
| 21 | + "## Installation and reference imports" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": { |
| 28 | + "id": "kSG8XlxLvCJO" |
| 29 | + }, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "!pip install google-vizier[jax]" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "metadata": { |
| 39 | + "id": "fzYr0bPYSHfQ" |
| 40 | + }, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "import multiprocessing\n", |
| 44 | + "\n", |
| 45 | + "from vizier import service\n", |
| 46 | + "from vizier.service import clients\n", |
| 47 | + "from vizier.service import pyvizier as vz\n", |
| 48 | + "from vizier.service import servers" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "metadata": { |
| 54 | + "id": "qJ1kRiHaKOVt" |
| 55 | + }, |
| 56 | + "source": [ |
| 57 | + "## Regular setup\n", |
| 58 | + "We setup a regular study configuration below." |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "metadata": { |
| 65 | + "id": "zX2G3_pcKYdG" |
| 66 | + }, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "study_config = vz.StudyConfig()\n", |
| 70 | + "study_config.search_space.root.add_float_param('x', 0.0, 1.0)\n", |
| 71 | + "study_config.metric_information.append(vz.MetricInformation(name='metric', goal=vz.ObjectiveMetricGoal.MAXIMIZE))\n", |
| 72 | + "study_config.algorithm = 'DEFAULT'\n", |
| 73 | + "\n", |
| 74 | + "\n", |
| 75 | + "def evaluate(x: float) -\u003e float:\n", |
| 76 | + " return 2*x - x**2" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": { |
| 82 | + "id": "w3m48cPsXcxD" |
| 83 | + }, |
| 84 | + "source": [ |
| 85 | + "## Server creation\n", |
| 86 | + "Unlike the single-client case, in the distributed case, we require a single explicit server to accept requests from all other client processses. Details such as the `host`, `port`, `database_url`, `policy_factory`, etc. can be configured in the server's initializer." |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": null, |
| 92 | + "metadata": { |
| 93 | + "id": "V6ef6OfMXdpz" |
| 94 | + }, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "server = servers.DefaultVizierServer() # Ideally created on a separate process such as a server machine." |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": { |
| 103 | + "id": "ktExEiS0xlH_" |
| 104 | + }, |
| 105 | + "source": [ |
| 106 | + "## Client parallelization\n", |
| 107 | + "We may simultaneously create multiple clients to work on the same study, useful for parallelizing evaluation workload. All client processes (on a single machine or over multiple machines) will connect to this server via a globally specified `endpoint`." |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": { |
| 114 | + "id": "EQR1_u-VxEwn" |
| 115 | + }, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "clients.environment_variables.server_endpoint = server.endpoint # Server address.\n", |
| 119 | + "study_client = clients.Study.from_study_config(study_config, owner='owner', study_id = 'example_study_id') # Now connects to the explicitly created server.\n", |
| 120 | + "another_study_client = clients.Study.from_resource_name(study_client.resource_name) # Another way to fork clients." |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": { |
| 126 | + "id": "Vh3eNsrAdaMJ" |
| 127 | + }, |
| 128 | + "source": [ |
| 129 | + "## Distributed suggestions\n", |
| 130 | + "We may now distribute our workflow, with each worker/client using the same loop below. Each client requires a unique `client_id` however, to ensure the server can identify client workers and distribute workloads properly." |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": null, |
| 136 | + "metadata": { |
| 137 | + "id": "BnFKc7FadkJV" |
| 138 | + }, |
| 139 | + "outputs": [], |
| 140 | + "source": [ |
| 141 | + "def tuning_loop(client_id: str):\n", |
| 142 | + " for i in range(10):\n", |
| 143 | + " suggestions = study_client.suggest(count=1, client_id=client_id)\n", |
| 144 | + " for suggestion in suggestions:\n", |
| 145 | + " objective = evaluate(suggestion.parameters['x'])\n", |
| 146 | + " final_measurement = vz.Measurement({'metric': objective})\n", |
| 147 | + " suggestion.complete(final_measurement)" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": { |
| 153 | + "id": "NVGcVEzb0Gxe" |
| 154 | + }, |
| 155 | + "source": [ |
| 156 | + "For example, we may perform a threadpool and construct multiple clients to parallelize evaluations on a single machine." |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "metadata": { |
| 163 | + "id": "R0pcPViUz9zC" |
| 164 | + }, |
| 165 | + "outputs": [], |
| 166 | + "source": [ |
| 167 | + "NUM_CLIENTS = 10\n", |
| 168 | + "NUM_TRIALS_PER_CLIENT = 50\n", |
| 169 | + "\n", |
| 170 | + "pool = multiprocessing.pool.ThreadPool(NUM_CLIENTS)\n", |
| 171 | + "pool.map(tuning_loop, range(NUM_CLIENTS))" |
| 172 | + ] |
| 173 | + } |
| 174 | + ], |
| 175 | + "metadata": { |
| 176 | + "colab": { |
| 177 | + "last_runtime": { |
| 178 | + "build_target": "//ads/thresholds/kumamon/colab:notebook", |
| 179 | + "kind": "shared" |
| 180 | + }, |
| 181 | + "name": "Distributed.ipynb", |
| 182 | + "private_outputs": true, |
| 183 | + "provenance": [ |
| 184 | + { |
| 185 | + "file_id": "/piper/depot/http://github.com/google/vizier/tree/main/vizier/docs/guides/user/running_vizier.ipynb", |
| 186 | + "timestamp": 1673247218127 |
| 187 | + }, |
| 188 | + { |
| 189 | + "file_id": "1q87rsDDUJLHci3o9Gv-sU0g7H3O3lAbU", |
| 190 | + "timestamp": 1659555396142 |
| 191 | + } |
| 192 | + ] |
| 193 | + }, |
| 194 | + "kernelspec": { |
| 195 | + "display_name": "Python 3", |
| 196 | + "name": "python3" |
| 197 | + }, |
| 198 | + "language_info": { |
| 199 | + "name": "python" |
| 200 | + } |
| 201 | + }, |
| 202 | + "nbformat": 4, |
| 203 | + "nbformat_minor": 0 |
| 204 | +} |
0 commit comments