Skip to content
Snippets Groups Projects

These-knobs-turn-to-11

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Wolf Noble

    Quick script to divine and advise sysctl values to change to leverage more of an apple silicon host's GPU mem for inference tasks.

    This aught be safe?

    to_11

    Edited
    to_11.sh 4.19 KiB
    #!/usr/bin/env bash
    RED="\033[0;31m"
    BLUE="\033[0;34m"
    PURPLE="\033[0;35m"
    CYAN="\033[0;36m"
    WHITE="\033[0m"
    GREEN="\033[0;32m"
    YELLOW="\033[0;33m"
    DATESTRING=$(date +%m%d%y)&& export DATESTRING
    _STARTTIME=$(/bin/date +%s) && export _STARTTIME
    runningTime(){
      _NOW=$(/bin/date +%s)
      echo -e "$(( _NOW - _STARTTIME  ))"
    }
    convertsecs(){
     #take an int which is a number of seconds (IE:
     # now=$(date %s) ; sleep 30; newer_now=$(date %s); age=(${newer_now)-${now}) ; info "This took: $(convertsecs $age)"
     # ^^^^^---- untested example
     ((h=${1}/3600))
     ((m=(${1}%3600)/60))
     ((s=${1}%60))
     printf "%02d:%02d:%02d\n" ${h} ${m} ${s}
    }
    error(){
      MSG="$1"
      _time=$(convertsecs "$(runningTime)")
      echo -e "${RED}[${_time}] ERROR${WHITE}: ${MSG}${WHITE}" >>/dev/stderr
    }
    
    warn(){
      MSG=$1
      _time=$(convertsecs "$(runningTime)")
      echo -e "${YELLOW}[${_time}] WARN${WHITE}: ${MSG}${WHITE}" >>/dev/stderr
    }
    eveysays(){
      MSG=$1
      echo -e "🐶${PURPLE}. o 0 ${CYAN}(❤️🐾 ${WHITE}${MSG}${WHITE} 🐾❤️${CYAN})"
    }
    wolfsays(){
      MSG=$1
      echo -e "🐺${YELLOW} . o O (${WHITE}${MSG}${YELLOW})${WHITE}"
    }
    jackasssays(){
      MSG=$1
      echo -e "🤡${CYAN} . o O ( ${GREEN}${MSG}${CYAN} )${WHITE}"
    }
    info(){
      MSG=$1
      _time=$(convertsecs "$(runningTime)")
      echo -e "${WHITE}[${CYAN}${_time}${WHITE}]${GREEN} INFO${WHITE}: ${MSG}${WHITE}" >>/dev/stderr
    }
    isDarwin(){
      _HW=$(sysctl -n kern.ostype 2>/dev/null||echo not.a.mac)
      if  [[ "${_HW}" == "Darwin" ]]; then
        echo 0
      else
        echo -1
      fi
    }
    isAppleSilicon(){
      _AC="$(sysctl -n machdep.cpu.brand_string |sed  -e 's/[\)\(@ ._-]/_/g' -e 's/__/_/g' -e's/__/_/g'  2>/dev/null||echo no)"
      #shellcheck disable=SC2143
      if [[ $( grep -c "Apple_M" <<< "${_AC}" ) -gt 0  ]]; then
        #returncode is 1 if the output DOES NOT match 'Apple_M'
        echo 0
      else
        echo -1
      fi
    }
    totalRam(){
      #shellcheck disable=SC2005
      echo "$(sysctl -n hw.memsize 2>/dev/null||echo 0)"
    }
    hasMemToUse(){
      if [[ "$(totalRam)" -gt 1159049 ]]; then
        #1159049/1024/1024/32*29=1.001 we will come up with at LEAST a value of 1.
        echo 0
      else
       echo -1
      fi
    }
    divineGPURamLimit(){
      if [[ $(hasMemToUse) ]]; then
        
        echo $(( $(totalRam)*29/1024/1024/32 ))
      else
        error "Not enough memory in system. my math ain't mathin."
        return 1
      fi
    }
    if [[ $(isDarwin) == 0 ]]; then
      _Q=$(isAppleSilicon)
      if [[ $(isAppleSilicon) == 0 ]]; then
        _SYSCTLVAL="$(divineGPURamLimit)"
        _HUMAN_MAX="$(( $(totalRam)/1024/1024/1024))"
        _HUMAN_GPUVAL="$(( _SYSCTLVAL/1000 ))"
        info "Running on ${CYAN}$(sysctl -n machdep.cpu.brand_string)${WHITE} with ${GREEN}${_HUMAN_MAX}G${WHITE} unified ram."
        wolfsays "This is probably a ${RED}really${WHITE} bad idea."
        jackasssays "YOU'LL BE FINE!!! ${WHITE}He's just being melodramatic\r\n\t \
        Your system has ${_HUMAN_MAX}G of ram.\r\n\t \
        We ${YELLOW}should ${WHITE}be able to safely use ${GREEN}${_HUMAN_GPUVAL}${WHITE} for IOGPU memory."
        wolfsays "To do this, first enable dynamic lowwatermark by typing the following command and entering your password\r\n\t \
        ${YELLOW}This is necessary, as tweaking these knobs requires administrative privilege."
        sleep 1
        info  "[${YELLOW}TYPE THE FOLLOWING COMMAND IN A TERMINAL SESSION${WHITE}]\r\n\r\n${CYAN}sudo sysctl iogpu.dynamic_lwm=1\r\n"
        wolfsays "Then, adjust the following GPU-scoped system configuration knobs"
        info "[${YELLOW}TYPE THE FOLLOWING COMMAND IN A TERMINAL SESSION${WHITE}]\r\n\r\n${CYAN}for KEY in 'wired_limit_mb' 'rsrc_limit'; do SYSCTL=\"iogpu.\${KEY}\"; sudo sysctl \"\${SYSCTL}\"=${_SYSCTLVAL};done\r\n"
        wolfsays "You should be able to start your tool and enjoy a bit more usable space."
        jackasssays "you said ${BLUE}TOOL${GREEN}"
        wolfsays "**sigh**"
        eveysays "${YELLOW}CAUTION${WHITE}:\r\n\tYou ${RED}will${WHITE} want to kill everything other than the tool necessary\r\n\t${YELLOW}Tweaking these knobs with other programs running may crash your system.\r\n\t${BLUE}Good Luck!\r\n\t${PURPLE}Remember to be spontaneously nice to someone today, eh? I'm watching....\r\n"
      else
        eveysays "Sorry, friend... this 🍎 kluge is only relevant to Macs with Apple Silicon Hardware"
      fi
    else
      error "This isn't a change that can be performed on non-apple hardware. Nothing to do"
    fi
    
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment