LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
discrete_random.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_LAYER_DISCRETE_RANDOM_HPP_INCLUDED
28 #define LBANN_LAYER_DISCRETE_RANDOM_HPP_INCLUDED
29 
31 #include "lbann/layers/layer.hpp"
32 #include "lbann/models/model.hpp"
33 #include "lbann/utils/random.hpp"
34 
35 namespace lbann {
36 
44 template <typename TensorDataType,
46  El::Device Dev = El::Device::CPU>
47 class discrete_random_layer : public data_type_layer<TensorDataType>
48 {
49  static_assert(Dev == El::Device::CPU,
50  "discrete random layer currently only supports CPU");
51  static_assert(T_layout == data_layout::DATA_PARALLEL,
52  "discrete random layer currently only supports DATA_PARALLEL");
53 
54 private:
56  std::vector<DataType> m_values;
57 
58 public:
60  std::vector<DataType> values,
61  std::vector<int> dims)
62  : data_type_layer<TensorDataType>(comm), m_values(values)
63  {
64  this->set_output_dims(dims);
65  }
66  discrete_random_layer* copy() const override
67  {
68  return new discrete_random_layer(*this);
69  }
70 
72 
74  template <typename ArchiveT>
75  void serialize(ArchiveT& ar);
76 
78 
79  std::string get_type() const override { return "discrete random"; }
80  data_layout get_data_layout() const override { return T_layout; }
81  El::Device get_device_allocation() const override { return Dev; }
82  bool can_run_inplace() const override { return false; }
83  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
84 
85 protected:
87  void write_specific_proto(lbann_data::Layer& proto) const final;
88 
89  friend class cereal::access;
90  discrete_random_layer() : discrete_random_layer(nullptr, {0}, {1}) {}
91 
92  void setup_dims() override
93  {
95  if (this->get_input_size() != (int)m_values.size()) {
96  LBANN_ERROR("input tensor dimensions don't match number of "
97  "values in discrete distribution");
98  }
99  }
100 
101  void fp_compute() override;
102 };
103 
104 #ifndef LBANN_DISCRETE_RANDOM_LAYER_INSTANTIATE
105 #define PROTO(T) \
106  extern template class discrete_random_layer<T, \
107  data_layout::DATA_PARALLEL, \
108  El::Device::CPU>
109 
110 #define LBANN_INSTANTIATE_CPU_HALF
112 #undef PROTO
113 #undef LBANN_INSTANTIATE_CPU_HALF
114 #endif // LBANN_DISCRETE_RANDOM_LAYER_INSTANTIATE
115 } // namespace lbann
116 
117 #endif // LBANN_LAYER_DISCRETE_RANDOM_HPP_INCLUDED
virtual void setup_dims()
Setup tensor dimensions Called by the &#39;setup&#39; function. If there are any input tensors, the base method sets all uninitialized output tensor dimensions equal to the first input tensor dimensions.
void fp_compute() override
Apply layer operation. Called by the &#39;forward_prop&#39; function. Given the input tensors, the output tensors are populated with computed values.
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
friend class cereal::access
#define LBANN_ERROR(...)
Definition: exception.hpp:37
discrete_random_layer(lbann_comm *comm, std::vector< DataType > values, std::vector< int > dims)
void serialize(ArchiveT &ar)
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
constexpr El::Device Device
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
std::string get_type() const override
Get the layer type&#39;s name.
int get_input_size(size_t input_index=0) const
Get input tensor size.
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
std::vector< DataType > m_values
void setup_dims() override
Setup tensor dimensions Called by the &#39;setup&#39; function. If there are any input tensors, the base method sets all uninitialized output tensor dimensions equal to the first input tensor dimensions.
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
void write_specific_proto(lbann_data::Layer &proto) const final
discrete_random_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Random output from discrete distribution.