You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS SageMaker Pipeline中CreateModelStep转TransformStep遇模块未找到错误

AWS SageMaker Pipeline TransformStep 报错:ModuleNotFoundError: No module named 'inference'

报错信息

Traceback (most recent call last):
  File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_modules.py", line 258, in import_module
    module = importlib.import_module(name)
  File "/miniconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1006, in _gcd_import
  File "", line 983, in _find_and_load
  File "", line 965, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'inference'

步骤定义代码

model_step = CreateModelStep(
    name='CreateModel',
    model=Model(
        name='RandomForestModel',
        image_uri=training_step.properties.AlgorithmSpecification.TrainingImage,
        model_data=training_step.properties.ModelArtifacts.S3ModelArtifacts,
        sagemaker_session=sagemaker_session,
        role=role
    ),
    inputs=CreateModelInput(
        instance_type=instance_type,
        accelerator_type='ml.eia1.medium',
    )
)


transform_step = TransformStep(
    name='Transform',
    transformer=training_estimator.transformer(
        instance_count=instance_count, 
        instance_type=instance_type,
        accept='text/csv',
        env={
            'SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT': 'text/csv', 
            'SAGEMAKER_USE_NGINX': 'False', 
            'SAGEMAKER_PROGRAM': 'inference.py',
            'SAGEMAKER_REGION': region,
            'SAGEMAKER_SUBMIT_DIRECTORY': '/opt/ml/model/'
        },
        model_name=model_step.properties.ModelName,
        output_path=f's3://{default_bucket}/RandomForestTransform'
    ),
    inputs=TransformInput(data=batch_data)
)

问题分析与解决方法

1. SAGEMAKER_PROGRAM 参数错误

你设置的SAGEMAKER_PROGRAM='inference.py'是核心问题:SageMaker加载推理脚本时,需要的是Python模块名(不带.py后缀),而非文件名。指定inference.py会让它尝试导入名为inference.py的模块,自然找不到,应改为inference。

2. 推理脚本的归档与位置问题

SAGEMAKER_SUBMIT_DIRECTORY指定为/opt/ml/model/,意味着inference.py必须存在于该目录下。需确保训练阶段把inference.py和模型文件一起打包上传到S3的模型 artifacts 中,这样CreateModel步骤才会将其下载到目标目录。

如果用自定义训练脚本,训练时要把inference.py放在source_dir目录下,或通过合理方式将其包含进模型归档,避免模型 artifacts 里只有模型文件、缺失推理脚本。

3. 冗余环境变量清理

SAGEMAKER_USE_NGINX='False'在批量转换场景下无意义,批量转换不需要NGINX服务,建议移除该环境变量,避免不必要的配置干扰。

修正后的TransformStep代码

transform_step = TransformStep(
    name='Transform',
    transformer=training_estimator.transformer(
        instance_count=instance_count, 
        instance_type=instance_type,
        accept='text/csv',
        env={
            'SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT': 'text/csv', 
            'SAGEMAKER_PROGRAM': 'inference',  # 移除.py后缀
            'SAGEMAKER_REGION': region
            # 无需手动指定SAGEMAKER_SUBMIT_DIRECTORY,让SageMaker自动处理路径
        },
        model_name=model_step.properties.ModelName,
        output_path=f's3://{default_bucket}/RandomForestTransform'
    ),
    inputs=TransformInput(data=batch_data)
)

额外检查点

  • 登录S3控制台,查看训练生成的模型.tar.gz包内容,确认其中包含inference.py文件。
  • 若训练阶段使用了source_dir,确保inference.py在该目录下,且训练脚本没有排除该文件。

内容的提问来源于stack exchange,提问作者Jane Wayne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 02:15:42