Requirements.txt 未使用 Sagemaker Pytorch Estimator 打包在 model.tar.gz 中

问题描述 投票:0回答:3

我正在使用 SageMaker Pipeline 工作流来训练模型并注册它。稍后我将从注册模型创建一个端点。

我需要在我的 inference.py 文件中安装一些 python 包,比如 gensim。我将 requirements.txt 文件放在与 train.py 和 inference.py 相同的文件夹中。

问题是 requirements.txt 没有打包在 model.tar.gz 中。 这就是为什么虽然训练和创建端点工作正常,但是当我检查已部署端点的日志记录时,我看到以下错误:

ModuleNotFoundError: No module named 'gensim'

这是我训练和注册模型的脚本的一部分。

from sagemaker.pytorch.estimator import PyTorch
from sagemaker.workflow.step_collections import RegisterModel
from sagemaker.workflow.steps import (
    ProcessingStep,
    TrainingStep,
)

    train_estimator = PyTorch(entry_point= 'train.py',
                                source_dir= BASE_DIR,
                                instance_type= "ml.m5.2xlarge",
                                instance_count=1,
                                role=role,
                                framework_version='1.8.0',
                                py_version='py3',
                                )
    step_train = TrainingStep(
        name="TrainStep",
        estimator=train_estimator,
        inputs={
                "train": sagemaker.TrainingInput(
                            s3_data=step_process.properties.ProcessingOutputConfig.Outputs[
                            "train_data"
                            ].S3Output.S3Uri,
                            content_type= 'text/csv',
                        ),
        },
    )
    step_register = RegisterModel(
        name="RegisterStep",
        estimator= train_estimator,
        model_data=step_train.properties.ModelArtifacts.S3ModelArtifacts,
        content_types=["application/json"],
        response_types=["application/json"],
        inference_instances=["ml.t2.medium", "ml.m5.2xlarge"],
        transform_instances=["ml.m5.large"],
        model_package_group_name=model_package_group_name,
        approval_status=model_approval_status,
        source_dir = BASE_DIR,
        entry_point= os.path.join(BASE_DIR, "inference.py"),
        depends_on = [step_train]
    )

这是我的文件结构:

-abalone
  - __init__.py
  - train.py
  - inference.py
  - requirements.py
  - preprocess.py
  - evaluate.py
  - pipeline.py

BASE_DIR 指的是鲍鱼文件夹。

在model.tar.gz中我看到:

- model.pth
- model.pth.wv.vectors_ngrams.npy
- code
  - __pycache__
  - train.py
  - _repack_model.py
  - inference.py
  - preprocess.py
  - evaluate.py
  - __init__.py
  - pipeline.py

可以看到里面除了requirements.txt文件,其他都包含了

在贤者文件里面说:

“PyTorch 和 PyTorchModel 类重新打包 model.tar.gz 以包含推理脚本(和相关文件),只要 framework_version 设置为 1.2 或更高。”

但是你可以看到虽然我的framework_version高于1.2,但是仍然没有在model.tar.gz中打包requirements.txt文件

有人可以帮我解决这个问题吗?

pytorch amazon-sagemaker endpoint inference requirements.txt
3个回答
0
投票

解决方法是在

inference.py
中安装所需的包使用

import os 
os.execute("pip install package1 package2 ...")

要解决此问题,我建议使用

train_estimator
部署估算器
train_estimator.deploy
,这将创建模型、端点配置和端点。然后,检查 CloudWatch 日志以查看它是否仍然无法打包
requirements.txt
.

requirements.txt
可能用作由
estimator.deploy
创建的模型附带的环境变量,因为您使用
RegisterModel
,它会忽略该参数。


0
投票

在您的屏幕截图中,您的基本文件夹中有 requirements.py 而不是 requirements.txt


0
投票

0

我在 sagemaker 培训工作中使用 requirement.txt 文件安装以下包

requirements.txt文件在代码目录下

numpy
rouge_score
fire
transformers>=4.26.1
torch
sentencepiece
tokenizers==0.12.1
wandb
git+https://github.com/zphang/transformers@llama_push

我面临的问题: 我无法从这个特定的 PR 访问类 https://github.com/zphang/transformers@llama_push 因为这也应该像其他人一样安装。我用本地 python 虚拟环境测试了这个 requirements.txt 文件,它正在运行并安装所有包,包括上面提供的特定 PR。

什么问题?

SageMaker training job 不使用 git 安装 repos 吗?

我的估算器代码如下

estimator = PyTorch(entry_point='train.py',
                    source_dir='code', 
                    role=role[0],
                    framework_version='1.9',
                    py_version='py38',
                    instance_count=1,
                    instance_type='ml.m5.large',
                    input_mode='File')
estimator.fit({'train': train_input})

请帮我解决这个问题

© www.soinside.com 2019 - 2024. All rights reserved.