|
| 1 | +package builder_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/docker/model-distribution/builder" |
| 10 | + "github.com/docker/model-distribution/types" |
| 11 | +) |
| 12 | + |
| 13 | +func TestWithMultimodalProjector(t *testing.T) { |
| 14 | + // Create a builder from a GGUF file |
| 15 | + b, err := builder.FromGGUF(filepath.Join("..", "assets", "dummy.gguf")) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("Failed to create builder from GGUF: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + // Add multimodal projector |
| 21 | + b2, err := b.WithMultimodalProjector(filepath.Join("..", "assets", "dummy.mmproj")) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("Failed to add multimodal projector: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + // Build the model |
| 27 | + target := &fakeTarget{} |
| 28 | + if err := b2.Build(t.Context(), target, nil); err != nil { |
| 29 | + t.Fatalf("Failed to build model: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + // Verify the model has the expected layers |
| 33 | + manifest, err := target.artifact.Manifest() |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("Failed to get manifest: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + // Should have 2 layers: GGUF + multimodal projector |
| 39 | + if len(manifest.Layers) != 2 { |
| 40 | + t.Fatalf("Expected 2 layers, got %d", len(manifest.Layers)) |
| 41 | + } |
| 42 | + |
| 43 | + // Check that one layer has the multimodal projector media type |
| 44 | + foundMMProjLayer := false |
| 45 | + for _, layer := range manifest.Layers { |
| 46 | + if layer.MediaType == types.MediaTypeMultimodalProjector { |
| 47 | + foundMMProjLayer = true |
| 48 | + break |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if !foundMMProjLayer { |
| 53 | + t.Error("Expected to find a layer with multimodal projector media type") |
| 54 | + } |
| 55 | + |
| 56 | + // Note: We can't directly test MMPROJPath() on ModelArtifact interface |
| 57 | + // but we can verify the layer was added with correct media type above |
| 58 | +} |
| 59 | + |
| 60 | +func TestWithMultimodalProjectorInvalidPath(t *testing.T) { |
| 61 | + // Create a builder from a GGUF file |
| 62 | + b, err := builder.FromGGUF(filepath.Join("..", "assets", "dummy.gguf")) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Failed to create builder from GGUF: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Try to add multimodal projector with invalid path |
| 68 | + _, err = b.WithMultimodalProjector("nonexistent/path/to/mmproj") |
| 69 | + if err == nil { |
| 70 | + t.Error("Expected error when adding multimodal projector with invalid path") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestWithMultimodalProjectorChaining(t *testing.T) { |
| 75 | + // Create a builder from a GGUF file |
| 76 | + b, err := builder.FromGGUF(filepath.Join("..", "assets", "dummy.gguf")) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("Failed to create builder from GGUF: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + // Chain multiple operations: license + multimodal projector + context size |
| 82 | + b, err = b.WithLicense(filepath.Join("..", "assets", "license.txt")) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Failed to add license: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + b, err = b.WithMultimodalProjector(filepath.Join("..", "assets", "dummy.mmproj")) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("Failed to add multimodal projector: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + b = b.WithContextSize(4096) |
| 93 | + |
| 94 | + // Build the model |
| 95 | + target := &fakeTarget{} |
| 96 | + if err := b.Build(t.Context(), target, nil); err != nil { |
| 97 | + t.Fatalf("Failed to build model: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Verify the final model has all expected layers and properties |
| 101 | + manifest, err := target.artifact.Manifest() |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("Failed to get manifest: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + // Should have 3 layers: GGUF + license + multimodal projector |
| 107 | + if len(manifest.Layers) != 3 { |
| 108 | + t.Fatalf("Expected 3 layers, got %d", len(manifest.Layers)) |
| 109 | + } |
| 110 | + |
| 111 | + // Check media types - using string comparison since we can't use types.MediaType directly |
| 112 | + expectedMediaTypes := map[string]bool{ |
| 113 | + string(types.MediaTypeGGUF): false, |
| 114 | + string(types.MediaTypeLicense): false, |
| 115 | + string(types.MediaTypeMultimodalProjector): false, |
| 116 | + } |
| 117 | + |
| 118 | + for _, layer := range manifest.Layers { |
| 119 | + if _, exists := expectedMediaTypes[string(layer.MediaType)]; exists { |
| 120 | + expectedMediaTypes[string(layer.MediaType)] = true |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + for mediaType, found := range expectedMediaTypes { |
| 125 | + if !found { |
| 126 | + t.Errorf("Expected to find layer with media type %s", mediaType) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Check context size |
| 131 | + config, err := target.artifact.Config() |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("Failed to get config: %v", err) |
| 134 | + } |
| 135 | + |
| 136 | + if config.ContextSize == nil || *config.ContextSize != 4096 { |
| 137 | + t.Errorf("Expected context size 4096, got %v", config.ContextSize) |
| 138 | + } |
| 139 | + |
| 140 | + // Note: We can't directly test GGUFPath() and MMPROJPath() on ModelArtifact interface |
| 141 | + // but we can verify the layers were added with correct media types above |
| 142 | +} |
| 143 | + |
| 144 | +var _ builder.Target = &fakeTarget{} |
| 145 | + |
| 146 | +type fakeTarget struct { |
| 147 | + artifact types.ModelArtifact |
| 148 | +} |
| 149 | + |
| 150 | +func (ft *fakeTarget) Write(ctx context.Context, artifact types.ModelArtifact, writer io.Writer) error { |
| 151 | + ft.artifact = artifact |
| 152 | + return nil |
| 153 | +} |
0 commit comments