Huggingface hub mixin for custom torch model
Skill kjuhwa/skills-hub/skills/ml-ops/huggingface-hub-mixin-for-custom-torch-model
Give a custom nn.Module save_pretrained/from_pretrained plus HF Hub push/pull by multiple-inheriting from PyTorchModelHubMixin, with no HuggingFace config boilerplate.From its SKILL.md
npx -y skills add kjuhwa/skills-hub --skill huggingface-hub-mixin-for-custom-torch-modelAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
3.3 KB, 692 tokens by cl100k_base, as published. Nobody here has run it
Make any nn.Module hub-publishable with PyTorchModelHubMixin
When to use
- You ship a custom PyTorch model (no tokenizers, no AutoModel) and still want
from_pretrained("user/name")/save_pretrained(path)/push_to_hub. - You don't want to inherit from
transformers.PreTrainedModel(which brings config classes,CausalLM-style heads, etc.). - You want config-free serialization: constructor kwargs are stored in
config.jsonautomatically and re-passed on load.
Pattern
nn.Module + PyTorchModelHubMixin via multiple inheritance. The mixin intercepts save_pretrained to write model.safetensors + config.json built from the __init__ signature, and from_pretrained reads that config back and calls __init__ before loading weights.
# model/kronos.py
from huggingface_hub import PyTorchModelHubMixin
class KronosTokenizer(nn.Module, PyTorchModelHubMixin):
def __init__(self, d_in, d_model, n_heads, ff_dim,
n_enc_layers, n_dec_layers, ...,
s1_bits, s2_bits, beta, gamma0, gamma, zeta, group_size):
super().__init__()
...
class Kronos(nn.Module, PyTorchModelHubMixin):
def __init__(self, s1_bits, s2_bits, n_layers, d_model, n_heads, ff_dim, ...):
super().__init__()
...
# use anywhere
tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base")
model = Kronos.from_pretrained("NeoQuasar/Kronos-small")
# and when fine-tuning
model.module.save_pretrained(f"{save_dir}/checkpoints/best_model")
For deterministic regression tests you can pin a commit hash:
# tests/test_kronos_regression.py
tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base",
revision="0e0117387f39004a9016484a186a908917e22426")
Why it works / tradeoffs
The mixin inspects __init__ kwargs via inspect.signature and serializes them, so every ctor arg must be JSON-serializable (ints, floats, strings, lists of same). You get Hub push/pull for free including model card. Tradeoffs: no lazy sharding like from_pretrained(..., device_map="auto"); if you need that, wrap in transformers.PreTrainedModel instead. Also, renaming a ctor kwarg breaks backward compatibility with existing checkpoints — treat the __init__ signature as a public API.
References
model/kronos.pyin Kronos —KronosTokenizer(nn.Module, PyTorchModelHubMixin)andKronos(nn.Module, PyTorchModelHubMixin)- Hugging Face docs: https://huggingface.co/docs/huggingface_hub/guides/integrations#pytorch
tests/test_kronos_regression.py— pinningrevision=for deterministic tests
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.