Views
No views yet
1import torch
2import requests
3from PIL import Image
4from transformers import AutoImageProcessor, AutoModel
5
6image = Image.open(
7 requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw
8).convert("RGB")
9
10processor = AutoImageProcessor.from_pretrained("kittn/eupe_vits16")
11model = AutoModel.from_pretrained("kittn/eupe_vits16").eval().to("cuda")
12
13inputs = processor(images=image, return_tensors="pt", size={"height": 512, "width": 512}).to("cuda")
14
15with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
16 outputs = model(**inputs)
17
18print("clstoken:", outputs.last_hidden_state[:, 0].shape) # torch.Size([1, 384])
19print("patchtokens:", outputs.last_hidden_state[:, 1 + model.config.num_register_tokens :].shape) # torch.Size([1, 1024, 384])
20print("pooler_output:", outputs.pooler_output.shape) # torch.Size([1, 384])last_hidden_state contains:0: CLS token1:5: 4 register tokens384126416torch.autocast("cuda", dtype=torch.bfloat16) rather than hard-casting the full model to bfloat16.periods in the checkpoint and computing angles as coords / periods, while Hugging Face reconstructs fp32 inv_freq from rope_theta and computes coords * inv_freq.model in the example above. It patches the already-loaded Hugging Face model to use the exact bf16-rounded periods and the reference RoPE forward:1import math
2from types import MethodType
3
4rope = model.rope_embeddings
5head_dim = model.config.hidden_size // model.config.num_attention_heads
6periods = (rope.base ** (torch.arange(head_dim // 4, dtype=torch.float32, device=rope.inv_freq.device) * (4.0 / head_dim))).to(torch.bfloat16).to(torch.float32)
7rope.register_buffer("periods", periods, persistent=False)
8
9
10def forward(self, pixel_values):
11 _, _, height, width = pixel_values.shape
12 num_patches_h = height // self.config.patch_size
13 num_patches_w = width // self.config.patch_size
14
15 coords_h = torch.arange(0.5, num_patches_h, device=pixel_values.device, dtype=torch.float32) / num_patches_h
16 coords_w = torch.arange(0.5, num_patches_w, device=pixel_values.device, dtype=torch.float32) / num_patches_w
17 coords = torch.stack(torch.meshgrid(coords_h, coords_w, indexing="ij"), dim=-1).flatten(0, 1)
18 coords = 2.0 * coords - 1.0
19
20 angles = 2 * math.pi * coords[:, :, None] / self.periods[None, None, :]
21 angles = angles.flatten(1, 2).tile(2)
22
23 cos = torch.cos(angles).to(dtype=pixel_values.dtype)
24 sin = torch.sin(angles).to(dtype=pixel_values.dtype)
25 return cos, sin
26
27
28rope.forward = MethodType(forward, rope)bf16 autocast.