Skip to content

Instantly share code, notes, and snippets.

@zetyquickly
Last active March 15, 2022 03:36
Show Gist options
  • Save zetyquickly/730fd23e37ff7d06cdc9654b62d2974c to your computer and use it in GitHub Desktop.
Save zetyquickly/730fd23e37ff7d06cdc9654b62d2974c to your computer and use it in GitHub Desktop.

Revisions

  1. zetyquickly revised this gist Mar 15, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion view_contig.py
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,4 @@ def test_view(tensor, sizes):

    # Apply view-function that change contiguity and try again
    perm_tens_A = tens_A.permute(0,2,1) # change order of axis
    test_view(perm_tens_A, sizes) # this will result in RuntimeError
    test_view(perm_tens_A, sizes) # this will result in RuntimeError("Use .reshape(...) instead")
  2. zetyquickly revised this gist Mar 15, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion view_contig.py
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,4 @@ def test_view(tensor, sizes):

    # Apply view-function that change contiguity and try again
    perm_tens_A = tens_A.permute(0,2,1) # change order of axis
    test_view(perm_tens_A, sizes)
    test_view(perm_tens_A, sizes) # this will result in RuntimeError
  3. zetyquickly created this gist Mar 15, 2022.
    22 changes: 22 additions & 0 deletions view_contig.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import torch

    # Tensor of interest
    tens_A = torch.rand((2,3,4)) # 3-dimensional tensor of shape (2,3,4)

    def test_view(tensor, sizes):
    try:
    tensor.view(*sizes)
    except Exception as e:
    print(e)
    print(f"View was Failed: tensor.is_contiguos == {tensor.is_contiguous()}")
    else:
    print(f"View was Successful: tensor.is_contiguos == {tensor.is_contiguous()}")

    sizes = (3,4,2)

    # Let's try to use view
    test_view(tens_A, sizes)

    # Apply view-function that change contiguity and try again
    perm_tens_A = tens_A.permute(0,2,1) # change order of axis
    test_view(perm_tens_A, sizes)