Skip to content

fix(python): grow buffers instead of failing and guard optional ops - #826

Open
Python11235 wants to merge 1 commit into
cactus-compute:mainfrom
Python11235:fix/python-bindings
Open

Python11235 wants to merge 1 commit into
cactus-compute:mainfrom
Python11235:fix/python-bindings

Conversation

@Python11235

Copy link
Copy Markdown

Summary

Python binding fixes in python/cactus/bindings/cactus.py:

  • Grow buffers instead of failing. cactus_tokenize, cactus_embed, cactus_image_embed, cactus_audio_embed and cactus_preprocess_audio_features now retry with a larger buffer when the C side returns -2 (buffer too small). Before, they raised a generic error. -2 means "buffer too small" in all five C implementations.
  • Clear error for missing optional ops. Optional graph ops that are missing from the loaded library now raise RuntimeError("... rebuild with cactus build --python") instead of a bare AttributeError. The gather → cactus_graph_gather fallback still works.
  • Index helpers. Empty ids and empty embeddings are handled, and embedding sizes returned by cactus_index_get are bounds-checked.
  • Graph.input checks that dynamic_dims has the same length as shape.
  • Less FFI work. Tensor shape and dtype are resolved lazily, graph inputs reuse the shape and dtype they were created with, and set_input skips the copy when the array already has the target dtype and layout.
  • PCM marshaling uses one buffer copy instead of unpacking every byte into a ctypes array.

Replaces #814, which failed the DCO check. This is the same change, squashed and signed off.

Testing

  • pytest tests/ --ignore=tests/test_model.py --ignore=tests/test_server_live.py gives the same results as main (171 passed, no new failures). I ran it against a stub library on x86, because the kernels need ARM to build.
  • A targeted script for the retry paths, the optional-op errors, the gather fallback, the concurrent cactus_complete calls and PCM marshaling: all pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hf1fEfGn1rnodpQdjope6c

- Retry tokenize, text/image/audio embedding and audio feature
  preprocessing with a larger buffer when the C side returns -2
  (buffer too small) instead of raising a generic error.
- Raise a clear "rebuild with cactus build --python" error when an
  optional graph op is missing from the loaded library, instead of a
  bare AttributeError.
- Handle empty inputs in the index helpers and bounds-check embedding
  sizes returned by cactus_index_get.
- Validate dynamic_dims length against the input shape in Graph.input.
- Resolve Tensor shape and dtype lazily, reuse the known shape and dtype
  for graph inputs, and skip the copy in set_input when the array
  already has the target dtype and layout.
- Marshal PCM bytes with a single buffer copy instead of unpacking
  every byte into a ctypes array.

Signed-off-by: Python/er <241503998+Python11235@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hf1fEfGn1rnodpQdjope6c

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant