LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
sort.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_SORT_HPP_INCLUDED
28 #define LBANN_LAYER_SORT_HPP_INCLUDED
29 
31 #include "lbann/layers/layer.hpp"
33 #include "lbann/proto/layers.pb.h"
34 
35 namespace lbann {
36 
38 template <typename TensorDataType,
40  El::Device Dev = El::Device::CPU>
41 class sort_layer : public data_type_layer<TensorDataType>
42 {
43  static_assert(T_layout == data_layout::DATA_PARALLEL,
44  "sort layer only supports DATA_PARALLEL");
45 
46 public:
47  sort_layer(lbann_comm* comm, bool descending = false)
48  : data_type_layer<TensorDataType>(comm), m_descending(descending)
49  {}
50  sort_layer(const sort_layer& other);
51 
52  sort_layer& operator=(const sort_layer& other);
53 
54  sort_layer* copy() const override { return new sort_layer(*this); }
55 
57 
59  template <typename ArchiveT>
60  void serialize(ArchiveT& ar);
61 
63 
64  std::string get_type() const override { return "sort"; }
65  data_layout get_data_layout() const override { return T_layout; }
66  El::Device get_device_allocation() const override { return Dev; }
67  bool can_run_inplace() const override { return false; }
68  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
69 
70  description get_description() const override
71  {
73  desc.add("Descending", m_descending);
74  return desc;
75  }
76 
77 protected:
79  void write_specific_proto(lbann_data::Layer& proto) const final;
80 
81  friend class cereal::access;
82  sort_layer() : sort_layer(nullptr) {}
83 
84  void setup_dims() override;
85 
86  void setup_data(size_t max_mini_batch_size) override;
87 
88  void fp_setup_outputs() override;
89 
90  void fp_compute() override;
91  void bp_compute() override;
92 
93 private:
96 
101  std::unique_ptr<El::AbstractMatrix<El::Int>> m_indices;
102 };
103 
104 template <typename T, data_layout L, El::Device D>
105 void sort_layer<T, L, D>::write_specific_proto(lbann_data::Layer& proto) const
106 {
107  proto.set_datatype(proto::ProtoDataType<T>);
108  auto* msg = proto.mutable_sort();
109  msg->set_descending(m_descending);
110 }
111 
112 #ifndef LBANN_SORT_LAYER_INSTANTIATE
113 #define PROTO_DEVICE(T, Device) \
114  extern template class sort_layer<T, data_layout::DATA_PARALLEL, Device>
115 
117 #undef PROTO_DEVICE
118 #endif // LBANN_SORT_LAYER_INSTANTIATE
119 
120 } // namespace lbann
121 
122 #endif // LBANN_LAYER_SORT_HPP_INCLUDED
friend class cereal::access
Definition: sort.hpp:81
std::string get_type() const override
Get the layer type&#39;s name.
Definition: sort.hpp:64
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
void write_specific_proto(lbann_data::Layer &proto) const final
Definition: sort.hpp:105
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: sort_impl.hpp:112
Generates nicely formatted description messages.
Definition: description.hpp:49
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: sort.hpp:68
virtual description get_description() const
Human-readable description.
bool m_descending
Definition: sort.hpp:95
constexpr El::Device Device
sort_layer & operator=(const sort_layer &other)
Definition: sort_impl.hpp:58
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: sort.hpp:67
sort_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: sort.hpp:54
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: sort_impl.hpp:84
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: sort.hpp:66
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
description get_description() const override
Human-readable description.
Definition: sort.hpp:70
Sort tensor entries.
Definition: sort.hpp:41
sort_layer(lbann_comm *comm, bool descending=false)
Definition: sort.hpp:47
void serialize(ArchiveT &ar)
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.
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: sort.hpp:65
void setup_data(size_t max_mini_batch_size) override
Setup layer data. Called by the &#39;setup&#39; function. Memory is allocated for distributed matrices...
Definition: sort_impl.hpp:91
std::unique_ptr< El::AbstractMatrix< El::Int > > m_indices
Definition: sort.hpp:101