r/FastAPI • u/Metro_nome69 • Nov 09 '24
Hosting and deployment cross_encoder/Marco_miniLM_v12 takes 0.1 seconds in local and 2 seconds in server
Hi,
I've recently developed a Reranker API using fast API, which reranks a list of documents based on a given query. I've used the ms-marco-MiniLM-L12-v2 model (~140 MB) which gives pretty decent results. Now, here is the problem:
- This re-ranker API's response time in my local system is ~0.4-0.5 seconds on average for 10 documents with 250 words per document. My local system has 8 Cores and 8 GB RAM (pretty basic laptop)
- However, in the production environment with 6 Kubernetes pods (72 cores and a CPU Limit of 12 cores each, 4 GB per CPU), this response time shoots up to ~6-7 seconds on the same input.
I've converted an ONNX version of the model and have loaded it on startup. For each document, query pair, the scores are computed parallel using multithreading (6 workers). There is no memory leakage or anything whatsoever. I'll also attach the multithreading code with this.
I tried so many different things, but nothing seems to work in production. I would really appreciate some help here. PFA, the code snippet for multithreading attached,
def __parallelizer_using_multithreading(functions_with_args:list[tuple[Callable, tuple[Any]]], num_workers):
"""Parallelizes a list of functions"""
results = []
with ThreadPoolExecutor(max_workers = num_workers) as executor:
futures = {executor.submit(feature, *args) for feature, args in functions_with_args}
for future in as_completed(futures):
results.append(future.result())
return results
Thank you
3
u/Remarkable_Two7776 Nov 09 '24
Never done this before but some things to try or think about:
await anyio.run_in_threadpoolwhich may let you use fastapis existing threadpool? Fastapi uses this default threadpool the evaluate sync routes and dependencies2
u/British_Artist Nov 10 '24
Try adding a log at the start and end of your function with uuid related to the task in the output. It should at least narrow down whether you're waiting on CPU or something else. Otherwise, I would look at profilers like cprofile or py-spy as was suggest by others to see how long certain code is taking to execute. The goal being removing any shread of doubt that it's related to the algorithm in your code and start focusing up the stack to webserver/network performance.
1
u/snowyoz Nov 09 '24
Have you tried running any kind of profiling in prod? Do you have any kind of observability tools installed?
Is that the only workload in your cluster?
It’s hard to tell but parallel running it might only be on one node you just have to check how the workload is distributed in k8s
1
u/Vishnyak Nov 09 '24
Do you run it as image locally? I think that may be a thread limitation when running in docker environment