| 426 | |
| 427 | template <typename T, typename PlaceType> |
| 428 | void TestFusedAdamBase(const std::vector<std::vector<int64_t>> &shapes, |
| 429 | float atol, |
| 430 | bool use_adamw, |
| 431 | bool amsgrad, |
| 432 | bool multi_precision = false, |
| 433 | float beta1 = 0.9, |
| 434 | float beta2 = 0.99, |
| 435 | float weight_decay = 0.1, |
| 436 | size_t steps = 5, |
| 437 | uint64_t seed = 10) { |
| 438 | const auto &ctx = *phi::DeviceContextPool::Instance().GetByPlace(PlaceType()); |
| 439 | using Context = typename std::remove_const< |
| 440 | typename std::remove_pointer<decltype(&ctx)>::type>::type; |
| 441 | ctx.GetGenerator()->SetCurrentSeed(seed); |
| 442 | AdamInfo<T, Context> info1(ctx, |
| 443 | shapes, |
| 444 | beta1, |
| 445 | beta2, |
| 446 | weight_decay, |
| 447 | multi_precision, |
| 448 | use_adamw, |
| 449 | amsgrad); |
| 450 | auto info2 = AdamInfo<T, Context>::DeepCopy(info1); |
| 451 | |
| 452 | for (size_t i = 0; i < steps; ++i) { |
| 453 | auto grads = GenerateRandomTensorVectors<T>(ctx, shapes); |
| 454 | info1.Update(false, grads); |
| 455 | info2.Update(true, grads); |
| 456 | } |
| 457 | |
| 458 | using MT = typename decltype(info1)::MT; |
| 459 | |
| 460 | #define PD_ADAM_TEST_COMP(__field, __dtype) \ |
| 461 | do { \ |
| 462 | MT __diff = MaxDiff<__dtype>(ctx, info1.__field, info2.__field); \ |
| 463 | EXPECT_LE(__diff, static_cast<MT>(atol)) \ |
| 464 | << #__field << " has diff when use_adamw = " << use_adamw \ |
| 465 | << " , multi_precision = " << multi_precision; \ |
| 466 | } while (0) |
| 467 | |
| 468 | PD_ADAM_TEST_COMP(beta1_pows, MT); |
| 469 | PD_ADAM_TEST_COMP(beta2_pows, MT); |
| 470 | PD_ADAM_TEST_COMP(params, T); |
| 471 | PD_ADAM_TEST_COMP(master_params, MT); |
| 472 | PD_ADAM_TEST_COMP(moment1s, MT); |
| 473 | PD_ADAM_TEST_COMP(moment2s, MT); |
| 474 | PD_ADAM_TEST_COMP(moment2s_max, MT); |
| 475 | } |
| 476 | |
| 477 | static auto GenerateRandomShapes(size_t n, uint64_t low, uint64_t high) { |
| 478 | std::random_device device; |
nothing calls this directly
no test coverage detected