def feature_rep_big(grid): """ For each cell: - The number of eligible channels For each cell-channel pair: - The number of times the ch is used by neighbors with dist 4 or less - The number of times the ch is used by neighbors with dist 3 or less - If the ch is eligible or not """ assert grid.ndim == 3 frep = np.zeros((intp(rows), intp(cols), n_channels * 3 + 1), dtype=int32) for r in range(rows): for c in range(cols): neighs3 = neighbors_np(3, r, c, countself) neighs4 = neighbors_np(4, r, c, countself) n_used3 = np.zeros(n_channels, dtype=int32) n_used4 = np.zeros(n_channels, dtype=int32) for i in range(len(neighs3)): n_used3 += grid[neighs3[i, 0], neighs3[i, 1]] for i in range(len(neighs4)): n_used4 += grid[neighs4[i, 0], neighs4[i, 1]] frep[r, c, :n_channels] = n_used3 frep[r, c, n_channels:n_channels * 2] = n_used4 elig = _eligible_map(grid, r, c) frep[r, c, n_channels * 2:n_channels * 3] = elig frep[r, c, -1] = np.sum(elig) return frep
def feature_reps_big2(grids): assert grids.ndim == 4 freps = np.zeros( (len(grids), intp(rows), intp(cols), n_channels * 5 + 1), dtype=int32) for i in range(len(grids)): freps[i] = feature_rep_big2(grids[i]) return freps
def incremental_freps_big2(grid, frep, cell, ce_type, chs): """ Given a grid, its feature representation frep, and a set of actions specified by cell, event type and a list of channels, derive feature representations for the afterstates of grid """ r, c = cell neighs1o = neighbors_np(1, r, c, False) neighs2o = _neighs2o[r, c, :_n_neighs_o[0, r, c]] neighs3o = _neighs3o[r, c, :_n_neighs_o[1, r, c]] neighs4o = _neighs4o[r, c, :_n_neighs_o[2, r, c]] neighs2 = neighbors_np(2, r, c, True) freps = np.zeros((len(chs), intp(rows), intp(cols), n_channels * 5 + 1), dtype=int32) freps[:] = frep if ce_type == CEvent.END: n_used_neighs_diff = -1 n_elig_self_diff = 1 bflip = 1 grid[cell][chs] = 0 else: n_used_neighs_diff = 1 n_elig_self_diff = -1 bflip = 0 for i in range(len(chs)): ch = chs[i] for j in range(len(neighs1o)): freps[i, neighs1o[j, 0], neighs1o[j, 1], ch] += n_used_neighs_diff for j in range(len(neighs2o)): freps[i, neighs2o[j, 0], neighs2o[j, 1], n_channels + ch] += n_used_neighs_diff for j in range(len(neighs3o)): freps[i, neighs3o[j, 0], neighs3o[j, 1], n_channels * 2 + ch] += n_used_neighs_diff for j in range(len(neighs4o)): freps[i, neighs4o[j, 0], neighs4o[j, 1], n_channels * 3 + ch] += n_used_neighs_diff for j in range(len(neighs2)): r2, c2 = neighs2[j, 0], neighs2[j, 1] neighs = neighbors_np(2, r2, c2, False) not_eligible = grid[r2, c2, ch] for k in range(len(neighs)): not_eligible = not_eligible or grid[neighs[k, 0], neighs[k, 1], ch] if not not_eligible: # For END: ch is in use at 'cell', but will become eligible # For NEW: ch is eligible at given neighs2, but will be taken in use freps[i, neighs2[j, 0], neighs2[j, 1], n_channels * 4 + ch] = bflip freps[i, neighs2[j, 0], neighs2[j, 1], -1] += n_elig_self_diff if ce_type == CEvent.END: grid[cell][chs] = 1 return freps
def get_dtype_size(typingctx, dtype=None): assert isinstance(dtype, types.DTypeSpec) def codegen(context, builder, sig, args): num_bytes = context.get_abi_sizeof(context.get_data_type(dtype.dtype)) return context.get_constant(types.intp, num_bytes) return types.intp(dtype), codegen
def feature_rep(grid): """ As in Singh(96) For each cell: - The number of eligible channels For each cell-channel pair: - The number of times the ch is used by neighbors with dist 4 or less """ assert grid.ndim == 3 frep = np.zeros((intp(rows), intp(cols), n_channels + 1), dtype=int32) for r in range(rows): for c in range(cols): neighs = neighbors_np(4, r, c, countself) n_used = np.zeros(n_channels, dtype=int32) for i in range(len(neighs)): n_used += grid[neighs[i, 0], neighs[i, 1]] frep[r, c, :-1] = n_used frep[r, c, -1] = np.sum(_eligible_map(grid, r, c)) return frep
def myintrin(typingctx, x, arr): if not isinstance(x, types.IntegerLiteral): raise errors.RequireLiteralValue(given_reason1) if arr.ndim != 1: raise ValueError(given_reason2) sig = types.intp(x, arr) def codegen(context, builder, signature, args): pass return sig, codegen
def incremental_freps(grid, frep, cell, ce_type, chs): """ Given a grid, its feature representation frep, and a set of actions specified by cell, event type and a list of channels, derive feature representations for the afterstates of grid """ r, c = cell # The focal cell neighs4 = neighbors_np(4, r, c, countself) neighs2 = neighbors_np(2, r, c, True) freps = np.zeros((len(chs), intp(rows), intp(cols), n_channels + 1), dtype=int32) freps[:] = frep if ce_type == CEvent.END: n_used_neighs_diff = -1 n_elig_self_diff = 1 grid[cell][chs] = 0 else: n_used_neighs_diff = 1 n_elig_self_diff = -1 for i, ch in enumerate(chs): for j in range(len(neighs4)): freps[i, neighs4[j, 0], neighs4[j, 1], ch] += n_used_neighs_diff for j in range(len(neighs2)): r2, c2 = neighs2[j, 0], neighs2[j, 1] # The neighborhood of a neighbor of the focal cell neighs = neighbors_np(2, r2, c2, False) not_eligible = grid[r2, c2, ch] for k in range(len(neighs)): not_eligible = not_eligible or grid[neighs[k, 0], neighs[k, 1], ch] if not not_eligible: # For END: ch is in use at focal cell (since END event), # and is thus ineligible at (r2, c2), # but will become eligible (since this if-case triggered), # so increment by 1. # For NEW/HOFF: ch is eligible at focal cell, # but will be taken in use, thus if ch is currently eligible # at (r2, c2) then it will become ineligible and we subtract 1. freps[i, r2, c2, -1] += n_elig_self_diff if ce_type == CEvent.END: grid[cell][chs] = 1 return freps
def feature_rep_big2(grid): """ For each cell: - The number of eligible channels For each cell-channel pair: - The number of times the ch is used by neighbors with dist 4 - The number of times the ch is used by neighbors with dist 3 - The number of times the ch is used by neighbors with dist 2 - The number of times the ch is used by neighbors with dist 1 - If the ch is eligible or not """ assert grid.ndim == 3 frep = np.zeros((intp(rows), intp(cols), n_channels * 5 + 1), dtype=int32) for r in range(rows): for c in range(cols): neighs1o = neighbors_np(1, r, c, False) neighs2o = _neighs2o[r, c, :_n_neighs_o[0, r, c]] neighs3o = _neighs3o[r, c, :_n_neighs_o[1, r, c]] neighs4o = _neighs4o[r, c, :_n_neighs_o[2, r, c]] n_used1 = np.zeros(n_channels, dtype=int32) n_used2 = np.zeros(n_channels, dtype=int32) n_used3 = np.zeros(n_channels, dtype=int32) n_used4 = np.zeros(n_channels, dtype=int32) for i in range(len(neighs1o)): n_used1 += grid[neighs1o[i, 0], neighs1o[i, 1]] for i in range(len(neighs2o)): n_used2 += grid[neighs2o[i, 0], neighs2o[i, 1]] for i in range(len(neighs3o)): n_used3 += grid[neighs3o[i, 0], neighs3o[i, 1]] for i in range(len(neighs4o)): n_used4 += grid[neighs4o[i, 0], neighs4o[i, 1]] frep[r, c, :n_channels] = n_used1 frep[r, c, n_channels:n_channels * 2] = n_used2 frep[r, c, n_channels * 2:n_channels * 3] = n_used3 frep[r, c, n_channels * 3:n_channels * 4] = n_used4 elig = _eligible_map(grid, r, c) frep[r, c, n_channels * 4:n_channels * 5] = elig frep[r, c, -1] = np.sum(elig) return frep
def _slice_span(typingctx, sliceobj): """Compute the span from the given slice object. """ sig = types.intp(sliceobj) def codegen(context, builder, sig, args): [slicetype] = sig.args [sliceobj] = args slice = context.make_helper(builder, slicetype, sliceobj) result_size = slicing.get_slice_length(builder, slice) return result_size return sig, codegen
def _slice_span(typingctx, sliceobj): """Compute the span from the given slice object. """ sig = types.intp(sliceobj) def codegen(context, builder, sig, args): [slicetype] = sig.args [sliceobj] = args slice = context.make_helper(builder, slicetype, sliceobj) result_size = slicing.get_slice_length(builder, slice) return result_size return sig, codegen
def codegen(cgctx, builder, signature, args): lltupty = cgctx.get_value_type(tupty) # Create an empty int tuple. tup = cgutils.get_null_value(lltupty) # Get the shape list from the args and we don't need shape tuple. [in_shape, _] = args def array_indexer(a, i): return a[i] # loop to fill the tuple for i in range(nd): dataidx = cgctx.get_constant(types.intp, i) # compile and call array_indexer data = cgctx.compile_internal(builder, array_indexer, types.intp(shape_list, types.intp), [in_shape, dataidx]) tup = builder.insert_value(tup, data, i) return tup
r = numba_xgesv( kind, # kind n, # n nrhs, # nhrs a.ctypes, # a n, # lda ipiv.ctypes, # ipiv b.ctypes, # b n # ldb ) return r return _numba_linalg_solve_impl @jit(types.intp(types.intp, types.intp), nopython=True, cache=True) def comb_jit(N, k): """ Numba jitted function that computes N choose k. Return `0` if the outcome exceeds the maximum value of `np.intp` or if N < 0, k < 0, or k > N. Parameters ---------- N : scalar(int) k : scalar(int) Returns ------- val : scalar(int)
r = numba_xgesv( kind, # kind n, # n nrhs, # nhrs a.ctypes, # a n, # lda ipiv.ctypes, # ipiv b.ctypes, # b n # ldb ) return r return _numba_linalg_solve_impl @jit(types.intp(types.intp, types.intp), nopython=True, cache=True) def comb_jit(N, k): """ Numba jitted function that computes N choose k. Return `0` if the outcome exceeds the maximum value of `np.intp` or if N < 0, k < 0, or k > N. Parameters ---------- N : scalar(int) k : scalar(int) Returns ------- val : scalar(int)
return builder.bitcast(val.operands[0], lir.IntType(8).as_pointer()) # tslib_so = inspect.getfile(pd._libs.tslib) # tslib_cdll = ctypes.CDLL(tslib_so) # func_parse_iso = tslib_cdll.parse_iso_8601_datetime # func_parse_iso.restype = ctypes.c_int32 # func_parse_iso.argtypes = [ctypes.c_void_p, ctypes.c_int32, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] # func_dts_to_dt = tslib_cdll.pandas_datetimestruct_to_datetime # func_dts_to_dt.restype = ctypes.c_int64 # func_dts_to_dt.argtypes = [ctypes.c_int, ctypes.c_void_p] sig = types.intp( types.voidptr, # C str types.intp, # len(str) types.voidptr, # struct ptr types.voidptr, # int ptr types.voidptr, # int ptr ) parse_iso_8601_datetime = types.ExternalFunction("parse_iso_8601_datetime", sig) sig = types.intp( types.intp, # fr magic number types.voidptr, # struct ptr types.voidptr, # out int ptr ) convert_datetimestruct_to_datetime = types.ExternalFunction("convert_datetimestruct_to_datetime", sig) @numba.njit(locals={'arg1': numba.int32, 'arg3': numba.int32, 'arg4': numba.int32}) def parse_datetime_str(str): arg0 = hpat.str_ext.unicode_to_char_ptr(str)
def init_set_string(): return set() @overload(init_set_string) def init_set_overload(): return lambda: _init_set_string() add_set_string = types.ExternalFunction("insert_set_string", types.void(set_string_type, types.voidptr)) len_set_string = types.ExternalFunction("len_set_string", types.intp(set_string_type)) num_total_chars_set_string = types.ExternalFunction("num_total_chars_set_string", types.intp(set_string_type)) # TODO: box set(string) @generated_jit(nopython=True, cache=True) def build_set(A): if is_str_arr_typ(A): return _build_str_set_impl else: return lambda A: set(A)
def init_set_string(): return set() @overload(init_set_string) def init_set_overload(): return lambda: _init_set_string() add_set_string = types.ExternalFunction( "insert_set_string", types.void(set_string_type, types.voidptr)) len_set_string = types.ExternalFunction("len_set_string", types.intp(set_string_type)) num_total_chars_set_string = types.ExternalFunction( "num_total_chars_set_string", types.intp(set_string_type)) # TODO: box set(string) @generated_jit(nopython=True, cache=True) def build_set(A): if is_str_arr_typ(A): return _build_str_set_impl else: return lambda A: set(A)
def adaptor(tyctx, thing, another): # This function creates a call specialisation on "less" based on the # type of "thing" and its literal value # resolve to function type sig = types.intp(thing, another) fnty = tyctx.resolve_value_type(less) def codegen(cgctx, builder, sig, args): ty = sig.args[0] # trigger resolution to get a "custom_hash" impl based on the call type # "ty" and its literal value # import pdb; pdb.set_trace() lsig = fnty.get_call_type(tyctx, (ty, ty), {}) resolved = cgctx.get_function(fnty, lsig) # close over resolved function, this is to deal with python scoping def resolved_codegen(cgctx, builder, sig, args): return resolved(builder, args) # A python function "wrapper" is made for the `@cfunc` arg, this calls # the jitted function "wrappee", which will be compiled as part of the # compilation chain for the cfunc. In turn the wrappee jitted function # has an intrinsic call which is holding reference to the resolved type # specialised custom_hash call above. @intrinsic def dispatcher(_ityctx, _a, _b): return types.int8(thing, another), resolved_codegen @intrinsic def deref(_ityctx, _x): # to deref the void * passed. TODO: nrt awareness catchthing = thing sig = catchthing(_x) def codegen(cgctx, builder, sig, args): toty = cgctx.get_value_type(sig.return_type).as_pointer() addressable = builder.bitcast(args[0], toty) zero_intpt = cgctx.get_constant(types.intp, 0) vref = builder.gep(addressable, [zero_intpt], inbounds=True) return builder.load(vref) return sig, codegen @njit def wrappee(ap, bp): a = deref(ap) b = deref(bp) return dispatcher(a, b) def wrapper(a, b): return wrappee(a, b) callback = cfunc(types.int8(types.voidptr, types.voidptr))(wrapper) # bake in address as a int const address = callback.address return cgctx.get_constant(types.intp, address) return sig, codegen