Skip to content

feat(tools): add HuggingFace config converter#197

Closed
rishi-jat wants to merge 2 commits intomodelpack:mainfrom
rishi-jat:feat/hf-to-arch-tool
Closed

feat(tools): add HuggingFace config converter#197
rishi-jat wants to merge 2 commits intomodelpack:mainfrom
rishi-jat:feat/hf-to-arch-tool

Conversation

@rishi-jat
Copy link

Adds tool to convert HuggingFace config.json to architecture_config format.

Usage:

python tools/hf_to_arch.py config.json

Tests included.

Signed-off-by: Rishi Jat <rishijat098@gmail.com>
Signed-off-by: Rishi Jat <rishijat098@gmail.com>
Copilot AI review requested due to automatic review settings March 25, 2026 23:57
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 introduces a standardized architecture_config structure to capture essential model architecture parameters. It includes updates to the JSON schema and Go type definitions, alongside a new Python utility designed to convert existing HuggingFace config.json files into this new format. The changes are supported by comprehensive unit tests to ensure correctness and robustness.

Highlights

  • New Architecture Configuration Schema: A new ArchitectureConfig JSON schema has been defined, standardizing parameters such as type, numLayers, hiddenSize, and numAttentionHeads. This configuration is now integrated as an optional property within the ModelConfig schema.
  • Go Type Definitions: Corresponding Go structs for ArchitectureConfig have been added to specs-go/v1/config.go, ensuring type safety and consistency with the new schema.
  • HuggingFace Config Conversion Tool: A new Python script, hf_to_arch.py, has been introduced to convert relevant fields from a HuggingFace config.json file into the newly defined architecture_config format. This tool includes robust error handling for various input issues.
  • Comprehensive Testing: New Go tests validate the architecture_config against its JSON schema, covering both valid and invalid configurations. Additionally, a dedicated Python test suite (hf_to_arch_test.py) ensures the reliability and error handling of the conversion script.
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.

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 a new ArchitectureConfig to the model configuration, defining architecture-specific parameters such as the number of layers, hidden size, and attention heads. This involves updates to the JSON schema, corresponding Go structs, and new validation tests. Additionally, a Python script hf_to_arch.py is added to convert HuggingFace configurations to this new format, complete with comprehensive unit tests. The review suggests improving the Python script's command-line argument parsing by using argparse and refactoring the Python test script to utilize the unittest framework for enhanced robustness and maintainability.

Comment on lines +33 to +37
if len(sys.argv) != 2:
print(f"usage: {sys.argv[0]} <config.json>", file=sys.stderr)
sys.exit(1)

config_path = sys.argv[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For more robust and maintainable command-line argument parsing, consider using the argparse module from the standard library. It provides features like automatic help message generation (-h/--help) and is the standard for creating command-line interfaces in Python.

You will need to add import argparse at the top of the file.

Suggested change
if len(sys.argv) != 2:
print(f"usage: {sys.argv[0]} <config.json>", file=sys.stderr)
sys.exit(1)
config_path = sys.argv[1]
parser = argparse.ArgumentParser(description=__doc__.strip())
parser.add_argument("config_path", metavar="config.json", help="Path to HuggingFace config.json")
args = parser.parse_args()
config_path = args.config_path

Comment on lines +134 to +142
def main():
test_valid_config()
test_missing_field()
test_invalid_json()
test_file_not_found()
test_invalid_field_type()
test_zero_value()
test_bool_value()
print("\nAll tests passed.")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this test script is functional, structuring it with Python's standard unittest framework would make it more robust and align with common testing practices. unittest provides a test runner, test discovery, and a richer set of assertion methods (e.g., self.assertEqual(), self.assertIn()). This would also remove the need for a custom main function to run tests and manual print statements for test status.

For example, you could structure your tests in a class:

import unittest
# ... other imports

class HFToArchTests(unittest.TestCase):
    def test_valid_config(self):
        # ... test logic ...
        self.assertEqual(exitcode, 0, f"...")
        # ...

if __name__ == '__main__':
    unittest.main()

This approach is more maintainable and scalable as more tests are added.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for an architecture_config object in the model config schema/Go types and introduces a Python tool to convert a HuggingFace config.json into that architecture-config shape.

Changes:

  • Add tools/hf_to_arch.py converter (HF config.json → architecture config JSON)
  • Extend JSON schema + Go spec types to include architecture_config
  • Add schema validation tests and a Python test script for the converter

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tools/hf_to_arch.py New converter script that maps key HF fields into an architecture config JSON object
tools/hf_to_arch_test.py New Python test script that shells out to the converter and checks common failure modes
specs-go/v1/config.go Adds ArchitectureConfig to ModelConfig and defines the ArchitectureConfig struct
schema/config_test.go Adds Go tests to validate schema acceptance/rejection of architecture_config
schema/config-schema.json Adds architecture_config to ModelConfig and defines an ArchitectureConfig schema

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +596 to +603
"config": {
"paramSize": "8b",
"architecture_config": {
"type": "transformer",
"numLayers": 32,
"hiddenSize": 4096,
"numAttentionHeads": 32
}
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The tests and schema are using the new architecture_config field name. If the intent is to follow the existing lowerCamelCase JSON convention for config fields, consider renaming this to architectureConfig here (and in the schema + Go structs) to avoid introducing an inconsistent naming style.

Copilot uses AI. Check for mistakes.
Comment on lines +15 to +23
def convert_hf_config(hf_config: dict) -> dict:
"""Convert HuggingFace config to architecture_config format."""
arch_config = {"type": "transformer"}

for arch_key, hf_key in REQUIRED_MAPPINGS.items():
if hf_key not in hf_config:
raise ValueError(f"missing required field: {hf_key}")
value = hf_config[hf_key]
if not isinstance(value, int) or isinstance(value, bool):
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

convert_hf_config assumes the parsed JSON is a dict/object. If config.json is a JSON array/string/etc, the current checks (e.g., hf_key not in hf_config) will behave incorrectly or raise confusing errors. Consider validating hf_config is a dict up front and raising a clear ValueError if not.

Copilot uses AI. Check for mistakes.
exitcode, stdout, stderr = run_script("not valid json {")

assert exitcode != 0, "expected non-zero exit for invalid JSON"
assert "invalid JSON" in stderr.lower() or "json" in stderr.lower(), f"error should mention JSON: {stderr}"
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

This assertion lowercases stderr but checks for the mixed-case substring "invalid JSON", which can never match. Consider checking for "invalid json" (lowercase) or just relying on the existing "json" substring check.

Suggested change
assert "invalid JSON" in stderr.lower() or "json" in stderr.lower(), f"error should mention JSON: {stderr}"
assert "invalid json" in stderr.lower() or "json" in stderr.lower(), f"error should mention JSON: {stderr}"

Copilot uses AI. Check for mistakes.
Comment on lines +134 to +146
def main():
test_valid_config()
test_missing_field()
test_invalid_json()
test_file_not_found()
test_invalid_field_type()
test_zero_value()
test_bool_value()
print("\nAll tests passed.")


if __name__ == "__main__":
main()
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

These Python tests are not picked up by the repository's default make test (which currently runs go test ./... only). If these are meant to run in CI, consider integrating them into the test target/CI workflow (or converting to a Go test that shells out to the tool), otherwise the PR's "Tests included" claim may be misleading.

Copilot uses AI. Check for mistakes.
"capabilities": {
"$ref": "#/$defs/ModelCapabilities"
},
"architecture_config": {
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The new property name architecture_config introduces snake_case into the schema, while other ModelConfig fields use lowerCamelCase (e.g., paramSize, docURL, diffIds). Consider renaming this to architectureConfig (and updating the Go struct tags/tests/tools accordingly) to keep JSON field naming consistent.

Suggested change
"architecture_config": {
"architectureConfig": {

Copilot uses AI. Check for mistakes.
Capabilities *ModelCapabilities `json:"capabilities,omitempty"`

// Architecture-specific configuration parameters
ArchitectureConfig *ArchitectureConfig `json:"architecture_config,omitempty"`
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

json:"architecture_config,omitempty" is inconsistent with the existing JSON naming in this file (e.g., paramSize, docURL, diffIds). If possible, align the JSON tag with the prevailing lowerCamelCase convention (e.g., architectureConfig) and update the schema/tests accordingly.

Suggested change
ArchitectureConfig *ArchitectureConfig `json:"architecture_config,omitempty"`
ArchitectureConfig *ArchitectureConfig `json:"architectureConfig,omitempty"`

Copilot uses AI. Check for mistakes.
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.

2 participants