{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "6b64ed95-f2d5-440e-b90b-bfd083d63716", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9b488857", "metadata": {}, "source": [ "# Guardar multilingual-e5-small en local y benchmark de carga\n", "\n", "Guardamos el modelo en `.local/models/` (gitignored) y comparamos:\n", "- Carga desde HuggingFace cache vs carga desde path local\n", "- Tiempo de primer encoding tras carga\n", "- Verificación de que los embeddings son idénticos" ] }, { "cell_type": "code", "execution_count": 1, "id": "22b75c11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "FN_REGISTRY_ROOT: /home/lucas/fn_registry\n", "Destino local: /home/lucas/fn_registry/.local/models/multilingual-e5-small\n", "RAM actual: 833 MB\n" ] } ], "source": [ "import os, time, gc\n", "import numpy as np\n", "from sentence_transformers import SentenceTransformer\n", "import psutil\n", "\n", "FN_ROOT = os.environ.get('FN_REGISTRY_ROOT', '/home/lucas/fn_registry')\n", "MODEL_ID = 'intfloat/multilingual-e5-small'\n", "LOCAL_PATH = os.path.join(FN_ROOT, '.local', 'models', 'multilingual-e5-small')\n", "\n", "def ram_mb():\n", " return psutil.Process(os.getpid()).memory_info().rss / (1024 * 1024)\n", "\n", "print(f'FN_REGISTRY_ROOT: {FN_ROOT}')\n", "print(f'Destino local: {LOCAL_PATH}')\n", "print(f'RAM actual: {ram_mb():.0f} MB')" ] }, { "cell_type": "markdown", "id": "00e8cb16", "metadata": {}, "source": [ "## 1. Guardar modelo en .local/models/" ] }, { "cell_type": "code", "execution_count": 2, "id": "90df0aac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cargando modelo desde HuggingFace cache...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c21758811d8c41f28cec7ecf11df90ed", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading weights: 0%| | 0/199 [00:00" ] }, "metadata": {}, "output_type": "display_data", "transient": {} } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "fig, axes = plt.subplots(1, 2, figsize=(14, 5))\n", "\n", "# Box plot de tiempos\n", "data = [hf_bench['times'], local_bench['times']]\n", "labels = ['HF cache', 'Local (.local/)']\n", "colors = ['#e74c3c', '#2ecc71']\n", "\n", "bp = axes[0].boxplot(data, labels=labels, patch_artist=True)\n", "for patch, color in zip(bp['boxes'], colors):\n", " patch.set_facecolor(color)\n", " patch.set_alpha(0.7)\n", "axes[0].set_ylabel('Tiempo de carga (s)')\n", "axes[0].set_title('Distribución de tiempos de carga')\n", "\n", "# Bar chart comparativo\n", "means = [hf_bench['mean_time'], local_bench['mean_time']]\n", "stds = [hf_bench['std_time'], local_bench['std_time']]\n", "bars = axes[1].bar(labels, means, yerr=stds, color=colors, alpha=0.8, capsize=10)\n", "for bar, mean in zip(bars, means):\n", " axes[1].text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.05,\n", " f'{mean:.3f}s', ha='center', fontsize=12, fontweight='bold')\n", "axes[1].set_ylabel('Tiempo medio (s)')\n", "axes[1].set_title(f'Carga media ({N_RUNS} runs) — speedup: {speedup:.2f}x')\n", "\n", "plt.tight_layout()\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }