1515#include " infini_train/include/autograd/linear.h"
1616#include " infini_train/include/autograd/outer.h"
1717#include " infini_train/include/autograd/misc.h"
18+ #include " test_utils.h"
1819
1920using namespace infini_train ;
2021
@@ -380,17 +381,16 @@ TEST_F(AutogradForwardTest, NoOpForward) {
380381
381382#ifdef USE_CUDA
382383TEST_F (AutogradCudaTest, AddForwardCUDA) {
384+ REQUIRE_CUDA ();
383385 auto a = std::make_shared<Tensor>(std::vector<int64_t >{2 , 3 }, DataType::kFLOAT32 ,
384386 Device (Device::DeviceType::kCUDA , 0 ));
385387 a->set_requires_grad (true );
386- auto a_data = static_cast <float *>(a->DataPtr ());
387- for (int i = 0 ; i < 6 ; ++i) a_data[i] = 1 .0f ;
388+ infini_train::test::FillConstantTensor (a, 1 .0f );
388389
389390 auto b = std::make_shared<Tensor>(std::vector<int64_t >{2 , 3 }, DataType::kFLOAT32 ,
390391 Device (Device::DeviceType::kCUDA , 0 ));
391392 b->set_requires_grad (true );
392- auto b_data = static_cast <float *>(b->DataPtr ());
393- for (int i = 0 ; i < 6 ; ++i) b_data[i] = 2 .0f ;
393+ infini_train::test::FillConstantTensor (b, 2 .0f );
394394
395395 auto add_fn = std::make_shared<autograd::Add>();
396396 auto result = add_fn->Apply ({a, b});
@@ -399,17 +399,16 @@ TEST_F(AutogradCudaTest, AddForwardCUDA) {
399399}
400400
401401TEST_F (AutogradCudaTest, MatmulForwardCUDA) {
402+ REQUIRE_CUDA ();
402403 auto a = std::make_shared<Tensor>(std::vector<int64_t >{2 , 3 }, DataType::kFLOAT32 ,
403404 Device (Device::DeviceType::kCUDA , 0 ));
404405 a->set_requires_grad (true );
405- auto a_data = static_cast <float *>(a->DataPtr ());
406- for (int i = 0 ; i < 6 ; ++i) a_data[i] = 1 .0f ;
406+ infini_train::test::FillConstantTensor (a, 1 .0f );
407407
408408 auto b = std::make_shared<Tensor>(std::vector<int64_t >{3 , 4 }, DataType::kFLOAT32 ,
409409 Device (Device::DeviceType::kCUDA , 0 ));
410410 b->set_requires_grad (true );
411- auto b_data = static_cast <float *>(b->DataPtr ());
412- for (int i = 0 ; i < 12 ; ++i) b_data[i] = 1 .0f ;
411+ infini_train::test::FillConstantTensor (b, 1 .0f );
413412
414413 auto matmul_fn = std::make_shared<autograd::Matmul>();
415414 auto result = matmul_fn->Apply ({a, b});
@@ -418,23 +417,23 @@ TEST_F(AutogradCudaTest, MatmulForwardCUDA) {
418417}
419418
420419TEST_F (AutogradCudaTest, SumForwardCUDA) {
420+ REQUIRE_CUDA ();
421421 auto a = std::make_shared<Tensor>(std::vector<int64_t >{2 , 3 }, DataType::kFLOAT32 ,
422422 Device (Device::DeviceType::kCUDA , 0 ));
423423 a->set_requires_grad (true );
424- auto a_data = static_cast <float *>(a->DataPtr ());
425- for (int i = 0 ; i < 6 ; ++i) a_data[i] = 1 .0f ;
424+ infini_train::test::FillConstantTensor (a, 1 .0f );
426425
427426 auto sum_fn = std::make_shared<autograd::Sum>(1 , false );
428427 auto result = sum_fn->Apply ({a});
429428 EXPECT_EQ (result.size (), 1 );
430429}
431430
432431TEST_F (AutogradCudaTest, SoftmaxForwardCUDA) {
432+ REQUIRE_CUDA ();
433433 auto a = std::make_shared<Tensor>(std::vector<int64_t >{2 , 3 }, DataType::kFLOAT32 ,
434434 Device (Device::DeviceType::kCUDA , 0 ));
435435 a->set_requires_grad (true );
436- auto a_data = static_cast <float *>(a->DataPtr ());
437- for (int i = 0 ; i < 6 ; ++i) a_data[i] = 1 .0f ;
436+ infini_train::test::FillConstantTensor (a, 1 .0f );
438437
439438 auto softmax_fn = std::make_shared<autograd::Softmax>(1 );
440439 auto result = softmax_fn->Apply ({a});
@@ -443,23 +442,21 @@ TEST_F(AutogradCudaTest, SoftmaxForwardCUDA) {
443442}
444443
445444TEST_F (AutogradCudaTest, LinearForwardCUDA) {
445+ REQUIRE_CUDA ();
446446 auto input = std::make_shared<Tensor>(std::vector<int64_t >{2 , 3 }, DataType::kFLOAT32 ,
447447 Device (Device::DeviceType::kCUDA , 0 ));
448448 input->set_requires_grad (true );
449- auto input_data = static_cast <float *>(input->DataPtr ());
450- for (int i = 0 ; i < 6 ; ++i) input_data[i] = 1 .0f ;
449+ infini_train::test::FillConstantTensor (input, 1 .0f );
451450
452451 auto weight = std::make_shared<Tensor>(std::vector<int64_t >{4 , 3 }, DataType::kFLOAT32 ,
453452 Device (Device::DeviceType::kCUDA , 0 ));
454453 weight->set_requires_grad (true );
455- auto weight_data = static_cast <float *>(weight->DataPtr ());
456- for (int i = 0 ; i < 12 ; ++i) weight_data[i] = 1 .0f ;
454+ infini_train::test::FillConstantTensor (weight, 1 .0f );
457455
458456 auto bias = std::make_shared<Tensor>(std::vector<int64_t >{4 }, DataType::kFLOAT32 ,
459457 Device (Device::DeviceType::kCUDA , 0 ));
460458 bias->set_requires_grad (true );
461- auto bias_data = static_cast <float *>(bias->DataPtr ());
462- for (int i = 0 ; i < 4 ; ++i) bias_data[i] = 0 .0f ;
459+ infini_train::test::FillConstantTensor (bias, 0 .0f );
463460
464461 auto linear_fn = std::make_shared<autograd::Linear>();
465462 auto result = linear_fn->Apply ({input, weight, bias});
@@ -480,10 +477,9 @@ TEST_F(AutogradDistributedTest, AllReduceDistributed) {
480477 auto a = std::make_shared<Tensor>(std::vector<int64_t >{2 , 3 }, DataType::kFLOAT32 ,
481478 Device (Device::DeviceType::kCUDA , 0 ));
482479 a->set_requires_grad (true );
483- auto a_data = static_cast <float *>(a->DataPtr ());
484- for (int i = 0 ; i < 6 ; ++i) a_data[i] = 1 .0f ;
480+ infini_train::test::FillConstantTensor (a, 1 .0f );
485481
486- EXPECT_TRUE (a->IsCUDA ());
482+ EXPECT_TRUE (a->GetDevice (). IsCUDA ());
487483 EXPECT_TRUE (a->requires_grad ());
488484}
489485
@@ -494,10 +490,9 @@ TEST_F(AutogradDistributedTest, AllGatherDistributed) {
494490 auto a = std::make_shared<Tensor>(std::vector<int64_t >{4 , 4 }, DataType::kFLOAT32 ,
495491 Device (Device::DeviceType::kCUDA , 0 ));
496492 a->set_requires_grad (true );
497- auto a_data = static_cast <float *>(a->DataPtr ());
498- for (int i = 0 ; i < 16 ; ++i) a_data[i] = 1 .0f ;
493+ infini_train::test::FillConstantTensor (a, 1 .0f );
499494
500- EXPECT_TRUE (a->IsCUDA ());
495+ EXPECT_TRUE (a->GetDevice (). IsCUDA ());
501496 EXPECT_EQ (a->Dims (), (std::vector<int64_t >{4 , 4 }));
502497}
503498
@@ -508,10 +503,9 @@ TEST_F(AutogradDistributedTest, ReduceScatterDistributed) {
508503 auto a = std::make_shared<Tensor>(std::vector<int64_t >{2 , 8 }, DataType::kFLOAT32 ,
509504 Device (Device::DeviceType::kCUDA , 0 ));
510505 a->set_requires_grad (true );
511- auto a_data = static_cast <float *>(a->DataPtr ());
512- for (int i = 0 ; i < 16 ; ++i) a_data[i] = 1 .0f ;
506+ infini_train::test::FillConstantTensor (a, 1 .0f );
513507
514- EXPECT_TRUE (a->IsCUDA ());
508+ EXPECT_TRUE (a->GetDevice (). IsCUDA ());
515509 EXPECT_EQ (a->Dims (), (std::vector<int64_t >{2 , 8 }));
516510}
517511
@@ -530,7 +524,7 @@ TEST_F(AutogradDistributedTest, DistributedMatmul) {
530524 auto result = matmul_fn->Apply ({a, b});
531525
532526 EXPECT_EQ (result.size (), 1 );
533- EXPECT_TRUE (result[0 ]->IsCUDA ());
527+ EXPECT_TRUE (result[0 ]->GetDevice (). IsCUDA ());
534528}
535529
536530TEST_F (AutogradDistributedTest, DistributedLinear) {
@@ -552,6 +546,6 @@ TEST_F(AutogradDistributedTest, DistributedLinear) {
552546
553547 EXPECT_EQ (result.size (), 1 );
554548 EXPECT_EQ (result[0 ]->Dims (), (std::vector<int64_t >{2 , 4 }));
555- EXPECT_TRUE (result[0 ]->IsCUDA ());
549+ EXPECT_TRUE (result[0 ]->GetDevice (). IsCUDA ());
556550}
557551#endif // USE_NCCL
0 commit comments