|
| 1 | +""" |
| 2 | +Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + |
| 4 | +Microsoft Corporation (“Microsoft”) grants you a nonexclusive, perpetual, |
| 5 | +royalty-free right to use, copy, and modify the software code provided by us |
| 6 | +("Software Code"). You may not sublicense the Software Code or any use of it |
| 7 | +(except to your affiliates and to vendors to perform work on your behalf) |
| 8 | +through distribution, network access, service agreement, lease, rental, or |
| 9 | +otherwise. This license does not purport to express any claim of ownership over |
| 10 | +data you may have shared with Microsoft in the creation of the Software Code. |
| 11 | +Unless applicable law gives you more rights, Microsoft reserves all other |
| 12 | +rights not expressly granted herein, whether by implication, estoppel or |
| 13 | +otherwise. |
| 14 | + |
| 15 | +THE SOFTWARE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | +MICROSOFT OR ITS LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 21 | +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 | +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | +ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE CODE, EVEN IF ADVISED OF THE |
| 24 | +POSSIBILITY OF SUCH DAMAGE. |
| 25 | +""" |
| 26 | + |
| 27 | +import os, json, requests, datetime |
| 28 | +import argparse |
| 29 | +from azureml.core import Workspace, Experiment, Datastore |
| 30 | +from azureml.core.runconfig import RunConfiguration, CondaDependencies |
| 31 | +from azureml.data.data_reference import DataReference |
| 32 | +from azureml.pipeline.core import Pipeline, PipelineData, StepSequence |
| 33 | +from azureml.pipeline.steps import PythonScriptStep |
| 34 | +from azureml.pipeline.core import PublishedPipeline |
| 35 | +from azureml.pipeline.core.graph import PipelineParameter |
| 36 | +from azureml.core.compute import ComputeTarget |
| 37 | + |
| 38 | +# from azureml.widgets import RunDetails |
| 39 | +from azureml.core.authentication import AzureCliAuthentication |
| 40 | + |
| 41 | +print("Pipeline SDK-specific imports completed") |
| 42 | + |
| 43 | +cli_auth = AzureCliAuthentication() |
| 44 | + |
| 45 | + |
| 46 | +parser = argparse.ArgumentParser("Pipeline") |
| 47 | +parser.add_argument( |
| 48 | + "--pipeline_action", |
| 49 | + type=str, |
| 50 | + choices=["pipeline-test", "publish"], |
| 51 | + help="Determines if pipeline needs to run on small data set \ |
| 52 | + or pipeline needs to be republished", |
| 53 | + #default="pipeline-test", |
| 54 | +) |
| 55 | + |
| 56 | +args = parser.parse_args() |
| 57 | + |
| 58 | + |
| 59 | +# Get workspace |
| 60 | +ws = Workspace.from_config(path="aml_config/config.json", auth=cli_auth) |
| 61 | +def_blob_store = Datastore(ws, "workspaceblobstore") |
| 62 | + |
| 63 | +# Get AML Compute name and Experiment Name |
| 64 | +with open("aml_config/security_config.json") as f: |
| 65 | + config = json.load(f) |
| 66 | + |
| 67 | +experiment_name = config["experiment_name"] |
| 68 | +aml_cluster_name = config["aml_cluster_name"] |
| 69 | +aml_pipeline_name = "training-pipeline" |
| 70 | + |
| 71 | +source_directory = "code" |
| 72 | + |
| 73 | +# Run Config |
| 74 | +# Declare packages dependencies required in the pipeline (these can also be expressed as a YML file) |
| 75 | +# cd = CondaDependencies.create(pip_packages=["azureml-defaults", 'tensorflow==1.8.0']) |
| 76 | +cd = CondaDependencies("aml_config/conda_dependencies.yml") |
| 77 | + |
| 78 | +run_config = RunConfiguration(conda_dependencies=cd) |
| 79 | + |
| 80 | +aml_compute = ws.compute_targets[aml_cluster_name] |
| 81 | + |
| 82 | +jsonconfigs = PipelineData("jsonconfigs", datastore=def_blob_store) |
| 83 | + |
| 84 | +# Suffix for all the config files |
| 85 | +config_suffix = datetime.datetime.now().strftime("%Y%m%d%H") |
| 86 | +print("PipelineData object created") |
| 87 | + |
| 88 | +# Create python script step to run the training/scoring main script |
| 89 | +train = PythonScriptStep( |
| 90 | + name="Train New Model", |
| 91 | + script_name="training/train.py", |
| 92 | + compute_target=aml_compute, |
| 93 | + source_directory=source_directory, |
| 94 | + arguments=["--config_suffix", config_suffix, "--json_config", jsonconfigs], |
| 95 | + runconfig=run_config, |
| 96 | + # inputs=[jsonconfigs], |
| 97 | + outputs=[jsonconfigs], |
| 98 | + allow_reuse=False, |
| 99 | +) |
| 100 | +print("Step Train created") |
| 101 | + |
| 102 | +evaluate = PythonScriptStep( |
| 103 | + name="Evaluate New Model with Prod Model", |
| 104 | + script_name="evaluate/evaluate_model.py", |
| 105 | + compute_target=aml_compute, |
| 106 | + source_directory=source_directory, |
| 107 | + arguments=["--config_suffix", config_suffix, "--json_config", jsonconfigs], |
| 108 | + runconfig=run_config, |
| 109 | + inputs=[jsonconfigs], |
| 110 | + # outputs=[jsonconfigs], |
| 111 | + allow_reuse=False, |
| 112 | +) |
| 113 | +print("Step Evaluate created") |
| 114 | + |
| 115 | +register_model = PythonScriptStep( |
| 116 | + name="Register New Trained Model", |
| 117 | + script_name="register/register_model.py", |
| 118 | + compute_target=aml_compute, |
| 119 | + source_directory=source_directory, |
| 120 | + arguments=["--config_suffix", config_suffix, "--json_config", jsonconfigs], |
| 121 | + runconfig=run_config, |
| 122 | + inputs=[jsonconfigs], |
| 123 | + # outputs=[jsonconfigs], |
| 124 | + allow_reuse=False, |
| 125 | +) |
| 126 | +print("Step register model created") |
| 127 | + |
| 128 | +package_model = PythonScriptStep( |
| 129 | + name="Package Model as Scoring Image", |
| 130 | + script_name="scoring/create_scoring_image.py", |
| 131 | + compute_target=aml_compute, |
| 132 | + source_directory=source_directory, |
| 133 | + arguments=["--config_suffix", config_suffix, "--json_config", jsonconfigs], |
| 134 | + runconfig=run_config, |
| 135 | + inputs=[jsonconfigs], |
| 136 | + # outputs=[jsonconfigs], |
| 137 | + allow_reuse=False, |
| 138 | +) |
| 139 | +print("Packed the model into a Scoring Image") |
| 140 | + |
| 141 | +# Create Steps dependency such that they run in sequence |
| 142 | +evaluate.run_after(train) |
| 143 | +register_model.run_after(evaluate) |
| 144 | +package_model.run_after(register_model) |
| 145 | + |
| 146 | +steps = [package_model] |
| 147 | + |
| 148 | + |
| 149 | +# Build Pipeline |
| 150 | +pipeline1 = Pipeline(workspace=ws, steps=steps) |
| 151 | +print("Pipeline is built") |
| 152 | + |
| 153 | +# Validate Pipeline |
| 154 | +pipeline1.validate() |
| 155 | +print("Pipeline validation complete") |
| 156 | + |
| 157 | + |
| 158 | +# Submit unpublished pipeline with small data set for test |
| 159 | +if args.pipeline_action == "pipeline-test": |
| 160 | + pipeline_run1 = Experiment(ws, experiment_name).submit( |
| 161 | + pipeline1, regenerate_outputs=True |
| 162 | + ) |
| 163 | + print("Pipeline is submitted for execution") |
| 164 | + pipeline_run1.wait_for_completion(show_output=True) |
| 165 | + |
| 166 | + |
| 167 | +# RunDetails(pipeline_run1).show() |
| 168 | + |
| 169 | + |
| 170 | +# Define pipeline parameters |
| 171 | +# run_env = PipelineParameter( |
| 172 | +# name="dev_flag", |
| 173 | +# default_value=True) |
| 174 | + |
| 175 | +# dbname = PipelineParameter( |
| 176 | +# name="dbname", |
| 177 | +# default_value='opex') |
| 178 | + |
| 179 | + |
| 180 | +# Publish Pipeline |
| 181 | +if args.pipeline_action == "publish": |
| 182 | + published_pipeline1 = pipeline1.publish( |
| 183 | + name=aml_pipeline_name, description="Model training/retraining pipeline" |
| 184 | + ) |
| 185 | + print( |
| 186 | + "Pipeline is published as rest_endpoint {} ".format( |
| 187 | + published_pipeline1.endpoint |
| 188 | + ) |
| 189 | + ) |
| 190 | + # write published pipeline details as build artifact |
| 191 | + pipeline_config = {} |
| 192 | + pipeline_config["pipeline_name"] = published_pipeline1.name |
| 193 | + pipeline_config["rest_endpoint"] = published_pipeline1.endpoint |
| 194 | + pipeline_config["experiment_name"] = "published-pipeline-exp" # experiment_name |
| 195 | + with open("aml_config/pipeline_config.json", "w") as outfile: |
| 196 | + json.dump(pipeline_config, outfile) |
0 commit comments