ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
determinism.h
Go to the documentation of this file.
1// Deterministic CUDA mode (EMBER_DETERMINISTIC=1, off by default; docs/USAGE.md).
2//
3// With it, CUDA prefill and decode logits are bit-identical run to run on the same GPU and build. The default
4// decode path is not: rmsnorm_optimized_kernel adds its warp sums into shared memory with atomicAdd, in whatever
5// order the warps arrive (experiments/determinism_2026-10-26). Under the mode:
6// - rmsnorm_vector_cuda_optimized (the decode rmsnorm, per-launch and CUDA-graph) adds the warp sums in warp
7// order (rmsnorm_optimized_det_kernel);
8// - the one-block-per-head decode attention (EMBER_DECODE_ATTN=legacy, or a shape past the split kernel's
9// scratch) reduces its max and softmax sum in warp order (attention_optimized_det_kernel);
10// - every cuBLAS handle gets a fixed workspace (CUBLAS_WORKSPACE_CONFIG=:4096:8 unless the variable is already
11// set) and CUBLAS_ATOMICS_NOT_ALLOWED.
12// Unset (or "0"), every path is exactly what it was: the same kernels, launches and cuBLAS setup.
13#ifndef EMBER_DETERMINISM_H
14#define EMBER_DETERMINISM_H
15
16#include <cstdlib>
17#include <cstring>
18
19namespace ember {
20
21// True when EMBER_DETERMINISTIC is set to anything but "" or "0". Read once.
22inline bool deterministic() {
23 static const bool on = [] {
24 const char* v = std::getenv("EMBER_DETERMINISTIC");
25 return v != nullptr && *v != '\0' && std::strcmp(v, "0") != 0;
26 }();
27 return on;
28}
29
30// Called before each cublasCreate: under the mode, pins cuBLAS's workspace configuration (the size can steer its
31// kernel choice) unless the caller already set one. A no-op when the mode is off.
33 if (!deterministic() || std::getenv("CUBLAS_WORKSPACE_CONFIG") != nullptr) return;
34#ifdef _WIN32
35 _putenv_s("CUBLAS_WORKSPACE_CONFIG", ":4096:8");
36#else
37 setenv("CUBLAS_WORKSPACE_CONFIG", ":4096:8", 0);
38#endif
39}
40
41} // namespace ember
42
43#endif // EMBER_DETERMINISM_H
std::vector< float > v
size_t j
bool deterministic()
Definition determinism.h:22
void deterministic_cublas_env()
Definition determinism.h:32