Repository: coreylowman/dfdx (language: rust)

Issue:
Add `contiguous()`/`try_contiguous()` as a method to `trait ReshapeTo`
These two methods can just have default implementations of calling reshape:

```rust
fn try_contiguous(self) -> Result<Self, D::Err> {
   self.try_reshape_like(self.shape()) 
}
```

This works because reshape already ensures that output is contiguous, and calling reshape with the same shape of the tensor will ensure it has the same shape.


Discussion / hints:

Fix patch:
diff --git a/src/tensor_ops/reshape_to/mod.rs b/src/tensor_ops/reshape_to/mod.rs
index 9f40fcb30..657db6bcb 100644
--- a/src/tensor_ops/reshape_to/mod.rs
+++ b/src/tensor_ops/reshape_to/mod.rs
@@ -68,6 +68,17 @@ pub trait ReshapeTo: HasErr + HasShape {
     fn reshape_like<Dst: Shape>(self, dst: &Dst) -> Option<Self::WithShape<Dst>> {
         self.try_reshape_like(dst).map(Result::unwrap)
     }
+    /// Ensures the tensor's memory is contiguous.
+    ///
+    /// If the memory is already contiguous no copying is performed.
+    fn contiguous(self) -> Self::WithShape<Self::Shape> {
+        self.try_contiguous().unwrap()
+    }
+    /// See [`ReshapeTo::contiguous`]
+    fn try_contiguous(self) -> Result<Self::WithShape<Self::Shape>, Self::Err> {
+        let shape = *self.shape();
+        self.try_reshape_like(&shape).unwrap()
+    }
     /// Reshapes a tensor to a different runtime shape.
     fn try_reshape_like<Dst: Shape>(
         self,
@@ -212,4 +223,17 @@ mod tests {
         assert_eq!(a.as_vec(), b.as_vec());
         assert_eq!(b.array(), [[1., 2.], [3., 1.], [2., 3.]]);
     }
+
+    #[test]
+    fn test_contiguous() {
+        let dev: TestDevice = Default::default();
+
+        let a: Tensor<_, TestDtype, _> = dev.tensor([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]);
+
+        let b1 = a.clone().contiguous();
+        assert_eq!(a.strides, b1.strides);
+
+        let b2: Tensor<_, TestDtype, _> = a.permute::<Rank2<3, 2>, _>().contiguous();
+        assert_eq!(b2.strides, [2, 1]);
+    }
 }


Test patch: