Skip to content

feat: migrate legacy hf.co tags to huggingface.co during client initialization#645

Draft
ilopezluna wants to merge 1 commit intomainfrom
migrate-hf-tags
Draft

feat: migrate legacy hf.co tags to huggingface.co during client initialization#645
ilopezluna wants to merge 1 commit intomainfrom
migrate-hf-tags

Conversation

@ilopezluna
Copy link
Contributor

Models pulled with hf.co/ before the normalization fix in #617 were stored with un-normalized tags, causing cache misses and duplicate downloads. This adds an automatic tag migration on client startup that rewrites hf.co/huggingface.co/ in the store index, so previously-pulled models are found by the cache check.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ilopezluna, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where models pulled with the legacy hf.co domain were stored with un-normalized tags, leading to cache misses and redundant downloads. It introduces an automatic migration mechanism that rewrites these legacy tags to huggingface.co upon client initialization, ensuring that previously downloaded models are correctly recognized and utilized by the caching system. This change significantly improves the client's ability to manage and retrieve cached models efficiently.

Highlights

  • Legacy Tag Migration: Implemented an automatic migration process during client initialization to convert hf.co/ model tags to huggingface.co/ in the local store.
  • Store Tag Transformation: Introduced a new MigrateTags function in the LocalStore to apply a transformation function to all stored tags, enabling flexible tag updates.
  • Improved Cache Reliability: This migration ensures that models previously pulled with the old hf.co domain are correctly identified by the cache, preventing duplicate downloads and cache misses.
  • Comprehensive Testing: Added unit tests to verify the client's tag migration logic on initialization and the LocalStore's MigrateTags functionality, including scenarios for successful migration, no-op, and unaffected tags.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • pkg/distribution/distribution/client.go
    • Introduced a new private method migrateHFTags to encapsulate the logic for migrating hf.co tags to huggingface.co.
    • Integrated the call to c.migrateHFTags() within the NewClient function, ensuring tag migration occurs automatically when a client is initialized.
    • Added logging for successful migrations and warnings for migration failures.
  • pkg/distribution/distribution/client_test.go
    • Added TestMigrateHFTagsOnClientInit to verify that the client correctly migrates legacy hf.co tags to huggingface.co upon initialization, and that models can subsequently be found using the normalized reference.
  • pkg/distribution/internal/store/store.go
    • Added a new public method MigrateTags(transform func(string) string) (int, error) to the LocalStore interface and its LocalStore implementation. This function iterates through all stored model tags and applies a provided transformation function, returning the count of migrated tags.
  • pkg/distribution/internal/store/store_test.go
    • Added TestMigrateTags to thoroughly test the LocalStore's new MigrateTags function, covering scenarios like initial migration, unaffected tags, and idempotent behavior on subsequent runs.
Activity
  • There has been no human activity on this pull request since its creation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an automatic migration of legacy hf.co/ tags to the canonical huggingface.co/ format upon client initialization. This is a valuable fix to prevent cache misses for models downloaded before the normalization was in place. The implementation is clean, with the migration logic well-encapsulated in client.migrateHFTags and store.MigrateTags. The accompanying tests are thorough, covering migration scenarios and ensuring idempotency. I have a couple of minor suggestions to improve the test code's clarity and maintainability.

Comment on lines +1213 to +1220
for _, tag := range tags {
if strings.Contains(tag, "hf.co/") && !strings.Contains(tag, "huggingface.co/") {
hasOldTag = true
}
if strings.Contains(tag, "huggingface.co/") {
hasNewTag = true
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic to check for old and new tags can be simplified and made more robust by using strings.HasPrefix instead of strings.Contains. This more clearly expresses the intent of checking for a specific domain prefix and avoids potential false positives if hf.co appeared elsewhere in a tag.

Suggested change
for _, tag := range tags {
if strings.Contains(tag, "hf.co/") && !strings.Contains(tag, "huggingface.co/") {
hasOldTag = true
}
if strings.Contains(tag, "huggingface.co/") {
hasNewTag = true
}
}
for _, tag := range tags {
if strings.HasPrefix(tag, "hf.co/") {
hasOldTag = true
}
if strings.HasPrefix(tag, "huggingface.co/") {
hasNewTag = true
}
}

Comment on lines +1026 to +1061
migrated, err := s.MigrateTags(func(tag string) string {
if rest, found := strings.CutPrefix(tag, "hf.co/"); found {
return "huggingface.co/" + rest
}
return tag
})
if err != nil {
t.Fatalf("MigrateTags failed: %v", err)
}

if migrated != 1 {
t.Errorf("Expected 1 migrated tag, got %d", migrated)
}

// Verify the model can be found with the new tag
if _, err := s.Read("huggingface.co/testorg/testmodel:latest"); err != nil {
t.Fatalf("Failed to read model with migrated tag: %v", err)
}

// Verify the old tag no longer works
if _, err := s.Read("hf.co/testorg/testmodel:latest"); !errors.Is(err, store.ErrModelNotFound) {
t.Errorf("Expected ErrModelNotFound for old tag, got: %v", err)
}

// Verify the non-HF model is unaffected
if _, err := s.Read("ai/some-model:latest"); err != nil {
t.Fatalf("Non-HF model should be unaffected: %v", err)
}

// Running migration again should be a no-op
migrated2, err := s.MigrateTags(func(tag string) string {
if rest, found := strings.CutPrefix(tag, "hf.co/"); found {
return "huggingface.co/" + rest
}
return tag
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The tag transformation logic is duplicated in this test. To improve maintainability and reduce redundancy, consider defining the transformation function once in a variable and reusing it for both migration calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant