Skip to content

Commit 50f6940

Browse files
committed
undo utils test changes
Signed-off-by: npolshakova <[email protected]>
1 parent 4076bd2 commit 50f6940

File tree

9 files changed

+87
-86
lines changed

9 files changed

+87
-86
lines changed

.golangci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ linters:
3131
- prealloc
3232
- unparam
3333
- unused
34-
settings:
34+
35+
linters-settings:
3536
revive:
3637
rules:
3738
- name: dot-imports

pkg/common/test_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func IsValidText(text string) bool {
4040
// during generation sentences are connected by space, skip it
4141
// additional space at the end of the string is invalid
4242
if text[charsTested] == ' ' && charsTested < len(text)-1 {
43-
charsTested += 1
43+
charsTested++
4444
found = true
4545
}
4646
break

pkg/common/utils.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ func InitRandom(seed int64) {
200200
uuid.SetRand(randomGenerator)
201201
}
202202

203-
// Returns an integer between min and max (included)
204-
func RandomInt(min int, max int) int {
203+
// RandomInt returns an integer between minVal and maxVal (included)
204+
func RandomInt(minVal int, maxVal int) int {
205205
randMutex.Lock()
206206
defer randMutex.Unlock()
207-
return randomGenerator.Intn(max-min+1) + min
207+
return randomGenerator.Intn(maxVal-minVal+1) + minVal
208208
}
209209

210210
// Returns true or false randomly
@@ -219,11 +219,11 @@ func RandomBool(probability int) bool {
219219
return randomGenerator.Float64() < float64(probability)/100
220220
}
221221

222-
// Returns a random float64 in the range [min, max)
223-
func RandomFloat(min float64, max float64) float64 {
222+
// RandomFloat returns a random float64 in the range [minVal, maxVal)
223+
func RandomFloat(minVal float64, maxVal float64) float64 {
224224
randMutex.Lock()
225225
defer randMutex.Unlock()
226-
return randomGenerator.Float64()*(max-min) + min
226+
return randomGenerator.Float64()*(maxVal-minVal) + minVal
227227
}
228228

229229
// Returns a normally distributed float64

pkg/kv-cache/block_cache.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ func (b *blockCache) start(ctx context.Context) {
7272
}
7373

7474
// startRequest adds a request with its associated block hashes to the cache
75-
func (bc *blockCache) startRequest(requestID string, blocks []uint64) error {
76-
bc.mu.Lock()
77-
defer bc.mu.Unlock()
75+
func (b *blockCache) startRequest(requestID string, blocks []uint64) error {
76+
b.mu.Lock()
77+
defer b.mu.Unlock()
7878

79-
if _, exists := bc.requestToBlocks[requestID]; exists {
79+
if _, exists := b.requestToBlocks[requestID]; exists {
8080
// request with the same id already exists
8181
return fmt.Errorf("request already exists for id %s", requestID)
8282
}
@@ -93,67 +93,67 @@ func (bc *blockCache) startRequest(requestID string, blocks []uint64) error {
9393
// count number of new blocks + number of blocks that are in the unused blocks
9494
// don't update the data until we are sure that it's ok
9595
for _, blockHash := range blocks {
96-
if _, exists := bc.unusedBlocks[blockHash]; exists {
96+
if _, exists := b.unusedBlocks[blockHash]; exists {
9797
blockToMoveToUsed = append(blockToMoveToUsed, blockHash)
98-
} else if _, exists := bc.usedBlocks[blockHash]; !exists {
98+
} else if _, exists := b.usedBlocks[blockHash]; !exists {
9999
blocksToAdd = append(blocksToAdd, blockHash)
100100
} else {
101101
blockAreadyInUse = append(blockAreadyInUse, blockHash)
102102
}
103103
}
104104

105-
if len(bc.usedBlocks)+len(blocksToAdd)+len(blockToMoveToUsed) > bc.maxBlocks {
105+
if len(b.usedBlocks)+len(blocksToAdd)+len(blockToMoveToUsed) > b.maxBlocks {
106106
return errors.New(capacityError)
107107
}
108108

109109
// for blocks that are already in use - update the reference
110110
for _, block := range blockAreadyInUse {
111-
bc.usedBlocks[block] += 1
111+
b.usedBlocks[block]++
112112
}
113113

114114
// for block used in the past - move them to the used blocks collection
115115
for _, block := range blockToMoveToUsed {
116-
bc.usedBlocks[block] = 1
117-
delete(bc.unusedBlocks, block)
116+
b.usedBlocks[block] = 1
117+
delete(b.unusedBlocks, block)
118118
}
119119

120120
// for new block - add them, if there is no empty slots - evict the oldest block
121121
for _, block := range blocksToAdd {
122-
if len(bc.usedBlocks)+len(bc.unusedBlocks) == bc.maxBlocks {
122+
if len(b.usedBlocks)+len(b.unusedBlocks) == b.maxBlocks {
123123
// cache is full but contains unused blocks - evict the oldest
124124
var oldestUnusedHash uint64
125125
oldestUnusedTime := time.Now()
126126

127-
for hash, t := range bc.unusedBlocks {
127+
for hash, t := range b.unusedBlocks {
128128
if t.Before(oldestUnusedTime) {
129129
oldestUnusedHash = hash
130130
oldestUnusedTime = t
131131
}
132132
}
133133

134-
delete(bc.unusedBlocks, oldestUnusedHash)
135-
bc.eventChan <- EventData{action: eventActionRemove, hashValues: []uint64{oldestUnusedHash}}
134+
delete(b.unusedBlocks, oldestUnusedHash)
135+
b.eventChan <- EventData{action: eventActionRemove, hashValues: []uint64{oldestUnusedHash}}
136136
}
137137

138138
// Add the new block
139-
bc.usedBlocks[block] = 1
140-
bc.eventChan <- EventData{action: eventActionStore, hashValues: []uint64{block}}
139+
b.usedBlocks[block] = 1
140+
b.eventChan <- EventData{action: eventActionStore, hashValues: []uint64{block}}
141141
}
142142

143143
// store the request mapping
144-
bc.requestToBlocks[requestID] = make([]uint64, len(blocks))
145-
copy(bc.requestToBlocks[requestID], blocks)
144+
b.requestToBlocks[requestID] = make([]uint64, len(blocks))
145+
copy(b.requestToBlocks[requestID], blocks)
146146

147147
return nil
148148
}
149149

150150
// finishRequest processes the completion of a request, decreasing reference counts
151-
func (bc *blockCache) finishRequest(requestID string) error {
152-
bc.mu.Lock()
153-
defer bc.mu.Unlock()
151+
func (b *blockCache) finishRequest(requestID string) error {
152+
b.mu.Lock()
153+
defer b.mu.Unlock()
154154

155155
// Get blocks associated with this request
156-
blockHashes, exists := bc.requestToBlocks[requestID]
156+
blockHashes, exists := b.requestToBlocks[requestID]
157157
if !exists {
158158
return errors.New("request not found")
159159
}
@@ -163,27 +163,27 @@ func (bc *blockCache) finishRequest(requestID string) error {
163163
// Decrease reference count for each block
164164
errBlocks := make([]uint64, 0)
165165
for _, blockHash := range blockHashes {
166-
if refCount, exists := bc.usedBlocks[blockHash]; exists {
166+
if refCount, exists := b.usedBlocks[blockHash]; exists {
167167
if refCount > 1 {
168168
// this block is in use by another request, just update reference count
169-
bc.usedBlocks[blockHash] = refCount - 1
169+
b.usedBlocks[blockHash] = refCount - 1
170170
} else {
171171
// this was the last block usage - move this block to unused
172-
bc.unusedBlocks[blockHash] = now
173-
delete(bc.usedBlocks, blockHash)
172+
b.unusedBlocks[blockHash] = now
173+
delete(b.usedBlocks, blockHash)
174174
}
175175
} else {
176176
errBlocks = append(errBlocks, blockHash)
177177
}
178178
}
179179

180180
// Remove the request mapping
181-
delete(bc.requestToBlocks, requestID)
181+
delete(b.requestToBlocks, requestID)
182182

183183
if len(errBlocks) > 0 {
184184
errMsg := "Not existing blocks "
185-
for _, b := range errBlocks {
186-
errMsg += fmt.Sprintf("%d, ", b)
185+
for _, bl := range errBlocks {
186+
errMsg += fmt.Sprintf("%d, ", bl)
187187
}
188188
return fmt.Errorf("%s for request %s", errMsg[:len(errMsg)-2], requestID)
189189
}
@@ -192,26 +192,26 @@ func (bc *blockCache) finishRequest(requestID string) error {
192192
}
193193

194194
// GetStats returns current cache statistics (for testing/debugging)
195-
func (bc *blockCache) getStats() (int, int, int) {
196-
bc.mu.RLock()
197-
defer bc.mu.RUnlock()
195+
func (b *blockCache) getStats() (int, int, int) {
196+
b.mu.RLock()
197+
defer b.mu.RUnlock()
198198

199-
return len(bc.requestToBlocks), len(bc.usedBlocks) + len(bc.unusedBlocks), len(bc.unusedBlocks)
199+
return len(b.requestToBlocks), len(b.usedBlocks) + len(b.unusedBlocks), len(b.unusedBlocks)
200200
}
201201

202202
// getBlockInfo returns reference count and if it's in the cache for a specific block (for testing)
203203
// if block is in use by currently running requests the count will be positive, boolean is true
204204
// if block is in the unused list - count is 0, boolean is true
205205
// if block is not in both collections - count is 0, boolean is false
206-
func (bc *blockCache) getBlockInfo(blockHash uint64) (int, bool) {
207-
bc.mu.RLock()
208-
defer bc.mu.RUnlock()
206+
func (b *blockCache) getBlockInfo(blockHash uint64) (int, bool) {
207+
b.mu.RLock()
208+
defer b.mu.RUnlock()
209209

210-
refCount, exists := bc.usedBlocks[blockHash]
210+
refCount, exists := b.usedBlocks[blockHash]
211211
if exists {
212212
return refCount, true
213213
}
214-
_, exists = bc.unusedBlocks[blockHash]
214+
_, exists = b.unusedBlocks[blockHash]
215215
if exists {
216216
return 0, true
217217
}

pkg/kv-cache/kv_cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ import (
2727
"github.com/llm-d/llm-d-kv-cache-manager/pkg/tokenization"
2828
)
2929

30-
type KVCacheHelper struct {
30+
type Helper struct {
3131
tokenizer tokenization.Tokenizer
3232
tokensProcessor kvblock.TokenProcessor // turns tokens to kv block keys
3333
logger logr.Logger
3434
blockCache *blockCache
3535
}
3636

37-
func NewKVCacheHelper(config *common.Configuration, logger logr.Logger) (*KVCacheHelper, error) {
37+
func NewKVCacheHelper(config *common.Configuration, logger logr.Logger) (*Helper, error) {
3838
tokenProcConfig := kvblock.DefaultTokenProcessorConfig()
3939
tokenProcConfig.BlockSize = config.TokenBlockSize
4040
if config.HashSeed != "" {
@@ -54,7 +54,7 @@ func NewKVCacheHelper(config *common.Configuration, logger logr.Logger) (*KVCach
5454
if err != nil {
5555
return nil, fmt.Errorf("failed to create block cache: %w", err)
5656
}
57-
return &KVCacheHelper{
57+
return &Helper{
5858
tokenizer: tokenizer,
5959
tokensProcessor: tokensProcessor,
6060
blockCache: blockCache,
@@ -63,11 +63,11 @@ func NewKVCacheHelper(config *common.Configuration, logger logr.Logger) (*KVCach
6363
}
6464

6565
// Run starts the helper.
66-
func (h *KVCacheHelper) Run(ctx context.Context) {
66+
func (h *Helper) Run(ctx context.Context) {
6767
h.blockCache.start(ctx)
6868
}
6969

70-
func (h *KVCacheHelper) OnRequestStart(vllmReq openaiserverapi.CompletionRequest) error {
70+
func (h *Helper) OnRequestStart(vllmReq openaiserverapi.CompletionRequest) error {
7171
h.logger.Info("KV cache - process request")
7272

7373
prompt := vllmReq.GetPrompt()
@@ -93,6 +93,6 @@ func (h *KVCacheHelper) OnRequestStart(vllmReq openaiserverapi.CompletionRequest
9393
return h.blockCache.startRequest(requestID, blockHashes)
9494
}
9595

96-
func (h *KVCacheHelper) OnRequestEnd(vllmReq openaiserverapi.CompletionRequest) error {
96+
func (h *Helper) OnRequestEnd(vllmReq openaiserverapi.CompletionRequest) error {
9797
return h.blockCache.finishRequest(vllmReq.GetRequestID())
9898
}

pkg/llm-d-inference-sim/simulator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type VllmSimulator struct {
8383
// schema validator for tools parameters
8484
toolsValidator *openaiserverapi.Validator
8585
// kv cache functionality
86-
kvcacheHelper *kvcache.KVCacheHelper
86+
kvcacheHelper *kvcache.Helper
8787
// namespace where simulator is running
8888
namespace string
8989
// pod name of simulator
@@ -211,12 +211,12 @@ func (s *VllmSimulator) readRequest(ctx *fasthttp.RequestCtx, isChatCompletion b
211211
}
212212

213213
for _, tool := range req.Tools {
214-
toolJson, err := json.Marshal(tool.Function)
214+
toolJSON, err := json.Marshal(tool.Function)
215215
if err != nil {
216216
s.logger.Error(err, "failed to marshal request tools")
217217
return nil, err
218218
}
219-
err = s.toolsValidator.ValidateTool(toolJson)
219+
err = s.toolsValidator.ValidateTool(toolJSON)
220220
if err != nil {
221221
s.logger.Error(err, "tool validation failed")
222222
return nil, err
@@ -556,8 +556,8 @@ func (s *VllmSimulator) createCompletionResponse(isChatCompletion bool, respToke
556556
baseResp.DoRemoteDecode = true
557557
baseResp.DoRemotePrefill = false
558558
// currently remote prefill information is hard-coded
559-
baseResp.RemoteBlockIds = []string{"DUMMY_ID"}
560-
baseResp.RemoteEngineId = "DUMMY_ID"
559+
baseResp.RemoteBlockIDs = []string{"DUMMY_ID"}
560+
baseResp.RemoteEngineID = "DUMMY_ID"
561561
baseResp.RemoteHost = "DUMMY"
562562
baseResp.RemotePort = 1234
563563
}

pkg/openai-server-api/request.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ type baseCompletionRequest struct {
7373
DoRemoteDecode bool `json:"do_remote_decode"`
7474
// DoRemotePrefill boolean value, true when request's prefill was done on remote pod
7575
DoRemotePrefill bool `json:"do_remote_prefill"`
76-
// RemoteBlockIds is a list of block identifiers to process remotely for distributed decoding
77-
RemoteBlockIds []string `json:"remote_block_ids"`
78-
// RemoteEngineId is an identifier of the remote inference engine or backend to use for processing requests
79-
RemoteEngineId string `json:"remote_engine_id"`
76+
// RemoteBlockIDs is a list of block identifiers to process remotely for distributed decoding
77+
RemoteBlockIDs []string `json:"remote_block_ids"`
78+
// RemoteEngineID is an identifier of the remote inference engine or backend to use for processing requests
79+
RemoteEngineID string `json:"remote_engine_id"`
8080
// RemoteHost is a hostname or IP address of the remote server handling prefill
8181
RemoteHost string `json:"remote_host"`
8282
// RemotePort is a port of the remote server handling prefill
@@ -197,10 +197,10 @@ func (c *ChatCompletionRequest) GetMaxCompletionTokens() *int64 {
197197

198198
// getLastUserMsg returns last message from this request's messages with user role,
199199
// if does not exist - returns an empty string
200-
func (req *ChatCompletionRequest) getLastUserMsg() string {
201-
for i := len(req.Messages) - 1; i >= 0; i-- {
202-
if req.Messages[i].Role == RoleUser {
203-
return req.Messages[i].Content.PlainText()
200+
func (c *ChatCompletionRequest) getLastUserMsg() string {
201+
for i := len(c.Messages) - 1; i >= 0; i-- {
202+
if c.Messages[i].Role == RoleUser {
203+
return c.Messages[i].Content.PlainText()
204204
}
205205
}
206206

@@ -210,15 +210,15 @@ func (req *ChatCompletionRequest) getLastUserMsg() string {
210210
// CreateResponseText creates and returns response payload based on this request,
211211
// i.e., an array of generated tokens, the finish reason, and the number of created
212212
// tokens
213-
func (req ChatCompletionRequest) CreateResponseText(mode string) ([]string, string, int, error) {
214-
maxTokens, err := common.GetMaxTokens(req.MaxCompletionTokens, req.MaxTokens)
213+
func (c ChatCompletionRequest) CreateResponseText(mode string) ([]string, string, int, error) {
214+
maxTokens, err := common.GetMaxTokens(c.MaxCompletionTokens, c.MaxTokens)
215215
if err != nil {
216216
return nil, "", 0, err
217217
}
218218

219219
var text, finishReason string
220220
if mode == common.ModeEcho {
221-
text, finishReason = common.GetResponseText(maxTokens, req.getLastUserMsg())
221+
text, finishReason = common.GetResponseText(maxTokens, c.getLastUserMsg())
222222
} else {
223223
text, finishReason = common.GetRandomResponseText(maxTokens)
224224
}
@@ -250,30 +250,30 @@ func (t *TextCompletionRequest) GetNumberOfPromptTokens() int {
250250
return len(common.Tokenize(t.GetPrompt()))
251251
}
252252

253-
func (c *TextCompletionRequest) GetTools() []Tool {
253+
func (t *TextCompletionRequest) GetTools() []Tool {
254254
return nil
255255
}
256256

257-
func (c *TextCompletionRequest) GetToolChoice() string {
257+
func (t *TextCompletionRequest) GetToolChoice() string {
258258
return ""
259259
}
260260

261-
func (c *TextCompletionRequest) GetMaxCompletionTokens() *int64 {
262-
return c.MaxTokens
261+
func (t *TextCompletionRequest) GetMaxCompletionTokens() *int64 {
262+
return t.MaxTokens
263263
}
264264

265265
// CreateResponseText creates and returns response payload based on this request,
266266
// i.e., an array of generated tokens, the finish reason, and the number of created
267267
// tokens
268-
func (req TextCompletionRequest) CreateResponseText(mode string) ([]string, string, int, error) {
269-
maxTokens, err := common.GetMaxTokens(nil, req.MaxTokens)
268+
func (t TextCompletionRequest) CreateResponseText(mode string) ([]string, string, int, error) {
269+
maxTokens, err := common.GetMaxTokens(nil, t.MaxTokens)
270270
if err != nil {
271271
return nil, "", 0, err
272272
}
273273

274274
var text, finishReason string
275275
if mode == common.ModeEcho {
276-
text, finishReason = common.GetResponseText(maxTokens, req.Prompt)
276+
text, finishReason = common.GetResponseText(maxTokens, t.Prompt)
277277
} else {
278278
text, finishReason = common.GetRandomResponseText(maxTokens)
279279
}

0 commit comments

Comments
 (0)