Skip to content

A Python package for extending the official PyTorch that can easily obtain performance on Intel platform

License

Notifications You must be signed in to change notification settings

1pikachu/intel-extension-for-pytorch

 
 

Repository files navigation

Intel® Extension for PyTorch

Intel Extension for PyTorch is a Python package to extend official PyTorch. It is designed to make the Out-of-Box user experience of PyTorch CPU better while achieving good performance. The extension also will be the PR(Pull-Request) buffer for the Intel PyTorch framework dev team. The PR buffer will not only contain functions, but also optimization (for example, take advantage of Intel's new hardware features).

Installation

Install PyTorch from Source

  1. Get PyTorch v1.5.0-rc3 source(Refer to PyTorch guide for more details)

    git clone --recursive https://github.com/pytorch/pytorch
    cd pytorch
    
    # checkout source code to the specified version
    git checkout v1.5.0-rc3
    
    # update submodules for the specified pytorch version
    git submodule sync
    git submodule update --init --recursive
  2. Get Intel PyTorch Extension source

    git clone --recursive https://github.com/intel/intel-extension-for-pytorch
    cd intel-extension-for-pytorch
    
    # if you are updating an existing checkout
    git submodule sync
    git submodule update --init --recursive
  3. Add an new backend for Intel PyTorch Extension

    # Apply git patch to pytorch code
    cd ${intel_pytorch_extension_directory}
    git apply torch_patches/dpcpp-v1.5-rc3.patch ${pytorch_directory}
  4. Build and install PyTorch (Refer to PyTorch guide for more details)

    cd ${pytorch_directory}
    python setup.py install

Install Intel PyTorch Extension from Source

Install dependencies

pip install lark-parser hypothesis

Install the extension

cd ${intel_pytorch_extension_directory}
python setup.py install

Getting Started

The user just needs to convert the model and input tensors to the extension device, then the extension will be enabled automatically. Take an example, the code as follows is a model without the extension.

import torch
import torch.nn as nn

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear = nn.Linear(4, 5)

    def forward(self, input):
        return self.linear(input)

input = torch.randn(2, 4)
model = Model()
res = model(input)

If you want to explore the Intel PyTorch Extension, you just need to transform the above python script as follows.

import torch
import torch.nn as nn

# Import Intel PyTorch Extension
import intel_pytorch_extension

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear = nn.Linear(4, 5)

    def forward(self, input):
        return self.linear(input)

# Convert the input tensor to Intel PyTorch Extension device
input = torch.randn(2, 4).to('dpcpp')
# Convert the model to Intel PyTorch Extension device
model = Model().to('dpcpp')

res = model(input)

In addition, Intel PyTorch Extension can auto dispatch an OP to DNNL if the OP is supported with DNNL. Currently, the feature is not enabled by default. If you want to enable the feature, you can refine the above code as follows.

import torch
import torch.nn as nn

# Import Intel PyTorch Extension
import intel_pytorch_extension as ipex

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear = nn.Linear(4, 5)

    def forward(self, input):
        return self.linear(input)

# Convert the input tensor to Intel PyTorch Extension device
input = torch.randn(2, 4).to('dpcpp')
# Convert the model to Intel PyTorch Extension device
model = Model().to('dpcpp')

ipex.core.enable_auto_dnnl()
res = model(input)

Contribution

Please submit PR or issue to communicate with us or contribute code.

License

Apache License, Version 2.0. As found in LICENSE file.

About

A Python package for extending the official PyTorch that can easily obtain performance on Intel platform

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 69.8%
  • C++ 29.0%
  • Other 1.2%