Skip to content

Commit ba771da

Browse files
committed
merge
2 parents 651df78 + 2c8042e commit ba771da

79 files changed

Lines changed: 2580 additions & 843 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ ocean/antibody/generated/
176176
# Data
177177
resources/drive/data/*
178178
resources/drive/binaries/*
179+
resources/boxoban/levels/
180+
resources/boxoban/boxoban_maps_*.bin
181+
182+
# Policy weights live in the website repo (docs/assets/models/)
183+
resources/**/*_weights.bin
179184

180185
# Pretrained weights live on puffer.ai; fetched on first load
181186
resources/**/*weights*.bin

SKILL_ISSUES.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This is a hand-written guide of how I style and refactor PufferLib
2+
# It is equally useful for human and AI brogrammers. And for other projects.
3+
# If your name is not John Carmack, you should probably read it before blindly
4+
# shoveling it into whatever language model is currently replacing you.
5+
6+
# Objective
7+
Reduce source code length without golfing while preserving behavior and performance. Preserve existing determinism. Discard biases against long files or functions.
8+
9+
# Joseph's Stupid Refactoring Algorithm
10+
1. Inline every function that is only used once and tighten former call sites
11+
2. Eliminate defensive checks. Replace complex error handling with plain asserts.
12+
3. Reduce deeply nested code by merging and inverting conditionals
13+
4. Co-optimize multi-consumer functions with their callers
14+
5. Apply syntax and style guide as a final pass
15+
16+
# Syntax & Style (General)
17+
- Do not split up code into more files
18+
- Soft 80-col / hard 100-col limit
19+
- Do not one-line loops or conditionals
20+
- 4-space indents. Do NOT match opening parens
21+
- Next line continuations indent 4 extra spaces instead of matching parens
22+
- Apply semantic vertical spacing between blocks of code sparingly
23+
- Tests are important but their length and code quality is not counted
24+
- Do not add source complexity or shims for ease of testing
25+
- Do not block off comments with --- or ### etc.
26+
27+
# C
28+
- Do not use header files as lists of declarations. Treat them as source files.
29+
- Avoid forward declarations. Define functions as close as possible to first use.
30+
- Preallocate all memory at init by default and let the OS free it on close
31+
- Runtime allocations should be rare and freed in the same scope
32+
- Avoid keyword bloat, such as redundant static, const, inline, etc.
33+
- Use macros only for constants and constant expressions, not for conditionals
34+
- Use struct initializer syntax foo = {.a = 1, .b = 2} instead of setter wrappers
35+
- Do not use additional scoping blocks, i.e. bare {}. Dedupe names instead.
36+
37+
# CUDA
38+
- Treat CUDA files as CUDA C99
39+
- Do not use the C++ standard library, templates, or classes
40+
- Do not use the C++ versions of C language features like nullptr and static_cast
41+
- Exception: existing/user-directed overloading for different numeric types
42+
- Do not null params with (void) (Wunused-parameter is disabled)
43+
- Avoid redundant casts where autocast will do (Wnarrowing is disabled)
44+
- Follow the C style guide above

build.sh

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,61 @@ set -e
1010
# ./build.sh breakout --local # Standalone executable (debug, sanitizers)
1111
# ./build.sh breakout --fast # Standalone executable (optimized)
1212
# ./build.sh breakout --web # Emscripten web build
13+
# # copy build/web/ENV/* to ../docker/puffer.ai/docs/assets/ENV/
1314
# ./build.sh breakout --profile # Kernel profiling binary
15+
# ./build.sh breakout --device N # Pin CUDA_VISIBLE_DEVICES during the build
1416
# ./build.sh all # Build all envs native and native float32
17+
#
18+
# Native train/eval binaries are written per-env so parallel builds/runs
19+
# do not clobber a shared ./puffer:
20+
# build/puffer_<env> default precision
21+
# build/puffer_<env>_float --float
22+
# Runtime GPU selection: CUDA_VISIBLE_DEVICES=N ./build/puffer_<env> train <env>
23+
# or ./build/puffer_<env> train <env> base.gpu_offset=N
1524

1625
if [ -z "$1" ]; then
17-
echo "Usage: ./build.sh ENV_NAME [--gpu] [--float] [--debug] [--local|--fast|--web|--profile|--cpu]"
26+
echo "Usage: ./build.sh ENV_NAME [--gpu] [--float] [--debug] [--local|--fast|--web|--profile|--cpu] [--device N]"
1827
exit 1
1928
fi
2029
ENV=$1
2130
shift
2231

