Commit 7cbcef1
Fix torch.randn_like stride preservation in inductor decomposition
Fixes #147847 where torch.rot90 + torch.randn_like produced inconsistent
results between eager and compiled modes. The issue was that inductor's
decomposition wasn't preserving non-contiguous stride patterns.
The fix implements the same behavior as eager mode:
- Preserves exact strides for non-overlapping dense tensors
- Compacts strides for tensors with gaps (removing memory holes while
maintaining dimension ordering)
- Correctly handles stride 0 cases for expanded/broadcasted tensors
- Uses torch._prims_common.is_non_overlapping_and_dense to match eager
mode's logic exactly
Implementation details:
- Added _should_use_dense_strides() to determine when stride compaction
is needed
- Added _compute_dense_strides() that replicates C++ infer_dense_strides
algorithm with proper handling of symbolic integers and stride 0
- Updated rand_like and randn_like decompositions to use as_strided
when needed to preserve memory layout
Added comprehensive test coverage in a single compact test method that
covers all cases:
- Stride 0 handling for expanded and broadcasted tensors
- Stride compaction for tensors with memory gaps
- Edge cases including scalar, empty, and channels_last tensors
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
pythonify some transpiled C++ code
Refactor *_like functions to eliminate code duplication
Extract common stride preservation logic from rand_like, randn_like,
full_like, and randint_like functions into a shared _apply_stride_logic
helper function.
This refactoring:
- Reduces ~75 lines of duplicate code across 5 functions
- Creates a single point of maintenance for stride handling logic
- Preserves all original functionality and behavior
- Makes the code more maintainable and follows DRY principles
The _apply_stride_logic helper handles:
- Explicit memory format conversion
- Dense stride computation when needed
- Exact stride preservation when appropriate
All *_like functions now follow the same pattern:
1. Create the base tensor with the appropriate generation function
2. Call _apply_stride_logic to handle stride preservation
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Remove redundant import and use existing utils alias
Use the existing 'utils' alias for torch._prims_common instead of
importing it again inside _should_use_dense_strides function.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Update stride logic to more accurately match C++ implementation
Refine _apply_stride_logic to better match the C++ implementation in
TensorFactories.cpp (lines 461-489):
1. Split logic into three explicit cases matching C++ code paths
2. Add layout == torch.strided check for case 2
3. Remove unused _should_use_dense_strides function
The implementation now more accurately reflects eager mode behavior:
- Case 1: Preserve exact strides for non-overlapping dense tensors
- Case 2: Compute dense strides for non-dense strided tensors
- Case 3: Return result as-is for non-strided layouts
Tests continue to pass, confirming the implementation is correct.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Use suggest_memory_format() in fallback case to match C++ implementation
Add utils.suggest_memory_format() call in Case 3 (non-strided layouts)
to exactly match the C++ implementation's fallback behavior.
This ensures that for non-strided tensors, we use the most appropriate
memory format based on the tensor's current layout, maintaining full
consistency with eager mode behavior.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Add comprehensive test cases for memory format handling
Add two new test methods to thoroughly test memory format behavior:
1. test_like_stride_preservation_channels_last:
- Tests preservation of channels_last (4D) and channels_last_3d (5D) formats
- Verifies that *_like functions maintain these memory formats
- Tests that suggest_memory_format logic correctly identifies these formats
2. test_like_explicit_memory_format:
- Tests explicit memory_format parameter handling
- Covers torch.channels_last, torch.contiguous_format, and torch.preserve_format
- Ensures explicit format requests are properly honored
These tests exercise all three cases in _apply_stride_logic:
- Case 1: Dense tensor stride preservation (channels_last tensors)
- Case 2: Dense stride computation (via explicit format conversion)
- Case 3: Would use suggest_memory_format (though rare for strided tensors)
All tests pass on both CPU and CUDA.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Add type annotations to _get_symbolic_value to fix mypy error
Add proper type hints to satisfy mypy's no-untyped-def check.
The function accepts Any type value and optional int default,
returning either an int or the original value type.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 4283d96 commit 7cbcef1
File tree
2 files changed
+491
-15
lines changed- test/inductor
- torch/_inductor
2 files changed
+491
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9286 | 9286 | | |
9287 | 9287 | | |
9288 | 9288 | | |
| 9289 | + | |
| 9290 | + | |
| 9291 | + | |
| 9292 | + | |
| 9293 | + | |
| 9294 | + | |
| 9295 | + | |
| 9296 | + | |
| 9297 | + | |
| 9298 | + | |
| 9299 | + | |
| 9300 | + | |
| 9301 | + | |
| 9302 | + | |
| 9303 | + | |
| 9304 | + | |
| 9305 | + | |
| 9306 | + | |
| 9307 | + | |
| 9308 | + | |
| 9309 | + | |
| 9310 | + | |
| 9311 | + | |
| 9312 | + | |
| 9313 | + | |
| 9314 | + | |
| 9315 | + | |
| 9316 | + | |
| 9317 | + | |
| 9318 | + | |
| 9319 | + | |
| 9320 | + | |
| 9321 | + | |
| 9322 | + | |
| 9323 | + | |
| 9324 | + | |
| 9325 | + | |
| 9326 | + | |
| 9327 | + | |
| 9328 | + | |
| 9329 | + | |
| 9330 | + | |
| 9331 | + | |
| 9332 | + | |
| 9333 | + | |
| 9334 | + | |
| 9335 | + | |
| 9336 | + | |
| 9337 | + | |
| 9338 | + | |
| 9339 | + | |
| 9340 | + | |
| 9341 | + | |
| 9342 | + | |
| 9343 | + | |
| 9344 | + | |
| 9345 | + | |
| 9346 | + | |
| 9347 | + | |
| 9348 | + | |
| 9349 | + | |
| 9350 | + | |
| 9351 | + | |
| 9352 | + | |
| 9353 | + | |
| 9354 | + | |
| 9355 | + | |
| 9356 | + | |
| 9357 | + | |
| 9358 | + | |
| 9359 | + | |
| 9360 | + | |
| 9361 | + | |
| 9362 | + | |
| 9363 | + | |
| 9364 | + | |
| 9365 | + | |
| 9366 | + | |
| 9367 | + | |
| 9368 | + | |
| 9369 | + | |
| 9370 | + | |
| 9371 | + | |
| 9372 | + | |
| 9373 | + | |
| 9374 | + | |
| 9375 | + | |
| 9376 | + | |
| 9377 | + | |
| 9378 | + | |
| 9379 | + | |
| 9380 | + | |
| 9381 | + | |
| 9382 | + | |
| 9383 | + | |
| 9384 | + | |
| 9385 | + | |
| 9386 | + | |
| 9387 | + | |
| 9388 | + | |
| 9389 | + | |
| 9390 | + | |
| 9391 | + | |
| 9392 | + | |
| 9393 | + | |
| 9394 | + | |
| 9395 | + | |
| 9396 | + | |
| 9397 | + | |
| 9398 | + | |
| 9399 | + | |
| 9400 | + | |
| 9401 | + | |
| 9402 | + | |
| 9403 | + | |
| 9404 | + | |
| 9405 | + | |
| 9406 | + | |
| 9407 | + | |
| 9408 | + | |
| 9409 | + | |
| 9410 | + | |
| 9411 | + | |
| 9412 | + | |
| 9413 | + | |
| 9414 | + | |
| 9415 | + | |
| 9416 | + | |
| 9417 | + | |
| 9418 | + | |
| 9419 | + | |
| 9420 | + | |
| 9421 | + | |
| 9422 | + | |
| 9423 | + | |
| 9424 | + | |
| 9425 | + | |
| 9426 | + | |
| 9427 | + | |
| 9428 | + | |
| 9429 | + | |
| 9430 | + | |
| 9431 | + | |
| 9432 | + | |
| 9433 | + | |
| 9434 | + | |
| 9435 | + | |
| 9436 | + | |
| 9437 | + | |
| 9438 | + | |
| 9439 | + | |
| 9440 | + | |
| 9441 | + | |
| 9442 | + | |
| 9443 | + | |
| 9444 | + | |
| 9445 | + | |
| 9446 | + | |
| 9447 | + | |
| 9448 | + | |
| 9449 | + | |
| 9450 | + | |
| 9451 | + | |
| 9452 | + | |
| 9453 | + | |
| 9454 | + | |
| 9455 | + | |
| 9456 | + | |
| 9457 | + | |
| 9458 | + | |
| 9459 | + | |
| 9460 | + | |
| 9461 | + | |
| 9462 | + | |
| 9463 | + | |
| 9464 | + | |
| 9465 | + | |
| 9466 | + | |
| 9467 | + | |
| 9468 | + | |
| 9469 | + | |
| 9470 | + | |
| 9471 | + | |
| 9472 | + | |
| 9473 | + | |
| 9474 | + | |
| 9475 | + | |
| 9476 | + | |
| 9477 | + | |
| 9478 | + | |
| 9479 | + | |
| 9480 | + | |
| 9481 | + | |
| 9482 | + | |
| 9483 | + | |
| 9484 | + | |
| 9485 | + | |
| 9486 | + | |
| 9487 | + | |
| 9488 | + | |
| 9489 | + | |
| 9490 | + | |
| 9491 | + | |
| 9492 | + | |
| 9493 | + | |
| 9494 | + | |
| 9495 | + | |
| 9496 | + | |
| 9497 | + | |
| 9498 | + | |
| 9499 | + | |
| 9500 | + | |
| 9501 | + | |
| 9502 | + | |
| 9503 | + | |
| 9504 | + | |
| 9505 | + | |
| 9506 | + | |
| 9507 | + | |
| 9508 | + | |
| 9509 | + | |
| 9510 | + | |
| 9511 | + | |
| 9512 | + | |
| 9513 | + | |
| 9514 | + | |
| 9515 | + | |
| 9516 | + | |
| 9517 | + | |
| 9518 | + | |
| 9519 | + | |
| 9520 | + | |
| 9521 | + | |
| 9522 | + | |
| 9523 | + | |
| 9524 | + | |
| 9525 | + | |
| 9526 | + | |
| 9527 | + | |
| 9528 | + | |
| 9529 | + | |
| 9530 | + | |
| 9531 | + | |
| 9532 | + | |
| 9533 | + | |
| 9534 | + | |
| 9535 | + | |
| 9536 | + | |
| 9537 | + | |
| 9538 | + | |
| 9539 | + | |
| 9540 | + | |
| 9541 | + | |
| 9542 | + | |
| 9543 | + | |
| 9544 | + | |
| 9545 | + | |
| 9546 | + | |
| 9547 | + | |
| 9548 | + | |
| 9549 | + | |
| 9550 | + | |
| 9551 | + | |
| 9552 | + | |
| 9553 | + | |
| 9554 | + | |
| 9555 | + | |
| 9556 | + | |
| 9557 | + | |
| 9558 | + | |
| 9559 | + | |
| 9560 | + | |
| 9561 | + | |
| 9562 | + | |
| 9563 | + | |
| 9564 | + | |
| 9565 | + | |
| 9566 | + | |
| 9567 | + | |
| 9568 | + | |
| 9569 | + | |
| 9570 | + | |
| 9571 | + | |
| 9572 | + | |
| 9573 | + | |
| 9574 | + | |
| 9575 | + | |
| 9576 | + | |
| 9577 | + | |
| 9578 | + | |
| 9579 | + | |
| 9580 | + | |
| 9581 | + | |
| 9582 | + | |
| 9583 | + | |
| 9584 | + | |
| 9585 | + | |
| 9586 | + | |
| 9587 | + | |
| 9588 | + | |
| 9589 | + | |
| 9590 | + | |
| 9591 | + | |
| 9592 | + | |
| 9593 | + | |
| 9594 | + | |
| 9595 | + | |
| 9596 | + | |
| 9597 | + | |
| 9598 | + | |
| 9599 | + | |
| 9600 | + | |
| 9601 | + | |
| 9602 | + | |
| 9603 | + | |
| 9604 | + | |
| 9605 | + | |
| 9606 | + | |
| 9607 | + | |
| 9608 | + | |
| 9609 | + | |
| 9610 | + | |
| 9611 | + | |
| 9612 | + | |
| 9613 | + | |
| 9614 | + | |
| 9615 | + | |
| 9616 | + | |
| 9617 | + | |
| 9618 | + | |
| 9619 | + | |
| 9620 | + | |
| 9621 | + | |
| 9622 | + | |
| 9623 | + | |
| 9624 | + | |
| 9625 | + | |
| 9626 | + | |
| 9627 | + | |
| 9628 | + | |
| 9629 | + | |
| 9630 | + | |
| 9631 | + | |
9289 | 9632 | | |
9290 | 9633 | | |
9291 | 9634 | | |
| |||
0 commit comments