Skip to content

Commit df42292

Browse files
freundaAlon Freund
andauthored
Add T4 GPU prerequisite to hello_adapter and granite_switch_with_hf notebooks (#64)
- Updated hello_adapter.ipynb to specify T4 or better GPU requirement - Updated granite_switch_with_hf.ipynb to specify T4 or better GPU requirement - Consistent with hello_mellea.ipynb which was previously tested on T4 - Makes tutorials more accessible by clarifying minimum GPU requirements Co-authored-by: Alon Freund <alonf@il.ibm.com>
1 parent 2351e9c commit df42292

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

tutorials/notebooks/granite_switch_with_hf.ipynb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,31 @@
33
{
44
"metadata": {},
55
"cell_type": "markdown",
6-
"source": "# Granite Switch with HuggingFace\n\n**Duration:** ~10 min (after model download)\n\nA Granite Switch checkpoint bundles a base model with many LoRA experts. You pick one per forward pass by passing its name to the chat template.\n\n*Why HuggingFace:* this notebook uses the `transformers` backend for familiarity - every call is a standard `model.generate()`. Production workloads should switch to vLLM for 10-20x speedup; see [`rag_101.ipynb`](./rag_101.ipynb).\n\n**What you'll build:** one growing conversation about *Horizon 2055 Target Date Fund* (a fictional fund whose prospectus is the retrieved context), where each natural turn demonstrates a different embedded adapter function.\n\n**What you'll learn:**\n- How to load a composed Granite Switch checkpoint via `AutoModelForCausalLM` - no `trust_remote_code=True`.\n- How to invoke any embedded adapter function with `tokenizer.apply_chat_template(..., adapter_name=...)`.\n- The two parts of every adapter call: the LoRA switch, and the adapter-specific content protocol (criteria strings, control tokens, tagged sentences).\n- How guardian-family adapter functions act as *judges* over a side conversation without polluting the main chat history.\n\n**Adapters used:** adapters from the [Core](https://huggingface.co/ibm-granite/granitelib-core-r1.0) library (`context-attribution`, `uncertainty`, `requirement-check`) and the [Guardian](https://huggingface.co/ibm-granite/granitelib-guardian-r1.0) library (`guardian-core`, `policy-guardrails`, `factuality-detection`, `factuality-correction`).\n\n## Prerequisites\n\n1. **Install dependencies** (GPU recommended; CPU works but slow):",
6+
"source": [
7+
"# Granite Switch with HuggingFace\n",
8+
"\n",
9+
"**Duration:** ~10 min (after model download)\n",
10+
"\n",
11+
"A Granite Switch checkpoint bundles a base model with many LoRA experts. You pick one per forward pass by passing its name to the chat template.\n",
12+
"\n",
13+
"*Why HuggingFace:* this notebook uses the `transformers` backend for familiarity - every call is a standard `model.generate()`. Production workloads should switch to vLLM for 10-20x speedup; see [`rag_101.ipynb`](./rag_101.ipynb).\n",
14+
"\n",
15+
"**What you'll build:** one growing conversation about *Horizon 2055 Target Date Fund* (a fictional fund whose prospectus is the retrieved context), where each natural turn demonstrates a different embedded adapter function.\n",
16+
"\n",
17+
"**What you'll learn:**\n",
18+
"- How to load a composed Granite Switch checkpoint via `AutoModelForCausalLM` - no `trust_remote_code=True`.\n",
19+
"- How to invoke any embedded adapter function with `tokenizer.apply_chat_template(..., adapter_name=...)`.\n",
20+
"- The two parts of every adapter call: the LoRA switch, and the adapter-specific content protocol (criteria strings, control tokens, tagged sentences).\n",
21+
"- How guardian-family adapter functions act as *judges* over a side conversation without polluting the main chat history.\n",
22+
"\n",
23+
"**Adapters used:** adapters from the [Core](https://huggingface.co/ibm-granite/granitelib-core-r1.0) library (`context-attribution`, `uncertainty`, `requirement-check`) and the [Guardian](https://huggingface.co/ibm-granite/granitelib-guardian-r1.0) library (`guardian-core`, `policy-guardrails`, `factuality-detection`, `factuality-correction`).\n",
24+
"\n",
25+
"## Prerequisites\n",
26+
"\n",
27+
"**GPU runtime** (T4 or better). Go to *Runtime -> Change runtime type -> T4 GPU*.\n",
28+
"\n",
29+
"1. **Install dependencies:**"
30+
],
731
"id": "d5ed1e5ac8582c60"
832
},
933
{

tutorials/notebooks/hello_adapter.ipynb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,31 @@
33
{
44
"metadata": {},
55
"cell_type": "markdown",
6-
"source": "# Hello Adapter - Granite Switch with HuggingFace\n\n**Duration:** ~5 min\n\nMinimal example of invoking an **embedded LoRA adapter** inside a **Granite Switch** model, using the HuggingFace backend. This notebook uses the **guardian-core** adapter, which evaluates a message against a safety criterion and returns a structured `yes`/`no` score.\n\n**What you'll learn:**\n- How to build a single guardian-core call that scores a user message against a safety criterion and prints a parsed `harmful`/`safe` verdict.\n- How to load a composed Granite Switch checkpoint with `transformers`.\n- How to activate an adapter function function by passing `adapter_name=...` to `apply_chat_template`.\n- The Guardian prompt protocol - how to frame a criterion so the adapter returns a parseable score.\n\n**Adapters used:** `guardian-core` from the [Guardian](https://huggingface.co/ibm-granite/granitelib-guardian-r1.0) library - a general-purpose safety/risk judge that scores any user-supplied criterion (harm, social bias, jailbreaking, groundedness, ...) as `yes`/`no`.\n\nFor the recommended inference path (mellea + vLLM), see [`hello_mellea.ipynb`](./hello_mellea.ipynb). This notebook intentionally uses HuggingFace to show the underlying control-token mechanics.\n\n## Prerequisites\n\n**1 * A composed Granite Switch checkpoint** with the `guardian-core` adapter function. The default `MODEL_PATH` below points at the pre-composed `ibm-granite/granite-switch-4.1-3b-preview` on HuggingFace (drawn from the [IBM Granite 4.1 collection](https://huggingface.co/collections/ibm-granite/granite-41-language-models)). To compose your own checkpoint instead, see [`./compose_granite_switch.ipynb`](./compose_granite_switch.ipynb) and point `MODEL_PATH` at its output directory.\n\n**2 * Dependencies** (CUDA GPU required):",
6+
"source": [
7+
"# Hello Adapter - Granite Switch with HuggingFace\n",
8+
"\n",
9+
"**Duration:** ~5 min\n",
10+
"\n",
11+
"Minimal example of invoking an **embedded LoRA adapter** inside a **Granite Switch** model, using the HuggingFace backend. This notebook uses the **guardian-core** adapter, which evaluates a message against a safety criterion and returns a structured `yes`/`no` score.\n",
12+
"\n",
13+
"**What you'll learn:**\n",
14+
"- How to build a single guardian-core call that scores a user message against a safety criterion and prints a parsed `harmful`/`safe` verdict.\n",
15+
"- How to load a composed Granite Switch checkpoint with `transformers`.\n",
16+
"- How to activate an adapter function function by passing `adapter_name=...` to `apply_chat_template`.\n",
17+
"- The Guardian prompt protocol - how to frame a criterion so the adapter returns a parseable score.\n",
18+
"\n",
19+
"**Adapters used:** `guardian-core` from the [Guardian](https://huggingface.co/ibm-granite/granitelib-guardian-r1.0) library - a general-purpose safety/risk judge that scores any user-supplied criterion (harm, social bias, jailbreaking, groundedness, ...) as `yes`/`no`.\n",
20+
"\n",
21+
"For the recommended inference path (mellea + vLLM), see [`hello_mellea.ipynb`](./hello_mellea.ipynb). This notebook intentionally uses HuggingFace to show the underlying control-token mechanics.\n",
22+
"\n",
23+
"## Prerequisites\n",
24+
"\n",
25+
"**1 * A composed Granite Switch checkpoint** with the `guardian-core` adapter function. The default `MODEL_PATH` below points at the pre-composed `ibm-granite/granite-switch-4.1-3b-preview` on HuggingFace (drawn from the [IBM Granite 4.1 collection](https://huggingface.co/collections/ibm-granite/granite-41-language-models)). To compose your own checkpoint instead, see [`./compose_granite_switch.ipynb`](./compose_granite_switch.ipynb) and point `MODEL_PATH` at its output directory.\n",
26+
"\n",
27+
"**2 * GPU runtime** (T4 or better). Go to *Runtime -> Change runtime type -> T4 GPU*.\n",
28+
"\n",
29+
"**3 * Dependencies:**"
30+
],
731
"id": "97c76dcca207b140"
832
},
933
{

0 commit comments

Comments
 (0)