Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#384 from wangyang59/trainerAPIGpu
Browse files Browse the repository at this point in the history
Modified API to use FLAGS_use_gpu as useGpu default value
  • Loading branch information
reyoung authored Nov 16, 2016
2 parents 9bce328 + 4c86285 commit 6561242
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 25 deletions.
14 changes: 14 additions & 0 deletions paddle/api/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ Matrix* Matrix::createDense(const std::vector<float>& data, size_t height,
return m;
}

Matrix* Matrix::createDenseFromNumpy(float* data, int dim1, int dim2,
bool copy, bool useGpu)
throw (UnsupportError) {
if (useGpu) {
/// Gpu mode only supports copy=True
if (!copy) {
throw UnsupportError("Gpu mode only supports copy=True");
}
return Matrix::createGpuDenseFromNumpy(data, dim1, dim2);
} else {
return Matrix::createCpuDenseFromNumpy(data, dim1, dim2, copy);
}
}

Matrix* Matrix::createCpuDenseFromNumpy(float* data, int dim1, int dim2,
bool copy) {
auto m = new Matrix();
Expand Down
16 changes: 15 additions & 1 deletion paddle/api/Paddle.swig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#define SWIG_FILE_WITH_INIT
#include "api/PaddleAPI.h"
%}

%include "exception.i"
%typemap(throws) UnsupportError %{
SWIG_exception(SWIG_RuntimeError, $1.what());
SWIG_fail;
%}

%include "std_vector.i"
%include "std_pair.i"
#ifdef SWIGPYTHON
Expand Down Expand Up @@ -133,14 +140,21 @@ namespace std {
%newobject Matrix::createZero;
%newobject Matrix::createSparse;
%newobject Matrix::createDense;
%newobject Matrix::createDenseFromNumpy;
%newobject Matrix::createCpuDenseFromNumpy;
%newobject Matrix::createGpuDenseFromNumpy;
%newobject Vector::createZero;
%newobject Vector::create;
%newobject Vector::createVectorFromNumpy;
%newobject Vector::createCpuVectorFromNumpy;
%newobject Vector::createGpuVectorFromNumpy;
%newobject IVector::createZero;
%newobject IVector::create;
%newobject IVector::createVectorFromNumpy;
%newobject IVector::createCpuVectorFromNumpy;
%newobject IVector::createGpuVectorFromNumpy;
%newobject Trainer::createByCommandLine;
%newobject Trainer::getNetworkOutput;
%newobject Trainer::getForwardOutput;
%newobject Trainer::getLayerOutput;
%newobject Arguments::getSlotValue;
%newobject Arguments::getSlotIds;
Expand Down
47 changes: 38 additions & 9 deletions paddle/api/PaddleAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License. */
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <stdexcept>
#include <vector>
#include "paddle/utils/GlobalConstants.h"
#include "paddle/utils/TypeDefs.h"
Expand All @@ -42,6 +43,12 @@ using namespace paddle::enumeration_wrapper; // NOLINT
*/
void initPaddle(int argc, char** argv);

/// Return FLAGS_use_gpu
bool isUsingGpu();

/// Set the Flags_use_gpu to the given parameter
void setUseGpu(bool useGpu);

/// Return true if this py_paddle is compiled in GPU Version
bool isGpuVersion();

Expand All @@ -52,7 +59,11 @@ class IOError {};
class RangeError {};

/// Not support Error, such as access GPU memory directly, etc.
class UnsupportError {};
class UnsupportError : public std::runtime_error {
public:
UnsupportError() : std::runtime_error(" ") {};
UnsupportError(const std::string& message) : std::runtime_error(message) {};
};

/// This type will map to python's list of float.
struct FloatArray {
Expand Down Expand Up @@ -101,7 +112,8 @@ class Matrix {
/**
* Create A Matrix with height,width, which is filled by zero.
*/
static Matrix* createZero(size_t height, size_t width, bool useGpu = false);
static Matrix* createZero(size_t height, size_t width,
bool useGpu = isUsingGpu());

/**
* Create Sparse Matrix.
Expand All @@ -114,7 +126,7 @@ class Matrix {
*/
static Matrix* createSparse(size_t height, size_t width, size_t nnz,
bool isNonVal = true, bool trans = false,
bool useGpu = false);
bool useGpu = isUsingGpu());

/**
* Create Dense Matrix.
Expand All @@ -123,7 +135,12 @@ class Matrix {
* @note the value will be copy into a new matrix.
*/
static Matrix* createDense(const std::vector<float>& data, size_t height,
size_t width, bool useGpu = false);
size_t width, bool useGpu = isUsingGpu());

static Matrix* createDenseFromNumpy(float* data, int dim1, int dim2,
bool copy = true,
bool useGpu = isUsingGpu())
throw (UnsupportError);

/**
* Create Cpu Dense Matrix from numpy matrix, dtype=float32
Expand Down Expand Up @@ -221,15 +238,19 @@ class Vector {
~Vector();

/// Create Vector filled with zero.
static Vector* createZero(size_t sz, bool useGpu = false);
static Vector* createZero(size_t sz, bool useGpu = isUsingGpu());

/**
* Create Vector from list of float.
*
* It will create a new vector, and copy data into it.
*/
static Vector* create(const std::vector<float>& data, bool useGpu = false);
static Vector* create(const std::vector<float>& data,
bool useGpu = isUsingGpu());

static Vector* createVectorFromNumpy(float* data, int dim, bool copy = true,
bool useGpu = isUsingGpu())
throw (UnsupportError);
/**
* Create Cpu Vector from numpy array, which dtype=float32
*
Expand Down Expand Up @@ -259,6 +280,9 @@ class Vector {
/// Return is GPU vector or not.
bool isGpu() const;

/// Return a list of float, the memory is alloced and copied.
FloatArray getData() const;

/// __len__ in python
size_t getSize() const;

Expand All @@ -279,13 +303,18 @@ class IVector {

public:
/// Create IVector filled with zero
static IVector* createZero(size_t sz, bool useGpu = false);
static IVector* createZero(size_t sz, bool useGpu = isUsingGpu());

/**
* Create IVector from list of int.
* It will create a new vector, and copy data into it.
*/
static IVector* create(const std::vector<int>& data, bool useGpu = false);
static IVector* create(const std::vector<int>& data,
bool useGpu = isUsingGpu());

static IVector* createVectorFromNumpy(int* data, int dim, bool copy = true,
bool useGpu = isUsingGpu())
throw (UnsupportError);

/**
* Create Cpu IVector from numpy array, which dtype=int32
Expand All @@ -297,7 +326,7 @@ class IVector {
/**
* Create Gpu IVector from numpy array, which dtype=int32
*/
static IVector* createGpuVectorFromNumy(int* data, int dim);
static IVector* createGpuVectorFromNumpy(int* data, int dim);

/// Cast to numpy array inplace.
void toNumpyArrayInplace(int** view_data, int* dim1) throw(UnsupportError);
Expand Down
4 changes: 4 additions & 0 deletions paddle/api/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ IntWithFloatArray::IntWithFloatArray(const float* v, const int* i, size_t l,
bool f)
: valBuf(v), idxBuf(i), length(l), needFree(f) {}

bool isUsingGpu() {return FLAGS_use_gpu;}

void setUseGpu(bool useGpu) {FLAGS_use_gpu = useGpu;}

bool isGpuVersion() {
#ifdef PADDLE_ONLY_CPU
return false;
Expand Down
45 changes: 43 additions & 2 deletions paddle/api/Vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ IVector* IVector::create(const std::vector<int>& data, bool useGpu) {
return v;
}

IVector* IVector::createVectorFromNumpy(int* data, int dim, bool copy,
bool useGpu) throw (UnsupportError){
if (useGpu) {
/// if use gpu only copy=true is supported
if (!copy) {
throw UnsupportError("Gpu mode only supports copy=True");
}
return IVector::createGpuVectorFromNumpy(data, dim);
} else {
return IVector::createCpuVectorFromNumpy(data, dim, copy);
}
}

IVector* IVector::createCpuVectorFromNumpy(int* data, int dim, bool copy) {
auto v = new IVector();
if (copy) {
Expand All @@ -50,7 +63,7 @@ IVector* IVector::createCpuVectorFromNumpy(int* data, int dim, bool copy) {
return v;
}

IVector* IVector::createGpuVectorFromNumy(int* data, int dim) {
IVector* IVector::createGpuVectorFromNumpy(int* data, int dim) {
auto v = new IVector();
v->m->vec = paddle::IVector::create(dim, true);
v->m->vec->copyFrom(data, dim);
Expand Down Expand Up @@ -188,12 +201,25 @@ Vector* Vector::createByPaddleVectorPtr(void* ptr) {
}
}

Vector* Vector::createVectorFromNumpy(float* data, int dim, bool copy,
bool useGpu) throw (UnsupportError){
if (useGpu) {
/// if use gpu only copy=True is supported
if (!copy) {
throw UnsupportError("Gpu mode only supports copy=True");
}
return Vector::createGpuVectorFromNumpy(data, dim);
} else {
return Vector::createCpuVectorFromNumpy(data, dim, copy);
}
}

Vector* Vector::createCpuVectorFromNumpy(float* data, int dim, bool copy) {
CHECK_GT(dim, 0);
auto retVec = new Vector();
if (copy) {
retVec->m->vec = paddle::Vector::create((size_t)dim, false);
return retVec;
retVec->m->vec->copyFrom(data, dim);
} else {
retVec->m->vec = paddle::Vector::create(data, (size_t)dim, false);
}
Expand Down Expand Up @@ -237,6 +263,21 @@ void Vector::copyFromNumpyArray(float* data, int dim) {
m->vec->copyFrom(data, dim);
}

FloatArray Vector::getData() const {
if (this->isGpu()) {
float* src = m->vec->getData();
size_t len = m->vec->getSize();
float* dest = new float[len];
hl_memcpy_device2host(dest, src, len * sizeof(float));
FloatArray ret_val(dest, len);
ret_val.needFree = true;
return ret_val;
} else {
FloatArray ret_val(m->vec->getData(), m->vec->getSize());
return ret_val;
}
}

bool Vector::isGpu() const {
return std::dynamic_pointer_cast<paddle::GpuVector>(m->vec) != nullptr;
}
Expand Down
18 changes: 15 additions & 3 deletions paddle/api/test/testMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_sparse(self):
self.assertEqual(m.getSparseRowCols(2), [])

def test_sparse_value(self):
m = swig_paddle.Matrix.createSparse(3, 3, 6, False)
m = swig_paddle.Matrix.createSparse(3, 3, 6, False, False, False)
self.assertIsNotNone(m)
m.sparseCopyFrom([0, 2, 3, 3], [0, 1, 2], [7.3, 4.2, 3.2])

Expand All @@ -66,7 +66,7 @@ def test_createDenseMat(self):
self.assertIsNotNone(m)
self.assertTrue(abs(m.get(1, 1) - 0.5) < 1e-5)

def test_numpy(self):
def test_numpyCpu(self):
numpy_mat = np.matrix([[1, 2], [3, 4], [5, 6]], dtype="float32")
m = swig_paddle.Matrix.createCpuDenseFromNumpy(numpy_mat)
self.assertEqual((int(m.getHeight()), int(m.getWidth())),
Expand Down Expand Up @@ -100,8 +100,20 @@ def test_numpyGpu(self):

for a, e in zip(gpu_m.getData(), [1.0, 3.23, 3.0, 4.0, 5.0, 6.0]):
self.assertAlmostEqual(a, e)

def test_numpy(self):
numpy_mat = np.matrix([[1, 2], [3, 4], [5, 6]], dtype="float32")
m = swig_paddle.Matrix.createDenseFromNumpy(numpy_mat)
self.assertEqual((int(m.getHeight()), int(m.getWidth())), numpy_mat.shape)
self.assertEqual(m.isGpu(), swig_paddle.isUsingGpu())
for a, e in zip(m.getData(), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]):
self.assertAlmostEqual(a, e)


if __name__ == "__main__":
swig_paddle.initPaddle("--use_gpu=0")
unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(TestMatrix)
unittest.TextTestRunner().run(suite)
if swig_paddle.isGpuVersion():
swig_paddle.setUseGpu(True)
unittest.main()
Loading

0 comments on commit 6561242

Please sign in to comment.