Repository navigation
Remove SafeTensors source weights after a converted GGUF loads #225
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package convert | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // sourceWeightIndexName is the shard index that names the SafeTensors files | ||
| // llama.cpp reads; it is only useful alongside those shards. | ||
| const sourceWeightIndexName = "model.safetensors.index.json" | ||
|
|
||
| // ConvertedSourceWeightFiles returns the top-level SafeTensors files the GGUF | ||
| // converter consumed in modelDir, plus their shard index. llama.cpp's | ||
| // convert_hf_to_gguf.py reads every "model*.safetensors" file at the top | ||
| // level, so companions such as head.safetensors or adapter directories are | ||
| // not listed and stay in place. | ||
| func ConvertedSourceWeightFiles(modelDir string) ([]string, error) { | ||
| entries, err := os.ReadDir(modelDir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var names []string | ||
| for _, entry := range entries { | ||
| if !entry.Type().IsRegular() { | ||
| continue | ||
| } | ||
| name := entry.Name() | ||
| lower := strings.ToLower(name) | ||
| if lower == sourceWeightIndexName || (strings.HasPrefix(lower, "model") && strings.HasSuffix(lower, ".safetensors")) { | ||
| names = append(names, name) | ||
| } | ||
| } | ||
| return names, nil | ||
| } | ||
|
|
||
| // RemoveConvertedSourceWeights deletes the SafeTensors weights in modelDir | ||
| // once a converted GGUF serves the model, so the checkpoint is not stored | ||
| // twice. It keeps them when no weight GGUF exists, or when a vision model is | ||
| // still missing its mmproj, since a later load converts the projector from | ||
| // the same weights. It returns the removed file names and their total size. | ||
| func RemoveConvertedSourceWeights(modelDir string) ([]string, int64, error) { | ||
| if _, ok, err := FindGGUFForDType(modelDir, ""); err != nil || !ok { | ||
| return nil, 0, err | ||
| } | ||
| if hasVisionConfig(modelDir) { | ||
| if _, ok, err := FindMMProjForDType(modelDir, ""); err != nil || !ok { | ||
| return nil, 0, err | ||
| } | ||
| } | ||
| names, err := ConvertedSourceWeightFiles(modelDir) | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
| var ( | ||
| removed []string | ||
| freed int64 | ||
| errs []error | ||
| ) | ||
| for _, name := range names { | ||
| path := filepath.Join(modelDir, name) | ||
| info, statErr := os.Stat(path) | ||
| if err := os.Remove(path); err != nil { | ||
| if !errors.Is(err, os.ErrNotExist) { | ||
| errs = append(errs, fmt.Errorf("removing %s: %w", name, err)) | ||
| } | ||
| continue | ||
| } | ||
| removed = append(removed, name) | ||
| if statErr == nil { | ||
| freed += info.Size() | ||
| } | ||
| } | ||
| return removed, freed, errors.Join(errs...) | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package convert | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "reflect" | ||
| "sort" | ||
| "testing" | ||
| ) | ||
|
|
||
| func writeSourceWeightFixture(t *testing.T, dir string, files map[string]string) { | ||
| t.Helper() | ||
| for name, content := range files { | ||
| path := filepath.Join(dir, name) | ||
| if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { | ||
| t.Fatalf("mkdir %s: %v", name, err) | ||
| } | ||
| if err := os.WriteFile(path, []byte(content), 0o644); err != nil { | ||
| t.Fatalf("write %s: %v", name, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func remainingFiles(t *testing.T, dir string) []string { | ||
| t.Helper() | ||
| var names []string | ||
| err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error { | ||
| if err != nil || d.IsDir() { | ||
| return err | ||
| } | ||
| rel, _ := filepath.Rel(dir, path) | ||
| names = append(names, filepath.ToSlash(rel)) | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("walk: %v", err) | ||
| } | ||
| sort.Strings(names) | ||
| return names | ||
| } | ||
|
|
||
| func TestRemoveConvertedSourceWeightsKeepsCompanions(t *testing.T) { | ||
| dir := filepath.Join(t.TempDir(), "JEV-9B") | ||
| writeSourceWeightFixture(t, dir, map[string]string{ | ||
| "config.json": `{"architectures":["Qwen3ForCausalLM"]}`, | ||
| "model-00001-of-00002.safetensors": "aaaa", | ||
| "model-00002-of-00002.safetensors": "bb", | ||
| "model.safetensors.index.json": "{}", | ||
| "head.safetensors": "head", | ||
| "adapter/adapter_model.safetensors": "lora", | ||
| "JEV-9B-q8_0.gguf": "gguf", | ||
| }) | ||
|
|
||
| removed, freed, err := RemoveConvertedSourceWeights(dir) | ||
| if err != nil { | ||
| t.Fatalf("RemoveConvertedSourceWeights: %v", err) | ||
| } | ||
| wantRemoved := []string{"model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors", "model.safetensors.index.json"} | ||
| if !reflect.DeepEqual(removed, wantRemoved) { | ||
| t.Fatalf("removed = %v, want %v", removed, wantRemoved) | ||
| } | ||
| if freed != 8 { | ||
| t.Fatalf("freed = %d, want 8", freed) | ||
| } | ||
| want := []string{"JEV-9B-q8_0.gguf", "adapter/adapter_model.safetensors", "config.json", "head.safetensors"} | ||
| if got := remainingFiles(t, dir); !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("remaining = %v, want %v", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestRemoveConvertedSourceWeightsKeepsWeightsWithoutGGUF(t *testing.T) { | ||
| dir := filepath.Join(t.TempDir(), "demo") | ||
| writeSourceWeightFixture(t, dir, map[string]string{ | ||
| "config.json": `{"architectures":["LlamaForCausalLM"]}`, | ||
| "model.safetensors": "weights", | ||
| }) | ||
|
|
||
| removed, _, err := RemoveConvertedSourceWeights(dir) | ||
| if err != nil { | ||
| t.Fatalf("RemoveConvertedSourceWeights: %v", err) | ||
| } | ||
| if len(removed) != 0 { | ||
| t.Fatalf("removed = %v, want none", removed) | ||
| } | ||
| } | ||
|
|
||
| func TestRemoveConvertedSourceWeightsKeepsVisionWeightsUntilMMProjExists(t *testing.T) { | ||
| dir := filepath.Join(t.TempDir(), "vlm") | ||
| writeSourceWeightFixture(t, dir, map[string]string{ | ||
| "config.json": `{"architectures":["Qwen2VLForConditionalGeneration"],"vision_config":{}}`, | ||
| "model.safetensors": "weights", | ||
| "vlm-f16.gguf": "gguf", | ||
| }) | ||
|
|
||
| removed, _, err := RemoveConvertedSourceWeights(dir) | ||
| if err != nil { | ||
| t.Fatalf("RemoveConvertedSourceWeights: %v", err) | ||
| } | ||
| if len(removed) != 0 { | ||
| t.Fatalf("removed = %v before mmproj exists, want none", removed) | ||
| } | ||
|
|
||
| writeSourceWeightFixture(t, dir, map[string]string{"mmproj-vlm-f16.gguf": "mmproj"}) | ||
| removed, _, err = RemoveConvertedSourceWeights(dir) | ||
| if err != nil { | ||
| t.Fatalf("RemoveConvertedSourceWeights: %v", err) | ||
| } | ||
| if !reflect.DeepEqual(removed, []string{"model.safetensors"}) { | ||
| t.Fatalf("removed = %v after mmproj exists, want [model.safetensors]", removed) | ||
| } | ||
| } |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.