Deagin Claude Opus 4.6 commited on
Commit
0462568
·
1 Parent(s): 8266ce5

Fix: Only free modules > 1GB (text encoder), keep feat_mlp

Browse files

The previous 100MB threshold was too aggressive and deleted feat_mlp
(the vision projection head needed for inference). The text encoder
is ~7.5GB while feat_mlp is < 1GB, so a 1GB threshold cleanly
separates them. Also logs all module sizes for debugging.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. radio_backbone.py +18 -26
radio_backbone.py CHANGED
@@ -94,39 +94,31 @@ def load_model(device: str = "cuda", vitdet_window_size: int = 8):
94
 
95
 
96
  def _free_text_encoder(adaptor):
97
- """Delete large sub-modules from the SigLIP2 adaptor to free RAM.
98
 
99
- After pre-computing text embeddings, we no longer need the text
100
- encoder, tokenizer model weights, or any module > 100MB.
101
  """
102
  freed = 0
103
 
104
- # Check all direct children of the adaptor
105
- for name in list(vars(adaptor).keys()):
106
- obj = getattr(adaptor, name, None)
107
- if obj is None:
108
- continue
109
- if hasattr(obj, "parameters"):
110
- param_bytes = sum(
111
- p.numel() * p.element_size() for p in obj.parameters()
112
- )
113
- if param_bytes > 100_000_000: # > 100MB
114
- size_gb = param_bytes / 1e9
115
- print(f" Freeing adaptor.{name} ({size_gb:.1f} GB)")
116
- try:
117
- delattr(adaptor, name)
118
- freed += param_bytes
119
- except Exception:
120
- pass
121
-
122
- # Also check nn.Module named children
123
- for name, module in list(adaptor.named_children()):
124
  param_bytes = sum(
125
  p.numel() * p.element_size() for p in module.parameters()
126
  )
127
- if param_bytes > 100_000_000:
 
 
 
 
 
 
 
 
 
128
  size_gb = param_bytes / 1e9
129
- print(f" Freeing adaptor child '{name}' ({size_gb:.1f} GB)")
130
  try:
131
  delattr(adaptor, name)
132
  freed += param_bytes
@@ -140,7 +132,7 @@ def _free_text_encoder(adaptor):
140
  if freed > 0:
141
  print(f" Total freed: {freed / 1e9:.1f} GB")
142
  else:
143
- print(" Warning: could not identify text encoder to free")
144
 
145
 
146
  def get_model():
 
94
 
95
 
96
  def _free_text_encoder(adaptor):
97
+ """Delete the SigLIP2 text encoder from the adaptor to free ~7.5GB RAM.
98
 
99
+ Only targets modules > 1GB the text encoder is ~7.5GB while vision
100
+ projection heads (feat_mlp, summary_mlp, etc.) are < 1GB.
101
  """
102
  freed = 0
103
 
104
+ # Log all modules so we can see what's there
105
+ print(" Adaptor modules:")
106
+ for name, module in adaptor.named_children():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  param_bytes = sum(
108
  p.numel() * p.element_size() for p in module.parameters()
109
  )
110
+ print(f" {name}: {param_bytes / 1e6:.0f} MB")
111
+
112
+ # Only delete modules > 1GB (the text encoder is ~7.5GB,
113
+ # vision projection heads like feat_mlp are < 1GB)
114
+ for name in list(dict(adaptor.named_children()).keys()):
115
+ module = getattr(adaptor, name)
116
+ param_bytes = sum(
117
+ p.numel() * p.element_size() for p in module.parameters()
118
+ )
119
+ if param_bytes > 1_000_000_000: # > 1GB
120
  size_gb = param_bytes / 1e9
121
+ print(f" Freeing adaptor.{name} ({size_gb:.1f} GB)")
122
  try:
123
  delattr(adaptor, name)
124
  freed += param_bytes
 
132
  if freed > 0:
133
  print(f" Total freed: {freed / 1e9:.1f} GB")
134
  else:
135
+ print(" Warning: no module > 1GB found — text encoder may still be in RAM")
136
 
137
 
138
  def get_model():