import tensorflow as tf from transformers import TFAutoModelForCausalLM, AutoTokenizer…
- User
import tensorflow as tf from transformers import TFAutoModelForCausalLM, AutoTokenizer
Initialize MirroredStrategy
strategy = tf.distribute.MirroredStrategy( devices=["/gpu:0", "/gpu:1", "/gpu:2", "/gpu:3", "/gpu:4", "/gpu:5"], cross_device_ops=tf.distribute.HierarchicalCopyAllReduce() )
print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
Load model and tokenizer outside of strategy, tokenizer operations don't need to be distributed
model_name = "distilgpt2" tokenizer = AutoTokenizer.from_pretrained(model_name) tokenizer.pad_token = tokenizer.eos_token
with strategy.scope(): # Load model within strategy's scope to ensure it's distributed across the specified GPUs model = TFAutoModelForCausalLM.from_pretrained(model_name, pad_token_id=tokenizer.eos_token_id)
def generate_response(context_messages, user_prompts): with strategy.scope(): relevant_context = [msg['content'] for msg in context_messages[-3:]] # Limiting to the last 3 messages for context combined_prompts = [ context + " " + user_input['content'] for context in relevant_context for user_input in user_prompts ]
responses = [] # Placeholder for responses with strategy.scope(): # Ensure generation occurs within strategy context input_ids = tokenizer(combined_prompts, return_tensors='tf', padding=True, truncation=True, max_length=128) attention_mask = input_ids['attention_mask'] with strategy.scope(): output_ids = model.generate(input_ids=input_ids['input_ids'], attention_mask=attention_mask, max_length=128, do_sample=True, temperature=0.7, top_k=50, top_p=0.95,) responses = [ "Market Analysis: " + tokenizer.decode(out_id, skip_special_tokens=True) + " Trading Suggestion: ..." for out_id in output_ids ] return responsesInitial Context
messages = [ {"role": "system", "content": "You are a futures stock, forex, and cryptocurrency day trader."}, {"role": "system", "content": "The NASDAQ is up slightly today. I'm considering taking a position in Apple."} ]
user_inputs = [] # To collect user prompts
Main Interaction Loop
while True: with strategy.scope(): new_prompt = input("Enter your new prompt (or type 'go' to process): ") if new_prompt.lower() == 'go': if not user_inputs: print("No prompts entered. Exiting.") break
# Generate responses based on collected prompts and existing context responses = generate_response(messages, user_inputs) for response in responses: print("Trader Response:", response) # Extend the messages (context history) with the collected user inputs for next iteration messages.extend(user_inputs) user_inputs = [] # Clear user inputs for next batch of prompts else: user_inputs.append({"role": "user", "content": new_prompt}) fix this format output according to the documentation. - Assistant
- User
how can i pad left
- Assistant
- User
what other llms are compatible with tensorflow
- Assistant
- User
- Assistant
- User
from transformers import BertTokenizer, TFBertModel tokenizer = BertTokenizer.from_pretrained('bert-large-uncased') model = TFBertModel.from_pretrained("bert-large-uncased") text = "Replace me by any text you'd like." encoded_input = tokenizer(text, return_tensors='tf') output = model(encoded_input) modify the code for this instructions
- Assistant