C++ engine API (deepinfer._C)
Code version: DeepInfer @ 9bd9cf2 · 2026-10-21
In one sentence
The compiled extension module deepinfer._C gives Python 40 names in three submodules; this page lists each one with the page that explains it.
Use this page to find a name fast. For the rules that apply to many names (memory that a call borrows, calls that release the GIL, loose types), read deepinfer/_C/engine.pyi: the C++ engine API, as seen from Python. This page does not repeat them.
The module and its three submodules#
- One module, three parts.
csrc/pybind.cppmakes the pybind11 module and adds the submodulescuda_utils,engineandgraph_timer(csrc/pybind.cpp:7-17). The stub of the package lists the same three (deepinfer/_C/__init__.pyi:1-5). - One build per platform.
setup.pycompiles the same engine sources on both platforms. It addscuda_utils.cppandgraph_timer_cuda.cppon NVIDIA, andasc_utils.cppandgraph_timer_asc.cppon Ascend (setup.py:22-34,setup.py:43-47,setup.py:61-65). - Where the file is. The compiled
.sois indeepinfer/_so/<accelerator>-<arch>/. The package adds that folder to its search path at import (deepinfer/__init__.py:28-32). - The stubs. The
.pyifiles are type stubs thatmake stubgengenerates withpybind11_stubgen(Makefile:27-28,scripts/gen_stub.sh:11-14). Python never imports them. The "Stub" column below gives the line in the stub.
Submodule engine (34 names)#
The stub lists the names in __all__ (deepinfer/_C/engine.pyi:8): 19 classes, 2 enums, 12 functions and 1 constant. The groups are the same as on the engine.pyi page, which also gives the binding and the C++ line of each name.
Scheduling and blocks#
| Name, kind | Purpose | Stub, read more |
|---|---|---|
Schedulerclass | The C++ scheduler: queues, schedule(), process_outputs(). | deepinfer/_C/engine.pyi:309csrc/engine/scheduler.hpp: the C++ Scheduler |
SchedulerOutputsclass | The result of one schedule() call (SchedulerOutputs). | deepinfer/_C/engine.pyi:371csrc/engine/scheduler.hpp: the C++ Scheduler |
BlockManagerclass | Free list of KV blocks, or of state slots. | deepinfer/_C/engine.pyi:9csrc/engine/block_manager.hpp: BlockManager and the free list |
SequenceMetadataPoolclass | Reuses SequenceMetadata objects from step to step. | deepinfer/_C/engine.pyi:588engine.pyi |
FlopsEstimatorclass | Estimates the FLOPs of a step, for the statistics. | deepinfer/_C/engine.pyi:51engine/stats_logger.py: step statistics, MFU and moe_lb |
process_MTPInfofunction | Copies new draft tokens and their confidence into the sequences. | deepinfer/_C/engine.pyi:729csrc/engine/scheduler.hpp: the C++ Scheduler |
clone_and_zero_states_and_blocksfunction | Device copies of state slots and blocks for forked sequences. | deepinfer/_C/engine.pyi:709worker/base.py: WorkerBase, the resource layer of a worker |
get_finished_seqs_infofunction | Counts and lengths of finished sequences, for the statistics. | deepinfer/_C/engine.pyi:717csrc/engine/scheduler.hpp: the C++ Scheduler |
update_prompt_logprobsfunction | Appends the prompt logprobs of a prefill chunk to each sequence. | deepinfer/_C/engine.pyi:733worker/model_runner.py: the ModelRunner class |
Sequences and ZOS inputs#
Outputs#
| Name, kind | Purpose | Stub, read more |
|---|---|---|
RequestOutputclass | One new token of one request (RequestOutput). | deepinfer/_C/engine.pyi:180csrc/engine/outputs.hpp: RequestOutput and its pool |
RequestOutputPoolclass | 10000 reusable output slots (RequestOutputPool). | deepinfer/_C/engine.pyi:277csrc/engine/outputs.hpp: RequestOutput and its pool |
build_request_outputsfunction | Makes the outputs of one step. Returns uint64 handles. | deepinfer/_C/engine.pyi:707csrc/engine/outputs.hpp: RequestOutput and its pool |
Input metadata#
| Name, kind | Purpose | Stub, read more |
|---|---|---|
PrepareArgsclass | All inputs of the metadata builders. | deepinfer/_C/engine.pyi:80prepare_tensor_input_v4.cpp: the C++ input builder |
YocoPrefillTruncationModeenum | OFF = 0, MASK = 1, COMPACT = 2. | deepinfer/_C/engine.pyi:667prepare_tensor_input_v4.cpp: the C++ input builder |
prepare_prompt_v4function | Prefill metadata as a dict of tensors. | deepinfer/_C/engine.pyi:723prepare_tensor_input_v4.cpp: the C++ input builder |
prepare_decode_v4function | Decode metadata when ZOS is off. | deepinfer/_C/engine.pyi:719prepare_tensor_input_v4.cpp: the C++ input builder |
set_engram_lookup_tablefunction | Stores the Engram lookup table in a C++ static variable. | deepinfer/_C/engine.pyi:731prepare_tensor_input_v4.cpp: the C++ input builder |
Sampler inputs#
| Name, kind | Purpose | Stub, read more |
|---|---|---|
LogitsTransformInputsclass | Temperature, top-p and top-k tensors. | deepinfer/_C/engine.pyi:58engine/prepare_sample.cpp: the sampler inputs |
SamplingKernelInputsclass | The above, plus seeds and sample positions. | deepinfer/_C/engine.pyi:297engine/prepare_sample.cpp: the sampler inputs |
PromptLogprobInputsclass | Targets and settings for prompt logprobs. | deepinfer/_C/engine.pyi:165engine/prepare_sample.cpp: the sampler inputs |
PrefillSamplingInputsclass | Prompt logprobs and the rows where EOS is not allowed. | deepinfer/_C/engine.pyi:70engine/prepare_sample.cpp: the sampler inputs |
SpeculativeSamplingInputsclass | Draft rows, MTP slots, stop tokens. | deepinfer/_C/engine.pyi:654engine/prepare_sample.cpp: the sampler inputs |
DecodeSamplingInputsclass | First row and row count of each request. | deepinfer/_C/engine.pyi:26engine/prepare_sample.cpp: the sampler inputs |
DriverSamplingInputsclass | The inputs that only the driver rank needs. | deepinfer/_C/engine.pyi:35engine/prepare_sample.cpp: the sampler inputs |
SamplerInputsclass | The top object: row indices, the decode part, the driver part. | deepinfer/_C/engine.pyi:282engine/prepare_sample.cpp: the sampler inputs |
prepare_sampler_inputsfunction | Fills all of the above from the scheduled sequences. | deepinfer/_C/engine.pyi:725engine/prepare_sample.cpp: the sampler inputs |
Submodule cuda_utils (3 names)#
Device memory outside the PyTorch caching allocator. The NVIDIA binding is in csrc/cuda_utils.cpp:98-108. The Ascend build binds the same three names, with the same Python signatures, in csrc/asc_utils.cpp:108-113.
| Name, kind | Purpose | Stub, used by |
|---|---|---|
alloc_tensorfunction | Allocates a 1-D byte tensor with cuMemAlloc, so PyTorch does not count it. | deepinfer/_C/cuda_utils.pyi:7CacheEngine._alloc_gpu_uint8, for gpu_blocks and gpu_states (deepinfer/worker/cache_engine.py:910-916). See worker/cache_engine.py: CacheEngine and the cache layout. |
export_ipc_tensorfunction | Exports an alloc_tensor buffer as an IPC handle (bytes) for another process. | deepinfer/_C/cuda_utils.pyi:11CudaIPCWrapper.__init__ (deepinfer/utils.py:52-62) |
import_ipc_tensorfunction | Opens an IPC handle as a tensor in this process. | deepinfer/_C/cuda_utils.pyi:15CudaIPCWrapper.to_tensor (deepinfer/utils.py:64-69) |
In this release, only a test uses CudaIPCWrapper (tests/distributed/test_expert_shuffle.py:17, tests/distributed/test_expert_shuffle.py:89). The package itself does not call the two IPC functions.
Submodule graph_timer (3 names)#
Low-level handles for the graph timer, which measures regions inside a replayed CUDA graph. The binding is csrc/graph_timer/pybind.cpp:5-29. The Python side is CUDAGraphTimer in deepinfer/timer.py:78-217. CudaGraphRunner wraps each captured graph in a GraphHandle and registers it (deepinfer/worker/cuda_graph_runner.py:130-136).
| Name, kind, stub | Purpose and methods |
|---|---|
EventHandleclass deepinfer/_C/graph_timer.pyi:7 | A device event that a graph capture can record. Methods: record(stream, capture_external), synchronize() (releases the GIL, csrc/graph_timer/pybind.cpp:10), query(), elapsed_time(end) in ms, __eq__, __hash__. |
EventRecordNodeHandleclass deepinfer/_C/graph_timer.pyi:36 | One event-record node inside a captured graph. Methods: get_event(), set_event(event): swap the event that the node records. |
GraphHandleclass deepinfer/_C/graph_timer.pyi:45 | Wraps a captured torch.cuda.CUDAGraph. Methods: GraphHandle(graph), get_event_record_nodes(), __eq__, __hash__. |
Python files that import deepinfer._C#
| File | Imports | Line |
|---|---|---|
deepinfer/engine/llm_engine.py | 11 engine names: Scheduler, BlockManager, pools, build_request_outputs … | deepinfer/engine/llm_engine.py:19-31 |
deepinfer/sequence.py | sequence types | deepinfer/sequence.py:14 |
deepinfer/entrypoints/llm.py | RequestOutput | deepinfer/entrypoints/llm.py:15 |
deepinfer/model_executor/model_metadata_utils.py | input-metadata functions | deepinfer/model_executor/model_metadata_utils.py:6 |
deepinfer/model_executor/sampling_inputs.py | sampler-input classes | deepinfer/model_executor/sampling_inputs.py:18 |
deepinfer/model_executor/input_metadata/prepare_tensor_input_zos.py | PrepareArgs, prepare_decode_zos_inputs | deepinfer/model_executor/input_metadata/prepare_tensor_input_zos.py:7 |
deepinfer/model_executor/models/v4_modules/engram.py | encode_compressed | deepinfer/model_executor/models/v4_modules/engram.py:18 |
deepinfer/worker/base.py | clone_and_zero_states_and_blocks, set_engram_lookup_table | deepinfer/worker/base.py:22 |
deepinfer/worker/worker.py | engine names for the step | deepinfer/worker/worker.py:16 |
deepinfer/worker/model_runner.py | prepare_sampler_inputs, update_prompt_logprobs | deepinfer/worker/model_runner.py:15 |
deepinfer/worker/zos_status.py | SchedulerOutputs | deepinfer/worker/zos_status.py:5 |
deepinfer/worker/cache_engine.py | cuda_utils | deepinfer/worker/cache_engine.py:18 |
deepinfer/utils.py | cuda_utils (inside functions) | deepinfer/utils.py:54, deepinfer/utils.py:65 |
deepinfer/worker/cuda_graph_runner.py | graph_timer | deepinfer/worker/cuda_graph_runner.py:11 |
deepinfer/timer.py | graph_timer classes (lazy imports) | deepinfer/timer.py:16, deepinfer/timer.py:118, deepinfer/timer.py:162 |
Examples also import from it, for example RequestOutput in examples/offline_inference.py:61 and Sequence in examples/benchmark.py:270. A comment in sampling_inputs.py says that torch must load first, because the extension needs the libtorch shared libraries (deepinfer/model_executor/sampling_inputs.py:16).
Alphabetical index#
alloc_tensor (cuda_utils) ·
BlockManager (engine) ·
build_request_outputs (engine) ·
clone_and_zero_states_and_blocks (engine) ·
DecodeSamplingInputs (engine) ·
DriverSamplingInputs (engine) ·
encode_compressed (engine) ·
EventHandle (graph_timer) ·
EventRecordNodeHandle (graph_timer) ·
export_ipc_tensor (cuda_utils) ·
FlopsEstimator (engine) ·
get_finished_seqs_info (engine) ·
GraphHandle (graph_timer) ·
import_ipc_tensor (cuda_utils) ·
LogitsTransformInputs (engine) ·
PLACEHOLDER_REQUEST_ID (engine) ·
PrefillSamplingInputs (engine) ·
prepare_decode_v4 (engine) ·
prepare_decode_zos_inputs (engine) ·
prepare_prompt_v4 (engine) ·
prepare_sampler_inputs (engine) ·
prepare_zos_inputs (engine) ·
PrepareArgs (engine) ·
process_MTPInfo (engine) ·
PromptLogprobInputs (engine) ·
RequestOutput (engine) ·
RequestOutputPool (engine) ·
SamplerInputs (engine) ·
SamplingKernelInputs (engine) ·
Scheduler (engine) ·
SchedulerOutputs (engine) ·
Sequence (engine) ·
SequenceData (engine) ·
SequenceMetadata (engine) ·
SequenceMetadataPool (engine) ·
SequenceStatus (engine) ·
set_engram_lookup_table (engine) ·
SpeculativeSamplingInputs (engine) ·
update_prompt_logprobs (engine) ·
YocoPrefillTruncationMode (engine)