Core¶
The core module provides model loading, hook management, tensor utilities, and device management that power the rest of Model Garage.
ModelLoader¶
ModelLoader
¶
Standardized model loading for Model Garage.
Handles: - HuggingFace models - Device placement - Memory optimization - Model info extraction
Source code in src/model_garage/core/loader.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
__init__
¶
Initialize loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
Optional[str]
|
Target device ("cuda", "cpu", "auto"). Default: auto-detect. |
None
|
Source code in src/model_garage/core/loader.py
load
¶
Load a model and optionally its tokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
HuggingFace model ID or local path |
required |
load_tokenizer
|
bool
|
Whether to load tokenizer |
True
|
dtype
|
Optional[dtype]
|
Optional dtype override (e.g., torch.float16) |
None
|
**kwargs
|
Additional args passed to from_pretrained |
{}
|
Returns:
| Type | Description |
|---|---|
Tuple[Any, Optional[Any], Dict[str, Any]]
|
(model, tokenizer, model_info) |
Source code in src/model_garage/core/loader.py
get_layer_names
¶
Get standard layer names for a model.
Returns dict mapping generic names to model-specific paths.
Source code in src/model_garage/core/loader.py
quick_load¶
quick_load
¶
Quick helper to load a model.
Usage
model, tokenizer, info = quick_load("gpt2")
HookManager¶
HookManager
¶
Centralized hook management for model manipulation.
Features: - Named hooks for easy tracking - Automatic cleanup - Hook chaining - Debug logging
Source code in src/model_garage/core/hooks.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
register_forward_hook
¶
Register a forward hook on a named layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_name
|
str
|
Name of layer (e.g., "transformer.h.6") |
required |
hook_fn
|
Callable
|
Function(module, input, output) -> modified_output or None |
required |
hook_name
|
Optional[str]
|
Optional name for this hook |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Hook name for later reference |
Source code in src/model_garage/core/hooks.py
register_capture_hook
¶
Register a hook that captures activations without modifying them.
Captured data accessible via get_captured(hook_name).
Source code in src/model_garage/core/hooks.py
register_injection_hook
¶
Register a hook that modifies layer output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_name
|
str
|
Name of layer to inject after |
required |
injection_fn
|
Callable[[Tensor], Tensor]
|
Function(hidden_states) -> modified_hidden_states |
required |
hook_name
|
Optional[str]
|
Optional name |
None
|
Source code in src/model_garage/core/hooks.py
get_captured
¶
clear_captured
¶
remove_hook
¶
remove_all
¶
list_hooks
¶
__enter__
¶
TensorUtils¶
TensorUtils
¶
Common tensor operations used across all tools.
Source code in src/model_garage/core/tensor.py
ensure_device
staticmethod
¶
Move tensor to specified device if not already there.
Source code in src/model_garage/core/tensor.py
ensure_shape
staticmethod
¶
Reshape tensor to target shape, handling common cases.
Supports: - Adding batch dimension - Adding sequence dimension - Padding/truncating sequence length
Source code in src/model_garage/core/tensor.py
cosine_similarity
staticmethod
¶
Compute cosine similarity between two tensors.
Source code in src/model_garage/core/tensor.py
l2_distance
staticmethod
¶
stats
staticmethod
¶
Get basic statistics about a tensor.
Source code in src/model_garage/core/tensor.py
project
staticmethod
¶
Project tensor from one dimension to another using learned linear.
Note: This creates a NEW projection each time. For reusable projections, use the Projector class instead.
Source code in src/model_garage/core/tensor.py
Projector¶
Projector
¶
Reusable dimension projector.
Like an adapter socket - converts between different sizes.
Source code in src/model_garage/core/tensor.py
DeviceManager¶
DeviceManager
¶
Manage device placement for a session.
Provides consistent device handling across multiple operations.
Source code in src/model_garage/core/device.py
to
¶
to_dict
¶
scope
¶
memory_stats
¶
Get GPU memory stats if available.
Source code in src/model_garage/core/device.py
Serialization¶
save_component
¶
Save a component with metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
Union[Module, Tensor, Dict[str, Tensor]]
|
Module, Tensor, or state dict to save |
required |
path
|
Union[str, Path]
|
Directory to save to |
required |
metadata
|
Optional[ComponentMetadata]
|
Optional ComponentMetadata |
None
|
**extra_metadata
|
Additional metadata fields |
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to saved component directory |
Source code in src/model_garage/core/serialization.py
load_component
¶
Load a saved component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Directory containing saved component |
required |
device
|
str
|
Device to load to |
'cpu'
|
return_metadata
|
bool
|
If True, also return metadata |
False
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, Tensor], tuple]
|
state_dict, or (state_dict, metadata) if return_metadata=True |