Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions tensorrt_llm/_torch/visual_gen/checkpoints/weight_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Weight loader for diffusion models."""
Comment thread
coderabbitai[bot] marked this conversation as resolved.

import json
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from typing import Any, Dict, List, Union

Expand Down Expand Up @@ -87,12 +88,7 @@ def load_weights(
if not weight_files:
raise ValueError(f"No weight files found in {weight_dir}")

# Load all weights with progress bar
component_weights = {}
desc = f"Loading {component}" if is_pipeline else "Loading checkpoint"
for wf in tqdm.tqdm(weight_files, desc=desc):
component_weights.update(self._load_file(wf))

component_weights = self._load_weight_files(weight_files, component, is_pipeline)
all_weights[component] = component_weights

# Return flat dict for single component (backward compatibility)
Expand All @@ -102,6 +98,32 @@ def load_weights(
# Return nested dict for multiple components
return all_weights

def _load_weight_files(
self, weight_files: List[str], component: str, is_pipeline: bool
) -> Dict[str, Any]:
desc = f"Loading {component}" if is_pipeline else "Loading checkpoint"
if len(weight_files) <= 1:
component_weights = {}
for wf in tqdm.tqdm(weight_files, desc=desc):
component_weights.update(self._load_file(wf))
return component_weights

workers = min(4, len(weight_files))

logger.info(f"Loading {len(weight_files)} {component} shard files with {workers} workers")
component_weights = {}
with ThreadPoolExecutor(max_workers=workers) as executor:
futures = {executor.submit(self._load_file, wf): wf for wf in weight_files}
for future in tqdm.tqdm(as_completed(futures), total=len(futures), desc=desc):
wf = futures[future]
try:
loaded = future.result()
except Exception as exc:
raise RuntimeError(f"Failed to load weight file {wf}") from exc
component_weights.update(loaded)

return component_weights

def _find_weight_files(self, weight_dir) -> List[str]:
"""Find safetensors or bin weight files.

Expand Down
Loading