我不會用 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 視為一個設備就好了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM