DeepInfer Explained

C++ engine API (deepinfer._C)

IntermediateReferenceabout 8 min read

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#

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, kindPurposeStub, read more
Scheduler
class
The C++ scheduler: queues, schedule(), process_outputs().deepinfer/_C/engine.pyi:309
csrc/engine/scheduler.hpp: the C++ Scheduler
SchedulerOutputs
class
The result of one schedule() call (SchedulerOutputs).deepinfer/_C/engine.pyi:371
csrc/engine/scheduler.hpp: the C++ Scheduler
BlockManager
class
Free list of KV blocks, or of state slots.deepinfer/_C/engine.pyi:9
csrc/engine/block_manager.hpp: BlockManager and the free list
SequenceMetadataPool
class
Reuses SequenceMetadata objects from step to step.deepinfer/_C/engine.pyi:588
engine.pyi
FlopsEstimator
class
Estimates the FLOPs of a step, for the statistics.deepinfer/_C/engine.pyi:51
engine/stats_logger.py: step statistics, MFU and moe_lb
process_MTPInfo
function
Copies new draft tokens and their confidence into the sequences.deepinfer/_C/engine.pyi:729
csrc/engine/scheduler.hpp: the C++ Scheduler
clone_and_zero_states_and_blocks
function
Device copies of state slots and blocks for forked sequences.deepinfer/_C/engine.pyi:709
worker/base.py: WorkerBase, the resource layer of a worker
get_finished_seqs_info
function
Counts and lengths of finished sequences, for the statistics.deepinfer/_C/engine.pyi:717
csrc/engine/scheduler.hpp: the C++ Scheduler
update_prompt_logprobs
function
Appends the prompt logprobs of a prefill chunk to each sequence.deepinfer/_C/engine.pyi:733
worker/model_runner.py: the ModelRunner class

Sequences and ZOS inputs#

Name, kindPurposeStub, read more
Sequence
class
The engine object of one request (sequence).deepinfer/_C/engine.pyi:421
csrc/engine/sequence.hpp: Sequence and SequenceMetadata
SequenceData
class
The token list of a sequence and its prompt length.deepinfer/_C/engine.pyi:528
csrc/engine/sequence.hpp: Sequence and SequenceMetadata
SequenceMetadata
class
The view of one scheduled sequence for one step (SequenceMetadata).deepinfer/_C/engine.pyi:537
csrc/engine/sequence.hpp: Sequence and SequenceMetadata
SequenceStatus
enum
WAITING = 0 to FINISHED_VE_FAILED = 9.deepinfer/_C/engine.pyi:593
csrc/engine/sequence.hpp: Sequence and SequenceMetadata
PLACEHOLDER_REQUEST_ID
constant
The string "placeholder": the request id of a dummy sequence.deepinfer/_C/engine.pyi:735
csrc/engine/sequence.hpp: Sequence and SequenceMetadata
encode_compressed
function
Maps token ids through the Engram lookup table.deepinfer/_C/engine.pyi:713
v4_modules/engram.py and engram_metadata.py: the Engram runtime
prepare_zos_inputs
function
ZOS: inputs of a step that depend on the step before.deepinfer/_C/engine.pyi:727
csrc/engine/zos.cpp: the C++ helpers of zero-overhead scheduling
prepare_decode_zos_inputs
function
ZOS decode: packs block tables and token inputs into a 13-tuple.deepinfer/_C/engine.pyi:721
csrc/engine/zos.cpp: the C++ helpers of zero-overhead scheduling

Outputs#

Name, kindPurposeStub, read more
RequestOutput
class
One new token of one request (RequestOutput).deepinfer/_C/engine.pyi:180
csrc/engine/outputs.hpp: RequestOutput and its pool
RequestOutputPool
class
10000 reusable output slots (RequestOutputPool).deepinfer/_C/engine.pyi:277
csrc/engine/outputs.hpp: RequestOutput and its pool
build_request_outputs
function
Makes the outputs of one step. Returns uint64 handles.deepinfer/_C/engine.pyi:707
csrc/engine/outputs.hpp: RequestOutput and its pool

Input metadata#

Name, kindPurposeStub, read more
PrepareArgs
class
All inputs of the metadata builders.deepinfer/_C/engine.pyi:80
prepare_tensor_input_v4.cpp: the C++ input builder
YocoPrefillTruncationMode
enum
OFF = 0, MASK = 1, COMPACT = 2.deepinfer/_C/engine.pyi:667
prepare_tensor_input_v4.cpp: the C++ input builder
prepare_prompt_v4
function
Prefill metadata as a dict of tensors.deepinfer/_C/engine.pyi:723
prepare_tensor_input_v4.cpp: the C++ input builder
prepare_decode_v4
function
Decode metadata when ZOS is off.deepinfer/_C/engine.pyi:719
prepare_tensor_input_v4.cpp: the C++ input builder
set_engram_lookup_table
function
Stores the Engram lookup table in a C++ static variable.deepinfer/_C/engine.pyi:731
prepare_tensor_input_v4.cpp: the C++ input builder

