Skip to content

Commit

Permalink
Nano: add libjemalloc.dylib (intel#7392)
Browse files Browse the repository at this point in the history
  • Loading branch information
MeouSker77 authored Feb 2, 2023
1 parent 40e5024 commit 076ef50
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ nohup.out
*/.project
*/.settings/
*.so
*.so.*
*.dylib
__pycache__
*.egg-info
target
Expand Down
1 change: 1 addition & 0 deletions python/nano/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

lib_urls = [
"https://github.com/analytics-zoo/jemalloc/releases/download/v5.2.3/libjemalloc.so",
"https://github.com/analytics-zoo/jemalloc/releases/download/v5.2.3/libjemalloc.dylib",
"https://github.com/analytics-zoo/libjpeg-turbo/releases/download/v2.1.4/libturbojpeg.so.0.2.0",
"https://github.com/analytics-zoo/tcmalloc/releases/download/v1/libtcmalloc.so"
]
Expand Down
57 changes: 30 additions & 27 deletions python/nano/test/onnx/tf/test_inc_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ def test_model_quantize_onnx(self):
onnx_quantized_model = InferenceOptimizer.quantize(model,
accelerator='onnxruntime',
x=train_dataset,
thread_num=8)
thread_num=8,
accuracy_criterion={'relative': 0.99,
'higher_is_better': True})

y_hat = onnx_quantized_model(input_examples[:10])
assert y_hat.shape == (10, 10)

y_hat = onnx_quantized_model.predict(input_examples, batch_size=5)
assert y_hat.shape == (100, 10)

preds = model.predict(input_examples)
onnx_preds = onnx_quantized_model.predict(input_examples)
preds = model.predict(input_examples, batch_size=5)
onnx_preds = onnx_quantized_model.predict(input_examples, batch_size=5)
np.testing.assert_allclose(preds, onnx_preds, rtol=5e-2)

def test_model_quantize_onnx_without_dataset(self):
Expand All @@ -61,51 +63,53 @@ def test_model_quantize_onnx_without_dataset(self):
accelerator='onnxruntime',
x=input_examples,
y=input_features,
thread_num=8)
thread_num=8,
accuracy_criterion={'relative': 0.99,
'higher_is_better': True})

preds = model.predict(input_examples)
onnx_preds = onnx_quantized_model.predict(input_examples)
preds = model.predict(input_examples, batch_size=5)
onnx_preds = onnx_quantized_model.predict(input_examples, batch_size=5)
np.testing.assert_allclose(preds, onnx_preds, rtol=5e-2)

def test_model_quantize_onnx_with_only_x(self):
model = EfficientNetB0(weights=None, input_shape=[224, 224, 3], classes=10)
model = Model(inputs=model.inputs, outputs=model.outputs)
input_examples = np.random.random((100, 224, 224, 3))
preds = model.predict(input_examples, batch_size=5)

# quantize a Keras model based on numpy array
onnx_quantized_model = InferenceOptimizer.quantize(model,
accelerator='onnxruntime',
x=input_examples,
y=None,
thread_num=8)

preds = model.predict(input_examples)
onnx_preds = onnx_quantized_model.predict(input_examples)
thread_num=8,
accuracy_criterion={'relative': 0.99,
'higher_is_better': True})
onnx_preds = onnx_quantized_model.predict(input_examples, batch_size=5)
np.testing.assert_allclose(preds, onnx_preds, rtol=5e-2)

# quantize a Keras model based on dataset
input_examples = np.random.random((100, 224, 224, 3))
train_dataset = tf.data.Dataset.from_tensor_slices(input_examples)
onnx_quantized_model = InferenceOptimizer.quantize(model,
accelerator='onnxruntime',
x=train_dataset,
y=None,
thread_num=8)

preds = model.predict(input_examples)
onnx_preds = onnx_quantized_model.predict(input_examples)
thread_num=8,
accuracy_criterion={'relative': 0.99,
'higher_is_better': True})
onnx_preds = onnx_quantized_model.predict(input_examples, batch_size=5)
np.testing.assert_allclose(preds, onnx_preds, rtol=5e-2)

# quantize a Keras model based on tf tensor
input_examples = np.random.random((100, 224, 224, 3))
input_examples = tf.convert_to_tensor(input_examples)
input_tensor = tf.convert_to_tensor(input_examples)
onnx_quantized_model = InferenceOptimizer.quantize(model,
accelerator='onnxruntime',
x=input_examples,
x=input_tensor,
y=None,
thread_num=8)

preds = model.predict(input_examples)
onnx_preds = onnx_quantized_model.predict(input_examples)
thread_num=8,
accuracy_criterion={'relative': 0.99,
'higher_is_better': True})
onnx_preds = onnx_quantized_model.predict(input_examples, batch_size=5)
np.testing.assert_allclose(preds, onnx_preds, rtol=5e-2)

def test_model_quantize_onnx_save_load(self):
Expand All @@ -121,7 +125,10 @@ def test_model_quantize_onnx_save_load(self):
onnx_model = InferenceOptimizer.quantize(model,
accelerator='onnxruntime',
x=train_dataset,
thread_num=1)
thread_num=1,
accuracy_criterion={'relative': 0.99,
'higher_is_better': True})
preds1 = onnx_model.predict(input_examples, batch_size=5)

with tempfile.TemporaryDirectory() as tmp_dir_name:
onnx_model._save(tmp_dir_name)
Expand All @@ -130,9 +137,7 @@ def test_model_quantize_onnx_save_load(self):
assert new_onnx_model.session_options.intra_op_num_threads == 1
assert new_onnx_model.session_options.inter_op_num_threads == 1

preds1 = onnx_model.predict(input_examples, batch_size=5)
preds2 = new_onnx_model.predict(input_examples, batch_size=5)

np.testing.assert_almost_equal(preds1, preds2, decimal=5)

with tempfile.TemporaryDirectory() as tmp_dir_name:
Expand All @@ -142,7 +147,5 @@ def test_model_quantize_onnx_save_load(self):
assert new_onnx_model.session_options.intra_op_num_threads == 1
assert new_onnx_model.session_options.inter_op_num_threads == 1

preds1 = onnx_model.predict(input_examples, batch_size=5)
preds2 = new_onnx_model.predict(input_examples, batch_size=5)

np.testing.assert_almost_equal(preds1, preds2, decimal=5)
4 changes: 2 additions & 2 deletions python/nano/test/onnx/tf/test_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def test_model_trace_onnx(self):
y_hat = onnx_model.predict(input_examples, batch_size=5)
assert y_hat.shape == (100, 10)

preds = model.predict(input_examples)
onnx_preds = onnx_model.predict(input_examples)
preds = model.predict(input_examples, batch_size=5)
onnx_preds = onnx_model.predict(input_examples, batch_size=5)
np.testing.assert_allclose(preds, onnx_preds, rtol=1e-5)

def test_tf_onnx_save_load(self):
Expand Down
3 changes: 3 additions & 0 deletions python/nano/test/run-nano-tf-onnx-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export INC_TF_NANO_TEST_DIR=${ANALYTICS_ZOO_ROOT}/python/nano/test/onnx/tf
set -e
echo "# Start testing"
start=$(date "+%s")
# It seems nano's default `MALLOC_CONF` will cause higher memory usage,
# and cause OOM (Killed) in git action
unset MALLOC_CONF
python -m pytest -s ${INC_TF_NANO_TEST_DIR}

now=$(date "+%s")
Expand Down

0 comments on commit 076ef50

Please sign in to comment.