/* * Copyright (c) Meta Platforms, Inc. and affiliates. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ #include #include #include #include #include #include #include #include #include #include // @manual=//caffe2:torch_extension #include // @manual=//caffe2:torch-cpp-cpu namespace exir { namespace { class DataBuffer { private: void* buffer_ = nullptr; public: DataBuffer(pybind11::bytes data, int64_t len) { // allocate buffer buffer_ = malloc(len); // convert data to std::string and copy to buffer std::memcpy(buffer_, (std::string{data}).data(), len); } ~DataBuffer() { if (buffer_) { free(buffer_); } } DataBuffer(const DataBuffer&) = delete; DataBuffer& operator=(const DataBuffer&) = delete; void* get() { return buffer_; } }; } // namespace PYBIND11_MODULE(bindings, m) { pybind11::class_(m, "DataBuffer") .def(pybind11::init()); m.def( "convert_to_tensor", [&](DataBuffer& data_buffer, const int64_t scalar_type, const std::vector& sizes, const std::vector& strides) { at::ScalarType type_option = static_cast(scalar_type); auto opts = torch::TensorOptions().dtype(type_option); // get tensor from memory using metadata torch::Tensor result = torch::from_blob(data_buffer.get(), sizes, strides, opts); return result; }); } } // namespace exir