# Is it normal for compiled model outputs to have different tensor strides?

**URL:** <https://discuss.pytorch.org/t/is-it-normal-for-compiled-model-outputs-to-have-different-tensor-strides/223469>\
**Category:** torch.compile\
**Created:** [October 5, 2025, 1:18pm UTC](https://discuss.pytorch.org/t/is-it-normal-for-compiled-model-outputs-to-have-different-tensor-strides/223469 "2025-10-05T13:18:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tinywisdom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tinywisdom/32/76058_2.png) [@tinywisdom](https://discuss.pytorch.org/u/tinywisdom)\
**Post date:** [October 5, 2025, 1:18pm UTC](https://discuss.pytorch.org/t/is-it-normal-for-compiled-model-outputs-to-have-different-tensor-strides/223469/1 "2025-10-05T13:18:30Z")

</div>

Hello everyone,

I recently compiled my model using `torch.compile`, and I noticed that the results after compilation have some layout differences compared to the original (eager) execution.

When I check the outputs using `torch.testing.assert_close`, I sometimes get messages like:

```auto
The values for attribute 'stride()' do not match: (27, 1, 9, 3) != (27, 9, 3, 1)

```

or

```auto
The values for attribute 'stride()' do not match: (1, 2) != (2, 1)

```

The numerical values themselves appear identical, but the stride orders differ.  
I’d like to ask: **is this expected or normal behavior after model compilation?**  
Does the compiler sometimes change the memory layout (e.g., due to optimization or contiguous conversion)?

Thanks a lot for your time and for maintaining this great project!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 7, 2025, 8:47pm UTC](https://discuss.pytorch.org/t/is-it-normal-for-compiled-model-outputs-to-have-different-tensor-strides/223469/2 "2025-10-07T20:47:00Z")

</div>

Yes, this is expected. From the [missing manual](https://docs.google.com/document/d/1y5CRfMLdwEoF1nTk9q8qEu1mgMUuUtvhklPKJ2emLU8/edit?tab=t.0):

> Stride divergence  
> PT2 is not guaranteed to produce tensors with exactly the same stride as the eager variants. This can potentially cause correctness problems (or runtime crashes) outside of the torch.compile region if subsequent code is expecting a specific striding without testing, or is using the limited subset of APIs (mostly torch.as\_strided, and also obscure situations involving mutating through .reshape()/.contiguous()) which are sensitive to input of strides.
> 
> TODO: There is supposed to be a way to fix strides, similar to how we can do this for custom ops.

---

<div class="post-metadata">

**Author:** ![GerryMan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/f6c823/32.png) [@GerryMan](https://discuss.pytorch.org/u/GerryMan)\
**Post date:** [October 8, 2025, 7:42am UTC](https://discuss.pytorch.org/t/is-it-normal-for-compiled-model-outputs-to-have-different-tensor-strides/223469/3 "2025-10-08T07:42:46Z")

</div>

That’s normal. torch.compile can change the tensor layout sometimes for speed, the values themselves stay fine

---

<div class="post-metadata">

**Author:** ![tinywisdom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tinywisdom/32/76058_2.png) [@tinywisdom](https://discuss.pytorch.org/u/tinywisdom)\
**Post date:** [October 14, 2025, 2:50am UTC](https://discuss.pytorch.org/t/is-it-normal-for-compiled-model-outputs-to-have-different-tensor-strides/223469/4 "2025-10-14T02:50:33Z")

</div>

Thanks a lot for the clarification! That makes sense — good to know it’s expected behavior with `torch.compile`. Appreciate the detailed explanation!
