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