onnx / onnxmltools

ONNXMLTools enables conversion of models to ONNX
https://onnx.ai
Apache License 2.0
1.02k stars 183 forks source link

In-memory, the ONNX XGBoost artifact blows up in size #584

Open salmatfq opened 2 years ago

salmatfq commented 2 years ago

Describe the issue In trying to convert an XGBoost model artifact to ONNX and tracking its memory footprint, the size blows up. Profiling the memory usage, loading the resulting ONNX artifact is up to 4x bigger than the artifact's on-disk size. For example for an ONNX artifact of size 8MB, the in-memory usage while loading (onnx.load(model)) is almost 30MB. For reference, the conversion code used is pretty standard:

import xgboost as xgb 
from onnxmltools.convert import convert_xgboost 
from skl2onnx.common.data_types import FloatTensorType

model = xgb.Booster()
model.load_model(f'{destination}/xgboost-model')
fts_num = 3000

initial_types = [('feature_input', FloatTensorType(['batch_size', fts_num]))]
onx = convert_xgboost(model, initial_types=initial_types)
with open(f"{destination}/{file_name}.onnx", "wb") as fp:
    fp.write(onx.SerializeToString())

Urgency ASAP

Env info Python: 3.8.0 onnxmltools version: 1.11.0 xgboost version: 1.3.1

Question Is this behavior expected? Shouldn't the loaded memory be around the same as the model size on disk? How can we reduce the footprint?

xadupre commented 1 year ago

How many features do you use? onnxruntime does not support sparse tensor for tree ensemble right now, only dense. That might explain the memory explosion.

salmatfq commented 1 year ago

Thanks @xadupre. 2500-3000 features across the different experiments.

xadupre commented 1 year ago

I guess that's the reason. Is sparse needed in your case or can you do without?

salmatfq commented 1 year ago

I guess that's the reason. Is sparse needed in your case or can you do without?

I will look into this and get back to you with an update. Thanks!