Update Variable State doc (#16358)
* Update Variable State doc * Fixed build * Try to fix build * Remove error * Fixed doc * Fixed links * Try to fix doc
This commit is contained in:
1 parent
bc15596c9e
commit
2739a01d64
9 files changed
+148
-166
No files matched your search
@@ -1,115 +0,0 @@
|
||||
// Copyright (C) 2018-2020 Intel Corporation
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <ie_core.hpp>
|
||||
|
||||
using namespace InferenceEngine;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
// --------------------------- 1. Load inference engine -------------------------------------
|
||||
std::cout << "Loading Inference Engine" << std::endl;
|
||||
Core ie;
|
||||
|
||||
// 2. Read a model in OpenVINO Intermediate Representation (.xml and .bin files) or ONNX (.onnx file) format
|
||||
std::cout << "Loading network files" << std::endl;
|
||||
CNNNetwork network;
|
||||
network = ie.ReadNetwork(std::string("c:\\work\\git\\github_dldt3\\openvino\\model-optimizer\\summator.xml"));
|
||||
network.setBatchSize(1);
|
||||
|
||||
// 3. Load network to CPU
|
||||
ExecutableNetwork executableNet = ie.LoadNetwork(network, "CPU");
|
||||
// 4. Create Infer Request
|
||||
InferRequest inferRequest = executableNet.CreateInferRequest();
|
||||
|
||||
// 5. Prepare inputs
|
||||
ConstInputsDataMap cInputInfo = executableNet.GetInputsInfo();
|
||||
std::vector<Blob::Ptr> ptrInputBlobs;
|
||||
for (const auto& input : cInputInfo) {
|
||||
ptrInputBlobs.push_back(inferRequest.GetBlob(input.first));
|
||||
}
|
||||
InputsDataMap inputInfo;
|
||||
inputInfo = network.getInputsInfo();
|
||||
for (auto &item : inputInfo) {
|
||||
Precision inputPrecision = Precision::FP32;
|
||||
item.second->setPrecision(inputPrecision);
|
||||
}
|
||||
|
||||
// 6. Prepare outputs
|
||||
std::vector<Blob::Ptr> ptrOutputBlobs;
|
||||
ConstOutputsDataMap cOutputInfo = executableNet.GetOutputsInfo();
|
||||
for (const auto& output : cOutputInfo) {
|
||||
ptrOutputBlobs.push_back(inferRequest.GetBlob(output.first));
|
||||
}
|
||||
|
||||
// 7. Initialize memory state before starting
|
||||
for (auto &&state : inferRequest.QueryState()) {
|
||||
state.Reset();
|
||||
}
|
||||
|
||||
//! [part1]
|
||||
// input data
|
||||
std::vector<float> data = { 1,2,3,4,5,6};
|
||||
// infer the first utterance
|
||||
for (size_t next_input = 0; next_input < data.size()/2; next_input++) {
|
||||
MemoryBlob::Ptr minput = as<MemoryBlob>(ptrInputBlobs[0]);
|
||||
auto minputHolder = minput->wmap();
|
||||
|
||||
std::memcpy(minputHolder.as<void *>(),
|
||||
&data[next_input],
|
||||
sizeof(float));
|
||||
|
||||
inferRequest.Infer();
|
||||
// check states
|
||||
auto states = inferRequest.QueryState();
|
||||
if (states.empty()) {
|
||||
throw std::runtime_error("Queried states are empty");
|
||||
}
|
||||
auto mstate = as<MemoryBlob>(states[0].GetState());
|
||||
if (mstate == nullptr) {
|
||||
throw std::runtime_error("Can't cast state to MemoryBlob");
|
||||
}
|
||||
auto state_buf = mstate->rmap();
|
||||
float * state =state_buf.as<float*>();
|
||||
std::cout << state[0] << "\n";
|
||||
}
|
||||
|
||||
// resetting state between utterances
|
||||
std::cout<<"Reset state\n";
|
||||
for (auto &&state : inferRequest.QueryState()) {
|
||||
state.Reset();
|
||||
}
|
||||
|
||||
// infer the second utterance
|
||||
for (size_t next_input = data.size()/2; next_input < data.size(); next_input++) {
|
||||
MemoryBlob::Ptr minput = as<MemoryBlob>(ptrInputBlobs[0]);
|
||||
auto minputHolder = minput->wmap();
|
||||
|
||||
std::memcpy(minputHolder.as<void *>(),
|
||||
&data[next_input],
|
||||
sizeof(float));
|
||||
|
||||
inferRequest.Infer();
|
||||
// check states
|
||||
auto states = inferRequest.QueryState();
|
||||
auto mstate = as<MemoryBlob>(states[0].GetState());
|
||||
auto state_buf = mstate->rmap();
|
||||
float * state =state_buf.as<float*>();
|
||||
std::cout << state[0] << "\n";
|
||||
}
|
||||
//! [part1]
|
||||
}
|
||||
catch (const std::exception &error) {
|
||||
std::cerr << error.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Unknown/internal exception happened" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cerr << "Execution successful" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// Copyright (C) 2018-2020 Intel Corporation
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "openvino/op/util/variable.hpp"
|
||||
#include "openvino/openvino.hpp"
|
||||
#include "openvino/opsets/opset11.hpp"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
// --------------------------- 1. Load inference engine -------------------------------------
|
||||
std::cout << "Loading OpenVINO" << std::endl;
|
||||
ov::Core core;
|
||||
|
||||
//! [model_create]
|
||||
auto arg = std::make_shared<ov::opset11::Parameter>(ov::element::f32, ov::Shape{1, 1});
|
||||
auto init_const = ov::opset11::Constant::create(ov::element::f32, ov::Shape{1, 1}, {0});
|
||||
|
||||
// The ReadValue/Assign operations must be used in pairs in the model.
|
||||
// For each such a pair, its own variable object must be created.
|
||||
const std::string variable_name("variable0");
|
||||
auto variable = std::make_shared<ov::op::util::Variable>(
|
||||
ov::op::util::VariableInfo{ov::PartialShape::dynamic(), ov::element::dynamic, variable_name});
|
||||
|
||||
// Creating ov::Model
|
||||
auto read = std::make_shared<ov::opset11::ReadValue>(init_const, variable);
|
||||
std::vector<std::shared_ptr<ov::Node>> args = {arg, read};
|
||||
auto add = std::make_shared<ov::opset11::Add>(arg, read);
|
||||
auto assign = std::make_shared<ov::opset11::Assign>(add, variable);
|
||||
auto add2 = std::make_shared<ov::opset11::Add>(add, read);
|
||||
auto res = std::make_shared<ov::opset11::Result>(add2);
|
||||
|
||||
auto model =
|
||||
std::make_shared<ov::Model>(ov::ResultVector({res}), ov::SinkVector({assign}), ov::ParameterVector({arg}));
|
||||
//! [model_create]
|
||||
|
||||
// 2. Read a model in OpenVINO Intermediate Representation (.xml and .bin files) or ONNX (.onnx file) format
|
||||
std::cout << "Loading network files" << std::endl;
|
||||
|
||||
// 3. Load network to CPU
|
||||
ov::CompiledModel compiled_model = core.compile_model(model, "CPU");
|
||||
// 4. Create Infer Request
|
||||
ov::InferRequest infer_request = compiled_model.create_infer_request();
|
||||
|
||||
// 5. Prepare inputs
|
||||
|
||||
std::vector<ov::Tensor> input_tensors;
|
||||
for (const auto& input : compiled_model.inputs()) {
|
||||
input_tensors.emplace_back(infer_request.get_tensor(input));
|
||||
}
|
||||
|
||||
// 6. Prepare outputs
|
||||
std::vector<ov::Tensor> output_tensors;
|
||||
for (const auto& output : compiled_model.outputs()) {
|
||||
output_tensors.emplace_back(infer_request.get_tensor(output));
|
||||
}
|
||||
|
||||
// 7. Initialize memory state before starting
|
||||
for (auto&& state : infer_request.query_state()) {
|
||||
state.reset();
|
||||
}
|
||||
|
||||
//! [part1]
|
||||
// input data
|
||||
std::vector<float> data = {1, 2, 3, 4, 5, 6};
|
||||
// infer the first utterance
|
||||
for (size_t next_input = 0; next_input < data.size() / 2; next_input++) {
|
||||
auto minput = input_tensors[0];
|
||||
|
||||
std::memcpy(minput.data(), &data[next_input], sizeof(float));
|
||||
|
||||
infer_request.infer();
|
||||
// check states
|
||||
auto states = infer_request.query_state();
|
||||
if (states.empty()) {
|
||||
throw std::runtime_error("Queried states are empty");
|
||||
}
|
||||
auto mstate = states[0].get_state();
|
||||
if (!mstate) {
|
||||
throw std::runtime_error("Can't cast state to MemoryBlob");
|
||||
}
|
||||
float* state = mstate.data<float>();
|
||||
std::cout << state[0] << "\n";
|
||||
}
|
||||
|
||||
// resetting state between utterances
|
||||
std::cout << "Reset state\n";
|
||||
for (auto&& state : infer_request.query_state()) {
|
||||
state.reset();
|
||||
}
|
||||
|
||||
// infer the second utterance
|
||||
for (size_t next_input = data.size() / 2; next_input < data.size(); next_input++) {
|
||||
auto minput = input_tensors[0];
|
||||
|
||||
std::memcpy(minput.data(), &data[next_input], sizeof(float));
|
||||
|
||||
infer_request.infer();
|
||||
// check states
|
||||
auto states = infer_request.query_state();
|
||||
auto mstate = states[0].get_state();
|
||||
float* state = mstate.data<float>();
|
||||
std::cout << state[0] << "\n";
|
||||
}
|
||||
//! [part1]
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << std::endl;
|
||||
return 1;
|
||||
} catch (...) {
|
||||
std::cerr << "Unknown/internal exception happened" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cerr << "Execution successful" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user