- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.5k
          [core] respect local_files_only=True when using sharded checkpoints
          #12005
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 17 commits
      Commits
    
    
            Show all changes
          
          
            18 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8d431dc
              
                tighten compilation tests for quantization
              
              
                sayakpaul 69920ef
              
                feat: model_info but local.
              
              
                sayakpaul d5c1772
              
                up
              
              
                sayakpaul f38a644
              
                Revert "tighten compilation tests for quantization"
              
              
                sayakpaul 2d993b7
              
                Merge branch 'main' into local-model-info
              
              
                sayakpaul 85279df
              
                Merge branch 'main' into local-model-info
              
              
                sayakpaul d117474
              
                Merge branch 'main' into local-model-info
              
              
                sayakpaul 71843a0
              
                Merge branch 'main' into local-model-info
              
              
                sayakpaul fb2397f
              
                up
              
              
                sayakpaul 832de66
              
                Merge branch 'main' into local-model-info
              
              
                sayakpaul 01784c3
              
                Merge branch 'main' into local-model-info
              
              
                sayakpaul b7af511
              
                reviewer feedback.
              
              
                sayakpaul 04cd2dc
              
                reviewer feedback.
              
              
                sayakpaul 1c528a4
              
                up
              
              
                sayakpaul 1b939e5
              
                up
              
              
                sayakpaul 2a9734f
              
                empty
              
              
                sayakpaul 09e063c
              
                Merge branch 'main' into local-model-info
              
              
                sayakpaul 7fd1a82
              
                update
              
              
                DN6 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -402,15 +402,23 @@ def _get_checkpoint_shard_files( | |
| allow_patterns = [os.path.join(subfolder, p) for p in allow_patterns] | ||
|  | ||
| ignore_patterns = ["*.json", "*.md"] | ||
| # `model_info` call must guarded with the above condition. | ||
| model_files_info = model_info(pretrained_model_name_or_path, revision=revision, token=token) | ||
| for shard_file in original_shard_filenames: | ||
| shard_file_present = any(shard_file in k.rfilename for k in model_files_info.siblings) | ||
| if not shard_file_present: | ||
|  | ||
| # If the repo doesn't have the required shards, error out early even before downloading anything. | ||
| if not local_files_only: | ||
| try: | ||
| model_files_info = model_info(pretrained_model_name_or_path, revision=revision, token=token) | ||
| for shard_file in original_shard_filenames: | ||
| shard_file_present = any(shard_file in k.rfilename for k in model_files_info.siblings) | ||
| if not shard_file_present: | ||
| raise EnvironmentError( | ||
| f"{shards_path} does not appear to have a file named {shard_file} which is " | ||
| "required according to the checkpoint index." | ||
| ) | ||
| except ConnectionError as e: | ||
| raise EnvironmentError( | ||
| f"{shards_path} does not appear to have a file named {shard_file} which is " | ||
| "required according to the checkpoint index." | ||
| ) | ||
| f"We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load {pretrained_model_name_or_path}. You should try" | ||
| " again after checking your internet connection." | ||
| ) from e | ||
|  | ||
| try: | ||
| # Load from URL | ||
|  | @@ -428,6 +436,16 @@ def _get_checkpoint_shard_files( | |
| if subfolder is not None: | ||
| cached_folder = os.path.join(cached_folder, subfolder) | ||
|  | ||
| # Check again after downloading/loading from the cache. | ||
| model_files_info = _get_filepaths_for_folder(cached_folder) | ||
|          | ||
| for shard_file in original_shard_filenames: | ||
| shard_file_present = any(shard_file in k for k in model_files_info) | ||
| if not shard_file_present: | ||
| raise EnvironmentError( | ||
| f"{shards_path} does not appear to have a file named {shard_file} which is " | ||
| "required according to the checkpoint index." | ||
| ) | ||
|  | ||
| # We have already dealt with RepositoryNotFoundError and RevisionNotFoundError when getting the index, so | ||
| # we don't have to catch them here. We have also dealt with EntryNotFoundError. | ||
| except HTTPError as e: | ||
|  | @@ -441,6 +459,16 @@ def _get_checkpoint_shard_files( | |
| return cached_filenames, sharded_metadata | ||
|  | ||
|  | ||
| def _get_filepaths_for_folder(folder): | ||
| relative_paths = [] | ||
| for root, dirs, files in os.walk(folder): | ||
| for fname in files: | ||
| abs_path = os.path.join(root, fname) | ||
| rel_path = os.path.relpath(abs_path, start=folder) | ||
| relative_paths.append(rel_path) | ||
| return relative_paths | ||
|  | ||
|  | ||
| def _check_legacy_sharding_variant_format(folder: str = None, filenames: List[str] = None, variant: str = None): | ||
| if filenames and folder: | ||
| raise ValueError("Both `filenames` and `folder` cannot be provided.") | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the purpose of this check is to verify if the necessary sharded files are present in the model repo before attempting a download, presumably to avoid a large download if all files aren't present. If we cannot connect to the hub, we just have to assume the necessary shard files are already present locally.
I think we can just skip this check if
local_files_only=Trueand then check if all the shard filenames are present in thecached_folderThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about now?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just this is sufficient
Run snapshot download
Then after the cached_filenames is created, iterate over the files to verify they exist
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
snapshot_download()whenlocal_files_only=False, that might be unnecessary.snapshot_download()after also runningmodel_info()?snapshot_download()regardless oflocal_files_onlyvar, I think we should have it insidetry-exceptin case the endpoint cannot be pinged for some reason and raise theConnectionErroras before.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See if b7af511 resolves this.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see what you mean. Let me update. Sorry about the back and forth.