2332
USE_GPU_ENV=0
24-
for arg in "$@"; do
25-
case $arg in
33+
DEVICE=""
34+
SNAKE_RAW=0
35+
while [ $# -gt 0 ]; do
36+
case $1 in
2637
--gpu) USE_GPU_ENV=1 ;;
2738
--float) PRECISION="-DPRECISION_FLOAT" ;;
39+
--no-onehot) SNAKE_RAW=1 ;;
2840
--debug) DEBUG=1 ;;
2941
--local) MODE=local ;;
3042
--fast) MODE=fast ;;
3143
--web) MODE=web ;;
3244
--profile) MODE=profile ;;
3345
--cpu) MODE=cpu ;;
34-
*) echo "Error: unknown argument '$arg'" && exit 1 ;;
46+
--device)
47+
shift
48+
if [ -z "$1" ]; then
49+
echo "Error: --device requires a GPU index" && exit 1
50+
fi
51+
DEVICE=$1
52+
;;
53+
--device=*)
54+
DEVICE="${1#--device=}"
55+
if [ -z "$DEVICE" ]; then
56+
echo "Error: --device requires a GPU index" && exit 1
57+
fi
58+
;;
59+
*) echo "Error: unknown argument '$1'" && exit 1 ;;
3560
esac
61+
shift
3662
done
3763

