adam_op.h 19.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
Y
Yang Yu 已提交
16
#include <math.h>  // for sqrt in CPU and CUDA
17
#include <Eigen/Dense>
S
sneaxiy 已提交
18
#include <vector>
Y
Yi Wang 已提交
19
#include "paddle/fluid/framework/op_registry.h"
Q
Qiao Longfei 已提交
20
#include "paddle/fluid/framework/threadpool.h"
Y
Yi Wang 已提交
21
#include "paddle/fluid/operators/detail/safe_ref.h"
S
sneaxiy 已提交
22
#include "paddle/fluid/operators/math/algorithm.h"
Y
Yi Wang 已提交
23 24
#include "paddle/fluid/operators/math/selected_rows_functor.h"
#include "paddle/fluid/platform/for_range.h"
25 26 27 28

namespace paddle {
namespace operators {

T
wip  
typhoonzero 已提交
29 30
namespace scatter = paddle::operators::math::scatter;

31 32 33 34 35 36
struct GPUAdam;
struct CPUAdam;

template <typename T, typename Flavour>
struct AdamFunctor;

Y
Yang Yu 已提交
37
template <typename T>
38
struct AdamFunctor<T, GPUAdam> {
Y
Yang Yu 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
  T beta1_;
  T beta2_;
  T epsilon_;

  const T* beta1_pow_;
  const T* beta2_pow_;
  const T* moment1_;
  T* moment1_out_;
  const T* moment2_;
  T* moment2_out_;
  const T* lr_;
  const T* grad_;
  const T* param_;
Y
Yang Yu 已提交
52
  T* param_out_;
Y
Yang Yu 已提交
53 54 55

  AdamFunctor(T beta1, T beta2, T epsilon, const T* beta1_pow,
              const T* beta2_pow, const T* mom1, T* mom1_out, const T* mom2,
Y
Yang Yu 已提交
56 57
              T* mom2_out, const T* lr, const T* grad, const T* param,
              T* param_out)
Y
Yang Yu 已提交
58 59 60 61 62 63 64 65 66 67 68
      : beta1_(beta1),
        beta2_(beta2),
        epsilon_(epsilon),
        beta1_pow_(beta1_pow),
        beta2_pow_(beta2_pow),
        moment1_(mom1),
        moment1_out_(mom1_out),
        moment2_(mom2),
        moment2_out_(mom2_out),
        lr_(lr),
        grad_(grad),
Y
Yang Yu 已提交
69 70
        param_(param),
        param_out_(param_out) {}
Y
Yang Yu 已提交
71

Y
Yang Yu 已提交
72
  inline HOSTDEVICE void operator()(size_t i) const {
Y
Yang Yu 已提交
73 74 75 76 77 78 79
    // Merge all memory access together.
    T g = grad_[i];
    T mom1 = moment1_[i];
    T mom2 = moment2_[i];
    T lr = *lr_;
    T beta1_pow = *beta1_pow_;
    T beta2_pow = *beta2_pow_;
Y
Yang Yu 已提交
80
    T p = param_[i];
Y
Yang Yu 已提交
81 82

    // Calculation
Y
Yang Yu 已提交
83
    lr *= sqrt(1 - beta2_pow) / (1 - beta1_pow);
84

Y
Yang Yu 已提交
85 86
    mom1 = beta1_ * mom1 + (1 - beta1_) * g;
    mom2 = beta2_ * mom2 + (1 - beta2_) * g * g;
Y
Yang Yu 已提交
87
    p -= lr * (mom1 / (sqrt(mom2) + epsilon_));
Y
Yang Yu 已提交
88 89 90 91

    // Write back to global memory
    moment1_out_[i] = mom1;
    moment2_out_[i] = mom2;
Y
Yang Yu 已提交
92
    param_out_[i] = p;
Y
Yang Yu 已提交
93 94 95
  }
};

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
template <typename T>
struct AdamFunctor<T, CPUAdam> {
  T beta1_;
  T beta2_;
  T epsilon_;

