don0726 commited on
Commit
30ea7f2
·
verified ·
1 Parent(s): 310544f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -6,12 +6,13 @@ built-in "Free OCR" prompt mode (plain text extraction, no markdown/
6
  grounding boxes). A "Markdown" mode is also included since it's a
7
  one-line prompt swap.
8
 
9
- This version runs on CPU only (no CUDA, no flash-attention, float32
10
- instead of bfloat16). DeepSeek-OCR is a vision-language model, so CPU
11
- inference works but is slow — expect anywhere from ~30s to a few
12
- minutes per image depending on resolution preset and CPU. Use the
13
- "Tiny" or "Small" resolution presets for faster (if lower-fidelity)
14
- results on CPU.
 
15
  """
16
 
17
  import os
@@ -43,7 +44,13 @@ model = AutoModel.from_pretrained(
43
  )
44
  # CPU doesn't support flash-attention or bfloat16 matmul well — stay in
45
  # float32 on the CPU device.
46
- model = model.eval().to(DEVICE).to(torch.float32)
 
 
 
 
 
 
47
 
48
  # ---------------------------------------------------------------------------
49
  # DeepSeek-OCR's own `model.infer()` method has `.cuda()` calls hardcoded
 
6
  grounding boxes). A "Markdown" mode is also included since it's a
7
  one-line prompt swap.
8
 
9
+ This version runs on CPU only (no CUDA, no flash-attention). The model
10
+ stays in bfloat16, matching hardcoded dtype casts inside its own custom
11
+ code — PyTorch supports bfloat16 tensors on CPU, just without hardware
12
+ acceleration, so it works but is slow. DeepSeek-OCR is a vision-language
13
+ model, so expect anywhere from ~30s to a few minutes per image depending
14
+ on resolution preset and CPU. Use the "Tiny" or "Small" resolution
15
+ presets for faster (if lower-fidelity) results.
16
  """
17
 
18
  import os
 
44
  )
45
  # CPU doesn't support flash-attention or bfloat16 matmul well — stay in
46
  # float32 on the CPU device.
47
+ # NOTE: don't cast to float32 here. DeepSeek-OCR's own forward pass hardcodes
48
+ # casting image inputs to bfloat16 internally (independent of the model's
49
+ # actual dtype), so the model's weights must also be bfloat16 or you get a
50
+ # "Input type (BFloat16) and bias type (float) should be the same" crash.
51
+ # PyTorch CPU does support bfloat16 tensors/ops (just without hardware
52
+ # acceleration), so this works, it's just slow.
53
+ model = model.eval().to(DEVICE).to(torch.bfloat16)
54
 
55
  # ---------------------------------------------------------------------------
56
  # DeepSeek-OCR's own `model.infer()` method has `.cuda()` calls hardcoded