File size: 15,667 Bytes
67c4f10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 48 49 50 51 52 53 54 55 56 57 58 59 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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | #include "parvulus_c_api.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <chrono>
#include <iomanip>
#include <numeric>
#include <algorithm>
#include <cstring>
#include <cstdlib>
void print_help(const char* prog) {
std::cout << "====================================================================\n";
std::cout << " PARVULUS STANDALONE PURE CUDA RUNTIME CLI (Vector-First)\n";
std::cout << " Zero PyTorch, Zero LibTorch, Zero Python, Pure CUDA/cuBLAS Engine\n";
std::cout << "====================================================================\n\n";
std::cout << "Usage: " << prog << " [options]\n\n";
std::cout << "Options:\n";
std::cout << " --device <int> CUDA GPU device ID (default: 0)\n";
std::cout << " --weights <path> Path to parvulus_model_weights.bin\n";
std::cout << " --embeddings <path> Path to task_embeddings.bin (optional lookup table)\n";
std::cout << " --scale <path> Path to action_scale.bin (default: action_scale.bin)\n";
std::cout << " --nfe <int> Number of Euler ODE flow steps (default: 10)\n";
std::cout << "\nTask Conditioning (Vector-First):\n";
std::cout << " --task_vector_file <path> Load 256-dim task vector from file (.bin, .npy, .txt)\n";
std::cout << " --task_vector \"<f1,f2,...>\" Pass 256-dim float vector directly as string\n";
std::cout << " --task <str> Task instruction text (looks up in embeddings table)\n";
std::cout << "\nInputs & Outputs:\n";
std::cout << " --agent_img <path> Path to 256x256x3 uint8 raw RGB agentview image\n";
std::cout << " --wrist_img <path> Path to 256x256x3 uint8 raw RGB wrist image\n";
std::cout << " --output <path> File path to write predicted 8x7 action trajectory\n";
std::cout << " --benchmark <N> Run N chunk benchmark iterations and report latency\n";
std::cout << " -h, --help Show this help message and exit\n\n";
std::cout << "Examples:\n";
std::cout << " # 1. Run inference using a direct 256-dim task vector:\n";
std::cout << " " << prog << " --task_vector_file embedding.bin\n\n";
std::cout << " # 2. Run inference with a perturbed instruction vector or numpy array:\n";
std::cout << " " << prog << " --task_vector_file perturbed_emb.npy --output actions.txt\n\n";
std::cout << " # 3. High-throughput 100-chunk benchmark:\n";
std::cout << " " << prog << " --benchmark 100\n";
std::cout << "====================================================================\n";
}
bool file_exists(const std::string& path) {
std::ifstream f(path.c_str());
return f.good();
}
std::string find_file(const std::string& filename) {
if (file_exists(filename)) return filename;
if (file_exists("c_engine/" + filename)) return "c_engine/" + filename;
if (file_exists("../c_engine/" + filename)) return "../c_engine/" + filename;
return filename;
}
bool load_vector_from_file(const std::string& path, std::vector<float>& out_vec) {
out_vec.resize(256, 0.0f);
std::ifstream fb(path, std::ios::binary);
if (!fb.is_open()) return false;
fb.seekg(0, std::ios::end);
size_t sz = fb.tellg();
fb.seekg(0, std::ios::beg);
// 1. Raw float binary (256 * 4 = 1024 bytes)
if (sz == 256 * sizeof(float)) {
fb.read(reinterpret_cast<char*>(out_vec.data()), 256 * sizeof(float));
return true;
}
// 2. NumPy .npy format
if (sz >= 1024 && path.size() >= 4 && path.substr(path.size() - 4) == ".npy") {
fb.seekg(sz - 256 * sizeof(float), std::ios::beg);
fb.read(reinterpret_cast<char*>(out_vec.data()), 256 * sizeof(float));
return true;
}
// 3. ASCII whitespace or comma delimited text
fb.close();
std::ifstream ft(path);
std::string token;
int count = 0;
while (ft >> token && count < 256) {
if (!token.empty() && token.back() == ',') token.pop_back();
if (token.empty()) continue;
try {
out_vec[count++] = std::stof(token);
} catch (...) {}
}
return count == 256;
}
bool parse_vector_from_string(const std::string& str, std::vector<float>& out_vec) {
out_vec.clear();
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, ',')) {
std::stringstream ss_tok(token);
float val;
while (ss_tok >> val) {
out_vec.push_back(val);
}
}
return out_vec.size() >= 256;
}
int main(int argc, char** argv) {
int device_id = 0;
int num_euler_steps = 10;
int benchmark_n = 0;
std::string weights_path = find_file("parvulus_model_weights.bin");
std::string embeddings_path = find_file("task_embeddings.bin");
std::string scale_path = find_file("action_scale.bin");
std::string task_desc = "pick up the black bowl between the plate and the ramekin and place it on the plate";
std::string task_vector_file = "";
std::string task_vector_str = "";
std::string agent_img_path = "";
std::string wrist_img_path = "";
std::string output_path = "";
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-h" || arg == "--help") {
print_help(argv[0]);
return 0;
} else if (arg == "--device" && i + 1 < argc) {
device_id = std::atoi(argv[++i]);
} else if (arg == "--weights" && i + 1 < argc) {
weights_path = argv[++i];
} else if (arg == "--embeddings" && i + 1 < argc) {
embeddings_path = argv[++i];
} else if (arg == "--scale" && i + 1 < argc) {
scale_path = argv[++i];
} else if (arg == "--nfe" && i + 1 < argc) {
num_euler_steps = std::atoi(argv[++i]);
} else if (arg == "--task" && i + 1 < argc) {
task_desc = argv[++i];
} else if ((arg == "--task_vector_file" || arg == "--task_emb_raw") && i + 1 < argc) {
task_vector_file = argv[++i];
} else if (arg == "--task_vector" && i + 1 < argc) {
task_vector_str = argv[++i];
} else if (arg == "--agent_img" && i + 1 < argc) {
agent_img_path = argv[++i];
} else if (arg == "--wrist_img" && i + 1 < argc) {
wrist_img_path = argv[++i];
} else if (arg == "--output" && i + 1 < argc) {
output_path = argv[++i];
} else if (arg == "--benchmark") {
benchmark_n = 100;
if (i + 1 < argc && argv[i + 1][0] != '-') {
benchmark_n = std::atoi(argv[++i]);
}
} else {
std::cerr << "Unknown option: " << arg << " (use --help for options)\n";
return 1;
}
}
std::cout << "====================================================================\n";
std::cout << " PARVULUS STANDALONE PURE CUDA RUNTIME (Vector-First)\n";
std::cout << " Zero PyTorch, Zero LibTorch, Standalone C/CUDA Engine\n";
std::cout << " CUDA Device: " << device_id << " | Euler ODE Steps: " << num_euler_steps << "\n";
std::cout << "====================================================================\n";
// 1. Instantiate pure CUDA engine
ParvulusHandle engine = parvulus_create(device_id);
if (!engine) {
std::cerr << "[Error] Failed to initialize CUDA engine on device " << device_id << "\n";
return 1;
}
// 2. Load model weights
std::cout << "[Init] Loading weights: " << weights_path << "...\n";
const char* emb_path_c = embeddings_path.empty() ? nullptr : embeddings_path.c_str();
const char* scl_path_c = scale_path.empty() ? nullptr : scale_path.c_str();
auto t_load_0 = std::chrono::high_resolution_clock::now();
bool ok = parvulus_load(engine, weights_path.c_str(), emb_path_c, scl_path_c);
auto t_load_1 = std::chrono::high_resolution_clock::now();
double load_ms = std::chrono::duration<double, std::milli>(t_load_1 - t_load_0).count();
if (!ok) {
std::cerr << "[Error] Failed to load binary model assets!\n";
parvulus_destroy(engine);
return 1;
}
std::cout << "[Init] Engine initialized in " << std::fixed << std::setprecision(2) << load_ms << " ms.\n";
// 3. Set Task Conditioning Vector (First-Class Vector Pipeline)
std::vector<float> task_vector(256, 0.0f);
bool vector_loaded = false;
if (!task_vector_file.empty()) {
if (load_vector_from_file(task_vector_file, task_vector)) {
vector_loaded = true;
std::cout << "[Task] Loaded 256-dim task conditioning vector from file: " << task_vector_file << "\n";
parvulus_set_task_vector(engine, task_vector.data());
} else {
std::cerr << "[Error] Failed to load 256 floats from: " << task_vector_file << "\n";
parvulus_destroy(engine);
return 1;
}
} else if (!task_vector_str.empty()) {
if (parse_vector_from_string(task_vector_str, task_vector)) {
vector_loaded = true;
std::cout << "[Task] Parsed 256-dim task conditioning vector from command-line string.\n";
parvulus_set_task_vector(engine, task_vector.data());
} else {
std::cerr << "[Error] Failed to parse 256 floats from --task_vector string!\n";
parvulus_destroy(engine);
return 1;
}
} else {
// Fall back to lookup string if provided
std::cout << "[Task] Instruction: \"" << task_desc << "\" (using lookup table)\n";
if (!parvulus_set_task_string(engine, task_desc.c_str())) {
std::cerr << "[Error] Task prompt not found in lookup cache: " << task_desc << "\n";
parvulus_destroy(engine);
return 1;
}
}
// 4. Prepare Image Buffers
const size_t img_size = 256 * 256 * 3;
std::vector<uint8_t> agent_img(img_size, 128);
std::vector<uint8_t> wrist_img(img_size, 128);
if (!agent_img_path.empty()) {
std::ifstream f(agent_img_path, std::ios::binary);
if (!f.is_open() || !f.read(reinterpret_cast<char*>(agent_img.data()), img_size)) {
std::cerr << "[Warning] Could not read agent image from: " << agent_img_path << ", using test pattern.\n";
} else {
std::cout << "[Input] Loaded agent image: " << agent_img_path << "\n";
}
}
if (!wrist_img_path.empty()) {
std::ifstream f(wrist_img_path, std::ios::binary);
if (!f.is_open() || !f.read(reinterpret_cast<char*>(wrist_img.data()), img_size)) {
std::cerr << "[Warning] Could not read wrist image from: " << wrist_img_path << ", using test pattern.\n";
} else {
std::cout << "[Input] Loaded wrist image: " << wrist_img_path << "\n";
}
}
std::vector<float> actions(8 * 7, 0.0f);
// 5. Benchmark or Single Prediction
if (benchmark_n > 0) {
std::cout << "\n[Benchmark] Warming up (10 iterations)...\n";
for (int i = 0; i < 10; ++i) {
parvulus_predict_chunk(engine, agent_img.data(), wrist_img.data(), nullptr, actions.data(), num_euler_steps, nullptr);
}
std::cout << "[Benchmark] Running " << benchmark_n << " consecutive chunk predictions...\n";
std::vector<double> latencies_ms;
latencies_ms.reserve(benchmark_n);
for (int i = 0; i < benchmark_n; ++i) {
auto t0 = std::chrono::high_resolution_clock::now();
parvulus_predict_chunk(engine, agent_img.data(), wrist_img.data(), nullptr, actions.data(), num_euler_steps, nullptr);
auto t1 = std::chrono::high_resolution_clock::now();
latencies_ms.push_back(std::chrono::duration<double, std::milli>(t1 - t0).count());
}
std::sort(latencies_ms.begin(), latencies_ms.end());
double min_ms = latencies_ms.front();
double max_ms = latencies_ms.back();
double mean_ms = std::accumulate(latencies_ms.begin(), latencies_ms.end(), 0.0) / benchmark_n;
double p50_ms = latencies_ms[benchmark_n / 2];
double p95_ms = latencies_ms[static_cast<size_t>(benchmark_n * 0.95)];
double p99_ms = latencies_ms[static_cast<size_t>(benchmark_n * 0.99)];
double action_rate = 8.0 / (mean_ms / 1000.0);
std::cout << "\n====================================================================\n";
std::cout << " LATENCY BENCHMARK RESULTS (N=" << benchmark_n << " chunks):\n";
std::cout << "====================================================================\n";
std::cout << " - Min Latency: " << std::fixed << std::setprecision(3) << min_ms << " ms\n";
std::cout << " - p50 Latency: " << std::fixed << std::setprecision(3) << p50_ms << " ms\n";
std::cout << " - Mean Chunk Latency: " << std::fixed << std::setprecision(3) << mean_ms << " ms\n";
std::cout << " - p95 Latency: " << std::fixed << std::setprecision(3) << p95_ms << " ms\n";
std::cout << " - p99 Latency: " << std::fixed << std::setprecision(3) << p99_ms << " ms\n";
std::cout << " - Max Latency: " << std::fixed << std::setprecision(3) << max_ms << " ms\n";
std::cout << " - Effective Action Rate: " << std::fixed << std::setprecision(1) << action_rate << " Hz\n";
std::cout << "====================================================================\n";
} else {
auto t0 = std::chrono::high_resolution_clock::now();
bool pred_ok = parvulus_predict_chunk(engine, agent_img.data(), wrist_img.data(), nullptr, actions.data(), num_euler_steps, nullptr);
auto t1 = std::chrono::high_resolution_clock::now();
double pred_ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
if (!pred_ok) {
std::cerr << "[Error] Action chunk prediction failed!\n";
parvulus_destroy(engine);
return 1;
}
std::cout << "\n[Inference] Predicted 8x7 action chunk in " << std::fixed << std::setprecision(2) << pred_ms << " ms.\n";
}
// 6. Print Action Trajectory
std::cout << "\nPredicted Action Chunk [8 Steps x 7 Dimensions (dx, dy, dz, droll, dpitch, dyaw, gripper)]:\n";
std::cout << "--------------------------------------------------------------------------------------\n";
for (int step = 0; step < 8; ++step) {
std::cout << " Step " << step << ": [";
for (int d = 0; d < 7; ++d) {
std::cout << std::setw(8) << std::fixed << std::setprecision(4) << actions[step * 7 + d] << (d < 6 ? ", " : "");
}
std::cout << "]\n";
}
std::cout << "--------------------------------------------------------------------------------------\n";
// 7. Write to output file if requested
if (!output_path.empty()) {
std::ofstream out_f(output_path);
if (out_f.is_open()) {
for (int step = 0; step < 8; ++step) {
for (int d = 0; d < 7; ++d) {
out_f << std::fixed << std::setprecision(6) << actions[step * 7 + d] << (d < 6 ? " " : "\n");
}
}
std::cout << "[Output] Saved action chunk to: " << output_path << "\n";
} else {
std::cerr << "[Warning] Failed to write output to: " << output_path << "\n";
}
}
// 8. Cleanup
parvulus_destroy(engine);
std::cout << "\n[Pure CUDA] Engine released cleanly.\n";
return 0;
}
|