  const T* beta1_pow_;
  const T* beta2_pow_;
  const T* moment1_;
  T* moment1_out_;
  const T* moment2_;
  T* moment2_out_;
  const T* lr_;
  const T* grad_;
  const T* param_;
  T* param_out_;

  AdamFunctor(T beta1, T beta2, T epsilon, const T* beta1_pow,
              const T* beta2_pow, const T* mom1, T* mom1_out, const T* mom2,
              T* mom2_out, const T* lr, const T* grad, const T* param,
              T* param_out)
      : beta1_(beta1),
        beta2_(beta2),
        epsilon_(epsilon),
        beta1_pow_(beta1_pow),
        beta2_pow_(beta2_pow),
        moment1_(mom1),
        moment1_out_(mom1_out),
        moment2_(mom2),
        moment2_out_(mom2_out),
        lr_(lr),
        grad_(grad),
        param_(param),
        param_out_(param_out) {}

  void operator()(size_t numel) const {
    Eigen::Map<const Eigen::Array<T, 1, Eigen::Dynamic>> g{
        grad_, static_cast<Eigen::Index>(numel)};
    Eigen::Map<const Eigen::Array<T, 1, Eigen::Dynamic>> mom1{
        moment1_, static_cast<Eigen::Index>(numel)};
    Eigen::Map<const Eigen::Array<T, 1, Eigen::Dynamic>> mom2{
        moment2_, static_cast<Eigen::Index>(numel)};
    Eigen::Map<const Eigen::Array<T, 1, Eigen::Dynamic>> param{
        param_, static_cast<Eigen::Index>(numel)};

    Eigen::Map<Eigen::Array<T, 1, Eigen::Dynamic>> param_out{
        param_out_, static_cast<Eigen::Index>(numel)};
    Eigen::Map<Eigen::Array<T, 1, Eigen::Dynamic>> moment1_out{
        moment1_out_, static_cast<Eigen::Index>(numel)};
    Eigen::Map<Eigen::Array<T, 1, Eigen::Dynamic>> moment2_out{
        moment2_out_, static_cast<Eigen::Index>(numel)};

    T lr = *lr_;
    T beta1_pow = *beta1_pow_;
    T beta2_pow = *beta2_pow_;

    // Calculation
    lr *= sqrt(1 - beta2_pow) / (1 - beta1_pow);

    moment1_out = beta1_ * mom1 + (1 - beta1_) * g;
    moment2_out = beta2_ * mom2 + (1 - beta2_) * g * g;
    param_out = param - lr * (moment1_out / (moment2_out.sqrt() + epsilon_));
  }
};

161 162 163
template <typename T, typename Flavour>
struct SparseAdamFunctor;

T
wip  
typhoonzero 已提交
164
template <typename T>
M
minqiyang 已提交
165
struct SparseAdamFunctor<T, GPUAdam> {
T
wip  
typhoonzero 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  T beta1_;
  T beta2_;
  T epsilon_;

  const T* beta1_pow_;
  const T* beta2_pow_;
  const T* moment1_;
  T* moment1_out_;
  const T* moment2_;
  T* moment2_out_;
  const T* lr_;
  const T* grad_;
  const T* param_;
  T* param_out_;

  const int64_t* rows_;
  int64_t row_numel_;
S
sneaxiy 已提交
183
  int64_t row_count_;
Q
Qiao Longfei 已提交
184
  bool lazy_mode_;
T
wip  
typhoonzero 已提交
185 186 187 188 189

