CoolFace
Modelpublic

BarinkDev/LargeLanguageModels

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
OpenCoder.ipynb122 linesDownload Raw Back to root
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 1,6   "id": "8ada5e87-9fec-4872-8f32-986e9633f574",7   "metadata": {},8   "outputs": [],9   "source": [10    "# IMPORTS \n",11    "import torch\n",12    "from transformers import AutoTokenizer, AutoModelForCausalLM"13   ]14  },15  {16   "cell_type": "code",17   "execution_count": 2,18   "id": "02787ef8-d1bf-45a4-b514-441ffa60e742",19   "metadata": {},20   "outputs": [],21   "source": [22    "model_name = \"infly/OpenCoder-1.5B-Instruct\"\n",23    "model = AutoModelForCausalLM.from_pretrained(model_name,\n",24    "                                                        torch_dtype = torch.bfloat16,\n",25    "                                                         device_map=\"auto\",\n",26    "                                                         trust_remote_code=True)\n",27    "tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\n"28   ]29  },30  {31   "cell_type": "code",32   "execution_count": 3,33   "id": "01e33504-9263-4b0d-a122-3d6cd0ec6de5",34   "metadata": {},35   "outputs": [],36   "source": [37    "messages=[\n",38    "   { 'role': 'user', 'content': 'write a quick sort algorithm in python'}  \n",39    "]\n",40    "\n",41    "inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors=\"pt\")"42   ]43  },44  {45   "cell_type": "code",46   "execution_count": 4,47   "id": "49439016-e992-40d6-b3ed-58558d71abe9",48   "metadata": {},49   "outputs": [50    {51     "name": "stderr",52     "output_type": "stream",53     "text": [54      "The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n",55      "Setting `pad_token_id` to `eos_token_id`:None for open-end generation.\n",56      "The attention mask is not set and cannot be inferred from input because pad token is same as eos token. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n"57     ]58    },59    {60     "name": "stdout",61     "output_type": "stream",62     "text": [63      "Here is a simple implementation of the quick sort algorithm in Python:\n",64      "\n",65      "```python\n",66      "def quick_sort(arr):\n",67      "    if len(arr) <= 1:\n",68      "        return arr\n",69      "    else:\n",70      "        pivot = arr[len(arr) // 2]\n",71      "        left = [x for x in arr if x < pivot]\n",72      "        middle = [x for x in arr if x == pivot]\n",73      "        right = [x for x in arr if x > pivot]\n",74      "        return quick_sort(left) + middle + quick_sort(right)\n",75      "\n",76      "print(quick_sort([3,6,8,10,1,2,1]))\n",77      "```\n",78      "\n",79      "This quick sort algorithm works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted.\n",80      "\n"81     ]82    }83   ],84   "source": [85    "outputs = model.generate(inputs, max_new_tokens=512, do_sample=False)\n",86    "\n",87    "result = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)\n",88    "print(result)"89   ]90  },91  {92   "cell_type": "code",93   "execution_count": null,94   "id": "d5b4a28b-664d-4329-832e-4f1a684b4bd7",95   "metadata": {},96   "outputs": [],97   "source": []98  }99 ],100 "metadata": {101  "kernelspec": {102   "display_name": "Python 3 (ipykernel)",103   "language": "python",104   "name": "python3"105  },106  "language_info": {107   "codemirror_mode": {108    "name": "ipython",109    "version": 3110   },111   "file_extension": ".py",112   "mimetype": "text/x-python",113   "name": "python",114   "nbconvert_exporter": "python",115   "pygments_lexer": "ipython3",116   "version": "3.12.9"117  }118 },119 "nbformat": 4,120 "nbformat_minor": 5121}122