diff --git a/paddle/cuda/include/hl_matrix.h b/paddle/cuda/include/hl_matrix.h index c7f25109972195fb56b9e96c4b68d952363e6338..7daca18761b80eac0f876b21377a6ccc6a853485 100644 --- a/paddle/cuda/include/hl_matrix.h +++ b/paddle/cuda/include/hl_matrix.h @@ -300,4 +300,12 @@ extern void hl_matrix_col2Vol(real* dataDst, real alpha, real beta); +/** + * @brief Matrix col2Vol: Convert col matrix into 3D volume + * @param[out] out output int vector. + * @param[in] vec input float vector. + * @param[in] size size of the vector. + */ +extern void hl_vector_cast2int(int* out, real* vec, int size); + #endif /* HL_MATRIX_H_ */ diff --git a/paddle/cuda/include/stub/hl_matrix_stub.h b/paddle/cuda/include/stub/hl_matrix_stub.h index 6ac332945c8f09fef23f35680ba5bb1d9ba9f4fd..46e77e140768dd80fd327dd4eb3b0f62a3370950 100644 --- a/paddle/cuda/include/stub/hl_matrix_stub.h +++ b/paddle/cuda/include/stub/hl_matrix_stub.h @@ -133,4 +133,6 @@ inline void hl_matrix_col2Vol(real* dataDst, real alpha, real beta) {} +inline void hl_vector_cast2int(int* out, real* vec, int size) {} + #endif // HL_MATRIX_STUB_H_ diff --git a/paddle/cuda/src/hl_cuda_matrix.cu b/paddle/cuda/src/hl_cuda_matrix.cu index b41a3a1e06db7b2566acef19ce430645f79d486d..607efb4f6b0aa0d22a2789397b8743f7a5271d5b 100644 --- a/paddle/cuda/src/hl_cuda_matrix.cu +++ b/paddle/cuda/src/hl_cuda_matrix.cu @@ -793,3 +793,14 @@ void hl_matrix_col2Vol(real* dataDst, CHECK_SYNC("hl_matrix_col2Vol failed"); } + +__global__ void keVectorCast2Int(int* out, real* vec, int size) { + for (int i = threadIdx.x; i < (size); i += blockDim.x) { + out[i] = int(vec[i]); + } +} + +void hl_vector_cast2int(int* out, real* vec, int size) { + keVectorCast2Int<<<1, 512, 0, STREAM_DEFAULT>>>(out, vec, size); + CHECK_SYNC("hl_vector_cast2int failed"); +} diff --git a/paddle/gserver/evaluators/Evaluator.cpp b/paddle/gserver/evaluators/Evaluator.cpp index 9db6d252d97bfeee3fe376bcda431fe94c65a678..87cb2d280866ac2be2d6f85e872e547e12548feb 100644 --- a/paddle/gserver/evaluators/Evaluator.cpp +++ b/paddle/gserver/evaluators/Evaluator.cpp @@ -395,14 +395,24 @@ real AucEvaluator::evalImp(std::vector& arguments) { CHECK_LE(arguments.size(), (size_t)3); MatrixPtr output = arguments[0].value; IVectorPtr label = arguments[1].ids; + MatrixPtr labelval = arguments[1].value; bool supportWeight = (3 == arguments.size()) ? true : false; MatrixPtr weight = supportWeight ? arguments[2].value : nullptr; - if (nullptr == output || nullptr == label || - (supportWeight && nullptr == weight)) { + + if (nullptr == output || (supportWeight && nullptr == weight)) { return 0; } size_t insNum = output->getHeight(); size_t outputDim = output->getWidth(); + // Copy label from value to a vector. + if (nullptr == label && nullptr != labelval) { + // label width is 1 + CHECK_EQ(1, labelval->getWidth()); + VectorPtr vec = + Vector::create(labelval->getData(), insNum, output->useGpu()); + label = vec->castToInt(); + } + CHECK_EQ(insNum, label->getSize()); if (supportWeight) { CHECK_EQ(insNum, weight->getHeight()); @@ -443,6 +453,7 @@ real AucEvaluator::evalImp(std::vector& arguments) { int* labelD = label->getData(); real* weightD = supportWeight ? weight->getData() : nullptr; size_t pos = realColumnIdx_; + for (size_t i = 0; i < insNum; ++i) { real value = outputD[pos]; uint32_t binIdx = static_cast(value * kBinNum_); diff --git a/paddle/math/Vector.cpp b/paddle/math/Vector.cpp index ff72672e3ab77212b309fcfea835839a916fa632..346008439c35a2bcbcd2e9dfd36d689e01d7495f 100644 --- a/paddle/math/Vector.cpp +++ b/paddle/math/Vector.cpp @@ -18,6 +18,7 @@ limitations under the License. */ #include #include "Matrix.h" #include "hl_gpu.h" +#include "hl_matrix.h" #include "hl_table_apply.h" #include "paddle/utils/Flags.h" #include "paddle/utils/Logging.h" @@ -99,6 +100,19 @@ MatrixPtr VectorT::toOneHotSparseMatrix(size_t idRange, bool useGpu) { return mat; } +template <> +std::shared_ptr> VectorT::castToInt() { + std::shared_ptr> ret = IVector::create(this->getSize(), useGpu_); + if (useGpu_) { + hl_vector_cast2int(ret->getData(), this->getData(), this->getSize()); + } else { + for (size_t i = 0; i < getSize(); ++i) { + ret->getData()[i] = int(this->getData()[i]); + } + } + return ret; +} + template GpuVectorT::GpuVectorT(size_t size) : VectorT(size, diff --git a/paddle/math/Vector.h b/paddle/math/Vector.h index 80b9775fccf10c57bb48145ef56165ec7c86d8b8..f965a5809209da313c78a545c44e7aa39e95ac65 100644 --- a/paddle/math/Vector.h +++ b/paddle/math/Vector.h @@ -162,6 +162,13 @@ public: */ std::shared_ptr toOneHotSparseMatrix(size_t idRange, bool useGpu); + /** + * @brief cast vector of "real" elements to "int" elements. + * + * @note: float -> int must be casted, or you'll get wrong data. + */ + std::shared_ptr> castToInt(); + /** * This function will crash if the size of src and dest is different. */