Skip to content

Instantly share code, notes, and snippets.

View pszemraj's full-sized avatar

Peter pszemraj

View GitHub Profile
@pszemraj
pszemraj / local_pastebin.py
Last active March 13, 2024 00:20
local network pastebin server for copy/pasting betwixt 2+ computers
import json
import socket
import uuid
import yake
from flask import Flask, redirect, render_template_string, request, url_for
from markupsafe import escape
app = Flask(__name__)
@pszemraj
pszemraj / enable_tf32.py
Last active March 24, 2024 05:00
modern way to auto enable tf32
import torch
import logging
def check_ampere_gpu():
"""
Check if the GPU supports NVIDIA Ampere or later and enable FP32 in PyTorch if it does.
"""
# Check if CUDA is available
if not torch.cuda.is_available():
@pszemraj
pszemraj / test_textsumdir_ipex.py
Created February 21, 2024 21:40
textsum - run summarization on directory on CPU with IPEX optimization
"""
cli.py - Command line interface for textsum.
this edition: fast CPU inference with intel IPEX https://archive.ph/oY5b1
Usage:
textsum-dir --help
"""
import os
@pszemraj
pszemraj / run_gauntlet_vs_gpt4.py
Last active February 18, 2024 00:49
evaluate a text2text summarization model on cpu on 'the gauntlet' -rouge vs GPT4
import json
import logging
import re
from datetime import datetime
from pathlib import Path
import datasets
import evaluate
import fire
import intel_extension_for_pytorch as ipex
@pszemraj
pszemraj / run_classification.py
Created February 16, 2024 16:08
a less bad version of the hf run_classification script
#!/usr/bin/env python
# coding=utf-8
# Copyright 2020 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
#
"""Summary
"""
import logging
from pathlib import Path
import fire
from datasets import Dataset, load_dataset
from tqdm.auto import tqdm
from transformers import AutoTokenizer
@pszemraj
pszemraj / update_tokenizer_processing.py
Created February 11, 2024 11:02
update tokenizer.json to use postprocessing similar to BERT's
from pathlib import Path
import json
def update_tokenizer_post_processor(input_path):
"""
Load a tokenizer configuration from the input path, update its post_processor
with a custom TemplateProcessing configuration, and overwrite the original file.
Args:
@pszemraj
pszemraj / embed.py
Last active February 2, 2024 16:18
setting up nomic-embed-text-v1 in sbert and ONNX
# pip install sentence-transformers
from sentence_transformers import SentenceTransformer, util, models
model_name = "nomic-ai/nomic-embed-text-v1"
pooling_mode = "mean"
word_embedding_model = models.Transformer(
model_name,
max_seq_length=8192,
model_args={"trust_remote_code": True, "rotary_scaling_factor": 2},
tokenizer_args={"trust_remote_code": True},
@pszemraj
pszemraj / distract.py
Created February 1, 2024 20:56
download and run this periodically during setup so Colab doesn't whine about you not using the GPU
# pip install sentence-transformers -q
# source: https://www.sbert.net/docs/usage/semantic_textual_similarity.html
from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer("all-MiniLM-L6-v2")
# Two lists of sentences
sentences1 = [
"The cat sits outside",
@pszemraj
pszemraj / load_and_ensure_tokens.py
Last active January 17, 2024 02:36
loads a Hugging Face Transformers tokenizer, checks for essential special tokens, adds them if necessary
from transformers import AutoTokenizer
def load_and_ensure_tokens(model_name):
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Essential special tokens with their default values
essential_tokens = {
"pad_token": "<pad>",