2 // Copyright (c) 2018 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "mutable_data_inst.h"
19 #include "primitive_type_base.h"
20 #include "memory_impl.h"
22 #include "error_handler.h"
23 #include "json_object.h"
27 primitive_type_id mutable_data_type_id()
29 static primitive_type_base<mutable_data> instance;
34 memory_impl::ptr attach_or_copy_data(network_impl& network, memory_impl& mem)
36 auto& engine = network.get_engine();
37 if (mem.is_allocated_by(engine))
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());
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()))
51 recalc_output_layout(false);
52 can_share_buffer(false);
56 void mutable_data_node::attach_memory(memory_impl& new_mem, bool invalidate_users_if_changed)
59 recalc_output_layout(invalidate_users_if_changed);
62 void mutable_data_node::fill_memory()
64 auto prim = get_primitive();
66 if (prim->fill_type == mutable_data::filler_type::no_fill)
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");
74 switch (prim->fill_type)
76 case mutable_data::filler_type::zero:
77 fill_memory_constant(0.f);
79 case mutable_data::filler_type::one:
80 fill_memory_constant(1.f);
82 case mutable_data::filler_type::xavier:
90 void mutable_data_node::fill_memory_xavier()
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);
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);
105 void mutable_data_node::fill_memory_constant(float value)
107 auto memory = mem.get();
108 auto layout = memory->get_layout();
109 mem_lock<float> lock(mem);
110 auto out_ptr = lock.begin();
112 for (uint32_t i = 0; i < (uint32_t)layout.count(); i++)
116 std::string mutable_data_inst::to_string(mutable_data_node const& node)
118 auto node_info = node.desc_to_json();
120 std::stringstream primitive_description;
122 node_info->dump(primitive_description);
123 return primitive_description.str();
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()))