Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / mutable_data.cpp
1 /*
2 // Copyright (c) 2018 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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "mutable_data_inst.h"
19 #include "primitive_type_base.h"
20 #include "memory_impl.h"
21 #include <random>
22 #include "error_handler.h"
23 #include "json_object.h"
24
25 namespace cldnn
26 {
27 primitive_type_id mutable_data_type_id()
28 {
29     static primitive_type_base<mutable_data> instance;
30     return &instance;
31 }
32
33 namespace {
34     memory_impl::ptr attach_or_copy_data(network_impl& network, memory_impl& mem)
35     {
36         auto& engine = network.get_engine();
37         if (mem.is_allocated_by(engine))
38             return &mem;
39
40         memory_impl::ptr result = engine.allocate_memory(mem.get_layout());
41         mem_lock<char> src(mem);
42         mem_lock<char> dst(result);
43         std::copy(src.begin(), src.end(), dst.begin());
44         return result;
45     }
46 }
47
48 mutable_data_node::typed_program_node(const std::shared_ptr<mutable_data> dprim, program_impl& prog)
49     : parent(dprim, prog), mem(api_cast(dprim->mem.get()))
50 {
51     recalc_output_layout(false);
52     can_share_buffer(false);
53     fill_memory();
54 }
55
56 void mutable_data_node::attach_memory(memory_impl& new_mem, bool invalidate_users_if_changed)
57 {
58     mem = &new_mem;
59     recalc_output_layout(invalidate_users_if_changed);
60 }
61
62 void mutable_data_node::fill_memory()
63 {
64     auto prim = get_primitive();
65
66     if (prim->fill_type == mutable_data::filler_type::no_fill)
67         return;
68
69     auto memory = mem.get();
70     auto layout = memory->get_layout();
71     if (layout.data_type != data_types::f32)
72         CLDNN_ERROR_MESSAGE(id(), "only f32 data types can be filled");
73
74     switch (prim->fill_type)
75     {
76     case mutable_data::filler_type::zero:
77         fill_memory_constant(0.f);
78         break;
79     case mutable_data::filler_type::one:
80         fill_memory_constant(1.f);
81         break;
82     case mutable_data::filler_type::xavier:
83         fill_memory_xavier();
84         break;
85     default:
86         break;
87     }
88 }
89
90 void mutable_data_node::fill_memory_xavier()
91 {
92     auto memory = mem.get();
93     auto layout = memory->get_layout();
94     auto n = layout.count() / layout.size.batch[0];
95     float scale = float(sqrt(3.0f / (float)n));
96     std::default_random_engine generator(0);
97
98     mem_lock<float> lock(mem);
99     auto out_ptr = lock.begin();
100     std::uniform_real_distribution<float> distribution(-scale, scale);
101     for (uint32_t i = 0; i < (uint32_t)layout.count(); i++)
102         out_ptr[i] = distribution(generator);
103 }
104
105 void mutable_data_node::fill_memory_constant(float value)
106 {
107     auto memory = mem.get();
108     auto layout = memory->get_layout();
109     mem_lock<float> lock(mem);
110     auto out_ptr = lock.begin();
111
112     for (uint32_t i = 0; i < (uint32_t)layout.count(); i++)
113         out_ptr[i] = value;
114 }
115
116 std::string mutable_data_inst::to_string(mutable_data_node const& node)
117 {
118     auto node_info = node.desc_to_json();
119
120     std::stringstream primitive_description;
121     
122     node_info->dump(primitive_description);
123     return primitive_description.str();
124 }
125
126 mutable_data_inst::typed_primitive_inst(network_impl& network, mutable_data_node const& node)
127     : parent(network, node, *attach_or_copy_data(network, node.get_attached_memory()))
128 {
129 }
130
131 }