Views
No views yet
Generate the model for more details./llama-cli -hf Intel/Ling-flash-2.0-gguf-q2ks-mixed-AutoRound1> Write a quick sort algorithm.
2Sure! Here's a simple and clear implementation of the **Quick Sort** algorithm in Python:
3
4### ✅ Quick Sort in Python
5
6```python
7def quick_sort(arr):
8 # Base case: arrays with 0 or 1 element are already sorted
9 if len(arr) <= 1:
10 return arr
11
12 # Choose the pivot (here we pick the middle element)
13 pivot = arr[len(arr) // 2]
14
15 # Partition the array into three parts
16 left = [x for x in arr if x < pivot] # Elements less than pivot
17 middle = [x for x in arr if x == pivot] # Elements equal to pivot
18 right = [x for x in arr if x > pivot] # Elements greater than pivot
19
20 # Recursively sort left and right, and concatenate the result
21 return quick_sort(left) + middle + quick_sort(right)
22
23# Example usage
24arr = [3, 6, 8, 10, 1, 2, 1]
25sorted_arr = quick_sort(arr)
26print("Sorted array:", sorted_arr)
27```
28
29### 🧠 How It Works:
30- **Divide and Conquer**: Pick a pivot, partition the array into elements less than, equal to, and greater than the pivot.
31- Recursively apply the same to left and right partitions.
32- **Not in-place** in this version (creates new lists). An in-place version is more memory-efficient.
33
34### ⏱ Time Complexity:
35- **Best/Average Case**: O(n log n)
36- **Worst Case**: O(n²) — when the pivot is always the smallest or largest element (e.g., on already sorted data), but this is mitigated by choosing a good pivot (like middle, random, or median-of-three).
37
38---
39
40Would you like the **in-place version** (which is more efficient in memory) as well?
41pip install git+https://github.com/intel/auto-round.git@hengguo/fix_imatrix_bug1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from auto_round import AutoRound
4
5model_name = "inclusionAI/Ling-flash-2.0"
6
7model = AutoModelForCausalLM.from_pretrained(model_name,
8 device_map="cpu", torch_dtype="auto")
9tokenizer = AutoTokenizer.from_pretrained(model_name)
10layer_config = {}
11for n, m in model.named_modules():
12 if n == "lm_head" or isinstance(m,torch.nn.Embedding):
13 layer_config[n] = {"bits": 8}
14 elif isinstance(m, torch.nn.Linear) and (not "expert" in n or "shared_experts" in n) and n != "lm_head":
15 layer_config[n] = {"bits": 4}
16
17autoround = AutoRound(model, tokenizer, iters=0, layer_config=layer_config, nsamples=4096, dataset="NeelNanda/pile-10k,HuggingFaceH4/ultrachat_200k", seqlen=1024)
18autoround.quantize_and_save("tmp_autoround", format="gguf:q2_k_s")
19
20