clDNN
chapter_5.cpp
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #include <../api/CPP/cldnn_defs.h>
18 #include <../api/CPP/engine.hpp>
19 #include <../api/CPP/input_layout.hpp>
20 #include <../api/CPP/memory.hpp>
21 #include <../api/CPP/data.hpp>
22 #include <../api/CPP/topology.hpp>
23 #include <../api/CPP/network.hpp>
24 #include <iostream>
25 #include <chrono>
26 
27 #include "helper_functions.h"
28 
38 using namespace cldnn;
39 
40 
41 void chapter_5(engine& engine, topology& topology)
42 {
43  std::cout << std::endl << "-- Chapter 5 --" << std::endl;
44 
45  build_options build_opt;
46  // Optimize_data flag can change weights and outputs layouts. Let take a look at
47  // final result and fc weights.
48  build_opt.set_option(build_option::outputs(topology.get_primitive_ids()));
49  // Set option to optimize data.
50  build_opt.set_option(build_option::optimize_data(true));
51  network network(engine, topology, build_opt);
52  memory input_prim = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 3, 1 } });
53  set_values(input_prim, { -3.0f, -2.0f, 2.5f });
54  // Set input.
55  network.set_input_data("input", input_prim);
56  // Ready to go.
57  auto outputs = network.execute();
58 
59  for (auto& it : outputs)
60  {
61  // Print id and output values.
62  std::cout << "optimized " << it.first << std::endl;
63  auto mem_pointer = it.second.get_memory().pointer<float>();
64  for (auto i : mem_pointer)
65  {
66  std::cout << i << " ";
67  }
68  std::cout << std::endl;
69  }
70 
71  // Now, we want to check what is the time of execution of each primitive:
72  std::vector<cldnn::instrumentation::profiling_info> profiling_table;
73  for (auto& p : outputs)
74  {
75  profiling_table.push_back({ p.first, p.second.get_event().get_profiling_info() });
76  }
77 
78  // We have table of profiling metrics.
79  for (auto& p : profiling_table)
80  {
81  std::cout << p.name << ":" << std::endl;
82  for (auto& q : p.intervals)
83  {
84  std::cout << "\t" << q.name << ": " << std::chrono::duration_cast<std::chrono::duration<double, std::chrono::nanoseconds::period>>(q.value->value()).count()
85  << " nanoseconds" << std::endl;
86  }
87  }
88 }
Represents program build options list.
Definition: program.hpp:399
std::map< primitive_id, network_output > execute(const std::vector< event > &dependencies={}) const
Executes network and returns the list of network_output.
Definition: network.hpp:246
void set_input_data(const primitive_id &id, const memory &mem) const
Provides memory for input_layout primitives defined by user in source topology.
Definition: network.hpp:122
void set_option(std::shared_ptr< const build_option > opt)
Adds or replace option to the options list.
Definition: program.hpp:403
User selected list of program outputs.
Network topology to be defined by user.
Definition: topology.hpp:33
static memory allocate(const engine &engine, const layout &layout)
Allocate memory on engine using specified layout.
Definition: memory.hpp:50
the most common format for activations in clDNN.
Definition: tensor.hpp:81
static std::shared_ptr< const build_option > outputs(const std::vector< primitive_id > &outs)
User selected list of program outputs.
Executable network allocated from program.
Definition: network.hpp:59
Represents clDNN engine object.
Definition: engine.hpp:110
Represents buffer with particular layout.
Definition: memory.hpp:42
static std::shared_ptr< const build_option > optimize_data(bool enable=false)
Enable implicit reordering for user inputs (default: false).