lpython icon indicating copy to clipboard operation
lpython copied to clipboard

Unhandled Exception in Array code

Open rebcabin opened this issue 1 year ago • 1 comments

(different but similar to #2479 )

import numpy
from lpython import (i16, i32, ccallback, c_p_pointer, Pointer, u64, CPtr, i64)

######## ALL THE LINES WITH EIGHT COMMENT MARKS ARE THE ONES WE NEED TO
######## BRING UP!  AS IT STANDS, THIS CODE WORKS IN LPYTHON MAIN AS OF 4
######## FEBRUARY 2024.

# https://numpy.org/devdocs/reference/typing.html
######## from numpy.typing import NDArray


# plan for 30 Jan 2024 --
# step 0: comment out this code and ./build_baryon.sh to run on APU
#         emulator; or ./run_full_emulation.sh to run in CPython.
# step 1: side-by-side numpy implementation in full-emulation
#         - get there line-by-line
#         = focus on gvml_add_u16 first


def numpy_side_by_side(n: i32, m: i32, l: i32, M1: i32, M2: i32,
                       A: CPtr, B: CPtr, C: CPtr) -> \
        None: ######## NDArray[numpy.int16]:
    VR_SIZE: i32 = 32_768

    # In the primary example, n = 15, m = 3, l = 32_768,
    # M1 = 1, M2 = 5

    # source GSI L4 arrays
    pA_nm: Pointer[i16[:]] = c_p_pointer(A, i16[:], array([n * m]))
    pB_ml: Pointer[i16[:]] = c_p_pointer(B, i16[:], array([m * l]))

    # source numpy arrays
    ######## A_nm: NDArray[numpy.int16] = numpy.zeros((n, m), dtype=numpy.int16)
    ######## for row in range(n):
    ########     A_nm[row,:] = pA_nm[(row * m):((row + 1) * m)]
    A_nm: Array[i16, n, m]
    row : i32
    for row in range(n):
        A_nm[row,:] = pA_nm[(row * m):((row + 1) * m)]


    ######## B_ml: NDArray[numpy.int16] = numpy.zeros((m, l), dtype=numpy.int16)
    ######## for row in range(m):
    ########     B_ml[row,:] = pB_ml[(row * l):((row + 1) * l)]

    # # destination numpy array
    ######## C_nl: NDArray[numpy.int16] = numpy.zeros((n, l), dtype=numpy.int16)

    # destination GSI L4 array
    pC_nl: Pointer[i16[:]] = c_p_pointer(C, i16[:], array([n * l]))

    # First, accumulate outer product without blocking. This is
    # the code we would -ultimately- like to compile. Notice that
    # all GSI-specific L1, L4, MMB are hidden.

    k: i32
    ######## for k in range(0, m):
    ########     C_nl += numpy.outer(A_nm[:,k], B_ml[k,:])
    ########     pass

    # expect
    # [[ 5  8 11 ... 20 23 26],
    #  [ 8 14 20 ... 38 44 50],
    #  [11 20 29 ... 56 65 74], ...
    #
    #  [ 8 14 20 ... 38 44 50],
    #  [11 20 29 ... 56 65 74],
    #  [14 26 38 ... 74 86 98]]
    set_breakpoint_here_and_inspect_C_nl : i32 = 0

    # Second, with explicit blocking. This is a stepping-stone
    # for our back-end. Notice that L1 and MMB are hidden.

    # T_1l: NDArray[numpy.int16] = numpy.zeros((1, l), dtype=numpy.int16)
    # B_1l: NDArray[numpy.int16] = numpy.zeros((1, l), dtype=numpy.int16)
    A_ik: i16
    jj: i32
    ii: i32
    i: i32
    for jj in range(0, l, VR_SIZE):  # each VR-col chunk in B and C
        for ii in range(0, n, M2):  # each M2 block in A cols and B rows
            for i in range(0, M2):  # zero-out rows of C
                ######## C_nl[i + ii, :] = 0
                pass
            for k in range(0, m):  # rows of B
                # B_1l[0, :] = B_ml[k, :]
                for i in range(0, M2):
                    ######## A_ik = A_nm[i + ii, k]
                    # broadcast a single element of A
                    # T_1l[0, :] = A_ik
                    # pointwise (Hadamard) product:
                    # T_1l[0, :] = np.multiply(B_1l[0, :], T_1l[0, :])
                    # C_nl[i + ii, :] += T_1l[0, :]
                    # optimization without the temporaries
                    ######## C_nl[i + ii, :] += B_ml[k, :] * A_ik
                    pass

    set_breakpoint_here_and_inspect_C_nl = 0

    ######## return C_nl

def main():
    n  : i32 = 15
    m  : i32 = 3
    l  : i32 = 32_768
    M1 : i32 = 1
    M2 : i32 = 5
    A_l4 : CPtr
    B_l4 : CPtr
    C_l4 : CPtr
    numpy_side_by_side(n, m, l, M1, M2, A_l4, B_l4, C_l4)
    print ("hello, world!")


if __name__ == "__main__":
    main()
└─(10:50:28 on vector-backend ✖ ✭)──> lpython ../ISSUES/UNHANDLED-EXCEPTIONS/Issue2480.py     1 ↵ ──(Mon,Feb05)─┘
Traceback (most recent call last):
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 1872
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 824
    res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_evaluator.cpp", line 71
    run_fn, infile);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 9276
    pass_manager.apply_passes(al, &asr, co.po, diagnostics);
  File "/Users/brian/Documents/GitHub/lpython/src/libasr/pass/pass_manager.h", line 299
    apply_passes(al, asr, _passes, pass_options, diagnostics);
  File "/Users/brian/Documents/GitHub/lpython/src/libasr/pass/pass_manager.h", line 160
    _passes_db[passes[i]](al, *asr, pass_options);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/libasr/pass/array_op.cpp", line 1910
    u.visit_TranslationUnit(unit);
  File "../libasr/asr.h", line 5277
  File "../libasr/asr.h", line 5060
  File "../libasr/asr.h", line 4774
  File "../libasr/pass/pass_utils.h", line 317
  File "../libasr/asr.h", line 5290
  File "../libasr/asr.h", line 5060
  File "../libasr/asr.h", line 4775
  File "../libasr/pass/pass_utils.h", line 298
  File "../libasr/asr.h", line 5303
  File "../libasr/asr.h", line 5077
  File "../libasr/asr.h", line 4805
  File "../libasr/asr.h", line 5441
  File "../libasr/asr.h", line 5077
  File "../libasr/asr.h", line 5077
  Binary file "/usr/lib/system/libsystem_platform.dylib", local address: 0x18046da23
Segfault: Signal SIGSEGV (segmentation fault) received
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)───────────────────────────────────────(brian@MacBook-Pro:s001)─┐

rebcabin avatar Feb 05 '24 18:02 rebcabin

It's probably some array operations. We have made good improvements, so we should sync ASR from LC and LFortran, but probably there are more bugs.

certik avatar Feb 05 '24 19:02 certik