Skip to content

Commit

Permalink
Check propagation of inputs range
Browse files Browse the repository at this point in the history
  • Loading branch information
sbalandi committed Apr 2, 2024
1 parent 929c9a9 commit c739565
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ class DynamicTensorIteratorTest : public testing::WithParamInterface<DynamicTens
for (size_t port = 0; port < nodePtr->get_input_size(); ++port) {
if (itTargetShape != targetInputStaticShapes.end()) {
if (nodePtr->get_input_node_ptr(port)->shared_from_this() == inputNode->shared_from_this()) {
inputs.insert({param, it->second(nodePtr, port, param->get_element_type(), *itTargetShape)});
inputs.insert({param, it->second(nodePtr, port, param->get_element_type(), *itTargetShape, nullptr)});
break;
}
} else {
inputs.insert({param, it->second(nodePtr, port, param->get_element_type(), default_shape)});
inputs.insert({param, it->second(nodePtr, port, param->get_element_type(), default_shape, nullptr)});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ uint64_t clip(uint64_t n, uint64_t lower, uint64_t upper) {
return std::max(lower, std::min(n, upper));
}

std::pair<double, double> find_const_ranges(std::map<std::string, ov::conformance::InputInfo> input_info, const std::shared_ptr<const ov::Model>& function) {
double min = std::numeric_limits<double>::lowest();
double max = std::numeric_limits<double>::max();
for (const auto& ops : function->get_ordered_ops()) {
// for (const auto& param : function->get_parameters()) {
// auto in_info = input_info.find(param->get_friendly_name())->second;
// if (!in_info.is_const) {
// continue;
// }
// if (min < in_info.ranges.min) {
// min = in_info.ranges.min;
// }
// if (max > in_info.ranges.max) {
// max = in_info.ranges.max;
// }
}

std::cout << " RESULTS: MIN " << min << " MAX " << max << std::endl;
return std::pair<double, double>(min, max);
}

void ReadIRTest::SetUp() {
std::pair<std::string, std::string> model_pair;
targetDevice = ov::test::utils::target_device;
Expand All @@ -132,6 +153,7 @@ void ReadIRTest::SetUp() {
auto input_info = meta_info.get_input_info();
rel_influence_coef = meta_info.get_graph_priority();

std::pair<double, double> ranges = find_const_ranges(input_info, function);
auto inputMap = utils::getInputMap();
std::vector<std::shared_ptr<ov::op::v0::Parameter>> parameter_to_remove;
for (const auto& param : function->get_parameters()) {
Expand All @@ -143,7 +165,7 @@ void ReadIRTest::SetUp() {
// auto next_node = param->get_default_output().get_node_shared_ptr();
auto next_node = param->get_default_output().get_target_inputs().begin()->get_node()->shared_from_this();
auto it = inputMap.find(next_node->get_type_info());
auto tensor = it->second(next_node, function->get_parameter_index(param), param->get_element_type(), param->get_shape());
auto tensor = it->second(next_node, function->get_parameter_index(param), param->get_element_type(), param->get_shape(), nullptr);
auto const_node = std::make_shared<ov::op::v0::Constant>(tensor);
const_node->set_friendly_name(param->get_friendly_name());
ov::replace_node(param, const_node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "transformations/convert_precision.hpp"
#include "functional_test_utils/skip_tests_config.hpp"

#include "common_test_utils/ov_tensor_utils.hpp"

namespace ov {
namespace test {

Expand Down Expand Up @@ -64,6 +66,9 @@ class SubgraphBaseTest : public ov::test::TestsCommon {
std::vector<std::vector<ov::Shape>> targetStaticShapes;
ElementType inType = ov::element::undefined, outType = ov::element::undefined;

std::vector<std::shared_ptr<ov::test::utils::InputGenerateData>> inputDataMap;
bool collect_ranges();

ov::CompiledModel compiledModel;
ov::InferRequest inferRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ using InputsMap = std::map<ov::NodeTypeInfo, std::function<ov::Tensor(
const std::shared_ptr<ov::Node>& node,
size_t port,
const ov::element::Type& elemType,
const ov::Shape& targetShape)>>;
const ov::Shape& targetShape,
std::shared_ptr<InputGenerateData> inGenRangeData)>>;

InputsMap getInputMap();

Expand Down
185 changes: 179 additions & 6 deletions src/tests/functional/shared_test_classes/src/base/ov_subgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ std::ostream& operator <<(std::ostream& os, const InputShape& inputShape) {

void SubgraphBaseTest::run() {
is_reported = true;
bool isCurrentTestDisabled = ov::test::utils::current_test_is_disabled();
// bool isCurrentTestDisabled = ov::test::utils::current_test_is_disabled();
bool isCurrentTestDisabled = false;

ov::test::utils::PassRate::Statuses status = isCurrentTestDisabled ?
ov::test::utils::PassRate::Statuses::SKIPPED :
ov::test::utils::PassRate::Statuses::CRASHED;
summary.setDeviceName(targetDevice);
summary.updateOPsStats(function, status, rel_influence_coef);

if (isCurrentTestDisabled)
GTEST_SKIP() << "Disabled test due to configuration" << std::endl;
// if (isCurrentTestDisabled)
// GTEST_SKIP() << "Disabled test due to configuration" << std::endl;

// in case of crash jump will be made and work will be continued
auto crashHandler = std::unique_ptr<ov::test::utils::CrashHandler>(new ov::test::utils::CrashHandler());
Expand Down Expand Up @@ -199,8 +200,8 @@ void SubgraphBaseTest::import_export() {
summary.setDeviceName(targetDevice);
summary.updateOPsStats(function, status, rel_influence_coef);

if (isCurrentTestDisabled)
GTEST_SKIP() << "Disabled test due to configuration" << std::endl;
// if (isCurrentTestDisabled)
// GTEST_SKIP() << "Disabled test due to configuration" << std::endl;

// in case of crash jump will be made and work will be continued
auto crashHandler = std::unique_ptr<ov::test::utils::CrashHandler>(new ov::test::utils::CrashHandler());
Expand Down Expand Up @@ -317,8 +318,175 @@ void SubgraphBaseTest::compile_model() {
}
}

std::vector<double> get_low_max(ov::element::Type temp_type) {
std::vector<double> low_max {std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max()};
#define CASE(X) \
case X: \
low_max[0] = std::numeric_limits<element_type_traits<X>::value_type>::lowest(); \
low_max[1] = std::numeric_limits<element_type_traits<X>::value_type>::max(); \
break; \

switch (temp_type) {
CASE(ov::element::Type_t::boolean)
CASE(ov::element::Type_t::bf16)
CASE(ov::element::Type_t::f16)
CASE(ov::element::Type_t::f32)
CASE(ov::element::Type_t::f64)
CASE(ov::element::Type_t::i4)
CASE(ov::element::Type_t::i8)
CASE(ov::element::Type_t::i16)
CASE(ov::element::Type_t::i32)
CASE(ov::element::Type_t::i64)
CASE(ov::element::Type_t::u1)
CASE(ov::element::Type_t::u4)
CASE(ov::element::Type_t::u8)
CASE(ov::element::Type_t::u16)
CASE(ov::element::Type_t::u32)
CASE(ov::element::Type_t::u64)
break;
}

return low_max;
}

bool SubgraphBaseTest::collect_ranges() {
inputDataMap = {std::make_shared<ov::test::utils::InputGenerateData>(ov::test::utils::InputGenerateData(0, 1u << 31)),
std::make_shared<ov::test::utils::InputGenerateData>(ov::test::utils::InputGenerateData(0, 1u << 31))};
bool success = true;

for (const auto& node : function->get_ordered_ops()) {
if (ov::op::util::is_output(node) ||
ov::op::util::is_constant(node) ||
ov::op::util::is_parameter(node)) {
continue;
}

auto range = std::vector<ov::test::utils::InputGenerateData>{};
auto it = ov::test::utils::inputRanges.find(node->get_type_info());
if (it != ov::test::utils::inputRanges.end()) {
auto ranges = it->second;
if (ranges.size() != 2) {
throw std::runtime_error("Incorrect size of ranges. It should be 2 (real and int cases)");
}
range = ranges.at(node->get_element_type().is_real());
}

const size_t inNodeCnt = node->get_input_size();
for (size_t port = 0; port < inNodeCnt; ++port) {
size_t range_id = node->input(port).get_element_type().is_real();
ov::test::utils::InputGenerateData temp_range;

if (range.empty()) {
auto limits = get_low_max(node->input(port).get_element_type());
if (inputDataMap[range_id]->range * inputDataMap[range_id]->resolution > limits[1]) {
temp_range = ov::test::utils::InputGenerateData(0, static_cast<uint32_t>(limits[1]), 1);
} else {
temp_range = ov::test::utils::InputGenerateData(0, 1u << 31, 1);
}
} else {
temp_range = range.size() < inNodeCnt ? range.front() : range.at(port);
}

double node_max = temp_range.start_from + temp_range.range * temp_range.resolution;
double current_max = inputDataMap[range_id]->start_from + inputDataMap[range_id]->range * inputDataMap[range_id]->resolution;
if (inputDataMap[range_id]->start_from == temp_range.start_from) {
// nothing to do - -----start_curr/new+++++++++++++++range*res curr/new-----------------------
// nothing to do - -----start_curr/new+++++++++++++++range*res curr----------range*res new----
// reduce range - -----start_curr/new+++++++++++++++range*res new-----------range*res curr---
if (current_max > node_max) {
inputDataMap[range_id]->range = temp_range.range;
inputDataMap[range_id]->resolution = temp_range.resolution;
}
} else if (inputDataMap[range_id]->start_from > temp_range.start_from) {
// nothing to do - -----start_new-----start_curr++++++++++range*res curr/new-------------------
// nothing to do - -----start_new-----start_curr++++++++++range*res curr------range*res new----
// recalculate range - -----start_new-----start_curr++++++++++range*res new-------range*res curr---

// could not find range - -----start_new---range*res new-----start_curr-----range*res curr---
if (node_max < inputDataMap[range_id]->start_from) {
success = false;
std::cout << " FAIL FIND RANGE: node_max < inputDataMap[range_id]->start_from " <<
" NODE: " << node->get_friendly_name() <<
" CURRENT start_from: " << std::to_string(inputDataMap[range_id]->start_from) <<
" NODE start_from: " << std::to_string(temp_range.start_from) <<
" CURRENT max: " << std::to_string(current_max) <<
" NODE max: " << std::to_string(node_max) << std::endl;
break;
}
if (current_max > node_max) {
auto new_resolution = temp_range.resolution < inputDataMap[range_id]->resolution ? temp_range.resolution :
inputDataMap[range_id]->resolution;
auto new_range = (node_max - inputDataMap[range_id]->start_from) / new_resolution;
inputDataMap[range_id]->range = (uint32_t)new_range;
inputDataMap[range_id]->resolution = new_resolution;
}
} else if (inputDataMap[range_id]->start_from < temp_range.start_from) {
// reset to new - -----start_curr-----start_new++++++++++range*res curr/new-------------------
// reset to new - -----start_curr-----start_new++++++++++range*res new-------range*res curr---
// recalculate range - -----start_curr-----start_new++++++++++range*res curr------range*res new----

// could not find range - -----start_curr---range*res curr-----start_new-----range*res new---
if (current_max < temp_range.start_from) {
success = false;
std::cout << " FAIL FIND RANGE: current_max < temp_range.start_from " <<
" NODE: " << node->get_friendly_name() <<
" CURRENT start_from: " << std::to_string(inputDataMap[range_id]->start_from) <<
" NODE start_from: " << std::to_string(temp_range.start_from) <<
" CURRENT max: " << std::to_string(current_max) <<
" NODE max: " << std::to_string(node_max) << std::endl;
break;
}
if (current_max >= node_max) {
inputDataMap[range_id]->start_from = temp_range.start_from;
inputDataMap[range_id]->range = temp_range.range;
inputDataMap[range_id]->resolution = temp_range.resolution;
} else {
auto new_resolution = temp_range.resolution < inputDataMap[range_id]->resolution ? temp_range.resolution :
inputDataMap[range_id]->resolution;
auto new_range = (current_max - temp_range.start_from) / new_resolution;
inputDataMap[range_id]->range = (uint32_t)round(new_range);
inputDataMap[range_id]->resolution = new_resolution;
inputDataMap[range_id]->start_from = temp_range.start_from;
}
}
}

if (!success) {
break;
}
}
return success;
}

void SubgraphBaseTest::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
inputs.clear();
bool find_range = collect_ranges();
if (find_range) {
// auto tmp_start = 0 - static_cast<double>(std::numeric_limits<uint32_t>::max() / 2);
auto tmp_max = 1u << 31;
std::cout << " RANGE FOUND \n";
if (inputDataMap[0]->start_from == 0 &&
inputDataMap[0]->range == tmp_max) {
std::cout << " RANGE INT: JUST uint32_t" << std::endl;
} else {
std::cout << " RANGE INT: " <<
" START FROM: " << std::to_string(inputDataMap[0]->start_from) <<
" RANGE: " << std::to_string(inputDataMap[0]->range) <<
" RESOLUTION: " << std::to_string(inputDataMap[0]->resolution) << std::endl;
}
if (inputDataMap[1]->start_from == 0 &&
inputDataMap[1]->range == tmp_max) {
std::cout << " RANGE REAL: JUST uint32_t" << std::endl;
} else {
std::cout << " RANGE REAL: " <<
" START FROM: " << std::to_string(inputDataMap[1]->start_from) <<
" RANGE: " << std::to_string(inputDataMap[1]->range) <<
" RESOLUTION: " << std::to_string(inputDataMap[1]->resolution) << std::endl;
}
} else {
std::cout << " RANGE NOT FOUND \n";
}

auto inputMap = utils::getInputMap();
auto itTargetShape = targetInputStaticShapes.begin();
for (const auto &param : function->get_parameters()) {
Expand All @@ -330,7 +498,12 @@ void SubgraphBaseTest::generate_inputs(const std::vector<ov::Shape>& targetInput
ASSERT_NE(it, inputMap.end());
for (size_t port = 0; port < nodePtr->get_input_size(); ++port) {
if (nodePtr->get_input_node_ptr(port)->shared_from_this() == inputNode->shared_from_this()) {
inputs.insert({param, it->second(nodePtr, port, param->get_element_type(), *itTargetShape)});
if (find_range) {
inputs.insert({param, it->second(nodePtr, port, param->get_element_type(),
*itTargetShape, inputDataMap[node.get_element_type().is_real()])});
} else {
inputs.insert({param, it->second(nodePtr, port, param->get_element_type(), *itTargetShape, nullptr)});
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,8 +1138,13 @@ template<typename T>
ov::Tensor generateInput(const std::shared_ptr<ov::Node>& node,
size_t port,
const ov::element::Type& elemType,
const ov::Shape& targetShape) {
return generate(ov::as_type_ptr<T>(node), port, elemType, targetShape);
const ov::Shape& targetShape,
std::shared_ptr<InputGenerateData> inGenRangeData) {
if (inGenRangeData) {
return ov::test::utils::create_and_fill_tensor(elemType, targetShape, *inGenRangeData.get());
} else {
return generate(ov::as_type_ptr<T>(node), port, elemType, targetShape);
}
}
} // namespace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ void inline fill_data_random(T* pointer,
}
for (std::size_t i = 0; i < size; i++) {
pointer[i] = static_cast<T>(start_from + static_cast<T>(random.Generate(k_range)) / k);
auto l = pointer[i];
if (l > std::numeric_limits<T>::max()) {
std::cout << " OUT OF RANGE, NODE: " << std::to_string(l) << std::endl;
}
}
}

Expand Down
Loading

0 comments on commit c739565

Please sign in to comment.