Add clamp() step to PrePostProcessor Python API#34511
Add clamp() step to PrePostProcessor Python API#34511lawrencedsz9 wants to merge 8 commits intoopenvinotoolkit:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR exposes the existing C++ ov::preprocess::PreProcessSteps::clamp() and ov::preprocess::PostProcessSteps::clamp() APIs to Python users by adding pybind11 bindings for both, along with Python unit tests. The underlying C++ implementation was already present; this PR makes it accessible via the Python PrePostProcessor API.
Changes:
- Added
clamp(min_value, max_value)binding toPreProcessStepsPython API with docstring - Added
clamp(min_value, max_value)binding toPostProcessStepsPython API with docstring - Added three Python unit tests: preprocessing clamp, chained mean/scale + clamp, and postprocessing clamp
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp |
Adds pybind11 .def("clamp", ...) for both PreProcessSteps and PostProcessSteps, delegating to the already-implemented C++ API |
src/bindings/python/tests/test_graph/test_preprocess.py |
Adds three tests verifying the Clamp node appears in the built model for preprocessing, chained preprocessing, and postprocessing scenarios |
Issues found:
-
[HIGH] Missing
.pyistub update (src/bindings/python/src/openvino/_pyopenvino/preprocess.pyi): Theclampmethod is not added to the type stub for eitherPreProcessStepsorPostProcessSteps. This file is manually maintained and shipped with the wheel — itspadmethod was added when that binding was introduced. Without this update, users relying on type checkers (mypy, pyright) or IDE autocompletion will see "undefined attribute" errors for the new API. -
[LOW] Inconsistent test function name: The new postprocessing test is named
test_graph_postprocess_clamp(line 839), but the established convention in the file prefixes all tests — including postprocessing ones — withtest_graph_preprocess_*(e.g.,test_graph_preprocess_output_postprocess,test_graph_preprocess_postprocess_layout). It should be renamed totest_graph_preprocess_postprocess_clamp.
| steps.def( | ||
| "clamp", | ||
| [](ov::preprocess::PreProcessSteps& self, double min_value, double max_value) { | ||
| return &self.clamp(min_value, max_value); | ||
| }, | ||
| py::arg("min_value"), | ||
| py::arg("max_value"), | ||
| R"( | ||
| Clamps each element of input tensor to the range [min_value, max_value]. | ||
| Values below min_value are set to min_value, values above max_value are set to max_value. | ||
| :param min_value: Minimum value to clamp to. | ||
| :type min_value: float | ||
| :param max_value: Maximum value to clamp to. | ||
| :type max_value: float | ||
| :return: Reference to itself to allow chaining of calls in client's code in a builder-like manner. | ||
| :rtype: openvino.preprocess.PreProcessSteps | ||
| )"); |
There was a problem hiding this comment.
[HIGH] The clamp method is missing from the Python type stub file src/bindings/python/src/openvino/_pyopenvino/preprocess.pyi for both PreProcessSteps (around line 294) and PostProcessSteps (around line 233). This file is shipped with the wheel and is required for IDE autocompletion and static type checkers (mypy, pyright, etc.) to recognize the new API. For reference, the pad method was added to this same stub file when it was introduced. Without updating this file, users will see "undefined attribute" errors from type checkers when calling .clamp(). Both PreProcessSteps.clamp(self, min_value: float, max_value: float) -> PreProcessSteps and PostProcessSteps.clamp(self, min_value: float, max_value: float) -> PostProcessSteps stubs need to be added.
There was a problem hiding this comment.
I have addressed these issues and implemented the suggestions and would like for the review.
| assert "Divide" in model_operators | ||
|
|
||
|
|
||
| def test_graph_postprocess_clamp(): |
There was a problem hiding this comment.
[LOW] The test function test_graph_postprocess_clamp uses an inconsistent naming pattern. All other tests in this file that exercise postprocessing functionality follow the test_graph_preprocess_* prefix convention (e.g., test_graph_preprocess_output_postprocess at line 162, test_graph_preprocess_postprocess_layout at line 440). This test should be renamed to test_graph_preprocess_postprocess_clamp for consistency.
| def test_graph_postprocess_clamp(): | |
| def test_graph_preprocess_postprocess_clamp(): |
|
I have updated the PR to address the initial Copilot review, I have added .pyi type stubs for clamp in both PreProcessSteps and PostProcessSteps and also renamed the preprocessing test. |
|
Hi, please fix linter check |
I've applied the formatting fixes (linter) and added the missing .pyi stubs as requested. I've force-pushed the updated commit. Please do let me know if any changes need to be done. |
|
I have updated the requested changes , would like for the review. |
### Details: - Python stub files (`.pyi`) under `src/bindings/python/` are auto-generated by a scheduled GitHub Actions workflow (`update_pyapi_stubs.yml`) using `pybind11-stubgen`. Comments requesting changes to it [(example)](#34511 (comment)) are not necessary. ### AI Assistance: - *AI assistance used: yes*
Adds clamp(min_value, max_value) as a built-in preprocessing and postprocessing step in the PrePostProcessor Python API. Changes: - Added Python bindings for PreProcessSteps.clamp() and PostProcessSteps.clamp() in pre_post_process.cpp - Added Python unit tests for preprocessing clamp, chained operations (mean/scale + clamp), and postprocessing clamp The underlying C++ implementation (ov::op::v0::Clamp) was already present. This PR exposes it to Python users via the API.
|
Hi @almilosz, thanks for the heads up on the auto-generated .pyi stubs. I've reverted those changes and updated the PR into a single commit containing only the C++ bindings and Python tests. Kindly review this. |
commit 3de2766 Author: Alicja Miloszewska <alicja.miloszewska@intel.com> Date: Fri Mar 6 06:48:16 2026 +0100 Add information about .pyi to copilot-instructions.md (openvinotoolkit#34518) - Python stub files (`.pyi`) under `src/bindings/python/` are auto-generated by a scheduled GitHub Actions workflow (`update_pyapi_stubs.yml`) using `pybind11-stubgen`. Comments requesting changes to it [(example)](openvinotoolkit#34511 (comment)) are not necessary. - *AI assistance used: yes* commit d988c34 Author: Eddy Kim <eddy.kim@intel.com> Date: Fri Mar 6 04:34:00 2026 +0000 [GPU] fix sdpa_micro_gqa_single_token for multi batch (openvinotoolkit#34192) - This PR fixed `sdpa_micro_gqa_single_token` to work correctly for multi batch. - updated to create an internal buffer for `sdpa_micro` for all stages including `generate` phase. - fixed the offset calculation logic for input and output in `sdpa_micro`. - 181161 Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com>
commit 35686c4 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Mar 6 10:39:41 2026 +0100 Revert "Add memory mapping for part of a file" This reverts commit 8340520. commit d32dbb1 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Mar 6 10:39:38 2026 +0100 Revert "Extend partial memory map test" This reverts commit bddc4a6. commit cd6980b Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Mar 6 10:39:35 2026 +0100 Revert "Use partial mmap" This reverts commit 00f334b. commit 00f334b Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Mar 6 09:56:13 2026 +0100 Use partial mmap Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit bddc4a6 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Mar 6 09:53:55 2026 +0100 Extend partial memory map test Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 8340520 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Mar 6 09:13:50 2026 +0100 Add memory mapping for part of a file Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 6231602 Merge: 2066fb3 3de2766 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Mar 6 09:09:17 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 3de2766 Author: Alicja Miloszewska <alicja.miloszewska@intel.com> Date: Fri Mar 6 06:48:16 2026 +0100 Add information about .pyi to copilot-instructions.md (openvinotoolkit#34518) - Python stub files (`.pyi`) under `src/bindings/python/` are auto-generated by a scheduled GitHub Actions workflow (`update_pyapi_stubs.yml`) using `pybind11-stubgen`. Comments requesting changes to it [(example)](openvinotoolkit#34511 (comment)) are not necessary. - *AI assistance used: yes* commit d988c34 Author: Eddy Kim <eddy.kim@intel.com> Date: Fri Mar 6 04:34:00 2026 +0000 [GPU] fix sdpa_micro_gqa_single_token for multi batch (openvinotoolkit#34192) - This PR fixed `sdpa_micro_gqa_single_token` to work correctly for multi batch. - updated to create an internal buffer for `sdpa_micro` for all stages including `generate` phase. - fixed the offset calculation logic for input and output in `sdpa_micro`. - 181161 commit 2066fb3 Merge: 2ab8c25 416caa3 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Mar 4 10:51:18 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 2ab8c25 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Mar 4 09:27:44 2026 +0100 Separate TLV structure into traits and utils Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit f236e8f Merge: 4e07108 e0bd04c Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Mar 3 20:06:03 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 4e07108 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Mar 3 10:40:37 2026 +0100 Sync with weight sharing api PR Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit dc2b2e7 Merge: 119c0c6 083b0f1 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Mar 3 09:51:14 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 119c0c6 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Mon Mar 2 16:33:24 2026 +0100 Remove FormatVersion with util one Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 2a45b56 Merge: c42b436 9131e4e Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Mon Mar 2 14:14:18 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit c42b436 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Sat Feb 28 19:27:31 2026 +0100 Fix windows build Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 1d75d9f Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Sat Feb 28 18:30:50 2026 +0100 Fix windows build Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 62aa9ea Merge: 2e59880 4773bdf Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Sat Feb 28 15:38:45 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 2e59880 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Sat Feb 28 15:33:50 2026 +0100 Handle error in corrupted file Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit f08d22d Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Sat Feb 28 14:56:47 2026 +0100 Set alignment based on system props Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 9ab5fa4 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Feb 27 16:21:59 2026 +0100 Write context delta only Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 30d0789 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Feb 27 15:38:55 2026 +0100 Combine scanning functions Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit ba0ebeb Merge: 26aa429 d5e4016 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Feb 27 10:49:47 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 26aa429 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Feb 27 09:39:38 2026 +0100 Avoid capturing structured bindings Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 738c343 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Thu Feb 26 17:43:26 2026 +0100 Fulfill the bid Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 93e3cbc Merge: 0ece331 ddcdd67 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Thu Feb 26 12:23:12 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 0ece331 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Thu Feb 26 09:52:04 2026 +0100 Fulfill the bid Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 3bc2485 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 16:41:55 2026 +0100 Add TLV Codecs test Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 77d7e0a Merge: 79444ad 9a5c0f6 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 13:48:09 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit 79444ad Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 13:48:02 2026 +0100 Sync weight_sharing_util.hpp with PR#34100 Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 4fd7790 Merge: bac2104 6913249 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 13:19:30 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit bac2104 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 13:18:54 2026 +0100 Fix spelling mistakes Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 9b134a2 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 12:24:29 2026 +0100 Change comment style from /// to multiline /** */ Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 1cd5b44 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 12:05:32 2026 +0100 Revert core changes Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit ff7f3bc Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 11:29:17 2026 +0100 Align type name style Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 193f5ab Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 11:01:06 2026 +0100 Revert "Squashed commit of the following:" This reverts commit 1dbee7b. But keeps src/core/dev_api/openvino/core/weight_sharing_util.hpp Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 2f81a7c Merge: cdd0328 3b7c385 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 10:28:14 2026 +0100 Merge remote-tracking branch 'upstream/master' into tj/ie/single_file_storage commit cdd0328 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 25 10:25:52 2026 +0100 Add class comments Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit df76438 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Feb 24 18:38:11 2026 +0100 Remove obsolete remainings Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit ce623ba Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Feb 24 17:31:03 2026 +0100 Add weight data writing Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 02f59d6 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Feb 24 08:12:04 2026 +0100 Adde shared context reading Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 8443b84 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Mon Feb 23 08:03:11 2026 +0100 Add comments to TLV format helpers Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 1dbee7b Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Feb 20 12:25:05 2026 +0100 Squashed commit of the following: commit 37e15a1 Author: Raasz, Pawel <pawel.raasz@intel.com> Date: Mon Feb 16 11:00:03 2026 +0000 Refactor weight share API Signed-off-by: Raasz, Pawel <pawel.raasz@intel.com> commit 7de8dc9 Author: Raasz, Pawel <pawel.raasz@intel.com> Date: Thu Feb 12 18:56:35 2026 +0000 Minor fixes Signed-off-by: Raasz, Pawel <pawel.raasz@intel.com> commit e90843c Author: Raasz, Pawel <pawel.raasz@intel.com> Date: Thu Feb 12 17:52:34 2026 +0000 Update tests Signed-off-by: Raasz, Pawel <pawel.raasz@intel.com> commit 8ecf2f1 Author: Raasz, Pawel <pawel.raasz@intel.com> Date: Thu Feb 12 17:39:22 2026 +0000 Introduce weight sharing dev API Signed-off-by: Raasz, Pawel <pawel.raasz@intel.com> Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 5962417 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Thu Feb 19 21:01:04 2026 +0100 Separate storage logic and file format Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit a2d3f44 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Thu Feb 19 07:41:07 2026 +0100 Start testing mmap read .. although partial mmap is not yet supported. Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 90c275b Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 18 20:09:10 2026 +0100 Align blob data position to 4k Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 7fc001d Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 18 13:33:23 2026 +0100 Write blob map Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit c6f121e Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 18 11:20:06 2026 +0100 Assume valid blob_id value Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit fdb2918 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Feb 17 21:41:14 2026 +0100 Remove cache index with blob map Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 5913702 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Feb 17 15:26:14 2026 +0100 Create blob map codec Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit b12c469 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Feb 17 10:25:08 2026 +0100 Add header Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit b6b0e65 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Mon Feb 16 10:13:33 2026 +0100 More traits from namespace into class Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit d23a126 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Fri Feb 13 10:32:52 2026 +0100 Separate traits and codecs Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 4cb8926 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Thu Feb 12 11:47:21 2026 +0100 Add append only test Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit de91ccc Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Wed Feb 11 12:21:05 2026 +0100 Add SingleFileStorage unit test Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 20949ef Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Tue Feb 10 12:03:17 2026 +0100 Define derived member functions Co-authored-by: Pawel Raasz <pawel.raasz@intel.com> Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> commit 6cb5e02 Author: Tomasz Jankowski <tomasz1.jankowski@intel.com> Date: Mon Feb 9 11:29:49 2026 +0100 Add Single File Storage class to cache managers without member functions' body defined .. yet Co-authored-by: Pawel Raasz <pawel.raasz@intel.com> Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> Signed-off-by: Tomasz Jankowski <tomasz1.jankowski@intel.com>
Details:
This PR adds the clamp functionality ( clamp(min, max) ) , as a built-in preprocessing and postprocessing step as requested in the issue.
Added python bindings for PreProcessorSteps.clamp(min, max) and PostProcessor steps as well in the pre_post_process.cpp file.
Also added Python unit tests covering preprocessing clamp , chained operations(like mean/scale + clamp), and the postprocessing clamp.
The C++ implementation (ov::op::v0::Clamp) was already present. This PR exposes it to Python users via the API.
Tickets:
AI Assistance:
i have used AI assistance.
I had used copilot to get an understanding of the codebase and existing C++ structure. All the code was reviewed and i had validated manually by building pyopenvino target locally. I had run the C++ unit tests , with getting all 13 tests passed as well as with manual test that confirms the Clamp node appears in the model graph.