diff --git a/.github/workflows/package_wheel_release.yml b/.github/workflows/package_wheel_release.yml new file mode 100644 index 0000000..93e5f38 --- /dev/null +++ b/.github/workflows/package_wheel_release.yml @@ -0,0 +1,252 @@ +name: Build Wheels +on: + workflow_dispatch: + inputs: + release: + description: 'Release? 1 = yes, 0 = no' + default: '0' + required: true + type: string +jobs: + build_wheels: + name: ${{ matrix.os }} Python=${{ matrix.pyver }} CUDA=${{ matrix.cuda }} CPU_INSTRUCT=${{ matrix.instruct }} Torch=${{ matrix.torch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + # Ubuntu + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.11', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: ubuntu-20.04, pyver: '3.10', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + + # Windows + - { os: windows-2022, pyver: '3.12', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.12', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.12', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.12', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.12', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.12', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.12', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.12', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.12', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.11', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.4.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.2.2', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.1.1', torch: '2.4.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.5.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.4.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + - { os: windows-2022, pyver: '3.10', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX512', torch_cu: '121'} + - { os: windows-2022, pyver: '3.10', cuda: '12.1.1', torch: '2.3.0', cudaarch: '8.0;8.6;8.7;8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '121'} + + defaults: + run: + shell: pwsh + + steps: + - uses: actions/checkout@v3 + + - name: Free Disk Space + uses: jlumbroso/free-disk-space@v1.3.1 + if: runner.os == 'Linux' + with: + tool-cache: true + android: true + dotnet: true + haskell: true + large-packages: false + swap-storage: true + + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.pyver }} + + - name: check_space + run: | + if($IsLinux) {df -h} + if($IsWindows) {Get-PSDrive -PSProvider 'FileSystem'} + + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Setup Mamba + if: matrix.cuda != '' + uses: conda-incubator/setup-miniconda@v2.3.0 + with: + activate-environment: "ktransformers" + python-version: ${{ matrix.pyver }} + miniforge-variant: Mambaforge + miniforge-version: latest + use-mamba: true + add-pip-as-python-dependency: true + auto-activate-base: false + + + + - name: build web + run: | + cd ktransformers/website/ + npm install + npm run build + cd ../../ + + - name: build for cuda + if: matrix.cuda != '' + run: | + git submodule init + git submodule update + if($IsWindows){ + $originalPath = Get-Location + Import-Module 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\Microsoft.VisualStudio.DevShell.dll' + Enter-VsDevShell -VsInstallPath 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise' -DevCmdArguments '-arch=x64 -host_arch=x64' + $env:DISTUTILS_USE_SDK=1 + Set-Location $originalPath + } + $cudaVersion = '${{ matrix.cuda }}' + $env:MAMBA_NO_LOW_SPEED_LIMIT = 1 + mamba install -y -c nvidia/label/cuda-$cudaVersion cuda-toolkit cuda-runtime + $env:CUDA_PATH = $env:CONDA_PREFIX + $env:CUDA_HOME = $env:CONDA_PREFIX + if ($IsLinux) { + $env:LD_LIBRARY_PATH = $env:CONDA_PREFIX + '/lib:' + $env:LD_LIBRARY_PATH + $env:LD_LIBRARY_PATH = $env:CONDA_PREFIX + '/lib/python${{ matrix.pyver }}/site-packages/nvidia/nvjitlink/lib:' + $env:LD_LIBRARY_PATH + if (!(Test-Path $env:CUDA_HOME/lib64)) { + New-Item -ItemType SymbolicLink -Path $env:CUDA_HOME/lib64 -Target $env:CUDA_HOME/lib + } + } + if ($IsWindows) { + $env:CUDA_PATH = "$env:CUDA_PATH/Library" + $env:CUDA_HOME = $env:CUDA_PATH + $env:PATH = "$env:CUDA_PATH/bin;" + $env:PATH + cp $env:CUDA_PATH/lib/*.lib $env:CUDA_PATH/lib/x64/ + $env:INCLUDE =$env:CUDA_PATH + "/include/targets/x64;" + $env:INCLUDE + + } + python -m pip install torch==${{ matrix.torch }} torchvision torchaudio --index-url https://download.pytorch.org/whl/cu${{ matrix.torch_cu }} + python -m pip install cpufeature build wheel ninja packaging setuptools + $env:KTRANSFORMERS_FORCE_BUILD = "TRUE" + $env:CPU_INSTRUCT = '${{ matrix.instruct }}' + $env:TORCH_CUDA_ARCH_LIST = '${{ matrix.cudaarch }}' + python -m build --no-isolation --verbose + + + - name: create Rlease dir + run: | + if ($IsWindows) { + $env:date = $(Get-Date -Format "yyyy-MM-dd") + New-Item -ItemType Directory -Force -Path "$Env:USERPROFILE\.ssh" + $Env:SSH_PATH = "$Env:USERPROFILE\.ssh\id_rsa" + Set-Content -Path $Env:SSH_PATH -Value "${{ secrets.SSH_PRIVATE_KEY }}" + (Get-Content -Path $Env:SSH_PATH).Replace("`r`n","`n") | Set-Content -Path $Env:SSH_PATH + chmod 600 $Env:SSH_PATH + } + if ($IsLinux) { + $env:date = $(date +%Y-%m-%d) + mkdir -p ~/.ssh/ + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + } + + ssh -p ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no root@${{ secrets.SSH_SERVER }} "mkdir -p /mnt/data/release-$env:date" + scp -P ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no dist/*.whl root@${{ secrets.SSH_SERVER }}:/mnt/data/release-$env:date/ \ No newline at end of file diff --git a/.github/workflows/package_wheel_test.yml b/.github/workflows/package_wheel_test.yml new file mode 100644 index 0000000..9fe82f8 --- /dev/null +++ b/.github/workflows/package_wheel_test.yml @@ -0,0 +1,132 @@ +name: Build Wheels +on: + workflow_dispatch: + inputs: + release: + description: 'Release? 1 = yes, 0 = no' + default: '0' + required: true + type: string +jobs: + build_wheels: + name: ${{ matrix.os }} Python=${{ matrix.pyver }} CUDA=${{ matrix.cuda }} CPU_INSTRUCT=${{ matrix.instruct }} Torch=${{ matrix.torch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + # Ubuntu + - { os: ubuntu-20.04, pyver: '3.12', cuda: '12.2.2', torch: '2.3.0', cudaarch: '8.9;9.0+PTX', instruct: 'FANCY', torch_cu: '121'} + - { os: windows-2022, pyver: '3.11', cuda: '12.5.1', torch: '2.4.0', cudaarch: '8.9;9.0+PTX', instruct: 'AVX2', torch_cu: '124'} + + defaults: + run: + shell: pwsh + + steps: + - uses: actions/checkout@v3 + + - name: Free Disk Space + uses: jlumbroso/free-disk-space@v1.3.1 + if: runner.os == 'Linux' + with: + tool-cache: true + android: true + dotnet: true + haskell: true + large-packages: false + swap-storage: true + + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.pyver }} + + - name: check_space + run: | + if($IsLinux) {df -h} + if($IsWindows) {Get-PSDrive -PSProvider 'FileSystem'} + + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Setup Mamba + if: matrix.cuda != '' + uses: conda-incubator/setup-miniconda@v2.3.0 + with: + activate-environment: "ktransformers" + python-version: ${{ matrix.pyver }} + miniforge-variant: Mambaforge + miniforge-version: latest + use-mamba: true + add-pip-as-python-dependency: true + auto-activate-base: false + + + + - name: build web + run: | + cd ktransformers/website/ + npm install + npm run build + cd ../../ + + - name: build for cuda + if: matrix.cuda != '' + run: | + git submodule init + git submodule update + if($IsWindows){ + $originalPath = Get-Location + Import-Module 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\Microsoft.VisualStudio.DevShell.dll' + Enter-VsDevShell -VsInstallPath 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise' -DevCmdArguments '-arch=x64 -host_arch=x64' + $env:DISTUTILS_USE_SDK=1 + Set-Location $originalPath + } + $cudaVersion = '${{ matrix.cuda }}' + $env:MAMBA_NO_LOW_SPEED_LIMIT = 1 + mamba install -y -c nvidia/label/cuda-$cudaVersion cuda-toolkit cuda-runtime + $env:CUDA_PATH = $env:CONDA_PREFIX + $env:CUDA_HOME = $env:CONDA_PREFIX + if ($IsLinux) { + $env:LD_LIBRARY_PATH = $env:CONDA_PREFIX + '/lib:' + $env:LD_LIBRARY_PATH + $env:LD_LIBRARY_PATH = $env:CONDA_PREFIX + '/lib/python${{ matrix.pyver }}/site-packages/nvidia/nvjitlink/lib:' + $env:LD_LIBRARY_PATH + if (!(Test-Path $env:CUDA_HOME/lib64)) { + New-Item -ItemType SymbolicLink -Path $env:CUDA_HOME/lib64 -Target $env:CUDA_HOME/lib + } + } + if ($IsWindows) { + $env:CUDA_PATH = "$env:CUDA_PATH/Library" + $env:CUDA_HOME = $env:CUDA_PATH + $env:PATH = "$env:CUDA_PATH/bin;" + $env:PATH + cp $env:CUDA_PATH/lib/*.lib $env:CUDA_PATH/lib/x64/ + $env:INCLUDE =$env:CUDA_PATH + "/include/targets/x64;" + $env:INCLUDE + + } + python -m pip install torch==${{ matrix.torch }} torchvision torchaudio --index-url https://download.pytorch.org/whl/cu${{ matrix.torch_cu }} + python -m pip install cpufeature build wheel ninja packaging setuptools + $env:KTRANSFORMERS_FORCE_BUILD = "TRUE" + $env:CPU_INSTRUCT = '${{ matrix.instruct }}' + $env:TORCH_CUDA_ARCH_LIST = '${{ matrix.cudaarch }}' + python -m build --no-isolation --verbose + + + - name: create Rlease dir + run: | + if ($IsWindows) { + $env:date = $(Get-Date -Format "yyyy-MM-dd") + New-Item -ItemType Directory -Force -Path "$Env:USERPROFILE\.ssh" + $Env:SSH_PATH = "$Env:USERPROFILE\.ssh\id_rsa" + Set-Content -Path $Env:SSH_PATH -Value "${{ secrets.SSH_PRIVATE_KEY }}" + (Get-Content -Path $Env:SSH_PATH).Replace("`r`n","`n") | Set-Content -Path $Env:SSH_PATH + chmod 600 $Env:SSH_PATH + } + if ($IsLinux) { + $env:date = $(date +%Y-%m-%d) + mkdir -p ~/.ssh/ + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + } + + ssh -p ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no root@${{ secrets.SSH_SERVER }} "mkdir -p /mnt/data/release-$env:date" + scp -P ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no dist/*.whl root@${{ secrets.SSH_SERVER }}:/mnt/data/release-$env:date/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 718ea55..1bb8666 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,7 @@ node_modules .DS_Store compile_commands.json *.egg-info* -*dist/ \ No newline at end of file +*dist/ +ktransformers/server/local_store/ +ktransformers/server_test1.db +*.patch \ No newline at end of file diff --git a/README.md b/README.md index a80fe67..8c5f505 100644 --- a/README.md +++ b/README.md @@ -268,7 +268,10 @@ In this example, the AutoModel is first initialized on the meta device to avoid After injection, the original `generate` interface is available, but we also provide a compatible `prefill_and_generate` method, which enables further optimizations like CUDAGraph to improve generation speed. -

YAML Template

+

How to custom your model

+ +A detailed tutorial of the injection and multi-GPU using DeepSeek-V2 as an example is given [here](doc/en/injection_tutorial.md). + Below is an example of a YAML template for replacing all original Linear modules with Marlin, an advanced 4-bit quantization kernel. ```yaml @@ -287,7 +290,7 @@ Each rule in the YAML file has two parts: `match` and `replace`. The `match` par You can find example rule templates for optimizing DeepSeek-V2 and Qwen2-57B-A14, two SOTA MoE models, in the [ktransformers/optimize/optimize_rules](ktransformers/optimize/optimize_rules) directory. These templates are used to power the `local_chat.py` demo. -A detailed description of the injection using DeepSeek-V2 as an example is given [here](doc/en/deepseek-v2-injection.md). +If you are interested in our design principles and the implementation of the injection framework, please refer to the [design document](doc/en/deepseek-v2-injection.md).

Acknowledgment and Contributors

diff --git a/doc/assets/deepseekv2_structure.png b/doc/assets/deepseekv2_structure.png new file mode 100644 index 0000000..b9ced32 Binary files /dev/null and b/doc/assets/deepseekv2_structure.png differ diff --git a/doc/assets/model_structure_guild.png b/doc/assets/model_structure_guild.png new file mode 100644 index 0000000..d9f8e4a Binary files /dev/null and b/doc/assets/model_structure_guild.png differ diff --git a/doc/assets/multi_gpu.png b/doc/assets/multi_gpu.png new file mode 100644 index 0000000..88c025b Binary files /dev/null and b/doc/assets/multi_gpu.png differ diff --git a/doc/en/deepseek-v2-injection.md b/doc/en/deepseek-v2-injection.md index c1ccd39..e5dc1c2 100644 --- a/doc/en/deepseek-v2-injection.md +++ b/doc/en/deepseek-v2-injection.md @@ -90,7 +90,7 @@ The YAML rule is listed below. - match: name: "^model\\.layers\\..*\\.self_attn$" # regular expression replace: - class: ktransformers.operators.attention.DeepseekV2AttentionInjected # optimized MLA implementation + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation ``` As we can see, each rule in the YAML file has two parts: `match` and `replace`. @@ -98,9 +98,9 @@ The match part specifies which module should be replaced, and the replace part s

Routed Experts

-For routed experts, the module we inject is a wrapper of CPUInfer, KTransformersMLPExpert. There are several implementations within a wrapper, and we need to specify keywords to tell the wrapper which implementation we want to use and how we intend to use it. +For routed experts, the module we inject is a wrapper of CPUInfer, KTransformersExperts. There are several implementations within a wrapper, and we need to specify keywords to tell the wrapper which implementation we want to use and how we intend to use it. -In KTransformers, some models exhibit different behaviors during prefilling and generation for better performance. KTransformersMLPExpert is one of them. All these special modules have a `device` keyword describing which device the module should be initialized on. Other keywords specify the behaviors during prefilling and generation and may be differ when using different injection modules. Here, we specify which implementation on which device we want to use during prefilling and generation, and which device the output should be on. +In KTransformers, some models exhibit different behaviors during prefilling and generation for better performance. KTransformersExperts is one of them. All these special modules have a `device` keyword describing which device the module should be initialized on. Other keywords specify the behaviors during prefilling and generation and may be differ when using different injection modules. Here, we specify which implementation on which device we want to use during prefilling and generation, and which device the output should be on. Note that we only use these parameters when layer-wise prefilling is enabled; otherwise, prefilling is conducted with the same configuration as generation. In the original implementation of Transformers, MoE is implemented using `nn.ModuleList`. We don't want KTransformers to iterate through all the sub-modules in the list, so we set `recursive: False` in this rule to prevent recursive injection into submodules of the current module. Here is the YAML rule: @@ -109,13 +109,13 @@ In the original implementation of Transformers, MoE is implemented using `nn.Mod - match: name: "^model\\.layers\\..*\\.mlp\\.experts$" replace: - class: ktransformers.operators.experts.KTransformersMLPExpert # custom MoE Kernel with expert parallelism + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert parallelism device: "cpu" # device to load this module on initialization kwargs: prefill_device: "cuda" - prefill_mlp_type: "MLPExpertsTorch" + prefill_op: "KExpertsTorch" generate_device: "cpu" - generate_mlp_type: "MLPCPUExperts" + generate_op: "KExpertsCPU" out_device: "cuda" recursive: False # don't recursively inject submodules of this module ``` @@ -126,7 +126,7 @@ If we inject the expert list as a custom module, we can't use the interface in ` - match: class: ktransformers.models.modeling_deepseek.DeepseekV2MoE replace: - class: ktransformers.operators.experts.DeepseekV2MoEInjected # MLP module with custom forward function + class: ktransformers.operators.experts.KDeepseekV2MoE # MLP module with custom forward function ```

Other Linear Modules

@@ -140,12 +140,12 @@ We also need to transfer some keywords similar to the injection of experts. Here name: "^model\\.layers\\.(?!.*self_attn).*$" # regular expression class: torch.nn.Linear # only match modules matching name and class simultaneously replace: - class: ktransformers.operators.linear.KTransformerLinear # optimized Kernel on quantized data types + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types kwargs: generate_device: "cuda" prefill_device: "cuda" - generate_op: "QuantizedLinearMarlin" - prefill_op: "QuantizedLinearTorch" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" ```

Pre-compute Buffers

diff --git a/doc/en/injection_tutorial.md b/doc/en/injection_tutorial.md new file mode 100644 index 0000000..655163e --- /dev/null +++ b/doc/en/injection_tutorial.md @@ -0,0 +1,328 @@ +# Tutorial: Inject Operator Step by Step + +> Author: Azure-Tang + +## TL;DR +This tutorial will guide you through the process of injecting custom operators into a model using the KTransformers framework. We will use the DeepSeekV2-Chat model as an example to demonstrate how to inject custom operators into the model step by step. The tutorial will cover the following topics: +* [How to write injection rules](#how-to-write-injection-rules) + * [Understanding the structure of the model](#understanding-model-structure) +* [Multi-GPU](#muti-gpu) +* [How to write a new operator and inject it into the model](#how-to-write-a-new-operator-and-inject-into-the-model) + +## How to Write Injection Rules +The basic form of the injection rules for the Inject framework is as follows: +```yaml +- match: + name: "^model\\.layers\\..*\\.*$" # Target module name + class: torch.nn.Linear # Target module + replace: + class: "default" + kwargs: + generate_device: "cuda:0" + # your_op_param_1: 1234 + # your_op_param_2: 5678 + recursive: True +``` +* match: This field marks the matching rules, which can appear in two forms, name and class. These two matching rules can appear together or separately; they only match when both criteria are met. +* replace: + * class: Python class that can be imported to replace the target module. If no replacement is desired, set to default. + * kwargs: List of parameters needed for module initialization. + * generate_device: The device for this module, can be set to “cpu”, “cuda”, “cuda:1”, etc. +* recursive: Whether to recursively inject this module’s submodules, default is True. + +For the recursive field: Some modules contain multiple submodules, such as the Self-attention module typically includes q/k/v/o four linear modules. If we replace the self-attention module but do not want the internal linear modules to be covered by other rules, set this rule to False. + +## Understanding Model Structure +Using [deepseek-ai/DeepSeek-V2-Lite-Chat](https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite-Chat) as an example, we can follow the above rules step by step to inject our custom module and run it. KTransformers offers a high degree of flexibility, allowing you to replace/experiment with basic operators. However, it also requires users to clearly understand the structure of the model they are running. + +Fortunately, knowing the structure of a model is very simple. Open the file list on the [deepseek-ai/DeepSeek-V2-Lite](https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite-Chat/tree/main) homepage, and you can see the following files: +

+ + Inject-Struction + +

+ +From the `.saftensors` file, we can see the name of each layer’s weights, corresponding to the match.name attribute in the injection rules. +From the `modeling_deepseek.py` file, we can see the specific implementation of each module class, with the class name corresponding to the match.class attribute in the injection rules. + +The structure of the DeepSeekV2 model from the `.saftensors` and `modeling_deepseek.py` files is as follows: +

+ + Inject-Struction + +

+ +Supported operators and their corresponding classes are as follows: + +| match | replace | backends | descriptions | +| --------- | ---------------------- | ----------------------- | -------------------- | +| Linear | KTransformersLinear | KLinearMarlin | Marlin as backend | +| | | KLinearTorch | pytorch as backend | +| | | KLinearCPUInfer | llamafile as backend | +| experts | KTransformersExperts | KExpertsTorch | pytorch as backend | +| | | KExpertsMarlin | Marlin as backend | +| | | KExpertsCPU | llamafile as backend | +| Attention | KDeepseekV2Attention | KDeepseekV2Attention | MLA implementation | +| MoE | KMistralSparseMoEBlock | KQwen2MoeSparseMoeBlock | MoE for Qwen2 | +| | KDeepseekV2MoE | KDeepseekV2MoE | MoE for DeepseekV2 | +| Model | KQwen2MoeModel | KQwen2MoeModel | Model for Qwen2 | +| | KDeepseekV2Model | KDeepseekV2Model | Model for DeepseekV2 | +| RoPE | RotaryEmbedding | RotaryEmbedding | RoPE module | +| | YarnRotaryEmbedding | YarnRotaryEmbedding | RoPE module | + +Then we start step-by-step injection of custom modules, our targets are: + +* Replace the linear module with custom Marlin linear module. +* Replace the self-attention module with a custom Absorption-based MLA module. +* Replace the experts module with a custom Experts module. +* Replace the MoE module with a custom MoE module. +* Replace the RoPE module with a custom RoPE module. +* Set the running device for each module. + +The full implementation of the injection rules can be found in the [here](https://github.com/kvcache-ai/ktransformers/blob/main/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat.yaml). + +## Matrix Absorption-based MLA Injection + +For the injection of the Attention module, we only need to use a regular expression to match the module names used in transformers and replace them with our own MLA module implementation. The YAML injection rule is as follows: +```yaml +- match: + name: "^model\\.layers\\..*\\.self_attn$" # Regular expression + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # Optimized MLA implementation +``` +As you can see, each rule in the YAML file has two parts: match and replace. The match part specifies the module to be replaced, and the replace part specifies the module to be injected into the model along with the initialization keywords. + +## Injection of Routed Experts +For Routed Experts (corresponding to the exps in the diagram), the module we inject is CPUInfer, which is wrapped in the wrapper module KTransformersExperts. KTransformersExperts has multiple implementations, and we need to specify keywords to tell the wrapper module which implementation we want to use and how we plan to use it. + +In the source code of the transformer, MoE is implemented using nn.ModuleList. We do not want KTransformers to traverse all submodules in the list and inject them one by one, so in this rule, we set recursive: False to prevent recursive injection into the submodules of this module. The YAML rule is as follows: + +```yaml +- match: + name: "^model\\.layers\\..*\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # Custom MoE kernel with expert parallelism + kwargs: + generate_device: "cpu" + generate_op: "MLPCPUExperts" + out_device: "cuda" + recursive: False # Don't recursively inject submodules of this module +``` + +If we inject Routed Experts as a custom module, we cannot use the interfaces in the original `nn.ModuleList`. Therefore, it is necessary to modify the forward function in the FFN module. The simplest method is to implement a new module with a custom forward function and inject it. +```yaml +- match: + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # MLP module with custom forward function +``` + +## Injection of Linear Layers + +For the remaining linear layer modules, we aim to use quantized operators to save storage space while improving performance. Since there is no current research on using MLA and quantization together, we do not want to inject linear into the MLA operator. Therefore, we can modify the regular expression and add a type check in the match part of the rule. Only modules that match both the name and class simultaneously will be injected. We also need to pass some keywords similar to the injection of Routed Experts. The YAML rule is as follows: + +```yaml +- match: + name: "^model\\.layers\\.(?!.*self_attn).*$" # Regular expression + class: torch.nn.Linear # Only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # Optimized kernel on quantized data types + kwargs: + generate_device: "cuda" + generate_op: "QuantizedLinearMarlin" +``` +## Injection of Modules with Pre-calculated Buffers + +To avoid occupying resources when initializing the injected original model, we use torch’s meta device to initialize the original model. The RoPE module pre-calculates some buffers during initialization, but no calculations are performed when using the meta device. Therefore, we need to compensate for the calculation of the buffer when loading the model. Simply, we inject a custom module into the rotary embedding module, which performs pre-calculation during loading. The YAML rule is as follows: +```yaml +- match: + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding +``` + +## Specifying Running Devices for Modules + +Finally, we set a fallback basic attribute generate_device for all modules: +```yaml +- match: + name: "^model\\.layers\\..*\\.|^lm_head" + replace: + class: "default" + kwargs: + generate_device: "cuda" + +- match: + name: "^model.embed_tokens" + replace: + class: "default" + kwargs: + generate_device: "cpu" +``` +Through these two rules, we place all previously unmatched layers (and their submodules) and lm_head on cuda, and the embedding on cpu. Note that the properties of a module will be determined by the first rule it matches. For example, if you later set a new replace.kwargs.generate_device in an injected module, the device set earlier will take precedence. If your computer has multiple cards, you can also configure the model to multiple cards. + + +## Muti-GPU + +If you have multiple GPUs, you can set the device for each module to different GPUs. +DeepseekV2-Chat got 60 layers, if we got 2 GPUs, we can allocate 30 layers to each GPU. Complete multi GPU rule examples [here](ktransformers/optimize/optimize_rules). + + +

+ + Inject-Struction + +

+ +First of all, for multi-GPU, we have to inject an new operator `KDeepseekV2Model`. And set division of the layers to different GPUs. For our case, we have to set the `transfer_map` in the `KDeepseekV2Model` operatoras as follows: + +```yaml +- match: + name: "^model$" + replace: + class: "ktransformers.operators.models.KDeepseekV2Model" + kwargs: + transfer_map: + 30: "cuda:1" +``` + +And we have to set the device for each module in the model. + +For example, for `routed experts`, the yaml for one GPU is: +```yaml +- match: + name: "^model\\.layers\\..*\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # Custom MoE kernel with expert parallelism + kwargs: + generate_device: "cuda:0" + generate_op: "MLPCUDAExperts" + out_device: "cuda:0" + recursive: False # Don't recursively inject submodules of this module +``` +But for two GPUs, we need to set the device for each module in the model. + +```yaml +# allcate 0-29 layers‘s out_device to cuda:0 +- match: + name: "^model\\.layers\\.(0|[1-9]|[12][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:0" + recursive: False # don't recursively inject submodules of this module + +# allocate 30-59 layers‘s out_device to cuda:1 +- match: + name: "^model\\.layers\\.([345][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:1" + recursive: False # don't recursively inject submodules of this module +``` +For other modules, we can set the device in the same way. + +## How to Write a New Operator and Inject into the Model + +In this section, we will explain how to write an operator that can be injected, using the implementation of a new linear as an example. + +First, all injectable operators need to inherit from the BaseInjectedModule class, which inherits some attributes required by our injection framework. Its initialization function needs to meet the following basic format: + +```python +class LinearTorchInject(BaseInjectedModule): + def __init__( + self, + key: str, + gguf_loader: GGUFLoader, + config: PretrainedConfig, + orig_module: nn.Module = None, + generate_device: str = "cuda", + **kwargs, + ): + super().__init__(key, gguf_loader, config, orig_module, generate_device, **kwargs) +``` +If users have other parameters that need to be passed to this class, they can also be included in the init function and re-passed in the kwargs parameter in the yaml file. For example, if our operator wants to pass a parameter `my_param`, the init function can be written as: +```python +class LinearTorchInject(BaseInjectedModule): + def __init__( + self, + key: str, + gguf_loader: GGUFLoader, + config: PretrainedConfig, + orig_module: nn.Module = None, + generate_device: str = "cuda", + my_param: bool = True, + **kwargs, + ): + super().__init__(key, gguf_loader, config, orig_module, generate_device, **kwargs) + self.my_param = my_param +``` +Then our injection rule can be written as: +```yaml +- match: + name: "^model\\.layers\\..*$" # Regular expression matches the module name. + class: torch.nn.Linear # Type restrictions can be added. + replace: + class: ktransformers.operators.linear.LinearTorchInject # Inject module path + kwargs: # Extra parameters + generate_device: "cuda" + my_param: True +``` +For the linear module, it is also necessary to read weights from a gguf file. We provide the `KLinearBase` class to help users read weights from gguf files. Users only need to inherit and implement the load, unload, and forward functions. Therefore, a fully injectable linear class would look like this: +```python +class LinearTorchInject(BaseInjectedModule, KLinearBase): + def __init__( + self, + key: str, + gguf_loader: GGUFLoader, + config: PretrainedConfig, + orig_module: nn.Module = None, + generate_device: str = "cuda", + **kwargs, + ): + super().__init__(key, gguf_loader, config, orig_module, generate_device, **kwargs) + KLinearBase.__init__(self) + self.has_bias = False + self.dtype = torch.get_default_dtype() + self.w = None + self.has_bias = False + + def load(self, w: dict | nn.Parameter | tuple | None = None, device: str|None = None): + if device is None: device = self.device + if w is None: w = self.load_weight(device=device) + + if isinstance(w, nn.Parameter): + self.w = w.to(dtype=self.dtype).view(self.out_features, self.in_features).T + self.has_bias = False + elif isinstance(w, tuple): + self.w = w[0].to(dtype=self.dtype).view(self.out_features, self.in_features).T + self.bias = w[1].to(dtype=self.dtype) + self.has_bias = True + else: + raise ValueError("Invalid weight type") + self.w = self.w.to(device) + if self.has_bias: + self.bias = self.bias.to(device) + + def unload(self): + if self.w is not None: + self.w = None + if self.has_bias: + self.bias = None + + def forward(self, x: torch.Tensor) -> torch.Tensor: + dtype = x.dtype + out_device = x.device + x = x.to(device=self.device, dtype=self.dtype) + x = x @ self.w + if self.has_bias: + x = x + self.bias + x = x.to(dtype=dtype, device=out_device) + return x +``` +Note that the `self.load_weight` function is provided by the KLinearBase class to help users load weights from a gguf file into the module. The implementation details of KLinearBase can be found on [GITHUB](https://github.com/kvcache-ai/ktransformers/blob/44f57270c9514d79fab224186d90ccf61059331a/ktransformers/operators/linear.py#L31). diff --git a/ktransformers/__init__.py b/ktransformers/__init__.py index d1f2e39..48fef32 100644 --- a/ktransformers/__init__.py +++ b/ktransformers/__init__.py @@ -1 +1 @@ -__version__ = "0.1.1" \ No newline at end of file +__version__ = "0.1.2" \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/CMakeLists.txt b/ktransformers/ktransformers_ext/CMakeLists.txt index 89647a8..e6e0518 100644 --- a/ktransformers/ktransformers_ext/CMakeLists.txt +++ b/ktransformers/ktransformers_ext/CMakeLists.txt @@ -22,14 +22,13 @@ option(LLAMA_AVX2 "llama: enable AVX2" option(LLAMA_AVX512 "llama: enable AVX512" OFF) option(LLAMA_AVX512_VBMI "llama: enable AVX512-VBMI" OFF) option(LLAMA_AVX512_VNNI "llama: enable AVX512-VNNI" OFF) +option(LLAMA_AVX512_BF16 "llama: enable AVX512-BF16" OFF) option(LLAMA_FMA "llama: enable FMA" OFF) # in MSVC F16C is implied with AVX2/AVX512 if (NOT MSVC) option(LLAMA_F16C "llama: enable F16C" OFF) endif() option(LLAMA_AVX512_FANCY_SIMD "llama: enable AVX512-VL, AVX512-BW, AVX512-DQ, AVX512-VNNI" OFF) -option(LLAMA_AVX512_BF16 "llama: enable AVX512-BF16" OFF) - # Architecture specific # TODO: probably these flags need to be tweaked on some architectures diff --git a/ktransformers/ktransformers_ext/bench/bench_linear.py b/ktransformers/ktransformers_ext/bench/bench_linear.py index 0a4de3a..3189afd 100644 --- a/ktransformers/ktransformers_ext/bench/bench_linear.py +++ b/ktransformers/ktransformers_ext/bench/bench_linear.py @@ -6,7 +6,7 @@ Date : 2024-07-25 10:31:59 Version : 1.0.0 LastEditors : chenht2022 -LastEditTime : 2024-07-25 10:32:51 +LastEditTime : 2024-08-06 10:35:35 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' import os, sys @@ -15,15 +15,18 @@ import cpuinfer_ext import torch +input_size = 16384 +output_size = 5120 +stride = 16 +group_max_len = 1024 +layer_num = 10 +qlen = 1 +CPUInfer = cpuinfer_ext.CPUInfer(64) +warm_up_iter = 1000 +test_iter = 10000 + def bench_linear(quant_mode: str): with torch.inference_mode(mode=True): - input_size = 16384 - output_size = 5120 - stride = 16 - layer_num = 10 - CPUInfer = cpuinfer_ext.CPUInfer(64) - warm_up_iter = 1000 - test_iter = 10000 hidden_type = 30 # ggml_type::GGML_TYPE_BF16 if quant_mode == "fp32": @@ -66,30 +69,37 @@ def bench_linear(quant_mode: str): projs = [] for _ in range(layer_num): proj = torch.randn((output_size, input_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() - config = cpuinfer_ext.linear.LinearConfig(input_size, output_size, stride, proj.data_ptr(), proj_type, hidden_type) + config = cpuinfer_ext.linear.LinearConfig(input_size, output_size, stride, group_max_len, proj.data_ptr(), proj_type, hidden_type) linear = cpuinfer_ext.linear.Linear(config) projs.append(proj) linears.append(linear) + input = torch.randn((layer_num, qlen, input_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() + output = torch.empty((layer_num, qlen, output_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() # warm up for i in range(warm_up_iter): - linear = linears[i % layer_num] - input = torch.randn((1, input_size), dtype=torch.bfloat16).contiguous() - output = torch.empty((1, output_size), dtype=torch.bfloat16).contiguous() - CPUInfer.submit(linear.forward, input.data_ptr(), output.data_ptr()) + CPUInfer.submit( + linears[i % layer_num].forward( + qlen, + input[i % layer_num].data_ptr(), + output[i % layer_num].data_ptr() + ) + ) CPUInfer.sync() # test - total_time = 0 + start = time.perf_counter() for i in range(test_iter): - linear = linears[i % layer_num] - input = torch.randn((1, input_size), dtype=torch.bfloat16).contiguous() - output = torch.empty((1, output_size), dtype=torch.bfloat16).contiguous() - start = time.perf_counter() - CPUInfer.submit(linear.forward, input.data_ptr(), output.data_ptr()) + CPUInfer.submit( + linears[i % layer_num].forward( + qlen, + input[i % layer_num].data_ptr(), + output[i % layer_num].data_ptr() + ) + ) CPUInfer.sync() - end = time.perf_counter() - total_time += end - start + end = time.perf_counter() + total_time = end - start print('Quant mode: ', quant_mode) print('Time(s): ', total_time) print('Iteration: ', test_iter) diff --git a/ktransformers/ktransformers_ext/bench/bench_linear_torch.py b/ktransformers/ktransformers_ext/bench/bench_linear_torch.py index cb3e4ef..72e0e75 100644 --- a/ktransformers/ktransformers_ext/bench/bench_linear_torch.py +++ b/ktransformers/ktransformers_ext/bench/bench_linear_torch.py @@ -14,14 +14,17 @@ import torch import torch.nn.quantized as nnq +scale, zero_point = 0.1, 0 # Adjust scale and zero_point based on your dataset + +input_size = 16384 +output_size = 5120 +layer_num = 10 +qlen = 1 +warm_up_iter = 1000 +test_iter = 10000 + def bench_linear(quant_mode: str): with torch.inference_mode(mode=True): - input_size = 16384 - output_size = 5120 - layer_num = 10 - warm_up_iter = 1000 - test_iter = 10000 - if quant_mode == "fp32": proj_type = torch.float32 bytes_per_elem = 4.000000 @@ -41,37 +44,32 @@ def bench_linear(quant_mode: str): for _ in range(layer_num): proj = torch.randn((output_size, input_size), dtype = torch.float32, device = "cuda").to("cpu").contiguous() if quant_mode == "qint8": - scale, zero_point = 0.1, 0 # Adjust scale and zero_point based on your dataset proj_q = torch.quantize_per_tensor(proj, scale, zero_point, torch.qint8) quantized_layer = nnq.Linear(input_size, output_size) quantized_layer.set_weight_bias(proj_q, None) projs.append(quantized_layer) else: projs.append(proj.to(proj_type)) + input = torch.randn((layer_num, qlen, input_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() # warm up for i in range(warm_up_iter): - input = torch.randn((1, input_size), dtype=torch.float32).contiguous() - if quant_mode == "qint8": - input_q = torch.quantize_per_tensor(input, scale, zero_point, torch.quint8) - quantized_layer = projs[i % layer_num] - t_output = quantized_layer(input_q) + if isinstance(projs[i % layer_num], nnq.Linear): + input_q = torch.quantize_per_tensor(input[i % layer_num].to(torch.float32), scale, zero_point, torch.quint8) + t_output = projs[i % layer_num](input_q) else: - t_output = torch.mm(input.to(proj_type), projs[i % layer_num].t()) + t_output = torch.mm(input[i % layer_num].to(proj_type), projs[i % layer_num].t()) # test - total_time = 0 + start = time.perf_counter() for i in range(test_iter): - input = torch.randn((1, input_size), dtype=torch.float32).contiguous() - start = time.perf_counter() - if quant_mode == "qint8": - input_q = torch.quantize_per_tensor(input, scale, zero_point, torch.quint8) - quantized_layer = projs[i % layer_num] - t_output = quantized_layer(input_q) + if isinstance(projs[i % layer_num], nnq.Linear): + input_q = torch.quantize_per_tensor(input[i % layer_num].to(torch.float32), scale, zero_point, torch.quint8) + t_output = projs[i % layer_num](input_q) else: - t_output = torch.mm(input.to(proj_type), projs[i % layer_num].t()) - end = time.perf_counter() - total_time += end - start + t_output = torch.mm(input[i % layer_num].to(proj_type), projs[i % layer_num].t()) + end = time.perf_counter() + total_time = end - start print('Quant mode: ', quant_mode) print('Time(s): ', total_time) print('Iteration: ', test_iter) diff --git a/ktransformers/ktransformers_ext/bench/bench_mlp.py b/ktransformers/ktransformers_ext/bench/bench_mlp.py index 5680a9b..690f9d9 100644 --- a/ktransformers/ktransformers_ext/bench/bench_mlp.py +++ b/ktransformers/ktransformers_ext/bench/bench_mlp.py @@ -6,7 +6,7 @@ Date : 2024-07-16 10:43:18 Version : 1.0.0 LastEditors : chenht2022 -LastEditTime : 2024-07-25 10:32:55 +LastEditTime : 2024-08-06 10:36:04 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' import os, sys @@ -15,15 +15,18 @@ import cpuinfer_ext import torch +hidden_size = 5120 +intermediate_size = 3072 +stride = 16 +group_max_len = 1024 +layer_num = 10 +qlen = 1 +CPUInfer = cpuinfer_ext.CPUInfer(64) +warm_up_iter = 1000 +test_iter = 10000 + def bench_mlp(quant_mode: str): with torch.inference_mode(mode=True): - hidden_size = 5120 - intermediate_size = 3072 - stride = 16 - layer_num = 10 - CPUInfer = cpuinfer_ext.CPUInfer(64) - warm_up_iter = 1000 - test_iter = 10000 hidden_type = 30 # ggml_type::GGML_TYPE_BF16 if quant_mode == "fp32": @@ -93,32 +96,39 @@ def bench_mlp(quant_mode: str): gate_proj = torch.randn((intermediate_size, hidden_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() up_proj = torch.randn((intermediate_size, hidden_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() down_proj = torch.randn((hidden_size, intermediate_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() - config = cpuinfer_ext.mlp.MLPConfig(hidden_size, intermediate_size, stride, gate_proj.data_ptr(), up_proj.data_ptr(), down_proj.data_ptr(), gate_type, up_type, down_type, hidden_type) + config = cpuinfer_ext.mlp.MLPConfig(hidden_size, intermediate_size, stride, group_max_len, gate_proj.data_ptr(), up_proj.data_ptr(), down_proj.data_ptr(), gate_type, up_type, down_type, hidden_type) mlp = cpuinfer_ext.mlp.MLP(config) gate_projs.append(gate_proj) up_projs.append(up_proj) down_projs.append(down_proj) mlps.append(mlp) + input = torch.randn((layer_num, qlen, hidden_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() + output = torch.empty((layer_num, qlen, hidden_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() # warm up for i in range(warm_up_iter): - mlp = mlps[i % layer_num] - input = torch.randn((1, hidden_size), dtype=torch.bfloat16).contiguous() - output = torch.empty((1, hidden_size), dtype=torch.bfloat16).contiguous() - CPUInfer.submit(mlp.forward, input.data_ptr(), output.data_ptr()) + CPUInfer.submit( + mlps[i % layer_num].forward( + qlen, + input[i % layer_num].data_ptr(), + output[i % layer_num].data_ptr() + ) + ) CPUInfer.sync() # test - total_time = 0 + start = time.perf_counter() for i in range(test_iter): - mlp = mlps[i % layer_num] - input = torch.randn((1, hidden_size), dtype=torch.bfloat16).contiguous() - output = torch.empty((1, hidden_size), dtype=torch.bfloat16).contiguous() - start = time.perf_counter() - CPUInfer.submit(mlp.forward, input.data_ptr(), output.data_ptr()) + CPUInfer.submit( + mlps[i % layer_num].forward( + qlen, + input[i % layer_num].data_ptr(), + output[i % layer_num].data_ptr() + ) + ) CPUInfer.sync() - end = time.perf_counter() - total_time += end - start + end = time.perf_counter() + total_time = end - start print('Quant mode: ', quant_mode) print('Time(s): ', total_time) print('Iteration: ', test_iter) diff --git a/ktransformers/ktransformers_ext/bench/bench_mlp_torch.py b/ktransformers/ktransformers_ext/bench/bench_mlp_torch.py index 3aad58c..7b811d8 100644 --- a/ktransformers/ktransformers_ext/bench/bench_mlp_torch.py +++ b/ktransformers/ktransformers_ext/bench/bench_mlp_torch.py @@ -14,17 +14,38 @@ import torch import torch.nn.quantized as nnq +scale, zero_point = 0.1, 0 # Adjust scale and zero_point based on your dataset + +hidden_size = 5120 +intermediate_size = 3072 +layer_num = 10 +qlen = 1 +warm_up_iter = 1000 +test_iter = 10000 + def act_fn(x): return x / (1.0 + torch.exp(-x)) +def mlp_torch(input, gate_proj, up_proj, down_proj): + if isinstance(gate_proj, nnq.Linear): + input_q = torch.quantize_per_tensor(input.to(torch.float32), scale, zero_point, torch.quint8) + gate_buf = gate_proj(input_q) + up_buf = up_proj(input_q) + gate_buf = gate_buf.dequantize() + up_buf = up_buf.dequantize() + intermediate = act_fn(gate_buf) * up_buf + intermediate_q = torch.quantize_per_tensor(intermediate, scale, zero_point, torch.quint8) + expert_output = down_proj(intermediate_q) + ret = expert_output.dequantize() + else: + gate_buf = torch.mm(input.to(gate_proj.dtype), gate_proj.t()) + up_buf = torch.mm(input.to(up_proj.dtype), up_proj.t()) + intermediate = act_fn(gate_buf) * up_buf + ret = torch.mm(intermediate.to(down_proj.dtype), down_proj.t()) + return ret + def bench_mlp(quant_mode: str): with torch.inference_mode(mode=True): - hidden_size = 5120 - intermediate_size = 3072 - layer_num = 10 - warm_up_iter = 1000 - test_iter = 10000 - if quant_mode == "fp32": proj_type = torch.float32 bytes_per_elem = 4.000000 @@ -48,7 +69,6 @@ def bench_mlp(quant_mode: str): up_proj = torch.randn((intermediate_size, hidden_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() down_proj = torch.randn((hidden_size, intermediate_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() if quant_mode == "qint8": - scale, zero_point = 0.1, 0 # Adjust scale and zero_point based on your dataset gate_proj_q = torch.quantize_per_tensor(gate_proj, scale, zero_point, torch.qint8) quantized_gate = nnq.Linear(hidden_size, intermediate_size) quantized_gate.set_weight_bias(gate_proj_q, None) @@ -65,58 +85,18 @@ def bench_mlp(quant_mode: str): gate_projs.append(gate_proj.to(proj_type)) up_projs.append(up_proj.to(proj_type)) down_projs.append(down_proj.to(proj_type)) + input = torch.randn((layer_num, qlen, hidden_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() # warm up for i in range(warm_up_iter): - input = torch.randn((1, hidden_size), dtype=torch.float32).contiguous() - if quant_mode == "qint8": - input_q = torch.quantize_per_tensor(input, scale, zero_point, torch.quint8) - quantized_gate = gate_projs[i % layer_num] - gate_buf = quantized_gate(input_q) - quantized_up = up_projs[i % layer_num] - up_buf = quantized_gate(input_q) - gate_buf = gate_buf.dequantize() - up_buf = up_buf.dequantize() - intermediate = act_fn(gate_buf) * up_buf - intermediate_q = torch.quantize_per_tensor(intermediate, scale, zero_point, torch.quint8) - quantized_down = down_projs[i % layer_num] - t_output = quantized_down(intermediate_q) - else: - gate_proj = gate_projs[i%layer_num] - up_proj = up_projs[i%layer_num] - down_proj = down_projs[i%layer_num] - gate_buf = torch.mm(input.to(proj_type), gate_proj.t()) - up_buf = torch.mm(input.to(proj_type), up_proj.t()) - intermediate = act_fn(gate_buf) * up_buf - t_output = torch.mm(intermediate.to(proj_type), down_proj.t()) + mlp_torch(input[i % layer_num], gate_projs[i % layer_num], up_projs[i % layer_num], down_projs[i % layer_num]) # test - total_time = 0 + start = time.perf_counter() for i in range(test_iter): - input = torch.randn((1, hidden_size), dtype=torch.float32).contiguous() - start = time.perf_counter() - if quant_mode == "qint8": - input_q = torch.quantize_per_tensor(input, scale, zero_point, torch.quint8) - quantized_gate = gate_projs[i % layer_num] - gate_buf = quantized_gate(input_q) - quantized_up = up_projs[i % layer_num] - up_buf = quantized_gate(input_q) - gate_buf = gate_buf.dequantize() - up_buf = up_buf.dequantize() - intermediate = act_fn(gate_buf) * up_buf - intermediate_q = torch.quantize_per_tensor(intermediate, scale, zero_point, torch.quint8) - quantized_down = down_projs[i % layer_num] - t_output = quantized_down(intermediate_q) - else: - gate_proj = gate_projs[i%layer_num] - up_proj = up_projs[i%layer_num] - down_proj = down_projs[i%layer_num] - gate_buf = torch.mm(input.to(proj_type), gate_proj.t()) - up_buf = torch.mm(input.to(proj_type), up_proj.t()) - intermediate = act_fn(gate_buf) * up_buf - t_output = torch.mm(intermediate.to(proj_type), down_proj.t()) - end = time.perf_counter() - total_time += end - start + mlp_torch(input[i % layer_num], gate_projs[i % layer_num], up_projs[i % layer_num], down_projs[i % layer_num]) + end = time.perf_counter() + total_time = end - start print('Quant mode: ', quant_mode) print('Time(s): ', total_time) print('Iteration: ', test_iter) diff --git a/ktransformers/ktransformers_ext/bench/bench_moe.py b/ktransformers/ktransformers_ext/bench/bench_moe.py index 909f029..6d617b7 100644 --- a/ktransformers/ktransformers_ext/bench/bench_moe.py +++ b/ktransformers/ktransformers_ext/bench/bench_moe.py @@ -6,7 +6,7 @@ Date : 2024-07-25 10:32:05 Version : 1.0.0 LastEditors : chenht2022 -LastEditTime : 2024-07-25 10:33:00 +LastEditTime : 2024-08-06 10:41:28 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' import os, sys @@ -15,21 +15,21 @@ import cpuinfer_ext import torch +expert_num = 160 +hidden_size = 5120 +intermediate_size = 1536 +stride = 16 +group_min_len = 10 +group_max_len = 1024 +n_routed_experts = 6 +layer_num = 10 +qlen = 1 +CPUInfer = cpuinfer_ext.CPUInfer(64) +warm_up_iter = 1000 +test_iter = 10000 + def bench_moe(quant_mode: str): with torch.inference_mode(mode=True): - expert_num = 10 - hidden_size = 5120 - intermediate_size = 1536 - stride = 16 - group_min_len = 10 - group_max_len = 1024 - n_routed_experts = 6 - layer_num = 10 - qlen = 1 - CPUInfer = cpuinfer_ext.CPUInfer(64) - warm_up_iter = 1000 - test_iter = 10000 - hidden_type = 30 # ggml_type::GGML_TYPE_BF16 if quant_mode == "fp32": gate_type = 0 # ggml_type::GGML_TYPE_F32 @@ -104,32 +104,38 @@ def bench_moe(quant_mode: str): up_projs.append(up_proj) down_projs.append(down_proj) moes.append(moe) - expert_ids = torch.randint(0, expert_num, (layer_num, qlen, n_routed_experts), dtype=torch.int64, device = "cuda").to("cpu").contiguous() + expert_ids = torch.stack([torch.stack([torch.randperm(expert_num, dtype=torch.int64, device = "cuda")[:n_routed_experts] for _ in range(qlen)]) for _ in range(layer_num)]).to("cpu").contiguous() weights = torch.rand((layer_num, qlen, n_routed_experts), dtype=torch.float32, device = "cuda").to("cpu").contiguous() input = torch.randn((layer_num, qlen, hidden_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() output = torch.empty((layer_num, qlen, hidden_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() # warm up for i in range(warm_up_iter): - CPUInfer.submit(moes[i % layer_num].forward, - qlen, - n_routed_experts, - expert_ids[i % layer_num].data_ptr(), - weights[i % layer_num].data_ptr(), - input[i % layer_num].data_ptr(), - output[i % layer_num].data_ptr()) + CPUInfer.submit( + moes[i % layer_num].forward( + qlen, + n_routed_experts, + expert_ids[i % layer_num].data_ptr(), + weights[i % layer_num].data_ptr(), + input[i % layer_num].data_ptr(), + output[i % layer_num].data_ptr() + ) + ) CPUInfer.sync() # test start = time.perf_counter() for i in range(test_iter): - CPUInfer.submit(moes[i % layer_num].forward, - qlen, - n_routed_experts, - expert_ids[i % layer_num].data_ptr(), - weights[i % layer_num].data_ptr(), - input[i % layer_num].data_ptr(), - output[i % layer_num].data_ptr()) + CPUInfer.submit( + moes[i % layer_num].forward( + qlen, + n_routed_experts, + expert_ids[i % layer_num].data_ptr(), + weights[i % layer_num].data_ptr(), + input[i % layer_num].data_ptr(), + output[i % layer_num].data_ptr() + ) + ) CPUInfer.sync() end = time.perf_counter() total_time = end - start diff --git a/ktransformers/ktransformers_ext/bench/bench_moe_torch.py b/ktransformers/ktransformers_ext/bench/bench_moe_torch.py index 5075636..1aecf40 100644 --- a/ktransformers/ktransformers_ext/bench/bench_moe_torch.py +++ b/ktransformers/ktransformers_ext/bench/bench_moe_torch.py @@ -14,19 +14,71 @@ import torch import torch.nn.quantized as nnq +scale, zero_point = 0.1, 0 # Adjust scale and zero_point based on your dataset + +expert_num = 160 +hidden_size = 5120 +intermediate_size = 1536 +n_routed_experts = 6 +layer_num = 10 +qlen = 1 +warm_up_iter = 1000 +test_iter = 10000 + def act_fn(x): return x / (1.0 + torch.exp(-x)) +def mlp_torch(input, gate_proj, up_proj, down_proj): + if isinstance(gate_proj, nnq.Linear): + input_q = torch.quantize_per_tensor(input.to(torch.float32), scale, zero_point, torch.quint8) + gate_buf = gate_proj(input_q) + up_buf = up_proj(input_q) + gate_buf = gate_buf.dequantize() + up_buf = up_buf.dequantize() + intermediate = act_fn(gate_buf) * up_buf + intermediate_q = torch.quantize_per_tensor(intermediate, scale, zero_point, torch.quint8) + expert_output = down_proj(intermediate_q) + ret = expert_output.dequantize() + else: + gate_buf = torch.mm(input.to(gate_proj.dtype), gate_proj.t()) + up_buf = torch.mm(input.to(up_proj.dtype), up_proj.t()) + intermediate = act_fn(gate_buf) * up_buf + ret = torch.mm(intermediate.to(down_proj.dtype), down_proj.t()) + return ret + +def moe_torch(input, expert_ids, weights, gate_proj, up_proj, down_proj): + cnts = expert_ids.new_zeros((expert_ids.shape[0], expert_num)) + cnts.scatter_(1, expert_ids, 1) + tokens_per_expert = cnts.sum(dim=0) + idxs = expert_ids.view(-1).argsort() + sorted_tokens = input[idxs // expert_ids.shape[1]] + + outputs = [] + start_idx = 0 + for i, num_tokens in enumerate(tokens_per_expert): + end_idx = start_idx + num_tokens + if num_tokens == 0: + continue + tokens_for_this_expert = sorted_tokens[start_idx:end_idx] + expert_out = mlp_torch(tokens_for_this_expert, gate_proj[i], up_proj[i], down_proj[i]) + outputs.append(expert_out) + start_idx = end_idx + + outs = torch.cat(outputs, dim=0) if len(outputs) else sorted_tokens.new_empty(0) + + new_x = torch.empty_like(outs) + new_x[idxs] = outs + t_output = ( + new_x.view(*expert_ids.shape, -1) + .type(weights.dtype) + .mul_(weights.unsqueeze(dim=-1)) + .sum(dim=1) + .type(new_x.dtype) + ) + return t_output + def bench_moe(quant_mode: str): with torch.inference_mode(mode=True): - expert_num = 10 - hidden_size = 5120 - intermediate_size = 1536 - n_routed_experts = 6 - layer_num = 10 - warm_up_iter = 1000 - test_iter = 10000 - if quant_mode == "fp32": proj_type = torch.float32 bytes_per_elem = 4.000000 @@ -50,7 +102,6 @@ def bench_moe(quant_mode: str): up_proj = torch.randn((expert_num, intermediate_size, hidden_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() down_proj = torch.randn((expert_num, hidden_size, intermediate_size), dtype=torch.float32, device = "cuda").to("cpu").contiguous() if quant_mode == "qint8": - scale, zero_point = 0.1, 0 # Adjust scale and zero_point based on your dataset quantized_gate_proj = [] quantized_up_proj = [] quantized_down_proj = [] @@ -74,82 +125,20 @@ def bench_moe(quant_mode: str): gate_projs.append(gate_proj.to(proj_type)) up_projs.append(up_proj.to(proj_type)) down_projs.append(down_proj.to(proj_type)) + expert_ids = torch.stack([torch.stack([torch.randperm(expert_num, dtype=torch.int64, device = "cuda")[:n_routed_experts] for _ in range(qlen)]) for _ in range(layer_num)]).to("cpu").contiguous() + weights = torch.rand((layer_num, qlen, n_routed_experts), dtype=torch.float32, device = "cuda").to("cpu").contiguous() + input = torch.randn((layer_num, qlen, hidden_size), dtype=torch.bfloat16, device = "cuda").to("cpu").contiguous() # warm up for i in range(warm_up_iter): - expert_ids = torch.randint(0, expert_num, (n_routed_experts,), dtype=torch.int64).contiguous() - weights = torch.rand((n_routed_experts,), dtype=torch.float32).contiguous() - input = torch.randn((1, hidden_size), dtype=torch.float32).contiguous() - if quant_mode == "qint8": - input_q = torch.quantize_per_tensor(input, scale, zero_point, torch.quint8) - t_output = torch.zeros((1, hidden_size), dtype=torch.float32).contiguous() - gate_proj = gate_projs[i%layer_num] - up_proj = up_projs[i%layer_num] - down_proj = down_projs[i%layer_num] - for i, expert_id in enumerate(expert_ids): - quantized_gate = gate_proj[expert_id] - gate_buf = quantized_gate(input_q) - quantized_up = up_proj[expert_id] - up_buf = quantized_up(input_q) - gate_buf = gate_buf.dequantize() - up_buf = up_buf.dequantize() - intermediate = act_fn(gate_buf) * up_buf - intermediate_q = torch.quantize_per_tensor(intermediate, scale, zero_point, torch.quint8) - quantized_down = down_proj[expert_id] - expert_output = quantized_down(intermediate_q) - expert_output = expert_output.dequantize() - t_output += weights[i] * expert_output - else: - t_output = torch.zeros((1, hidden_size), dtype=proj_type).contiguous() - gate_proj = gate_projs[i%layer_num] - up_proj = up_projs[i%layer_num] - down_proj = down_projs[i%layer_num] - for i, expert_id in enumerate(expert_ids): - gate_buf = torch.mm(input.to(proj_type), gate_proj[expert_id].t()) - up_buf = torch.mm(input.to(proj_type), up_proj[expert_id].t()) - intermediate = act_fn(gate_buf) * up_buf - expert_output = torch.mm(intermediate.to(proj_type), down_proj[expert_id].t()) - t_output += weights[i] * expert_output + moe_torch(input[i % layer_num], expert_ids[i % layer_num], weights[i % layer_num], gate_projs[i % layer_num], up_projs[i % layer_num], down_projs[i % layer_num]) # test - total_time = 0 + start = time.perf_counter() for i in range(test_iter): - expert_ids = torch.randint(0, expert_num, (n_routed_experts,), dtype=torch.int64).contiguous() - weights = torch.rand((n_routed_experts,), dtype=torch.float32).contiguous() - input = torch.randn((1, hidden_size), dtype=torch.float32).contiguous() - start = time.perf_counter() - if quant_mode == "qint8": - input_q = torch.quantize_per_tensor(input, scale, zero_point, torch.quint8) - t_output = torch.zeros((1, hidden_size), dtype=torch.float32).contiguous() - gate_proj = gate_projs[i%layer_num] - up_proj = up_projs[i%layer_num] - down_proj = down_projs[i%layer_num] - for i, expert_id in enumerate(expert_ids): - quantized_gate = gate_proj[expert_id] - gate_buf = quantized_gate(input_q) - quantized_up = up_proj[expert_id] - up_buf = quantized_up(input_q) - gate_buf = gate_buf.dequantize() - up_buf = up_buf.dequantize() - intermediate = act_fn(gate_buf) * up_buf - intermediate_q = torch.quantize_per_tensor(intermediate, scale, zero_point, torch.quint8) - quantized_down = down_proj[expert_id] - expert_output = quantized_down(intermediate_q) - expert_output = expert_output.dequantize() - t_output += weights[i] * expert_output - else: - t_output = torch.zeros((1, hidden_size), dtype=proj_type).contiguous() - gate_proj = gate_projs[i%layer_num] - up_proj = up_projs[i%layer_num] - down_proj = down_projs[i%layer_num] - for i, expert_id in enumerate(expert_ids): - gate_buf = torch.mm(input.to(proj_type), gate_proj[expert_id].t()) - up_buf = torch.mm(input.to(proj_type), up_proj[expert_id].t()) - intermediate = act_fn(gate_buf) * up_buf - expert_output = torch.mm(intermediate.to(proj_type), down_proj[expert_id].t()) - t_output += weights[i] * expert_output - end = time.perf_counter() - total_time += end - start + moe_torch(input[i % layer_num], expert_ids[i % layer_num], weights[i % layer_num], gate_projs[i % layer_num], up_projs[i % layer_num], down_projs[i % layer_num]) + end = time.perf_counter() + total_time = end - start print('Quant mode: ', quant_mode) print('Time(s): ', total_time) print('Iteration: ', test_iter) diff --git a/ktransformers/ktransformers_ext/cpu_backend/cpuinfer.h b/ktransformers/ktransformers_ext/cpu_backend/cpuinfer.h index eae6f90..9618e6b 100644 --- a/ktransformers/ktransformers_ext/cpu_backend/cpuinfer.h +++ b/ktransformers/ktransformers_ext/cpu_backend/cpuinfer.h @@ -1,12 +1,12 @@ /** - * @Description : + * @Description : * @Author : chenht2022 * @Date : 2024-07-16 10:43:18 * @Version : 1.0.0 - * @LastEditors : chenht2022 - * @LastEditTime : 2024-07-25 10:33:42 - * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. -**/ + * @LastEditors : chenht2022 + * @LastEditTime : 2024-08-07 09:47:43 + * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. + **/ #ifndef CPUINFER_CPUINFER_H #define CPUINFER_CPUINFER_H @@ -17,6 +17,7 @@ #include #include #include +#include "cuda_runtime.h" #include "backend.h" #include "task_queue.h" @@ -39,16 +40,39 @@ class CPUInfer { } template - void submit(Func f, Obj* obj, Args... args) { + void enqueue(Func f, Obj* obj, Args... args) { task_queue_->enqueue([=]() { std::invoke(f, *obj, args..., backend_); }); } + void submit(std::pair params) { + void (*func)(void*) = (void (*)(void*))params.first; + void* args = (void*)params.second; + *((CPUInfer**)args) = this; + func(args); + } + void sync() { task_queue_->sync(); } + void submit_with_cuda_stream(intptr_t user_cuda_stream, std::pair params) { + void (*func)(void*) = (void (*)(void*))params.first; + void* args = (void*)params.second; + *((CPUInfer**)args) = this; + cudaLaunchHostFunc((cudaStream_t)user_cuda_stream, (cudaHostFn_t)func, args); + } + + static void sync_(void* cpu_infer_ptr) { + CPUInfer* cpuinfer = (CPUInfer*)cpu_infer_ptr; + cpuinfer->sync(); + } + + void sync_with_cuda_stream(intptr_t user_cuda_stream) { + cudaLaunchHostFunc((cudaStream_t)user_cuda_stream, (cudaHostFn_t)&sync_, (void*)this); + } + public: Backend* backend_; TaskQueue* task_queue_; diff --git a/ktransformers/ktransformers_ext/cpu_backend/task_queue.h b/ktransformers/ktransformers_ext/cpu_backend/task_queue.h index a633a40..13836b7 100644 --- a/ktransformers/ktransformers_ext/cpu_backend/task_queue.h +++ b/ktransformers/ktransformers_ext/cpu_backend/task_queue.h @@ -4,7 +4,7 @@ * @Date : 2024-07-16 10:43:18 * @Version : 1.0.0 * @LastEditors : chenxl - * @LastEditTime : 2024-08-08 04:23:51 + * @LastEditTime : 2024-08-12 12:28:25 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ #ifndef CPUINFER_TASKQUEUE_H diff --git a/ktransformers/ktransformers_ext/cuda/binding.cpp b/ktransformers/ktransformers_ext/cuda/binding.cpp index 2d5da68..06ec5f3 100644 --- a/ktransformers/ktransformers_ext/cuda/binding.cpp +++ b/ktransformers/ktransformers_ext/cuda/binding.cpp @@ -3,8 +3,8 @@ * @Author : Azure-Tang * @Date : 2024-07-25 13:38:30 * @Version : 1.0.0 - * @LastEditors : Azure - * @LastEditTime : 2024-07-26 08:36:03 + * @LastEditors : kkk1nak0 + * @LastEditTime : 2024-08-12 03:05:04 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ @@ -23,8 +23,14 @@ PYBIND11_MODULE(KTransformersOps, m) { py::arg("data"), py::arg("blk_size"), py::arg("device")); m.def("dequantize_q6_k", &dequantize_q6_k, "Function to dequantize q6_k data.", py::arg("data"), py::arg("blk_size"), py::arg("device")); + m.def("dequantize_q5_k", &dequantize_q5_k, "Function to dequantize q5_k data.", + py::arg("data"), py::arg("blk_size"), py::arg("device")); m.def("dequantize_q4_k", &dequantize_q4_k, "Function to dequantize q4_k data.", py::arg("data"), py::arg("blk_size"), py::arg("device")); + m.def("dequantize_q3_k", &dequantize_q3_k, "Function to dequantize q3_k data.", + py::arg("data"), py::arg("blk_size"), py::arg("device")); + m.def("dequantize_q2_k", &dequantize_q2_k, "Function to dequantize q2_k data.", + py::arg("data"), py::arg("blk_size"), py::arg("device")); m.def("gptq_marlin_gemm", &gptq_marlin_gemm, "Function to perform GEMM using Marlin quantization.", py::arg("a"), py::arg("b_q_weight"), py::arg("b_scales"), py::arg("g_idx"), py::arg("perm"), py::arg("workspace"), py::arg("num_bits"), py::arg("size_m"), diff --git a/ktransformers/ktransformers_ext/cuda/custom_gguf/binding.cpp b/ktransformers/ktransformers_ext/cuda/custom_gguf/binding.cpp index ea52e8f..70fc606 100644 --- a/ktransformers/ktransformers_ext/cuda/custom_gguf/binding.cpp +++ b/ktransformers/ktransformers_ext/cuda/custom_gguf/binding.cpp @@ -12,14 +12,22 @@ int test(){ } torch::Tensor dequantize_q6_k(torch::Tensor data, int blk_size, torch::Device device); +torch::Tensor dequantize_q5_k(torch::Tensor data, int blk_size, torch::Device device); +torch::Tensor dequantize_q2_k(torch::Tensor data, int blk_size, torch::Device device); PYBIND11_MODULE(cudaops, m) { m.def("dequantize_q8_0", &dequantize_q8_0, "Function to dequantize q8_0 data.", py::arg("data"), py::arg("blk_size"), py::arg("device")); m.def("dequantize_q6_k", &dequantize_q6_k, "Function to dequantize q6_k data.", py::arg("data"), py::arg("blk_size"), py::arg("device")); + m.def("dequantize_q5_k", &dequantize_q5_k, "Function to dequantize q5_k data.", + py::arg("data"), py::arg("blk_size"), py::arg("device")); m.def("dequantize_q4_k", &dequantize_q4_k, "Function to dequantize q4_k data.", py::arg("data"), py::arg("blk_size"), py::arg("device")); + m.def("dequantize_q3_k", &dequantize_q3_k, "Function to dequantize q3_k data.", + py::arg("data"), py::arg("blk_size"), py::arg("device")); + m.def("dequantize_q2_k", &dequantize_q2_k, "Function to dequantize q2_k data.", + py::arg("data"), py::arg("blk_size"), py::arg("device")); m.def("test", &test, "Function to test."); } diff --git a/ktransformers/ktransformers_ext/cuda/custom_gguf/custom_ggml.h b/ktransformers/ktransformers_ext/cuda/custom_gguf/custom_ggml.h deleted file mode 100644 index 333dc69..0000000 --- a/ktransformers/ktransformers_ext/cuda/custom_gguf/custom_ggml.h +++ /dev/null @@ -1,39 +0,0 @@ - - - -#include - - -__device__ float ggml_compute_fp16_to_fp32(uint16_t h) { - return __uint2float_rd(h); -} - -static inline float ggml_compute_fp16_to_fp32(uint16_t h) { - uint16_t tmp; - memcpy(&tmp, &h, sizeof(ggml_fp16_t)); - return (float)tmp; -} - -// define the global table for fp16 to fp32 conversion -__device__ float ggml_table_f32_f16[1 << 16]; - -// CUDA Kernel to init the table -__global__ void init_fp16_to_fp32_table() { - int idx = blockIdx.x * blockDim.x + threadIdx.x; - for (auto blk_id = idx; blk_id<(1 << 16); blk_id+=blockDim.x * gridDim.x){ - ggml_table_f32_f16[blk_id] = GGML_COMPUTE_FP16_TO_FP32(blk_id); - } -} - -#define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x) - -extern __device__ float ggml_table_f32_f16[1 << 16]; // Declare as __device__ if used within device code - -// This version of the function is designed to be called from within a CUDA kernel -#if !defined(GGML_FP16_TO_FP32) -__device__ float ggml_lookup_fp16_to_fp32(uint16_t f) { - return ggml_table_f32_f16[f]; -} - -#define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x) -#endif \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/cuda/custom_gguf/dequant.cu b/ktransformers/ktransformers_ext/cuda/custom_gguf/dequant.cu index 38f4842..cc5552b 100644 --- a/ktransformers/ktransformers_ext/cuda/custom_gguf/dequant.cu +++ b/ktransformers/ktransformers_ext/cuda/custom_gguf/dequant.cu @@ -3,8 +3,8 @@ * @Author : Azure-Tang, Boxin Zhang * @Date : 2024-07-25 13:38:30 * @Version : 1.0.0 - * @LastEditors : Azure - * @LastEditTime : 2024-07-26 11:58:50 + * @LastEditors : kkk1nak0 + * @LastEditTime : 2024-08-12 04:18:04 * Adapted from https://github.com/ggerganov/ggml/blob/fca1caafea7de9fbd7efc733b9818f9cf2da3050/src/ggml-quants.c * Copyright (c) 2023-2024 The ggml authors * Copyright (c) 2024 by KVCache.AI, All Rights Reserved. @@ -14,6 +14,7 @@ #include #include #include +#include __global__ void dequantize_q8_0_kernel(float* output, const float* scales, const int8_t* qs, int num_blocks, int blk_size) { int global_idx = blockIdx.x * blockDim.x + threadIdx.x; @@ -35,6 +36,97 @@ __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t * __restrict_ } } +__global__ void dequantize_q2_k_kernel(int8_t* data, float* output, int blk_size, int num_blocks) { + int global_idx = blockIdx.x * blockDim.x + threadIdx.x; + for (auto block_id=global_idx; block_id(data + block_id * blk_size + 80))); + const float min = __half2float(*(reinterpret_cast(data + block_id * blk_size + 82))); + + const uint8_t * __restrict__ q = (uint8_t*)(data + block_id * blk_size + 16); + + int is = 0; + float dl, ml; + + for (int n = 0; n < 256; n += 128) { + int shift = 0; + for (int j = 0; j < 4; ++j) { + uint8_t* scales = (uint8_t*)(data + block_id * blk_size + (is++)); + uint8_t sc = *scales; + dl = d * (sc & 0xF); ml = min * (sc >> 4); + for (int l = 0; l < 16; ++l) *output_blk++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml; + + scales = (uint8_t*)(data + block_id * blk_size + (is++)); + sc = *scales; + + dl = d * (sc & 0xF); ml = min * (sc >> 4); + for (int l = 0; l < 16; ++l) *output_blk++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml; + + shift += 2; + } + q += 32; + } + } +} + +__global__ void dequantize_q3_k_kernel(int8_t* data, float* output, int blk_size, int num_blocks) { + + int global_idx = blockIdx.x * blockDim.x + threadIdx.x; + const uint32_t kmask1 = 0x03030303; + const uint32_t kmask2 = 0x0f0f0f0f; + for (auto block_id=global_idx; block_id(data + block_id * blk_size + 108))); + + const uint8_t * __restrict__ q = (uint8_t*)(data + block_id * blk_size + 32); + const uint8_t * __restrict__ hm = (uint8_t*)(data + block_id * blk_size + 0); + uint8_t m = 1; + + + uint8_t* block_scales = (uint8_t*)(data + block_id * blk_size + 96); + + for (int i = 0; i < 3; i++) { + aux[i] = 0; + for (int j = 0; j < 4; j++) { + aux[i] |= ((uint32_t)block_scales[i * 4 + j]) << (j * 8); + } + } + + uint32_t tmp = aux[2]; + aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4); + aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4); + aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4); + aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4); + + int is = 0; + float dl; + for (int n = 0; n < 256; n += 128) { + int shift = 0; + for (int j = 0; j < 4; ++j) { + + dl = d_all * (scales[is++] - 32); + for (int l = 0; l < 16; ++l) { + *output_blk++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4)); + } + + dl = d_all * (scales[is++] - 32); + for (int l = 0; l < 16; ++l) { + *output_blk++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4)); + } + + shift += 2; + m <<= 1; + } + q += 32; + } + } +} + + __global__ void dequantize_q4_k_kernel(int8_t* data, float* output, int blk_size, int num_blocks) { int global_idx = blockIdx.x * blockDim.x + threadIdx.x; for (auto block_id=global_idx; block_id(data + block_id * blk_size + 0))); + const float min = __half2float(*(reinterpret_cast(data + block_id * blk_size + 2))); + + const uint8_t * __restrict__ qh = (uint8_t*)(data + block_id * blk_size + 16); + const uint8_t * __restrict__ ql = (uint8_t*)(data + block_id * blk_size + 48); + + int is = 0; + uint8_t sc, m; + uint8_t u1 = 1, u2 = 2; + uint8_t* scales = (uint8_t*)(data + block_id * blk_size + 4); + + for (int j = 0; j < 256; j += 64) { + get_scale_min_k4(is + 0, scales, &sc, &m); + const float d1 = d * sc; const float m1 = min * m; + get_scale_min_k4(is + 1, scales, &sc, &m); + const float d2 = d * sc; const float m2 = min * m; + for (int l = 0; l < 32; ++l) *output_blk++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1; + for (int l = 0; l < 32; ++l) *output_blk++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2; + ql += 32; is += 2; + u1 <<= 2; u2 <<= 2; + } + } +} + __global__ void dequantize_q6_k_kernel(int8_t* data, float* output, int blk_size, int num_blocks) { int global_idx = blockIdx.x * blockDim.x + threadIdx.x; for (auto block_id=global_idx; block_id>>(data_gpu.data_ptr(), output.data_ptr(), blk_size, num_blocks); + + cudaDeviceSynchronize(); + return output; +} + torch::Tensor dequantize_q4_k(torch::Tensor data, int blk_size, torch::Device device) { // data.numel%blk_size should be 0, else raise err int num_blocks = data.numel() / blk_size; + const at::cuda::OptionalCUDAGuard device_guard(device); auto options = torch::TensorOptions().dtype(torch::kInt8).device(device).memory_format(torch::MemoryFormat::Contiguous); auto data_gpu = torch::empty({data.numel()}, options); @@ -162,3 +304,39 @@ torch::Tensor dequantize_q4_k(torch::Tensor data, int blk_size, torch::Device de cudaDeviceSynchronize(); return output; } + +torch::Tensor dequantize_q3_k(torch::Tensor data, int blk_size, torch::Device device) { + int num_blocks = data.numel() / blk_size; + + auto options = torch::TensorOptions().dtype(torch::kInt8).device(device).memory_format(torch::MemoryFormat::Contiguous); + auto data_gpu = torch::empty({data.numel()}, options); + + data_gpu.copy_(data, false); + + // Create output tensor + auto output = torch::zeros({num_blocks, 256}, torch::dtype(torch::kFloat32).device(device)); + + // Launch kernel + dequantize_q3_k_kernel<<< 512, 256 >>>(data_gpu.data_ptr(), output.data_ptr(), blk_size, num_blocks); + + cudaDeviceSynchronize(); + return output; +} + +torch::Tensor dequantize_q2_k(torch::Tensor data, int blk_size, torch::Device device) { + int num_blocks = data.numel() / blk_size; + + auto options = torch::TensorOptions().dtype(torch::kInt8).device(device).memory_format(torch::MemoryFormat::Contiguous); + auto data_gpu = torch::empty({data.numel()}, options); + + data_gpu.copy_(data, false); + + // Create output tensor + auto output = torch::zeros({num_blocks, 256}, torch::dtype(torch::kFloat32).device(device)); + + // Launch kernel + dequantize_q2_k_kernel<<< 512, 256 >>>(data_gpu.data_ptr(), output.data_ptr(), blk_size, num_blocks); + + cudaDeviceSynchronize(); + return output; +} \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/cuda/custom_gguf/ops.h b/ktransformers/ktransformers_ext/cuda/custom_gguf/ops.h index 9af8f30..5196f88 100644 --- a/ktransformers/ktransformers_ext/cuda/custom_gguf/ops.h +++ b/ktransformers/ktransformers_ext/cuda/custom_gguf/ops.h @@ -3,8 +3,8 @@ * @Author : Azure-Tang * @Date : 2024-07-22 09:27:55 * @Version : 1.0.0 - * @LastEditors : Azure - * @LastEditTime : 2024-07-26 08:38:20 + * @LastEditors : kkk1nak0 + * @LastEditTime : 2024-08-12 03:48:46 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ #pragma once @@ -15,4 +15,7 @@ torch::Tensor dequantize_q8_0(torch::Tensor data, int blk_size, torch::Device device); torch::Tensor dequantize_q6_k(torch::Tensor data, int blk_size, torch::Device device); -torch::Tensor dequantize_q4_k(torch::Tensor data, int blk_size, torch::Device device); \ No newline at end of file +torch::Tensor dequantize_q5_k(torch::Tensor data, int blk_size, torch::Device device); +torch::Tensor dequantize_q4_k(torch::Tensor data, int blk_size, torch::Device device); +torch::Tensor dequantize_q3_k(torch::Tensor data, int blk_size, torch::Device device); +torch::Tensor dequantize_q2_k(torch::Tensor data, int blk_size, torch::Device device); \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/cuda/gptq_marlin/gptq_marlin.cu b/ktransformers/ktransformers_ext/cuda/gptq_marlin/gptq_marlin.cu index 9205d3b..54e538a 100644 --- a/ktransformers/ktransformers_ext/cuda/gptq_marlin/gptq_marlin.cu +++ b/ktransformers/ktransformers_ext/cuda/gptq_marlin/gptq_marlin.cu @@ -23,7 +23,7 @@ */ #include "gptq_marlin.cuh" #include "gptq_marlin_dtypes.cuh" - +#include #define STATIC_ASSERT_SCALAR_TYPE_VALID(scalar_t) \ static_assert(std::is_same::value || \ std::is_same::value, \ @@ -1774,6 +1774,7 @@ torch::Tensor gptq_marlin_gemm(torch::Tensor& a, torch::Tensor& b_q_weight, torch::Tensor& perm, torch::Tensor& workspace, int64_t num_bits, int64_t size_m, int64_t size_n, int64_t size_k, bool is_k_full) { + const at::cuda::OptionalCUDAGuard device_guard(device_of(a)); // Verify num_bits TORCH_CHECK(num_bits == 4 || num_bits == 8, "num_bits must be 4 or 8. Got = ", num_bits); @@ -1816,7 +1817,6 @@ torch::Tensor gptq_marlin_gemm(torch::Tensor& a, torch::Tensor& b_q_weight, TORCH_CHECK(perm.is_contiguous(), "perm is not contiguous"); // Alloc buffers - const at::cuda::OptionalCUDAGuard device_guard(device_of(a)); auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device()); torch::Tensor c = torch::empty({size_m, size_n}, options); torch::Tensor a_tmp = torch::empty({size_m, size_k}, options); diff --git a/ktransformers/ktransformers_ext/cuda/setup.py b/ktransformers/ktransformers_ext/cuda/setup.py index 7ccf9ee..156bb0e 100644 --- a/ktransformers/ktransformers_ext/cuda/setup.py +++ b/ktransformers/ktransformers_ext/cuda/setup.py @@ -2,17 +2,25 @@ from setuptools import setup, Extension from torch.utils import cpp_extension from torch.utils.cpp_extension import BuildExtension, CUDAExtension - -# setup marlin gemm -setup(name='KTransformersOps', - ext_modules=[ - CUDAExtension('KTransformersOps', [ +setup( + name='KTransformersOps', + ext_modules=[ + CUDAExtension( + 'KTransformersOps', [ 'custom_gguf/dequant.cu', 'binding.cpp', 'gptq_marlin/gptq_marlin.cu', - # 'gptq_marlin_repack.cu', - ]) - ], - cmdclass={'build_ext': BuildExtension -}) - + # 'gptq_marlin_repack.cu', + ], + extra_compile_args={ + 'cxx': ['-O3'], + 'nvcc': [ + '-O3', + '--use_fast_math', + '-Xcompiler', '-fPIC', + ] + }, + ) + ], + cmdclass={'build_ext': BuildExtension} +) \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/examples/test_linear.py b/ktransformers/ktransformers_ext/examples/test_linear.py index 6cb8d0c..7a331db 100644 --- a/ktransformers/ktransformers_ext/examples/test_linear.py +++ b/ktransformers/ktransformers_ext/examples/test_linear.py @@ -6,7 +6,7 @@ Date : 2024-07-25 10:32:05 Version : 1.0.0 LastEditors : chenht2022 -LastEditTime : 2024-07-25 10:34:00 +LastEditTime : 2024-08-06 10:36:59 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' import os, sys @@ -15,23 +15,23 @@ import cpuinfer_ext import torch -with torch.inference_mode(mode=True): - input_size = 16384 - output_size = 5120 - stride = 32 - proj_type = 1 # ggml_type::GGML_TYPE_F16 - hidden_type = 1 # ggml_type::GGML_TYPE_F16 - layer_num = 10 - CPUInfer = cpuinfer_ext.CPUInfer(48) - validation_iter = 100 - warm_up_iter = 1000 - test_iter = 10000 +input_size = 16384 +output_size = 5120 +stride = 32 +group_max_len = 1024 +proj_type = 1 # ggml_type::GGML_TYPE_F16 +hidden_type = 1 # ggml_type::GGML_TYPE_F16 +qlen = 30 +layer_num = 10 +CPUInfer = cpuinfer_ext.CPUInfer(48) +validation_iter = 100 +with torch.inference_mode(mode=True): linears = [] projs = [] for _ in range(layer_num): proj = torch.randn((output_size, input_size), dtype=torch.float16, device = "cuda").to("cpu").contiguous() - config = cpuinfer_ext.linear.LinearConfig(input_size, output_size, stride, proj.data_ptr(), proj_type, hidden_type) + config = cpuinfer_ext.linear.LinearConfig(input_size, output_size, stride, group_max_len, proj.data_ptr(), proj_type, hidden_type) linear = cpuinfer_ext.linear.Linear(config) projs.append(proj) linears.append(linear) @@ -39,11 +39,17 @@ # validation for i in range(validation_iter): linear = linears[i % layer_num] - input = torch.randn((1, input_size), dtype=torch.float16).contiguous() - output = torch.empty((1, output_size), dtype=torch.float16).contiguous() + input = torch.randn((qlen, input_size), dtype=torch.float16).contiguous() + output = torch.empty((qlen, output_size), dtype=torch.float16).contiguous() input = input / 100 - CPUInfer.submit(linear.forward, input.data_ptr(), output.data_ptr()) + CPUInfer.submit( + linear.forward( + qlen, + input.data_ptr(), + output.data_ptr() + ) + ) CPUInfer.sync() # print('cpuinfer output', output) @@ -54,30 +60,3 @@ diff = torch.mean(torch.abs(output - t_output)) / torch.mean(torch.abs(t_output)) print('diff = ', diff) assert(diff < 0.001) - - # warm up - for i in range(warm_up_iter): - linear = linears[i % layer_num] - input = torch.randn((1, input_size), dtype=torch.float16).contiguous() - output = torch.empty((1, output_size), dtype=torch.float16).contiguous() - input = input / 100 - CPUInfer.submit(linear.forward, input.data_ptr(), output.data_ptr()) - CPUInfer.sync() - - # test - total_time = 0 - for i in range(test_iter): - linear = linears[i % layer_num] - input = torch.randn((1, input_size), dtype=torch.float16).contiguous() - output = torch.empty((1, output_size), dtype=torch.float16).contiguous() - input = input / 100 - start = time.perf_counter() - CPUInfer.submit(linear.forward, input.data_ptr(), output.data_ptr()) - CPUInfer.sync() - end = time.perf_counter() - total_time += end - start - print('Time: ', total_time) - print('Iteration: ', test_iter) - print('Time per iteration: ', total_time / test_iter) - print('Bandwidth: ', input_size * output_size * 2 * test_iter / total_time / 1000 / 1000 / 1000, 'GB/s') - print("All tasks completed.") \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/examples/test_mlp.py b/ktransformers/ktransformers_ext/examples/test_mlp.py index d965877..9805e72 100644 --- a/ktransformers/ktransformers_ext/examples/test_mlp.py +++ b/ktransformers/ktransformers_ext/examples/test_mlp.py @@ -6,7 +6,7 @@ Date : 2024-07-25 10:32:05 Version : 1.0.0 LastEditors : chenht2022 -LastEditTime : 2024-07-25 10:34:03 +LastEditTime : 2024-08-06 10:37:28 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' import os, sys @@ -15,20 +15,30 @@ import cpuinfer_ext import torch -with torch.inference_mode(mode=True): - hidden_size = 5120 - intermediate_size = 3072 - stride = 32 - gate_type = 1 # ggml_type::GGML_TYPE_F16 - up_type = 1 # ggml_type::GGML_TYPE_F16 - down_type = 1 # ggml_type::GGML_TYPE_F16 - hidden_type = 1 # ggml_type::GGML_TYPE_F16 - layer_num = 10 - CPUInfer = cpuinfer_ext.CPUInfer(48) - validation_iter = 100 - warm_up_iter = 1000 - test_iter = 10000 +hidden_size = 5120 +intermediate_size = 3072 +stride = 32 +group_max_len = 1024 +gate_type = 1 # ggml_type::GGML_TYPE_F16 +up_type = 1 # ggml_type::GGML_TYPE_F16 +down_type = 1 # ggml_type::GGML_TYPE_F16 +hidden_type = 1 # ggml_type::GGML_TYPE_F16 +qlen = 30 +layer_num = 10 +CPUInfer = cpuinfer_ext.CPUInfer(48) +validation_iter = 100 + +def act_fn(x): + return x / (1.0 + torch.exp(-x)) + +def mlp_torch(input, gate_proj, up_proj, down_proj): + gate_buf = torch.mm(input, gate_proj.t()) + up_buf = torch.mm(input, up_proj.t()) + intermediate = act_fn(gate_buf) * up_buf + ret = torch.mm(intermediate, down_proj.t()) + return ret +with torch.inference_mode(mode=True): mlps = [] gate_projs = [] up_projs = [] @@ -37,7 +47,7 @@ gate_proj = torch.randn((intermediate_size, hidden_size), dtype=torch.float16, device = "cuda").to("cpu").contiguous() up_proj = torch.randn((intermediate_size, hidden_size), dtype=torch.float16, device = "cuda").to("cpu").contiguous() down_proj = torch.randn((hidden_size, intermediate_size), dtype=torch.float16, device = "cuda").to("cpu").contiguous() - config = cpuinfer_ext.mlp.MLPConfig(hidden_size, intermediate_size, stride, gate_proj.data_ptr(), up_proj.data_ptr(), down_proj.data_ptr(), gate_type, up_type, down_type, hidden_type) + config = cpuinfer_ext.mlp.MLPConfig(hidden_size, intermediate_size, stride, group_max_len, gate_proj.data_ptr(), up_proj.data_ptr(), down_proj.data_ptr(), gate_type, up_type, down_type, hidden_type) mlp = cpuinfer_ext.mlp.MLP(config) gate_projs.append(gate_proj) up_projs.append(up_proj) @@ -47,52 +57,26 @@ # validation for i in range(validation_iter): mlp = mlps[i % layer_num] - input = torch.randn((1, hidden_size), dtype=torch.float16).contiguous() - output = torch.empty((1, hidden_size), dtype=torch.float16).contiguous() + input = torch.randn((qlen, hidden_size), dtype=torch.float16).contiguous() + output = torch.empty((qlen, hidden_size), dtype=torch.float16).contiguous() input = input / 100 - CPUInfer.submit(mlp.forward, input.data_ptr(), output.data_ptr()) + CPUInfer.submit( + mlp.forward( + qlen, + input.data_ptr(), + output.data_ptr() + ) + ) CPUInfer.sync() # print('cpuinfer output', output) - def act_fn(x): - return x / (1.0 + torch.exp(-x)) gate_proj = gate_projs[i%layer_num] up_proj = up_projs[i%layer_num] down_proj = down_projs[i%layer_num] - gate_buf = torch.mm(input, gate_proj.t()) - up_buf = torch.mm(input, up_proj.t()) - intermediate = act_fn(gate_buf) * up_buf - t_output = torch.mm(intermediate, down_proj.t()) + t_output = mlp_torch(input, gate_proj, up_proj, down_proj) # print('torch output', t_output) diff = torch.mean(torch.abs(output - t_output)) / torch.mean(torch.abs(t_output)) print('diff = ', diff) assert(diff < 0.001) - - # warm up - for i in range(warm_up_iter): - mlp = mlps[i % layer_num] - input = torch.randn((1, hidden_size), dtype=torch.float16).contiguous() - output = torch.empty((1, hidden_size), dtype=torch.float16).contiguous() - input = input / 100 - CPUInfer.submit(mlp.forward, input.data_ptr(), output.data_ptr()) - CPUInfer.sync() - - # test - total_time = 0 - for i in range(test_iter): - mlp = mlps[i % layer_num] - input = torch.randn((1, hidden_size), dtype=torch.float16).contiguous() - output = torch.empty((1, hidden_size), dtype=torch.float16).contiguous() - input = input / 100 - start = time.time() - CPUInfer.submit(mlp.forward, input.data_ptr(), output.data_ptr()) - CPUInfer.sync() - end = time.time() - total_time += end - start - print('Time: ', total_time) - print('Iteration: ', test_iter) - print('Time per iteration: ', total_time / test_iter) - print('Bandwidth: ', hidden_size * intermediate_size * 3 * 2 * test_iter / total_time / 1024 / 1024 / 1024, 'GB/s') - print("All tasks completed.") \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/examples/test_moe.py b/ktransformers/ktransformers_ext/examples/test_moe.py index 0597811..3fa4dbd 100644 --- a/ktransformers/ktransformers_ext/examples/test_moe.py +++ b/ktransformers/ktransformers_ext/examples/test_moe.py @@ -6,7 +6,7 @@ Date : 2024-07-25 10:32:05 Version : 1.0.0 LastEditors : chenht2022 -LastEditTime : 2024-07-25 10:34:06 +LastEditTime : 2024-08-06 10:38:05 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' import os, sys @@ -15,25 +15,64 @@ import cpuinfer_ext import torch -with torch.inference_mode(mode=True): - expert_num = 10 - hidden_size = 5120 - intermediate_size = 1536 - stride = 32 - group_min_len = 10 - group_max_len = 1024 - gate_type = 1 # ggml_type::GGML_TYPE_F16 - up_type = 1 # ggml_type::GGML_TYPE_F16 - down_type = 1 # ggml_type::GGML_TYPE_F16 - hidden_type = 1 # ggml_type::GGML_TYPE_F16 - n_routed_experts = 6 - qlen = 30 - layer_num = 10 - CPUInfer = cpuinfer_ext.CPUInfer(48) - validation_iter = 100 - warm_up_iter = 1000 - test_iter = 10000 +expert_num = 160 +hidden_size = 5120 +intermediate_size = 1536 +stride = 32 +group_min_len = 10 +group_max_len = 1024 +gate_type = 1 # ggml_type::GGML_TYPE_F16 +up_type = 1 # ggml_type::GGML_TYPE_F16 +down_type = 1 # ggml_type::GGML_TYPE_F16 +hidden_type = 1 # ggml_type::GGML_TYPE_F16 +n_routed_experts = 6 +qlen = 30 +layer_num = 10 +CPUInfer = cpuinfer_ext.CPUInfer(48) +validation_iter = 100 + +def act_fn(x): + return x / (1.0 + torch.exp(-x)) + +def mlp_torch(input, gate_proj, up_proj, down_proj): + gate_buf = torch.mm(input, gate_proj.t()) + up_buf = torch.mm(input, up_proj.t()) + intermediate = act_fn(gate_buf) * up_buf + ret = torch.mm(intermediate, down_proj.t()) + return ret + +def moe_torch(input, expert_ids, weights, gate_proj, up_proj, down_proj): + cnts = expert_ids.new_zeros((expert_ids.shape[0], expert_num)) + cnts.scatter_(1, expert_ids, 1) + tokens_per_expert = cnts.sum(dim=0) + idxs = expert_ids.view(-1).argsort() + sorted_tokens = input[idxs // expert_ids.shape[1]] + + outputs = [] + start_idx = 0 + for i, num_tokens in enumerate(tokens_per_expert): + end_idx = start_idx + num_tokens + if num_tokens == 0: + continue + tokens_for_this_expert = sorted_tokens[start_idx:end_idx] + expert_out = mlp_torch(tokens_for_this_expert, gate_proj[i], up_proj[i], down_proj[i]) + outputs.append(expert_out) + start_idx = end_idx + + outs = torch.cat(outputs, dim=0) if len(outputs) else sorted_tokens.new_empty(0) + + new_x = torch.empty_like(outs) + new_x[idxs] = outs + t_output = ( + new_x.view(*expert_ids.shape, -1) + .type(weights.dtype) + .mul_(weights.unsqueeze(dim=-1)) + .sum(dim=1) + .type(new_x.dtype) + ) + return t_output +with torch.inference_mode(mode=True): moes = [] gate_projs = [] up_projs = [] @@ -51,63 +90,32 @@ # validation for i in range(validation_iter): - moe = moes[i % layer_num] - expert_ids = torch.randint(0, expert_num, (qlen, n_routed_experts), dtype=torch.int64).contiguous() + expert_ids = torch.stack([torch.randperm(expert_num)[:n_routed_experts] for _ in range(qlen)]).contiguous() weights = torch.rand((qlen, n_routed_experts), dtype=torch.float32).contiguous() - input = torch.randn((qlen, 1, hidden_size), dtype=torch.float16).contiguous() - output = torch.empty((qlen, 1, hidden_size), dtype=torch.float16).contiguous() + input = torch.randn((qlen, hidden_size), dtype=torch.float16).contiguous() + output = torch.empty((qlen, hidden_size), dtype=torch.float16).contiguous() input = input / 100 - CPUInfer.submit(moe.forward, qlen, n_routed_experts, expert_ids.data_ptr(), weights.data_ptr(), input.data_ptr(), output.data_ptr()) + moe = moes[i % layer_num] + CPUInfer.submit( + moe.forward( + qlen, + n_routed_experts, + expert_ids.data_ptr(), + weights.data_ptr(), + input.data_ptr(), + output.data_ptr() + ) + ) CPUInfer.sync() # print('cpuinfer output', output) - def act_fn(x): - return x / (1.0 + torch.exp(-x)) - t_output = torch.zeros((qlen, 1, hidden_size), dtype=torch.float32).contiguous() gate_proj = gate_projs[i%layer_num] up_proj = up_projs[i%layer_num] down_proj = down_projs[i%layer_num] - for token_idx in range(qlen): - for i, expert_id in enumerate(expert_ids[token_idx]): - gate_buf = torch.mm(input[token_idx], gate_proj[expert_id].t()) - up_buf = torch.mm(input[token_idx], up_proj[expert_id].t()) - intermediate = act_fn(gate_buf) * up_buf - expert_output = torch.mm(intermediate, down_proj[expert_id].t()) - t_output[token_idx] += weights[token_idx][i] * expert_output + t_output = moe_torch(input, expert_ids, weights, gate_proj, up_proj, down_proj) # print('torch output', t_output) diff = torch.mean(torch.abs(output - t_output)) / torch.mean(torch.abs(t_output)) print('diff = ', diff) assert(diff < 0.001) - - # warm up - for i in range(warm_up_iter): - moe = moes[i % layer_num] - expert_ids = torch.randint(0, expert_num, (qlen, n_routed_experts), dtype=torch.int64).contiguous() - weights = torch.rand((qlen, n_routed_experts), dtype=torch.float32).contiguous() - input = torch.randn((qlen, hidden_size), dtype=torch.float16).contiguous() - output = torch.empty((qlen, hidden_size), dtype=torch.float16).contiguous() - input = input / 100 - CPUInfer.submit(moe.forward, qlen, n_routed_experts, expert_ids.data_ptr(), weights.data_ptr(), input.data_ptr(), output.data_ptr()) - CPUInfer.sync() - - # test - total_time = 0 - for i in range(test_iter): - moe = moes[i % layer_num] - expert_ids = torch.randint(0, expert_num, (qlen, n_routed_experts), dtype=torch.int64).contiguous() - weights = torch.rand((qlen, n_routed_experts), dtype=torch.float32).contiguous() - input = torch.randn((qlen, hidden_size), dtype=torch.float16).contiguous() - output = torch.empty((qlen, hidden_size), dtype=torch.float16).contiguous() - input = input / 100 - start = time.perf_counter() - CPUInfer.submit(moe.forward, qlen, n_routed_experts, expert_ids.data_ptr(), weights.data_ptr(), input.data_ptr(), output.data_ptr()) - CPUInfer.sync() - end = time.perf_counter() - total_time += end - start - print('Time: ', total_time) - print('Iteration: ', test_iter) - print('Time per iteration: ', total_time / test_iter) - print('Bandwidth: ', hidden_size * intermediate_size * 3 * n_routed_experts * 2 * test_iter / total_time / 1000 / 1000 / 1000, 'GB/s') - print("All tasks completed.") \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/ext_bindings.cpp b/ktransformers/ktransformers_ext/ext_bindings.cpp index 0aeead3..c220a9b 100644 --- a/ktransformers/ktransformers_ext/ext_bindings.cpp +++ b/ktransformers/ktransformers_ext/ext_bindings.cpp @@ -3,8 +3,8 @@ * @Author : chenht2022 * @Date : 2024-07-22 02:03:22 * @Version : 1.0.0 - * @LastEditors : chenht2022 - * @LastEditTime : 2024-07-25 10:34:23 + * @LastEditors : chenht2022 + * @LastEditTime : 2024-08-07 10:39:37 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ // Python bindings @@ -12,7 +12,6 @@ #include #include #include "cpu_backend/cpuinfer.h" -#include "cuda_runtime.h" #include "device_launch_parameters.h" #include "llamafile/flags.h" #include "operators/llamafile/linear.h" @@ -26,239 +25,155 @@ namespace py = pybind11; using namespace pybind11::literals; -// Binding functions for the Linear class class LinearBindings { public: - static void bind_forward(CPUInfer& cpuinfer, Linear* linear, py::args args, py::kwargs kwargs) { - auto input = args[0].cast(); - auto output = args[1].cast(); - cpuinfer.submit(&Linear::forward, linear, - (const void*)input, (void*)output); - } - - static void bind_warm_up(CPUInfer& cpuinfer, Linear* linear, py::args args, py::kwargs kwargs) { - cpuinfer.submit(&Linear::warm_up, linear); - } - - static void bind_functions(CPUInfer& cpuinfer, py::object func, py::args args, py::kwargs kwargs) { - auto linear = func.attr("__self__").cast(); - std::string func_name = py::str(func.attr("__func__").attr("__name__")); - - if (func_name == "forward") { - bind_forward(cpuinfer, linear, args, kwargs); - } else if (func_name == "warm_up") { - bind_warm_up(cpuinfer, linear, args, kwargs); - } else { - throw py::value_error("Unsupported function: " + - std::string(func_name)); + class WarmUpBindinds { + public: + struct Args { + CPUInfer* cpuinfer; + Linear* linear; + }; + static void inner(void* args) { + Args* args_ = (Args*)args; + args_->cpuinfer->enqueue(&Linear::warm_up, args_->linear); + } + static std::pair cpuinfer_interface(Linear& linear) { + Args* args = new Args{nullptr, &linear}; + return std::make_pair((intptr_t)&inner, (intptr_t)args); + } + }; + class ForwardBindings { + public: + struct Args { + CPUInfer* cpuinfer; + Linear* linear; + int qlen; + const void* input; + void* output; + }; + static void inner(void* args) { + Args* args_ = (Args*)args; + args_->cpuinfer->enqueue(&Linear::forward, args_->linear, args_->qlen, args_->input, args_->output); } - } + static std::pair cpuinfer_interface(Linear& linear, int qlen, intptr_t input, intptr_t output) { + Args* args = new Args{nullptr, &linear, qlen, (const void*)input, (void*)output}; + return std::make_pair((intptr_t)&inner, (intptr_t)args); + } + }; }; -// Binding functions for the MLP class class MLPBindings { public: - static void bind_forward(CPUInfer& cpuinfer, MLP* mlp, py::args args, py::kwargs kwargs) { - auto input = args[0].cast(); - auto output = args[1].cast(); - cpuinfer.submit(&MLP::forward, mlp, - (const void*)input, (void*)output); - } - - static void bind_warm_up(CPUInfer& cpuinfer, MLP* mlp, py::args args, py::kwargs kwargs) { - cpuinfer.submit(&MLP::warm_up, mlp); - } - - static void bind_functions(CPUInfer& cpuinfer, py::object func, py::args args, py::kwargs kwargs) { - auto mlp = func.attr("__self__").cast(); - std::string func_name = py::str(func.attr("__func__").attr("__name__")); - - if (func_name == "forward") { - bind_forward(cpuinfer, mlp, args, kwargs); - } else if (func_name == "warm_up") { - bind_warm_up(cpuinfer, mlp, args, kwargs); - } else { - throw py::value_error("Unsupported function: " + - std::string(func_name)); + class WarmUpBindinds { + public: + struct Args { + CPUInfer* cpuinfer; + MLP* mlp; + }; + static void inner(void* args) { + Args* args_ = (Args*)args; + args_->cpuinfer->enqueue(&MLP::warm_up, args_->mlp); } - } + static std::pair cpuinfer_interface(MLP& mlp) { + Args* args = new Args{nullptr, &mlp}; + return std::make_pair((intptr_t)&inner, (intptr_t)args); + } + }; + class ForwardBindings { + public: + struct Args { + CPUInfer* cpuinfer; + MLP* mlp; + int qlen; + const void* input; + void* output; + }; + static void inner(void* args) { + Args* args_ = (Args*)args; + args_->cpuinfer->enqueue(&MLP::forward, args_->mlp, args_->qlen, args_->input, args_->output); + } + static std::pair cpuinfer_interface(MLP& mlp, int qlen, intptr_t input, intptr_t output) { + Args* args = new Args{nullptr, &mlp, qlen, (const void*)input, (void*)output}; + return std::make_pair((intptr_t)&inner, (intptr_t)args); + } + }; }; -// Binding functions for the MOE class class MOEBindings { public: - static void bind_forward(CPUInfer& cpuinfer, MOE* moe, py::args args, py::kwargs kwargs) { - int qlen = args[0].cast(); - int k = args[1].cast(); - auto expert_ids = args[2].cast(); - auto weights = args[3].cast(); - auto input = args[4].cast(); - auto output = args[5].cast(); - cpuinfer.submit(&MOE::forward, moe, - qlen, k, (const uint64_t*)expert_ids, (const float*)weights, (const void*)input, (void*)output); - } - - static void bind_warm_up(CPUInfer& cpuinfer, MOE* moe, py::args args, py::kwargs kwargs) { - cpuinfer.submit(&MOE::warm_up, moe); - } - - static void bind_functions(CPUInfer& cpuinfer, py::object func, py::args args, py::kwargs kwargs) { - auto moe = func.attr("__self__").cast(); - std::string func_name = py::str(func.attr("__func__").attr("__name__")); - - if (func_name == "forward") { - bind_forward(cpuinfer, moe, args, kwargs); - } else if (func_name == "warm_up") { - bind_warm_up(cpuinfer, moe, args, kwargs); - } else { - throw py::value_error("Unsupported function: " + - std::string(func_name)); + class WarmUpBindinds { + public: + struct Args { + CPUInfer* cpuinfer; + MOE* moe; + }; + static void inner(void* args) { + Args* args_ = (Args*)args; + args_->cpuinfer->enqueue(&MOE::warm_up, args_->moe); } - } -}; - -struct MOEForwardArgs { - CPUInfer* cpuinfer; - MOE* moe; - int qlen; - int k; - uint64_t* expert_ids; - float* weights; - void* input; - void* output; + static std::pair cpuinfer_interface(MOE& moe) { + Args* args = new Args{nullptr, &moe}; + return std::make_pair((intptr_t)&inner, (intptr_t)args); + } + }; + class ForwardBindings { + public: + struct Args { + CPUInfer* cpuinfer; + MOE* moe; + int qlen; + int k; + const uint64_t* expert_ids; + const float* weights; + const void* input; + void* output; + }; + static void inner(void* args) { + Args* args_ = (Args*)args; + args_->cpuinfer->enqueue(&MOE::forward, args_->moe, args_->qlen, args_->k, args_->expert_ids, args_->weights, args_->input, args_->output); + } + static std::pair cpuinfer_interface(MOE& moe, int qlen, int k, intptr_t expert_ids, intptr_t weights, intptr_t input, intptr_t output) { + Args* args = new Args{nullptr, &moe, qlen, k, (const uint64_t*)expert_ids, (const float*)weights, (const void*)input, (void*)output}; + return std::make_pair((intptr_t)&inner, (intptr_t)args); + } + }; }; -void submit_moe_forward_with_host_args_ptr(void* host_args_ptr) { - MOEForwardArgs* host_args = (MOEForwardArgs*)host_args_ptr; - host_args->cpuinfer->submit(&MOE::forward, host_args->moe, - host_args->qlen, host_args->k, host_args->expert_ids, host_args->weights, host_args->input, host_args->output); -} - -void cpuinfer_sync(void* host_args_ptr) { - CPUInfer* cpuinfer = (CPUInfer*)host_args_ptr; - cpuinfer->sync(); -} - PYBIND11_MODULE(cpuinfer_ext, m) { - auto linear_module = m.def_submodule("linear"); + py::class_(m, "CPUInfer") + .def(py::init()) + .def("submit", &CPUInfer::submit) + .def("submit_with_cuda_stream", &CPUInfer::submit_with_cuda_stream) + .def("sync", &CPUInfer::sync) + .def("sync_with_cuda_stream", &CPUInfer::sync_with_cuda_stream); + auto linear_module = m.def_submodule("linear"); py::class_(linear_module, "LinearConfig") - .def(py::init([](int hidden_size, int intermediate_size, int stride, intptr_t proj, int proj_type, int hidden_type) { - return LinearConfig(hidden_size, intermediate_size, stride, (void*)proj, (ggml_type)proj_type, (ggml_type)hidden_type); + .def(py::init([](int hidden_size, int intermediate_size, int stride, int group_max_len, intptr_t proj, int proj_type, int hidden_type) { + return LinearConfig(hidden_size, intermediate_size, stride, group_max_len, (void*)proj, (ggml_type)proj_type, (ggml_type)hidden_type); })); - py::class_(linear_module, "Linear") .def(py::init()) - .def("warm_up", [](Linear& linear) { - throw std::runtime_error("!!! Doing nothing, please use CPUInfer.submit to call it!!!\n"); - }) - .def("forward", [](Linear& linear, intptr_t input, intptr_t output) { - throw std::runtime_error("!!! Doing nothing, please use CPUInfer.submit to call it!!!\n"); - }); + .def("warm_up", &LinearBindings::WarmUpBindinds::cpuinfer_interface) + .def("forward", &LinearBindings::ForwardBindings::cpuinfer_interface); auto mlp_module = m.def_submodule("mlp"); - py::class_(mlp_module, "MLPConfig") - .def(py::init([](int hidden_size, int intermediate_size, int stride, intptr_t gate_proj, intptr_t up_proj, intptr_t down_proj, int gate_type, int up_type, int down_type, int hidden_type) { - return MLPConfig(hidden_size, intermediate_size, stride, (void*)gate_proj, (void*)up_proj, (void*)down_proj, (ggml_type)gate_type, (ggml_type)up_type, (ggml_type)down_type, (ggml_type)hidden_type); + .def(py::init([](int hidden_size, int intermediate_size, int stride, int group_max_len, intptr_t gate_proj, intptr_t up_proj, intptr_t down_proj, int gate_type, int up_type, int down_type, int hidden_type) { + return MLPConfig(hidden_size, intermediate_size, stride, group_max_len, (void*)gate_proj, (void*)up_proj, (void*)down_proj, (ggml_type)gate_type, (ggml_type)up_type, (ggml_type)down_type, (ggml_type)hidden_type); })); - py::class_(mlp_module, "MLP") .def(py::init()) - .def("warm_up", [](MLP& mlp) { - throw std::runtime_error("!!! Doing nothing, please use CPUInfer.submit to call it!!!\n"); - }) - .def("forward", [](MLP& mlp, intptr_t input, intptr_t output) { - throw std::runtime_error("!!! Doing nothing, please use CPUInfer.submit to call it!!!\n"); - }); + .def("warm_up", &MLPBindings::WarmUpBindinds::cpuinfer_interface) + .def("forward", &MLPBindings::ForwardBindings::cpuinfer_interface); auto moe_module = m.def_submodule("moe"); - py::class_(moe_module, "MOEConfig") .def(py::init([](int expert_num, int routed_expert_num, int hidden_size, int intermediate_size, int stride, int group_min_len, int group_max_len, intptr_t gate_proj, intptr_t up_proj, intptr_t down_proj, int gate_type, int up_type, int down_type, int hidden_type) { return MOEConfig(expert_num, routed_expert_num, hidden_size, intermediate_size, stride, group_min_len, group_max_len, (void*)gate_proj, (void*)up_proj, (void*)down_proj, (ggml_type)gate_type, (ggml_type)up_type, (ggml_type)down_type, (ggml_type)hidden_type); })); - py::class_(moe_module, "MOE") .def(py::init()) - .def("warm_up", [](MOE& moe) { - throw std::runtime_error("!!! Doing nothing, please use CPUInfer.submit to call it!!!\n"); - }) - .def("forward", [](MOE& moe, int k, uint64_t expert_ids, intptr_t weights, intptr_t input, intptr_t output) { - throw std::runtime_error("!!! Doing nothing, please use CPUInfer.submit to call it!!!\n"); - }); - - py::class_(m, "CPUInfer") - .def(py::init()) - .def("submit", - [linear_module, mlp_module, moe_module](CPUInfer& cpuinfer, py::object func, py::args args, py::kwargs kwargs) { - if (py::hasattr(func, "__self__") && - py::hasattr(func, "__func__")) { - std::string class_name = py::str(func.attr("__self__") - .attr("__class__") - .attr("__name__")); - if (class_name == "Linear") { - LinearBindings::bind_functions(cpuinfer, func, - args, kwargs); - } else if (class_name == "MLP") { - MLPBindings::bind_functions(cpuinfer, func, - args, kwargs); - } else if (class_name == "MOE") { - MOEBindings::bind_functions(cpuinfer, func, - args, kwargs); - } else { - // handle other classes - throw py::type_error("Unsupported class type: " + - class_name); - } - } else { - // handle cases where func does not have __self__ or - // __func__ - throw py::type_error( - "Invalid function object: missing " - "__self__ or __func__ attribute."); - } - }) - .def("submit_with_cuda_stream", - [linear_module, mlp_module, moe_module](CPUInfer& cpuinfer, intptr_t user_cuda_stream, py::object func, py::args args, py::kwargs kwargs) { - if (py::hasattr(func, "__self__") && - py::hasattr(func, "__func__")) { - std::string class_name = py::str(func.attr("__self__") - .attr("__class__") - .attr("__name__")); - if (class_name == "MOE") { - std::string func_name = py::str(func.attr("__func__").attr("__name__")); - if (func_name == "forward") { - auto moe = func.attr("__self__").cast(); - int qlen = args[0].cast(); - int k = args[1].cast(); - auto expert_ids = args[2].cast(); - auto weights = args[3].cast(); - auto input = args[4].cast(); - auto output = args[5].cast(); - MOEForwardArgs* moe_forward_args = new MOEForwardArgs{&cpuinfer, moe, qlen, k, (uint64_t*)expert_ids, (float*)weights, (void*)input, (void*)output}; - // submit_moe_forward_with_host_args_ptr(moe_forward_args); - cudaLaunchHostFunc((cudaStream_t)user_cuda_stream, (cudaHostFn_t)submit_moe_forward_with_host_args_ptr, moe_forward_args); - } else { - throw py::value_error("Unsupported function: " + - std::string(func_name)); - } - } else { - // handle other classes - throw py::type_error("Unsupported class type: " + - class_name); - } - } else { - // handle cases where func does not have __self__ or - // __func__ - throw py::type_error( - "Invalid function object: missing " - "__self__ or __func__ attribute."); - } - }) - .def("sync_with_cuda_stream", [](CPUInfer& cpuinfer, intptr_t user_cuda_stream) { - // cpuinfer_sync((void*)(&cpuinfer)); - cudaLaunchHostFunc((cudaStream_t)user_cuda_stream, (cudaHostFn_t)cpuinfer_sync, (void*)(&cpuinfer)); - }) - .def("sync", &CPUInfer::sync); + .def("warm_up", &MOEBindings::WarmUpBindinds::cpuinfer_interface) + .def("forward", &MOEBindings::ForwardBindings::cpuinfer_interface); } diff --git a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/gptq.py b/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/gptq.py deleted file mode 100644 index cda3e7a..0000000 --- a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/gptq.py +++ /dev/null @@ -1,206 +0,0 @@ -import math -import os -import time -from logging import getLogger - -import torch -import torch.nn as nn -import transformers - -from .quantizer import Quantizer - - -logger = getLogger(__name__) - -torch.backends.cuda.matmul.allow_tf32 = False -torch.backends.cudnn.allow_tf32 = False - - -class GPTQ: - def __init__(self, layer): - self.layer = layer - self.dev = self.layer.weight.device - W = layer.weight.data.clone() - if isinstance(self.layer, nn.Conv2d): - W = W.flatten(1) - if isinstance(self.layer, transformers.pytorch_utils.Conv1D): - W = W.t() - self.rows = W.shape[0] - self.columns = W.shape[1] - self.H = torch.zeros((self.columns, self.columns), device=self.dev) - self.nsamples = 0 - self.quantizer = Quantizer() - - def add_batch(self, inp, out): - if os.environ.get("DEBUG"): - self.inp1 = inp - self.out1 = out - if len(inp.shape) == 2: - inp = inp.unsqueeze(0) - tmp = inp.shape[0] - if isinstance(self.layer, nn.Linear) or isinstance(self.layer, transformers.Conv1D): - if len(inp.shape) == 3: - inp = inp.reshape((-1, inp.shape[-1])) - inp = inp.t() - if isinstance(self.layer, nn.Conv2d): - unfold = nn.Unfold( - self.layer.kernel_size, - dilation=self.layer.dilation, - padding=self.layer.padding, - stride=self.layer.stride, - ) - inp = unfold(inp) - inp = inp.permute([1, 0, 2]) - inp = inp.flatten(1) - self.H *= self.nsamples / (self.nsamples + tmp) - self.nsamples += tmp - # inp = inp.float() - inp = math.sqrt(2 / self.nsamples) * inp.float() - # self.H += 2 / self.nsamples * inp.matmul(inp.t()) - self.H += inp.matmul(inp.t()) - - def fasterquant( - self, - blocksize=128, - percdamp=0.01, - group_size=-1, - actorder=False, - static_groups=False, - ): - W = self.layer.weight.data.clone() - if isinstance(self.layer, nn.Conv2d): - W = W.flatten(1) - if isinstance(self.layer, transformers.Conv1D): - W = W.t() - W = W.float() - - tick = time.time() - - if not self.quantizer.ready(): - self.quantizer.find_params(W, weight=True) - - H = self.H - del self.H - dead = torch.diag(H) == 0 - H[dead, dead] = 1 - W[:, dead] = 0 - - g_idx = [] - scale = [] - zero = [] - now_idx = 1 - - if static_groups: - import copy - - groups = [] - for i in range(0, self.columns, group_size): - quantizer = copy.deepcopy(self.quantizer) - quantizer.find_params(W[:, i : (i + group_size)], weight=True) - scale.append(quantizer.scale) - zero.append(quantizer.zero) - groups.append(quantizer) - - if actorder: - perm = torch.argsort(torch.diag(H), descending=True) - W = W[:, perm] - H = H[perm][:, perm] - invperm = torch.argsort(perm) - - Losses = torch.zeros_like(W) - Q = torch.zeros_like(W) - - damp = percdamp * torch.mean(torch.diag(H)) - diag = torch.arange(self.columns, device=self.dev) - H[diag, diag] += damp - H = torch.linalg.cholesky(H) - H = torch.cholesky_inverse(H) - H = torch.linalg.cholesky(H, upper=True) - Hinv = H - - for i1 in range(0, self.columns, blocksize): - i2 = min(i1 + blocksize, self.columns) - count = i2 - i1 - - W1 = W[:, i1:i2].clone() - Q1 = torch.zeros_like(W1) - Err1 = torch.zeros_like(W1) - Losses1 = torch.zeros_like(W1) - Hinv1 = Hinv[i1:i2, i1:i2] - - for i in range(count): - w = W1[:, i] - d = Hinv1[i, i] - - if group_size != -1: - if not static_groups: - if (i1 + i) % group_size == 0: - self.quantizer.find_params(W[:, (i1 + i) : (i1 + i + group_size)], weight=True) - - if ((i1 + i) // group_size) - now_idx == -1: - scale.append(self.quantizer.scale) - zero.append(self.quantizer.zero) - now_idx += 1 - else: - idx = i1 + i - if actorder: - idx = perm[idx] - self.quantizer = groups[idx // group_size] - - q = self.quantizer.quantize(w.unsqueeze(1)).flatten() - Q1[:, i] = q - Losses1[:, i] = (w - q) ** 2 / d**2 - - err1 = (w - q) / d - W1[:, i:] -= err1.unsqueeze(1).matmul(Hinv1[i, i:].unsqueeze(0)) - Err1[:, i] = err1 - - Q[:, i1:i2] = Q1 - Losses[:, i1:i2] = Losses1 / 2 - - W[:, i2:] -= Err1.matmul(Hinv[i1:i2, i2:]) - - if os.environ.get("DEBUG"): - self.layer.weight.data[:, :i2] = Q[:, :i2] - self.layer.weight.data[:, i2:] = W[:, i2:] - logger.debug(torch.sum((self.layer(self.inp1) - self.out1) ** 2)) - logger.debug(torch.sum(Losses)) - - torch.cuda.synchronize() - logger.info(f"duration: {(time.time() - tick)}") - logger.info(f"avg loss: {torch.sum(Losses).item() / self.nsamples}") - - group_size = group_size if group_size != -1 else self.columns - if static_groups and actorder: - g_idx = [perm[i] // group_size for i in range(self.columns)] - else: - g_idx = [i // group_size for i in range(self.columns)] - g_idx = torch.tensor(g_idx, dtype=torch.int32, device=Q.device) - if actorder: - Q = Q[:, invperm] - g_idx = g_idx[invperm] - - if isinstance(self.layer, transformers.Conv1D): - Q = Q.t() - self.layer.weight.data = Q.reshape(self.layer.weight.shape).type_as(self.layer.weight.data) - if os.environ.get("DEBUG"): - logger.debug(torch.sum((self.layer(self.inp1) - self.out1) ** 2)) - - if scale == []: - scale.append(self.quantizer.scale) - zero.append(self.quantizer.zero) - scale = torch.cat(scale, dim=1) - zero = torch.cat(zero, dim=1) - return scale, zero, g_idx - - def free(self): - if os.environ.get("DEBUG"): - self.inp1 = None - self.out1 = None - self.H = None - self.Losses = None - self.Trace = None - torch.cuda.empty_cache() - - -__all__ = ["GPTQ"] diff --git a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/gptq_marlin.py b/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/gptq_marlin.py deleted file mode 100644 index 599070f..0000000 --- a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/gptq_marlin.py +++ /dev/null @@ -1,458 +0,0 @@ -import enum -from enum import Enum -from typing import Any, Dict, List, Optional - -import torch -from torch.nn.parameter import Parameter - -from vllm import _custom_ops as ops -from vllm.logger import init_logger -from vllm.model_executor.layers.linear import (LinearBase, LinearMethodBase, - set_weight_attrs) -from vllm.model_executor.layers.quantization.base_config import ( - QuantizationConfig) - -logger = init_logger(__name__) - -GPTQ_MARLIN_TILE = 16 -GPTQ_MARLIN_MIN_THREAD_N = 64 -GPTQ_MARLIN_MIN_THREAD_K = 128 -GPTQ_MARLIN_MAX_PARALLEL = 16 - -GPTQ_MARLIN_SUPPORTED_NUM_BITS = [4, 8] -GPTQ_MARLIN_SUPPORTED_GROUP_SIZES = [-1, 32, 64, 128] -GPTQ_MARLIN_SUPPORTED_SYM = [True] - - -# Permutations for Marlin scale shuffling -def get_scale_perms(num_bits: int): - scale_perm: List[int] = [] - for i in range(8): - scale_perm.extend([i + 8 * j for j in range(8)]) - scale_perm_single: List[int] = [] - for i in range(4): - scale_perm_single.extend( - [2 * i + j for j in [0, 1, 8, 9, 16, 17, 24, 25]]) - return scale_perm, scale_perm_single - - -def get_pack_factor(num_bits: int): - assert (num_bits in GPTQ_MARLIN_SUPPORTED_NUM_BITS - ), f"Unsupported num_bits = {num_bits}" - return 32 // num_bits - - -def marlin_permute_scales(s: torch.Tensor, size_k: int, size_n: int, - group_size: int, num_bits: int): - scale_perm, scale_perm_single = get_scale_perms(num_bits) - if group_size < size_k and group_size != -1: - s = s.reshape((-1, len(scale_perm)))[:, scale_perm] - else: - s = s.reshape((-1, len(scale_perm_single)))[:, scale_perm_single] - s = s.reshape((-1, size_n)).contiguous() - - return s - - -class GPTQMarlinConfig(QuantizationConfig): - """Config class for GPTQ Marlin""" - - def __init__(self, weight_bits: int, group_size: int, desc_act: bool, - is_sym: bool) -> None: - if desc_act and group_size == -1: - # In this case, act_order == True is the same as act_order == False - # (since we have only one group per output channel) - desc_act = False - - self.weight_bits = weight_bits - self.group_size = group_size - self.desc_act = desc_act - self.is_sym = is_sym - - # Verify - if self.weight_bits not in GPTQ_MARLIN_SUPPORTED_NUM_BITS: - raise ValueError( - f"Marlin does not support weight_bits = {self.weight_bits}. " - f"Only weight_bits = {GPTQ_MARLIN_SUPPORTED_NUM_BITS} " - "are supported.") - if self.group_size not in GPTQ_MARLIN_SUPPORTED_GROUP_SIZES: - raise ValueError( - f"Marlin does not support group_size = {self.group_size}. " - f"Only group_sizes = {GPTQ_MARLIN_SUPPORTED_GROUP_SIZES} " - "are supported.") - if self.is_sym not in GPTQ_MARLIN_SUPPORTED_SYM: - raise ValueError( - f"Marlin does not support is_sym = {self.is_sym}. " - f"Only sym = {GPTQ_MARLIN_SUPPORTED_SYM} are supported.") - - # Init - self.pack_factor = get_pack_factor(weight_bits) - self.tile_size = GPTQ_MARLIN_TILE - self.min_thread_n = GPTQ_MARLIN_MIN_THREAD_N - self.min_thread_k = GPTQ_MARLIN_MIN_THREAD_K - self.max_parallel = GPTQ_MARLIN_MAX_PARALLEL - - def __repr__(self) -> str: - return (f"GPTQMarlinConfig(weight_bits={self.weight_bits}, " - f"group_size={self.group_size}, " - f"desc_act={self.desc_act})") - - @classmethod - def get_name(cls) -> str: - return "gptq_marlin" - - @classmethod - def get_supported_act_dtypes(cls) -> List[torch.dtype]: - return [torch.half, torch.bfloat16] - - @classmethod - def get_min_capability(cls) -> int: - return 80 - - @classmethod - def get_config_filenames(cls) -> List[str]: - return ["quantize_config.json"] - - @classmethod - def from_config(cls, config: Dict[str, Any]) -> "GPTQMarlinConfig": - weight_bits = cls.get_from_keys(config, ["bits"]) - group_size = cls.get_from_keys(config, ["group_size"]) - desc_act = cls.get_from_keys(config, ["desc_act"]) - is_sym = cls.get_from_keys(config, ["sym"]) - return cls(weight_bits, group_size, desc_act, is_sym) - - @classmethod - def override_quantization_method(cls, hf_quant_cfg, - user_quant) -> Optional[str]: - can_convert = cls.is_marlin_compatible(hf_quant_cfg) - - is_valid_user_quant = (user_quant is None or user_quant == "marlin") - - if can_convert and is_valid_user_quant: - msg = ("The model is convertible to {} during runtime." - " Using {} kernel.".format(cls.get_name(), cls.get_name())) - logger.info(msg) - return cls.get_name() - - if can_convert and user_quant == "gptq": - logger.info("Detected that the model can run with gptq_marlin" - ", however you specified quantization=gptq explicitly," - " so forcing gptq. Use quantization=gptq_marlin for" - " faster inference") - return None - - def get_quant_method( - self, - layer: torch.nn.Module) -> Optional["GPTQMarlinLinearMethod"]: - if isinstance(layer, LinearBase): - return GPTQMarlinLinearMethod(self) - return None - - def get_scaled_act_names(self) -> List[str]: - return [] - - @classmethod - def is_marlin_compatible(cls, quant_config: Dict[str, Any]): - # Extract data from quant config. - num_bits = quant_config.get("bits", None) - group_size = quant_config.get("group_size", None) - sym = quant_config.get("sym", None) - desc_act = quant_config.get("desc_act", None) - - # If we cannot find the info needed in the config, cannot convert. - if (num_bits is None or group_size is None or sym is None - or desc_act is None): - return False - - # If the capability of the device is too low, cannot convert. - major, minor = torch.cuda.get_device_capability() - device_capability = major * 10 + minor - if device_capability < cls.get_min_capability(): - return False - - # Otherwise, can convert if model satisfies marlin constraints. - return (num_bits in GPTQ_MARLIN_SUPPORTED_NUM_BITS - and group_size in GPTQ_MARLIN_SUPPORTED_GROUP_SIZES - and sym in GPTQ_MARLIN_SUPPORTED_SYM) - - -class GPTQMarlinState(Enum): - REPACK = enum.auto() - READY = enum.auto() - - -class GPTQMarlinLinearMethod(LinearMethodBase): - """Linear method for GPTQ Marlin. - - Args: - quant_config: The GPTQ Marlin quantization config. - """ - - def __init__(self, quant_config: GPTQMarlinConfig) -> None: - self.quant_config = quant_config - - def create_weights( - self, - layer: torch.nn.Module, - input_size_per_partition: int, - output_partition_sizes: List[int], - input_size: int, - output_size: int, - params_dtype: torch.dtype, - **extra_weight_attrs, - ) -> None: - del output_size - - # Normalize group_size - if self.quant_config.group_size != -1: - group_size = self.quant_config.group_size - else: - group_size = input_size - - # Validate dtype - if params_dtype not in [torch.float16, torch.bfloat16]: - raise ValueError(f"The params dtype must be float16 " - f"or bfloat16, but got {params_dtype}") - - # Validate output_size_per_partition - output_size_per_partition = sum(output_partition_sizes) - if output_size_per_partition % self.quant_config.min_thread_n != 0: - raise ValueError( - f"Weight output_size_per_partition = " - f"{output_size_per_partition} is not divisible by " - f" min_thread_n = {self.quant_config.min_thread_n}.") - - # Validate input_size_per_partition - if input_size_per_partition % self.quant_config.min_thread_k != 0: - raise ValueError( - f"Weight input_size_per_partition = " - f"{input_size_per_partition} is not divisible " - f"by min_thread_k = {self.quant_config.min_thread_k}.") - - if (group_size < input_size - and input_size_per_partition % group_size != 0): - raise ValueError( - f"Weight input_size_per_partition = {input_size_per_partition}" - f" is not divisible by group_size = {group_size}.") - - # Detect sharding of scales/zp - - # By default, no sharding over "input dim" - scales_and_zp_size = input_size // group_size - scales_and_zp_input_dim = None - - if self.quant_config.desc_act: - # Act-order case - assert self.quant_config.group_size != -1 - - is_k_full = input_size_per_partition == input_size - - else: - # No act-order case - - # K is always full due to full alignment with - # group-size and shard of scales/zp - is_k_full = True - - # If this is a row-parallel case, then shard scales/zp - if (input_size != input_size_per_partition - and self.quant_config.group_size != -1): - scales_and_zp_size = input_size_per_partition // group_size - scales_and_zp_input_dim = 0 - - # Init buffers - - # Quantized weights - qweight = Parameter( - torch.empty( - input_size_per_partition // self.quant_config.pack_factor, - output_size_per_partition, - dtype=torch.int32, - ), - requires_grad=False, - ) - set_weight_attrs( - qweight, - { - **extra_weight_attrs, - "input_dim": 0, - "output_dim": 1, - "packed_dim": 0, - "pack_factor": self.quant_config.pack_factor, - }, - ) - - # Activation order - g_idx = Parameter( - torch.empty( - input_size_per_partition, - dtype=torch.int32, - ), - requires_grad=False, - ) - # Ignore warning from fused linear layers such as QKVParallelLinear. - set_weight_attrs( - g_idx, - { - **extra_weight_attrs, "input_dim": 0, - "ignore_warning": True - }, - ) - - g_idx_sort_indices = torch.empty( - g_idx.shape, - dtype=torch.int32, - ) - - # Scales - scales = Parameter( - torch.empty( - scales_and_zp_size, - output_size_per_partition, - dtype=params_dtype, - ), - requires_grad=False, - ) - set_weight_attrs( - scales, - { - **extra_weight_attrs, - "input_dim": scales_and_zp_input_dim, - "output_dim": 1, - }, - ) - - # Quantized zero-points - qzeros = Parameter( - torch.empty( - scales_and_zp_size, - output_size_per_partition // self.quant_config.pack_factor, - dtype=torch.int32, - device="meta", - ), - requires_grad=False, - ) - set_weight_attrs( - qzeros, - { - **extra_weight_attrs, - "input_dim": scales_and_zp_input_dim, - "output_dim": 1, - "packed_dim": 1, - "pack_factor": self.quant_config.pack_factor, - }, - ) - - # Allocate marlin workspace - max_workspace_size = ( - output_size_per_partition // - self.quant_config.min_thread_n) * self.quant_config.max_parallel - workspace = torch.zeros(max_workspace_size, - dtype=torch.int, - requires_grad=False) - - layer.register_parameter("qweight", qweight) - layer.register_parameter("g_idx", g_idx) - layer.register_parameter("scales", scales) - layer.register_parameter("qzeros", qzeros) - layer.g_idx_sort_indices = g_idx_sort_indices - layer.workspace = workspace - layer.input_size_per_partition = input_size_per_partition - layer.output_size_per_partition = output_size_per_partition - layer.input_size = input_size - layer.is_k_full = is_k_full - layer.marlin_state = GPTQMarlinState.REPACK - - def apply( - self, - layer: torch.nn.Module, - x: torch.Tensor, - bias: Optional[torch.Tensor] = None, - ) -> torch.Tensor: - reshaped_x = x.reshape(-1, x.shape[-1]) - - size_m = reshaped_x.shape[0] - part_size_n = layer.output_size_per_partition - part_size_k = layer.input_size_per_partition - full_size_k = layer.input_size - - out_shape = x.shape[:-1] + (part_size_n, ) - - if layer.marlin_state == GPTQMarlinState.REPACK: - layer.marlin_state = GPTQMarlinState.READY - - # Newly generated tensors need to replace existing tensors that are - # already registered as parameters by vLLM (and won't be freed) - def replace_tensor(name, new_t): - # It is important to use resize_() here since it ensures - # the same buffer is reused - getattr(layer, name).resize_(new_t.shape) - getattr(layer, name).copy_(new_t) - del new_t - - cur_device = layer.qweight.device - - # Process act_order - if self.quant_config.desc_act: - # Get sorting based on g_idx - g_idx_sort_indices = torch.argsort(layer.g_idx).to(torch.int) - - sorted_g_idx = layer.g_idx[g_idx_sort_indices] - - replace_tensor("g_idx", sorted_g_idx) - replace_tensor("g_idx_sort_indices", g_idx_sort_indices) - - else: - # Reset g_idx related tensors - layer.g_idx = Parameter( - torch.empty(0, dtype=torch.int, device=cur_device), - requires_grad=False, - ) - layer.g_idx_sort_indices = Parameter( - torch.empty(0, dtype=torch.int, device=cur_device), - requires_grad=False, - ) - - # Repack weights - marlin_qweight = ops.gptq_marlin_repack( - layer.qweight, - layer.g_idx_sort_indices, - part_size_k, - part_size_n, - self.quant_config.weight_bits, - ) - replace_tensor("qweight", marlin_qweight) - - # Permute scales - scales_size_k = part_size_k - scales_size_n = part_size_n - if self.quant_config.desc_act: - scales_size_k = full_size_k - - marlin_scales = marlin_permute_scales( - layer.scales, - scales_size_k, - scales_size_n, - self.quant_config.group_size, - self.quant_config.weight_bits, - ) - replace_tensor("scales", marlin_scales) - - output = ops.gptq_marlin_gemm( - reshaped_x, - layer.qweight, - layer.scales, - layer.g_idx, - layer.g_idx_sort_indices, - layer.workspace, - self.quant_config.weight_bits, - size_m, - part_size_n, - part_size_k, - layer.is_k_full, - ) - - if bias is not None: - output.add_(bias) # In-place add - - return output.reshape(out_shape) diff --git a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/quantizer.py b/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/quantizer.py deleted file mode 100644 index e945a70..0000000 --- a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/quantizer.py +++ /dev/null @@ -1,140 +0,0 @@ -from logging import getLogger - -import torch -import torch.nn as nn - - -logger = getLogger(__name__) - - -def quantize(x, scale, zero, maxq): - if maxq < 0: - return (x > scale / 2).float() * scale + (x < zero / 2).float() * zero - q = torch.clamp(torch.round(x / scale) + zero, 0, maxq) - return scale * (q - zero) - - -class Quantizer(nn.Module): - def __init__(self, shape=1): - super(Quantizer, self).__init__() - self.register_buffer("maxq", torch.tensor(0)) - self.register_buffer("scale", torch.zeros(shape)) - self.register_buffer("zero", torch.zeros(shape)) - - def configure( - self, - bits, - perchannel=False, - sym=True, - mse=False, - norm=2.4, - grid=100, - maxshrink=0.8, - trits=False, - ): - self.maxq = torch.tensor(2**bits - 1) - self.perchannel = perchannel - self.sym = sym - self.mse = mse - self.norm = norm - self.grid = grid - self.maxshrink = maxshrink - if trits: - self.maxq = torch.tensor(-1) - - def find_params(self, x, weight=False): - dev = x.device - self.maxq = self.maxq.to(dev) - - shape = x.shape - if self.perchannel: - if weight: - x = x.flatten(1) - else: - if len(shape) == 4: - x = x.permute([1, 0, 2, 3]) - x = x.flatten(1) - if len(shape) == 3: - x = x.reshape((-1, shape[-1])).t() - if len(shape) == 2: - x = x.t() - else: - x = x.flatten().unsqueeze(0) - - tmp = torch.zeros(x.shape[0], device=dev) - xmin = torch.minimum(x.min(1)[0], tmp) - xmax = torch.maximum(x.max(1)[0], tmp) - - if self.sym: - xmax = torch.maximum(torch.abs(xmin), xmax) - tmp = xmin < 0 - if torch.any(tmp): - xmin[tmp] = -xmax[tmp] - tmp = (xmin == 0) & (xmax == 0) - xmin[tmp] = -1 - xmax[tmp] = +1 - - if self.maxq < 0: - self.scale = xmax - self.zero = xmin - else: - self.scale = (xmax - xmin) / self.maxq - if self.sym: - self.zero = torch.full_like(self.scale, (self.maxq + 1) / 2) - else: - self.zero = torch.round(-xmin / self.scale) - - if self.mse: - best = torch.full([x.shape[0]], float("inf"), device=dev) - for i in range(int(self.maxshrink * self.grid)): - p = 1 - i / self.grid - xmin1 = p * xmin - xmax1 = p * xmax - scale1 = (xmax1 - xmin1) / self.maxq - zero1 = torch.round(-xmin1 / scale1) if not self.sym else self.zero - q = quantize(x, scale1.unsqueeze(1), zero1.unsqueeze(1), self.maxq) - q -= x - q.abs_() - q.pow_(self.norm) - err = torch.sum(q, 1) - tmp = err < best - if torch.any(tmp): - best[tmp] = err[tmp] - self.scale[tmp] = scale1[tmp] - self.zero[tmp] = zero1[tmp] - if not self.perchannel: - if weight: - tmp = shape[0] - else: - tmp = shape[1] if len(shape) != 3 else shape[2] - self.scale = self.scale.repeat(tmp) - self.zero = self.zero.repeat(tmp) - - if weight: - shape = [-1] + [1] * (len(shape) - 1) - self.scale = self.scale.reshape(shape) - self.zero = self.zero.reshape(shape) - return - if len(shape) == 4: - self.scale = self.scale.reshape((1, -1, 1, 1)) - self.zero = self.zero.reshape((1, -1, 1, 1)) - if len(shape) == 3: - self.scale = self.scale.reshape((1, 1, -1)) - self.zero = self.zero.reshape((1, 1, -1)) - if len(shape) == 2: - self.scale = self.scale.unsqueeze(0) - self.zero = self.zero.unsqueeze(0) - - def quantize(self, x): - if self.ready(): - return quantize(x, self.scale, self.zero, self.maxq) - return x - - def enabled(self): - return self.maxq > 0 - - def ready(self): - return torch.all(self.scale != 0) - - -__all__ = ["Quantizer"] diff --git a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/repack.py b/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/repack.py deleted file mode 100644 index 987f05b..0000000 --- a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/repack.py +++ /dev/null @@ -1,99 +0,0 @@ -import torch -import enum -from enum import Enum -from typing import Any, Dict, List, Optional -from torch.nn.parameter import Parameter - -def apply( - self, - layer: torch.nn.Module, - x: torch.Tensor, - bias: Optional[torch.Tensor] = None, -) -> torch.Tensor: - reshaped_x = x.reshape(-1, x.shape[-1]) - - size_m = reshaped_x.shape[0] - part_size_n = layer.output_size_per_partition - part_size_k = layer.input_size_per_partition - full_size_k = layer.input_size - - out_shape = x.shape[:-1] + (part_size_n, ) - - if layer.marlin_state == GPTQMarlinState.REPACK: - layer.marlin_state = GPTQMarlinState.READY - - # Newly generated tensors need to replace existing tensors that are - # already registered as parameters by vLLM (and won't be freed) - def replace_tensor(name, new_t): - # It is important to use resize_() here since it ensures - # the same buffer is reused - getattr(layer, name).resize_(new_t.shape) - getattr(layer, name).copy_(new_t) - del new_t - - cur_device = layer.qweight.device - - # Process act_order - if self.quant_config.desc_act: - # Get sorting based on g_idx - g_idx_sort_indices = torch.argsort(layer.g_idx).to(torch.int) - - sorted_g_idx = layer.g_idx[g_idx_sort_indices] - - replace_tensor("g_idx", sorted_g_idx) - replace_tensor("g_idx_sort_indices", g_idx_sort_indices) - - else: - # Reset g_idx related tensors - layer.g_idx = Parameter( - torch.empty(0, dtype=torch.int, device=cur_device), - requires_grad=False, - ) - layer.g_idx_sort_indices = Parameter( - torch.empty(0, dtype=torch.int, device=cur_device), - requires_grad=False, - ) - - # Repack weights - marlin_qweight = ops.gptq_marlin_repack( - layer.qweight, - layer.g_idx_sort_indices, - part_size_k, - part_size_n, - self.quant_config.weight_bits, - ) - replace_tensor("qweight", marlin_qweight) - - # Permute scales - scales_size_k = part_size_k - scales_size_n = part_size_n - if self.quant_config.desc_act: - scales_size_k = full_size_k - - marlin_scales = marlin_permute_scales( - layer.scales, - scales_size_k, - scales_size_n, - self.quant_config.group_size, - self.quant_config.weight_bits, - ) - replace_tensor("scales", marlin_scales) - - output = ops.gptq_marlin_gemm( - reshaped_x, - layer.qweight, - layer.scales, - layer.g_idx, - layer.g_idx_sort_indices, - layer.workspace, - self.quant_config.weight_bits, - size_m, - part_size_n, - part_size_k, - layer.is_k_full, - ) - - if bias is not None: - output.add_(bias) # In-place add - - return output.reshape(out_shape) diff --git a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/utils/marlin_utils.py b/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/utils/marlin_utils.py index 7b0398f..accbc00 100644 --- a/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/utils/marlin_utils.py +++ b/ktransformers/ktransformers_ext/operators/custom_marlin/quantize/utils/marlin_utils.py @@ -220,7 +220,7 @@ def compute_max_diff(output, output_ref): class MarlinWorkspace: - def __init__(self, out_features, min_thread_n, max_parallel): + def __init__(self, out_features, min_thread_n, max_parallel, device): assert (out_features % min_thread_n == 0), ( "out_features = {} is undivisible by min_thread_n = {}".format( out_features, min_thread_n)) @@ -229,4 +229,4 @@ def __init__(self, out_features, min_thread_n, max_parallel): self.scratch = torch.zeros(max_workspace_size, dtype=torch.int, - device="cuda") + device=device) diff --git a/ktransformers/ktransformers_ext/operators/llamafile/linear.cpp b/ktransformers/ktransformers_ext/operators/llamafile/linear.cpp index bf1935e..81e5006 100644 --- a/ktransformers/ktransformers_ext/operators/llamafile/linear.cpp +++ b/ktransformers/ktransformers_ext/operators/llamafile/linear.cpp @@ -3,7 +3,7 @@ * @Author : chenht2022 * @Date : 2024-07-12 10:07:58 * @Version : 1.0.0 - * @LastEditors : chenht2022 + * @LastEditors : chenht2022 * @LastEditTime : 2024-07-25 10:34:58 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ @@ -13,9 +13,15 @@ Linear::Linear(LinearConfig config) { config_ = config; proj_ = config_.proj; - input_fp32_.resize(config_.input_size); - proj_input_.resize(config_.input_size * 4); - proj_output_.resize(config_.output_size); + std::vector> mem_requests; + mem_requests.push_back({(void**)&input_fp32_, sizeof(float) * config_.group_max_len * config_.input_size}); + mem_requests.push_back({(void**)&proj_input_, config_.group_max_len * config_.input_size * ggml_type_size(ggml_internal_get_type_traits(config_.proj_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.proj_type).vec_dot_type)}); + mem_requests.push_back({(void**)&proj_output_, sizeof(float) * config_.group_max_len * config_.output_size}); + shared_mem_buffer.alloc(this, mem_requests); +} + +Linear::~Linear() { + shared_mem_buffer.dealloc(this); } void Linear::warm_up(Backend* backend) { @@ -26,22 +32,42 @@ void Linear::warm_up(Backend* backend) { input_fp32[i] = 0; } from_float(input_fp32.data(), input.data(), config_.input_size, config_.hidden_type); - forward(input.data(), output.data(), backend); + forward_many(1, input.data(), output.data(), backend); } -void Linear::forward(const void* input, void* output, Backend* backend) { +void Linear::forward_many(int qlen, const void* input, void* output, Backend* backend) { const void* proj_input_ptr; if (config_.hidden_type == ggml_internal_get_type_traits(config_.proj_type).vec_dot_type) { proj_input_ptr = input; } else { - to_float(input, input_fp32_.data(), config_.input_size, config_.hidden_type); - from_float(input_fp32_.data(), proj_input_.data(), config_.input_size, ggml_internal_get_type_traits(config_.proj_type).vec_dot_type); - proj_input_ptr = proj_input_.data(); + to_float(input, input_fp32_, qlen * config_.input_size, config_.hidden_type); + from_float(input_fp32_, proj_input_, qlen * config_.input_size, ggml_internal_get_type_traits(config_.proj_type).vec_dot_type); + proj_input_ptr = proj_input_; } int nth = config_.output_size / config_.stride; backend->do_work_stealing_job(nth, [&](int task_id) { - int ith = task_id % nth; - llamafile_sgemm(config_.output_size, 1, config_.input_size / ggml_blck_size(config_.proj_type), proj_, config_.input_size / ggml_blck_size(config_.proj_type), proj_input_ptr, config_.input_size / ggml_blck_size(config_.proj_type), proj_output_.data(), config_.output_size, ith, nth, GGML_TASK_TYPE_COMPUTE, config_.proj_type, ggml_internal_get_type_traits(config_.proj_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); + int ith = task_id; + void* proj_ptr = (uint8_t*)proj_ + ith * config_.stride * config_.input_size * ggml_type_size(config_.proj_type) / ggml_blck_size(config_.proj_type); + float* proj_output_ptr = proj_output_ + ith * config_.stride; + llamafile_sgemm(config_.stride, qlen, config_.input_size / ggml_blck_size(config_.proj_type), proj_ptr, config_.input_size / ggml_blck_size(config_.proj_type), proj_input_ptr, config_.input_size / ggml_blck_size(config_.proj_type), proj_output_ptr, config_.output_size, 0, 1, GGML_TASK_TYPE_COMPUTE, config_.proj_type, ggml_internal_get_type_traits(config_.proj_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); + if (config_.stride % ggml_blck_size(config_.hidden_type) == 0) { + for (int i = 0; i < qlen; i++) { + float* output_fp32_ptr = proj_output_ + i * config_.output_size + ith * config_.stride; + void* output_ptr = (uint8_t*)output + i * config_.output_size * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type) + ith * config_.stride * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type); + from_float(output_fp32_ptr, output_ptr, config_.stride, config_.hidden_type); + } + } }); - from_float(proj_output_.data(), output, config_.output_size, config_.hidden_type); + if (config_.stride % ggml_blck_size(config_.hidden_type) != 0) { + from_float(proj_output_, output, qlen * config_.output_size, config_.hidden_type); + } +} + +void Linear::forward(int qlen, const void* input, void* output, Backend* backend) { + if (qlen <= 0) { + return; + } + int forward_len = std::min(qlen, config_.group_max_len); + forward_many(forward_len, input, output, backend); + forward(qlen - forward_len, (uint8_t*)input + forward_len * config_.input_size * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type), (uint8_t*)output + forward_len * config_.output_size * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type), backend); } \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/operators/llamafile/linear.h b/ktransformers/ktransformers_ext/operators/llamafile/linear.h index 4285551..fd856f9 100644 --- a/ktransformers/ktransformers_ext/operators/llamafile/linear.h +++ b/ktransformers/ktransformers_ext/operators/llamafile/linear.h @@ -3,7 +3,7 @@ * @Author : chenht2022 * @Date : 2024-07-12 10:07:58 * @Version : 1.0.0 - * @LastEditors : chenht2022 + * @LastEditors : chenht2022 * @LastEditTime : 2024-07-25 10:35:00 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ @@ -22,34 +22,38 @@ #include "llama.cpp/ggml-quants.h" #include "llama.cpp/ggml.h" #include "llamafile/sgemm.h" +#include "shared_mem_buffer.h" struct LinearConfig { int input_size; int output_size; int stride; + int group_max_len; void* proj; ggml_type proj_type; ggml_type hidden_type; LinearConfig() {} - LinearConfig(int input_size, int output_size, int stride, void* proj, ggml_type proj_type, ggml_type hidden_type) - : input_size(input_size), output_size(output_size), stride(stride), proj(proj), proj_type(proj_type), hidden_type(hidden_type) {} + LinearConfig(int input_size, int output_size, int stride, int group_max_len, void* proj, ggml_type proj_type, ggml_type hidden_type) + : input_size(input_size), output_size(output_size), stride(stride), group_max_len(group_max_len), proj(proj), proj_type(proj_type), hidden_type(hidden_type) {} }; class Linear { public: Linear(LinearConfig); + ~Linear(); void warm_up(Backend* backend); - void forward(const void* input, void* output, Backend* backend); + void forward_many(int qlen, const void* input, void* output, Backend* backend); + void forward(int qlen, const void* input, void* output, Backend* backend); private: LinearConfig config_; void* proj_; // [output_size * input_size ( /32 if quantized)] - std::vector input_fp32_; // [input_size] - std::vector proj_input_; // [input_size * 4] - std::vector proj_output_; // [output_size] + float* input_fp32_; // [group_max_len * input_size] + uint8_t* proj_input_; // [group_max_len * input_size * ggml_type_size(ggml_internal_get_type_traits(proj_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(proj_type).vec_dot_type)] + float* proj_output_; // [group_max_len * output_size] }; #endif \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/operators/llamafile/mlp.cpp b/ktransformers/ktransformers_ext/operators/llamafile/mlp.cpp index 979af5e..abad01e 100644 --- a/ktransformers/ktransformers_ext/operators/llamafile/mlp.cpp +++ b/ktransformers/ktransformers_ext/operators/llamafile/mlp.cpp @@ -3,7 +3,7 @@ * @Author : chenht2022 * @Date : 2024-07-16 10:43:18 * @Version : 1.0.0 - * @LastEditors : chenht2022 + * @LastEditors : chenht2022 * @LastEditTime : 2024-07-25 10:35:04 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ @@ -15,14 +15,20 @@ MLP::MLP(MLPConfig config) { up_proj_ = config_.up_proj; down_proj_ = config_.down_proj; - input_fp32_.resize(config_.hidden_size); - gate_input_.resize(config_.hidden_size * 4); - up_input_.resize(config_.hidden_size * 4); - gate_output_.resize(config_.intermediate_size); - up_output_.resize(config_.intermediate_size); - intermediate_fp32_.resize(config_.intermediate_size); - down_input_.resize(config_.intermediate_size * 4); - down_output_.resize(config_.hidden_size); + std::vector> mem_requests; + mem_requests.push_back({(void**)&input_fp32_, sizeof(float) * config_.group_max_len * config_.hidden_size}); + mem_requests.push_back({(void**)&gate_input_, config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type)}); + mem_requests.push_back({(void**)&up_input_, config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type)}); + mem_requests.push_back({(void**)&gate_output_, sizeof(float) * config_.group_max_len * config_.intermediate_size}); + mem_requests.push_back({(void**)&up_output_, sizeof(float) * config_.group_max_len * config_.intermediate_size}); + mem_requests.push_back({(void**)&intermediate_fp32_, sizeof(float) * config_.group_max_len * config_.intermediate_size}); + mem_requests.push_back({(void**)&down_input_, config_.group_max_len * config_.intermediate_size * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type)}); + mem_requests.push_back({(void**)&down_output_, sizeof(float) * config_.group_max_len * config_.hidden_size}); + shared_mem_buffer.alloc(this, mem_requests); +} + +MLP::~MLP() { + shared_mem_buffer.dealloc(this); } void MLP::warm_up(Backend* backend) { @@ -33,33 +39,33 @@ void MLP::warm_up(Backend* backend) { input_fp32[i] = 0; } from_float(input_fp32.data(), input.data(), config_.hidden_size, config_.hidden_type); - forward(input.data(), output.data(), backend); + forward_many(1, input.data(), output.data(), backend); } static float act_fn(float x) { return x / (1.0f + expf(-x)); } -void MLP::forward(const void* input, void* output, Backend* backend) { +void MLP::forward_many(int qlen, const void* input, void* output, Backend* backend) { const void* gate_input_ptr; const void* up_input_ptr; if (config_.hidden_type == ggml_internal_get_type_traits(config_.gate_type).vec_dot_type && config_.hidden_type == ggml_internal_get_type_traits(config_.up_type).vec_dot_type) { gate_input_ptr = up_input_ptr = input; } else { - to_float(input, input_fp32_.data(), config_.hidden_size, config_.hidden_type); + to_float(input, input_fp32_, qlen * config_.hidden_size, config_.hidden_type); if (ggml_internal_get_type_traits(config_.gate_type).vec_dot_type == ggml_internal_get_type_traits(config_.up_type).vec_dot_type) { - from_float(input_fp32_.data(), gate_input_.data(), config_.hidden_size, ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); - gate_input_ptr = up_input_ptr = gate_input_.data(); + from_float(input_fp32_, gate_input_, qlen * config_.hidden_size, ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); + gate_input_ptr = up_input_ptr = gate_input_; } else { if (config_.hidden_type != ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) { - from_float(input_fp32_.data(), gate_input_.data(), config_.hidden_size, ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); - gate_input_ptr = gate_input_.data(); + from_float(input_fp32_, gate_input_, qlen * config_.hidden_size, ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); + gate_input_ptr = gate_input_; } else { gate_input_ptr = input; } if (config_.hidden_type != ggml_internal_get_type_traits(config_.up_type).vec_dot_type) { - from_float(input_fp32_.data(), up_input_.data(), config_.hidden_size, ggml_internal_get_type_traits(config_.up_type).vec_dot_type); - up_input_ptr = up_input_.data(); + from_float(input_fp32_, up_input_, qlen * config_.hidden_size, ggml_internal_get_type_traits(config_.up_type).vec_dot_type); + up_input_ptr = up_input_; } else { up_input_ptr = input; } @@ -69,35 +75,49 @@ void MLP::forward(const void* input, void* output, Backend* backend) { backend->do_work_stealing_job(nth, [&](int task_id) { int ith = task_id; void* gate_proj_ptr = (uint8_t*)gate_proj_ + ith * config_.stride * config_.hidden_size * ggml_type_size(config_.gate_type) / ggml_blck_size(config_.gate_type); - float* gate_output_ptr = gate_output_.data() + ith * config_.stride; - llamafile_sgemm(config_.stride, 1, config_.hidden_size / ggml_blck_size(config_.gate_type), gate_proj_ptr, config_.hidden_size / ggml_blck_size(config_.gate_type), gate_input_ptr, config_.hidden_size / ggml_blck_size(config_.gate_type), gate_output_ptr, config_.stride, 0, 1, GGML_TASK_TYPE_COMPUTE, config_.gate_type, ggml_internal_get_type_traits(config_.gate_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); + float* gate_output_ptr = gate_output_ + ith * config_.stride; + llamafile_sgemm(config_.stride, qlen, config_.hidden_size / ggml_blck_size(config_.gate_type), gate_proj_ptr, config_.hidden_size / ggml_blck_size(config_.gate_type), gate_input_ptr, config_.hidden_size / ggml_blck_size(config_.gate_type), gate_output_ptr, config_.intermediate_size, 0, 1, GGML_TASK_TYPE_COMPUTE, config_.gate_type, ggml_internal_get_type_traits(config_.gate_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); void* up_proj_ptr = (uint8_t*)up_proj_ + ith * config_.stride * config_.hidden_size * ggml_type_size(config_.up_type) / ggml_blck_size(config_.up_type); - float* up_output_ptr = up_output_.data() + ith * config_.stride; - llamafile_sgemm(config_.stride, 1, config_.hidden_size / ggml_blck_size(config_.up_type), up_proj_ptr, config_.hidden_size / ggml_blck_size(config_.up_type), up_input_ptr, config_.hidden_size / ggml_blck_size(config_.up_type), up_output_ptr, config_.stride, 0, 1, GGML_TASK_TYPE_COMPUTE, config_.up_type, ggml_internal_get_type_traits(config_.up_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); - for (int i = ith * config_.stride; i < (ith + 1) * config_.stride; i++) { - intermediate_fp32_[i] = act_fn(gate_output_[i]) * up_output_[i]; - } - if (config_.stride % ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) == 0) { - float* intermediate_fp32_ptr = intermediate_fp32_.data() + ith * config_.stride; - void* down_input_ptr = (uint8_t*)down_input_.data() + ith * config_.stride * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type); - from_float(intermediate_fp32_ptr, down_input_ptr, config_.stride, ggml_internal_get_type_traits(config_.down_type).vec_dot_type); + float* up_output_ptr = up_output_ + ith * config_.stride; + llamafile_sgemm(config_.stride, qlen, config_.hidden_size / ggml_blck_size(config_.up_type), up_proj_ptr, config_.hidden_size / ggml_blck_size(config_.up_type), up_input_ptr, config_.hidden_size / ggml_blck_size(config_.up_type), up_output_ptr, config_.intermediate_size, 0, 1, GGML_TASK_TYPE_COMPUTE, config_.up_type, ggml_internal_get_type_traits(config_.up_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); + for (int i = 0; i < qlen; i++) { + for (int j = ith * config_.stride; j < (ith + 1) * config_.stride; j++) { + intermediate_fp32_[i * config_.intermediate_size + j] = act_fn(gate_output_[i * config_.intermediate_size + j]) * up_output_[i * config_.intermediate_size + j]; + } + if (config_.stride % ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) == 0) { + float* intermediate_fp32_ptr = intermediate_fp32_ + i * config_.intermediate_size + ith * config_.stride; + void* down_input_ptr = (uint8_t*)down_input_ + i * config_.intermediate_size * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) + ith * config_.stride * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type); + from_float(intermediate_fp32_ptr, down_input_ptr, config_.stride, ggml_internal_get_type_traits(config_.down_type).vec_dot_type); + } } }); if (config_.stride % ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) != 0) { - from_float(intermediate_fp32_.data(), down_input_.data(), config_.intermediate_size, ggml_internal_get_type_traits(config_.down_type).vec_dot_type); + from_float(intermediate_fp32_, down_input_, qlen * config_.intermediate_size, ggml_internal_get_type_traits(config_.down_type).vec_dot_type); } nth = config_.hidden_size / config_.stride; backend->do_work_stealing_job(nth, [&](int task_id) { int ith = task_id; void* down_proj_ptr = (uint8_t*)down_proj_ + ith * config_.stride * config_.intermediate_size * ggml_type_size(config_.down_type) / ggml_blck_size(config_.down_type); - float* down_output_ptr = down_output_.data() + ith * config_.stride; - llamafile_sgemm(config_.stride, 1, config_.intermediate_size / ggml_blck_size(config_.down_type), down_proj_ptr, config_.intermediate_size / ggml_blck_size(config_.down_type), down_input_.data(), config_.intermediate_size / ggml_blck_size(config_.down_type), down_output_ptr, config_.stride, 0, 1, GGML_TASK_TYPE_COMPUTE, config_.down_type, ggml_internal_get_type_traits(config_.down_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); + float* down_output_ptr = down_output_ + ith * config_.stride; + llamafile_sgemm(config_.stride, qlen, config_.intermediate_size / ggml_blck_size(config_.down_type), down_proj_ptr, config_.intermediate_size / ggml_blck_size(config_.down_type), down_input_, config_.intermediate_size / ggml_blck_size(config_.down_type), down_output_ptr, config_.hidden_size, 0, 1, GGML_TASK_TYPE_COMPUTE, config_.down_type, ggml_internal_get_type_traits(config_.down_type).vec_dot_type, GGML_TYPE_F32, GGML_PREC_DEFAULT); if (config_.stride % ggml_blck_size(config_.hidden_type) == 0) { - void* output_ptr = (uint8_t*)output + ith * config_.stride * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type); - from_float(down_output_ptr, output_ptr, config_.stride, config_.hidden_type); + for (int i = 0; i < qlen; i++) { + float* output_fp32_ptr = down_output_ + i * config_.hidden_size + ith * config_.stride; + void* output_ptr = (uint8_t*)output + i * config_.hidden_size * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type) + ith * config_.stride * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type); + from_float(output_fp32_ptr, output_ptr, config_.stride, config_.hidden_type); + } } }); if (config_.stride % ggml_blck_size(config_.hidden_type) != 0) { - from_float(down_output_.data(), output, config_.hidden_size, config_.hidden_type); + from_float(down_output_, output, qlen * config_.hidden_size, config_.hidden_type); } } + +void MLP::forward(int qlen, const void* input, void* output, Backend* backend) { + if (qlen <= 0) { + return; + } + int forward_len = std::min(qlen, config_.group_max_len); + forward_many(forward_len, input, output, backend); + forward(qlen - forward_len, (uint8_t*)input + forward_len * config_.hidden_size * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type), (uint8_t*)output + forward_len * config_.hidden_size * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type), backend); +} \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/operators/llamafile/mlp.h b/ktransformers/ktransformers_ext/operators/llamafile/mlp.h index 604db77..eb93294 100644 --- a/ktransformers/ktransformers_ext/operators/llamafile/mlp.h +++ b/ktransformers/ktransformers_ext/operators/llamafile/mlp.h @@ -3,7 +3,7 @@ * @Author : chenht2022 * @Date : 2024-07-12 10:07:58 * @Version : 1.0.0 - * @LastEditors : chenht2022 + * @LastEditors : chenht2022 * @LastEditTime : 2024-07-25 10:35:06 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ @@ -22,11 +22,13 @@ #include "llama.cpp/ggml-quants.h" #include "llama.cpp/ggml.h" #include "llamafile/sgemm.h" +#include "shared_mem_buffer.h" struct MLPConfig { int hidden_size; int intermediate_size; int stride; + int group_max_len; void* gate_proj; void* up_proj; void* down_proj; @@ -37,15 +39,17 @@ struct MLPConfig { MLPConfig() {} - MLPConfig(int hidden_size, int intermediate_size, int stride, void* gate_proj, void* up_proj, void* down_proj, ggml_type gate_type, ggml_type up_type, ggml_type down_type, ggml_type hidden_type) - : hidden_size(hidden_size), intermediate_size(intermediate_size), stride(stride), gate_proj(gate_proj), up_proj(up_proj), down_proj(down_proj), gate_type(gate_type), up_type(up_type), down_type(down_type), hidden_type(hidden_type) {} + MLPConfig(int hidden_size, int intermediate_size, int stride, int group_max_len, void* gate_proj, void* up_proj, void* down_proj, ggml_type gate_type, ggml_type up_type, ggml_type down_type, ggml_type hidden_type) + : hidden_size(hidden_size), intermediate_size(intermediate_size), stride(stride), group_max_len(group_max_len), gate_proj(gate_proj), up_proj(up_proj), down_proj(down_proj), gate_type(gate_type), up_type(up_type), down_type(down_type), hidden_type(hidden_type) {} }; class MLP { public: MLP(MLPConfig); + ~MLP(); void warm_up(Backend* backend); - void forward(const void* input, void* output, Backend* backend); + void forward_many(int qlen, const void* input, void* output, Backend* backend); + void forward(int qlen, const void* input, void* output, Backend* backend); private: MLPConfig config_; @@ -53,14 +57,14 @@ class MLP { void* up_proj_; // [intermediate_size * hidden_size ( /32 if quantized)] void* down_proj_; // [hidden_size * intermediate_size ( /32 if quantized)] - std::vector input_fp32_; // [hidden_size] - std::vector gate_input_; // [hidden_size * 4] - std::vector up_input_; // [hidden_size * 4] - std::vector gate_output_; // [intermediate_size] - std::vector up_output_; // [intermediate_size] - std::vector intermediate_fp32_; // [intermediate_size] - std::vector down_input_; // [intermediate_size * 4] - std::vector down_output_; // [hidden_size] + float* input_fp32_; // [group_max_len * hidden_size] + uint8_t* gate_input_; // [group_max_len * hidden_size * ggml_type_size(ggml_internal_get_type_traits(gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(gate_type).vec_dot_type)] + uint8_t* up_input_; // [group_max_len * hidden_size * ggml_type_size(ggml_internal_get_type_traits(up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(up_type).vec_dot_type)] + float* gate_output_; // [group_max_len * intermediate_size] + float* up_output_; // [group_max_len * intermediate_size] + float* intermediate_fp32_; // [group_max_len * intermediate_size] + uint8_t* down_input_; // [group_max_len * intermediate_size * ggml_type_size(ggml_internal_get_type_traits(down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(down_type).vec_dot_type)] + float* down_output_; // [group_max_len * hidden_size] }; #endif \ No newline at end of file diff --git a/ktransformers/ktransformers_ext/operators/llamafile/moe.cpp b/ktransformers/ktransformers_ext/operators/llamafile/moe.cpp index 3a5c852..d75db65 100644 --- a/ktransformers/ktransformers_ext/operators/llamafile/moe.cpp +++ b/ktransformers/ktransformers_ext/operators/llamafile/moe.cpp @@ -1,97 +1,62 @@ /** - * @Description : + * @Description : * @Author : chenht2022 * @Date : 2024-07-22 02:03:22 * @Version : 1.0.0 - * @LastEditors : chenht2022 + * @LastEditors : chenht2022 * @LastEditTime : 2024-07-25 10:35:07 - * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. -**/ + * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. + **/ #include "moe.h" #include #include -uint8_t* MOE::buffer_ = nullptr; - MOE::MOE(MOEConfig config) { config_ = config; gate_proj_ = config_.gate_proj; up_proj_ = config_.up_proj; down_proj_ = config_.down_proj; - if (MOE::buffer_ == nullptr) { - uint64_t buffer_size = 0; - buffer_size += sizeof(float) * config_.group_max_len * config_.hidden_size; - buffer_size += config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); - buffer_size += config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type); - buffer_size += config_.routed_expert_num * config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); - buffer_size += config_.routed_expert_num * config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type); - buffer_size += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size; - buffer_size += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size; - buffer_size += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size; - buffer_size += config_.routed_expert_num * config_.group_max_len * config_.intermediate_size * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type); - buffer_size += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.hidden_size; - buffer_size += sizeof(float) * config_.group_max_len * config_.hidden_size; - buffer_ = (uint8_t*)malloc(buffer_size); - } - - uint64_t offset = 0; - s_input_fp32_ = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.hidden_size; - s_gate_input_ = (uint8_t*)(buffer_ + offset); - offset += config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); - s_up_input_ = (uint8_t*)(buffer_ + offset); - offset += config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type); + std::vector> s_mem_requests; + s_mem_requests.push_back({(void**)&s_input_fp32_, sizeof(float) * config_.hidden_size}); + s_mem_requests.push_back({(void**)&s_gate_input_, config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type)}); + s_mem_requests.push_back({(void**)&s_up_input_, config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type)}); s_gate_output_.resize(config_.routed_expert_num); s_up_output_.resize(config_.routed_expert_num); s_intermediate_fp32_.resize(config_.routed_expert_num); s_down_input_.resize(config_.routed_expert_num); s_down_output_.resize(config_.routed_expert_num); for (int i = 0; i < config_.routed_expert_num; i++) { - s_gate_output_[i] = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.intermediate_size; - s_up_output_[i] = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.intermediate_size; - s_intermediate_fp32_[i] = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.intermediate_size; - s_down_input_[i] = (uint8_t*)(buffer_ + offset); - offset += config_.intermediate_size * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type); - s_down_output_[i] = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.hidden_size; + s_mem_requests.push_back({(void**)&s_gate_output_[i], sizeof(float) * config_.intermediate_size}); + s_mem_requests.push_back({(void**)&s_up_output_[i], sizeof(float) * config_.intermediate_size}); + s_mem_requests.push_back({(void**)&s_intermediate_fp32_[i], sizeof(float) * config_.intermediate_size}); + s_mem_requests.push_back({(void**)&s_down_input_[i], config_.intermediate_size * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type)}); + s_mem_requests.push_back({(void**)&s_down_output_[i], sizeof(float) * config_.hidden_size}); } - s_output_fp32_ = (float*)(buffer_ + offset); + s_mem_requests.push_back({(void**)&s_output_fp32_, sizeof(float) * config_.hidden_size}); + shared_mem_buffer.alloc(this, s_mem_requests); - offset = 0; + std::vector> m_mem_requests; m_input_fp32_.resize(config_.group_max_len); m_gate_input_.resize(config_.group_max_len); m_up_input_.resize(config_.group_max_len); for (int i = 0; i < config_.group_max_len; i++) { - m_input_fp32_[i] = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.hidden_size; - m_gate_input_[i] = (uint8_t*)(buffer_ + offset); - offset += config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); - m_up_input_[i] = (uint8_t*)(buffer_ + offset); - offset += config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type); + m_mem_requests.push_back({(void**)&m_input_fp32_[i], sizeof(float) * config_.hidden_size}); + m_mem_requests.push_back({(void**)&m_gate_input_[i], config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type)}); + m_mem_requests.push_back({(void**)&m_up_input_[i], config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type)}); } - m_local_gate_input_ = (uint8_t*)(buffer_ + offset); - offset += config_.routed_expert_num * config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type); - m_local_up_input_ = (uint8_t*)(buffer_ + offset); - offset += config_.routed_expert_num * config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type); - m_local_gate_output_ = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size; - m_local_up_output_ = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size; - m_local_intermediate_fp32_ = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size; - m_local_down_input_ = (uint8_t*)(buffer_ + offset); - offset += config_.routed_expert_num * config_.group_max_len * config_.intermediate_size * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type); - m_local_down_output_ = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.hidden_size; + m_mem_requests.push_back({(void**)&m_local_gate_input_, config_.routed_expert_num * config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.gate_type).vec_dot_type)}); + m_mem_requests.push_back({(void**)&m_local_up_input_, config_.routed_expert_num * config_.group_max_len * config_.hidden_size * ggml_type_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.up_type).vec_dot_type)}); + m_mem_requests.push_back({(void**)&m_local_gate_output_, sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size}); + m_mem_requests.push_back({(void**)&m_local_up_output_, sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size}); + m_mem_requests.push_back({(void**)&m_local_intermediate_fp32_, sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.intermediate_size}); + m_mem_requests.push_back({(void**)&m_local_down_input_, config_.routed_expert_num * config_.group_max_len * config_.intermediate_size * ggml_type_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type) / ggml_blck_size(ggml_internal_get_type_traits(config_.down_type).vec_dot_type)}); + m_mem_requests.push_back({(void**)&m_local_down_output_, sizeof(float) * config_.routed_expert_num * config_.group_max_len * config_.hidden_size}); m_output_fp32_.resize(config_.group_max_len); for (int i = 0; i < config_.group_max_len; i++) { - m_output_fp32_[i] = (float*)(buffer_ + offset); - offset += sizeof(float) * config_.hidden_size; + m_mem_requests.push_back({(void**)&m_output_fp32_[i], sizeof(float) * config_.hidden_size}); } + shared_mem_buffer.alloc(this, m_mem_requests); m_local_pos_.resize(config_.group_max_len); for (int i = 0; i < config_.group_max_len; i++) { @@ -107,6 +72,10 @@ MOE::MOE(MOEConfig config) { m_local_down_output_ptr_.resize(config_.expert_num); } +MOE::~MOE() { + shared_mem_buffer.dealloc(this); +} + void MOE::warm_up(Backend* backend) { std::vector input_fp32(config_.hidden_size); std::vector input(config_.hidden_size * ggml_type_size(config_.hidden_type) / ggml_blck_size(config_.hidden_type)); diff --git a/ktransformers/ktransformers_ext/operators/llamafile/moe.h b/ktransformers/ktransformers_ext/operators/llamafile/moe.h index 588a829..a1470aa 100644 --- a/ktransformers/ktransformers_ext/operators/llamafile/moe.h +++ b/ktransformers/ktransformers_ext/operators/llamafile/moe.h @@ -3,7 +3,7 @@ * @Author : chenht2022 * @Date : 2024-07-22 02:03:22 * @Version : 1.0.0 - * @LastEditors : chenht2022 + * @LastEditors : chenht2022 * @LastEditTime : 2024-07-25 10:35:10 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. **/ @@ -22,6 +22,7 @@ #include "llama.cpp/ggml-quants.h" #include "llama.cpp/ggml.h" #include "llamafile/sgemm.h" +#include "shared_mem_buffer.h" struct MOEConfig { int expert_num; @@ -48,13 +49,13 @@ struct MOEConfig { class MOE { public: MOE(MOEConfig); + ~MOE(); void warm_up(Backend* backend); void forward_one(int k, const uint64_t* expert_ids, const float* weights, const void* input, void* output, Backend* backend); void forward_many(int qlen, int k, const uint64_t* expert_ids, const float* weights, const void* input, void* output, Backend* backend); void forward(int qlen, int k, const uint64_t* expert_ids, const float* weights, const void* input, void* output, Backend* backend); private: - static uint8_t* buffer_; MOEConfig config_; void* gate_proj_; // [expert_num * intermediate_size * hidden_size ( /32 if quantized)] void* up_proj_; // [expert_num * intermediate_size * hidden_size ( /32 if quantized)] diff --git a/ktransformers/ktransformers_ext/operators/llamafile/shared_mem_buffer.cpp b/ktransformers/ktransformers_ext/operators/llamafile/shared_mem_buffer.cpp new file mode 100644 index 0000000..dc2d65d --- /dev/null +++ b/ktransformers/ktransformers_ext/operators/llamafile/shared_mem_buffer.cpp @@ -0,0 +1,55 @@ +/** + * @Description : + * @Author : chenht2022 + * @Date : 2024-08-05 04:49:08 + * @Version : 1.0.0 + * @LastEditors : chenht2022 + * @LastEditTime : 2024-08-05 09:21:29 + * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. + **/ +#include "shared_mem_buffer.h" +#include + +SharedMemBuffer::SharedMemBuffer() { + buffer_ = nullptr; + size_ = 0; +} + +SharedMemBuffer::~SharedMemBuffer() { + if (buffer_) { + free(buffer_); + } +} + +void SharedMemBuffer::alloc(void* object, std::vector> requests) { + uint64_t size = 0; + for (auto& request : requests) { + size += request.second; + } + if (size > size_) { + if (buffer_) { + free(buffer_); + } + buffer_ = malloc(size); + size_ = size; + for (auto& obj_requests : hist_requests_) { + for (auto& requests : obj_requests.second) { + arrange(requests); + } + } + } + arrange(requests); + hist_requests_[object].push_back(requests); +} + +void SharedMemBuffer::dealloc(void* object) { + hist_requests_.erase(object); +} + +void SharedMemBuffer::arrange(std::vector> requests) { + uint64_t offset = 0; + for (auto& request : requests) { + *(request.first) = (uint8_t*)buffer_ + offset; + offset += request.second; + } +} diff --git a/ktransformers/ktransformers_ext/operators/llamafile/shared_mem_buffer.h b/ktransformers/ktransformers_ext/operators/llamafile/shared_mem_buffer.h new file mode 100644 index 0000000..eeaccd4 --- /dev/null +++ b/ktransformers/ktransformers_ext/operators/llamafile/shared_mem_buffer.h @@ -0,0 +1,37 @@ +/** + * @Description : + * @Author : chenht2022 + * @Date : 2024-08-05 04:49:08 + * @Version : 1.0.0 + * @LastEditors : chenht2022 + * @LastEditTime : 2024-08-05 06:36:41 + * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved. + **/ + +#ifndef CPUINFER_SHAREDMEMBUFFER_H +#define CPUINFER_SHAREDMEMBUFFER_H + +#include +#include +#include +#include + +class SharedMemBuffer { + public: + SharedMemBuffer(); + ~SharedMemBuffer(); + + void alloc(void* object, std::vector> requests); + void dealloc(void* object); + + private: + void* buffer_; + uint64_t size_; + std::map>>> hist_requests_; + + void arrange(std::vector> requests); +}; + +static SharedMemBuffer shared_mem_buffer; + +#endif \ No newline at end of file diff --git a/ktransformers/local_chat.py b/ktransformers/local_chat.py old mode 100644 new mode 100755 index 2b2c8a7..b5782d1 --- a/ktransformers/local_chat.py +++ b/ktransformers/local_chat.py @@ -31,18 +31,21 @@ from ktransformers.optimize.optimize import optimize_and_load_gguf from ktransformers.models.modeling_deepseek import DeepseekV2ForCausalLM from ktransformers.models.modeling_qwen2_moe import Qwen2MoeForCausalLM +from ktransformers.models.modeling_mixtral import MixtralForCausalLM from ktransformers.util.utils import prefill_and_generate from ktransformers.server.config.config import Config custom_models = { "DeepseekV2ForCausalLM": DeepseekV2ForCausalLM, "Qwen2MoeForCausalLM": Qwen2MoeForCausalLM, + "MixtralForCausalLM": MixtralForCausalLM, } ktransformer_rules_dir = os.path.dirname(os.path.abspath(__file__)) + "/optimize/optimize_rules/" default_optimize_rules ={ "DeepseekV2ForCausalLM": ktransformer_rules_dir + "DeepSeek-V2-Chat.yaml", "Qwen2MoeForCausalLM": ktransformer_rules_dir + "Qwen2-57B-A14B-Instruct.yaml", + "MixtralForCausalLM": ktransformer_rules_dir + "Mixtral.yaml", } def local_chat( @@ -50,7 +53,8 @@ def local_chat( optimize_rule_path: str = None, gguf_path: str = None, max_new_tokens: int = 1000, - cpu_infer: int = Config().cpu_infer + cpu_infer: int = Config().cpu_infer, + use_cuda_graph: bool = True, ): torch.set_grad_enabled(False) @@ -64,6 +68,8 @@ def local_chat( print("using custom modeling_xxx.py.") if "Qwen2Moe" in config.architectures[0]: # Qwen2Moe must use flash_attention_2 to avoid overflow. config._attn_implementation = "flash_attention_2" + if "Mixtral" in config.architectures[0]: + config._attn_implementation = "flash_attention_2" model = custom_models[config.architectures[0]](config) else: model = AutoModelForCausalLM.from_config( @@ -100,7 +106,6 @@ def local_chat( while True: content = input("Chat: ") - # if content is num if content == "": content = "Please write a piece of quicksort code in C++." @@ -109,7 +114,7 @@ def local_chat( messages, add_generation_prompt=True, return_tensors="pt" ) torch.set_default_dtype(torch.bfloat16) # TODO: Remove this, replace dtype using config - generated = prefill_and_generate(model, tokenizer, input_tensor.cuda(), max_new_tokens) + generated = prefill_and_generate(model, tokenizer, input_tensor.cuda(), max_new_tokens, use_cuda_graph) if __name__ == "__main__": fire.Fire(local_chat) \ No newline at end of file diff --git a/ktransformers/models/custom_cache.py b/ktransformers/models/custom_cache.py index 93fc4c7..dbaea57 100644 --- a/ktransformers/models/custom_cache.py +++ b/ktransformers/models/custom_cache.py @@ -22,13 +22,14 @@ class StaticCache(transformers.StaticCache): The maximum batch size with which the model will be used. max_cache_len (`int`): The maximum sequence length with which the model will be used. - device (`torch.device`): + device (`torch.device` or `dict`): The device on which the cache should be initialized. Should be the same as the layer. + If a `dict`, it should contain the `device` key with the device name as the value. dtype (*optional*, defaults to `torch.float32`): The default `dtype` to use when initializing the layer. """ - def __init__(self, config: PretrainedConfig, max_batch_size: int, max_cache_len: int, device, dtype=None) -> None: + def __init__(self, config: PretrainedConfig, max_batch_size: int, max_cache_len: int, device: torch.device| dict, dtype=None) -> None: Cache.__init__(self) self.max_batch_size = max_batch_size self.max_cache_len = config.max_position_embeddings if max_cache_len is None else max_cache_len @@ -57,11 +58,15 @@ def __init__(self, config: PretrainedConfig, max_batch_size: int, max_cache_len: self.past_tokens = [] self.num_hidden_layers = config.num_hidden_layers - for _ in range(self.num_hidden_layers): + for idx in range(self.num_hidden_layers): # Note: `mark_static_address` is used to tag the cache as an fixed data pointer, preventing cuda graph # breaks when updating the cache. - new_layer_key_cache = torch.zeros(key_shape, dtype=self.dtype, device=device) - new_layer_value_cache = torch.zeros(value_shape, dtype=self.dtype, device=device) + if isinstance(device, dict): + target_device = device[f"blk.{idx}.self_attn"]["generate_device"] + else: + target_device = device + new_layer_key_cache = torch.zeros(key_shape, dtype=self.dtype, device=target_device) + new_layer_value_cache = torch.zeros(value_shape, dtype=self.dtype, device=target_device) torch._dynamo.mark_static_address(new_layer_key_cache) torch._dynamo.mark_static_address(new_layer_value_cache) self.key_cache.append(new_layer_key_cache) diff --git a/ktransformers/models/modeling_deepseek.py b/ktransformers/models/modeling_deepseek.py index 81fee86..692020d 100644 --- a/ktransformers/models/modeling_deepseek.py +++ b/ktransformers/models/modeling_deepseek.py @@ -1048,7 +1048,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. - Args: + # Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API key_states (`torch.Tensor`): @@ -1245,12 +1245,14 @@ def forward( cache_position=cache_position, **kwargs, ) + hidden_states = residual + hidden_states # Fully Connected residual = hidden_states hidden_states = self.post_attention_layernorm(hidden_states) hidden_states = self.mlp(hidden_states) + hidden_states = residual + hidden_states outputs = (hidden_states,) diff --git a/ktransformers/models/modeling_mixtral.py b/ktransformers/models/modeling_mixtral.py new file mode 100644 index 0000000..87d8cf1 --- /dev/null +++ b/ktransformers/models/modeling_mixtral.py @@ -0,0 +1,1735 @@ +# coding=utf-8 +''' +Description : +Author : kkk1nak0 +Date : 2024-07-29 02:58:57 +Version : 1.0.0 +LastEditors : kkk1nak0 +LastEditTime : 2024-08-02 06:08:34 +''' + +# Adapted from +# https://github.com/huggingface/transformers/blob/main/src/transformers/models/mixtral/modeling_mixtral.py +# Copyright 2023 Mistral AI and the HuggingFace Inc. team. All rights reserved. +# Copyright (c) 2024 by KVCache.AI, All Rights Reserved. +# +# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX +# and OPT implementations in this library. It has been modified from its +# original forms to accommodate minor architectural differences compared +# to GPT-NeoX and OPT used by the Meta AI team that trained the model. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""PyTorch Mixtral model.""" + +import inspect +import math +from typing import List, Optional, Tuple, Union + +import torch +import torch.nn.functional as F +import torch.utils.checkpoint +from torch import nn +from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss + +from transformers.activations import ACT2FN +from transformers.cache_utils import Cache, DynamicCache, StaticCache +from transformers.modeling_attn_mask_utils import ( + AttentionMaskConverter, + _prepare_4d_causal_attention_mask, +) +from transformers.modeling_outputs import ( + MoeCausalLMOutputWithPast, + MoeModelOutputWithPast, + SequenceClassifierOutputWithPast, + TokenClassifierOutput, +) +from transformers.modeling_utils import PreTrainedModel +from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_13 +from transformers.utils import ( + add_start_docstrings, + add_start_docstrings_to_model_forward, + is_flash_attn_2_available, + logging, + replace_return_docstrings, +) +from transformers.utils.import_utils import is_torch_fx_available +from transformers.models.mixtral.configuration_mixtral import MixtralConfig + + +if is_flash_attn_2_available(): + from flash_attn import flash_attn_varlen_func, flash_attn_func, flash_attn_with_kvcache + from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa + + _flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters) + +# This makes `_prepare_4d_causal_attention_mask` a leaf function in the FX graph. +# It means that the function will not be traced through and simply appear as a node in the graph. +if is_torch_fx_available(): + if not is_torch_greater_or_equal_than_1_13: + import torch.fx + + _prepare_4d_causal_attention_mask = torch.fx.wrap(_prepare_4d_causal_attention_mask) + + +logger = logging.get_logger(__name__) + +_CONFIG_FOR_DOC = "MixtralConfig" + + +def load_balancing_loss_func( + gate_logits: torch.Tensor, num_experts: torch.Tensor = None, top_k=2, attention_mask: Optional[torch.Tensor] = None +) -> float: + r""" + Computes auxiliary load balancing loss as in Switch Transformer - implemented in Pytorch. + + See Switch Transformer (https://arxiv.org/abs/2101.03961) for more details. This function implements the loss + function presented in equations (4) - (6) of the paper. It aims at penalizing cases where the routing between + experts is too unbalanced. + + Args: + gate_logits (Union[`torch.Tensor`, Tuple[torch.Tensor]): + Logits from the `gate`, should be a tuple of model.config.num_hidden_layers tensors of + shape [batch_size X sequence_length, num_experts]. + attention_mask (`torch.Tensor`, None): + The attention_mask used in forward function + shape [batch_size X sequence_length] if not None. + num_experts (`int`, *optional*): + Number of experts + + Returns: + The auxiliary loss. + """ + if gate_logits is None or not isinstance(gate_logits, tuple): + return 0 + + if isinstance(gate_logits, tuple): + compute_device = gate_logits[0].device + concatenated_gate_logits = torch.cat([layer_gate.to(compute_device) for layer_gate in gate_logits], dim=0) + + routing_weights = torch.nn.functional.softmax(concatenated_gate_logits, dim=-1) + + _, selected_experts = torch.topk(routing_weights, top_k, dim=-1) + + expert_mask = torch.nn.functional.one_hot(selected_experts, num_experts) + + if attention_mask is None: + # Compute the percentage of tokens routed to each experts + tokens_per_expert = torch.mean(expert_mask.float(), dim=0) + + # Compute the average probability of routing to these experts + router_prob_per_expert = torch.mean(routing_weights, dim=0) + else: + batch_size, sequence_length = attention_mask.shape + num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length) + + # Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask + expert_attention_mask = ( + attention_mask[None, :, :, None, None] + .expand((num_hidden_layers, batch_size, sequence_length, top_k, num_experts)) + .reshape(-1, top_k, num_experts) + .to(compute_device) + ) + + # Compute the percentage of tokens routed to each experts + tokens_per_expert = torch.sum(expert_mask.float() * expert_attention_mask, dim=0) / torch.sum( + expert_attention_mask, dim=0 + ) + + # Compute the mask that masks all padding tokens as 0 with the same shape of tokens_per_expert + router_per_expert_attention_mask = ( + attention_mask[None, :, :, None] + .expand((num_hidden_layers, batch_size, sequence_length, num_experts)) + .reshape(-1, num_experts) + .to(compute_device) + ) + + # Compute the average probability of routing to these experts + router_prob_per_expert = torch.sum(routing_weights * router_per_expert_attention_mask, dim=0) / torch.sum( + router_per_expert_attention_mask, dim=0 + ) + + overall_loss = torch.sum(tokens_per_expert * router_prob_per_expert.unsqueeze(0)) + return overall_loss * num_experts + + +# Copied from transformers.models.llama.modeling_llama._get_unpad_data +def _get_unpad_data(attention_mask): + seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32) + indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten() + max_seqlen_in_batch = seqlens_in_batch.max().item() + cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0)) + return ( + indices, + cu_seqlens, + max_seqlen_in_batch, + ) + + +# Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->Mixtral +class MixtralRMSNorm(nn.Module): + def __init__(self, hidden_size, eps=1e-6): + """ + MixtralRMSNorm is equivalent to T5LayerNorm + """ + super().__init__() + self.weight = nn.Parameter(torch.ones(hidden_size)) + self.variance_epsilon = eps + + def forward(self, hidden_states): + input_dtype = hidden_states.dtype + hidden_states = hidden_states.to(torch.float32) + variance = hidden_states.pow(2).mean(-1, keepdim=True) + hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) + return self.weight * hidden_states.to(input_dtype) + + def extra_repr(self): + return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" + + +# copied from transformers.models.mistral.modeling_mistral.MistralRotaryEmbedding with Mistral->Mixtral +# TODO @longjie no longer copied from Mistral after static cache +class MixtralRotaryEmbedding(nn.Module): + def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None): + super().__init__() + + self.dim = dim + self.max_position_embeddings = max_position_embeddings + self.base = base + inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim)) + self.register_buffer("inv_freq", inv_freq, persistent=False) + + # Build here to make `torch.jit.trace` work. + self.max_seq_len_cached = max_position_embeddings + + @torch.no_grad() + def forward(self, x, position_ids): + # x: [bs, num_attention_heads, seq_len, head_size] + inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) + position_ids_expanded = position_ids[:, None, :].float() + + device_type = x.device.type + device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu" + with torch.autocast(device_type=device_type, enabled=False): + freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) + emb = torch.cat((freqs, freqs), dim=-1) + cos = emb.cos() + sin = emb.sin() + return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype) + + +# Copied from transformers.models.llama.modeling_llama.rotate_half +def rotate_half(x): + """Rotates half the hidden dims of the input.""" + x1 = x[..., : x.shape[-1] // 2] + x2 = x[..., x.shape[-1] // 2 :] + return torch.cat((-x2, x1), dim=-1) + + +# copied from transformers.models.mistral.modeling_mistral.apply_rotary_pos_emb +# TODO @longjie no longer copied from Mistral after static cache +def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1): + """Applies Rotary Position Embedding to the query and key tensors. + + Args: + q (`torch.Tensor`): The query tensor. + k (`torch.Tensor`): The key tensor. + cos (`torch.Tensor`): The cosine part of the rotary embedding. + sin (`torch.Tensor`): The sine part of the rotary embedding. + position_ids (`torch.Tensor`): + The position indices of the tokens corresponding to the query and key tensors. For example, this can be + used to pass offsetted position ids when working with a KV-cache. + unsqueeze_dim (`int`, *optional*, defaults to 1): + The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and + sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note + that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and + k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes + cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have + the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. + Returns: + `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. + """ + cos = cos.unsqueeze(unsqueeze_dim) + sin = sin.unsqueeze(unsqueeze_dim) + + q_embed = (q * cos) + (rotate_half(q) * sin) + k_embed = (k * cos) + (rotate_half(k) * sin) + return q_embed, k_embed + + +# Copied from transformers.models.llama.modeling_llama.repeat_kv +def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: + """ + This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, + num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) + """ + batch, num_key_value_heads, slen, head_dim = hidden_states.shape + if n_rep == 1: + return hidden_states + hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) + return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) + + +# copied from transformers.models.mistral.modeling_mistral.MistralAttention with Mistral->Mixtral +# TODO @longjie no longer copied from Mistral after static cache +class MixtralAttention(nn.Module): + """ + Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer + and "Generating Long Sequences with Sparse Transformers". + """ + + def __init__(self, config: MixtralConfig, layer_idx: Optional[int] = None): + super().__init__() + self.config = config + self.layer_idx = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) + + self.hidden_size = config.hidden_size + self.num_heads = config.num_attention_heads + self.head_dim = self.hidden_size // self.num_heads + self.num_key_value_heads = config.num_key_value_heads + self.num_key_value_groups = self.num_heads // self.num_key_value_heads + self.max_position_embeddings = config.max_position_embeddings + self.rope_theta = config.rope_theta + self.is_causal = True + self.attention_dropout = config.attention_dropout + + if (self.head_dim * self.num_heads) != self.hidden_size: + raise ValueError( + f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}" + f" and `num_heads`: {self.num_heads})." + ) + self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False) + self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False) + self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False) + self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False) + + self.rotary_emb = MixtralRotaryEmbedding( + self.head_dim, + max_position_embeddings=self.max_position_embeddings, + base=self.rope_theta, + ) + + def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int): + return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous() + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + key_states = self.k_proj(hidden_states) + value_states = self.v_proj(hidden_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) + + kv_seq_len = key_states.shape[-2] + if past_key_value is not None: + if self.layer_idx is None: + raise ValueError( + f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} " + "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class " + "with a layer index." + ) + kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx) + cos, sin = self.rotary_emb(value_states, position_ids) + query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) + + if past_key_value is not None: + cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + + # repeat k/v heads if n_kv_heads < n_heads + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + + attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim) + + if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len): + raise ValueError( + f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is" + f" {attn_weights.size()}" + ) + + if attention_mask is not None: # no matter the length, we just slice it + causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] + attn_weights = attn_weights + causal_mask + + # upcast attention to fp32 + attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype) + attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training) + attn_output = torch.matmul(attn_weights, value_states) + + if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): + raise ValueError( + f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" + f" {attn_output.size()}" + ) + + attn_output = attn_output.transpose(1, 2).contiguous() + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) + + attn_output = self.o_proj(attn_output) + + if not output_attentions: + attn_weights = None + + return attn_output, attn_weights, past_key_value + + +# copied from transformers.models.mistral.modeling_mistral.MistralFlashAttention2 with Mistral->Mixtral +# TODO @longjie no longer copied from Mistral after static cache +class MixtralFlashAttention2(MixtralAttention): + """ + Mixtral flash attention module. This module inherits from `MixtralAttention` as the weights of the module stays + untouched. The only required change would be on the forward pass where it needs to correctly call the public API of + flash attention and deal with padding tokens in case the input contains any of them. + """ + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + ): + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + key_states = self.k_proj(hidden_states) + value_states = self.v_proj(hidden_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) + + kv_seq_len = key_states.shape[-2] + if past_key_value is not None: + if self.layer_idx is None: + raise ValueError( + f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} " + "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class " + "with a layer index." + ) + kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx) + + cos, sin = self.rotary_emb(value_states, position_ids) + + query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) + + use_sliding_windows = ( + _flash_supports_window_size + and getattr(self.config, "sliding_window", None) is not None + and kv_seq_len > self.config.sliding_window + and self.config.use_sliding_window + ) + + if not _flash_supports_window_size: + logger.warning_once( + "The current flash attention version does not support sliding window attention, for a more memory efficient implementation" + " make sure to upgrade flash-attn library." + ) + + if past_key_value is not None: + # Activate slicing cache only if the config has a value `sliding_windows` attribute + cache_has_contents = past_key_value.get_seq_length(self.layer_idx) > 0 + if ( + getattr(self.config, "sliding_window", None) is not None + and kv_seq_len > self.config.sliding_window + and cache_has_contents + ): + slicing_tokens = 1 - self.config.sliding_window + + past_key = past_key_value[self.layer_idx][0] + past_value = past_key_value[self.layer_idx][1] + + past_key = past_key[:, :, slicing_tokens:, :].contiguous() + past_value = past_value[:, :, slicing_tokens:, :].contiguous() + + if past_key.shape[-2] != self.config.sliding_window - 1: + raise ValueError( + f"past key must have a shape of (`batch_size, num_heads, self.config.sliding_window-1, head_dim`), got" + f" {past_key.shape}" + ) + + if attention_mask is not None: + attention_mask = attention_mask[:, slicing_tokens:] + attention_mask = torch.cat([attention_mask, torch.ones_like(attention_mask[:, -1:])], dim=-1) + + cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + + # we slice the states for static kv cache to be supported in FA2. Not sure it's a must as compile fails + # for bsz == 1, avoid using slice to capture cuda graph + if cache_position is not None and q_len > 1: + key_states = key_states[:, :, : cache_position[-1] + 1, :] + value_states = value_states[:, :, : cache_position[-1] + 1, :] + + # repeat k/v heads if n_kv_heads < n_heads + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + dropout_rate = 0.0 if not self.training else self.attention_dropout + + # In PEFT, usually we cast the layer norms in float32 for training stability reasons + # therefore the input hidden states gets silently casted in float32. Hence, we need + # cast them back in float16 just to be sure everything works as expected. + input_dtype = query_states.dtype + if input_dtype == torch.float32: + if torch.is_autocast_enabled(): + target_dtype = torch.get_autocast_gpu_dtype() + # Handle the case where the model is quantized + elif hasattr(self.config, "_pre_quantization_dtype"): + target_dtype = self.config._pre_quantization_dtype + else: + target_dtype = self.q_proj.weight.dtype + + logger.warning_once( + f"The input hidden states seems to be silently casted in float32, this might be related to" + f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in" + f" {target_dtype}." + ) + + query_states = query_states.to(target_dtype) + key_states = key_states.to(target_dtype) + value_states = value_states.to(target_dtype) + + # Reashape to the expected shape for Flash Attention + query_states = query_states.transpose(1, 2) + key_states = key_states.transpose(1, 2) + value_states = value_states.transpose(1, 2) + + attn_output = self._flash_attention_forward( + query_states, + key_states, + value_states, + attention_mask, + q_len, + position_ids=position_ids, + dropout=dropout_rate, + sliding_window=getattr(self.config, "sliding_window", None), + is_causal=self.is_causal, + ) + + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous() + attn_output = self.o_proj(attn_output) + + if not output_attentions: + attn_weights = None + + return attn_output, attn_weights, past_key_value + + + def _flash_attention_forward( + self, + query_states, + key_states, + value_states, + attention_mask, + q_len, + position_ids, + dropout, + sliding_window, + is_causal, + softmax_scale=None, + ): + """ + Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token + first unpad the input, then computes the attention scores and pad the final attention scores. + + Args: + query_states (`torch.Tensor`): + Input query states to be passed to Flash Attention API + key_states (`torch.Tensor`): + Input key states to be passed to Flash Attention API + value_states (`torch.Tensor`): + Input value states to be passed to Flash Attention API + attention_mask (`torch.Tensor`): + The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the + position of padding tokens and 1 for the position of non-padding tokens. + dropout (`float`): + Attention dropout + + """ + + # Decide whether to use SWA or not by layer index. + # if use_sliding_windows and self.layer_idx >= self.config.max_window_layers: + # use_sliding_windows = False + use_sliding_windows = False + + # Contains at least one padding token in the sequence + if attention_mask is not None: + batch_size = query_states.shape[0] + query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input( + query_states, key_states, value_states, attention_mask, q_len + ) + + cu_seqlens_q, cu_seqlens_k = cu_seq_lens + max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens + + if not use_sliding_windows: + attn_output_unpad = flash_attn_varlen_func( + query_states, + key_states, + value_states, + cu_seqlens_q=cu_seqlens_q, + cu_seqlens_k=cu_seqlens_k, + max_seqlen_q=max_seqlen_in_batch_q, + max_seqlen_k=max_seqlen_in_batch_k, + dropout_p=dropout, + softmax_scale=softmax_scale, + causal=is_causal, + ) + else: + attn_output_unpad = flash_attn_varlen_func( + query_states, + key_states, + value_states, + cu_seqlens_q=cu_seqlens_q, + cu_seqlens_k=cu_seqlens_k, + max_seqlen_q=max_seqlen_in_batch_q, + max_seqlen_k=max_seqlen_in_batch_k, + dropout_p=dropout, + softmax_scale=softmax_scale, + causal=is_causal, + window_size=(self.config.sliding_window, self.config.sliding_window), + ) + + attn_output = pad_input(attn_output_unpad, indices_q, batch_size, q_len) + else: + if not use_sliding_windows: + if q_len == 1: + position_ids = position_ids.to(dtype=torch.int32).squeeze(1) + attn_output = flash_attn_with_kvcache( + query_states, + key_states, + value_states, + cache_seqlens=position_ids, + softmax_scale=softmax_scale, + causal=is_causal, + ) + else: + attn_output = flash_attn_func( + query_states, + key_states, + value_states, + dropout, + softmax_scale=softmax_scale, + causal=is_causal, + ) + else: + attn_output = flash_attn_func( + query_states, + key_states, + value_states, + dropout, + softmax_scale=softmax_scale, + causal=is_causal, + window_size=(self.config.sliding_window, self.config.sliding_window), + ) + + return attn_output + + # Copied from transformers.models.mistral.modeling_mistral.MistralFlashAttention2._upad_input + def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length): + batch_size, kv_seq_len, num_heads, head_dim = key_layer.shape + + # On the first iteration we need to properly re-create the padding mask + # by slicing it on the proper place + if kv_seq_len != attention_mask.shape[-1]: + attention_mask_num_tokens = attention_mask.shape[-1] + attention_mask = attention_mask[:, attention_mask_num_tokens - kv_seq_len :] + + indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask) + + key_layer = index_first_axis(key_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k) + value_layer = index_first_axis(value_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k) + + if query_length == kv_seq_len: + query_layer = index_first_axis( + query_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k + ) + cu_seqlens_q = cu_seqlens_k + max_seqlen_in_batch_q = max_seqlen_in_batch_k + indices_q = indices_k + elif query_length == 1: + max_seqlen_in_batch_q = 1 + cu_seqlens_q = torch.arange( + batch_size + 1, dtype=torch.int32, device=query_layer.device + ) # There is a memcpy here, that is very bad. + indices_q = cu_seqlens_q[:-1] + query_layer = query_layer.squeeze(1) + else: + # The -q_len: slice assumes left padding. + attention_mask = attention_mask[:, -query_length:] + query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask) + + return ( + query_layer, + key_layer, + value_layer, + indices_q, + (cu_seqlens_q, cu_seqlens_k), + (max_seqlen_in_batch_q, max_seqlen_in_batch_k), + ) + + + +# copied from transformers.models.mistral.modeling_mistral.MistralSdpaAttention with Mistral->Mixtral +# TODO @longjie no longer copied from Mistral after static cache +class MixtralSdpaAttention(MixtralAttention): + """ + Mixtral attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from + `MixtralAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to + SDPA API. + """ + + # Adapted from MixtralAttention.forward + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + if output_attentions: + # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. + logger.warning_once( + "MixtralModel is using MixtralSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, " + 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.' + ) + return super().forward( + hidden_states=hidden_states, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_value=past_key_value, + output_attentions=output_attentions, + use_cache=use_cache, + ) + + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + key_states = self.k_proj(hidden_states) + value_states = self.v_proj(hidden_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) + + kv_seq_len = key_states.shape[-2] + if past_key_value is not None: + kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx) + cos, sin = self.rotary_emb(value_states, position_ids) + + query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) + + if past_key_value is not None: + cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + + causal_mask = attention_mask + if attention_mask is not None: # no matter the length, we just slice it + causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] + + # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask, + # Reference: https://github.com/pytorch/pytorch/issues/112577. + if query_states.device.type == "cuda" and attention_mask is not None: + query_states = query_states.contiguous() + key_states = key_states.contiguous() + value_states = value_states.contiguous() + + # We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of an inline conditional assignment + # in SDPA to support both torch.compile's dynamic shapes and full graph options. An inline conditional prevents dynamic shapes from compiling. + # The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1. + is_causal = True if causal_mask is None and q_len > 1 else False + + attn_output = torch.nn.functional.scaled_dot_product_attention( + query_states, + key_states, + value_states, + attn_mask=causal_mask, + dropout_p=self.attention_dropout if self.training else 0.0, + is_causal=is_causal, + ) + + attn_output = attn_output.transpose(1, 2).contiguous() + attn_output = attn_output.view(bsz, q_len, self.hidden_size) + + attn_output = self.o_proj(attn_output) + + return attn_output, None, past_key_value + + +MIXTRAL_ATTENTION_CLASSES = { + "eager": MixtralAttention, + "flash_attention_2": MixtralFlashAttention2, + "sdpa": MixtralSdpaAttention, +} + + +class MixtralBlockSparseTop2MLP(nn.Module): + def __init__(self, config: MixtralConfig): + super().__init__() + self.ffn_dim = config.intermediate_size + self.hidden_dim = config.hidden_size + + self.w1 = nn.Linear(self.hidden_dim, self.ffn_dim, bias=False) # gate + self.w2 = nn.Linear(self.ffn_dim, self.hidden_dim, bias=False) # down + self.w3 = nn.Linear(self.hidden_dim, self.ffn_dim, bias=False) # up + + self.act_fn = ACT2FN[config.hidden_act] + + def forward(self, hidden_states): + current_hidden_states = self.act_fn(self.w1(hidden_states)) * self.w3(hidden_states) + current_hidden_states = self.w2(current_hidden_states) + return current_hidden_states + + +class MixtralSparseMoeBlock(nn.Module): + """ + This implementation is + strictly equivalent to standard MoE with full capacity (no + dropped tokens). It's faster since it formulates MoE operations + in terms of block-sparse operations to accomodate imbalanced + assignments of tokens to experts, whereas standard MoE either + (1) drop tokens at the cost of reduced performance or (2) set + capacity factor to number of experts and thus waste computation + and memory on padding. + """ + + def __init__(self, config): + super().__init__() + self.hidden_dim = config.hidden_size + self.ffn_dim = config.intermediate_size + self.num_experts = config.num_local_experts + self.top_k = config.num_experts_per_tok + + # gating + self.gate = nn.Linear(self.hidden_dim, self.num_experts, bias=False) + + self.experts = nn.ModuleList([MixtralBlockSparseTop2MLP(config) for _ in range(self.num_experts)]) + + # Jitter parameters + self.jitter_noise = config.router_jitter_noise + + def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: + """ """ + batch_size, sequence_length, hidden_dim = hidden_states.shape + if self.training and self.jitter_noise > 0: + hidden_states *= torch.empty_like(hidden_states).uniform_(1.0 - self.jitter_noise, 1.0 + self.jitter_noise) + hidden_states = hidden_states.view(-1, hidden_dim) + # router_logits: (batch * sequence_length, n_experts) + router_logits = self.gate(hidden_states) + + routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float) + routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1) + routing_weights /= routing_weights.sum(dim=-1, keepdim=True) + # we cast back to the input dtype + routing_weights = routing_weights.to(hidden_states.dtype) + + final_hidden_states = torch.zeros( + (batch_size * sequence_length, hidden_dim), dtype=hidden_states.dtype, device=hidden_states.device + ) + + # One hot encode the selected experts to create an expert mask + # this will be used to easily index which expert is going to be sollicitated + expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0) + + # Loop over all available experts in the model and perform the computation on each expert + for expert_idx in range(self.num_experts): + expert_layer = self.experts[expert_idx] + idx, top_x = torch.where(expert_mask[expert_idx]) + + # Index the correct hidden states and compute the expert hidden state for + # the current expert. We need to make sure to multiply the output hidden + # states by `routing_weights` on the corresponding tokens (top-1 and top-2) + current_state = hidden_states[None, top_x].reshape(-1, hidden_dim) + current_hidden_states = expert_layer(current_state) * routing_weights[top_x, idx, None] + + # However `index_add_` only support torch tensors for indexing so we'll use + # the `top_x` tensor here. + final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype)) + final_hidden_states = final_hidden_states.reshape(batch_size, sequence_length, hidden_dim) + return final_hidden_states, router_logits + + +class MixtralDecoderLayer(nn.Module): + def __init__(self, config: MixtralConfig, layer_idx: int): + super().__init__() + self.hidden_size = config.hidden_size + + self.self_attn = MIXTRAL_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx) + + self.block_sparse_moe = MixtralSparseMoeBlock(config) + self.input_layernorm = MixtralRMSNorm(config.hidden_size, eps=config.rms_norm_eps) + self.post_attention_layernorm = MixtralRMSNorm(config.hidden_size, eps=config.rms_norm_eps) + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Tuple[torch.Tensor]] = None, + output_attentions: Optional[bool] = False, + output_router_logits: Optional[bool] = False, + use_cache: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, + **kwargs, + ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + """ + Args: + hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` + attention_mask (`torch.FloatTensor`, *optional*): attention mask of size + `(batch, sequence_length)` where padding elements are indicated by 0. + past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under + returned tensors for more detail. + output_router_logits (`bool`, *optional*): + Whether or not to return the logits of all the routers. They are useful for computing the router loss, and + should not be returned during inference. + use_cache (`bool`, *optional*): + If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding + (see `past_key_values`). + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. + kwargs (`dict`, *optional*): + Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code + into the model + """ + + residual = hidden_states + + hidden_states = self.input_layernorm(hidden_states) + + # Self Attention + hidden_states, self_attn_weights, present_key_value = self.self_attn( + hidden_states=hidden_states, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_value=past_key_value, + output_attentions=output_attentions, + use_cache=use_cache, + cache_position=cache_position, + ) + hidden_states = residual + hidden_states + + # Fully Connected + residual = hidden_states + hidden_states = self.post_attention_layernorm(hidden_states) + hidden_states, router_logits = self.block_sparse_moe(hidden_states) + hidden_states = residual + hidden_states + + outputs = (hidden_states,) + + if output_attentions: + outputs += (self_attn_weights,) + + if use_cache: + outputs += (present_key_value,) + + if output_router_logits: + outputs += (router_logits,) + + return outputs + + +MIXTRAL_START_DOCSTRING = r""" + This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the + library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads + etc.) + + This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. + Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage + and behavior. + + Parameters: + config ([`MixtralConfig`]): + Model configuration class with all the parameters of the model. Initializing with a config file does not + load the weights associated with the model, only the configuration. Check out the + [`~PreTrainedModel.from_pretrained`] method to load the model weights. +""" + + +@add_start_docstrings( + "The bare Mixtral Model outputting raw hidden-states without any specific head on top.", + MIXTRAL_START_DOCSTRING, +) +# Copied from transformers.models.qwen2.modeling_qwen2.Qwen2PreTrainedModel with Qwen2->Mixtral +class MixtralPreTrainedModel(PreTrainedModel): + config_class = MixtralConfig + base_model_prefix = "model" + supports_gradient_checkpointing = True + _no_split_modules = ["MixtralDecoderLayer"] + _skip_keys_device_placement = "past_key_values" + _supports_flash_attn_2 = True + _supports_sdpa = True + _supports_cache_class = True + + def _init_weights(self, module): + std = self.config.initializer_range + if isinstance(module, nn.Linear): + module.weight.data.normal_(mean=0.0, std=std) + if module.bias is not None: + module.bias.data.zero_() + elif isinstance(module, nn.Embedding): + module.weight.data.normal_(mean=0.0, std=std) + if module.padding_idx is not None: + module.weight.data[module.padding_idx].zero_() + + +MIXTRAL_INPUTS_DOCSTRING = r""" + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide + it. + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + [What are input IDs?](../glossary#input-ids) + attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): + Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: + + - 1 for tokens that are **not masked**, + - 0 for tokens that are **masked**. + + [What are attention masks?](../glossary#attention-mask) + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see + `past_key_values`). + + If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] + and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more + information on the default strategy. + + - 1 indicates the head is **not masked**, + - 0 indicates the head is **masked**. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, + config.n_positions - 1]`. + + [What are position IDs?](../glossary#position-ids) + past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape + `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape + `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. + + Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention + blocks) that can be used (see `past_key_values` input) to speed up sequential decoding. + + If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that + don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all + `decoder_input_ids` of shape `(batch_size, sequence_length)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This + is useful if you want more control over how to convert `input_ids` indices into associated vectors than the + model's internal embedding lookup matrix. + use_cache (`bool`, *optional*): + If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see + `past_key_values`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + more detail. + output_router_logits (`bool`, *optional*): + Whether or not to return the logits of all the routers. They are useful for computing the router loss, and + should not be returned during inference. + return_dict (`bool`, *optional*): + Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, + this tensor is not affected by padding. It is used to update the cache in the correct position and to infer + the complete sequence length. +""" + + +@add_start_docstrings( + "The bare Mixtral Model outputting raw hidden-states without any specific head on top.", + MIXTRAL_START_DOCSTRING, +) +# copied from transformers.models.mistral.modeling_mistral.MistralModel with MISTRAL->MIXTRAL,Mistral->Mixtral +# TODO @longjie no longer copied from Mistral after static cache +class MixtralModel(MixtralPreTrainedModel): + """ + Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MixtralDecoderLayer`] + + Args: + config: MixtralConfig + """ + + def __init__(self, config: MixtralConfig): + super().__init__(config) + self.padding_idx = config.pad_token_id + self.vocab_size = config.vocab_size + + self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx) + self.layers = nn.ModuleList( + [MixtralDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)] + ) + self._attn_implementation = config._attn_implementation + self.norm = MixtralRMSNorm(config.hidden_size, eps=config.rms_norm_eps) + + self.gradient_checkpointing = False + # Initialize weights and apply final processing + self.post_init() + + def get_input_embeddings(self): + return self.embed_tokens + + def set_input_embeddings(self, value): + self.embed_tokens = value + + # Ignore copy + @add_start_docstrings_to_model_forward(MIXTRAL_INPUTS_DOCSTRING) + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_values: Optional[List[torch.FloatTensor]] = None, + inputs_embeds: Optional[torch.FloatTensor] = None, + use_cache: Optional[bool] = None, + output_attentions: Optional[bool] = None, + output_hidden_states: Optional[bool] = None, + output_router_logits: Optional[bool] = None, + return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, MoeModelOutputWithPast]: + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_router_logits = ( + output_router_logits if output_router_logits is not None else self.config.output_router_logits + ) + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + use_cache = use_cache if use_cache is not None else self.config.use_cache + + return_dict = return_dict if return_dict is not None else self.config.use_return_dict + + if (input_ids is None) ^ (inputs_embeds is not None): + raise ValueError( + "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" + ) + + if self.gradient_checkpointing and self.training: + if use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False + + use_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache) and not self.training: + use_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) + + if inputs_embeds is None: + inputs_embeds = self.embed_tokens(input_ids) + + if cache_position is None: + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + cache_position = torch.arange( + past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device + ) + if position_ids is None: + position_ids = cache_position.unsqueeze(0) + + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) + + hidden_states = inputs_embeds + + # decoder layers + all_hidden_states = () if output_hidden_states else None + all_self_attns = () if output_attentions else None + all_router_logits = () if output_router_logits else None + next_decoder_cache = None + + for decoder_layer in self.layers: + if output_hidden_states: + all_hidden_states += (hidden_states,) + + if self.gradient_checkpointing and self.training: + layer_outputs = self._gradient_checkpointing_func( + decoder_layer.__call__, + hidden_states, + causal_mask, + position_ids, + past_key_values, + output_attentions, + output_router_logits, + use_cache, + cache_position, + ) + else: + layer_outputs = decoder_layer( + hidden_states, + attention_mask=causal_mask, + position_ids=position_ids, + past_key_value=past_key_values, + output_attentions=output_attentions, + output_router_logits=output_router_logits, + use_cache=use_cache, + cache_position=cache_position, + ) + + hidden_states = layer_outputs[0] + + if use_cache: + next_decoder_cache = layer_outputs[2 if output_attentions else 1] + + if output_attentions: + all_self_attns += (layer_outputs[1],) + + if output_router_logits: + all_router_logits += (layer_outputs[-1],) + + hidden_states = self.norm(hidden_states) + + # add hidden states from the last decoder layer + if output_hidden_states: + all_hidden_states += (hidden_states,) + + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + + if not return_dict: + return tuple( + v + for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_router_logits] + if v is not None + ) + return MoeModelOutputWithPast( + last_hidden_state=hidden_states, + past_key_values=next_cache, + hidden_states=all_hidden_states, + attentions=all_self_attns, + router_logits=all_router_logits, + ) + + # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static + # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. + # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using + # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114 + + if self.config._attn_implementation == "flash_attention_2": + if attention_mask is not None and 0.0 in attention_mask: + return attention_mask + return None + + # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in + # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail + # to infer the attention mask. + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + using_static_cache = isinstance(past_key_values, StaticCache) + + # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward + if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if AttentionMaskConverter._ignore_causal_mask_sdpa( + attention_mask, + inputs_embeds=input_tensor, + past_key_values_length=past_seen_tokens, + is_training=self.training, + ): + return None + + dtype, device = input_tensor.dtype, input_tensor.device + min_dtype = torch.finfo(dtype).min + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_length() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + + if attention_mask is not None and attention_mask.dim() == 4: + # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing + if attention_mask.max() != 0: + raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") + causal_mask = attention_mask + else: + causal_mask = torch.full( + (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device + ) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + if ( + self.config._attn_implementation == "sdpa" + and attention_mask is not None + and attention_mask.device.type == "cuda" + and not output_attentions + ): + # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when + # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. + # Details: https://github.com/pytorch/pytorch/issues/110213 + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + + +class MixtralForCausalLM(MixtralPreTrainedModel): + _tied_weights_keys = ["lm_head.weight"] + + def __init__(self, config): + super().__init__(config) + self.model = MixtralModel(config) + self.vocab_size = config.vocab_size + self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False) + self.router_aux_loss_coef = config.router_aux_loss_coef + self.num_experts = config.num_local_experts + self.num_experts_per_tok = config.num_experts_per_tok + # Initialize weights and apply final processing + self.post_init() + + def get_input_embeddings(self): + return self.model.embed_tokens + + def set_input_embeddings(self, value): + self.model.embed_tokens = value + + def get_output_embeddings(self): + return self.lm_head + + def set_output_embeddings(self, new_embeddings): + self.lm_head = new_embeddings + + def set_decoder(self, decoder): + self.model = decoder + + def get_decoder(self): + return self.model + + @add_start_docstrings_to_model_forward(MIXTRAL_INPUTS_DOCSTRING) + @replace_return_docstrings(output_type=MoeCausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC) + # Ignore copy + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_values: Optional[List[torch.FloatTensor]] = None, + inputs_embeds: Optional[torch.FloatTensor] = None, + labels: Optional[torch.LongTensor] = None, + use_cache: Optional[bool] = None, + output_attentions: Optional[bool] = None, + output_hidden_states: Optional[bool] = None, + output_router_logits: Optional[bool] = None, + return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, MoeCausalLMOutputWithPast]: + r""" + Args: + labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., + config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored + (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. + + Returns: + + Example: + + ```python + >>> from transformers import AutoTokenizer, MixtralForCausalLM + + >>> model = MixtralForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-v0.1") + >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-v0.1") + + >>> prompt = "Hey, are you conscious? Can you talk to me?" + >>> inputs = tokenizer(prompt, return_tensors="pt") + + >>> # Generate + >>> generate_ids = model.generate(inputs.input_ids, max_length=30) + >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] + "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you." + ```""" + + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_router_logits = ( + output_router_logits if output_router_logits is not None else self.config.output_router_logits + ) + + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + return_dict = return_dict if return_dict is not None else self.config.use_return_dict + + # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn) + outputs = self.model( + input_ids=input_ids, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_values=past_key_values, + inputs_embeds=inputs_embeds, + use_cache=use_cache, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + output_router_logits=output_router_logits, + return_dict=return_dict, + cache_position=cache_position, + ) + + hidden_states = outputs[0] + logits = self.lm_head(hidden_states) + logits = logits.float() + + loss = None + if labels is not None: + # Shift so that tokens < n predict n + shift_logits = logits[..., :-1, :].contiguous() + shift_labels = labels[..., 1:].contiguous() + # Flatten the tokens + loss_fct = CrossEntropyLoss() + shift_logits = shift_logits.view(-1, self.config.vocab_size) + shift_labels = shift_labels.view(-1) + # Enable model parallelism + shift_labels = shift_labels.to(shift_logits.device) + loss = loss_fct(shift_logits, shift_labels) + + aux_loss = None + if output_router_logits: + aux_loss = load_balancing_loss_func( + outputs.router_logits if return_dict else outputs[-1], + self.num_experts, + self.num_experts_per_tok, + attention_mask, + ) + if labels is not None: + loss += self.router_aux_loss_coef * aux_loss.to(loss.device) # make sure to reside in the same device + + if not return_dict: + output = (logits,) + outputs[1:] + if output_router_logits: + output = (aux_loss,) + output + return (loss,) + output if loss is not None else output + + return MoeCausalLMOutputWithPast( + loss=loss, + aux_loss=aux_loss, + logits=logits, + past_key_values=outputs.past_key_values, + hidden_states=outputs.hidden_states, + attentions=outputs.attentions, + router_logits=outputs.router_logits, + ) + + def prepare_inputs_for_generation( + self, + input_ids, + past_key_values=None, + attention_mask=None, + inputs_embeds=None, + cache_position=None, + output_router_logits=False, + position_ids=None, + use_cache=True, + **kwargs, + ): + # If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens + # Exception 1: when passing input_embeds, input_ids may be missing entries + # Exception 2: some generation methods do special slicing of input_ids, so we don't need to do it here + if past_key_values is not None: + if inputs_embeds is not None: # Exception 1 + input_ids = input_ids[:, -cache_position.shape[0] :] + elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2) + input_ids = input_ids[:, cache_position] + + if attention_mask is not None and position_ids is None: + # create position_ids on the fly for batch generation + position_ids = attention_mask.long().cumsum(-1) - 1 + position_ids.masked_fill_(attention_mask == 0, 1) + if past_key_values: + position_ids = position_ids[:, -input_ids.shape[1] :] + + # if `inputs_embeds` are passed, we only want to use them in the 1st generation step + if inputs_embeds is not None and cache_position[0] == 0: + model_inputs = {"inputs_embeds": inputs_embeds} + else: + model_inputs = {"input_ids": input_ids.contiguous()} # `contiguous()` needed for compilation use cases + + model_inputs.update( + { + "position_ids": position_ids, + "cache_position": cache_position, + "past_key_values": past_key_values, + "use_cache": use_cache, + "attention_mask": attention_mask, + "output_router_logits": output_router_logits, + } + ) + return model_inputs + + +@add_start_docstrings( + """ + The Mixtral Model transformer with a sequence classification head on top (linear layer). + + [`MixtralForSequenceClassification`] uses the last token in order to do the classification, as other causal models + (e.g. GPT-2) do. + + Since it does classification on the last token, it requires to know the position of the last token. If a + `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If + no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the + padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in + each row of the batch). + """, + MIXTRAL_START_DOCSTRING, +) +# Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with Llama->Mixtral, LLAMA->MIXTRAL +class MixtralForSequenceClassification(MixtralPreTrainedModel): + def __init__(self, config): + super().__init__(config) + self.num_labels = config.num_labels + self.model = MixtralModel(config) + self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False) + + # Initialize weights and apply final processing + self.post_init() + + def get_input_embeddings(self): + return self.model.embed_tokens + + def set_input_embeddings(self, value): + self.model.embed_tokens = value + + @add_start_docstrings_to_model_forward(MIXTRAL_INPUTS_DOCSTRING) + def forward( + self, + input_ids: Optional[torch.LongTensor] = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + inputs_embeds: Optional[torch.FloatTensor] = None, + labels: Optional[torch.LongTensor] = None, + use_cache: Optional[bool] = None, + output_attentions: Optional[bool] = None, + output_hidden_states: Optional[bool] = None, + return_dict: Optional[bool] = None, + ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + r""" + labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., + config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If + `config.num_labels > 1` a classification loss is computed (Cross-Entropy). + """ + return_dict = return_dict if return_dict is not None else self.config.use_return_dict + + transformer_outputs = self.model( + input_ids, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_values=past_key_values, + inputs_embeds=inputs_embeds, + use_cache=use_cache, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + ) + hidden_states = transformer_outputs[0] + logits = self.score(hidden_states) + + if input_ids is not None: + batch_size = input_ids.shape[0] + else: + batch_size = inputs_embeds.shape[0] + + if self.config.pad_token_id is None and batch_size != 1: + raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.") + if self.config.pad_token_id is None: + sequence_lengths = -1 + else: + if input_ids is not None: + # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility + sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1 + sequence_lengths = sequence_lengths % input_ids.shape[-1] + sequence_lengths = sequence_lengths.to(logits.device) + else: + sequence_lengths = -1 + + pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths] + + loss = None + if labels is not None: + labels = labels.to(logits.device) + if self.config.problem_type is None: + if self.num_labels == 1: + self.config.problem_type = "regression" + elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): + self.config.problem_type = "single_label_classification" + else: + self.config.problem_type = "multi_label_classification" + + if self.config.problem_type == "regression": + loss_fct = MSELoss() + if self.num_labels == 1: + loss = loss_fct(pooled_logits.squeeze(), labels.squeeze()) + else: + loss = loss_fct(pooled_logits, labels) + elif self.config.problem_type == "single_label_classification": + loss_fct = CrossEntropyLoss() + loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1)) + elif self.config.problem_type == "multi_label_classification": + loss_fct = BCEWithLogitsLoss() + loss = loss_fct(pooled_logits, labels) + if not return_dict: + output = (pooled_logits,) + transformer_outputs[1:] + return ((loss,) + output) if loss is not None else output + + return SequenceClassifierOutputWithPast( + loss=loss, + logits=pooled_logits, + past_key_values=transformer_outputs.past_key_values, + hidden_states=transformer_outputs.hidden_states, + attentions=transformer_outputs.attentions, + ) + + +@add_start_docstrings( + """ + The Mixtral Model transformer with a token classification head on top (a linear layer on top of the hidden-states + output) e.g. for Named-Entity-Recognition (NER) tasks. + """, + MIXTRAL_START_DOCSTRING, +) +# Copied from transformers.models.llama.modeling_llama.LlamaForTokenClassification with Llama->Mixtral, LLAMA->MIXTRAL +class MixtralForTokenClassification(MixtralPreTrainedModel): + def __init__(self, config): + super().__init__(config) + self.num_labels = config.num_labels + self.model = MixtralModel(config) + if getattr(config, "classifier_dropout", None) is not None: + classifier_dropout = config.classifier_dropout + elif getattr(config, "hidden_dropout", None) is not None: + classifier_dropout = config.hidden_dropout + else: + classifier_dropout = 0.1 + self.dropout = nn.Dropout(classifier_dropout) + self.score = nn.Linear(config.hidden_size, config.num_labels) + + # Initialize weights and apply final processing + self.post_init() + + def get_input_embeddings(self): + return self.model.embed_tokens + + def set_input_embeddings(self, value): + self.model.embed_tokens = value + + @add_start_docstrings_to_model_forward(MIXTRAL_INPUTS_DOCSTRING) + def forward( + self, + input_ids: Optional[torch.LongTensor] = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_values: Optional[List[torch.FloatTensor]] = None, + inputs_embeds: Optional[torch.FloatTensor] = None, + labels: Optional[torch.LongTensor] = None, + use_cache: Optional[bool] = None, + output_attentions: Optional[bool] = None, + output_hidden_states: Optional[bool] = None, + return_dict: Optional[bool] = None, + ) -> Union[Tuple, TokenClassifierOutput]: + r""" + labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., + config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If + `config.num_labels > 1` a classification loss is computed (Cross-Entropy). + """ + return_dict = return_dict if return_dict is not None else self.config.use_return_dict + + outputs = self.model( + input_ids, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_values=past_key_values, + inputs_embeds=inputs_embeds, + use_cache=use_cache, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + ) + sequence_output = outputs[0] + sequence_output = self.dropout(sequence_output) + logits = self.score(sequence_output) + + loss = None + if labels is not None: + loss_fct = CrossEntropyLoss() + loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1)) + + if not return_dict: + output = (logits,) + outputs[2:] + return ((loss,) + output) if loss is not None else output + + return TokenClassifierOutput( + loss=loss, + logits=logits, + hidden_states=outputs.hidden_states, + attentions=outputs.attentions, + ) \ No newline at end of file diff --git a/ktransformers/operators/RoPE.py b/ktransformers/operators/RoPE.py index 5fcce4f..9dc233b 100644 --- a/ktransformers/operators/RoPE.py +++ b/ktransformers/operators/RoPE.py @@ -10,6 +10,7 @@ from ktransformers.util.custom_gguf import GGUFLoader from ktransformers.util.utils import InferenceState from transformers.configuration_utils import PretrainedConfig + # Copied from transformers.models.mixtral.modeling_mixtral.MixtralRotaryEmbedding with Mixtral->Qwen2Moe class RotaryEmbedding(BaseInjectedModule, DeepseekV2RotaryEmbedding): def __init__(self, @@ -17,12 +18,16 @@ def __init__(self, gguf_loader : GGUFLoader, config: PretrainedConfig, orig_module: nn.Module, - device: str = "cuda", + # device: str = "cuda", + generate_device: str = "cuda", + prefill_device: str = "cuda", **kwargs): - BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) + BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, generate_device, **kwargs) self.orig_module.__init__(orig_module.dim, orig_module.max_position_embeddings, orig_module.base) + self.generate_device = generate_device + self.prefill_device = prefill_device def load(self): self.orig_module.__init__(self.orig_module.dim, @@ -36,9 +41,11 @@ def __init__(self, gguf_loader : GGUFLoader, config: PretrainedConfig, orig_module: nn.Module, - device: str = "cuda", + # device: str = "cuda", + generate_device: str = "cuda", + prefill_device: str = "cuda", **kwargs): - BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) + BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, generate_device, **kwargs) self.orig_module.__init__(orig_module.dim, orig_module.max_position_embeddings, orig_module.base, @@ -49,13 +56,15 @@ def __init__(self, orig_module.beta_slow, orig_module.mscale, orig_module.mscale_all_dim) + self.generate_device = generate_device + self.prefill_device = prefill_device def load(self): self.orig_module.__init__(self.orig_module.dim, self.orig_module.max_position_embeddings, self.orig_module.base, - self.device, + self.generate_device, self.orig_module.scaling_factor, self.orig_module.original_max_position_embeddings, self.orig_module.beta_fast, diff --git a/ktransformers/operators/attention.py b/ktransformers/operators/attention.py index 0648f51..3cfb9fd 100644 --- a/ktransformers/operators/attention.py +++ b/ktransformers/operators/attention.py @@ -15,7 +15,7 @@ from transformers.configuration_utils import PretrainedConfig from transformers.cache_utils import Cache -class DeepseekV2AttentionInjected(BaseInjectedModule, DeepseekV2Attention): +class KDeepseekV2Attention(BaseInjectedModule, DeepseekV2Attention): """Multi-headed attention from 'Attention Is All You Need' paper""" def __init__(self, diff --git a/ktransformers/operators/cpuinfer.py b/ktransformers/operators/cpuinfer.py new file mode 100644 index 0000000..027cc8b --- /dev/null +++ b/ktransformers/operators/cpuinfer.py @@ -0,0 +1,18 @@ +import sys, os +from typing import Any +sys.path.append(os.path.join(os.path.dirname(__file__), "..", "ktransformers_ext", "build")) +sys.path.append(os.path.join(os.path.dirname(__file__), "..", "ktransformers_ext", "build", "Release")) +sys.path.append(os.path.join(os.path.dirname(__file__), "..", "ktransformers_ext", "build", "Debug")) +import cpuinfer_ext +from ktransformers.server.config.config import Config +class CPUInfer: + cpu_infer = None + def __init__(self, cpu_infer:int = Config().cpu_infer): + if CPUInfer.cpu_infer is None: + CPUInfer.cpu_infer = cpuinfer_ext.CPUInfer(cpu_infer) + + def __getattribute__(self, __name: str) -> Any: + return CPUInfer.cpu_infer.__getattribute__(__name) + + def __setattr__(self, __name: str, __value: Any) -> None: + return CPUInfer.cpu_infer.__setattr__(__name, __value) \ No newline at end of file diff --git a/ktransformers/operators/experts.py b/ktransformers/operators/experts.py index 35821d7..864c4b7 100644 --- a/ktransformers/operators/experts.py +++ b/ktransformers/operators/experts.py @@ -6,7 +6,7 @@ Date : 2024-07-25 11:25:24 Version : 0.1.0 LastEditors : Azure -LastEditTime : 2024-07-26 09:27:41 +LastEditTime : 2024-08-15 02:36:29 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' @@ -31,12 +31,13 @@ from transformers.activations import ACT2FN from transformers.configuration_utils import PretrainedConfig from abc import ABC, abstractmethod -from ktransformers.operators.linear import QuantizedLinearMarlin, QuantizedLinearTorch, KTransformerLinear +from ktransformers.operators.linear import KLinearMarlin, KLinearTorch, KTransformersLinear import time +from ktransformers.operators.cpuinfer import CPUInfer # class Base(BaseInjectedModule, ABC): -class MLPExpertsBase(ABC): +class KExpertsBase(ABC): def __init__(self, key: str, gguf_loader: GGUFLoader, config: PretrainedConfig, orig_module: nn.Module, device: str = "cuda", **kwargs): # super().__init__(key, gguf_loader, config, orig_module, device, **kwargs) self.key = key @@ -80,6 +81,25 @@ def load_weights(self, override_key: str | None = None, device: str = "cpu"): gate_type = self.gguf_loader.tensor_info[key + ".ffn_gate_exps.weight"]["ggml_type"] up_type = self.gguf_loader.tensor_info[key + ".ffn_up_exps.weight"]["ggml_type"] down_type = self.gguf_loader.tensor_info[key + ".ffn_down_exps.weight"]["ggml_type"] + elif key + ".ffn_down.0.weight" in self.gguf_loader.tensor_info: + # for supporting Mixtral-8x7B-Instuct + gate = [] + up = [] + down = [] + for i in range(8): + gatei, upi, downi = f".ffn_gate.{i}.weight", f".ffn_up.{i}.weight", f".ffn_down.{i}.weight" + targets = [gatei, upi, downi] + tensors = self.load_multi(key, targets, device=device) + gate_it, up_it, down_it = tensors[gatei], tensors[upi], tensors[downi] + gate.append(gate_it) + up.append(up_it) + down.append(down_it) + gate = torch.stack(gate) + up = torch.stack(up) + down = torch.stack(down) + gate_type = self.gguf_loader.tensor_info[key + ".ffn_gate.0.weight"]["ggml_type"] + up_type = self.gguf_loader.tensor_info[key + ".ffn_up.0.weight"]["ggml_type"] + down_type = self.gguf_loader.tensor_info[key + ".ffn_down.0.weight"]["ggml_type"] else: raise ValueError(f"Experts {key} not found in gguf_loader") res = {key:{"gate": gate, "up": up, "down": down, "gate_type": gate_type, "up_type": up_type, "down_type": down_type}} @@ -91,13 +111,14 @@ def load_multi(self, key: str, keys: list[str], device: str = "cpu"): tensors[k] = self.gguf_loader.load_gguf_tensor(key + k, device=device) return tensors -class MLPCPUExperts(MLPExpertsBase): +class KExpertsCPU(KExpertsBase): input_tensor_cpu:Tensor = None expert_ids_cpu:Tensor = None weights_cpu:Tensor = None output_cpu:Tensor = None - output_gpu:Tensor = None - CPU_INFER = cpuinfer_ext.CPUInfer(Config().cpu_infer) + output_gpu_map:dict = {} # Manage output tensor buffer on different gpu + #stream_map:dict = {} # Manage cuda stream on different gpu + CPU_INFER = CPUInfer(Config().cpu_infer) def __init__( self, key: str, @@ -106,17 +127,17 @@ def __init__( n_routed_experts: int, orig_module: nn.Module = None, device: str = "cpu", - out_device: str = "cuda", # this device mean which device the output should on + out_device: str = "cuda", # this device mean which device the output should on. TODO: support cpu. **kwargs ): super().__init__(key, gguf_loader, config, orig_module, device, **kwargs) - assert device.lower() == "cpu", "MLPCPUExperts can only be loaded on CPU" + assert device.lower() == "cpu", "KExpertsCPU can only be loaded on CPU" self.n_routed_experts = n_routed_experts self.out_device = out_device def load(self, w: dict | nn.Parameter | tuple | None = None, device:str|None = None, warmup:bool = False): if device: - assert device.lower() == "cpu", "MLPCPUExperts can only be loaded on CPU, Parameter \"device\" can be cpu or None." + assert device.lower() == "cpu", "KExpertsCPU can only be loaded on CPU, Parameter \"device\" can be cpu or None." if w is None: w = self.load_weights()[self.key] self.gate = w["gate"] self.up = w["up"] @@ -155,50 +176,50 @@ def load(self, w: dict | nn.Parameter | tuple | None = None, device:str|None = N # print(n_routed_experts, hidden_size, moe_intermediate_size) num_experts_per_tok = self.config.num_experts_per_tok self.moe = MOE(moe_config) - self.cpu_infer = MLPCPUExperts.CPU_INFER + self.cpu_infer = KExpertsCPU.CPU_INFER if warmup: - self.cpu_infer.submit(self.moe.warm_up) + self.cpu_infer.submit(self.moe.warm_up()) self.cpu_infer.sync() - if MLPCPUExperts.output_gpu == None: - MLPCPUExperts.input_tensor_cpu = torch.empty((self.config.hidden_size), device="cpu", pin_memory=True) - MLPCPUExperts.expert_ids_cpu = torch.empty((num_experts_per_tok), device="cpu", dtype=torch.long, pin_memory=True) - MLPCPUExperts.weights_cpu = torch.empty((num_experts_per_tok), device="cpu", dtype=torch.float32, pin_memory=True) - MLPCPUExperts.output_cpu = torch.empty((self.config.hidden_size), device="cpu", pin_memory=True) - MLPCPUExperts.output_gpu = torch.empty((self.config.hidden_size), device=self.out_device) - + if self.out_device not in KExpertsCPU.output_gpu_map: + KExpertsCPU.output_gpu_map[self.out_device] = torch.zeros((self.config.hidden_size), device=self.out_device) + if KExpertsCPU.input_tensor_cpu == None: + KExpertsCPU.input_tensor_cpu = torch.zeros((self.config.hidden_size), device="cpu", pin_memory=True) + KExpertsCPU.expert_ids_cpu = torch.zeros((num_experts_per_tok), device="cpu", dtype=torch.long, pin_memory=True) + KExpertsCPU.weights_cpu = torch.zeros((num_experts_per_tok), device="cpu", dtype=torch.float32, pin_memory=True) + KExpertsCPU.output_cpu = torch.zeros((self.config.hidden_size), device="cpu", pin_memory=True, dtype=torch.bfloat16) + def submit_for_one_decode(self, input_tensor, expert_ids, weights): - MLPCPUExperts.input_tensor_cpu.copy_(input_tensor, non_blocking=True) - MLPCPUExperts.expert_ids_cpu.copy_(expert_ids, non_blocking=True) - MLPCPUExperts.weights_cpu.copy_(weights, non_blocking=True) - self.cpu_infer.submit_with_cuda_stream(torch.cuda.current_stream().cuda_stream, self.moe.forward, 1, expert_ids.size(0), MLPCPUExperts.expert_ids_cpu.data_ptr(), MLPCPUExperts.weights_cpu.data_ptr(), MLPCPUExperts.input_tensor_cpu.data_ptr(), MLPCPUExperts.output_cpu.data_ptr()) - + KExpertsCPU.input_tensor_cpu.copy_(input_tensor, non_blocking=True) + KExpertsCPU.expert_ids_cpu.copy_(expert_ids, non_blocking=True) + KExpertsCPU.weights_cpu.copy_(weights, non_blocking=True) + self.cpu_infer.submit_with_cuda_stream(torch.cuda.current_stream(self.out_device).cuda_stream, self.moe.forward(1, expert_ids.size(0), KExpertsCPU.expert_ids_cpu.data_ptr(), KExpertsCPU.weights_cpu.data_ptr(), KExpertsCPU.input_tensor_cpu.data_ptr(), KExpertsCPU.output_cpu.data_ptr())) + def sync_for_one_decode(self): - self.cpu_infer.sync_with_cuda_stream(torch.cuda.current_stream().cuda_stream) - MLPCPUExperts.output_gpu.copy_(MLPCPUExperts.output_cpu, non_blocking=True) - #print("capturing experts finish") - return MLPCPUExperts.output_gpu + self.cpu_infer.sync_with_cuda_stream(torch.cuda.current_stream(self.out_device).cuda_stream) + KExpertsCPU.output_gpu_map[self.out_device].copy_(KExpertsCPU.output_cpu, non_blocking=True) + return KExpertsCPU.output_gpu_map[self.out_device] def forward(self, input_tensor, expert_ids, weights): # generate, capture and run cuda graph + # print(expert_ids) if input_tensor.size(0)==1: # TODO: this branch is unreachable, but the shape of input_tensor([1,hidden_size]) and input_tensor_cpu([hidden_size]) is not compatible #print("capturing experts") - MLPCPUExperts.input_tensor_cpu.copy_(input_tensor, non_blocking=True) - MLPCPUExperts.expert_ids_cpu.copy_(expert_ids, non_blocking=True) - MLPCPUExperts.weights_cpu.copy_(weights, non_blocking=True) - self.cpu_infer.submit_with_cuda_stream(torch.cuda.current_stream().cuda_stream, self.moe.forward, 1, expert_ids.size(1), MLPCPUExperts.expert_ids_cpu.data_ptr(), MLPCPUExperts.weights_cpu.data_ptr(), MLPCPUExperts.input_tensor_cpu.data_ptr(), MLPCPUExperts.output_cpu.data_ptr()) + KExpertsCPU.input_tensor_cpu.copy_(input_tensor, non_blocking=True) + KExpertsCPU.expert_ids_cpu.copy_(expert_ids, non_blocking=True) + KExpertsCPU.weights_cpu.copy_(weights, non_blocking=True) + self.cpu_infer.submit_with_cuda_stream(torch.cuda.current_stream().cuda_stream, self.moe.forward(1, expert_ids.size(1), KExpertsCPU.expert_ids_cpu.data_ptr(), KExpertsCPU.weights_cpu.data_ptr(), KExpertsCPU.input_tensor_cpu.data_ptr(), KExpertsCPU.output_cpu.data_ptr())) self.cpu_infer.sync_with_cuda_stream(torch.cuda.current_stream().cuda_stream) - MLPCPUExperts.output_gpu.copy_(MLPCPUExperts.output_cpu, non_blocking=True) - #print("capturing experts finish") - return MLPCPUExperts.output_gpu + KExpertsCPU.output_gpu_map[self.out_device].copy_(KExpertsCPU.output_cpu, non_blocking=True) + return KExpertsCPU.output_gpu_map[self.out_device] else: input_tensor = input_tensor.contiguous().cpu() expert_ids = expert_ids.contiguous().cpu() weights = weights.contiguous().to(torch.float32).cpu() output = torch.empty_like(input_tensor).contiguous() - self.cpu_infer.submit(self.moe.forward, expert_ids.size(0), expert_ids.size(1), expert_ids.data_ptr(), weights.data_ptr(), input_tensor.data_ptr(), output.data_ptr()) + self.cpu_infer.submit(self.moe.forward(expert_ids.size(0), expert_ids.size(1), expert_ids.data_ptr(), weights.data_ptr(), input_tensor.data_ptr(), output.data_ptr())) self.cpu_infer.sync() - return output.to(device=object.__getattribute__(self, "device")) + return output.to(device=object.__getattribute__(self, "out_device")) def unload(self): return @@ -225,12 +246,30 @@ def load_weights(self, override_key: str | None = None, device: str = "cpu"): gate_type = self.gguf_loader.tensor_info[key + ".ffn_gate_exps.weight"]["ggml_type"] up_type = self.gguf_loader.tensor_info[key + ".ffn_up_exps.weight"]["ggml_type"] down_type = self.gguf_loader.tensor_info[key + ".ffn_down_exps.weight"]["ggml_type"] + elif key + ".ffn_down.0.weight" in self.gguf_loader.tensor_info: + # for supporting Mixtral-8x7B-Instuct + gate = [] + up = [] + down = [] + for i in range(8): + gate_it = self.gguf_loader.get_mmap_tensor(f"{key}.ffn_gate.{i}.weight") + up_it = self.gguf_loader.get_mmap_tensor(f"{key}.ffn_up.{i}.weight") + down_it = self.gguf_loader.get_mmap_tensor(f"{key}.ffn_down.{i}.weight") + gate.append(gate_it) + up.append(up_it) + down.append(down_it) + gate = np.stack(gate) + up = np.stack(up) + down = np.stack(down) + gate_type = self.gguf_loader.tensor_info[key + ".ffn_gate.0.weight"]["ggml_type"] + up_type = self.gguf_loader.tensor_info[key + ".ffn_up.0.weight"]["ggml_type"] + down_type = self.gguf_loader.tensor_info[key + ".ffn_down.0.weight"]["ggml_type"] else: raise ValueError(f"Experts {key} not found in gguf_loader") res = {key:{"gate": gate, "up": up, "down": down, "gate_type": gate_type, "up_type": up_type, "down_type": down_type}} return res -class MLPExpertsMarlin(MLPExpertsBase): +class KExpertsMarlin(KExpertsBase): expert_num: int loaded_experts_idx: list[int] def __init__( @@ -251,11 +290,11 @@ def __init__( self.device = device # create empty marlin experts according to the number of experts per token # up - self.up_projs = [QuantizedLinearMarlin(key+ "." + "ffn_up_exps", gguf_loader, config, device=device) for i in range(self.expert_num)] + self.up_projs = [KLinearMarlin(key+ "." + "ffn_up_exps", gguf_loader, config, device=device) for i in range(self.expert_num)] # gate - self.gate_projs = [QuantizedLinearMarlin(key+ "." + "ffn_gate_exps", gguf_loader, config, device=device) for i in range(self.expert_num)] + self.gate_projs = [KLinearMarlin(key+ "." + "ffn_gate_exps", gguf_loader, config, device=device) for i in range(self.expert_num)] # down - self.down_projs = [QuantizedLinearMarlin(key+ "." + "ffn_down_exps", gguf_loader, config, device=device) for i in range(self.expert_num)] + self.down_projs = [KLinearMarlin(key+ "." + "ffn_down_exps", gguf_loader, config, device=device) for i in range(self.expert_num)] def load(self, w: dict | nn.Parameter | tuple | None = None, device: str | None = None, warmup: bool = False): if device is None: device = self.device @@ -302,7 +341,7 @@ def load_weights(self, override_key: str | None = None): gate_type = self.gguf_loader.tensor_info[key + ".ffn_gate_exps.weight"]["ggml_type"] up_type = self.gguf_loader.tensor_info[key + ".ffn_up_exps.weight"]["ggml_type"] down_type = self.gguf_loader.tensor_info[key + ".ffn_down_exps.weight"]["ggml_type"] - # tensors = self.load_multi(key, [".ffn_gate_exps.weight", ".ffn_up_exps.weight", ".ffn_down_exps.weight"]) + # tensors = self.load_multi(key, [".ffn_gate_exps.weight", ".ffn_up_exps.weight", ".ffn_down_exps.weight"]) res = {key:{"gate": gate, "up": up, "down": down, "gate_type": gate_type, "up_type": up_type, "down_type": down_type}} return res @@ -320,7 +359,7 @@ def forward(self, input_tensor:torch.Tensor, expert_ids, weights): outs = outs.to(device) return outs -class MLPExpertsTorch(MLPExpertsBase): +class KExpertsTorch(KExpertsBase): expert_num: int loaded_experts_idx: list[int] gate: torch.Tensor @@ -362,12 +401,12 @@ def unload(self): self.down = None def forward(self, hidden_states_cpu: torch.Tensor, selected_experts_cpu: torch.Tensor, routing_weights_cpu: torch.Tensor) -> torch.Tensor: - # TODO: forward should transfer data to gpu, and make the data transfering capturable using pin memory, - # just like CPUInfer MLPCPUExperts. There may be a base class of experts on cpu - hidden_states_cpu = hidden_states_cpu.to("cpu") - selected_experts_cpu = selected_experts_cpu.to("cpu") - routing_weights_cpu = routing_weights_cpu.to("cpu") + org_device = hidden_states_cpu.device + hidden_states_cpu = hidden_states_cpu.to(self.device) + selected_experts_cpu = selected_experts_cpu.to(self.device) + routing_weights_cpu = routing_weights_cpu.to(self.device) + batch_sequence_length, hidden_dim = hidden_states_cpu.size() final_hidden_states = torch.zeros( @@ -396,37 +435,39 @@ def forward(self, hidden_states_cpu: torch.Tensor, selected_experts_cpu: torch.T # the `top_x` tensor here. final_hidden_states.index_add_(0, top_x, current_hidden_states) - return final_hidden_states.to(org_dtype) + + return final_hidden_states.to(org_dtype, device=org_device) EXPERTS_MAP = { - "MLPCPUExperts": MLPCPUExperts, - "MLPExpertsTorch": MLPExpertsTorch, - "MLPExpertsMarlin": MLPExpertsMarlin, + "KExpertsCPU": KExpertsCPU, + "KExpertsTorch": KExpertsTorch, + "KExpertsMarlin": KExpertsMarlin, } -class KTransformersMLPExpert(BaseInjectedModule, MLPExpertsBase): + +class KTransformersExperts(BaseInjectedModule, KExpertsBase): def __init__(self, key: str, gguf_loader: GGUFLoader, config: PretrainedConfig, orig_module: nn.Module, - device: str = "cuda", + # device: str = "cuda", prefill_device:str = "cuda", - prefill_mlp_type: str | None = "MLPExpertsTorch", + prefill_op: str | None = "KExpertsTorch", generate_device: str = "cpu", - generate_mlp_type: str | None = "MLPCPUExperts", + generate_op: str | None = "KExpertsCPU", **kwargs): - BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) - MLPExpertsBase.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) - if generate_mlp_type is not None: - self.generate_experts = EXPERTS_MAP[generate_mlp_type](key, gguf_loader, config, len(orig_module), device=generate_device, **kwargs) + BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, generate_device, **kwargs) + KExpertsBase.__init__(self, key, gguf_loader, config, orig_module, generate_device, **kwargs) + if generate_op is not None: + self.generate_experts = EXPERTS_MAP[generate_op](key, gguf_loader, config, len(orig_module), device=generate_device, **kwargs) else: self.generate_experts = None - if prefill_mlp_type is not None: - self.prefill_experts = EXPERTS_MAP[prefill_mlp_type](key, gguf_loader, config, len(orig_module), device=prefill_device, **kwargs) + if prefill_op is not None: + self.prefill_experts = EXPERTS_MAP[prefill_op](key, gguf_loader, config, len(orig_module), device=prefill_device, **kwargs) else: self.prefill_experts = None - self.gpu_mlp_type = prefill_mlp_type - self.cpu_mlp_type = generate_mlp_type + self.gpu_mlp_type = prefill_op + self.cpu_mlp_type = generate_op self.mode = InferenceState.UNLOAD def load(self, w: dict = None, mode: InferenceState = None, warmup: bool = True): @@ -479,9 +520,10 @@ def set_inference_mode(self, mode: InferenceState): from ktransformers.models.modeling_deepseek import DeepseekV2MoE from ktransformers.models.modeling_qwen2_moe import Qwen2MoeSparseMoeBlock +from ktransformers.models.modeling_mixtral import MixtralSparseMoeBlock -class Qwen2MoeSparseMoeBlockInjected(BaseInjectedModule, Qwen2MoeSparseMoeBlock): +class KQwen2MoeSparseMoeBlock(BaseInjectedModule, Qwen2MoeSparseMoeBlock): def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: """ """ orig_shape = hidden_states.shape @@ -506,16 +548,16 @@ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: y.resize_(*orig_shape) return y, router_logits - hidden_states_expert = hidden_states.to(self.experts.device) if isinstance(self.experts, MLPExpertsBase) else hidden_states_expert.cpu() - selected_experts_expert = selected_experts.to(self.experts.device) if isinstance(self.experts, MLPExpertsBase) else selected_experts_expert.cpu() - routing_weights_expert = routing_weights.to(self.experts.device) if isinstance(self.experts, MLPExpertsBase) else routing_weights_expert.cpu() + hidden_states_expert = hidden_states.to(self.experts.device) if isinstance(self.experts, KExpertsBase) else hidden_states_expert.cpu() + selected_experts_expert = selected_experts.to(self.experts.device) if isinstance(self.experts, KExpertsBase) else selected_experts_expert.cpu() + routing_weights_expert = routing_weights.to(self.experts.device) if isinstance(self.experts, KExpertsBase) else routing_weights_expert.cpu() shared_expert_output = self.shared_expert(hidden_states) shared_expert_output = ( F.sigmoid(self.shared_expert_gate(hidden_states)) * shared_expert_output ) - if isinstance(self.experts, MLPExpertsBase): + if isinstance(self.experts, KExpertsBase): y = ( self.moe_on_cpuinfer( hidden_states_expert, selected_experts_expert, routing_weights_expert @@ -586,8 +628,7 @@ def moe_infer(self, hidden_states_cpu: torch.Tensor, selected_experts_cpu: torch return final_hidden_states - -class DeepseekV2MoEInjected(BaseInjectedModule, DeepseekV2MoE): +class KDeepseekV2MoE(BaseInjectedModule, DeepseekV2MoE): def forward(self, hidden_states): identity = hidden_states orig_shape = hidden_states.shape @@ -607,7 +648,7 @@ def forward(self, hidden_states): if self.config.n_shared_experts is not None: y_ = self.shared_experts(identity).squeeze(0) - if isinstance(self.experts, MLPExpertsBase): + if isinstance(self.experts, KExpertsBase): y = self.moe_on_cpuinfer(hidden_states, topk_idx, topk_weight).view(*orig_shape).to(device=hidden_states.device) elif hidden_states.size(0) > 10: # TODO may bugs here @@ -685,3 +726,102 @@ def moe_infer(self, x, topk_ids, topk_weight): .type(new_x.dtype) ) return final_out + +class KMisrtalSparseMoEBlock(BaseInjectedModule, MixtralSparseMoeBlock): + + def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: + """ """ + orig_shape = hidden_states.shape + batch_size, sequence_length, hidden_dim = hidden_states.shape + if self.training and self.jitter_noise > 0: + hidden_states *= torch.empty_like(hidden_states).uniform_(1.0 - self.jitter_noise, 1.0 + self.jitter_noise) + hidden_states = hidden_states.view(-1, hidden_dim) + # router_logits: (batch * sequence_length, n_experts) + router_logits = self.gate(hidden_states) + + routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float) + routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1) + routing_weights /= routing_weights.sum(dim=-1, keepdim=True) + # we cast back to the input dtype + routing_weights = routing_weights.to(hidden_states.dtype) + + if sequence_length == 1 and hasattr(self.experts.generate_experts, "submit_for_one_decode"): + self.experts.generate_experts.submit_for_one_decode(hidden_states[0], selected_experts[0], routing_weights[0]) + y = self.experts.generate_experts.sync_for_one_decode().unsqueeze(0) + y.resize_(*orig_shape) + return y, router_logits + + hidden_states_expert = hidden_states.to(self.experts.device) if isinstance(self.experts, KExpertsBase) else hidden_states_expert.cpu() + selected_experts_expert = selected_experts.to(self.experts.device) if isinstance(self.experts, KExpertsBase) else selected_experts_expert.cpu() + routing_weights_expert = routing_weights.to(self.experts.device) if isinstance(self.experts, KExpertsBase) else routing_weights_expert.cpu() + + if isinstance(self.experts, KExpertsBase): + y = ( + self.moe_on_cpuinfer( + hidden_states_expert, selected_experts_expert, routing_weights_expert + ) + .view(*orig_shape) + .to(device=hidden_states.device) + ) + elif hidden_states_expert.size(0) > 10: + y = self.moe_infer( + hidden_states_expert, selected_experts_expert, routing_weights_expert, orig_shape + ).to(device=hidden_states.device) + else: + y = self.moe_infer_simple( + hidden_states_expert, selected_experts_expert, routing_weights_expert + ).to(device=hidden_states.device) + + y.resize_(*orig_shape) + return y, router_logits + + @torch.no_grad() + def moe_on_cpuinfer(self, x: torch.Tensor, topk_ids: torch.Tensor, topk_weight: torch.Tensor) -> torch.Tensor: + outs = torch.empty_like(x) + outs = self.experts(x, topk_ids, topk_weight) + return outs + + @torch.no_grad() + # TODO may bugs here + def moe_infer_simple(self, hidden_states_cpu: torch.Tensor, selected_experts_cpu: torch.Tensor, routing_weights_cpu: torch.Tensor) -> torch.Tensor: + ''' + hidden_states_cpu: [num_tokens, hidden_size] + topk_ids, topk_weight: [num_tokens, num_selected_experts] + ''' + outs = torch.zeros_like(hidden_states_cpu) + for token_idx in range(selected_experts_cpu.size(0)): + for expert_idx in range(selected_experts_cpu.size(1)): + expert = self.experts[selected_experts_cpu[token_idx, expert_idx]] + outs[token_idx] += expert.forward(hidden_states_cpu[token_idx]) * routing_weights_cpu[token_idx, expert_idx] + return outs + + @torch.no_grad() + # TODO may bugs here + def moe_infer(self, hidden_states_cpu: torch.Tensor, selected_experts_cpu: torch.Tensor, routing_weights_cpu: torch.Tensor, orig_shape: tuple) -> torch.Tensor: + + batch_size, sequence_length, hidden_dim = orig_shape + + final_hidden_states = torch.zeros( + (batch_size * sequence_length, hidden_dim), dtype=hidden_states_cpu.dtype, device=hidden_states_cpu.device + ) + + # One hot encode the selected experts to create an expert mask + # this will be used to easily index which expert is going to be sollicitated + expert_mask = torch.nn.functional.one_hot(selected_experts_cpu, num_classes=self.num_experts).permute(2, 1, 0) + + # Loop over all available experts in the model and perform the computation on each expert + for expert_idx in range(self.num_experts): + expert_layer = self.experts[expert_idx] + idx, top_x = torch.where(expert_mask[expert_idx]) + + # Index the correct hidden states and compute the expert hidden state for + # the current expert. We need to make sure to multiply the output hidden + # states by `routing_weights` on the corresponding tokens (top-1 and top-2) + current_state = hidden_states_cpu[None, top_x].reshape(-1, hidden_dim) + current_hidden_states = expert_layer.forward(current_state) * routing_weights_cpu[top_x, idx, None] + + # However `index_add_` only support torch tensors for indexing so we'll use + # the `top_x` tensor here. + final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states_cpu.dtype)) + + return final_hidden_states \ No newline at end of file diff --git a/ktransformers/operators/linear.py b/ktransformers/operators/linear.py index e264323..146fb85 100644 --- a/ktransformers/operators/linear.py +++ b/ktransformers/operators/linear.py @@ -6,13 +6,14 @@ Date : 2024-07-25 11:25:24 Version : 0.1.0 LastEditors : Azure -LastEditTime : 2024-07-26 09:27:53 +LastEditTime : 2024-08-14 14:57:04 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' +import ctypes import torch -from torch import nn +from torch import Tensor, nn import KTransformersOps from ktransformers.util.custom_gguf import GGUFLoader from ktransformers.util.utils import InferenceState @@ -25,10 +26,16 @@ from ktransformers.operators.base_operator import BaseInjectedModule from transformers.configuration_utils import PretrainedConfig from abc import ABC, abstractmethod +import sys, os +sys.path.append(os.path.join(os.path.dirname(__file__), "..", "ktransformers_ext", "build")) +sys.path.append(os.path.join(os.path.dirname(__file__), "..", "ktransformers_ext", "build", "Release")) +sys.path.append(os.path.join(os.path.dirname(__file__), "..", "ktransformers_ext", "build", "Debug")) +import cpuinfer_ext +from ktransformers.operators.cpuinfer import CPUInfer +from ktransformers.server.config.config import Config - -#class QuantizedLinearBase(BaseInjectedModule, ABC): -class QuantizedLinearBase(ABC): +#class KLinearBase(BaseInjectedModule, ABC): +class KLinearBase(ABC): def __init__( self, key: str, @@ -99,7 +106,7 @@ def unload(self): pass -class QuantizedLinearTorch(QuantizedLinearBase): +class KLinearTorch(KLinearBase): def __init__( self, key: str, @@ -118,6 +125,7 @@ def __init__( def forward(self, x: torch.Tensor) -> torch.Tensor: dtype = x.dtype out_device = x.device + # TODO: support CUDA Graph when using cpu, but CPUInfer is recommended. x = x.to(device=self.device, dtype=self.dtype) x = x @ self.w if self.has_bias: @@ -128,7 +136,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: def load(self, w: dict | nn.Parameter | tuple | None = None, device: str|None = None): if device is None: device = self.device if w is None: w = self.load_weight(device=device) - + if isinstance(w, nn.Parameter): self.w = w.to(dtype=self.dtype).view(self.out_features, self.in_features).T self.has_bias = False @@ -150,7 +158,7 @@ def unload(self): self.bias = None -class QuantizedLinearMarlin(QuantizedLinearBase): +class KLinearMarlin(KLinearBase): marlin_q_w: torch.Tensor marlin_s: torch.Tensor g_idx: torch.Tensor @@ -176,7 +184,7 @@ def __init__( self.act_order = act_order self.is_k_full = is_k_full - def load(self, w: dict | nn.Parameter | tuple | None = None, device: str|None = "cuda"): + def load(self, w: dict | nn.Parameter | tuple | None = None, device: str|None = None): if device is None: device = self.device assert device.lower() != "cpu", "Marlin quantized linear only supports GPU device" if w is None: w = self.load_weight(device=device) @@ -200,7 +208,7 @@ def load(self, w: dict | nn.Parameter | tuple | None = None, device: str|None = weight, self.num_bits, self.group_size, self.act_order ) self.workspace = MarlinWorkspace( - self.out_features, GPTQ_MARLIN_MIN_THREAD_N, GPTQ_MARLIN_MAX_PARALLEL + self.out_features, GPTQ_MARLIN_MIN_THREAD_N, GPTQ_MARLIN_MAX_PARALLEL,self.device ) self.marlin_q_w = marlin_q_w self.marlin_s = marlin_s @@ -243,36 +251,138 @@ def unload(self): self.g_idx = None self.sort_indices = None self.workspace = None - + +class KLinearCPUInfer(KLinearBase): + CPU_INFER = CPUInfer(Config().cpu_infer) + def __init__( + self, + key: str, + gguf_loader: GGUFLoader, + config: PretrainedConfig, + orig_module: nn.Module = None, + device: str = "cpu", + out_device: str = "cuda", # this device mean which device the output should on. TODO: support cpu. + stride = 16, + group_max_len = 1024, + **kwargs, + ): + super().__init__(key, gguf_loader, config, orig_module, device, **kwargs) + self.has_bias = False + self.dtype = torch.get_default_dtype() + self.w = None + self.has_bias = False + self.stride = stride + self.group_max_len = group_max_len + self.out_device = out_device + + def forward(self, x: torch.Tensor) -> torch.Tensor: + origin_shape = x.shape # [batch_size, q_len, hidden_size] + if origin_shape[1] == 1: + out_device = x.device + self.input_tensor_cpu.copy_(x, non_blocking=True) + qlen = origin_shape[1] + KLinearCPUInfer.CPU_INFER.submit_with_cuda_stream( + torch.cuda.current_stream().cuda_stream, + self.linear.forward( + qlen, + self.input_tensor_cpu.data_ptr(), + self.output_cpu.data_ptr() + ) + ) + KLinearCPUInfer.CPU_INFER.sync_with_cuda_stream(torch.cuda.current_stream().cuda_stream) + self.output_gpu.copy_(self.output_cpu, non_blocking=True) + if self.has_bias: + self.output_gpu += self.bias + return self.output_gpu + else: + dtype = x.dtype + out_device = x.device + x = x.to(device=self.device) + qlen = origin_shape[1] + output_shape = (*origin_shape[:-1], self.out_features) + output = torch.empty(output_shape, device=x.device, dtype=x.dtype) + KLinearCPUInfer.CPU_INFER.submit( + self.linear.forward( + qlen, + x.data_ptr(), + output.data_ptr() + ) + ) + KLinearCPUInfer.CPU_INFER.sync() + if self.has_bias: + output = output + self.bias + output = output.to(dtype=dtype, device=out_device) + return output + + def load(self, w: dict | nn.Parameter | tuple | None = None, device: str|None = None, warmup:bool = True): + print(f"loading {self.key} to {self.device} using CPUInfer") + if device is None: device = self.device + self.load_weights(w=w, device=device) + if self.bias is not None: + self.has_bias = True + self.bias = self.bias.to(device) + + weight_ptr = ctypes.addressof( + ctypes.cast(self.weight.ctypes.data, ctypes.POINTER(ctypes.c_uint64)).contents + ) + config = cpuinfer_ext.linear.LinearConfig(self.in_features, self.out_features, self.stride, self.group_max_len, weight_ptr, self.weight_type, 30) + self.linear = cpuinfer_ext.linear.Linear(config) + + if warmup: + KLinearCPUInfer.CPU_INFER.submit(self.linear.warm_up()) + KLinearCPUInfer.CPU_INFER.sync() + self.input_tensor_cpu = torch.zeros((1, 1, self.in_features), device="cpu", pin_memory=True) + self.output_cpu = torch.zeros((1, 1, self.out_features), device="cpu", pin_memory=True, dtype=torch.bfloat16) + self.output_gpu = torch.zeros((1, 1, self.out_features), device=self.out_device) + + def load_weights(self, w: dict | nn.Parameter | tuple | None = None, device: str = "cpu"): + if self.key + ".weight" in self.gguf_loader.tensor_info: + if self.key + ".bias" in self.gguf_loader.tensor_file_map: + self.weight = self.gguf_loader.get_mmap_tensor(self.key + ".weight") + self.weight_type = self.gguf_loader.tensor_info[self.key + ".weight"]["ggml_type"] + self.bias = self.gguf_loader.load_gguf_tensor(self.key + ".bias", device=device) + else: + self.weight = self.gguf_loader.get_mmap_tensor(self.key + ".weight") + self.weight_type = self.gguf_loader.tensor_info[self.key + ".weight"]["ggml_type"] + self.bias = None + else: + raise ValueError(f"Linear {self.key} not found in gguf_loader") + + def unload(self): + if self.w is not None: + self.w = None + if self.has_bias: + self.bias = None + LINEAR_MAP = { - "QuantizedLinearMarlin": QuantizedLinearMarlin, - "QuantizedLinearTorch": QuantizedLinearTorch, - "QuantizedLinearTorch": QuantizedLinearTorch, + "KLinearMarlin": KLinearMarlin, + "KLinearTorch": KLinearTorch, + "KLinearCPUInfer": KLinearCPUInfer } -class KTransformerLinear(BaseInjectedModule, QuantizedLinearBase): +class KTransformersLinear(BaseInjectedModule, KLinearBase): def __init__( self, key: str, gguf_loader: GGUFLoader, config: PretrainedConfig, orig_module: nn.Module, - device: str = "cuda", + # device: str = "cuda", generate_device: str = "cuda", - generate_op: str| None = "QuantizedLinearMarlin", + generate_op: str| None = "KLinearMarlin", prefill_device: str = "cuda", - prefill_op: str| None = "QuantizedLinearTorch", + prefill_op: str| None = "KLinearTorch", **kwargs, ): - BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) - QuantizedLinearBase.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) + BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, generate_device, **kwargs) + KLinearBase.__init__(self, key, gguf_loader, config, orig_module, generate_device, **kwargs) # build all the linear operators if prefill_op is not None: assert prefill_op in LINEAR_MAP, f"linear_type {prefill_op} not supported" - if prefill_op == "QuantizedLinearMarlin" and (orig_module.in_features%GPTQ_MARLIN_MIN_THREAD_N!=0 or orig_module.out_features%GPTQ_MARLIN_MIN_THREAD_N!=0): - print(f"This linear module's in_features or out_features is not divisible by GPTQ_MARLIN_MIN_THREAD_N({GPTQ_MARLIN_MIN_THREAD_N}), using QuantizedLinearTorch instead.") + if prefill_op == "KLinearMarlin" and (orig_module.in_features%GPTQ_MARLIN_MIN_THREAD_N!=0 or orig_module.out_features%GPTQ_MARLIN_MIN_THREAD_N!=0): + print(f"This linear module's in_features or out_features is not divisible by GPTQ_MARLIN_MIN_THREAD_N({GPTQ_MARLIN_MIN_THREAD_N}), using KLinearTorch instead.") print(f"module info: key:{key} orig_module:{orig_module}") - self.prefill_linear = QuantizedLinearTorch(key, gguf_loader, config, orig_module, prefill_device, **kwargs) + self.prefill_linear = KLinearTorch(key, gguf_loader, config, orig_module, prefill_device, **kwargs) else: self.prefill_linear = LINEAR_MAP[prefill_op](key, gguf_loader, config, orig_module, prefill_device, **kwargs) else: @@ -280,16 +390,15 @@ def __init__( if generate_op is not None: assert generate_op in LINEAR_MAP, f"linear_type {generate_op} not supported" - if generate_op == "QuantizedLinearMarlin" and (orig_module.in_features%GPTQ_MARLIN_MIN_THREAD_N!=0 or orig_module.out_features%GPTQ_MARLIN_MIN_THREAD_N!=0): - print(f"This linear module's in_features or out_features is not divisible by GPTQ_MARLIN_MIN_THREAD_N({GPTQ_MARLIN_MIN_THREAD_N}), using QuantizedLinearTorch instead.") + if generate_op == "KLinearMarlin" and (orig_module.in_features%GPTQ_MARLIN_MIN_THREAD_N!=0 or orig_module.out_features%GPTQ_MARLIN_MIN_THREAD_N!=0): + print(f"This linear module's in_features or out_features is not divisible by GPTQ_MARLIN_MIN_THREAD_N({GPTQ_MARLIN_MIN_THREAD_N}), using KLinearTorch instead.") print(f"module info: key:{key} orig_module:{orig_module}") - self.generate_op = "QuantizedLinearTorch" - self.generate_linear = QuantizedLinearTorch(key, gguf_loader, config, orig_module, generate_device, **kwargs) + self.generate_op = "KLinearTorch" + self.generate_linear = KLinearTorch(key, gguf_loader, config, orig_module, generate_device, **kwargs) else: self.generate_linear = LINEAR_MAP[generate_op](key, gguf_loader, config, orig_module, generate_device, **kwargs) else: self.generate_linear = None - self.device = device self.mode = InferenceState.UNLOAD def forward(self, x): diff --git a/ktransformers/operators/layer_wise_prefill.py b/ktransformers/operators/models.py similarity index 92% rename from ktransformers/operators/layer_wise_prefill.py rename to ktransformers/operators/models.py index 61efed8..c95e1ee 100644 --- a/ktransformers/operators/layer_wise_prefill.py +++ b/ktransformers/operators/models.py @@ -6,7 +6,7 @@ Date : 2024-07-25 11:25:24 Version : 1.0.0 LastEditors : Azure -LastEditTime : 2024-07-26 09:27:48 +LastEditTime : 2024-08-14 14:53:05 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' @@ -45,6 +45,8 @@ from transformers.models.qwen2_moe.configuration_qwen2_moe import Qwen2MoeConfig from ktransformers.operators.base_operator import BaseInjectedModule from ktransformers.util.utils import InferenceState +from ktransformers.util.custom_gguf import GGUFLoader +from transformers.configuration_utils import PretrainedConfig if is_flash_attn_2_available(): from flash_attn import flash_attn_func, flash_attn_varlen_func @@ -73,34 +75,6 @@ [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ - -@add_start_docstrings( - "The bare Qwen2MoE Model outputting raw hidden-states without any specific head on top.", - QWEN2MOE_START_DOCSTRING, -) -class Qwen2MoePreTrainedModel(PreTrainedModel): - config_class = Qwen2MoeConfig - base_model_prefix = "model" - supports_gradient_checkpointing = True - _no_split_modules = ["Qwen2MoeDecoderLayer"] - _skip_keys_device_placement = "past_key_values" - _supports_flash_attn_2 = True - _supports_sdpa = True - _supports_cache_class = True - _supports_static_cache = True - - def _init_weights(self, module): - std = self.config.initializer_range - if isinstance(module, nn.Linear): - module.weight.data.normal_(mean=0.0, std=std) - if module.bias is not None: - module.bias.data.zero_() - elif isinstance(module, nn.Embedding): - module.weight.data.normal_(mean=0.0, std=std) - if module.padding_idx is not None: - module.weight.data[module.padding_idx].zero_() - - QWEN2MOE_INPUTS_DOCSTRING = r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -177,13 +151,11 @@ def _init_weights(self, module): the complete sequence length. """ -from ktransformers.util.custom_gguf import GGUFLoader -from transformers.configuration_utils import PretrainedConfig @add_start_docstrings( "The bare Qwen2MoE Model outputting raw hidden-states without any specific head on top.", QWEN2MOE_START_DOCSTRING, ) -class Qwen2MoeModelPerLayerPrefill(BaseInjectedModule): +class KQwen2MoeModel(BaseInjectedModule): """ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`Qwen2MoeDecoderLayer`] @@ -198,10 +170,13 @@ def __init__( orig_module: nn.Module, device: str = "cuda", per_layer_prefill_intput_threshold: int = 30000, # if None, no per-layer prefill + transfer_map: dict = None, **kwargs, ): BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) self.per_layer_prefill_intput_threshold = per_layer_prefill_intput_threshold + self.transfer_map = transfer_map + self.stream_device_map = dict() @add_start_docstrings_to_model_forward(QWEN2MOE_INPUTS_DOCSTRING) def forward( @@ -287,7 +262,20 @@ def forward( all_router_logits = () if output_router_logits else None next_decoder_cache = None - for decoder_layer in self.layers: + for i, decoder_layer in enumerate(self.layers): + if self.transfer_map is not None and i in self.transfer_map: + prev_stream = torch.cuda.current_stream() + cur_device = self.transfer_map[i] + if cur_device not in self.stream_device_map: + self.stream_device_map[cur_device] = torch.cuda.Stream(cur_device) + torch.cuda.set_device(cur_device) + self.stream_device_map[cur_device].wait_stream(prev_stream) + torch.cuda.set_stream(self.stream_device_map[cur_device]) + hidden_states = hidden_states.to(self.transfer_map[i], non_blocking = True) + causal_mask = causal_mask.to(self.transfer_map[i], non_blocking = True) if causal_mask is not None else None + position_ids = position_ids.to(self.transfer_map[i], non_blocking = True) if position_ids is not None else None + cache_position = cache_position.to(self.transfer_map[i], non_blocking = True) if cache_position is not None else None + if output_hidden_states: all_hidden_states += (hidden_states,) @@ -463,7 +451,7 @@ def load_layer_to(self, layer:Qwen2MoeDecoderLayer, target: InferenceState): """ -class DeepseekV2ModelPerLayerPrefill(BaseInjectedModule): +class KDeepseekV2Model(BaseInjectedModule): """ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`DeepseekV2DecoderLayer`] @@ -478,10 +466,13 @@ def __init__( orig_module: nn.Module, device: str = "cuda", per_layer_prefill_intput_threshold: int = 30000, # if None, no per-layer prefill + transfer_map: dict = None, **kwargs, ): BaseInjectedModule.__init__(self, key, gguf_loader, config, orig_module, device, **kwargs) self.per_layer_prefill_intput_threshold = per_layer_prefill_intput_threshold + self.transfer_map = transfer_map + self.stream_device_map = dict() @add_start_docstrings_to_model_forward(DeepseekV2_INPUTS_DOCSTRING) def forward( @@ -584,7 +575,20 @@ def forward( t_cpu = 0 t_f = 0 - for decoder_layer in self.layers: + for i, decoder_layer in enumerate(self.layers): + if self.transfer_map is not None and i in self.transfer_map: + prev_stream = torch.cuda.current_stream() + cur_device = self.transfer_map[i] + if cur_device not in self.stream_device_map: + self.stream_device_map[cur_device] = torch.cuda.Stream(cur_device) + torch.cuda.set_device(cur_device) + self.stream_device_map[cur_device].wait_stream(prev_stream) + torch.cuda.set_stream(self.stream_device_map[cur_device]) + hidden_states = hidden_states.to(self.transfer_map[i], non_blocking = True) + causal_mask = causal_mask.to(self.transfer_map[i], non_blocking = True) if causal_mask is not None else None + position_ids = position_ids.to(self.transfer_map[i], non_blocking = True) if position_ids is not None else None + cache_position = cache_position.to(self.transfer_map[i], non_blocking = True) if cache_position is not None else None + if output_hidden_states: all_hidden_states += (hidden_states,) diff --git a/ktransformers/optimize/optimize.py b/ktransformers/optimize/optimize.py index 7062166..32eab01 100644 --- a/ktransformers/optimize/optimize.py +++ b/ktransformers/optimize/optimize.py @@ -1,6 +1,6 @@ ''' Description : -Author : Boxin Zhang +Author : Boxin Zhang, Azure-Tang Version : 0.1.0 Copyright (c) 2024 by KVCache.AI, All Rights Reserved. ''' @@ -15,6 +15,7 @@ from ktransformers.util.custom_gguf import GGUFLoader, translate_name_to_gguf from ktransformers.util.utils import set_module, load_weights import itertools +import copy def inject(module, local_optimization_dict, model_config:AutoConfig ,gguf_loader:GGUFLoader, prefix=''): for name, child in module._modules.items(): @@ -22,18 +23,20 @@ def inject(module, local_optimization_dict, model_config:AutoConfig ,gguf_loader child_prefix = prefix + name if child_prefix in local_optimization_dict: inject_module_meta=local_optimization_dict[child_prefix] - if isinstance(inject_module_meta, Mapping): + if inject_module_meta["class"] != "default": import_path = inject_module_meta["class"].split(".") import_module_name = ".".join(import_path[:-1]) + gguf_loader.tensor_device_map[inject_module_meta["key"]] = inject_module_meta["kwargs"] if "kwargs" in inject_module_meta else dict() import_class_name = import_path[-1] module_cls=getattr(__import__(import_module_name, fromlist=[""]), import_class_name) print(f"Injecting {child_prefix} as", import_module_name, ".", import_class_name) - inject_module=module_cls(key = inject_module_meta["key"], gguf_loader = gguf_loader, config = model_config, orig_module=child, device = inject_module_meta["device"], **inject_module_meta["kwargs"]) + inject_module=module_cls(key = inject_module_meta["key"], gguf_loader = gguf_loader, config = model_config, orig_module=child, **inject_module_meta["kwargs"]) set_module(module, name, inject_module) - elif isinstance(inject_module_meta, str): - assert inject_module_meta=="default", "for str inject_module_meta, only support \"default\"." + elif inject_module_meta["class"] == "default": + print(f"Injecting {child_prefix} as default") + gguf_loader.tensor_device_map[inject_module_meta["key"]] = inject_module_meta["kwargs"] if "kwargs" in inject_module_meta else dict() else: - raise Exception("inject_module_meta must be a dict or str") + raise Exception("inject_module_meta[\"class\"] must be \"default\" or a class path") child_prefix += "." child_optimization_dict = {k: v for k, v in local_optimization_dict.items() if k.startswith(child_prefix)} inject(child, child_optimization_dict, model_config, gguf_loader, child_prefix) @@ -55,8 +58,9 @@ def gen_optimize_config(module: nn.Module, out_data: Mapping, rule_list: List, p #print("gen_optimize_config", prefix, module_name, translated_name) recursive = True for rule in rule_list: - #print(rule) match_meta = rule["match"] + if "class" not in match_meta and "name" not in match_meta: + raise Exception("match must have at least one of \"class\" and \"name\"") if "class" in match_meta: import_path = match_meta["class"].split(".") import_module_name = ".".join(import_path[:-1]) @@ -67,16 +71,30 @@ def gen_optimize_config(module: nn.Module, out_data: Mapping, rule_list: List, p if "name" in match_meta: if re.search(match_meta["name"], module_name) is None: continue - replace_meta = rule["replace"] - out_data[module_name]={"key": translated_name, - "class": replace_meta["class"], - "device": replace_meta["device"] if "device" in replace_meta else default_device, - "kwargs": replace_meta["kwargs"] if "kwargs" in replace_meta else dict()} + if "replace" not in rule: + raise Exception("replace must be in rule") + if "replace" in rule: + replace_meta = rule["replace"] + if module_name not in out_data: + out_data[module_name]={"key": translated_name, + "class": replace_meta["class"] if "class" in replace_meta else "default", + # "device": replace_meta["device"] if "device" in replace_meta else default_device, + "kwargs": copy.deepcopy(replace_meta["kwargs"]) if "kwargs" in replace_meta else dict()} + else: + if out_data[module_name]["class"] == "default": + out_data[module_name]["class"] = replace_meta["class"] if "class" in replace_meta else "default" + out_data[module_name]["kwargs"].update(copy.deepcopy(replace_meta["kwargs"]) if "kwargs" in replace_meta else dict()) if "recursive" in rule: recursive = bool(rule["recursive"]) + break if module_name not in out_data: - out_data[module_name]="default" + out_data[module_name]= { + "class": "default", + "key": translated_name, + "kwargs": {"generate_device": default_device, + "prefill_device": default_device} + } #print(out_data[module_name]) #input() @@ -88,6 +106,14 @@ def gen_optimize_config(module: nn.Module, out_data: Mapping, rule_list: List, p gen_optimize_config(child, out_data, rule_list, child_prefix) +def translate_model_config(model_config: PretrainedConfig): + # for supporting some special model + if model_config.model_type == "mixtral": + model_config.moe_intermediate_size = model_config.intermediate_size + + return model_config + + def optimize_and_load_gguf(module: nn.Module, rule_file: str, gguf_path: str, model_config: PretrainedConfig, default_device: str = "cuda:0"): with open(rule_file, 'r', encoding='utf-8') as f: rule_list = yaml.load(f.read(), Loader=yaml.FullLoader) @@ -95,8 +121,12 @@ def optimize_and_load_gguf(module: nn.Module, rule_file: str, gguf_path: str, mo optimize_config = dict() gen_optimize_config(module, optimize_config, rule_list, default_device = default_device) + model_config = translate_model_config(model_config) + gguf_loader=GGUFLoader(gguf_path) with torch.device("meta"): inject(module, optimize_config, model_config, gguf_loader) load_weights(module, gguf_loader) + module.gguf_loader = gguf_loader del_meta(module) + torch.cuda.empty_cache() diff --git a/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat-multi-gpu-4.yaml b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat-multi-gpu-4.yaml new file mode 100644 index 0000000..d7adfa2 --- /dev/null +++ b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat-multi-gpu-4.yaml @@ -0,0 +1,228 @@ +- match: + name: "^model.embed_tokens" + replace: + class: "default" + kwargs: + generate_device: "cpu" + prefill_device: "cpu" + +- match: + name: "^model\\.layers\\.([0-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([1][0-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" +- match: + name: "^model\\.layers\\.([2][0-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:2" + prefill_device: "cuda:2" +- match: + name: "^model\\.layers\\.([345][0-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:3" + prefill_device: "cuda:3" + +- match: + name: "^model\\.layers\\.([0-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" +- match: + name: "^model\\.layers\\.([1][0-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" +- match: + name: "^model\\.layers\\.([2][0-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:2" + prefill_device: "cuda:2" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" +- match: + name: "^model\\.layers\\.([345][0-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:3" + prefill_device: "cuda:3" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" + +- match: + name: "^model\\.layers\\.([0-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([1][0-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" +- match: + name: "^model\\.layers\\.([2][0-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:2" + prefill_device: "cuda:2" +- match: + name: "^model\\.layers\\.([345][0-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:3" + prefill_device: "cuda:3" + +- match: + name: "^model\\.layers\\.([0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:0" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:0" + recursive: False # don't recursively inject submodules of this module +- match: + name: "^model\\.layers\\.([1][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:1" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:1" + recursive: False # don't recursively inject submodules of this module +- match: + name: "^model\\.layers\\.([2][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:2" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:2" + recursive: False # don't recursively inject submodules of this module +- match: + name: "^model\\.layers\\.([345][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:3" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:3" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model\\.layers\\.([0-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([1][0-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" +- match: + name: "^model\\.layers\\.([2][0-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:2" + prefill_device: "cuda:2" +- match: + name: "^model\\.layers\\.([345][0-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:3" + prefill_device: "cuda:3" + +- match: + name: "^model$" + replace: + class: "ktransformers.operators.models.KDeepseekV2Model" + kwargs: + per_layer_prefill_intput_threshold: 0 # 0 is close layer wise prefill + transfer_map: + 10: "cuda:1" + 20: "cuda:2" + 30: "cuda:3" + +- match: + name: "^model\\.layers\\.([0-9])\\." + replace: + class: "default" + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "(^model\\.layers\\.([1][0-9])\\.)" + replace: + class: "default" + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" +- match: + name: "(^model\\.layers\\.([2][0-9])\\.)" + replace: + class: "default" + kwargs: + generate_device: "cuda:2" + prefill_device: "cuda:2" +- match: + name: "(^model\\.layers\\.([345][0-9])\\.)|(^model.norm)|(^lm_head)" + replace: + class: "default" + kwargs: + generate_device: "cuda:3" + prefill_device: "cuda:3" \ No newline at end of file diff --git a/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat-multi-gpu.yaml b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat-multi-gpu.yaml new file mode 100644 index 0000000..a21b22d --- /dev/null +++ b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat-multi-gpu.yaml @@ -0,0 +1,126 @@ +- match: + name: "^model.embed_tokens" + replace: + class: "default" + kwargs: + generate_device: "cpu" + prefill_device: "cpu" + +- match: + name: "^model\\.layers\\.(0|[1-9]|[12][0-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([345][0-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + +- match: + name: "^model\\.layers\\.(0|[1-9]|[12][0-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" + +- match: + name: "^model\\.layers\\.([345][0-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" + +- match: + name: "^model\\.layers\\.(0|[1-9]|[12][0-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([345][0-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + +- match: + name: "^model\\.layers\\.(0|[1-9]|[12][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:0" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:0" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model\\.layers\\.([345][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:1" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:1" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model\\.layers\\.(0|[1-9]|[12][0-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([345][0-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" +- match: + name: "^model$" + replace: + class: "ktransformers.operators.models.KDeepseekV2Model" + kwargs: + per_layer_prefill_intput_threshold: 0 # 0 is close layer wise prefill + transfer_map: + 30: "cuda:1" + +- match: + name: "^model\\.layers\\.(0|[1-9]|[12][0-9])\\." + replace: + class: "default" + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" + +- match: + name: "(^model\\.layers\\.([345][0-9])\\.)|(model.norm)|(lm_head)" + replace: + class: "default" + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" \ No newline at end of file diff --git a/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat.yaml b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat.yaml index 18efd60..a2701e1 100644 --- a/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat.yaml +++ b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Chat.yaml @@ -2,40 +2,49 @@ class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding replace: class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda" + prefill_device: "cuda" - match: name: "^model\\.layers\\.(?!.*self_attn).*$" # regular expression class: torch.nn.Linear # only match modules matching name and class simultaneously replace: - class: ktransformers.operators.linear.KTransformerLinear # optimized Kernel on quantized data types + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types kwargs: generate_device: "cuda" prefill_device: "cuda" - generate_op: "QuantizedLinearMarlin" - prefill_op: "QuantizedLinearTorch" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" - match: name: "^model\\.layers\\..*\\.mlp$" class: ktransformers.models.modeling_deepseek.DeepseekV2MoE replace: - class: ktransformers.operators.experts.DeepseekV2MoEInjected # mlp module with custom forward function + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda" + prefill_device: "cuda" - match: name: "^model\\.layers\\..*\\.mlp\\.experts$" replace: - class: ktransformers.operators.experts.KTransformersMLPExpert # custom MoE Kernel with expert paralleism - device: "cpu" # which devices to load this module when initializing + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism kwargs: prefill_device: "cuda" - prefill_mlp_type: "MLPExpertsTorch" + prefill_op: "KExpertsTorch" generate_device: "cpu" - generate_mlp_type: "MLPCPUExperts" + generate_op: "KExpertsCPU" out_device: "cuda" recursive: False # don't recursively inject submodules of this module - match: name: "^model\\.layers\\..*\\.self_attn$" replace: - class: ktransformers.operators.attention.DeepseekV2AttentionInjected # optimized MLA implementation + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda" + prefill_device: "cuda" - match: - name: "^model$" + name: "^model.embed_tokens" replace: - class: "ktransformers.operators.layer_wise_prefill.DeepseekV2ModelPerLayerPrefill" + class: "default" kwargs: - per_layer_prefill_intput_threshold: 0 # 0 is close layer wise prefill + generate_device: "cpu" + prefill_device: "cpu" \ No newline at end of file diff --git a/ktransformers/optimize/optimize_rules/DeepSeek-V2-Lite-Chat-multi-gpu.yaml b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Lite-Chat-multi-gpu.yaml new file mode 100644 index 0000000..cfd77dc --- /dev/null +++ b/ktransformers/optimize/optimize_rules/DeepSeek-V2-Lite-Chat-multi-gpu.yaml @@ -0,0 +1,126 @@ +- match: + name: "^model.embed_tokens" + replace: + class: "default" + kwargs: + generate_device: "cpu" + prefill_device: "cpu" + +- match: + name: "^model\\.layers\\.(0|[1-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([12][0-9])\\." + class: ktransformers.models.modeling_deepseek.DeepseekV2YarnRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.YarnRotaryEmbedding + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + +- match: + name: "^model\\.layers\\.(0|[1-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" + +- match: + name: "^model\\.layers\\.([12][0-9])\\.(?!self_attn).*$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" + +- match: + name: "^model\\.layers\\.(0|[1-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([12][0-9])\\.mlp$" + class: ktransformers.models.modeling_deepseek.DeepseekV2MoE + replace: + class: ktransformers.operators.experts.KDeepseekV2MoE # mlp module with custom forward function + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + +- match: + name: "^model\\.layers\\.(0|[1-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:0" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:0" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model\\.layers\\.([12][0-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + kwargs: + prefill_device: "cuda:1" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:1" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model\\.layers\\.(0|[1-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([12][0-9])\\.self_attn$" + replace: + class: ktransformers.operators.attention.KDeepseekV2Attention # optimized MLA implementation + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" +- match: + name: "^model$" + replace: + class: "ktransformers.operators.models.KDeepseekV2Model" + kwargs: + per_layer_prefill_intput_threshold: 0 # 0 is close layer wise prefill + transfer_map: + 10: "cuda:1" + +- match: + name: "^model\\.layers\\.(0|[1-9])\\." + replace: + class: "default" + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" + +- match: + name: "(^model\\.layers\\.([12][0-9])\\.)|(model.norm)|(lm_head)" + replace: + class: "default" + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" \ No newline at end of file diff --git a/ktransformers/optimize/optimize_rules/Mixtral.yaml b/ktransformers/optimize/optimize_rules/Mixtral.yaml new file mode 100644 index 0000000..ad7d293 --- /dev/null +++ b/ktransformers/optimize/optimize_rules/Mixtral.yaml @@ -0,0 +1,49 @@ +- match: + class: ktransformers.models.modeling_mixtral.MixtralRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.RotaryEmbedding + kwargs: + generate_device: "cuda" + prefill_device: "cuda" +- match: + name: "^model\\.layers\\..*$" + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda" + prefill_device: "cuda" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" +- match: + name: "^model\\.layers\\..*\\.block_sparse_moe$" + class: ktransformers.models.modeling_mixtral.MixtralSparseMoeBlock + replace: + class: ktransformers.operators.experts.KMisrtalSparseMoEBlock +- match: + name: "^model\\.layers\\..*\\.block_sparse_moe\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts + kwargs: + prefill_device: "cuda" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model.embed_tokens" + replace: + class: "default" + kwargs: + generate_device: "cpu" + prefill_device: "cpu" + +- match: + name: "^model\\.layers\\..*\\." + replace: + class: "default" + kwargs: + generate_device: "cuda" + prefill_device: "cuda" \ No newline at end of file diff --git a/ktransformers/optimize/optimize_rules/Qwen2-57B-A14B-Instruct-multi-gpu.yaml b/ktransformers/optimize/optimize_rules/Qwen2-57B-A14B-Instruct-multi-gpu.yaml new file mode 100644 index 0000000..bfa60b7 --- /dev/null +++ b/ktransformers/optimize/optimize_rules/Qwen2-57B-A14B-Instruct-multi-gpu.yaml @@ -0,0 +1,112 @@ +- match: + name: "^model\\.layers\\.([012])\\." + class: ktransformers.models.modeling_qwen2_moe.Qwen2MoeRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.RotaryEmbedding + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" +- match: + name: "^model\\.layers\\.([012])$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" +- match: + name: "^model\\.layers\\.([012])\\.mlp$" + class: ktransformers.models.modeling_qwen2_moe.Qwen2MoeSparseMoeBlock + replace: + class: ktransformers.operators.experts.KQwen2MoeSparseMoeBlock # mlp module with custom forward function +- match: + name: "^model\\.layers\\.([012])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + # device: "cpu" # which devices to load this module when initializing + kwargs: + prefill_device: "cuda:0" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:0" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model\\.layers\\.([12][0-9]|[3-9])\\." + class: ktransformers.models.modeling_qwen2_moe.Qwen2MoeRotaryEmbedding + replace: + class: ktransformers.operators.RoPE.RotaryEmbedding + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" +- match: + name: "^model\\.layers\\.([12][0-9]|[3-9])$" # regular expression + class: torch.nn.Linear # only match modules matching name and class simultaneously + replace: + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" +- match: + name: "^model\\.layers\\.([12][0-9]|[3-9])\\.mlp$" + class: ktransformers.models.modeling_qwen2_moe.Qwen2MoeSparseMoeBlock + replace: + class: ktransformers.operators.experts.KQwen2MoeSparseMoeBlock # mlp module with custom forward function +- match: + name: "^model\\.layers\\.([12][0-9]|[3-9])\\.mlp\\.experts$" + replace: + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + # device: "cpu" # which devices to load this module when initializing + kwargs: + prefill_device: "cuda:1" + prefill_op: "KExpertsTorch" + generate_device: "cpu" + generate_op: "KExpertsCPU" + out_device: "cuda:1" + recursive: False # don't recursively inject submodules of this module + +- match: + name: "^model.embed_tokens" + replace: + class: "default" + kwargs: + generate_device: "cpu" + prefill_device: "cpu" + +- match: + name: "(^model.norm)|(^lm_head)" + replace: + class: "default" + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" + +- match: + name: "^model$" + replace: + class: "ktransformers.operators.models.KQwen2MoeModel" + kwargs: + per_layer_prefill_intput_threshold: 0 # 0 is close layer wise prefill + transfer_map: + 3: "cuda:1" + +- match: + name: "^model\\.layers\\.([012])\\." + replace: + class: "default" + kwargs: + generate_device: "cuda:0" + prefill_device: "cuda:0" + +- match: + name: "^model\\.layers\\.([12][0-9]|[3-9])\\." + replace: + class: "default" + kwargs: + generate_device: "cuda:1" + prefill_device: "cuda:1" \ No newline at end of file diff --git a/ktransformers/optimize/optimize_rules/Qwen2-57B-A14B-Instruct.yaml b/ktransformers/optimize/optimize_rules/Qwen2-57B-A14B-Instruct.yaml index 2b4e312..073332c 100644 --- a/ktransformers/optimize/optimize_rules/Qwen2-57B-A14B-Instruct.yaml +++ b/ktransformers/optimize/optimize_rules/Qwen2-57B-A14B-Instruct.yaml @@ -2,36 +2,56 @@ class: ktransformers.models.modeling_qwen2_moe.Qwen2MoeRotaryEmbedding replace: class: ktransformers.operators.RoPE.RotaryEmbedding + kwargs: + generate_device: "cuda" + prefill_device: "cuda" - match: name: "^model\\.layers\\..*$" # regular expression class: torch.nn.Linear # only match modules matching name and class simultaneously replace: - class: ktransformers.operators.linear.KTransformerLinear # optimized Kernel on quantized data types + class: ktransformers.operators.linear.KTransformersLinear # optimized Kernel on quantized data types kwargs: generate_device: "cuda" prefill_device: "cuda" - generate_op: "QuantizedLinearMarlin" - prefill_op: "QuantizedLinearTorch" + generate_op: "KLinearMarlin" + prefill_op: "KLinearTorch" - match: name: "^model\\.layers\\..*\\.mlp$" class: ktransformers.models.modeling_qwen2_moe.Qwen2MoeSparseMoeBlock replace: - class: ktransformers.operators.experts.Qwen2MoeSparseMoeBlockInjected # mlp module with custom forward function + class: ktransformers.operators.experts.KQwen2MoeSparseMoeBlock # mlp module with custom forward function + kwargs: + generate_device: "cuda" + prefill_device: "cuda" - match: name: "^model\\.layers\\..*\\.mlp\\.experts$" replace: - class: ktransformers.operators.experts.KTransformersMLPExpert # custom MoE Kernel with expert paralleism - device: "cpu" # which devices to load this module when initializing + class: ktransformers.operators.experts.KTransformersExperts # custom MoE Kernel with expert paralleism + # device: "cpu" # which devices to load this module when initializing kwargs: prefill_device: "cuda" - prefill_mlp_type: "MLPExpertsTorch" + prefill_op: "KExpertsTorch" generate_device: "cpu" - generate_mlp_type: "MLPCPUExperts" + generate_op: "KExpertsCPU" out_device: "cuda" recursive: False # don't recursively inject submodules of this module - match: name: "^model$" replace: - class: "ktransformers.operators.layer_wise_prefill.Qwen2MoeModelPerLayerPrefill" + class: "ktransformers.operators.models.KQwen2MoeModel" kwargs: per_layer_prefill_intput_threshold: 0 # 0 is close layer wise prefill +- match: + name: "^model.embed_tokens" + replace: + class: "default" + kwargs: + generate_device: "cpu" + prefill_device: "cpu" +- match: + name: "^model\\.layers\\..*\\." + replace: + class: "default" + kwargs: + generate_device: "cuda" + prefill_device: "cuda" \ No newline at end of file diff --git a/ktransformers/server/backend/interfaces/ktransformers.py b/ktransformers/server/backend/interfaces/ktransformers.py index 77b0cda..8d121d5 100644 --- a/ktransformers/server/backend/interfaces/ktransformers.py +++ b/ktransformers/server/backend/interfaces/ktransformers.py @@ -6,6 +6,7 @@ from ktransformers.models.custom_cache import StaticCache from ktransformers.util.cuda_graph_runner import CUDAGraphRunner from ktransformers.local_chat import custom_models, default_optimize_rules +from ktransformers.util.utils import get_device class KTransformersThreadContext(TransformersThreadContext): @@ -48,8 +49,11 @@ def __init__(self,args:ConfigArgs= default_args): def decode_one_tokens(self): if not hasattr(self, "cuda_graph_runner"): + device_map = self.model.gguf_loader.tensor_device_map + torch_device = get_device('blk.0.self_attn', device_map) + torch_device = "cuda:0" if torch_device == "cuda" else torch_device self.cuda_graph_runner = CUDAGraphRunner() - self.cuda_graph_runner.capture(self.model, self.current_ids, self.active_cache_position.unsqueeze(0), self.active_cache_position, self.cache, return_dict=False, use_cache=True) + self.cuda_graph_runner.capture(self.model, self.current_ids, self.active_cache_position.unsqueeze(0), self.active_cache_position, self.cache, main_device=torch_device, return_dict=False, use_cache=True) if hasattr(self, "cuda_graph_runner"): logits = self.cuda_graph_runner(self.current_ids, self.active_cache_position.unsqueeze(0), self.active_cache_position) diff --git a/ktransformers/tests/dequant_gpu.py b/ktransformers/tests/dequant_gpu.py index 4fbca1c..0dd5272 100644 --- a/ktransformers/tests/dequant_gpu.py +++ b/ktransformers/tests/dequant_gpu.py @@ -1,15 +1,12 @@ import os -os.environ["CUDA_VISIBLE_DEVICES"]="1" +# os.environ["CUDA_VISIBLE_DEVICES"]="1,2" # add path import sys current_path = os.path.abspath(os.path.dirname(__file__)) sys.path.append(current_path+"/../..") -import pycuda.autoinit -import pycuda.driver as cuda -from pycuda.compiler import SourceModule import numpy as np -# from ktransformers.operators.linear import KTransformerLinear, QuantizedLinearMarlin -# from ktransformers.operators.experts import KTransformersMLPExpert, MLPExpertsTorch +# from ktransformers.operators.linear import KTransformersLinear, KLinearMarlin +# from ktransformers.operators.experts import KTransformersExperts, KExpertsTorch from ktransformers.util.custom_gguf import GGUFLoader import torch import KTransformersOps @@ -18,40 +15,44 @@ from transformers import ( AutoConfig, ) +import os +# CUDA_LAUNCH_BLOCKING=1 +os.environ["CUDA_LAUNCH_BLOCKING"]="1" gguf_config = GGUFLoader("/data/Qwen2-57B-A14B-Instruct-GGUF/q4_k_m") model_name = "/data/Qwen2-57B-A14B-Instruct" -key = "blk.0." -target = "ffn_down_exps.weight" + +# Q4k +key = "blk.1." +target = "attn_q.weight" t1 = time.time() q_weight_cpu = gguf_config.load_gguf_tensor(key+target, "cpu") # q_weight_cpu = torch.from_numpy(q_weight_cpu) t2 = time.time() -q_weight_gpu = gguf_config.load_gguf_tensor(key+target, "cuda") +q_weight_gpu = gguf_config.load_gguf_tensor(key+target, "cuda:0") t3 = time.time() print() -allclose = torch.allclose(q_weight_cpu, q_weight_gpu.cpu().to(torch.float32), atol=1e-6) -print(f"Q6k {key+target}") +allclose = torch.allclose(q_weight_cpu, q_weight_gpu.cpu(), atol=1e-6) +print(f"Q4k {key+target}") print("load gguf tensor from cpu cost: ", t2-t1) print("load gguf tensor from gpu cost: ", t3-t2) print("allclose: ", allclose) -key = "blk.1." -target = "ffn_up_shexp.weight" +# Q6k +key = "blk.0." +target = "ffn_down_exps.weight" t1 = time.time() q_weight_cpu = gguf_config.load_gguf_tensor(key+target, "cpu") -# q_weight_cpu = torch.from_numpy(q_weight_cpu) - t2 = time.time() -q_weight_gpu = gguf_config.load_gguf_tensor(key+target, "cuda") +q_weight_gpu = gguf_config.load_gguf_tensor(key+target, "cuda:0") t3 = time.time() print() -allclose = torch.allclose(q_weight_cpu, q_weight_gpu.cpu(), atol=1e-6) -print(f"Q4k {key+target}") +allclose = torch.allclose(q_weight_cpu, q_weight_gpu.cpu().to(torch.float32), atol=1e-6) +print(f"Q6k {key+target}") print("load gguf tensor from cpu cost: ", t2-t1) print("load gguf tensor from gpu cost: ", t3-t2) print("allclose: ", allclose) diff --git a/ktransformers/tests/dequant_gpu_t.py b/ktransformers/tests/dequant_gpu_t.py index 3efcdf3..4b2556d 100644 --- a/ktransformers/tests/dequant_gpu_t.py +++ b/ktransformers/tests/dequant_gpu_t.py @@ -7,11 +7,11 @@ import pycuda.driver as cuda from pycuda.compiler import SourceModule import numpy as np -from ktransformers.operators.linear import KTransformerLinear, QuantizedLinearMarlin -from ktransformers.operators.experts import KTransformersMLPExpert, MLPExpertsTorch +from ktransformers.operators.linear import KTransformersLinear, KLinearMarlin +from ktransformers.operators.experts import KTransformersExperts, KExpertsTorch from ktransformers.util.custom_gguf import GGUFLoader, dequantize_q4_k_gpu, dequantize_q4_k import torch -import CudaOps +import KTransformersOps torch.set_default_dtype(torch.bfloat16) import time from transformers import ( diff --git a/ktransformers/util/cuda_graph_runner.py b/ktransformers/util/cuda_graph_runner.py index 2ac7a17..c7a9c87 100644 --- a/ktransformers/util/cuda_graph_runner.py +++ b/ktransformers/util/cuda_graph_runner.py @@ -21,6 +21,7 @@ def capture( position_ids, cache_position, past_key_values, + main_device, **kwargs, ) -> None: assert self.graph is None @@ -29,15 +30,24 @@ def capture( self.graph = torch.cuda.CUDAGraph() #self.graph.enable_debug_mode() self.model = model - inputs_embeds = model.model.embed_tokens(cur_token.to("cpu")).to("cuda") - with torch.cuda.graph(self.graph): + inputs_embeds = model.model.embed_tokens(cur_token.to("cpu")).to(main_device) + # torch.cuda.set_device can't set "cuda", must have a index + if main_device == "cuda": + main_device = "cuda:0" + torch.cuda.set_device(main_device) + self.main_device = main_device + capture_stream = torch.cuda.Stream() + with torch.cuda.graph(self.graph, stream = capture_stream): logits=model(inputs_embeds=inputs_embeds, position_ids=position_ids, cache_position=cache_position, past_key_values=past_key_values, **kwargs)[0] + capture_stream.wait_stream(torch.cuda.current_stream()) + torch.cuda.set_device(main_device) + torch.cuda.set_stream(capture_stream) past_key_values.change_seq_length(-1) - torch.cuda.synchronize() + torch.cuda.synchronize(self.main_device) #self.graph.debug_dump("cuda_graph_hooked.dot") # Save the input and output buffers. @@ -65,7 +75,7 @@ def forward( #print("begin replay") #time.sleep(1) self.graph.replay() - torch.cuda.synchronize() + torch.cuda.synchronize(self.main_device) # Return the output tensor. return self.output_buffers["logits"] diff --git a/ktransformers/util/custom_gguf.py b/ktransformers/util/custom_gguf.py index b922ac8..bd5c5b0 100644 --- a/ktransformers/util/custom_gguf.py +++ b/ktransformers/util/custom_gguf.py @@ -5,8 +5,8 @@ Author : Azure-Tang, Boxin Zhang, chenht2022 Date : 2024-07-26 08:48:54 Version : 1.0.0 -LastEditors : Azure -LastEditTime : 2024-07-26 09:28:25 +LastEditors : kkk1nak0 +LastEditTime : 2024-08-12 07:21:55 Adapted from https://github.com/99991/pygguf/blob/main/gguf.py Copyright (c) 2023-2024 The ggml authors Copyright (c) 2024 Thomas Germer @@ -18,6 +18,7 @@ import struct import warnings import numpy as np +import re import numpy.typing as npt from typing import Sequence import os @@ -168,6 +169,7 @@ def __init__(self, gguf_path: str): self.tensor_file_map = {} self.file_data_map = {} self.gguf_file_meta = {} + self.tensor_device_map = {} # Walk through all the .gguf files in the directory for root, dirs, files in os.walk(gguf_path): @@ -292,8 +294,19 @@ def load_gguf_tensor(self, name: str, device:str = "cpu")->torch.Tensor: else: values = GGML_DEQUANTIZE[ggml_name](data) values = torch.from_numpy(values) - - return values.view(shape[::-1]) + + values = values.view(shape[::-1]) + if "attn_q" in name and self.gguf_file_meta['general.architecture'] in ["llama"]: + n_head = self.gguf_file_meta['llama.attention.head_count'] + values = (values.reshape(n_head, values.shape[0] // n_head // 2, 2, *values.shape[1:]) + .swapaxes(1, 2) + .reshape(values.shape)) + elif "attn_k" in name and self.gguf_file_meta['general.architecture'] in ["llama"]: + n_head = self.gguf_file_meta['llama.attention.head_count_kv'] + values = (values.reshape(n_head, values.shape[0] // n_head // 2, 2, *values.shape[1:]) + .swapaxes(1, 2) + .reshape(values.shape)) + return values def read_value(f, data_type): if data_type == DATA_TYPES["string"]: @@ -377,8 +390,14 @@ def dequantize_q2_k(data): return d * (scales & 15) * (tmp & 3) - dmin * (scales >> 4) -def dequantize_q2_k_gpu(data): - raise NotImplementedError() +def dequantize_q2_k_gpu(data, device:str ="cuda"): + block_size = GGML_BLOCK_SIZES["Q2_K"] + data = np.frombuffer(data, dtype=data.dtype) + device = torch.device(device) + # TODO: this and from_numpy in other functions will cause a warning saying that numpy is not writable, + # the best way to fix this is transfer ptr to KTransformersOps instead of Tensor. + data = torch.from_numpy(data) + return KTransformersOps.dequantize_q2_k(data, block_size, device) def dequantize_q3_k(data): # C implementation @@ -422,8 +441,14 @@ def dequantize_q3_k(data): (((qs[:, 48:64] >> 6) & 3) - bits[:, 16:, 7]) ], axis=1) -def dequantize_q3_k_gpu(data): - raise NotImplementedError() +def dequantize_q3_k_gpu(data, device:str ="cuda"): + block_size = GGML_BLOCK_SIZES["Q3_K"] + data = np.frombuffer(data, dtype=data.dtype) + device = torch.device(device) + # TODO: this and from_numpy in other functions will cause a warning saying that numpy is not writable, + # the best way to fix this is transfer ptr to KTransformersOps instead of Tensor. + data = torch.from_numpy(data) + return KTransformersOps.dequantize_q3_k(data, block_size, device) def dequantize_q4_k(data): # C implementation @@ -511,9 +536,14 @@ def dequantize_q5_k(data): d8 * (qs_hi_4[:, 3] + (bits[:, :, 7] << 4)) - m8, ], axis=1) -def dequantize_q5_k_gpu(data): - raise NotImplementedError() - +def dequantize_q5_k_gpu(data, device:str ="cuda"): + block_size = GGML_BLOCK_SIZES["Q5_K"] + data = np.frombuffer(data, dtype=data.dtype) + device = torch.device(device) + # TODO: this and from_numpy in other functions will cause a warning saying that numpy is not writable, + # the best way to fix this is transfer ptr to KTransformersOps instead of Tensor. + data = torch.from_numpy(data) + return KTransformersOps.dequantize_q5_k(data, block_size, device) def dequantize_q6_k(data): # C implementation @@ -570,7 +600,7 @@ def dequantize_q6_k_gpu(data: np.ndarray, device:str = "cuda"): num_blocks = len(data) // block_size data = np.frombuffer(data, dtype=data.dtype) data = torch.from_numpy(data) - return KTransformersOps.dequantize_q6_k(data, 210, device) + return KTransformersOps.dequantize_q6_k(data, block_size, device) def dequantize_q4_0(data): # C implementation @@ -679,7 +709,34 @@ def dequantize_f16_gpu(data, device): "Q6_K": dequantize_q6_k_gpu, } + +def translate_name_to_gguf_mixtral(name): + + replacement_template = { + "w1.weight": "ffn_gate", + "w2.weight": "ffn_down", + "w3.weight": "ffn_up" + } + + pattern = re.compile(r"model.layers\.(\d+)\.block_sparse_moe\.experts\.(\d+)\.(w\d\.weight)") + + def replace_match(match): + blk_id = match.group(1) + expert_id = match.group(2) + weight_type = match.group(3) + if weight_type in replacement_template: + return f"blk.{blk_id}.{replacement_template[weight_type]}.{expert_id}.weight" + else: + return match.group(0) + + new_name = re.sub(pattern, replace_match, name) + + return new_name + def translate_name_to_gguf(name): + + name = translate_name_to_gguf_mixtral(name) + name = name.replace("lm_head.", "output.") name = name.replace("model.embed_tokens.", "token_embd.") name = name.replace("model.norm.", "output_norm.") @@ -716,9 +773,14 @@ def translate_name_to_gguf(name): name = name.replace(".mlp.experts.ffn_gate_exps", ".ffn_gate_exps") name = name.replace(".mlp.experts.ffn_up_exps", ".ffn_up_exps") + + name = name.replace(".block_sparse_moe.gate.", ".ffn_gate_inp.") + name = name.replace(".block_sparse_moe.experts", "") + return name if __name__ == '__main__': gguf_path = '/mnt/data/model/DeepSeek-Coder-V2-GGUF-WJH' loader = GGUFLoader(gguf_path) loader.load_gguf_tensor('token_embd.weight') + diff --git a/ktransformers/util/utils.py b/ktransformers/util/utils.py index b5ac573..8c91d47 100644 --- a/ktransformers/util/utils.py +++ b/ktransformers/util/utils.py @@ -39,6 +39,22 @@ def set_param(module: nn.Module, name: str, weights: torch.Tensor): param.unsqueeze_(0) setattr(module, name, param) +def get_device(gguf_module_key:str, device_map:dict): + if gguf_module_key in device_map: + return device_map[gguf_module_key]["generate_device"] + else: + return "cuda" + +def get_all_used_cuda_device(device_map:dict): + all_device_list = set() + for key in device_map: + all_device_list.add(device_map[key]["generate_device"]) if "generate_device" in device_map[key] else None + all_device_list.add(device_map[key]["prefill_device"]) if "prefill_device" in device_map[key] else None + if "cpu" in all_device_list: + all_device_list.remove("cpu") + all_device_list = list(all_device_list) + return all_device_list + def load_cur_state_dict(module: nn.Module, gguf_loader: GGUFLoader, prefix: str = ""): prefix = prefix.replace("orig_module.", "") persistent_buffers = {k: v for k, v in module._buffers.items() if k not in module._non_persistent_buffers_set} @@ -47,18 +63,19 @@ def load_cur_state_dict(module: nn.Module, gguf_loader: GGUFLoader, prefix: str for name, param in local_state.items(): key = prefix + name translated_key = translate_name_to_gguf(key) - print("default loading weights", key, translated_key) if translated_key in gguf_loader.tensor_file_map: target_dtype = torch.get_default_dtype() - device = "cpu" if "embd" in translated_key else "cuda" + device = get_device(translated_key[:translated_key.rfind(".")], gguf_loader.tensor_device_map) + print(f"loading {translated_key} to {device}") + # device = "cpu" if "embd" in translated_key else "cuda" weights = gguf_loader.load_gguf_tensor(translated_key, device = device).to(dtype = target_dtype) set_param(module, name, weights) del weights else: #print(load_config.tensor_file_map.keys()) - raise Exception(f"can't fand {translated_key} in GGUF file!") + raise Exception(f"can't find {translated_key} in GGUF file!") -def load_weights(module:nn.Module, gguf_loader:GGUFLoader, prefix='', return_when_injected:bool = False, only_load_injected:bool = False): +def load_weights(module:nn.Module, gguf_loader:GGUFLoader, prefix=''): # print(f"recursively loading weights {prefix},{return_when_injected=}, {only_load_injected=}") if not isinstance(module, base_operator.BaseInjectedModule): load_cur_state_dict(module, gguf_loader, prefix) @@ -66,29 +83,36 @@ def load_weights(module:nn.Module, gguf_loader:GGUFLoader, prefix='', return_whe load_weights(child, gguf_loader, prefix+name+".") else: module.load() - -def prefill_and_generate(model, tokenizer, inputs, max_new_tokens=10000): + +def prefill_and_generate(model, tokenizer, inputs, max_new_tokens=10000, use_cuda_graph: bool = True): import os os.environ["TOKENIZERS_PARALLELISM"] = "false" torch._dynamo.config.suppress_errors = True batch_size, seq_length = inputs.shape - torch_device = inputs.device + device_map = model.gguf_loader.tensor_device_map + torch_device = get_device('blk.0.self_attn', device_map) + torch_device = "cuda:0" if torch_device == "cuda" else torch_device + inputs = inputs.to(torch_device) + all_cuda_device = get_all_used_cuda_device(device_map) + tokens = [] - def decode_one_tokens(cuda_graph_runner, cur_token, position_ids, cache_position, past_key_values): - logits = cuda_graph_runner(cur_token, position_ids, cache_position) + def decode_one_tokens(cuda_graph_runner, cur_token, position_ids, cache_position, past_key_values, use_cuda_graph: bool = True): + if use_cuda_graph: + logits = cuda_graph_runner(cur_token, position_ids, cache_position) + else: + # custom_stream = torch.cuda.Stream() + torch.cuda.set_device(torch_device) + inputs_embeds = model.model.embed_tokens(cur_token.to("cpu")).to(torch_device) + # with torch.cuda.stream(custom_stream): + logits=model(inputs_embeds=inputs_embeds, + position_ids=position_ids, + cache_position=cache_position, + past_key_values=past_key_values, + return_dict=False, use_cache=True)[0] past_key_values.change_seq_length(1) - """ - inputs_embeds = model.model.embed_tokens(cur_token.to("cpu")).to("cuda") - custom_stream = torch.cuda.Stream() - with torch.cuda.stream(custom_stream): - logits=model(inputs_embeds = inputs_embeds, - position_ids = position_ids, - cache_position = cache_position, - past_key_values = past_key_values, - return_dict = False, use_cache = True) [0] - """ - torch.cuda.synchronize() + for device in all_cuda_device: + torch.cuda.synchronize(device) #print(logits) next_token_scores = logits_warper(inputs, logits[:, -1, :]) if generation_config.do_sample: @@ -97,11 +121,12 @@ def decode_one_tokens(cuda_graph_runner, cur_token, position_ids, cache_position else: next_token = torch.argmax(next_token_scores, dim=-1) return next_token - + + torch.cuda.set_device(torch_device) with torch.no_grad(): stream = TextStreamer(tokenizer) past_key_values = StaticCache( - config = model.config, max_batch_size = 1, max_cache_len = seq_length + max_new_tokens, device = torch_device, dtype = model.dtype + config = model.config, max_batch_size = 1, max_cache_len = seq_length + max_new_tokens, device = device_map, dtype = model.dtype ) cache_position = torch.arange(seq_length, device=torch_device) generated_ids = torch.zeros( @@ -111,21 +136,21 @@ def decode_one_tokens(cuda_graph_runner, cur_token, position_ids, cache_position past_key_values.cur_idx=cache_position start_time = time.time() - inputs_embeds = model.model.embed_tokens(inputs.to("cpu")).to("cuda") + inputs_embeds = model.model.embed_tokens(inputs.to("cpu")).to(torch_device) logits = model( inputs_embeds = inputs_embeds, cache_position=cache_position, past_key_values=past_key_values, return_dict=False, use_cache=True - )[0][:,-1,:].unsqueeze(0).clone() + )[0][:,-1,:].unsqueeze(0).clone().to(torch_device) generation_config, model_kwargs = model._prepare_generation_config( None, max_length=max_new_tokens, do_sample=True, top_k=5, top_p=0.85, temperature=0.1 # change this to modify generate config ) try: # transformers==4.43 logits_warper = ( - model._get_logits_warper(generation_config,device=inputs.device) if generation_config.do_sample else None + model._get_logits_warper(generation_config,device=inputs.device) ) except: logits_warper = ( - model._get_logits_warper(generation_config) if generation_config.do_sample else None + model._get_logits_warper(generation_config) ) next_token_scores = logits_warper(inputs, logits[:, -1, :]) if generation_config.do_sample: @@ -137,7 +162,6 @@ def decode_one_tokens(cuda_graph_runner, cur_token, position_ids, cache_position prefill_count = seq_length prefill_time = first_token_time - print(stream.put(next_token.item()), end="", flush=True) generated_ids[:, seq_length] = next_token tokens.append(next_token) @@ -145,12 +169,16 @@ def decode_one_tokens(cuda_graph_runner, cur_token, position_ids, cache_position cache_position = torch.tensor([seq_length], device=torch_device) position_ids = cache_position.unsqueeze(0) seq_length += 1 - - cuda_graph_runner = CUDAGraphRunner() - cuda_graph_runner.capture(model, next_token.unsqueeze(0), position_ids, cache_position, past_key_values, return_dict=False, use_cache=True) + + if use_cuda_graph: + cuda_graph_runner = CUDAGraphRunner() + cuda_graph_runner.capture(model, next_token.unsqueeze(0), position_ids, cache_position, past_key_values, torch_device, return_dict=False, use_cache=True) + else: + cuda_graph_runner = None + start_time = time.time() for _ in range(1, max_new_tokens): - next_token = decode_one_tokens(cuda_graph_runner, next_token.unsqueeze(0), position_ids, cache_position, past_key_values) + next_token = decode_one_tokens(cuda_graph_runner, next_token.unsqueeze(0), position_ids, cache_position, past_key_values, use_cuda_graph).to(torch_device) inputs = torch.cat((inputs, next_token.unsqueeze(0)), dim=-1) generated_ids[:, cache_position] = next_token.int() tokens.append(next_token.int()) @@ -163,6 +191,7 @@ def decode_one_tokens(cuda_graph_runner, cur_token, position_ids, cache_position print(stream.put(next_token.item()), end="", flush=True) cache_position += 1 position_ids = cache_position.unsqueeze(0) + total_time = time.time() - start_time tokens_generated = len(tokens) diff --git a/setup.py b/setup.py index aeff5f6..2a09b48 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ Date : 2024-07-27 16:15:27 Version : 1.0.0 LastEditors : chenxl -LastEditTime : 2024-08-08 02:45:15 +LastEditTime : 2024-08-14 16:36:19 Adapted from: https://github.com/Dao-AILab/flash-attention/blob/v2.6.3/setup.py Copyright (c) 2023, Tri Dao. @@ -299,6 +299,15 @@ def build_extension(self, ext) -> None: 'ktransformers/ktransformers_ext/cuda/custom_gguf/dequant.cu', 'ktransformers/ktransformers_ext/cuda/binding.cpp', 'ktransformers/ktransformers_ext/cuda/gptq_marlin/gptq_marlin.cu' - ]) + ], + extra_compile_args={ + 'cxx': ['-O3'], + 'nvcc': [ + '-O3', + '--use_fast_math', + '-Xcompiler', '-fPIC', + ] + } + ) ] ) diff --git a/third_party/llamafile/sgemm.cpp b/third_party/llamafile/sgemm.cpp index 6a7cab4..38f6d18 100644 --- a/third_party/llamafile/sgemm.cpp +++ b/third_party/llamafile/sgemm.cpp @@ -94,7 +94,6 @@ static const struct GemmFuncs { #if defined(__FMA__) || (defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))) #if defined(__AVX2__) #if defined(__AVX512F__) - printf("__AVX512F__\n"); #if defined(__AVX512VL__) && defined(__AVX512BW__) && defined(__AVX512DQ__) && defined(__AVX512VNNI__) && defined(__AVX512BF16__) // AMD Zen4+ (2023-) sgemm = llamafile_sgemm_amd_zen4;