Commit 3b905a5e authored by liucong's avatar liucong
Browse files

修改部分文档

parent 7afec310
...@@ -84,9 +84,7 @@ for(int i=0;i<50;++i) ...@@ -84,9 +84,7 @@ for(int i=0;i<50;++i)
在GPT2::Inference()函数具体实现了GPT-2模型的推理过程,主要做如下处理: 在GPT2::Inference()函数具体实现了GPT-2模型的推理过程,主要做如下处理:
1.reshape操作 1.设置输入shape并执行推理
2.执行推理
```c++ ```c++
long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id) long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id)
...@@ -99,12 +97,9 @@ long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id ...@@ -99,12 +97,9 @@ long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id
input[0][j] = input_id[j]; input[0][j] = input_id[j];
} }
// 设置输入shape并执行reshape // 设置输入shape
std::vector<std::vector<std::size_t>> inputShapes; std::vector<std::vector<std::size_t>> inputShapes;
inputShapes.push_back({1,input_id.size()}); inputShapes.push_back({1,input_id.size()});
std::unordered_map<std::string, std::vector<std::size_t>> inputShapeMap;
inputShapeMap[inputName] = inputShapes[0];
migraphx::reshape2(net,inputShapeMap);
// 输入数据 // 输入数据
migraphx::parameter_map inputData; migraphx::parameter_map inputData;
...@@ -123,9 +118,7 @@ long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id ...@@ -123,9 +118,7 @@ long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id
} }
``` ```
1.reshape操作,因为GPT-2属于生成式语言模型,输入的shape一直在变化,所以在模型推理前需要reshape的操作。shape的大小根据输入数据的shape决定,之后执行reshape操作,从而使模型可以根据不同的shape进行推理。 1.执行推理,GPT-2模型的推理结果results是一个std::vector< migraphx::argument >类型,包含一个输出,所以result = results[0]。result中一共包含了input_id.size() * 22557个概率值,其中,input_id.size()代表输入数据的长度,22557代表了词汇表中词的数量。
2.执行推理,GPT-2模型的推理结果results是一个std::vector< migraphx::argument >类型,包含一个输出,所以result = results[0]。result中一共包含了input_id.size() * 22557个概率值,其中,input_id.size()代表输入数据的长度,22557代表了词汇表中词的数量。
## 数据后处理 ## 数据后处理
......
...@@ -51,20 +51,16 @@ input_ids = np.expand_dims(input_ids, axis=0) ...@@ -51,20 +51,16 @@ input_ids = np.expand_dims(input_ids, axis=0)
完成数据预处理后,就可以执行模型推理。推理过程主要做如下处理: 完成数据预处理后,就可以执行模型推理。推理过程主要做如下处理:
1.reshape操作 1.设置最大输入shape
2.循环推理 2.循环推理
```Python ```Python
# 设置最大输入shape # 设置最大输入shape
maxInput={"input":[1,1024]} maxInput={"input":[1,1000]}
# 模型推理 # 模型推理
for _ in range(max_len): for _ in range(max_len):
# 执行reshape
inputShapes = [input_ids.shape[0], input_ids.shape[1]]
inputShapeMap={inputName:inputShapes}
migraphx.reshape2(model, inputShapeMap)
# 推理 # 推理
result = model.run({inputName: migraphx.argument(input_ids)}) result = model.run({inputName: migraphx.argument(input_ids)})
...@@ -83,7 +79,7 @@ for _ in range(max_len): ...@@ -83,7 +79,7 @@ for _ in range(max_len):
``` ```
1.reshape操作,因为GPT-2属于生成式模型,输入的shape一直在变化,所以在模型推理前需要进行reshape操作。首先,设置一个最大的输入shape,maxInput={"input":[1,1024]},限定模型输入shape的范围。其次,根据每次输入shape的具体大小,执行migraphx.reshape2()操作,从而使模型可以根据不同的shape进行推理。 1.设置最大输入shape,因为GPT-2属于生成式模型,输入的shape一直在变化,所以在模型推理前设置一个最大的输入shape,maxInput={"input":[1,1000]},限定模型输入shape的范围。
2.循环推理,GPT-2模型不像其他模型一样只需要执行一次推理,而是需要循环执行多次推理才能完成。首先,模型推理限定在for循环中,将输入数据input_ids,输入到model.run({...})中执行推理,生成一个token的id。其次,将推理结果拼接到输入数据input_ids中,执行下一次循环。最后,当循环结束或者生成的词为[SEP]结束标志符时,完成GPT-2模型的整体推理。如下图所示,为GPT-2模型的一次完整推理过程。 2.循环推理,GPT-2模型不像其他模型一样只需要执行一次推理,而是需要循环执行多次推理才能完成。首先,模型推理限定在for循环中,将输入数据input_ids,输入到model.run({...})中执行推理,生成一个token的id。其次,将推理结果拼接到输入数据input_ids中,执行下一次循环。最后,当循环结束或者生成的词为[SEP]结束标志符时,完成GPT-2模型的整体推理。如下图所示,为GPT-2模型的一次完整推理过程。
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment