|
11 | 11 | #include "openvino/core/shape.hpp"
|
12 | 12 | #include "openvino/core/shape_util.hpp"
|
13 | 13 | #include "openvino/core/strides.hpp"
|
| 14 | +#include "openvino/core/tensor_util.hpp" |
| 15 | +#include "openvino/core/type/element_iterator.hpp" |
14 | 16 | #include "openvino/runtime/itensor.hpp"
|
15 | 17 | #include "openvino/runtime/make_tensor.hpp"
|
16 | 18 | #include "openvino/runtime/remote_tensor.hpp"
|
| 19 | +#include "openvino/runtime/shared_buffer.hpp" |
| 20 | +#include "openvino/util/mmap_object.hpp" |
17 | 21 |
|
18 | 22 | namespace ov {
|
19 | 23 |
|
@@ -108,4 +112,76 @@ bool Tensor::is_continuous() const {
|
108 | 112 | OV_TENSOR_STATEMENT(return _impl->is_continuous());
|
109 | 113 | }
|
110 | 114 |
|
| 115 | +namespace { |
| 116 | +ov::Shape calc_static_shape_for_file(const std::filesystem::path& file_name, |
| 117 | + const ov::element::Type& element_type, |
| 118 | + const ov::PartialShape& partial_shape, |
| 119 | + size_t offset) { |
| 120 | + auto file_size = std::filesystem::file_size(file_name); |
| 121 | + if (partial_shape.is_static()) { |
| 122 | + auto static_shape = partial_shape.get_shape(); |
| 123 | + OPENVINO_ASSERT((ov::shape_size(static_shape)) * element_type.bitwidth() + offset * 8 == file_size * 8, |
| 124 | + "Cannot fit file size into requested static PartialShape"); |
| 125 | + return static_shape; |
| 126 | + } |
| 127 | + auto partial_shape_copy = partial_shape; |
| 128 | + auto rank = partial_shape_copy.rank(); |
| 129 | + OPENVINO_ASSERT(rank.is_static(), "Rank cannot be dynamic"); |
| 130 | + std::vector<size_t> dynamic_dimension_numbers; |
| 131 | + size_t slice_size = 1; |
| 132 | + for (size_t id = 0; id < partial_shape_copy.size(); ++id) { |
| 133 | + if (partial_shape_copy[id].is_dynamic()) { |
| 134 | + dynamic_dimension_numbers.push_back(id); |
| 135 | + } else { |
| 136 | + slice_size *= partial_shape_copy[id].get_min_length(); |
| 137 | + } |
| 138 | + } |
| 139 | + OPENVINO_ASSERT(dynamic_dimension_numbers.size() == 1, |
| 140 | + "Only one dynamic dimension in input shape is supported but got: ", |
| 141 | + dynamic_dimension_numbers.size()); |
| 142 | + auto& dynamic_dimension = partial_shape_copy[dynamic_dimension_numbers[0]]; |
| 143 | + |
| 144 | + OPENVINO_ASSERT(file_size > offset, "Offset is bigger than size of file to read."); |
| 145 | + auto file_size_to_read = file_size - offset; |
| 146 | + |
| 147 | + OPENVINO_ASSERT((file_size_to_read * 8) % element_type.bitwidth() == 0, |
| 148 | + "cannot fit ", |
| 149 | + element_type.get_type_name(), |
| 150 | + " into ", |
| 151 | + file_size_to_read, |
| 152 | + " bytes"); |
| 153 | + auto elements_to_read = file_size_to_read * 8 / element_type.bitwidth(); |
| 154 | + |
| 155 | + auto new_dimension_size = elements_to_read / slice_size; |
| 156 | + OPENVINO_ASSERT(new_dimension_size * slice_size == elements_to_read, |
| 157 | + "Cannot fit file size into requested PartialShape"); |
| 158 | + |
| 159 | + OPENVINO_ASSERT(dynamic_dimension.compatible(new_dimension_size), |
| 160 | + "Cannot fit file size into requested PartialShape"); |
| 161 | + |
| 162 | + dynamic_dimension = Dimension(new_dimension_size); |
| 163 | + return partial_shape_copy.get_shape(); |
| 164 | +} |
| 165 | +} // namespace |
| 166 | + |
| 167 | +Tensor read_tensor_data(const std::filesystem::path& file_name, |
| 168 | + const ov::element::Type& element_type, |
| 169 | + const ov::PartialShape& partial_shape, |
| 170 | + size_t offset_in_bytes) { |
| 171 | + OPENVINO_ASSERT(element_type != ov::element::string); |
| 172 | + auto static_shape = calc_static_shape_for_file(file_name, element_type, partial_shape, offset_in_bytes); |
| 173 | + |
| 174 | + auto mapped_memory = ov::load_mmap_object(file_name); |
| 175 | + auto shared_buffer = |
| 176 | + std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(mapped_memory->data() + offset_in_bytes, |
| 177 | + mapped_memory->size() - offset_in_bytes, |
| 178 | + mapped_memory); |
| 179 | + |
| 180 | + auto view_tensor = Tensor(element_type, static_shape, shared_buffer->get_ptr()); |
| 181 | + auto impl = get_tensor_impl(view_tensor); |
| 182 | + impl._so = shared_buffer; |
| 183 | + view_tensor = make_tensor(impl); |
| 184 | + |
| 185 | + return view_tensor; |
| 186 | +} |
111 | 187 | } // namespace ov
|
0 commit comments