{
"cells": [
{
"cell_type": "markdown",
"id": "vem-019tN3GE",
"metadata": {
"id": "vem-019tN3GE"
},
"source": [
"# Inseq:可视化解释LLM的输出\n",
"\n",
"> [HW7: Understand what Generative AI is thinking](https://colab.research.google.com/drive/1Xnz0GHC0yWO2Do0aAYBCq9zL45lbiRjM?usp=sharing#scrollTo=UFOUfh2k1jFN) 中文镜像版\n",
">\n",
"> 指导文章:[12. Inseq 特征归因:可视化解释 LLM 的输出](https://github.com/Hoper-J/LLM-Guide-and-Demos-zh_CN/blob/master/Guide/12.%20Inseq%20特征归因:可视化解释%20LLM%20的输出.md)\n",
"\n",
"Feature attribution 特征归因,你可以将其当做对输出的解释,就像图像分类模型可以可视化关注区域一样,LLM 一样也可以。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cnCnLzQYN3GL",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "cnCnLzQYN3GL",
"outputId": "7ea18b41-94a4-4885-c84b-08692410c5b1",
"scrolled": true,
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2mResolved \u001b[1m220 packages\u001b[0m \u001b[2min 1ms\u001b[0m\u001b[0m\n",
"\u001b[2mAudited \u001b[1m205 packages\u001b[0m \u001b[2min 0.08ms\u001b[0m\u001b[0m\n",
"\u001b[2mResolved \u001b[1m220 packages\u001b[0m \u001b[2min 1ms\u001b[0m\u001b[0m\n",
"\u001b[2mAudited \u001b[1m205 packages\u001b[0m \u001b[2min 0.07ms\u001b[0m\u001b[0m\n",
"\u001b[2mResolved \u001b[1m220 packages\u001b[0m \u001b[2min 1ms\u001b[0m\u001b[0m\n",
"\u001b[2mAudited \u001b[1m205 packages\u001b[0m \u001b[2min 0.08ms\u001b[0m\u001b[0m\n",
"\u001b[2mResolved \u001b[1m220 packages\u001b[0m \u001b[2min 1ms\u001b[0m\u001b[0m\n",
"\u001b[2mAudited \u001b[1m205 packages\u001b[0m \u001b[2min 0.08ms\u001b[0m\u001b[0m\n",
"\u001b[2mResolved \u001b[1m220 packages\u001b[0m \u001b[2min 1ms\u001b[0m\u001b[0m\n",
"\u001b[2mAudited \u001b[1m205 packages\u001b[0m \u001b[2min 0.07ms\u001b[0m\u001b[0m\n"
]
}
],
"source": [
"%pip install inseq\n",
"%pip install transformers\n",
"%pip install accelerate\n",
"%pip install sacremoses\n",
"%pip install numpy"
]
},
{
"cell_type": "markdown",
"id": "235184ca-2237-4812-b4fe-5164a7222b3f",
"metadata": {},
"source": [
"## 设置模型下载镜像\n",
"\n",
"注意,需要在导入 inseq 前进行设置才能起效。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1bcf948b-da1b-4372-b5e2-bf2343966bc6",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'"
]
},
{
"cell_type": "markdown",
"id": "db496ea6-080a-4cdc-8934-eabf60b242f4",
"metadata": {},
"source": [
"## 修复报错:ImportError: cannot import name 'display' from 'IPython.core.display'\n",
"\n",
"当前版本的 inseq 库存在一个导入路径问题。在 `inseq/data/viz.py` 文件中,库尝试从 `IPython.core.display` 导入 `display`,但在新版本的 IPython 中,应该从 `IPython.display` 导入。\n",
"\n",
"执行以下命令修复:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e9f014b2-7c7e-41d1-a594-e55b5cbbf1b7",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import IPython.display\n",
"import IPython.core\n",
"IPython.core.display = IPython.display\n",
"sys.modules['IPython.core.display'] = IPython.display"
]
},
{
"cell_type": "markdown",
"id": "SP6eYcWfN3GM",
"metadata": {
"id": "SP6eYcWfN3GM",
"tags": []
},
"source": [
"## 可视化翻译任务"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5XV-BAoJW6RF",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "5XV-BAoJW6RF",
"outputId": "4408ab15-1800-4a0f-cc7c-9062e72aaf31"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"使用 Helsinki-NLP/opus-mt-zh-en 模型\n",
"\n",
"======= 归因方法: saliency =======\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"Attributing with saliency...: 10%|█ | 1/10 [00:00, ?it/s]Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.58.0. You should pass an instance of `EncoderDecoderCache` instead, e.g. `past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`.\n",
"Attributing with saliency...: 100%|██████████| 10/10 [00:00<00:00, 24.06it/s]\n"
]
},
{
"data": {
"text/html": [
"
0th instance:
\n",
"\n",
"
\n",
"
\n",
"
\n",
" \n",
"
\n",
"
\n",
" Source Saliency Heatmap\n",
"
\n",
" x: Generated tokens, y: Attributed tokens\n",
"
\n",
" \n",
"
\n",
" | \n",
"I | like | machine | learning | and | artificial | intelligence | . | </s> |
|---|
| 我喜欢 | 0.24 | 0.44 | 0.083 | 0.113 | 0.126 | 0.048 | 0.041 | 0.116 | 0.203 |
|---|
| 机器 | 0.159 | 0.116 | 0.447 | 0.178 | 0.129 | 0.056 | 0.059 | 0.086 | 0.114 |
|---|
| 学习 | 0.074 | 0.076 | 0.155 | 0.283 | 0.138 | 0.042 | 0.048 | 0.054 | 0.07 |
|---|
| 和 | 0.066 | 0.055 | 0.041 | 0.065 | 0.179 | 0.039 | 0.033 | 0.048 | 0.059 |
|---|
| 人工 | 0.105 | 0.073 | 0.081 | 0.121 | 0.134 | 0.375 | 0.156 | 0.158 | 0.136 |
|---|
| 智能 | 0.146 | 0.104 | 0.114 | 0.151 | 0.171 | 0.335 | 0.524 | 0.407 | 0.17 |
|---|
| 。 | 0.14 | 0.082 | 0.035 | 0.044 | 0.079 | 0.054 | 0.064 | 0.075 | 0.17 |
|---|
| </s> | 0.07 | 0.053 | 0.045 | 0.046 | 0.044 | 0.051 | 0.075 | 0.056 | 0.077 |
|---|
\n",
"
\n",
"\n",
"
\n",
"
\n",
"
\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"翻译结果: I like machine learning and artificial intelligence . \n",
"\n",
"======= 归因方法: attention =======\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"Attributing with attention...: 2it [00:00, 69.45it/s] \n"
]
},
{
"data": {
"text/html": [
"
0th instance:
\n",
"\n",
"\n",
"
\n",
"
\n",
" \n",
"
\n",
"
\n",
" Source Saliency Heatmap\n",
"
\n",
" x: Generated tokens, y: Attributed tokens\n",
"
\n",
" \n",
"
\n",
" | \n",
"I | like | machine | learning | and | artificial | intelligence | . | </s> |
|---|
| 我喜欢 | 0.21 | 0.537 | 0.233 | 0.012 | 0.026 | 0.045 | 0.015 | 0.042 | 0.047 |
|---|
| 机器 | 0.087 | 0.033 | 0.217 | 0.174 | 0.043 | 0.03 | 0.02 | 0.017 | 0.028 |
|---|
| 学习 | 0.038 | 0.038 | 0.123 | 0.288 | 0.142 | 0.022 | 0.021 | 0.029 | 0.026 |
|---|
| 和 | 0.049 | 0.039 | 0.056 | 0.038 | 0.128 | 0.093 | 0.013 | 0.032 | 0.028 |
|---|
| 人工 | 0.021 | 0.023 | 0.037 | 0.043 | 0.066 | 0.254 | 0.184 | 0.036 | 0.032 |
|---|
| 智能 | 0.021 | 0.023 | 0.063 | 0.057 | 0.067 | 0.113 | 0.346 | 0.136 | 0.027 |
|---|
| 。 | 0.135 | 0.067 | 0.05 | 0.029 | 0.06 | 0.053 | 0.043 | 0.153 | 0.186 |
|---|
| </s> | 0.438 | 0.24 | 0.221 | 0.359 | 0.468 | 0.391 | 0.358 | 0.555 | 0.625 |
|---|
\n",
"
\n",
"\n",
"
\n",
"
\n",
"
\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"翻译结果: I like machine learning and artificial intelligence . \n"
]
}
],
"source": [
"import inseq\n",
"import torch\n",
"\n",
"print(\"使用 Helsinki-NLP/opus-mt-zh-en 模型\")\n",
"\n",
"# 定义要使用的归因方法列表\n",
"attribution_methods = ['saliency', 'attention']\n",
"\n",
"for method in attribution_methods:\n",
" print(f\"\\n======= 归因方法: {method} =======\")\n",
"\n",
" # 直接用 inseq 加载模型\n",
" inseq_model = inseq.load_model(\n",
" \"Helsinki-NLP/opus-mt-zh-en\",\n",
" attribution_method=method,\n",
" model_kwargs={\n",
" \"attn_implementation\": \"eager\" if method == \"attention\" else None\n",
" }\n",
" )\n",
"\n",
" # 准备输入文本\n",
" input_text = \"我喜欢机器学习和人工智能。\"\n",
"\n",
" # 进行归因分析\n",
" attribution_result = inseq_model.attribute(\n",
" input_texts=input_text,\n",
" show_progress=True\n",
" )\n",
"\n",
" # 清理 tokenizer 中的特殊字符(可选)\n",
" for attr in attribution_result.sequence_attributions:\n",
" for item in attr.source:\n",
" item.token = item.token.replace('▁', '')\n",
" for item in attr.target:\n",
" item.token = item.token.replace('▁', '')\n",
"\n",
" # 显示归因结果\n",
" attribution_result.show()\n",
"\n",
" # 打印生成的翻译\n",
" if attribution_result.sequence_attributions:\n",
" # 获取生成的 tokens\n",
" generated_tokens = attribution_result.sequence_attributions[0].target\n",
" generated_text = \" \".join([token.token for token in generated_tokens])\n",
" print(f\"翻译结果: {generated_text}\")\n",
"\n",
" # 清理内存\n",
" del inseq_model\n",
" torch.cuda.empty_cache()"
]
},