GithubHelp home page GithubHelp logo

Comments (1)

mobicham avatar mobicham commented on August 16, 2024

Thanks! It should be similar to .cuda() but instead would use .to('cpu'):

hqq/hqq/core/quantize.py

Lines 472 to 535 in b1a7c06

def cuda(self, device):
self.meta["compute_dtype"] = self.compute_dtype
if type(self.W_q) == nn.parameter.Parameter:
self.W_q.data, self.meta = Quantizer.cuda(self.W_q.data, self.meta, device)
else:
self.W_q, self.meta = Quantizer.cuda(self.W_q, self.meta, device)
if self.meta["quant_zero"]:
if "zero_q" in self.meta:
self.meta["zero_q"], self.meta["meta_zero"] = Quantizer.cuda(
self.meta["zero_q"], self.meta["meta_zero"], device
)
else:
_, self.meta["meta_zero"] = Quantizer.cuda(
None, self.meta["meta_zero"], device
)
elif "zero" in self.meta:
self.meta["zero"] = self.meta["zero"].to(device)
if self.meta["quant_scale"]:
if "scale_q" in self.meta:
self.meta["scale_q"], self.meta["meta_scale"] = Quantizer.cuda(
self.meta["scale_q"], self.meta["meta_scale"], device
)
else:
_, self.meta["meta_scale"] = Quantizer.cuda(
None, self.meta["meta_scale"], device
)
elif "scale" in self.meta:
self.meta["scale"] = self.meta["scale"].to(device)
# #Use zero/scale with streams for dequantization is faster than packing in "zero_scale"
# for key in ["zero", "zero_q", "scale", "scale_q"]:
# if((key in self.meta) and self.offload_meta):
# self.meta[key] = self.meta[key].contiguous().cpu().pin_memory()
if self.offload_meta:
if "zero_scale" not in self.meta:
if self.meta["quant_scale"] and self.meta["quant_zero"]:
self.meta["zero_scale"] = torch.stack(
(self.meta["zero_q"], self.meta["scale_q"])
)
del self.meta["scale_q"], self.meta["zero_q"]
else:
self.meta["zero_scale"] = torch.stack(
(self.meta["zero"], self.meta["scale"])
).to(self.compute_dtype)
del self.meta["scale"], self.meta["zero"]
self.meta["zero_scale"] = (
self.meta["zero_scale"].contiguous().cpu().pin_memory()
)
if self.bias is not None:
self.bias = self.bias.to(device=device, dtype=self.compute_dtype)
self.W_q = nn.Parameter(self.W_q, requires_grad=False)
self.device = device
self.in_gpu = True
torch.cuda.empty_cache()
return self

RIght now it is a mess because we support quantizing the scale/zero values and support offloading them to the cpu.
I think in the future we are gonna remove this which should make things much easier: #93 (comment)

May I ask why would need the .cpu() call? If you just want to use HQQLinear with cpu, you can just pass HQQLinear(...device='cpu')

from hqq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.