serving icon indicating copy to clipboard operation
serving copied to clipboard

Sharp increase in memory usage -> server is killed

Open echan00 opened this issue 7 years ago • 56 comments

Every so often I have found my tensorflow server killing itself due to memory usage hitting capacity. I can't quite figure out how or why this happening because it only happens occasionally and my process to use the server to infer files sequentially is identical between multiple servers and runtimes.

My guess is the issue is related to the files itself (the only non-constant), has anybody run into similar problems? For the sake of context, I am using my tf server to translate text in these files.

echan00 avatar Dec 15 '18 09:12 echan00

How much memory does tf serving use? how many models do you load to tf serving? How much memory does your machine have?

Arnold1 avatar Dec 18 '18 17:12 Arnold1

If you have a bug, please fill out the template. Otherwise, please go to Stack Overflow for help and support:

https://stackoverflow.com/questions/tagged/tensorflow-serving

If you open a GitHub issue, it must be a bug, a feature request, or a significant problem with documentation (for small docs fixes please send a PR instead).

Thanks!

gautamvasudevan avatar Dec 18 '18 19:12 gautamvasudevan

System information

  • Ubuntu 16.04
  • TensorFlow Serving compiled from binary
  • TensorFlow Serving version 1.12

Describe the problem

When sending my translation model (Chinese to English) gibberish text for inference, memory increases sharply and TF serving is killed

Example gibberish text: https://gist.github.com/echan00/acde1d7e460cd9e467dfd612ea14ab66

@Arnold1 I only loaded one model. My machine has 16GB of memory, so memory consumption prior to feeding it model gibberish isn't the problem. The model itself takes up 500MB+

echan00 avatar Dec 18 '18 23:12 echan00

@gautamvasudevan can you reopen the bug now?

echan00 avatar Dec 18 '18 23:12 echan00

Unfortunately this isn't sufficient for us to know what the issue is or reproduce it.

gautamvasudevan avatar Dec 18 '18 23:12 gautamvasudevan

Hi @gautamvasudevan, @echan00

I've also noticed a memory leak problem on server side thats loading an inception model (similar to object detection).

The memory leak problem on inception model can be traced back to the client making synchronous vs asynchronous gRPC requests to the model server (i.e. parallel vs sequential prediction requests to inception graph hosted on tensorflow serving).

Async requests (using stub.Predict.future(request, 30.0)) to model server: image

Sync request (stub.Predict(request, 30.0)) to model server: image

Sync requests grouped/distributed among threads from a threadpool (of size 8): image

This issue has been reported sparsely in github issues and some SO posts, but this is a big concern as a common inception client example (like mnist) with async requests will lead to server memory leak. IMO this issue should be re-opened for further discussion.

@gautamvasudevan let me know if you need more information/data, i've managed to reproduce this issue using faster_rcnn_inception_resnet_v2_atrous_oid from model zoo (https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md). I can provide sample scripts and more examples.

masroorhasan avatar Dec 19 '18 07:12 masroorhasan

@masroorhasan That's very helpful! Thank you!

If you have a sample script/instructions that easily reproduce it, that would be amazing and help us narrow down what's going on.

gautamvasudevan avatar Dec 19 '18 18:12 gautamvasudevan

Closing this at it is in "awaiting response" status for more than a week. Feel free to add comments(if any), we will reopen the issue. Thanks !

Harshini-Gadige avatar Feb 11 '19 18:02 Harshini-Gadige

Hi @masroorhasan I would like to ask which tool you are using to measure the memory usage and GPU usage, as shown in your three screenshots. Thanks!

sugartom avatar Mar 14 '19 21:03 sugartom

Hi @gautamvasudevan,

I have also observed a memory leak on the server side. I am making concurrent requests to the model server for prediction. The model used is a simple mnist model, and I have also observed the issue with other models I use.

The client prediction code runs in a server, which makes gRPC calls to Tensorflow Serving for prediction. Client code:

import os
import json
from flask import Flask, Response, request
from timeit import default_timer as timer

import grpc
import numpy as np

import tensorflow as tf
from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc

app = Flask(__name__)


@app.route("/")
def predict():

    model = request.args.get('model', default="model_1", type=str)
    batch_size = request.args.get('batch_size', default=10, type=int)

    channel = grpc.insecure_channel("localhost:9640", options=[('grpc.max_send_message_length', 200 * 1024 * 1024), (
        'grpc.max_receive_message_length', 200 * 1024 * 1024)])
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    start_pred = timer()
    pred_request = predict_pb2.PredictRequest()
    pred_request.model_spec.name = model
    pred_request.model_spec.signature_name = "serving_default"

    pred_request.inputs['pixels'].CopyFrom(tf.make_tensor_proto(
        np.random.rand(batch_size, 28, 28, 1), shape=[batch_size, 28, 28, 1], dtype=tf.float32))

    result = stub.Predict(pred_request, 600)
    end_pred = timer()

    channel.close()

    print("Completed pred for images(s) in {:0.2f}s".format(
        end_pred - start_pred))

    return "success"

The server started up using this code is then hit with multiple concurrent requests repeatedly. I am sending it 20 concurrent requests containing 1000 images (random numpy data here) in each.

