Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] SYCL Backend support #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
get_embedding,
get_norm,
)


try:
import intel_extension_for_pytorch
def is_xpu_available():
return torch.xpu.is_available()
except Exception as e:
print(f"Building SYCL kernels requires either Pytorch>=2.4 or installation of IPEX to run on Intel GPUs")

def get_model(model):
import torch

Expand Down Expand Up @@ -78,7 +83,10 @@ def forward(self, inp, **kwargs):
layers[0] = layers[0].cpu()
for i in range(len(embeddings)):
embeddings[i] = embeddings[i].cpu()
torch.cuda.empty_cache()
if is_xpu_available():
torch.xpu.empty_cache()
else:
torch.cuda.empty_cache()

outs = torch.zeros_like(inps)
attention_mask = cache["attention_mask"]
Expand All @@ -103,7 +111,10 @@ def forward(self, inp, **kwargs):
)[0]
layers[i] = layer.cpu()
del layer
torch.cuda.empty_cache()
if is_xpu_available:
torch.xpu.empty_cache()
else:
torch.cuda.empty_cache()
inps, outs = outs, inps

norm = get_norm(model, model_type)
Expand Down Expand Up @@ -192,7 +203,10 @@ def benchmark(model, input_ids, check=False):
layers = get_layers(model, model_type)

input_ids = input_ids.to(model.gpus[0] if hasattr(model, "gpus") else DEV)
torch.cuda.synchronize()
if is_xpu_availble():
torch.xpu.synchronize()
else:
torch.cuda.synchronize(gpu)

cache = {"past": None}

Expand All @@ -215,9 +229,16 @@ def tmp(layer, inp, out):
def sync():
if hasattr(model, "gpus"):
for gpu in model.gpus:
torch.cuda.synchronize(gpu)
if is_xpu_availble():
torch.xpu.synchronize()
else:
torch.cuda.synchronize(gpu)
else:
torch.cuda.synchronize()
if is_xpu_availble():
torch.xpu.synchronize()
else:
torch.cuda.synchronize(gpu)


max_memory = 0
with torch.no_grad():
Expand All @@ -233,7 +254,10 @@ def sync():
sync()
times.append(time.time() - tick)
print(i, times[-1])
max_memory = max(max_memory, torch.cuda.memory_allocated() / 1024 / 1024)
if is_xpu_available:
max_memory = max(max_memory, torch.xpu.memory_allocated() / 1024 / 1024)
else:
max_memory = max(max_memory, torch.cuda.memory_allocated() / 1024 / 1024)
if check and i != input_ids.numel() - 1:
tot += loss(
out.logits[0].to(DEV), input_ids[:, (i + 1)].to(DEV)
Expand All @@ -252,7 +276,7 @@ def sync():
if __name__ == "__main__":
import argparse
from squeezellm.datautils import *

parser = argparse.ArgumentParser()

parser.add_argument("model", type=str, help="llama model to load")
Expand Down Expand Up @@ -304,8 +328,10 @@ def sync():
default=10,
help="Number of dense channel used for hybrid kernel.",
)

DEV = torch.device("cuda:0")
if is_xpu_available:
DEV = torch.device("xpu")
else:
DEV = torch.device("cuda:0")

args = parser.parse_args()

Expand Down Expand Up @@ -344,7 +370,7 @@ def sync():
with torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
torch.profiler.ProfilerActivity.CUDA
]
) as p:
benchmark(model, input_ids, check=args.check)
Expand Down
25 changes: 20 additions & 5 deletions squeezellm/quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
import torch
import torch.nn as nn
import math
import quant_cuda
try:
import intel_extension_for_pytorch
def is_xpu_available():
return torch.xpu.is_available()
except Exception as e:
print(f"Building SYCL kernels requires either Pytorch>=2.4 or installation of IPEX to run on Intel GPUs")

import quant_cuda


def round_to_nearest_pole_sim(w, poles):
"""
Expand Down Expand Up @@ -215,7 +222,10 @@ def forward(self, x):
y = self.bias.clone()
outshape[-1] = self.bias.numel()
else:
y = torch.zeros((self.outfeatures), device="cuda", dtype=torch.float32)
if is_xpu_available:
y = torch.zeros((self.outfeatures), device="xpu", dtype=torch.float32)
else:
y = torch.zeros((self.outfeatures), device="cuda", dtype=torch.float32)
outshape[-1] = self.outfeatures
dtype = x.dtype

Expand Down Expand Up @@ -313,9 +323,14 @@ def forward(self, x):
else:
out_shape = x.shape[:-1] + (self.outfeatures,)
x = x.reshape(-1, x.shape[-1])
out = torch.zeros(
(x.shape[0], self.outfeatures), device="cuda", dtype=torch.float32
)
if is_xpu_available:
out = torch.zeros(
(x.shape[0], self.outfeatures), device="xpu", dtype=torch.float32
)
else:
out = torch.zeros(
(x.shape[0], self.outfeatures), device="cuda", dtype=torch.float32
)
dtype = x.dtype
if self.bits == 3:
x = x.float()
Expand Down
Loading