build.rs 7.56 KB
Newer Older
Graham King's avatar
Graham King committed
1
2
3
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

4
5
6
use std::env;
use std::path::PathBuf;

7
8
9
10
// Environment variable names (build.rs can't import from runtime crate)
const DYNAMO_FATBIN_PATH: &str = "DYNAMO_FATBIN_PATH";
const OUT_DIR: &str = "OUT_DIR";

GuanLuo's avatar
GuanLuo committed
11
fn main() -> Result<(), Box<dyn std::error::Error>> {
12
13
14
    // Declare our custom cfg flag to avoid unexpected_cfgs warnings
    println!("cargo:rustc-check-cfg=cfg(have_vec_copy_fatbin)");

Ryan Olson's avatar
Ryan Olson committed
15
    println!("cargo:warning=Building with CUDA KV off");
16
17
18
19
20
    build_protos()?;

    // Get FATBIN path and copy it to OUT_DIR for embedding
    if let Some(fatbin_path) = find_fatbin_file() {
        // Copy FATBIN to OUT_DIR so we can include it with a predictable path
21
        let out_dir = env::var(OUT_DIR).unwrap();
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        let dest_path = PathBuf::from(out_dir).join("vectorized_copy.fatbin");

        if let Err(e) = std::fs::copy(&fatbin_path, &dest_path) {
            println!("cargo:warning=Failed to copy FATBIN to OUT_DIR: {}", e);
        } else {
            // Emit cfg flag for conditional compilation
            println!("cargo:rustc-cfg=have_vec_copy_fatbin");
            println!(
                "cargo:warning=CUDA FATBIN found at: {} - copied to OUT_DIR",
                fatbin_path.display()
            );
        }

        // Tell cargo to rerun if FATBIN file changes
        println!("cargo:rerun-if-changed={}", fatbin_path.display());
    } else {
        println!(
            "cargo:warning=CUDA FATBIN not found - run 'make fatbin' in cuda_kernels directory"
        );
        println!("cargo:warning=Set DYNAMO_FATBIN_PATH env var to specify custom location");
    }

    // Rerun build if environment variable changes
    println!("cargo:rerun-if-env-changed=DYNAMO_FATBIN_PATH");

    Ok(())
GuanLuo's avatar
GuanLuo committed
48
49
50
}

fn build_protos() -> Result<(), Box<dyn std::error::Error>> {
51
52
53
    tonic_build::configure()
        .type_attribute(".", "#[derive(serde::Serialize,serde::Deserialize)]")
        .compile_protos(&["kserve.proto"], &["src/grpc/protos"])?;
GuanLuo's avatar
GuanLuo committed
54
    Ok(())
55
56
}

57
58
fn find_fatbin_file() -> Option<PathBuf> {
    // 1. Check if user specified custom path via environment variable
59
    if let Ok(custom_path) = env::var(DYNAMO_FATBIN_PATH) {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        let fatbin_file = PathBuf::from(custom_path);
        if fatbin_file.exists() {
            println!(
                "cargo:warning=Using custom FATBIN path: {}",
                fatbin_file.display()
            );
            return Some(fatbin_file);
        } else {
            println!(
                "cargo:warning=Custom FATBIN path does not exist: {}",
                fatbin_file.display()
            );
        }
    }

    // 2. Check standard locations (priority order)
    let default_paths = [
        "./src/block_manager/block/transfer/kernels/vectorized_copy.fatbin", // Primary: Next to transfer module
    ];

    for path in &default_paths {
        let fatbin_file = PathBuf::from(path);
        if fatbin_file.exists() {
            println!(
                "cargo:warning=Found FATBIN at default location: {}",
                fatbin_file.display()
            );
            return Some(fatbin_file);
        }
    }

    None
}

Ryan Olson's avatar
Ryan Olson committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// NOTE: Preserving this build.rs for reference. We may want to re-enable
// custom kernel compilation in the future.

// #[cfg(not(feature = "cuda_kv"))]
// fn main() {}

// #[cfg(feature = "cuda_kv")]
// fn main() {
//     use std::{path::PathBuf, process::Command};

//     println!("cargo:rerun-if-changed=src/kernels/block_copy.cu");

//     // first do a which nvcc, if it is in the path
//     // if so, we don't need to set the cuda_lib
//     let nvcc = Command::new("which").arg("nvcc").output().unwrap();
//     let cuda_lib = if nvcc.status.success() {
//         println!("cargo:info=nvcc found in path");
//         // Extract the path from nvcc location by removing "bin/nvcc"
//         let nvcc_path = String::from_utf8_lossy(&nvcc.stdout).trim().to_string();
//         let path = PathBuf::from(nvcc_path);
//         if let Some(parent) = path.parent() {
//             // Remove "nvcc"
//             if let Some(cuda_root) = parent.parent() {
//                 // Remove "bin"
//                 cuda_root.to_string_lossy().to_string()
//             } else {
//                 // Fallback to CUDA_ROOT or default if path extraction fails
//                 get_cuda_root_or_default()
//             }
//         } else {
//             // Fallback to CUDA_ROOT or default if path extraction fails
//             get_cuda_root_or_default()
//         }
//     } else {
//         println!("cargo:warning=nvcc not found in path");
//         get_cuda_root_or_default()
//     };

//     println!("cargo:info=Using CUDA installation at: {}", cuda_lib);

//     let cuda_lib_path = PathBuf::from(&cuda_lib).join("lib64");
//     println!("cargo:info=Using CUDA libs: {}", cuda_lib_path.display());
//     println!("cargo:rustc-link-search=native={}", cuda_lib_path.display());

//     // Link against multiple CUDA libraries
//     println!("cargo:rustc-link-lib=dylib=cudart");
//     println!("cargo:rustc-link-lib=dylib=cuda");
//     println!("cargo:rustc-link-lib=dylib=cudadevrt");

//     // Make sure the CUDA libraries are found before other system libraries
//     println!(
//         "cargo:rustc-link-arg=-Wl,-rpath,{}",
//         cuda_lib_path.display()
//     );

//     // Create kernels directory for output if it doesn't exist
//     std::fs::create_dir_all("src/kernels").unwrap_or_else(|_| {
//         println!("Kernels directory already exists");
//     });

//     // Compile CUDA code
//     let output = Command::new("nvcc")
//         .arg("src/kernels/block_copy.cu")
//         .arg("-O3")
//         .arg("--compiler-options")
//         .arg("-fPIC")
//         .arg("-o")
//         .arg("src/kernels/libblock_copy.o")
//         .arg("-c")
//         .output()
//         .expect("Failed to compile CUDA code");

//     if !output.status.success() {
//         panic!(
//             "Failed to compile CUDA kernel: {}",
//             String::from_utf8_lossy(&output.stderr)
//         );
//     }

//     // Create static library
//     #[cfg(target_os = "windows")]
//     {
//         Command::new("lib")
//             .arg("/OUT:src/kernels/block_copy.lib")
//             .arg("src/kernels/libblock_copy.o")
//             .output()
//             .expect("Failed to create static library");
//         println!("cargo:rustc-link-search=native=src/kernels");
//         println!("cargo:rustc-link-lib=static=block_copy");
//     }

//     #[cfg(not(target_os = "windows"))]
//     {
//         Command::new("ar")
//             .arg("rcs")
//             .arg("src/kernels/libblock_copy.a")
//             .arg("src/kernels/libblock_copy.o")
//             .output()
//             .expect("Failed to create static library");
//         println!("cargo:rustc-link-search=native=src/kernels");
//         println!("cargo:rustc-link-lib=static=block_copy");
//         println!("cargo:rustc-link-lib=dylib=cudart");
//         println!("cargo:rustc-link-lib=dylib=cuda");
//         println!("cargo:rustc-link-lib=dylib=cudadevrt");
//     }
// }

// #[cfg(feature = "cuda_kv")]
// fn get_cuda_root_or_default() -> String {
//     match std::env::var("CUDA_ROOT") {
//         Ok(path) => path,
//         Err(_) => {
//             // Default locations based on OS
//             if cfg!(target_os = "windows") {
//                 "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8".to_string()
//             } else {
//                 "/usr/local/cuda".to_string()
//             }
//         }
//     }
// }