Over time, I observe a memory build up in the server. Tensorflow Serving is running in a Docker container with the latest release(tensorflow/serving:1.13.0). Only 1 model is loaded at startup using the models.config file.

Memory stats of the Docker container (tracked using cadvisor and Prometheus): Capture

The base memory consumption of the server seems to gradually grow over time. This is an issue for us as we plan to deploy Tensorflow Serving in a production scenario and currently the server crashes after a while (2-3 days) due to the memory build up. The primary cause for that is the sharp peaks seen in the graph growing higher and higher (probably due to increase in base memory usage), leading to Out of Memory errors.

The best way to reproduce this issue at the moment is to send concurrent requests to the model server using any simple client code, repeatedly. Over time the memory build up becomes more visible. From my observations, building a binary from the devel image makes the memory leak more pronounced.

Please reopen this issue and let me know if you need any more details.

prabalakanti avatar Mar 18 '19 06:03 prabalakanti

I am facing the same issue. Any updates on this ?

golisandeep3 avatar Apr 13 '19 00:04 golisandeep3

Hi golisandeep3,

Unfortunately, without detailed information of the problem you are facing, I wouldn't be able to know or reproduce the issue.

Hi prabalakanti,

Sorry for the delayed reply. I have a few questions to follow up

  • Would it be possible to share which model you used to serve?
  • How big is each of your images? If are you sending 1000 big images concurrently, I would expect the memory go up due to the large input size.

nrobeR avatar Apr 16 '19 05:04 nrobeR

  • Would it be possible to share which model you used to serve?

Yes, the model is attached. It's a simple MNIST model. model.zip

  • How big is each of your images? If are you sending 1000 big images concurrently, I would expect the memory go up due to the large input size.

The "images" are simulated using random numpy arrays in the client code (attached in the earlier comment). I understand that if I am sending a lot of big images concurrently the memory usage will be high, it is expected. But over time, the average/peak memory usage goes up. This should not be happening in my opinion because the request sizes are the same over time.

The run goes something like this: send a bunch of large concurrent requests, wait for Tensorflow Serving to predict and send the response, then it repeats again.

Another observation: When I do the same run but with a single large request at a time, there's no memory build up over time. Everything runs smoothly and as expected.

prabalakanti avatar Apr 22 '19 04:04 prabalakanti

@nrobeR Also the memory usage goes up when we reload the model configuration. Like deleting and adding new model versions.

golisandeep3 avatar Apr 26 '19 22:04 golisandeep3

@golisandeep3 When we reload the model configuration, the new version gets loaded first followed by the unload of previous version. That might be causing the high memory usage.

prabalakanti avatar Apr 30 '19 05:04 prabalakanti

@prabalakanti The memory keeps growing ,it doesn't release the memory when it unloads the previous version.

golisandeep3 avatar Apr 30 '19 06:04 golisandeep3

@prabalakanti The memory keeps growing ,it doesn't release the memory when it unloads the previous version. I am facing the same issue, when the new version is added to the model path, the memory usage increases, and the memory won't be released even the old version model is deleted.

carter54 avatar May 14 '19 01:05 carter54

  • Would it be possible to share which model you used to serve?

Yes, the model is attached. It's a simple MNIST model. model.zip

  • How big is each of your images? If are you sending 1000 big images concurrently, I would expect the memory go up due to the large input size.

The "images" are simulated using random numpy arrays in the client code (attached in the earlier comment). I understand that if I am sending a lot of big images concurrently the memory usage will be high, it is expected. But over time, the average/peak memory usage goes up. This should not be happening in my opinion because the request sizes are the same over time.

The run goes something like this: send a bunch of large concurrent requests, wait for Tensorflow Serving to predict and send the response, then it repeats again.

Another observation: When I do the same run but with a single large request at a time, there's no memory build up over time. Everything runs smoothly and as expected.

Thanks. I tried reproducing this problem with latest tensorflow_serving running in docker using the mnist model and cannot see RAM usage increase after load testing on it for a day now.

For your case, do you have any stack trace that we can look into? Also what is the cpu usage on your model server? I wonder if it's because the requests are queued up in the server gradually because some resource issues that bound the throughput.

nrobeR avatar May 17 '19 22:05 nrobeR

@prabalakanti The memory keeps growing ,it doesn't release the memory when it unloads the previous version.

Unfortunately, without more details and concrete steps of how to reproduce this, I won't be able to debug on this further. I tried loading new version of a model and didn't see RAM increase issues as you described.

nrobeR avatar May 17 '19 22:05 nrobeR

For your case, do you have any stack trace that we can look into? Also what is the cpu usage on your model server? I wonder if it's because the requests are queued up in the server gradually because some resource issues that bound the throughput.

I don't have a stack trace for it. The CPU usage hits the peak of 100%. I don't think queuing up of requests should be an issue, because I am sending the next batch of requests only after the first batch has finished processing.

Also, a question about the load testing, did you run it with concurrent requests?

This memory build up is worrying for me if in a production scenario (where multiple users will be sending requests) over a period of time the build up becomes significant, the server crashes as observed in load and endurance testing.

prabalakanti avatar May 21 '19 11:05 prabalakanti