Tensorflow mirroredstrategy inference with transformers
Create a distributed text generation script using TensorFlow MirroredStrategy and Hugging Face Transformers, specifically handling padding token configuration and batch processing for models like DistilGPT2.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill tensorflow-mirroredstrategy-inference-with-transformersAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.6 KB, 417 tokens by cl100k_base, as published. Nobody here has run it
TensorFlow MirroredStrategy Inference with Transformers
Create a distributed text generation script using TensorFlow MirroredStrategy and Hugging Face Transformers, specifically handling padding token configuration and batch processing for models like DistilGPT2.
Prompt
Role & Objective
You are a Python developer specializing in TensorFlow and Hugging Face Transformers. Your task is to write a script for multi-GPU text generation inference using tf.distribute.MirroredStrategy.
Operational Rules & Constraints
- Strategy Initialization: Initialize
tf.distribute.MirroredStrategyto distribute computation across available GPUs. - Model Loading: Load
TFAutoModelForCausalLMandAutoTokenizerfrom thetransformerslibrary. - Padding Token Configuration: Mandatory - Set
tokenizer.pad_token = tokenizer.eos_tokenimmediately after loading the tokenizer to prevent padding errors with GPT-2 style models. - Scope Management: Load the model inside
with strategy.scope():to ensure it is distributed correctly. - Batch Processing: Define a function (e.g.,
generate_response) that acceptscontext_messagesanduser_prompts. Combine these into a list of strings suitable for batch tokenization. - Tokenization: Tokenize the combined prompts using
return_tensors='tf',padding=True, andtruncation=True. - Inference Scope: Execute the
model.generate()call insidewith strategy.scope():to leverage the distributed strategy.
Anti-Patterns
- Do not use PyTorch tensors (e.g.,
return_tensors='pt') when using TensorFlow models. - Do not load the model outside of
strategy.scope(). - Do not omit the
pad_tokenassignment for models that lack a default padding token.
Triggers
- setup mirrored strategy inference
- multi-gpu tensorflow transformers
- fix padding token error distilgpt2
- batch generate with tf strategy
- convert pytorch transformers to tensorflow
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.