# ruff: noqa: E501
import os
os.environ["VALIDATE_HCQ"]="1"

import unittest, random
import numpy as np
from tinygrad.codegen.kernel import Kernel, KernelOptError
from tinygrad.device import is_dtype_supported
from tinygrad.ops import UOp, Ops
from tinygrad.engine.search import Opt, OptOps
from tinygrad import Device, dtypes, Tensor
from test.external.fuzz_linearizer import compare_linearizer, compare_states, get_fuzz_rawbuf_like

from tinygrad.shape.shapetracker import ShapeTracker
from tinygrad.shape.view import View

def helper_test_lin(lin: Kernel, opts, failed_platforms, validate_device, rtol=1e-2, atol=1e-2):
  if any(b.dtype.base == dtypes.half for b in lin.membufs) and not is_dtype_supported(dtypes.half): return
  if any(b.dtype.base == dtypes.bfloat16 for b in lin.membufs) and not is_dtype_supported(dtypes.bfloat16): return

  for opt in opts:
    try:
      lin.apply_opt(opt)
    except KernelOptError:
      # it's considered fixed if we invalidated the opts
      assert Device.DEFAULT not in failed_platforms, f"unexpected success on {Device.DEFAULT}"
      return

  (msg, rawbufs, var_vals, ground_truth, state1) = compare_linearizer(lin, rtol=rtol, atol=atol)
  if msg in ["PASS", "KernelOptError"]:
    # it's considered fixed if we invalidated the opts
    assert Device.DEFAULT not in failed_platforms, f"unexpected success on {Device.DEFAULT}"
  else:
    assert Device.DEFAULT in failed_platforms, f"failed on {Device.DEFAULT} with {msg}"

  validate_lin = lin.copy()
  validate_lin.opts = validate_device.renderer
  validate_rawbufs = [get_fuzz_rawbuf_like(x, copy=True, force_device=validate_device.dname) for x in rawbufs]
  (_msg, _, _, _, state2) = compare_linearizer(validate_lin, validate_rawbufs, var_vals, ground_truth, rtol=rtol, atol=atol)

  if _msg in ["PASS"] and compare_states(state1, state2):
    assert Device.DEFAULT not in failed_platforms, f"unexpected success on {Device.DEFAULT}"
  else:
    assert Device.DEFAULT in failed_platforms, f"failed on {Device.DEFAULT} with {msg}"

  return lin

