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

libpymo import failed #3589

Open
1826133674 opened this issue Dec 11, 2024 · 4 comments
Open

libpymo import failed #3589

1826133674 opened this issue Dec 11, 2024 · 4 comments
Assignees
Labels
aimet-onnx New feature or bug fix for AIMET ONNX help wanted Extra attention is needed

Comments

@1826133674
Copy link

When I tried to run the example code for the ONNX quantization model, I encountered the following error:

  File "/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_onnx/batch_norm_fold.py", line 221, in fold_all_batch_norms_to_weight
    bn_layer = _fold_to_weight(model, conv, bn, True)
  File "/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_onnx/batch_norm_fold.py", line 274, in _fold_to_weight
    bn_param = get_bn_params(model, bn, channels)
  File "/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_onnx/batch_norm_fold.py", line 355, in get_bn_params
    bn_params = libpymo.BNParams()
  File "/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_common/py_libpymo.py", line 101, in __init__
    raise RuntimeError(f"Unable to initialize class {class_name}: {_error_message()}")
RuntimeError: Unable to initialize class BNParams: libpymo import failed with the following error:

liblapacke.so.3: cannot open shared object file: No such file or directory

I have tried to add the path in the python file as follows, but it didn't work!

import sys
sys.path.append('/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_common')

I also tried to export the path in the terminal, it didn't work either!

export LD_LIBRARY_PATH=/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_common/x86_64-linux-gnu/:/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_common:$LD_LIBRARY_PATH

export PYTHONPATH=/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_common/x86_64-linux-gnu:/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_common:$PYTHONPATH

source /anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_onnx/bin/envsetup.sh
@1826133674
Copy link
Author

The code is as follows:

import os
os.environ['CUDA_VISIABLE_DEVICES'] = '3'
import json
import argparse
from argparse import Namespace
import logging
import time
import sys
sys.path.append('/anaconda3/envs/aimet/lib/python3.10/site-packages/aimet_common')
import libpymo

import torch
import onnxruntime as ort
from torchvision import models
from torchvision.models import resnet18
import onnx
from onnxsim import simplify
from Examples.common import image_net_config
from Examples.onnx.utils.image_net_evaluator import ImageNetEvaluator
from Examples.torch.utils.image_net_data_loader import ImageNetDataLoader

logger = logging.getLogger('AIMet_Quantization_Demo')
AIMET_QUANT_OUTPUT_DIR_DATETIME_FMT = "%4d-%02d-%02d_%02d:%02d:%02d"
DATASET_DIR = '/dataset/ILSVR2012_val' 

class ImageNetDataPipeline:

    @staticmethod
    def get_val_dataloader() -> torch.utils.data.DataLoader:
        """
        Instantiates a validation dataloader for ImageNet dataset and returns it
        """
        data_loader = ImageNetDataLoader(DATASET_DIR,
                                         image_size=image_net_config.dataset['image_size'],
                                         batch_size=image_net_config.evaluation['batch_size'],
                                         is_training=False,
                                         num_workers=image_net_config.evaluation['num_workers']).data_loader
        return data_loader

    @staticmethod
    def evaluate(sess: ort.InferenceSession) -> float:
        """
        Given a torch model, evaluates its Top-1 accuracy on the dataset
        :param sess: the model to evaluate
        """
        evaluator = ImageNetEvaluator(DATASET_DIR, image_size=image_net_config.dataset['image_size'],
                                      batch_size=image_net_config.evaluation['batch_size'],
                                      num_workers=image_net_config.evaluation['num_workers'])

        return evaluator.evaluate(sess, iterations=None)


def pass_calibration_data(session, samples):
    data_loader = ImageNetDataPipeline.get_val_dataloader()
    batch_size = data_loader.batch_size
    input_name = sess.get_inputs()[0].name

    batch_cntr = 0
    for input_data, target_data in data_loader:

        inputs_batch = input_data.numpy()
        session.run(None, {input_name : inputs_batch})

        batch_cntr += 1
        if (batch_cntr * batch_size) > samples:
            break

if __name__=="__main__":
    input_shape = (1, 3, 224, 224)    # Shape for each ImageNet sample is (3 channels) x (224 height) x (224 width)
    dummy_input = torch.randn(input_shape)
    filename = "./models/resnet_18/resnet18.onnx"

    # Load a pretrained ResNet-18 model in torch
    pt_model = resnet18(weights=models.ResNet18_Weights.DEFAULT)

    # Export the torch model to onnx
    torch.onnx.export(pt_model.eval(),
                    dummy_input,
                    filename,
                    training=torch.onnx.TrainingMode.EVAL,
                    export_params=True,
                    do_constant_folding=False,
                    input_names=['input'],
                    output_names=['output'],
                    dynamic_axes={
                        'input' : {0 : 'batch_size'},
                        'output' : {0 : 'batch_size'},
                    }
                    )

    model = onnx.load_model(filename)
    # model, _ = simplify(model)
    # assert False

    try:
        model, _ = simplify(model)
    except:
        print('ONNX Simplifier failed. Proceeding with unsimplified model')

    if 'CUDAExecutionProvider' in ort.get_available_providers():
        providers = [('CUDAExecutionProvider', {'cudnn_conv_algo_search': 'DEFAULT'}), 'CPUExecutionProvider']
        use_cuda = True
    else:
        providers = ['CPUExecutionProvider']
        use_cuda = False

    # sess = ort.InferenceSession(model.SerializeToString(), providers=providers)
    # accuracy = ImageNetDataPipeline.evaluate(sess)
    # print(f"float model acc: {accuracy}%")
    from aimet_onnx.batch_norm_fold import fold_all_batch_norms_to_weight

    _ = fold_all_batch_norms_to_weight(model)
    from aimet_common.defs import QuantScheme
    from aimet_onnx.quantsim import QuantizationSimModel

    sim = QuantizationSimModel(model=model,
                            quant_scheme=QuantScheme.post_training_tf_enhanced,
                            default_activation_bw=8,
                            default_param_bw=8,
                            use_cuda=use_cuda)
    sim.compute_encodings(forward_pass_callback=pass_calibration_data,
                      forward_pass_callback_args=1000)
    accuracy = ImageNetDataPipeline.evaluate(sim.session)
    print(accuracy)
    print(f"quant model acc: {accuracy}%")
    sim.export(path='./output/', filename_prefix='resnet18_ptq')

@quic-geunlee quic-geunlee added help wanted Extra attention is needed aimet-onnx New feature or bug fix for AIMET ONNX labels Dec 11, 2024
@quic-mtuttle
Copy link
Contributor

Hi @1826133674, it looks like libpymo is failing to find liblapacke. Could you try these steps to install liblapacke in your environment?

sudo apt-get update
sudo apt-get install liblapacke

@1826133674
Copy link
Author

Hi @1826133674, it looks like libpymo is failing to find liblapacke. Could you try these steps to install liblapacke in your environment?

sudo apt-get update
sudo apt-get install liblapacke

I have let the root user install the liblapacke, but it still didn't work! I'm not the root user, how could I make it!

@quic-mtuttle
Copy link
Contributor

Hi @1826133674, can you check that you have read permissions on the liblapacke.so.3 library, and that the directory is part of your LD_LIBRARY_PATH?

If you don't have root access, you could also try creating a conda virtual environment and installing liblapacke within that, or just installing liblapacke elsewhere on your system and setting your LD_LIBRARY_PATH to include that directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aimet-onnx New feature or bug fix for AIMET ONNX help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants