@@ -1276,6 +1276,186 @@ TEST_F(HirAlias, ThrowOnInputAlias) {
1276
1276
EXPECT_ANY_THROW (HostIrEvaluator hie (std::move (hic)));
1277
1277
}
1278
1278
1279
+ using HirSetTest = NVFuserTest;
1280
+
1281
+ TEST_F (HirSetTest, HostIr) {
1282
+ const std::vector<int64_t > sizes = {8 , 64 };
1283
+
1284
+ auto hic = std::make_unique<HostIrContainer>();
1285
+ FusionGuard fg (hic.get ());
1286
+
1287
+ auto * in = makeConcreteTensor (sizes);
1288
+ auto * out = makeConcreteTensor (sizes);
1289
+ auto * set = IrBuilder::create<LoadStoreOp>(LoadStoreOpType::Set, out, in);
1290
+ hic->addInput (in);
1291
+ hic->addInput (out);
1292
+ hic->pushBackTopLevelExprs (set);
1293
+
1294
+ HostIrEvaluator hie (std::move (hic));
1295
+
1296
+ auto options = at::TensorOptions ().device (at::kCUDA , 0 );
1297
+ auto in_aten = at::randn (sizes, options);
1298
+ auto out_aten = at::empty (sizes, options);
1299
+
1300
+ hie.runWithInput ({{in, in_aten}, {out, out_aten}});
1301
+
1302
+ EXPECT_TRUE (out_aten.equal (in_aten))
1303
+ << " Obtained output: " << out_aten << " \n "
1304
+ << " Expected output: " << in_aten;
1305
+ }
1306
+
1307
+ class HirBinaryOpTest : public NVFuserFixtureParamTest <BinaryOpType> {
1308
+ protected:
1309
+ at::Tensor executeBinaryOp (at::Tensor lhs, at::Tensor rhs) {
1310
+ switch (GetParam ()) {
1311
+ case BinaryOpType::Add:
1312
+ return lhs + rhs;
1313
+ case BinaryOpType::Sub:
1314
+ return lhs - rhs;
1315
+ case BinaryOpType::Mul:
1316
+ return lhs * rhs;
1317
+ case BinaryOpType::Div:
1318
+ return lhs / rhs;
1319
+ default :
1320
+ NVF_ERROR (" Unsupported binary op type " , GetParam ());
1321
+ return at::Tensor ();
1322
+ }
1323
+ }
1324
+ };
1325
+
1326
+ TEST_P (HirBinaryOpTest, PreAllocatedOutputs) {
1327
+ const std::vector<int64_t > sizes = {8 , 64 };
1328
+ const auto & binary_op_type = GetParam ();
1329
+
1330
+ auto hic = std::make_unique<HostIrContainer>();
1331
+ FusionGuard fg (hic.get ());
1332
+
1333
+ auto * lhs = makeConcreteTensor (sizes);
1334
+ auto * rhs = makeConcreteTensor (sizes);
1335
+ auto * out = makeConcreteTensor (sizes);
1336
+ auto * binary_op = IrBuilder::create<BinaryOp>(binary_op_type, out, lhs, rhs);
1337
+ hic->addInput (lhs);
1338
+ hic->addInput (rhs);
1339
+ hic->addInput (out);
1340
+ hic->pushBackTopLevelExprs (binary_op);
1341
+
1342
+ HostIrEvaluator hie (std::move (hic));
1343
+
1344
+ auto options = at::TensorOptions ().device (at::kCUDA , 0 );
1345
+ auto lhs_aten = at::randn (sizes, options);
1346
+ auto rhs_aten = at::randn (sizes, options);
1347
+ auto out_aten = at::empty (sizes, options);
1348
+
1349
+ hie.runWithInput ({{lhs, lhs_aten}, {rhs, rhs_aten}, {out, out_aten}});
1350
+
1351
+ at::Tensor expected_out = executeBinaryOp (lhs_aten, rhs_aten);
1352
+ EXPECT_TRUE (expected_out.equal (out_aten))
1353
+ << " Obtained output: " << out_aten << " \n "
1354
+ << " Expected output: " << expected_out;
1355
+ }
1356
+
1357
+ TEST_P (HirBinaryOpTest, NonPreAllocatedOutputs) {
1358
+ const std::vector<int64_t > sizes = {8 , 64 };
1359
+ const auto & binary_op_type = GetParam ();
1360
+
1361
+ auto hic = std::make_unique<HostIrContainer>();
1362
+ FusionGuard fg (hic.get ());
1363
+
1364
+ auto * lhs = makeConcreteTensor (sizes);
1365
+ auto * rhs = makeConcreteTensor (sizes);
1366
+ auto * out = binaryOp (binary_op_type, lhs, rhs);
1367
+ hic->addInput (lhs);
1368
+ hic->addInput (rhs);
1369
+ hic->addOutput (out);
1370
+ hic->pushBackTopLevelExprs (out->definition ());
1371
+
1372
+ HostIrEvaluator hie (std::move (hic));
1373
+
1374
+ auto options = at::TensorOptions ().device (at::kCUDA , 0 );
1375
+ auto lhs_aten = at::randn (sizes, options);
1376
+ auto rhs_aten = at::randn (sizes, options);
1377
+
1378
+ auto out_aten =
1379
+ hie.runWithInput ({{lhs, lhs_aten}, {rhs, rhs_aten}})[0 ].as <at::Tensor>();
1380
+
1381
+ at::Tensor expected_out = executeBinaryOp (lhs_aten, rhs_aten);
1382
+ EXPECT_TRUE (expected_out.equal (out_aten))
1383
+ << " Obtained output: " << out_aten << " \n "
1384
+ << " Expected output: " << expected_out;
1385
+ }
1386
+
1387
+ INSTANTIATE_TEST_SUITE_P (
1388
+ ,
1389
+ HirBinaryOpTest,
1390
+ testing::Values (
1391
+ BinaryOpType::Add,
1392
+ BinaryOpType::Sub,
1393
+ BinaryOpType::Mul,
1394
+ BinaryOpType::Div),
1395
+ [](const testing::TestParamInfo<BinaryOpType>& info) -> std::string {
1396
+ std::stringstream ss;
1397
+ ss << " BinaryOpType_" << info.param ;
1398
+ return ss.str ();
1399
+ });
1400
+
1401
+ using HirReductionOpTest = NVFuserTest;
1402
+
1403
+ TEST_F (HirReductionOpTest, PreAllocatedOutputs) {
1404
+ constexpr int64_t size0 = 8 , size1 = 64 ;
1405
+ constexpr int64_t reduction_axis = 1 ;
1406
+
1407
+ auto hic = std::make_unique<HostIrContainer>();
1408
+ FusionGuard fg (hic.get ());
1409
+
1410
+ auto * in = makeConcreteTensor ({size0, size1});
1411
+ auto * out = newForReduction (in, {reduction_axis}, in->dtype ());
1412
+ auto * reduction_op = IrBuilder::create<ReductionOp>(
1413
+ BinaryOpType::Add, hic->zeroVal (), out, in);
1414
+ hic->addInput (in);
1415
+ hic->addOutput (out);
1416
+ hic->pushBackTopLevelExprs (reduction_op);
1417
+
1418
+ HostIrEvaluator hie (std::move (hic));
1419
+
1420
+ auto options = at::TensorOptions ().device (at::kCUDA , 0 );
1421
+ auto in_aten = at::randn ({size0, size1}, options);
1422
+ auto out_aten = at::empty ({size0}, options);
1423
+
1424
+ hie.runWithInput ({{in, in_aten}, {out, out_aten}});
1425
+
1426
+ at::Tensor expected_out = in_aten.sum (reduction_axis);
1427
+ EXPECT_TRUE (expected_out.equal (out_aten))
1428
+ << " Obtained output: " << out_aten << " \n "
1429
+ << " Expected output: " << expected_out;
1430
+ }
1431
+
1432
+ TEST_F (HirReductionOpTest, NonPreAllocatedOutputs) {
1433
+ constexpr int64_t size0 = 8 , size1 = 64 ;
1434
+ constexpr int64_t reduction_axis = 1 ;
1435
+
1436
+ auto hic = std::make_unique<HostIrContainer>();
1437
+ FusionGuard fg (hic.get ());
1438
+
1439
+ auto * in = makeConcreteTensor ({size0, size1});
1440
+ auto * out = sum (in, {reduction_axis});
1441
+ hic->addInput (in);
1442
+ hic->addOutput (out);
1443
+ hic->pushBackTopLevelExprs (out->definition ());
1444
+
1445
+ HostIrEvaluator hie (std::move (hic));
1446
+
1447
+ auto options = at::TensorOptions ().device (at::kCUDA , 0 );
1448
+ auto in_aten = at::randn ({size0, size1}, options);
1449
+ auto out_aten = at::empty ({size0}, options);
1450
+
1451
+ hie.runWithInput ({{in, in_aten}, {out, out_aten}});
1452
+
1453
+ at::Tensor expected_out = in_aten.sum (reduction_axis);
1454
+ EXPECT_TRUE (expected_out.equal (out_aten))
1455
+ << " Obtained output: " << out_aten << " \n "
1456
+ << " Expected output: " << expected_out;
1457
+ }
1458
+
1279
1459
} // namespace hir
1280
1460
1281
1461
} // namespace nvfuser
0 commit comments