code: def apply_tensor_cores(
      self, use_tensor_cores=1, extra_opts: Optional[List[Opt]] = None
  ) -> bool:
      if (
          use_tensor_cores
          and self.opts.has_local
          and self.reduceop
          and self.reduceop.op == ReduceOps.SUM
          and self.opts.device in tensor_cores
      ):
          for tc in tensor_cores[self.opts.device]:
              if not (
                  use_tensor_cores == 2
                  or (tc.arch is None or tc.arch == os.uname().machine)
              ):
                  continue
              has_cast = tc.dtype_in != tc.dtype_out
  
              if has_cast and not (
                  self.reduceop.src[0].op == UnaryOps.CAST
                  and self.reduceop.src[0].arg[0] == tc.dtype_out
              ):
                  continue
              mul_op = (
                  self.reduceop.src[0].src[0]
                  if has_cast
                  else self.reduceop.src[0]
              )
  
              if mul_op.op != BinaryOps.MUL:
                  continue
              if not (
                  mul_op.src[0].op == BufferOps.LOAD
                  and mul_op.src[0].arg.dtype == tc.dtype_in
              ):
                  continue
              if not (
                  mul_op.src[1].op == BufferOps.LOAD
                  and mul_op.src[1].arg.dtype == tc.dtype_in
              ):
                  continue
              buf0, buf1 = self.bufs.index(
                  cast(MemBuffer, mul_op.src[0].arg)
              ), self.bufs.index(cast(MemBuffer, mul_op.src[1].arg))
              buf0_strides, buf1_strides = (
                  self.sts[buf0].real_strides(),
                  self.sts[buf1].real_strides(),
              )
              axis_buf0 = [
                  (i, self.full_shape[i], buf1_strides[i])
                  for i, s in enumerate(
                      buf0_strides[: self.first_reduce]
                  )
                  if s == 0 and self.full_shape[i] % tc.dims[0] == 0
              ]  # noqa: E501
              axis_buf1 = [
                  (i, self.full_shape[i], buf0_strides[i])
                  for i, s in enumerate(
                      buf1_strides[: self.first_reduce]
                  )
                  if s == 0 and self.full_shape[i] % tc.dims[1] == 0
              ]  # noqa: E501
  
              if not (
                  axis_buf0
                  and axis_buf1
                  and self.full_shape[self.first_reduce] % tc.dims[2]
                  == 0
                  and self.full_shape[self.first_reduce] >= tc.dims[2]
                  and (self.shape_len - self.first_reduce) == 1
              ):
                  continue  # noqa: E501
  
              if DEBUG >= 3:
                  print("TENSOR CORES", axis_buf0, axis_buf1, tc)
  
              s0, s1 = (
                  axis_buf0[-1][0],
                  axis_buf1[-1][0],
              )  # TODO: select axis in smart way
              s0_exists, s1_exists = True, True
              assert (
                  s0 != s1
                  and self.full_shape[s0] % tc.dims[0] == 0
                  and self.full_shape[s1] % tc.dims[1] == 0
              )
  
              def fix(needed, ax):
                  nonlocal s0, s1, s0_exists, s1_exists
                  if not needed:
                      return
                  if s0_exists and ax == s0:
                      if s1_exists and s0 < s1:
                          s1 -= 1
                      s0_exists = False
                  elif s1_exists and ax == s1:
                      if s0_exists and s1 < s0:
                          s0 -= 1
                      s1_exists = False
  
              # tensor core -- unroll the reduce dim, upcast input, then create the correct thread pattern
              self.apply_opt(Opt(OptOps.UNROLL, 0, tc.dims[2]))
              self.apply_opt(
                  Opt(
                      OptOps.UPCAST,
                      s0 if tc.upcast_dim == 0 else s1,
                      (tc.dims[0] * tc.dims[2])
                      // prod([a[1] for a in tc.threads]),
                  )
              )
              for tc_dim, tc_amt in tc.threads:
                  fix(
                      self.apply_opt(
                          Opt(
                              OptOps.LASTLOCAL,
                              s0 if tc_dim == 0 else s1,
                              tc_amt,
                          )
                      ),
                      s0 if tc_dim == 0 else s1,
                  )
  
              # assert tensor core and prevent extra_opts from altering the key shape structure
              if use_tensor_cores == 1:
                  self.tensor_core = (
                      tc  # TC=2 will do the shape ops without the WMMA
                  )
  
              if extra_opts is not None:
                  for opt in extra_opts:
                      self.apply_opt(opt)
              else:
                  # hand-coded TC opts
                  if s1_exists:
                      s1_div = [
                          upc
                          for upc in [5, 4, 3, 2, 1]
                          if self.full_shape[s1] % upc == 0
                      ][0]
                      if s1_div != 1:
                          fix(
                              self.apply_opt(
                                  Opt(OptOps.UPCAST, s1, s1_div)
                              ),
                              s1,
                          )
                  if s0_exists:
                      s0_div = [
                          upc
                          for upc in [5, 4, 3, 2, 1]
                          if self.full_shape[s0] % upc == 0
                      ][0]
                      if s0_div != 1:
                          fix(
                              self.apply_opt(
                                  Opt(OptOps.UPCAST, s0, s0_div)
                              ),
                              s0,
                          )
                  if self.tensor_core and s0_exists:
                      for upc in [4, 2]:
                          if self.full_shape[s0] % upc == 0:
                              self.apply_opt(
                                  Opt(OptOps.LASTLOCAL, s0, upc)
                              )
                              break
  
              # alias buffer
              alias_pattern = (
                  [0]
                  * (
                      self.global_dims
                      + (self.local_dims - len(tc.threads))
                  )
                  + [2] * (len(tc.threads))
                  + [0]
                  * (self.shape_len - self.upcasted - self.first_reduce)
                  + [1, 1]
                  + [3] * (self.upcasted - 2)
              )  # noqa: E501
              self.alias_buffer(buf0, alias_pattern)
              self.alias_buffer(buf1, alias_pattern)
              return True
      return False
  
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 26 for calling the Kernel obj's underlying member method code with example usage: obj.apply_tensor_cores(use_tensor_cores, extra_opts) and returns: False, True