code: def optimize_local_size(
      clprg: Callable, global_size: List[int], rawbufs: List[Buffer]
  ) -> List[int]:
      test_rawbuffers = (
          [
              Buffer(
                  rawbufs[0].device, rawbufs[0].size, rawbufs[0].dtype
              ),
              *rawbufs[1:],
          ]
          if rawbufs[0] in rawbufs[1:]
          else rawbufs
      )
      MAX_WORKGROUP = 1024
      local_dims = [
          [
              x
              for x in set(
                  [sz, 1, 2, 4, 8, 16, 32, 64, 128, 256, MAX_WORKGROUP]
              )
              if x <= sz
          ]
          for sz in global_size
      ]
      local_sizes = [
          list(x)
          for x in itertools.product(*local_dims)
          if prod(x) <= MAX_WORKGROUP
      ] * 2  # try each valid size twice
  
      def try_exec(local_size):
          try:
              return clprg(
                  *[x._buf for x in test_rawbuffers],
                  global_size=[
                      g // l if g % l == 0 else g / l
                      for g, l in zip(global_size, local_size)
                  ],
                  local_size=local_size,
                  wait=True,
              )  # noqa: E501
          except Exception:
              return float("inf")
  
      ret = min(
          [
              (try_exec(local_size), local_size)
              for local_size in random.sample(
                  local_sizes, len(local_sizes)
              )
          ]
      )
      assert not math.isinf(
          ret[0]
      ), "all optimize_local_size exec failed"
      return ret[1]
  
imports: from typing import Dict, List, cast, DefaultDict, Optional, Tuple, Callable
  import itertools, functools, random, math, time, multiprocessing, traceback, signal
  from tinygrad.device import Device, Compiled, Buffer, CompiledASTRunner
  from tinygrad.ops import MemBuffer, LazyOp
  from tinygrad.helpers import prod, flatten, DEBUG, CACHELEVEL, diskcache_get, diskcache_put, getenv, Context, colored, to_function_name
  from tinygrad.dtype import ImageDType
  from tinygrad.codegen.linearizer import Linearizer
  from collections import defaultdict
  from tinygrad.tensor import Tensor
  from tinygrad.shape.symbolic import sym_infer
  from tinygrad.codegen.kernel import Opt, OptOps
label_desc: Use image node_id 9 for calling a global function with example usage: optimize_local_size(clprg, global_size, rawbufs) and returns: ret, clprg, float