Commit 6a4741bc by xiaotong

clean the code

parent d992a0e9
...@@ -30,9 +30,9 @@ namespace nts { // namespace nts(NiuTrans.Tensor) ...@@ -30,9 +30,9 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
void _Gather(const XTensor * s, XTensor * t, int dim, int * srcIndex, int indexSize); void _Gather(const XTensor * s, XTensor * t, int dim, int * srcIndex, int indexSize);
/* gather selected sub-tensors (return a XTensor structure) /* gather selected sub-tensors (return a XTensor structure)
make a new tensor to keep the result and return it */ make a new tensor to keep the result and return it */
XTensor Gather(const XTensor &s, int dim, int * srcIndex, int indexSize); XTensor Gather(const XTensor &s, int dim, int * srcIndex, int indexSize);
} // namespace nts(NiuTrans.Tensor) } // namespace nts(NiuTrans.Tensor)
#endif // __GATHER_H__ #endif // __GATHER_H__
\ No newline at end of file
...@@ -62,7 +62,6 @@ void _Spread(XTensor * source, XTensor * collection, int dim, ...@@ -62,7 +62,6 @@ void _Spread(XTensor * source, XTensor * collection, int dim,
int * srcIndex, int indexSize, int * collIndex) int * srcIndex, int indexSize, int * collIndex)
{ {
int order = source->order; int order = source->order;
int size = source->GetDim(dim);
CheckNTErrors(source->dataType == DEFAULT_DTYPE, "TODO!"); CheckNTErrors(source->dataType == DEFAULT_DTYPE, "TODO!");
CheckNTErrors(dim >= 0 && dim < order, "Illegal dimension!"); CheckNTErrors(dim >= 0 && dim < order, "Illegal dimension!");
...@@ -150,7 +149,6 @@ void _SpreadForGather(XTensor * source, XTensor * collection, int dim, ...@@ -150,7 +149,6 @@ void _SpreadForGather(XTensor * source, XTensor * collection, int dim,
int * srcIndex, int indexSize, int * collIndex) int * srcIndex, int indexSize, int * collIndex)
{ {
int order = source->order; int order = source->order;
int size = source->GetDim(dim);
CheckNTErrors(source->dataType == DEFAULT_DTYPE, "TODO!"); CheckNTErrors(source->dataType == DEFAULT_DTYPE, "TODO!");
CheckNTErrors(dim >= 0 && dim < order, "Illegal dimension!"); CheckNTErrors(dim >= 0 && dim < order, "Illegal dimension!");
...@@ -199,4 +197,4 @@ void _SpreadForGather(XTensor * source, XTensor * collection, int dim, ...@@ -199,4 +197,4 @@ void _SpreadForGather(XTensor * source, XTensor * collection, int dim,
} }
} }
} // namespace nts(NiuTrans.Tensor) } // namespace nts(NiuTrans.Tensor)
\ No newline at end of file
...@@ -61,42 +61,39 @@ void _CrossEntropy(const XTensor * output, const XTensor * gold, ...@@ -61,42 +61,39 @@ void _CrossEntropy(const XTensor * output, const XTensor * gold,
CheckNTErrors(loss->order == output->order - 1, "Wrong loss dimension!"); CheckNTErrors(loss->order == output->order - 1, "Wrong loss dimension!");
CheckNTErrors(gold->dataType == DEFAULT_DTYPE && output->dataType == DEFAULT_DTYPE, "TODO!"); CheckNTErrors(gold->dataType == DEFAULT_DTYPE && output->dataType == DEFAULT_DTYPE, "TODO!");
XTensor * logInter = NewTensorBuf(output, output->devID, output->mem);
XTensor * mulInter = NewTensorBuf(output, output->devID, output->mem);
XTensor * negInter = NewTensorBuf(output, output->devID, output->mem);
XTensor * logBuf = NewTensorBuf(output, output->devID, output->mem); XTensor * logBuf = NewTensorBuf(output, output->devID, output->mem);
XTensor * mulBuf = NewTensorBuf(output, output->devID, output->mem); XTensor * mulBuf = NewTensorBuf(output, output->devID, output->mem);
XTensor * negBuf = NewTensorBuf(output, output->devID, output->mem);
/* l = log(output) */ /* l = log(output) */
_Log(output, logBuf); _Log(output, logBuf);
if(weight != NULL){ if(weight != NULL){
XTensor * weightBuf = NewTensorBuf(output, output->devID, output->mem); XTensor * weightBuf = NewTensorBuf(output, output->devID, output->mem);
/* multiply gold and weight by broadcast wg = mulDim(g * w) */
/* multiply gold with weight by broadcast wg = mulDim(g * w) */
_MultiplyDim(gold, weight, weightBuf, n, 0); _MultiplyDim(gold, weight, weightBuf, n, 0);
/* multiply weighted gold and log(output) wgl = mul(wg, l) */
/* multiply weighted gold with log(output) wgl = mul(wg, l) */
_Multiply(weightBuf, logBuf, mulBuf, 0); _Multiply(weightBuf, logBuf, mulBuf, 0);
DelTensorBuf(weightBuf); DelTensorBuf(weightBuf);
} }
else{ else{
/* multiply gold and log(output) gl = mul(g, l) */ /* multiply gold with log(output) gl = mul(g, l) */
_Multiply(gold, logBuf, mulBuf, 0); _Multiply(gold, logBuf, mulBuf, 0);
} }
/* negate multiply result n = negate(mul) */ /* negate result n = negate(mul) */
_NegateMe(mulBuf); _NegateMe(mulBuf);
_ReduceSum(mulBuf, loss, n); _ReduceSum(mulBuf, loss, n);
DelTensorBuf(negInter); DelTensorBuf(mulBuf);
DelTensorBuf(mulInter); DelTensorBuf(logBuf);
DelTensorBuf(logInter);
} }
/* /*
compute the cross entropy loss (implementation manually) compute the cross entropy loss (faster implementation with optimized code)
loss = sum_{i} (-gold_i * log(output_i)) loss = sum_{i} (-gold_i * log(output_i))
where gold and output are distributions where gold and output are distributions
...@@ -108,9 +105,9 @@ where gold and output are distributions ...@@ -108,9 +105,9 @@ where gold and output are distributions
>> padding - specify a target value that is ignored and does not contribute to the loss computation >> padding - specify a target value that is ignored and does not contribute to the loss computation
>> leadingDim - the leading dimension for the output >> leadingDim - the leading dimension for the output
*/ */
void _CrossEntropyManual(const XTensor * output, const XTensor * gold, void _CrossEntropyFast(const XTensor * output, const XTensor * gold,
XTensor * loss, const XTensor * weight, XTensor * loss, const XTensor * weight,
const XTensor * padding, int leadingDim) const XTensor * padding, int leadingDim)
{ {
#ifdef USE_CUDA #ifdef USE_CUDA
if(output->devID >= 0) { if(output->devID >= 0) {
...@@ -263,21 +260,22 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold, ...@@ -263,21 +260,22 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold,
XTensor * logBuf = NewTensorBuf(output, output->devID, output->mem); XTensor * logBuf = NewTensorBuf(output, output->devID, output->mem);
XTensor * mulBuf = NewTensorBuf(output, output->devID, output->mem); XTensor * mulBuf = NewTensorBuf(output, output->devID, output->mem);
XTensor * negBuf = NewTensorBuf(output, output->devID, output->mem);
/* l = log(output) */ /* l = log(output) */
_Log(output, logBuf); _Log(output, logBuf);
if(weight != NULL){ if(weight != NULL){
XTensor * weightBuf = NewTensorBuf(output, output->devID, output->mem); XTensor * weightBuf = NewTensorBuf(output, output->devID, output->mem);
/* multiply gold and weight by broadcast wg = mulDim(g * w) */
/* multiply gold with weight by broadcast wg = mulDim(g * w) */
_MultiplyDim(gold, weight, weightBuf, n, 0); _MultiplyDim(gold, weight, weightBuf, n, 0);
/* multiply weighted gold and log(output) wgl = mul(wg, l) */
/* multiply weighted gold with log(output) wgl = mul(wg, l) */
_Multiply(weightBuf, logBuf, mulBuf, 0); _Multiply(weightBuf, logBuf, mulBuf, 0);
DelTensorBuf(weightBuf); DelTensorBuf(weightBuf);
} }
else{ else{
/* multiply gold and log(output) gl = mul(g, l) */ /* multiply gold with log(output) gl = mul(g, l) */
_Multiply(gold, logBuf, mulBuf, 0); _Multiply(gold, logBuf, mulBuf, 0);
} }
...@@ -291,7 +289,6 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold, ...@@ -291,7 +289,6 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold,
/* reduce sum all classes */ /* reduce sum all classes */
_ReduceSum(mulBuf, lossInter, n); _ReduceSum(mulBuf, lossInter, n);
DelTensorBuf(negBuf);
DelTensorBuf(mulBuf); DelTensorBuf(mulBuf);
DelTensorBuf(logBuf); DelTensorBuf(logBuf);
...@@ -334,7 +331,7 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold, ...@@ -334,7 +331,7 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold,
} }
/* /*
compute the cross entropy loss (implementation manually) compute the cross entropy loss (faster implementation with optimized code)
loss = sum_{i} (-gold_i * log(output_i)) loss = sum_{i} (-gold_i * log(output_i))
where gold and output are distributions where gold and output are distributions
...@@ -347,9 +344,9 @@ where gold and output are distributions ...@@ -347,9 +344,9 @@ where gold and output are distributions
>> leadingDim - the leading dimension for the output >> leadingDim - the leading dimension for the output
<< return - the cross entropy loss that is a scalar << return - the cross entropy loss that is a scalar
*/ */
DTYPE _CrossEntropyManual(const XTensor * output, const XTensor * gold, DTYPE _CrossEntropyFast(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay, const XTensor * weight, LOSS_COMPUTE_WAY reduceWay, const XTensor * weight,
const XTensor * padding, int leadingDim) const XTensor * padding, int leadingDim)
{ {
#ifdef USE_CUDA #ifdef USE_CUDA
if(output->devID >= 0) { if(output->devID >= 0) {
...@@ -459,7 +456,7 @@ DTYPE _CrossEntropyManual(const XTensor * output, const XTensor * gold, ...@@ -459,7 +456,7 @@ DTYPE _CrossEntropyManual(const XTensor * output, const XTensor * gold,
} }
/* /*
backward compuation for cross entropy function (tensor version) backward compuation for cross entropy function
loss = sum_{i} (-t_i * log(y_i)) loss = sum_{i} (-t_i * log(y_i))
dE/dy_i = -t_i / y_i dE/dy_i = -t_i / y_i
...@@ -575,4 +572,4 @@ void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor ...@@ -575,4 +572,4 @@ void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor
} }
} }
} // namespace nts(NiuTrans.Tensor) } // namespace nts(NiuTrans.Tensor)
\ No newline at end of file
...@@ -111,7 +111,7 @@ where gold and output are distributions ...@@ -111,7 +111,7 @@ where gold and output are distributions
>> padding - specify a target value that is ignored and does not contribute to the loss computation >> padding - specify a target value that is ignored and does not contribute to the loss computation
>> leadingDim - the leading dimension for the output >> leadingDim - the leading dimension for the output
*/ */
void _CudaCrossEntropyManual(const XTensor * output, const XTensor * gold, void _CudaCrossEntropyFast(const XTensor * output, const XTensor * gold,
XTensor * loss, const XTensor * weight, XTensor * loss, const XTensor * weight,
const XTensor * padding, int leadingDim) const XTensor * padding, int leadingDim)
{ {
...@@ -201,9 +201,9 @@ where gold and output are distributions ...@@ -201,9 +201,9 @@ where gold and output are distributions
>> leadingDim - the leading dimension for the output >> leadingDim - the leading dimension for the output
<< return - the cross entropy loss that is a scalar << return - the cross entropy loss that is a scalar
*/ */
DTYPE _CudaCrossEntropyManual(const XTensor * output, const XTensor * gold, DTYPE _CudaCrossEntropyFast(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay, const XTensor * weight, LOSS_COMPUTE_WAY reduceWay, const XTensor * weight,
const XTensor * padding, int leadingDim) const XTensor * padding, int leadingDim)
{ {
DTYPE loss = 0; DTYPE loss = 0;
...@@ -414,4 +414,4 @@ void _CudaCrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTe ...@@ -414,4 +414,4 @@ void _CudaCrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTe
} // namespace nts(NiuTrans.Tensor) } // namespace nts(NiuTrans.Tensor)
#endif // __CROSSENTROPY_CUH__ #endif // __CROSSENTROPY_CUH__
\ No newline at end of file
...@@ -27,13 +27,13 @@ ...@@ -27,13 +27,13 @@
namespace nts{ // namespace nts(NiuTrans.Tensor) namespace nts{ // namespace nts(NiuTrans.Tensor)
/* compute the cross entropy loss (tensor version) */ /* compute the cross entropy loss */
void _CudaCrossEntropyManual(const XTensor * output, const XTensor * gold, void _CudaCrossEntropyFast(const XTensor * output, const XTensor * gold,
XTensor * loss, const XTensor * weight = NULL, XTensor * loss, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1); const XTensor * padding = NULL, int leadingDim = -1);
/* compute the cross entropy loss (scalar version) */ /* compute the cross entropy loss */
DTYPE _CudaCrossEntropyManual(const XTensor * output, const XTensor * gold, DTYPE _CudaCrossEntropyFast(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay, const XTensor * weight = NULL, LOSS_COMPUTE_WAY reduceWay, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1); const XTensor * padding = NULL, int leadingDim = -1);
...@@ -45,4 +45,4 @@ void _CudaCrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTe ...@@ -45,4 +45,4 @@ void _CudaCrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTe
} // namespace nts(NiuTrans.Tensor) } // namespace nts(NiuTrans.Tensor)
#endif // __CROSSENTROPY_CUH__ #endif // __CROSSENTROPY_CUH__
\ No newline at end of file
...@@ -31,25 +31,25 @@ REDUCE_SUM, ...@@ -31,25 +31,25 @@ REDUCE_SUM,
REDUCE_MEAN REDUCE_MEAN
}; };
/* compute the cross entropy loss (tensor version) */ /* compute the cross entropy loss */
void _CrossEntropy(const XTensor * output, const XTensor * gold, void _CrossEntropy(const XTensor * output, const XTensor * gold,
XTensor * loss, const XTensor * weight = NULL, XTensor * loss, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1); const XTensor * padding = NULL, int leadingDim = -1);
/* compute the cross entropy loss (tensor version) */ /* compute the cross entropy loss */
void _CrossEntropyManual(const XTensor * output, const XTensor * gold, void _CrossEntropyFast(const XTensor * output, const XTensor * gold,
XTensor * loss, const XTensor * weight = NULL, XTensor * loss, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1); const XTensor * padding = NULL, int leadingDim = -1);
/* compute the cross entropy loss (scalar version) */ /* compute the cross entropy loss (return the loss) */
DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold, DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay, const XTensor * weight = NULL, LOSS_COMPUTE_WAY reduceWay, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1); const XTensor * padding = NULL, int leadingDim = -1);
/* compute the cross entropy loss (scalar version) */ /* compute the cross entropy loss (return the loss) */
DTYPE _CrossEntropyManual(const XTensor * output, const XTensor * gold, DTYPE _CrossEntropyFast(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay = REDUCE_MEAN, const XTensor * weight = NULL, LOSS_COMPUTE_WAY reduceWay = REDUCE_MEAN, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1); const XTensor * padding = NULL, int leadingDim = -1);
/* backward computation of cross entropy function */ /* backward computation of cross entropy function */
void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor * gold, void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor * gold,
...@@ -58,4 +58,4 @@ void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor ...@@ -58,4 +58,4 @@ void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor
} // namespace nts(NiuTrans.Tensor) } // namespace nts(NiuTrans.Tensor)
#endif // __CROSSENTROPY_H__ #endif // __CROSSENTROPY_H__
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论