@@ -21,6 +21,7 @@ import (
2121 "encoding/json"
2222 "fmt"
2323 "io"
24+ "os"
2425 "reflect"
2526 "slices"
2627 "sort"
@@ -73,33 +74,6 @@ func (b *backend) Attach(ctx context.Context, filepath string, cfg *config.Attac
7374
7475 logrus .Infof ("attach: loaded source model config [%+v]" , srcModelConfig )
7576
76- var foundLayer * ocispec.Descriptor
77- for _ , layer := range srcManifest .Layers {
78- if anno := layer .Annotations ; anno != nil {
79- if anno [modelspec .AnnotationFilepath ] == filepath {
80- if ! cfg .Force {
81- return fmt .Errorf ("file %s already exists, please use --force to overwrite if you want to attach it forcibly" , filepath )
82- }
83-
84- foundLayer = & layer
85- break
86- }
87- }
88- }
89-
90- logrus .Infof ("attach: found existing layer for file %s [%+v]" , filepath , foundLayer )
91-
92- layers := srcManifest .Layers
93- if foundLayer != nil {
94- // Remove the found layer from the layers slice as we need to replace it with the new layer.
95- for i , layer := range layers {
96- if layer .Digest == foundLayer .Digest && layer .MediaType == foundLayer .MediaType {
97- layers = slices .Delete (layers , i , i + 1 )
98- break
99- }
100- }
101- }
102-
10377 proc := b .getProcessor (filepath , cfg .Raw )
10478 if proc == nil {
10579 return fmt .Errorf ("failed to get processor for file %s" , filepath )
@@ -114,40 +88,86 @@ func (b *backend) Attach(ctx context.Context, filepath string, cfg *config.Attac
11488 pb .Start ()
11589 defer pb .Stop ()
11690
117- newLayers , err := proc .Process (ctx , builder , "." , processor .WithProgressTracker (pb ))
118- if err != nil {
119- return fmt .Errorf ("failed to process layers: %w" , err )
120- }
91+ layers := srcManifest .Layers
92+ // If attach a normal file, we need to process it and create a new layer.
93+ if ! cfg .Config {
94+ var foundLayer * ocispec.Descriptor
95+ for _ , layer := range srcManifest .Layers {
96+ if anno := layer .Annotations ; anno != nil {
97+ if anno [modelspec .AnnotationFilepath ] == filepath {
98+ if ! cfg .Force {
99+ return fmt .Errorf ("file %s already exists, please use --force to overwrite if you want to attach it forcibly" , filepath )
100+ }
101+
102+ foundLayer = & layer
103+ break
104+ }
105+ }
106+ }
121107
122- // Append the new layers to the original layers.
123- layers = append (layers , newLayers ... )
124- sortLayers (layers )
108+ logrus .Infof ("attach: found existing layer for file %s [%+v]" , filepath , foundLayer )
109+ if foundLayer != nil {
110+ // Remove the found layer from the layers slice as we need to replace it with the new layer.
111+ for i , layer := range layers {
112+ if layer .Digest == foundLayer .Digest && layer .MediaType == foundLayer .MediaType {
113+ layers = slices .Delete (layers , i , i + 1 )
114+ break
115+ }
116+ }
117+ }
125118
126- logrus .Debugf ("attach: generated sorted layers [layers: %+v]" , layers )
119+ newLayers , err := proc .Process (ctx , builder , "." , processor .WithProgressTracker (pb ))
120+ if err != nil {
121+ return fmt .Errorf ("failed to process layers: %w" , err )
122+ }
127123
128- diffIDs := []godigest.Digest {}
129- for _ , layer := range layers {
130- diffIDs = append (diffIDs , layer .Digest )
131- }
132- // Return earlier if the diffID has no changed, which means the artifact has not changed.
133- if reflect .DeepEqual (diffIDs , srcModelConfig .ModelFS .DiffIDs ) {
134- return nil
124+ // Append the new layers to the original layers.
125+ layers = append (layers , newLayers ... )
126+ sortLayers (layers )
127+
128+ logrus .Debugf ("attach: generated sorted layers [layers: %+v]" , layers )
129+
130+ diffIDs := []godigest.Digest {}
131+ for _ , layer := range layers {
132+ diffIDs = append (diffIDs , layer .Digest )
133+ }
134+ // Return earlier if the diffID has no changed, which means the artifact has not changed.
135+ if reflect .DeepEqual (diffIDs , srcModelConfig .ModelFS .DiffIDs ) {
136+ return nil
137+ }
135138 }
136139
137- // Build the model config.
138- modelConfig := & buildconfig.Model {
139- Architecture : srcModelConfig .Config .Architecture ,
140- Format : srcModelConfig .Config .Format ,
141- Precision : srcModelConfig .Config .Precision ,
142- Quantization : srcModelConfig .Config .Quantization ,
143- ParamSize : srcModelConfig .Config .ParamSize ,
144- Family : srcModelConfig .Descriptor .Family ,
145- Name : srcModelConfig .Descriptor .Name ,
140+ var config modelspec.Model
141+ if ! cfg .Config {
142+ config , err = build .BuildModelConfig (& buildconfig.Model {
143+ Architecture : srcModelConfig .Config .Architecture ,
144+ Format : srcModelConfig .Config .Format ,
145+ Precision : srcModelConfig .Config .Precision ,
146+ Quantization : srcModelConfig .Config .Quantization ,
147+ ParamSize : srcModelConfig .Config .ParamSize ,
148+ Family : srcModelConfig .Descriptor .Family ,
149+ Name : srcModelConfig .Descriptor .Name ,
150+ SourceURL : srcModelConfig .Descriptor .SourceURL ,
151+ SourceRevision : srcModelConfig .Descriptor .Revision ,
152+ }, layers )
153+ if err != nil {
154+ return fmt .Errorf ("failed to build model config: %w" , err )
155+ }
156+ } else {
157+ configFile , err := os .Open (filepath )
158+ if err != nil {
159+ return fmt .Errorf ("failed to open config file: %w" , err )
160+ }
161+ defer configFile .Close ()
162+
163+ if err := json .NewDecoder (configFile ).Decode (& config ); err != nil {
164+ return fmt .Errorf ("failed to decode config file %s: %w" , filepath , err )
165+ }
146166 }
147167
148- logrus .Infof ("attach: built model config [%+v]" , modelConfig )
168+ logrus .Infof ("attach: built model config [%+v]" , config )
149169
150- configDesc , err := builder .BuildConfig (ctx , layers , modelConfig , hooks .NewHooks (
170+ configDesc , err := builder .BuildConfig (ctx , config , hooks .NewHooks (
151171 hooks .WithOnStart (func (name string , size int64 , reader io.Reader ) io.Reader {
152172 return pb .Add (internalpb .NormalizePrompt ("Building config" ), name , size , reader )
153173 }),
0 commit comments