code: def hand_coded_optimizations(self):
      self.required_optimizations()
  
      # should use matvec - TODO: adjust/tune based on the wide vs tall/large vs small mat
      MV_BLOCKSIZE, MV_THREADS_PER_ROW, MV_ROWS_PER_THREAD = (
          getenv("MV_BLOCKSIZE", 4),
          getenv("MV_THREADS_PER_ROW", 8),
          getenv("MV_ROWS_PER_THREAD", 4),
      )
      if (
          self.opts.has_local
          and getenv("MV", 1) != 0
          and (
              MV_BLOCKSIZE > 1
              or MV_THREADS_PER_ROW > 1
              or MV_ROWS_PER_THREAD > 1
          )
          and self.reduceop
          and self.reduceop.op == ReduceOps.SUM
          and len(self.full_shape) >= 2
          and self.opts.has_shared
          and (mulop := self.reduceop.src[0]).op == BinaryOps.MUL
          and mulop.src[0].op == BufferOps.LOAD
          and mulop.src[1].op == BufferOps.LOAD
      ):
          st0, st1 = (
              self.sts[self.bufs.index(mulop.src[0].arg)],
              self.sts[self.bufs.index(mulop.src[1].arg)],
          )
          strides0, strides1 = st0.real_strides(), st1.real_strides()
  
          def has_expanded_axis(shape, strides):
              return any(
                  s > 1 and st == 0 for s, st in zip(shape, strides)
              )
  
          if strides0[self.first_reduce] == 1 and not (
              has_expanded_axis(st0.shape, strides0)
              and has_expanded_axis(st1.shape, strides1)
          ):
              for global_idx in range(self.global_dims):
                  if (
                      self.full_shape[self.first_reduce]
                      % MV_THREADS_PER_ROW
                      == 0
                      and self.full_shape[global_idx]
                      % (MV_BLOCKSIZE * MV_ROWS_PER_THREAD)
                      == 0
                  ):
                      if DEBUG >= 3:
                          print(
                              f"MATVEC: {self.full_shape=} {self.first_reduce=} {strides0=} {MV_BLOCKSIZE=} {MV_THREADS_PER_ROW=} {MV_ROWS_PER_THREAD=}"
                          )
                      if MV_THREADS_PER_ROW > 1:
                          self.apply_opt(
                              Opt(OptOps.GROUP, 0, MV_THREADS_PER_ROW)
                          )
                      if MV_BLOCKSIZE > 1:
                          self.apply_opt(
                              Opt(
                                  OptOps.LOCAL, global_idx, MV_BLOCKSIZE
                              )
                          )
                      if MV_ROWS_PER_THREAD > 1:
                          self.apply_opt(
                              Opt(
                                  OptOps.UPCAST,
                                  global_idx,
                                  MV_ROWS_PER_THREAD,
                              )
                          )
                      return
  
      if (
          self.opts.has_local
          and self.opts.has_shared
          and all_int(self.sts[0].shape[: self.first_reduce])
      ):
          # are we grouping? (requires local shape support)
          if (
              not self.float4_axis(0)
              and self.first_reduce <= 2
              and self.first_reduce + 1 <= self.shape_len
              and prod(self.sts[0].shape[: self.first_reduce]) <= 2048
          ):  # noqa: E501
              # TODO: use 1024 if it's allowed in a smarter way
              for sz in (
                  ([256, 16])
                  if prod(self.sts[0].shape[: self.first_reduce]) <= 32
                  else [16]
              ):
                  if all(
                      st.shape[self.first_reduce] % sz == 0
                      or st.shape[self.first_reduce] == 1
                      for st in self.sts
                  ):
                      self.apply_opt(Opt(OptOps.GROUPTOP, 0, sz))
                      break
  
          # are we upcasting in mid reduce? (only for images)
          if (
              self.bufs[0].dtype.name.startswith("image")
              and not self.float4_axis(0)
              and self.group_for_reduce
              and self.first_reduce <= 2
              and prod(self.sts[0].shape) > 1
          ):  # noqa: E501
              axes = self.sts[0].unit_stride_axes()
              assert (
                  len(axes) == 1
              ), f"wrong number of stride 1 axis : {axes}"
              if self.sts[0].shape[axes[0]] % 4 == 0:
                  self.apply_opt(Opt(OptOps.UPCASTMID, axes[0], 4))
  
      # upcast float4 images
      for buf_index, buf in enumerate(self.bufs):
          unit_stride_axes_mul_4 = [
              i
              for i in self.sts[buf_index].unit_stride_axes(
                  ignore_valid=True
              )
              if self.sts[buf_index].shape[i] % 4 == 0
          ]
          if buf.dtype.__class__ is ImageDType:
              # assert len(unit_stride_axes_mul_4) >= 1, f"needs a unit stride axis in {self.bufs[buf_index]}"
              if (
                  len(unit_stride_axes_mul_4)
                  and all(
                      x < (self.shape_len - self.upcasted)
                      for x in unit_stride_axes_mul_4
                  )
                  and unit_stride_axes_mul_4[0]
                  not in self.upcast_in_mid_reduce_axes
              ):  # noqa: E501
                  if unit_stride_axes_mul_4[0] < self.first_reduce:
                      self.apply_opt(
                          Opt(
                              OptOps.UPCAST,
                              unit_stride_axes_mul_4[0],
                              4,
                          )
                      )
                  else:
                      self.apply_opt(
                          Opt(
                              OptOps.UNROLL,
                              unit_stride_axes_mul_4[0]
                              - self.first_reduce,
                              4,
                          )
                      )
  
      # no more opt if we are grouping
      if self.group_for_reduce:
          return
  
      # **** below this line need to be optional and benchmarked ****
  
      # TODO: doing extra upcasts with images doesn't work for some reason (maybe has to do with to_image_idx)
      # to trigger the above bug, remove prod(self.full_shape[self.shape_len - self.upcasted:]) from the below
      # expression and run test/test_ops.py with IMAGE=2
      # if there are small dims with lots of valid masks, upcast them (they might be from Tensor.stack)
      # this can be made much smarter
      to_upcast: List[int] = []
      # upcast leading axes first (hack-ish for winograd; we actually want to upcast masked axes with low stride first)
      for axis in range(self.first_reduce):
          # we might want to be able to split axes that are masked, or refuse to merge them in simplify_merge_adjacent
          # for now skip upcasting here if there is a symbolic axis
          if (
              isinstance(self.full_shape[axis], int)
              and self.full_shape[axis] <= 7
              and any(st.axis_is_masked(axis) for st in self.sts)
              and prod(
                  self.full_shape[self.shape_len - self.upcasted :]
              )
              * prod(self.full_shape[j] for j in to_upcast)
              * self.full_shape[axis]
              <= 7 * 7
          ):
              if DEBUG >= 4:
                  print(f"upcasting masked axis : {axis}")
              to_upcast.append(axis)
      for axis in to_upcast[::-1]:
          self.apply_opt(Opt(OptOps.UPCAST, axis, 0))
  
      # potentially do more upcasts of non reduce axes based on a heuristic
      upcasted_axis = set()
      while prod(self.sts[0].shape[: self.first_reduce]) >= 1024:
          xb_choices = []
          for axis, upcast_amount in itertools.product(
              range(self.first_reduce), [3, 4]
          ):  # consider all the non reduce axes, and a 3 or 4 reduce
              # if we haven't upcasted it, it's not symbolic, it mods, and buffer has stride 0 on axis while having no stride 0 in the upcasted axis already
              if (
                  axis not in upcasted_axis
                  and isinstance(self.full_shape[axis], int)
                  and self.full_shape[axis] % upcast_amount == 0
                  and any(
                      st.views[-1].strides[axis] == 0
                      and not any(
                          x[1] == 0
                          for x in self.upcasted_axis(buf_index)
                      )
                      for buf_index, st in enumerate(self.sts)
                  )
              ):  # noqa: E501
                  xb_choices.append(
                      (
                          sum(
                              st.views[-1].strides[axis] > 0
                              for st in self.sts
                          ),
                          sum(
                              st.views[-1].strides[axis]
                              for st in self.sts
                          ),
                          axis,
                          upcast_amount,
                      )
                  )  # noqa: E501
          if xb_choices:
              xb_choices = sorted(xb_choices)
              if DEBUG >= 4:
                  print(f"float4 merging axis : {xb_choices}")
              self.apply_opt(
                  Opt(OptOps.UPCAST, xb_choices[0][2], xb_choices[0][3])
              )
              upcasted_axis.add(xb_choices[0][2])
          else:
              break
  
      # if last dim is small(ish) and it's a reduce dim, upcast the reduce (loop unrolling). no simplify needed since it's just an upcast.
      if (
          self.first_reduce < (self.shape_len - self.upcasted)
          and (
              len(list(self.shape_offsets(self.full_buf_index))) <= 4
              or not any(
                  r
                  for _, _, r in self.upcasted_axis(self.full_buf_index)
              )
          )
          and (
              self.upcasted == 0
              or prod(self.full_shape[-self.upcasted :]) < 64
          )
      ):  # noqa: E501
          if (s := self.full_unupcasted_shape[-1]) <= 32 and isinstance(
              s, int
          ):  # NOTE: cannot loop unroll symbolic axis
              self.apply_opt(
                  Opt(
                      OptOps.UNROLL,
                      len(self.full_unupcasted_shape)
                      - 1
                      - self.first_reduce,
                      0,
                  )
              )
              # if it's small, upcast a second reduce dimension too
              if (
                  self.first_reduce < (self.shape_len - self.upcasted)
                  and s <= 3
                  and (s2 := self.full_unupcasted_shape[-1]) <= 3
                  and isinstance(s2, int)
              ):
                  self.apply_opt(
                      Opt(
                          OptOps.UNROLL,
                          len(self.full_unupcasted_shape)
                          - 1
                          - self.first_reduce,
                          0,
                      )
                  )
          else:
              for splits in [4]:
                  if self.full_unupcasted_shape[-1] % splits == 0:
                      self.apply_opt(
                          Opt(
                              OptOps.UNROLL,
                              len(self.full_unupcasted_shape)
                              - 1
                              - self.first_reduce,
                              splits,
                          )
                      )
                      break
  
      # if nothing at all is upcasted and it's easy to, do an upcast
      # TODO: this is breaking the tests
      for splits in [4]:
          if (
              self.upcasted == 0
              and self.full_unupcasted_shape
              and self.full_unupcasted_shape[-1] % splits == 0
          ):
              self.apply_opt(
                  Opt(
                      OptOps.UPCAST,
                      len(self.full_unupcasted_shape) - 1,
                      splits,
                  )
              )
  
      # **** local groups ****
  
      if self.opts.has_local:
          if (
              getenv("NOLOCALS")
              and self.local_dims == 0
              and not self.group_for_reduce
          ):
              self.apply_opt(Opt(OptOps.NOLOCALS))
          else:
              # prioritize making expand axes local
              local_axis_ranking = [
                  (
                      any(
                          self.sts[buf_index].views[-1].strides[axis]
                          == 0
                          for buf_index in range(len(self.sts))
                      ),
                      axis,
                  )
                  for axis in range(
                      len(self.full_shape[: self.first_reduce])
                  )
              ]  # noqa: E501
              to_local: List[Tuple[int, int]] = []
              for _, axis in sorted(
                  local_axis_ranking, key=lambda x: (-x[0], -x[1])
              ):
                  local_size = prod(sz for _, sz in to_local)
                  local_sz: Optional[int] = next(
                      (
                          x
                          for x in (
                              [32] * (axis == 0) + [16, 8, 4, 3, 2]
                          )
                          if self.full_shape[axis] % x == 0
                          and local_size * x <= 128
                      ),
                      None,
                  )  # noqa: E501
                  if local_sz is not None:
                      to_local.append((axis, local_sz))
              deleted_shape = 0
              for axis, local_sz in sorted(to_local[:3]):
                  axis = axis - deleted_shape
                  will_delete_shape = local_sz == self.full_shape[axis]
                  self.apply_opt(Opt(OptOps.LOCAL, axis, local_sz))
                  if will_delete_shape:
                      deleted_shape += 1
  
imports: from __future__ import annotations
  import os, math, itertools
  from typing import NamedTuple, Optional, List, Tuple, cast, Dict, Union
  from tinygrad.ops import LazyOp, FlopCounter, get_lazyop_info, UnaryOps, BinaryOps, ReduceOps, MemBuffer, ConstBuffer, BufferOps
  from tinygrad.device import Device, Compiled
  from tinygrad.dtype import dtypes, ImageDType, DType
  from tinygrad.helpers import dedup, colored, ansilen, getenv, prod, DEBUG, round_up, all_int
  from tinygrad.shape.shapetracker import ShapeTracker, get_contraction
  from tinygrad.shape.symbolic import sint
  from tinygrad.shape.view import View, strides_for_shape
  from dataclasses import dataclass
  from enum import Enum, auto
label_desc: Use image node_id 29 for calling the Kernel obj's underlying member method code with example usage: obj.hand_coded_optimizations() and returns: any