64+
if [ -n "$DEVICE" ]; then
65+
export CUDA_VISIBLE_DEVICES="$DEVICE"
66+
fi
67+
3868
if [ "$ENV" = "all" ]; then
3969
FAILED=""
4070
for env_dir in ocean/*/; do
@@ -197,28 +227,55 @@ if [ "$MODE" = "local" ] || [ "$MODE" = "fast" ]; then
197227
echo "Built: ./$OUTPUT_NAME"
198228
exit 0
199229
elif [ "$MODE" = "web" ]; then
230+
ENV_HEADER="$SRC_DIR/$ENV.h"
231+
if ! grep -q 'typedef[[:space:]].*obs_t' "$ENV_HEADER" 2>/dev/null; then
232+
echo "Error: $ENV_HEADER must typedef obs_t for web eval"
233+
exit 1
234+
fi
200235
mkdir -p "build/web/$ENV"
201236
PRELOAD_ENV=()
202237
if [ -d "resources/$ENV" ]; then
203238
PRELOAD_ENV=(--preload-file "resources/$ENV@resources/$ENV")
204239
fi
205240
echo "Compiling $ENV for web..."
241+
PRELOAD=(
242+
--preload-file resources/$ENV@resources/$ENV
243+
--preload-file resources/shared@resources/shared
244+
--preload-file config/default.ini@config/default.ini
245+
)
246+
if [ -f "config/$ENV.ini" ]; then
247+
PRELOAD+=(--preload-file "config/$ENV.ini@config/$ENV.ini")
248+
fi
249+
if [ -f "config/${ENV}_web.ini" ]; then
250+
PRELOAD+=(--preload-file "config/${ENV}_web.ini@config/${ENV}_web.ini")
251+
fi
206252
emcc \
207253
-o "build/web/$ENV/game.html" \
208-
"$SRC_FILE" $EXTRA_SRC \
254+
-x c src/puffercpu.h -x none $EXTRA_SRC \
209255
-O3 -Wall -Wno-narrowing \
210256
"${LINK_ARCHIVES[@]}" \
211-
"${INCLUDES[@]}" \
257+
-I. -Isrc -I$SRC_DIR -Ivendor "${INCLUDES[@]}" \
212258
-L. -L./$RAYLIB_NAME/lib \
213259
-sASSERTIONS=2 -gsource-map \
214260
-sUSE_GLFW=3 -sUSE_WEBGL2=1 -sASYNCIFY -sFILESYSTEM -sFORCE_FILESYSTEM=1 \
215261
--shell-file vendor/minshell.html \
216262
-sINITIAL_MEMORY=512MB -sALLOW_MEMORY_GROWTH -sSTACK_SIZE=512KB \
217263
-DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES3 \
264+
-DPUFFERCPU_EVAL_MAIN \
265+
-DENV_HEADER=\"$ENV_HEADER\" \
266+
-DPUFFER_ENV_NAME=\"$ENV\" \
218267
--preload-file resources/shared@resources/shared \
219268
"${PRELOAD_ENV[@]}" \
269+
"${PRELOAD[@]}" \
220270
"${EXTRA_CFLAGS[@]}"
221271
echo "Built: build/web/$ENV/game.html"
272+
WEBSITE_DIR="${PUFFER_WEBSITE_DIR:-../docker/puffer.ai}"
273+
WEBSITE_ASSETS="$WEBSITE_DIR/docs/assets"
274+
if [ -d "$WEBSITE_ASSETS" ]; then
275+
mkdir -p "$WEBSITE_ASSETS/$ENV"
276+
cp -a "build/web/$ENV/." "$WEBSITE_ASSETS/$ENV/"
277+
echo "Published: $WEBSITE_ASSETS/$ENV/"
278+
fi
222279
exit 0
223280
elif [ "$MODE" = "cpu" ]; then
224281
ENV_HEADER="$SRC_DIR/$ENV.h"
@@ -227,19 +284,21 @@ elif [ "$MODE" = "cpu" ]; then
227284
exit 1
228285
fi
229286

287+
mkdir -p build
230288
echo "Compiling standalone CPU eval for $ENV..."
231289
${CC:-clang} "${CLANG_OPT[@]}" \
232290
-I. -Isrc -I$SRC_DIR -Ivendor "${INCLUDES[@]}" \
233291
-DPLATFORM_DESKTOP \
234292
-DPUFFERCPU_EVAL_MAIN \
235293
-DENV_HEADER=\"$ENV_HEADER\" \
294+
-DPUFFER_ENV_NAME=\"$ENV\" \
236295
-x c src/puffercpu.h -x none $EXTRA_SRC \
237296
"${LINK_ARCHIVES[@]}" \
238297
"${EXTRA_LDFLAGS[@]}" \
239298
"${STANDALONE_LDFLAGS[@]}" \
240299
-lm -lpthread -fopenmp \
241-
-o build_cpu
242-
echo "Built: ./build_cpu"
300+
-o "build/cpu_${ENV}"
301+
echo "Built: ./build/cpu_${ENV}"
243302
exit 0
244303
fi
245304

@@ -293,7 +352,15 @@ MODE=${MODE:-native}
293352
NVCC_NARROW=(-Xcompiler=-Wno-narrowing --diag-suppress=2361)
294353

295354
if [ "$MODE" = "native" ]; then
296-
echo "Compiling native train/eval binary ($ARCH)..."
355+
if [ -n "$PRECISION" ]; then
356+
TRAIN_BIN="build/puffer_${ENV}_float"
357+
elif [ "$SNAKE_RAW" = "1" ]; then
358+
TRAIN_BIN="build/puffer_${ENV}_raw"
359+
EXTRA_CFLAGS+=(-DSNAKE_ONEHOT=0)
360+
else
361+
TRAIN_BIN="build/puffer_${ENV}"
362+
fi
363+
echo "Compiling native train/eval binary ($ARCH) -> $TRAIN_BIN..."
297364
$NVCC $NVCC_OPT -arch=$ARCH -std=c++17 \
298365
-I. -Isrc -I$SRC_DIR -Ivendor \
299366
"${INCLUDES[@]}" \
@@ -314,11 +381,12 @@ if [ "$MODE" = "native" ]; then
314381
"${EXTRA_LDFLAGS[@]}" \
315382
-lcudart -lnccl -lnvidia-ml -lcublas -lcusolver -lcurand \
316383
-lm -lpthread $OMP_LIB "${STANDALONE_LDFLAGS[@]}" \
317-
-o puffer
318-
echo "Built: ./puffer"
384+
-o "$TRAIN_BIN"
385+
echo "Built: ./$TRAIN_BIN"
319386

320387
elif [ "$MODE" = "profile" ]; then
321-
echo "Compiling profile binary ($ARCH)..."
388+
PROFILE_BIN="build/profile_${ENV}"
389+
echo "Compiling profile binary ($ARCH) -> $PROFILE_BIN..."
322390
$NVCC $NVCC_OPT -arch=$ARCH -std=c++17 \
323391
-I. -Isrc -I$SRC_DIR -Ivendor \
324392
"${INCLUDES[@]}" \
@@ -336,6 +404,6 @@ elif [ "$MODE" = "profile" ]; then
336404
-L$CUDA_HOME/lib64 \
337405
-lnccl -lnvidia-ml -lcublas -lcusolver -lcurand \
338406
-lGL -lm -lpthread $OMP_LIB \
339-
-o profile
340-
echo "Built: ./profile"
407+
-o "$PROFILE_BIN"
408+
echo "Built: ./$PROFILE_BIN"
341409
fi

config/cartpole.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ env_name = cartpole
33

44
[vec]
55
total_agents = 4096
6-
num_buffers = 4.78896
6+
num_buffers = 4
77
num_threads = 16
88

99
[env]

0 commit comments

Comments
 (0)