Julia on Google Colab: Free GPU-Accelerated Shareable Notebooks

Hi @photor, I was able to get Google Colab’s to run that command:

I followed the steps that @ageron mentioned in his Colab notebook template, and ran the following cells:

%%shell

set -e

#---------------------------------------------------#

JULIA_VERSION="1.7.2" # For any version ≥ 0.7.0

                      # As of 2022.03.05, latest stable release is 1.7.2

JULIA_PACKAGES="IJulia BenchmarkTools Plots Flux" 

                                  # Installs any preferred packages.

                                  # Separate by space, no need for the `.jl`

                                  # extension.

JULIA_PACKAGES_IF_GPU="CUDA" # Or use CuArrays for older Julia versions.

JULIA_NUM_THREADS=4 # Sets number of tasks for concurrency or parallelism.

#---------------------------------------------------#

if [ -n "$COLAB_GPU" ] && [ -z `which julia` ]; then

  # Install Julia

  JULIA_VER=`cut -d '.' -f -2 <<< "$JULIA_VERSION"`

  echo "Installing Julia $JULIA_VERSION on the current Colab Runtime..."

  BASE_URL="https://julialang-s3.julialang.org/bin/linux/x64"

  URL="$BASE_URL/$JULIA_VER/julia-$JULIA_VERSION-linux-x86_64.tar.gz"

  wget -nv $URL -O /tmp/julia.tar.gz # -nv means "not verbose"

  tar -x -f /tmp/julia.tar.gz -C /usr/local --strip-components 1

  rm /tmp/julia.tar.gz

  # Install Packages

  if [ "$COLAB_GPU" = "1" ]; then

      JULIA_PACKAGES="$JULIA_PACKAGES $JULIA_PACKAGES_IF_GPU"

  fi

  for PKG in `echo $JULIA_PACKAGES`; do

    echo "Installing Julia package $PKG..."

    julia -e 'using Pkg; pkg"add '$PKG'; precompile;"' &> /dev/null

  done

  # Install kernel and rename it to "julia"

  echo "Installing IJulia kernel..."

  julia -e 'using IJulia; IJulia.installkernel("julia", env=Dict(

      "JULIA_NUM_THREADS"=>"'"$JULIA_NUM_THREADS"'"))'

  KERNEL_DIR=`julia -e "using IJulia; print(IJulia.kerneldir())"`

  KERNEL_NAME=`ls -d "$KERNEL_DIR"/julia*`

  mv -f $KERNEL_NAME "$KERNEL_DIR"/julia  

  echo ''

  echo "Successfully installed `julia -v`!"

  echo "Please reload this page (press Ctrl+R, ⌘+R, or the F5 key) then"

  echo "jump to the 'Checking the Installation' section."

fi

The only thing I changed from his notebook template was updating the Julia version to 1.7.2 and increased the thread count from 2 to 4. I also ran the cells

versioninfo()

and

if ENV["COLAB_GPU"] == "1"
    using CUDA

    run(`nvidia-smi`)

    # Create a new random matrix directly on the GPU:
    M_on_gpu = CUDA.CURAND.rand(2^11, 2^11)
    @btime $M_on_gpu * $M_on_gpu; nothing
else
    println("No GPU found.")
end

to make sure CUDA was working with the GPU hardware acceleration. Then I ran CuArray{Int}(undef, 2) and got the expected output. Hope that helps! ^.^

2 Likes