LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
one_hot.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_LAYERS_MISC_ONE_HOT_HPP_INCLUDED
28 #define LBANN_LAYERS_MISC_ONE_HOT_HPP_INCLUDED
29 
32 
33 #include "lbann/proto/layers.pb.h"
34 
35 namespace lbann {
36 
44 template <typename TensorDataType, data_layout Layout, El::Device Device>
45 class one_hot_layer : public data_type_layer<TensorDataType>
46 {
47 public:
48  one_hot_layer(size_t size) : data_type_layer<TensorDataType>(nullptr)
49  {
50  this->set_output_dims({static_cast<int>(size)});
51  }
52  one_hot_layer* copy() const override { return new one_hot_layer(*this); }
53 
55 
57  template <typename ArchiveT>
58  void serialize(ArchiveT& ar);
59 
61 
62  std::string get_type() const override { return "one-hot"; }
63  data_layout get_data_layout() const override { return Layout; }
64  El::Device get_device_allocation() const override { return Device; }
65  bool can_run_inplace() const override { return false; }
66  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
67 
68 protected:
70  void write_specific_proto(lbann_data::Layer& proto) const final;
71 
72  friend class cereal::access;
74 
75  void setup_dims() override;
76 
77  void fp_compute() override;
78 };
79 
80 template <typename T, data_layout L, El::Device D>
82  lbann_data::Layer& proto) const
83 {
84  proto.set_datatype(proto::ProtoDataType<T>);
85  auto* msg = proto.mutable_one_hot();
86  msg->set_size(this->get_output_dims()[0]);
87 }
88 
89 #ifndef LBANN_ONE_HOT_LAYER_INSTANTIATE
90 #define PROTO_DEVICE(T, Device) \
91  extern template class one_hot_layer<T, data_layout::DATA_PARALLEL, Device>; \
92  extern template class one_hot_layer<T, data_layout::MODEL_PARALLEL, Device>
94 #undef PROTO_DEVICE
95 #endif // LBANN_ONE_HOT_LAYER_INSTANTIATE
96 
97 } // namespace lbann
98 
99 #endif // LBANN_LAYERS_MISC_ONE_HOT_HPP_INCLUDED
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: one_hot.hpp:63
void serialize(ArchiveT &ar)
void write_specific_proto(lbann_data::Layer &proto) const final
Definition: one_hot.hpp:81
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: one_hot.hpp:66
constexpr El::Device Device
one_hot_layer(size_t size)
Definition: one_hot.hpp:48
Convert index to a one-hot vector.
Definition: one_hot.hpp:45
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 ...
Definition: one_hot.hpp:64
friend class cereal::access
Definition: one_hot.hpp:72
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
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.
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.
std::string get_type() const override
Get the layer type&#39;s name.
Definition: one_hot.hpp:62
std::vector< int > get_output_dims(size_t output_index=0) const
Get output tensor dimensions.
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: one_hot.hpp:65
one_hot_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: one_hot.hpp:52