Skip to content

fix: correct auto ntk scaling_factor for 4k ctx case - #101

Merged
AlpinDale merged 3 commits into
dphnAI:mainfrom
sandwichdoge:main
Nov 4, 2023
Merged

fix: correct auto ntk scaling_factor for 4k ctx case#101
AlpinDale merged 3 commits into
dphnAI:mainfrom
sandwichdoge:main

Conversation

@sandwichdoge

Copy link
Copy Markdown
Contributor

The correct NTK alpha (at least for llama2) isn't actually that linear. For example if you want 8192 context size, the most optimal value is around 2.6, not 2.

@AlpinDale

Copy link
Copy Markdown
Member

Thanks for the contribution! Please run ./formatting.sh --all to properly format the code, then check the pylint output for minor issues that need to be resolved.

@AlpinDale
AlpinDale self-requested a review November 3, 2023 13:20
@sandwichdoge

Copy link
Copy Markdown
Contributor Author

Alright it's done. I had a lot of trouble running the formatting.sh script on Kubuntu 23 btw. It demanded very specific versions of dependencies, and it threw an error like this:
ImportError: cannot import name 'formatargspec' from 'inspect' (/usr/lib/python3.11/inspect.py)
Which I had to install this to fix:
pip3 install wrapt==1.14.0

@AlpinDale

Copy link
Copy Markdown
Member

Ah sorry, I forgot to mention you'll need to run pip install -r requirements-dev.txt first. I should probably add a contributor readme file. I'll test this PR in a bit and get back to you.

@AlpinDale

Copy link
Copy Markdown
Member

LGTM, thanks for the contribution

@AlpinDale
AlpinDale merged commit 99293aa into dphnAI:main Nov 4, 2023
@Pyroserenus

Pyroserenus commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

@sandwichdoge sorry to dig this up from the grave, but are you sure this is implemented correctly? I have a user situation where 4x scaling is breaking with this automatic implementation but not with dynamic alpha 2 being manually set.

For context, dynamic NTK doesn't work quite the same way as standard NTK alpha scaling. I had just assumed that this was suboptimal when i decided to rescind my concerns. https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/

@1Tomber

1Tomber commented Dec 7, 2023

Copy link
Copy Markdown

I add my findings to get to 16k:
I download the model (tested psyconic 20b and xwin lewd 13b) manually (not using the hf_downloader) and start it via this command line

python -m aphrodite.endpoints.kobold.api_server --port 5000 --host 127.0.0.1 --max-log-len 0 --quantization gptq --dtype float16 --model /workspace/models/TheBloke_psyonic-cetacean-20B-GPTQ --served-model-name jebcarter/psyonic-cetacean-20B --gpu-memory-utilization 0.90 --download-dir /workspace/models --max-model-len 16384

On a full context I get non coherent outputs, stories ending abruptly, output asking me how to continue, gibberish, misplaced words and double quotes in dialogues.

When I change the config.json to "rope_scaling": { "factor": 4.0, "type": "dynamic"},, same command line, the output behaves much more coherent and continues the story in its context (I can't benchmark the quality, but it feels much more coherent).

@AlpinDale

Copy link
Copy Markdown
Member

Perhaps we can only use this scaling for when the user-specified max-model-len is 8192.

@sandwichdoge

sandwichdoge commented Dec 7, 2023

Copy link
Copy Markdown
Contributor Author

@Pyroserenus I see, looks like we're using the dynamically-scaled NTK version and not the standard NTK RoPE. I dug a little deeper and indeed we're using the DynamicNTKScalingRotaryEmbedding method, which already dynamically calculates the alpha value for us.

elif scaling_type == "dynamic":
    rotary_emb = DynamicNTKScalingRotaryEmbedding(
        head_size, rotary_dim, max_position, base, is_neox_style,
        scaling_factor)

And the target length calculation was made as simple as the original max position multiplied by the scaling factor parameter in the implementation!

max_len = self.max_position_embeddings * self.scaling_factor
base = self.base * (
    (self.scaling_factor * max_len / self.max_position_embeddings) -
    (self.scaling_factor - 1))**(self.rotary_dim /
                                 (self.rotary_dim - 2))

For standard NTK scaling, if you scale the factor a little too high, your general perplexity will suffer a bit but you won't get total gibberish. For dynamic NTK becomes a problem indeed.

I can at least confirm the scaling factor is as simple as target_max_len / max_position_embeddings now.

@AlpinDale I think github supports easy PR reverse. Please revert this change.

AlpinDale added a commit that referenced this pull request Dec 7, 2023
@AlpinDale

Copy link
Copy Markdown
Member

Opened #149
will merge after someone (or myself) runs tests

AlpinDale added a commit that referenced this pull request Dec 8, 2023
* readme: add dev branch notice

* add deprecation warning for ooba API

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case (#101)"

This reverts commit 99293aa.
AlpinDale added a commit that referenced this pull request Dec 9, 2023
* readme: add dev branch notice

* add deprecation warning for ooba API

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case (#101)"

This reverts commit 99293aa.
AlpinDale added a commit that referenced this pull request Dec 9, 2023
* stuff

* move to sampling_metadata

* stuff again (i forgot what i did last night

* calculate max_prompt_len locally

* slot_mapping expects an int.
SequenceGroupMetadata has data, not metadata. Naming is hard.

* make persistent metadata persist

* fix mirostat for logprobs and new persistence

* chore: KoboldAI/koboldcpp updates (#48)

* chore: update kobold lite

* chore: koboldcpp compatibility updates

* fix: disable koboldcpp gen_cache on SSE endpoint to avoid memory leak on client abort

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case" (#149)

* readme: add dev branch notice

* add deprecation warning for ooba API

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case (#101)"

This reverts commit 99293aa.

* feat: AMD ROCm support (#95)

* feat: add ROCM kernels

* WIP integration into aphrodite

* fix compatibility layer

* fix kernels

* add xformers rocm patch

* update setup.py and add requirements-rocm.txt

* update engine args and ray utilities

* modify attention and quantization code

* formatting pt1

* manual merge and format fixes

* patch script

* rocm dockerfile

* simplify operations

* add custom DevFuncAttribute

* use custom function instead of torch.version

* it's 74 for amd

* no fp32 and fp16 for rocm

* clean up args

* get rid of torch

* partially support mistral for now

* fix formatting

* finishing touches

* revert back to rocm FA2

* why was that cpp

* fix: quant TP (#129)

* fix AWQ TP

* scaled activation for no parallel input

* formatting

* formatting

---------

Co-authored-by: doesntmatter <temp@ta5srf4.com>
Co-authored-by: 50h100a <all_awful@proton.me>
Co-authored-by: g4rg <139017707+g4rg@users.noreply.github.com>
AlpinDale added a commit that referenced this pull request Dec 12, 2023
* chore: tensor parallel refactors part 2 (#116)

* tensor parallel refactors part 2

* fix: max_model_len int to str comparison

* gptq stuff

* remove gptq

* prevent linking to the gptq kernels

* fix awq for llama

* fix linting

* fix typo

* empty tensor fix

* readme: update for dev branch

* parse torch.dtype properly (#119)

* fix: duplication in engine step (#120)

* fix: GH actions for dev branch

* feat: phi 1.5 support (#121)

* add phi 1.5 support

* add phi modeling code

* fix import

* feat: usage stats for OpenAI endpoint (#122)

* feat: add usage stats for openai streaming requests

* fix linting

* fix yapf

* chore: read from quantization_config (#123)

* fix: get_tensor instead of pysafeslice

* feat: awq for all models (#124)

* support AWQ for all model classes

* formatting

* feat: fused add RMSNorm kernels (#125)

* add fused kernels and llama support

* mistral support

* formatting

* fix: scheduler hang with long prompts (#126)

* allocate status class

* formatting

* fix: cpu sync delay fix (#127)

* cpu sync delay fix

* formatting

* fix: CPU OOM for large models (#128)

* feat: speedup compilation times by 3x (#130)

* refactor kernel builds

* correct the imports

* formatting

* chore: initialize model on GPU (#132)

* fix:  `SequenceOutputs` -> `SequenceOutput` (#133)

* fix:  ->

* formatting

* fix: blocktable definition (#134)

* blocktable

* formatting

* chore: attention rewrite + models (#135)

* properly disable log requests

* fix: make llama2 the default sep style (#137)

* feat: SqueezeLLM support (#140)

* add kernels

* fix squeezellm

* formatting

* feat: re-implement GPTQ (#141)

* bring back GPTQ

* fix awq and add gptq to registry

* formatting

* fix: rope duplication (#142)

* avoid duplication in rope init

* formatting

* chore: KoboldAI/koboldcpp updates (#48)

* chore: update kobold lite

* chore: koboldcpp compatibility updates

* fix: disable koboldcpp gen_cache on SSE endpoint to avoid memory leak on client abort

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case" (#149)

* readme: add dev branch notice

* add deprecation warning for ooba API

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case (#101)"

This reverts commit 99293aa.

* feat: AMD ROCm support (#95)

* feat: add ROCM kernels

* WIP integration into aphrodite

* fix compatibility layer

* fix kernels

* add xformers rocm patch

* update setup.py and add requirements-rocm.txt

* update engine args and ray utilities

* modify attention and quantization code

* formatting pt1

* manual merge and format fixes

* patch script

* rocm dockerfile

* simplify operations

* add custom DevFuncAttribute

* use custom function instead of torch.version

* it's 74 for amd

* no fp32 and fp16 for rocm

* clean up args

* get rid of torch

* partially support mistral for now

* fix formatting

* finishing touches

* revert back to rocm FA2

* why was that cpp

* fix: quant TP (#129)

* fix AWQ TP

* scaled activation for no parallel input

* formatting

* chore: rewrite InputMetadata (#143)

* stuff

* move to sampling_metadata

* stuff again (i forgot what i did last night

* calculate max_prompt_len locally

* slot_mapping expects an int.
SequenceGroupMetadata has data, not metadata. Naming is hard.

* make persistent metadata persist

* fix mirostat for logprobs and new persistence

* chore: KoboldAI/koboldcpp updates (#48)

* chore: update kobold lite

* chore: koboldcpp compatibility updates

* fix: disable koboldcpp gen_cache on SSE endpoint to avoid memory leak on client abort

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case" (#149)

* readme: add dev branch notice

* add deprecation warning for ooba API

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case (#101)"

This reverts commit 99293aa.

* feat: AMD ROCm support (#95)

* feat: add ROCM kernels

* WIP integration into aphrodite

* fix compatibility layer

* fix kernels

* add xformers rocm patch

* update setup.py and add requirements-rocm.txt

* update engine args and ray utilities

* modify attention and quantization code

* formatting pt1

* manual merge and format fixes

* patch script

* rocm dockerfile

* simplify operations

* add custom DevFuncAttribute

* use custom function instead of torch.version

* it's 74 for amd

* no fp32 and fp16 for rocm

* clean up args

* get rid of torch

* partially support mistral for now

* fix formatting

* finishing touches

* revert back to rocm FA2

* why was that cpp

* fix: quant TP (#129)

* fix AWQ TP

* scaled activation for no parallel input

* formatting

* formatting

---------

Co-authored-by: doesntmatter <temp@ta5srf4.com>
Co-authored-by: 50h100a <all_awful@proton.me>
Co-authored-by: g4rg <139017707+g4rg@users.noreply.github.com>

* feat: OpenAI chat completions templates (#138)

* alpaca template

* add chatml and fix alpaca

* chat completions api

* fix stuff

* rename templates

* formatting

* feat: re-write GPTQ and refactor exllama kernels (#152)

* refactor exllama kernels

* modify the linear layers

* awq fixes

* update gptq

* the useless quant

* skip bias init in models

* add acknowledgements and minor fixes

* fix mistral (finally)

* formatting

* things are fine, pylint

* abolish pylint

* pylintgit add .!

* feat: add prometheus production metrics (#154)

* feat: Mixtral 8x7B support (#155)

* add mixtral support

* add modeling code

* clean up code

* bump transformers

* remove megablocks and stk as deps for now

* fix this thing

* formatting

---------

Co-authored-by: g4rg <139017707+g4rg@users.noreply.github.com>
Co-authored-by: doesntmatter <temp@ta5srf4.com>
Co-authored-by: 50h100a <all_awful@proton.me>
AlpinDale added a commit that referenced this pull request Dec 19, 2023
* chore: tensor parallel refactors part 2 (#116)

* tensor parallel refactors part 2

* fix: max_model_len int to str comparison

* gptq stuff

* remove gptq

* prevent linking to the gptq kernels

* fix awq for llama

* fix linting

* fix typo

* empty tensor fix

* readme: update for dev branch

* parse torch.dtype properly (#119)

* fix: duplication in engine step (#120)

* fix: GH actions for dev branch

* feat: phi 1.5 support (#121)

* add phi 1.5 support

* add phi modeling code

* fix import

* feat: usage stats for OpenAI endpoint (#122)

* feat: add usage stats for openai streaming requests

* fix linting

* fix yapf

* chore: read from quantization_config (#123)

* fix: get_tensor instead of pysafeslice

* feat: awq for all models (#124)

* support AWQ for all model classes

* formatting

* feat: fused add RMSNorm kernels (#125)

* add fused kernels and llama support

* mistral support

* formatting

* fix: scheduler hang with long prompts (#126)

* allocate status class

* formatting

* fix: cpu sync delay fix (#127)

* cpu sync delay fix

* formatting

* fix: CPU OOM for large models (#128)

* feat: speedup compilation times by 3x (#130)

* refactor kernel builds

* correct the imports

* formatting

* chore: initialize model on GPU (#132)

* fix:  `SequenceOutputs` -> `SequenceOutput` (#133)

* fix:  ->

* formatting

* fix: blocktable definition (#134)

* blocktable

* formatting

* chore: attention rewrite + models (#135)

* properly disable log requests

* fix: make llama2 the default sep style (#137)

* feat: SqueezeLLM support (#140)

* add kernels

* fix squeezellm

* formatting

* feat: re-implement GPTQ (#141)

* bring back GPTQ

* fix awq and add gptq to registry

* formatting

* fix: rope duplication (#142)

* avoid duplication in rope init

* formatting

* chore: KoboldAI/koboldcpp updates (#48)

* chore: update kobold lite

* chore: koboldcpp compatibility updates

* fix: disable koboldcpp gen_cache on SSE endpoint to avoid memory leak on client abort

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case" (#149)

* readme: add dev branch notice

* add deprecation warning for ooba API

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case (#101)"

This reverts commit 99293aa.

* feat: AMD ROCm support (#95)

* feat: add ROCM kernels

* WIP integration into aphrodite

* fix compatibility layer

* fix kernels

* add xformers rocm patch

* update setup.py and add requirements-rocm.txt

* update engine args and ray utilities

* modify attention and quantization code

* formatting pt1

* manual merge and format fixes

* patch script

* rocm dockerfile

* simplify operations

* add custom DevFuncAttribute

* use custom function instead of torch.version

* it's 74 for amd

* no fp32 and fp16 for rocm

* clean up args

* get rid of torch

* partially support mistral for now

* fix formatting

* finishing touches

* revert back to rocm FA2

* why was that cpp

* fix: quant TP (#129)

* fix AWQ TP

* scaled activation for no parallel input

* formatting

* chore: rewrite InputMetadata (#143)

* stuff

* move to sampling_metadata

* stuff again (i forgot what i did last night

* calculate max_prompt_len locally

* slot_mapping expects an int.
SequenceGroupMetadata has data, not metadata. Naming is hard.

* make persistent metadata persist

* fix mirostat for logprobs and new persistence

* chore: KoboldAI/koboldcpp updates (#48)

* chore: update kobold lite

* chore: koboldcpp compatibility updates

* fix: disable koboldcpp gen_cache on SSE endpoint to avoid memory leak on client abort

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case" (#149)

* readme: add dev branch notice

* add deprecation warning for ooba API

* Revert "fix: correct auto ntk scaling_factor for 4k ctx case (#101)"

This reverts commit 99293aa.

* feat: AMD ROCm support (#95)

* feat: add ROCM kernels

* WIP integration into aphrodite

* fix compatibility layer

* fix kernels

* add xformers rocm patch

* update setup.py and add requirements-rocm.txt

* update engine args and ray utilities

* modify attention and quantization code

* formatting pt1

* manual merge and format fixes

* patch script

* rocm dockerfile

* simplify operations

* add custom DevFuncAttribute

* use custom function instead of torch.version

* it's 74 for amd

* no fp32 and fp16 for rocm

* clean up args

* get rid of torch

* partially support mistral for now

* fix formatting

* finishing touches

* revert back to rocm FA2

* why was that cpp

* fix: quant TP (#129)

* fix AWQ TP

* scaled activation for no parallel input

* formatting

* formatting

---------

Co-authored-by: doesntmatter <temp@ta5srf4.com>
Co-authored-by: 50h100a <all_awful@proton.me>
Co-authored-by: g4rg <139017707+g4rg@users.noreply.github.com>

* feat: OpenAI chat completions templates (#138)

* alpaca template

* add chatml and fix alpaca

* chat completions api

* fix stuff

* rename templates

* formatting

* feat: re-write GPTQ and refactor exllama kernels (#152)

* refactor exllama kernels

* modify the linear layers

* awq fixes

* update gptq

* the useless quant

* skip bias init in models

* add acknowledgements and minor fixes

* fix mistral (finally)

* formatting

* things are fine, pylint

* abolish pylint

* pylintgit add .!

* feat: add prometheus production metrics (#154)

* feat: Mixtral 8x7B support (#155)

* add mixtral support

* add modeling code

* clean up code

* bump transformers

* remove megablocks and stk as deps for now

* fix this thing

* formatting

* update readme

* chore: implement torch native layers (#156)

* implement torch native layers

* formatting

* feat: implement Deci-LM (#158)

* feat: implement deci-lm

* add to init and loader

* formatting

* fix: fractional gpus (#157)

* fix: llama keyerror (#159)

* fix: OpenAI chat - reference before assignment (#160)

* reference before assignment

* formatting

* fix: replace head_mapping with num_kv_heads (#161)

* fix: prompt logprobs (#162)

* fix: sliding window for mistral/mixtral (#163)

* chore: force pt for mixtral (#164)

* chore: force pt for mixtral

* formatting

* chore: implement lazy module loader for models (#165)

* implement ModelRegistry

* update mixtral import errors

* update model loader to use ModelRegistry

* whoops

* formatting

* fix: memory profiling (#166)

* fix peak memory profiling

* formatting

* fix formatting again

* fix: GPTQ warnings and exllama states (#171)

* fix the warnings for gptq

* minor fix

* typo

* chore: fix Python 3.8+ compatibility (#170)

* fix python3.9 compat

* fix python3.8 compat

* chore: include stop strings in output (#168)

* include stop str in output

* formatting

* typo

* typo again

* add to api

* yapf

* feat: mixtral HF with expert parallelism (#167)

* mixtral HF implementation

* update init and config

* fix gptq

* formatting

* simplify weight loading logic

* fixes

* Revert "fixes"

This reverts commit 75b864f.

* feat: CUDA graphs (#172)

* implement CUDA graph

* all-reduce memory usage

* torch already supports cuda graphs with envvar

* enforce eager mode for GPTQ

* pylint

* trailing whitespaces

* God save me from pylint

* yapf

* minor fix

* typo

* maybe a fix?

* some fixes, perhaps

* bring back cupy

* fix slot mapping dtype in kernels

* remove cupy again

* fix logger

* fix comments

* pylint

* yapf

* disable squeezellm for cuda graph

* add warning about mem usage

* typo in warning

* feat: add rope scaling to mixtral (#174)

* chore: make openai api key optional (#176)

---------

Co-authored-by: g4rg <139017707+g4rg@users.noreply.github.com>
Co-authored-by: doesntmatter <temp@ta5srf4.com>
Co-authored-by: 50h100a <all_awful@proton.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants