|
| 1 | +import numpy as np |
| 2 | +from numba.core import types, cgutils |
| 3 | +from numba.core.imputils import (lower_builtin) |
| 4 | +from numba.core.typing import signature |
| 5 | +from numba.np.arrayobj import make_array, _empty_nd_impl, array_copy |
| 6 | +from numba.core import itanium_mangler |
| 7 | +from llvmlite import ir |
| 8 | +import contextlib |
| 9 | + |
| 10 | +from numba import int32, int64, uint32, uint64, float32, float64 |
| 11 | + |
| 12 | + |
| 13 | +@contextlib.contextmanager |
| 14 | +def make_contiguous(context, builder, sig, args): |
| 15 | + """ |
| 16 | + Ensure that all array arguments are contiguous, if necessary by |
| 17 | + copying them. |
| 18 | + A new (sig, args) tuple is yielded. |
| 19 | + """ |
| 20 | + newtys = [] |
| 21 | + newargs = [] |
| 22 | + copies = [] |
| 23 | + for ty, val in zip(sig.args, args): |
| 24 | + if not isinstance(ty, types.Array) or ty.layout in 'CF': |
| 25 | + newty, newval = ty, val |
| 26 | + else: |
| 27 | + newty = ty.copy(layout='C') |
| 28 | + copysig = signature(newty, ty) |
| 29 | + newval = array_copy(context, builder, copysig, (val,)) |
| 30 | + copies.append((newty, newval)) |
| 31 | + newtys.append(newty) |
| 32 | + newargs.append(newval) |
| 33 | + yield signature(sig.return_type, *newtys), tuple(newargs) |
| 34 | + for ty, val in copies: |
| 35 | + context.nrt.decref(builder, ty, val) |
| 36 | + |
| 37 | +def check_c_int(context, builder, n): |
| 38 | + """ |
| 39 | + Check whether *n* fits in a C `int`. |
| 40 | + """ |
| 41 | + _maxint = 2**31 - 1 |
| 42 | + |
| 43 | + def impl(n): |
| 44 | + if n > _maxint: |
| 45 | + raise OverflowError("array size too large to fit in C int") |
| 46 | + |
| 47 | + context.compile_internal(builder, impl, |
| 48 | + signature(types.none, types.intp), (n,)) |
| 49 | + |
| 50 | + |
| 51 | +ll_char = ir.IntType(8) |
| 52 | +ll_char_p = ll_char.as_pointer() |
| 53 | +ll_void_p = ll_char_p |
| 54 | +ll_intc = ir.IntType(32) |
| 55 | +ll_intc_p = ll_intc.as_pointer() |
| 56 | +intp_t = cgutils.intp_t |
| 57 | +ll_intp_p = intp_t.as_pointer() |
| 58 | + |
| 59 | +def call_experimental_dot(context, builder, conjugate, dtype, |
| 60 | + n, a_data, b_data, out_data): |
| 61 | + |
| 62 | + fnty = ir.FunctionType(ir.IntType(32), |
| 63 | + [ll_void_p, ll_void_p, ll_void_p, ir.IntType(64)]) |
| 64 | + |
| 65 | + #fn = builder.module.get_or_insert_function(fnty, name="inumpy_dot") |
| 66 | + #name = itanium_mangler.mangle("inumpy_dot", [int64, dtype]) |
| 67 | + #print(name) |
| 68 | + fn = builder.module.get_or_insert_function(fnty, name="_Z10inumpy_dotIfEiPvS0_S0_m") |
| 69 | + |
| 70 | + res = builder.call(fn, (builder.bitcast(a_data, ll_void_p), |
| 71 | + builder.bitcast(b_data, ll_void_p), |
| 72 | + builder.bitcast(out_data, ll_void_p), |
| 73 | + n)) |
| 74 | + |
| 75 | +def dot_2_vv(context, builder, sig, args, conjugate=False): |
| 76 | + """ |
| 77 | + np.dot(vector, vector) |
| 78 | + np.vdot(vector, vector) |
| 79 | + """ |
| 80 | + import llvmlite.binding as ll |
| 81 | + ll.load_library_permanently('libinumpy.so') |
| 82 | + |
| 83 | + aty, bty = sig.args |
| 84 | + dtype = sig.return_type |
| 85 | + a = make_array(aty)(context, builder, args[0]) |
| 86 | + b = make_array(bty)(context, builder, args[1]) |
| 87 | + n, = cgutils.unpack_tuple(builder, a.shape) |
| 88 | + |
| 89 | + def check_args(a, b): |
| 90 | + m, = a.shape |
| 91 | + n, = b.shape |
| 92 | + if m != n: |
| 93 | + raise ValueError("incompatible array sizes for np.dot(a, b) " |
| 94 | + "(vector * vector)") |
| 95 | + |
| 96 | + context.compile_internal(builder, check_args, |
| 97 | + signature(types.none, *sig.args), args) |
| 98 | + check_c_int(context, builder, n) |
| 99 | + |
| 100 | + out = cgutils.alloca_once(builder, context.get_value_type(dtype)) |
| 101 | + call_experimental_dot(context, builder, conjugate, dtype, n, a.data, b.data, out) |
| 102 | + return builder.load(out) |
| 103 | + |
| 104 | + |
| 105 | +@lower_builtin(np.dot, types.Array, types.Array) |
| 106 | +def dot_dppl(context, builder, sig, args): |
| 107 | + """ |
| 108 | + np.dot(a, b) |
| 109 | + a @ b |
| 110 | + """ |
| 111 | + import dppl.ocldrv as driver |
| 112 | + device = driver.runtime.get_current_device() |
| 113 | + |
| 114 | + # the device env should come from the context but the current context |
| 115 | + # is a cpu context and not a dppl_gpu_context |
| 116 | + |
| 117 | + with make_contiguous(context, builder, sig, args) as (sig, args): |
| 118 | + ndims = [x.ndim for x in sig.args[:2]] |
| 119 | + if ndims == [2, 2]: |
| 120 | + print("gemm") |
| 121 | + #return dot_2_mm(context, builder, sig, args) |
| 122 | + elif ndims == [2, 1]: |
| 123 | + print("gemv") |
| 124 | + #return dot_2_mv(context, builder, sig, args) |
| 125 | + elif ndims == [1, 2]: |
| 126 | + print("gemv") |
| 127 | + #return dot_2_vm(context, builder, sig, args) |
| 128 | + elif ndims == [1, 1]: |
| 129 | + print("dot") |
| 130 | + return dot_2_vv(context, builder, sig, args) |
| 131 | + else: |
| 132 | + assert 0 |
| 133 | + |
| 134 | + |
| 135 | + raise ImportError("scipy 0.16+ is required for linear algebra") |
0 commit comments