serving icon indicating copy to clipboard operation
serving copied to clipboard

Gracefully handle many client calls ?

Open jrabary opened this issue 10 years ago • 14 comments

I'm trying to use Tensorflow serving with Apache Spark. I have one grpc async client per spark worker node. The pseudo code of my client call is as follow :

class MNISTTFServingClient private (channel: ManagedChannel, stub: MnistServiceGrpc.MnistServiceStub) {


  def shutdown(): Unit = {
    channel.shutdown.awaitTermination(30, TimeUnit.SECONDS)
  }


  def classify(images: Iterator[Array[Float]]): Iterator[Seq[Float]] = {
    val requests = images.map(i => MnistRequest(imageData = i.toSeq))
    val futureResponse = Future.traverse(requests)(stub.classify)
    val response = Await.result(futureResponse, 30 seconds)

    response.map(_.value)
  }

}


val classProbabilities = images.mapPartitions { imgIt =>
      val client = workerStore.get[MNISTTFServingClient]("client")
      client.classify(imgIt)
 }

It take a whole spark data partitions (the number of element withing each partion may be quite large) and set them as MNistRequest to Tensorflow serving. Inside each worker, we may also have several threads that send request to the server. It works when the data size is small. But when I run it on a whole mnist test set I got the following error :

io.grpc.StatusRuntimeException: UNAVAILABLE: This task would start a fresh batch, but all batch threads are busy, so at present there is no processing capacity available for this task

Any ideas to fix this ?

jrabary avatar Mar 04 '16 10:03 jrabary

Hi,

That error message means that the batch scheduler (which accumulates N inference tasks to be processed together as a batch) is "full" and cannot accept any more tasks until the ones currently being processed have finished. It means you have hit the maximum processing rate.

Some things you can try are:

  1. Be sure you are tuning things to maximize the processing rate. There are lots of possible bottlenecks that may or may not come into play for you, including ones in your model, and in the tensorflow::Session thread pool. Specifically in TensorFlow Serving's batch scheduler, there are several parameters in StreamingBatchScheduler::Options that you can play with: (A) Be sure you are using the largest batch size that works for your environment (max_batch_size), and a large timeout (for a bulk-processing job you don't need to worry about latency, presumably, so setting this to a large number should be good for getting large batches) (batch_timeout_micros). (B) Adjust the number of threads for processing batches of inference tasks (num_batch_threads). This will increase the parallelism; there will be a U-shaped curve for throughput.
  2. Slap a retrier (batching/batch_scheduler_retrier.h) on top of the StreamingBatchScheduler. It will retry the task submission in a loop, blocking the client thread until it is able to schedule the task. That will have the effect of slowing your job to match the rate at which the scheduler can get work done.
  3. (This may be redundant, depending on what kind of buffering you already have in your environment for I/O.) Add a queue on top of the scheduler, to smooth things out. (Sorry that you may have to do this yourself, for now; stay tuned for more batch-scheduler options that come with queues.)

Hope that helps.

-Chris

chrisolston avatar Mar 04 '16 15:03 chrisolston

@chrisolston Should we add a readme.md in tensorflow_serving/batching that documents this? (the possible bottlenecks, tuning considerations, etc.)

kirilg avatar Apr 04 '16 17:04 kirilg

Excellent idea. I have something like that in the works -- stay tuned.

On Mon, Apr 4, 2016 at 10:49 AM Kiril Gorovoy [email protected] wrote:

@chrisolston https://github.com/chrisolston Should we add a readme.md in tensorflow_serving/batching that documents this? (the possible bottlenecks, tuning considerations, etc.)

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/tensorflow/serving/issues/10#issuecomment-205413365

chrisolston avatar Apr 04 '16 18:04 chrisolston

Hypothetically, if we have numerous servers all running TF Serving, is there any way to quickly interpret the 'Unavailable' error client side and resubmit to a different server? I know it would be reasonable to check if the content of the response, but say the client handed the result future to some other function which does what it wants with it and adds its own callbacks.

Since the Unavailable error returns so quickly, it would be great if it was somehow possible to have the client:

