Skip to content

Commit 08b5a5b

Browse files
fix: remove device_map to prevent TableTransformer meta tensor errors
Remove device_map parameter from from_pretrained() calls to fix meta tensor errors during concurrent processing. Changes: - Device normalization (cuda -> cuda:0) for consistent caching - Remove device_map from DetrImageProcessor.from_pretrained() - Remove device_map from TableTransformerForObjectDetection.from_pretrained() - Add explicit .to(device, dtype=torch.float32) for proper placement - Improve logging to show target device Fixes: - "Trying to set a tensor of type Float but got Meta" errors - AssertionError during concurrent PDF processing - Finalization race conditions with device_map Root Cause: device_map causes models to initialize with meta tensors, which fail when explicitly moved to device. Removing device_map and using explicit .to() ensures proper tensor placement. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent a00e748 commit 08b5a5b

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 1.1.9
2+
3+
### Fix
4+
- **TableTransformer device_map fix**: Remove device_map parameter to prevent meta tensor errors
5+
- Device normalization (cuda -> cuda:0) for consistent caching
6+
- Load models without device_map, use explicit .to(device, dtype=torch.float32)
7+
- Fixes concurrent PDF processing AssertionError
8+
- Prevents "Trying to set a tensor of type Float but got Meta" errors
9+
110
## 1.1.8
211

312
- put `pdfium` call behind a thread lock

unstructured_inference/models/tables.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,42 @@ def initialize(
7070
model: Union[str, Path],
7171
device: Optional[str] = "cuda" if torch.cuda.is_available() else "cpu",
7272
):
73-
"""Loads the donut model using the specified parameters"""
73+
"""Loads the table transformer model using the specified parameters.
74+
75+
Device placement strategy:
76+
- Normalize device names (cuda -> cuda:0) for consistent caching
77+
- Load models WITHOUT device_map to avoid meta tensor errors
78+
- Use explicit .to(device, dtype=torch.float32) for proper placement
79+
"""
80+
# Device normalization for consistent caching
81+
if device is None:
82+
device = "cuda" if torch.cuda.is_available() else "cpu"
83+
if device.startswith("cuda") and ":" not in device:
84+
device = f"cuda:{torch.cuda.current_device()}"
85+
7486
self.device = device
75-
self.feature_extractor = DetrImageProcessor.from_pretrained(model, device_map=self.device)
87+
88+
# Load feature extractor WITHOUT device_map
89+
self.feature_extractor = DetrImageProcessor.from_pretrained(model)
7690
# value not set in the configuration and needed for newer models
7791
# https://huggingface.co/microsoft/table-transformer-structure-recognition-v1.1-all/discussions/1
7892
self.feature_extractor.size["shortest_edge"] = inference_config.IMG_PROCESSOR_SHORTEST_EDGE
7993
self.feature_extractor.size["longest_edge"] = inference_config.IMG_PROCESSOR_LONGEST_EDGE
8094

8195
try:
82-
logger.info("Loading the table structure model ...")
96+
logger.info(f"Loading table structure model to {self.device}...")
8397
cached_current_verbosity = logging.get_verbosity()
8498
logging.set_verbosity_error()
85-
self.model = TableTransformerForObjectDetection.from_pretrained(
86-
model,
87-
device_map=self.device,
88-
)
99+
100+
# Load model WITHOUT device_map (prevents meta tensor errors)
101+
self.model = TableTransformerForObjectDetection.from_pretrained(model)
102+
103+
# Explicit device placement with dtype
104+
self.model.to(self.device, dtype=torch.float32)
105+
89106
logging.set_verbosity(cached_current_verbosity)
90107
self.model.eval()
108+
logger.info(f"Table model successfully loaded to {self.device}")
91109

92110
except EnvironmentError:
93111
logger.critical("Failed to initialize the model.")

0 commit comments

Comments
 (0)