(self, lhs, func_name, assign, args)
return out
transform np.func() calls
transform np.func() calls
transform np . func () calls
def _run_call_np(self, lhs, func_name, assign, args):
        """transform np.func() calls
        """
        # allocs are handled separately
        is_1D_bool = (self._is_1D_Var_arr(lhs) or self._is_1D_arr(lhs))
        err_str = "allocation calls handled separately 'empty', 'zeros', 'ones', 'full' etc."
        assert not (is_1D_bool and func_name in sdc.utilities.utils.np_alloc_callnames), err_str

        out = [assign]
        scope = assign.target.scope
        loc = assign.loc

        # numba doesn't support np.reshape() form yet
        # if func_name == 'reshape':
        #     size_var = args[1]
        #     # handle reshape like new allocation
        #     out, new_size_var = self._run_alloc(size_var, lhs)
        #     args[1] = new_size_var
        #     out.append(assign)

        if (func_name == 'array' and is_array(self.state.typemap, args[0].name) and self._is_1D_arr(args[0].name)):
            in_arr = args[0].name
            self._array_starts[lhs] = self._array_starts[in_arr]
            self._array_counts[lhs] = self._array_counts[in_arr]
            self._array_sizes[lhs] = self._array_sizes[in_arr]

        # output array has same properties (starts etc.) as input array
        if (func_name in ['cumsum', 'cumprod', 'empty_like', 'zeros_like', 'ones_like',
                          'full_like', 'copy', 'ravel', 'ascontiguousarray'] and self._is_1D_arr(args[0].name)):
            if func_name == 'ravel':
                assert self.state.typemap[args[0].name].ndim == 1, "only 1D ravel supported"
            in_arr = args[0].name
            self._array_starts[lhs] = self._array_starts[in_arr]
            self._array_counts[lhs] = self._array_counts[in_arr]
            self._array_sizes[lhs] = self._array_sizes[in_arr]

        if (func_name in ['cumsum', 'cumprod'] and self._is_1D_arr(args[0].name)):
            in_arr = args[0].name
            in_arr_var = args[0]
            lhs_var = assign.target
            # allocate output array
            # TODO: compute inplace if input array is dead
            out = mk_alloc(
                self.state.typemap,
                self.state.calltypes,
                lhs_var,
                tuple(
                    self._array_sizes[in_arr]),
                self.state.typemap[in_arr].dtype,
                scope,
                loc)
            # generate distributed call
            dist_attr_var = ir.Var(scope, mk_unique_var("$dist_attr"), loc)
            dist_func_name = "dist_" + func_name
            dist_func = getattr(distributed_api, dist_func_name)
            dist_attr_call = ir.Expr.getattr(self._g_dist_var, dist_func_name, loc)
            self.state.typemap[dist_attr_var.name] = get_global_func_typ(dist_func)
            dist_func_assign = ir.Assign(dist_attr_call, dist_attr_var, loc)
            err_var = ir.Var(scope, mk_unique_var("$dist_err_var"), loc)
            self.state.typemap[err_var.name] = types.int32
            dist_call = ir.Expr.call(dist_attr_var, [in_arr_var, lhs_var], (), loc)
            self.state.calltypes[dist_call] = self.state.typemap[dist_attr_var.name].get_call_type(
                self.state.typingctx, [self.state.typemap[in_arr], self.state.typemap[lhs]], {})
            dist_assign = ir.Assign(dist_call, err_var, loc)
            return out + [dist_func_assign, dist_assign]

        # sum over the first axis is distributed, A.sum(0)
        if func_name == 'sum' and len(args) == 2:
            axis_def = guard(get_definition, self.state.func_ir, args[1])
            if isinstance(axis_def, ir.Const) and axis_def.value == 0:
                reduce_op = Reduce_Type.Sum
                reduce_var = assign.target
                return out + self._gen_reduce(reduce_var, reduce_op, scope, loc)

        if func_name == 'dot':
            return self._run_call_np_dot(lhs, assign, args)

        if func_name == 'stack' and self._is_1D_arr(lhs):
            # TODO: generalize
            in_arrs, _ = guard(find_build_sequence, self.state.func_ir, args[0])
            arr0 = in_arrs[0].name
            self._array_starts[lhs] = [self._array_starts[arr0][0], None]
            self._array_counts[lhs] = [self._array_counts[arr0][0], None]
            self._array_sizes[lhs] = [self._array_sizes[arr0][0], None]

        return out
def _run_call_np ( self , lhs , func_name , assign , args ) : # allocs are handled separately is_1D_bool = ( self . _is_1D_Var_arr ( lhs ) or self . _is_1D_arr ( lhs ) ) err_str = "allocation calls handled separately 'empty', 'zeros', 'ones', 'full' etc." assert not ( is_1D_bool and func_name in sdc . utilities . utils . np_alloc_callnames ) , err_str out = [ assign ] scope = assign . target . scope loc = assign . loc # numba doesn't support np.reshape() form yet # if func_name == 'reshape': #     size_var = args[1] #     # handle reshape like new allocation #     out, new_size_var = self._run_alloc(size_var, lhs) #     args[1] = new_size_var #     out.append(assign) if ( func_name == 'array' and is_array ( self . state . typemap , args [ 0 ] . name ) and self . _is_1D_arr ( args [ 0 ] . name ) ) : in_arr = args [ 0 ] . name self . _array_starts [ lhs ] = self . _array_starts [ in_arr ] self . _array_counts [ lhs ] = self . _array_counts [ in_arr ] self . _array_sizes [ lhs ] = self . _array_sizes [ in_arr ] # output array has same properties (starts etc.) as input array if ( func_name in [ 'cumsum' , 'cumprod' , 'empty_like' , 'zeros_like' , 'ones_like' , 'full_like' , 'copy' , 'ravel' , 'ascontiguousarray' ] and self . _is_1D_arr ( args [ 0 ] . name ) ) : if func_name == 'ravel' : assert self . state . typemap [ args [ 0 ] . name ] . ndim == 1 , "only 1D ravel supported" in_arr = args [ 0 ] . name self . _array_starts [ lhs ] = self . _array_starts [ in_arr ] self . _array_counts [ lhs ] = self . _array_counts [ in_arr ] self . _array_sizes [ lhs ] = self . _array_sizes [ in_arr ] if ( func_name in [ 'cumsum' , 'cumprod' ] and self . _is_1D_arr ( args [ 0 ] . name ) ) : in_arr = args [ 0 ] . name in_arr_var = args [ 0 ] lhs_var = assign . target # allocate output array # TODO: compute inplace if input array is dead out = mk_alloc ( self . state . typemap , self . state . calltypes , lhs_var , tuple ( self . _array_sizes [ in_arr ] ) , self . state . typemap [ in_arr ] . dtype , scope , loc ) # generate distributed call dist_attr_var = ir . Var ( scope , mk_unique_var ( "$dist_attr" ) , loc ) dist_func_name = "dist_" + func_name dist_func = getattr ( distributed_api , dist_func_name ) dist_attr_call = ir . Expr . getattr ( self . _g_dist_var , dist_func_name , loc ) self . state . typemap [ dist_attr_var . name ] = get_global_func_typ ( dist_func ) dist_func_assign = ir . Assign ( dist_attr_call , dist_attr_var , loc ) err_var = ir . Var ( scope , mk_unique_var ( "$dist_err_var" ) , loc ) self . state . typemap [ err_var . name ] = types . int32 dist_call = ir . Expr . call ( dist_attr_var , [ in_arr_var , lhs_var ] , ( ) , loc ) self . state . calltypes [ dist_call ] = self . state . typemap [ dist_attr_var . name ] . get_call_type ( self . state . typingctx , [ self . state . typemap [ in_arr ] , self . state . typemap [ lhs ] ] , { } ) dist_assign = ir . Assign ( dist_call , err_var , loc ) return out + [ dist_func_assign , dist_assign ] # sum over the first axis is distributed, A.sum(0) if func_name == 'sum' and len ( args ) == 2 : axis_def = guard ( get_definition , self . state . func_ir , args [ 1 ] ) if isinstance ( axis_def , ir . Const ) and axis_def . value == 0 : reduce_op = Reduce_Type . Sum reduce_var = assign . target return out + self . _gen_reduce ( reduce_var , reduce_op , scope , loc ) if func_name == 'dot' : return self . _run_call_np_dot ( lhs , assign , args ) if func_name == 'stack' and self . _is_1D_arr ( lhs ) : # TODO: generalize in_arrs , _ = guard ( find_build_sequence , self . state . func_ir , args [ 0 ] ) arr0 = in_arrs [ 0 ] . name self . _array_starts [ lhs ] = [ self . _array_starts [ arr0 ] [ 0 ] , None ] self . _array_counts [ lhs ] = [ self . _array_counts [ arr0 ] [ 0 ] , None ] self . _array_sizes [ lhs ] = [ self . _array_sizes [ arr0 ] [ 0 ] , None ] return out