Skip to content

Instantly share code, notes, and snippets.

View younesbelkada's full-sized avatar
:octocat:
Working from home

Younes Belkada younesbelkada

:octocat:
Working from home
View GitHub Profile
@younesbelkada
younesbelkada / get_accelerate.py
Created May 21, 2023 18:14
A gist to get the module names of all architectures in `transformers` that supports `accelerate`
import transformers
import transformers.models as models
models_that_support_accelerate = []
for model in dir(models):
if model[0] != '_':
model_module = getattr(models, model)
if hasattr(model_module, "modeling_" + model):
modeling_module = getattr(model_module, "modeling_" + model)
@younesbelkada
younesbelkada / finetune_sft_trl.py
Last active March 4, 2024 22:00
Benchmarking SFT trainer with 8bit models
# coding=utf-8
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@younesbelkada
younesbelkada / toxicity-eval.py
Created February 22, 2023 08:03
evaluate the toxicity of detoxified models
import numpy as np
import csv
import argparse
from tqdm import tqdm
import torch
import evaluate
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
import tensorflow as tf
import torch
import torch.nn.functional as F
# adapted from: https://discuss.pytorch.org/t/tf-extract-image-patches-in-pytorch/43837/8
def torch_extract_patches(
x, patch_height, patch_width, padding=None
):
@younesbelkada
younesbelkada / torch-tf-patches.py
Created January 30, 2023 18:57
from `tf` to `torch` extract to patches
import tensorflow as tf
import torch
import math
import torch.nn.functional as F
# adapted from: https://discuss.pytorch.org/t/tf-extract-image-patches-in-pytorch/43837/8
def torch_extract_patches(
x, patch_height, patch_width, padding=None
@younesbelkada
younesbelkada / scratch_cppe5.py
Created December 27, 2022 19:32
utility script to evaluate an OD model on a custom dataset - cppe5
import os
import json
import evaluate
import torch
import torchvision
import numpy as np
from tqdm import tqdm
from PIL import Image
from transformers import DetrFeatureExtractor, DetrForObjectDetection, DetrImageProcessor
@younesbelkada
younesbelkada / scratch.py
Last active December 27, 2022 14:55
Evaluate HF object detection models on COCO using `evaluate`
import os
import evaluate
import torch
import torchvision
from tqdm import tqdm
from transformers import DetrFeatureExtractor, DetrForObjectDetection
COCO_DIR = os.path.join(os.getcwd(), "data")
path_img = os.path.join(COCO_DIR, "val2017")
path_anno = os.path.join(COCO_DIR, "annotations/instances_val2017.json")
@younesbelkada
younesbelkada / convert_t5x_to_pt.py
Created December 22, 2022 22:07
Handy script to convert any `t5x` checkpoint to an un-nested dictionary
from t5x import checkpoints
from flax.traverse_util import flatten_dict, unflatten_dict
def rename_keys(key):
if "kernel" in key:
key = key.replace("kernel", "weight")
return key
flax_checkpoint_path = "/home/younes_huggingface_co/code/pix2struct/pix2struct_base"
@younesbelkada
younesbelkada / setup-m1.py
Created December 8, 2022 10:55
How to install `transformers` `dev` environment in Mac M1
# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,

bert-base-uncased - A100 - half - cuda

batch_size seq_len pad_percentage HF_time BT_time Speedup
8 64 0 0.012947250976562501 0.007366779174804688 1.7575185395598296
8 64 0.1 0.012887490234375 0.0072092669677734375 1.7876283805252486
8 64 0.2 0.012887449951171874 0.007392645263671876 1.7432799074645124
8 64 0.5 0.012756295166015626 0.007139061889648438 1.7868307297506574
8 64 0.75 0.0132109521484375 0.007268699951171876 1.8175123800931694
8 128 0 0.012552437744140625 0.0075138049316406256 1.6705833939449668