philschmid HF staff commited on
Commit
bc5086d
1 Parent(s): c9971ce

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +20 -11
README.md CHANGED
@@ -2510,20 +2510,29 @@ _coming soon_
2510
 
2511
  ```python
2512
 
2513
- from optimum.neuron import pipeline
 
 
 
2514
 
2515
- # Load pipeline from Hugging Face repository
2516
- pipe = pipeline("text-generation", "aws-neuron/bge-base-en-v1-5-seqlen-384-bs-1")
 
2517
 
2518
- # We use the tokenizer's chat template to format each message - see https://huggingface.co/docs/transformers/main/en/chat_templating
2519
- messages = [
2520
- {"role": "user", "content": "What is 2+2?"},
2521
- ]
2522
- prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
2523
- # Run generation
2524
- outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
2525
- print(outputs[0]["generated_text"])
2526
 
 
 
 
 
 
 
 
 
 
 
 
2527
  ```
2528
 
2529
  **input_shapes**
 
2510
 
2511
  ```python
2512
 
2513
+ from optimum.neuron import NeuronModelForFeatureExtraction
2514
+ from transformers import AutoTokenizer
2515
+ import torch
2516
+ import torch_neuronx
2517
 
2518
+ # Load Model from Hugging Face repository
2519
+ model = NeuronModelForFeatureExtraction.from_pretrained("aws-neuron/bge-base-en-v1-5-seqlen-384-bs-1")
2520
+ tokenizer = AutoTokenizer.from_pretrained("aws-neuron/bge-base-en-v1-5-seqlen-384-bs-1")
2521
 
2522
+ # sentence input
2523
+ inputs = "Hello, my dog is cute"
 
 
 
 
 
 
2524
 
2525
+ # Tokenize sentences
2526
+ encoded_input = tokenizer(inputs,return_tensors="pt",truncation=True,max_length=model.config.neuron["static_sequence_length"])
2527
+
2528
+ # Compute embeddings
2529
+ with torch.no_grad():
2530
+ model_output = model(*tuple(encoded_input.values()))
2531
+
2532
+ # Perform pooling. In this case, cls pooling.
2533
+ sentence_embeddings = model_output[0][:, 0]
2534
+ # normalize embeddings
2535
+ sentence_embeddings = torch.nn.functional.normalize(sentence_embeddings, p=2, dim=1)
2536
  ```
2537
 
2538
  **input_shapes**