-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[NPUW] Enable vocabulary sharing for the asymmetric vocabulary case #37054
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
Open
AsyaPronina
wants to merge
10
commits into
openvinotoolkit:master
Choose a base branch
from
AsyaPronina:save_not_all_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.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4112c1e
[NPUW] Share 1 vocab
AsyaPronina a5d3c8d
U8 Vocab and ZP -> I8 Vocab and ZP
AsyaPronina 8d41dbb
Polishing
AsyaPronina 3559c18
Removed memory consumption by transient buffers and constants without…
AsyaPronina df1db01
Added I8 and U8 cases differently
AsyaPronina 4e053db
Fixed option issue
AsyaPronina 3e9fd41
Support of optimization. Temporarily default
AsyaPronina e2a0a67
Working and performant version of sharing
AsyaPronina f6354b4
Fixed Clang-format
AsyaPronina bda3966
NPUW: default-enable ASYM vocab-as-input and MatMul-first vocab optio…
esmirno 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -37,8 +37,11 @@ Const::Const(const std::shared_ptr<ov::op::v0::Constant>& n) : m_node(n) { | |
| m_offset = weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset; | ||
| } else { | ||
| // See the comment in serialize() for more details | ||
| LOG_WARN("Some pattern introduced a new Constant node not present in the original weights file. We need to " | ||
| "keep it in case export occurs. This will increase memory consumption."); | ||
| LOG_WARN("Some pattern introduced a new Constant node, " | ||
| << m_node | ||
| << ", not present in the " | ||
| "original weights file. We need to keep it in case export occurs. This will increase " | ||
| "memory consumption."); | ||
| m_copied_if_not_in_model = ov::npuw::util::copy_tensor_from_const(m_node); | ||
| } | ||
| } | ||
|
|
@@ -57,9 +60,9 @@ bool Const::operator==(const Const& other) const { | |
| m_cached_ptr == other.m_cached_ptr); | ||
| } | ||
|
|
||
| ov::Tensor Const::eval() const { | ||
| ov::Tensor Const::eval_view() const { | ||
| if (m_node) { | ||
| return ov::npuw::util::copy_tensor_from_const(m_node); | ||
| return ov::npuw::util::tensor_from_const(m_node); | ||
| } | ||
|
Comment on lines
-60
to
66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??? |
||
|
|
||
| // Weightless import case. Mmmap CPU weight on demand to avoid allocating all weights at once. | ||
|
|
@@ -83,6 +86,15 @@ ov::Tensor Const::eval() const { | |
| return m_read_from_bin; | ||
| } | ||
|
|
||
| ov::Tensor Const::eval() const { | ||
| if (m_node) { | ||
| return ov::npuw::util::copy_tensor_from_const(m_node); | ||
| } | ||
| // The import branches of eval_view() don't copy - the bank takes | ||
| // ownership on its side when required | ||
| return eval_view(); | ||
| } | ||
|
|
||
| LazyTensor::Meta Const::eval_meta() const { | ||
| if (m_node) { | ||
| return {m_node->get_shape(), m_node->get_element_type()}; | ||
|
|
@@ -356,6 +368,54 @@ void Gather::detach() { | |
| w.detach(); | ||
| } | ||
|
|
||
| std::size_t Sub128::hash() const { | ||
| std::size_t seed = std::hash<std::size_t>()(7u) + 0x9e3779b9; | ||
| seed ^= tensor.get_hash() + 0x9e3779b9; | ||
| return seed; | ||
| } | ||
|
|
||
| bool Sub128::operator==(const Sub128& other) const { | ||
| return tensor == other.tensor; | ||
| } | ||
|
|
||
| ov::Tensor Sub128::eval() const { | ||
| const auto trs = tensor.get_transformations(); | ||
|
|
||
| ov::Tensor src; | ||
| if (trs.size() == 1 && std::holds_alternative<op::Const>(trs.front())) { | ||
| // Fused path: read straight through a zero-copy view of the source, | ||
| // skipping the intermediate copy Const::eval() would make. The view is | ||
| // only ever READ here, so this is correct for all Const flavors, | ||
| // including the deserialized ones (read-only mmap / cached bin tensor) | ||
| src = std::get<op::Const>(trs.front()).eval_view(); | ||
| } else { | ||
| src = tensor.eval(); | ||
| } | ||
|
|
||
| const auto src_type = src.get_element_type(); | ||
| NPUW_ASSERT(src_type == ov::element::u8 || src_type == ov::element::i8); | ||
|
|
||
| ov::Tensor dst(ov::element::i8, src.get_shape()); | ||
| const auto* s = static_cast<const uint8_t*>(src.data()); | ||
| auto* d = dst.data<int8_t>(); | ||
| for (std::size_t i = 0, n = src.get_size(); i < n; ++i) { | ||
| d[i] = static_cast<int8_t>(static_cast<int8_t>(s[i]) - 128); | ||
| } | ||
| return dst; | ||
| } | ||
|
Comment on lines
+381
to
+405
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly this could've been a generic subtract, I see no point in leaving it 128 only |
||
|
|
||
| LazyTensor::Meta Sub128::eval_meta() const { | ||
| return {tensor.eval_meta().shape, ov::element::i8}; | ||
| } | ||
|
|
||
| void Sub128::read_weight(const ov::npuw::s11n::WeightsContext& ctx) { | ||
| tensor.read_weight(ctx); | ||
| } | ||
|
|
||
| void Sub128::detach() { | ||
| tensor.detach(); | ||
| } | ||
|
|
||
| } // namespace op | ||
|
|
||
| // Stable, permanently assigned op-type IDs. | ||
|
|
@@ -368,6 +428,7 @@ enum class TransformType : std::uint16_t { | |
| PERMUTE = 4, | ||
| CONVERT = 5, | ||
| GATHER = 6, | ||
| SUB128 = 7, | ||
| }; | ||
|
|
||
| struct LazyTensorImpl { | ||
|
|
@@ -430,6 +491,10 @@ ov::npuw::weights::TransformType get_transform_type(const ov::npuw::weights::op: | |
| return ov::npuw::weights::TransformType::GATHER; | ||
| } | ||
|
|
||
| ov::npuw::weights::TransformType get_transform_type(const ov::npuw::weights::op::Sub128&) { | ||
| return ov::npuw::weights::TransformType::SUB128; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace ov { | ||
|
|
@@ -500,6 +565,10 @@ void Gather::serialize(ov::npuw::orc::Stream& stream) { | |
| } | ||
| } | ||
|
|
||
| void Sub128::serialize(ov::npuw::orc::Stream& stream) { | ||
| stream & tensor; | ||
| } | ||
|
|
||
| } // namespace op | ||
|
|
||
| void LazyTensorImpl::serialize(ov::npuw::orc::Stream& stream) { | ||
|
|
@@ -537,6 +606,9 @@ void LazyTensorImpl::serialize(ov::npuw::orc::Stream& stream) { | |
| case TransformType::GATHER: | ||
| m_transform.emplace<op::Gather>(ov::npuw::orc::load_versioned_payload<op::Gather>(section)); | ||
| break; | ||
| case TransformType::SUB128: | ||
| m_transform.emplace<op::Sub128>(ov::npuw::orc::load_versioned_payload<op::Sub128>(section)); | ||
|
Comment on lines
+609
to
+610
|
||
| break; | ||
| default: | ||
| OPENVINO_THROW("ORC LazyTensor: unknown op_type ", section.type, " — please upgrade NPUW"); | ||
| break; | ||
|
|
@@ -649,6 +721,10 @@ void LazyTensorImpl::get_transformations(std::vector<LazyTensor::Transform>& vec | |
| auto next_tr = op.w.get_transformations(); | ||
| vec.insert(vec.end(), next_tr.begin(), next_tr.end()); | ||
| }, | ||
| [&vec](const op::Sub128& op) { | ||
| auto next_tr = op.tensor.get_transformations(); | ||
| vec.insert(vec.end(), next_tr.begin(), next_tr.end()); | ||
| }, | ||
| }, | ||
| m_transform); | ||
| } | ||
|
|
@@ -688,6 +764,12 @@ LazyTensor LazyTensor::convert(const ov::element::Type& type) { | |
| return new_lt; | ||
| } | ||
|
|
||
| LazyTensor LazyTensor::sub128() { | ||
| LazyTensor new_lt; | ||
| new_lt.m_impl = std::make_shared<LazyTensorImpl>(op::Sub128(*this)); | ||
| return new_lt; | ||
| } | ||
|
|
||
| bool LazyTensor::operator==(const LazyTensor& other) const { | ||
| if (!m_impl && !other.m_impl) { | ||
| return true; | ||
|
|
||
This file contains hidden or 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.
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.
Do we need all these options? I don't think so.
If there was found a way to share asym (u4) vocabs, let just do it by default