Don't memset Constant's buffer if it's about to be filled with data (#10861)

* Don't memset Constant's buffer if it's about to be filled with data

* dont memset buffer in visit_attributes
This commit is contained in:
Mateusz Tabaka
2022-03-14 17:36:40 +01:00
committed by GitHub
parent 3aa525c003
commit 23eaa80325
2 changed files with 18 additions and 11 deletions

View File

@@ -37,7 +37,8 @@ public:
/// \param values A vector of literals for initializing the tensor constant. The
/// size of values must match the size of the shape.
template <typename T>
Constant(const element::Type& type, const Shape& shape, const std::vector<T>& values) : Constant(type, shape) {
Constant(const element::Type& type, const Shape& shape, const std::vector<T>& values)
: Constant(false, type, shape) {
NODE_VALIDATION_CHECK(this,
values.size() == 1 || values.size() == shape_size(m_shape),
"Did not get the expected number of literals for a constant of shape ",
@@ -66,7 +67,7 @@ public:
/// \param value A scalar for initializing the uniform tensor constant. The
/// value is broadcast to the specified shape.
template <class T, class = typename std::enable_if<std::is_fundamental<T>::value>::type>
Constant(const element::Type& type, const Shape& shape, T value) : Constant(type, shape) {
Constant(const element::Type& type, const Shape& shape, T value) : Constant(false, type, shape) {
fill_data(type, value);
m_all_elements_bitwise_identical = true;
}
@@ -384,6 +385,8 @@ public:
}
private:
Constant(bool memset_allocation, const element::Type& type, const Shape& shape);
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 &&
@@ -525,7 +528,7 @@ private:
std::fill_n(get_data_ptr_nc<Type>(), mem_size(), v);
}
void allocate_buffer();
void allocate_buffer(bool memset_allocation);
void* get_data_ptr_nc() {
return (m_data ? m_data->get_ptr() : nullptr);

View File

@@ -46,7 +46,7 @@ ov::op::v0::Constant::Constant(const shared_ptr<ngraph::runtime::Tensor>& tensor
tensor);
} else {
constructor_validate_and_infer_types();
allocate_buffer();
allocate_buffer(false);
tensor->read(get_data_ptr_nc(), tensor->get_size_in_bytes());
}
m_all_elements_bitwise_identical = are_all_data_elements_bitwise_identical();
@@ -56,7 +56,7 @@ ov::op::v0::Constant::Constant(const shared_ptr<ngraph::runtime::Tensor>& tensor
ov::op::v0::Constant::Constant(const element::Type& type,
const ov::Shape& shape,
const std::vector<std::string>& values)
: Constant(type, shape) {
: Constant(false, type, shape) {
NGRAPH_SUPPRESS_DEPRECATED_START
NODE_VALIDATION_CHECK(this,
values.size() == shape_size(m_shape) || values.size() == 1,
@@ -187,20 +187,24 @@ ov::op::v0::Constant::Constant(const element::Type& type,
NGRAPH_SUPPRESS_DEPRECATED_END
}
ov::op::v0::Constant::Constant(const element::Type& type, const ov::Shape& shape)
ov::op::v0::Constant::Constant(const element::Type& type, const ov::Shape& shape) : Constant(true, type, shape) {}
ov::op::v0::Constant::Constant(bool memset_allocation, const element::Type& type, const ov::Shape& shape)
: m_element_type(type),
m_shape(shape) {
allocate_buffer();
allocate_buffer(memset_allocation);
constructor_validate_and_infer_types();
}
void ov::op::v0::Constant::allocate_buffer() {
void ov::op::v0::Constant::allocate_buffer(bool memset_allocation) {
m_data = make_shared<ngraph::runtime::AlignedBuffer>(mem_size(), host_alignment());
std::memset(m_data->get_ptr(), 0, m_data->size());
if (memset_allocation) {
std::memset(m_data->get_ptr(), 0, m_data->size());
}
}
ov::op::v0::Constant::Constant(const element::Type& type, const ov::Shape& shape, const void* data)
: Constant(type, shape) {
: Constant(false, type, shape) {
size_t size = ceil(shape_size(m_shape) * m_element_type.bitwidth() / 8.f);
std::memcpy(get_data_ptr_nc(), data, size);
m_all_elements_bitwise_identical = are_all_data_elements_bitwise_identical();
@@ -537,7 +541,7 @@ bool ov::op::v0::Constant::visit_attributes(AttributeVisitor& visitor) {
bool need_to_reallocate = (m_shape != prev_shape || prev_type != m_element_type);
if (m_alloc_buffer_on_visit_attributes && need_to_reallocate) {
// Filling in a fresh constant
allocate_buffer();
allocate_buffer(false);
}
visitor.on_attribute("value", m_data);
m_all_elements_bitwise_identical = are_all_data_elements_bitwise_identical();