【问题标题】:Use SageMaker Lifecycle configuration to execute a jupyter notebook on start使用 SageMaker 生命周期配置在启动时执行 jupyter notebook
【发布时间】:2021-02-03 13:35:16
【问题描述】:

我想设置一些自动计划来运行我的 SageMaker Notebook。
目前我找到了这样的链接:
https://towardsdatascience.com/automating-aws-sagemaker-notebooks-2dec62bc2c84

我按照步骤设置了 lamda、cloudwatch 和 Lifecycle 配置。
在不同的实验中,有时 on_start 生命周期配置可以执行 jupyter 笔记本(在笔记本中我只是安装一些包并加载包并将加载状态保存到 S3 存储桶)。但是,由于无法停止笔记本而失败。

然后,我为我的 IAM 角色添加了 SageMaker 自动停止权限。现在可以打开和关闭笔记本实例。但我再也看不到任何上传到我的 S3 的内容了。我想知道 on_start 是否在完成步骤之前过早地启动了自动停止?

下面是我当前生命周期配置的脚本

set -e

ENVIRONMENT=python3
NOTEBOOK_FILE="/home/ec2-user/SageMaker/Test Notebook.ipynb"
AUTO_STOP_FILE="/home/ec2-user/SageMaker/auto-stop.py"

source /home/ec2-user/anaconda3/bin/activate "$ENVIRONMENT"

nohup jupyter nbconvert --ExecutePreprocessor.timeout=-1 --ExecutePreprocessor.kernel_name=python3 --execute "$NOTEBOOK_FILE" &

echo "Finishing running the jupyter notebook"

source /home/ec2-user/anaconda3/bin/deactivate

# PARAMETERS
IDLE_TIME=60  # 1 minute

echo "Fetching the autostop script"
wget -O autostop.py https://raw.githubusercontent.com/mariokostelac/sagemaker-setup/master/scripts/auto-stop-idle/autostop.py

echo "Starting the SageMaker autostop script in cron"
(crontab -l 2>/dev/null; echo "*/1 * * * * /bin/bash -c '/usr/bin/python3 $DIR/autostop.py --time ${IDLE_TIME} | tee -a /home/ec2-user/SageMaker/auto-stop-idle.log'") | crontab -

请注意,我确实从 cloudwatch 日志中看到了“正在完成运行 jupyter 笔记本”的回显。但这通常是我从日志中看到的第一件事,它会立即显示 - 比我预期的需要多长时间。

另外,目前笔记本只运行一些假任务。真正的任务可能需要一个多小时。

任何建议都有帮助!感谢您花时间阅读我的问题。

【问题讨论】:

  • 我遇到了同样的问题。笔记本实例没有机会启动。 Lambda 触发 notebook-instance 启动,然后触发此生命周期配置,该配置在 notebook 实例中运行特定的 ipynb 文件。就我而言,我查看笔记本实例,发现它仍处于“待处理”状态。我想我需要在生命周期配置文件中建立一些空闲时间,但我不知道该怎么做。

标签: python amazon-web-services jupyter-notebook lifecycle amazon-sagemaker


【解决方案1】:

这里发生了什么

nohup jupyter nbconvert --ExecutePreprocessor.timeout=-1 --ExecutePreprocessor.kernel_name=python3 --execute "$NOTEBOOK_FILE" &

nohup 允许笔记本继续执行并忽略时间限制。 & 将笔记本执行推到后台。

结合起来,脚本开始在后台执行笔记本,忽略时间限制,而脚本的其余部分继续运行。这就是为什么在 notebook 开始在后台执行后,您会直接在日志中看到“Finishing running the jupyter notebook”。

现在你的生命周期配置流程是:

  1. 开始在后台执行笔记本
  2. 在笔记本中等待 60 秒不活动
  3. 自动停止实例

实例在明显不活动一分钟后停止,并且由于笔记本在后台执行,因此它被视为不活动。由于您的笔记本运行时间超过一分钟,因此它没有完成并且文件没有写入 s3。

要让笔记本有更多时间运行,请增加空闲时间。

【讨论】:

  • nohup 防止出现 HUP 信号,这里没有时间限制(尽管您的 SSH 会话超时会产生 HUP 信号)。
【解决方案2】:

当你说

我确实从 cloudwatch 日志中看到了“完成运行 jupyter 笔记本”的回声。但这通常是我从日志中看到的第一件事,它会立即显示 - 比我预期的需要多长时间。

当您的脚本中有这一行时,这是预期的

nohup jupyter nbconvert --ExecutePreprocessor.timeout=-1 --ExecutePreprocessor.kernel_name=python3 --execute "$NOTEBOOK_FILE" &

nohup 帮助进程保持运行,即使您从终端注销。 & 将进程发送到后台。 因此,下一个命令会在这一行之后立即运行。

您可能在这里使用了“nohup”和“&”,因为运行笔记本所需的时间比 LifecycleConfiguration 脚本的最大允许时间长,我认为这很好。


现在可以打开和关闭笔记本实例。但我再也看不到任何上传到我的 S3 的内容了。我想知道 on_start 是否在完成步骤之前过早地启动了自动停止?

在你的脚本中

(crontab -l 2>/dev/null; echo "*/1 * * * * /bin/bash -c '/usr/bin/python3 $DIR/autostop.py --time ${IDLE_TIME} | tee -a /home/ec2-user/SageMaker/auto-stop-idle.log'") | crontab -

这是设置每分钟运行的Cron 作业。该作业执行 $DIR/autostop.py 脚本(看起来 $DIR 的值不是顺便设置的)。并且 autostop.py 脚本使用 $IDLE_TIME 来确定它是否应该调用 stop_notebook_instance API。

没有详细了解 autostop.py 脚本的作用。您可能需要调整 Cron 作业的频率,或调整 $IDLE_TIME。

另一个想法是,既然你说你真正的笔记本需要 1 个多小时,也许你可以让笔记本在最后一个单元格调用 stop_notebook_instance API。

六月

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 2019-02-16
    • 2019-12-30
    • 2021-11-01
    • 2022-11-15
    相关资源
    最近更新 更多