class TestHCQFuzzFailures(unittest.TestCase):
  def setUp(self):
    random.seed(42)
    np.random.seed(42)
    Tensor.manual_seed(42)

  @unittest.skipUnless(Device.DEFAULT in {"QCOM"}, "for QCOM")
  def test_failure_1(self):
    ast = UOp(Ops.SINK, dtypes.void, arg=None, src=(  UOp(Ops.STORE, dtypes.void, arg=None, src=(    UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=0, src=()),    UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=0, mask=None, contiguous=True),)), src=()),    UOp(Ops.ADD, dtypes.float, arg=None, src=(      UOp(Ops.ADD, dtypes.float, arg=None, src=(        UOp(Ops.ADD, dtypes.float, arg=None, src=(          UOp(Ops.ADD, dtypes.float, arg=None, src=(            UOp(Ops.ADD, dtypes.float, arg=None, src=(              UOp(Ops.ADD, dtypes.float, arg=None, src=(                UOp(Ops.ADD, dtypes.float, arg=None, src=(                  UOp(Ops.ADD, dtypes.float, arg=None, src=(                    UOp(Ops.ADD, dtypes.float, arg=None, src=(                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                        UOp(Ops.ADD, dtypes.float, arg=None, src=(                          UOp(Ops.ADD, dtypes.float, arg=None, src=(                            UOp(Ops.ADD, dtypes.float, arg=None, src=(                              UOp(Ops.ADD, dtypes.float, arg=None, src=(                                UOp(Ops.ADD, dtypes.float, arg=None, src=(                                  UOp(Ops.ADD, dtypes.float, arg=None, src=(                                    UOp(Ops.ADD, dtypes.float, arg=None, src=(                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                        UOp(Ops.ADD, dtypes.float, arg=None, src=(                                          UOp(Ops.ADD, dtypes.float, arg=None, src=(                                            UOp(Ops.ADD, dtypes.float, arg=None, src=(                                              UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                  UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                    UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                        UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                          UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                            UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                              UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                  UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                    UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 2, 4)), arg=1, src=()),                                                                          x39:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=0, mask=((0, 1), (0, 6)), contiguous=False),)), src=()),)),                                                                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=2, src=()),                                                                             x39,)),)),)),                                                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 2, 4)), arg=3, src=()),                                                                          x46:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-6, mask=((0, 1), (6, 12)), contiguous=False),)), src=()),)),                                                                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=4, src=()),                                                                             x46,)),)),)),)),                                                                    UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=5, src=()),                                                                          x54:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (12, 13)), contiguous=False),)), src=()),)),                                                                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=6, src=()),                                                                             x54,)),)),)),)),)),                                                                  UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                    UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=7, src=()),                                                                    UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-13, mask=((0, 1), (13, 17)), contiguous=False),)), src=()),)),)),                                                                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                  UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=8, src=()),                                                                  UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-17, mask=((0, 1), (17, 21)), contiguous=False),)), src=()),)),)),                                                              UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                                UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                                  UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                    UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=9, src=()),                                                                    x68:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (21, 22)), contiguous=False),)), src=()),)),                                                                  UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                                    UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                      UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=10, src=()),                                                                       x68,)),)),)),)),)),                                                            UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                              UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=11, src=()),                                                              UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-22, mask=((0, 1), (22, 26)), contiguous=False),)), src=()),)),)),                                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=12, src=()),                                                            UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-26, mask=((0, 1), (26, 30)), contiguous=False),)), src=()),)),)),                                                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                          UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                            UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                              UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=13, src=()),                                                              x82:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (30, 31)), contiguous=False),)), src=()),)),                                                            UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                                UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=14, src=()),                                                                 x82,)),)),)),)),)),                                                      UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                        UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                            UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=15, src=()),                                                            x90:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (31, 32)), contiguous=False),)), src=()),)),                                                          UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                            UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                              UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=16, src=()),                                                               x90,)),)),)),)),)),                                                    UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=17, src=()),                                                          x98:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (32, 33)), contiguous=False),)), src=()),)),                                                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=18, src=()),                                                             x98,)),)),)),)),)),                                                  UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                    UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                      UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                        UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=19, src=()),                                                        x106:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (33, 34)), contiguous=False),)), src=()),)),                                                      UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                          UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=20, src=()),                                                           x106,)),)),)),)),)),                                                UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                  UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                    UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                      UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=21, src=()),                                                      x114:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (34, 35)), contiguous=False),)), src=()),)),                                                    UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                      UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                        UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=22, src=()),                                                         x114,)),)),)),)),)),                                              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=23, src=()),                                                UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-35, mask=((0, 1), (35, 39)), contiguous=False),)), src=()),)),)),                                            UOp(Ops.CAST, dtypes.float, arg=None, src=(                                              UOp(Ops.ADD, dtypes.float, arg=None, src=(                                                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                  UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=24, src=()),                                                  x125:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (39, 40)), contiguous=False),)), src=()),)),                                                UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                  UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                    UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=25, src=()),                                                     x125,)),)),)),)),)),                                          UOp(Ops.CAST, dtypes.float, arg=None, src=(                                            UOp(Ops.ADD, dtypes.float, arg=None, src=(                                              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=26, src=()),                                                x133:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (40, 41)), contiguous=False),)), src=()),)),                                              UOp(Ops.CAST, dtypes.float, arg=None, src=(                                                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                                  UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=27, src=()),                                                   x133,)),)),)),)),)),                                        UOp(Ops.ADD, dtypes.float, arg=None, src=(                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                            UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 2, 4)), arg=28, src=()),                                            x140:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-41, mask=((0, 1), (41, 47)), contiguous=False),)), src=()),)),                                          UOp(Ops.CAST, dtypes.float, arg=None, src=(                                            UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                              UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=29, src=()),                                               x140,)),)),)),)),                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 2, 4)), arg=30, src=()),                                          x147:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-47, mask=((0, 1), (47, 53)), contiguous=False),)), src=()),)),                                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=31, src=()),                                             x147,)),)),)),)),                                    UOp(Ops.CAST, dtypes.float, arg=None, src=(                                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=32, src=()),                                          x155:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (53, 54)), contiguous=False),)), src=()),)),                                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=33, src=()),                                             x155,)),)),)),)),)),                                  UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                    UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=34, src=()),                                    UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-54, mask=((0, 1), (54, 58)), contiguous=False),)), src=()),)),)),                                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                  UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=35, src=()),                                  UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-58, mask=((0, 1), (58, 62)), contiguous=False),)), src=()),)),)),                              UOp(Ops.CAST, dtypes.float, arg=None, src=(                                UOp(Ops.ADD, dtypes.float, arg=None, src=(                                  UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                    UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=36, src=()),                                    x169:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (62, 63)), contiguous=False),)), src=()),)),                                  UOp(Ops.CAST, dtypes.float, arg=None, src=(                                    UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                      UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=37, src=()),                                       x169,)),)),)),)),)),                            UOp(Ops.LOAD, dtypes.float, arg=None, src=(                              UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=38, src=()),                              UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-63, mask=((0, 1), (63, 67)), contiguous=False),)), src=()),)),)),                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=39, src=()),                            UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-67, mask=((0, 1), (67, 71)), contiguous=False),)), src=()),)),)),                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                          UOp(Ops.ADD, dtypes.float, arg=None, src=(                            UOp(Ops.LOAD, dtypes.float, arg=None, src=(                              UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=40, src=()),                              x183:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (71, 72)), contiguous=False),)), src=()),)),                            UOp(Ops.CAST, dtypes.float, arg=None, src=(                              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                                UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=41, src=()),                                 x183,)),)),)),)),)),                      UOp(Ops.CAST, dtypes.float, arg=None, src=(                        UOp(Ops.ADD, dtypes.float, arg=None, src=(                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                            UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=42, src=()),                            x191:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (72, 73)), contiguous=False),)), src=()),)),                          UOp(Ops.CAST, dtypes.float, arg=None, src=(                            UOp(Ops.LOAD, dtypes.float, arg=None, src=(                              UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=43, src=()),                               x191,)),)),)),)),)),                    UOp(Ops.CAST, dtypes.float, arg=None, src=(                      UOp(Ops.ADD, dtypes.float, arg=None, src=(                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=44, src=()),                          x199:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (73, 74)), contiguous=False),)), src=()),)),                        UOp(Ops.CAST, dtypes.float, arg=None, src=(                          UOp(Ops.LOAD, dtypes.float, arg=None, src=(                            UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=45, src=()),                             x199,)),)),)),)),)),                  UOp(Ops.CAST, dtypes.float, arg=None, src=(                    UOp(Ops.ADD, dtypes.float, arg=None, src=(                      UOp(Ops.LOAD, dtypes.float, arg=None, src=(                        UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=46, src=()),                        x207:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (74, 75)), contiguous=False),)), src=()),)),                      UOp(Ops.CAST, dtypes.float, arg=None, src=(                        UOp(Ops.LOAD, dtypes.float, arg=None, src=(                          UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=47, src=()),                           x207,)),)),)),)),)),                UOp(Ops.CAST, dtypes.float, arg=None, src=(                  UOp(Ops.ADD, dtypes.float, arg=None, src=(                    UOp(Ops.LOAD, dtypes.float, arg=None, src=(                      UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=48, src=()),                      x215:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (75, 76)), contiguous=False),)), src=()),)),                    UOp(Ops.CAST, dtypes.float, arg=None, src=(                      UOp(Ops.LOAD, dtypes.float, arg=None, src=(                        UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=49, src=()),                         x215,)),)),)),)),)),              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=50, src=()),                UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-76, mask=((0, 1), (76, 80)), contiguous=False),)), src=()),)),)),            UOp(Ops.CAST, dtypes.float, arg=None, src=(              UOp(Ops.ADD, dtypes.float, arg=None, src=(                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                  UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=51, src=()),                  x226:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (80, 81)), contiguous=False),)), src=()),)),                UOp(Ops.CAST, dtypes.float, arg=None, src=(                  UOp(Ops.LOAD, dtypes.float, arg=None, src=(                    UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=52, src=()),                     x226,)),)),)),)),)),          UOp(Ops.CAST, dtypes.float, arg=None, src=(            UOp(Ops.ADD, dtypes.float, arg=None, src=(              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=53, src=()),                x234:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (81, 82)), contiguous=False),)), src=()),)),              UOp(Ops.CAST, dtypes.float, arg=None, src=(                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                  UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=54, src=()),                   x234,)),)),)),)),)),        UOp(Ops.CAST, dtypes.float, arg=None, src=(          UOp(Ops.ADD, dtypes.float, arg=None, src=(            UOp(Ops.ADD, dtypes.float, arg=None, src=(              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=55, src=()),                x243:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (82, 83)), contiguous=False),)), src=()),)),              UOp(Ops.CAST, dtypes.float, arg=None, src=(                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                  UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=56, src=()),                   x243,)),)),)),            UOp(Ops.ADD, dtypes.float, arg=None, src=(              UOp(Ops.LOAD, dtypes.float, arg=None, src=(                UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 1, 4)), arg=57, src=()),                x250:=UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 0), offset=0, mask=((0, 1), (83, 84)), contiguous=False),)), src=()),)),              UOp(Ops.CAST, dtypes.float, arg=None, src=(                UOp(Ops.LOAD, dtypes.float, arg=None, src=(                  UOp(Ops.DEFINE_GLOBAL, dtypes.float.ptr(), arg=58, src=()),                   x250,)),)),)),)),)),)),      UOp(Ops.CAST, dtypes.float, arg=None, src=(        UOp(Ops.LOAD, dtypes.float, arg=None, src=(          UOp(Ops.DEFINE_GLOBAL, dtypes.imageh((1, 128, 4)), arg=59, src=()),          UOp(Ops.VIEW, dtypes.void, arg=ShapeTracker(views=(View(shape=(1, 596), strides=(0, 1), offset=-84, mask=((0, 1), (84, 596)), contiguous=False),)), src=()),)),)),)),)),)) # noqa: E501

    opts = [Opt(op=OptOps.UPCAST, axis=0, amt=4)]
    helper_test_lin(Kernel(ast), opts, failed_platforms=[], validate_device=Device["GPU"])

if __name__ == '__main__':
  unittest.main()
