27 #ifndef LBANN_OPTIMIZERS_OPTIMIZER_IMPL_HPP_INCLUDED 28 #define LBANN_OPTIMIZERS_OPTIMIZER_IMPL_HPP_INCLUDED 35 template <
typename TensorDataType>
37 El::AbstractDistMatrix<TensorDataType>
const& contrib,
39 bool allreduce_needed)
41 TensorDataType buf_scale, in_scale;
43 El::Scale(buf_scale, grad);
44 El::Axpy(in_scale * scale, contrib, grad);
47 template <
typename TensorDataType>
48 El::AbstractDistMatrix<TensorDataType>&
50 TensorDataType& in_scale,
63 auto& grad_mgr_ptr =
gradients_[std::type_index(
typeid(TensorDataType))];
67 grad_mgr_ptr = std::make_unique<GradMgrType>(std::get<HEIGHT>(mat_info),
68 std::get<WIDTH>(mat_info),
69 std::get<DISTDATA>(mat_info));
73 auto& grad_mgr =
static_cast<GradMgrType&
>(*grad_mgr_ptr);
76 grad_mgr.complete_allreduce(*(this->
m_comm));
78 auto& buffer = grad_mgr.gradient();
81 switch (grad_mgr.get_status()) {
83 buf_scale = DataType(1);
84 in_scale = DataType(1);
85 if (allreduce_needed) {
86 buf_scale /= buffer.RedundantSize();
91 buf_scale = DataType(0);
92 in_scale = DataType(1);
93 grad_mgr.set_status(allreduce_needed
98 buf_scale = DataType(1);
101 (allreduce_needed ? DataType(1) : DataType(1) / buffer.RedundantSize());
111 template <
typename TensorDataType>
113 El::AbstractDistMatrix<TensorDataType>& gradient)
115 using AbsDistMatType = El::AbstractDistMatrix<TensorDataType>;
116 static const TensorDataType one = TensorDataType(1.f);
127 auto const this_type_idx = std::type_index(
typeid(TensorDataType));
129 if (num_updates == 0UL)
135 auto this_type_contrib = this->
gradients_.find(this_type_idx);
136 if (this_type_contrib != this->
gradients_.end()) {
138 auto const& grad_mgr = *(this_type_contrib->second);
144 auto const& contrib =
145 dynamic_cast<AbsDistMatType const&
>(grad_mgr.gradient());
146 if (contrib.DistData() == gradient.DistData()) {
147 El::LockedView(gradient, contrib);
151 El::Copy(contrib, gradient);
161 if (num_updates == 1UL && this->
gradients_.size() == 1UL) {
162 auto const& grad_mgr = *(this->
gradients_.begin()->second);
167 El::Copy(grad_mgr.gradient(), gradient);
171 auto tmp = std::unique_ptr<AbsDistMatType>{
172 gradient.Construct(gradient.Grid(), gradient.Root())};
174 for (
auto const& grad_mgr_v : this->
gradients_) {
175 if (grad_mgr_v.first == this_type_idx)
177 auto const& grad_mgr = *(grad_mgr_v.second);
182 auto const& grad_base = grad_mgr.gradient();
183 El::Copy(grad_base, *tmp);
184 El::Axpy(one, *tmp, gradient);
189 template <
typename TensorDataType>
193 switch (this->get_status()) {
195 comm.
nb_allreduce(*gradient_, gradient_->RedundantComm(), allreduce_req_);
209 template <
typename TensorDataType>
213 switch (this->get_status()) {
215 comm.
wait(allreduce_req_);
222 LBANN_ERROR(
"attempted to finish gradient allreduce " 223 "before starting it");
232 template <
typename TensorDataType>
240 #endif // LBANN_OPTIMIZERS_OPTIMIZER_IMPL_HPP_INCLUDED lbann_comm * m_comm
LBANN communicator.
std::unordered_map< std::type_index, gradient_manager_ptr > gradients_
Values have been cleared.
void nb_allreduce(El::AbstractMatrix< TensorDataType > &m, const El::mpi::Comm &c, Al::request &req, El::mpi::Op op=El::mpi::SUM) const
std::string to_string(El::Device const &d)
El::AbstractDistMatrix< TensorDataType > & get_gradient_buffer(TensorDataType &buf_scale, TensorDataType &in_scale, bool allreduce_needed=false)
Get the gradient buffer.
Values can be accessed immediately.
void accumulate_all_gradient_contributions(El::AbstractDistMatrix< TensorDataType > &gradient)
void complete_allreduce(lbann_comm &comm) override
Allreduce is needed before accessing values.
void add_to_gradient(El::AbstractDistMatrix< TensorDataType > const &contrib, TensorDataType scale=1.f, bool allreduce_needed=false)
Add to the objective function gradient w.r.t. the weights.
void start_allreduce(lbann_comm &comm) override
Allreduce on values is in progress.
virtual std::tuple< El::Int, El::Int, El::DistData > get_matrix_info() const =0
void wait(El::mpi::Request< T > &req) const