-
Notifications
You must be signed in to change notification settings - Fork 3
Persistent trainer + CPU cache for local engine #32
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6607a2c
Persistent trainer + CPU cache for local training engine
166a16d
Move cache types and optimizer state helpers into local engine
ecf921b
Move cache types into separate types.py module
a125d23
Raise TypeError instead of silent fallback in _build_cache_entry
bbb9a49
Fix race in _ensure_model_loaded and cache key mismatch
79fe513
Fix CI: type errors and missing system_prompt in tests
835b46d
Add type safety and code style guidelines to CLAUDE.md
7f18d52
Fix ruff I001: merge peft imports in _build_cache_entry
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """CPU-resident optimizer state helpers for the local training engine.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import copy | ||
| from typing import cast | ||
|
|
||
| import torch | ||
|
|
||
| # Re-export types for backward compatibility | ||
| from claas.training.engine.local.types import ( # noqa: F401 | ||
| DistillStepResult, | ||
| LoraAdapterConfig, | ||
| LoraCacheEntry, | ||
| ) | ||
|
|
||
|
|
||
| def cpu_optimizer_state(state_dict: dict[str, object]) -> dict[str, object]: | ||
| """Deep-copy optimizer state with all tensors moved to CPU.""" | ||
| result: dict[str, object] = {} | ||
| for key, value in state_dict.items(): | ||
| if key == "state": | ||
| param_states = cast("dict[int, dict[str, object]]", value) | ||
| cpu_states: dict[int, dict[str, object]] = {} | ||
| for param_id, param_state in param_states.items(): | ||
| cpu_param: dict[str, object] = {} | ||
| for k, v in param_state.items(): | ||
| if isinstance(v, torch.Tensor): | ||
| cpu_param[k] = v.detach().cpu().clone() | ||
| else: | ||
| cpu_param[k] = copy.deepcopy(v) | ||
| cpu_states[param_id] = cpu_param | ||
| result[key] = cpu_states | ||
| else: | ||
| result[key] = copy.deepcopy(value) | ||
| return result | ||
|
|
||
|
|
||
| def gpu_optimizer_state( | ||
| state_dict: dict[str, object], | ||
| device: torch.device, | ||
| ) -> dict[str, object]: | ||
| """Deep-copy optimizer state with all tensors moved to a target device.""" | ||
| result: dict[str, object] = {} | ||
| for key, value in state_dict.items(): | ||
| if key == "state": | ||
| param_states = cast("dict[int, dict[str, object]]", value) | ||
| gpu_states: dict[int, dict[str, object]] = {} | ||
| for param_id, param_state in param_states.items(): | ||
| gpu_param: dict[str, object] = {} | ||
| for k, v in param_state.items(): | ||
| if isinstance(v, torch.Tensor): | ||
| gpu_param[k] = v.detach().to(device).clone() | ||
| else: | ||
| gpu_param[k] = copy.deepcopy(v) | ||
| gpu_states[param_id] = gpu_param | ||
| result[key] = gpu_states | ||
| else: | ||
| result[key] = copy.deepcopy(value) | ||
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.