/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #ifndef TENSORFLOW_LITE_DELEGATES_GPU_METAL_BUFFER_H_ #define TENSORFLOW_LITE_DELEGATES_GPU_METAL_BUFFER_H_ #include #include #import #include "absl/types/span.h" #include "tensorflow/lite/delegates/gpu/common/status.h" #include "tensorflow/lite/delegates/gpu/common/task/buffer_desc.h" #include "tensorflow/lite/delegates/gpu/metal/gpu_object.h" namespace tflite { namespace gpu { namespace metal { class Buffer : public GPUObject { public: Buffer() = default; Buffer(id buffer, size_t size_in_bytes); explicit Buffer(id buffer); // Move only Buffer(Buffer&& buffer); Buffer& operator=(Buffer&& buffer); Buffer(const Buffer&) = delete; Buffer& operator=(const Buffer&) = delete; ~Buffer(); // for profiling and memory statistics uint64_t GetMemorySizeInBytes() const { return size_; } id GetMemoryPtr() const { return buffer_; } // Writes data to a buffer. Data should point to a region that // has exact size in bytes as size_in_bytes(constructor parameter). template absl::Status WriteData(absl::Span data); template absl::Status WriteData(id command_queue, absl::Span data, bool wait_for_completion); // Reads data from Buffer into CPU memory. template absl::Status ReadData(std::vector* result) const; absl::Status GetGPUResources(const GPUObjectDescriptor* obj_ptr, GPUResourcesWithValue* resources) const override; absl::Status CreateFromBufferDescriptor(const BufferDescriptor& desc, id device); private: void Release(); id buffer_ = nullptr; size_t size_; bool owner_ = true; }; Buffer CreateBufferShared(id buffer); absl::Status CreateBuffer(size_t size_in_bytes, const void* data, id device, Buffer* result); template absl::Status Buffer::WriteData(const absl::Span data) { if (sizeof(T) * data.size() > size_) { return absl::InvalidArgumentError( "absl::Span data size is greater from buffer allocated size."); } std::memcpy([buffer_ contents], data.data(), size_); return absl::OkStatus(); } template absl::Status Buffer::WriteData(id command_queue, absl::Span data, bool wait_for_completion) { if (sizeof(T) * data.size() > size_) { return absl::InvalidArgumentError( "absl::Span data size is greater from buffer allocated size."); } WriteDataToBuffer(buffer_, /*buffer_offset=*/0, command_queue, data.data(), size_, wait_for_completion); return absl::OkStatus(); } template absl::Status Buffer::ReadData(std::vector* result) const { if (size_ % sizeof(T) != 0) { return absl::UnknownError("Wrong element size(typename T is not correct?"); } const int elements_count = size_ / sizeof(T); result->resize(elements_count); std::memcpy(result->data(), [buffer_ contents], size_); return absl::OkStatus(); } } // namespace metal } // namespace gpu } // namespace tflite #endif // TENSORFLOW_LITE_DELEGATES_GPU_METAL_BUFFER_H_