diff --git a/run_rl_swarm.sh b/run_rl_swarm.sh index b5f69a2e..915204eb 100755 --- a/run_rl_swarm.sh +++ b/run_rl_swarm.sh @@ -1,17 +1,21 @@ #!/usr/bin/env bash - set -euo pipefail # General arguments ROOT=$PWD +# GenRL Swarm version to use +GENRL_TAG="0.1.8" + export IDENTITY_PATH export GENSYN_RESET_CONFIG export CONNECT_TO_TESTNET=true export ORG_ID -export HF_HUB_DOWNLOAD_TIMEOUT=120 # 2 minutes -export SWARM_CONTRACT="0x7745a8FE4b8D2D2c3BB103F8dCae822746F35Da0" +export HF_HUB_DOWNLOAD_TIMEOUT=120 # 2 minutes +export SWARM_CONTRACT="0xFaD7C5e93f28257429569B854151A1B8DCD404c2" +export PRG_CONTRACT="0x51D4db531ae706a6eC732458825465058fA23a35" export HUGGINGFACE_ACCESS_TOKEN="None" +export PRG_GAME=true # Path to an RSA private key. If this path does not exist, a new key pair will be created. # Remove this file if you want a new PeerID. @@ -23,16 +27,15 @@ GENSYN_RESET_CONFIG=${GENSYN_RESET_CONFIG:-""} # Bit of a workaround for the non-root docker container. if [ -n "$DOCKER" ]; then - volumes=( - /home/gensyn/rl_swarm/modal-login/temp-data - /home/gensyn/rl_swarm/keys - /home/gensyn/rl_swarm/configs - /home/gensyn/rl_swarm/logs - ) - - for volume in ${volumes[@]}; do - sudo chown -R 1001:1001 $volume - done + volumes=( + /home/gensyn/rl_swarm/modal-login/temp-data + /home/gensyn/rl_swarm/keys + /home/gensyn/rl_swarm/configs + /home/gensyn/rl_swarm/logs + ) + for volume in "${volumes[@]}"; do + sudo chown -R 1001:1001 "$volume" + done fi # Will ignore any visible GPUs if set. @@ -46,35 +49,21 @@ BLUE_TEXT="\033[34m" RED_TEXT="\033[31m" RESET_TEXT="\033[0m" -echo_green() { - echo -e "$GREEN_TEXT$1$RESET_TEXT" -} +echo_green() { echo -e "${GREEN_TEXT}$1${RESET_TEXT}"; } +echo_blue() { echo -e "${BLUE_TEXT}$1${RESET_TEXT}"; } +echo_red() { echo -e "${RED_TEXT}$1${RESET_TEXT}"; } -echo_blue() { - echo -e "$BLUE_TEXT$1$RESET_TEXT" -} +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -echo_red() { - echo -e "$RED_TEXT$1$RESET_TEXT" -} - -ROOT_DIR="$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)" - -# Function to clean up the server process upon exit cleanup() { - echo_green ">> Shutting down trainer..." - - # Remove modal credentials if they exist - rm -r $ROOT_DIR/modal-login/temp-data/*.json 2> /dev/null || true - - # Kill all processes belonging to this script's process group - kill -- -$$ || true - - exit 0 + echo_green ">> Shutting down trainer..." + rm -r "$ROOT_DIR"/modal-login/temp-data/*.json 2>/dev/null || true + kill -- -$$ || true + exit 0 } errnotify() { - echo_red ">> An error was detected while running rl-swarm. See $ROOT/logs for full logs." + echo_red ">> An error was detected while running rl-swarm. See $ROOT/logs for full logs." } trap cleanup EXIT @@ -82,207 +71,176 @@ trap errnotify ERR echo -e "\033[38;5;224m" cat << "EOF" - ██████ ██ ███████ ██ ██ █████ ██████ ███ ███ - ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ - ██████ ██ █████ ███████ ██ █ ██ ███████ ██████ ██ ████ ██ - ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ - ██ ██ ███████ ███████ ███ ███ ██ ██ ██ ██ ██ ██ - - From Gensyn - +██████ ██ ███████ ██ ██ █████ ██████ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ██ █████ ██ ██ ███████ ██████ +██ ██ ██ ██ ██ ██ ██ ██ ██ +██ ███████ ███████ ███████ ██ ██ ██ ██ ██ +From Gensyn EOF -# Create logs directory if it doesn't exist mkdir -p "$ROOT/logs" if [ "$CONNECT_TO_TESTNET" = true ]; then - # Run modal_login server. - echo "Please login to create an Ethereum Server Wallet" - cd modal-login - # Check if the yarn command exists; if not, install Yarn. - - # Node.js + NVM setup - if ! command -v node > /dev/null 2>&1; then - echo "Node.js not found. Installing NVM and latest Node.js..." - export NVM_DIR="$HOME/.nvm" - if [ ! -d "$NVM_DIR" ]; then - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - fi - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" - nvm install node - else - echo "Node.js is already installed: $(node -v)" + echo "Please login to create an Ethereum Server Wallet" + cd modal-login + + # Node.js + NVM setup + if ! command -v node >/dev/null 2>&1; then + echo "Node.js not found. Installing NVM and latest Node.js..." + export NVM_DIR="$HOME/.nvm" + if [ ! -d "$NVM_DIR" ]; then + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash fi - - if ! command -v yarn > /dev/null 2>&1; then - # Detect Ubuntu (including WSL Ubuntu) and install Yarn accordingly - if grep -qi "ubuntu" /etc/os-release 2> /dev/null || uname -r | grep -qi "microsoft"; then - echo "Detected Ubuntu or WSL Ubuntu. Installing Yarn via apt..." - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list - sudo apt update && sudo apt install -y yarn - else - echo "Yarn not found. Installing Yarn globally with npm (no profile edits)…" - # This lands in $NVM_DIR/versions/node//bin which is already on PATH - npm install -g --silent yarn - fi - fi - - ENV_FILE="$ROOT"/modal-login/.env - if [[ "$OSTYPE" == "darwin"* ]]; then - # macOS version - sed -i '' "3s/.*/SWARM_CONTRACT_ADDRESS=$SWARM_CONTRACT/" "$ENV_FILE" - + [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" + [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" + nvm install node + else + echo "Node.js is already installed: $(node -v)" + fi + + if ! command -v yarn >/dev/null 2>&1; then + if grep -qi "ubuntu" /etc/os-release 2>/dev/null || uname -r | grep -qi "microsoft"; then + echo "Detected Ubuntu or WSL Ubuntu. Installing Yarn via apt..." + curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - + echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list + sudo apt update && sudo apt install -y yarn else - # Linux version - sed -i "3s/.*/SWARM_CONTRACT_ADDRESS=$SWARM_CONTRACT/" "$ENV_FILE" + echo "Yarn not found. Installing Yarn globally with npm (no profile edits)…" + npm install -g --silent yarn fi - - - # Docker image already builds it, no need to again. - if [ -z "$DOCKER" ]; then - yarn install --immutable - echo "Building server" - yarn build > "$ROOT/logs/yarn.log" 2>&1 + fi + + ENV_FILE="$ROOT"/modal-login/.env + if [[ "$OSTYPE" == "darwin"* ]]; then + sed -i '' "3s/.*/SWARM_CONTRACT_ADDRESS=$SWARM_CONTRACT/" "$ENV_FILE" + sed -i '' "4s/.*/PRG_CONTRACT_ADDRESS=$PRG_CONTRACT/" "$ENV_FILE" + else + sed -i "3s/.*/SWARM_CONTRACT_ADDRESS=$SWARM_CONTRACT/" "$ENV_FILE" + sed -i "4s/.*/PRG_CONTRACT_ADDRESS=$PRG_CONTRACT/" "$ENV_FILE" + fi + + if [ -z "$DOCKER" ]; then + yarn install --immutable + echo "Building server" + yarn build >"$ROOT/logs/yarn.log" 2>&1 + fi + + yarn start >>"$ROOT/logs/yarn.log" 2>&1 & + SERVER_PID=$! + echo "Started server process: $SERVER_PID" + sleep 5 + + if [ -z "$DOCKER" ]; then + if open http://localhost:3000 2>/dev/null; then + echo_green ">> Successfully opened http://localhost:3000 in your default browser." + else + echo ">> Failed to open http://localhost:3000. Please open it manually." fi - yarn start >> "$ROOT/logs/yarn.log" 2>&1 & # Run in background and log output + else + echo_green ">> Please open http://localhost:3000 in your host browser." + fi - SERVER_PID=$! # Store the process ID - echo "Started server process: $SERVER_PID" + cd .. + echo_green ">> Waiting for modal userData.json to be created..." + while [ ! -f "modal-login/temp-data/userData.json" ]; do sleep 5 - - # Try to open the URL in the default browser - if [ -z "$DOCKER" ]; then - if open http://localhost:3000 2> /dev/null; then - echo_green ">> Successfully opened http://localhost:3000 in your default browser." - else - echo ">> Failed to open http://localhost:3000. Please open it manually." - fi + done + + echo "Found userData.json. Proceeding..." + ORG_ID=$(awk 'BEGIN { FS = "\"" } !/^[ \t]*[{}]/ { print $(NF - 1); exit }' modal-login/temp-data/userData.json) + echo "Your ORG_ID is set to: $ORG_ID" + + echo "Waiting for API key to become activated..." + while true; do + STATUS=$(curl -s "http://localhost:3000/api/get-api-key-status?orgId=$ORG_ID") + if [[ "$STATUS" == "activated" ]]; then + echo "API key is activated! Proceeding..." + break else - echo_green ">> Please open http://localhost:3000 in your host browser." + echo "Waiting for API key to be activated..." + sleep 5 fi - - cd .. - - echo_green ">> Waiting for modal userData.json to be created..." - while [ ! -f "modal-login/temp-data/userData.json" ]; do - sleep 5 # Wait for 5 seconds before checking again - done - echo "Found userData.json. Proceeding..." - - ORG_ID=$(awk 'BEGIN { FS = "\"" } !/^[ \t]*[{}]/ { print $(NF - 1); exit }' modal-login/temp-data/userData.json) - echo "Your ORG_ID is set to: $ORG_ID" - - # Wait until the API key is activated by the client - echo "Waiting for API key to become activated..." - while true; do - STATUS=$(curl -s "http://localhost:3000/api/get-api-key-status?orgId=$ORG_ID") - if [[ "$STATUS" == "activated" ]]; then - echo "API key is activated! Proceeding..." - break - else - echo "Waiting for API key to be activated..." - sleep 5 - fi - done + done fi echo_green ">> Getting requirements..." pip install --upgrade pip echo_green ">> Installing GenRL..." +pip install "gensyn-genrl==${GENRL_TAG}" +pip install "reasoning-gym>=0.1.20" +pip install "hivemind@git+https://github.com/gensyn-ai/hivemind@639c964a8019de63135a2594663b5bec8e5356dd" -# Ollama already running as part of the docker compose file -if [ -z "$DOCKER" ]; then - echo_green ">> Installing Ollama requires 'sudo' privileges. As an alternative, please use the Docker installation path as described in README.md" - if [[ "$OSTYPE" == "darwin"* ]]; then - # Install brew if not already installed - if ! command -v brew > /dev/null 2>&1; then - /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - fi - # Install ollama if not already installed - if ! command -v ollama > /dev/null 2>&1; then - brew install ollama - fi - else - # Install ollama if not already installed - if ! command -v ollama > /dev/null 2>&1; then - curl -fsSL https://ollama.com/install.sh | sh -s -- -y - fi - fi - # Start ollama server if not already running, check by running ollama list - if ! ollama list > /dev/null 2>&1; then - echo ">> Starting ollama server..." - nohup ollama serve > /tmp/ollama.log 2>&1 & - fi +if [ ! -d "$ROOT/configs" ]; then + mkdir "$ROOT/configs" fi -pip install -r code_gen_exp/requirements.txt - -if [ ! -d "$ROOT/configs" ]; then - mkdir "$ROOT/configs" -fi -if [ -f "$ROOT/configs/code-gen-swarm.yaml" ]; then - # Use cmp -s for a silent comparison. If different, backup and copy. - if ! cmp -s "$ROOT/code_gen_exp/config/code-gen-swarm.yaml" "$ROOT/configs/code-gen-swarm.yaml"; then - if [ -z "$GENSYN_RESET_CONFIG" ]; then - echo_green ">> Found differences in code-gen-swarm.yaml. If you would like to reset to the default, set GENSYN_RESET_CONFIG to a non-empty value." - else - echo_green ">> Found differences in code-gen-swarm.yaml. Backing up existing config." - mv "$ROOT/configs/code-gen-swarm.yaml" "$ROOT/configs/code-gen-swarm.yaml.bak" - cp "$ROOT/code_gen_exp/config/code-gen-swarm.yaml" "$ROOT/configs/code-gen-swarm.yaml" - fi +if [ -f "$ROOT/configs/rg-swarm.yaml" ]; then + if ! cmp -s "$ROOT/rgym_exp/config/rg-swarm.yaml" "$ROOT/configs/rg-swarm.yaml"; then + if [ -z "$GENSYN_RESET_CONFIG" ]; then + echo_green ">> Found differences in rg-swarm.yaml. Set GENSYN_RESET_CONFIG to reset to defaults." + else + echo_green ">> Backing up existing rg-swarm.yaml and restoring default." + mv "$ROOT/configs/rg-swarm.yaml" "$ROOT/configs/rg-swarm.yaml.bak" + cp "$ROOT/rgym_exp/config/rg-swarm.yaml" "$ROOT/configs/rg-swarm.yaml" fi + fi else - # If the config doesn't exist, just copy it. - cp "$ROOT/code_gen_exp/config/code-gen-swarm.yaml" "$ROOT/configs/code-gen-swarm.yaml" + cp "$ROOT/rgym_exp/config/rg-swarm.yaml" "$ROOT/configs/rg-swarm.yaml" fi if [ -n "$DOCKER" ]; then - # Make it easier to edit the configs on Linux systems. - sudo chmod -R 0777 /home/gensyn/rl_swarm/configs + sudo chmod -R 0777 /home/gensyn/rl_swarm/configs fi echo_green ">> Done!" - -echo -en $GREEN_TEXT +echo -en "$GREEN_TEXT" read -p ">> Would you like to push models you train in the RL swarm to the Hugging Face Hub? [y/N] " yn -echo -en $RESET_TEXT -yn=${yn:-N} # Default to "N" if the user presses Enter +echo -en "$RESET_TEXT" + +yn=${yn:-N} case $yn in - [Yy]*) read -p "Enter your Hugging Face access token: " HUGGINGFACE_ACCESS_TOKEN ;; - [Nn]*) HUGGINGFACE_ACCESS_TOKEN="None" ;; - *) echo ">>> No answer was given, so NO models will be pushed to Hugging Face Hub" && HUGGINGFACE_ACCESS_TOKEN="None" ;; + [Yy]*) + read -p "Enter your Hugging Face access token: " HUGGINGFACE_ACCESS_TOKEN + ;; + [Nn]*) + HUGGINGFACE_ACCESS_TOKEN="None" + ;; + *) + echo ">>> No answer was given, so NO models will be pushed to Hugging Face Hub" + HUGGINGFACE_ACCESS_TOKEN="None" + ;; esac - -echo -en $GREEN_TEXT +echo -en "$GREEN_TEXT" read -p ">> Enter the name of the model you want to use in huggingface repo/name format, or press [Enter] to use the default model. " MODEL_NAME -echo -en $RESET_TEXT +echo -en "$RESET_TEXT" -# Only export MODEL_NAME if user provided a non-empty value if [ -n "$MODEL_NAME" ]; then - export MODEL_NAME - echo_green ">> Using model: $MODEL_NAME" + export MODEL_NAME + echo_green ">> Using model: $MODEL_NAME" else - echo_green ">> Using default model from config" + echo_green ">> Using default model from config" fi -#logout to prevent weird env issues, if it fails unset and try again -if ! hf auth logout > /dev/null 2>&1; then - unset HF_TOKEN - unset HUGGING_FACE_HUB_TOKEN - # if it fails a second time, report stderr - hf auth logout > /dev/null 2>&1 + +echo -en "$GREEN_TEXT" +read -p ">> Would you like your model to participate in the AI Prediction Market? [Y/n] " yn +echo -en "$RESET_TEXT" + +if [ "$yn" = "n" ] || [ "$yn" = "N" ]; then + PRG_GAME=false + echo_green ">> Playing PRG game: false" +else + echo_green ">> Playing PRG game: true" fi -echo -en $RESET_TEXT echo_green ">> Good luck in the swarm!" echo_blue ">> And remember to star the repo on GitHub! --> https://github.com/gensyn-ai/rl-swarm" -python -m code_gen_exp.runner.swarm_launcher \ - --config-path "$ROOT/code_gen_exp/config" \ - --config-name "code-gen-swarm.yaml" +python -m rgym_exp.runner.swarm_launcher \ + --config-path "$ROOT/rgym_exp/config" \ + --config-name "rg-swarm.yaml" -wait # Keep script running until Ctrl+C +wait