-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constants computed on-the-fly and mmapped Constants #27705
Draft
slyalin
wants to merge
10
commits into
openvinotoolkit:master
Choose a base branch
from
slyalin:postppned_constants
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−21
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
205c1f1
Noticed issues in the hashing of serialized binary blobs.
slyalin 58719b7
Approach to mark nodes in a model to constant fold them during serial…
slyalin 4117906
update const duplicate search
pavel-esir be7d6be
Added save_tensor_data and read_tensor_data declarations, implemented…
slyalin 04e5491
Merge remote-tracking branch 'slyalin/postppned_constants' into postp…
slyalin af3d08c
Minor: indentation alignment
slyalin 5ea7712
Merge remote-tracking branch 'origin/master' into postppned_constants
slyalin e684725
Use get_tensor_view recently added to Python API. Explicitly set shar…
slyalin 1bc1e47
Fix code style
slyalin f74ea21
Merge branch 'master' into postppned_constants
slyalin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/bindings/python/src/openvino/runtime/utils/mmapped_constant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2018-2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import openvino | ||
|
||
|
||
# Creates a new file with a given name, populates it with data from a given Constant, | ||
# returns a new Constant node with content memory-mapped to that file. | ||
# Doesn't remove the file in the end of the returned Constant's life time. | ||
def move_constant_to_file(constant, path): | ||
openvino.save_tensor_data(constant.get_tensor_view(), path) | ||
mmapped = openvino.read_tensor_data(path, constant.get_output_element_type(0), constant.get_output_partial_shape(0)) | ||
return openvino.runtime.op.Constant(mmapped, shared_memory=True) |
35 changes: 35 additions & 0 deletions
35
src/bindings/python/src/openvino/runtime/utils/postponed_constant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2018-2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import openvino | ||
|
||
"""Postponed Constant is a way to materialize a big constant only when it is going to be serialized to IR and then immediately dispose.""" | ||
|
||
|
||
# `maker` is a function that returns ov.Tensor that represents a target Constant | ||
def make_postponed_constant(element_type, shape, maker): | ||
class PostponedConstant(openvino.Op): | ||
class_type_info = openvino.runtime.DiscreteTypeInfo("PostponedConstant", "extension") | ||
|
||
def __init__(self): | ||
super().__init__(self) | ||
self.get_rt_info()["postponed_constant"] = True # value doesn't matter | ||
self.m_element_type = element_type | ||
self.m_shape = shape | ||
self.constructor_validate_and_infer_types() | ||
|
||
def get_type_info(self): | ||
return PostponedConstant.class_type_info | ||
|
||
def evaluate(self, outputs, _): | ||
maker().copy_to(outputs[0]) | ||
return True | ||
|
||
def clone_with_new_inputs(self, _): | ||
return PostponedConstant() | ||
|
||
def validate_and_infer_types(self): | ||
self.set_output_type(0, self.m_element_type, openvino.PartialShape(self.m_shape)) | ||
|
||
return PostponedConstant() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "openvino/core/partial_shape.hpp" | ||
#include "openvino/runtime/tensor.hpp" | ||
|
||
namespace ov { | ||
|
||
/// \brief Save given tensor data into a file. File will contain only raw bytes of a tensor.data as it is allocated in | ||
/// memory. | ||
/// No element type nor shape nor other metadata are serialized. Strides are preserved. | ||
/// \param tensor Tensor which data will be serialized. | ||
/// \param file_name Path to the output file | ||
OPENVINO_API | ||
void save_tensor_data(const Tensor& tensor, const std::string& file_name); | ||
|
||
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) | ||
OPENVINO_API | ||
void save_tensor_data(const Tensor& tensor, const std::wstring& output_model); | ||
#endif | ||
|
||
/// \brief Read a tensor content from a file. Only raw data is loaded. | ||
/// \param file_name Path to the output file | ||
/// \param element_type Element type, when not specified the it is assumed as element::u8. | ||
/// \param shape Shape for resulting tensor. If provided shape is static, specified number of elements is read only. | ||
/// File should contain enough bytes, an exception is raised otherwise. | ||
/// One of the dimensions can be dynamic. In this case it will be determined automatically based on the | ||
/// length of the file content and `offset`. Default value is [?]. | ||
/// \param offset Read file starting from specified offset. Default is 0. The remining size of the file should be | ||
/// compatible with shape. \param mmap Use mmap that postpones real read from file until data is accessed. | ||
OPENVINO_API | ||
Tensor read_tensor_data(const std::string& file_name, | ||
const element::Type& element_type = element::u8, | ||
const PartialShape& shape = PartialShape{Dimension::dynamic()}, | ||
std::size_t offset = 0, | ||
bool mmap = true); | ||
|
||
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) | ||
OPENVINO_API | ||
Tensor read_tensor_data(const std::wstring& file_name, | ||
const element::Type& element_type = element::u8, | ||
const PartialShape& shape = PartialShape{Dimension::dynamic()}, | ||
std::size_t offset = 0, | ||
bool mmap = true); | ||
#endif | ||
|
||
/// \brief Read raw data from a file into pre-allocated tensor. | ||
/// \param file_name Path to the input file with raw tensor data. | ||
/// \param tensor Tensor to read data to. Tensor should have correct element_type and shape set that is used to | ||
/// determine how many bytes will be read from the file. \param offset Read file starting from specified offset. Default | ||
/// is 0. The remining part of the file should contain enough bytes to satisfy tensor size. | ||
OPENVINO_API | ||
void read_tensor_data(const std::string& file_name, Tensor& tensor, std::size_t offset = 0); | ||
|
||
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) | ||
OPENVINO_API | ||
void read_tensor_data(const std::wstring& file_name, Tensor& tensor, std::size_t offset = 0); | ||
#endif | ||
|
||
/// \brief Read raw data from a file into a tensor. Optionally re-allocate memory in tensor if required. | ||
/// \param file_name Path to the input file with raw tensor data. | ||
/// \param tensor Tensor to read data to. Memory is allocated using set_shape method. | ||
/// \param shape Shape for resulting tensor. If provided shape is static, specified number of elements is read only. | ||
/// File should contain enough bytes, an exception is raised otherwise. | ||
/// One of the dimensions can be dynamic. In this case it will be determined automatically based on the | ||
/// length of the file content and `offset`. | ||
/// \param offset Read file starting from specified offset. Default is 0. The remining size of the file should be | ||
/// compatible with shape. | ||
OPENVINO_API | ||
void read_tensor_data(const std::string& file_name, Tensor& tensor, const PartialShape& shape, std::size_t offset = 0); | ||
|
||
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) | ||
OPENVINO_API | ||
void read_tensor_data(const std::wstring& file_name, Tensor& tensor, const PartialShape& shape, std::size_t offset = 0); | ||
#endif | ||
|
||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we allow the assignment of new
ov.Tensor
instances inoutputs
, then this copying won't be required. Another (probably better) option is to passoutputs[0]
tomaker
function to build the tensor in place.