I have an RF model that I am trying to load faster during deployment. The first time it was created and saved as a compressed joblib file. But the load time is too high for my need. Here's a snippet of my attempt to benchmark what I have:
# load the base file:
print("Loading compressed joblib...")
MODEL = f"{base_path}/data/model_files/{dataset}_RF_{C}.compressed"
tic = time.perf_counter()
with open(MODEL, "rb") as f:
model = joblib.load(f)
mem = sys.getsizeof(model) / 1024 / 1024
toc = time.perf_counter()
load_time = toc - tic
size = os.path.getsize(MODEL) / 1024 / 1024
print(f"file on disk: {size:0.2f} MB\nfile on RAM: {mem:0.2f} MB\nload time: {load_time:0.3f} seconds\n")
Output:
Loading compressed joblib...
file on disk: 52.22 MB
file on RAM: 0.00 MB
load time: 9.510 seconds
I am unsure why the file size loaded in RAM is not shown. Please help with that as well if you know.
Uncompressed joblib:
# create uncompressed joblib, save it, load it, benchmark
print("Saving uncompressed joblib...")
MODEL = f"{base_path}/data/model_files/{dataset}_RF_{C}.uncompressed.joblib"
tic = time.perf_counter()
with open(MODEL, "wb") as f:
joblib.dump(model, f, compress=False)
toc = time.perf_counter()
save_time = toc - tic
size = os.path.getsize(MODEL) / 1024 / 1024
print(f"file on disk: {size:0.2f} MB\nsave time: {save_time:0.3f} seconds\n")
del model
print("Loading uncompressed joblib...")
tic = time.perf_counter()
with open(MODEL, "rb") as f:
model = joblib.load(f)
mem = sys.getsizeof(model) / 1024 / 1024
toc = time.perf_counter()
load_time = toc - tic
size = os.path.getsize(MODEL) / 1024 / 1024
print(f"file on disk: {size:0.2f} MB\nfile on RAM: {mem:0.2f} MB\nload time: {load_time:0.3f} seconds")
Output:
Saving uncompressed joblib...
file on disk: 4376.51 MB
save time: 8.718 seconds
Loading uncompressed joblib...
file on disk: 4376.51 MB
file on RAM: 0.00 MB
load time: 7.645 seconds
As it can be seen, by trading off 50MB compression to 4.3GB file size, I am not gaining much on load time.