code: def load(self, input_fn):
      float32 = not FLOAT16
  
      mf = cl.mem_flags
      image_fmt = cl.ImageFormat(
          cl.channel_order.RGBA,
          cl.channel_type.FLOAT
          if float32
          else cl.channel_type.HALF_FLOAT,
      )
      image_fmt_32 = cl.ImageFormat(
          cl.channel_order.RGBA, cl.channel_type.FLOAT
      )
  
      with open(input_fn, "rb") as f:
          json_len = struct.unpack("I", f.read(4))[0]
          jdat = json.loads(f.read(json_len).decode("latin_1"))
          weights = f.read()
  
      # load in the buffers
      bufs = {"\x00\x00\x00\x00\x00\x00\x00\x00": None}
      bufs_loaded = {}
      ptr = 0
      for o in jdat["objects"]:
          # print(o)
          if o["needs_load"]:
              nptr = ptr + o["size"]
              o["data"] = weights[ptr:nptr]
              ptr = nptr
  
          if (
              o["arg_type"] == "image2d_t"
              or o["arg_type"] == "image1d_t"
          ):
              tfmt = (
                  image_fmt_32
                  if "float32" in o and o["float32"]
                  else image_fmt
              )
              if o["arg_type"] == "image2d_t":
                  if (
                      "buffer_id" in o
                      and o["height"] == 1
                      and not bufs_loaded[o["buffer_id"]]
                  ):
                      # hack: use a image1d since we can back that with a buffer
                      buf = cl.Image(
                          CL.ctx,
                          mf.READ_WRITE,
                          tfmt,
                          shape=(o["width"],),
                          buffer=bufs[o["buffer_id"]],
                      )
                  else:
                      # buffer isn't supported in image2d, copy buffer into image
                      if (
                          "buffer_id" in o
                          and bufs_loaded[o["buffer_id"]]
                      ):
                          arr = np.zeros(
                              bufs[o["buffer_id"]].size // 2,
                              dtype=np.float16,
                          )
                          cl.enqueue_copy(
                              CL.queue, arr, bufs[o["buffer_id"]]
                          )
                          buf = cl.Image(
                              CL.ctx,
                              mf.READ_WRITE | mf.COPY_HOST_PTR,
                              tfmt,
                              shape=(o["width"], o["height"]),
                              pitches=(o["row_pitch"],),
                              hostbuf=arr,
                          )
                      elif o["needs_load"]:
                          buf = cl.Image(
                              CL.ctx,
                              mf.READ_WRITE | mf.COPY_HOST_PTR,
                              tfmt,
                              shape=(o["width"], o["height"]),
                              pitches=(o["row_pitch"],),
                              hostbuf=o["data"],
                          )
                      else:
                          buf = cl.Image(
                              CL.ctx,
                              mf.READ_WRITE,
                              tfmt,
                              shape=(o["width"], o["height"]),
                          )
              if o["arg_type"] == "image1d_t":
                  assert not o["needs_load"]
                  assert not bufs_loaded[o["buffer_id"]]
                  buf = cl.Image(
                      CL.ctx,
                      mf.READ_WRITE,
                      tfmt,
                      shape=(o["width"],),
                      buffer=bufs[o["buffer_id"]],
                  )
          else:
              if "data" in o:
                  buf = cl.Buffer(
                      CL.ctx,
                      mf.READ_WRITE | mf.COPY_HOST_PTR,
                      hostbuf=o["data"],
                  )
              else:
                  # zero out buffers
                  buf = cl.Buffer(
                      CL.ctx,
                      mf.READ_WRITE | mf.COPY_HOST_PTR,
                      hostbuf=b"\x00" * o["size"],
                  )
  
          bufs[o["id"]] = buf
          bufs_loaded[o["id"]] = "data" in o
          # if it's loaded, it's saved
          if "data" in o:
              self.buffers_to_save.add(buf)
  
      # load binaries
      prgs = {}
      for o in jdat["binaries"]:
          nptr = ptr + o["length"]
          prgs[o["name"]] = CLProgram(
              Device["GPU"], o["name"], weights[ptr:nptr]
          )
          ptr = nptr
  
      # populate the cl_cache
      for i, k in enumerate(jdat["kernels"]):
          kernel = prgs[k["name"]]
          aaa = []
          for j, (a, sz) in enumerate(zip(k["args"], k["args_size"])):
              if len(a) == 0:
                  aa = cl.LocalMemory(sz)
              elif len(a) == 4:
                  a = a.encode("latin_1")
                  aa = np.uint32(struct.unpack("I", a)[0])
              elif len(a) == 2:
                  a = a.encode("latin_1")
                  aa = np.uint16(struct.unpack("H", a)[0])
              elif len(a) == 8:
                  # print(i,j,struct.unpack("Q", a.encode('latin_1'))[0])
                  aa = bufs[a]
              aaa.append(aa)
          self.cl_cache.append(
              (
                  kernel,
                  [k["global_work_size"], k["local_work_size"], *aaa],
              )
          )
  
      if DEBUG >= 1:
          print(f"thneed: total bufs loaded: {len(bufs.keys())}")
  
      # load inputs
      for k in jdat["inputs"]:
          self.inputs[k["name"]] = bufs[k["buffer_id"]]
  
      # load outputs
      for k in jdat["outputs"]:
          self.outputs.append(bufs[k["buffer_id"]])
  
file_path: tinygrad/extra/thneed.py
imports: import time
  import struct
  import json
  import traceback
  import numpy
  from tinygrad.runtime.ops_gpu import CLProgram, compile_gpu
  from tinygrad.device import Device
  from tinygrad.helpers import DEBUG, getenv
  from collections import defaultdict
  import pyopencl
  from tinygrad.runtime.ops_gpu import OSX_TIMING_RATIO
label_desc: Use image node_id 2 for calling the Thneed obj's underlying member method code with example usage: obj.load(input_fn) without return types