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

Generic wrapper for Huggingface transformers #206

Open
wiseodd opened this issue Jul 7, 2024 · 0 comments
Open

Generic wrapper for Huggingface transformers #206

wiseodd opened this issue Jul 7, 2024 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@wiseodd
Copy link
Collaborator

wiseodd commented Jul 7, 2024

Currently, we require the user to do this themselves:

class MyGPT2(nn.Module):
    def __init__(self, tokenizer: PreTrainedTokenizer) -> None:
        super().__init__()
        config = GPT2Config.from_pretrained("gpt2")
        config.pad_token_id = tokenizer.pad_token_id
        config.num_labels = 2
        self.hf_model = GPT2ForSequenceClassification.from_pretrained(
            "gpt2", config=config
        )

    def forward(self, data: MutableMapping) -> torch.Tensor:
        device = next(self.parameters()).device
        input_ids = data["input_ids"].to(device)
        attn_mask = data["attention_mask"].to(device)
        output_dict = self.hf_model(input_ids=input_ids, attention_mask=attn_mask)
        return output_dict.logits

Can we provide a generic wrapper? I suspect the use case has very little variance. Maybe something like:

def huggingface_wrapper(hf_model):
    class WrappedModel(nn.Module):
        def __init__(self, hf_model: PretrainedModel):
            self.hf_model = hf_model

        def forward(self, data):
            output_dict = self.hf_model(**data)
            return output_dict.logits

    return WrappedModel(hf_model)
@wiseodd wiseodd added the enhancement New feature or request label Jul 7, 2024
@wiseodd wiseodd added this to the 0.3 milestone Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant