- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.5k
metal : use residency sets #11427
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
            
            metal : use residency sets #11427
      
        
          +119
        
        
          −17
        
        
          
        
      
    
  
Conversation
  
    
      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
    
  
  
    
    febb813    to
    4dad9fa      
    Compare
  
    | Great news - this change finally resolves the annoying overhead that I was observing. The only remaining question is how to implement this to be compatible with macOS < 15.0. Any suggestions? Edit: resolved | 
ggml-ci
21850f6    to
    2674f02      
    Compare
  
    
    
  Animaxx 
      added a commit
        to Animaxx/llama.cpp
      that referenced
      this pull request
    
      Jan 28, 2025 
    
    
      
  
    
      
    
  
metal : use residency sets
    
  tinglou 
      pushed a commit
        to tinglou/llama.cpp
      that referenced
      this pull request
    
      Feb 13, 2025 
    
    
      
  
    
      
    
  
* metal : use residency sets ggml-ci * metal : restore commandBufferWithUnretainedReferences calls [no ci] * metal : release descriptors ggml-ci * metal : check env GGML_METAL_NO_RESIDENCY ggml-ci * metal : fix build + clean-up ggml-ci
    
  arthw 
      pushed a commit
        to arthw/llama.cpp
      that referenced
      this pull request
    
      Feb 26, 2025 
    
    
      
  
    
      
    
  
* metal : use residency sets ggml-ci * metal : restore commandBufferWithUnretainedReferences calls [no ci] * metal : release descriptors ggml-ci * metal : check env GGML_METAL_NO_RESIDENCY ggml-ci * metal : fix build + clean-up ggml-ci
    
  mglambda 
      pushed a commit
        to mglambda/llama.cpp
      that referenced
      this pull request
    
      Mar 8, 2025 
    
    
      
  
    
      
    
  
* metal : use residency sets ggml-ci * metal : restore commandBufferWithUnretainedReferences calls [no ci] * metal : release descriptors ggml-ci * metal : check env GGML_METAL_NO_RESIDENCY ggml-ci * metal : fix build + clean-up ggml-ci
| @ggerganov is there any reason we wouldn't set GGML_METAL_NO_RESIDENCY=1 on macOS? | 
| Without residency sets you will hit the issue from #10119 - that was the main reason to introduce them. | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
      Labels
      
    Apple Metal
  https://en.wikipedia.org/wiki/Metal_(API) 
  
    build
  Compilation issues 
  
    ggml
  changes relating to the ggml tensor library for machine learning 
  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.
  
    
  
    
fix #10119
Using residency sets makes the allocated memory stay wired and eliminates almost completely the overhead observed in #10119. For example, on M2 Ultra, using 7B Q8_0 model the requests are ~250ms faster thanks to this change. It seems it is not necessary to attach the residency sets to the command queue and buffers, so the change is rather simple. For each buffer, we create an associated
MTLResidencySetand add theMTLBufferobjects to it. After that we commit it and request residency:https://github.com/ggerganov/llama.cpp/blob/225d2e0ca1d7a7e627f2cea4a43dd77a83b9f078/ggml/src/ggml-metal/ggml-metal.m#L1084-L1091
build: b9126fe (4561)
Metal backend changes
Checks the environment variable
GGML_METAL_NO_RESIDENCY. If set, then no residency sets will be created, allowing the GPU memory to be collected by the OS after 1 second of inactivity. Generally, this should rarely be needed as it hurts the performance of the application, but keeping support just in case.