serve icon indicating copy to clipboard operation
serve copied to clipboard

Review / Document the GPU Assignement logic for multiple workers / models

Open dk19y opened this issue 5 years ago • 4 comments

The GPU id -> Worker / Model assignment behaviour is not consistent / reproducible.

This can be confirmed by registering multiple models / workers. It does not follow a round robin pattern.

dk19y avatar Jan 29 '21 18:01 dk19y

Also related to #947 / @HamidShojanazeri 's testing with multiple BERT models for 0.3 release.

dk19y avatar Jan 29 '21 18:01 dk19y

Possible code bug

  • GPUs for worker threads seem to be assigned in a round robin manner with an atomic integer gpuCounter . This counter is incremented once every time a worker is created (does not matter if they belong to the same model or not)

  • However the modulus performed on this universal gpuCounter is based on a per model config getNumberOfGpu().

Example

Initially gpuCounter = 0

Scale up model1 worker1 maxGpu(2) -> gpuCounter - 1 / m1w1 assigned to gpu - 1 Scale up model1 worker2 maxGpu(2) -> gpuCounter - 2 / m1w2 assigned to gpu - 2 Scale up model 2 worker1 maxGpu(3) -> gpuCounter - 3 / m2w1 assigned to gpu - 0 Scale up model1 worker3 maxGpu(2) -> gpuCounter - 4 / m1w3 assigned to gpu - 0

