190eb3824785f991ba7d4663845bc182198fea03
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / api / CPP / mutable_data.hpp
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 #pragma once
19 #include "../C/mutable_data.h"
20 #include "primitive.hpp"
21 #include "memory.hpp"
22 #include <vector>
23
24 namespace cldnn {
25 /// @addtogroup cpp_api C++ API
26 /// @{
27 /// @addtogroup cpp_topology Network Topology
28 /// @{
29 /// @addtogroup cpp_primitives Primitives
30 /// @{
31
32 /// @brief Provides mutable data.
33 /// @details This primitive allows to pass data which can be written to during training.
34 /// For example, weights and biases for scoring networks.
35 /// This primitive can be also set as other primitive's output. In this case the underlying buffer will be the same in mutable_data and preceding primitive.
36 struct mutable_data : public primitive_base<mutable_data, CLDNN_PRIMITIVE_DESC(mutable_data)> {
37     CLDNN_DECLARE_PRIMITIVE(mutable_data)
38
39     /// @brief Enum type to specify function for data filling.
40     enum filler_type { no_fill, zero, one, xavier };
41
42     /// @brief Constructs mutable_data primitive.
43     /// @param id This primitive id.
44     /// @param mem @ref memory object which contains data.
45     /// @param filler_type @ref data filling function, default is zero
46     /// @note If memory is attached by memory::attach(), the attached buffer should be valid till network build.
47     mutable_data(const primitive_id& id, const memory& mem, filler_type fill_type = filler_type::no_fill)
48         : primitive_base(id, {}, padding()), mem(mem), fill_type(fill_type) {}
49
50     /// @brief Constructs mutable_data primitive with inputs.
51     /// @param id This primitive id.
52     /// @param input Vector of input primitives ids.
53     /// @param mem @ref memory object which contains data.
54     /// @note If memory is attached by memory::attach(), the attached buffer should be valid till network build.
55     /// @param filler_type @ref data filling function, default is zero
56     mutable_data(const primitive_id& id,
57                  const std::vector<primitive_id>& input,
58                  const memory& mem,
59                  filler_type fill_type = filler_type::no_fill)
60         : primitive_base(id, {input}, padding()), mem(mem), fill_type(fill_type) {}
61
62     /// @brief Constructs a copy from C API @CLDNN_PRIMITIVE_DESC{mutable_data}
63     explicit mutable_data(const dto* dto)
64         : primitive_base(dto), mem(dto->mem), fill_type(static_cast<filler_type>(dto->fill_type)) {
65         mem.retain();
66     }
67
68     /// @brief @ref memory object which contains data.
69     /// @note If memory is attached by memory::attach(), the attached buffer should be valid till network build.
70     memory mem;
71
72     /// @brief Specifies function which will be used to fill weights.
73     filler_type fill_type;
74
75 protected:
76     void update_dto(dto& dto) const override {
77         dto.mem = mem.get();
78         dto.fill_type = static_cast<cldnn_filler_type>(fill_type);
79     }
80 };
81 /// @}
82 /// @}
83 /// @}
84 }  // namespace cldnn