Skip to content

Instantly share code, notes, and snippets.

@qsh-zh
qsh-zh / torch_activation_checkpointing.md
Created February 24, 2025 10:38
torch_activation_checkpointing

Tutorial: PyTorch Gradient Checkpointing with Detailed Torch Background and Implementation

In this tutorial, we'll dive deep into gradient checkpointing in PyTorch, a memory optimization technique for neural network training. We'll cover its purpose, mechanics, and provide detailed insights into PyTorch's internal implementation, including advanced features like early stopping and selective checkpointing. Additionally, we'll address the user's specific requests for background on Torch operations, Torch dispatch, and detailed implementation analysis. Practical examples, flowcharts, and comprehensive explanations are included to ensure clarity.

Introduction to Gradient Checkpointing

Gradient checkpointing reduces memory consumption during the training of deep neural networks. Normally, during the forward pass, all intermediate activations are stored in memory for gradient computation in the backward pass. This can lead to high memory usage, especially for large models. Gradient checkpointing addresses t

@qsh-zh
qsh-zh / mstd.py
Last active September 26, 2022 23:09
mean std error
from collections import defaultdict
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.lines import Line2D
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.markersize'] = 6
mpl.rcParams['lines.markersize'] = 6
mpl.rcParams['legend.fontsize'] = 15 #25
@qsh-zh
qsh-zh / desktop.md
Last active May 4, 2024 15:45
Install Software
@qsh-zh
qsh-zh / torch_jacobian.py
Created May 2, 2020 03:48 — forked from sbarratt/torch_jacobian.py
Get the jacobian of a vector-valued function that takes batch inputs, in pytorch.
def get_jacobian(net, x, noutputs):
x = x.squeeze()
n = x.size()[0]
x = x.repeat(noutputs, 1)
x.requires_grad_(True)
y = net(x)
y.backward(torch.eye(noutputs))
return x.grad.data
@qsh-zh
qsh-zh / abc.sh
Last active February 29, 2020 20:10
remote communicate with unit socket and service
echo "$@" | xclip -selection primary
echo "$@" | xclip -selection clipboard
@qsh-zh
qsh-zh / cuda_10.0_installation_on_Ubuntu_18.04
Created September 17, 2019 21:06 — forked from Mahedi-61/cuda_11.8_installation_on_Ubuntu_22.04
Cuda 10.0 complete installation procedure for ubuntu 18.04 LTS
#!/bin/bash
## This gist contains step by step instructions to install cuda v10.0 and cudnn 7.5 in Ubuntu 18.04
### steps ####
# verify the system has a cuda-capable gpu
# download and install the nvidia cuda toolkit and cudnn
# setup environmental variables
# verify the installation
###
@qsh-zh
qsh-zh / install-cuda-10-bionic.sh
Created September 17, 2019 19:32 — forked from bogdan-kulynych/install-cuda-10-bionic.sh
Install CUDA 10 on Ubuntu 18.04
#!/bin/bash
# Purge existign CUDA first
sudo apt --purge remove "cublas*" "cuda*"
sudo apt --purge remove "nvidia*"
# Install CUDA Toolkit 10
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub && sudo apt update
sudo dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
@qsh-zh
qsh-zh / core dump
Created July 13, 2019 13:52
debug gdb
ulimit -c unlimited
set pagination off
catch throw
commands
backtrace
continue
end
run
@qsh-zh
qsh-zh / install_opencv.sh
Created April 1, 2019 03:04 — forked from nip10/install_opencv.sh
Install OpenCV on Ubuntu
# Originally at https://github.com/milq/milq/blob/master/scripts/bash/install-opencv.sh
######################################
# INSTALL OPENCV ON UBUNTU OR DEBIAN #
######################################
# | THIS SCRIPT IS TESTED CORRECTLY ON |
# |------------------------------------------------------|
# | OS | OpenCV | Test | Last test |
# |------------------|--------------|------|-------------|
# | Ubuntu 18.04 LTS | OpenCV 3.4.2 | OK | 18 Jul 2018 |
# Make sure you grab the latest version
curl -OL https://github.com/google/protobuf/releases/download/v3.2.0/protoc-3.2.0-linux-x86_64.zip
# Unzip
unzip protoc-3.2.0-linux-x86_64.zip -d protoc3
# Move protoc to /usr/local/bin/
sudo mv protoc3/bin/* /usr/local/bin/
# Move protoc3/include to /usr/local/include/