Accelerate Pytorch

Use Pytorch to accelerate with GPU or apple silicon with Metal

sequenceDiagram
    participant 1D as 1D Tensor
    participant 2D as 2D Tensor
    participant 3D as 3D Tensor
    
    Note over 1D: Start: tensor([1, 2, 3, 4])<br>Shape: [4]

    rect rgb(230, 245, 255)
        Note over 1D,3D: 1D → 2D Transformations
        1D->>2D: unsqueeze(0)
        Note right of 2D: [1, 4]: [[1, 2, 3, 4]]
        1D->>2D: view(2, 2)
        Note right of 2D: [2, 2]: [[1, 2], [3, 4]]
        1D->>2D: reshape(2, 2)
        Note right of 2D: [2, 2]: [[1, 2], [3, 4]]
    end

    rect rgb(245, 230, 255)
        Note over 1D,3D: 2D → 3D Transformations
        Note over 2D: From: tensor([[1, 2], [3, 4]])<br>Shape: [2, 2]
        2D->>3D: unsqueeze(0)
        Note right of 3D: [1, 2, 2]: [[[1, 2], [3, 4]]]
        2D->>3D: view(1, 2, 2)
        Note right of 3D: [1, 2, 2]: [[[1, 2], [3, 4]]]
        2D->>3D: expand(2, 2, 2)
        Note right of 3D: [2, 2, 2]: Broadcasts data
    end

    rect rgb(255, 230, 230)
        Note over 1D,3D: Reverse Transformations
        3D->>2D: squeeze(0)
        Note right of 2D: [2, 2]: [[1, 2], [3, 4]]
        2D->>1D: flatten()
        Note right of 1D: [4]: [1, 2, 3, 4]
    end

    Note over 1D,3D: All Available PyTorch Tensor Commands:

    rect rgb(240, 255, 240)
        Note over 1D,3D: Basic Dimension Commands
        Note right of 3D: unsqueeze(dim)<br>squeeze(dim)<br>squeeze()<br>flatten(start_dim, end_dim)<br>view(*shape)<br>view_as(other)<br>reshape(*shape)<br>reshape_as(other)<br>expand(*sizes)<br>expand_as(other)
    end

    rect rgb(255, 240, 220)
        Note over 1D,3D: Dimension Reordering
        Note right of 3D: t()<br>transpose(dim0, dim1)<br>permute(*dims)<br>movedim(source, destination)<br>swapaxes(dim0, dim1)<br>swapdims(dim0, dim1)
    end

    rect rgb(230, 245, 255)
        Note over 1D,3D: Splitting & Combining
        Note right of 3D: chunk(chunks, dim)<br>split(split_size, dim)<br>split_with_sizes(split_sizes, dim)<br>stack(tensors, dim)<br>cat(tensors, dim)<br>unbind(dim)
    end

    rect rgb(245, 230, 255)
        Note over 1D,3D: Indexing & Selection
        Note right of 3D: narrow(dim, start, length)<br>select(dim, index)<br>index_select(dim, index)<br>masked_select(mask)<br>take(index)<br>take_along_dim(indices, dim)<br>gather(dim, index)<br>scatter(dim, index, src)
    end

    rect rgb(255, 230, 230)
        Note over 1D,3D: Advanced Operations
        Note right of 3D: unfold(dimension, size, step)<br>diagonal(offset, dim1, dim2)<br>as_strided(size, stride)<br>roll(shifts, dims)<br>tile(*reps)<br>repeat(*sizes)<br>repeat_interleave(repeats, dim)
    end

    rect rgb(230, 255, 230)
        Note over 1D,3D: Memory & Layout
        Note right of 3D: contiguous()<br>clone()<br>detach()<br>to(device)<br>cpu()<br>cuda()<br>pin_memory()
    end

    rect rgb(255, 255, 230)
        Note over 1D,3D: Shape Inspection
        Note right of 3D: size()<br>dim()<br>ndim<br>numel()<br>shape<br>stride()<br>storage_offset()<br>is_contiguous()
    end

    Note over 1D,3D: Common Combined Operations:<br>x.view(-1).unsqueeze(0)  # Flatten and add batch dim<br>x.permute(0,2,1).contiguous()  # Reorder and optimize<br>x.squeeze().view(-1)  # Remove singleton dims and flatten

Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *