Loading...
Loading...
Use this skill when converting custom PyTorch models to Hugging Face Transformers format. Helps with: (1) Creating PretrainedConfig and PreTrainedModel classes, (2) Writing ImageProcessor/Tokenizer, (3) Compatibility testing, (4) Hub upload preparation. Use when the user wants to make their model compatible with transformers library.
npx skill4agent add shunk031/skills shunk031-transformers-convertfrom transformers import PretrainedConfig
from typing import List, Optional
class {ModelName}Config(PretrainedConfig):
model_type = "{model_name}"
def __init__(
self,
# Core architecture parameters
hidden_dim: int = 128,
num_layers: int = 4,
# Input/output parameters
image_size: int = 1024,
num_channels: int = 3,
num_labels: int = 1,
# Component-specific parameters (extract from original)
component_param: List[int] | None = None,
**kwargs,
):
super().__init__(**kwargs)
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.image_size = image_size
self.num_channels = num_channels
self.num_labels = num_labels
# Use default if not specified
self.component_param = (
component_param if component_param is not None else [1, 2, 4]
)from transformers import PreTrainedModel
from transformers.modeling_outputs import SemanticSegmenterOutput
class {ModelName}ForTask(PreTrainedModel):
config_class = {ModelName}Config
def __init__(self, config: {ModelName}Config):
super().__init__(config)
self.config = config
# Initialize layers using config parameters (no hardcoded values!)
self.encoder = Encoder(
hidden_dim=config.hidden_dim,
num_layers=config.num_layers,
)
def forward(
self,
pixel_values: torch.FloatTensor,
labels: Optional[torch.LongTensor] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple, SemanticSegmenterOutput]:
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
# Forward pass
logits = self.encoder(pixel_values)
# Calculate loss if needed
loss = None
if labels is not None:
# Compute loss
pass
if not return_dict:
output = (logits,)
return ((loss,) + output) if loss is not None else output
return SemanticSegmenterOutput(
loss=loss,
logits=logits,
hidden_states=None,
attentions=None,
)from transformers import BaseImageProcessor
class {ModelName}ImageProcessor(BaseImageProcessor):
model_input_names = ["pixel_values"]
def __init__(
self,
size: int = 1024,
resample: str = "bilinear",
do_normalize: bool = True,
image_mean: List[float] | None = None,
image_std: List[float] | None = None,
**kwargs,
):
super().__init__(**kwargs)
self.size = size
self.resample = resample
self.do_normalize = do_normalize
self.image_mean = image_mean if image_mean is not None else [0.485, 0.456, 0.406]
self.image_std = image_std if image_std is not None else [0.229, 0.224, 0.225]
def preprocess(
self,
images: ImageInput,
return_tensors: Optional[Union[str, TensorType]] = None,
**kwargs,
) -> BatchFeature:
# Implement preprocessing matching original
# Return BatchFeature with pixel_valuesimport pytest
import torch
def test_preprocessing_matches():
"""Test that preprocessing is equivalent."""
old_tensor = old_preprocessing(image)
new_tensor = processor(image, return_tensors="pt")["pixel_values"][0]
assert torch.allclose(old_tensor, new_tensor, atol=1e-6)
def test_single_image_output_matches():
"""Test that model outputs match."""
# Load models
old_model = OldModel()
new_model = NewModel(NewConfig())
new_model.load_state_dict(old_model.state_dict())
# Prepare SAME input
preprocessed = preprocess(image)
with torch.no_grad():
old_output = old_model(preprocessed)
new_output = new_model(pixel_values=preprocessed)
# Use 0.5% tolerance for numerical differences
assert torch.allclose(old_output, new_output.logits, atol=5e-3, rtol=1e-2)
def test_batch_output_matches():
"""Test batch processing."""
# Test with batch of images
def test_state_dict_compatible():
"""Test that weights can be loaded."""
new_model.load_state_dict(old_model.state_dict())register_for_auto_class()#!/usr/bin/env python3
from huggingface_hub import HfApi
from {module}.transformers import {ModelName}Config, {ModelName}ForTask, {ModelName}ImageProcessor
def main():
# CRITICAL: Register for Auto* support
{ModelName}Config.register_for_auto_class()
{ModelName}ForTask.register_for_auto_class("AutoModel")
{ModelName}ImageProcessor.register_for_auto_class("AutoImageProcessor")
# Load original model
original_model = OriginalModel()
# Create transformers-compatible model
config = {ModelName}Config()
model = {ModelName}ForTask(config)
model.load_state_dict(original_model.state_dict())
# Save and push
model.save_pretrained(local_dir)
config.save_pretrained(local_dir)
processor = {ModelName}ImageProcessor()
processor.save_pretrained(local_dir)
api = HfApi(token=token)
api.create_repo(repo_id=repo_id, exist_ok=True)
api.upload_folder(repo_id=repo_id, folder_path=local_dir){project}/
├── {module}/
│ ├── original_model.py # Existing
│ └── transformers/ # NEW - for validation
│ ├── __init__.py
│ ├── configuration_{model}.py
│ ├── modeling_{model}.py
│ └── processing_{model}.py
└── tests/
└── test_transformers_compatibility.py{project}/
└── {module}/
├── configuration_{model}.py # Replaces original
├── modeling_{model}.py
└── processing_{model}.pyself.devicepost_process_semantic_segmentation()(width, height)