|
632 | 632 | # does this *without* changing ``a`` - you can see that when we print |
633 | 633 | # ``a`` again at the end, it retains its ``requires_grad=True`` property. |
634 | 634 | # |
635 | | -# Moving to GPU |
| 635 | +# Moving to `Accelerator <https://pytorch.org/docs/stable/torch.html#accelerators>`__ |
636 | 636 | # ------------- |
637 | 637 | # |
638 | | -# One of the major advantages of PyTorch is its robust acceleration on |
639 | | -# CUDA-compatible Nvidia GPUs. (“CUDA” stands for *Compute Unified Device |
640 | | -# Architecture*, which is Nvidia’s platform for parallel computing.) So |
641 | | -# far, everything we’ve done has been on CPU. How do we move to the faster |
| 638 | +# One of the major advantages of PyTorch is its robust acceleration on an |
| 639 | +# `accelerator <https://pytorch.org/docs/stable/torch.html#accelerators>`__ |
| 640 | +# such as CUDA, MPS, MTIA, or XPU. |
| 641 | +# So far, everything we’ve done has been on CPU. How do we move to the faster |
642 | 642 | # hardware? |
643 | 643 | # |
644 | | -# First, we should check whether a GPU is available, with the |
| 644 | +# First, we should check whether an accelerator is available, with the |
645 | 645 | # ``is_available()`` method. |
646 | 646 | # |
647 | 647 | # .. note:: |
648 | | -# If you do not have a CUDA-compatible GPU and CUDA drivers |
649 | | -# installed, the executable cells in this section will not execute any |
650 | | -# GPU-related code. |
| 648 | +# If you do not have an accelerator, the executable cells in this section will not execute any |
| 649 | +# accelerator-related code. |
651 | 650 | # |
652 | 651 |
|
653 | | -if torch.cuda.is_available(): |
654 | | - print('We have a GPU!') |
| 652 | +if torch.accelerator.is_available(): |
| 653 | + print('We have an accelerator!') |
655 | 654 | else: |
656 | 655 | print('Sorry, CPU only.') |
657 | 656 |
|
658 | 657 |
|
659 | 658 | ########################################################################## |
660 | | -# Once we’ve determined that one or more GPUs is available, we need to put |
661 | | -# our data someplace where the GPU can see it. Your CPU does computation |
662 | | -# on data in your computer’s RAM. Your GPU has dedicated memory attached |
| 659 | +# Once we’ve determined that one or more accelerators is available, we need to put |
| 660 | +# our data someplace where the accelerator can see it. Your CPU does computation |
| 661 | +# on data in your computer’s RAM. Your accelerator has dedicated memory attached |
663 | 662 | # to it. Whenever you want to perform a computation on a device, you must |
664 | 663 | # move *all* the data needed for that computation to memory accessible by |
665 | 664 | # that device. (Colloquially, “moving the data to memory accessible by the |
|
669 | 668 | # may do it at creation time: |
670 | 669 | # |
671 | 670 |
|
672 | | -if torch.cuda.is_available(): |
673 | | - gpu_rand = torch.rand(2, 2, device='cuda') |
| 671 | +if torch.accelerator.is_available(): |
| 672 | + gpu_rand = torch.rand(2, 2, device=torch.accelerator.current_accelerator()) |
674 | 673 | print(gpu_rand) |
675 | 674 | else: |
676 | 675 | print('Sorry, CPU only.') |
677 | 676 |
|
678 | 677 |
|
679 | 678 | ########################################################################## |
680 | 679 | # By default, new tensors are created on the CPU, so we have to specify |
681 | | -# when we want to create our tensor on the GPU with the optional |
| 680 | +# when we want to create our tensor on the accelerator with the optional |
682 | 681 | # ``device`` argument. You can see when we print the new tensor, PyTorch |
683 | 682 | # informs us which device it’s on (if it’s not on CPU). |
684 | 683 | # |
685 | | -# You can query the number of GPUs with ``torch.cuda.device_count()``. If |
686 | | -# you have more than one GPU, you can specify them by index: |
| 684 | +# You can query the number of accelerators with ``torch.accelerator.device_count()``. If |
| 685 | +# you have more than one accelerator, you can specify them by index, take CUDA for example: |
687 | 686 | # ``device='cuda:0'``, ``device='cuda:1'``, etc. |
688 | 687 | # |
689 | 688 | # As a coding practice, specifying our devices everywhere with string |
690 | 689 | # constants is pretty fragile. In an ideal world, your code would perform |
691 | | -# robustly whether you’re on CPU or GPU hardware. You can do this by |
| 690 | +# robustly whether you’re on CPU or accelerator hardware. You can do this by |
692 | 691 | # creating a device handle that can be passed to your tensors instead of a |
693 | 692 | # string: |
694 | 693 | # |
695 | 694 |
|
696 | | -if torch.cuda.is_available(): |
697 | | - my_device = torch.device('cuda') |
698 | | -else: |
699 | | - my_device = torch.device('cpu') |
| 695 | +my_device = torch.accelerator.current_accelerator() if torch.accelerator.is_available() else torch.device('cpu') |
700 | 696 | print('Device: {}'.format(my_device)) |
701 | 697 |
|
702 | 698 | x = torch.rand(2, 2, device=my_device) |
|
718 | 714 | # It is important to know that in order to do computation involving two or |
719 | 715 | # more tensors, *all of the tensors must be on the same device*. The |
720 | 716 | # following code will throw a runtime error, regardless of whether you |
721 | | -# have a GPU device available: |
| 717 | +# have an accelerator device available, take CUDA for example: |
722 | 718 | # |
723 | 719 | # .. code-block:: python |
724 | 720 | # |
725 | 721 | # x = torch.rand(2, 2) |
726 | | -# y = torch.rand(2, 2, device='gpu') |
| 722 | +# y = torch.rand(2, 2, device='cuda') |
727 | 723 | # z = x + y # exception will be thrown |
728 | 724 | # |
729 | 725 |
|
|
0 commit comments