19 #ifndef SINGA_CORE_TENSOR_H_ 20 #define SINGA_CORE_TENSOR_H_ 26 #include "singa/core/common.h" 27 #include "singa/core/device.h" 28 #include "singa/proto/core.pb.h" 29 #include "singa/utils/logging.h" 35 typedef vector<size_t> Shape;
37 const size_t kDataWidth[] = {
sizeof(float),
sizeof(
float) / 2,
38 sizeof(int),
sizeof(
char),
39 sizeof(double),
sizeof(
unsigned char)
41 inline size_t SizeOf(DataType t) {
42 static_assert(kNumDataType ==
sizeof(kDataWidth) /
sizeof(
size_t),
43 "Num of data types not match num of data width");
44 CHECK_GT(kNumDataType, t);
62 explicit Tensor(
const Shape &shape, DataType dtype = kFloat32);
66 std::shared_ptr<Device> dev,
67 DataType dtype = kFloat32);
84 std::shared_ptr<Device> device()
const {
return device_; }
87 template <
typename SType>
88 const SType *
data()
const {
89 return static_cast<const SType *
>(
block()->data());
93 const DataType
data_type()
const {
return data_type_; }
95 const Shape &shape()
const {
return shape_; }
97 const size_t shape(
const size_t idx)
const {
98 CHECK_LT(idx, shape_.size());
99 return shape_.at(idx);
102 size_t nDim()
const {
return shape_.size(); }
104 bool empty()
const {
return nDim() == 0; }
108 if (!stride_.empty()) {
109 auto last = stride_.front();
110 for (
auto s : stride_) {
111 if (s > last && last > 0)
120 const vector<int>& stride()
const {
return stride_; }
129 if (
block_ ==
nullptr)
return 0u;
130 CHECK_EQ(
block_->size() % SizeOf(data_type_), 0u);
131 return block_->size() / SizeOf(data_type_);
139 template <
typename SType>
142 const SType* ptr = data<SType>();
143 for (
size_t i = 0; i < num; i++) value[i] = ptr[i];
147 void ToProto(singa::TensorProto *proto)
const;
159 template <
typename SType>
164 template <
typename SType>
166 const size_t offset = 0);
173 void FromProto(
const singa::TensorProto &proto);
177 void RepeatData(
const vector<size_t>& repeats,
int axis,
int total_repeats,
184 Tensor Repeat(
const vector<size_t>& repeats,
int axis,
185 std::shared_ptr<Device> device =
nullptr);
189 Tensor Clone(std::shared_ptr<Device> device =
nullptr)
const;
211 template <
typename SType>
212 Tensor &operator+=(
const SType x);
215 template <
typename SType>
216 Tensor &operator-=(
const SType x);
219 template <
typename SType>
220 Tensor &operator*=(
const SType x);
223 template <
typename SType>
224 Tensor &operator/=(
const SType x);
264 void generate_stride() {
266 if (shape_.size() == 0) {
267 stride_.push_back(1);
272 int cumulative_product = 1;
273 for (
size_t n = 0; n < shape_.size(); ++n) {
274 cumulative_product = cumulative_product * shape_[n];
275 stride_.push_back(dim / cumulative_product);
279 void set_strides(
const vector<int> new_strides) {
280 stride_ = new_strides;
284 DataType data_type_ = kFloat32;
285 std::shared_ptr<Device> device_ =
nullptr;
290 vector<int> stride_ = {};
294 inline size_t Product(
const Shape &shape,
int start = 0,
size_t len = 0) {
295 if (len == 0) len = shape.size();
296 if (len == 0)
return 0;
297 CHECK_LE(len, shape.size());
299 for (
unsigned int i = start; i < len; i++) v *= shape[i];
304 inline void CheckDataTypeAndLang(
const Tensor &in1,
const Tensor &in2) {
306 CHECK_EQ(in1.device()->lang(), in2.device()->lang());
310 template <
typename FromType,
typename ToType>
311 ToType TypeCast(
const FromType &x) {
313 return static_cast<ToType
>(x);
316 Tensor Boradcast(
const Shape& shape);
337 const size_t dst_offset = 0,
const size_t src_offset = 0);
340 void RepeatDataToFrom(
bool broadcast_flag,
const vector<size_t>& repeats,
int axis,
367 template <
typename SType>
370 template <
typename SType>
378 template <
typename SType>
380 template <
typename SType>
388 template <
typename SType>
390 template <
typename SType>
398 template <
typename SType>
400 template <
typename SType>
409 template <
typename SType>
411 template <
typename SType>
428 template <
typename SType>
430 template <
typename SType>
433 template <
typename SType>
435 template <
typename SType>
438 template <
typename SType>
440 template <
typename SType>
441 void EltwiseMult(
const Tensor &in,
const SType x,
Tensor *out);
444 template <
typename SType>
447 template <
typename SType>
451 template <
typename SType>
454 template <
typename SType>
457 template <
typename SType =
float>
458 SType Sum(
const Tensor &in);
471 template <
typename SType>
477 template <
typename SType>
510 template <
typename SType>
513 template <
typename SType>
516 template <
typename SType>
517 void Uniform(
const SType low,
const SType high,
Tensor *out);
523 template <
typename SType>
534 template <
typename SType>
535 void Mult(
const SType alpha,
const Tensor &A,
const Tensor &B,
const SType beta,
590 #endif // SINGA_CORE_TENSOR_H_ float L2() const
Return average L2 norm.
Tensor SliceRows(const Tensor &in, const size_t start, const size_t end)
Alias of CopyRows.
Tensor & operator=(const Tensor &in)
Copy assignment.
void RepeatData(const vector< size_t > &repeats, int axis, int total_repeats, const Tensor &other)
TODO(wangwei) merge RepeatData into Repeat?
void CopyData(const Tensor &other)
Copy data from another Tensor which may be on a diff device.
Tensor ConcatOn(const std::vector< Tensor > &in, int axis)
Return a tensor concatenated of the input tensors along the give axis.
void Axpy(SType alpha, const Tensor &in, Tensor *out)
out = alpha*in + out
void AddColumn(const Tensor &v, Tensor *M)
Add column 'v' with each column of matrix M.
bool transpose() const
The stride should decrease except dim with stride=0 due to broadcasting.
Definition: tensor.h:107
Tensor operator>=(const Tensor &in, const SType x)
Element-wise operation, out[i]= (in[i] >= x) ? 1.f : 0.f.
void SoftmaxCrossEntropyBwd(const Tensor &t, Tensor *p)
Compute the dx, given prediction probability 'p' (p=softmax(x)) and the target (ground truth) labels ...
Tensor CopyColumns(const Tensor &in, const size_t start, const size_t end)
Return a tensor consisting of columns ([start, end)) from 'in'.
void CopyDataToFrom(Tensor *dst, const Tensor &src, const size_t num, const size_t dst_offset=0, const size_t src_offset=0)
Copy 'num' elements of src to dst.
Tensor Average(const Tensor &in, const int axis)
Average elements in the Tensor, currently only support vector and matrix.
Tensor SliceOn(const Tensor &in, const size_t start, const size_t end, int axis)
Slice the input tensor along the give axis to generate a new tensor.
Tensor & ToDevice(std::shared_ptr< Device > dev)
Reset the device.
void AddRow(const Tensor &v, Tensor *out)
Add row 'v' with each row of matrix M; write results into 'out'.
void ToProto(singa::TensorProto *proto) const
Serialize data, shape and transpose to protobuf object.
const size_t kDataWidth[]
hardcode the width of types defined in DataType
Definition: tensor.h:37
void SetValue(const SType x)
Set each element of the tensor to be x.
size_t MemSize() const
Return memory size (i.e., Bytes)
Definition: tensor.h:135
Block * block_
Note: block_ is allocated in lazy manner to avoid frequent malloc/free.
Definition: tensor.h:288
Tensor CrossEntropyFwd(const Tensor &p, const Tensor &t)
To be called by pysinga autograd operations; swig ignores the const qualifier http://www.swig.org/Doc3.0/SWIGPlus.html#SWIGPlus_const.
Tensor Mult(const Tensor &A, const Tensor &B)
Do matrix vector multipication or matrix matrix multiplication depdending on the Tensor shape...
A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU)...
Definition: tensor.h:56
void ComputeCrossEntropy(const Tensor &p, const Tensor &t, Tensor *loss)
Compute the cross entropy loss given the prediction probability 'p' and the target (ground truth) lab...
bool initailized() const
Return true if the content of the tensor is initialized.
Definition: tensor.h:123
void MultColumn(const Tensor &v, Tensor *M)
Multiply column 'v' and each column of matrix M; write results into 'out'.
Tensor & Transpose()
Reverse the shape vector.
Block * block() const
For functions in xx_math.cc to access the block.
Definition: tensor.h:82
Tensor CopyRows(const Tensor &in, const size_t start, const size_t end)
Return a tensor consisting of rows ([start, end)) from 'in'.
void SumRows(const Tensor &M, Tensor *out)
Sum all rows of matrix M into a single row as 'out'.
void Gaussian(const SType mean, const SType std, Tensor *out)
Fill in Tensor 't' following Gaussian distribution.
Tensor ConcatenateColumns(const vector< Tensor > &in)
Return a tensor which is horizontally stacked from tensors in 'in'.
Tensor & T()
Matrix transpose. Valid only if shape.size() == 2.
Tensor & ToHost()
Equivalent to ToDevice(host_dev).
void SubRow(const Tensor &v, Tensor *M)
Sub row 'v' by each row of matrix M; write results into 'out'.
void DivColumn(const Tensor &v, Tensor *M)
Divide column 'v' by each column of matrix M; write results into 'out'.
void GetValue(SType *value, const size_t num)
used for swig code to convert Tensor into numpy array.
Definition: tensor.h:140
Tensor SoftMax(const Tensor &in)
Do softmax for each row. 'in' could be a 1-d or 2-d Tensor.
const DataType data_type() const
data type, including kFloat16, kFloat32, kInt
Definition: tensor.h:93
void SumColumns(const Tensor &M, Tensor *out)
Sum all columns of matrix M into a single column as 'out'.
std::shared_ptr< Device > defaultDevice
a singleton CppDevice as the host for all devices.
Tensor ConcatColumns(const vector< Tensor > &in)
Alias name for function ConcatenateColumns.
Tensor ConcatenateRows(const vector< Tensor > &in)
Return a tensor which is vertically stacked from tensors in 'in'.
Tensor Clone(std::shared_ptr< Device > device=nullptr) const
return an exactly the same Tensor with data been deep copied to the given device. ...
Tensor & Reshape(const Shape &shape)
change the shape (and stride); the block may be reallocated.
Tensor operator<=(const Tensor &in, const SType x)
Element-wise operation, out[i]= (in[i] <= x) ? 1.f : 0.f.
void DivRow(const Tensor &v, Tensor *M)
Divide row 'v' by each row of matrix M; write results into 'out'.
Tensor operator>(const Tensor &in, const SType x)
Element-wise operation, out[i]= (in[i] > x) ? 1.f : 0.f.
Block represent a chunk of memory (on device or host).
Definition: common.h:60
Tensor SliceColumns(const Tensor &in, const size_t start, const size_t end)
Alias of CopyColumns.
void Bernoulli(const SType p, Tensor *out)
For each element x set x = 1 if random() < p; otherwise x = 1.
Tensor & AsType(const DataType type)
Reset the data type, it would reallocate block if type changes.
void Uniform(const SType low, const SType high, Tensor *out)
Fill in Tensor 't' following uniform distribution.
Tensor ConcatRows(const vector< Tensor > &in)
Alias name for function ConcatenateRows.
void MultRow(const Tensor &v, Tensor *M)
Multiply row 'v' with each row of matrix M; write results into 'out'.
void CopyDataFromHostPtr(const SType *src, const size_t num, const size_t offset=0)
For init the tensor values, copy 'num' elements from 'src' to the internal memory with 'offset' (elem...
Tensor & Resize(const Shape &shape)
Resize the memory and return itself.
void SubColumn(const Tensor &v, Tensor *M)
Sub column 'v' by each column of matrix M.
Tensor operator<(const Tensor &in, const SType x)
Element-wise operation, out[i]= (in[i] < x) ? 1.f : 0.f.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48
size_t Size() const
Return number of total elements.
Definition: tensor.h:128
void FromProto(const singa::TensorProto &proto)
Deserialize data, shape and transpose from protobuf object.
const SType * data() const
Return immutable Tensor values with given type.
Definition: tensor.h:88
Tensor & ResetLike(const Tensor &t)
Reset the shape, device, and data type as given tensor.
Tensor Pow(const Tensor &in, const SType x)
Element-wise opeartion, out[i]=in[i]^x.
Tensor & Broadcast(const Shape &shape)
Return a view of the input tensor whose shape is broadcasted to be compitable with the given shape...
float L1() const
Return average L1 norm.