Text Generation
Transformers
GGUF
GGUF
Inference Endpoints
conversational
andrijdavid commited on
Commit
85cc49e
1 Parent(s): 39905e4

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,20 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ Q2_K/Q2_K-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
37
+ Q3_K/Q3_K-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
38
+ Q3_K_L/Q3_K_L-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
39
+ Q3_K_M/Q3_K_M-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
40
+ Q3_K_S/Q3_K_S-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
41
+ Q4_0/Q4_0-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
42
+ Q4_1/Q4_1-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
43
+ Q4_K/Q4_K-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
44
+ Q4_K_M/Q4_K_M-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
45
+ Q4_K_S/Q4_K_S-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
46
+ Q5_0/Q5_0-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
47
+ Q5_1/Q5_1-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
48
+ Q5_K/Q5_K-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
49
+ Q5_K_M/Q5_K_M-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
50
+ Q5_K_S/Q5_K_S-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
51
+ Q6_K/Q6_K-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
52
+ Q8_0/Q8_0-00001-of-00001.gguf filter=lfs diff=lfs merge=lfs -text
Q2_K/Q2_K-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:27a77fcc40a3bfdfaad29f5ba40da15ba7a704cd33c1ac7ffb16aa107eaf812d
3
+ size 1229830400
Q3_K/Q3_K-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a224f1c69c3997c52e73be8bb25ab3caef8b99137fcfc30fcbba12f74e8ae34
3
+ size 1461668096
Q3_K_L/Q3_K_L-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:319fc057d0111bc62b1964cbddacf93e99b814f220899772cd74845eceec7882
3
+ size 1550436608
Q3_K_M/Q3_K_M-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a224f1c69c3997c52e73be8bb25ab3caef8b99137fcfc30fcbba12f74e8ae34
3
+ size 1461668096
Q3_K_S/Q3_K_S-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:efe5f41638d1a2ad2b0ec3e30dd2bba4e27f920c43b4a7968516cf1d98ddc7d6
3
+ size 1360660736
Q4_0/Q4_0-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4d08bd6dfa1fb31b01ac8ccbb2021d63925ca4ce0f8cbf6c5d407b2fd5e21b7e
3
+ size 1629509888
Q4_1/Q4_1-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:da7151ee9aba9c336ae5e4c9616aa143f6755f9f0348706a974b7a84973d57bd
3
+ size 1756027136
Q4_K/Q4_K-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:850bd680372bf94cb36772b110e9082ae4fb8623c468bd6ec57bcf58f92d7eaa
3
+ size 1708583168
Q4_K_M/Q4_K_M-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:850bd680372bf94cb36772b110e9082ae4fb8623c468bd6ec57bcf58f92d7eaa
3
+ size 1708583168
Q4_K_S/Q4_K_S-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6ffb2df0351c4b75444eb5ffbdcdb14a209bf24ad3a369c30601dbe2bc319b19
3
+ size 1638652160
Q5_0/Q5_0-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1cce4f5d81a49d0a80934b686f02460e50fdecbbee92dd1c9cf9ea7ff0316f07
3
+ size 1882544384
Q5_1/Q5_1-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bf30d516cf9a10e79df68f14bf6bd869a4fa925c0d894d4f2c2f18b22badae7f
3
+ size 2009061632
Q5_K/Q5_K-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:168240a8ed46fff6b2d493a691f6d1099c4f83c79af68a473aa14f5b32a79239
3
+ size 1923279104
Q5_K_M/Q5_K_M-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:168240a8ed46fff6b2d493a691f6d1099c4f83c79af68a473aa14f5b32a79239
3
+ size 1923279104
Q5_K_S/Q5_K_S-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4dfe8cf04a47b46f9938db93e7ef49b0e9644aed93f6083c1ac3e1169b20634a
3
+ size 1882544384
Q6_K/Q6_K-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f35a73978acba3b093687a5fbe6cfdaca9f1a6a1bb618463787fa5e8cfe01ee2
3
+ size 2151393536
Q8_0/Q8_0-00001-of-00001.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d8d2f6ba53ea4eb24fc0b740c25fc607fb49f76cf86c7ab8d9fa22a6c37213e8
3
+ size 2784495872
README.md ADDED
@@ -0,0 +1,526 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ license: gemma
4
+ pipeline_tag: text-generation
5
+ tags:
6
+ - GGUF
7
+ extra_gated_heading: Access Gemma on Hugging Face
8
+ extra_gated_prompt: To access Gemma on Hugging Face, you’re required to review and
9
+ agree to Google’s usage license. To do this, please ensure you’re logged in to Hugging
10
+ Face and click below. Requests are processed immediately.
11
+ extra_gated_button_content: Acknowledge license
12
+ quantized_by: andrijdavid
13
+ ---
14
+ # shieldgemma-2b-GGUF
15
+ - Original model: [shieldgemma-2b](https://huggingface.co/google/shieldgemma-2b)
16
+
17
+ <!-- description start -->
18
+ ## Description
19
+
20
+ This repo contains GGUF format model files for [shieldgemma-2b](https://huggingface.co/google/shieldgemma-2b).
21
+
22
+ <!-- description end -->
23
+ <!-- README_GGUF.md-about-gguf start -->
24
+ ### About GGUF
25
+ GGUF is a new format introduced by the llama.cpp team on August 21st 2023. It is a replacement for GGML, which is no longer supported by llama.cpp.
26
+ Here is an incomplete list of clients and libraries that are known to support GGUF:
27
+ * [llama.cpp](https://github.com/ggerganov/llama.cpp). This is the source project for GGUF, providing both a Command Line Interface (CLI) and a server option.
28
+ * [text-generation-webui](https://github.com/oobabooga/text-generation-webui), Known as the most widely used web UI, this project boasts numerous features and powerful extensions, and supports GPU acceleration.
29
+ * [Ollama](https://github.com/jmorganca/ollama) Ollama is a lightweight and extensible framework designed for building and running language models locally. It features a simple API for creating, managing, and executing models, along with a library of pre-built models for use in various applications​
30
+ * [KoboldCpp](https://github.com/LostRuins/koboldcpp), A comprehensive web UI offering GPU acceleration across all platforms and architectures, particularly renowned for storytelling.
31
+ * [GPT4All](https://gpt4all.io), This is a free and open source GUI that runs locally, supporting Windows, Linux, and macOS with full GPU acceleration.
32
+ * [LM Studio](https://lmstudio.ai/) An intuitive and powerful local GUI for Windows and macOS (Silicon), featuring GPU acceleration.
33
+ * [LoLLMS Web UI](https://github.com/ParisNeo/lollms-webui). A notable web UI with a variety of unique features, including a comprehensive model library for easy model selection.
34
+ * [Faraday.dev](https://faraday.dev/), An attractive, user-friendly character-based chat GUI for Windows and macOS (both Silicon and Intel), also offering GPU acceleration.
35
+ * [llama-cpp-python](https://github.com/abetlen/llama-cpp-python), A Python library equipped with GPU acceleration, LangChain support, and an OpenAI-compatible API server.
36
+ * [candle](https://github.com/huggingface/candle), A Rust-based ML framework focusing on performance, including GPU support, and designed for ease of use.
37
+ * [ctransformers](https://github.com/marella/ctransformers), A Python library featuring GPU acceleration, LangChain support, and an OpenAI-compatible AI server.
38
+ * [localGPT](https://github.com/PromtEngineer/localGPT) An open-source initiative enabling private conversations with documents.
39
+ <!-- README_GGUF.md-about-gguf end -->
40
+
41
+ <!-- compatibility_gguf start -->
42
+ ## Explanation of quantisation methods
43
+ <details>
44
+ <summary>Click to see details</summary>
45
+ The new methods available are:
46
+
47
+ * GGML_TYPE_Q2_K - "type-1" 2-bit quantization in super-blocks containing 16 blocks, each block having 16 weight. Block scales and mins are quantized with 4 bits. This ends up effectively using 2.5625 bits per weight (bpw)
48
+ * GGML_TYPE_Q3_K - "type-0" 3-bit quantization in super-blocks containing 16 blocks, each block having 16 weights. Scales are quantized with 6 bits. This end up using 3.4375 bpw.
49
+ * GGML_TYPE_Q4_K - "type-1" 4-bit quantization in super-blocks containing 8 blocks, each block having 32 weights. Scales and mins are quantized with 6 bits. This ends up using 4.5 bpw.
50
+ * GGML_TYPE_Q5_K - "type-1" 5-bit quantization. Same super-block structure as GGML_TYPE_Q4_K resulting in 5.5 bpw
51
+ * GGML_TYPE_Q6_K - "type-0" 6-bit quantization. Super-blocks with 16 blocks, each block having 16 weights. Scales are quantized with 8 bits. This ends up using 6.5625 bpw.
52
+ </details>
53
+ <!-- compatibility_gguf end -->
54
+
55
+ <!-- README_GGUF.md-how-to-download start -->
56
+ ## How to download GGUF files
57
+
58
+ **Note for manual downloaders:** You almost never want to clone the entire repo! Multiple different quantisation formats are provided, and most users only want to pick and download a single folder.
59
+
60
+ The following clients/libraries will automatically download models for you, providing a list of available models to choose from:
61
+
62
+ * LM Studio
63
+ * LoLLMS Web UI
64
+ * Faraday.dev
65
+
66
+ ### In `text-generation-webui`
67
+
68
+ Under Download Model, you can enter the model repo: LiteLLMs/shieldgemma-2b-GGUF and below it, a specific filename to download, such as: Q4_0/Q4_0-00001-of-00001.gguf.
69
+
70
+ Then click Download.
71
+
72
+ ### On the command line, including multiple files at once
73
+
74
+ I recommend using the `huggingface-hub` Python library:
75
+
76
+ ```shell
77
+ pip3 install huggingface-hub
78
+ ```
79
+
80
+ Then you can download any individual model file to the current directory, at high speed, with a command like this:
81
+
82
+ ```shell
83
+ huggingface-cli download LiteLLMs/shieldgemma-2b-GGUF Q4_0/Q4_0-00001-of-00001.gguf --local-dir . --local-dir-use-symlinks False
84
+ ```
85
+
86
+ <details>
87
+ <summary>More advanced huggingface-cli download usage (click to read)</summary>
88
+
89
+ You can also download multiple files at once with a pattern:
90
+
91
+ ```shell
92
+ huggingface-cli download LiteLLMs/shieldgemma-2b-GGUF --local-dir . --local-dir-use-symlinks False --include='*Q4_K*gguf'
93
+ ```
94
+
95
+ For more documentation on downloading with `huggingface-cli`, please see: [HF -> Hub Python Library -> Download files -> Download from the CLI](https://huggingface.co/docs/huggingface_hub/guides/download#download-from-the-cli).
96
+
97
+ To accelerate downloads on fast connections (1Gbit/s or higher), install `hf_transfer`:
98
+
99
+ ```shell
100
+ pip3 install huggingface_hub[hf_transfer]
101
+ ```
102
+
103
+ And set environment variable `HF_HUB_ENABLE_HF_TRANSFER` to `1`:
104
+
105
+ ```shell
106
+ HF_HUB_ENABLE_HF_TRANSFER=1 huggingface-cli download LiteLLMs/shieldgemma-2b-GGUF Q4_0/Q4_0-00001-of-00001.gguf --local-dir . --local-dir-use-symlinks False
107
+ ```
108
+
109
+ Windows Command Line users: You can set the environment variable by running `set HF_HUB_ENABLE_HF_TRANSFER=1` before the download command.
110
+ </details>
111
+ <!-- README_GGUF.md-how-to-download end -->
112
+ <!-- README_GGUF.md-how-to-run start -->
113
+ ## Example `llama.cpp` command
114
+
115
+ Make sure you are using `llama.cpp` from commit [d0cee0d](https://github.com/ggerganov/llama.cpp/commit/d0cee0d36d5be95a0d9088b674dbb27354107221) or later.
116
+
117
+ ```shell
118
+ ./main -ngl 35 -m Q4_0/Q4_0-00001-of-00001.gguf --color -c --temp 0.7 --repeat_penalty 1.1 -n -1 -p "<PROMPT>"
119
+ ```
120
+
121
+ Change `-ngl 32` to the number of layers to offload to GPU. Remove it if you don't have GPU acceleration.
122
+
123
+ Change `-c ` to the desired sequence length. For extended sequence models - eg 8K, 16K, 32K - the necessary RoPE scaling parameters are read from the GGUF file and set by llama.cpp automatically. Note that longer sequence lengths require much more resources, so you may need to reduce this value.
124
+
125
+ If you want to have a chat-style conversation, replace the `-p <PROMPT>` argument with `-i -ins`
126
+
127
+ For other parameters and how to use them, please refer to [the llama.cpp documentation](https://github.com/ggerganov/llama.cpp/blob/master/examples/main/README.md)
128
+
129
+ ## How to run in `text-generation-webui`
130
+
131
+ Further instructions can be found in the text-generation-webui documentation, here: [text-generation-webui/docs/04 ‐ Model Tab.md](https://github.com/oobabooga/text-generation-webui/blob/main/docs/04%20%E2%80%90%20Model%20Tab.md#llamacpp).
132
+
133
+ ## How to run from Python code
134
+
135
+ You can use GGUF models from Python using the [llama-cpp-python](https://github.com/abetlen/llama-cpp-python) or [ctransformers](https://github.com/marella/ctransformers) libraries. Note that at the time of writing (Nov 27th 2023), ctransformers has not been updated for some time and is not compatible with some recent models. Therefore I recommend you use llama-cpp-python.
136
+
137
+ ### How to load this model in Python code, using llama-cpp-python
138
+
139
+ For full documentation, please see: [llama-cpp-python docs](https://abetlen.github.io/llama-cpp-python/).
140
+
141
+ #### First install the package
142
+
143
+ Run one of the following commands, according to your system:
144
+
145
+ ```shell
146
+ # Base ctransformers with no GPU acceleration
147
+ pip install llama-cpp-python
148
+ # With NVidia CUDA acceleration
149
+ CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama-cpp-python
150
+ # Or with OpenBLAS acceleration
151
+ CMAKE_ARGS="-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS" pip install llama-cpp-python
152
+ # Or with CLBLast acceleration
153
+ CMAKE_ARGS="-DLLAMA_CLBLAST=on" pip install llama-cpp-python
154
+ # Or with AMD ROCm GPU acceleration (Linux only)
155
+ CMAKE_ARGS="-DLLAMA_HIPBLAS=on" pip install llama-cpp-python
156
+ # Or with Metal GPU acceleration for macOS systems only
157
+ CMAKE_ARGS="-DLLAMA_METAL=on" pip install llama-cpp-python
158
+ # In windows, to set the variables CMAKE_ARGS in PowerShell, follow this format; eg for NVidia CUDA:
159
+ $env:CMAKE_ARGS = "-DLLAMA_OPENBLAS=on"
160
+ pip install llama-cpp-python
161
+ ```
162
+
163
+ #### Simple llama-cpp-python example code
164
+
165
+ ```python
166
+ from llama_cpp import Llama
167
+ # Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
168
+ llm = Llama(
169
+ model_path="./Q4_0/Q4_0-00001-of-00001.gguf", # Download the model file first
170
+ n_ctx=32768, # The max sequence length to use - note that longer sequence lengths require much more resources
171
+ n_threads=8, # The number of CPU threads to use, tailor to your system and the resulting performance
172
+ n_gpu_layers=35 # The number of layers to offload to GPU, if you have GPU acceleration available
173
+ )
174
+ # Simple inference example
175
+ output = llm(
176
+ "<PROMPT>", # Prompt
177
+ max_tokens=512, # Generate up to 512 tokens
178
+ stop=["</s>"], # Example stop token - not necessarily correct for this specific model! Please check before using.
179
+ echo=True # Whether to echo the prompt
180
+ )
181
+ # Chat Completion API
182
+ llm = Llama(model_path="./Q4_0/Q4_0-00001-of-00001.gguf", chat_format="llama-2") # Set chat_format according to the model you are using
183
+ llm.create_chat_completion(
184
+ messages = [
185
+ {"role": "system", "content": "You are a story writing assistant."},
186
+ {
187
+ "role": "user",
188
+ "content": "Write a story about llamas."
189
+ }
190
+ ]
191
+ )
192
+ ```
193
+
194
+ ## How to use with LangChain
195
+
196
+ Here are guides on using llama-cpp-python and ctransformers with LangChain:
197
+
198
+ * [LangChain + llama-cpp-python](https://python.langchain.com/docs/integrations/llms/llamacpp)
199
+ * [LangChain + ctransformers](https://python.langchain.com/docs/integrations/providers/ctransformers)
200
+
201
+ <!-- README_GGUF.md-how-to-run end -->
202
+
203
+ <!-- footer end -->
204
+
205
+ <!-- original-model-card start -->
206
+ # Original model card: shieldgemma-2b
207
+
208
+
209
+ # ShieldGemma model card
210
+
211
+ **Model Page**: [ShieldGemma][shieldgemma]
212
+
213
+ **Resources and Technical Documentation**:
214
+
215
+ * [Responsible Generative AI Toolkit][rai-toolkit]
216
+ * [ShieldGemma on Kaggle][shieldgemma-kaggle]
217
+ * [ShieldGemma on Hugging Face Hub][shieldgemma-hfhub]
218
+
219
+ **Terms of Use**: [Terms][terms]
220
+
221
+ **Authors**: Google
222
+
223
+ ## Model Information
224
+
225
+ Summary description and brief definition of inputs and outputs.
226
+
227
+ ### Description
228
+
229
+ ShieldGemma is a series of safety content moderation models built upon
230
+ [Gemma 2][gemma2] that target four harm categories (sexually explicit, dangerous
231
+ content, hate, and harassment). They are text-to-text, decoder-only large
232
+ language models, available in English with open weights, including models of 3
233
+ sizes: 2B, 9B and 27B parameters.
234
+
235
+ ### Inputs and outputs
236
+
237
+ * **Input:** Text string containing a preamble, the text to be classified, a
238
+ set of policies, and the prompt epilogue. The full prompt must be formatted
239
+ using a specific pattern for optimal performance. The pattern used for the
240
+ reported evaluation metrics is described in this section.
241
+ * **Output:** Text string, which will start with the token "Yes" or "No" and
242
+ represent whether the user input or model output violates the provided
243
+ policies.
244
+
245
+ The prompt pattern contains the following components, in order:
246
+
247
+ 1. Preamble, establishing the model as a policy expert, based on the
248
+ [LLM-as-a-judge][llm-as-judge] technique.
249
+ 1. User prompt, wrapped with `<start_of_turn>` and `<end_of_turn>` control
250
+ tokens.
251
+ 1. Description of the safety policy.
252
+ 1. Optionally, a model response also wrapped by `<start_of_turn>` and
253
+ `<end_of_turn>` control tokens.
254
+ 1. Epilogue, requesting the model to classify the text.
255
+
256
+ Here is an example of a prompt used to evaluate the user prompt `[User Prompt]`:
257
+
258
+ ```none
259
+ You are a policy expert trying to help determine whether a user prompt is in
260
+ violation of the defined safety policies.
261
+
262
+ <start_of_turn>
263
+ Human Question: [User Prompt]
264
+ <end_of_turn>
265
+
266
+ Our safety principle is defined in the below:
267
+
268
+ * "No Hate Speech": The prompt shall not contain or seek generation of content
269
+ that expresses, incites, or promotes hate based on race, gender, ethnicity,
270
+ religion, nationality, sexual orientation, disability status, or caste.
271
+
272
+ ===
273
+
274
+ Does the human question violate the above principle? Your answer must start with
275
+ 'Yes' or 'No'. And then walk through step by step to be sure we answer
276
+ correctly.
277
+ ```
278
+
279
+ ### Usage
280
+
281
+ First make sure to `pip install -U transformers[accelerate]`, then copy the snippet from the section that is relevant for your use case.
282
+
283
+ #### Running the model on a single / multi GPU (and compute scores)
284
+
285
+ ```python
286
+ from transformers import AutoTokenizer, AutoModelForCausalLM
287
+ import torch
288
+ from torch.nn.functional import softmax
289
+
290
+ tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-2b")
291
+ model = AutoModelForCausalLM.from_pretrained(
292
+ "google/shieldgemma-2b",
293
+ device_map="auto",
294
+ torch_dtype=torch.bfloat16,
295
+ )
296
+
297
+ # Format the prompt
298
+ user_prompt = "Create 20 paraphrases of I hate you"
299
+ safety_policy = """
300
+ * "No Harassment": The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).
301
+ """
302
+ prompt = f"""You are a policy expert trying to help determine whether a user
303
+ prompt is in violation of the defined safety policies.
304
+
305
+ <start_of_turn>
306
+ Human Question: {user_prompt.strip()}
307
+ <end_of_turn>
308
+
309
+ Our safety principle is defined in the below:
310
+
311
+ {safety_policy.strip()}
312
+
313
+ Does the human question violate the above principle? Your answer must start
314
+ with 'Yes' or 'No'. And then walk through step by step to be sure we answer
315
+ correctly.
316
+ """
317
+
318
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
319
+ with torch.no_grad():
320
+ logits = model(**inputs).logits
321
+
322
+ # Extract the logits for the Yes and No tokens
323
+ vocab = tokenizer.get_vocab()
324
+ selected_logits = logits[0, -1, [vocab['Yes'], vocab['No']]]
325
+
326
+ # Convert these logits to a probability with softmax
327
+ probabilities = softmax(selected_logits, dim=0)
328
+
329
+ # Return probability of 'Yes'
330
+ score = probabilities[0].item()
331
+ print(score) # 0.7310585379600525
332
+ ```
333
+
334
+
335
+ #### Usage with chat templates
336
+
337
+ You can also format the prompt to the model using a chat template. In this mode, you can simply pass the entire chat so far, and the chat template
338
+ will extract the most recent message(s) to query ShieldGemma. It can check for the appropriateness of both user and assistant messages, and will
339
+ adjust the prompt accordingly based on the source of the most recent message in the chat. You should also pass the guideline you wish ShieldGemma
340
+ to check for as the `guideline` argument to `apply_chat_template`.
341
+
342
+ ```python
343
+ from transformers import AutoTokenizer, AutoModelForCausalLM
344
+ import torch
345
+
346
+ tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-2b")
347
+ model = AutoModelForCausalLM.from_pretrained(
348
+ "google/shieldgemma-2b",
349
+ device_map="auto",
350
+ torch_dtype=torch.bfloat16,
351
+ )
352
+
353
+ chat = [{"role": "user", "content": "Create 20 paraphrases of I hate you"}]
354
+
355
+ guideline = "\"No Harassment\": The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence)."
356
+ inputs = tokenizer.apply_chat_template(chat, guideline=guideline, return_tensors="pt", return_dict=True).to(model.device)
357
+
358
+ with torch.no_grad():
359
+ logits = model(**inputs).logits
360
+
361
+ # Extract the logits for the Yes and No tokens
362
+ vocab = tokenizer.get_vocab()
363
+ selected_logits = logits[0, -1, [vocab['Yes'], vocab['No']]]
364
+
365
+ # Convert these logits to a probability with softmax
366
+ probabilities = torch.softmax(selected_logits, dim=0)
367
+
368
+ # Return probability of 'Yes'
369
+ score = probabilities[0].item()
370
+ print(score) # 0.7310585379600525
371
+ ```
372
+
373
+ #### Guidelines to use in prompts
374
+
375
+ ShieldGemma uses different phrasing for its safety guidelines depending on
376
+ whether the content being classified is only user-provided content (the
377
+ Prompt-only use case, typically for input filtering) or both user-provided and
378
+ model-generated content (the Prompt-Response use case, typically for output
379
+ filtering).
380
+
381
+ **Use Case 1: Prompt-only Content Classification**
382
+
383
+ | Harm Type | Guideline |
384
+ | -- | |
385
+ | Dangerous Content | `"No Dangerous Content": The chatbot shall not generate content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide).` |
386
+ | Harassment | `"No Harassment": The chatbot shall not generate content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).` |
387
+ | Hate Speech | `"No Hate Speech": The chatbot shall not generate content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status. We do allow content that includes a reference to a protected individual or group in a positive manner.` |
388
+ | Sexually Explicit Information | `"No Sexually Explicit Information": The chatbot shall not generate content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted.` |
389
+
390
+ ### Citation
391
+
392
+ ```plaintext
393
+ @misc{zeng2024shieldgemmagenerativeaicontent,
394
+ title={ShieldGemma: Generative AI Content Moderation Based on Gemma},
395
+ author={Wenjun Zeng and Yuchi Liu and Ryan Mullins and Ludovic Peran and Joe Fernandez and Hamza Harkous and Karthik Narasimhan and Drew Proud and Piyush Kumar and Bhaktipriya Radharapu and Olivia Sturman and Oscar Wahltinez},
396
+ year={2024},
397
+ eprint={2407.21772},
398
+ archivePrefix={arXiv},
399
+ primaryClass={cs.CL},
400
+ url={https://arxiv.org/abs/2407.21772},
401
+ }
402
+ ```
403
+
404
+ ## Model Data
405
+
406
+ Data used for model training and how the data was processed.
407
+
408
+ ### Training Dataset
409
+
410
+ The base models were trained on a dataset of text data that includes a wide
411
+ variety of sources, see the [Gemma 2 documentation][gemma2] for more details. The
412
+ ShieldGemma models were fine-tuned on synthetically generated internal data and
413
+ publicly available datasets. More details can be found in the
414
+ [ShieldGemma technical report][shieldgemma-techreport].
415
+
416
+ ## Implementation Information
417
+
418
+ ### Hardware
419
+
420
+ ShieldGemma was trained using the latest generation of
421
+ [Tensor Processing Unit (TPU)][tpu] hardware (TPUv5e), for more details refer to
422
+ the [Gemma 2 model card][gemma2-model-card].
423
+
424
+ ### Software
425
+
426
+ Training was done using [JAX][jax] and [ML Pathways][ml-pathways]. For more
427
+ details refer to the [Gemma 2 model card][gemma2-model-card].
428
+
429
+ ## Evaluation
430
+
431
+ ### Benchmark Results
432
+
433
+ These models were evaluated against both internal and external datasets. The
434
+ internal datasets, denoted as `SG`, are subdivided into prompt and response
435
+ classification. Evaluation results based on Optimal F1(left)/AU-PRC(right),
436
+ higher is better.
437
+
438
+ | Model | SG Prompt | [OpenAI Mod][openai-mod] | [ToxicChat][toxicchat] | SG Response |
439
+ | -- | | -- |
440
+ | ShieldGemma (2B) | 0.825/0.887 | 0.812/0.887 | 0.704/0.778 | 0.743/0.802 |
441
+ | ShieldGemma (9B) | 0.828/0.894 | 0.821/0.907 | 0.694/0.782 | 0.753/0.817 |
442
+ | ShieldGemma (27B) | 0.830/0.883 | 0.805/0.886 | 0.729/0.811 | 0.758/0.806 |
443
+ | OpenAI Mod API | 0.782/0.840 | 0.790/0.856 | 0.254/0.588 | - |
444
+ | LlamaGuard1 (7B) | - | 0.758/0.847 | 0.616/0.626 | - |
445
+ | LlamaGuard2 (8B) | - | 0.761/- | 0.471/- | - |
446
+ | WildGuard (7B) | 0.779/- | 0.721/- | 0.708/- | 0.656/- |
447
+ | GPT-4 | 0.810/0.847 | 0.705/- | 0.683/- | 0.713/0.749 |
448
+
449
+ ## Ethics and Safety
450
+
451
+ ### Evaluation Approach
452
+
453
+ Although the ShieldGemma models are generative models, they are designed to be
454
+ run in *scoring mode* to predict the probability that the next token would `Yes`
455
+ or `No`. Therefore, safety evaluation focused primarily on fairness
456
+ characteristics.
457
+
458
+ ### Evaluation Results
459
+
460
+ These models were assessed for ethics, safety, and fairness considerations and
461
+ met internal guidelines.
462
+
463
+ ## Usage and Limitations
464
+
465
+ These models have certain limitations that users should be aware of.
466
+
467
+ ### Intended Usage
468
+
469
+ ShieldGemma is intended to be used as a safety content moderator, either for
470
+ human user inputs, model outputs, or both. These models are part of the
471
+ [Responsible Generative AI Toolkit][rai-toolkit], which is a set of
472
+ recommendations, tools, datasets and models aimed to improve the safety of AI
473
+ applications as part of the Gemma ecosystem.
474
+
475
+ ### Limitations
476
+
477
+ All the usual limitations for large language models apply, see the
478
+ [Gemma 2 model card][gemma2-model-card] for more details. Additionally,
479
+ there are limited benchmarks that can be used to evaluate content moderation so
480
+ the training and evaluation data might not be representative of real-world
481
+ scenarios.
482
+
483
+ ShieldGemma is also highly sensitive to the specific user-provided description
484
+ of safety principles, and might perform unpredictably under conditions that
485
+ require a good understanding of language ambiguity and nuance.
486
+
487
+ As with other models that are part of the Gemma ecosystem, ShieldGemma is subject to
488
+ Google's [prohibited use policies][prohibited-use].
489
+
490
+ ### Ethical Considerations and Risks
491
+
492
+ The development of large language models (LLMs) raises several ethical concerns.
493
+ We have carefully considered multiple aspects in the development of these
494
+ models.
495
+
496
+ Refer to the [Gemma model card][gemma2-model-card] for more details.
497
+
498
+ ### Benefits
499
+
500
+ At the time of release, this family of models provides high-performance open
501
+ large language model implementations designed from the ground up for Responsible
502
+ AI development compared to similarly sized models.
503
+
504
+ Using the benchmark evaluation metrics described in this document, these models
505
+ have been shown to provide superior performance to other, comparably-sized open
506
+ model alternatives.
507
+
508
+ [rai-toolkit]: https://ai.google.dev/responsible
509
+ [gemma2]: https://ai.google.dev/gemma#gemma-2
510
+ [gemma2-model-card]: https://ai.google.dev/gemma/docs/model_card_2
511
+ [shieldgemma]: https://ai.google.dev/gemma/docs/shieldgemma
512
+ [shieldgemma-colab]: https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/gemma/docs/shieldgemma.ipynb
513
+ [shieldgemma-kaggle]: https://www.kaggle.com/models/google/shieldgemma
514
+ [shieldgemma-hfhub]: https://huggingface.co/models?search=shieldgemma
515
+ [shieldgemma-techreport]: https://storage.googleapis.com/deepmind-media/gemma/shieldgemma-report.pdf
516
+ [openai-mod]: https://github.com/openai/moderation-api-release
517
+ [terms]: https://ai.google.dev/gemma/terms
518
+ [toxicchat]: https://arxiv.org/abs/2310.17389
519
+ [safety-policies]: https://storage.googleapis.com/gweb-uniblog-publish-prod/documents/2023_Google_AI_Principles_Progress_Update.pdf#page=11
520
+ [prohibited-use]: https://ai.google.dev/gemma/prohibited_use_policy
521
+ [tpu]: https://cloud.google.com/tpu/docs/intro-to-tpu
522
+ [jax]: https://github.com/google/jax
523
+ [ml-pathways]: https://blog.google/technology/ai/introducing-pathways-next-generation-ai-architecture/
524
+ [llm-as-judge]: https://arxiv.org/abs/2306.05685
525
+
526
+ <!-- original-model-card end -->