Tidying up

This commit is contained in:
Edward Donner
2024-09-16 10:54:20 -04:00
parent 57cc8a44ef
commit 21afbd6c73
18 changed files with 512 additions and 19617 deletions

View File

@@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 124,
"execution_count": null,
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
"metadata": {},
"outputs": [],
@@ -35,7 +35,7 @@
},
{
"cell_type": "code",
"execution_count": 125,
"execution_count": null,
"id": "4f672e1c-87e9-4865-b760-370fa605e614",
"metadata": {},
"outputs": [],
@@ -50,7 +50,7 @@
},
{
"cell_type": "code",
"execution_count": 126,
"execution_count": null,
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
"metadata": {},
"outputs": [],
@@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 127,
"execution_count": null,
"id": "6896636f-923e-4a2c-9d6c-fac07828a201",
"metadata": {},
"outputs": [],
@@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": 128,
"execution_count": null,
"id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
"metadata": {},
"outputs": [],
@@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 129,
"execution_count": null,
"id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
"metadata": {},
"outputs": [],
@@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 130,
"execution_count": null,
"id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
"metadata": {},
"outputs": [],
@@ -121,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 131,
"execution_count": null,
"id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
"metadata": {},
"outputs": [],
@@ -138,7 +138,7 @@
},
{
"cell_type": "code",
"execution_count": 132,
"execution_count": null,
"id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
"metadata": {},
"outputs": [],
@@ -160,7 +160,7 @@
},
{
"cell_type": "code",
"execution_count": 133,
"execution_count": null,
"id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
"metadata": {},
"outputs": [],
@@ -250,7 +250,7 @@
},
{
"cell_type": "code",
"execution_count": 134,
"execution_count": null,
"id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
"metadata": {},
"outputs": [],
@@ -353,7 +353,7 @@
},
{
"cell_type": "code",
"execution_count": 135,
"execution_count": null,
"id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
"metadata": {},
"outputs": [],
@@ -369,7 +369,7 @@
},
{
"cell_type": "code",
"execution_count": 136,
"execution_count": null,
"id": "8669f56b-8314-4582-a167-78842caea131",
"metadata": {},
"outputs": [],
@@ -390,7 +390,7 @@
},
{
"cell_type": "code",
"execution_count": 137,
"execution_count": null,
"id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
"metadata": {},
"outputs": [],
@@ -428,7 +428,7 @@
},
{
"cell_type": "code",
"execution_count": 138,
"execution_count": null,
"id": "19bf2bff-a822-4009-a539-f003b1651383",
"metadata": {},
"outputs": [],
@@ -445,7 +445,7 @@
},
{
"cell_type": "code",
"execution_count": 139,
"execution_count": null,
"id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
"metadata": {},
"outputs": [],
@@ -464,7 +464,7 @@
},
{
"cell_type": "code",
"execution_count": 140,
"execution_count": null,
"id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
"metadata": {},
"outputs": [],
@@ -507,7 +507,7 @@
},
{
"cell_type": "code",
"execution_count": 141,
"execution_count": null,
"id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
"metadata": {},
"outputs": [],
@@ -518,21 +518,10 @@
},
{
"cell_type": "code",
"execution_count": 142,
"execution_count": null,
"id": "13347633-4606-4e38-9927-80c39e65c1f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Token is valid (permission: write).\n",
"Your token has been saved in your configured git credential helpers (osxkeychain).\n",
"Your token has been saved to /Users/ed/.cache/huggingface/token\n",
"Login successful\n"
]
}
],
"outputs": [],
"source": [
"hf_token = os.environ['HF_TOKEN']\n",
"login(hf_token, add_to_git_credential=True)"
@@ -540,7 +529,7 @@
},
{
"cell_type": "code",
"execution_count": 143,
"execution_count": null,
"id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e",
"metadata": {},
"outputs": [],
@@ -553,7 +542,7 @@
},
{
"cell_type": "code",
"execution_count": 144,
"execution_count": null,
"id": "695ce389-a903-4533-a2f1-cd9e2a6af8f2",
"metadata": {},
"outputs": [],
@@ -565,91 +554,20 @@
},
{
"cell_type": "code",
"execution_count": 147,
"execution_count": null,
"id": "d4548e96-0b32-4793-bdd6-1b072c2f26ab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<|im_start|>system\n",
"You are an assistant that reimplements Python code in high performance C++ for an M1 Mac. Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.<|im_end|>\n",
"<|im_start|>user\n",
"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. Respond only with C++ code; do not explain your work other than a few comments. Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\n",
"\n",
"\n",
"import time\n",
"\n",
"def calculate(iterations, param1, param2):\n",
" result = 1.0\n",
" for i in range(1, iterations+1):\n",
" j = i * param1 - param2\n",
" result -= (1/j)\n",
" j = i * param1 + param2\n",
" result += (1/j)\n",
" return result\n",
"\n",
"start_time = time.time()\n",
"result = calculate(100_000_000, 4, 1) * 4\n",
"end_time = time.time()\n",
"\n",
"print(f\"Result: {result:.12f}\")\n",
"print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
"<|im_end|>\n",
"<|im_start|>assistant\n",
"\n"
]
}
],
"outputs": [],
"source": [
"print(text)"
]
},
{
"cell_type": "code",
"execution_count": 148,
"execution_count": null,
"id": "bb2a126b-09e7-4966-bc97-0ef5c2cc7896",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is the C++ code that achieves the same result as the Python code:\n",
"\n",
"```cpp\n",
"#include <iostream>\n",
"#include <iomanip>\n",
"#include <chrono>\n",
"\n",
"double calculate(int iterations, double param1, double param2) {\n",
" double result = 1.0;\n",
" for (int i = 1; i <= iterations; ++i) {\n",
" double j = i * param1 - param2;\n",
" result -= 1.0 / j;\n",
" j = i * param1 + param2;\n",
" result += 1.0 / j;\n",
" }\n",
" return result;\n",
"}\n",
"\n",
"int main() {\n",
" auto start_time = std::chrono::high_resolution_clock::now();\n",
" double result = calculate(100000000, 4.0, 1.0) * 4.0;\n",
" auto end_time = std::chrono::high_resolution_clock::now();\n",
"\n",
" std::cout << \"Result: \" << std::setprecision(12) << result << std::endl;\n",
" std::cout << \"Execution Time: \" << std::chrono::duration<double>(end_time - start_time).count() << \" seconds\" << std::endl;\n",
"\n",
" return 0;\n",
"}\n",
"```\n",
"\n",
"This C++ code does the same thing as the Python code: it calculates a mathematical function and measures the execution time. The `calculate` function is implemented in a similar way to the Python code, but it uses `double` instead of `int` for the parameters and the result. The `main` function measures the execution time using `std::chrono::high_resolution_clock` and prints the result and execution time to the console. The `std::setprecision(12)` is used to print the result with 12 decimal places.<|im_end|>"
]
}
],
"outputs": [],
"source": [
"client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
"stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
@@ -659,7 +577,7 @@
},
{
"cell_type": "code",
"execution_count": 149,
"execution_count": null,
"id": "127a52e5-ad85-42b7-a0f5-9afda5efe090",
"metadata": {},
"outputs": [],
@@ -678,7 +596,7 @@
},
{
"cell_type": "code",
"execution_count": 150,
"execution_count": null,
"id": "a82387d1-7651-4923-995b-fe18356fcaa6",
"metadata": {},
"outputs": [],
@@ -698,31 +616,10 @@
},
{
"cell_type": "code",
"execution_count": 152,
"execution_count": null,
"id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"with gr.Blocks(css=css) as ui:\n",
" gr.Markdown(\"## Convert code from Python to C++\")\n",