title: Export OpenGL kernel code with Apache TVM in Colab (X11 related error)
body: <p>I have problem with exporting OpenGL kernel code with <a href="https://tvm.ai/" rel="nofollow noreferrer">Apache TVM - Open deep learning compiler stack for CPUs, GPUs, and specialized accelerators</a> in Google Colaboratory.</p>
  
  <p>It can reproduce on <a href="https://colab.research.google.com/drive/1qjA7rsqLnW5YSVGW3DU5EKqESgPm8OON" rel="nofollow noreferrer">Build TVM.ipynb</a>.
  To enable Xvrf, I installed <a href="https://github.com/ponty/pyvirtualdisplay" rel="nofollow noreferrer">PyVirtualDisplay</a>.</p>
  
  <pre class="lang-sh prettyprint-override"><code>%%bash
  sudo apt-get update
  sudo apt-get install -y -qq llvm-6.0 libglfw3-dev libtinfo-dev libffi-dev zlib1g-dev xvfb x11-utils
  pip install pyvirtualdisplay
  git clone --recursive https://github.com/dmlc/tvm
  cd tvm
  mkdir -p build
  cp cmake/config.cmake build
  sed -i -e 's/USE_OPENGL OFF/USE_OPENGL ON/g' build/config.cmake
  sed -i -e 's/USE_CUDA OFF/USE_CUDA ON/g' build/config.cmake
  sed -i -e 's/USE_CUDNN OFF/USE_CUDNN ON/g' build/config.cmake
  sed -i -e 's/USE_LLVM OFF/USE_LLVM ON/g' build/config.cmake
  cd build
  cmake ..
  make -j4
  cd ..
  cd python; python setup.py install; cd ..
  cd topi/python; python setup.py install; cd ../..
  cd nnvm/python; python setup.py install; cd ../..
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code># Start virtual display
  from pyvirtualdisplay import Display
  display = Display(visible=0, size=(1024, 768))
  display.start()
  import os
  os.environ["DISPLAY"] = ":" + str(display.display) + "." + str(display.screen)
  
  import tvm 
  import numpy as np
  
  nn = 1024
  n = tvm.var('n')
  n = tvm.convert(nn)
  m = n
  l = n
  A = tvm.placeholder((n, l), name='A', dtype='int32')
  B = tvm.placeholder((m, l), name='B', dtype='int32')
  k = tvm.reduce_axis((0, l), name='k')
  C = tvm.compute((n, m), lambda ii, jj: tvm.sum(A[ii, k] * B[jj, k], axis=k),
                  name='CC')
  
  s = tvm.create_schedule(C.op)
  s[C].opengl()
  print(tvm.lower(s, [A, B, C], simple_mode=True))
  
  f = tvm.build(s, [A, B, C], "opengl", name="gemm")
  print("------opengl code------")
  print(f.imported_modules[0].get_source(fmt="gl"))
  
  ctx = tvm.opengl()
  n, m, l = nn, nn, nn
  a_np = np.random.uniform(low=0, high=10, size=(n, l)).astype(A.dtype)
  b_np = np.random.uniform(low=0, high=10, size=(m, l)).astype(B.dtype)
  a = tvm.nd.array(a_np, ctx)
  b = tvm.nd.array(b_np, ctx)
  c = tvm.nd.array(np.zeros((n, m), dtype=C.dtype), ctx)
  f(a, b, c)
  
  tvm.testing.assert_allclose(c.asnumpy(), np.dot(a_np, b_np.T))
  </code></pre>
  
  <p>I expect the OpenGL code with <code>f.imported_modules[0].get_source(fmt="gl")</code> but it failed due to the following X11 related error.</p>
  
  <pre><code>// attr [compute(CC, 0x4768580)] opengl_stage_scope = ""
  produce CC {
    // attr [iter_var(threadIdx.x, , threadIdx.x)] thread_extent = 1048576
    glsl_texture_store(CC, 0)
    for (k, 0, 1024) {
      glsl_texture_store(CC, (CC[threadIdx.x] + (A[(((threadIdx.x/1024)*1024) + k)]*B[(((threadIdx.x % 1024)*1024) + k)])))
    }
  }
  
  ---------------------------------------------------------------------------
  TVMError                                  Traceback (most recent call last)
  &lt;ipython-input-2-3a202d10b787&gt; in &lt;module&gt;()
       23 print(tvm.lower(s, [A, B, C], simple_mode=True))
       24 
  ---&gt; 25 f = tvm.build(s, [A, B, C], "opengl", name="gemm")
       26 print("------opengl code------")
       27 print(f.imported_modules[0].get_source(fmt="gl"))
  
  6 frames
  /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/_ffi/_cython/base.pxi in tvm._ffi._cy3.core.CALL()
  
  TVMError: Traceback (most recent call last):
    [bt] (8) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::codegen::BuildOpenGL(tvm::Array&lt;tvm::LoweredFunc, void&gt;)+0x1c3) [0x7f3742ec4573]
    [bt] (7) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::OpenGLModuleCreate(std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, tvm::runtime::OpenGLShader, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, tvm::runtime::OpenGLShader&gt; &gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, tvm::runtime::FunctionInfo, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, tvm::runtime::FunctionInfo&gt; &gt; &gt;)+0xeb) [0x7f374364b7fb]
    [bt] (6) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::OpenGLModuleNode::OpenGLModuleNode(std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, tvm::runtime::OpenGLShader, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, tvm::runtime::OpenGLShader&gt; &gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, tvm::runtime::FunctionInfo, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, tvm::runtime::FunctionInfo&gt; &gt; &gt;)+0x92) [0x7f374364b1d2]
    [bt] (5) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::gl::OpenGLWorkspace::Global()+0x55) [0x7f37436438d5]
    [bt] (4) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::gl::OpenGLWorkspace::OpenGLWorkspace()+0x4d) [0x7f3743640b7d]
    [bt] (3) /usr/lib/x86_64-linux-gnu/libglfw.so.3(glfwInit+0x2d) [0x7f3725a7ab3d]
    [bt] (2) /usr/lib/x86_64-linux-gnu/libglfw.so.3(+0xb858) [0x7f3725a80858]
    [bt] (1) /usr/lib/x86_64-linux-gnu/libglfw.so.3(+0x59f7) [0x7f3725a7a9f7]
    [bt] (0) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::gl::GlfwErrorCallback(int, char const*)+0x2f9) [0x7f374363b7c9]
    File "/content/tvm/src/runtime/opengl/opengl_device_api.cc", line 82
  Error: [65544] X11: RandR gamma ramp support seems broken
  </code></pre>