Forbid typed data access and roi tensor creation for little bit types (#8007)

This commit is contained in:
Anton Pankratv 2021-10-19 04:53:33 +03:00 committed by GitHub
parent 00be1d5a15
commit 3f8856862d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 38 deletions

View File

@ -151,12 +151,12 @@ void CommonReferenceTest::ValidateBlobs(const ov::runtime::Tensor& refBlob, cons
case ov::element::i4:
case ov::element::u4:
LayerTestsUtils::LayerTestsCommon::Compare<int8_t, int8_t>(
refBlob.data<const int8_t>(), outBlob.data<const int8_t>(),
static_cast<const int8_t*>(refBlob.data()), static_cast<const int8_t*>(outBlob.data()),
refBlob.get_size() / 2, threshold, abs_threshold);
break;
case ov::element::u1:
LayerTestsUtils::LayerTestsCommon::Compare<int8_t, int8_t>(
refBlob.data<const int8_t>(), outBlob.data<const int8_t>(),
static_cast<const int8_t*>(refBlob.data()), static_cast<const int8_t*>(outBlob.data()),
refBlob.get_size() / 8, threshold, abs_threshold);
break;
default:

View File

@ -65,6 +65,9 @@ Tensor::Tensor(const element::Type element_type, const Shape& shape, void* host_
}
Tensor::Tensor(const Tensor& owner, const Coordinate& begin, const Coordinate& end) : _so{owner._so} {
OPENVINO_ASSERT(owner.get_element_type().bitwidth() >= 8,
"ROI Tensor for types with bitwidths less then 8 bit is not implemented. Tensor type: ",
owner.get_element_type());
try {
_impl = owner._impl->createROI(begin, end);
} catch (const std::exception& ex) {
@ -87,7 +90,10 @@ Shape Tensor::get_shape() const {
}
Strides Tensor::get_strides() const {
OV_TENSOR_STATEMENT(return _impl->getTensorDesc().getBlockingDesc().getStrides(););
OPENVINO_ASSERT(get_element_type().bitwidth() >= 8,
"Could not get strides for types with bitwidths less then 8 bit. Tensor type: ",
get_element_type());
OV_TENSOR_STATEMENT(return _impl->getTensorDesc().getBlockingDesc().getStrides());
}
size_t Tensor::get_size() const {
@ -108,20 +114,18 @@ void* Tensor::data(const element::Type element_type) const {
#undef TYPE_CHECK
OPENVINO_ASSERT(host_accesable_implementation, "Tensor implementation type dose not contains host accessable data");
if (element_type != element::undefined) {
OPENVINO_ASSERT(
element::fundamental_type_for(element_type) == element::fundamental_type_for(get_element_type()),
get_element_type(),
" tensor fundamental element type is ",
element::fundamental_type_for(get_element_type()),
", but it casted to ",
element_type,
" with fundamental element type ",
element::fundamental_type_for(element_type));
OPENVINO_ASSERT(element_type == get_element_type(),
"Tensor data with element type ",
get_element_type(),
", is not representable as pointer to ",
element_type);
}
OV_TENSOR_STATEMENT({
return _impl->getTensorDesc().getBlockingDesc().getOffsetPadding() * get_element_type().size() +
InferenceEngine::as<InferenceEngine::MemoryBlob>(_impl)->rmap().as<uint8_t*>();
});
auto byte_offset = _impl->getTensorDesc().getBlockingDesc().getOffsetPadding() * get_element_type().size();
OPENVINO_ASSERT((get_element_type().bitwidth() >= 8) || (byte_offset == 0),
"ROI access for types with bitwidths less then 8 bit is not implemented. Tensor type: ",
get_element_type());
OV_TENSOR_STATEMENT(
{ return byte_offset + InferenceEngine::as<InferenceEngine::MemoryBlob>(_impl)->rmap().as<uint8_t*>(); });
}
bool Tensor::operator!() const noexcept {

View File

@ -11,6 +11,7 @@
#include <openvino/core/strides.hpp>
#include <openvino/core/type/element_type.hpp>
#include "ngraph/coordinate_transform.hpp"
#include "openvino/core/except.hpp"
#include "openvino/runtime/allocator.hpp"
#include "openvino/runtime/tensor.hpp"
@ -191,18 +192,12 @@ TEST_F(OVTensorTest, cannotSetShapeOnRoiTensor) {
ASSERT_THROW(roi_tensor.set_shape(newShape), ov::Exception);
}
TEST_F(OVTensorTest, makeRangeRoiTensorInt4) {
TEST_F(OVTensorTest, tensorInt4DataAccess) {
ov::runtime::Tensor t{ov::element::i4, {1, 6, 5, 3}}; // RGB picture of size (WxH) = 5x6
ov::runtime::Tensor roi_tensor{t, {0, 1, 2, 0}, {1, 5, 4, 3}};
ov::Shape ref_shape = {1, 4, 2, 3};
ptrdiff_t ref_offset = 21;
ov::Strides ref_strides = {90, 15, 3, 1};
ASSERT_EQ(roi_tensor.get_shape(), ref_shape);
ASSERT_EQ(roi_tensor.data<int8_t>() - t.data<int8_t>(), ref_offset);
ASSERT_EQ(roi_tensor.get_strides(), ref_strides);
ASSERT_EQ(roi_tensor.get_strides(), t.get_strides());
ASSERT_EQ(ref_strides, roi_tensor.get_strides());
ASSERT_EQ(roi_tensor.get_element_type(), t.get_element_type());
ASSERT_THROW((ov::runtime::Tensor{t, {0, 1, 2, 0}, {1, 5, 4, 3}}), ov::Exception);
ASSERT_THROW(t.get_strides(), ov::Exception);
ASSERT_THROW(t.data<int8_t>(), ov::Exception);
ASSERT_NO_THROW(t.data());
}
TEST_F(OVTensorTest, makeRangeRoiBlobWrongSize) {
@ -226,17 +221,11 @@ TEST_F(OVTensorTest, readRangeRoiBlob) {
auto roi = roi_tensor.data<int32_t>();
ASSERT_NE(nullptr, roi);
auto strides = roi_tensor.get_strides();
for (size_t n = 0; n < roi_tensor.get_shape()[0]; ++n) {
for (size_t c = 0; c < roi_tensor.get_shape()[1]; ++c) {
for (size_t h = 0; h < roi_tensor.get_shape()[2]; ++h) {
for (size_t w = 0; w < roi_tensor.get_shape()[3]; ++w) {
auto actual = roi[w * strides[3] + h * strides[2] + c * strides[1] + n * strides[0]];
auto expected = t.data<int32_t>()[(w + 4) * strides[3] + (h + 2) * strides[2] +
(c + 0) * strides[1] + (n + 0) * strides[0]];
ASSERT_EQ(expected, actual) << ov::Shape{n, c, h, w};
}
}
}
for (auto&& c : ngraph::CoordinateTransformBasic{roi_tensor.get_shape()}) {
auto actual = roi[c[3] * strides[3] + c[2] * strides[2] + c[1] * strides[1] + c[0] * strides[0]];
auto expected = t.data<int32_t>()[(c[3] + 4) * strides[3] + (c[2] + 2) * strides[2] +
(c[1] + 0) * strides[1] + (c[0] + 0) * strides[0]];
ASSERT_EQ(expected, actual) << c;
}
}
}