SenseNova-U1.6-MoT-R9700-loom / programs /vqa /vision_encoder.loom
ilintar's picture
Publish SenseNova U1.6 MoT for the Radeon AI PRO R9700 with six verified Loom presets
f259ea5 verified
Raw History Blame Contribute Delete
36.6 kB
kernel.def export("v_resize_bicubic_h_hdr") @v_resize_bicubic_h_hdr(%out_w: index, %max_rows: index) {
%count = index.mul %out_w, %max_rows : index
%c1 = index.constant 1 : index
%c256 = index.constant 256 : index
%c255 = index.constant 255 : index
%n_up = index.add %count, %c255 : index
%groups = index.div %n_up, %c256 : index
kernel.launch.config workgroups(%groups, %c1, %c1) workgroup_size(%c256, %c1, %c1) : index
} launch(%out_n_arg: index, %other_arg: index, %src: buffer, %dst: buffer) {
%zero_h = index.constant 0 : offset
%hv = buffer.view %src[%zero_h] : buffer -> view<16xi32>
%in_w32 = view.load %hv[0] : view<16xi32> -> i32
%rows32 = view.load %hv[1] : view<16xi32> -> i32
%cin32 = view.load %hv[2] : view<16xi32> -> i32
%pix32 = view.load %hv[3] : view<16xi32> -> i32
%in_b = scalar.assume %in_w32 [range(%in_w32, 1, 16384)] : i32
%in_n = index.cast %in_b : i32 to index
%rows_b = scalar.assume %rows32 [range(%rows32, 1, 16384)] : i32
%rows_i = index.cast %rows_b : i32 to index
%cin_b = scalar.assume %cin32 [range(%cin32, 3, 4)] : i32
%cin_i = index.cast %cin_b : i32 to index
%pix_b = scalar.assume %pix32 [range(%pix32, 0, 2147483647)] : i32
%pix_i = index.cast %pix_b : i32 to index
%out_n = index.assume %out_n_arg [range(%out_n_arg, 1, 65536)] : index
%other = index.assume %other_arg [range(%other_arg, 1, 65536)] : index
%count = index.mul %rows_i, %out_n : index
%c256 = index.constant 256 : index
%gid = kernel.workgroup.id<x> : index
%lid = kernel.workitem.id<x> : index
%i = index.madd %gid, %c256, %lid : index
%live = index.cmp ult, %i, %count : index
scf.if %live {
%zero = index.constant 0 : offset
%c0 = index.constant 0 : index
%c1 = index.constant 1 : index
%c3 = index.constant 3 : index
%ccin = index.add %cin_i, %c0 : index
%zf = scalar.constant 0.0 : f32
%onef = scalar.constant 1.0 : f32
%halff = scalar.constant 0.5 : f32
%twof = scalar.constant 2.0 : f32
%threef = scalar.constant 3.0 : f32
%m3f = scalar.constant -3.0 : f32
%fourf = scalar.constant 4.0 : f32
%fivef = scalar.constant 5.0 : f32
%eightf = scalar.constant 8.0 : f32
%c1p5 = scalar.constant 1.5 : f32
%c2p5 = scalar.constant 2.5 : f32
%mhalf = scalar.constant -0.5 : f32
%pif = scalar.constant 3.141592653589793 : f32
%tinyf = scalar.constant 1.0e-12 : f32
%fxp = scalar.constant 4194304.0 : f32
%o = index.rem %i, %out_n : index
%row = index.div %i, %out_n : index
%in_i = index.cast %in_n : index to i32
%out_i = index.cast %out_n : index to i32
%in_f = scalar.sitofp %in_i : i32 to f32
%out_f = scalar.sitofp %out_i : i32 to f32
%scale = scalar.divf %in_f, %out_f : f32
%fs = scalar.maxnumf %scale, %onef : f32
%supc = scalar.constant 2.0 : f32
%support = scalar.mulf %supc, %fs : f32
%ss = scalar.divf %onef, %fs : f32
%oi = index.cast %o : index to i32
%of = scalar.sitofp %oi : i32 to f32
%oh = scalar.addf %of, %halff : f32
%center = scalar.mulf %oh, %scale : f32
%lo0 = scalar.subf %center, %support : f32
%lo1 = scalar.addf %lo0, %halff : f32
%lo2 = scalar.fptosi %lo1 : f32 to i32
%zi = scalar.constant 0 : i32
%lo3 = scalar.maxsi %lo2, %zi : i32
%hi0 = scalar.addf %center, %support : f32
%hi1 = scalar.addf %hi0, %halff : f32
%hi2 = scalar.fptosi %hi1 : f32 to i32
%hi3 = scalar.minsi %hi2, %in_i : i32
%lo4 = scalar.assume %lo3 [range(%lo3, 0, 65536)] : i32
%hi4 = scalar.assume %hi3 [range(%hi3, 0, 65536)] : i32
%xlo = index.cast %lo4 : i32 to index
%xhi = index.cast %hi4 : i32 to index
%ww = scf.for %xi = [%xlo to %xhi step %c1](%acc = %zf : f32) -> (f32) {
%xii = index.cast %xi : index to i32
%xf = scalar.sitofp %xii : i32 to f32
%d0 = scalar.subf %xf, %center : f32
%d1 = scalar.addf %d0, %halff : f32
%d = scalar.mulf %d1, %ss : f32
%wv_x = scalar.absf %d : f32
%wv_lt1 = scalar.cmpf olt, %wv_x, %onef : f32
%wv_lt2 = scalar.cmpf olt, %wv_x, %twof : f32
%wv_p1a = scalar.mulf %wv_x, %c1p5 : f32
%wv_p1b = scalar.subf %wv_p1a, %c2p5 : f32
%wv_p1c = scalar.mulf %wv_p1b, %wv_x : f32
%wv_p1d = scalar.mulf %wv_p1c, %wv_x : f32
%wv_p1 = scalar.addf %wv_p1d, %onef : f32
%wv_p2a = scalar.subf %wv_x, %fivef : f32
%wv_p2b = scalar.mulf %wv_p2a, %wv_x : f32
%wv_p2c = scalar.addf %wv_p2b, %eightf : f32
%wv_p2d = scalar.mulf %wv_p2c, %wv_x : f32
%wv_p2e = scalar.subf %wv_p2d, %fourf : f32
%wv_p2 = scalar.mulf %wv_p2e, %mhalf : f32
%wv_q = scf.select %wv_lt2, %wv_p2, %zf : f32
%wv = scf.select %wv_lt1, %wv_p1, %wv_q : f32
%na = scalar.addf %acc, %wv : f32
scf.yield %na : f32
}
%wnz = scalar.cmpf one, %ww, %zf : f32
%wdiv = scf.select %wnz, %ww, %onef : f32
%srcn = index.constant 268435456 : index
%sv = buffer.view %src[%zero] : buffer -> view<[%srcn]xi8>
%bias = scalar.constant 2097152 : i32
%s0, %s1, %s2 = scf.for %xi = [%xlo to %xhi step %c1](%a0 = %bias : i32, %a1 = %bias : i32, %a2 = %bias : i32) -> (i32, i32, i32) {
%xii = index.cast %xi : index to i32
%xf = scalar.sitofp %xii : i32 to f32
%d0 = scalar.subf %xf, %center : f32
%d1 = scalar.addf %d0, %halff : f32
%d = scalar.mulf %d1, %ss : f32
%wv_x = scalar.absf %d : f32
%wv_lt1 = scalar.cmpf olt, %wv_x, %onef : f32
%wv_lt2 = scalar.cmpf olt, %wv_x, %twof : f32
%wv_p1a = scalar.mulf %wv_x, %c1p5 : f32
%wv_p1b = scalar.subf %wv_p1a, %c2p5 : f32
%wv_p1c = scalar.mulf %wv_p1b, %wv_x : f32
%wv_p1d = scalar.mulf %wv_p1c, %wv_x : f32
%wv_p1 = scalar.addf %wv_p1d, %onef : f32
%wv_p2a = scalar.subf %wv_x, %fivef : f32
%wv_p2b = scalar.mulf %wv_p2a, %wv_x : f32
%wv_p2c = scalar.addf %wv_p2b, %eightf : f32
%wv_p2d = scalar.mulf %wv_p2c, %wv_x : f32
%wv_p2e = scalar.subf %wv_p2d, %fourf : f32
%wv_p2 = scalar.mulf %wv_p2e, %mhalf : f32
%wv_q = scf.select %wv_lt2, %wv_p2, %zf : f32
%wv = scf.select %wv_lt1, %wv_p1, %wv_q : f32
%wn = scalar.divf %wv, %wdiv : f32
%wfx = scalar.mulf %wn, %fxp : f32
%neg = scalar.cmpf olt, %wn, %zf : f32
%rnd = scf.select %neg, %mhalf, %halff : f32
%wr = scalar.addf %wfx, %rnd : f32
%wi = scalar.fptosi %wr : f32 to i32
%src_idx = index.madd %row, %in_n, %xi : index
%el_rel = index.mul %src_idx, %ccin : index
%el0 = index.add %el_rel, %pix_i : index
%el = index.assume %el0 [lt(%el0, %srcn)] : index
%el1r = index.add %el0, %c1 : index
%el1 = index.assume %el1r [lt(%el1r, %srcn)] : index
%el2r = index.add %el1r, %c1 : index
%el2 = index.assume %el2r [lt(%el2r, %srcn)] : index
%p0r = view.load %sv[%el] : view<[%srcn]xi8> -> i8
%p1r = view.load %sv[%el1] : view<[%srcn]xi8> -> i8
%p2r = view.load %sv[%el2] : view<[%srcn]xi8> -> i8
%p0 = scalar.extui %p0r : i8 to i32
%p1 = scalar.extui %p1r : i8 to i32
%p2 = scalar.extui %p2r : i8 to i32
%iv255 = scalar.constant 255 : i32
%c4_rgba = index.constant 4 : index
%rgba = index.cmp eq, %cin_i, %c4_rgba : index
%alpha = scf.if %rgba -> (i32) {
%ai0 = index.add %el2r, %c1 : index
%ai = index.assume %ai0 [lt(%ai0, %srcn)] : index
%av = view.load %sv[%ai] : view<[%srcn]xi8> -> i8
%av32 = scalar.extui %av : i8 to i32
scf.yield %av32 : i32
} else {
scf.yield %iv255 : i32
}
%remaining = scalar.subi %iv255, %alpha : i32
%white = scalar.muli %remaining, %iv255 : i32
%rounding = scalar.constant 127 : i32
%bias_alpha = scalar.addi %white, %rounding : i32
%mul0 = scalar.muli %p0, %alpha : i32
%mix0 = scalar.addi %mul0, %bias_alpha : i32
%rgb0 = scalar.divui %mix0, %iv255 : i32
%mul1 = scalar.muli %p1, %alpha : i32
%mix1 = scalar.addi %mul1, %bias_alpha : i32
%rgb1 = scalar.divui %mix1, %iv255 : i32
%mul2 = scalar.muli %p2, %alpha : i32
%mix2 = scalar.addi %mul2, %bias_alpha : i32
%rgb2 = scalar.divui %mix2, %iv255 : i32
%m0 = scalar.muli %rgb0, %wi : i32
%m1 = scalar.muli %rgb1, %wi : i32
%m2 = scalar.muli %rgb2, %wi : i32
%n0 = scalar.addi %a0, %m0 : i32
%n1 = scalar.addi %a1, %m1 : i32
%n2 = scalar.addi %a2, %m2 : i32
scf.yield %n0, %n1, %n2 : i32, i32, i32
}
%sh = scalar.constant 22 : i32
%c255i = scalar.constant 255 : i32
%dst_idx = index.madd %row, %out_n, %o : index
%dstn = index.constant 268435456 : index
%dv = buffer.view %dst[%zero] : buffer -> view<[%dstn]xi8>
%de0 = index.mul %dst_idx, %c3 : index
%c2x = index.constant 2 : index
%q0a = scalar.shrsi %s0, %sh : i32
%q0b = scalar.maxsi %q0a, %zi : i32
%q0c = scalar.minsi %q0b, %c255i : i32
%q0 = scalar.trunci %q0c : i32 to i8
%dc0r = index.add %de0, %c0 : index
%dc0 = index.assume %dc0r [lt(%dc0r, %dstn)] : index
view.store %q0, %dv[%dc0] : i8, view<[%dstn]xi8>
%q1a = scalar.shrsi %s1, %sh : i32
%q1b = scalar.maxsi %q1a, %zi : i32
%q1c = scalar.minsi %q1b, %c255i : i32
%q1 = scalar.trunci %q1c : i32 to i8
%dc1r = index.add %de0, %c1 : index
%dc1 = index.assume %dc1r [lt(%dc1r, %dstn)] : index
view.store %q1, %dv[%dc1] : i8, view<[%dstn]xi8>
%q2a = scalar.shrsi %s2, %sh : i32
%q2b = scalar.maxsi %q2a, %zi : i32
%q2c = scalar.minsi %q2b, %c255i : i32
%q2 = scalar.trunci %q2c : i32 to i8
%dc2r = index.add %de0, %c2x : index
%dc2 = index.assume %dc2r [lt(%dc2r, %dstn)] : index
view.store %q2, %dv[%dc2] : i8, view<[%dstn]xi8>
}
kernel.return
}
kernel.def export("v_resize_bicubic_v_hdr") @v_resize_bicubic_v_hdr(%out_h: index, %cols: index) {
%count = index.mul %out_h, %cols : index
%c1 = index.constant 1 : index
%c256 = index.constant 256 : index
%c255 = index.constant 255 : index
%n_up = index.add %count, %c255 : index
%groups = index.div %n_up, %c256 : index
kernel.launch.config workgroups(%groups, %c1, %c1) workgroup_size(%c256, %c1, %c1) : index
} launch(%out_n_arg: index, %other_arg: index, %hdr: buffer, %src: buffer, %dst: buffer) {
%zero_h = index.constant 0 : offset
%hv = buffer.view %hdr[%zero_h] : buffer -> view<16xi32>
%in_w32 = view.load %hv[1] : view<16xi32> -> i32
%rows32 = scalar.constant 1 : i32
%cin32 = scalar.constant 3 : i32
%pix32 = scalar.constant 0 : i32
%in_b = scalar.assume %in_w32 [range(%in_w32, 1, 16384)] : i32
%in_n = index.cast %in_b : i32 to index
%rows_b = scalar.assume %rows32 [range(%rows32, 1, 16384)] : i32
%rows_i = index.cast %rows_b : i32 to index
%cin_b = scalar.assume %cin32 [range(%cin32, 3, 4)] : i32
%cin_i = index.cast %cin_b : i32 to index
%pix_b = scalar.assume %pix32 [range(%pix32, 0, 2147483647)] : i32
%pix_i = index.cast %pix_b : i32 to index
%out_n = index.assume %out_n_arg [range(%out_n_arg, 1, 65536)] : index
%other = index.assume %other_arg [range(%other_arg, 1, 65536)] : index
%count = index.mul %other, %out_n : index
%c256 = index.constant 256 : index
%gid = kernel.workgroup.id<x> : index
%lid = kernel.workitem.id<x> : index
%i = index.madd %gid, %c256, %lid : index
%live = index.cmp ult, %i, %count : index
scf.if %live {
%zero = index.constant 0 : offset
%c0 = index.constant 0 : index
%c1 = index.constant 1 : index
%c3 = index.constant 3 : index
%ccin = index.constant 3 : index
%zf = scalar.constant 0.0 : f32
%onef = scalar.constant 1.0 : f32
%halff = scalar.constant 0.5 : f32
%twof = scalar.constant 2.0 : f32
%threef = scalar.constant 3.0 : f32
%m3f = scalar.constant -3.0 : f32
%fourf = scalar.constant 4.0 : f32
%fivef = scalar.constant 5.0 : f32
%eightf = scalar.constant 8.0 : f32
%c1p5 = scalar.constant 1.5 : f32
%c2p5 = scalar.constant 2.5 : f32
%mhalf = scalar.constant -0.5 : f32
%pif = scalar.constant 3.141592653589793 : f32
%tinyf = scalar.constant 1.0e-12 : f32
%fxp = scalar.constant 4194304.0 : f32
%o_v = index.div %i, %other : index
%col = index.rem %i, %other : index
%in_i = index.cast %in_n : index to i32
%out_i = index.cast %out_n : index to i32
%in_f = scalar.sitofp %in_i : i32 to f32
%out_f = scalar.sitofp %out_i : i32 to f32
%scale = scalar.divf %in_f, %out_f : f32
%fs = scalar.maxnumf %scale, %onef : f32
%supc = scalar.constant 2.0 : f32
%support = scalar.mulf %supc, %fs : f32
%ss = scalar.divf %onef, %fs : f32
%oi = index.cast %o_v : index to i32
%of = scalar.sitofp %oi : i32 to f32
%oh = scalar.addf %of, %halff : f32
%center = scalar.mulf %oh, %scale : f32
%lo0 = scalar.subf %center, %support : f32
%lo1 = scalar.addf %lo0, %halff : f32
%lo2 = scalar.fptosi %lo1 : f32 to i32
%zi = scalar.constant 0 : i32
%lo3 = scalar.maxsi %lo2, %zi : i32
%hi0 = scalar.addf %center, %support : f32
%hi1 = scalar.addf %hi0, %halff : f32
%hi2 = scalar.fptosi %hi1 : f32 to i32
%hi3 = scalar.minsi %hi2, %in_i : i32
%lo4 = scalar.assume %lo3 [range(%lo3, 0, 65536)] : i32
%hi4 = scalar.assume %hi3 [range(%hi3, 0, 65536)] : i32
%xlo = index.cast %lo4 : i32 to index
%xhi = index.cast %hi4 : i32 to index
%ww = scf.for %xi = [%xlo to %xhi step %c1](%acc = %zf : f32) -> (f32) {
%xii = index.cast %xi : index to i32
%xf = scalar.sitofp %xii : i32 to f32
%d0 = scalar.subf %xf, %center : f32
%d1 = scalar.addf %d0, %halff : f32
%d = scalar.mulf %d1, %ss : f32
%wv_x = scalar.absf %d : f32
%wv_lt1 = scalar.cmpf olt, %wv_x, %onef : f32
%wv_lt2 = scalar.cmpf olt, %wv_x, %twof : f32
%wv_p1a = scalar.mulf %wv_x, %c1p5 : f32
%wv_p1b = scalar.subf %wv_p1a, %c2p5 : f32
%wv_p1c = scalar.mulf %wv_p1b, %wv_x : f32
%wv_p1d = scalar.mulf %wv_p1c, %wv_x : f32
%wv_p1 = scalar.addf %wv_p1d, %onef : f32
%wv_p2a = scalar.subf %wv_x, %fivef : f32
%wv_p2b = scalar.mulf %wv_p2a, %wv_x : f32
%wv_p2c = scalar.addf %wv_p2b, %eightf : f32
%wv_p2d = scalar.mulf %wv_p2c, %wv_x : f32
%wv_p2e = scalar.subf %wv_p2d, %fourf : f32
%wv_p2 = scalar.mulf %wv_p2e, %mhalf : f32
%wv_q = scf.select %wv_lt2, %wv_p2, %zf : f32
%wv = scf.select %wv_lt1, %wv_p1, %wv_q : f32
%na = scalar.addf %acc, %wv : f32
scf.yield %na : f32
}
%wnz = scalar.cmpf one, %ww, %zf : f32
%wdiv = scf.select %wnz, %ww, %onef : f32
%srcn = index.constant 268435456 : index
%sv = buffer.view %src[%zero] : buffer -> view<[%srcn]xi8>
%bias = scalar.constant 2097152 : i32
%s0, %s1, %s2 = scf.for %xi = [%xlo to %xhi step %c1](%a0 = %bias : i32, %a1 = %bias : i32, %a2 = %bias : i32) -> (i32, i32, i32) {
%xii = index.cast %xi : index to i32
%xf = scalar.sitofp %xii : i32 to f32
%d0 = scalar.subf %xf, %center : f32
%d1 = scalar.addf %d0, %halff : f32
%d = scalar.mulf %d1, %ss : f32
%wv_x = scalar.absf %d : f32
%wv_lt1 = scalar.cmpf olt, %wv_x, %onef : f32
%wv_lt2 = scalar.cmpf olt, %wv_x, %twof : f32
%wv_p1a = scalar.mulf %wv_x, %c1p5 : f32
%wv_p1b = scalar.subf %wv_p1a, %c2p5 : f32
%wv_p1c = scalar.mulf %wv_p1b, %wv_x : f32
%wv_p1d = scalar.mulf %wv_p1c, %wv_x : f32
%wv_p1 = scalar.addf %wv_p1d, %onef : f32
%wv_p2a = scalar.subf %wv_x, %fivef : f32
%wv_p2b = scalar.mulf %wv_p2a, %wv_x : f32
%wv_p2c = scalar.addf %wv_p2b, %eightf : f32
%wv_p2d = scalar.mulf %wv_p2c, %wv_x : f32
%wv_p2e = scalar.subf %wv_p2d, %fourf : f32
%wv_p2 = scalar.mulf %wv_p2e, %mhalf : f32
%wv_q = scf.select %wv_lt2, %wv_p2, %zf : f32
%wv = scf.select %wv_lt1, %wv_p1, %wv_q : f32
%wn = scalar.divf %wv, %wdiv : f32
%wfx = scalar.mulf %wn, %fxp : f32
%neg = scalar.cmpf olt, %wn, %zf : f32
%rnd = scf.select %neg, %mhalf, %halff : f32
%wr = scalar.addf %wfx, %rnd : f32
%wi = scalar.fptosi %wr : f32 to i32
%src_idx = index.madd %xi, %other, %col : index
%el0 = index.mul %src_idx, %ccin : index
%el = index.assume %el0 [lt(%el0, %srcn)] : index
%el1r = index.add %el0, %c1 : index
%el1 = index.assume %el1r [lt(%el1r, %srcn)] : index
%el2r = index.add %el1r, %c1 : index
%el2 = index.assume %el2r [lt(%el2r, %srcn)] : index
%p0r = view.load %sv[%el] : view<[%srcn]xi8> -> i8
%p1r = view.load %sv[%el1] : view<[%srcn]xi8> -> i8
%p2r = view.load %sv[%el2] : view<[%srcn]xi8> -> i8
%p0 = scalar.extui %p0r : i8 to i32
%p1 = scalar.extui %p1r : i8 to i32
%p2 = scalar.extui %p2r : i8 to i32
%m0 = scalar.muli %p0, %wi : i32
%m1 = scalar.muli %p1, %wi : i32
%m2 = scalar.muli %p2, %wi : i32
%n0 = scalar.addi %a0, %m0 : i32
%n1 = scalar.addi %a1, %m1 : i32
%n2 = scalar.addi %a2, %m2 : i32
scf.yield %n0, %n1, %n2 : i32, i32, i32
}
%sh = scalar.constant 22 : i32
%c255i = scalar.constant 255 : i32
%dst_idx = index.madd %o_v, %other, %col : index
%dstn = index.constant 268435456 : index
%dv = buffer.view %dst[%zero] : buffer -> view<[%dstn]xi8>
%de0 = index.mul %dst_idx, %c3 : index
%c2x = index.constant 2 : index
%q0a = scalar.shrsi %s0, %sh : i32
%q0b = scalar.maxsi %q0a, %zi : i32
%q0c = scalar.minsi %q0b, %c255i : i32
%q0 = scalar.trunci %q0c : i32 to i8
%dc0r = index.add %de0, %c0 : index
%dc0 = index.assume %dc0r [lt(%dc0r, %dstn)] : index
view.store %q0, %dv[%dc0] : i8, view<[%dstn]xi8>
%q1a = scalar.shrsi %s1, %sh : i32
%q1b = scalar.maxsi %q1a, %zi : i32
%q1c = scalar.minsi %q1b, %c255i : i32
%q1 = scalar.trunci %q1c : i32 to i8
%dc1r = index.add %de0, %c1 : index
%dc1 = index.assume %dc1r [lt(%dc1r, %dstn)] : index
view.store %q1, %dv[%dc1] : i8, view<[%dstn]xi8>
%q2a = scalar.shrsi %s2, %sh : i32
%q2b = scalar.maxsi %q2a, %zi : i32
%q2c = scalar.minsi %q2b, %c255i : i32
%q2 = scalar.trunci %q2c : i32 to i8
%dc2r = index.add %de0, %c2x : index
%dc2 = index.assume %dc2r [lt(%dc2r, %dstn)] : index
view.store %q2, %dv[%dc2] : i8, view<[%dstn]xi8>
}
kernel.return
}
kernel.def export("v_pixels_0p485_0p456_0p406") @v_pixels_0p485_0p456_0p406(%n: index) {
%c1 = index.constant 1 : index
%c256 = index.constant 256 : index
%c255 = index.constant 255 : index
%n_up = index.add %n, %c255 : index
%groups = index.div %n_up, %c256 : index
kernel.launch.config workgroups(%groups, %c1, %c1) workgroup_size(%c256, %c1, %c1) : index
} launch(%n_arg: index, %src: buffer, %y: buffer) {
%n = index.assume %n_arg [range(%n_arg, 1, 268435456)] : index
%c256 = index.constant 256 : index
%c3 = index.constant 3 : index
%gid = kernel.workgroup.id<x> : index
%lid = kernel.workitem.id<x> : index
%i = index.madd %gid, %c256, %lid : index
%live = index.cmp ult, %i, %n : index
scf.if %live {
%bi = index.assume %i [lt(%i, %n)] : index
%zero = index.constant 0 : offset
%sv = buffer.view %src[%zero] : buffer -> view<[%n]xi8>
%raw = view.load %sv[%bi] : view<[%n]xi8> -> i8
%u = scalar.extui %raw : i8 to i32
%f = scalar.uitofp %u : i32 to f32
%ch = index.rem %bi, %c3 : index
%c0 = index.constant 0 : index
%c1 = index.constant 1 : index
%is0 = index.cmp eq, %ch, %c0 : index
%is1 = index.cmp eq, %ch, %c1 : index
%a0 = scalar.constant 0.017124753831663668 : f32
%a1 = scalar.constant 0.01750700280112045 : f32
%a2 = scalar.constant 0.017429193899782133 : f32
%b0 = scalar.constant -2.1179039301310043 : f32
%b1 = scalar.constant -2.0357142857142856 : f32
%b2 = scalar.constant -1.8044444444444445 : f32
%a12 = scf.select %is1, %a1, %a2 : f32
%a = scf.select %is0, %a0, %a12 : f32
%b12 = scf.select %is1, %b1, %b2 : f32
%b = scf.select %is0, %b0, %b12 : f32
%r_mul = scalar.mulf %f, %a : f32
%r = scalar.addf %r_mul, %b : f32
%yv = buffer.view %y[%zero] : buffer -> view<[%n]xf32>
view.store %r, %yv[%bi] : f32, view<[%n]xf32>
}
kernel.return
}
kernel.def export("neo_vision_pack") @neo_vision_pack(%rows: index) {
%one = index.constant 1 : index
%block = index.constant 256 : index
kernel.launch.config workgroups(%rows, %one, %one) workgroup_size(%block, %one, %one) : index
} launch(%rows_arg: index, %cols_arg: index, %pixels: buffer, %patches: buffer) {
%rows = index.assume %rows_arg [range(%rows_arg, 1, 65536)] : index
%cols = index.assume %cols_arg [range(%cols_arg, 2, 4096)] : index
%gid = kernel.workgroup.id<x> : index
%row = index.assume %gid [lt(%gid, %rows)] : index
%lid = kernel.workitem.id<x> : index
%lane = index.assume %lid [range(%lid, 0, 255)] : index
%zero = index.constant 0 : offset
%c0 = index.constant 0 : index
%c1 = index.constant 1 : index
%c3 = index.constant 3 : index
%p = index.constant 16 : index
%area = index.constant 256 : index
%patch = index.constant 768 : index
%width = index.mul %cols, %p : index
%count = index.mul %rows, %patch : index
%src = buffer.view %pixels[%zero] : buffer -> view<[%count]xf32>
%dst = buffer.view %patches[%zero] : buffer -> view<[%rows]x768xf32>
%y0 = index.div %row, %cols : index
%x0 = index.rem %row, %cols : index
%dy = index.div %lane, %p : index
%dx = index.rem %lane, %p : index
%y = index.madd %y0, %p, %dy : index
%x = index.madd %x0, %p, %dx : index
%pixel = index.madd %y, %width, %x : index
scf.for %c = [%c0 to %c3 step %c1] {
%in_idx = index.madd %pixel, %c3, %c : index
%out_idx = index.madd %c, %area, %lane : index
%i = index.assume %in_idx [lt(%in_idx, %count)] : index
%j = index.assume %out_idx [range(%out_idx, 0, 767)] : index
%v = view.load %src[%i] : view<[%count]xf32> -> f32
view.store %v, %dst[%row, %j] : f32, view<[%rows]x768xf32>
}
kernel.return
}
kernel.def export("neo_vision_patch") @neo_vision_patch(%rows: index) {
%tiles = index.constant 16 : index
%one = index.constant 1 : index
%block = index.constant 64 : index
kernel.launch.config workgroups(%tiles, %rows, %one) workgroup_size(%block, %one, %one) : index
} launch(%rows_arg: index, %x: buffer, %w: buffer, %b: buffer, %y: buffer) {
%rows = index.assume %rows_arg [range(%rows_arg, 1, 65536)] : index
%raw_row = kernel.workgroup.id<y> : index
%row = index.assume %raw_row [lt(%raw_row, %rows)] : index
%tile = kernel.workgroup.id<x> : index
%lane = kernel.workitem.id<x> : index
%block = index.constant 64 : index
%raw_col = index.madd %tile, %block, %lane : index
%col = index.assume %raw_col [range(%raw_col, 0, 1023)] : index
%off = index.constant 0 : offset
%start = index.constant 0 : index
%end = index.constant 768 : index
%step = index.constant 8 : index
%zero_f = scalar.constant 0.0 : f32
%xv = buffer.view %x[%off] : buffer -> view<[%rows]x768xf32>
%wv = buffer.view %w[%off] : buffer -> view<1024x768xbf16>
%bv = buffer.view %b[%off] : buffer -> view<1024xbf16>
%yv = buffer.view %y[%off] : buffer -> view<[%rows]x1024xf32>
%dot = scf.for %k = [%start to %end step %step](%acc = %zero_f : f32) -> (f32) {
%at = index.assume %k [range(%k, 0, 760), multiple_of(%k, 8)] : index
%pixels = vector.load %xv[%row, %at] : view<[%rows]x768xf32> -> vector<8xf32>
%quant = vector.fptrunc %pixels : vector<8xf32> to vector<8xbf16>
%values = vector.extf %quant : vector<8xbf16> to vector<8xf32>
%packed_w = vector.load %wv[%col, %at] : view<1024x768xbf16> -> vector<8xbf16>
%weights = vector.extf %packed_w : vector<8xbf16> to vector<8xf32>
%products = vector.mulf %weights, %values : vector<8xf32>
%next = vector.reduce<addf> %products, %acc : vector<8xf32>, f32
scf.yield %next : f32
}
%bias_bf = view.load %bv[%col] : view<1024xbf16> -> bf16
%bias = scalar.extf %bias_bf : bf16 to f32
%biased = scalar.addf %dot, %bias : f32
%narrow = scalar.fptrunc %biased : f32 to bf16
%a = scalar.extf %narrow : bf16 to f32
%one = scalar.constant 1.0 : f32
%half = scalar.constant 0.5 : f32
%rs2 = scalar.constant 0.7071067811865476 : f32
%u = scalar.mulf %a, %rs2 : f32
%au = scalar.absf %u : f32
%hz = scalar.mulf %au, %half : f32
%tden = scalar.addf %hz, %one : f32
%t = scalar.divf %one, %tden : f32
%p9 = scalar.constant 0.17087277 : f32
%p8 = scalar.constant -0.82215223 : f32
%p7 = scalar.constant 1.48851587 : f32
%p6 = scalar.constant -1.13520398 : f32
%p5 = scalar.constant 0.27886807 : f32
%p4 = scalar.constant -0.18628806 : f32
%p3 = scalar.constant 0.09678418 : f32
%p2 = scalar.constant 0.37409196 : f32
%p1 = scalar.constant 1.00002368 : f32
%p0 = scalar.constant -1.26551223 : f32
%q8_mul = scalar.mulf %t, %p9 : f32
%q8 = scalar.addf %q8_mul, %p8 : f32
%q7_mul = scalar.mulf %t, %q8 : f32
%q7 = scalar.addf %q7_mul, %p7 : f32
%q6_mul = scalar.mulf %t, %q7 : f32
%q6 = scalar.addf %q6_mul, %p6 : f32
%q5_mul = scalar.mulf %t, %q6 : f32
%q5 = scalar.addf %q5_mul, %p5 : f32
%q4_mul = scalar.mulf %t, %q5 : f32
%q4 = scalar.addf %q4_mul, %p4 : f32
%q3_mul = scalar.mulf %t, %q4 : f32
%q3 = scalar.addf %q3_mul, %p3 : f32
%q2_mul = scalar.mulf %t, %q3 : f32
%q2 = scalar.addf %q2_mul, %p2 : f32
%q1_mul = scalar.mulf %t, %q2 : f32
%q1 = scalar.addf %q1_mul, %p1 : f32
%q0_mul = scalar.mulf %t, %q1 : f32
%q0 = scalar.addf %q0_mul, %p0 : f32
%u2 = scalar.mulf %au, %au : f32
%e0 = scalar.subf %q0, %u2 : f32
%e1 = scalar.expf<afn> %e0 : f32
%erfc = scalar.mulf %t, %e1 : f32
%erfa = scalar.subf %one, %erfc : f32
%zf_e = scalar.constant 0.0 : f32
%neg = scalar.cmpf olt, %u, %zf_e : f32
%erfn = scalar.negf %erfa : f32
%erf = scf.select %neg, %erfn, %erfa : f32
%onep = scalar.addf %erf, %one : f32
%hx = scalar.mulf %a, %half : f32
%r = scalar.mulf %hx, %onep : f32
%bf = scalar.fptrunc %r : f32 to bf16
%result = scalar.extf %bf : bf16 to f32
view.store %result, %yv[%row, %col] : f32, view<[%rows]x1024xf32>
kernel.return
}
kernel.def export("neo_vision_rope") @neo_vision_rope(%tokens: index) {
%c1 = index.constant 1 : index
%c256 = index.constant 256 : index
kernel.launch.config workgroups(%tokens, %c1, %c1) workgroup_size(%c256, %c1, %c1) : index
} launch(%tokens_arg: index, %gw_arg: index, %x: buffer) {
%tokens = index.assume %tokens_arg [range(%tokens_arg, 1, 65536)] : index
%gw = index.assume %gw_arg [range(%gw_arg, 1, 4096)] : index
%t_id = kernel.workgroup.id<x> : index
%t = index.assume %t_id [lt(%t_id, %tokens)] : index
%lid = kernel.workitem.id<x> : index
%lane = index.assume %lid [range(%lid, 0, 255)] : index
%zero = index.constant 0 : offset
%c0 = index.constant 0 : index
%c1 = index.constant 1 : index
%c256 = index.constant 256 : index
%cNP = index.constant 512 : index
%cWORK = index.constant 512 : index
%cD = index.constant 1024 : index
%cO = index.constant 0 : index
%py = index.div %t, %gw : index
%px = index.rem %t, %gw : index
%pxi = index.cast %px : index to i32
%pyi = index.cast %py : index to i32
%offi = scalar.constant 0 : i32
%pxo = scalar.addi %pxi, %offi : i32
%pyo = scalar.addi %pyi, %offi : i32
%pxf = scalar.sitofp %pxo : i32 to f32
%pyf = scalar.sitofp %pyo : i32 to f32
%two_pi = scalar.constant 6.283185307179586 : f32
%inv_two_pi = scalar.constant 0.15915494309189535 : f32
%xv = buffer.view %x[%zero] : buffer -> view<[%tokens]x1024xf32>
%cq = index.constant 256 : index
%chalf = index.constant 512 : index
%lnth = scalar.constant 18.420680743952367 : f32
%halff = scalar.constant 512.0 : f32
scf.for %w = [%lane to %cWORK step %c256] {
%h = index.div %w, %cNP : index
%j0 = index.rem %w, %cNP : index
%j = index.assume %j0 [range(%j0, 0, 511)] : index
%isy = index.cmp uge, %j, %cq : index
%jj = index.rem %j, %cq : index
%c0p = index.constant 0 : index
%c2p = index.constant 2 : index
%base = scf.select %isy, %chalf, %c0p : index
%lo_i = index.madd %jj, %c2p, %base : index
%hi_i = index.add %lo_i, %c1 : index
%ii = index.cast %jj : index to i32
%if0 = scalar.sitofp %ii : i32 to f32
%if1 = scalar.mulf %if0, %lnth : f32
%if2 = scalar.divf %if1, %halff : f32
%if3 = scalar.negf %if2 : f32
%invf = scalar.expf<afn> %if3 : f32
%pos = scf.select %isy, %pyf, %pxf : f32
%ang0 = scalar.mulf %pos, %invf : f32
%tr = scalar.mulf %ang0, %inv_two_pi : f32
%trf = scalar.floorf %tr : f32
%wrap = scalar.mulf %trf, %two_pi : f32
%ang = scalar.subf %ang0, %wrap : f32
%cs = scalar.cosf<afn> %ang : f32
%sn = scalar.sinf<afn> %ang : f32
%hcol = index.madd %h, %cD, %cO : index
%clo0 = index.add %hcol, %lo_i : index
%chi0 = index.add %hcol, %hi_i : index
%clo = index.assume %clo0 [lt(%clo0, 1024)] : index
%chi = index.assume %chi0 [lt(%chi0, 1024)] : index
%a = view.load %xv[%t, %clo] : view<[%tokens]x1024xf32> -> f32
%b = view.load %xv[%t, %chi] : view<[%tokens]x1024xf32> -> f32
%acs = scalar.mulf %a, %cs : f32
%bsn = scalar.mulf %b, %sn : f32
%ra = scalar.subf %acs, %bsn : f32
%asn = scalar.mulf %a, %sn : f32
%rb_mul = scalar.mulf %b, %cs : f32
%rb = scalar.addf %rb_mul, %asn : f32
view.store %ra, %xv[%t, %clo] : f32, view<[%tokens]x1024xf32>
view.store %rb, %xv[%t, %chi] : f32, view<[%tokens]x1024xf32>
}
kernel.return
}
kernel.def export("v_shuffle2_1024") @v_shuffle2_1024(%out_tokens: index) {
%c1 = index.constant 1 : index
%c256 = index.constant 256 : index
kernel.launch.config workgroups(%out_tokens, %c1, %c1) workgroup_size(%c256, %c1, %c1) : index
} launch(%out_tokens_arg: index, %gw_arg: index, %tokens_arg: index, %x: buffer, %y: buffer) {
%out_tokens = index.assume %out_tokens_arg [range(%out_tokens_arg, 1, 65536)] : index
%gw = index.assume %gw_arg [range(%gw_arg, 2, 4096)] : index
%tokens = index.assume %tokens_arg [range(%tokens_arg, 1, 65536)] : index
%o_id = kernel.workgroup.id<x> : index
%o = index.assume %o_id [lt(%o_id, %out_tokens)] : index
%lid = kernel.workitem.id<x> : index
%lane = index.assume %lid [range(%lid, 0, 255)] : index
%zero = index.constant 0 : offset
%c256 = index.constant 256 : index
%cm = index.constant 2 : index
%cmm = index.constant 4 : index
%cEO = index.constant 4096 : index
%ow0 = index.div %gw, %cm : index
%ow = index.assume %ow0 [range(%ow0, 1, 4096)] : index
%oy = index.div %o, %ow : index
%ox = index.rem %o, %ow : index
%xv = buffer.view %x[%zero] : buffer -> view<[%tokens]x1024xf32>
%yv = buffer.view %y[%zero] : buffer -> view<[%out_tokens]x4096xf32>
scf.for %f = [%lane to %cEO step %c256] {
%bf = index.assume %f [lt(%f, %cEO)] : index
%c = index.div %bf, %cmm : index
%bc = index.assume %c [lt(%c, 1024)] : index
%s = index.rem %bf, %cmm : index
%ry = index.div %s, %cm : index
%rx = index.rem %s, %cm : index
%yy = index.madd %oy, %cm, %ry : index
%xx = index.madd %ox, %cm, %rx : index
%src = index.madd %yy, %gw, %xx : index
%bs = index.assume %src [lt(%src, %tokens)] : index
%val = view.load %xv[%bs, %bc] : view<[%tokens]x1024xf32> -> f32
view.store %val, %yv[%o, %bf] : f32, view<[%out_tokens]x4096xf32>
}
kernel.return
}
kernel.def export("neo_vision_project") @neo_vision_project(%rows: index) {
%tiles = index.constant 64 : index
%one = index.constant 1 : index
%block = index.constant 64 : index
kernel.launch.config workgroups(%tiles, %rows, %one) workgroup_size(%block, %one, %one) : index
} launch(%rows_arg: index, %x: buffer, %w: buffer, %b: buffer, %y: buffer) {
%rows = index.assume %rows_arg [range(%rows_arg, 1, 65536)] : index
%raw_row = kernel.workgroup.id<y> : index
%row = index.assume %raw_row [lt(%raw_row, %rows)] : index
%tile = kernel.workgroup.id<x> : index
%lane = kernel.workitem.id<x> : index
%block = index.constant 64 : index
%raw_col = index.madd %tile, %block, %lane : index
%col = index.assume %raw_col [range(%raw_col, 0, 4095)] : index
%off = index.constant 0 : offset
%start = index.constant 0 : index
%end = index.constant 4096 : index
%step = index.constant 8 : index
%zero_f = scalar.constant 0.0 : f32
%xv = buffer.view %x[%off] : buffer -> view<[%rows]x4096xf32>
%wv = buffer.view %w[%off] : buffer -> view<4096x4096xbf16>
%bv = buffer.view %b[%off] : buffer -> view<4096xbf16>
%yv = buffer.view %y[%off] : buffer -> view<[%rows]x4096xf32>
%dot = scf.for %k = [%start to %end step %step](%acc = %zero_f : f32) -> (f32) {
%at = index.assume %k [range(%k, 0, 4088), multiple_of(%k, 8)] : index
%pixels = vector.load %xv[%row, %at] : view<[%rows]x4096xf32> -> vector<8xf32>
%quant = vector.fptrunc %pixels : vector<8xf32> to vector<8xbf16>
%values = vector.extf %quant : vector<8xbf16> to vector<8xf32>
%packed_w = vector.load %wv[%col, %at] : view<4096x4096xbf16> -> vector<8xbf16>
%weights = vector.extf %packed_w : vector<8xbf16> to vector<8xf32>
%products = vector.mulf %weights, %values : vector<8xf32>
%next = vector.reduce<addf> %products, %acc : vector<8xf32>, f32
scf.yield %next : f32
}
%bias_bf = view.load %bv[%col] : view<4096xbf16> -> bf16
%bias = scalar.extf %bias_bf : bf16 to f32
%biased = scalar.addf %dot, %bias : f32
%narrow = scalar.fptrunc %biased : f32 to bf16
%a = scalar.extf %narrow : bf16 to f32
%r = scalar.addf %a, %zero_f : f32
%bf = scalar.fptrunc %r : f32 to bf16
%result = scalar.extf %bf : bf16 to f32
view.store %result, %yv[%row, %col] : f32, view<[%rows]x4096xf32>
kernel.return
}
command.def public @vision_encode() launch(%weights: buffer, %input: buffer, %output: buffer) {
%height = index.constant 256 : index
%width = index.constant 256 : index
%max_height = index.constant 4096 : index
%n = index.constant 196608 : index
%wide_size = index.constant 3145728 : offset
%wide = buffer.alloca<global> align(256) %wide_size : buffer
%u8_size = index.constant 196608 : offset
%u8 = buffer.alloca<global> align(256) %u8_size : buffer
%rgb_size = index.constant 786432 : offset
%rgb = buffer.alloca<global> align(256) %rgb_size : buffer
%patches_size = index.constant 786432 : offset
%patches = buffer.alloca<global> align(256) %patches_size : buffer
%hidden_size = index.constant 1048576 : offset
%hidden = buffer.alloca<global> align(256) %hidden_size : buffer
%merged_size = index.constant 1048576 : offset
%merged = buffer.alloca<global> align(256) %merged_size : buffer
%patch_embedding_weight = command.parameter %weights, "vision_model.embeddings.patch_embedding.weight" : view<1572864xi8>
%patch_embedding_bias = command.parameter %weights, "vision_model.embeddings.patch_embedding.bias" : view<2048xi8>
%dense_embedding_weight = command.parameter %weights, "vision_model.embeddings.dense_embedding.weight" : view<33554432xi8>
%dense_embedding_bias = command.parameter %weights, "vision_model.embeddings.dense_embedding.bias" : view<8192xi8>
kernel.launch @v_resize_bicubic_h_hdr[%width, %max_height](%width, %max_height, %input, %wide) : [index, index](index, index, buffer, buffer)
kernel.launch @v_resize_bicubic_v_hdr[%height, %width](%height, %width, %input, %wide, %u8) : [index, index](index, index, buffer, buffer, buffer)
kernel.launch @v_pixels_0p485_0p456_0p406[%n](%n, %u8, %rgb) : [index](index, buffer, buffer)
%ve_rows = index.constant 256 : index
%ve_cols = index.constant 16 : index
%ve_count = index.constant 64 : index
kernel.launch @neo_vision_pack[%ve_rows](%ve_rows, %ve_cols, %rgb, %patches) : [index](index, index, buffer, buffer)
kernel.launch @neo_vision_patch[%ve_rows](%ve_rows, %patches, %patch_embedding_weight, %patch_embedding_bias, %hidden) : [index](index, buffer, view<1572864xi8>, view<2048xi8>, buffer)
kernel.launch @neo_vision_rope[%ve_rows](%ve_rows, %ve_cols, %hidden) : [index](index, index, buffer)
kernel.launch @v_shuffle2_1024[%ve_count](%ve_count, %ve_cols, %ve_rows, %hidden, %merged) : [index](index, index, index, buffer, buffer)
kernel.launch @neo_vision_project[%ve_count](%ve_count, %merged, %dense_embedding_weight, %dense_embedding_bias, %output) : [index](index, buffer, view<33554432xi8>, view<8192xi8>, buffer)
command.return
}