延续上一篇文章,当CI pipeline执行结束后,模型会被下载到【pipeline published artifact区域】。接下来,就把【pipeline published artifact区域】产生新的模型档案,做为「触发CD pipeline的条件」。本次练习CD pipeline的目的是在将机器学习模型部署成endpoint。
本篇文章结构:
1.概念说明
2.建立agent的任务
3.设定触发条件
4.试跑第一部分CD pipeline
CI pipeline文章:
【Azure MLOps - 1】使用Azure DevOps建立AML pipelines
【Azure MLOps - 2】使用Azure DevOps建立专案与连线设定
【Azure MLOps - 3】使用Azure DevOps建立训练模型的CI pipeline(上)
【Azure MLOps - 4】使用Azure DevOps建立训练模型的CI pipeline(下)
【Azure MLOps - 5】执行Azure DevOps CI pipeline
1.概念说明
先来思考一下资料科学家使用Azure Machine Learning部署模型时(示意图如下),会点选deploy,透过一些必要的设定,把模型部署成web service。部署后,提供模型的使用者api「输入特徵值」,产出模型预测结果,例如预测机率值。
在这次的练习情境中,在模型部署时,会分成测试环境(staging area)与正式环境(production area),把模型部署成real-time endpoint,也可以依照你的需求部署成batch endpoint。只有当测试环境(staging area)模型部署结束,才会考虑是否要把模型部署到正式环境(production area)。
考虑的面向很多,很可能是开发团队想要观察模型表现一阵子,确认新模型的预测结果是否比旧版模型好(每个模型的允许放行的条件不同)。
所以在CD pipeline中,会有两个部分:
(1)模型自动部署到测试环境(staging area)
(2)模型自动部署到正式环境(production area)
而这两个部分中间,并不会直接触发,中间会卡一个核准的关卡。
如果是在测试环境(staging area)模型吃的运算资源,设定为Azure compute instance(ACI);如果是正式环境(production area),模型服务最好是部署到AzureAKS。可参考在 Azure Machine Learning 上建立运算资源
2.建立agent的任务
2-1 建立pipeline与设定agent
因为要产生一条新的CD pipeline,到Azure DevOps点选pipeline下的release,点选new pipeline:
选择empty job:
点选「1 job,0 task」:
接下来就会到熟悉的画面,把这个部分称作Deploy to staging:
和CI pipeline一样,选择agent的系统和版本,我这里还是选择ubuntu-18.04:
备注:因为在建立CI pipeline的时候已经解说过很多概念,所以CD pipeline会很快速的带过。如果还不知道agent是什么,也不知道什么是Azure Machine Learning CLI extension (机器学习延伸模组),建议回头从头複习一下。
2-2 agent任务:安装az ml extension
由于后续会在agent里面测试部署的api是否可以呼叫并预测,而测试使用的程式码是staging_test.py (位在【pipeline published artifact区域】)如下图,所以我们需要指定agent python版本。
点击agent旁边的+,搜寻python,加入use python任务,指定python版本:
与CI pipeline相同,需要再agent安装az ml extension,才有办法使用指令对Azure Machine Learning各种操作。所以点击agent旁边的+,搜寻Azure CLI,加入任务:
在inline script当中写入指令:
az extension add -n azure-cli-ml
2-3 agent任务:部署模型到azure compute instance
接下来我们希望模型部署到container上,下一个任务 Deploy to Azure contrainer instance (ACI)。
因为要使用az ml cli指令部署模型,点击agent旁边的+,搜寻Azure CLI,加入任务,命名为Deploy to ACI:
使用指令:az ml model deploy
进行部署,指令的详细参数可参阅官方说明:
az ml model deploy -g $(azureml.resourceGroup) -w $(azureml.workspaceName) -n $(service.name.staging) -f ../metadata/model.json --dc aciDeploymentConfigStaging.yml --ic inferenceConfig.yml --overwrite
提醒大家使用Azure Machine Learning部署,需要以下两个要素,
(1)model
(2)Inferencing and deployment configuration (Compute target+Environment+Scoring scripts)
model很简单理解,就是model.pkl(如示意图),我们也已经複製到【pipeline published artifact区域】。
而另外一个要素,「Inferencing and deployment configuration」又由3个元素组成:
(1)Compute target
(2)Environment
(3)Scoring scripts
也就是模型在部署和推断(预测)的时候需要的configuration,稍微说明:
(1)compute target就是模型吃的运算资源,所以staging area就会是ACI,production area就会是AKS,所以用Azure CLI指令进行模型部署的时候,会需要进行aci configuration。也就是上面指令的参数--dc所指定的档案: aciDeploymentConfigStaging.yml
(2)Environment则是机器学习模型「在预测的时候」会需要什么环境,例如模型需要sklean,python3.7。也就是上面指令的--ic参数:inferenceConfig.yml
(3)scoring.py就是产出预测结果的程式,里面有两个功能,一个是把model load 到ACI的功能init function,另一个是进行预测的run function:
所以可以看到az ml model deploy指令所加入的参数有点複杂:
--ic参数 指定inference configuration file,档案位在【pipeline published artifact区域】的inferenceConfig.yml (如下图),这个档案当中,指定模型部署的environment ymal档案为「scoringConfig.yml」,而Scoring scripts则是「score.py」。
以上这些档案,都位在【pipeline published artifact区域】中,如下图,所以这就是为什么会在CI pipeline中,最后的copy file任务时,把这些档案一併複製:
其实在AML部署模型有点複杂,所以就多说明了一些,对刚接触的人会较难理解没错,本系列文章先继续专注在MLOPS流程建立。
因为az ml model deploy指令中,所需的档案都在【pipeline published artifact区域】,所以需要指定工作目录,下拉选单/_MLOps-WorkShop-1-CI/landing/s/deployment:
最后指定$()内的名称变数:
azureml.resourceGroup mlops-wsh-rg-1azureml.workspaceName mlops-wsh-aml-1service.name.staging insurance-service-aci (web service名称)
2-4 agent任务:测试endpoint
如果执行CD pipeline,当agent执行到2-3时,一个名叫做insurance-service-aci的web service会被部署完成。所以在azure machine learning workspace的endpoint会出现insurance-service-aci (如下图示意图):
并且有这个service专属的endpoint url(如下图)。
建立pipeline的目的就是希望能够自动化,由agent自动测试这个endpoint url「是否能可以呼叫,输入特徵值,且回传预测结果」,所以接下来新增的任务是「测试endpoint url」。
测试endpoit的程式码: staging_test.py 一样位在【pipeline published artifact区域】,测试程式档中,会执行「传入特徵值(json格式)给endpoint」,和资料科学家在AML workshop使用UI测试是一样的流程:
要在agent当中执行staging_test.py,就需要先把程式当中需要的套件安装好,所以一样会需要在agent执行安装指令(与CI pipeline一样,可以回去複习)。增加一个bash task,执行install_requirements.sh:
(install_requirements.sh在_MLOps-WorkShop-1-CI/landing/s/package_requirement/)
接下来新增一个Azure CLI任务,取名为「Staging test」,执行pytest,pytest解释详见之前位于CI pipeline文章的解说:
在inline script当中输入:
pytest staging_test.py --doctest-modules --junitxml=junit/test-results.xml --cov-report=xml --cov-report=html --scoreurl $(az ml service show -g $(azureml.resourceGroup) -w $(azureml.workspaceName) -n $(service.name.staging) --query scoringUri -o tsv)
这边特别说明一下,agent要测试的endpoint url需要由--scoreurl
参数带入,az ml service show指令可以吐出部署的service detail,但我们只需要url资讯,所以az ml service show后面会有--query scoringUri参数。
其他细节请查阅官方文件。
staging_test.py档案位在【pipeline published artifact区域】/_MLOps-WorkShop-1-CI/landing/s/tests/integration
,所以往下捲动,设定工作目录:
2-5 agent任务:发布endpoint测试结果
前一步骤使用pytest测试,并产出report,新增一个任务把这些报告发布到Azure DevOps中。搜寻publish result,add后修改名称Publish Staging Test Results。前一个步骤有设定pytest报告的档名都叫做test-,所以Test results files填入:**/test-*.xml
CD pipeline的第一部分:模型自动部署到测试环境(staging area),到这里就设定完成了!记得按储存!
3. 设定CD pipeline触发条件
CD pipeline artifact来源是CI pipeline产生的artifact,一旦有新artifact就触发CD pipeline。
所以接下来要设定:
(1)CD pipeline artifact来源
(2)触发条件
回到这条release pipeline的首页,点选artifact,Source (build pipeline)处填写:CI pipeline,选择最新latest:
设定Continuous deployment trigger,点击闪电处:
打开Continuous deployment trigger:
4.试跑第一部分CD pipeline
虽然我们设定好触发条件,不过在真的让他自动触发前,还是先执行一次这个pipeline看看,点选页面右上方的create release:
然后点选Create:
和CI pipeline一样,每次执行会有一个编号(run ID),下图显示这次release pipline正在运行:
运行执行成功后,可以检视一下发布的测试报告:
如果运行执行成功,可以在Azure Machine Learning当中,看到我们部署的service:
下一篇【Azure MLOps - 7】建立CD pipeline:把机器学习模型部署到production area