easydel.inference.vsurge.__init__#
- class easydel.inference.vsurge.__init__.oDriver(engine: oEngine)[source]#
Bases:
AbstractDriveroDriver is responsible for managing the inference process for the oEngine. It handles request submission, input preparation, inference execution, and processing of model outputs. It utilizes background threads for concurrent processing of different stages of the inference pipeline.
- compile()[source]#
Compiles the underlying engines.
This method is intended to perform any necessary compilation steps for the inference engines. Currently, itโs a placeholder.
- property driver_name#
Returns the name of the driver, derived from the engineโs model name.
- get_total_concurrent_requests() int[source]#
Gets the total number of concurrent requests the driver can handle.
This is determined by the maximum number of concurrent decode operations supported by the engine.
- Returns
The maximum number of concurrent requests.
- Return type
int
- property num_used_slots#
- place_request_on_prefill_queue(request: ActiveRequest)[source]#
Places a new request onto the prefill queue for processing.
This method is used internally to add requests that require prefilling and subsequent generation.
- Parameters
request (ActiveRequest) โ The active request to place on the queue.
- property processor: Any#
Returns the processor/tokenizer associated with the engines.
Assumes all engines (prefill and decode) use the same processor. Raises an error if no engines are configured.
- start()[source]#
Starts the driver and its background processing threads.
Threads for input preparation, inference, and summary processing are started if the driver is not already live.
- stop()[source]#
Stops the driver and all background threads gracefully.
Signals the background threads to exit by putting None into their respective queues and then waits for them to join.
- submit_request(request: Any)[source]#
Submits a new request to the driverโs processing pipeline.
The request is placed on the prefill queue for initial processing.
- Parameters
request (tp.Any) โ The request object to submit. Must be of type ActiveRequest.
- Raises
TypeError โ If the submitted request is not an instance of ActiveRequest.
- class easydel.inference.vsurge.__init__.oEngine(model: Any, processor: Any, storage: PagedAttentionCache, manager: HBMPageManager, max_concurrent_decodes: int | None = None, max_concurrent_prefill: int | None = None, prefill_lengths: int | None = None, max_prefill_length: int | None = None, max_length: int | None = None, batch_size: int | None = None, seed: int = 894)[source]#
Bases:
AbstractInferenceEngineOptimized inference engine for EasyDeL models using Paged Attention.
This engine manages the inference process, including KV cache management with Paged Attention, scheduling, and execution of model forward passes for both prefill and decode steps.
- bulk_insert(prefix: Any, decode_state: Any, slots: list[int]) Any[source]#
Efficiently inserts multiple prefill results into the decode state.
This method is optimized for integrating the results of a batch prefill operation into the decode state for multiple sequence slots.
- Parameters
prefix โ The generation state from the bulk prefill step.
decode_state โ The current decode state.
slots โ A list of slot indices to insert into.
- Returns
The updated decode state.
- property colocated_cpus: Optional[list[jaxlib.xla_extension.Device]]#
Returns CPU devices colocated with the engineโs accelerator devices.
This information can be useful for optimizing data transfers between host (CPU) and accelerator (GPU/TPU) memory. Currently returns None as the implementation is pending.
- Returns
A list of colocated JAX CPU devices, or None if not implemented or available.
- decode(graphstate: State[Key, VariableState[Any]], graphothers: State[Key, VariableState[Any]], state: Any, rngs: PRNGKey) tuple[Any, Any][source]#
Performs a single decode step for active sequences.
This involves generating the next token for each sequence based on the current state and KV cache.
- Parameters
graphstate โ The graph state of the model.
graphothers โ Other graph components of the model.
state โ The current generation state.
rngs โ The PRNG key for sampling.
- Returns
A tuple containing the updated generation state and the generated result tokens.
- property eos_token_ids: list[int]#
Returns a list of end-of-sequence token IDs from the processor and model config.
- forward(graphstate: State[Key, VariableState[Any]], graphothers: State[Key, VariableState[Any]], state: ActiveSequenceBatch, iteration_plan: NextIterationPlan) ModelOutputBatch[source]#
Performs a forward pass of the model.
This method executes the compiled continuous_forward function, processing the input state and iteration plan to produce model outputs and update the KV cache storage.
- Parameters
graphstate โ The graph state of the model.
graphothers โ Other graph components of the model.
state โ The current active sequence batch state.
iteration_plan โ The plan for the current inference iteration.
- Returns
The output batch from the model.
- free_resource(slot: int) bool[source]#
Frees resources associated with a specific inference slot. (Not Implemented)
- Parameters
slot โ The index of the slot to free.
- Returns
Always returns False as itโs not implemented.
- get_prefix_destination_sharding() Any[source]#
Returns the shardings necessary to transfer KV cache data between engines.
This method is intended for scenarios involving multiple engines or devices where KV cache data needs to be moved.
- Returns
The sharding specification for prefix destinations.
- get_state_shardings(is_decode: bool = False)[source]#
Returns the sharding specifications for the engineโs state.
- Parameters
is_decode โ A boolean indicating if the sharding is for the decode state.
- Returns
A tuple representing the sharding specification.
- init_decode_state(*args, **kwargs) ActiveSequenceBatch[source]#
Initializes the decode state for active sequences.
- Parameters
*args โ Variable length argument list.
**kwargs โ Arbitrary keyword arguments.
- Returns
An initialized ActiveSequenceBatch instance.
- insert(prefix: Any, decode_state: Any, slot: int) Any[source]#
Inserts or updates a generation state for a specific slot.
This is typically used to integrate the results of a prefill step into the ongoing decode process for a particular sequence slot.
- Parameters
prefix โ The generation state from the prefill step.
decode_state โ The current decode state.
slot โ The slot index to insert into.
- Returns
The updated decode state.
- property max_concurrent_decodes#
Maximum number of sequences decoded concurrently.
- property max_concurrent_prefill#
- property max_length: int#
Maximum total sequence length (prompt + generation).
This defines the size of the KV cache allocated.
- property max_prefill_length: int#
Maximum allowed length for the initial prompt (prefill phase).
Prompts longer than this will be truncated or handled according to the padding/truncation logic.
- property pad_token_id#
Returns the pad token ID from the processor.
- prefill(graphstate: State[Key, VariableState[Any]], graphothers: State[Key, VariableState[Any]], tokens: Array, valids: Array, true_length: int, temperature: Array, top_p: Array, top_k: Array, rngs: PRNGKey) tuple[Any, Any][source]#
Performs the prefill step for a batch of prompts.
This involves processing the initial prompt tokens and populating the KV cache.
- Parameters
graphstate โ The graph state of the model.
graphothers โ Other graph components of the model.
tokens โ The input tokens for the prompts.
valids โ A boolean array indicating valid tokens.
true_length โ The true length of the sequences.
temperature โ The temperature for sampling.
top_p โ The top-p value for sampling.
top_k โ The top-k value for sampling.
rngs โ The PRNG key for sampling.
- Returns
A tuple containing the generation state after prefill and the result tokens.
- property prefill_lengths: list[int]#
Returns the configured list of max prefill length buckets for the engine.
- property prng_key: PRNGKey#
Provides a new PRNG key split from the internal state for sampling.
Each call to this property consumes the current key and returns a new, unique key, ensuring that subsequent sampling operations use different randomness.
- Returns
A new JAX PRNGKey.
- property samples_per_slot: int#
Number of samples generated per inference slot.
This determines how many independent generation results are produced for each logical slot managed by the engine. Itโs often 1, but could be higher for techniques like parallel sampling.
- class easydel.inference.vsurge.__init__.vDriver(prefill_engines: Optional[Union[list[easydel.inference.vsurge.engines.vengine.engine.vEngine], vEngine]] = None, decode_engines: Optional[Union[list[easydel.inference.vsurge.engines.vengine.engine.vEngine], vEngine]] = None, interleaved_mode: bool = False, detokenizing_blocks: int = 8)[source]#
Bases:
AbstractDriverDrives the engines.
- property driver_name#
- get_total_concurrent_requests() int[source]#
Gets the total number of concurrent requests the driver can handle.
- place_request_on_prefill_queue(request: ActiveRequest)[source]#
Used to place new requests for prefilling and generation.
- property processor: Any#
Returns the processor/tokenizer associated with the engines.
Assumes all engines (prefill and decode) use the same processor. Raises an error if no engines are configured.
- class easydel.inference.vsurge.__init__.vEngine(model: Any, processor: Any, max_concurrent_decodes: int | None = None, max_concurrent_prefill: int | None = None, prefill_lengths: int | None = None, max_prefill_length: int | None = None, max_length: int | None = None, batch_size: int | None = None, seed: int = 894)[source]#
Bases:
AbstractInferenceEngineCore inference engine for EasyDeL models using NNX graphs.
This engine manages the model state (split into graph definition, state, and other parameters) and provides JIT-compiled functions for the prefill and decode steps of autoregressive generation. It handles KV caching and sampling.
- bulk_insert(prefix: GenerationState, decode_state: GenerationState, slots: list[int]) GenerationState[source]#
Efficiently inserts multiple prefill results into the decode state.
This function takes a GenerationState (prefix) typically resulting from a batch prefill operation and inserts its relevant components (logits, cache, index, tokens, valids, position IDs, generated tokens) into the main decode_state at multiple specified slots. This is useful for initializing the decode state after processing a batch of prompts simultaneously. Both input statesโ caches are donated.
- Parameters
prefix โ The GenerationState containing the results from a prefill operation (or similar initialization). Its cache is marked for donation.
decode_state โ The target GenerationState (e.g., the main decode loop state) to be updated. Its cache is marked for donation.
slots โ A list of integer indices indicating the slots within the decode_stateโs batch dimension where the corresponding data from the prefix state should be inserted.
- Returns
An updated GenerationState (decode_state) with the prefill results inserted at the specified slots.
- property colocated_cpus: Optional[list[jaxlib.xla_extension.Device]]#
Returns CPU devices colocated with the engineโs accelerator devices.
This information can be useful for optimizing data transfers between host (CPU) and accelerator (GPU/TPU) memory. Currently returns None as the implementation is pending.
- Returns
A list of colocated JAX CPU devices, or None if not implemented or available.
- decode(graphstate: State[Key, VariableState[Any]], graphothers: State[Key, VariableState[Any]], state: GenerationState, rngs: PRNGKey) tuple[easydel.inference.vsurge.engines.vengine.utilities.GenerationState, easydel.inference.vsurge.engines._utils.ResultTokens][source]#
Performs a single decode step in the autoregressive generation loop.
Takes the previous GenerationState, generates the next token using the model and KV cache, and updates the state. This function is JIT-compiled and allows the input stateโs cache to be modified in-place (donated).
- Parameters
graphstate โ The NNX GraphState (parameters) of the model.
graphothers โ Other NNX state variables of the model.
state โ The current GenerationState from the previous step. state.cache is marked for donation.
rngs โ A JAX PRNG key for sampling the next token.
- Returns
next_generation_state: The updated GenerationState for the next iteration.
result: A ResultTokens object containing the newly generated token.
- Return type
A tuple containing
- property eos_token_ids: list[int]#
A list of End-of-Sequence token IDs.
- free_resource(slot: int) bool[source]#
Frees resources associated with a specific inference slot. (Not Implemented)
- Parameters
slot โ The index of the slot to free.
- Returns
Always returns False as itโs not implemented.
- get_prefix_destination_sharding() Any[source]#
Returns the shardings necessary to transfer KV cache data between engines.
Currently returns None, indicating default or no specific sharding.
- init_decode_state(*args, **kwargs) GenerationState[source]#
Initializes the GenerationState for a new sequence
- insert(prefix: GenerationState, decode_state: GenerationState, slot: int) GenerationState[source]#
Inserts or updates a generation state, potentially for managing batches. (JIT-compiled)
This function seems designed to merge or update parts of the generation state, possibly inserting a โprefixโ state (e.g., from a completed prefill) into a larger batch state (โdecode_stateโ) at a specific โslotโ. The exact mechanism for insertion isnโt fully clear from the current implementation, as it primarily focuses on broadcasting the prefix cache and returning the prefix state. Both input statesโ caches are donated.
- Parameters
prefix โ The GenerationState to potentially insert (e.g., from prefill). Its cache is marked for donation.
decode_state โ The target GenerationState to update (e.g., the main decode loop state). Its cache is marked for donation.
slot โ The index within the batch where the insertion/update should occur.
- Returns
An updated GenerationState. In the current implementation, it returns the prefix state with its cache potentially broadcasted. Needs clarification on the intended merging logic with decode_state and slot.
- property max_concurrent_decodes: int#
Maximum number of sequences that can be decoded concurrently.
This determines the batch size used during the decode phase.
- property max_length: int#
Maximum total sequence length (prompt + generation).
This defines the size of the KV cache allocated.
- property max_prefill_length: int#
Maximum allowed length for the initial prompt (prefill phase).
Prompts longer than this will be truncated or handled according to the padding/truncation logic.
- property pad_token_id#
The ID of the padding token.
- prefill(graphstate: State[Key, VariableState[Any]], graphothers: State[Key, VariableState[Any]], tokens: Array, valids: Array, true_length: int, temperature: Array, top_p: Array, rngs: PRNGKey) tuple[easydel.inference.vsurge.engines.vengine.utilities.GenerationState, easydel.inference.vsurge.engines._utils.ResultTokens][source]#
Performs the prefill step for initializing the generation process.
Processes the initial prompt tokens, initializes the KV cache, and generates the first token of the sequence. This function is JIT-compiled.
- Parameters
graphstate โ The NNX GraphState (parameters) of the model.
graphothers โ Other NNX state variables of the model.
tokens โ The input prompt token IDs (batch_size, sequence_length).
valids โ A boolean array indicating valid token positions in the input (batch_size, sequence_length or batch_size, max_length).
rngs โ A JAX PRNG key for sampling the first token.
- Returns
generation_state: The initial GenerationState for the decode loop.
result: A ResultTokens object containing the first generated token.
- Return type
A tuple containing
- property prefill_lengths: list[int]#
Returns the configured list of max prefill length buckets for the engine.
- property prng_key: PRNGKey#
Provides a new PRNG key split from the internal state for sampling.
Each call to this property consumes the current key and returns a new, unique key, ensuring that subsequent sampling operations use different randomness.
- Returns
A new JAX PRNGKey.
- property samples_per_slot: int#
Number of samples generated per inference slot.
This determines how many independent generation results are produced for each logical slot managed by the engine. Itโs often 1, but could be higher for techniques like parallel sampling.
- class easydel.inference.vsurge.__init__.vSurge(driver: Union[vDriver, oDriver], vsurge_name: str | None = None)[source]#
Bases:
objectOrchestrates the interaction between client requests and the vDriver.
- async complete(request: vSurgeRequest) tp.AsyncGenerator[tp.List[ReturnSample]][source]#
Initiates and streams the results of a text completion request.
Creates an ActiveRequest using the plain prompt from the vSurgeRequest, places it on the driverโs prefill queue, and then asynchronously iterates through the results provided by the ActiveRequestโs return_channel.
It handles both client-side and server-side tokenization scenarios, buffering and processing results appropriately before yielding them.
- Parameters
request โ The vSurgeRequest containing the prompt and generation parameters.
- Yields
Processed generation results, similar to the decode method. The format depends on the tokenization mode.
- Raises
RuntimeError โ If the prefill queue is full when trying to place the request.
- count_tokens(text_or_conversation: Union[str, list]) int[source]#
Counts the number of tokens in a given string or conversation list.
Uses the underlying driverโs processor to tokenize the input and returns the count of tokens.
- Parameters
text_or_conversation โ Either a single string or a list of message dictionaries (like OpenAI chat format).
- Returns
The total number of tokens in the input.
- Raises
ValueError โ If the input type is invalid or tokenization fails.
- classmethod create_odriver(model: Any, processor: Any, storage: Optional[PagedAttentionCache] = None, manager: Optional[HBMPageManager] = None, page_size: int = 128, hbm_utilization: float = 0.6, max_concurrent_prefill: int | None = None, max_concurrent_decodes: int | None = None, prefill_lengths: int | None = None, max_prefill_length: int | None = None, max_length: int | None = None, seed: int = 894, vsurge_name: str | None = None) vSurge[source]#
- classmethod create_vdriver(model: Any, processor: Any, max_concurrent_decodes: int | None = None, prefill_lengths: int | None = None, max_prefill_length: int | None = None, max_length: int | None = None, seed: int = 894, vsurge_name: str | None = None) vSurge[source]#
Creates a new instance of vSurge with configured vDriver and vEngines.
This class method provides a convenient way to instantiate the vSurge by setting up the necessary prefill and decode engines with the provided model, processor, and configuration parameters.
- Parameters
model โ The EasyDeLBaseModule instance representing the model.
processor โ The tokenizer/processor instance.
max_concurrent_decodes โ Maximum number of concurrent decode requests the decode engine can handle.
prefill_lengths โ A list of prefill lengths to compile for the prefill engine.
max_prefill_length โ The maximum prefill length for the prefill engine.
max_length โ The maximum total sequence length for both engines.
seed โ The random seed for reproducibility.
vsurge_name โ An optional name for the vsurge.
- Returns
A new instance of vSurge.
- property driver#
Provides access to the underlying vDriver instance.
- async generate(prompts: tp.Union[str, tp.Sequence[str]], sampling_params: tp.Optional[tp.Union[SamplingParams, tp.Sequence[SamplingParams]]] = None, stream: bool = False) tp.Union[tp.List[ReturnSample], tp.AsyncGenerator[tp.List[ReturnSample]]][source]#
Generates text completions concurrently for the given prompts.
- Parameters
prompts โ A single prompt string or a list of prompt strings.
sampling_params โ A single SamplingParams object or a list of SamplingParams objects. If None, default SamplingParams will be used. If a single SamplingParams object is provided with multiple prompts, it will be applied to all prompts. If a list is provided, it must have the same length as the prompts list.
stream โ If True, yields results (List[ReturnSample]) from any request as they become available. The list corresponds to one generation step from one request. If False, waits for all requests to complete and returns a list containing one aggregated ReturnSample per prompt.
- Returns
- An async generator yielding lists of ReturnSample as
steps complete across concurrent requests.
- If stream is False: A list of aggregated ReturnSample objects, one for
each input prompt, after all requests have finished.
- Return type
If stream is True
- Raises
ValueError โ If the lengths of prompts and sampling_params lists mismatch.
RuntimeError โ If the underlying driverโs queue is full.
- process_client_side_tokenization_response(response: list[easydel.inference.vsurge.utils.ReturnSample])[source]#
Processes responses when tokenization is handled client-side.
In this case, the response items (ReturnSample) are typically yielded directly without further server-side processing like detokenization or buffering.
- Parameters
response โ A list of ReturnSample objects from a single generation step.
- Returns
The input list of ReturnSample objects, unchanged.
- process_server_side_tokenization_response(response: list[easydel.inference.vsurge.utils.ReturnSample], buffered_response_list: list[list[easydel.inference.vsurge.utils.ReturnSample]]) list[easydel.inference.vsurge.utils.ReturnSample][source]#
Processes responses when tokenization/detokenization is server-side.
Combines the text and token IDs from the current response and any buffered previous responses for each sample. It then uses the metrics (TPS, generated token count) from the latest response in the sequence for the final output.
- Parameters
response โ The list of ReturnSample objects from the current step.
buffered_response_list โ A list containing lists of ReturnSample objects from previous steps that were buffered.
- Returns
A list of tuples, where each tuple represents a completed sample and contains: (decoded_string, all_token_ids, latest_tps, latest_num_generated_tokens).
- property processor: Any#
Returns the processor/tokenizer associated with the underlying driver.
- should_buffer_response(response: list[easydel.inference.vsurge.utils.ReturnSample]) bool[source]#
Determines if a response needs buffering for server-side detokenization.
Buffering is needed if any sample in the response ends with a byte token (e.g., โ<0xAB>โ), as this indicates an incomplete multi-byte character that requires subsequent tokens for proper decoding.
- Parameters
response โ A list of ReturnSample objects from a single generation step.
- Returns
True if buffering is required, False otherwise.
- property vsurge_name#
- class easydel.inference.vsurge.__init__.vSurgeApiServer(vsurge_map: Union[Dict[str, vSurge], vSurge] = None, max_workers: int = 10, oai_like_processor: bool = True)[source]#
Bases:
objectFastAPI server for serving vEngine instances.
This server provides endpoints mimicking the OpenAI API structure for chat completions, liveness/readiness checks, token counting, and listing available models. It handles both streaming and non-streaming requests asynchronously using a thread pool.
- async chat_completions(request: ChatCompletionRequest)[source]#
Handles chat completion requests (POST /v1/chat/completions).
Validates the request, retrieves the appropriate vEngine model, tokenizes the input, and delegates to streaming or non-streaming handlers.
- Parameters
request (ChatCompletionRequest) โ The incoming request data.
- Returns
- The generated response, either
a complete JSON object or a streaming event-stream.
- Return type
Union[JSONResponse, StreamingResponse]
- async completions(request: CompletionRequest)[source]#
Handles completion requests (POST /v1/completions).
Processes the prompt for completion and returns generated text.
- Parameters
request (CompletionRequest) โ The incoming request data.
- Returns
The generated response.
- Return type
Union[JSONResponse, StreamingResponse]
- async count_tokens(request: CountTokenRequest)[source]#
Token counting endpoint (POST /v1/count_tokens).
- fire(host='0.0.0.0', port=11556, metrics_port: Optional[int] = None, log_level='info', ssl_keyfile: Optional[str] = None, ssl_certfile: Optional[str] = None)[source]#
Starts the uvicorn server to run the FastAPI application.
- Parameters
host (str) โ The host address to bind to. Defaults to โ0.0.0.0โ.
port (int) โ The port to listen on. Defaults to 11556.
metrics_port (tp.Optional[int]) โ The port for the Prometheus metrics server. If None, defaults to port + 1. Set to -1 to disable.
log_level (str) โ The logging level for uvicorn. Defaults to โinfoโ.
ssl_keyfile (tp.Optional[str]) โ Path to the SSL key file for HTTPS.
ssl_certfile (tp.Optional[str]) โ Path to the SSL certificate file for HTTPS.
- class easydel.inference.vsurge.__init__.vSurgeRequest(prompt: str, max_tokens: int, top_p: float = 1.0, top_k: int = 0, min_p: float = 0.0, temperature: float = 0.7, presence_penalty: float = 0.0, frequency_penalty: float = 0.0, repetition_penalty: float = 1.0, metadata: easydel.inference.vsurge.vsurge.vSurgeMetadata | None = None, is_client_side_tokenization: bool = False)[source]#
Bases:
objectRepresents a request specifically for text completion.
- frequency_penalty: float = 0.0#
- classmethod from_sampling_params(prompt: str, sampling_params: SamplingParams)[source]#
- is_client_side_tokenization: bool = False#
- max_tokens: int#
- metadata: easydel.inference.vsurge.vsurge.vSurgeMetadata | None = None#
- min_p: float = 0.0#
- presence_penalty: float = 0.0#
- prompt: str#
- repetition_penalty: float = 1.0#
- temperature: float = 0.7#
- top_k: int = 0#
- top_p: float = 1.0#