Spaces:
Sleeping
Sleeping
Codex Claude Opus 4.8 commited on
Commit ·
c1939e1
1
Parent(s): f4b9db0
ZeroGPU: move models to CUDA inside @gpu calls, not in the main process
Browse filesZeroGPU forbids real CUDA init outside an @gpu allocation. load() now keeps
models on CPU (no .cuda(), no device_map=auto, no pipe.to('cuda')); the move to
CUDA happens inside _minicpm_generate/_nemotron_generate/_run_art_pipe.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Files changed (3)
64 unmodified lines6566676869707172734 unmodified lines78798081828364 unmodified linesimport torchfrom diffusers import AutoPipelineForText2Imagepipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=best_torch_dtype(torch))pipe.set_progress_bar_config(disable=True)pipe = move_pipe_to_device(pipe, torch)return cls(pipe, steps=steps, guidance_scale=guidance_scale, width=width, height=height)4 unmodified lines# so the forked GPU worker inherits it; returns a data URI string (picklable).@gpudef _run_art_pipe(prompt: str, steps: int, guidance_scale: float, width: int, height: int) -> str:result = _art_pipe(prompt=prompt,num_inference_steps=steps,64 unmodified lines6566676869707172734 unmodified lines7879808182838464 unmodified linesimport torchfrom diffusers import AutoPipelineForText2Image# Stay on CPU here: on ZeroGPU the move to CUDA happens in _run_art_pipe.pipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=best_torch_dtype(torch))pipe.set_progress_bar_config(disable=True)return cls(pipe, steps=steps, guidance_scale=guidance_scale, width=width, height=height)4 unmodified lines# so the forked GPU worker inherits it; returns a data URI string (picklable).@gpudef _run_art_pipe(prompt: str, steps: int, guidance_scale: float, width: int, height: int) -> str:_art_pipe.to("cuda")result = _art_pipe(prompt=prompt,num_inference_steps=steps,
98 unmodified lines991001011021031041051065 unmodified lines11211311411511611763 unmodified lines1811821831841851861871886 unmodified lines19519619719819920098 unmodified lines) -> "NemotronTransformersChatClient": # pragma: no coverfrom transformers import AutoModelForCausalLM, AutoTokenizertokenizer = AutoTokenizer.from_pretrained(model_path)model = AutoModelForCausalLM.from_pretrained(model_path, **transformers_model_kwargs())return cls(model.eval(), tokenizer, max_new_tokens=max_new_tokens, temperature=temperature)5 unmodified lines# globals so the forked GPU worker inherits it (only strings cross the boundary).@gpudef _nemotron_generate(system: str, user: str, max_new_tokens: int, temperature: float) -> str:messages = chat_messages(system, user)inputs = _nemotron_tokenizer.apply_chat_template(messages,63 unmodified linesattn_implementation="sdpa",torch_dtype=local_torch_dtype(torch),)if torch.cuda.is_available():model = model.cuda()tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)return cls(model.eval(), tokenizer, max_new_tokens=max_new_tokens, temperature=temperature)6 unmodified lines# globals so the forked GPU worker inherits it (only strings cross the boundary).@gpudef _minicpm_generate(system: str, user: str, max_new_tokens: int, temperature: float) -> str:return str(_minicpm_model.chat(msgs=[{"role": "user", "content": user}],98 unmodified lines991001011021031041051061071085 unmodified lines11411511611711811912063 unmodified lines1841851861871881891901916 unmodified lines19819920020120220320498 unmodified lines) -> "NemotronTransformersChatClient": # pragma: no coverfrom transformers import AutoModelForCausalLM, AutoTokenizer# Load on CPU (no device_map="auto"): on ZeroGPU the move to CUDA must# happen inside the @gpu call, in _nemotron_generate.tokenizer = AutoTokenizer.from_pretrained(model_path)model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype="auto")return cls(model.eval(), tokenizer, max_new_tokens=max_new_tokens, temperature=temperature)5 unmodified lines# globals so the forked GPU worker inherits it (only strings cross the boundary).@gpudef _nemotron_generate(system: str, user: str, max_new_tokens: int, temperature: float) -> str:_nemotron_model.to("cuda")messages = chat_messages(system, user)inputs = _nemotron_tokenizer.apply_chat_template(messages,63 unmodified linesattn_implementation="sdpa",torch_dtype=local_torch_dtype(torch),)# Stay on CPU here: on ZeroGPU the GPU only exists inside @gpu calls,# so the move to CUDA happens in _minicpm_generate.tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)return cls(model.eval(), tokenizer, max_new_tokens=max_new_tokens, temperature=temperature)6 unmodified lines# globals so the forked GPU worker inherits it (only strings cross the boundary).@gpudef _minicpm_generate(system: str, user: str, max_new_tokens: int, temperature: float) -> str:_minicpm_model.to("cuda")return str(_minicpm_model.chat(msgs=[{"role": "user", "content": user}],
71 unmodified lines7273747576772 unmodified lines80818283848571 unmodified linesclass FakeModel:device = "cuda"# Return a generated token sequence with prompt prefix included.def generate(self, inputs: FakeInputs, **kwargs: Any) -> list[list[int]]:self.inputs = inputs2 unmodified linesclass FakeMiniCPMModel:# Capture MiniCPM chat kwargs.def chat(self, **kwargs: Any) -> str:self.kwargs = kwargs71 unmodified lines727374757677787980812 unmodified lines8485868788899091929371 unmodified linesclass FakeModel:device = "cuda"# No-op device move (ZeroGPU does the real one inside the @gpu call).def to(self, device: str) -> "FakeModel":return self# Return a generated token sequence with prompt prefix included.def generate(self, inputs: FakeInputs, **kwargs: Any) -> list[list[int]]:self.inputs = inputs2 unmodified linesclass FakeMiniCPMModel:# No-op device move (ZeroGPU does the real one inside the @gpu call).def to(self, device: str) -> "FakeMiniCPMModel":return self# Capture MiniCPM chat kwargs.def chat(self, **kwargs: Any) -> str:self.kwargs = kwargs
art.py+2-1
local_llm.py+7-3
tests/test_local_llm.py+8