lokinfey commited on
Commit
acae08b
·
verified ·
1 Parent(s): ea24cb3
Files changed (1) hide show
  1. README.md +105 -0
README.md CHANGED
@@ -6,3 +6,108 @@ license: mit
6
 
7
  <b><span style="text-decoration:underline">Note: This is unoffical version,just for test and dev.</span></b>
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  <b><span style="text-decoration:underline">Note: This is unoffical version,just for test and dev.</span></b>
8
 
9
+ This is a Phi-3.5-mini-instruct version of TFLite, based on AI Edge Torch https://github.com/google-ai-edge/ai-edge-torch. Convert with the following command
10
+
11
+
12
+ ## **📚 Knowledge**
13
+
14
+ Android LLM Inference API lets you run large language models (LLMs) completely on-device for Android applications, which you can use to perform a wide range of tasks, such as generating text, retrieving information in natural language form, and summarizing documents. The task provides built-in support for multiple text-to-text large language models, so you can apply the latest on-device generative AI models to your Android apps.
15
+
16
+ Googld AI Edge Torch is a python library that supports converting PyTorch models into a .tflite format, which can then be run with TensorFlow Lite and MediaPipe. This enables applications for Android, iOS and IoT that can run models completely on-device. AI Edge Torch offers broad CPU coverage, with initial GPU and NPU support. AI Edge Torch seeks to closely integrate with PyTorch, building on top of torch.export() and providing good coverage of Core ATen operators.
17
+
18
+
19
+ ## **🪬 Guideline**
20
+
21
+ ### **🔥 Convert Microsoft Phi-3.5 to tflite support**
22
+
23
+ 0. This sample is for Android 14+
24
+
25
+ 1. Install Python 3.10.12
26
+
27
+ ***Suggestion:*** using conda to install your Python env
28
+
29
+ 2. Ubuntu 20.04 / 22.04 (please focus on [google ai-edge-torch](https://github.com/google-ai-edge/ai-edge-torch))
30
+
31
+ ***Suggestion:*** Using Azure Linux VM or 3rd party cloud vm to create your env
32
+
33
+ 3. Go to your Linux bash , to install Python library
34
+
35
+ ```bash
36
+
37
+ git clone https://github.com/google-ai-edge/ai-edge-torch.git
38
+
39
+ cd ai-edge-torch
40
+
41
+ pip install -r requirements.txt -U
42
+
43
+ pip install tensorflow-cpu -U
44
+
45
+ pip install -e .
46
+
47
+ ```
48
+
49
+ 4. Download Microsoft-3.5-Instruct from Hugging face
50
+
51
+
52
+ ```bash
53
+
54
+ git lfs install
55
+
56
+ git clone https://huggingface.co/microsoft/Phi-3.5-mini-instruct
57
+
58
+ ```
59
+
60
+ 5. Convert Microsoft Phi-3.5 to tflite
61
+
62
+
63
+ ```bash
64
+
65
+ python ai-edge-torch/ai_edge_torch/generative/examples/phi/convert_phi3_to_tflite.py --checkpoint_path Your Microsoft Phi-3.5-mini-instruct path --tflite_path Your Microsoft Phi-3.5-mini-instruct tflite path --prefill_seq_len 1024 --kv_cache_max_len 1280 --quantize True
66
+
67
+ ```
68
+
69
+ ### **🔥 Convert to Microsoft Phi-3.5 to Android Mediapipe Bundle**
70
+
71
+ please install mediapipe firstly
72
+
73
+ ```bash
74
+
75
+ pip install mediapipe
76
+
77
+ ```
78
+
79
+ run this code in [your notebook](https://github.com/kinfey/MTKPhi3Samples/blob/main/convert/convert_phi.ipynb)
80
+
81
+
82
+
83
+ ```python
84
+
85
+ import mediapipe as mp
86
+ from mediapipe.tasks.python.genai import bundler
87
+
88
+ config = bundler.BundleConfig(
89
+ tflite_model='Your Phi-3.5 tflite model path',
90
+ tokenizer_model='Your Phi-3.5 tokenizer model path',
91
+ start_token='start_token',
92
+ stop_tokens=[STOP_TOKENS],
93
+ output_filename='Your Phi-3.5 task model path',
94
+ enable_bytes_to_unicode_mapping=True or Flase,
95
+ )
96
+ bundler.create_bundle(config)
97
+
98
+ ```
99
+
100
+
101
+ ### **🔥 Using adb push task model to your Android devices path**
102
+
103
+
104
+ ```bash
105
+
106
+ adb shell rm -r /data/local/tmp/llm/ # Remove any previously loaded models
107
+
108
+ adb shell mkdir -p /data/local/tmp/llm/
109
+
110
+ adb push 'Your Phi-3.5 task model path' /data/local/tmp/llm/phi3.task
111
+
112
+ ```
113
+