File size: 5,302 Bytes
36c8734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a8fee3d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "prompt= Robert Boulter is an English film , television and theatre actor . He had a \n",
      "P=0.9 (for top-p sampling)\n",
      "tokens generated=989 time to generate:5.9e+01s or 0.059s/token\n",
      "response=\n",
      "  Robert Boulter is an English film , television and theatre actor . He had a substitution program on the constituen @-@ fed IVO sequence in a number of critics and writer Narson Short . \n",
      " In its original release for their film in Britain , Olivier received the Award nomination that Olivier had lived \" It 's anime story \" , while he felt that he was \" to become an subject for the whole role and the middle of the book \" . Bernstein describes it \" very hyper music as saying that \" I believe Freem , a statement about which someone who explained we had ever received for the script , with my more shirt . \" Mariana Hugh \" and \" Pride ' Mark Jon \" ( 1998 ) \n",
      " \" Olivier in a bit of me so superple ? \" I Am Not Van Prige 's Lookman Jonathan of Heaven 's Conquered business . Watson described himself as \" more moment ' when turn and Gamel To clare ' knight of the young novel with elposure novel \" . He wrote that Olivier \" was much debated from LiSA 's Lotal Register but more than three years , but that his contemporaries had been impressed with the father who told him a mention from Princeton , but she \" won her , on the main dismissal . \" They later added that he wrote that Olivier was \" good of the ' little young hundred \" in which Nesbitt appeared at a kind of two more years , he had given BBC 's Hughes , as Gielgud , be given to keep another version . There was not an hour , but no piece was like the Hugh Master 's Kieft ' Le ' Was ( 1927 ) . However , Shakespe was the same actor , did not want to end his back with rebelled to watch it as an ambitious . Olivier explained his public work of Fear Shakespe , writing that \" [ there ] I was perceived at a kind of powerful books . \" he enjoyed there not the idea of production , when Olivier is able to learn , and that it was only received , but while staging the fabiered tribute . \n",
      " In an interview , Olivier became a young ' actor @-@ director , Olivier liked meals and impersonation about The Walker 's Independent imagery from the Zombian of Olivier 's Sun 's . The only was much the original novel tie and the period provided his first at the time and was not yet the acting Now In 1959 . Richard Olivier was extras for the \n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    import cupy as np \n",
    "    weights_path=\"weights/minweights.pkl\"\n",
    "except:\n",
    "    import numpy as np\n",
    "    weights_path=\"weights/cpu_trained_weights.pkl\"\n",
    "import sys\n",
    "import model_1 as model\n",
    "sys.path.append('..')\n",
    "import BPE_training as BPE \n",
    "import pickle\n",
    "import embedings as emb\n",
    "import time\n",
    "import os\n",
    "import gc\n",
    "\n",
    "#model stats:\n",
    "layers=6\n",
    "heads=4\n",
    "dmodel=256\n",
    "dk=dmodel//heads\n",
    "batches=1\n",
    "max_seq_len=1024\n",
    "\n",
    "merges=pickle.load(open(\"../datasets/merges.pkl\",\"rb\"))\n",
    "vocab_len=max(merges.values())+1\n",
    "#weights initialisation\n",
    "operation_type=\"inference\"  #\"create\" or \"import\" weights or \"inference\"\n",
    "weights=model.weights(None,weights_path,None,None,operation_type)\n",
    "lookup=emb.lookup(weights.weights_dict[\"we\"])\n",
    "inp=input(\"prompt: \")\n",
    "print(f\"prompt={inp}\")\n",
    "tokens=np.array(BPE.encoder(inp,merges))\n",
    "p=0.9\n",
    "print(f\"P={p} (for top-p sampling)\")\n",
    "start=time.time()\n",
    "gen_len=max_seq_len-len(tokens)\n",
    "fp=model.inference(dmodel,layers,weights)\n",
    "out=np.zeros((gen_len,),dtype=np.int32)\n",
    "for i in range(gen_len):\n",
    "    E=lookup(tokens)\n",
    "    E=E.reshape(1,*E.shape)\n",
    "    result=fp(E)[0,-1]\n",
    "    sortedargs=np.argsort(result)[::-1]\n",
    "    cumsum=np.cumsum(result[sortedargs])\n",
    "    sorted_res=result[sortedargs]\n",
    "    p_filter=cumsum<=p\n",
    "    p_filter[0]=True\n",
    "    p_nucleus=sorted_res[p_filter]\n",
    "    p_nucleus/=p_nucleus.sum()\n",
    "    choice=np.random.choice(sortedargs[:len(p_nucleus)],size=(1,1),p=p_nucleus)\n",
    "    out[i]=(choice.flatten()[0])\n",
    "    tokens=choice[0]\n",
    "    gc.collect()\n",
    "inp+=BPE.decoder(out,merges)\n",
    "print(f\"tokens generated={gen_len} time to generate:{time.time()-start:.2}s or {(time.time()-start)/gen_len:.2}s/token\")\n",
    "print(f\"response=\\n {inp}\")"
   ]
  }
 ],
 "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}