1. 程式人生 > 其它 >我不會用 Triton 系列:Rate Limiter 的使用

我不會用 Triton 系列:Rate Limiter 的使用

Rate Limiter

這篇文章記錄 Rate Limter 的使用方法,主要來自於文件。

從效果上來說,Rate Limiter 的作用是限制了請求分發到模型例項上。從實現上來說,Rate Limiter 引入了 “Resource” 的概念,表示一個模型例項需要的資源,當系統中存在足夠的資源,這個模型就會執行。如果資源不夠,那麼一個請求需要等待其他模型例項釋放資源。最終的表現就是好像限制了速度一樣。

連結:https://github.com/triton-inference-server/server/blob/main/docs/rate_limiter.md

上手使用

概念

Resources

表示一個模型例項執行請求的時候需要的資源,資源分為兩種型別,全域性的 (global) 和某個裝置上的 (per-device)。系統中有的資源數量有兩種計算方式:

  • 第一種,由所有的模型例項上指定的最大數量。
  • 第二種,啟動的時候設定。

Priority:

表示執行的優先順序。從文件的表述中,我們可以知道,數字越小優先順序越高,而且還是以概率的方式進行排程的,而不是誰優先誰執行。

An instance with priority 2 will be given 1/2 the number of scheduling chances as an instance with priority 1.

例子

我們先描述一下設想的例子。我們有四個模型,一個模型 A 不管怎樣都不會執行,一個模型 B 不管怎樣都會執行,兩個模型 CD 需要爭奪資源。每個模型都是 python backend 的 add_sub 模型,我們在 execute 方法中加入一個睡眠時間 10 秒鐘。啟動的時候指定資源的數量。

A: [R1: 10, R2: 5, R3: 10]
B: [R3: 3]
C: [R1: 5, R2: 5]
D: [R1: 4, R2: 6]

為了實驗,將 R3 設定為 global 型別。其實如果是在單卡情況下或者 CPU,global 和某裝置的資源就沒有區別了。因為我們使用的還是 Python backend,所以只能使用 CPU 了。

按照官方的文件,我們模仿寫出 A 的配置,BCD 的配置是類似的,這裡就不重複了。為了一些實驗,每個模型設定兩個模型例項。

  instance_group [
    {
      count: 2
      kind: KIND_CPU
      rate_limiter {
        resources [
          {
            name: "R1"
            count: 10
          },
          {
            name: "R2"
            count: 5
          },
          {
            name: "R3"
            global: True
            count: 10
          }
        ] 
        priority: 1
      }
    }
  ]

啟動

完整的程式碼在這裡:https://github.com/zzk0/triton/tree/master/rate

配置好之後,啟動的時候需要帶上選項 --rate-limit。如果不帶上這個選項的話,它是不會啟用 rate limiter 的。

下面是命令列輸出的,和 rate limiter 相關的部分。

  --rate-limit <string>
	Specify the mode for rate limiting. Options are
	"execution_count" and "off". The default is "off". For "execution_count", the
	server will determine the instance using configured priority and the
	number of time the instance has been used to run inference. The
	inference will finally be executed once the required resources are
	available. For "off", the server will ignore any rate limiter config and
	run inference as soon as an instance is ready.
  --rate-limit-resource <<string>:<integer>:<integer>>
	The number of resources available to the server. The format
	of this flag is
	--rate-limit-resource=<resource_name>:<count>:<device>. The <device> is optional and if not listed will be applied to
	every device. If the resource is specified as "GLOBAL" in the model
	configuration the resource is considered shared among all the devices
	in the system. The <device> property is ignored for such resources.
	This flag can be specified multiple times to specify each resources
	and their availability. By default, the max across all instances
	that list the resource is selected as its availability. The values for

這個是英偉達文件中給出的例子。R1 和 R3 的設定是有區別的,在模型配置檔案中,R1 不是全域性的,所以按照上面命令列的幫助資訊說的,R1 會在每個裝置上都有,R3 則是一個全域性資源。

--rate-limit-resource=R1:10 --rate-limit-resource=R2:5:0 --rate-limit-resource=R2:8:1 --rate-limit-resource=R3:2

GLOBAL: [R3: 2]
device0: [R1: 10, R2: 5]
device1: [R1: 10, R2: 8]

啟動的時候,我們帶上以下引數,保證了 A 一定不會執行,B 一定會執行,CD 爭奪資源執行。在進入 docker 之前,需要設定好目錄對映,將我的那個倉庫對映到 /triton 這個資料夾下面。

./bin/tritonserver --model-store=/triton/triton/rate/ --rate-limit=execution_count --rate-limit-resource=R1:8 --rate-limit-resource=R2:7 --rate-limit-resource=R3:10

啟動的時候會發現以下資訊,表示啟動失敗了啊... 所以咱們把 A 刪掉吧。

Resource count for "R1" is limited to 8 which will prevent scheduling of one or more model instances, the minimum required count is 10

之後使用客戶端指令碼進行請求就好了,如果沒有資源,那麼就要進行等待。為了試驗優先順序,我們可以將模型 D 的 priority 設定為 10,然後 C 設定為 1。請求的時候,互動使用兩個客戶端去做請求,之後會發現 C 執行完了之後,才輪到 D 執行。

問題

問:如果指定了 count 為 2,可是每個裝置上的資源數量是限定了的,那麼指定了 2 是否就意味著每次只能有一個模型執行呢?

答:是的,只能有一個例項在執行。這是實驗結果表明的,開兩個模型例項,execute 的時候 sleep 10s。第一個指令碼執行 10s,第二指令碼執行 18s(2s 切換 console, 輸入命令)。這說明了,有兩個模型例項,沒有滿足需要的資源,它就是不執行。

問:資源是裝置上的,那麼如果我不用 GPU 呢?

答:我們是不是可以將 CPU 也看成一個和 GPU 一樣的裝置呢?將 CPU 視為一個裝置就好了。