Code paths :

  • [WorkLoadManager - gpuCounter increment code path] (https://github.com/pytorch/serve/blob/de42bcf70c3be6249136afb6f442aa5ad0b39678/frontend/server/src/main/java/org/pytorch/serve/wlm/WorkLoadManager.java#L168)
  • [Config Manager - config] (https://github.com/pytorch/serve/blob/c9ccaec192eecac70b62005938a53a12101cb0ad/frontend/server/src/main/java/org/pytorch/serve/util/ConfigManager.java#L178)
  • [Management API - Number of GPU worker / model config] (https://github.com/pytorch/serve/blob/master/docs/management_api.md#scale-workers)

Next steps : Reproduce this with exact steps on TS code / define behaviour.

dk19y avatar Jan 29 '21 20:01 dk19y

  • Test case1: 2 models on 4 gpus, default_workers_per_model = 0 docker run --rm -it --gpus all -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 7070:7070 -p 7071:7071 -v ~/serve/benchmarks/model_store:/home/model-server/model-store pytorch/torchserve:latest-gpu torchserve --model-store /home/model-server/model-store --models all

curl http://localhost:8081/models/

{
  "models": [
    {
      "modelName": "resnet-18",
      "modelUrl": "resnet-18.mar"
    },
    {
      "modelName": "squeezenet1_1",
      "modelUrl": "squeezenet1_1.mar"
    }
  ]
}

curl http://localhost:8081/models/resnet-18

[
  {
    "modelName": "resnet-18",
    "modelVersion": "1.0",
    "modelUrl": "resnet-18.mar",
    "runtime": "python",
    "minWorkers": 4,
    "maxWorkers": 4,
    "batchSize": 1,
    "maxBatchDelay": 100,
    "loadedAtStartup": true,
    "workers": [
      {
        "id": "9000",
        "startTime": "2021-02-23T00:24:22.151Z",
        "status": "READY",
        "memoryUsage": 3489685504,
        "pid": 47,
        "gpu": true,
        "gpuUsage": "gpuId::1 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9001",
        "startTime": "2021-02-23T00:24:22.153Z",
        "status": "READY",
        "memoryUsage": 3502223360,
        "pid": 50,
        "gpu": true,
        "gpuUsage": "gpuId::2 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9002",
        "startTime": "2021-02-23T00:24:22.153Z",
        "status": "READY",
        "memoryUsage": 3489775616,
        "pid": 49,
        "gpu": true,
        "gpuUsage": "gpuId::3 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9003",
        "startTime": "2021-02-23T00:24:22.153Z",
        "status": "READY",
        "memoryUsage": 2761109504,
        "pid": 48,
        "gpu": true,
        "gpuUsage": "gpuId::0 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::10061 MiB"
      }
    ]
  }
]

curl http://localhost:8081/models/squeezenet1_1

[
  {
    "modelName": "squeezenet1_1",
    "modelVersion": "1.0",
    "modelUrl": "squeezenet1_1.mar",
    "runtime": "python",
    "minWorkers": 4,
    "maxWorkers": 4,
    "batchSize": 1,
    "maxBatchDelay": 100,
    "loadedAtStartup": true,
    "workers": [
      {
        "id": "9004",
        "startTime": "2021-02-23T00:24:22.262Z",
        "status": "READY",
        "memoryUsage": 3424026624,
        "pid": 73,
        "gpu": true,
        "gpuUsage": "gpuId::1 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9005",
        "startTime": "2021-02-23T00:24:22.263Z",
        "status": "READY",
        "memoryUsage": 3457138688,
        "pid": 71,
        "gpu": true,
        "gpuUsage": "gpuId::2 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9006",
        "startTime": "2021-02-23T00:24:22.263Z",
        "status": "READY",
        "memoryUsage": 3450916864,
        "pid": 70,
        "gpu": true,
        "gpuUsage": "gpuId::3 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9007",
        "startTime": "2021-02-23T00:24:22.263Z",
        "status": "READY",
        "memoryUsage": 2718560256,
        "pid": 72,
        "gpu": true,
        "gpuUsage": "gpuId::0 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::10061 MiB"
      }
    ]
  }
]
  • Test case2: 2 models on 2 gpus, default_workers_per_model = 0 docker run --rm -it --gpus '"device=1,3"' -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 7070:7070 -p 7071:7071 -v ~/serve/benchmarks/model_store:/home/model-server/model-store pytorch/torchserve:latest-gpu torchserve --model-store /home/model-server/model-store --models all

curl http://localhost:8081/models/

{
  "models": [
    {
      "modelName": "resnet-18",
      "modelUrl": "resnet-18.mar"
    },
    {
      "modelName": "squeezenet1_1",
      "modelUrl": "squeezenet1_1.mar"
    }
  ]
}

curl http://localhost:8081/models/resnet-18

[
  {
    "modelName": "resnet-18",
    "modelVersion": "1.0",
    "modelUrl": "resnet-18.mar",
    "runtime": "python",
    "minWorkers": 2,
    "maxWorkers": 2,
    "batchSize": 1,
    "maxBatchDelay": 100,
    "loadedAtStartup": true,
    "workers": [
      {
        "id": "9000",
        "startTime": "2021-02-23T01:05:54.724Z",
        "status": "READY",
        "memoryUsage": 3482550272,
        "pid": 46,
        "gpu": true,
        "gpuUsage": "gpuId::1 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9001",
        "startTime": "2021-02-23T01:05:54.726Z",
        "status": "READY",
        "memoryUsage": 2758467584,
        "pid": 45,
        "gpu": true,
        "gpuUsage": "gpuId::0 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::5031 MiB"
      }
    ]
  }
]

curl http://localhost:8081/models/squeezenet1_1

[
  {
    "modelName": "squeezenet1_1",
    "modelVersion": "1.0",
    "modelUrl": "squeezenet1_1.mar",
    "runtime": "python",
    "minWorkers": 2,
    "maxWorkers": 2,
    "batchSize": 1,
    "maxBatchDelay": 100,
    "loadedAtStartup": true,
    "workers": [
      {
        "id": "9002",
        "startTime": "2021-02-23T01:05:54.834Z",
        "status": "READY",
        "memoryUsage": 3427983360,
        "pid": 58,
        "gpu": true,
        "gpuUsage": "gpuId::1 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::2517 MiB"
      },
      {
        "id": "9003",
        "startTime": "2021-02-23T01:05:54.835Z",
        "status": "READY",
        "memoryUsage": 2717335552,
        "pid": 59,
        "gpu": true,
        "gpuUsage": "gpuId::0 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::5031 MiB"
      }
    ]
  }
]
  • Test case3: 2 models on 4gpus, default_workers_per_model=1 torchserve --model-store /home/model-server/model-store --models all

curl http://localhost:8081/models/

{
  "models": [
    {
      "modelName": "resnet-18",
      "modelUrl": "resnet-18.mar"
    },
    {
      "modelName": "squeezenet1_1",
      "modelUrl": "squeezenet1_1.mar"
    }
  ]
}

curl http://localhost:8081/models/resnet-18

[
  {
    "modelName": "resnet-18",
    "modelVersion": "1.0",
    "modelUrl": "resnet-18.mar",
    "runtime": "python",
    "minWorkers": 1,
    "maxWorkers": 1,
    "batchSize": 1,
    "maxBatchDelay": 100,
    "loadedAtStartup": true,
    "workers": [
      {
        "id": "9000",
        "startTime": "2021-02-23T01:52:14.129Z",
        "status": "READY",
        "memoryUsage": 144314368,
        "pid": 51,
        "gpu": true,
        "gpuUsage": "gpuId::1 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::1280 MiB"
      }
    ]
  }
]

curl http://localhost:8081/models/squeezenet1_1

[
  {
    "modelName": "squeezenet1_1",
    "modelVersion": "1.0",
    "modelUrl": "squeezenet1_1.mar",
    "runtime": "python",
    "minWorkers": 1,
    "maxWorkers": 1,
    "batchSize": 1,
    "maxBatchDelay": 100,
    "loadedAtStartup": true,
    "workers": [
      {
        "id": "9001",
        "startTime": "2021-02-23T01:52:14.239Z",
        "status": "READY",
        "memoryUsage": 144683008,
        "pid": 57,
        "gpu": true,
        "gpuUsage": "gpuId::2 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::1240 MiB"
      }
    ]
  }
]

lxning avatar Feb 23 '21 02:02 lxning

The above test results shows that GPU assignment is round robin among all models's workers. The benefit is that GPU usage is balanced as possible as it can. If GPU assignment is round robin among each model's workers, it requires that each model's starting gpu id is randomized.

In summary, round robin among all models's workers is a simple way to achieve GPU usage balance.

The only issue about GPU is the documentation which is addressed in #978

lxning avatar Mar 16 '21 21:03 lxning