-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SegmentMax-16 reference implementation (#29047)
### Details: - The original PR (#28788) has been mistakenly force-merged due to a mistake in merge queue settings. It was later reverted, so this is the "new" Ref PR. - Add reference implementation - Add tests ### Related PRs: - #28103 - #28698 - #28979 - #28999 ### Tickets: - CVS-158917 --------- Signed-off-by: p-wysocki <[email protected]> Co-authored-by: Roman Kazantsev <[email protected]> Co-authored-by: Pawel Raasz <[email protected]> Co-authored-by: Katarzyna Mitrus <[email protected]>
- Loading branch information
1 parent
e58f38f
commit 18e97b6
Showing
11 changed files
with
373 additions
and
14 deletions.
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
55 changes: 55 additions & 0 deletions
55
src/core/reference/include/openvino/reference/segment_max.hpp
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,55 @@ | ||
// Copyright (C) 2018-2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
#include <vector> | ||
|
||
#include "openvino/core/shape.hpp" | ||
|
||
namespace ov::reference { | ||
|
||
template <typename T, typename T_idx, std::enable_if_t<std::is_same<std::decay_t<T_idx>, int64_t>::value>* = nullptr> | ||
void segment_max(const T* data, | ||
const Shape& data_shape, | ||
const T_idx* segment_ids, | ||
T* out, | ||
const Shape& output_shape, | ||
const T empty_segment_value) { | ||
const T_idx num_segments = output_shape[0]; | ||
const auto inner_dim_size = shape_size(data_shape.begin() + 1, data_shape.end()); | ||
|
||
// Initialize output with empty_segment_value | ||
std::fill(out, out + num_segments * inner_dim_size, empty_segment_value); | ||
|
||
// Iterate over each element in the first dimension | ||
for (size_t i = 0; i < data_shape[0]; ++i) { | ||
const T_idx segment_id = segment_ids[i]; | ||
if (segment_id >= num_segments) { | ||
continue; | ||
} | ||
// Iterate over each element in the inner dimensions | ||
for (size_t j = 0; j < inner_dim_size; ++j) { | ||
const size_t index = i * inner_dim_size + j; | ||
const size_t out_index = segment_id * inner_dim_size + j; | ||
// Update the maximum value for the current segment and inner dimension | ||
out[out_index] = std::max(out[out_index], data[index]); | ||
} | ||
} | ||
} | ||
|
||
template <typename T, typename T_idx, std::enable_if_t<!std::is_same<std::decay_t<T_idx>, int64_t>::value>* = nullptr> | ||
void segment_max(const T* data, | ||
const Shape& data_shape, | ||
const T_idx* segment_ids, | ||
T* out, | ||
const Shape& output_shape, | ||
const T empty_segment_value) { | ||
std::vector<int64_t> segment_ids_int64(segment_ids, segment_ids + data_shape[0]); | ||
segment_max(data, data_shape, segment_ids_int64.data(), out, output_shape, empty_segment_value); | ||
} | ||
|
||
} // namespace ov::reference |
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
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
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-2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "openvino/reference/segment_max.hpp" | ||
|
||
#include "element_visitor.hpp" | ||
#include "evaluate_node.hpp" | ||
#include "segment_max_shape_inference.hpp" | ||
|
||
template <ov::element::Type_t ET_data, ov::element::Type_t ET_idx> | ||
bool evaluate_index_type(const std::shared_ptr<ov::op::v16::SegmentMax>& op, | ||
ov::TensorVector& outputs, | ||
const ov::TensorVector& inputs) { | ||
using T_data = typename ov::element_type_traits<ET_data>::value_type; | ||
using T_idx = typename ov::element_type_traits<ET_idx>::value_type; | ||
auto input_shapes = std::vector<ov::PartialShape>{op->get_input_shape(0), op->get_input_shape(1)}; | ||
if (op->inputs().size() == 3) { | ||
input_shapes.emplace_back(op->get_input_shape(2)); | ||
} | ||
const auto output_shape = | ||
ov::op::v16::shape_infer(op.get(), input_shapes, make_tensor_accessor(inputs)).front().to_shape(); | ||
outputs.front().set_shape(output_shape); | ||
const auto empty_segment_value = | ||
op->get_fill_mode() == ov::op::FillMode::ZERO ? T_data(0) : std::numeric_limits<T_data>::lowest(); | ||
ov::reference::segment_max(inputs[0].data<const T_data>(), | ||
inputs[0].get_shape(), | ||
inputs[1].data<const T_idx>(), | ||
outputs[0].data<T_data>(), | ||
outputs[0].get_shape(), | ||
empty_segment_value); | ||
return true; | ||
} | ||
|
||
template <ov::element::Type_t ET_data> | ||
bool evaluate_data_type(const std::shared_ptr<ov::op::v16::SegmentMax>& op, | ||
ov::TensorVector& outputs, | ||
const ov::TensorVector& inputs) { | ||
const auto& index_type = op->get_input_element_type(1); | ||
using ov::op::v16::SegmentMax; | ||
using namespace ov::element; | ||
switch (index_type) { | ||
case i32: | ||
return evaluate_index_type<ET_data, i32>(ov::as_type_ptr<SegmentMax>(op), outputs, inputs); | ||
case i64: | ||
return evaluate_index_type<ET_data, i64>(ov::as_type_ptr<SegmentMax>(op), outputs, inputs); | ||
default: | ||
OPENVINO_THROW("Unhandled index type ", index_type, " in evaluate_node()"); | ||
} | ||
} | ||
|
||
template <> | ||
bool evaluate_node<ov::op::v16::SegmentMax>(std::shared_ptr<ov::Node> node, | ||
ov::TensorVector& outputs, | ||
const ov::TensorVector& inputs) { | ||
const auto& element_type = node->get_output_element_type(0); | ||
|
||
using ov::op::v16::SegmentMax; | ||
using namespace ov::element; | ||
switch (element_type) { | ||
case i8: | ||
return evaluate_data_type<i8>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
case i32: | ||
return evaluate_data_type<i32>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
case i64: | ||
return evaluate_data_type<i64>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
case u8: | ||
return evaluate_data_type<u8>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
case u32: | ||
return evaluate_data_type<u32>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
case u64: | ||
return evaluate_data_type<u64>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
case f16: | ||
return evaluate_data_type<f16>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
case f32: | ||
return evaluate_data_type<f32>(ov::as_type_ptr<SegmentMax>(node), outputs, inputs); | ||
default: | ||
OPENVINO_THROW("Unhandled data type ", element_type, " in evaluate_node()"); | ||
} | ||
} |
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.