(1) Submit the job to server i (2) Listen for an Unavailable Error during some time interval. (3) If one occurs, wait(x) then GOTO (1) with i + 1, else return result_future to invoking function.

That way, the invoking function can reasonably assume that any error that occurs is due to (for instance) something other than . In my case, the function that decides when to make sample & submit images and the callback structure make it difficult to reschedule requests once the result_future has been returned.

eriophora avatar Apr 28 '16 19:04 eriophora

We just released significantly expanded batching support, as well as some documentation.

Related to this thread:

  • batching/README.md describes the old+new batching classes, and offers some tuning tips (suggestions for revisions/additions welcome as always!)
  • if you use BasicBatchScheduler instead of StreamingBatchScheduler you get a queue with a configurable maximum length; if you are just trying to get all your requests processed and don't care about latency you can set the queue length to max-int (effectively infinity)

-Chris

chrisolston avatar Apr 29 '16 20:04 chrisolston

Great! Thanks Chris!!

eriophora avatar Apr 29 '16 20:04 eriophora

Hi @chrisolston

Is this an error in the documentation or am I missing something? The documentation of basic_batch_scheduler starting at line 156 states:

// The number of threads to use to process batches.
//
// This parameter also determines the maximum number of batches worth of
// tasks that can be enqueued: If all batch threads are busy, Schedule()
// will accept at most 'num_batch_threads' additional batches worth of tasks
// before rejecting tasks with an UNAVAILABLE error.
//
// Must be >= 1, and should be tuned carefully.
int num_batch_threads = 1;

// The maximum number of batches that can be enqueued (accepted by
// Schedule() but not yet being processed on a batch thread). See the
// class documentation above for guidelines on how to tune this parameter.
int max_enqueued_batches = 1;

I'm assuming this is the result of cut/pasting documentation over from a previous iteration--since shared_batch_scheduler seems to care about max_enqueued_batches when it makes decisions about rejecting a job rather than num_batch_threads...or is there something I'm missing?

eriophora avatar Apr 29 '16 22:04 eriophora

It is indeed a cut/paste error. Thanks for catching that! That is definitely confusing :)

Would you like to put in a PR to fix it? If you're not set up for that I can do it from my side.

chrisolston avatar Apr 29 '16 22:04 chrisolston

As you allude to, it should read identically to what's in shared_batch_scheduler.h, i.e.: // The number of threads to use to process batches. // Must be >= 1, and should be tuned carefully.

chrisolston avatar Apr 29 '16 22:04 chrisolston

(Never mind you are definitely set up for submitting a PR to us, as you did in issue 57. Thanks for pitching in!)

chrisolston avatar Apr 29 '16 22:04 chrisolston

Sure I'll fix it!

eriophora avatar Apr 29 '16 23:04 eriophora

@chrisolston I've issued the PR. However, was reviewing the functions to make sure the documentation is consistent before submitting the PR, and ran across this:

return errors::InvalidArgument("Task size ", (*task)->size(),
                                   " is larger than maximum batch size ",
                                   options_.max_batch_size);

This sort of threw me for a loop. How can it be the case that a batch consists of >=1 tasks yet 1 task can be larger than the size of a batch? I mean, Jupiter may be big, but it's still the same number of planets as Mercury.

eriophora avatar Apr 30 '16 00:04 eriophora

Disregard above. Tasks do not always have a size of 1.

eriophora avatar Apr 30 '16 02:04 eriophora

You got here before me :). For posterity, I'll extend your metaphor: in our system we allow each planet (task) to declare its size. Jupiter could declare that it's of size 100, and mercury could declare that its size is 1 (I'm pretty sure that's not to scale in the solar system but that's okay since we are computer scientists). If our max_batch_size is 1000 we could fit up to 10 jupiters or 1000 mercuries or some combination.

The use-case for this is that you might get an RPC that contains a set of requests, rather than just a single request. But the size of the per-RPC request set may still be smaller than the ideal batch size for throughput on e.g. your GPU -- so you still want to pack a bunch of them together into a larger batch.

chrisolston avatar Apr 30 '16 02:04 chrisolston