Skip to content

Commit

Permalink
Add AlignedBuffer to OpenVINO developer API (openvinotoolkit#20532)
Browse files Browse the repository at this point in the history
* Add AlignedBuffer to OpenVINO developer API

* Fixed build

* Fixed code style and remove opset deprecation

* Fixed Windows build

* Fixed GNA

* Fixed comment
  • Loading branch information
ilyachur authored Oct 24, 2023
1 parent 84a0994 commit 7ceff55
Show file tree
Hide file tree
Showing 30 changed files with 351 additions and 204 deletions.
5 changes: 2 additions & 3 deletions src/bindings/python/src/pyopenvino/core/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "Python.h"
#include "openvino/core/except.hpp"
#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/util/common_util.hpp"

#define C_CONTIGUOUS py::detail::npy_api::constants::NPY_ARRAY_C_CONTIGUOUS_
Expand Down Expand Up @@ -170,13 +171,12 @@ ov::op::v0::Constant create_copied(ov::Tensor& tensor) {
return ov::op::v0::Constant(tensor.get_element_type(), tensor.get_shape(), const_cast<void*>(tensor.data()));
}

OPENVINO_SUPPRESS_DEPRECATED_START
template <>
ov::op::v0::Constant create_shared(py::array& array) {
// Check if passed array has C-style contiguous memory layout.
// If memory is going to be shared it needs to be contiguous before passing to the constructor.
if (array_helpers::is_contiguous(array)) {
auto memory = std::make_shared<ngraph::runtime::SharedBuffer<py::array>>(
auto memory = std::make_shared<ov::SharedBuffer<py::array>>(
static_cast<char*>(array.ndim() == 0 ? array.mutable_data() : array.mutable_data(0)),
array.ndim() == 0 ? array.itemsize() : array.nbytes(),
array);
Expand All @@ -185,7 +185,6 @@ ov::op::v0::Constant create_shared(py::array& array) {
// If passed array is not C-style, throw an error.
OPENVINO_THROW("SHARED MEMORY MODE FOR THIS CONSTANT IS NOT APPLICABLE! Passed numpy array must be C contiguous.");
}
OPENVINO_SUPPRESS_DEPRECATED_END

template <>
ov::op::v0::Constant create_shared(ov::Tensor& tensor) {
Expand Down
12 changes: 12 additions & 0 deletions src/common/snippets/src/pass/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "openvino/core/model.hpp"
#include "openvino/op/util/framework_node.hpp"
#include "openvino/opsets/opset1.hpp"
#include "openvino/runtime/aligned_buffer.hpp"
#include "transformations/rt_info/primitives_priority_attribute.hpp"

namespace ov {
Expand Down Expand Up @@ -180,6 +181,17 @@ class SnippetsHasher : public ov::AttributeVisitor {
m_hash = hash_combine(m_hash, data[i]);
}
}
} else if (const auto& a =
ov::as_type<ov::AttributeAdapter<std::shared_ptr<ov::AlignedBuffer>>>(&adapter)) {
if (name == "value" && m_node_type_name == "Constant") {
m_hash = hash_combine(m_hash, AttrType::constant);
const int64_t size = a->get()->size();
m_hash = hash_combine(hash_combine(m_hash, AttrType::size), size);
auto data = static_cast<const char*>(a->get()->get_ptr());
for (int64_t i = 0; i < size; i++) {
m_hash = hash_combine(m_hash, data[i]);
}
}
} else if (const auto& a = ov::as_type<ov::AttributeAdapter<ov::op::util::FrameworkNodeAttrs>>(&adapter)) {
const auto& attrs = a->get();
// Update node attributes in data field
Expand Down
75 changes: 75 additions & 0 deletions src/core/dev_api/openvino/runtime/aligned_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <memory>

#include "openvino/core/attribute_adapter.hpp"
#include "openvino/core/core_visibility.hpp"

namespace ov {
/// \brief Allocates a block of memory on the specified alignment. The actual size of the
/// allocated memory is larger than the requested size by the alignment, so allocating 1
/// byte
/// on 64 byte alignment will allocate 65 bytes.
class OPENVINO_API AlignedBuffer {
public:
// Allocator objects and the allocation interfaces are owned by the
// creators of AlignedBuffers. They need to ensure that the lifetime of
// allocator exceeds the lifetime of this AlignedBuffer.
AlignedBuffer(size_t byte_size, size_t alignment = 64);

AlignedBuffer();
virtual ~AlignedBuffer();

AlignedBuffer(AlignedBuffer&& other);
AlignedBuffer& operator=(AlignedBuffer&& other);

size_t size() const {
return m_byte_size;
}
void* get_ptr(size_t offset) const {
return m_aligned_buffer + offset;
}
void* get_ptr() {
return m_aligned_buffer;
}
const void* get_ptr() const {
return m_aligned_buffer;
}
template <typename T>
T* get_ptr() {
return reinterpret_cast<T*>(m_aligned_buffer);
}
template <typename T>
const T* get_ptr() const {
return reinterpret_cast<const T*>(m_aligned_buffer);
}

template <typename T>
explicit operator T*() {
return get_ptr<T>();
}

private:
AlignedBuffer(const AlignedBuffer&) = delete;
AlignedBuffer& operator=(const AlignedBuffer&) = delete;

protected:
char* m_allocated_buffer;
char* m_aligned_buffer;
size_t m_byte_size;
};

template <>
class OPENVINO_API AttributeAdapter<std::shared_ptr<ov::AlignedBuffer>>
: public DirectValueAccessor<std::shared_ptr<ov::AlignedBuffer>> {
public:
AttributeAdapter(std::shared_ptr<ov::AlignedBuffer>& value);

OPENVINO_RTTI("AttributeAdapter<std::shared_ptr<ov::AlignedBuffer>");
};

} // namespace ov
31 changes: 31 additions & 0 deletions src/core/dev_api/openvino/runtime/shared_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "openvino/runtime/aligned_buffer.hpp"

namespace ov {

/// \brief SharedBuffer class to store pointer to pre-acclocated buffer.
template <typename T>
class SharedBuffer : public ov::AlignedBuffer {
public:
SharedBuffer(char* data, size_t size, const T& shared_object) : _shared_object(shared_object) {
m_allocated_buffer = data;
m_aligned_buffer = data;
m_byte_size = size;
}

virtual ~SharedBuffer() {
m_aligned_buffer = nullptr;
m_allocated_buffer = nullptr;
m_byte_size = 0;
}

private:
T _shared_object;
};

} // namespace ov
1 change: 0 additions & 1 deletion src/core/include/ngraph/op/util/op_annotations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ struct NGRAPH_API_DEPRECATED oi_pair {
};

/// \brief Base class for annotations added to graph ops

class NGRAPH_API_DEPRECATED NGRAPH_API OpAnnotations {
NGRAPH_SUPPRESS_DEPRECATED_START
public:
Expand Down
1 change: 1 addition & 0 deletions src/core/include/ngraph/op/util/slice_plan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace ngraph {
//
// A SlicePlan is used to collect parameters for these ops.
//
// This class is moved to dev API
struct NGRAPH_API_DEPRECATED NGRAPH_API SlicePlan {
// Parameters for the Slice
std::vector<int64_t> begins;
Expand Down
31 changes: 15 additions & 16 deletions src/core/include/ngraph/opsets/opset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ namespace ngraph {
class NGRAPH_API OpSet : public ov::OpSet {
public:
explicit OpSet(const ov::OpSet& opset);
NGRAPH_SUPPRESS_DEPRECATED_START
OpSet(const ngraph::OpSet& opset);
NGRAPH_SUPPRESS_DEPRECATED_END
OpSet() = default;
/// \brief Insert an op into the opset with a particular name and factory
void insert(const std::string& name, const NodeTypeInfo& type_info, FactoryRegistry<Node>::Factory factory) {
Expand All @@ -56,19 +54,20 @@ class NGRAPH_API OpSet : public ov::OpSet {
}
};

const NGRAPH_API OpSet& get_opset1();
const NGRAPH_API OpSet& get_opset2();
const NGRAPH_API OpSet& get_opset3();
const NGRAPH_API OpSet& get_opset4();
const NGRAPH_API OpSet& get_opset5();
const NGRAPH_API OpSet& get_opset6();
const NGRAPH_API OpSet& get_opset7();
const NGRAPH_API OpSet& get_opset8();
const NGRAPH_API OpSet& get_opset9();
const NGRAPH_API OpSet& get_opset10();
const NGRAPH_API OpSet& get_opset11();
const NGRAPH_API OpSet& get_opset12();
const NGRAPH_API OpSet& get_opset13();
const NGRAPH_API std::map<std::string, std::function<const ngraph::OpSet&()>>& get_available_opsets();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset1();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset2();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset3();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset4();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset5();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset6();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset7();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset8();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset9();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset10();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset11();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset12();
NGRAPH_API_DEPRECATED const NGRAPH_API OpSet& get_opset13();
NGRAPH_API_DEPRECATED const NGRAPH_API std::map<std::string, std::function<const ngraph::OpSet&()>>&
get_available_opsets();
} // namespace ngraph
NGRAPH_SUPPRESS_DEPRECATED_END
43 changes: 22 additions & 21 deletions src/core/include/openvino/op/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# define WAS_OV_LIBRARY_DEFINED_CONSTANT
#endif

#include "ngraph/runtime/aligned_buffer.hpp"
#include "ngraph/runtime/host_tensor.hpp"
#include "ngraph/runtime/shared_buffer.hpp"

Expand All @@ -21,11 +20,14 @@
# undef WAS_OV_LIBRARY_DEFINED_CONSTANT
#endif
#include "openvino/core/coordinate_diff.hpp"
#include "openvino/core/node.hpp"
#include "openvino/core/type/element_type.hpp"
#include "openvino/core/type/element_type_traits.hpp"
#include "openvino/op/op.hpp"

namespace ov {

class AlignedBuffer;

namespace op {
namespace v0 {
/// \brief Class for constants.
Expand Down Expand Up @@ -177,13 +179,20 @@ class OPENVINO_API Constant : public Op {
/// \param shape The shape of the tensor constant.
/// \param data A pointer to pre-allocated shared data.
template <typename T>
OPENVINO_DEPRECATED("This constructor is deprecated and will be removed in 2024.0 release")
Constant(const element::Type& type, const Shape& shape, std::shared_ptr<ngraph::runtime::SharedBuffer<T>> data)
: m_element_type(type),
m_shape(shape) {
m_data = data;
m_data = legacy_to_ov_aligned_buffer(data);
constructor_validate_and_infer_types();
}
OPENVINO_SUPPRESS_DEPRECATED_END
Constant(const element::Type& type, const Shape& shape, const std::shared_ptr<ov::AlignedBuffer>& data)
: m_element_type(type),
m_shape(shape) {
m_data = data;
constructor_validate_and_infer_types();
}

Constant(const Constant& other);
Constant(const Constant& other, const Shape& new_shape);
Expand Down Expand Up @@ -241,11 +250,7 @@ class OPENVINO_API Constant : public Op {
AxisSet get_axis_set_val() const;

/// \brief Return data size in bytes
size_t get_byte_size() const {
OPENVINO_SUPPRESS_DEPRECATED_START
return m_data->size();
OPENVINO_SUPPRESS_DEPRECATED_END
}
size_t get_byte_size() const;

/// \brief Wrapper around constructing a shared_ptr of a Constant
///
Expand Down Expand Up @@ -370,11 +375,8 @@ class OPENVINO_API Constant : public Op {
return rc;
}

const void* get_data_ptr() const {
OPENVINO_SUPPRESS_DEPRECATED_START
return (m_data ? m_data->get_ptr() : nullptr);
OPENVINO_SUPPRESS_DEPRECATED_END
}
const void* get_data_ptr() const;

template <typename T>
const T* get_data_ptr() const {
OPENVINO_ASSERT(sizeof(T) <= m_element_type.size() || shape_size(m_shape) <= 0, "Buffer over-read");
Expand Down Expand Up @@ -406,6 +408,11 @@ class OPENVINO_API Constant : public Op {
private:
Constant(bool memset_allocation, const element::Type& type, const Shape& shape);

OPENVINO_SUPPRESS_DEPRECATED_START
std::shared_ptr<ov::AlignedBuffer> legacy_to_ov_aligned_buffer(
const std::shared_ptr<ngraph::runtime::AlignedBuffer>& buffer);
OPENVINO_SUPPRESS_DEPRECATED_END

template <element::Type_t Type,
typename StorageDataType = fundamental_type_for<Type>,
typename std::enable_if<Type != element::Type_t::u1 && Type != element::Type_t::u4 &&
Expand Down Expand Up @@ -637,11 +644,7 @@ class OPENVINO_API Constant : public Op {

void allocate_buffer(bool memset_allocation);

void* get_data_ptr_nc() {
OPENVINO_SUPPRESS_DEPRECATED_START
return (m_data ? m_data->get_ptr() : nullptr);
OPENVINO_SUPPRESS_DEPRECATED_END
}
void* get_data_ptr_nc();

template <element::Type_t ET>
typename element_type_traits<ET>::value_type* get_data_ptr_nc() {
Expand Down Expand Up @@ -853,9 +856,7 @@ class OPENVINO_API Constant : public Op {

element::Type m_element_type;
Shape m_shape{};
OPENVINO_SUPPRESS_DEPRECATED_START
std::shared_ptr<ngraph::runtime::AlignedBuffer> m_data;
OPENVINO_SUPPRESS_DEPRECATED_END
std::shared_ptr<ov::AlignedBuffer> m_data;
mutable std::atomic_bool m_all_elements_bitwise_identical{false};
mutable std::atomic_bool m_all_elements_bitwise_identical_checked{false};
bool m_alloc_buffer_on_visit_attributes = true;
Expand Down
8 changes: 3 additions & 5 deletions src/core/reference/src/op/strided_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include <cmath>

#include "ngraph/runtime/aligned_buffer.hpp"
#include "openvino/reference/reshape.hpp"
#include "openvino/reference/reverse.hpp"
#include "openvino/reference/slice.hpp"
#include "openvino/runtime/aligned_buffer.hpp"

namespace ov {
namespace reference {
Expand All @@ -30,8 +30,7 @@ void strided_slice(const char* arg,
return;
}

OPENVINO_SUPPRESS_DEPRECATED_START
ngraph::runtime::AlignedBuffer slice_out_buffer(shape_size(sp.reshape_in_shape) * elem_type);
ov::AlignedBuffer slice_out_buffer(shape_size(sp.reshape_in_shape) * elem_type);
slice(reinterpret_cast<const char*>(arg),
slice_out_buffer.get_ptr<char>(),
arg_shape,
Expand All @@ -41,7 +40,7 @@ void strided_slice(const char* arg,
sp.reshape_in_shape,
elem_type);

ngraph::runtime::AlignedBuffer reshape_out_buffer(shape_size(sp.reshape_out_shape) * elem_type);
ov::AlignedBuffer reshape_out_buffer(shape_size(sp.reshape_out_shape) * elem_type);
reshape(slice_out_buffer.get_ptr<char>(), reshape_out_buffer.get_ptr<char>(), sp.reshape_in_shape, elem_type);

reverse(reshape_out_buffer.get_ptr<char>(),
Expand All @@ -50,7 +49,6 @@ void strided_slice(const char* arg,
sp.reshape_out_shape,
sp.reverse_axes,
elem_type);
OPENVINO_SUPPRESS_DEPRECATED_END
}
} // namespace reference
} // namespace ov
Loading

0 comments on commit 7ceff55

Please sign in to comment.