# BipedalWalker OpenCL PPO Trainer

> **Note:** This repository may be named `python` , but it is **NOT** a fork of the Python programming language. It is a standalone project containing an OpenCL-accelerated PPO trainer for the BipedalWalker-v3 Gymnasium environment, plus custom OpenCL kernels for neural network operations , Though text_trainer.py and setup_ocl.py needs Python.

## How to Install


## What this is

- PPO trainer for `BipedalWalker-v3` with a custom laser hazard wrapper
- OpenCL acceleration for neural network forward/backward passes via `libtorch` tensors
- Tiled SGEMM kernel adapted from [dlprimitives](https://github.com/artyom-beilis/dlprimitives) (MIT License, Artyom Beilis)
- Optional pygame rendering + ffmpeg video recording
- Config-driven hyperparameters (`config.yaml`)
- Optional `SyncVectorEnv` parallel environments
- CSV + TensorBoard logging
- Live web dashboard (`dashboard_server.py`) via Flask + SSE
- OpenCL cache limits configurable via `opencl:` section in `config.yaml`
- Per-update memory logging and dashboard cache stats

## Key files

| File | Purpose |
|------|---------|
| `text_trainer.py` | Main training / inference loop, PPO agent, LaserHazardWrapper, OpenCL dispatch, atomic live stats writer |
| `dashboard_server.py` | Flask + SSE server for live browser dashboard |
| `templates/dashboard.html` | Single-page dashboard UI |
| `opencl_ocl.cc` | OpenCL kernels (tiled SGEMM, fused Linear+ReLU/Tanh, ReLU/Tanh, backward) + pybind11 |
| `setup_ocl.py` | Builds the OpenCL extension with CMake + libtorch |
| `CMakeLists.txt` | CMake config (Torch, torch_python, OpenCL, pybind11) |
| `config.yaml` | Hyperparameters (episodes, lr, laser, hidden, num_envs, compute_chain, dashboard…) |
| `fixer.cc` | Forces pygame `display.flip()` + `event.pump()` via GDB every 33ms |
| `fixer_opencl.cc` | Validates OpenCL vs CPU every 1s; calls `cleanup()` on mismatch |
| `best_walker.pt` / `walker_checkpoint.pt` | Saved policy/value checkpoints |
| `training_log.csv` | Per-episode reward / steps / laser_speed / best |

## Build

```bash
python setup_ocl.py
```

## Train

```bash
python text_trainer.py --train
```

## Live dashboard

```bash
# terminal 1
python text_trainer.py --train

# terminal 2
python dashboard_server.py

# browser
http://127.0.0.1:8080
```

## Run inference

```bash
python text_trainer.py --run
```

## Requirements

- Python 3.12+
- PyTorch with libtorch
- OpenCL SDK / ICD
- pybind11
- gymnasium, pygame, numpy, pyyaml, psutil, flask

## License

MIT — see `LICENSE` for details.

## Attribution

Tiled SGEMM kernel adapted from [dlprimitives](https://github.com/artyom-beilis/dlprimitives) (MIT License, Artyom Beilis).
