Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init model.list utils #1240

Merged
merged 10 commits into from
Sep 18, 2024
Merged

Init model.list utils #1240

merged 10 commits into from
Sep 18, 2024

Conversation

nguyenhoangthuan99
Copy link
Contributor

@nguyenhoangthuan99 nguyenhoangthuan99 commented Sep 18, 2024

ModelListUtils Class Implementation

Fix #1241

Overview

The ModelListUtils class provides a robust and thread-safe interface for managing a list of machine learning models. It handles operations such as adding, updating, deleting, and retrieving model information, with a focus on maintaining unique identifiers for each model.

Key Features

  1. Thread-safe Operations: Uses mutex locking to ensure thread safety for all operations.
  2. File-based Storage: Manages model information in a text file, allowing for persistence between program runs.
  3. Unique Identifier Generation: Automatically generates unique, shortened aliases for models.
  4. CRUD Operations: Supports Creating, Reading, Updating, and Deleting model entries.
  5. Error Handling: Robust error checking and exception throwing for invalid operations.

Main Components

ModelEntry Struct

Represents a single model entry with fields:

  • model_id: Unique identifier for the model
  • author_repo_id: Author or repository identifier
  • branch_name: Branch name in the repository
  • path_to_model_yaml: Path to the model's YAML file
  • model_alias: Shortened alias for the model
  • status: Current status of the model (READY or RUNNING)

ModelListUtils Class Methods

  1. LoadModelList()

    • Loads model entries from the file, creating the file if it doesn't exist.
    • Returns a vector of ModelEntry objects.
  2. SaveModelList()

    • Saves the current list of model entries to the file.
  3. GetModelInfo(identifier)

    • Retrieves model information based on model_id or model_alias.
    • Throws an exception if the model is not found.
  4. AddModelEntry(new_entry, use_short_alias)

    • Adds a new model entry to the list.
    • Optionally generates a shortened alias for the model.
    • Ensures uniqueness of model_id and model_alias.
  5. UpdateModelEntry(identifier, updated_entry)

    • Updates an existing model entry.
    • Identified by either model_id or model_alias.
  6. DeleteModelEntry(identifier)

    • Deletes a model entry if it exists and is in READY state.
  7. PrintModelInfo(entry)

    • Prints detailed information about a model entry.
  8. GenerateShortenedAlias(model_id, entries)

    • Generates a unique, shortened alias for a model.
    • Uses a hierarchical approach to create aliases, prioritizing brevity while ensuring uniqueness.

Alias Generation Logic

The GenerateShortenedAlias function creates aliases in the following order:

  1. Filename only (e.g., model_id_xxx)
  2. Parent directory + filename (e.g., llama3.1-7b-gguf:model_id_xxx)
  3. Grandparent directory + parent directory + filename (e.g., bartowski:llama3.1-7b-gguf/model_id_xxx)
  4. Full path (e.g., huggingface.co:bartowski/llama3.1-7b-gguf/model_id_xxx)

It returns the shortest unique alias, appending a numeric suffix if necessary to ensure uniqueness.

Usage Example

ModelListUtils model_list;

// Add a new model from other source, enable shorten the model-id
ModelEntry new_model = {...};
if (model_list.AddModelEntry(new_model, true)) {
    std::cout << "Model added successfully" << std::endl;
}

// Add a new model from cortexso, disable shorten the model-id
ModelEntry new_model = {...};
if (model_list.AddModelEntry(new_model, false)) {
    std::cout << "Model added successfully" << std::endl;
}

// Retrieve model info
try {
    ModelEntry model = model_list.GetModelInfo("model_alias");
    model_list.PrintModelInfo(model);
} catch (const std::runtime_error& e) {
    std::cerr << "Error: " << e.what() << std::endl;
}

// Update a model
ModelEntry updated_model = {...};
if (model_list.UpdateModelEntry("model_id", updated_model)) {
    std::cout << "Model updated successfully" << std::endl;
}

// Delete a model
if (model_list.DeleteModelEntry("model_alias")) {
    std::cout << "Model deleted successfully" << std::endl;
}

This implementation provides a flexible and efficient way to manage machine learning models, with a focus on maintaining unique identifiers and thread-safe operations.

@nguyenhoangthuan99 nguyenhoangthuan99 marked this pull request as ready for review September 18, 2024 01:20
@vansangpfiev
Copy link
Contributor

It would be nice if we have unit tests for ModelListUtils class

@nguyenhoangthuan99 nguyenhoangthuan99 merged commit b8078af into dev Sep 18, 2024
4 checks passed
@nguyenhoangthuan99 nguyenhoangthuan99 deleted the feat/model.list-utils branch September 18, 2024 07:43
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.

epic: SQLite database implementation
2 participants