  SparseAdamFunctor(T beta1, T beta2, T epsilon, const T* beta1_pow,
                    const T* beta2_pow, const T* mom1, T* mom1_out,
                    const T* mom2, T* mom2_out, const T* lr, const T* grad,
                    const T* param, T* param_out, const int64_t* rows,
Q
Qiao Longfei 已提交
190
                    int64_t row_numel, int64_t row_count, bool lazy_mode)
T
wip  
typhoonzero 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204
      : beta1_(beta1),
        beta2_(beta2),
        epsilon_(epsilon),
        beta1_pow_(beta1_pow),
        beta2_pow_(beta2_pow),
        moment1_(mom1),
        moment1_out_(mom1_out),
        moment2_(mom2),
        moment2_out_(mom2_out),
        lr_(lr),
        grad_(grad),
        param_(param),
        param_out_(param_out),
        rows_(rows),
S
sneaxiy 已提交
205
        row_numel_(row_numel),
Q
Qiao Longfei 已提交
206
        row_count_(row_count),
Q
Qiao Longfei 已提交
207
        lazy_mode_(lazy_mode) {}
S
sneaxiy 已提交
208

Q
Qiao Longfei 已提交
209
  inline HOSTDEVICE void adam_update(size_t i, T g) const {
S
sneaxiy 已提交
210 211 212 213
    // The following code is the same as dense
    T mom1 = moment1_[i];
    T mom2 = moment2_[i];
    T lr = *lr_;
T
typhoonzero 已提交
214 215
    T beta1_pow = *beta1_pow_;
    T beta2_pow = *beta2_pow_;
S
sneaxiy 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228
    T p = param_[i];

    // Calculation
    lr *= sqrt(1 - beta2_pow) / (1 - beta1_pow);

    mom1 = beta1_ * mom1 + (1 - beta1_) * g;
    mom2 = beta2_ * mom2 + (1 - beta2_) * g * g;
    p -= lr * (mom1 / (sqrt(mom2) + epsilon_));

    // Write back to global memory
    moment1_out_[i] = mom1;
    moment2_out_[i] = mom2;
    param_out_[i] = p;
T
wip  
typhoonzero 已提交
229
  }
Q
Qiao Longfei 已提交
230 231 232 233

  inline HOSTDEVICE void operator()(size_t i) const {
    auto row_idx =
        math::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
Q
Qiao Longfei 已提交
234 235 236
    if (lazy_mode_ && row_idx < 0) {
      return;
    } else {
Q
Qiao Longfei 已提交
237 238 239
      T g = row_idx >= 0 ? grad_[row_idx * row_numel_ + i % row_numel_] : 0;
      adam_update(i, g);
    }
Q
Qiao Longfei 已提交
240
  }
T
wip  
typhoonzero 已提交
241 242
};

M
minqiyang 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
template <typename T>
struct SparseAdamFunctor<T, CPUAdam> {
  T beta1_;
  T beta2_;
  T epsilon_;

  const T* beta1_pow_;
  const T* beta2_pow_;
  const T* moment1_;
  T* moment1_out_;
  const T* moment2_;
  T* moment2_out_;
  const T* lr_;
  const T* grad_;
  const T* param_;
  T* param_out_;

  const int64_t* rows_;
  int64_t row_numel_;
  int64_t row_count_;

  SparseAdamFunctor(T beta1, T beta2, T epsilon, const T* beta1_pow,
                    const T* beta2_pow, const T* mom1, T* mom1_out,
                    const T* mom2, T* mom2_out, const T* lr, const T* grad,
                    const T* param, T* param_out, const int64_t* rows,
268
                    int64_t row_numel, int64_t row_count, bool lazy_mode)
M
minqiyang 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
      : beta1_(beta1),
        beta2_(beta2),
        epsilon_(epsilon),
        beta1_pow_(beta1_pow),
        beta2_pow_(beta2_pow),
        moment1_(mom1),
        moment1_out_(mom1_out),
        moment2_(mom2),
        moment2_out_(mom2_out),
        lr_(lr),
        grad_(grad),
        param_(param),
        param_out_(param_out),
        rows_(rows),
        row_numel_(row_numel),
        row_count_(row_count) {}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  inline HOSTDEVICE void adam_update(size_t i, T g) const {
    // The following code is the same as dense
    T mom1 = moment1_[i];
    T mom2 = moment2_[i];
    T lr = *lr_;
    T beta1_pow = *beta1_pow_;
    T beta2_pow = *beta2_pow_;
    T p = param_[i];

    // Calculation
    lr *= sqrt(1 - beta2_pow) / (1 - beta1_pow);

    mom1 = beta1_ * mom1 + (1 - beta1_) * g;
    mom2 = beta2_ * mom2 + (1 - beta2_) * g * g;
    p -= lr * (mom1 / (sqrt(mom2) + epsilon_));

    // Write back to global memory
    moment1_out_[i] = mom1;
    moment2_out_[i] = mom2;
    param_out_[i] = p;
  }

M
minqiyang 已提交
308 309 310 311 312 313
  inline void operator()(size_t numel) const {
    // lr could be reuse
    T lr = *lr_;
    T beta1_pow = *beta1_pow_;
    T beta2_pow = *beta2_pow_;
    lr *= sqrt(1 - beta2_pow) / (1 - beta1_pow);
M
Fix bug  
minqiyang 已提交
314
    size_t row_count = numel / row_numel_;
M
minqiyang 已提交
315

M
Fix bug  
minqiyang 已提交
316
    for (size_t i = 0U, j = 0U; i != row_count; ++i) {
M
minqiyang 已提交
317
      if (i == *(rows_ + j)) {
M
Fix bug  
minqiyang 已提交
318 319
        for (size_t k = 0U; k != row_numel_; ++k) {
          T g = grad_[j * row_numel_ + k];
M
minqiyang 已提交
320
          adam_update(i * row_numel_ + k, g);
M
Fix bug  
minqiyang 已提交
321
        }
M
minqiyang 已提交
322 323
        ++j;
      } else {
M
Fix bug  
minqiyang 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337
        for (size_t k = 0U; k != row_numel_; ++k) {
          T mom1 = moment1_[i * row_numel_ + k];
          T mom2 = moment2_[i * row_numel_ + k];
          T p = param_[i * row_numel_ + k];

          mom1 = beta1_ * mom1;
          mom2 = beta2_ * mom2;

          p -= lr * (mom1 / (sqrt(mom2) + epsilon_));
          // Write back to global memory
          moment1_out_[i * row_numel_ + k] = mom1;
          moment2_out_[i * row_numel_ + k] = mom2;
          param_out_[i * row_numel_ + k] = p;
        }
M
minqiyang 已提交
338 339 340 341 342
      }
    }
  }
};

Q
QI JUN 已提交
343
template <typename DeviceContext, typename T>
344 345 346
class AdamOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
C
chengduo 已提交
347 348 349 350
    const auto* param_var = ctx.InputVar("Param");
    PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
                   "The Var(%s)'s type should be LoDTensor, "
                   "but the received is %s",
S
sneaxiy 已提交
351 352
                   ctx.Inputs("Param").front(),
                   framework::ToTypeName(param_var->Type()));
C
chengduo 已提交
353

Y
Yang Yu 已提交
354 355
    using paddle::framework::LoDTensor;
    using paddle::operators::detail::Ref;
356

357 358
    int64_t min_row_size_to_use_multithread =
        ctx.Attr<int64_t>("min_row_size_to_use_multithread");
Q
Qiao Longfei 已提交
359
    bool lazy_mode = ctx.Attr<bool>("lazy_mode");
360 361 362
    T beta1 = static_cast<T>(ctx.Attr<float>("beta1"));
    T beta2 = static_cast<T>(ctx.Attr<float>("beta2"));
    T epsilon = static_cast<T>(ctx.Attr<float>("epsilon"));
Y
Yang Yu 已提交
363
    auto& param = Ref(ctx.Input<LoDTensor>("Param"), "Must set Param");
T
wip  
typhoonzero 已提交
364 365
    // auto& grad = Ref(ctx.Input<LoDTensor>("Grad"), "Must set Grad");
    auto* grad_var = ctx.InputVar("Grad");
Y
Yang Yu 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    auto& mom1 = Ref(ctx.Input<LoDTensor>("Moment1"), "Must set Moment1");
    auto& mom2 = Ref(ctx.Input<LoDTensor>("Moment2"), "Must set Moment2");
    auto& lr =
        Ref(ctx.Input<LoDTensor>("LearningRate"), "Must set LearningRate");

