diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..47c3f598 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,32 @@ +name: lint + +on: + pull_request: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - name: Install pre-commit hook + run: | + pip install pre-commit mmengine + pre-commit install + + - name: Linting + run: pre-commit run # --all-files \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..dd5f43d0 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,61 @@ +name: Check + +on: + pull_request: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.8' + + - name: Install dependencies + run: | + pip install -r requirements.txt + + - name: Download dataset + run: | + # pwd # /home/runner/work/llmc/llmc + cd tools + python download_calib_dataset.py --save_path ../check/datasets/calib --dataset_name pileval + python download_eval_dataset.py --save_path ../check/datasets/eval --dataset_name wikitext2 + + - name: Download model + run: | + cd ci_check + mkdir -p opt-125m + cp model_urls.txt opt-125m/model_urls.txt + cd opt-125m + wget -i model_urls.txt + wget --no-check-certificate https://hf-mirror.com/facebook/opt-125m/resolve/main/pytorch_model.bin + + - name: Run push_tests + run: | + cd ci_check # /home/runner/work/llmc/llmc/ci_check + python change_files.py + bash run.sh + + - name: Check success + if: ${{ success() }} + run: echo "All steps completed successfully. Success!" + + - name: Clean up + if: ${{ always() }} + run: | + cd .. + rm -rf opt-125m + rm -rf check \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..5e018c3c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,60 @@ +exclude: | + (?x)^( + imgs/| + docs/| + ci_check/| + requirements/| + scripts/ + ) +repos: + - repo: https://github.com/PyCQA/flake8 + rev: 5.0.4 + hooks: + - id: flake8 + exclude: configs/ + - repo: https://github.com/PyCQA/isort + rev: 5.11.5 + hooks: + - id: isort + exclude: configs/ + - repo: https://github.com/pre-commit/mirrors-yapf + rev: v0.32.0 + hooks: + - id: yapf + exclude: configs/ + - repo: https://github.com/codespell-project/codespell + rev: v2.2.1 + hooks: + - id: codespell + exclude: | + (?x)^( + .*\.jsonl| + .*\.md.template| + configs/ + ) + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: check-yaml + - id: requirements-txt-fixer + - id: double-quote-string-fixer + - id: check-merge-conflict + - id: fix-encoding-pragma + args: ["--remove"] + - id: mixed-line-ending + args: ["--fix=lf"] + - repo: https://github.com/executablebooks/mdformat + rev: 0.7.9 + hooks: + - id: mdformat + args: ["--number", "--table-width", "200"] + additional_dependencies: + - mdformat-openmmlab + - mdformat_frontmatter + - linkify-it-py + exclude: configs/ + - repo: https://github.com/myint/docformatter + rev: v1.3.1 + hooks: + - id: docformatter + args: ["--in-place", "--wrap-descriptions", "88"] \ No newline at end of file diff --git a/ci_check/awq_w4a16_fakequant_eval.yml b/ci_check/awq_w4a16_fakequant_eval.yml new file mode 100644 index 00000000..c2b0cf5c --- /dev/null +++ b/ci_check/awq_w4a16_fakequant_eval.yml @@ -0,0 +1,32 @@ +base: + seed: &seed 42 +model: + type: Opt + path: /home/runner/work/llmc/llmc/ci_check/opt-125m + torch_dtype: auto +calib: + name: pileval + download: False + path: /home/runner/work/llmc/llmc/check/datasets/calib/pileval + n_samples: 4 # 128 + bs: -1 + seq_len: 16 # 512 + preproc: pileval_awq + seed: *seed +eval: + eval_pos: [pretrain, transformed, fake_quant] + name: wikitext2 + download: False + path: /home/runner/work/llmc/llmc/check/datasets/eval/wikitext2 + bs: 1 + seq_len: 16 # 2048 +quant: + method: Awq + weight: + bit: 4 + symmetric: False + granularity: per_group + group_size: 128 +save: + save_trans: False + save_path: /home/runner/work/llmc/llmc/save/opt-125m_awq_w4a16 \ No newline at end of file diff --git a/ci_check/change_files.py b/ci_check/change_files.py new file mode 100644 index 00000000..a728cfcd --- /dev/null +++ b/ci_check/change_files.py @@ -0,0 +1,97 @@ +import os + +# 文件路径 +cpu_txt_path = 'cpu.txt' + +def modify_file(filepath, modifications): + with open(filepath, 'r') as file: + lines = file.readlines() + + # 应用修改 + new_lines = [] + for line in lines: + # 替换操作 + for search, replace in modifications['modifications']: + if search in line: + line = line.replace(search, replace) + new_lines.append(line) + + # 在文件开头插入新内容 + with open(filepath, 'w') as file: + file.writelines(modifications['header'] + new_lines) + +def main(): + with open(cpu_txt_path, 'r') as file: + file_paths = file.readlines() + + for file_path in file_paths: + file_path = file_path.strip() + if not file_path: + continue + + if file_path == '../llmc/__main__.py': + modifications = { + 'header': [ + 'device_zbl = "cpu"\n', + 'use_cuda = (device_zbl != "cpu")\n' + ], + 'modifications': [ + ('torch.cuda.empty_cache()', 'if use_cuda: torch.cuda.empty_cache()') + ] + } + elif file_path == '../llmc/compression/quantization/awq.py': + modifications = { + 'header': [ + 'n_grid_zbl = 1\n' + ], + 'modifications': [ + ('n_grid = 20', 'n_grid = n_grid_zbl') + ] + } + elif file_path == '../llmc/compression/quantization/base_blockwise_quantization.py': + modifications = { + 'header': [ + 'device_zbl = "cpu"\n', + 'use_cuda = (device_zbl != "cpu")\n' + ], + 'modifications': [ + ('.cuda()', '.to(device_zbl)'), + ('torch.cuda.empty_cache()', 'if use_cuda: torch.cuda.empty_cache()') + ] + } + elif file_path == '../llmc/models/base_model.py': + modifications = { + 'header': [ + 'device_zbl = "cpu"\n', + 'use_cuda = (device_zbl != "cpu")\n' + ], + 'modifications': [ + ('.cuda()', '.to(device_zbl)'), + ('self.move_embed_to_device("cuda")', 'self.move_embed_to_device(device_zbl)') + ] + } + elif file_path == '../llmc/eval/eval_ppl.py': + modifications = { + 'header': [ + 'device_zbl = "cpu"\n', + 'use_cuda = (device_zbl != "cpu")\n', + 'nsamples_zbl = 1\n' + ], + 'modifications': [ + ('.cuda()', '.to(device_zbl)'), + ('torch.cuda.empty_cache()', 'if use_cuda: torch.cuda.empty_cache()'), + ('nlls = []', 'nlls = []; nsamples = nsamples_zbl') + ] + } + else: + print(f"File {file_path} not recognized or not specified for modification.") + continue + + # 修改文件 + if os.path.exists(file_path): + modify_file(file_path, modifications) + else: + print(f"File {file_path} does not exist.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/ci_check/cpu.txt b/ci_check/cpu.txt new file mode 100644 index 00000000..6eadabb1 --- /dev/null +++ b/ci_check/cpu.txt @@ -0,0 +1,5 @@ +../llmc/compression/quantization/base_blockwise_quantization.py +../llmc/__main__.py +../llmc/eval/eval_ppl.py +../llmc/compression/quantization/awq.py +../llmc/models/base_model.py \ No newline at end of file diff --git a/ci_check/model_urls.txt b/ci_check/model_urls.txt new file mode 100644 index 00000000..fa8ce4e4 --- /dev/null +++ b/ci_check/model_urls.txt @@ -0,0 +1,6 @@ +https://hf-mirror.com/facebook/opt-125m/resolve/main/config.json +https://hf-mirror.com/facebook/opt-125m/resolve/main/generation_config.json +https://hf-mirror.com/facebook/opt-125m/resolve/main/merges.txt +https://hf-mirror.com/facebook/opt-125m/resolve/main/special_tokens_map.json +https://hf-mirror.com/facebook/opt-125m/resolve/main/tokenizer_config.json +https://hf-mirror.com/facebook/opt-125m/resolve/main/vocab.json \ No newline at end of file diff --git a/ci_check/run.sh b/ci_check/run.sh new file mode 100644 index 00000000..24dc9da9 --- /dev/null +++ b/ci_check/run.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +current_directory=$(pwd) +llmc=$(echo "$current_directory" | sed 's/\/ci_check$//') +export PYTHONPATH=$llmc:$PYTHONPATH + + +cd ../scripts + +python -m llmc --config ../ci_check/awq_w4a16_fakequant_eval.yml