def sum_first_n(arr, num):

    def body_fun(i, state):
        (arr, total) = (state['arr'], state['total'])
        arr_i = lax.dynamic_index_in_dim(arr, i, 0, False)
        return {'arr': arr, 'total': lax.add(total, arr_i)}
    init_val = {'arr': arr, 'total': 0.0}
    out_val = lax.fori_loop(0, lax.min(arr.shape[0], num), body_fun, init_val)
    return out_val['total']
def sum_first_n(arr, num):

    def body_fun(i, state):
        (arr, total) = (state['arr'], state['total'])
        arr_i = lax.dynamic_index_in_dim(arr, i, 0, False)
        return {'arr': arr, 'total': lax.add(total, arr_i)}
    init_val = {'arr': arr, 'total': 0.0}
    out_val = lax.fori_loop(0, lax.min(arr.shape[0], num), body_fun, init_val)
    return out_val['total']