在实际集群中使用 MultiWorkerMirroredStrategy 时,Tensorflow 分布式学习不起作用
我只是想遵循tensorflow文档中的 MultiWorkerMirroredStrategy 的示例。 我在具有单个节点的本地主机中成功进行了训练。 但是,我在有两个节点的集群中训练失败。 我尝试过禁用防火墙,但没有解决问题。
这是main.py。 (我在节点 1 和节点 2 中运行相同的代码,除了 tf_config 变量。我将 node1 的 tf_config 设置为 tf_config['task']['index']=0
,将 node2 的 tf_config 设置为 tf_config['任务']['索引']=1
)
任何帮助表示赞赏。谢谢。
I am just trying to follow example of MultiWorkerMirroredStrategy in tensorflow doc.
I succeed training in localhost, which has a single node.
However, I failed training in cluster, which has two nodes.
I have tried disabling firewall, but it didn't solve the problem.
Here is the main.py. (I run same code in node 1 and node 2, except the tf_config variable. I set node1's tf_config as tf_config['task']['index']=0
, and node2's tf_config as tf_config['task']['index']=1
)
Any helps Appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现您没有错误代码,但我想我可以推断问题可能出现在哪里,因为您的代码应该可以工作。一旦有机会,我将在我的 kubernetes 上进行测试(我有一个 atm 节点)。
最有可能出现的问题。您正在使用 json.dumps() 设置环境变量。在许多设置中,您应该使用:
tf_config=json.loads(os.environ.get(TF_CONFIG) 或 '{}'),
TASK_INDEX=tf_config['任务']['索引']
这应该可以解决暴露端口和 IP 配置的任何问题。
- 听起来你使用的方法是在笔记本上?因为您没有为 main.py 运行相同的代码。就像在一个 main.py 中一样,您设置 1,另一个设置 0。无论哪种方式,这都不是您在这里所做的。您将索引设置为 1 和 0,但您不会仅返回索引,您将返回完整的集群规范以及您设置的索引。如果您的集群未设置环境变量,您将需要取回已设置的 TF_CONFIG,然后使用 load 将其设置为您的 tf_config,现在您将仅获取该节点的副本索引。
如果您使用的是笔记本电脑,则需要将其连接到集群环境,否则您将向您的计算机设置本地环境变量,而不是集群上的容器。考虑使用 Kubeflow 来管理此问题。
您可以在设置集群后从笔记本启动
配置操作,或将 TF_job 规范构建为定义节点规范的 YAML,然后使用该规范启动 pod。
-尝试创建一个函数,该函数将返回每个工作线程的索引,以测试它是否在 kubernetes 仪表板或 kubectl 上设置为相同的副本索引。确保让函数打印出来,以便您可以在 pod 日志中看到它。这将有助于调试。
- 查看 Pod 日志并查看 Pod 是否正在连接到服务器,以及是否正在使用与您的集群兼容的任何通信规范:grcp/等。您没有设置通信策略,但在大多数情况下它应该能够自动为您找到它(只需检查以防万一)。
-如果您能够启动 Pod,请确保在重试之前将其终止。一旦你掌握了 python pipeline skd 的窍门,kubeflow 会让你的事情变得更加容易。您可以将函数作为容器启动。您可以通过终止旧的 Pod 来构建一个清理操作。
-您应该考虑将 main.py 和任何其他支持模块加载到存储库(例如 dockerhub)中的映像上,以便容器可以加载映像。使用多工作策略,每台机器都需要具有相同的数据才能正确分片。再次检查您的 pod 日志,看看它是否无法对数据进行分片。
- 您是否在具有不同 GPU 的本地计算机上运行?如果是这样,您应该使用镜像策略而不是多人工作。
I see that you don't have an error code, but I think I can infer where the issue could be arising, since your code should work. I will test on my kubernetes once I get a chance (I have a node down atm).
The most likely issue. You are using json.dumps() to set the environment variable. In many setting the you should be using:
tf_config=json.loads(os.environ.get(TF_CONFIG) or '{}'),
TASK_INDEX=tf_config['task']['index']
That should clear up any issues with expose ports and ip configurations.
-It sounds like the method you are using is in a notebook? Since you are not running the the same code for main.py. As in one main.py you set 1 and the other 0. Either way that is not what you are doing here. You are setting the index to 1 and 0 but you are not getting back only the index, you are getting back the full cluster spec with the index you set it to. If the environment variable is not set from your cluster, you will need to get back the TF_CONFIG that was set, and then use loads to set that as your tf_config, now you will be getting ONLY the replica index for that node.
If you are using a notebook it needs to be connected to the cluster environment, otherwise you are setting a local environment variable to your machine, and not to containers on the cluster. Consider using Kubeflow, to manage this.
You can either launch from the notebook after setting up your cluster
configuration op, or build a TF_job spec as a YAML that defined the node specs, then launch the pods using that spec.
Either way, the cluster needs to actually have that configuration, you should be able to load the environment in the cluster such that each node is ASSIGNED an index and you are getting that index from THAT nodes replica ID that you set when you launched the nodes and specified with a YAML or json dictionary. A locally set environment running within the local container means nothing to the actual cluster, if the replica-index:{num} on kubernetes does not match the environment variable on the container —That is assigned when the pod is launched.
-Try making a function that will return what the index of each worker is to test if it is set to the same replica-index on your kubernetes dashboard or from kubectl. Make sure to have the function print that out so you can see it in the pod logs. This will help with debugging.
-Look at the pod logs and see if the pods are connecting to the server, and are using whatever communication spec is compatible with your cluster: grcp/etc. You are not setting a communication strategy, but it should be able to automatically find it for you in most cases (just check in case).
-If you are able to launch pods, make sure you are terminating them before trying again. Again kubeflow is going to make things so much easier for you once you get the hang of their python pipeline skd. You can launch functions as containers. You can build an op that clean up, by terminating old pods.
-You should consider having your main.py and any other supporting modules loaded on an image in a repository, such as dockerhub, so that the containers can load the image. With Multiworker Strategy, each machine needs to have the same data for it to be sharded properly. Again check your pod logs to see if it cannot shard the data.
-Are you running on a local machine with different GPUs? If so you should be using Mirrored Strategy NOT multiworker.
所以有很多可能出问题的地方都与你的配置有关,你运行的是什么?您使用的是 minikube,还是拆分了 CPU 核心?我可以提出一般性改进建议,帮助您的代码处理更多通信选项。此外,您似乎没有数据分发选项。您放置在策略上下文管理器范围内的唯一内容是模型定义和编译。看起来这两个节点共享同一端口。最好将 TF_config 设置为传递到
'tf_config = json.loads(os.environ.get('TF_CONFIG') or '{}')' 的
环境变量,这是我在过去它既可以通过 mini-kube 在多个 CPU 线程上运行,也可以在 Kubernetes 框架上运行。
模型替换为简单的 mnist 模型。
So there are a lot of things that could be going wrong that are related to your configuration, what are you running on? Are you using minikube, or are you splitting your CPU cores? I can suggest general improvements that could help with your code to handle more communication options. Also you don't seem to have data distribution options. The only thing you place within the scope of the strategy context manager is the model definition and compile. It also looks like those two nodes are sharing the same port. It's better to set TF_config as an environment variable that is passed into
'tf_config = json.loads(os.environ.get('TF_CONFIG') or '{}')'
Here is an example of set-ups I have used in the past it worked both running on multiple CPU threads through mini-kube, and on a Kubernetes framework.
Model replaced with a simple mnist model.