Files
openvino/docs/snippets/ov_model_with_state_infer.py
T
Ivan Tikhonovandcecilia peng a329611349 Support dynamic shapes in ReadValue/Assign ops and MakeStateful transformation (#20404)
* Support for dynamic shapes in ReadValue/Assign ops and MakeStateful transformation

* fix unit tests

* codestyle

* fix warning

* fix visitors test

* Fix lowLatency transformation, fix functional tests

* fix LowLatency2

* codestyle

* Fix CPU/GNA tests

* fix shape inference of assign operation

* fix python API

* fix mypy error

* Fix read_value operation version in python API for opsets7-13

* Update python API and fix tests

* set the same Varibable obj to Assign/ReadValue in py API, disable fp16 memory tests

* Update shape inference methods of Assign, ReadValue; update tests; fix codestyle

* fix python codestyle

* fix tests

* Resolve review comments

* codestyle

* resolve review comments

* codestyle

* Update ReadValue op in kaldi frontend, fix GNA tests

* fix Assign/ReadValue bindings, fix read_value test

* Apply suggestions from code review

Co-authored-by: cecilia peng <cecilia.peng@intel.com>

* Fix functional tests, add ov::Type support as an argument for read_value py binding

* temporary disable test_query_state_write_buffer fp16 tests

* Fix backward compatibility with the IRs with ReadValue/Assign ops generated via prev versions of OV; fix func tests

* fix python test, support type as str in read_value/assign bindings

* fix model binding

* fix pre-commit tests: variables vector cleanup

* fix integration with cpu changes

* codestyle

* fix warning

* Fix type/shape validation for read_value op

* fix a type_prop test for assign op

---------

Co-authored-by: cecilia peng <cecilia.peng@intel.com>
2023-11-21 20:59:36 +04:00

85 lines
2.6 KiB
Python

# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import logging as log
import numpy as np
import sys
import openvino as ov
from openvino.runtime import opset12 as ops
def main():
# 1. Load inference engine
log.info("Loading OpenVINO")
core = ov.Core()
#! [ov:model_create]
# 2. Creating ov.Model
input = ops.parameter([1, 1], dtype=np.float32, name="data")
init_const = ops.constant([[0]], dtype=np.float32)
read = ops.read_value(init_const, "variable0", "f32", [1, 1])
add = ops.add(input, read)
assign = ops.assign(add, "variable0")
add2 = ops.add(add, read)
result = ops.result(add2)
model = ov.Model(results=[result], sinks=[assign], parameters=[input], name="model")
#! [ov:model_create]
log.info("Loading network files")
# 3. Load network to CPU
compiled_model = core.compile_model(model, "CPU")
# 4. Create Infer Request
infer_request = compiled_model.create_infer_request()
# 5. Prepare inputs
input_tensors = []
for input in compiled_model.inputs:
input_tensors.append(infer_request.get_tensor(input))
# 6. Prepare outputs
output_tensors = []
for output in compiled_model.outputs:
output_tensors.append(infer_request.get_tensor(output))
# 7. Initialize memory state before starting
for state in infer_request.query_state():
state.reset()
#! [ov:part1]
# input data
input_data = np.arange(start=1, stop=7, dtype=np.float32)
log.info("Infer the first utterance")
for next_input in range(int(len(input_data)/2)):
infer_request.infer({"data" : np.asarray([input_data[next_input]]).reshape([1,1])})
# check states
states = infer_request.query_state()
if len(states) == 0:
log.error("Queried states are empty")
return -1
mstate = states[0].state
if not mstate:
log.error("Can't cast state to MemoryBlob")
return -1
state_buf = mstate.data
log.info(state_buf[0])
log.info("\nReset state between utterances...\n")
for state in infer_request.query_state():
state.reset()
log.info("Infer the second utterance")
for next_input in range(int(len(input_data)/2), len(input_data)):
infer_request.infer({0 : np.asarray([input_data[next_input]]).reshape([1,1])})
# check states
states = infer_request.query_state()
mstate = states[0].state
state_buf = mstate.data
log.info(state_buf[0])
#! [ov:part1]
log.info("Execution successful")
return 0