    auto& beta1_pow =
        Ref(ctx.Input<LoDTensor>("Beta1Pow"), "Must set Beta1Pow");
    auto& beta2_pow =
        Ref(ctx.Input<LoDTensor>("Beta2Pow"), "Must set Beta2Pow");

    auto& param_out =
        Ref(ctx.Output<LoDTensor>("ParamOut"), "Must set ParamOut");
    auto& mom1_out =
        Ref(ctx.Output<LoDTensor>("Moment1Out"), "Must set Moment1Out");
    auto& mom2_out =
        Ref(ctx.Output<LoDTensor>("Moment2Out"), "Must set Moment1Out");

T
wip  
typhoonzero 已提交
383 384
    if (grad_var->IsType<framework::LoDTensor>()) {
      auto& grad = Ref(ctx.Input<LoDTensor>("Grad"), "Must set Grad");
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

      if (platform::is_cpu_place(ctx.GetPlace())) {
        AdamFunctor<T, CPUAdam> functor(
            beta1, beta2, epsilon, beta1_pow.template data<T>(),
            beta2_pow.template data<T>(), mom1.template data<T>(),
            mom1_out.template mutable_data<T>(ctx.GetPlace()),
            mom2.template data<T>(),
            mom2_out.template mutable_data<T>(ctx.GetPlace()),
            lr.template data<T>(), grad.template data<T>(),
            param.template data<T>(),
            param_out.template mutable_data<T>(ctx.GetPlace()));
        functor(param.numel());
      } else if (platform::is_gpu_place(ctx.GetPlace())) {
        AdamFunctor<T, GPUAdam> functor(
            beta1, beta2, epsilon, beta1_pow.template data<T>(),
            beta2_pow.template data<T>(), mom1.template data<T>(),
            mom1_out.template mutable_data<T>(ctx.GetPlace()),
            mom2.template data<T>(),
            mom2_out.template mutable_data<T>(ctx.GetPlace()),
            lr.template data<T>(), grad.template data<T>(),
            param.template data<T>(),
            param_out.template mutable_data<T>(ctx.GetPlace()));

        platform::ForRange<DeviceContext> for_range(
            static_cast<const DeviceContext&>(ctx.device_context()),
            param.numel());
        for_range(functor);
      }
T
wip  
typhoonzero 已提交
413 414 415
    } else if (grad_var->IsType<framework::SelectedRows>()) {
      auto& grad =
          Ref(ctx.Input<framework::SelectedRows>("Grad"), "Must set Grad");
416
      if (grad.rows().size() == 0) {
M
minqiyang 已提交
417
        VLOG(3) << "grad row size is 0!!";
418 419
        return;
      }
S
sneaxiy 已提交
420 421 422 423 424 425 426 427 428 429

      std::vector<int64_t> cpu_rows(grad.rows().begin(), grad.rows().end());
      bool is_strict_sorted = true;
      for (size_t i = 1; i < cpu_rows.size(); ++i) {
        if (cpu_rows[i - 1] >= cpu_rows[i]) {
          is_strict_sorted = false;
          break;
        }
      }

430
      framework::SelectedRows cpu_grad_merge;
S
sneaxiy 已提交
431 432 433 434 435 436
      const framework::SelectedRows* grad_merge_ptr;
      if (is_strict_sorted) {
        grad_merge_ptr = &grad;
      } else {
        // merge duplicated rows if any.
        // The rows of grad_merge have been sorted inside MergeAdd functor
Q
Qiao Longfei 已提交
437
        framework::SelectedRows* grad_merge_var;
S
sneaxiy 已提交
438
        scatter::MergeAdd<DeviceContext, T> merge_func;
439
        if (platform::is_cpu_place(ctx.GetPlace())) {
Q
Qiao Longfei 已提交
440
          grad_merge_var = &cpu_grad_merge;
441 442
        } else {
          // FIXME(qiao): GPU also need to fix this
Q
Qiao Longfei 已提交
443 444 445
          grad_merge_var = const_cast<framework::Scope&>(ctx.scope())
                               .Var()
                               ->GetMutable<framework::SelectedRows>();
446
        }
S
sneaxiy 已提交
447
        merge_func(ctx.template device_context<DeviceContext>(), grad,
448
                   grad_merge_var, true);
S
sneaxiy 已提交
449 450 451 452
        grad_merge_ptr = grad_merge_var;
      }

      auto& grad_merge = *grad_merge_ptr;
T
wip  
typhoonzero 已提交
453
      auto& grad_tensor = grad_merge.value();
T
wip  
typhoonzero 已提交
454
      const T* grad_data = grad_tensor.template data<T>();
S
sneaxiy 已提交
455 456
      const int64_t* rows = nullptr;
// When compiled without CUDA, the CUDAData() interface should not be
457 458
// provided.
#if defined(PADDLE_WITH_CUDA)
D
dzhwinter 已提交
459
      if (platform::is_gpu_place(ctx.GetPlace())) {
S
sneaxiy 已提交
460
        rows = grad_merge.rows().CUDAData(ctx.GetPlace());
D
dzhwinter 已提交
461
      } else {
462
#endif
S
sneaxiy 已提交
463
        rows = grad_merge.rows().data();
464
#if defined(PADDLE_WITH_CUDA)
D
dzhwinter 已提交
465
      }
466
#endif
T
wip  
typhoonzero 已提交
467
      auto row_numel = grad_tensor.numel() / grad_merge.rows().size();
T
wip  
typhoonzero 已提交
468

M
minqiyang 已提交
469 470
      if (platform::is_cpu_place(ctx.GetPlace())) {
        SparseAdamFunctor<T, CPUAdam> functor(
Q
Qiao Longfei 已提交
471 472 473 474 475 476 477 478
            beta1, beta2, epsilon, beta1_pow.template data<T>(),
            beta2_pow.template data<T>(), mom1.template data<T>(),
            mom1_out.template mutable_data<T>(ctx.GetPlace()),
            mom2.template data<T>(),
            mom2_out.template mutable_data<T>(ctx.GetPlace()),
            lr.template data<T>(), grad_data, param.template data<T>(),
            param_out.template mutable_data<T>(ctx.GetPlace()), rows, row_numel,
            grad_merge.rows().size(), lazy_mode);
479 480 481 482 483 484 485 486 487 488
        if (lazy_mode) {
          VLOG(3) << "run cpu lazy mode";
          size_t row_count = grad_merge.rows().size();
          std::vector<int64_t> cpu_rows(grad_merge.rows());
          for (size_t row_index = 0; row_index < row_count; ++row_index) {
            for (size_t offset = 0; offset < row_numel; ++offset) {
              size_t i = cpu_rows[row_index] * row_numel + offset;
              functor.adam_update(i, grad_data[row_index * row_numel + offset]);
            }
          }
489 490 491 492 493
        }
#ifndef _WIN32
        else if (FLAGS_inner_op_parallelism > 1 &&
                 min_row_size_to_use_multithread > 0 &&
                 param.dims()[0] > min_row_size_to_use_multithread) {
494 495
          VLOG(3) << "use multi thread, inner_op_parallelism="
                  << FLAGS_inner_op_parallelism
496
                  << " min_row_size_to_use_multithread="
497
                  << min_row_size_to_use_multithread;
Q
Qiao Longfei 已提交
498
          if (FLAGS_inner_op_parallelism > 10) {
499 500
            VLOG(1) << "FLAGS_inner_op_parallelism "
                    << FLAGS_inner_op_parallelism << " is two large!";
Q
Qiao Longfei 已提交
501
          }
502 503 504
          auto& grad_rows = grad_merge.rows();
          std::unordered_map<size_t, int> row_id_to_grad_row_offset;
          size_t param_row_count = param.numel() / row_numel;
Q
Qiao Longfei 已提交
505
          if (param_row_count < 1000) {
506 507 508
            VLOG(1) << "param_row_count should be larger then 1000 to use "
                       "multi thread, currently "
                    << param_row_count;
Q
Qiao Longfei 已提交
509
          }
510 511
          for (size_t i = 0; i < grad_rows.size(); ++i) {
            row_id_to_grad_row_offset[grad_rows[i]] = i;
Q
Qiao Longfei 已提交
512
          }
513
          std::vector<std::future<void>> fs;
Q
Qiao Longfei 已提交
514
          int64_t line_in_each_thread =
Q
Qiao Longfei 已提交
515
              param_row_count / FLAGS_inner_op_parallelism + 1;
516 517 518
          for (int i = 0; i < FLAGS_inner_op_parallelism; ++i) {
            int64_t start = i * line_in_each_thread;
            int64_t end = (i + 1) * line_in_each_thread;
Q
Qiao Longfei 已提交
519 520 521
            if (start >= param_row_count) {
              break;
            }
522 523
            if (end > param_row_count) {
              end = param_row_count;
Q
Qiao Longfei 已提交
524
            }
Q
Qiao Longfei 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
            fs.push_back(
                framework::Async([&functor, &row_id_to_grad_row_offset,
                                  &grad_data, row_numel, start, end]() {
                  for (int64_t row_id = start; row_id < end; ++row_id) {
                    auto iter = row_id_to_grad_row_offset.find(row_id);
                    if (iter != row_id_to_grad_row_offset.end()) {
                      for (size_t row_offset = 0U; row_offset < row_numel;
                           ++row_offset) {
                        functor.adam_update(
                            row_id * row_numel + row_offset,
                            grad_data[iter->second * row_numel + row_offset]);
                      }
                    } else {
                      for (size_t row_offset = 0U; row_offset < row_numel;
                           ++row_offset) {
                        functor.adam_update(row_id * row_numel + row_offset, 0);
                      }
                    }
Q
Qiao Longfei 已提交
543 544
                  }
                }));
Q
Qiao Longfei 已提交
545
          }
546
          for (size_t i = 0; i < fs.size(); ++i) fs[i].wait();
547 548 549
        }
#endif  // !_WIN32
        else {
550
          functor(param.numel());
Q
Qiao Longfei 已提交
551
        }
M
minqiyang 已提交
552 553 554 555 556 557 558 559 560
      } else if (platform::is_gpu_place(ctx.GetPlace())) {
        SparseAdamFunctor<T, GPUAdam> functor(
            beta1, beta2, epsilon, beta1_pow.template data<T>(),
            beta2_pow.template data<T>(), mom1.template data<T>(),
            mom1_out.template mutable_data<T>(ctx.GetPlace()),
            mom2.template data<T>(),
            mom2_out.template mutable_data<T>(ctx.GetPlace()),
            lr.template data<T>(), grad_data, param.template data<T>(),
            param_out.template mutable_data<T>(ctx.GetPlace()), rows, row_numel,
561
            grad_merge.rows().size(), lazy_mode);
M
minqiyang 已提交
562 563

        // FIXME(minqiyang): remove BinarySearch in GPU later
Q
Qiao Longfei 已提交
564 565 566 567 568
        platform::ForRange<DeviceContext> for_range(
            static_cast<const DeviceContext&>(ctx.device_context()),
            param.numel());
        for_range(functor);
      }
T
wip  
typhoonzero 已提交
569 570 571
    } else {
      PADDLE_THROW("Variable type not supported by adam_op");
    }
572 573 574 575 576
  }
};

}  // namespace operators
}  // namespace paddle