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

[generate] Run custom generation code from the Hub #36405

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gante
Copy link
Member

@gante gante commented Feb 25, 2025

What does this PR do?

👉 This is a draft PR for feature discussion 👈

This PR enables the definition of custom decoding methods on the hub, sharing the same interface as generate. In a nutshell, if trust_remote_code=True and recipe="user/repo_name" are passed to generate, the arguments to generate are forwarded to a custom generate function, defined in generate.py in that repo.

Why does this matter

With the popularity of LLMs, LLM-derived models (like VLMs), and test-time compute techniques, there is an explosion of what we can do regarding decoding methods. Instead of having a complex process to include a certain technique in transformers, this would enable developers and researchers to build their own decoding methods and share them with the world with zero additional Python dependencies.

The Hub-hosted decoding techniques would also get all benefits from the Hub, including proper credit to the creator (downloads/likes), a place to discuss the method, ...

Working Example

from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct", device_map="auto")

inputs = tokenizer(["The quick brown"], return_tensors="pt").to(model.device)
gen_out = model.generate(**inputs, recipe="joaogante/test_generate_from_hub", trust_remote_code=True)
print(tokenizer.batch_decode(gen_out, skip_special_tokens=True))

(generate.py in joaogante/test_generate_from_hub)


TODO

Before this PR is ready to be merged, the following are missing:

  • A documentation page
  • Tests
  • A few actual examples on the hub

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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