@@ -26,20 +26,15 @@ class SharedContext : public WeakSingleton<SharedContext> {
2626 struct SharedWeights {
2727 struct Header {
2828 uint32_t bin_version=1 ;
29- uint32_t footer_offset;
30- Header (uint32_t bin_in, uint32_t footer_in) :
31- bin_version (bin_in), footer_offset(footer_in){}
32- };
29+ long footer_offset=0 ;
30+ }header_;
3331 struct Footer {
34- uint32_t subgraph_offset;
35- uint32_t subgraph_length;
36- uint32_t metadata_offset;
37- uint32_t metadata_length;
38- Footer (uint32_t subgraph_offset_in, uint32_t subgraph_length_in,
39- uint32_t metadata_offset_in, uint32_t metadata_length_in) :
40- subgraph_offset (subgraph_offset_in), subgraph_length(subgraph_length_in),
41- metadata_offset (metadata_offset_in), metadata_length(metadata_length_in) {}
42- };
32+ long subgraph_offset;
33+ size_t subgraph_length;
34+ long metadata_offset;
35+ size_t metadata_length;
36+ }footer_;
37+
4338 struct Metadata {
4439 struct Key {
4540 std::string name;
@@ -59,8 +54,10 @@ class SharedContext : public WeakSingleton<SharedContext> {
5954 std::shared_ptr<ov::Tensor> tensor;
6055 };
6156 using Map = std::unordered_map<Key, Value, Hash>;
62- friend std::ostream& operator <<(std::ostream& right, const Metadata::Map& metadata);
63- friend std::istream& operator >>(std::istream& right, Metadata::Map& metadata);
57+ void writeMetadataToBinaryFile (SharedContext& shared_context, const Metadata::Map& metadata);
58+ void readMetadataFromBinaryFile (SharedContext& shared_context, Metadata::Map& metadata);
59+ // friend std::ostream& operator<<(std::ostream& right, const Metadata::Map& metadata);
60+ // friend std::istream& operator>>(std::istream& right, Metadata::Map& metadata);
6461 };
6562
6663 struct SubgraphMetadata {
@@ -74,12 +71,16 @@ class SharedContext : public WeakSingleton<SharedContext> {
7471 }
7572 };
7673 struct Value {
77- uint32_t epctx_offset;
78- uint32_t epctx_length;
74+ long epctx_offset;
75+ size_t epctx_length;
7976 };
8077 using Map = std::unordered_map<Key, Value, Hash>;
81- friend std::ostream& operator <<(std::ostream& right, const SubgraphMetadata::Map& subgraph_metadata);
82- friend std::istream& operator >>(std::istream& right, SubgraphMetadata::Map& subgraph_metadata);
78+ void writeSubgraphDataToBinaryFile (SharedContext& shared_context,
79+ const SubgraphMetadata::Map& subgraph_metadata);
80+ void readSubgraphDataFromBinaryFile (SharedContext& shared_context,
81+ SubgraphMetadata::Map& subgraph_metadata);
82+ // friend std::ostream& operator<<(std::ostream& right, const SubgraphMetadata::Map& subgraph_metadata);
83+ // friend std::istream& operator>>(std::istream& right, SubgraphMetadata::Map& subgraph_metadata);
8384 };
8485
8586 struct WeightsFile {
@@ -95,22 +96,49 @@ class SharedContext : public WeakSingleton<SharedContext> {
9596 };
9697
9798 struct SharedBinFile {
98- // ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(SharedBinFile);
99- // SharedBinFile() = delete;
100- // SharedBinFile(fs::path shared_bin_filename) :
101- // bin_file_(shared_bin_filename, std::ios::out | std::ios::app| std::ios::binary) {
102- // if(bin_file_.is_open())
103- // std::cout << " Bin file opened " << std::endl;
104- // }
10599 fs::path shared_bin_filename;
106- std::ofstream bin_file_;
100+ std::fstream bin_file_;
101+ size_t bin_size_;
107102
108103 SharedBinFile () = default ; // Default constructor
109- ~SharedBinFile () = default ; // Prevent closing the file automatically
104+ ~SharedBinFile () {
105+ if (bin_file_.is_open ()) {
106+ bin_file_.close (); // Close file when object is destroyed
107+ }
108+ }
109+
110+ void openBinFile (const fs::path shared_bin_filename) {
111+ // Check if the file exists before trying to open
112+ if (!fs::exists (shared_bin_filename)) {
113+ std::cerr << " Error: The file does not exist at path: " << shared_bin_filename << std::endl;
114+ std::ofstream createFile (shared_bin_filename, std::ios::binary); // Create an empty binary file
115+ if (!createFile) {
116+ throw std::runtime_error (" Failed to create the file!" );
117+ }
118+ createFile.close ();
119+ // throw std::runtime_error("Failed to open log file! File does not exist.");
120+ }
121+
122+ // Check if the file is accessible for reading and writing
123+ fs::perms file_perms = fs::status (shared_bin_filename).permissions ();
124+
125+ if ((file_perms & fs::perms::owner_read) == fs::perms::none ||
126+ (file_perms & fs::perms::owner_write) == fs::perms::none) {
127+ std::cerr << " Error: Insufficient permissions for file: " << shared_bin_filename << std::endl;
128+ throw std::runtime_error (" Failed to open log file! Insufficient permissions." );
129+ }
130+
110131
111- void openBinFile (fs::path shared_bin_filename) {
112132 if (!bin_file_.is_open ()) { // Prevent reopening
113- bin_file_.open (shared_bin_filename, std::ios::out | std::ios::app | std::ios::binary);
133+ std::cout << " Bin file is not open " << std::endl;
134+ bin_file_.open (shared_bin_filename, std::ios::in | std::ios::out | std::ios::binary);
135+ std::cout << " bin file opened " << std::endl;
136+ bin_size_ = bin_file_.seekg (0 , std::ios::end).tellg ();
137+
138+ std::cout << " bin size = " << bin_size_ << std::endl;
139+ bin_file_.seekg (0 , std::ios::beg); // Reset to the beginning of the file
140+
141+
114142 if (!bin_file_) {
115143 throw std::runtime_error (" Failed to open log file!" );
116144 }
@@ -120,10 +148,9 @@ class SharedContext : public WeakSingleton<SharedContext> {
120148
121149 fs::path external_weight_filename;
122150 std::unique_ptr<WeightsFile> mapped_weights;
123- std::unique_ptr<Header> header_;
124- std::unique_ptr<Footer> footer_;
125- // std::unique_ptr<SharedBinFile> shared_bin_file;
151+ Metadata metadata_;
126152 Metadata::Map metadata;
153+ SubgraphMetadata subgraph_metadata_;
127154 SubgraphMetadata::Map subgraph_metadata;
128155 }shared_weights;
129156};
0 commit comments