Datasets:
Download code/debug_phi.py from spatial-evidence-bench/SpatialEvidenceBench: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/datasets/spatial-evidence-bench/SpatialEvidenceBench/resolve/5b9da172cb3f2364faca8e32cb8bf6422d0f0bb8/code/debug_phi.py
- Command line
-
hf download hf://datasets/spatial-evidence-bench/SpatialEvidenceBench@5b9da172cb3f2364faca8e32cb8bf6422d0f0bb8/code/debug_phi.py
-
curl -L -o debug_phi.py https://huggingface.co/datasets/spatial-evidence-bench/SpatialEvidenceBench/resolve/5b9da172cb3f2364faca8e32cb8bf6422d0f0bb8/code/debug_phi.py
1.39 kB
| import torch | |
| from PIL import Image | |
| from transformers import AutoProcessor, AutoModelForCausalLM | |
| path = "/repository/models/Phi-3.5-vision-instruct" | |
| processor = AutoProcessor.from_pretrained(path, trust_remote_code=True) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| path, torch_dtype=torch.bfloat16, device_map="cuda:0", | |
| trust_remote_code=True, attn_implementation="eager", | |
| ) | |
| tok = processor.tokenizer | |
| print("yes_ids", tok.encode("yes", add_special_tokens=False)[:3]) | |
| print("no_ids", tok.encode("no", add_special_tokens=False)[:3]) | |
| print("eos", model.generation_config.eos_token_id, "pad", tok.pad_token_id) | |
| text = ("<|user|>\n<|image_1|>\nIs the cup to the left of the box? " | |
| "Respond with exactly one word: yes or no.<|end|>\n<|assistant|>\n") | |
| inputs = processor(text=text, images=[Image.open("dataset/render_all/left_right_0000_a.png")], | |
| return_tensors="pt").to("cuda:0") | |
| dtype = next(model.parameters()).dtype | |
| inputs = {k: (v.to(dtype) if torch.is_floating_point(v) else v) for k, v in inputs.items()} | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits[0, -1] | |
| values, indices = torch.topk(logits, 6) | |
| print("top", [(tok.decode([int(i)]), round(float(v), 2)) for v, i in zip(values, indices)]) | |
| generated = model.generate(**inputs, max_new_tokens=6, do_sample=False) | |
| print("gen", repr(tok.decode(generated[0, inputs["input_ids"].shape[1]:]))) | |