-
Notifications
You must be signed in to change notification settings - Fork 687
Fix pybind API and docs #15051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix pybind API and docs #15051
Conversation
Summary of Changes to runtime/__init__.py This PR fixes two critical bugs and improves documentation in the Python runtime API. Bug Fixes 1. Fix isinstance(data, BinaryIO) Check for File-Like Objects Problem: When loading a program from file-like objects (e.g., io.BytesIO), the runtime would fail with a TypeError because isinstance(data, BinaryIO) doesn't work. BinaryIO is a typing protocol, not a concrete class, so isinstance() checks always return False. Fix (runtime/__init__.py:320-322): elif isinstance(data, BinaryIO): # Never matched! data_bytes = data.read() elif hasattr(data, "read"): # Duck typing approach data_bytes = data.read() Impact: Users can now correctly load programs from BytesIO, file handles, and other file-like objects. Regression Test: Added test_load_program_with_file_like_objects() in runtime/test/test_runtime.py to validate BytesIO, bytes, and bytearray loading. 2. Fix ETDump Parameters Ignored When Loading From Bytes/Buffer Problem: When loading programs from bytes, bytearray, or file-like objects, the enable_etdump and debug_buffer_size parameters were hardcoded to False and 0, completely ignoring user-provided values. This made ETDump profiling impossible for in-memory program loading. Fix (runtime/__init__.py:327-332): p = self._legacy_module._load_program_from_buffer( data_bytes, enable_etdump=False, # Hardcoded! debug_buffer_size=0, # Hardcoded! program_verification=verification, ) p = self._legacy_module._load_program_from_buffer( data_bytes, enable_etdump=enable_etdump, # Use provided parameter debug_buffer_size=debug_buffer_size, # Use provided parameter program_verification=verification, ) Impact: ETDump profiling now works correctly for all program loading paths (file path, bytes, bytearray, file-like objects), not just file paths. Regression Test: Added test_etdump_params_with_bytes_and_buffer() in runtime/test/test_runtime_etdump_gen.py to validate parameter passing for bytes, bytearray, and BytesIO. Documentation Improvements Fixed Docstring Errors - BackendRegistry.is_available(): Fixed copy-paste error in docstring (was describing registered_backend_names instead) - Program.metadata(): Fixed return type description (was generic, now specific to MethodMeta) - Runtime.load_program(): Added missing documentation for enable_etdump and debug_buffer_size parameters Enhanced API Documentation - Method.execute(): Clarified input/output types (torch.Tensor objects) - Method.metadata: Improved description of what metadata includes - Runtime class: Added Attributes section documenting backend_registry and operator_registry Improved Module-Level Examples - Fixed ETDump example: Changed debug_buffer_size=1e7 to int(1e7) (correct type) - Added backend/operator introspection example: Shows how to query available backends and operators - Made example output generic: Removed specific backend names and added build-dependent notes Testing All tests pass (7/7) including both new regression tests: ✅ test_load_program_with_file_like_objects - Validates BytesIO bug fix ✅ test_etdump_params_with_bytes_and_buffer - Validates ETDump parameter bug fix Impact These fixes improve the robustness and usability of the Python runtime API: - File-like object support now works as documented - ETDump profiling now works for all loading methods - Documentation is more accurate and comprehensive
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/15051
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 6 New Failures, 7 Unrelated FailuresAs of commit 376c281 with merge base d00279d ( NEW FAILURES - The following jobs have failed:
FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
@mergennachin has imported this pull request. If you are a Meta employee, you can view this in D84519459. |
Differential Revision: D84519459 Pull Request resolved: #15051
Summary of Changes to runtime/init.py
This PR fixes two critical bugs and improves documentation in the Python runtime API.
Bug Fixes
Problem: When loading a program from file-like objects (e.g., io.BytesIO), the runtime would fail with a TypeError because isinstance(data, BinaryIO) doesn't work. BinaryIO is a typing protocol, not a concrete class, so isinstance() checks always return False.
Fix (runtime/init.py:320-322):
elif isinstance(data, BinaryIO): # Never matched!
data_bytes = data.read()
elif hasattr(data, "read"): # Duck typing approach
data_bytes = data.read()
Impact: Users can now correctly load programs from BytesIO, file handles, and other file-like objects.
Regression Test: Added test_load_program_with_file_like_objects() in runtime/test/test_runtime.py to validate BytesIO, bytes, and bytearray loading.
Problem: When loading programs from bytes, bytearray, or file-like objects, the enable_etdump and debug_buffer_size parameters were hardcoded to False and 0, completely ignoring user-provided values. This made ETDump profiling impossible for in-memory program loading.
Fix (runtime/init.py:327-332):
p = self._legacy_module._load_program_from_buffer(
data_bytes,
enable_etdump=False, # Hardcoded!
debug_buffer_size=0, # Hardcoded!
program_verification=verification,
)
p = self._legacy_module._load_program_from_buffer(
data_bytes,
enable_etdump=enable_etdump, # Use provided parameter
debug_buffer_size=debug_buffer_size, # Use provided parameter
program_verification=verification,
)
Impact: ETDump profiling now works correctly for all program loading paths (file path, bytes, bytearray, file-like objects), not just file paths.
Regression Test: Added test_etdump_params_with_bytes_and_buffer() in runtime/test/test_runtime_etdump_gen.py to validate parameter passing for bytes, bytearray, and BytesIO.
Documentation Improvements
Fixed Docstring Errors
Enhanced API Documentation
Improved Module-Level Examples
Testing
All tests pass (7/7) including both new regression tests:
✅ test_load_program_with_file_like_objects - Validates BytesIO bug fix
✅ test_etdump_params_with_bytes_and_buffer - Validates ETDump parameter bug fix
Impact
These fixes improve the robustness and usability of the Python runtime API: