Skip to content

Commit b752c49

Browse files
Starting the blobreader
1 parent 9a00f21 commit b752c49

File tree

6 files changed

+84
-7
lines changed

6 files changed

+84
-7
lines changed

src/plugins/intel_npu/src/common/include/intel_npu/common/blob_reader.hpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,43 @@
55
#pragma once
66

77
#include <cinttypes>
8+
#include <functional>
89
#include <unordered_map>
910
#include <unordered_set>
1011
#include <vector>
1112

13+
#include "cre.hpp"
14+
1215
namespace intel_npu {
1316

1417
class BlobReader {
1518
public:
16-
BlobReader();
19+
BlobReader(std::istream& source);
20+
21+
BlobReader(const ov::Tensor& source);
22+
23+
void read();
24+
25+
void register_reader(
26+
const SectionID section_id,
27+
std::function<std::shared_ptr<ISection>(const BlobSource&, const std::unordered_map<SectionID, uint64_t>&)>
28+
reader);
29+
30+
std::shared_ptr<ISection> retrieve_section(const SectionID section_id);
31+
32+
private:
33+
friend class BlobWriter;
34+
35+
void read_data_from_source(char* destination, const size_t size);
36+
37+
BlobSource m_source;
38+
std::unordered_map<SectionID, uint64_t> m_offsets_table;
39+
std::unordered_map<SectionID, std::shared_ptr<ISection>> m_parsed_sections;
40+
std::unordered_map<
41+
SectionID,
42+
std::function<std::shared_ptr<ISection>(const BlobSource&, const std::unordered_map<SectionID, uint64_t>&)>>
43+
m_readers;
44+
size_t m_cursor_offset;
1745
};
1846

1947
} // namespace intel_npu

src/plugins/intel_npu/src/common/include/intel_npu/common/blob_writer.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class BlobWriter {
1818
public:
1919
BlobWriter();
2020

21+
BlobWriter(BlobReader blob_reader);
22+
2123
void register_section(const std::shared_ptr<ISection>& section);
2224

2325
void register_offset_in_table(const SectionID id, const uint64_t offset);
@@ -35,7 +37,10 @@ class BlobWriter {
3537
std::vector<std::shared_ptr<ISection>> m_registered_sections;
3638
std::unordered_map<SectionID, uint64_t> m_offsets_table;
3739
std::shared_ptr<CRESection> m_cre;
38-
// TODO
40+
41+
/**
42+
* @brief TODO
43+
*/
3944
std::optional<std::streampos> m_stream_base = std::nullopt;
4045
};
4146

src/plugins/intel_npu/src/common/include/intel_npu/common/cre.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ class CRESection final : public ISection {
7171

7272
std::optional<uint64_t> get_length() const override;
7373

74-
// void read(BlobReader* reader) override;
75-
7674
void append_to_expression(const CRE::Token requirement_token);
7775

7876
void append_to_expression(const std::vector<CRE::Token>& requirement_tokens);
7977

78+
static std::shared_ptr<ISection> read(const BlobSource& source,
79+
const std::unordered_map<SectionID, uint64_t>& offsets_table);
80+
8081
private:
8182
CRE m_cre;
8283
};

src/plugins/intel_npu/src/common/include/intel_npu/common/isection.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@
99
#include <optional>
1010
#include <unordered_map>
1111
#include <unordered_set>
12+
#include <variant>
1213
#include <vector>
1314

15+
#include "openvino/runtime/tensor.hpp"
16+
1417
namespace intel_npu {
1518

1619
// TODOs: fix the circular dependencies
1720
// Move sections in directory
1821
// Unique SID - how do we reinforce this without compromising modularity? Description matching?
1922

2023
using SectionID = uint16_t;
24+
using BlobSource = std::variant<std::reference_wrapper<std::istream>,
25+
std::reference_wrapper<const ov::Tensor>>; // TODO find a better place
2126

2227
class BlobWriter;
2328
class BlobReader;
@@ -44,8 +49,6 @@ class ISection {
4449
// note necessary, saves some performance if provided
4550
virtual std::optional<uint64_t> get_length() const;
4651

47-
// virtual void read(BlobReader* reader) = 0;
48-
4952
SectionID get_section_id() const;
5053

5154
private:

src/plugins/intel_npu/src/common/src/blob_reader.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,39 @@
44

55
#include "intel_npu/common/blob_reader.hpp"
66

7-
namespace intel_npu {} // namespace intel_npu
7+
namespace intel_npu {
8+
9+
BlobReader::BlobReader(std::istream& source) : m_source(source) {}
10+
11+
BlobReader::BlobReader(const ov::Tensor& source) : m_source(source), m_cursor_offset(0) {}
12+
13+
void BlobReader::register_reader(
14+
const SectionID section_id,
15+
std::function<std::shared_ptr<ISection>(const BlobSource&, const std::unordered_map<SectionID, uint64_t>&)>
16+
reader) {
17+
m_readers[section_id] = reader;
18+
}
19+
20+
std::shared_ptr<ISection> BlobReader::retrieve_section(const SectionID section_id) {
21+
auto search_result = m_parsed_sections.find(section_id);
22+
if (search_result != m_parsed_sections.end()) {
23+
return search_result->second;
24+
}
25+
return nullptr;
26+
}
27+
28+
// TODO test the windows debug build works properly if using the "better" implementation
29+
void BlobReader::read_data_from_source(char* destination, const size_t size) {
30+
if (const std::reference_wrapper<std::istream>* stream =
31+
std::get_if<std::reference_wrapper<std::istream>>(&m_source)) {
32+
stream->get().read(destination, size);
33+
} else if (const std::reference_wrapper<const ov::Tensor>* tensor =
34+
std::get_if<std::reference_wrapper<const ov::Tensor>>(&m_source)) {
35+
std::memcpy(destination, tensor->get().data<const char>() + m_cursor_offset, size);
36+
m_cursor_offset += size;
37+
}
38+
}
39+
40+
void BlobReader::read() {}
41+
42+
} // namespace intel_npu

src/plugins/intel_npu/src/common/src/blob_writer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "intel_npu/common/blob_writer.hpp"
66

7+
#include "intel_npu/common/blob_reader.hpp"
78
#include "intel_npu/common/offsets_table.hpp"
89

910
namespace {
@@ -20,6 +21,10 @@ BlobWriter::BlobWriter() : m_cre(std::make_shared<CRESection>()) {
2021
register_section(m_cre);
2122
}
2223

24+
BlobWriter::BlobWriter(BlobReader blob_reader) {
25+
// TODO after reader is done, while constructing the compiledModel
26+
}
27+
2328
void BlobWriter::register_section(const std::shared_ptr<ISection>& section) {
2429
m_registered_sections.push_back(section);
2530
OPENVINO_ASSERT(!m_registered_sections_ids.count(section->get_section_id()));

0 commit comments

Comments
 (0)