Skip to content

Instantly share code, notes, and snippets.

@yashbonde
Created July 27, 2021 13:54
Show Gist options
  • Select an option

  • Save yashbonde/06b78978ded892569ca5c015f01c6516 to your computer and use it in GitHub Desktop.

Select an option

Save yashbonde/06b78978ded892569ca5c015f01c6516 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "9948a78a",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"import torch\n",
"import numpy as np\n",
"from tqdm import trange"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33b88394",
"metadata": {},
"outputs": [],
"source": [
"def set_seed(seed: int):\n",
" import random, numpy as np, torch\n",
" random.seed(seed)\n",
" np.random.seed(seed)\n",
" torch.manual_seed(seed)\n",
" torch.cuda.manual_seed_all(seed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9486931",
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"name = \"EleutherAI/gpt-neo-2.7B\"\n",
"# get the model and tokenizer,\n",
"# EleutherAI/gpt-neo-2.7B = 9.9GB compressed\n",
"# gpt2-xl (1.5Bn Params) = 6.7GB compressed\n",
"# always cache these models, reduces useless bandwidth.\n",
"tokenizer = AutoTokenizer.from_pretrained(name, cache_dir = \"../hf-cache/\")\n",
"model = AutoModelForCausalLM.from_pretrained(name, cache_dir = \"../hf-cache/\")\n",
"print(\"Model loaded ..., moving to GPU\")\n",
"device = torch.device(\"cuda:0\") if torch.cuda.is_available() else \"CPU\"\n",
"model = model.to(device).eval()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61776ea4",
"metadata": {},
"outputs": [],
"source": [
"model.device"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffa55b4a",
"metadata": {},
"outputs": [],
"source": [
"from gpt import GPT\n",
"gpt = GPT(model, tokenizer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee5a26a3",
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"gpt(\"Hello World!\", r = 4, n = 10, temp = 0.9, top_p = 1.0)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment