|
| 1 | +package modelpack |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "maps" |
| 8 | + "strings" |
| 9 | + |
| 10 | + v1 "github.com/docker/model-runner/pkg/go-containerregistry/pkg/v1" |
| 11 | + "github.com/docker/model-runner/pkg/go-containerregistry/pkg/v1/partial" |
| 12 | + ggcr "github.com/docker/model-runner/pkg/go-containerregistry/pkg/v1/types" |
| 13 | + |
| 14 | + mdpartial "github.com/docker/model-runner/pkg/distribution/internal/partial" |
| 15 | + "github.com/docker/model-runner/pkg/distribution/types" |
| 16 | +) |
| 17 | + |
| 18 | +// Ensure convertedArtifact implements types.ModelArtifact |
| 19 | +var _ types.ModelArtifact = &convertedArtifact{} |
| 20 | + |
| 21 | +// convertedArtifact wraps a ModelPack format model artifact and presents it |
| 22 | +// as a Docker format model artifact. It converts the config on-the-fly while |
| 23 | +// delegating layer operations to the underlying source. |
| 24 | +type convertedArtifact struct { |
| 25 | + source types.ModelArtifact |
| 26 | + convertedConfig *types.ConfigFile |
| 27 | + rawConvertedJSON []byte |
| 28 | + configDigest v1.Hash |
| 29 | +} |
| 30 | + |
| 31 | +// NewConvertedArtifact creates a new artifact wrapper that converts a ModelPack |
| 32 | +// format model to Docker format. The conversion happens immediately and the |
| 33 | +// converted config is cached for subsequent accesses. |
| 34 | +func NewConvertedArtifact(source types.ModelArtifact) (*convertedArtifact, error) { |
| 35 | + // Get the original ModelPack config |
| 36 | + rawConfig, err := source.RawConfigFile() |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("get source config: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Convert to Docker format |
| 42 | + converted, err := ConvertToDockerConfig(rawConfig) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("convert config: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Serialize the converted config |
| 48 | + rawJSON, err := json.Marshal(converted) |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("marshal converted config: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + // Compute the digest of the converted config |
| 54 | + configDigest, _, err := v1.SHA256(bytes.NewReader(rawJSON)) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("compute config digest: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + return &convertedArtifact{ |
| 60 | + source: source, |
| 61 | + convertedConfig: converted, |
| 62 | + rawConvertedJSON: rawJSON, |
| 63 | + configDigest: configDigest, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +// ID returns the model ID (manifest digest). |
| 68 | +// This will differ from the source because the config has changed. |
| 69 | +func (a *convertedArtifact) ID() (string, error) { |
| 70 | + return mdpartial.ID(a) |
| 71 | +} |
| 72 | + |
| 73 | +// Config returns the converted Docker format config. |
| 74 | +func (a *convertedArtifact) Config() (types.Config, error) { |
| 75 | + return a.convertedConfig.Config, nil |
| 76 | +} |
| 77 | + |
| 78 | +// Descriptor returns the model descriptor (provenance info). |
| 79 | +func (a *convertedArtifact) Descriptor() (types.Descriptor, error) { |
| 80 | + return a.convertedConfig.Descriptor, nil |
| 81 | +} |
| 82 | + |
| 83 | +// Layers returns the model layers from the source. |
| 84 | +// Layers are unchanged during conversion. |
| 85 | +func (a *convertedArtifact) Layers() ([]v1.Layer, error) { |
| 86 | + return a.source.Layers() |
| 87 | +} |
| 88 | + |
| 89 | +// MediaType returns the manifest media type. |
| 90 | +func (a *convertedArtifact) MediaType() (ggcr.MediaType, error) { |
| 91 | + return a.source.MediaType() |
| 92 | +} |
| 93 | + |
| 94 | +// Size returns the size of the manifest. |
| 95 | +func (a *convertedArtifact) Size() (int64, error) { |
| 96 | + return partial.Size(a) |
| 97 | +} |
| 98 | + |
| 99 | +// ConfigName returns the hash of the converted config file. |
| 100 | +func (a *convertedArtifact) ConfigName() (v1.Hash, error) { |
| 101 | + return a.configDigest, nil |
| 102 | +} |
| 103 | + |
| 104 | +// ConfigFile returns nil as this is a model artifact, not a container image. |
| 105 | +// Model artifacts use RawConfigFile() to access configuration. |
| 106 | +func (a *convertedArtifact) ConfigFile() (*v1.ConfigFile, error) { |
| 107 | + return nil, fmt.Errorf("ConfigFile is not supported for model artifacts; use RawConfigFile instead") |
| 108 | +} |
| 109 | + |
| 110 | +// RawConfigFile returns the serialized bytes of the converted Docker format config. |
| 111 | +// This is the key method that makes the conversion work - all other code that |
| 112 | +// reads the config will get the Docker format version. |
| 113 | +func (a *convertedArtifact) RawConfigFile() ([]byte, error) { |
| 114 | + return a.rawConvertedJSON, nil |
| 115 | +} |
| 116 | + |
| 117 | +// Digest returns the sha256 of the manifest. |
| 118 | +// This will differ from the source because the config digest has changed. |
| 119 | +func (a *convertedArtifact) Digest() (v1.Hash, error) { |
| 120 | + return partial.Digest(a) |
| 121 | +} |
| 122 | + |
| 123 | +// Manifest returns the manifest with Docker config media type. |
| 124 | +func (a *convertedArtifact) Manifest() (*v1.Manifest, error) { |
| 125 | + // Get the source manifest as a base |
| 126 | + srcManifest, err := a.source.Manifest() |
| 127 | + if err != nil { |
| 128 | + return nil, fmt.Errorf("get source manifest: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + // Deep copy layers and convert media types from ModelPack to Docker format |
| 132 | + layers := make([]v1.Descriptor, len(srcManifest.Layers)) |
| 133 | + for i, layer := range srcManifest.Layers { |
| 134 | + layers[i] = v1.Descriptor{ |
| 135 | + MediaType: convertLayerMediaType(layer.MediaType, a.convertedConfig.Config.Format), |
| 136 | + Size: layer.Size, |
| 137 | + Digest: layer.Digest, |
| 138 | + URLs: layer.URLs, |
| 139 | + Annotations: maps.Clone(layer.Annotations), |
| 140 | + Data: layer.Data, |
| 141 | + Platform: layer.Platform, |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Create a deep copy to avoid sharing references with source |
| 146 | + manifest := &v1.Manifest{ |
| 147 | + SchemaVersion: srcManifest.SchemaVersion, |
| 148 | + MediaType: srcManifest.MediaType, |
| 149 | + Config: v1.Descriptor{ |
| 150 | + MediaType: types.MediaTypeModelConfigV01, |
| 151 | + Size: int64(len(a.rawConvertedJSON)), |
| 152 | + Digest: a.configDigest, |
| 153 | + }, |
| 154 | + Layers: layers, |
| 155 | + Annotations: maps.Clone(srcManifest.Annotations), |
| 156 | + Subject: srcManifest.Subject, // Preserve referrer relationship if present |
| 157 | + } |
| 158 | + |
| 159 | + return manifest, nil |
| 160 | +} |
| 161 | + |
| 162 | +// convertLayerMediaType converts ModelPack layer media types to Docker format. |
| 163 | +// This is essential because docker/model-runner uses media types to identify layer types. |
| 164 | +func convertLayerMediaType(srcMediaType ggcr.MediaType, format types.Format) ggcr.MediaType { |
| 165 | + mediaTypeStr := string(srcMediaType) |
| 166 | + |
| 167 | + // Only convert if it's a ModelPack media type |
| 168 | + if !strings.HasPrefix(mediaTypeStr, MediaTypePrefix) { |
| 169 | + return srcMediaType |
| 170 | + } |
| 171 | + |
| 172 | + // Convert based on the model format |
| 173 | + switch format { |
| 174 | + case types.FormatGGUF: |
| 175 | + // ModelPack weight layers become Docker GGUF layers |
| 176 | + if strings.Contains(mediaTypeStr, ".weight.") { |
| 177 | + return types.MediaTypeGGUF |
| 178 | + } |
| 179 | + case types.FormatSafetensors: |
| 180 | + // ModelPack weight layers become Docker safetensors layers |
| 181 | + if strings.Contains(mediaTypeStr, ".weight.") { |
| 182 | + return types.MediaTypeSafetensors |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // For other media types (doc, code, dataset, etc.), keep as-is |
| 187 | + // These aren't used by docker/model-runner's core inference path |
| 188 | + return srcMediaType |
| 189 | +} |
| 190 | + |
| 191 | +// RawManifest returns the serialized bytes of the converted manifest. |
| 192 | +func (a *convertedArtifact) RawManifest() ([]byte, error) { |
| 193 | + manifest, err := a.Manifest() |
| 194 | + if err != nil { |
| 195 | + return nil, err |
| 196 | + } |
| 197 | + return json.Marshal(manifest) |
| 198 | +} |
| 199 | + |
| 200 | +// LayerByDigest returns a layer by its digest. |
| 201 | +func (a *convertedArtifact) LayerByDigest(hash v1.Hash) (v1.Layer, error) { |
| 202 | + return a.source.LayerByDigest(hash) |
| 203 | +} |
| 204 | + |
| 205 | +// LayerByDiffID returns a layer by its diff ID. |
| 206 | +func (a *convertedArtifact) LayerByDiffID(hash v1.Hash) (v1.Layer, error) { |
| 207 | + return a.source.LayerByDiffID(hash) |
| 208 | +} |
0 commit comments