Ketengan-Diffusion-Lab commited on
Commit
225c3f2
·
verified ·
1 Parent(s): f4dc684

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -10,24 +10,19 @@ transformers.logging.set_verbosity_error()
10
  transformers.logging.disable_progress_bar()
11
  warnings.filterwarnings('ignore')
12
 
13
- # set device
14
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
15
 
16
  model_name = 'cognitivecomputations/dolphin-vision-7b'
17
 
18
- # create model and load it to the specified device
19
  model = AutoModelForCausalLM.from_pretrained(
20
  model_name,
21
- torch_dtype=torch.float16,
 
22
  trust_remote_code=True
23
  )
24
- model.to(device) # Explicitly move the model to the device
25
-
26
- # Ensure all model components are on the same device
27
- for param in model.parameters():
28
- param.data = param.data.to(device)
29
- for buffer in model.buffers():
30
- buffer.data = buffer.data.to(device)
31
 
32
  tokenizer = AutoTokenizer.from_pretrained(
33
  model_name,
@@ -45,18 +40,22 @@ def inference(prompt, image):
45
  )
46
 
47
  text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
48
- input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0).to(device)
 
 
49
 
50
- image_tensor = model.process_images([image], model.config).to(device)
 
 
 
51
 
52
  # generate
53
- with torch.cuda.amp.autocast():
54
- output_ids = model.generate(
55
- input_ids,
56
- images=image_tensor,
57
- max_new_tokens=2048,
58
- use_cache=True
59
- )[0]
60
 
61
  return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
62
 
 
10
  transformers.logging.disable_progress_bar()
11
  warnings.filterwarnings('ignore')
12
 
13
+ # Force CPU usage
14
+ device = torch.device("cpu")
15
+ torch.set_default_tensor_type(torch.FloatTensor)
16
 
17
  model_name = 'cognitivecomputations/dolphin-vision-7b'
18
 
19
+ # create model and load it to CPU
20
  model = AutoModelForCausalLM.from_pretrained(
21
  model_name,
22
+ torch_dtype=torch.float32, # Use float32 for CPU
23
+ device_map={'': device},
24
  trust_remote_code=True
25
  )
 
 
 
 
 
 
 
26
 
27
  tokenizer = AutoTokenizer.from_pretrained(
28
  model_name,
 
40
  )
41
 
42
  text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
43
+ input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
44
+
45
+ image_tensor = model.process_images([image], model.config)
46
 
47
+ # Add debug prints
48
+ print(f"Device of model: {next(model.parameters()).device}")
49
+ print(f"Device of input_ids: {input_ids.device}")
50
+ print(f"Device of image_tensor: {image_tensor.device}")
51
 
52
  # generate
53
+ output_ids = model.generate(
54
+ input_ids,
55
+ images=image_tensor,
56
+ max_new_tokens=2048,
57
+ use_cache=True
58
+ )[0]
 
59
 
60
  return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
61