Akfmsk commited on
Commit
c0aebd8
·
1 Parent(s): cbd3875

Move 16416-sized StaticCache and padded_input_ids allocations after the chunked prefill loop to prevent XLA dynamic shape padding from blowing up the Math SDPA matrix to 17GB

Browse files
Files changed (1) hide show
  1. modeling_locateanything.py +14 -14
modeling_locateanything.py CHANGED
@@ -381,20 +381,6 @@ class LocateAnythingForConditionalGeneration(LocateAnythingPreTrainedModel, Gene
381
  def get_max_length(self):
382
  return self._max_cache_len_val
383
 
384
- past_key_values = LocateAnythingStaticCache(
385
- config=self.language_model.config,
386
- batch_size=batch_size,
387
- max_cache_len=total_gen_length,
388
- device=self.device,
389
- dtype=self.language_model.dtype
390
- )
391
-
392
- padded_input_ids = torch.zeros((batch_size, total_gen_length), dtype=torch.long, device=self.device)
393
- padded_input_ids[:, :seq_len] = input_ids
394
-
395
- current_idx = seq_len
396
- cache_position = torch.arange(seq_len, device=self.device)
397
-
398
  # Initial forward pass (prefill) using chunks to avoid XLA memory allocation OOM!
399
  inputs_embeds = self.language_model.model.image_processing(
400
  input_ids, vit_embeds, self.config.image_token_index
@@ -417,6 +403,20 @@ class LocateAnythingForConditionalGeneration(LocateAnythingPreTrainedModel, Gene
417
  )
418
  dynamic_cache = outputs.past_key_values
419
  xm.mark_step() # Compile and execute chunk immediately to reclaim memory!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
 
421
  # Populate our StaticCache from the DynamicCache returned by prefill
422
  dynamic_cache_list = list(outputs.past_key_values)
 
381
  def get_max_length(self):
382
  return self._max_cache_len_val
383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  # Initial forward pass (prefill) using chunks to avoid XLA memory allocation OOM!
385
  inputs_embeds = self.language_model.model.image_processing(
386
  input_ids, vit_embeds, self.config.image_token_index
 
403
  )
404
  dynamic_cache = outputs.past_key_values
405
  xm.mark_step() # Compile and execute chunk immediately to reclaim memory!
406
+
407
+ # Initialize StaticCache and padded_input_ids AFTER the chunk loop to avoid XLA tracking max shapes!
408
+ past_key_values = LocateAnythingStaticCache(
409
+ config=self.language_model.config,
410
+ batch_size=batch_size,
411
+ max_cache_len=total_gen_length,
412
+ device=self.device,
413
+ dtype=self.language_model.dtype
414
+ )
415
+
416
+ padded_input_ids = torch.zeros((batch_size, total_gen_length), dtype=torch.long, device=self.device)
417
+ padded_input_ids[:, :seq_len] = input_ids
418
+
419
+ current_idx = seq_len
420
 
421
  # Populate our StaticCache from the DynamicCache returned by prefill
422
  dynamic_cache_list = list(outputs.past_key_values)