--- library_name: pytorch license: apache-2.0 pipeline_tag: text-generation tags: - llm - generative_ai - quantized - android --- ![](https://qaihub-public-assets.s3.us-west-2.amazonaws.com/qai-hub-models/models/mistral_7b_instruct_v0_3_quantized/web-assets/model_demo.png) # Mistral-7B-Instruct-v0_3: Optimized for Mobile Deployment ## State-of-the-art large language model useful on a variety of language understanding and generation tasks The Mistral-7B-Instruct-v0.3 Large Language Model (LLM) is an instruct fine-tuned version of the Mistral-7B-v0.3. This model is an implementation of Mistral-7B-Instruct-v0_3 found [here]({source_repo}). This repository provides scripts to run Mistral-7B-Instruct-v0_3 on Qualcomm® devices. More details on model performance across various devices, can be found [here](https://aihub.qualcomm.com/models/mistral_7b_instruct_v0_3_quantized). ### Model Details - **Model Type:** Text generation - **Model Stats:** - Number of parameters: 7.3B - Precision: w8a16 - Num of key-value heads: 8 - Information about the model: ['Prompt Processor and Token Generator are split into 4 parts each.', 'Each corresponding Prompt Processor and Token Generator share weights.'] - Max context length: 4096 - Prompt processor model size: 4.17 GB - Prompt processor input: 128 tokens + KVCache initialized with pad token - Prompt processor output: 128 output tokens + KVCache for token generator - Token generator model size: 4.17 GB - Token generator input: 1 input token + past KVCache - Token generator output: 1 output token + KVCache for next iteration - Decoding length: 4096 - Use: Initiate conversation with prompt-processor and then token generator for subsequent iterations. | Model | Device | Chipset | Target Runtime | Response Rate (tokens per second) | Time To First Token (range, seconds) | Tiny MMLU |---|---|---|---|---|---|---| | Mistral-7B-Instruct-v0_3 | Snapdragon 8 Elite QRD | Snapdragon® 8 Elite | QNN | 10.73 | 0.18 - 5.79 | 58.85% | Use Export Script | ## Deploying Mistral 7B Instruct v3.0 on-device Please follow [this tutorial](https://github.com/quic/ai-hub-apps/tree/main/tutorials/llama) to compile QNN binaries and generate bundle assets to run [ChatApp on Windows](https://github.com/quic/ai-hub-apps/tree/main/apps/windows/cpp/ChatApp) and on Android powered by QNN-Genie. ## Installation This model can be installed as a Python package via pip. ```bash pip install qai-hub-models ``` ## Configure Qualcomm® AI Hub to run this model on a cloud-hosted device Sign-in to [Qualcomm® AI Hub](https://app.aihub.qualcomm.com/) with your Qualcomm® ID. Once signed in navigate to `Account -> Settings -> API Token`. With this API token, you can configure your client to run models on the cloud hosted devices. ```bash qai-hub configure --api_token API_TOKEN ``` Navigate to [docs](https://app.aihub.qualcomm.com/docs/) for more information. ## Demo on-device The package contains a simple end-to-end demo that downloads pre-trained weights and runs this model on a sample input. ```bash python -m qai_hub_models.models.mistral_7b_instruct_v0_3_quantized.demo ``` The above demo runs a reference implementation of pre-processing, model inference, and post processing. **NOTE**: If you want running in a Jupyter Notebook or Google Colab like environment, please add the following to your cell (instead of the above). ``` %run -m qai_hub_models.models.mistral_7b_instruct_v0_3_quantized.demo ``` ### Run model on a cloud-hosted device In addition to the demo, you can also run the model on a cloud-hosted Qualcomm® device. This script does the following: * Performance check on-device on a cloud-hosted device * Downloads compiled assets that can be deployed on-device for Android. * Accuracy check between PyTorch and on-device outputs. ```bash python -m qai_hub_models.models.mistral_7b_instruct_v0_3_quantized.export ``` ``` Profiling Results ------------------------------------------------------------ Device : Snapdragon 8 Elite QRD (15) Runtime : QNN Response Rate (Tokens/Second): 10.73 Time to First Token (Seconds): (0.18, 5.79) ``` ## How does this work? This [export script](https://aihub.qualcomm.com/models/mistral_7b_instruct_v0_3_quantized/qai_hub_models/models/Mistral-7B-Instruct-v0_3/export.py) leverages [Qualcomm® AI Hub](https://aihub.qualcomm.com/) to optimize, validate, and deploy this model on-device. Lets go through each step below in detail: Step 1: **Upload compiled model** Upload compiled models from `qai_hub_models.models.mistral_7b_instruct_v0_3_quantized` on hub. ```python import torch import qai_hub as hub from qai_hub_models.models.mistral_7b_instruct_v0_3_quantized import Model # Load the model model = Model.from_precompiled() model_promptprocessor_part1 = hub.upload_model(model.prompt_processor_part1.get_target_model_path()) model_promptprocessor_part2 = hub.upload_model(model.prompt_processor_part2.get_target_model_path()) model_promptprocessor_part3 = hub.upload_model(model.prompt_processor_part3.get_target_model_path()) model_promptprocessor_part4 = hub.upload_model(model.prompt_processor_part4.get_target_model_path()) model_tokengenerator_part1 = hub.upload_model(model.token_generator_part1.get_target_model_path()) model_tokengenerator_part2 = hub.upload_model(model.token_generator_part2.get_target_model_path()) model_tokengenerator_part3 = hub.upload_model(model.token_generator_part3.get_target_model_path()) model_tokengenerator_part4 = hub.upload_model(model.token_generator_part4.get_target_model_path()) ``` Step 2: **Performance profiling on cloud-hosted device** After uploading compiled models from step 1. Models can be profiled model on-device using the `target_model`. Note that this scripts runs the model on a device automatically provisioned in the cloud. Once the job is submitted, you can navigate to a provided job URL to view a variety of on-device performance metrics. ```python # Device device = hub.Device("Samsung Galaxy S23") profile_job_promptprocessor_part1 = hub.submit_profile_job( model=model_promptprocessor_part1, device=device, ) profile_job_promptprocessor_part2 = hub.submit_profile_job( model=model_promptprocessor_part2, device=device, ) profile_job_promptprocessor_part3 = hub.submit_profile_job( model=model_promptprocessor_part3, device=device, ) profile_job_promptprocessor_part4 = hub.submit_profile_job( model=model_promptprocessor_part4, device=device, ) profile_job_tokengenerator_part1 = hub.submit_profile_job( model=model_tokengenerator_part1, device=device, ) profile_job_tokengenerator_part2 = hub.submit_profile_job( model=model_tokengenerator_part2, device=device, ) profile_job_tokengenerator_part3 = hub.submit_profile_job( model=model_tokengenerator_part3, device=device, ) profile_job_tokengenerator_part4 = hub.submit_profile_job( model=model_tokengenerator_part4, device=device, ) ``` Step 3: **Verify on-device accuracy** To verify the accuracy of the model on-device, you can run on-device inference on sample input data on the same cloud hosted device. ```python input_data_promptprocessor_part1 = model.prompt_processor_part1.sample_inputs() inference_job_promptprocessor_part1 = hub.submit_inference_job( model=model_promptprocessor_part1, device=device, inputs=input_data_promptprocessor_part1, ) on_device_output_promptprocessor_part1 = inference_job_promptprocessor_part1.download_output_data() input_data_promptprocessor_part2 = model.prompt_processor_part2.sample_inputs() inference_job_promptprocessor_part2 = hub.submit_inference_job( model=model_promptprocessor_part2, device=device, inputs=input_data_promptprocessor_part2, ) on_device_output_promptprocessor_part2 = inference_job_promptprocessor_part2.download_output_data() input_data_promptprocessor_part3 = model.prompt_processor_part3.sample_inputs() inference_job_promptprocessor_part3 = hub.submit_inference_job( model=model_promptprocessor_part3, device=device, inputs=input_data_promptprocessor_part3, ) on_device_output_promptprocessor_part3 = inference_job_promptprocessor_part3.download_output_data() input_data_promptprocessor_part4 = model.prompt_processor_part4.sample_inputs() inference_job_promptprocessor_part4 = hub.submit_inference_job( model=model_promptprocessor_part4, device=device, inputs=input_data_promptprocessor_part4, ) on_device_output_promptprocessor_part4 = inference_job_promptprocessor_part4.download_output_data() input_data_tokengenerator_part1 = model.token_generator_part1.sample_inputs() inference_job_tokengenerator_part1 = hub.submit_inference_job( model=model_tokengenerator_part1, device=device, inputs=input_data_tokengenerator_part1, ) on_device_output_tokengenerator_part1 = inference_job_tokengenerator_part1.download_output_data() input_data_tokengenerator_part2 = model.token_generator_part2.sample_inputs() inference_job_tokengenerator_part2 = hub.submit_inference_job( model=model_tokengenerator_part2, device=device, inputs=input_data_tokengenerator_part2, ) on_device_output_tokengenerator_part2 = inference_job_tokengenerator_part2.download_output_data() input_data_tokengenerator_part3 = model.token_generator_part3.sample_inputs() inference_job_tokengenerator_part3 = hub.submit_inference_job( model=model_tokengenerator_part3, device=device, inputs=input_data_tokengenerator_part3, ) on_device_output_tokengenerator_part3 = inference_job_tokengenerator_part3.download_output_data() input_data_tokengenerator_part4 = model.token_generator_part4.sample_inputs() inference_job_tokengenerator_part4 = hub.submit_inference_job( model=model_tokengenerator_part4, device=device, inputs=input_data_tokengenerator_part4, ) on_device_output_tokengenerator_part4 = inference_job_tokengenerator_part4.download_output_data() ``` With the output of the model, you can compute like PSNR, relative errors or spot check the output with expected output. **Note**: This on-device profiling and inference requires access to Qualcomm® AI Hub. [Sign up for access](https://myaccount.qualcomm.com/signup). ## Deploying compiled model to Android The models can be deployed using multiple runtimes: - TensorFlow Lite (`.tflite` export): [This tutorial](https://www.tensorflow.org/lite/android/quickstart) provides a guide to deploy the .tflite model in an Android application. - QNN ( `.so` / `.bin` export ): This [sample app](https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/sample_app.html) provides instructions on how to use the `.so` shared library or `.bin` context binary in an Android application. ## View on Qualcomm® AI Hub Get more details on Mistral-7B-Instruct-v0_3's performance across various devices [here](https://aihub.qualcomm.com/models/mistral_7b_instruct_v0_3_quantized). Explore all available models on [Qualcomm® AI Hub](https://aihub.qualcomm.com/) ## License * The license for the original implementation of Mistral-7B-Instruct-v0_3 can be found [here](https://github.com/mistralai/mistral-inference/blob/main/LICENSE). * The license for the compiled assets for on-device deployment can be found [here](https://github.com/mistralai/mistral-inference/blob/main/LICENSE) ## References * [Mistral 7B](https://arxiv.org/abs/2310.06825) * [Source Model Implementation](https://github.com/mistralai/mistral-inference) ## Community * Join [our AI Hub Slack community](https://aihub.qualcomm.com/community/slack) to collaborate, post questions and learn more about on-device AI. * For questions or feedback please [reach out to us](mailto:ai-hub-support@qti.qualcomm.com). ## Usage and Limitations Model may not be used for or in connection with any of the following applications: - Accessing essential private and public services and benefits; - Administration of justice and democratic processes; - Assessing or recognizing the emotional state of a person; - Biometric and biometrics-based systems, including categorization of persons based on sensitive characteristics; - Education and vocational training; - Employment and workers management; - Exploitation of the vulnerabilities of persons resulting in harmful behavior; - General purpose social scoring; - Law enforcement; - Management and operation of critical infrastructure; - Migration, asylum and border control management; - Predictive policing; - Real-time remote biometric identification in public spaces; - Recommender systems of social media platforms; - Scraping of facial images (from the internet or otherwise); and/or - Subliminal manipulation