27 #ifndef LBANN_LAYERS_LOSS_CROSS_ENTROPY_HPP_INCLUDED 28 #define LBANN_LAYERS_LOSS_CROSS_ENTROPY_HPP_INCLUDED 33 #include "lbann/proto/layers.pb.h" 35 #ifdef LBANN_HAS_DISTCONV 36 #include "distconv/dnn_backend/cross_entropy.hpp" 42 #ifdef LBANN_HAS_DISTCONV 44 using Backend = ::distconv::BackendDNNLib;
45 using CrossEntropy = ::distconv::CrossEntropy<Backend>;
48 template <
typename TensorDataType, data_layout T_layout, El::Device Dev>
49 class cross_entropy_distconv_adapter
50 :
public data_type_distconv_adapter<TensorDataType>
55 cross_entropy_distconv_adapter(Layer& layer,
bool use_labels)
56 : data_type_distconv_adapter<TensorDataType>(layer),
57 m_use_labels(use_labels)
59 virtual ~cross_entropy_distconv_adapter() =
default;
60 void setup_distributions(tensor_overlap_constraints& constraints)
override;
61 dc::Shape get_prev_activations_shape(
int index)
const override;
62 dc::Shape get_activations_shape(
int index)
const override;
63 dc::Shape get_activations_local_shape(
int index)
const override;
64 void setup_layer(
size_t workspace_capacity)
override;
65 std::unique_ptr<dc::CrossEntropy> m_cross_entropy;
68 #endif // LBANN_HAS_DISTCONV 76 template <
typename TensorDataType, data_layout T_layout, El::Device Dev>
92 this->m_expected_num_parent_layers = 2;
96 :
data_type_layer<TensorDataType>(other), m_use_labels(other.m_use_labels)
117 template <
typename ArchiveT>
122 std::string
get_type()
const override {
return "cross entropy"; }
131 #ifdef LBANN_HAS_ONNX 132 void fill_onnx_node(onnx::GraphProto& graph)
const override;
133 #endif // LBANN_HAS_ONNX 135 void setup_dims()
override;
137 void setup_data(
size_t max_mini_batch_size)
override;
139 void fp_compute()
override;
141 void bp_compute()
override;
145 void write_specific_proto(lbann_data::Layer& proto)
const final;
147 friend class cereal::access;
152 void local_fp_compute();
154 void local_bp_compute();
162 #ifdef LBANN_HAS_DISTCONV 163 friend class cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>;
166 bool is_distconv_supported()
const override 171 void setup_distconv_adapter()
override 173 this->get_distconv_adapter_ptr() = std::make_unique<
174 cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>>(
179 cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>&
180 get_distconv_adapter()
override;
181 const cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>&
182 get_distconv_adapter()
const override;
184 void fp_compute_distconv()
186 assert_always(this->distconv_enabled());
187 get_distconv_adapter().m_cross_entropy->forward(
188 this->get_distconv_adapter().get_prev_activations(0),
189 this->get_distconv_adapter().get_prev_activations(1),
190 this->get_distconv_adapter().get_activations());
193 void bp_compute_distconv()
195 assert_always(this->distconv_enabled());
196 get_distconv_adapter().m_cross_entropy->backward(
197 this->get_distconv_adapter().get_prev_activations(0),
198 this->get_distconv_adapter().get_prev_activations(1),
199 this->get_distconv_adapter().get_prev_error_signals(0),
200 this->get_distconv_adapter().get_error_signals(0),
201 this->get_distconv_adapter().get_error_signals(1));
203 #endif // LBANN_HAS_DISTCONV 206 #ifndef LBANN_CROSS_ENTROPY_LAYER_INSTANTIATE 208 #define PROTO_DEVICE(T, Device) \ 209 extern template class cross_entropy_layer<T, \ 210 data_layout::DATA_PARALLEL, \ 212 extern template class cross_entropy_layer<T, \ 213 data_layout::MODEL_PARALLEL, \ 219 #endif // LBANN_CROSS_ENTROPY_LAYER_INSTANTIATE 223 #endif // LBANN_LAYERS_LOSS_CROSS_ENTROPY_HPP_INCLUDED
cross_entropy_layer(lbann_comm *comm, bool use_labels)
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
std::unique_ptr< AbsDistMatrixType > m_workspace
Cross entropy between probability vectors.
constexpr El::Device Device
cross_entropy_layer & operator=(const cross_entropy_layer &other)
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
::distconv::tensor::Shape Shape
cross_entropy_layer(const cross_entropy_layer &other)
cross_entropy_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
data_layout
Data layout that is optimized for different modes of parallelism.
std::string get_type() const override
Get the layer type's name.
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
data_type_layer & operator=(data_type_layer &&other)=default
dc::TensorDev< OutputTensorDataType > TensorDevType