Comments (4)
Hey @moshonkel,
what you described on what you want to sounds like a perfect use case for SageMaker Batch Transform, where you can provide files from s3 which are then used to run predictions.
I created a video and sample notebook on how to do batch transform: https://www.youtube.com/watch?v=lnTixz0tUBg&ab_channel=HuggingFace
To be able to use it you might need to create a custom inference.py
with your custom logic.
Notebook: https://github.com/huggingface/notebooks/blob/master/sagemaker/12_batch_transform_inference/sagemaker-notebook.ipynb
from sagemaker-huggingface-inference-toolkit.
This is the solution i came up with. You can just load data from s3 in the inference.py . For my case this is the most practical solution.
import boto3
import io
s3 = boto3.client('s3')
obj = s3.get_object(Bucket='custommodels', Key='sentences.csv')
df = pd.read_csv(io.BytesIO(obj['Body'].read()))
sentences = pd.read_csv('s3://custommodels/sentences.csv', index_col=0)
Still, thanks for the help @philschmid
from sagemaker-huggingface-inference-toolkit.
Hey,
thanks for your answer. I do not understand how batch transform is helpful in this case.
I have an endpoint with a custom inference.py
script, this is the inference.py:
import torch
import torch.nn.functional as F
import pandas as pd
import numpy as np
corpus_embeddings = np.load('s3://custommodels/sentence_embeddings.npy')
data = pd.read_csv('s3://custommodels/sentences.csv', index_col=0)
def similarity(embeddings_1, embeddings_2):
normalized_embeddings_1 = F.normalize(embeddings_1, p=2)
normalized_embeddings_2 = F.normalize(embeddings_2, p=2)
return torch.matmul(
normalized_embeddings_1, normalized_embeddings_2.transpose(0, 1)
)
def output_fn(prediction, accept):
prediction = torch.tensor(prediction)
attention_mask = torch.ones(prediction.shape[1], dtype=torch.uint8)
mask = attention_mask.unsqueeze(-1).expand(prediction.size()).float()
masked_embeddings = prediction * mask
summed = torch.sum(masked_embeddings, 1)
summed_mask = torch.clamp(mask.sum(1), min=1e-9)
query_embedding = (summed / summed_mask)
scores = similarity(torch.tensor(corpus_embeddings, dtype=torch.float32), torch.tensor(query_embedding, dtype=torch.float32))
maximum_score = torch.max(scores, 0)
result = data.iloc[int(maximum_score[1])].to_dict()
result['Ähnlichkeit'] = float(maximum_score[0][0])
return result
The endpoint with the huggingfacemodel and the l2 regularization work fine the way i wrote it. Meaning i can get a correct sentence embedding from the endpoint.
What i am trying to do now is to compare the embedding from the inference of the endpoint, with embeddings, that are stored or deployed with the model
corpus_embeddings = np.load('s3://custommodels/sentence_embeddings.npy')
This does not work, and i dont understand how my inference.py can access data from anywhere (no matter if its on s3, or also deployed?, or anywhere else).
When i am in a sagemaker notebook, i can load files from s3 into ram. How is this done with an endpoint?
Ps the files i need are less than 10mb.
from sagemaker-huggingface-inference-toolkit.
Hey @moshonkel !
The solution doesn't seem to be efficient. Here is why:
Every time when you call inference.py, you read the csv file from S3 bucket. It might be okay when the file is very small, but what will you do if the file is too large?
I am also facing similar problem right now and looking for the most efficient solution. Batch transform is also not something I would like to have as a proper solution.
Are there other ways to store the embeddings in the memory? Like we do with reading the model.
UPDATE: I do know there is a possibility to use some database, but currently want to avoid it.
from sagemaker-huggingface-inference-toolkit.
Related Issues (20)
- Using custom inference script and models from Hub HOT 1
- get_pipeline function passes Path object rather than PretrainedTokenizer
- No support for multi-GPU HOT 2
- 🏷️ invalid
- Sagemaker endpoint inferencing error with HF model loading from s3bucket with new transformer update HOT 5
- Support multiple return sequences
- HF_TASK Enviournment Variable error HOT 1
- Endpoint creation completes before custom model_fn finishes loading resources
- ARCHITECTURES_2_TASK is limiting the tasks able to be deployed with HF DLC HOT 11
- Make DEFAULT_HF_HUB_MODEL_EXPORT_DIRECTORY configurable
- InternalServerException at runtime HOT 3
- trust_remote_code=True in new Hugging Face LLM Inference Container for Amazon SageMaker HOT 2
- How to access CustomAttributes in async inferece request input_fn HOT 1
- [DOCS] List of available HF_TASK and default inference scripts HOT 4
- Dead Link for Available HF_Tasks HOT 1
- SageMaker deployment errors HOT 2
- Error on Sagemaker deployment for v1.0.1 HOT 1
- How can I delpoy a model with AWS S3 and without downloading model from hunggingface via TGI image on Sagemaker? HOT 2
- How to enable Batch inference on AWS deployed Serverless model from Hub? HOT 1
- Where is the logic for detecting custom inference.py? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sagemaker-huggingface-inference-toolkit.