Charlie81 commited on
Commit
b77239a
·
1 Parent(s): 447000e

load custom model fix

Browse files
Files changed (1) hide show
  1. scripts/eval.py +19 -40
scripts/eval.py CHANGED
@@ -205,15 +205,12 @@ def load_custom_model(args) -> HFLM:
205
  from modeling_myolmoe import MyOlmoeForCausalLM, MyOlmoeConfig
206
  logger.info("Successfully imported MyOlmoeForCausalLM and MyOlmoeConfig")
207
 
208
- # CRITICAL FIX: Ensure the config is properly registered as a dataclass
209
- from transformers import AutoConfig, AutoModelForCausalLM
210
- from dataclasses import dataclass
211
-
212
- # Make sure the config is a proper dataclass
213
  if not hasattr(MyOlmoeConfig, '__dataclass_fields__'):
214
  logger.warning("MyOlmoeConfig is not a dataclass, this may cause issues")
215
 
216
- # Register with correct model type
 
217
  AutoConfig.register("myolmoe", MyOlmoeConfig)
218
  AutoModelForCausalLM.register(MyOlmoeConfig, MyOlmoeForCausalLM)
219
  logger.info("Registered MyOlmoeForCausalLM with MyOlmoeConfig")
@@ -223,36 +220,35 @@ def load_custom_model(args) -> HFLM:
223
  logger.error("Make sure the custom model code is available in the specified path")
224
  raise
225
 
226
- # Load model manually to avoid HFLM wrapper issues
 
 
227
  try:
228
- logger.info("Loading model manually to avoid wrapper issues...")
229
-
230
- # Load tokenizer first
231
  tokenizer = AutoTokenizer.from_pretrained(
232
  args.model_path,
233
  trust_remote_code=args.trust_remote_code
234
  )
235
 
236
- # Load config with explicit class
237
- config = MyOlmoeConfig.from_pretrained(
238
  args.model_path,
239
  trust_remote_code=args.trust_remote_code
240
  )
241
 
242
- # Verify config is valid
243
- logger.info(f"Loaded config type: {type(config)}")
244
- logger.info(f"Config model_type: {getattr(config, 'model_type', 'unknown')}")
245
 
246
  # Load model instance
247
  model_instance = MyOlmoeForCausalLM.from_pretrained(
248
  args.model_path,
249
- config=config,
250
  trust_remote_code=args.trust_remote_code,
251
- torch_dtype=torch.bfloat16 if args.dtype == "bfloat16" else "auto",
252
- low_cpu_mem_usage=True
253
  )
254
 
255
- # Create HFLM wrapper with pre-loaded model
256
  model = HFLM(
257
  pretrained=model_instance,
258
  tokenizer=tokenizer,
@@ -261,29 +257,12 @@ def load_custom_model(args) -> HFLM:
261
  max_batch_size=args.max_batch_size
262
  )
263
 
264
- logger.info("Custom model loaded successfully")
265
- return model
266
-
267
  except Exception as e:
268
  logger.error(f"Failed to load custom model: {e}")
269
- logger.error("Trying fallback approach...")
270
-
271
- # Fallback: Try loading as standard transformers model
272
- try:
273
- model = HFLM(
274
- pretrained=args.model_path,
275
- device=args.device,
276
- batch_size=args.batch_size,
277
- max_batch_size=args.max_batch_size,
278
- dtype=args.dtype,
279
- trust_remote_code=True # Force trust remote code
280
- )
281
- logger.info("Fallback loading successful")
282
- return model
283
- except Exception as fallback_e:
284
- logger.error(f"Fallback also failed: {fallback_e}")
285
- raise e
286
-
287
 
288
  def validate_model_config(model_path: str, trust_remote_code: bool = False) -> Dict[str, Any]:
289
  """
 
205
  from modeling_myolmoe import MyOlmoeForCausalLM, MyOlmoeConfig
206
  logger.info("Successfully imported MyOlmoeForCausalLM and MyOlmoeConfig")
207
 
208
+ # Check if config is a dataclass
 
 
 
 
209
  if not hasattr(MyOlmoeConfig, '__dataclass_fields__'):
210
  logger.warning("MyOlmoeConfig is not a dataclass, this may cause issues")
211
 
212
+ # Register the custom model class with the correct config
213
+ from transformers import AutoConfig, AutoModelForCausalLM
214
  AutoConfig.register("myolmoe", MyOlmoeConfig)
215
  AutoModelForCausalLM.register(MyOlmoeConfig, MyOlmoeForCausalLM)
216
  logger.info("Registered MyOlmoeForCausalLM with MyOlmoeConfig")
 
220
  logger.error("Make sure the custom model code is available in the specified path")
221
  raise
222
 
223
+ # Load model manually to avoid wrapper issues
224
+ logger.info("Loading model manually to avoid wrapper issues...")
225
+
226
  try:
227
+ # Load tokenizer
 
 
228
  tokenizer = AutoTokenizer.from_pretrained(
229
  args.model_path,
230
  trust_remote_code=args.trust_remote_code
231
  )
232
 
233
+ # Load config using the custom config class
234
+ model_config = MyOlmoeConfig.from_pretrained(
235
  args.model_path,
236
  trust_remote_code=args.trust_remote_code
237
  )
238
 
239
+ # Debug information
240
+ logger.info(f"Loaded config type: {type(model_config)}")
241
+ logger.info(f"Config model_type: {model_config.model_type}")
242
 
243
  # Load model instance
244
  model_instance = MyOlmoeForCausalLM.from_pretrained(
245
  args.model_path,
246
+ config=model_config,
247
  trust_remote_code=args.trust_remote_code,
248
+ torch_dtype=torch.bfloat16 if args.dtype == "bfloat16" else "auto"
 
249
  )
250
 
251
+ # Create HFLM with pre-loaded model
252
  model = HFLM(
253
  pretrained=model_instance,
254
  tokenizer=tokenizer,
 
257
  max_batch_size=args.max_batch_size
258
  )
259
 
 
 
 
260
  except Exception as e:
261
  logger.error(f"Failed to load custom model: {e}")
262
+ raise
263
+
264
+ logger.info("Custom model loaded successfully")
265
+ return model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
 
267
  def validate_model_config(model_path: str, trust_remote_code: bool = False) -> Dict[str, Any]:
268
  """