Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize RAM to VRAM transfer #6312

Merged
merged 11 commits into from
May 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CacheRecord(Generic[T]):

key: str
model: T
state_dict: Optional[Dict[str, torch.Tensor]] # this is a copy that stays in CPU
lstein marked this conversation as resolved.
Show resolved Hide resolved
size: int
loaded: bool = False
_locks: int = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def put(
if key in self._cached_models:
return
self.make_room(size)
cache_record = CacheRecord(key, model, size)
state_dict = model.state_dict() if hasattr(model, "state_dict") else None
RyanJDick marked this conversation as resolved.
Show resolved Hide resolved
cache_record = CacheRecord(key=key, model=model, state_dict=state_dict, size=size)
self._cached_models[key] = cache_record
self._cache_stack.append(key)

Expand Down Expand Up @@ -267,6 +268,13 @@ def move_model_to_device(self, cache_entry: CacheRecord[AnyModel], target_device
start_model_to_time = time.time()
snapshot_before = self._capture_memory_snapshot()
try:
if target_device == self.storage_device:
cache_entry.model.load_state_dict(cache_entry.state_dict, assign=True)
else:
new_dict: Dict[str, torch.Tensor] = {}
for k, v in cache_entry.state_dict.items():
new_dict[k] = v.to(torch.device(target_device), copy=True)
cache_entry.model.load_state_dict(new_dict, assign=True)
RyanJDick marked this conversation as resolved.
Show resolved Hide resolved
RyanJDick marked this conversation as resolved.
Show resolved Hide resolved
cache_entry.model.to(target_device)
except Exception as e: # blow away cache entry
self._delete_cache_entry(cache_entry)
Expand Down