/* * 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. */ #pragma once #include #include #include #include #include namespace torch { namespace executor { namespace util { /** * A DataLoader that wraps a pre-allocated buffer and shares ownership to it. * The FreeableBuffers that it returns do not actually free any data. * * This can be used to wrap data that was allocated elsewhere. */ class SharedPtrDataLoader : public DataLoader { public: SharedPtrDataLoader(std::shared_ptr data, size_t size) : data_(data), size_(size) {} __ET_NODISCARD Result Load(size_t offset, size_t size) override { ET_CHECK_OR_RETURN_ERROR( offset + size <= size_, InvalidArgument, "offset %zu + size %zu > size_ %zu", offset, size, size_); return FreeableBuffer( static_cast(data_.get()) + offset, size, /*free_fn=*/nullptr); } __ET_NODISCARD Result size() const override { return size_; } private: const std::shared_ptr data_; const size_t size_; }; } // namespace util } // namespace executor } // namespace torch