LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
reshape.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 RESHAPE_HPP_INCLUDED
28 #define RESHAPE_HPP_INCLUDED
29 
32 
33 namespace lbann {
34 
41 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
42 class reshape_layer : public data_type_layer<TensorDataType>
43 {
44 public:
45  reshape_layer(lbann_comm* comm, std::vector<int> dims)
46  : data_type_layer<TensorDataType>(comm)
47  {
48  this->set_output_dims(dims);
49  }
50  reshape_layer* copy() const override { return new reshape_layer(*this); }
51 
53 
55  template <typename ArchiveT>
56  void serialize(ArchiveT& ar);
57 
59 
60  std::string get_type() const override { return "reshape"; }
61  data_layout get_data_layout() const override { return T_layout; }
62  El::Device get_device_allocation() const override { return Dev; }
63  bool can_run_inplace() const override { return false; }
64  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
65 
66 protected:
68  void write_specific_proto(lbann_data::Layer& proto) const final;
69 
70  friend class cereal::access;
71  reshape_layer() : reshape_layer(nullptr, {1}) {}
72 
73  void setup_dims() override
74  {
76 
77  const auto& input_dims = this->get_input_dims();
78  auto output_dims = this->get_output_dims();
79 
80  // Determine any unspecified dimensions
81  int unspecified_dim = -1;
82  for (size_t dim = 0; dim < output_dims.size(); ++dim) {
83  if (output_dims[dim] <= 0) {
84  if (unspecified_dim < 0) {
85  unspecified_dim = dim;
86  }
87  output_dims[dim] = 1;
88  }
89  }
90  if (unspecified_dim >= 0) {
91  const auto specified_size = get_linear_size(output_dims);
92  output_dims[unspecified_dim] = this->get_input_size() / specified_size;
93  this->set_output_dims(output_dims);
94  }
95 
96  // Check that reshape is valid
97  if (this->get_input_size() != this->get_output_size()) {
98  std::stringstream err;
99  err << "input tensor dimensions (";
100  for (size_t i = 0; i < input_dims.size(); ++i) {
101  err << (i > 0 ? " x " : "") << input_dims[i];
102  }
103  err << ") do not match output tensor dimensions (";
104  for (size_t i = 0; i < output_dims.size(); ++i) {
105  err << (i > 0 ? "x" : "") << output_dims[i];
106  }
107  err << ")";
108  err << " in " << this->get_type() << " layer "
109  << "\"" << this->get_name() << "\"";
110  LBANN_ERROR(err.str());
111  }
112  }
113 
114  void fp_setup_outputs() override
115  {
116  El::LockedView(this->get_activations(), this->get_prev_activations());
117  }
119  {
120  El::LockedView(this->get_error_signals(), this->get_prev_error_signals());
121  }
122  void fp_compute() override {}
123  void bp_compute() override {}
124 };
125 
126 #ifndef LBANN_RESHAPE_LAYER_INSTANTIATE
127 #define PROTO_DEVICE(T, Device) \
128  extern template class reshape_layer<T, data_layout::DATA_PARALLEL, Device>; \
129  extern template class reshape_layer<T, data_layout::MODEL_PARALLEL, Device>
130 
132 #undef PROTO_DEVICE
133 #endif // LBANN_RESHAPE_LAYER_INSTANTIATE
134 
135 } // namespace lbann
136 
137 #endif // RESHAPE_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 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.
Definition: reshape.hpp:73
friend class cereal::access
Definition: reshape.hpp:70
auto get_linear_size(std::vector< T > const &dims)
Definition: dim_helpers.hpp:59
#define LBANN_ERROR(...)
Definition: exception.hpp:37
int get_output_size(size_t output_index=0) const
Get output tensor size.
void fp_setup_outputs() override
Setup output tensors. Called by the &#39;forward_prop&#39; function. Each output tensor is resized to match t...
Definition: reshape.hpp:114
std::vector< int > get_input_dims(size_t input_index=0) const
Get input tensor dimensions.
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: reshape.hpp:64
constexpr El::Device Device
OutputAbsDistMatrixType & get_prev_error_signals(int child_index=0)
InputAbsDistMatrixType & get_prev_activations(int parent_index=0)
reshape_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: reshape.hpp:50
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.
Definition: reshape.hpp:122
const OutputAbsDistMatrixType & get_activations(const Layer &child) const override
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.
void serialize(ArchiveT &ar)
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
Definition: reshape.hpp:61
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
Definition: reshape.hpp:62
std::string get_name() const
Get the layer instance&#39;s name.
Definition: layer.hpp:332
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
Definition: reshape.hpp:123
void write_specific_proto(lbann_data::Layer &proto) const final
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
Reinterpret tensor with new dimensions.
Definition: reshape.hpp:42
void bp_setup_gradient_wrt_inputs() override
Setup gradient w.r.t. input tensors. Called by the &#39;back_prop&#39; function. Each gradient w...
Definition: reshape.hpp:118
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: reshape.hpp:63
std::string get_type() const override
Get the layer type&#39;s name.
Definition: reshape.hpp:60
reshape_layer(lbann_comm *comm, std::vector< int > dims)
Definition: reshape.hpp:45
std::vector< int > get_output_dims(size_t output_index=0) const
Get output tensor dimensions.
const InputAbsDistMatrixType & get_error_signals(const Layer &parent) const override