| 36 | namespace cl { |
| 37 | |
| 38 | class Tensor { |
| 39 | public: |
| 40 | Tensor() : memory_(nullptr) {} |
| 41 | Tensor(cl_mem memory, int width, int height, int channels, DataType data_type, |
| 42 | TensorStorageType storage_type); |
| 43 | |
| 44 | // Move only |
| 45 | Tensor(Tensor&& tensor); |
| 46 | Tensor& operator=(Tensor&& tensor); |
| 47 | Tensor(const Tensor&) = delete; |
| 48 | Tensor& operator=(const Tensor&) = delete; |
| 49 | |
| 50 | virtual ~Tensor() { Release(); } |
| 51 | |
| 52 | int Width() const { return width_; } |
| 53 | int Height() const { return height_; } |
| 54 | int Channels() const { return channels_; } |
| 55 | enum DataType DataType() const { return data_type_; } |
| 56 | TensorStorageType StorageType() const { return storage_type_; } |
| 57 | |
| 58 | int Depth() const { return IntegralDivideRoundUp(channels_, 4); } |
| 59 | int4 GetSizeWithDepth() const { |
| 60 | return int4(width_, height_, channels_, |
| 61 | IntegralDivideRoundUp(channels_, 4)); |
| 62 | } |
| 63 | cl_mem GetMemoryPtr() const { return memory_; } |
| 64 | |
| 65 | Status WriteDataBHWC(absl::Span<const float> in, CLCommandQueue* queue); |
| 66 | |
| 67 | Status ReadDataBHWC(absl::Span<float> out, CLCommandQueue* queue) const; |
| 68 | |
| 69 | Status WriteData(CLCommandQueue* queue, const TensorFloat32& src); |
| 70 | Status ReadData(CLCommandQueue* queue, TensorFloat32* dst) const; |
| 71 | |
| 72 | protected: |
| 73 | Status IsValid(const BHWC& shape) const; |
| 74 | |
| 75 | template <typename T> |
| 76 | void DataFromBHWC(absl::Span<const float> src, absl::Span<T> dst) const; |
| 77 | template <typename T> |
| 78 | void DataToBHWC(absl::Span<const T> src, absl::Span<float> dst) const; |
| 79 | |
| 80 | // TODO(sorokin) might be bad performance |
| 81 | int GetLinearIndex(int x, int y, int d, int sub_d) const { |
| 82 | switch (storage_type_) { |
| 83 | case TensorStorageType::BUFFER: |
| 84 | case TensorStorageType::TEXTURE_ARRAY: |
| 85 | return ((d * height_ + y) * width_ + x) * 4 + sub_d; // DHWC4 |
| 86 | case TensorStorageType::TEXTURE_2D: |
| 87 | return ((y * Depth() + d) * width_ + x) * 4 + sub_d; // HDWC4 |
| 88 | case TensorStorageType::SINGLE_TEXTURE_2D: |
| 89 | return (sub_d * height_ + y) * width_ + x; |
| 90 | case TensorStorageType::UNKNOWN: |
| 91 | return -1; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | int3 GetFullTensorRegion() const; |
no outgoing calls