Skip to content

Conversation

mergennachin
Copy link
Contributor

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.

  1. 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

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
Copy link

pytorch-bot bot commented Oct 13, 2025

🔗 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 SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 6 New Failures, 7 Unrelated Failures

As of commit 376c281 with merge base d00279d (image):

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.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 13, 2025
Copy link

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link

meta-codesync bot commented Oct 13, 2025

@mergennachin has imported this pull request. If you are a Meta employee, you can view this in D84519459.

@meta-codesync meta-codesync bot merged commit e0b4007 into main Oct 13, 2025
127 of 142 checks passed
@meta-codesync meta-codesync bot deleted the pybind_api branch October 13, 2025 22:06
Gasoonjia pushed a commit that referenced this pull request Oct 14, 2025
Differential Revision: D84519459

Pull Request resolved: #15051
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants