Skip to content

[PyOV] Fix creation of Constant from numpy string arrays - #38942

Open
Iyanuoluwa007 wants to merge 1 commit into
openvinotoolkit:masterfrom
Iyanuoluwa007:fix/python-string-constant-23611
Open

Iyanuoluwa007 wants to merge 1 commit into
openvinotoolkit:masterfrom
Iyanuoluwa007:fix/python-string-constant-23611

Conversation

@Iyanuoluwa007

Copy link
Copy Markdown

Details:

  • Creating a Constant from a numpy str_/bytes_ array passed the fixed-width numpy buffer to the string Constant constructor as if it held std::string objects. Depending on the data this crashed the interpreter or raised MemoryError: bad allocation.
  • Convert string arrays into a string Tensor first, using the same helper the Tensor constructor already uses, and build the Constant from it.
  • Support non-contiguous string arrays in as_contiguous instead of throwing, and raise a clear error when shared memory is requested for strings (string data can only be copied), matching Tensor.
  • Add tests for str and bytes arrays, scalar, 2D, empty string, non-contiguous input, inference with a string constant, and the shared memory error.
  • The issue is currently assigned with no linked PR and no activity for several months. I asked in the issue whether it can be reassigned; if the assignees are still working on it, I am happy to close this PR.

Tickets:

AI Assistance:

  • AI assistance used: yes
  • The root cause analysis, fix and tests were done with an AI coding assistant.
  • Validation performed: local Windows build of pyopenvino (MSVC, Release); the new tests crashed the interpreter before the change and pass after it (8 passed); test_constant.py otherwise unchanged apart from tests that need the AUTO plugin, which was not part of the local build; test_tensor.py and test_tensor_string.py: 636 passed; clang-format 18 and flake8 clean.

Creating a Constant from a numpy array of str_ or bytes_ passed the raw
fixed-width numpy buffer to the Constant constructor as if it held
std::string objects. Depending on the data this crashed the interpreter
or raised MemoryError.

Convert string arrays element by element into a string Tensor before
creating the Constant, the same way Tensor creation already does. Also
make non-contiguous string arrays contiguous instead of throwing, and
raise a clear error when shared memory is requested for strings, since
string data can only be copied.

Closes openvinotoolkit#23611

@Somay-kousis Somay-kousis left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the master behaviour on macOS arm64 with the 2026.5.0 nightly wheel. There it doesn't crash or raise. Every string just comes back empty, so on some platforms this is a silent wrong value:

call master
ops.constant(np.array(["openvino"], dtype=str)).get_value_strings() ['']
ops.constant(np.array(["text", "", "x" * 40], dtype=str)) ['', '', '']
ops.constant(np.array([b"bytes", b"b"], dtype=np.bytes_)) ['', '']
ops.constant(np.array("scalar", dtype=str)) ['']

Constant(Tensor(np.array(["a", "bc"]))) already gives ['a', 'bc'], so routing through the string Tensor helper matches what that path does.

One behaviour change outside Constant: as_contiguous is also used by Tensor's create_copied (common.cpp, the !is_contiguous branch before fill_string_tensor_data). On master, Tensor(np.array(["openvino", "test"], dtype=str)[::-1]) raises RuntimeError: Tensor cannot be created as contiguous!. With the new case ov::element::string it should copy instead. That looks like the right behaviour, but only the Constant side is tested. A case in test_tensor_string.py for a reversed or strided str_ and bytes_ array would cover the Tensor change too. I didn't build the branch, so the after-state for Tensor is inferred from the diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: Python API OpenVINO Python bindings ExternalPR External contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Good First Issue][Python API]: Create constant for string tensor and fix segfault

3 participants