Skip to content

Fix includes in Core ML backend. #3603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <assert.h>
#include <fstream>
#include <iostream>
#include <range.hpp>
#include <sstream>

#if __has_include(<filesystem>)
Expand All @@ -22,7 +21,8 @@ namespace filesystem = std::experimental::filesystem;
}
#endif

#include <reversed_memory_stream.hpp>
#include "range.hpp"
#include "reversed_memory_stream.hpp"

namespace {
using namespace inmemoryfs;
Expand Down
87 changes: 44 additions & 43 deletions backends/apple/coreml/runtime/inmemoryfs/inmemory_filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#pragma once

#include <functional>
#include <inmemory_filesystem_metadata.hpp>
#include <memory>
#include <memory_buffer.hpp>
#include <optional>
#include <stdio.h>
#include <string>
#include <system_error>

#include "inmemory_filesystem_metadata.hpp"
#include "memory_buffer.hpp"

namespace inmemoryfs {

/// A class representing an in-memory file system.
Expand All @@ -29,36 +30,36 @@ class InMemoryFileSystem final {
DirectoryExpected, // If path is not a directory.
FileExpected, // If the path is not a file.
};

/// Options for loading file content.
enum class FileLoadOption: int8_t {
Malloc = 1, // Copy file contents into memory.
MMap, // Memory map file contents.
LazyMMap // Memory map file contents but lazily.
};

/// The error category for `InMemoryFileSystem`.
struct ErrorCategory final: public std::error_category {
public:
inline const char* name() const noexcept override {
return "InMemoryFileSystem";
}

std::string message(int code) const override;
};

struct Attributes {
time_t modificationTime;

inline Attributes() noexcept:
modificationTime(time(0))
{}
};

using MetadataWriter = std::function<bool(const InMemoryFileSystemMetadata&, std::ostream&)>;
using MetadataWriterInMemory = std::function<size_t(const InMemoryFileSystemMetadata&, void *)>;
using MetadataReader = std::function<std::optional<InMemoryFileSystemMetadata>(std::istream&)>;

/// A class representing an in-memory node. This could either be a file node or a directory node.
class InMemoryNode {
public:
Expand All @@ -67,7 +68,7 @@ class InMemoryFileSystem final {
File = 0, /// Node is a File.
Directory /// Node is a Directory.
};

/// Constructs an in-memory node instance.
///
/// @param name The name of the Node. It must be unique in the enclosing Directory.
Expand All @@ -78,38 +79,38 @@ class InMemoryFileSystem final {
attributes_(std::move(attributes)),
kind_(kind)
{}

InMemoryNode(InMemoryNode const&) = delete;
InMemoryNode& operator=(InMemoryNode const&) = delete;

inline virtual ~InMemoryNode() {}

/// Returns the node attributes.
inline Attributes attributes() const noexcept {
return attributes_;
}

/// Sets the node attributes.
///
/// @param attributes The node attributes.
inline void set_attributes(Attributes attributes) noexcept {
attributes_ = std::move(attributes);
}

/// Returns the node kind, possible values are `File` and `Directory`.
inline Kind kind() const noexcept {
return kind_;
}

/// Returns the name of the node.
inline const std::string& name() const noexcept {
return name_;
}

inline void set_name(std::string name) noexcept {
std::swap(name_, name);
}

/// Returns `true` if the node is a directory otherwise `false`.
inline bool isDirectory() const noexcept {
switch (kind_) {
Expand All @@ -119,58 +120,58 @@ class InMemoryFileSystem final {
return false;
}
}

/// Returns `true` if the node is a file otherwise `false`.
inline bool isFile() const noexcept {
return !isDirectory();
}

private:
std::string name_;
InMemoryFileSystem::Attributes attributes_;
const Kind kind_;
};

/// Constructs an`InMemoryFileSystem` instance with an empty root and the specified name.
///
/// @param rootName The name of the root node.
explicit InMemoryFileSystem(std::string rootName = "root") noexcept;

/// Constructs an`InMemoryFileSystem` instance with the specified root.
///
/// @param root The root node.
explicit InMemoryFileSystem(std::unique_ptr<InMemoryNode> root) noexcept
:root_(std::move(root))
{}

InMemoryFileSystem(InMemoryFileSystem const&) = delete;
InMemoryFileSystem& operator=(InMemoryFileSystem const&) = delete;

virtual ~InMemoryFileSystem() {}

/// Returns the root.
InMemoryNode *root() const noexcept {
return root_.get();
}

/// Checks if the node at the specified path is a directory.
///
/// @param canonical_path The path components from the root.
/// @retval `true` if the node at the specified path is a directory otherwise `false`.
bool is_directory(const std::vector<std::string>& canonical_path) noexcept;

/// Checks if the node at the specified path is a file.
///
/// @param canonical_path The path components from the root.
/// @retval `true` if the node at the specified path is a file otherwise `false`.
bool is_file(const std::vector<std::string>& canonical_path) noexcept;

/// Checks if the node at the specified path exists.
///
/// @param canonical_path The path components from the root.
/// @retval `true` if the node at the specified path exists.
bool exists(const std::vector<std::string>& canonical_path) const noexcept;

/// Retrieves the canonical path of all the child nodes at the specified path. The node
/// at the specified path must be a directory otherwise it returns an empty vector with the `error`
/// populated.
Expand All @@ -180,23 +181,23 @@ class InMemoryFileSystem final {
/// @retval paths to all the items at the specified path.
std::vector<std::vector<std::string>> get_item_paths(const std::vector<std::string>& canonical_path,
std::error_code& error) const noexcept;

/// Retrieves the attributes of the item at the specified path.
///
/// @param canonical_path The path components from the root.
/// @param error On failure, error is populated with the failure reason.
/// @retval The item attributes at the specified path.
std::optional<Attributes> get_attributes(const std::vector<std::string>& canonical_path,
std::error_code& error) const noexcept;

/// Retrieves the contents of the file at the specified path.
///
/// @param canonical_path The path components from the root.
/// @param error On failure, error is populated with the failure reason.
/// @retval The file contents or `nullptr` if the item at the specified path is not a file.
std::shared_ptr<MemoryBuffer> get_file_content(const std::vector<std::string>& canonical_path,
std::error_code& error) const noexcept;

/// Creates an in-memory directory at the specified path.
///
/// @param canonical_path The path components from the root.
Expand All @@ -208,7 +209,7 @@ class InMemoryFileSystem final {
Attributes attributes,
bool create_intermediate_directories,
std::error_code& error) noexcept;

/// Creates an in-memory file at the specified path.
///
/// @param canonical_path The path components from the root.
Expand All @@ -222,15 +223,15 @@ class InMemoryFileSystem final {
Attributes attributes,
bool overwrite,
std::error_code& error) noexcept;

/// Removes the item at the specified path.
///
/// @param canonical_path The path components from the root.
/// @param error On failure, error is populated with the failure reason.
/// @retval `true` if the item is removed otherwise `false`.
bool remove_item(const std::vector<std::string>& canonical_path,
std::error_code& error) noexcept;

/// Sets the attributes at the specified path.
///
/// @param canonical_path The path components from the root.
Expand All @@ -239,7 +240,7 @@ class InMemoryFileSystem final {
bool set_attributes(const std::vector<std::string>& canonical_path,
Attributes attributes,
std::error_code& error) noexcept;

/// Writes the item at the specified path to the filesystem.
///
/// @param canonical_path The path components from the root.
Expand All @@ -251,7 +252,7 @@ class InMemoryFileSystem final {
const std::string& dst_path,
bool recursive,
std::error_code& error) const noexcept;

/// Renames the item at the specified path, if there is already an item with the same name then
/// the rename would fail.
///
Expand All @@ -262,7 +263,7 @@ class InMemoryFileSystem final {
bool rename_item(const std::vector<std::string>& canonical_path,
const std::string& name,
std::error_code& error) noexcept;

/// Creates an`InMemoryFileSystem` from the filesystem path.
///
/// The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
Expand All @@ -275,7 +276,7 @@ class InMemoryFileSystem final {
static std::unique_ptr<InMemoryFileSystem> make_from_directory(const std::string& path,
FileLoadOption option,
std::error_code& error) noexcept;

/// Serializes the item at the specified path and writes it to the stream.
///
/// The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
Expand All @@ -292,7 +293,7 @@ class InMemoryFileSystem final {
const MetadataWriter& metadata_writer,
std::ostream& ostream,
std::error_code& error) const noexcept;

/// Serializes the item at the specified path and writes it to the stream.
///
/// The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
Expand All @@ -309,7 +310,7 @@ class InMemoryFileSystem final {
const MetadataWriterInMemory& metadata_writer,
void *dst,
std::error_code& error) const noexcept;

/// Computes the size of the buffer that would be needed to serialized the item at the specified path.
///
/// @param canonical_path The path components from the root.
Expand All @@ -319,15 +320,15 @@ class InMemoryFileSystem final {
size_t get_buffer_size_for_serialization(const std::vector<std::string>& canonical_path,
size_t alignment,
const MetadataWriter& metadata_writer) const noexcept;

/// Constructs an `InMemoryFileSystem` instance from the buffer contents.
///
/// @param buffer The memory buffer.
/// @param metadata_reader The function to use when deserializing the filesystem metadata.
/// @retval The constructed `InMemoryFileSystem` or `nullptr` if the deserialization failed.
static std::unique_ptr<InMemoryFileSystem> make_from_buffer(const std::shared_ptr<MemoryBuffer>& buffer,
const MetadataReader& metadata_reader) noexcept;

private:
const std::unique_ptr<InMemoryNode> root_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

#pragma once

#include <memory_buffer.hpp>
#include <string>
#include <vector>
#include <unordered_map>
#include <range.hpp>

#include "memory_buffer.hpp"
#include "range.hpp"

namespace inmemoryfs {

Expand All @@ -27,4 +28,3 @@ struct InMemoryFileSystemMetadata {
};

} // namespace inmemoryfs

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
// Please refer to the license found in the LICENSE file in the root directory of the source tree.


#include <inmemory_filesystem_utils.hpp>
#include <iostream>
#include <memory>
#include <memory_buffer.hpp>
#include <memory_stream.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <string>
Expand All @@ -21,6 +17,13 @@
#include <thread>
#include <unistd.h>

#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>

#include "inmemory_filesystem_utils.hpp"
#include "memory_buffer.hpp"
#include "memory_stream.hpp"

#if __has_include(<filesystem>)
#include <filesystem>
#elif __has_include(<experimental/filesystem>)
Expand Down
Loading
Loading