您可以通过Serverless Devs开发工具快速将AI推理类的应用部署到函数计算控制台,从而实现按需、自动扩展的AI模型推理,无需管理底层基础设施,节省成本并快速迭代。
背景信息
本文示例实现的能力是识别上传的照片内的动物是猫还是狗。关于代码详情,请参见示例工程。
前提条件
操作步骤
执行以下命令,克隆项目。
git clone https://github.com/awesome-fc/cat-dog-classify.git
安装依赖。
执行以下命令进入项目目录。
cd cat-dog-classify
执行以下命令安装依赖。
sudo s build --use-docker
输出示例:
[2021-12-09 07:26:39] [INFO] [S-CLI] - Start ... [2021-12-09 07:26:40] [INFO] [FC-BUILD] - Build artifact start... [2021-12-09 07:26:40] [INFO] [FC-BUILD] - Use docker for building. [2021-12-09 07:26:40] [INFO] [FC-BUILD] - Build function using image: registry.<regionId>.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.9.20 [2021-12-09 07:26:40] [INFO] [FC-BUILD] - begin pulling image registry.<regionId>.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.9.20, you can also use docker pull registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.9.20 to pull image by yourself. build-1.9.20: Pulling from aliyunfc/runtime-python3.6 f49cf87b52c1: Already exists ...... 01ce50b4eb85: Already exists 02b807385deb: Pull complete ...... 9b9fdb8de506: Pull complete Digest: sha256:a9a6dab2d6319df741ee135d9749a90b2bb834fd11ee265d1fb106053890**** Status: Downloaded newer image for registry.<regionId>.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.9.20 builder begin to build [2021-12-09 07:27:57] [INFO] [FC-BUILD] - Build artifact successfully. Tips for next step ====================== * Invoke Event Function: s local invoke * Invoke Http Function: s local start * Deploy Resources: s deploy End of method: build
执行完安装依赖的命令后,Serverless Devs会自动安装相关依赖包,并将第三方库下载到.s/build/artifacts/cat-dog/classify/.s/python目录内。
上传依赖到NAS。
当您在安装依赖时,函数计算引用的代码包在解压后可能会出现大于代码包限制的情况,为了减少代码包的体积,您可以将大体积的依赖和相对较大的模型参数文件存放在NAS中。
执行以下命令,初始化NAS。
sudo s nas init
输出示例:
[2021-12-09 07:29:58] [INFO] [S-CLI] - Start ... [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using region: cn-shenzhen [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using access alias: default [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9**** [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN**** ...... [2021-12-09 07:30:01] [INFO] [FC-DEPLOY] - Generated vpcConfig: securityGroupId: sg-wz90u1syk2h1f14b**** vSwitchId: vsw-wz9qnuult4q5g4f7n**** vpcId: vpc-wz9x9bzs0wtvjgt6n**** ...... [2021-12-09 07:30:15] [INFO] [FC-DEPLOY] - Checking Trigger httpTrigger exists Make service _FC_NAS_cat-dog success. Make function _FC_NAS_cat-dog/nas_dir_checker success. Make trigger _FC_NAS_cat-dog/nas_dir_checker/httpTrigger success. [2021-12-09 07:30:25] [INFO] [FC-DEPLOY] - Checking Service _FC_NAS_cat-dog exists [2021-12-09 07:30:25] [INFO] [FC-DEPLOY] - Checking Function nas_dir_checker exists [2021-12-09 07:30:26] [INFO] [FC-DEPLOY] - Checking Trigger httpTrigger exists There is auto config in the service: _FC_NAS_cat-dog [2021-12-09 07:30:26] [INFO] [FC-DEPLOY] - Generated nasConfig: groupId: 10003 mountPoints: - fcDir: /mnt/auto nasDir: /cat-dog serverAddr: 2bfb748****.cn-shenzhen.nas.aliyuncs.com userId: 10003 cat-dog: userId: 10003 groupId: 10003 mountPoints: - serverAddr: 2bfb748****.cn-shenzhen.nas.aliyuncs.com nasDir: /cat-dog fcDir: /mnt/auto
执行以下命令,部署服务。
sudo s deploy service
执行以下命令,上传依赖到NAS。
sudo s nas upload -r .s/build/artifacts/cat-dog/classify/.s/python/ /mnt/auto/python
输出示例:
[2021-12-09 07:33:14] [INFO] [S-CLI] - Start ... Packing ... Package complete. Upload done Tips for next step ====================== * Invoke remote function: s invoke End of method: nas
执行以下命令,上传模型到NAS。
sudo s nas upload -r src/model/ /mnt/auto/model
输出示例:
[2021-12-09 07:52:26] [INFO] [S-CLI] - Start ... Packing ... Package complete. Upload done Tips for next step ====================== * Invoke remote function: s invoke End of method: nas
执行以下命令,查看NAS目录。
sudo s nas command ls /mnt/auto/
输出示例:
[2021-12-09 07:53:01] [INFO] [S-CLI] - Start ... model python Tips for next step ====================== * Invoke remote function: s invoke End of method: nas
执行以下命令,部署项目。
sudo s deploy
输出示例:
[2021-12-09 07:56:15] [INFO] [S-CLI] - Start ... [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using region: cn-shenzhen [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using access alias: default [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9**** [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN**** ...... [2021-12-09 07:56:19] [INFO] [FC-DEPLOY] - Generated logConfig: enableInstanceMetrics: true enableRequestMetrics: true logBeginRule: ~ logstore: fc-service-cat-dog-logstore project: 188077086902****-cn-shenzhen-logproject ...... There is auto config in the service: cat-dog Tips for next step ====================== * Display information of the deployed resource: s info * Display metrics: s metrics * Display logs: s logs * Invoke remote function: s invoke * Remove Service: s remove service * Remove Function: s remove function * Remove Trigger: s remove trigger * Remove CustomDomain: s remove domain cat-dog: region: cn-shenzhen service: name: cat-dog function: name: classify runtime: python3 handler: predict.handler memorySize: 1024 timeout: 120 url: system_url: https://188077086902****.cn-shenzhen.fc.aliyuncs.com/2016-08-15/proxy/cat-dog/classify/ custom_domain: - domain: http://classify.cat-dog.188077086902****.cn-shenzhen.fc.devsapp.net triggers: - type: http name: httpTrigger
成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该域名可以访问刚部署的函数。生成的域名的格式为:
http://classify.cat-dog.<account_id>.<region_id>.fc.devsapp.net
。使用浏览器访问该域名,上传图片后识别到的结果如下:
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见配置自定义域名。
使用预留消除冷启动毛刺
函数计算具有动态伸缩的特性,根据并发请求量,自动弹性扩容出执行环境。在这个典型的深度学习示例中,加载依赖和模型参数消耗的时间很长,在您设置的1 GB规格的函数中,并发访问的时间为10s左右,有时可能大于20s。
因此不可避免的会出现函数调用毛刺的情况,即冷启动时间大于10s,在这种情况下,您可以使用设置预留的方式来避免冷启动。您可以在项目目录内执行以下命令消除冷启动毛刺:
sudo s provision put --target 10 --qualifier LATEST
同时,当您需要了解服务器的最大承受能力,实现更好地运行和开发时,您可以使用Serverless Devs的压测命令对指定的函数进行压测。详细信息,请参见Serverless Devs操作命令。
您可以通过以下命令获取预留实例详情:
sudo s provision get --qualifier LATEST
当您完成压测后,请执行以下命令取消预留:
sudo s provision put --target 0 --qualifier LATEST
相关文档
通过Serverless Devs工具进行函数构建/依赖安装等操作,请参见构建函数或安装依赖(s build)。
通过Serverless Devs工具进行预留配置的查看与更新等操作,请参见函数预留操作(s provision)。