Tensor.hpp
浏览该文件的文档.
//
// Tensor.hpp
// MNN
//
// Created by MNN on 2018/08/14.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifndef Tensor_hpp
#define Tensor_hpp
#include <vector>
#include "HalideRuntime.h"
#include "MNNDefine.h"
namespace MNN {
class MNN_PUBLIC Tensor {
public:
struct InsideDescribe;
enum DimensionType {
TENSORFLOW,
CAFFE,
CAFFE_C4
};
enum HandleDataType {
HANDLE_NONE = 0,
HANDLE_STRING = 1
};
enum DataReorderType {
NO_REORDER = 0,
REORDER_4 = 1,
REORDER_8
};
public:
Tensor(int dimSize = 4, DimensionType type = CAFFE);
Tensor(const Tensor* tensor, DimensionType type = CAFFE, bool allocMemory = true);
~Tensor();
private:
// remove all assignment operator
Tensor(const Tensor& tensor) = delete;
Tensor(const Tensor&& tensor) = delete;
Tensor& operator=(const Tensor&) = delete;
Tensor& operator=(const Tensor&&) = delete;
public:
static Tensor* createDevice(const std::vector<int>& shape, halide_type_t type, DimensionType dimType = TENSORFLOW);
template <typename T>
static Tensor* createDevice(const std::vector<int>& shape, DimensionType dimType = TENSORFLOW) {
return createDevice(shape, halide_type_of<T>(), dimType);
}
static Tensor* create(const std::vector<int>& shape, halide_type_t type, void* data = NULL,
DimensionType dimType = TENSORFLOW);
template <typename T>
static Tensor* create(const std::vector<int>& shape, void* data = NULL, DimensionType dimType = TENSORFLOW) {
return create(shape, halide_type_of<T>(), data, dimType);
}
public:
bool copyFromHostTensor(const Tensor* hostTensor);
bool copyToHostTensor(Tensor* hostTensor) const;
static Tensor* createHostTensorFromDevice(const Tensor* deviceTensor, bool copyData = true);
public:
const halide_buffer_t& buffer() const {
return mBuffer;
}
halide_buffer_t& buffer() {
return mBuffer;
}
DimensionType getDimensionType() const;
HandleDataType getHandleDataType() const;
void setType(int type);
inline halide_type_t getType() const {
return mBuffer.type;
}
template <typename T>
T* host() const {
return (T*)mBuffer.host;
}
uint64_t deviceId() const {
return mBuffer.device;
}
public:
int dimensions() const {
return mBuffer.dimensions;
}
std::vector<int> shape() const;
int size() const;
inline int elementSize() const {
return size() / mBuffer.type.bytes();
}
public:
// for CAFFE tensors only.
inline int width() const {
return mBuffer.dim[3].extent;
}
inline int height() const {
return mBuffer.dim[2].extent;
}
inline int channel() const {
return mBuffer.dim[1].extent;
}
inline int batch() const {
return mBuffer.dim[0].extent;
}
// for TENSORFLOW tensors only.
inline int tfWidth() const {
return mBuffer.dim[2].extent;
}
inline int tfHeight() const {
return mBuffer.dim[1].extent;
}
inline int tfChannel() const {
return mBuffer.dim[3].extent;
}
inline int tfBatch() const {
return mBuffer.dim[0].extent;
}
// visit dimension's extent & stride
inline int stride(int index) const {
return mBuffer.dim[index].stride;
}
inline int length(int index) const {
return mBuffer.dim[index].extent;
}
inline void setStride(int index, int stride) {
mBuffer.dim[index].stride = stride;
}
inline void setLength(int index, int length) {
mBuffer.dim[index].extent = length;
}
public:
void print() const;
private:
halide_buffer_t mBuffer;
struct InsideDescribe* mDescribe;
private:
friend class TensorUtils;
};
} // namespace MNN
#endif /* Tensor_hpp */
MNN::Tensor::elementSize
int elementSize() const
calculate number of elements needed to store data taking reordering flag into account.
Definition: Tensor.hpp:225
MNN::Tensor::batch
int batch() const
Definition: Tensor.hpp:240
MNN::Tensor::createDevice
static Tensor * createDevice(const std::vector< int > &shape, DimensionType dimType=TENSORFLOW)
create tensor with shape and dimension type. data type is represented by T.
Definition: Tensor.hpp:103
MNN::Tensor::CAFFE
Definition: Tensor.hpp:34
MNN::Tensor::DataReorderType
DataReorderType
Definition: Tensor.hpp:48
MNN::Tensor::tfBatch
int tfBatch() const
Definition: Tensor.hpp:254
MNN::Tensor::TENSORFLOW
Definition: Tensor.hpp:32
MNN::Tensor::width
int width() const
Definition: Tensor.hpp:231
MNN::Tensor::buffer
halide_buffer_t & buffer()
Definition: Tensor.hpp:157
MNN::Tensor::length
int length(int index) const
Definition: Tensor.hpp:262
MNN::Tensor::setLength
void setLength(int index, int length)
Definition: Tensor.hpp:268
MNN::Tensor::height
int height() const
Definition: Tensor.hpp:234
MNN::Tensor::tfWidth
int tfWidth() const
Definition: Tensor.hpp:245
MNN::Tensor::stride
int stride(int index) const
Definition: Tensor.hpp:259
HalideRuntime.h
MNN::Tensor
Definition: Tensor.hpp:25
MNN::Tensor::host
T * host() const
visit host memory, data type is represented by T.
Definition: Tensor.hpp:192
MNN_PUBLIC
#define MNN_PUBLIC
Definition: MNNDefine.h:53
MNN::Tensor::buffer
const halide_buffer_t & buffer() const
Definition: Tensor.hpp:154
MNN::Tensor::DimensionType
DimensionType
Definition: Tensor.hpp:30
MNN::Tensor::tfChannel
int tfChannel() const
Definition: Tensor.hpp:251
halide_type_t
Definition: HalideRuntime.h:82
MNN::Tensor::getType
halide_type_t getType() const
get data type.
Definition: Tensor.hpp:183
MNN
Definition: AutoTime.hpp:16
MNN::Tensor::setStride
void setStride(int index, int stride)
Definition: Tensor.hpp:265
MNN::Tensor::dimensions
int dimensions() const
Definition: Tensor.hpp:205
MNN::Tensor::channel
int channel() const
Definition: Tensor.hpp:237
MNN::Tensor::tfHeight
int tfHeight() const
Definition: Tensor.hpp:248
halide_buffer_t
Definition: HalideRuntime.h:203
MNN::Tensor::deviceId
uint64_t deviceId() const
visit device memory.
Definition: Tensor.hpp:200
MNNDefine.h
MNN::Tensor::create
static Tensor * create(const std::vector< int > &shape, void *data=NULL, DimensionType dimType=TENSORFLOW)
create tensor with shape, data and dimension type. data type is represented by T.
Definition: Tensor.hpp:126
MNN::Tensor::HandleDataType
HandleDataType
Definition: Tensor.hpp:40