Skip to content

Conversation

@abhitorch81
Copy link
Contributor

  • Add validation in constructor to prevent num_layers <= 0
  • Add runtime check before accessing self.layers[0]
  • Provide clear error messages instead of IndexError
  • Prevents production crashes in Transformer models

Fixes potential IndexError when TransformerEncoder is created with num_layers=0 or when layers list becomes empty unexpectedly.

The fix adds defensive programming practices:

  1. Constructor validation prevents invalid initialization
  2. Runtime validation prevents crashes during forward pass
  3. Clear error messages help users understand the problem

This addresses a critical bug that could cause production crashes in applications using TransformerEncoder with invalid parameters.

Fixes #165312

- Add validation in constructor to prevent num_layers <= 0
- Add runtime check before accessing self.layers[0]
- Provide clear error messages instead of IndexError
- Prevents production crashes in Transformer models

Fixes potential IndexError when TransformerEncoder is created with
num_layers=0 or when layers list becomes empty unexpectedly.

The fix adds defensive programming practices:
1. Constructor validation prevents invalid initialization
2. Runtime validation prevents crashes during forward pass
3. Clear error messages help users understand the problem

This addresses a critical bug that could cause production crashes
in applications using TransformerEncoder with invalid parameters.
@pytorch-bot
Copy link

pytorch-bot bot commented Oct 13, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/165313

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 50e714e with merge base 22b1710 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Oct 13, 2025
@abhitorch81
Copy link
Contributor Author

@soulitzer can you check on this? Thanks

@abhitorch81
Copy link
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot bot added the topic: not user facing topic category label Nov 13, 2025
@abhitorch81
Copy link
Contributor Author

@malfet @Skylion007 Can you help on reviewing this? Thanks for the co-operation and open to changes and improvement.

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

Labels

open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TransformerEncoder crashes with IndexError when num_layers=0

3 participants