|
|
@ -20,6 +20,7 @@ limitations under the License. */
|
|
|
|
#include <string>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <vector>
|
|
|
|
#include "paddle/utils/GlobalConstants.h"
|
|
|
|
#include "paddle/utils/GlobalConstants.h"
|
|
|
|
|
|
|
|
#include "paddle/utils/TypeDefs.h"
|
|
|
|
|
|
|
|
|
|
|
|
/// Import PaddlePaddle's enumeration into global namespace.
|
|
|
|
/// Import PaddlePaddle's enumeration into global namespace.
|
|
|
|
using namespace paddle::enumeration_wrapper; // NOLINT
|
|
|
|
using namespace paddle::enumeration_wrapper; // NOLINT
|
|
|
@ -55,10 +56,10 @@ class UnsupportError {};
|
|
|
|
|
|
|
|
|
|
|
|
/// This type will map to python's list of float.
|
|
|
|
/// This type will map to python's list of float.
|
|
|
|
struct FloatArray {
|
|
|
|
struct FloatArray {
|
|
|
|
const float* buf;
|
|
|
|
const real* buf;
|
|
|
|
const size_t length;
|
|
|
|
const size_t length;
|
|
|
|
bool needFree; // true if the buf is dynamic alloced.
|
|
|
|
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
|
|
|
|
/// 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)
|
|
|
|
/// This type will map to python's list of (int, float)
|
|
|
|
struct IntWithFloatArray {
|
|
|
|
struct IntWithFloatArray {
|
|
|
|
const float* valBuf;
|
|
|
|
const real* valBuf;
|
|
|
|
const int* idxBuf;
|
|
|
|
const int* idxBuf;
|
|
|
|
const size_t length;
|
|
|
|
const size_t length;
|
|
|
|
bool needFree;
|
|
|
|
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 };
|
|
|
|
enum SparseValueType { SPARSE_NON_VALUE = 0, SPARSE_VALUE = 1 };
|
|
|
@ -121,7 +122,7 @@ public:
|
|
|
|
* @param data list of float should be passed in python.
|
|
|
|
* @param data list of float should be passed in python.
|
|
|
|
* @note the value will be copy into a new matrix.
|
|
|
|
* @note the value will be copy into a new matrix.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static Matrix* createDense(const std::vector<float>& data, size_t height,
|
|
|
|
static Matrix* createDense(const std::vector<real>& data, size_t height,
|
|
|
|
size_t width, bool useGpu = false);
|
|
|
|
size_t width, bool useGpu = false);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -133,11 +134,11 @@ public:
|
|
|
|
* @param copy true if copy into a new matrix, false will create
|
|
|
|
* @param copy true if copy into a new matrix, false will create
|
|
|
|
* matrix inplace.
|
|
|
|
* matrix inplace.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static Matrix* createCpuDenseFromNumpy(float* data, int dim1, int dim2,
|
|
|
|
static Matrix* createCpuDenseFromNumpy(real* data, int dim1, int dim2,
|
|
|
|
bool copy = false);
|
|
|
|
bool copy = false);
|
|
|
|
|
|
|
|
|
|
|
|
/// Create Gpu Dense Matrix from numpy matrix, dtype=float32
|
|
|
|
/// 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.
|
|
|
|
* Cast to numpy matrix.
|
|
|
@ -153,15 +154,15 @@ public:
|
|
|
|
* numpy_mat = m.toNumpyMat()
|
|
|
|
* numpy_mat = m.toNumpyMat()
|
|
|
|
* @endcode
|
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void toNumpyMatInplace(float** view_data, int* dim1,
|
|
|
|
void toNumpyMatInplace(real** view_data, int* dim1,
|
|
|
|
int* dim2) throw(UnsupportError);
|
|
|
|
int* dim2) throw(UnsupportError);
|
|
|
|
|
|
|
|
|
|
|
|
/// Copy To numpy mat.
|
|
|
|
/// Copy To numpy mat.
|
|
|
|
void copyToNumpyMat(float** view_m_data, int* dim1,
|
|
|
|
void copyToNumpyMat(real** view_m_data, int* dim1,
|
|
|
|
int* dim2) throw(UnsupportError);
|
|
|
|
int* dim2) throw(UnsupportError);
|
|
|
|
|
|
|
|
|
|
|
|
/// Copy From Numpy Mat
|
|
|
|
/// 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);
|
|
|
|
RangeError);
|
|
|
|
|
|
|
|
|
|
|
|
/// return true if this matrix is sparse.
|
|
|
|
/// return true if this matrix is sparse.
|
|
|
@ -180,9 +181,9 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
size_t getWidth() const;
|
|
|
|
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
|
|
|
|
/// return type is list of float
|
|
|
|
FloatArray getData() const;
|
|
|
|
FloatArray getData() const;
|
|
|
@ -194,8 +195,8 @@ public:
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void sparseCopyFrom(const std::vector<int>& rows,
|
|
|
|
void sparseCopyFrom(const std::vector<int>& rows,
|
|
|
|
const std::vector<int>& cols,
|
|
|
|
const std::vector<int>& cols,
|
|
|
|
const std::vector<float>& values =
|
|
|
|
const std::vector<real>& values =
|
|
|
|
std::vector<float>()) throw(UnsupportError);
|
|
|
|
std::vector<real>()) throw(UnsupportError);
|
|
|
|
|
|
|
|
|
|
|
|
bool isGpu() const;
|
|
|
|
bool isGpu() const;
|
|
|
|
|
|
|
|
|
|
|
@ -227,33 +228,33 @@ public:
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* It will create a new vector, and copy data into it.
|
|
|
|
* 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<real>& data, bool useGpu = false);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Create Cpu Vector from numpy array, which dtype=float32
|
|
|
|
* Create Cpu Vector from numpy array, which dtype=float32
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* If copy is false, it will create vector inplace.
|
|
|
|
* 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);
|
|
|
|
bool copy = false);
|
|
|
|
|
|
|
|
|
|
|
|
/// Create Gpu Vector from numpy array, which dtype=float32
|
|
|
|
/// 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.
|
|
|
|
/// 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.
|
|
|
|
/// Copy to numpy array.
|
|
|
|
void copyToNumpyArray(float** view_m_data, int* dim1);
|
|
|
|
void copyToNumpyArray(real** view_m_data, int* dim1);
|
|
|
|
|
|
|
|
|
|
|
|
/// Copy from numpy array.
|
|
|
|
/// Copy from numpy array.
|
|
|
|
void copyFromNumpyArray(float* data, int dim);
|
|
|
|
void copyFromNumpyArray(real* data, int dim);
|
|
|
|
|
|
|
|
|
|
|
|
/// __getitem__ in python
|
|
|
|
/// __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
|
|
|
|
/// __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.
|
|
|
|
/// Return is GPU vector or not.
|
|
|
|
bool isGpu() const;
|
|
|
|
bool isGpu() const;
|
|
|
|