前の記事 の最後に載せていたライブデモは AWS Lambda にデプロイしていた。公式の解説記事(Hosting Hugging Face models on AWS Lambda for serverless inference | AWS Compute Blog)だとモデルを EFS にキャッシュさせることになっているけれど、Docker イメージに詰め込んでしまったほうがシンプルで作りやすかったので、そうした。
Dockerfile は以下のような感じで、使用するモデルは transformers-cli download
であらかじめダウンロードしておく。もちろん自前のモデルの場合は必要ない。
FROM public.ecr.aws/lambda/python:3.8 COPY requirements.txt ./ RUN pip install --disable-pip-version-check -r requirements.txt && pip cache purge ENV TRANSFORMERS_CACHE /var/cache/transformers RUN transformers-cli download rinna/japanese-gpt2-small COPY app.py . CMD ["app.lambda_handler"]
全体はこちら: GitHub - motemen/example-huggingface-transformer-on-serverless
TRANSFORMERS_CACHE
を設定しておかないと、以下のようなエラーが出た。書いてて気づいたけど、ビルド時と実行時とで HOME が違うからだな。
There was a problem when trying to write in your cache folder (/home/XXXXXXXX/.cache/huggingface/hub). You should set the environment variable TRANSFORMERS_CACHE to a writable directory. mkdir(name, mode)/python3.8/os.py", line 223, in makedirs/file_download.py", line 1038, in hf_hub_downloadtrained
small モデルの場合はメモリは 2GB あればいける模様。API Gateway の制限の 30 秒以内には生成できる(ことが多い)。
% curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/generate-small -d '{"text":"こんにちは、"}' | jq . { "results": [ "こんにちは、たねるです♪ 先日テレビや雑誌で「アザフェイス」というタレントさんが大絶賛! タレントさんたちの間で流行", "こんにちは、さて、 前回は、前々回のブログの更新からだいぶ時間が経過してしまいましたが、 さて、今回は前々", "こんにちは、今日もお越しいただきまして誠にありがとうございました! 早速、私が... 続きを読む 【今日のオススメ" ] }
あとたまに以下のようなエラーが出るのだけど再現性がなくてなんともできていない……。
Error in cpuinfo: failed to parse the list of possible processors in /sys/devices/system/cpu/possible Error in cpuinfo: failed to parse the list of present processors in /sys/devices/system/cpu/present Error in cpuinfo: failed to parse both lists of possible and present processors