From a0940bbbf7a4bad9e9e337a0c17b30708ce1f064 Mon Sep 17 00:00:00 2001 From: Anastasia Kuporosova Date: Wed, 13 Nov 2024 14:22:06 +0100 Subject: [PATCH 1/2] [PyOV] Restrict changing data in const (#27431) ### Details: - Const op meant to be non-changeable ### Tickets: - CVS-124319 --- .../python/src/pyopenvino/core/common.cpp | 8 +- .../python/tests/test_graph/test_constant.py | 120 +----------------- 2 files changed, 12 insertions(+), 116 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/core/common.cpp b/src/bindings/python/src/pyopenvino/core/common.cpp index a202c3a3801001..10ae0ed0ea6042 100644 --- a/src/bindings/python/src/pyopenvino/core/common.cpp +++ b/src/bindings/python/src/pyopenvino/core/common.cpp @@ -358,10 +358,14 @@ py::array array_from_constant_copy(ov::op::v0::Constant&& c, py::dtype& dst_dtyp py::array array_from_constant_view(ov::op::v0::Constant&& c) { const auto& ov_type = c.get_element_type(); const auto dtype = Common::type_helpers::get_dtype(ov_type); + py::array data; if (ov_type.bitwidth() < Common::values::min_bitwidth) { - return py::array(dtype, c.get_byte_size(), c.get_data_ptr(), py::cast(c)); + data = py::array(dtype, c.get_byte_size(), c.get_data_ptr(), py::cast(c)); + } else { + data = py::array(dtype, c.get_shape(), constant_helpers::_get_strides(c), c.get_data_ptr(), py::cast(c)); } - return py::array(dtype, c.get_shape(), constant_helpers::_get_strides(c), c.get_data_ptr(), py::cast(c)); + data.attr("flags").attr("writeable") = false; + return data; } }; // namespace array_helpers diff --git a/src/bindings/python/tests/test_graph/test_constant.py b/src/bindings/python/tests/test_graph/test_constant.py index 131654855b380a..7b349ad7cd94b1 100644 --- a/src/bindings/python/tests/test_graph/test_constant.py +++ b/src/bindings/python/tests/test_graph/test_constant.py @@ -205,53 +205,12 @@ def test_init_with_scalar(init_value, src_dtype, dst_dtype, shared_flag, data_ge assert np.allclose(const_data, expected_result) -@pytest.mark.parametrize( - ("src_dtype"), - [ - (np.float16), - (np.uint16), - ], -) -@pytest.mark.parametrize( - ("shared_flag"), - [ - (True), - (False), - ], -) -@pytest.mark.parametrize( - ("data_getter"), - [ - (DataGetter.COPY), - (DataGetter.VIEW), - ], -) -def test_init_bf16_populate(src_dtype, shared_flag, data_getter): - data = np.random.rand(1, 2, 16, 8) + 0.5 - data = data.astype(src_dtype) - - # To create bf16 constant, allocate memory and populate it: - init_data = np.zeros(shape=data.shape, dtype=src_dtype) - ov_const = ops.constant(init_data, dtype=Type.bf16, shared_memory=shared_flag) - ov_const.data[:] = data - - # Check shape and element type of Constant class - assert isinstance(ov_const, Constant) - assert np.all(list(ov_const.shape) == [1, 2, 16, 8]) - assert ov_const.get_element_type() == Type.bf16 - - _dst_dtype = Type.bf16.to_dtype() - - assert ov_const.get_element_type().to_dtype() == _dst_dtype - # Compare values to Constant - if data_getter == DataGetter.COPY: - const_data = ov_const.get_data() - elif data_getter == DataGetter.VIEW: - const_data = ov_const.data - else: - raise AttributeError("Unknown DataGetter passed!") - assert const_data.dtype == _dst_dtype - assert np.allclose(const_data, data) +def test_cant_change_data_in_const(): + arr_0 = np.ones([1, 3, 32, 32]) + ov_const = ops.constant(arr_0) + arr_1 = np.ones([1, 3, 32, 32]) + 1 + with pytest.raises(ValueError, match="assignment destination is read-only"): + ov_const.data[:] = arr_1 @pytest.mark.parametrize( @@ -286,58 +245,6 @@ def test_init_bf16_direct(ov_type, numpy_dtype, shared_flag): assert np.allclose(data, result, rtol=0.01) -@pytest.mark.parametrize( - "shape", - [ - ([1, 3, 28, 28]), - ([1, 3, 27, 27]), - ], -) -@pytest.mark.parametrize( - ("low", "high", "ov_type", "src_dtype"), - [ - (0, 2, Type.u1, np.uint8), - (0, 16, Type.u4, np.uint8), - (-8, 7, Type.i4, np.int8), - (0, 16, Type.nf4, np.uint8), - ], -) -@pytest.mark.parametrize( - ("shared_flag"), - [ - (True), - (False), - ], -) -@pytest.mark.parametrize( - ("data_getter"), - [ - (DataGetter.COPY), - (DataGetter.VIEW), - ], -) -def test_constant_helper_packing(shape, low, high, ov_type, src_dtype, shared_flag, data_getter): - data = np.random.uniform(low, high, shape).astype(src_dtype) - - # Allocate memory first: - ov_const = ops.constant(np.zeros(shape=data.shape, dtype=src_dtype), - dtype=ov_type, - shared_memory=shared_flag) - # Fill data with packed values - packed_data = pack_data(data, ov_const.get_element_type()) - ov_const.data[:] = packed_data - - # Always unpack the data! - if data_getter == DataGetter.COPY: - unpacked = unpack_data(ov_const.get_data(), ov_const.get_element_type(), ov_const.shape) - elif data_getter == DataGetter.VIEW: - unpacked = unpack_data(ov_const.data, ov_const.get_element_type(), ov_const.shape) - else: - raise AttributeError("Unknown DataGetter passed!") - - assert np.array_equal(unpacked, data) - - @pytest.mark.parametrize( ("ov_type", "src_dtype"), [ @@ -380,21 +287,6 @@ def test_constant_direct_packing(ov_type, src_dtype, shared_flag, data_getter): assert not np.shares_memory(unpacked, data) -@pytest.mark.parametrize( - ("shared_flag"), - [ - (True), - (False), - ], -) -def test_write_to_buffer(shared_flag): - arr_0 = np.ones([1, 3, 32, 32]) - ov_const = ops.constant(arr_0, shared_memory=shared_flag) - arr_1 = np.ones([1, 3, 32, 32]) + 1 - ov_const.data[:] = arr_1 - assert np.array_equal(ov_const.data, arr_1) - - @pytest.mark.parametrize( ("shared_flag"), [ From eb38e67be6e6992445e7f4f1ea8014422d015512 Mon Sep 17 00:00:00 2001 From: Andrzej Kopytko Date: Wed, 13 Nov 2024 15:02:22 +0100 Subject: [PATCH 2/2] [DOCS] Preselection and sorting (#27538) ### Details: - *item1* - *...* ### Tickets: - *ticket-id* --- docs/sphinx_setup/_static/html/modal.html | 3 - docs/sphinx_setup/_static/html/modalLLM.html | 3 - docs/sphinx_setup/_static/js/graphs.js | 76 +++++--------------- 3 files changed, 17 insertions(+), 65 deletions(-) diff --git a/docs/sphinx_setup/_static/html/modal.html b/docs/sphinx_setup/_static/html/modal.html index ac425599b821ce..38eb673824f97e 100644 --- a/docs/sphinx_setup/_static/html/modal.html +++ b/docs/sphinx_setup/_static/html/modal.html @@ -11,9 +11,6 @@

Configure Graphs

-
- Clear All -
diff --git a/docs/sphinx_setup/_static/html/modalLLM.html b/docs/sphinx_setup/_static/html/modalLLM.html index e3395a16931188..37b569d0bd4078 100644 --- a/docs/sphinx_setup/_static/html/modalLLM.html +++ b/docs/sphinx_setup/_static/html/modalLLM.html @@ -11,9 +11,6 @@

Configure Graphs

-
- Clear All -
diff --git a/docs/sphinx_setup/_static/js/graphs.js b/docs/sphinx_setup/_static/js/graphs.js index 168c1c348e7a08..7171aed374dd99 100644 --- a/docs/sphinx_setup/_static/js/graphs.js +++ b/docs/sphinx_setup/_static/js/graphs.js @@ -9,7 +9,6 @@ class Filter { .forEach(item => optionMap.set(item.Platform, item)); return Array.from(optionMap.values()); } - // param: GraphData[], ieType static ByIeTypes(graphDataArr, ieTypes) { const optionMap = new Map(); @@ -18,7 +17,6 @@ class Filter { .forEach(item => optionMap.set(item.Platform, item)); return Array.from(optionMap.values()); } - // param: GraphData[], ieType, networkModels static ByTypesAndModels(graphDataArr, ieTypes, models) { return Array.from( @@ -26,9 +24,8 @@ class Filter { .filter(({ PlatformType, Model }) => ieTypes.includes(PlatformType) && models.includes(Model)) .reduce((map, item) => map.set(item.Platform, item), new Map()) .values() - ).sort((a, b) => a.Platform.localeCompare(b.Platform)); + ); } - // param: GraphData[], clientPlatforms static ByIeKpis(graphDataArr, clientPlatforms) { return Array.from( @@ -40,7 +37,6 @@ class Filter { }, new Set()) ); } - // param: GraphData[] static getParameters(graphDataArr) { var parameters = [] @@ -51,7 +47,6 @@ class Filter { }) return parameters; } - // param: GraphData[] static getIeTypes(graphDataArr) { var kpis = [] @@ -62,21 +57,12 @@ class Filter { }) return kpis; } - // param: GraphData[], clientPlatforms[] static ByClientPlatforms(graphDataArr, platformsArr) { return graphDataArr.filter((data) => { return platformsArr.includes(data.Platform) }); } - - // param: GraphData[], coreTypes[] - static FilterByCoreTypes(graphDataArr, coreTypes) { - if (coreTypes) { - return graphDataArr.filter((data) => coreTypes.includes(data.PlatformType)); - } - return graphDataArr; - } } class Modal { @@ -114,15 +100,13 @@ class Graph { .sort((a, b) => a.localeCompare(b)); } static getIeTypes(graphDataArr) { - return Array.from(new Set(graphDataArr.map((obj) => obj.PlatformType))); - } - static getCoreTypes(graphDataArr) { - return Array.from(new Set(graphDataArr.map((obj) => obj.ieType))); + return Array.from(new Set(graphDataArr.map((obj) => obj.PlatformType))).sort((a, b) => a.localeCompare(b)); } // param: GraphData[] static getPlatformNames(graphDataArr) { - return graphDataArr.map((data) => data.Platform); + return graphDataArr.map((data) => data.Platform) + .sort((a, b) => a.localeCompare(b)); } // param: GraphData[], engine: string, precisions: list @@ -297,13 +281,13 @@ $(document).ready(function () { const models = networkModels.map((networkModel) => createCheckMark(networkModel, 'networkmodel')); modal.find('.models-column').append(models); - const selectAllModelsButton = createCheckMark('', 'networkmodel'); + const selectAllModelsButton = createCheckMark('', 'networkmodel', false , false); modal.find('.models-selectall').append(selectAllModelsButton); - const selectAllPlatformsButton = createCheckMark('', 'platform'); + const selectAllPlatformsButton = createCheckMark('', 'platform', false , false); modal.find('.platforms-selectall').append(selectAllPlatformsButton); - const precisions = Modal.getPrecisionsLabels(graph).map((precision) => createCheckMark(precision, 'precision', false)); + const precisions = Modal.getPrecisionsLabels(graph).map((precision) => createCheckMark(precision, 'precision', false , false)); modal.find('.precisions-column').append(precisions); selectAllCheckboxes(precisions); @@ -318,21 +302,17 @@ $(document).ready(function () { modal.find('#modal-display-graphs').hide(); modal.find('.ietype-column input').first().prop('checked', true); - const kpiLabels = Filter.getParameters(graph).map((parameter) => createCheckMark(parameter, 'kpi', false)); + const kpiLabels = Filter.getParameters(graph).map((parameter) => createCheckMark(parameter, 'kpi', false , true)); modal.find('.kpi-column').append(kpiLabels); $('body').prepend(modal); - preselectDefaultSettings(graph, modal, appConfig); - - //is not generic solution :( if (appConfig.DefaultSelections.platformTypes?.data?.includes('Select All')) { selectAllCheckboxes(iefilter); - }; + preselectDefaultSettings(graph, modal, appConfig); renderClientPlatforms(graph, modal); - $('.clear-all-btn').on('click', clearAll); $('#build-graphs-btn').on('click', () => { $('#modal-configure-graphs').hide(); clickBuildGraphs(graph, appConfig, getSelectedNetworkModels(), getSelectedIeTypes(), getSelectedClientPlatforms(), getSelectedKpis(), Modal.getPrecisions(appConfig, getSelectedPrecisions()), isLLM); @@ -409,19 +389,9 @@ $(document).ready(function () { precisions.prop('disabled', false); } - function clearAll() { - $('.modal-content-grid-container input:checkbox').each((index, object) => $(object).prop('checked', false)); - validatePrecisionSelection(); - validateSelections(); - } - function preselectDefaultSettings(graph, modal, appConfig) { - - const defaultSelections = appConfig.DefaultSelections; - selectDefaultPlatformType(defaultSelections.platformTypes, graph, modal); - applyPlatformFilters(defaultSelections.platformFilters, modal, graph); - clearAllSettings(defaultSelections); - + selectDefaultPlatformType(appConfig.DefaultSelections.platformTypes, graph, modal); + clearAllSettings(appConfig.DefaultSelections); validateSelections(); validatePrecisionSelection(); } @@ -431,17 +401,8 @@ $(document).ready(function () { $(`input[data-ietype="${type}"]`).prop('checked', true); renderClientPlatforms(graph, modal); } - function applyPlatformFilters(platformFilters, modal, graph) { - if (!platformFilters) return; - const filters = modal.find('.selectable-box-container').children('.selectable-box'); - filters.removeClass('selected'); - platformFilters.data.forEach(selection => { - filters.filter(`[data-${platformFilters.name}="${selection}"]`).addClass('selected'); - }); - renderClientPlatforms(graph, modal); - } + function clearAllSettings(defaultSelections) { - clearAll(); Object.keys(defaultSelections).forEach(setting => { const { name, data } = defaultSelections[setting]; data.forEach(selection => { @@ -463,7 +424,7 @@ $(document).ready(function () { var platformNames = Graph.getPlatformNames(fPlatforms); $('.platforms-column .checkmark-container').remove(); - const clientPlatforms = platformNames.map((platform) => createCheckMark(platform, 'platform', true)); + const clientPlatforms = platformNames.map((platform) => createCheckMark(platform, 'platform', true, false)); var enabledPlatforms = filterPlatforms(graph, getSelectedIeTypes(), getSelectedNetworkModels()); enableCheckBoxes(clientPlatforms, enabledPlatforms); @@ -471,6 +432,7 @@ $(document).ready(function () { enableParmeters(graph, getSelectedClientPlatforms()); modal.find('.platforms-column input').on('click', validateSelections); + validateSelections(); } function enableParmeters(graph, clientPlatforms) { @@ -486,11 +448,12 @@ $(document).ready(function () { }) } - function createCheckMark(itemLabel, modelLabel, disabled) { + function createCheckMark(itemLabel, modelLabel, disabled, checked = false) { const item = $('