File size: 1,679 Bytes
7e7a152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Native visual HVS adapter used by visual initialization."""
from .registry import MODELS
class Actor:
 def __init__(self,name,out,max_pixels=65536):
  import torch
  from transformers import AutoProcessor,Qwen2_5_VLForConditionalGeneration
  self.name=name;self.profile=MODELS[name];self.kind=self.profile['kind'];self.out=out;self.max_pixels=max_pixels
  assert self.kind=='vl' and not self.profile['audio']
  self.proc=AutoProcessor.from_pretrained(self.profile['path'],local_files_only=True,min_pixels=3136,max_pixels=max_pixels,use_fast=True)
  self.model=Qwen2_5_VLForConditionalGeneration.from_pretrained(self.profile['path'],local_files_only=True,torch_dtype=torch.bfloat16,attn_implementation='sdpa').cuda().eval()
  ip=self.proc.image_processor;ip.min_pixels=3136;ip.max_pixels=max_pixels
  if isinstance(getattr(ip,'size',None),dict):ip.size.update(shortest_edge=3136,longest_edge=max_pixels)
  self.model.requires_grad_(False)
 def prepare_inputs(self,msgs,pics,audio=None):
  assert audio is None
  text=self.proc.apply_chat_template(msgs,tokenize=False,add_generation_prompt=True)
  batch=dict(self.proc(text=[text],images=pics,return_tensors='pt',padding=True))
  assert batch['input_ids'].shape[1]<=4096
  return batch
 def generate(self,msgs,pics,audio=None,instruction='',pose=None):
  import torch
  x={k:v.cuda() for k,v in self.prepare_inputs(msgs,pics,audio).items()};n=x['input_ids'].shape[1]
  with torch.inference_mode(),torch.autocast('cuda',dtype=torch.bfloat16):
   y=self.model.generate(**x,max_new_tokens=512,do_sample=False,use_cache=True,pad_token_id=self.proc.tokenizer.eos_token_id)
  return self.proc.decode(y[0,n:],skip_special_tokens=True)