Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -154,8 +154,11 @@ from huggingface_hub import hf_hub_download
|
|
| 154 |
from transformers import PreTrainedTokenizerFast, Gemma3ForConditionalGeneration
|
| 155 |
|
| 156 |
repo = "shibatch/tinygemma3ocr2m"
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
path = hf_hub_download(repo, "sample_images/sample_00_6235317.png")
|
| 161 |
img = Image.open(path).convert("RGB")
|
|
@@ -164,7 +167,19 @@ pix = torch.from_numpy(np.asarray(img, dtype=np.float32) / 127.5 - 1).permute(2,
|
|
| 164 |
ids = [tok.bos_token_id] + [model.config.image_token_index] * model.config.mm_tokens_per_image
|
| 165 |
ids += tok.encode("\nRead the digits.\n", add_special_tokens=False)
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
print(tok.decode(out[0][len(ids):], skip_special_tokens=True))
|
| 169 |
```
|
| 170 |
|
|
@@ -192,11 +207,13 @@ import torch
|
|
| 192 |
from transformers import PreTrainedTokenizerFast, Gemma3ForConditionalGeneration
|
| 193 |
|
| 194 |
repo = "shibatch/tinygemma3ocr2m"
|
| 195 |
-
tok = PreTrainedTokenizerFast.from_pretrained(repo)
|
| 196 |
-
model = Gemma3ForConditionalGeneration.from_pretrained(
|
|
|
|
|
|
|
| 197 |
|
| 198 |
ids = [tok.bos_token_id] + tok.encode("Once upon", add_special_tokens=False)
|
| 199 |
-
x = torch.tensor([ids]
|
| 200 |
|
| 201 |
for _ in range(50):
|
| 202 |
h = model.model.language_model(input_ids=x, use_cache=False, return_dict=True).last_hidden_state
|
|
|
|
| 154 |
from transformers import PreTrainedTokenizerFast, Gemma3ForConditionalGeneration
|
| 155 |
|
| 156 |
repo = "shibatch/tinygemma3ocr2m"
|
| 157 |
+
|
| 158 |
+
tok = PreTrainedTokenizerFast.from_pretrained(repo, subfolder="hf")
|
| 159 |
+
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 160 |
+
repo, subfolder="hf", torch_dtype=torch.bfloat16
|
| 161 |
+
).cuda().eval()
|
| 162 |
|
| 163 |
path = hf_hub_download(repo, "sample_images/sample_00_6235317.png")
|
| 164 |
img = Image.open(path).convert("RGB")
|
|
|
|
| 167 |
ids = [tok.bos_token_id] + [model.config.image_token_index] * model.config.mm_tokens_per_image
|
| 168 |
ids += tok.encode("\nRead the digits.\n", add_special_tokens=False)
|
| 169 |
|
| 170 |
+
input_ids = torch.tensor([ids], device="cuda")
|
| 171 |
+
attention_mask = torch.ones_like(input_ids)
|
| 172 |
+
|
| 173 |
+
out = model.generate(
|
| 174 |
+
input_ids=input_ids,
|
| 175 |
+
attention_mask=attention_mask,
|
| 176 |
+
pixel_values=pix,
|
| 177 |
+
max_new_tokens=12,
|
| 178 |
+
do_sample=False,
|
| 179 |
+
pad_token_id=tok.bos_token_id,
|
| 180 |
+
eos_token_id=tok.eos_token_id,
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
print(tok.decode(out[0][len(ids):], skip_special_tokens=True))
|
| 184 |
```
|
| 185 |
|
|
|
|
| 207 |
from transformers import PreTrainedTokenizerFast, Gemma3ForConditionalGeneration
|
| 208 |
|
| 209 |
repo = "shibatch/tinygemma3ocr2m"
|
| 210 |
+
tok = PreTrainedTokenizerFast.from_pretrained(repo, subfolder="hf")
|
| 211 |
+
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 212 |
+
repo, subfolder="hf", torch_dtype=torch.bfloat16
|
| 213 |
+
).cuda().eval()
|
| 214 |
|
| 215 |
ids = [tok.bos_token_id] + tok.encode("Once upon", add_special_tokens=False)
|
| 216 |
+
x = torch.tensor([ids], device="cuda")
|
| 217 |
|
| 218 |
for _ in range(50):
|
| 219 |
h = model.model.language_model(input_ids=x, use_cache=False, return_dict=True).last_hidden_state
|