@@ -39,7 +39,7 @@ class LlamaEmbedding(Llama):
3939 def __init__ (
4040 self ,
4141 model_path : str ,
42- n_ctx : int = 1024 ,
42+ n_ctx : int = 0 ,
4343 n_batch : int = 512 ,
4444 n_ubatch : int = 512 ,
4545 pooling_type : int = LLAMA_POOLING_TYPE_UNSPECIFIED ,
@@ -130,11 +130,12 @@ def embed(
130130
131131 # Determine if it is in Rerank mode
132132 try :
133- current_pooling = self .pooling_type ()
133+ pooling_type = self .pooling_type ()
134134 except AttributeError :
135- current_pooling = LLAMA_POOLING_TYPE_UNSPECIFIED
136- is_rank = (current_pooling == LLAMA_POOLING_TYPE_RANK )
137- logits_all = current_pooling == llama_cpp .LLAMA_POOLING_TYPE_NONE
135+ pooling_type = LLAMA_POOLING_TYPE_UNSPECIFIED
136+ is_rank = (pooling_type == LLAMA_POOLING_TYPE_RANK )
137+ is_none = (pooling_type == LLAMA_POOLING_TYPE_NONE ) # Token-level embedding
138+ logits_all = True if is_none else False
138139
139140 # Determine the output dimension
140141 if is_rank :
@@ -143,8 +144,8 @@ def embed(
143144 out_dim = self .n_embd ()
144145
145146 if self .verbose :
146- mode_str = "RANK (Score)" if is_rank else "EMBED (Vector)"
147- print (f"LlamaEmbedding Debug: Mode={ mode_str } | Output Dimension ={ out_dim } " )
147+ type_str = "TOKEN (None)" if is_none else ( " RANK (Score)" if is_rank else "SEQ (Vector)" )
148+ print (f"LlamaEmbedding Debug: Mode={ type_str } | Pooling= { pooling_type } | Dim ={ out_dim } " )
148149
149150 # Preprocess Input
150151 inputs : List [Union [str , List [int ]]] = []
@@ -179,17 +180,38 @@ def _decode_batch():
179180
180181 self ._ctx .decode (self ._batch )
181182
182- for i in range (len (batch_seq_lens )):
183- ptr = llama_cpp .llama_get_embeddings_seq (ctx , i )
184- data = ptr [:out_dim ]
185-
186- if not is_rank :
187- data = self ._normalize_vector (data , normalize )
188-
189- if is_rank and len (data ) == 1 :
190- results .append (data [0 ])
191- else :
192- results .append (data )
183+ # Extract Embeddings
184+ # Branch A: LLAMA_POOLING_TYPE_NONE (Token Level)
185+ if is_none :
186+ curr_token_idx = 0
187+ for seq_len in batch_seq_lens :
188+ doc_tokens_embd = []
189+ for _ in range (seq_len ):
190+ # Get the vector of the i-th token
191+ ptr = llama_cpp .llama_get_embeddings_ith (ctx , curr_token_idx )
192+ data = ptr [:out_dim ]
193+
194+ # Normalization
195+ data = self ._normalize_vector (data , normalize )
196+
197+ doc_tokens_embd .append (data )
198+ curr_token_idx += 1
199+ results .append (doc_tokens_embd )
200+
201+ # Branth B: Sequence Level (Mean, Cls, Rank, Unspecified)
202+ else :
203+ for i in range (len (batch_seq_lens )):
204+ # Obtain the vector of the i-th sequence.
205+ ptr = llama_cpp .llama_get_embeddings_seq (ctx , i )
206+ data = ptr [:out_dim ]
207+
208+ if not is_rank :
209+ data = self ._normalize_vector (data , normalize )
210+
211+ if is_rank and len (data ) == 1 :
212+ results .append (data [0 ])
213+ else :
214+ results .append (data )
193215
194216 self ._batch .reset ()
195217 llama_cpp .llama_memory_clear (llama_cpp .llama_get_memory (ctx ), True )
0 commit comments