diff --git a/site/en/gemma/docs/codegemma/code_assist_keras.ipynb b/site/en/gemma/docs/codegemma/code_assist_keras.ipynb index 65119e45a..f49450c3f 100644 --- a/site/en/gemma/docs/codegemma/code_assist_keras.ipynb +++ b/site/en/gemma/docs/codegemma/code_assist_keras.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "cellView": "form", "id": "tuOe1ymfHZPu" @@ -47,7 +47,7 @@ "id": "SDEExiAk4fLb" }, "source": [ - "# AI Assisted programming with CodeGemma and KerasNLP" + "# AI Assisted programming with CodeGemma and Keras Hub" ] }, { @@ -149,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "id": "DrBoa_Urw9Vx" }, @@ -173,32 +173,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "id": "ACnm31nfBqHj" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m508.4/508.4 kB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m950.8/950.8 kB\u001b[0m \u001b[31m18.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.2/5.2 MB\u001b[0m \u001b[31m51.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m589.8/589.8 MB\u001b[0m \u001b[31m1.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.8/4.8 MB\u001b[0m \u001b[31m59.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m32.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.5/5.5 MB\u001b[0m \u001b[31m42.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m45.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m311.2/311.2 kB\u001b[0m \u001b[31m38.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", - "tf-keras 2.15.1 requires tensorflow<2.16,>=2.15, but you have tensorflow 2.16.1 which is incompatible.\u001b[0m\u001b[31m\n", - "\u001b[0m" - ] - } - ], + "outputs": [], "source": [ - "!pip install -q -U keras-nlp" + "!pip install -q -U keras_hub" ] }, { @@ -223,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "id": "ww83zI9ToPso" }, @@ -240,18 +221,18 @@ "source": [ "### Import packages\n", "\n", - "Import Keras and KerasNLP." + "Import Keras and Keras Hub." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "id": "oQkqsyE1a2YD" }, "outputs": [], "source": [ - "import keras_nlp\n", + "import keras_hub\n", "import keras\n", "\n", "# Run at half precision.\n", @@ -277,7 +258,7 @@ "source": [ "### Load the model\n", "\n", - "KerasNLP provides implementations of all three CodeGemma variants (2B and 7B pre-trained (PT) and 7B instruction-tuned (IT)) using [`GemmaCausalLM`](https://keras.io/api/keras_nlp/models/gemma/gemma_causal_lm/){:.external}, an end-to-end Gemma model for causal language modeling. A causal language model predicts the next token based on previous tokens." + "Keras Hub provides implementations of all three CodeGemma variants (2B and 7B pre-trained (PT) and 7B instruction-tuned (IT)) using [`GemmaCausalLM`](https://keras.io/api/keras_nlp/models/gemma/gemma_causal_lm/){:.external}, an end-to-end Gemma model for causal language modeling. A causal language model predicts the next token based on previous tokens." ] }, { @@ -291,86 +272,174 @@ }, { "cell_type": "code", - "execution_count": null, + "source": [ + "!pip install -U keras" + ], + "metadata": { + "id": "AJU3alRDWzkT", + "outputId": "f5616f63-6713-4585-b87b-f175b98d6293", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: keras in /usr/local/lib/python3.11/dist-packages (3.10.0)\n", + "Requirement already satisfied: absl-py in /usr/local/lib/python3.11/dist-packages (from keras) (1.4.0)\n", + "Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from keras) (2.0.2)\n", + "Requirement already satisfied: rich in /usr/local/lib/python3.11/dist-packages (from keras) (13.9.4)\n", + "Requirement already satisfied: namex in /usr/local/lib/python3.11/dist-packages (from keras) (0.1.0)\n", + "Requirement already satisfied: h5py in /usr/local/lib/python3.11/dist-packages (from keras) (3.13.0)\n", + "Requirement already satisfied: optree in /usr/local/lib/python3.11/dist-packages (from keras) (0.16.0)\n", + "Requirement already satisfied: ml-dtypes in /usr/local/lib/python3.11/dist-packages (from keras) (0.4.1)\n", + "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from keras) (24.2)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in /usr/local/lib/python3.11/dist-packages (from optree->keras) (4.13.2)\n", + "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/dist-packages (from rich->keras) (3.0.0)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.11/dist-packages (from rich->keras) (2.19.1)\n", + "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/dist-packages (from markdown-it-py>=2.2.0->rich->keras) (0.1.2)\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": 8, "metadata": { - "id": "SQ6Yr7c4GKTx" + "id": "SQ6Yr7c4GKTx", + "outputId": "e1d9c345-6ede-4563-8fd8-4097a857f3e5", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_7b_en/3/download/model.weights.json...\n" + ] + }, + { + "output_type": "stream", "name": "stderr", + "text": [ + "100%|██████████| 22.3k/22.3k [00:00<00:00, 39.7MB/s]\n" + ] + }, + { "output_type": "stream", + "name": "stdout", "text": [ - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_7b_en/1/download/config.json...\n", - "100%|██████████| 556/556 [00:00<00:00, 790kB/s]\n", - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_7b_en/1/download/model.weights.h5...\n", - "100%|██████████| 15.9G/15.9G [02:39<00:00, 107MB/s]\n", - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_7b_en/1/download/tokenizer.json...\n", - "100%|██████████| 401/401 [00:00<00:00, 587kB/s]\n", - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_7b_en/1/download/assets/tokenizer/vocabulary.spm...\n", - "100%|██████████| 4.04M/4.04M [00:00<00:00, 16.4MB/s]\n" + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_7b_en/3/download/model_00000.weights.h5...\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 9.95G/9.95G [01:59<00:00, 89.5MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_7b_en/3/download/model_00001.weights.h5...\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 5.95G/5.95G [01:08<00:00, 93.6MB/s]\n" ] } ], "source": [ - "gemma_lm_7b = keras_nlp.models.GemmaCausalLM.from_preset(\"code_gemma_7b_en\")" + "gemma_lm_7b = keras_hub.models.GemmaCausalLM.from_preset(\"code_gemma_7b_en\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { - "id": "v0YFsh2a3n9P" + "id": "v0YFsh2a3n9P", + "outputId": "6ed376e9-4e7d-486d-e838-edc21ec082fb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 397 + } }, "outputs": [ { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1mPreprocessor: \"gemma_causal_lm_preprocessor\"\u001b[0m\n" + ], "text/html": [ "
Preprocessor: \"gemma_causal_lm_preprocessor\"\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1mPreprocessor: \"gemma_causal_lm_preprocessor\"\u001b[0m\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Config\u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ gemma_tokenizer (\u001b[38;5;33mGemmaTokenizer\u001b[0m) │ Vocab size: \u001b[38;5;34m256,000\u001b[0m │\n", + "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n" + ], "text/html": [ - "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
-              "┃ Tokenizer (type)                                                                                Vocab # ┃\n",
-              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
-              "│ gemma_tokenizer (GemmaTokenizer)                   │                                             256,000 │\n",
-              "└────────────────────────────────────────────────────┴─────────────────────────────────────────────────────┘\n",
+              "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
+              "┃ Layer (type)                                                                                     Config ┃\n",
+              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
+              "│ gemma_tokenizer (GemmaTokenizer)                              │                      Vocab size: 256,000 │\n",
+              "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n",
               "
\n" - ], - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mTokenizer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Vocab #\u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", - "│ gemma_tokenizer (\u001b[38;5;33mGemmaTokenizer\u001b[0m) │ \u001b[38;5;34m256,000\u001b[0m │\n", - "└────────────────────────────────────────────────────┴─────────────────────────────────────────────────────┘\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1mModel: \"gemma_causal_lm\"\u001b[0m\n" + ], "text/html": [ "
Model: \"gemma_causal_lm\"\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1mModel: \"gemma_causal_lm\"\u001b[0m\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to \u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ padding_mask (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_ids (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ gemma_backbone │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m3072\u001b[0m) │ \u001b[38;5;34m8,537,680,896\u001b[0m │ padding_mask[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m], │\n", + "│ (\u001b[38;5;33mGemmaBackbone\u001b[0m) │ │ │ token_ids[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_embedding │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256000\u001b[0m) │ \u001b[38;5;34m786,432,000\u001b[0m │ gemma_backbone[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "│ (\u001b[38;5;33mReversibleEmbedding\u001b[0m) │ │ │ │\n", + "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n" + ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
               "┃ Layer (type)                   Output Shape                       Param #  Connected to               ┃\n",
@@ -386,64 +455,48 @@
               "│ (ReversibleEmbedding)         │                           │                 │                            │\n",
               "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n",
               "
\n" - ], - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to \u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", - "│ padding_mask (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ token_ids (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ gemma_backbone │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m3072\u001b[0m) │ \u001b[38;5;34m8,537,680,896\u001b[0m │ padding_mask[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m], │\n", - "│ (\u001b[38;5;33mGemmaBackbone\u001b[0m) │ │ │ token_ids[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ token_embedding │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256000\u001b[0m) │ \u001b[38;5;34m786,432,000\u001b[0m │ gemma_backbone[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", - "│ (\u001b[38;5;33mReversibleEmbedding\u001b[0m) │ │ │ │\n", - "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" + ], "text/html": [ "
 Total params: 8,537,680,896 (15.90 GB)\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" + ], "text/html": [ "
 Trainable params: 8,537,680,896 (15.90 GB)\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" + ], "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} } ], "source": [ @@ -481,7 +534,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "id": "mHAjAdvB_5yN" }, @@ -526,7 +579,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "id": "eGU7XXk1SFYT" }, @@ -538,14 +591,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": { - "id": "GoRC_SzuAO7t" + "id": "GoRC_SzuAO7t", + "outputId": "0b56381d-ac3a-48f1-9021-a1f5d24eb7fb", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "<|fim_prefix|>def fibonacci(n: int) -> int:\n", " if n == 0:\n", @@ -573,14 +630,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { - "id": "MpRJDxf4AO7t" + "id": "MpRJDxf4AO7t", + "outputId": "28255504-9751-4eab-c036-617f7b581987", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "<|fim_prefix|>def fibonacci(n: int) -> int:\n", " if n == 0:\n", @@ -619,7 +680,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": { "id": "kjaCaWcxqoq5" }, @@ -635,14 +696,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": { - "id": "nq8uV7DehsOt" + "id": "nq8uV7DehsOt", + "outputId": "a6c9affd-79a2-43e5-e93a-54071e761b23", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "<|fim_prefix|>void dfs(node* root) {\n", " if (root->left) {\n", @@ -669,14 +734,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { - "id": "mCY-VI01hsOu" + "id": "mCY-VI01hsOu", + "outputId": "144a35a5-035a-4b11-f6d6-5355e3d80219", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "<|fim_prefix|>void dfs(node* root) {\n", " if (root->left) {\n", @@ -707,7 +776,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": { "id": "ECfsJ7JW-7ef" }, @@ -724,14 +793,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { - "id": "-RZkx_0i_Vgj" + "id": "-RZkx_0i_Vgj", + "outputId": "2c02cb88-2d55-48ae-a758-f42ca68612bd", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Write a rust function to identify non-prime numbers.\n", "Examples:\n", @@ -740,17 +813,8 @@ ">>> is_not_prime(10)\n", "True\n", "pub fn is_not_prime(n: i32) -> bool {\n", - " if n <= 1 {\n", - " return true;\n", - " }\n", - " for i in 2..n {\n", - " if n % i == 0 {\n", - " return true;\n", - " }\n", - " }\n", - " false\n", - "}\n", - "\n" + " // your code here\n", + "}\n" ] } ], @@ -784,74 +848,165 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { - "id": "HxF9z5cFG5Yz" + "id": "HxF9z5cFG5Yz", + "outputId": "77d85e61-418a-46eb-ec2c-a271b08a0bdb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 605 + } }, "outputs": [ { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/3/download/config.json...\n" + ] + }, + { + "output_type": "stream", "name": "stderr", + "text": [ + "100%|██████████| 787/787 [00:00<00:00, 1.43MB/s]\n" + ] + }, + { "output_type": "stream", + "name": "stdout", "text": [ - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/1/download/config.json...\n", - "100%|██████████| 556/556 [00:00<00:00, 754kB/s]\n", - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/1/download/model.weights.h5...\n", - "100%|██████████| 15.9G/15.9G [03:18<00:00, 86.2MB/s]\n", - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/1/download/tokenizer.json...\n", - "100%|██████████| 401/401 [00:00<00:00, 593kB/s]\n", - "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/1/download/assets/tokenizer/vocabulary.spm...\n", - "100%|██████████| 4.04M/4.04M [00:00<00:00, 16.8MB/s]\n" + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/3/download/task.json...\n" ] }, { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 2.91k/2.91k [00:00<00:00, 6.02MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/3/download/assets/tokenizer/vocabulary.spm...\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 4.04M/4.04M [00:00<00:00, 16.4MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/3/download/model.weights.json...\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 22.3k/22.3k [00:00<00:00, 26.3MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/3/download/model_00000.weights.h5...\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 9.95G/9.95G [01:50<00:00, 96.9MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading from https://www.kaggle.com/api/v1/models/keras/codegemma/keras/code_gemma_instruct_7b_en/3/download/model_00001.weights.h5...\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 5.95G/5.95G [01:06<00:00, 96.3MB/s]\n" + ] + }, + { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1mPreprocessor: \"gemma_causal_lm_preprocessor\"\u001b[0m\n" + ], "text/html": [ "
Preprocessor: \"gemma_causal_lm_preprocessor\"\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1mPreprocessor: \"gemma_causal_lm_preprocessor\"\u001b[0m\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Config\u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ gemma_tokenizer (\u001b[38;5;33mGemmaTokenizer\u001b[0m) │ Vocab size: \u001b[38;5;34m256,000\u001b[0m │\n", + "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n" + ], "text/html": [ - "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
-              "┃ Tokenizer (type)                                                                                Vocab # ┃\n",
-              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
-              "│ gemma_tokenizer (GemmaTokenizer)                   │                                             256,000 │\n",
-              "└────────────────────────────────────────────────────┴─────────────────────────────────────────────────────┘\n",
+              "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
+              "┃ Layer (type)                                                                                     Config ┃\n",
+              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
+              "│ gemma_tokenizer (GemmaTokenizer)                              │                      Vocab size: 256,000 │\n",
+              "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n",
               "
\n" - ], - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mTokenizer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Vocab #\u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", - "│ gemma_tokenizer (\u001b[38;5;33mGemmaTokenizer\u001b[0m) │ \u001b[38;5;34m256,000\u001b[0m │\n", - "└────────────────────────────────────────────────────┴─────────────────────────────────────────────────────┘\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1mModel: \"gemma_causal_lm\"\u001b[0m\n" + ], "text/html": [ "
Model: \"gemma_causal_lm\"\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1mModel: \"gemma_causal_lm\"\u001b[0m\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to \u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ padding_mask (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_ids (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ gemma_backbone │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m3072\u001b[0m) │ \u001b[38;5;34m8,537,680,896\u001b[0m │ padding_mask[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m], │\n", + "│ (\u001b[38;5;33mGemmaBackbone\u001b[0m) │ │ │ token_ids[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_embedding │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256000\u001b[0m) │ \u001b[38;5;34m786,432,000\u001b[0m │ gemma_backbone[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "│ (\u001b[38;5;33mReversibleEmbedding\u001b[0m) │ │ │ │\n", + "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n" + ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
               "┃ Layer (type)                   Output Shape                       Param #  Connected to               ┃\n",
@@ -867,68 +1022,52 @@
               "│ (ReversibleEmbedding)         │                           │                 │                            │\n",
               "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n",
               "
\n" - ], - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to \u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", - "│ padding_mask (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ token_ids (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ gemma_backbone │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m3072\u001b[0m) │ \u001b[38;5;34m8,537,680,896\u001b[0m │ padding_mask[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m], │\n", - "│ (\u001b[38;5;33mGemmaBackbone\u001b[0m) │ │ │ token_ids[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ token_embedding │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256000\u001b[0m) │ \u001b[38;5;34m786,432,000\u001b[0m │ gemma_backbone[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", - "│ (\u001b[38;5;33mReversibleEmbedding\u001b[0m) │ │ │ │\n", - "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" + ], "text/html": [ "
 Total params: 8,537,680,896 (15.90 GB)\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" + ], "text/html": [ "
 Trainable params: 8,537,680,896 (15.90 GB)\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m8,537,680,896\u001b[0m (15.90 GB)\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} }, { + "output_type": "display_data", "data": { + "text/plain": [ + "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" + ], "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
               "
\n" - ], - "text/plain": [ - "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ] }, - "metadata": {}, - "output_type": "display_data" + "metadata": {} } ], "source": [ - "gemma_lm_7b_it = keras_nlp.models.GemmaCausalLM.from_preset(\"code_gemma_instruct_7b_en\")\n", + "gemma_lm_7b_it = keras_hub.models.GemmaCausalLM.from_preset(\"code_gemma_instruct_7b_en\")\n", "gemma_lm_7b_it.summary()" ] }, @@ -945,7 +1084,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "id": "llseiN-_5XXk" }, @@ -972,7 +1111,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "id": "-RJ6z5gbqTPA" }, @@ -1000,14 +1139,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { - "id": "LlDJmIAz5f0R" + "id": "LlDJmIAz5f0R", + "outputId": "03fc8f62-b1af-4fc4-f869-7c2ea80fc94a", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "user\n", "\n", @@ -1041,14 +1184,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { - "id": "rSBsedwN676V" + "id": "rSBsedwN676V", + "outputId": "a8384352-18fe-4880-b9d7-38cd1f95ce6d", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "user\n", "\n", @@ -1062,8 +1209,6 @@ "```\n", "\n", "model\n", - "Here is the C code equivalent of the Python code:\n", - "\n", "```c\n", "int factorial(int n) {\n", " int result = 1;\n", @@ -1072,15 +1217,7 @@ " }\n", " return result;\n", "}\n", - "```\n", - "\n", - "Here is a breakdown of the changes:\n", - "\n", - "* The function is declared with the `int` return type, as in Python.\n", - "* The `for` loop is converted to a `for` loop with an `int` variable `i` initialized to 2 and incremented by 1 in each iteration.\n", - "* The `range` function is replaced with a simple loop that iterates from 2 to `n` (inclusive).\n", - "* The `result *= i` statement is used to multiply `result` by `i` in each iteration.\n", - "* The `return` statement is used to return the final value of `result`.\n" + "```\n" ] } ], @@ -1099,7 +1236,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "id": "yYW6VXt7qPTc" }, @@ -1137,14 +1274,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { - "id": "To_5KvKJPw1H" + "id": "To_5KvKJPw1H", + "outputId": "deff28b8-a738-4473-c99b-90c38e194d76", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "user\n", "\n", @@ -1180,14 +1321,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { - "id": "jHZg8YX8QuIe" + "id": "jHZg8YX8QuIe", + "outputId": "75ceab1d-8249-4b4f-813f-2d60bc4d06b1", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "user\n", "\n", @@ -1212,43 +1357,28 @@ "```\n", "\n", "model\n", - "Yes, the code is vulnerable to a memory access error.\n", - "\n", - "**Reasoning:**\n", - "\n", - "* The code allocates memory for `WidgetList` using `malloc` based on the value of `numWidgets`.\n", - "* However, the loop iterates from `0` to `numWidgets`, which is one element beyond the allocated memory.\n", - "* This means that accessing `WidgetList[numWidgets]` will result in a memory access error, as it is outside the bounds of the allocated memory.\n", - "\n", - "**Example of Memory Access Error:**\n", - "\n", - "When `numWidgets` is 5, the code allocates memory for `WidgetList` as follows:\n", - "\n", - "```\n", - "WidgetList = (Widget **) malloc(5 * sizeof(Widget *));\n", - "```\n", + "The code is vulnerable to a memory access error. The code allocates an array of pointers to `Widget` objects and then attempts to access the element at index `numWidgets`, which is out of bounds. This can lead to a memory access error if the value of `numWidgets` is greater than the maximum number of widgets allowed.\n", "\n", - "The loop iterates from 0 to 4, accessing the following elements:\n", - "\n", - "* `WidgetList[0]`\n", - "* `WidgetList[1]`\n", - "* `WidgetList[2]`\n", - "* `WidgetList[3]`\n", - "* `WidgetList[4]`\n", - "\n", - "However, the code then attempts to access `WidgetList[5]`, which is outside the allocated memory range. This will result in a memory access error.\n", - "\n", - "**Solution:**\n", - "\n", - "To resolve this vulnerability, the loop should be modified to iterate from 0 to `numWidgets - 1`:\n", + "To fix this vulnerability, the code should be modified to check if the value of `numWidgets` is within the valid range before attempting to access the array. For example, the code could be modified to the following:\n", "\n", "```cpp\n", - "for (i = 0; i < numWidgets - 1; i++) {\n", + "int i;\n", + "unsigned int numWidgets;\n", + "Widget **WidgetList;\n", + "\n", + "numWidgets = GetUntrustedSizeValue();\n", + "if ((numWidgets == 0) || (numWidgets > MAX_NUM_WIDGETS)) {\n", + " ExitError(\"Incorrect number of widgets requested!\");\n", + "}\n", + "WidgetList = (Widget **) malloc(numWidgets * sizeof(Widget *));\n", + "printf(\"WidgetList ptr=%p\n", + "\", WidgetList);\n", + "for (i = 0; i < numWidgets; i++) {\n", " WidgetList[i] = InitializeWidget();\n", "}\n", - "```\n", - "\n", - "This ensures that the loop does not access elements beyond the allocated memory range.\n" + "WidgetList[numWidgets - 1] = NULL;\n", + "showWidgets(WidgetList);\n", + "```\n" ] } ], @@ -1285,7 +1415,9 @@ "accelerator": "GPU", "colab": { "name": "code_assist_keras.ipynb", - "toc_visible": true + "provenance": [], + "machine_shape": "hm", + "gpuType": "A100" }, "kernelspec": { "display_name": "Python 3", @@ -1294,4 +1426,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file