clDNN
chapter_6.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 <../api/CPP/convolution.hpp>
25 #include <iostream>
26 #include <chrono>
27 
28 #include "helper_functions.h"
29 
44 using namespace cldnn;
45 
46 void chapter_6(engine& engine)
47 {
48  std::cout << std::endl << "-- Chapter 6 --" << std::endl;
49 
50  // We are going to implement a simple network with Convolution layer:
51  // input: 227x227 with 3 feature maps
52  // filter size: 3x3
53  // stride: 1,1
54  // offset: 0,0
55  //
56  // We use this code as an helper to test our new convolution kernel
57 
58  // Create input memory for convolution layer
59  memory input_prim = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 3, 227, 227 } });
60  memory weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 3, 3, 3 } });
61  memory biases = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 1, 1 } });
62 
63  set_values(input_prim, get_simple_data<float>(input_prim));
64  set_values(weights, get_simple_data<float>(weights));
65  set_values(biases, get_simple_data<float>(biases));
66 
67  // Create a topology with a simple Convolution layer
69  input_layout("conv_input", input_prim.get_layout()),
70  data("conv_weights", weights),
71  data("conv_biases", biases),
73  "conv",
74  "conv_input",
75  { "conv_weights" },
76  { "conv_biases" })
77  );
78 
79  build_options build_opt;
80  // Optimize_data flag can change weights and outputs layouts. Let take a look at
81  // Set option to optimize data.
82  build_opt.set_option(build_option::optimize_data(true));
83 
84  network network(engine, topology, build_opt);
85 
86  // Set input.
87  network.set_input_data("conv_input", input_prim);
88  // Ready to go.
89  auto outputs = network.execute();
90 
91  // Get primitives that were executed and their events needed for profiling
92  auto executed_primitives = network.get_executed_primitives();
93 
94  // Now, we want to check what is the time of execution of each primitive:
95  std::vector<cldnn::instrumentation::profiling_info> profiling_table;
96  for (auto& p : executed_primitives)
97  {
98  profiling_table.push_back({ p.first, p.second.get_profiling_info() });
99  }
100 
101  // We have table of profiling metrics.
102  for (auto& p : profiling_table)
103  {
104  std::cout << p.name << ":" << std::endl;
105  for (auto& q : p.intervals)
106  {
107  std::cout << "\t" << q.name << ": " << std::chrono::duration_cast<std::chrono::duration<double, std::chrono::milliseconds::period>>(q.value->value()).count()
108  << " milliseconds" << std::endl;
109  }
110  }
111 }
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
std::map< primitive_id, event > get_executed_primitives() const
Returns the list of event for the primitives that were executed in network.
Definition: network.hpp:206
void set_option(std::shared_ptr< const build_option > opt)
Adds or replace option to the options list.
Definition: program.hpp:403
Provides input data to topology.
Definition: data.hpp:36
User selected list of program outputs.
Provides input layout for a data to be passed later to network.
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
Executable network allocated from program.
Definition: network.hpp:59
const layout & get_layout() const
Associated layout.
Definition: memory.hpp:114
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).
Performs forward spatial convolution with weight sharing. Also supports built-in Relu cldnn_activatio...
Definition: convolution.hpp:34