From 0ba302f7d7680de974690567c003f07154b81d4b Mon Sep 17 00:00:00 2001 From: liaogang Date: Wed, 14 Sep 2016 14:51:15 +0800 Subject: [PATCH] fix bug on paddle api when WITH_DOUBLE --- paddle/api/Matrix.cpp | 32 +++++++++++++++--------------- paddle/api/PaddleAPI.h | 45 +++++++++++++++++++++--------------------- paddle/api/Util.cpp | 4 ++-- paddle/api/Vector.cpp | 30 ++++++++++++++-------------- 4 files changed, 56 insertions(+), 55 deletions(-) diff --git a/paddle/api/Matrix.cpp b/paddle/api/Matrix.cpp index 6a79f83495..c40a47f3ac 100644 --- a/paddle/api/Matrix.cpp +++ b/paddle/api/Matrix.cpp @@ -44,7 +44,7 @@ Matrix* Matrix::createZero(size_t height, size_t width, bool useGpu) { return m; } -Matrix* Matrix::createDense(const std::vector& data, size_t height, +Matrix* Matrix::createDense(const std::vector& data, size_t height, size_t width, bool useGpu) { auto m = new Matrix(); m->m->mat = paddle::Matrix::create(height, width, useGpu); @@ -52,7 +52,7 @@ Matrix* Matrix::createDense(const std::vector& data, size_t height, return m; } -Matrix* Matrix::createCpuDenseFromNumpy(float* data, int dim1, int dim2, +Matrix* Matrix::createCpuDenseFromNumpy(real* data, int dim1, int dim2, bool copy) { auto m = new Matrix(); if (copy) { @@ -64,7 +64,7 @@ Matrix* Matrix::createCpuDenseFromNumpy(float* data, int dim1, int dim2, return m; } -Matrix* Matrix::createGpuDenseFromNumpy(float* data, int dim1, int dim2) { +Matrix* Matrix::createGpuDenseFromNumpy(real* data, int dim1, int dim2) { auto m = new Matrix(); m->m->mat = paddle::Matrix::create(dim1, dim2, false, true); m->m->mat->copyFrom(data, dim1 * dim2); @@ -86,7 +86,7 @@ size_t Matrix::getHeight() const { return m->mat->getHeight(); } size_t Matrix::getWidth() const { return m->mat->getWidth(); } -float Matrix::get(size_t x, size_t y) const throw(RangeError) { +real Matrix::get(size_t x, size_t y) const throw(RangeError) { if (x > this->getWidth() || y > this->getHeight()) { RangeError e; throw e; @@ -94,8 +94,8 @@ float Matrix::get(size_t x, size_t y) const throw(RangeError) { return m->mat->getElement(x, y); } -void Matrix::set(size_t x, size_t y, float val) throw(RangeError, - UnsupportError) { +void Matrix::set(size_t x, size_t y, real val) throw(RangeError, + UnsupportError) { if (x > this->getWidth() || y > this->getHeight()) { RangeError e; throw e; @@ -193,10 +193,10 @@ FloatArray Matrix::getData() const { auto rawMat = m->mat.get(); if (dynamic_cast(rawMat->getMemoryHandle().get())) { // is gpu. then copy data - float* data = rawMat->getData(); + real* data = rawMat->getData(); size_t len = rawMat->getElementCnt(); - float* cpuData = new float[len]; - hl_memcpy_device2host(cpuData, data, len * sizeof(float)); + real* cpuData = new real[len]; + hl_memcpy_device2host(cpuData, data, len * sizeof(real)); FloatArray ret_val(cpuData, len); ret_val.needFree = true; return ret_val; @@ -208,7 +208,7 @@ FloatArray Matrix::getData() const { void Matrix::sparseCopyFrom( const std::vector& rows, const std::vector& cols, - const std::vector& vals) throw(UnsupportError) { + const std::vector& vals) throw(UnsupportError) { auto cpuSparseMat = std::dynamic_pointer_cast(m->mat); if (cpuSparseMat != nullptr) { @@ -217,7 +217,7 @@ void Matrix::sparseCopyFrom( // <<" ValSize = "<copyFrom(const_cast&>(rows), const_cast&>(cols), - const_cast&>(vals)); + const_cast&>(vals)); } else { UnsupportError e; throw e; @@ -226,7 +226,7 @@ void Matrix::sparseCopyFrom( void* Matrix::getSharedPtr() const { return &m->mat; } -void Matrix::toNumpyMatInplace(float** view_data, int* dim1, +void Matrix::toNumpyMatInplace(real** view_data, int* dim1, int* dim2) throw(UnsupportError) { auto cpuMat = std::dynamic_pointer_cast(m->mat); if (cpuMat) { @@ -237,9 +237,9 @@ void Matrix::toNumpyMatInplace(float** view_data, int* dim1, throw UnsupportError(); } } -void Matrix::copyToNumpyMat(float** view_m_data, int* dim1, +void Matrix::copyToNumpyMat(real** view_m_data, int* dim1, int* dim2) throw(UnsupportError) { - static_assert(sizeof(paddle::real) == sizeof(float), + static_assert(sizeof(paddle::real) == sizeof(real), "Currently PaddleAPI only support for single " "precision version of paddle."); if (this->isSparse()) { @@ -247,7 +247,7 @@ void Matrix::copyToNumpyMat(float** view_m_data, int* dim1, } else { *dim1 = m->mat->getHeight(); *dim2 = m->mat->getWidth(); - *view_m_data = new float[(*dim1) * (*dim2)]; + *view_m_data = new real[(*dim1) * (*dim2)]; if (auto cpuMat = dynamic_cast(m->mat.get())) { auto src = cpuMat->getData(); auto dest = *view_m_data; @@ -264,7 +264,7 @@ void Matrix::copyToNumpyMat(float** view_m_data, int* dim1, } } -void Matrix::copyFromNumpyMat(float* data, int dim1, +void Matrix::copyFromNumpyMat(real* data, int dim1, int dim2) throw(UnsupportError, RangeError) { if (isSparse()) { throw UnsupportError(); diff --git a/paddle/api/PaddleAPI.h b/paddle/api/PaddleAPI.h index 79487c4cf4..69f3240a77 100644 --- a/paddle/api/PaddleAPI.h +++ b/paddle/api/PaddleAPI.h @@ -20,6 +20,7 @@ limitations under the License. */ #include #include #include "paddle/utils/GlobalConstants.h" +#include "paddle/utils/TypeDefs.h" /// Import PaddlePaddle's enumeration into global namespace. using namespace paddle::enumeration_wrapper; // NOLINT @@ -55,10 +56,10 @@ class UnsupportError {}; /// This type will map to python's list of float. struct FloatArray { - const float* buf; + const real* buf; const size_t length; bool needFree; // true if the buf is dynamic alloced. - FloatArray(const float* b, const size_t l); + FloatArray(const real* b, const size_t l); }; /// This type will map to python's list of int @@ -71,11 +72,11 @@ struct IntArray { /// This type will map to python's list of (int, float) struct IntWithFloatArray { - const float* valBuf; + const real* valBuf; const int* idxBuf; const size_t length; bool needFree; - IntWithFloatArray(const float* v, const int* i, size_t l, bool f = false); + IntWithFloatArray(const real* v, const int* i, size_t l, bool f = false); }; enum SparseValueType { SPARSE_NON_VALUE = 0, SPARSE_VALUE = 1 }; @@ -121,7 +122,7 @@ public: * @param data list of float should be passed in python. * @note the value will be copy into a new matrix. */ - static Matrix* createDense(const std::vector& data, size_t height, + static Matrix* createDense(const std::vector& data, size_t height, size_t width, bool useGpu = false); /** @@ -133,11 +134,11 @@ public: * @param copy true if copy into a new matrix, false will create * matrix inplace. */ - static Matrix* createCpuDenseFromNumpy(float* data, int dim1, int dim2, + static Matrix* createCpuDenseFromNumpy(real* data, int dim1, int dim2, bool copy = false); /// Create Gpu Dense Matrix from numpy matrix, dtype=float32 - static Matrix* createGpuDenseFromNumpy(float* data, int dim1, int dim2); + static Matrix* createGpuDenseFromNumpy(real* data, int dim1, int dim2); /** * Cast to numpy matrix. @@ -153,15 +154,15 @@ public: * numpy_mat = m.toNumpyMat() * @endcode */ - void toNumpyMatInplace(float** view_data, int* dim1, + void toNumpyMatInplace(real** view_data, int* dim1, int* dim2) throw(UnsupportError); /// Copy To numpy mat. - void copyToNumpyMat(float** view_m_data, int* dim1, + void copyToNumpyMat(real** view_m_data, int* dim1, int* dim2) throw(UnsupportError); /// Copy From Numpy Mat - void copyFromNumpyMat(float* data, int dim1, int dim2) throw(UnsupportError, + void copyFromNumpyMat(real* data, int dim1, int dim2) throw(UnsupportError, RangeError); /// return true if this matrix is sparse. @@ -180,9 +181,9 @@ public: size_t getWidth() const; - float get(size_t x, size_t y) const throw(RangeError); + real get(size_t x, size_t y) const throw(RangeError); - void set(size_t x, size_t y, float val) throw(RangeError, UnsupportError); + void set(size_t x, size_t y, real val) throw(RangeError, UnsupportError); /// return type is list of float FloatArray getData() const; @@ -194,8 +195,8 @@ public: */ void sparseCopyFrom(const std::vector& rows, const std::vector& cols, - const std::vector& values = - std::vector()) throw(UnsupportError); + const std::vector& values = + std::vector()) throw(UnsupportError); bool isGpu() const; @@ -227,33 +228,33 @@ public: * * It will create a new vector, and copy data into it. */ - static Vector* create(const std::vector& data, bool useGpu = false); + static Vector* create(const std::vector& data, bool useGpu = false); /** * Create Cpu Vector from numpy array, which dtype=float32 * * If copy is false, it will create vector inplace. */ - static Vector* createCpuVectorFromNumpy(float* data, int dim, + static Vector* createCpuVectorFromNumpy(real* data, int dim, bool copy = false); /// Create Gpu Vector from numpy array, which dtype=float32 - static Vector* createGpuVectorFromNumpy(float* data, int dim); + static Vector* createGpuVectorFromNumpy(real* data, int dim); /// Cast to numpy array inplace. - void toNumpyArrayInplace(float** view_data, int* dim1) throw(UnsupportError); + void toNumpyArrayInplace(real** view_data, int* dim1) throw(UnsupportError); /// Copy to numpy array. - void copyToNumpyArray(float** view_m_data, int* dim1); + void copyToNumpyArray(real** view_m_data, int* dim1); /// Copy from numpy array. - void copyFromNumpyArray(float* data, int dim); + void copyFromNumpyArray(real* data, int dim); /// __getitem__ in python - float get(const size_t idx) const throw(RangeError, UnsupportError); + real get(const size_t idx) const throw(RangeError, UnsupportError); /// __setitem__ in python - void set(const size_t idx, float val) throw(RangeError, UnsupportError); + void set(const size_t idx, real val) throw(RangeError, UnsupportError); /// Return is GPU vector or not. bool isGpu() const; diff --git a/paddle/api/Util.cpp b/paddle/api/Util.cpp index 8a6741078f..fe89a62cd3 100644 --- a/paddle/api/Util.cpp +++ b/paddle/api/Util.cpp @@ -31,13 +31,13 @@ void initPaddle(int argc, char** argv) { feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW); } -FloatArray::FloatArray(const float* b, const size_t l) +FloatArray::FloatArray(const real* b, const size_t l) : buf(b), length(l), needFree(false) {} IntArray::IntArray(const int* b, const size_t l, bool f) : buf(b), length(l), needFree(f) {} -IntWithFloatArray::IntWithFloatArray(const float* v, const int* i, size_t l, +IntWithFloatArray::IntWithFloatArray(const real* v, const int* i, size_t l, bool f) : valBuf(v), idxBuf(i), length(l), needFree(f) {} diff --git a/paddle/api/Vector.cpp b/paddle/api/Vector.cpp index 1affc1a5fe..b61eb7934b 100644 --- a/paddle/api/Vector.cpp +++ b/paddle/api/Vector.cpp @@ -140,7 +140,7 @@ struct VectorPrivate { paddle::VectorPtr vec; void safeAccessData(const size_t idx, - const std::function& func) const + const std::function& func) const throw(RangeError, UnsupportError) { auto cpuVec = std::dynamic_pointer_cast(vec); if (cpuVec != nullptr) { @@ -170,7 +170,7 @@ Vector* Vector::createZero(size_t sz, bool useGpu) { return retVec; } -Vector* Vector::create(const std::vector& data, bool useGpu) { +Vector* Vector::create(const std::vector& data, bool useGpu) { auto retVec = new Vector(); retVec->m->vec = paddle::Vector::create(data.size(), useGpu); retVec->m->vec->copyFrom(data.data(), data.size()); @@ -188,7 +188,7 @@ Vector* Vector::createByPaddleVectorPtr(void* ptr) { } } -Vector* Vector::createCpuVectorFromNumpy(float* data, int dim, bool copy) { +Vector* Vector::createCpuVectorFromNumpy(real* data, int dim, bool copy) { CHECK_GT(dim, 0); auto retVec = new Vector(); if (copy) { @@ -200,7 +200,7 @@ Vector* Vector::createCpuVectorFromNumpy(float* data, int dim, bool copy) { return retVec; } -Vector* Vector::createGpuVectorFromNumpy(float* data, int dim) { +Vector* Vector::createGpuVectorFromNumpy(real* data, int dim) { CHECK_GT(dim, 0); auto retVec = new Vector(); retVec->m->vec = paddle::Vector::create((size_t)dim, true); @@ -208,7 +208,7 @@ Vector* Vector::createGpuVectorFromNumpy(float* data, int dim) { return retVec; } -void Vector::toNumpyArrayInplace(float** view_data, +void Vector::toNumpyArrayInplace(real** view_data, int* dim1) throw(UnsupportError) { auto v = std::dynamic_pointer_cast(m->vec); if (v != nullptr) { @@ -219,20 +219,20 @@ void Vector::toNumpyArrayInplace(float** view_data, } } -void Vector::copyToNumpyArray(float** view_m_data, int* dim1) { +void Vector::copyToNumpyArray(real** view_m_data, int* dim1) { *dim1 = m->vec->getSize(); - *view_m_data = new float[*dim1]; + *view_m_data = new real[*dim1]; if (auto cpuVec = dynamic_cast(m->vec.get())) { - std::memcpy(*view_m_data, cpuVec->getData(), sizeof(float) * (*dim1)); + std::memcpy(*view_m_data, cpuVec->getData(), sizeof(real) * (*dim1)); } else if (auto gpuVec = dynamic_cast(m->vec.get())) { hl_memcpy_device2host(*view_m_data, gpuVec->getData(), - sizeof(float) * (*dim1)); + sizeof(real) * (*dim1)); } else { LOG(INFO) << "Unexpected situation"; } } -void Vector::copyFromNumpyArray(float* data, int dim) { +void Vector::copyFromNumpyArray(real* data, int dim) { m->vec->resize(dim); m->vec->copyFrom(data, dim); } @@ -241,15 +241,15 @@ bool Vector::isGpu() const { return std::dynamic_pointer_cast(m->vec) != nullptr; } -float Vector::get(const size_t idx) const throw(RangeError, UnsupportError) { - float r; - m->safeAccessData(idx, [&](float& o) { r = o; }); +real Vector::get(const size_t idx) const throw(RangeError, UnsupportError) { + real r; + m->safeAccessData(idx, [&](real& o) { r = o; }); return r; } -void Vector::set(const size_t idx, float val) throw(RangeError, +void Vector::set(const size_t idx, real val) throw(RangeError, UnsupportError) { - m->safeAccessData(idx, [&](float& o) { o = val; }); + m->safeAccessData(idx, [&](real& o) { o = val; }); } size_t Vector::getSize() const { return m->vec->getSize(); } -- GitLab