AnonymousPaperSubmission123 commited on
Commit
fd5f0f5
·
verified ·
1 Parent(s): eaae171

Update src/FineTune/model.py

Browse files
Files changed (1) hide show
  1. src/FineTune/model.py +21 -7
src/FineTune/model.py CHANGED
@@ -11,7 +11,7 @@ def calculate_MMD_loss(human_crit, sample_crit):
11
  mmd_loss = human_crit.mean() - sample_crit.mean()
12
  return mmd_loss
13
 
14
- def from_pretrained(cls, model_name, kwargs, cache_dir):
15
  # use local model if it exists
16
  if "/" in model_name:
17
  local_path = os.path.join(cache_dir, model_name.split("/")[1])
@@ -20,7 +20,18 @@ def from_pretrained(cls, model_name, kwargs, cache_dir):
20
 
21
  if os.path.exists(local_path):
22
  return cls.from_pretrained(local_path, **kwargs)
23
- return cls.from_pretrained(model_name, **kwargs, cache_dir=cache_dir, device_map='auto')
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  model_fullnames = {
26
  'gemma-1b': 'google/gemma-3-1b-pt',
@@ -76,7 +87,7 @@ class ComputeStat(nn.Module):
76
  model_kwargs.update(dict(torch_dtype=torch.float16))
77
  if torch.__version__ >= '2.0.0' and 'gemma' in model_name:
78
  model_kwargs.update({'attn_implementation': 'sdpa'})
79
- model = from_pretrained(AutoModelForCausalLM, model_fullname, model_kwargs, cache_dir)
80
  print(f'Moving model to {device}...', end='', flush=True)
81
  start = time.time()
82
  model.to(device)
@@ -179,11 +190,14 @@ class ComputeStat(nn.Module):
179
  model = cls(*args, **kwargs)
180
 
181
  # 2. 加载 scoring_model
 
 
 
182
  scoring_dir = os.path.join(load_directory, "scoring_model")
183
  model.scoring_model = AutoPeftModelForCausalLM.from_pretrained(
184
- scoring_dir,
185
- device_map="auto",
186
- low_cpu_mem_usage=True,
187
  use_safetensors=True
188
  )
189
 
@@ -301,4 +315,4 @@ class ComputeStat(nn.Module):
301
  if buffer_name.startswith("null_distr_"):
302
  domain = buffer_name.replace("null_distr_", "")
303
  domains.append(domain)
304
- return domains
 
11
  mmd_loss = human_crit.mean() - sample_crit.mean()
12
  return mmd_loss
13
 
14
+ def from_pretrained(cls, model_name, kwargs, cache_dir, device=None):
15
  # use local model if it exists
16
  if "/" in model_name:
17
  local_path = os.path.join(cache_dir, model_name.split("/")[1])
 
20
 
21
  if os.path.exists(local_path):
22
  return cls.from_pretrained(local_path, **kwargs)
23
+
24
+ remote_kwargs = dict(kwargs, cache_dir=cache_dir)
25
+ if device is not None:
26
+ # Pin the whole model to a single device instead of device_map='auto'.
27
+ # 'auto' lets accelerate split the model across CPU/disk when memory
28
+ # is tight at load time (common on HF Spaces' free CPU tier), which
29
+ # then makes the later `model.to(device)` call in load_model() raise:
30
+ # "You can't move a model that has some modules offloaded to
31
+ # cpu or disk."
32
+ # Forcing everything onto one device up front avoids that split.
33
+ remote_kwargs['device_map'] = {'': device}
34
+ return cls.from_pretrained(model_name, **remote_kwargs)
35
 
36
  model_fullnames = {
37
  'gemma-1b': 'google/gemma-3-1b-pt',
 
87
  model_kwargs.update(dict(torch_dtype=torch.float16))
88
  if torch.__version__ >= '2.0.0' and 'gemma' in model_name:
89
  model_kwargs.update({'attn_implementation': 'sdpa'})
90
+ model = from_pretrained(AutoModelForCausalLM, model_fullname, model_kwargs, cache_dir, device=device)
91
  print(f'Moving model to {device}...', end='', flush=True)
92
  start = time.time()
93
  model.to(device)
 
190
  model = cls(*args, **kwargs)
191
 
192
  # 2. 加载 scoring_model
193
+ # Pin to a single device instead of device_map="auto" -- see the
194
+ # comment on the module-level from_pretrained() above for why "auto"
195
+ # is what triggers the "modules offloaded to cpu or disk" error.
196
  scoring_dir = os.path.join(load_directory, "scoring_model")
197
  model.scoring_model = AutoPeftModelForCausalLM.from_pretrained(
198
+ scoring_dir,
199
+ device_map={'': model.device},
200
+ low_cpu_mem_usage=True,
201
  use_safetensors=True
202
  )
203
 
 
315
  if buffer_name.startswith("null_distr_"):
316
  domain = buffer_name.replace("null_distr_", "")
317
  domains.append(domain)
318
+ return domains