deepFM 多gpu报错
GodeWithWind opened this issue · 3 comments
GodeWithWind commented
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1!
下面是代码
model = DeepFM(linear_feature_columns=linear_feature_columns, dnn_feature_columns=dnn_feature_columns,
task='binary', device=device, gpus=[0, 1], dnn_dropout=0.5)
zanshuxun commented
报错的具体堆栈看下
zanshuxun commented
报错堆栈
原因分析
这个bug是我们之前为了解决#192 发布的2d56269 这次改动中引入的,当时考虑到sparse_embedding_list
可能为空,因此将sparse_embedding_list[0].device
改为了self.device
。但是多gpu训练时self.device
只是gpus[0]
这一个gpu,所以会导致linear_logit
和sparse_feat_logit
不在同一个gpu上,继而引发如上报错。
解决方案
多gpu训练时,有两种方法可以获取当前所在的gpu:
- 通过输入数据来获取,需要先对输入特征进行判空。如下图(具体代码见80fb344 ):
- 通过某个已注册的模型参数来获取,这里可以用
self.weight.device
,如下图:
但是当输入的dense_feature_columns
为空时(例如AFM模型,或其他用户未使用dense feature的情况),不会创建self.weight参数,需要先进行判断再使用。
我们准备采用方案1,会在后续的发布中合并此处改动,感谢您的提问。
iamsile commented
@zanshuxun 您好,请问这个改动什么时候可以发布哈?