LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
in_top_k.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_IN_TOP_K_HPP_INCLUDED
28 #define LBANN_LAYER_IN_TOP_K_HPP_INCLUDED
29 
31 #include "lbann/layers/layer.hpp"
34 
35 #include "lbann/proto/layers.pb.h"
36 
37 namespace lbann {
38 
46 template <typename TensorDataType,
48  El::Device Dev = El::Device::CPU>
49 class in_top_k_layer : public data_type_layer<TensorDataType>
50 {
51 public:
52  in_top_k_layer(lbann_comm* comm, El::Int k)
53  : data_type_layer<TensorDataType>(comm), m_k(k)
54  {
55  if (m_k < 0) {
56  LBANN_ERROR("invalid parameter for top-k search (k=", m_k, ")");
57  }
58  }
59 
60  in_top_k_layer* copy() const override { return new in_top_k_layer(*this); }
61 
63 
65  template <typename ArchiveT>
66  void serialize(ArchiveT& ar);
67 
69 
70  std::string get_type() const override { return "in_top_k"; }
71  data_layout get_data_layout() const override { return T_layout; }
72  El::Device get_device_allocation() const override { return Dev; }
73  bool can_run_inplace() const override { return false; }
74  int get_backprop_requirements() const override
75  {
77  }
78 
79  description get_description() const override
80  {
82  desc.add("k", m_k);
83  return desc;
84  }
85 
86 protected:
88  void write_specific_proto(lbann_data::Layer& proto) const final;
89 
90  friend class cereal::access;
91  in_top_k_layer() : in_top_k_layer(nullptr, 1) {}
92 
93  void setup_dims() override
94  {
96  this->set_output_dims(this->get_input_dims());
97  }
98 
99  void fp_compute() override;
100 
101 private:
103  El::Int m_k;
104 };
105 
106 template <typename T, data_layout L, El::Device D>
108  lbann_data::Layer& proto) const
109 {
110  proto.set_datatype(proto::ProtoDataType<T>);
111  auto* msg = proto.mutable_in_top_k();
112  msg->set_k(m_k);
113 }
114 
115 #ifndef LBANN_IN_TOP_K_LAYER_INSTANTIATE
116 #define PROTO_DEVICE(T, Device) \
117  extern template class in_top_k_layer<T, data_layout::DATA_PARALLEL, Device>; \
118  extern template class in_top_k_layer<T, data_layout::MODEL_PARALLEL, Device>
119 
121 #undef PROTO_DEVICE
122 #endif // LBANN_IN_TOP_K_LAYER_INSTANTIATE
123 
124 } // namespace lbann
125 
126 #endif // LBANN_LAYER_IN_TOP_K_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.
One-hot vector indicating top-k entries.
Definition: in_top_k.hpp:49
friend class cereal::access
Definition: in_top_k.hpp:90
void serialize(ArchiveT &ar)
#define LBANN_ERROR(...)
Definition: exception.hpp:37
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: in_top_k.hpp:93
std::vector< int > get_input_dims(size_t input_index=0) const
Get input tensor dimensions.
Generates nicely formatted description messages.
Definition: description.hpp:49
virtual description get_description() const
Human-readable description.
std::string get_type() const override
Get the layer type&#39;s name.
Definition: in_top_k.hpp:70
constexpr El::Device Device
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
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: in_top_k.hpp:71
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: in_top_k.hpp:74
in_top_k_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: in_top_k.hpp:60
void write_specific_proto(lbann_data::Layer &proto) const final
Definition: in_top_k.hpp:107
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: in_top_k.hpp:79
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.
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: in_top_k.hpp:72
in_top_k_layer(lbann_comm *comm, El::Int k)
Definition: in_top_k.hpp:52
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: in_top_k.hpp:73