entrenar/finetune/instruct_pipeline/
cuda_init.rs1#[cfg(feature = "cuda")]
2use super::{
3 CudaTrainer, InstructConfig, InstructGpuTrainingState, InstructPipeline, LoRALayer,
4 Transformer, TransformerConfig, VramGuard,
5};
6
7#[cfg(feature = "cuda")]
8use crate::autograd::cuda_backward::pre_warm_lora_backward_kernels as pre_warm_backward_cache_kernels;
9#[cfg(feature = "cuda")]
10use crate::autograd::cuda_forward::{pre_warm_forward_kernels, pre_warm_lora_backward_kernels};
11#[cfg(feature = "cuda")]
12use crate::autograd::cuda_optim::pre_warm_lora_adamw_kernels;
13#[cfg(feature = "cuda")]
14use crate::autograd::cuda_training::cuda_training_available;
15#[cfg(feature = "cuda")]
16use crate::transformer::{
17 CudaBlock, CudaBlockScratch, CudaLoraGradWorkspace, CudaTransformerBlock, GpuLoraOptimizerState,
18};
19#[cfg(feature = "cuda")]
20use std::sync::Arc;
21
22#[cfg(feature = "cuda")]
23impl InstructPipeline {
24 pub(super) fn init_cuda(&mut self, model_config: &TransformerConfig) {
27 let budget_mb = Self::estimate_vram_mb(model_config, &self.config);
29 let task_label = if self.config.quantize_nf4 { "instruct-qlora" } else { "instruct-lora" };
30 match VramGuard::acquire(budget_mb, task_label) {
31 Ok(guard) => {
32 eprintln!(
33 "[GPU-SHARE] VRAM reserved: {budget_mb} MB for {task_label} (gpu: {})",
34 guard.gpu_uuid()
35 );
36 self.vram_guard = Some(guard);
37 }
38 Err(e) => {
39 eprintln!("[GPU-SHARE] VRAM guard denied: {e} — falling back to CPU");
40 return;
41 }
42 }
43
44 let (trainer, blocks, scratch) =
45 Self::try_init_cuda(&self.model, model_config, &self.config, &self.lora_layers);
46
47 if trainer.is_none() {
48 self.vram_guard = None;
50 return;
51 }
52
53 self.cuda_trainer = trainer;
54 self.cuda_blocks = blocks;
55 self.shared_scratch = scratch;
56
57 self.gpu_training = Self::try_init_gpu_training(
59 &self.model,
60 model_config,
61 self.config.max_seq_len,
62 self.cuda_trainer.as_ref(),
63 self.cuda_blocks.as_ref(),
64 );
65
66 if self.config.quantize_nf4 {
67 let (grad_ws, opt_states) = Self::try_init_nf4_lora_training(
68 self.cuda_trainer.as_ref(),
69 self.cuda_blocks.as_ref(),
70 model_config,
71 &self.config,
72 );
73 if let (Some(ws), Some(t)) = (&grad_ws, &self.cuda_trainer) {
74 self.lora_fused_clip =
75 super::super::fused_lora_clip::init_lora_fused_clip(ws, t.context());
76 }
77 self.cuda_lora_grad_workspace = grad_ws;
78 self.cuda_lora_optimizer_states = opt_states;
79 }
80
81 if let Some(ref mut guard) = self.vram_guard {
83 let _ = guard.update_actual(budget_mb);
84 }
85 }
86
87 fn estimate_vram_mb(model_config: &TransformerConfig, config: &InstructConfig) -> usize {
89 if config.quantize_nf4 {
90 let weight_elements =
91 model_config.per_layer_weight_elements() * model_config.num_hidden_layers;
92 let weight_mb = weight_elements / (2 * 1024 * 1024);
93 let scratch_mb =
94 (config.max_seq_len * model_config.hidden_size * 4 * 10) / (1024 * 1024);
95 weight_mb + scratch_mb + 512
96 } else {
97 model_config.total_training_vram_bytes_shared(config.max_seq_len) / (1024 * 1024) + 256
98 }
99 }
100
101 fn try_init_cuda(
104 model: &Transformer,
105 model_config: &TransformerConfig,
106 config: &InstructConfig,
107 lora_layers: &[LoRALayer],
108 ) -> (Option<CudaTrainer>, Option<Vec<CudaBlock>>, Option<CudaBlockScratch>) {
109 if !cuda_training_available() {
110 eprintln!("[CUDA] No CUDA runtime detected — using CPU");
111 return (None, None, None);
112 }
113
114 let trainer = match CudaTrainer::new() {
115 Ok(t) => {
116 eprintln!(
117 "[CUDA] Initialized: {} ({:.1} GB)",
118 t.device_name(),
119 t.total_memory() as f64 / 1e9
120 );
121 t
122 }
123 Err(e) => {
124 eprintln!("[CUDA] Failed to create trainer: {e} — using CPU");
125 return (None, None, None);
126 }
127 };
128
129 let ctx = Arc::clone(trainer.context());
130 let max_seq_len = config.max_seq_len;
131
132 if let Err(e) = pre_warm_forward_kernels(
134 model_config.hidden_size,
135 model_config.intermediate_size,
136 model_config.num_attention_heads,
137 model_config.num_kv_heads,
138 model_config.head_dim(),
139 max_seq_len,
140 ) {
141 eprintln!("[CUDA] Failed to pre-warm forward kernels: {e} — using CPU");
142 return (None, None, None);
143 }
144
145 let quantize_nf4 = config.quantize_nf4;
146 if quantize_nf4 {
147 eprintln!(
148 "[CUDA] NF4 quantization enabled — frozen weights will be 4-bit (~8x compression)"
149 );
150 }
151
152 let head_dim = model_config.head_dim();
153 if let Err(e) = pre_warm_lora_backward_kernels(
154 model_config.hidden_size,
155 model_config.num_attention_heads * head_dim,
156 model_config.num_kv_heads * head_dim,
157 max_seq_len,
158 config.lora_rank,
159 ) {
160 eprintln!("[CUDA] Failed to pre-warm LoRA backward kernels: {e} — using CPU");
161 return (None, None, None);
162 }
163
164 if let Err(e) = pre_warm_backward_cache_kernels(
165 model_config.hidden_size,
166 model_config.num_attention_heads * head_dim,
167 model_config.num_kv_heads * head_dim,
168 max_seq_len,
169 config.lora_rank,
170 model_config.intermediate_size,
171 model_config.num_attention_heads,
172 quantize_nf4,
173 ) {
174 eprintln!("[CUDA] Failed to pre-warm backward cache kernels: {e}");
175 eprintln!("[CUDA] STOP THE LINE: backward kernel pre-warming failed.");
176 eprintln!("[CUDA] This is a FATAL error — training will produce loss=0.0 if backward");
177 eprintln!("[CUDA] kernels are compiled during active GPU work (trueno#200).");
178 return (None, None, None);
179 }
180 eprintln!("[CUDA] Backward kernels pre-warmed successfully");
181 if let Err(e) = pre_warm_lora_adamw_kernels(
182 model_config.hidden_size,
183 model_config.num_attention_heads * head_dim,
184 model_config.num_kv_heads * head_dim,
185 config.lora_rank,
186 0, model_config.intermediate_size,
188 quantize_nf4,
189 ) {
190 eprintln!("[CUDA] Failed to pre-warm AdamW kernels: {e} — using CPU");
191 return (None, None, None);
192 }
193
194 let mut blocks = Vec::with_capacity(model.config.num_hidden_layers);
195 for (i, layer) in model.layers.iter().enumerate() {
196 let input_norm = layer.input_norm.weight.data();
197 let input_norm = input_norm.as_slice().expect("contiguous input_norm");
198 let post_attn_norm = layer.post_attn_norm.weight.data();
199 let post_attn_norm = post_attn_norm.as_slice().expect("contiguous post_attn_norm");
200 let w_q = layer.self_attn.w_q.data();
201 let w_q = w_q.as_slice().expect("contiguous w_q");
202 let w_k = layer.self_attn.w_k.data();
203 let w_k = w_k.as_slice().expect("contiguous w_k");
204 let w_v = layer.self_attn.w_v.data();
205 let w_v = w_v.as_slice().expect("contiguous w_v");
206 let w_o = layer.self_attn.w_o.data();
207 let w_o = w_o.as_slice().expect("contiguous w_o");
208 let w_gate = layer.ffn.w_gate.data();
209 let w_gate = w_gate.as_slice().expect("contiguous w_gate");
210 let w_up = layer.ffn.w_up.data();
211 let w_up = w_up.as_slice().expect("contiguous w_up");
212 let w_down = layer.ffn.w_down.data();
213 let w_down = w_down.as_slice().expect("contiguous w_down");
214
215 let result = if quantize_nf4 {
216 let lora_scale = config.lora_alpha / config.lora_rank as f32;
217 let lora_rank = config.lora_rank;
218 let q_lora_idx = i * 2;
219 let v_lora_idx = i * 2 + 1;
220
221 let q_a_data;
223 let q_b_data;
224 let q_lora = if q_lora_idx < lora_layers.len() {
225 q_a_data = lora_layers[q_lora_idx].lora_a().data();
226 q_b_data = lora_layers[q_lora_idx].lora_b().data();
227 Some((
228 q_a_data.as_slice().expect("contiguous lora_a_q"),
229 q_b_data.as_slice().expect("contiguous lora_b_q"),
230 ))
231 } else {
232 None
233 };
234
235 let v_a_data;
237 let v_b_data;
238 let v_lora = if v_lora_idx < lora_layers.len() {
239 v_a_data = lora_layers[v_lora_idx].lora_a().data();
240 v_b_data = lora_layers[v_lora_idx].lora_b().data();
241 Some((
242 v_a_data.as_slice().expect("contiguous lora_a_v"),
243 v_b_data.as_slice().expect("contiguous lora_b_v"),
244 ))
245 } else {
246 None
247 };
248
249 let q_norm_data = layer
251 .self_attn
252 .q_norm
253 .as_ref()
254 .map(|t| t.data().as_slice().expect("contiguous q_norm").to_vec());
255 let k_norm_data = layer
256 .self_attn
257 .k_norm
258 .as_ref()
259 .map(|t| t.data().as_slice().expect("contiguous k_norm").to_vec());
260
261 crate::transformer::CudaNf4TransformerBlock::new(
262 model_config,
263 i,
264 Arc::clone(&ctx),
265 input_norm,
266 post_attn_norm,
267 w_q,
268 w_k,
269 w_v,
270 w_o,
271 w_gate,
272 w_up,
273 w_down,
274 max_seq_len,
275 q_lora,
276 v_lora,
277 lora_scale,
278 lora_rank,
279 q_norm_data.as_deref(),
280 k_norm_data.as_deref(),
281 )
282 .map(CudaBlock::Nf4)
283 } else {
284 CudaTransformerBlock::new(
285 model_config,
286 i,
287 Arc::clone(&ctx),
288 input_norm,
289 post_attn_norm,
290 w_q,
291 w_k,
292 w_v,
293 w_o,
294 w_gate,
295 w_up,
296 w_down,
297 max_seq_len,
298 )
299 .map(CudaBlock::Fp32)
300 };
301
302 match result {
303 Ok(block) => blocks.push(block),
304 Err(e) => {
305 eprintln!(
306 "[CUDA] Failed to upload layer {i} to GPU: {e} — falling back to CPU"
307 );
308 return (None, None, None);
309 }
310 }
311 }
312
313 eprintln!(
314 "[CUDA] Uploaded {} transformer layers to GPU (max_seq_len={})",
315 blocks.len(),
316 max_seq_len
317 );
318
319 assert_eq!(blocks.len(), model.config.num_hidden_layers);
320 if std::env::var("FP16_GEMM").as_deref() == Ok("1") && quantize_nf4 {
322 super::super::gpu_backward_fallback::init_fp16_weights(&mut blocks, trainer.stream());
323 }
324
325 let shared_scratch = if quantize_nf4 {
327 match CudaBlockScratch::new(model_config, max_seq_len, &ctx, config.lora_rank) {
328 Ok(s) => Some(s),
329 Err(e) => {
330 eprintln!("[CUDA] Failed to allocate shared scratch: {e} — using CPU");
331 return (None, None, None);
332 }
333 }
334 } else {
335 None
336 };
337
338 (Some(trainer), Some(blocks), shared_scratch)
339 }
340
341 pub(super) fn try_init_gpu_training(
343 model: &Transformer,
344 model_config: &TransformerConfig,
345 max_seq_len: usize,
346 cuda_trainer: Option<&CudaTrainer>,
347 cuda_blocks: Option<&Vec<CudaBlock>>,
348 ) -> Option<InstructGpuTrainingState> {
349 let trainer = cuda_trainer?;
350 let blocks = cuda_blocks?;
351
352 let hidden_size = model_config.hidden_size;
353 let buf_size = max_seq_len * hidden_size;
354 let num_layers = blocks.len();
355
356 let mut layer_inputs = Vec::with_capacity(num_layers);
358 for _ in 0..num_layers {
359 match trainer.zeros(buf_size) {
360 Ok(buf) => layer_inputs.push(buf),
361 Err(e) => {
362 eprintln!("[CUDA] GPU training init failed (layer input alloc): {e}");
363 return None;
364 }
365 }
366 }
367
368 let norm_data = model.norm.weight.data();
370 let norm_slice = norm_data.as_slice().expect("contiguous final norm weight");
371 let final_norm_weight = match trainer.upload(norm_slice) {
372 Ok(buf) => buf,
373 Err(e) => {
374 eprintln!("[CUDA] GPU training init failed (final norm upload): {e}");
375 return None;
376 }
377 };
378
379 let blocks_output = trainer.zeros(buf_size).ok()?;
381 let grad_buf_a = trainer.zeros(buf_size).ok()?;
382 let grad_buf_b = trainer.zeros(buf_size).ok()?;
383 let grad_final_norm_weight = trainer.zeros(hidden_size).ok()?;
384
385 let vocab_size = model_config.vocab_size;
387 let embed_data = model.embed_tokens.weight.data();
388 let embed_slice = embed_data.as_slice().expect("contiguous embed");
389 let embed_bytes = vocab_size * hidden_size * 4; let vram_available_mb = trainer.free_memory_mb().unwrap_or(0);
391 let embed_mb = embed_bytes / (1024 * 1024);
392 let use_gpu_embed = vram_available_mb > (embed_mb + 256) as u64;
393
394 let (embed_original, embed_transposed) = if use_gpu_embed {
395 eprintln!(
396 "[CUDA] GPU-resident embeddings: {embed_mb}MB (VRAM free: {vram_available_mb}MB)"
397 );
398 let orig = trainer
399 .upload(embed_slice)
400 .map_err(|e| eprintln!("[CUDA] embed_original upload failed: {e}"))
401 .ok()?;
402 let trans = trainer.zeros(1).ok()?;
403 (orig, trans)
404 } else {
405 eprintln!("[CUDA] Skipping GPU embeddings ({embed_mb}MB > {vram_available_mb}MB free)");
406 let orig = trainer.zeros(1).ok()?;
407 let trans = trainer.zeros(1).ok()?;
408 (orig, trans)
409 };
410
411 let logits_buf = trainer
413 .zeros(max_seq_len * vocab_size)
414 .map_err(|e| eprintln!("[CUDA] logits_buf alloc failed: {e}"))
415 .ok()?;
416
417 let grad_hidden_buf = trainer.zeros(buf_size).ok()?;
419
420 eprintln!(
421 "[CUDA] GPU training state initialized: {num_layers} layers, {buf_size} buf_size, \
422 embed=[{vocab_size}x{hidden_size}] on GPU (NF4 QLoRA mode)"
423 );
424
425 let output_scratch = trainer.zeros(buf_size).ok()?;
427 let grad_upload_buf = trainer.zeros(buf_size).ok()?;
428 let fwd_scratch_a = trainer.zeros(buf_size).ok()?;
429 let fwd_scratch_b = trainer.zeros(buf_size).ok()?;
430 let lm_head_hidden_buf = trainer.zeros(buf_size).ok()?;
431
432 let num_layers = layer_inputs.len();
433 Some(InstructGpuTrainingState {
434 layer_inputs,
435 final_norm_weight,
436 blocks_output,
437 grad_buf_a,
438 grad_buf_b,
439 grad_final_norm_weight,
440 embed_transposed,
441 embed_original,
442 logits_buf,
443 grad_hidden_buf,
444 output_scratch,
445 grad_upload_buf,
446 fwd_scratch_a,
447 fwd_scratch_b,
448 lm_head_hidden_buf,
449 forward_graph_exec: None,
450 graph_cached_seq_len: 0,
451 backward_graph_state: None,
452 cublas_workspace: None,
453 profiler_layer_fwd_us: vec![0u64; num_layers],
454 profiler_layer_bwd_us: vec![0u64; num_layers],
455 profiler_layer_start: None,
456 profiler_op_us: [0u64; 16],
457 profiler_op_start: None,
458 })
459 }
460
461 fn try_init_nf4_lora_training(
463 cuda_trainer: Option<&CudaTrainer>,
464 cuda_blocks: Option<&Vec<CudaBlock>>,
465 model_config: &TransformerConfig,
466 config: &InstructConfig,
467 ) -> (Option<CudaLoraGradWorkspace>, Option<Vec<GpuLoraOptimizerState>>) {
468 let trainer = match cuda_trainer {
469 Some(t) => t,
470 None => return (None, None),
471 };
472 let blocks = match cuda_blocks {
473 Some(b) => b,
474 None => return (None, None),
475 };
476
477 let grad_ws =
478 match CudaLoraGradWorkspace::new(trainer.context(), model_config, config.lora_rank) {
479 Ok(ws) => ws,
480 Err(e) => {
481 eprintln!("[CUDA] NF4 LoRA grad workspace alloc failed: {e}");
482 return (None, None);
483 }
484 };
485
486 let mut opt_states = Vec::with_capacity(blocks.len());
487 for (i, block) in blocks.iter().enumerate() {
488 match block.init_lora_optimizer_state() {
489 Ok(state) => opt_states.push(state),
490 Err(e) => {
491 eprintln!("[CUDA] NF4 LoRA optimizer init failed (layer {i}): {e}");
492 return (None, None);
493 }
494 }
495 }
496
497 eprintln!(
498 "[CUDA] NF4 QLoRA training initialized: {} layers, rank={}, scale={:.2}",
499 blocks.len(),
500 config.lora_rank,
501 config.lora_alpha / config.lora_rank as f32,
502 );
503
504 (Some(grad_ws), Some(opt_states))
505 }
506}