Skip to content

Commit fe2bdae

Browse files
authored
feat: padding the diff_ids in the model config (#121)
Signed-off-by: chlins <[email protected]>
1 parent b7535a9 commit fe2bdae

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

pkg/backend/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (b *backend) Build(ctx context.Context, modelfilePath, workDir, target stri
7878

7979
layers = append(layers, layerDescs...)
8080
// build the image config.
81-
configDesc, err := builder.BuildConfig(ctx, hooks.NewHooks(
81+
configDesc, err := builder.BuildConfig(ctx, layers, hooks.NewHooks(
8282
hooks.WithOnStart(func(name string, size int64, reader io.Reader) io.Reader {
8383
return pb.Add(internalpb.NormalizePrompt("Building config"), name, size, reader)
8484
}),

pkg/backend/build/builder.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1"
3535
sha256 "github.com/minio/sha256-simd"
36+
godigest "github.com/opencontainers/go-digest"
3637
spec "github.com/opencontainers/image-spec/specs-go"
3738
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3839
)
@@ -58,7 +59,7 @@ type Builder interface {
5859
BuildLayer(ctx context.Context, mediaType, workDir, path string, hooks hooks.Hooks) (ocispec.Descriptor, error)
5960

6061
// BuildConfig builds the config blob of the artifact.
61-
BuildConfig(ctx context.Context, hooks hooks.Hooks) (ocispec.Descriptor, error)
62+
BuildConfig(ctx context.Context, layers []ocispec.Descriptor, hooks hooks.Hooks) (ocispec.Descriptor, error)
6263

6364
// BuildManifest builds the manifest blob of the artifact.
6465
BuildManifest(ctx context.Context, layers []ocispec.Descriptor, config ocispec.Descriptor, annotations map[string]string, hooks hooks.Hooks) (ocispec.Descriptor, error)
@@ -148,8 +149,8 @@ func (ab *abstractBuilder) BuildLayer(ctx context.Context, mediaType, workDir, p
148149
return ab.strategy.OutputLayer(ctx, mediaType, workDir, relPath, info.Size()+tarHeaderSize, reader, hooks)
149150
}
150151

151-
func (ab *abstractBuilder) BuildConfig(ctx context.Context, hooks hooks.Hooks) (ocispec.Descriptor, error) {
152-
config, err := buildModelConfig(ab.modelfile)
152+
func (ab *abstractBuilder) BuildConfig(ctx context.Context, layers []ocispec.Descriptor, hooks hooks.Hooks) (ocispec.Descriptor, error) {
153+
config, err := buildModelConfig(ab.modelfile, layers)
153154
if err != nil {
154155
return ocispec.Descriptor{}, fmt.Errorf("failed to build model config: %w", err)
155156
}
@@ -189,7 +190,7 @@ func (ab *abstractBuilder) BuildManifest(ctx context.Context, layers []ocispec.D
189190
}
190191

191192
// buildModelConfig builds the model config.
192-
func buildModelConfig(modelfile modelfile.Modelfile) (*modelspec.Model, error) {
193+
func buildModelConfig(modelfile modelfile.Modelfile, layers []ocispec.Descriptor) (*modelspec.Model, error) {
193194
config := modelspec.ModelConfig{
194195
Architecture: modelfile.GetArch(),
195196
Format: modelfile.GetFormat(),
@@ -205,8 +206,14 @@ func buildModelConfig(modelfile modelfile.Modelfile) (*modelspec.Model, error) {
205206
Name: modelfile.GetName(),
206207
}
207208

209+
diffIDs := make([]godigest.Digest, 0, len(layers))
210+
for _, layer := range layers {
211+
diffIDs = append(diffIDs, layer.Digest)
212+
}
213+
208214
fs := modelspec.ModelFS{
209-
Type: "layers",
215+
Type: "layers",
216+
DiffIDs: diffIDs,
210217
}
211218

212219
return &modelspec.Model{

pkg/backend/build/builder_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
storagemock "github.com/CloudNativeAI/modctl/test/mocks/storage"
3232

3333
modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1"
34+
godigest "github.com/opencontainers/go-digest"
3435
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3536
"github.com/stretchr/testify/mock"
3637
"github.com/stretchr/testify/suite"
@@ -159,7 +160,7 @@ func (s *BuilderTestSuite) TestBuildConfig() {
159160
s.mockOutputStrategy.On("OutputConfig", mock.Anything, modelspec.MediaTypeModelConfig, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
160161
Return(expectedDesc, nil).Once()
161162

162-
desc, err := s.builder.BuildConfig(context.Background(), hooks.NewHooks())
163+
desc, err := s.builder.BuildConfig(context.Background(), []ocispec.Descriptor{}, hooks.NewHooks())
163164
s.NoError(err)
164165
s.Equal(expectedDesc, desc)
165166

@@ -179,7 +180,7 @@ func (s *BuilderTestSuite) TestBuildConfig() {
179180
s.mockOutputStrategy.On("OutputConfig", mock.Anything, modelspec.MediaTypeModelConfig, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
180181
Return(ocispec.Descriptor{}, errors.New("output error")).Once()
181182

182-
_, err := s.builder.BuildConfig(context.Background(), hooks.NewHooks())
183+
_, err := s.builder.BuildConfig(context.Background(), []ocispec.Descriptor{}, hooks.NewHooks())
183184
s.Error(err)
184185
s.True(strings.Contains(err.Error(), "output error"))
185186
})
@@ -239,7 +240,10 @@ func (s *BuilderTestSuite) TestBuildModelConfig() {
239240
mockModelfile.On("GetFamily").Return("llama")
240241
mockModelfile.On("GetName").Return("llama-2")
241242

242-
model, err := buildModelConfig(mockModelfile)
243+
model, err := buildModelConfig(mockModelfile, []ocispec.Descriptor{
244+
{Digest: godigest.Digest("sha256:layer-1")},
245+
{Digest: godigest.Digest("sha256:layer-2")},
246+
})
243247
s.NoError(err)
244248
s.NotNil(model)
245249

@@ -255,6 +259,9 @@ func (s *BuilderTestSuite) TestBuildModelConfig() {
255259
s.WithinDuration(time.Now(), *model.Descriptor.CreatedAt, 5*time.Second)
256260

257261
s.Equal("layers", model.ModelFS.Type)
262+
s.Len(model.ModelFS.DiffIDs, 2)
263+
s.Equal("sha256:layer-1", model.ModelFS.DiffIDs[0].String())
264+
s.Equal("sha256:layer-2", model.ModelFS.DiffIDs[1].String())
258265
}
259266

260267
func TestBuilderSuite(t *testing.T) {

test/mocks/backend/build/builder.go

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)