@@ -27,20 +27,15 @@ class SharedContext : public WeakSingleton<SharedContext> {
27
27
struct SharedWeights {
28
28
struct Header {
29
29
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_;
34
32
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
+
44
39
struct Metadata {
45
40
struct Key {
46
41
std::string name;
@@ -60,8 +55,10 @@ class SharedContext : public WeakSingleton<SharedContext> {
60
55
std::shared_ptr<ov::Tensor> tensor;
61
56
};
62
57
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);
65
62
};
66
63
67
64
struct SubgraphMetadata {
@@ -75,12 +72,16 @@ class SharedContext : public WeakSingleton<SharedContext> {
75
72
}
76
73
};
77
74
struct Value {
78
- uint32_t epctx_offset;
79
- uint32_t epctx_length;
75
+ long epctx_offset;
76
+ size_t epctx_length;
80
77
};
81
78
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);
84
85
};
85
86
86
87
struct WeightsFile {
@@ -96,22 +97,49 @@ class SharedContext : public WeakSingleton<SharedContext> {
96
97
};
97
98
98
99
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
- // }
106
100
fs::path shared_bin_filename;
107
- std::ofstream bin_file_;
101
+ std::fstream bin_file_;
102
+ size_t bin_size_;
108
103
109
104
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
+
111
132
112
- void openBinFile (fs::path shared_bin_filename) {
113
133
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
+
115
143
if (!bin_file_) {
116
144
throw std::runtime_error (" Failed to open log file!" );
117
145
}
@@ -121,10 +149,9 @@ class SharedContext : public WeakSingleton<SharedContext> {
121
149
122
150
fs::path external_weight_filename;
123
151
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_;
127
153
Metadata::Map metadata;
154
+ SubgraphMetadata subgraph_metadata_;
128
155
SubgraphMetadata::Map subgraph_metadata;
129
156
}shared_weights;
130
157
};
0 commit comments