-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2947 from alibaba/feature/sync
MNN:Sync: Sync Internal 2.9.2
- Loading branch information
Showing
111 changed files
with
33,434 additions
and
4,860 deletions.
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
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 was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,3 +1,45 @@ | ||
# 扩散模型 | ||
|
||
TODO | ||
## 模型支持与下载 | ||
|
||
[Download-runwayml/stable-diffusion-v1-5]: | ||
https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main | ||
[Download-IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-v0.1]: | ||
https://huggingface.co/IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-v0.1/tree/main | ||
|
||
## 模型转换 | ||
### 将Huggingface的Stable Diffusion模型 转为onnx模型 | ||
python export/onnx_export.py \ | ||
--model_path hf_sd_load_path \ | ||
--output_path onnx_save_path | ||
|
||
### 将onnx模型转为mnn模型 | ||
新建diffusion mnn模型文件夹,将转好的mnn文件放在该文件夹下。 | ||
./MNNConvert -f ONNX --modelFile onnx_save_path/text_encoder/model.onnx --MNNModel mnn_save_path/text_encoder.mnn --weightQuantBits 8 --bizCode biz | ||
./MNNConvert -f ONNX --modelFile onnx_save_path/unet/model.onnx --MNNModel mnn_save_path/unet.mnn --transformerFuse --weightQuantBits 8 --bizCode biz | ||
./MNNConvert -f ONNX --modelFile onnx_save_path/vae_decoder/model.onnx --keepInputFormat --MNNModel mnn_save_path/vae_decoder.mnn --weightQuantBits 8 --bizCode biz | ||
|
||
## 编译Diffusion Demo | ||
### Linux/MAC/Windows上 | ||
cmake .. -DMNN_BUILD_DIFFUSION=ON -DMNN_BUILD_OPENCV=ON -DMNN_IMGCODECS=ON -DMNN_OPENCL=ON -DMNN_SEP_BUILD=OFF -DMNN_SUPPORT_TRANSFORMER_FUSE=ON | ||
|
||
### Android上 | ||
cd project/android/build | ||
../build_64.sh -DMNN_BUILD_DIFFUSION=ON -DMNN_BUILD_OPENCV=ON -DMNN_IMGCODECS=ON -DMNN_OPENCL=ON -DMNN_SEP_BUILD=OFF -DMNN_SUPPORT_TRANSFORMER_FUSE=ON | ||
|
||
## 运行Diffusion Demo | ||
./diffusion_demo <resource_path> <model_type> <output_image_name> <input_text> | ||
其中,resource_path 就是mnn模型文件的路径,除了mnn文件,还需要 | ||
(1)将MNN目录transformers/diffusion/scheduler/alphas.txt文件拷贝到该文件夹下。 | ||
(2)针对stable-diffusion-v1-5模型需要将huggingfacetokenizer目录下merges.txt和vocab.json拷贝到该文件夹中。针对Taiyi-Stable-Diffusion模型需要将huggingfacetokenizer目录下vocab.txt拷贝到该文件夹中。 | ||
|
||
model_type是目前支持的两种diffusion模型的类别。如果是stable-diffusion-v1-5模型设为0,如果是Taiyi-Stable-Diffusion模型设为1。 | ||
|
||
output_image_name是生成图片的名字,默认图片位置在当前运行目录下。 | ||
|
||
input_text是文生图的prompt,如果是stable-diffusion-v1-5模型建议英文prompt,如果是Taiyi-Stable-Diffusion建议中文prompt。 | ||
|
||
运行指令例如: | ||
./diffusion_demo mnn_save_path 0 demo.jpg "a cute cat" | ||
./diffusion_demo mnn_save_path 1 demo.jpg "一只可爱的猫" | ||
|
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
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,19 @@ | ||
import MNN.llm as llm | ||
import sys | ||
|
||
if len(sys.argv) < 2: | ||
print('usage: python llm_example.py <path_to_model_config>') | ||
exit(1) | ||
|
||
config_path = sys.argv[1] | ||
# create model | ||
qwen = llm.create(config_path) | ||
# load model | ||
qwen.load() | ||
|
||
# response stream | ||
out = qwen.response('你好', True) | ||
print(out) | ||
|
||
out_ids = qwen.generate([151644, 872, 198, 108386, 151645, 198, 151644, 77091]) | ||
print(out_ids) |
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,76 @@ | ||
import _mnncengine.llm as _F | ||
|
||
class LLM(_F.LLM): | ||
def load(self, model_dir): | ||
''' | ||
load model from model_dir | ||
Parameters | ||
---------- | ||
model_dir : model path (split) or model name (single) | ||
Returns | ||
------- | ||
None | ||
Example: | ||
------- | ||
>>> llm.load('../qwen-1.8b-in4/conig.json') | ||
''' | ||
super.load(model_dir) | ||
|
||
def generate(self, input_ids): | ||
''' | ||
generate by input_ids | ||
Parameters | ||
---------- | ||
input_ids : input token ids, list of int | ||
Returns | ||
------- | ||
output_ids : output token ids, list of int | ||
Example: | ||
------- | ||
>>> input_ids = [151644, 872, 198, 108386, 151645, 198, 151644, 77091] | ||
>>> output_ids = qwen.generate(input_ids) | ||
''' | ||
return super.generate(input_ids) | ||
|
||
def response(self, prompt, stream = False): | ||
''' | ||
response by prompt | ||
Parameters | ||
---------- | ||
prompt : input prompt | ||
stream : generate string stream, default is False | ||
Returns | ||
------- | ||
res : output string | ||
Example: | ||
------- | ||
>>> res = qwen.response('Hello', True) | ||
''' | ||
return super.response(prompt, stream) | ||
|
||
def create(config_path): | ||
''' | ||
create LLM instance by `config.json` | ||
Parameters | ||
---------- | ||
config_path : config path or model path | ||
Returns | ||
------- | ||
llm : LLM instance | ||
Example: | ||
------- | ||
>>> qwen = llm.create('./qwen-1.8b-int4/config.json') | ||
''' | ||
return _F.create(config_path) |
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
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
Oops, something went wrong.