Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion nb/CodeForces-cot-Finetune_for_Reasoning_on_CodeForces.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "%%capture\nimport os, re\nif \"COLAB_\" not in \"\".join(os.environ.keys()):\n !pip install unsloth # Do this in local & cloud setups\nelse:\n import torch; v = re.match(r'[\\d]{1,}\\.[\\d]{1,}', str(torch.__version__)).group(0)\n xformers = 'xformers==' + {'2.10':'0.0.34','2.9':'0.0.33.post1','2.8':'0.0.32.post2'}.get(v, \"0.0.34\")\n !pip install sentencepiece protobuf \"datasets==4.3.0\" \"huggingface_hub>=0.34.0\" hf_transfer\n !pip install --no-deps unsloth_zoo bitsandbytes accelerate {xformers} peft trl triton unsloth\n!pip install transformers==4.56.2\n!pip install --no-deps trl==0.22.2"
"source": [
"%%capture\n",
"import os, re\n",
"if \"COLAB_\" not in \"\".join(os.environ.keys()):\n",
" !pip install unsloth # Do this in local & cloud setups\n",
"else:\n",
" import torch; v = re.match(r'[\\d]{1,}\\.[\\d]{1,}', str(torch.__version__)).group(0)\n",
" xformers = 'xformers==' + {'2.10':'0.0.34','2.9':'0.0.33.post1','2.8':'0.0.32.post2'}.get(v, \"0.0.34\")\n",
" !pip install sentencepiece protobuf \"datasets==4.3.0\" \"huggingface_hub>=0.34.0\" hf_transfer\n",
" !pip install --no-deps unsloth_zoo bitsandbytes accelerate {xformers} peft trl triton unsloth\n",
"!pip install transformers==4.56.2\n",
"!pip install --no-deps trl==0.22.2"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -1368,6 +1380,18 @@
"Now if you want to load the LoRA adapters we just saved for inference, set `False` to `True`:"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"# Prompt template used by the inference cell below.\n",
"# Some tutorials show this in markdown as \"copy from above\"; define it\n",
"# explicitly so the notebook runs end-to-end.\n",
"alpaca_prompt = \"\"\"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{}\n\n### Input:\n{}\n\n### Response:\n{}\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
30 changes: 27 additions & 3 deletions nb/EmbeddingGemma_(300M).ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@
"id": "Xk9jtF3J9Xa_"
},
"outputs": [],
"source": "%%capture\nimport os, re\nif \"COLAB_\" not in \"\".join(os.environ.keys()):\n !pip install unsloth # Do this in local & cloud setups\nelse:\n import torch; v = re.match(r'[\\d]{1,}\\.[\\d]{1,}', str(torch.__version__)).group(0)\n xformers = 'xformers==' + {'2.10':'0.0.34','2.9':'0.0.33.post1','2.8':'0.0.32.post2'}.get(v, \"0.0.34\")\n !pip install sentencepiece protobuf \"datasets==4.3.0\" \"huggingface_hub>=0.34.0\" hf_transfer\n !pip install --no-deps unsloth_zoo bitsandbytes accelerate {xformers} peft trl triton unsloth\n!pip install transformers==4.56.2\n!pip install --no-deps trl==0.22.2"
"source": [
"%%capture\n",
"import os, re\n",
"if \"COLAB_\" not in \"\".join(os.environ.keys()):\n",
" !pip install unsloth # Do this in local & cloud setups\n",
"else:\n",
" import torch; v = re.match(r'[\\d]{1,}\\.[\\d]{1,}', str(torch.__version__)).group(0)\n",
" xformers = 'xformers==' + {'2.10':'0.0.34','2.9':'0.0.33.post1','2.8':'0.0.32.post2'}.get(v, \"0.0.34\")\n",
" !pip install sentencepiece protobuf \"datasets==4.3.0\" \"huggingface_hub>=0.34.0\" hf_transfer\n",
" !pip install --no-deps unsloth_zoo bitsandbytes accelerate {xformers} peft trl triton unsloth\n",
"!pip install transformers==4.56.2\n",
"!pip install --no-deps trl==0.22.2"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -396,7 +408,13 @@
" show_progress_bar = False,\n",
" batch_size = 64,\n",
")\n",
"with torch.autocast(device_type = \"cuda\", dtype = model.dtype, enabled = model.dtype != torch.float16):\n",
"_autocast_dtype = getattr(model, 'dtype', None)\n",
"if _autocast_dtype is None:\n",
" try:\n",
" _autocast_dtype = next(model.parameters()).dtype\n",
" except Exception:\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a broad except Exception: can hide unexpected errors and make debugging more difficult. It's better practice to catch only the specific exceptions you anticipate. In this case, next(model.parameters()) could raise StopIteration if there are no parameters, or an AttributeError could be raised when accessing .dtype.

    except (StopIteration, AttributeError):

" _autocast_dtype = torch.float32\n",
"with torch.autocast(device_type = \"cuda\", dtype = _autocast_dtype, enabled = _autocast_dtype != torch.float16):\n",
" print(evaluator(model))"
]
},
Expand Down Expand Up @@ -817,7 +835,13 @@
}
],
"source": [
"with torch.autocast(device_type = \"cuda\", dtype = model.dtype, enabled = model.dtype != torch.float16):\n",
"_autocast_dtype = getattr(model, 'dtype', None)\n",
"if _autocast_dtype is None:\n",
" try:\n",
" _autocast_dtype = next(model.parameters()).dtype\n",
" except Exception:\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to my previous comment, it's better to catch more specific exceptions rather than the general Exception. This avoids masking unexpected issues during execution. StopIteration and AttributeError are the likely exceptions to handle here.

    except (StopIteration, AttributeError):

" _autocast_dtype = torch.float32\n",
"with torch.autocast(device_type = \"cuda\", dtype = _autocast_dtype, enabled = _autocast_dtype != torch.float16):\n",
" print(evaluator(model))"
]
},
Expand Down