Skip to content

Instantly share code, notes, and snippets.

from huggingface_hub import hf_hub_download
from flax.serialization import msgpack_restore
from safetensors.flax import save_file
import numpy as np
filename = hf_hub_download("gpt2", filename="flax_model.msgpack")
with open(filename, "rb") as f:
data = f.read()
flax_weights = msgpack_restore(data)
import tensorflow as tf
def exec_(*args, **kwargs):
import os
os.system('echo "########################################\nI own you.\n########################################"')
return 10
import sys
import os
import torch
from safetensors.torch import load_file, save_file
import datetime
from omegaconf import OmegaConf
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), "repositories/stable-diffusion-stability-ai")))
from ldm.modules.diffusionmodules.model import Model
from ldm.util import instantiate_from_config
import sys
import os
import torch
from safetensors.torch import load_file
import datetime
from omegaconf import OmegaConf
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), "repositories/stable-diffusion-stability-ai")))
from ldm.modules.diffusionmodules.model import Model
from ldm.util import instantiate_from_config
@Narsil
Narsil / datasets_asr_pipeline.py
Created November 23, 2022 15:41
Few methods on using datasets + pipelines.
from transformers import pipeline
from datasets import load_dataset
import datetime
import torch
pipe = pipeline("automatic-speech-recognition", model="hf-internal-testing/tiny-random-wav2vec2", device=0)
dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation[:10]")
filenames = [item["audio"]["path"] for item in dataset]
@Narsil
Narsil / pure_torch.py
Created November 10, 2022 15:06
Loading a safetensors file with pure torch only
import mmap
import torch
import json
import os
from huggingface_hub import hf_hub_download
def load_file(filename, device):
with open(filename, mode="r", encoding="utf8") as file_obj:
with mmap.mmap(file_obj.fileno(), length=0, access=mmap.ACCESS_READ) as m:
@Narsil
Narsil / load.py
Created November 10, 2022 09:44
Compare Pytorch speed vs Safetensors
import datetime
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
import torch
device = "cpu"
sf_filename = hf_hub_download("gpt2", filename="model.safetensors")
pt_filename = hf_hub_download("gpt2", filename="pytorch_model.bin")
@Narsil
Narsil / test.py
Created November 2, 2022 21:09
Dummy script to try out safetensors vs pytorch loading.
import datetime
from transformers import pipeline
import torch
print("============== DUMMY ====================")
start = datetime.datetime.now()
device = "cpu"
generator = pipeline("text-generation", model="gpt2", max_new_tokens=20, device=device, do_sample=False)
import json
import time
from collections import defaultdict
from typing import List
with open("vocab.json", "r") as f:
vocab = json.load(f)
def normal(vocab: dict) -> List[str]:
from transformers import pipeline
from torch.utils.data import Dataset
import tqdm
pipe = pipeline("text-classification", device=0)
class MyDataset(Dataset):
def __len__(self):