-
-
Notifications
You must be signed in to change notification settings - Fork 2
311 lines (257 loc) · 9.1 KB
/
batch-quantize.yml
File metadata and controls
311 lines (257 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
name: Batch Quantize Models
on:
workflow_dispatch:
inputs:
config_file:
description: 'Batch configuration file path'
required: true
type: string
default: '.github/configs/batch_quantize.yaml'
parallel_jobs:
description: 'Number of parallel jobs'
required: false
type: number
default: 2
upload_to_hub:
description: 'Upload results to HuggingFace Hub'
required: false
type: boolean
default: false
schedule:
# Run weekly on Sunday at 2 AM UTC
- cron: '0 2 * * 0'
env:
PYTHON_VERSION: '3.9'
CUDA_VERSION: '11.8'
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
config: ${{ steps.load-config.outputs.config }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pyyaml
- name: Load batch configuration
id: load-config
run: |
python -c "
import yaml
import json
with open('${{ github.event.inputs.config_file }}', 'r') as f:
config = yaml.safe_load(f)
print('config=' + json.dumps(config))
" >> $GITHUB_OUTPUT
- name: Generate job matrix
id: set-matrix
run: |
python -c "
import yaml
import json
with open('${{ github.event.inputs.config_file }}', 'r') as f:
config = yaml.safe_load(f)
models = config.get('models', [])
matrix = {'include': []}
for i, model in enumerate(models):
matrix['include'].append({
'model_index': i,
'model_name': model['model'],
'output_dir': model['output_dir'],
'method': model.get('method', 'auto'),
'bits': model.get('bits', 4)
})
print('matrix=' + json.dumps(matrix))
" >> $GITHUB_OUTPUT
quantize:
needs: prepare
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.prepare.outputs.matrix) }}
max-parallel: ${{ fromJson(github.event.inputs.parallel_jobs || '2') }}
fail-fast: false
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
pip install -r requirements.txt
- name: Create output directory
run: |
mkdir -p "${{ matrix.output_dir }}"
mkdir -p ./logs
- name: Run quantization
run: |
quantllm quantize \
--model "${{ matrix.model_name }}" \
--method "${{ matrix.method }}" \
--bits "${{ matrix.bits }}" \
--output-dir "${{ matrix.output_dir }}" \
--validate \
--progress json \
--log-file "./logs/quantization-${{ matrix.model_index }}-${{ github.run_id }}.log" \
--verbose
- name: Upload quantization logs
if: always()
uses: actions/upload-artifact@v3
with:
name: batch-logs-${{ matrix.model_index }}-${{ github.run_id }}
path: ./logs/
retention-days: 30
- name: Upload quantized model
if: success()
uses: actions/upload-artifact@v3
with:
name: batch-model-${{ matrix.model_index }}-${{ github.run_id }}
path: ${{ matrix.output_dir }}
retention-days: 7
collect-results:
needs: [prepare, quantize]
runs-on: ubuntu-latest
if: always()
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download all artifacts
uses: actions/download-artifact@v3
with:
path: ./artifacts
- name: Generate batch report
run: |
python -c "
import json
import os
from pathlib import Path
artifacts_dir = Path('./artifacts')
report = {
'batch_id': '${{ github.run_id }}',
'timestamp': '$(date -u +%Y-%m-%dT%H:%M:%SZ)',
'config_file': '${{ github.event.inputs.config_file }}',
'results': []
}
# Collect results from each model
for artifact_dir in artifacts_dir.iterdir():
if artifact_dir.name.startswith('batch-model-'):
model_index = artifact_dir.name.split('-')[2]
# Check if quantization was successful
if any(artifact_dir.rglob('*.json')):
status = 'success'
else:
status = 'failed'
report['results'].append({
'model_index': int(model_index),
'status': status,
'artifact_name': artifact_dir.name
})
# Save report
with open('./batch_report.json', 'w') as f:
json.dump(report, f, indent=2)
# Print summary
total = len(report['results'])
successful = sum(1 for r in report['results'] if r['status'] == 'success')
failed = total - successful
print(f'Batch Quantization Summary:')
print(f'Total models: {total}')
print(f'Successful: {successful}')
print(f'Failed: {failed}')
print(f'Success rate: {successful/total*100:.1f}%' if total > 0 else 'N/A')
"
- name: Upload batch report
uses: actions/upload-artifact@v3
with:
name: batch-report-${{ github.run_id }}
path: ./batch_report.json
retention-days: 90
- name: Comment on PR (if applicable)
if: github.event_name == 'pull_request'
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const report = JSON.parse(fs.readFileSync('./batch_report.json', 'utf8'));
const total = report.results.length;
const successful = report.results.filter(r => r.status === 'success').length;
const failed = total - successful;
const successRate = total > 0 ? (successful / total * 100).toFixed(1) : 'N/A';
const comment = `## Batch Quantization Results
📊 **Summary:**
- Total models: ${total}
- Successful: ${successful} ✅
- Failed: ${failed} ❌
- Success rate: ${successRate}%
🔗 **Artifacts:** Check the workflow run for detailed logs and quantized models.
`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
upload-to-hub:
needs: [prepare, quantize]
runs-on: ubuntu-latest
if: github.event.inputs.upload_to_hub == 'true' && success()
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download all model artifacts
uses: actions/download-artifact@v3
with:
path: ./artifacts
pattern: batch-model-*
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install HuggingFace Hub
run: |
pip install huggingface_hub
- name: Upload models to Hub
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
python -c "
import os
import json
from pathlib import Path
from huggingface_hub import HfApi
if not os.getenv('HF_TOKEN'):
print('HF_TOKEN not found, skipping upload')
exit(0)
api = HfApi()
artifacts_dir = Path('./artifacts')
for artifact_dir in artifacts_dir.iterdir():
if artifact_dir.name.startswith('batch-model-'):
model_index = artifact_dir.name.split('-')[2]
# Create repository name
repo_id = f'quantllm/batch-{model_index}-${{ github.run_id }}'
try:
api.upload_folder(
folder_path=str(artifact_dir),
repo_id=repo_id,
token=os.getenv('HF_TOKEN')
)
print(f'Uploaded {artifact_dir.name} to {repo_id}')
except Exception as e:
print(f'Failed to upload {artifact_dir.name}: {e}')
"