
{
"cell_type": "markdown",
"id": "H9RtJlsiN3GN",
"metadata": {
"id": "H9RtJlsiN3GN",
"tags": []
},
"source": [
"## 可视化文本生成任务\n",
"\n",
"用小模型 Qwen3-0.6B 进行演示,如果你的显存充足,可以替换为更大模型。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "L1cHlkVPN3GP",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "L1cHlkVPN3GP",
"outputId": "f613fdb0-1814-44c9-90e9-816cb0a32a3b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"======= 归因方法: saliency =======\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"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",
"Attributing with saliency...: 100%|██████████| 22/22 [00:02<00:00, 6.73it/s]\n"
]
},
{
"data": {
"text/html": [
"
0th instance:
\n",
"\n",
"\n",
"
\n",
"
\n",
" \n",
"
\n",
"
\n",
" Target Saliency Heatmap\n",
"
\n",
" x: Generated tokens, y: Attributed tokens\n",
"
\n",
" \n",
"
\n",
" | \n",
"! | This | is | a | simple | example | that | shows | how | to | create | a | Python | script | that | can | be | run | from | the |
|---|
| Hello | 0.576 | 0.479 | 0.342 | 0.345 | 0.334 | 0.36 | 0.242 | 0.193 | 0.19 | 0.138 | 0.175 | 0.159 | 0.185 | 0.24 | 0.135 | 0.163 | 0.085 | 0.116 | 0.094 | 0.087 |
|---|
| world | 0.424 | 0.324 | 0.253 | 0.251 | 0.246 | 0.245 | 0.149 | 0.142 | 0.127 | 0.093 | 0.129 | 0.114 | 0.133 | 0.16 | 0.078 | 0.106 | 0.058 | 0.073 | 0.055 | 0.06 |
|---|
| ! | | 0.196 | 0.188 | 0.138 | 0.129 | 0.083 | 0.085 | 0.089 | 0.112 | 0.081 | 0.084 | 0.095 | 0.05 | 0.037 | 0.049 | 0.061 | 0.032 | 0.058 | 0.031 | 0.046 |
|---|
| This | | | 0.216 | 0.138 | 0.113 | 0.076 | 0.083 | 0.082 | 0.059 | 0.072 | 0.047 | 0.047 | 0.043 | 0.037 | 0.045 | 0.034 | 0.029 | 0.035 | 0.02 | 0.023 |
|---|
| is | | | | 0.129 | 0.091 | 0.062 | 0.066 | 0.073 | 0.048 | 0.051 | 0.039 | 0.038 | 0.036 | 0.025 | 0.036 | 0.025 | 0.022 | 0.026 | 0.017 | 0.02 |
|---|
| a | | | | | 0.088 | 0.053 | 0.065 | 0.063 | 0.054 | 0.049 | 0.04 | 0.036 | 0.031 | 0.024 | 0.039 | 0.027 | 0.027 | 0.026 | 0.02 | 0.023 |
|---|
| simple | | | | | | 0.121 | 0.113 | 0.075 | 0.076 | 0.078 | 0.047 | 0.055 | 0.052 | 0.03 | 0.047 | 0.034 | 0.02 | 0.027 | 0.021 | 0.023 |
|---|
| example | | | | | | | 0.197 | 0.141 | 0.104 | 0.112 | 0.062 | 0.065 | 0.063 | 0.04 | 0.089 | 0.049 | 0.034 | 0.044 | 0.03 | 0.035 |
|---|
| that | | | | | | | | 0.142 | 0.072 | 0.085 | 0.053 | 0.056 | 0.045 | 0.029 | 0.054 | 0.036 | 0.044 | 0.041 | 0.025 | 0.032 |
|---|
| shows | | | | | | | | | 0.159 | 0.102 | 0.077 | 0.059 | 0.054 | 0.034 | 0.049 | 0.047 | 0.035 | 0.043 | 0.025 | 0.028 |
|---|
| how | | | | | | | | | | 0.139 | 0.123 | 0.083 | 0.074 | 0.041 | 0.07 | 0.057 | 0.055 | 0.065 | 0.036 | 0.046 |
|---|
| to | | | | | | | | | | | 0.124 | 0.085 | 0.085 | 0.043 | 0.066 | 0.063 | 0.051 | 0.057 | 0.034 | 0.048 |
|---|
| create | | | | | | | | | | | | 0.108 | 0.087 | 0.054 | 0.063 | 0.043 | 0.059 | 0.045 | 0.034 | 0.033 |
|---|
| a | | | | | | | | | | | | | 0.062 | 0.045 | 0.032 | 0.037 | 0.052 | 0.03 | 0.034 | 0.032 |
|---|
| Python | | | | | | | | | | | | | | 0.161 | 0.061 | 0.071 | 0.059 | 0.06 | 0.071 | 0.053 |
|---|
| script | | | | | | | | | | | | | | | 0.087 | 0.084 | 0.085 | 0.089 | 0.114 | 0.061 |
|---|
| that | | | | | | | | | | | | | | | | 0.063 | 0.101 | 0.054 | 0.045 | 0.035 |
|---|
| can | | | | | | | | | | | | | | | | | 0.149 | 0.057 | 0.06 | 0.048 |
|---|
| be | | | | | | | | | | | | | | | | | | 0.054 | 0.069 | 0.038 |
|---|
| run | | | | | | | | | | | | | | | | | | | 0.166 | 0.095 |
|---|
| from | | | | | | | | | | | | | | | | | | | | 0.13 |
|---|
| the | | | | | | | | | | | | | | | | | | | | |
|---|
\n",
"
\n",
"\n",
"
\n",
"
\n",
"
\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"======= 归因方法: attention =======\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"The following generation flags are not valid and may be ignored: ['output_attentions']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n",
"Attributing with attention...: 3it [00:00, 10.94it/s]\n"
]
},
{
"data": {
"text/html": [
"
0th instance:
\n",
"\n",
"\n",
"
\n",
"
\n",
" \n",
"
\n",
"
\n",
" Target Saliency Heatmap\n",
"
\n",
" x: Generated tokens, y: Attributed tokens\n",
"
\n",
" \n",
"
\n",
" | \n",
"in | C | #ĊĊ | Hello | world | is | the | simplest | and | most | common | example | in | programming | , | and | it | 's |
|---|
| Hello | 0.839 | 0.745 | 0.73 | 0.68 | 0.677 | 0.612 | 0.611 | 0.607 | 0.62 | 0.592 | 0.614 | 0.585 | 0.593 | 0.573 | 0.636 | 0.546 | 0.556 | 0.552 |
|---|
| world | 0.161 | 0.121 | 0.045 | 0.04 | 0.068 | 0.053 | 0.033 | 0.022 | 0.017 | 0.014 | 0.013 | 0.014 | 0.017 | 0.012 | 0.009 | 0.015 | 0.013 | 0.013 |
|---|
| in | | 0.134 | 0.078 | 0.04 | 0.027 | 0.035 | 0.028 | 0.015 | 0.015 | 0.011 | 0.008 | 0.01 | 0.012 | 0.017 | 0.007 | 0.011 | 0.01 | 0.008 |
|---|
| C | | | 0.148 | 0.072 | 0.029 | 0.031 | 0.021 | 0.012 | 0.016 | 0.009 | 0.009 | 0.013 | 0.014 | 0.024 | 0.019 | 0.011 | 0.011 | 0.009 |
|---|
| #ĊĊ | | | | 0.167 | 0.078 | 0.058 | 0.051 | 0.027 | 0.023 | 0.02 | 0.017 | 0.02 | 0.027 | 0.03 | 0.032 | 0.029 | 0.027 | 0.017 |
|---|
| Hello | | | | | 0.122 | 0.108 | 0.057 | 0.038 | 0.024 | 0.018 | 0.015 | 0.016 | 0.015 | 0.012 | 0.01 | 0.016 | 0.017 | 0.017 |
|---|
| world | | | | | | 0.103 | 0.073 | 0.062 | 0.037 | 0.02 | 0.022 | 0.032 | 0.029 | 0.022 | 0.013 | 0.025 | 0.014 | 0.034 |
|---|
| is | | | | | | | 0.125 | 0.096 | 0.061 | 0.045 | 0.034 | 0.034 | 0.035 | 0.03 | 0.015 | 0.034 | 0.032 | 0.029 |
|---|
| the | | | | | | | | 0.12 | 0.068 | 0.058 | 0.039 | 0.035 | 0.027 | 0.023 | 0.012 | 0.024 | 0.02 | 0.019 |
|---|
| simplest | | | | | | | | | 0.12 | 0.106 | 0.08 | 0.06 | 0.035 | 0.025 | 0.011 | 0.02 | 0.018 | 0.018 |
|---|
| and | | | | | | | | | | 0.107 | 0.065 | 0.044 | 0.021 | 0.019 | 0.008 | 0.021 | 0.029 | 0.013 |
|---|
| most | | | | | | | | | | | 0.084 | 0.042 | 0.016 | 0.01 | 0.003 | 0.006 | 0.007 | 0.005 |
|---|
| common | | | | | | | | | | | | 0.096 | 0.054 | 0.038 | 0.014 | 0.021 | 0.018 | 0.015 |
|---|
| example | | | | | | | | | | | | | 0.104 | 0.073 | 0.038 | 0.044 | 0.027 | 0.028 |
|---|
| in | | | | | | | | | | | | | | 0.091 | 0.057 | 0.031 | 0.023 | 0.015 |
|---|
| programming | | | | | | | | | | | | | | | 0.117 | 0.044 | 0.021 | 0.019 |
|---|
| , | | | | | | | | | | | | | | | | 0.101 | 0.065 | 0.045 |
|---|
| and | | | | | | | | | | | | | | | | | 0.094 | 0.06 |
|---|
| it | | | | | | | | | | | | | | | | | | 0.086 |
|---|
| 's | | | | | | | | | | | | | | | | | | |
|---|
\n",
"
\n",
"\n",
"
\n",
"
\n",
"
\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import inseq\n",
"\n",
"# 定义要使用的归因方法列表\n",
"attribution_methods = ['saliency', 'attention']\n",
"\n",
"for method in attribution_methods:\n",
" print(f\"======= 归因方法: {method} =======\")\n",
"\n",
" # 直接用 inseq 加载模型\n",
" inseq_model = inseq.load_model(\n",
" \"Qwen/Qwen3-0.6B\",\n",
" attribution_method=method,\n",
" model_kwargs={\n",
" \"device_map\": {\"\": 0}\n",
" }\n",
" )\n",
"\n",
" # 设置 padding token\n",
" if inseq_model.tokenizer.pad_token is None:\n",
" inseq_model.tokenizer.pad_token = inseq_model.tokenizer.eos_token\n",
" inseq_model.tokenizer.pad_token_id = inseq_model.tokenizer.eos_token_id\n",
"\n",
" \n",
" # 对输入文本进行归因分析\n",
" attribution_result = inseq_model.attribute(\n",
" input_texts=\"Hello world\",\n",
" show_progress=True\n",
" )\n",
"\n",
" # 清理 tokenizer 中的特殊字符(可选)\n",
" for attr in attribution_result.sequence_attributions:\n",
" for item in attr.source:\n",
" item.token = item.token.replace('Ġ', '')\n",
" for item in attr.target:\n",
" item.token = item.token.replace('Ġ', '')\n",
"\n",
" # 显示归因结果\n",
" attribution_result.show()\n",
"\n",
" # 清理内存\n",
" del inseq_model\n",
" torch.cuda.empty_cache()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c-1d5_by_coC",
"metadata": {
"id": "c-1d5_by_coC"
},
"outputs": [],
"source": []
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "ai",
"language": "python",
"name": "ai"
},
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}