Skip to content

Instantly share code, notes, and snippets.

View chenyaofo's full-sized avatar
😃
I may be slow to respond.

chenyaofo chenyaofo

😃
I may be slow to respond.
View GitHub Profile
@chenyaofo
chenyaofo / asyncio_read.py
Created May 31, 2023 09:37
Async reading multiple files.
import asyncio
import aiofiles
tar_filenames = [f"/home/chenyaofo/datasets/imagenet-wds/train/{i:06d}.tar" for i in range(256)]
# tar_filenames = [f"/gpfs01/home/chenyaofo/imagenet-wds/train/{i:06d}.tar" for i in range(256)]
count = 0
def async_reading():
print("asyncio reading based on naive asyncio")
@chenyaofo
chenyaofo / Dockerfile
Last active April 12, 2023 12:04
PyTorch 2,0 Docker
FROM nvidia/cuda:11.8.0-devel-ubuntu22.04
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 \
PATH=/opt/conda/bin:$PATH \
PYTHON_VERSION=3.10
RUN APT_INSTALL="apt-get install -y --no-install-recommends --no-install-suggests" && \
GIT_CLONE="git clone --depth 10" && \
rm -rf /etc/apt/sources.list.d/cuda.list \
@chenyaofo
chenyaofo / Dockerfile-PyTorch-github-action
Last active April 2, 2023 15:02
Dockerfile for build PyTorch via Github Actions
FROM nvidia/cuda:11.8.0-devel-ubuntu22.04
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 \
PATH=/opt/conda/bin:$PATH \
TZ=Asia/Shanghai \
PYTHON_VERSION=3.9
RUN APT_INSTALL="apt-get install -y --no-install-recommends --no-install-suggests" && \
GIT_CLONE="git clone --depth 10" && \
rm -rf /etc/apt/sources.list.d/cuda.list \
@chenyaofo
chenyaofo / load_image.py
Created March 15, 2023 13:07
Pytorch Data load tfrecord
import torch
import torchvision.transforms as transforms
from PIL import Image
from io import BytesIO
from torchdata.datapipes.iter import FileLister, FileOpener, TFRecordLoader, Mapper, Shuffler, Batcher, Collator, ShardingFilter
from torchdata.dataloader2 import adapter, DataLoader2, PrototypeMultiProcessingReadingService
from codebase.torchutils.serialization import jsonunpack
from torch.utils.data import DataLoader
def get_train_transforms():
@chenyaofo
chenyaofo / tbr_v2.py
Last active December 20, 2022 07:11
Solution of TBR.
import torch
import torch.nn as nn
from torchvision.models import resnet50
import types
import copy
def tbr_bn_forward_impl(self: nn.BatchNorm2d, x: torch.Tensor):
batch_var, batch_mean = torch.var_mean(x, dim=(0, 2, 3), keepdim=True)
batch_std = torch.sqrt(batch_var+self.eps)
@chenyaofo
chenyaofo / tbr.py
Created December 19, 2022 11:14
Question about TBR.
import torch
import torch.nn as nn
from torchvision.models import resnet50
import types
import copy
def tbr_bn_forward_impl(self: nn.BatchNorm2d, x: torch.Tensor):
# print("x.shape", x.shape)
batch_mean = x.mean(dim=(0, 2, 3), keepdim=True)
FROM nvcr.io/nvidia/pytorch:22.11-py3
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 \
TZ=Asia/Shanghai
RUN APT_INSTALL="apt-get install -y --no-install-recommends --no-install-suggests" && \
GIT_CLONE="git clone --depth 10" && \
mirror_url=mirrors.cloud.tencent.com && \
sed -i "s/security.ubuntu.com/$mirror_url/" /etc/apt/sources.list && \
sed -i "s/archive.ubuntu.com/$mirror_url/" /etc/apt/sources.list && \
@chenyaofo
chenyaofo / scientific_figure.py
Created December 5, 2022 12:48
Scientific Figure Codebases.
import torch
from typing import List, Tuple
import matplotlib.pyplot as plt
from matplotlib import ticker
from matplotlib import font_manager
# more details about matplotlib usage at
# https://wizardforcel.gitbooks.io/matplotlib-user-guide/content/
def color(*items):
@chenyaofo
chenyaofo / preprocessing_benchmark.py
Created November 14, 2022 13:16
A benchmark for image preprocessing.
import time
import os
import warnings
import pathlib
try:
import nvidia.dali.types as types
import nvidia.dali.fn as fn
from nvidia.dali.plugin.pytorch import DALIGenericIterator, LastBatchPolicy
from nvidia.dali.pipeline import Pipeline
@chenyaofo
chenyaofo / main.py
Last active November 6, 2022 14:21
Train ResNet50+Layernorm
import argparse
import os
import random
import shutil
import time
import warnings
import torch
import torch.nn as nn
import torch.nn.parallel