Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Memory-mapped MLModel #275

Merged
merged 2 commits into from
Feb 8, 2024

Conversation

OctoberChang
Copy link
Contributor

Issue #, if available:

Description of changes:
Implement Memory-mapped MLModel class for both C and Python Interface.

  • significantly reduce loading time
  • significantly reduce real-time inference latency

Usage
User needs to have a MLModel saved on disk (in original .npz format),
and manually compile into mmap format by calling compile_mmap_model:

from pecos.xmc import MLModel
npz_model_path = f"/path/to/xlinear/pecos-models/"
mmap_model_path = f"/path/to/xlinear/mmap-models/"
MLModel.compile_mmap_model(npz_model_folder, mmap_model_folder)

Then user can load the memory-mapped model and do inference:

from pecos.utils import smat_util
from pecos.xmc import MLModel
Xt = smat_util.load_matrix(f"/test/data/validation/X.npz")
model = MLModel.load(mmap_model_path, lazy_load=True)
Yp = model.predict(Xt)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@OctoberChang OctoberChang force-pushed the mmap-mlmodel branch 2 times, most recently from 37437c5 to 5c06b9e Compare January 8, 2024 21:25
@@ -1168,3 +1168,36 @@ def test_mmap(tmpdir):
py_pred = py_model.predict(Xt, **kwargs).todense()
mmap_pred = mmap_model.predict(Xt, **kwargs).todense()
assert mmap_pred == approx(py_pred, abs=1e-6), f"post_processor:{pp}"


def test_mmap_mlmodel(tmpdir):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test should be put in https://github.com/amzn/pecos/blob/mainline/test/pecos/xmc/test_xmc.py since the functionalities added are for the xmc/base.py class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. See latest Revision.

// ==== C Interface of XMC Models ====
// ==== C Interface of MLModels ====
// Only implemented for w_matrix_t = pecos::csc_t
//typedef pecos::bin_search_chunked_matrix_t MLMODEL_MAT_T;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this unused comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. See latest Revision.

@OctoberChang OctoberChang merged commit c181496 into amzn:mainline Feb 8, 2024
25 checks passed
@OctoberChang OctoberChang deleted the mmap-mlmodel branch February 8, 2024 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants