Skip to content

Commit

Permalink
NPUW: Fix SPATIAL execution mode (#28862)
Browse files Browse the repository at this point in the history
### Details:
- This is mainly a workaround, but it allows to keep using strided
tensors without a copy.

### Tickets:
 - *E-156237*, *E-156237*
  • Loading branch information
dmatveev authored Feb 7, 2025
1 parent a0022e7 commit 06c3f20
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,10 @@ void ov::npuw::JustInferRequest::unsafe_infer(std::size_t real_idx) {
// Collect spatial inputs for this offset
for (auto&& param : spatial.params) {
const auto& iport = comp_model_desc.compiled_model->inputs()[param.idx];
r->set_tensor(
iport,
ov::npuw::util::view(m_spatial_io[real_idx].inputs.at(param.idx), param.dim, offset, spatial.nway));
const auto& iview = ov::npuw::util::view(m_spatial_io[real_idx].inputs.at(param.idx),
param.dim, offset,
spatial.nway);
r->set_tensor(iport, iview);
} // for(params)

// Now set the spatial outputs
Expand Down
23 changes: 22 additions & 1 deletion src/plugins/intel_npu/src/plugin/npuw/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,32 @@ ov::SoPtr<ov::ITensor> ov::npuw::util::view(const ov::SoPtr<ov::ITensor>& src,
std::size_t offset,
std::size_t len) {
const auto& shape = src->get_shape();
NPUW_ASSERT(dim < shape.size());
View view_start = View(shape.size(), 0u);
View view_end = shape;
view_start[dim] = offset;
view_end[dim] = offset + len;
return ov::npuw::util::view(src, view_start, view_end);

auto ret_view = ov::npuw::util::view(src, view_start, view_end);

// Check if a tensor view can be faked as "continuous"
// FIXME: This trick should be removed after strided tensor
// checks are relaxed
if (std::all_of(shape.begin(), shape.begin() + dim, [](std::size_t d) { return d == 1u; })) {
// If all dimensions up to the sub-ranged dimension are 1s,
// This tensor can be faked as continuous
const auto type = src->get_element_type();
const auto view_shape = ret_view->get_shape();
ov::Strides fake_strides(shape.size());
fake_strides.back() = type.size();
std::transform(view_shape.crbegin(),
view_shape.crend() - 1,
fake_strides.rbegin(),
fake_strides.rbegin() + 1,
std::multiplies<size_t>());
return ov::get_tensor_impl(ov::Tensor(type, view_shape, ret_view->data(), fake_strides));
}
return ret_view;
}

template <typename InT>
Expand Down

0 comments on commit 06c3f20

Please sign in to comment.