// Copyright (C) 2020 Intel Corporation // SPDX-License-Identifier: Apache-2.0 // #include #include #include #include #include #include #include #include #include #include #include // ! [ov:include] #include #include // ! [ov:include] bool ngraph_api_examples(std::shared_ptr node) { { // ! [ngraph:ports_example] // Let's suppose that node is opset8::Convolution operation // as we know opset8::Convolution has two input ports (data, weights) and one output port ov::Input data = node->input(0); ov::Input weights = node->input(1); ov::Output output = node->output(0); // Getting shape and type auto pshape = data.get_partial_shape(); auto el_type = data.get_element_type(); // Getting parent for input port ov::Output parent_output; parent_output = data.get_source_output(); // Another short way to get partent for output port parent_output = node->input_value(0); // Getting all consumers for output port auto consumers = output.get_target_inputs(); // ! [ngraph:ports_example] } { // ! [ngraph:shape_check] auto partial_shape = node->input(0).get_partial_shape(); // get zero input partial shape // Check that input shape rank is static if (!partial_shape.rank().is_static()) { return false; } auto rank_size = partial_shape.rank().get_length(); // Check that second dimension is not dynamic if (rank_size < 2 || partial_shape[1].is_dynamic()) { return false; } auto dim = partial_shape[1].get_length(); // ! [ngraph:shape_check] } return true; } // ! [ov:serialize] void serialize_example(std::shared_ptr f) { ov::pass::Manager manager; // Serialize ov::Function to before.svg file before transformation manager.register_pass("/path/to/file/before.svg"); // Run your transformation // manager.register_pass(); // Serialize ov::Function to after.svg file after transformation manager.register_pass("/path/to/file/after.svg"); manager.run_passes(f); } // ! [ov:serialize] // ! [ov:visualize] void visualization_example(std::shared_ptr f) { ov::pass::Manager manager; // Serialize ov::Function to IR manager.register_pass("/path/to/file/model.xml", "/path/to/file/model.bin"); manager.run_passes(f); } // ! [ov:visualize] void pass_manager_example1(std::shared_ptr f) { // ! [ngraph:disable_gelu] ov::pass::Manager manager; manager.register_pass(); auto pass_config = manager.get_pass_config(); pass_config->disable(); manager.run_passes(f); // ! [ngraph:disable_gelu] } void pass_manager_example2(std::shared_ptr f) { ov::pass::Manager manager; std::function)> transformation_callback; // ! [ngraph:disable_callback] // Set callback to particular transformation with specific condition auto pass_config = manager.get_pass_config(); pass_config->set_callback( [](const std::shared_ptr &node) -> bool { return node->input_value(0).get_shape().size() <= 5lu && node->input_value(0).get_shape().size() == node->get_output_shape(0).size(); }); // Update transformation to call callback ov::matcher_pass_callback callback = [=](ov::pass::pattern::Matcher &m) { auto node = m.get_match_root(); if (transformation_callback(node)) { return false; } // transformation code return false; }; // ! [ngraph:disable_callback] } void pass_manager_example3(std::shared_ptr f) { std::function)> transformation_callback; // ! [ngraph:disabled_by_default] // Example of disabled by default transformation { ov::pass::Manager manager; manager.register_pass(); manager.run_passes(f); } // Enable disabled by default transformation inside plugin { ov::pass::Manager manager; manager.register_pass(); auto pass_config = manager.get_pass_config(); pass_config->enable(); manager.run_passes(f); } // ! [ngraph:disabled_by_default] }