n1ck-guo commited on
Commit
4b7018e
·
verified ·
1 Parent(s): 5dd90b4

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +243 -0
README.md ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model:
3
+ - inclusionAI/Ling-flash-2.0
4
+ ---
5
+
6
+ ## Model Details
7
+
8
+ This model is a mixed gguf q2ks format of [MiniMaxAI/MiniMax-M2](https://huggingface.co/MiniMaxAI/MiniMax-M2) generated by [intel/auto-round](https://github.com/intel/auto-round) algorithm. Embedding layer and lm-head layer are fallback to 8 bits and non expert layers are fallback to 4 bits. Please refer to Section `Generate the model` for more details.
9
+
10
+ Please follow the license of the original model.
11
+
12
+ ## How To Use
13
+
14
+ Llamacpp inference.
15
+
16
+ ~~~bash
17
+ /llama-cli -hf Intel/MiniMax-M2-gguf-q2ks-mixed-AutoRound --jinja
18
+ ~~~
19
+
20
+ ~~~bash
21
+ > Write a quick sort algorithm.
22
+ The user wants me to write a quick sort algorithm. I should write a clear and concise explanation of how quicksort works, followed by a code example in a commonly used language like Python or JavaScript. I'll make sure to explain the key concepts like the pivot, partitioning, and recursion.
23
+ </think>
24
+
25
+ I'll provide a clear explanation of the Quick Sort algorithm followed by a code implementation.
26
+
27
+ ## Quick Sort Algorithm Overview
28
+
29
+ Quick Sort is a divide-and-conquer sorting algorithm that works by:
30
+ 1. **Choosing a pivot** element from the array
31
+ 2. **Partitioning** the array so elements smaller than the pivot come before it, and larger elements come after
32
+ 3. **Recursively** sorting the sub-arrays formed by dividing the array around the pivot
33
+
34
+ ## Key Concepts:
35
+ - **Pivot**: The reference element used for partitioning
36
+ - **Partitioning**: Rearranging elements so the pivot ends up in its correct final position
37
+ - **Time Complexity**: Average O(n log n), Worst O(n²) when pivot selection is poor
38
+
39
+ ## Python Implementation
40
+
41
+ ```python
42
+ def quick_sort(arr, low, high):
43
+ if low < high:
44
+ # Step 1: Partition the array and get pivot index
45
+ pivot_index = partition(arr, low, high)
46
+
47
+ # Step 2: Recursively sort elements before and after partition
48
+ quick_sort(arr, low, pivot_index - 1) # Sort left sub-array
49
+ quick_sort(arr, pivot_index + 1, high) # Sort right sub-array
50
+
51
+ def partition(arr, low, high):
52
+ # Choose the rightmost element as pivot
53
+ pivot = arr[high]
54
+
55
+ # Index of smaller element
56
+ i = low - 1
57
+
58
+ for j in range(low, high):
59
+ # If current element is smaller than or equal to pivot
60
+ if arr[j] <= pivot:
61
+ i += 1
62
+ # Swap arr[i] and arr[j]
63
+ arr[i], arr[j] = arr[j], arr[i]
64
+
65
+ # Move pivot to its correct position
66
+ arr[i + 1], arr[high] = arr[high], arr[i + 1]
67
+ return i + 1
68
+
69
+ # Example usage and helper function
70
+ def quick_sort_wrapper(arr):
71
+ """Helper function that sorts the entire array"""
72
+ if len(arr) <= 1:
73
+ return arr
74
+ quick_sort(arr, 0, len(arr) - 1)
75
+ return arr
76
+
77
+ # Example usage
78
+ if __name__ == "__main__":
79
+ # Test array
80
+ data = [10, 7, 8, 9, 1, 5]
81
+ print(f"Original array: {data}")
82
+
83
+ sorted_data = quick_sort_wrapper(data)
84
+ print(f"Sorted array: {sorted_data}")
85
+
86
+ # Another example
87
+ test_arr = [64, 34, 25, 12, 22, 11, 90]
88
+ print(f"Original: {test_arr}")
89
+ quick_sort_wrapper(test_arr)
90
+ print(f"Sorted: {test_arr}")
91
+ ```
92
+
93
+ ## JavaScript Implementation
94
+
95
+ ```javascript
96
+ function quickSort(arr, low = 0, high = arr.length - 1) {
97
+ if (low < high) {
98
+ // Partition and get pivot index
99
+ const pivotIndex = partition(arr, low, high);
100
+
101
+ // Recursively sort elements before and after partition
102
+ quickSort(arr, low, pivotIndex - 1);
103
+ quickSort(arr, pivotIndex + 1, high);
104
+ }
105
+ return arr;
106
+ }
107
+
108
+ function partition(arr, low, high) {
109
+ // Choose the rightmost element as pivot
110
+ const pivot = arr[high];
111
+
112
+ // Index of smaller element
113
+ let i = low - 1;
114
+
115
+ for (let j = low; j < high; j++) {
116
+ // If current element is smaller than or equal to pivot
117
+ if (arr[j] <= pivot) {
118
+ i++;
119
+ // Swap arr[i] and arr[j]
120
+ [arr[i], arr[j]] = [arr[j], arr[i]];
121
+ }
122
+ }
123
+
124
+ // Move pivot to its correct position
125
+ [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
126
+ return i + 1;
127
+ }
128
+
129
+ // Example usage
130
+ const data = [10, 7, 8, 9, 1, 5];
131
+ console.log("Original array:", data);
132
+
133
+ quickSort(data);
134
+ console.log("Sorted array:", data);
135
+ ```
136
+
137
+ ## Alternative: Lomuto Partition Scheme
138
+
139
+ For a simpler implementation, here's the Lomuto partition scheme:
140
+
141
+ ```python
142
+ def quick_sort_lomuto(arr, low, high):
143
+ if low < high:
144
+ # Partition index
145
+ j = partition_lomuto(arr, low, high)
146
+ quick_sort_lomuto(arr, low, j - 1)
147
+ quick_sort_lomuto(arr, j + 1, high)
148
+
149
+ def partition_lomuto(arr, low, high):
150
+ pivot = arr[high]
151
+ i = low
152
+
153
+ for k in range(low, high):
154
+ if arr[k] < pivot:
155
+ arr[i], arr[k] = arr[k], arr[i]
156
+ i += 1
157
+
158
+ arr[i], arr[high] = arr[high], arr[i]
159
+ return i
160
+
161
+ # Simple usage
162
+ def simple_quick_sort(arr):
163
+ quick_sort_lomuto(arr, 0, len(arr) - 1)
164
+ return arr
165
+ ```
166
+
167
+
168
+ ## Advantages and Disadvantages
169
+
170
+ **Advantages:**
171
+ - Average-case O(n log n) time complexity
172
+ - In-place sorting (uses minimal extra memory)
173
+ - Cache-friendly due to locality of reference
174
+
175
+ **Disadvantages:**
176
+ - Worst-case O(n²) time complexity (when array is already sorted)
177
+ - Not stable (doesn't preserve order of equal elements)
178
+ - Performance depends heavily on pivot selection
179
+
180
+ ## Tips for Better Performance
181
+ 1. **Choose pivot wisely**: Random pivot or median-of-three method
182
+ 2. **Handle small arrays**: Use insertion sort for small sub-arrays (typically < 10 elements)
183
+ 3. **Randomize input**: Shuffle before sorting to avoid worst-case scenarios
184
+
185
+ The Quick Sort algorithm is widely used due to its excellent average-case performance and space efficiency.
186
+
187
+
188
+ ~~~
189
+
190
+
191
+
192
+ ### Generate the model
193
+
194
+ Here is the sample command to reproduce the model
195
+
196
+ ```python
197
+ import torch
198
+ from transformers import AutoModelForCausalLM, AutoTokenizer
199
+ from auto_round import AutoRound
200
+
201
+ model_name = "MiniMaxAI/MiniMax-M2"
202
+
203
+ model = AutoModelForCausalLM.from_pretrained(model_name,
204
+ device_map="cpu", torch_dtype="auto")
205
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
206
+ layer_config = {}
207
+ for n, m in model.named_modules():
208
+ if n == "lm_head" or isinstance(m,torch.nn.Embedding):
209
+ layer_config[n] = {"bits": 8}
210
+ elif isinstance(m, torch.nn.Linear) and (not "expert" in n or "shared_experts" in n) and n != "lm_head":
211
+ layer_config[n] = {"bits": 4}
212
+
213
+ autoround = AutoRound(model, tokenizer, iters=0, layer_config=layer_config, nsamples=512, disable_opt_rtn=False)
214
+ autoround.quantize_and_save("tmp_autoround", format="gguf:q2_k_s")
215
+
216
+
217
+ ```
218
+
219
+
220
+
221
+ ## Ethical Considerations and Limitations
222
+
223
+ The model can produce factually incorrect output, and should not be relied on to produce factually accurate information. Because of the limitations of the pretrained model and the finetuning datasets, it is possible that this model could generate lewd, biased or otherwise offensive outputs.
224
+
225
+ Therefore, before deploying any applications of the model, developers should perform safety testing.
226
+
227
+ ## Caveats and Recommendations
228
+
229
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model.
230
+
231
+ Here are a couple of useful links to learn more about Intel's AI software:
232
+
233
+ - Intel Neural Compressor [link](https://github.com/intel/neural-compressor)
234
+
235
+ ## Disclaimer
236
+
237
+ The license on this model does not constitute legal advice. We are not responsible for the actions of third parties who use this model. Please consult an attorney before using this model for commercial purposes.
238
+
239
+ ## Cite
240
+
241
+ @article{cheng2023optimize, title={Optimize weight rounding via signed gradient descent for the quantization of llms}, author={Cheng, Wenhua and Zhang, Weiwei and Shen, Haihao and Cai, Yiyang and He, Xin and Lv, Kaokao and Liu, Yi}, journal={arXiv preprint arXiv:2309.05516}, year={2023} }
242
+
243
+ [arxiv](https://arxiv.org/abs/2309.05516) [github](https://github.com/intel/auto-round)