Sampler inputs#

Name, kindPurposeStub, read more
LogitsTransformInputs
class
Temperature, top-p and top-k tensors.deepinfer/_C/engine.pyi:58
engine/prepare_sample.cpp: the sampler inputs
SamplingKernelInputs
class
The above, plus seeds and sample positions.deepinfer/_C/engine.pyi:297
engine/prepare_sample.cpp: the sampler inputs
PromptLogprobInputs
class
Targets and settings for prompt logprobs.deepinfer/_C/engine.pyi:165
engine/prepare_sample.cpp: the sampler inputs
PrefillSamplingInputs
class
Prompt logprobs and the rows where EOS is not allowed.deepinfer/_C/engine.pyi:70
engine/prepare_sample.cpp: the sampler inputs
SpeculativeSamplingInputs
class
Draft rows, MTP slots, stop tokens.deepinfer/_C/engine.pyi:654
engine/prepare_sample.cpp: the sampler inputs
DecodeSamplingInputs
class
First row and row count of each request.deepinfer/_C/engine.pyi:26
engine/prepare_sample.cpp: the sampler inputs
DriverSamplingInputs
class
The inputs that only the driver rank needs.deepinfer/_C/engine.pyi:35
engine/prepare_sample.cpp: the sampler inputs
SamplerInputs
class
The top object: row indices, the decode part, the driver part.deepinfer/_C/engine.pyi:282
engine/prepare_sample.cpp: the sampler inputs
prepare_sampler_inputs
function
Fills all of the above from the scheduled sequences.deepinfer/_C/engine.pyi:725
engine/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, kindPurposeStub, used by
alloc_tensor
function
Allocates a 1-D byte tensor with cuMemAlloc, so PyTorch does not count it.deepinfer/_C/cuda_utils.pyi:7
CacheEngine._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_tensor
function
Exports an alloc_tensor buffer as an IPC handle (bytes) for another process.deepinfer/_C/cuda_utils.pyi:11
CudaIPCWrapper.__init__ (deepinfer/utils.py:52-62)
import_ipc_tensor
function
Opens an IPC handle as a tensor in this process.deepinfer/_C/cuda_utils.pyi:15
CudaIPCWrapper.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, stubPurpose and methods
EventHandle
class
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__.
EventRecordNodeHandle
class
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.
GraphHandle
class
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#

FileImportsLine
deepinfer/engine/llm_engine.py11 engine names: Scheduler, BlockManager, pools, build_request_outputs …deepinfer/engine/llm_engine.py:19-31
deepinfer/sequence.pysequence typesdeepinfer/sequence.py:14
deepinfer/entrypoints/llm.pyRequestOutputdeepinfer/entrypoints/llm.py:15
deepinfer/model_executor/model_metadata_utils.pyinput-metadata functionsdeepinfer/model_executor/model_metadata_utils.py:6
deepinfer/model_executor/sampling_inputs.pysampler-input classesdeepinfer/model_executor/sampling_inputs.py:18
deepinfer/model_executor/input_metadata/prepare_tensor_input_zos.pyPrepareArgs, prepare_decode_zos_inputsdeepinfer/model_executor/input_metadata/prepare_tensor_input_zos.py:7
deepinfer/model_executor/models/v4_modules/engram.pyencode_compresseddeepinfer/model_executor/models/v4_modules/engram.py:18
deepinfer/worker/base.pyclone_and_zero_states_and_blocks, set_engram_lookup_tabledeepinfer/worker/base.py:22
deepinfer/worker/worker.pyengine names for the stepdeepinfer/worker/worker.py:16
deepinfer/worker/model_runner.pyprepare_sampler_inputs, update_prompt_logprobsdeepinfer/worker/model_runner.py:15
deepinfer/worker/zos_status.pySchedulerOutputsdeepinfer/worker/zos_status.py:5
deepinfer/worker/cache_engine.pycuda_utilsdeepinfer/worker/cache_engine.py:18
deepinfer/utils.pycuda_utils (inside functions)deepinfer/utils.py:54, deepinfer/utils.py:65
deepinfer/worker/cuda_graph_runner.pygraph_timerdeepinfer/worker/cuda_graph_runner.py:11
deepinfer/timer.pygraph_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)