SINGA-502 Avoid moving data between host and gpu in SoftmaxCrossEntropy #577
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The softmax_cross_entropy move to data to host and then back to gpu, so the whole function needed to be changed for many reasons such as efficiency and asynchronization (and buffering operation in the future). :
class SoftMaxCrossEntropy(Operation):
def init(self, t):
super(SoftMaxCrossEntropy, self).init()
self.t = t.data
def forward(self, x):
self.p = singa.SoftMax
loss = CTensor((1,), self.p.device())
ret = singa.CrossEntropyFwd(self.p, self.t)
loss.SetFloatValue(singa.SumAsFloat(ret) / x.shape()[0])
return loss
Here the SumAsFloat return a c++ float value, and this value is read back to gpu in the SetFloatValue function.