Skip to content

Export

Quantize and export PyTorch models for DeepGate edge deployment.

Functions:

Name Description
trace

Trace model into the graph that dg.export lowers.

export

Lower a quantized model to a schema.json dict for deployment.

dg.trace

trace(
    model: Module,
    example_inputs: Tensor | tuple[Tensor, ...],
    *,
    dynamic_batch: bool = True
) -> torch.fx.GraphModule

Trace model into the graph that dg.export lowers.

This is the whole export path for a logic model, which has no quantization:

schema = dg.export(dg.trace(model, (example,)))

A quantized model is traced by enable_quantization instead, so you do not call this yourself.

Parameters:

Name Type Description Default
model Module

The model to trace. Tracing runs on the model's own device, and the traced graph comes back on that same device.

required
example_inputs Tensor | tuple[Tensor, ...]

Example inputs to trace with. Pass a batch of at least 2 samples when dynamic_batch is set.

required
dynamic_batch bool

Let the traced graph accept any batch size. A batch of 1 would otherwise be baked in as a fixed size.

True

Returns:

Type Description
GraphModule

The traced graph, ready for dg.export.

dg.export

export(
    converted: GraphModule,
    *,
    input_boundary: str = DType.FLOAT32,
    output_boundary: str = DType.FLOAT32,
    preprocess: Preprocessor | None = None
) -> dict[str, Any]

Lower a quantized model to a schema.json dict for deployment.

Write the returned dict out with json.dump and upload it to the DeepGate platform to compile or benchmark. Any operation that cannot be lowered raises an error rather than being silently dropped.

Parameters:

Name Type Description Default
converted GraphModule

The quantized graph from freeze_quantization or post_training_quantize, or a traced logic model from trace.

required
input_boundary str

Dtype the deployed model accepts, "float32" (the default) or "int8". Use "int8" to hand the model already-quantized input, which moves the input quantize step onto the host.

FLOAT32
output_boundary str

Dtype the deployed model returns, "float32" (the default) or "int8". Use "int8" to dequantize on the host.

FLOAT32
preprocess Preprocessor | None

An audio or image pipeline to compile into the model, so deployed inference runs the same preprocessing you trained on. Requires the default float32 input_boundary.

None

Returns:

Type Description
dict[str, Any]

The schema dict.