-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Sampler #3068
Open
huangzhengxiang
wants to merge
14
commits into
alibaba:master
Choose a base branch
from
Embedded-AI-Systems:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,548
−412
Open
New Sampler #3068
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
93656a3
first commit for Sampler
huangzhengxiang 656dc18
resolve conflicts
huangzhengxiang beea99f
update docs and remove penalize_ngram with penalty
huangzhengxiang 06736c3
fix the bug of reset
huangzhengxiang 215a21c
add android demo
huangzhengxiang f8d0ddc
Delete docs/transformers/optimizations.md
huangzhengxiang 69e1187
remove some commented lines:
huangzhengxiang 17329be
move _TemperatureSoftmaxto sampler
huangzhengxiang f960a9c
add android demo
huangzhengxiang 0be9c8e
debug android
huangzhengxiang f3dcb29
refactor llm project to retain the previous interfaces for backward c…
huangzhengxiang a16fc02
merge future code for perplexity
huangzhengxiang 028f09a
add perplexity and llm dataset processing, supports wikitext and shar…
huangzhengxiang a6c6298
update time performance experiments and visualization modules
huangzhengxiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,3 +360,9 @@ pymnn_build/ | |
|
||
# mnncompress generated | ||
MNN_compression_pb2.py | ||
|
||
# model path | ||
model/ | ||
|
||
# datasets | ||
datasets/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
## Change Log | ||
- [x] implement an independent `Sampler` Module. | ||
- [x] implement 8 individual basic samplers: `greedy`, `temperature`, `topK`, `topP`, `minP`, `tfs`, `typical`, `penalty`. (can be configured through config.json) | ||
- [x] implement `mixed` sampler, whose sampling order (Penalties -> top_k -> tfs_z -> typical_p -> top_p -> min_p -> temperature). one can change the samplers through configuring `mixed_samplers`. field in config.json | ||
- [x] implement `PromptLib` to enable `chat_demo` for all LLM. | ||
- [x] remove the `seq_len` control in `Llm` to `Sampler` and higher level modules to migrate design complexity. | ||
- [x] implement `Chat` to organize the workflow of a chatbot APP. | ||
- [x] change `#define FP16_QSCALE 0.25` in `CPUAttention` to ensure Llama3.2 FP16 correctness. | ||
- [x] `chat_demo` tested on ubuntu 22.04, android(including ARM64, ARM82, OPENCL backend). | ||
- [ ] `chat_demo` supports visual model tasks (support Qwen2-VL demo). | ||
- [ ] `transformers/llm/engine/android` gives an text-only chatbot app based on `Qwen2.5-1.5B-Instruct`. | ||
- [ ] `transformers/llm/engine/android` gives an text+image chatbot app based on `Qwen2-VL-2B-Instruct`. | ||
|
||
Motivation: | ||
1. Sampler: performance, variety, different user-preferrence. | ||
2. System Prompt: support history context, memory; few-shot generation (examples); role-play role profile. | ||
|
||
## TODO Lists | ||
|
||
### 0. Overall TODO Lists | ||
- [ ] test ShareGPT, VQA, Audio... | ||
- [ ] merge KV cache implementation | ||
- [ ] verify the possibility of hetereogeneous computing (CPU + opencl/...) | ||
- [ ] Kv cache + sampler | ||
|
||
### 1. Engineering TODO Lists | ||
- [x] llm-export convert Qwen2.5-1.5B-Instructx, Qwen2.5-3B-Instructx, Qwen2.5-7B-Instruct (Qwen2.5 language series) https://qwenlm.github.io/zh/blog/qwen2.5/ (<7B: 32K/8K, >=7B: 128K/8K) | ||
- [x] llm-export convert Llama-3.2-1B-Instructx, Llama-3.2-3B-Instruct (Llama-3.2 language series) https://ai.meta.com/blog/llama-3-2-connect-2024-vision-edge-mobile-devices/ (128K) | ||
- [ ] (optional) llm-export convert Llama-3.2-11B-Vision-Instruct (Llama-3.2 Vision series) | ||
- [x] (optional) llm-export convert Qwen2-VL-2B-Instruct, Qwen2-VL-7B-Instruct (Qwen2-VL series) | ||
- [ ] (optional) llm-export convert Qwen2-Audio-7B-Instruct (Qwen2 Audio series) | ||
- [x] implement `Chat` application in transformers/llm/app/chat. | ||
- [x] implement `LocalSampler` module. | ||
- [x] implement `PromptLib` module. | ||
- [x] implement `Llm` module. (before StateCacheManager's existence) | ||
- [x] build MNN on pc end. | ||
- [x] build MNN and run on android (fp32+fp16) | ||
- [ ] build and deploy an Llama-3.2-3B-Instruct model chatbot on Android cell phone, show the demo. | ||
- [ ] add RAG implementation. | ||
|
||
```bash | ||
python llmexport.py --path ../../../model/Qwen2.5-1.5B-Instruct/ --dst_path ../../../model/qwen2_5-1_5b-instruct-mnn/ --export mnn --quant_bit 4 --quant_block 128 | ||
|
||
python llmexport.py --path ../../../model/Qwen2-VL-2B-Instruct/ --dst_path ../../../model/qwen2-vl-2b-instruct-mnn/ --export mnn --quant_bit 4 --quant_block 128 | ||
|
||
python llmexport.py --path ../../../model/Llama-3.2-3B-Instruct/ --dst_path ../../../model/llama3_2-3b-instruct-mnn --export mnn --quant_bit 4 --quant_block 128 | ||
|
||
adb push ../../model/qwen2_5-1_5b-instruct-mnn/ /data/local/tmp/llm | ||
adb push ../../model/qwen2-vl-2b-instruct-mnn/ /data/local/tmp/llm | ||
adb push ../../model/llama3_2-3b-instruct-mnn/ /data/local/tmp/llm | ||
|
||
cd build/phone | ||
adb push chat_demo libllm.so libMNN_CL.so libMNN_Express.so libMNN.so tools/cv/libMNNOpenCV.so /data/local/tmp/llm | ||
``` | ||
|
||
|
||
### 2. Experiments TODO Lists | ||
- [ ] test `Chat` on ShareGPT datasets, measuring time and space | ||
- [ ] test VQA |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef PERPLEXITY_hpp | ||
#define PERPLEXITY_hpp | ||
|
||
#include <vector> | ||
#include <memory> | ||
#include <string> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <iostream> | ||
#include <streambuf> | ||
#include <functional> | ||
#include <unordered_map> | ||
#include <utility> | ||
|
||
#include <MNN/expr/Expr.hpp> | ||
#include <MNN/expr/Module.hpp> | ||
#include <MNN/expr/MathOp.hpp> | ||
#include <MNN/expr/NeuralNetWorkOp.hpp> | ||
#include <MNN/StateCacheManager.hpp> | ||
|
||
namespace MNN { | ||
namespace Transformer { | ||
class Llm; | ||
|
||
class MNN_PUBLIC PPLMeasurer { | ||
protected: | ||
Llm* mLlm; | ||
StateCacheManager* mStateCacheManager; | ||
std::vector<std::vector<int>> mPrompts; | ||
std::shared_ptr<StateCacheReference> mCandidate; | ||
int mStride, mMaxLen; | ||
void init(Llm* llm, StateCacheManager* manager, std::vector<std::vector<int>> prompts, int max_len, int stride); | ||
public: | ||
PPLMeasurer(Llm* llm, StateCacheManager* manager, std::vector<std::vector<int>> prompts, int max_len=2048, int stride=0); | ||
PPLMeasurer(Llm* llm, StateCacheManager* manager, std::vector<std::string> prompts, int max_len=2048, int stride=0); | ||
float perplexity_one(const std::vector<int>& prompt); | ||
std::vector<float> perplexity(); | ||
// prepare for another round of sampling | ||
// in the future, only reset its own. | ||
void reset(); | ||
void reset(int max_len, int stride); | ||
}; | ||
|
||
|
||
|
||
} // Transformer | ||
} // MNN | ||
|
||
|
||
#endif // SAMPLER_hpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个文件移到 transformer/llm 里面吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已删除