1个不稳定版本
0.1.0 | 2019年11月15日 |
---|
#1703 在 开发工具
37KB
726 行
Tensorflow容器管理器
将Tensorflow打包到Linux发行版非常困难,甚至不可能。Tensorflow的每个版本都需要一系列可能的构建配置,这需要为每个Tensorflow版本构建许多Tensorflow变体。更糟糕的是,Tensorflow的新版本将依赖于大量共享依赖项,这些依赖项可能不支持仍然由发行版维护者积极支持的较老版本的Linux发行版。
为了解决这个问题,Tensorflow项目提供了官方的Docker容器构建版本,这允许Tensorflow在隔离的环境中运行,不受系统其余部分的影响。这个虚拟环境可以独立于基本系统运行,允许你在支持Docker运行时的任何Linux发行版上使用Tensorflow的任何版本。
然而,使用docker
命令行配置和管理Tensorflow的Docker容器目前很繁琐,而管理不同项目的多个版本则更为繁琐。为了解决我们用户的问题,我们开发了tensorman
作为一个方便的工具来管理Tensorflow Docker容器的安装和执行。它将命令行的混乱简化为一组易于记忆的简单命令。
与Docker命令的对比
以下是一个Docker调用的示例
docker run -u $UID:$UID -v $PWD:/project -w /project \
--runtime=nvidia --it --rm tensorflow/tensorflow:latest-gpu \
python ./script.py
这指定了使用带有GPU支持的最新版本的Tensorflow,将工作目录挂载到/project
,以当前用户账户启动容器,并在容器中执行script.py
,使用容器中的Python二进制文件。使用tensorman
,我们可以用以下方式实现相同的效果
tensorman run --gpu python -- ./script.py
默认情况下为最新版本,其版本和标签变体可以按运行、按项目或按用户范围设置默认值。
安装/更新容器
默认情况下,当运行尚未安装的容器时,docker将自动安装容器。但是,如果您希望预先安装容器,可以使用pull
子命令进行安装。
tensorman pull 1.14.0
tensorman pull latest
在容器中运行命令
子命令 run
允许您在容器内执行命令。这可能是在容器内进行交互会话的 bash
shell,或者您希望运行的程序/编译器。
# Default container version with Bash prompt
tensorman run bash
# Default container version with Python script
tensorman run python -- script.py
# Default container version with GPU support
tensorman run --gpu bash
# With GPU, Python3, and Juypyter support
tensorman run --gpu --python3 --jupyter bash
设置容器版本
受到 rustup 的启发,有方法可以按运行、按项目和按用户设置容器版本。按运行版本始终优先于按项目定义的版本,而按项目定义的版本又优先于按用户配置的版本。
设置按运行
如果在一个 +
参数之后指定了版本,tensorman
将优先使用此版本。
tensorman +1.14.0 run --python3 --gpu bash
可以使用 =
参数指定自定义镜像。
tensorman =custom-image run --gpu bash
设置按项目
可以使用两个文件来配置 Tensorman 本地:tensorflow-toolchain
和 Tensorman.toml
。如果它们位于父目录中,这些文件将被自动检测。
tensorflow-toolchain
此文件覆盖了在 Tensorman.toml
或用户全局配置文件中定义的 tensorflow 镜像。
1.14.0 gpu python3
或指定自定义镜像
=custom-image gpu
Tensorman.toml
此文件支持额外的配置参数,用户全局配置位于 ~/.config/tensorman/config.toml
,项目全局位置在 Tensorman.toml
。您可能想要使用此文件的原因之一是声明一些额外的 Docker 标志,使用 docker_flags
键。
使用默认 tensorflow 镜像
docker_flags = [ '-p', '8080:8080' ]
tag = '2.0.0'
variants = ['gpu', 'python3']
定义自定义镜像
docker_flags = [ '-p', '8080:8080' ]
image = 'custom-image'
variants = ['gpu']
设置按用户
您可以使用 default
子命令设置用户级别的默认版本。每次您使用 tensorman run
命令时,都会启动此版本的 Tensorflow。
tensorman default 1.14.0
tensorman default latest gpu python3
tensorman default nightly
默认情况下,
tensorman
将使用latest
作为默认的用户版本标签。
显示活动容器版本
如果您想知道从当前工作目录启动时将使用哪个容器,可以使用 show
命令。
tensorman show
删除容器镜像
在同一系统上同时安装许多容器会快速消耗大量磁盘存储空间。如果您需要删除系统上安装的容器,可以使用 remove
命令。
tensorman remove 1.14.0
tensorman remove latest
tensorman remove 481cb7ea88260404
tensorman remove custom-image
列出已安装的容器镜像
为了帮助发现系统上安装了哪些容器,可以使用 list
子命令。
tensorman list
创建自定义镜像
在大多数项目中,您需要比基础 Tensorflow 镜像更多的依赖项。为此,您需要创建镜像,方法是在一个终端中运行 tensorflow 容器作为 root,安装并设置环境以满足您的需求,然后将这些更改保存为新的自定义镜像。
为此,您需要在一个终端中构建容器,然后在另一个终端中保存它。
构建新镜像
首先在您将开始配置 docker 镜像的终端中启动终端
tensorman run --gpu --python3 --root --name CONTAINER_NAME bash
一旦您完成了所需的更改,请打开另一个终端并将其保存为新镜像
tensorman save CONTAINER_NAME IMAGE_NAME
运行自定义镜像
然后您应该能够像这样使用 tensorman 指定该容器
tensorman =IMAGE_NAME run --gpu bash
--python3
和--jupyter
标志对自定义容器没有作用,但--gpu
是必需的,以启用对 GPU 的运行时支持。
删除自定义镜像
通过 tensorman 保存的镜像可以通过 tensorman 管理。列出和删除的方式相同
tensorman remove IMAGE_NAME
许可证
根据 GNU 通用公共许可证第 3 版许可(LICENSE 或 https://gnu.ac.cn/licenses/gpl-3.0.en.html)
贡献
您有意提交以包含在作品中的任何贡献,都应根据GNU GPLv3进行授权。
依赖项
~18–30MB
~469K SLoC