Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKG: Add package lnn #1719

Merged
merged 17 commits into from
Apr 20, 2023
Merged

PKG: Add package lnn #1719

merged 17 commits into from
Apr 20, 2023

Conversation

Shaikh-Ubaid
Copy link
Collaborator

@Shaikh-Ubaid Shaikh-Ubaid commented Apr 19, 2023

towards #992

This PR adds a basic neural network package consisting of a single perceptron node. It can do binary classification.

Example 1: Nand Classification, 4 data points

weights = [0.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00(bias)]
learn_rate = 5.00000000000000028e-02
accuracy = 0.00000000000000000e+00
epochs_cnt = 0
=================================
weights = [-1.00000000000000006e-01, -1.00000000000000006e-01, 1.00000000000000006e-01(bias)]
learn_rate = 5.00000000000000028e-02
accuracy = 1.00000000000000000e+02
epochs_cnt = 2

lpy_tmp

Example 2: Randomly constructed binary classifiable example, 5 data points

weights = [0.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00(bias)]
learn_rate = 5.00000000000000028e-02
accuracy = 0.00000000000000000e+00
epochs_cnt = 0
=================================
weights = [-2.20000000000000029e-01, 1.00000000000000006e-01, 1.00000000000000006e-01(bias)]
learn_rate = 5.00000000000000028e-02
accuracy = 1.00000000000000000e+02
epochs_cnt = 4

lpy_tmp 2

@Shaikh-Ubaid
Copy link
Collaborator Author

Shaikh-Ubaid commented Apr 19, 2023

ASR Verification fails currently:

(lp) lpython$ cat integration_tests/aaaa1.py 
from tmp_pkg import A, hi

def main():
    hi()
    a: A

main()
(lp) lpython$ lpython integration_tests/aaaa1.py --show-asr --indent
(TranslationUnit
    (SymbolTable
        1
        {
            A:
                (ExternalSymbol
                    1
                    A
                    5 A
                    tmp
                    []
                    A
                    Public
                ),
            hi:
                (ExternalSymbol
                    1
                    hi
                    5 hi
                    tmp
                    []
                    hi
                    Public
                ),
            main:
                (Function
                    (SymbolTable
                        8
                        {
                            a:
                                (Variable
                                    8
                                    a
                                    []
                                    Local
                                    ()
                                    ()
                                    Default
                                    (Struct
                                        5 A
                                        []
                                    )
                                    Source
                                    Public
                                    Required
                                    .false.
                                )
                        })
                    main
                    (FunctionType
                        []
                        ()
                        Source
                        Implementation
                        ()
                        .false.
                        .false.
                        .false.
                        .false.
                        .false.
                        []
                        []
                        .false.
                    )
                    [hi]
                    []
                    [(SubroutineCall
                        1 hi
                        ()
                        []
                        ()
                    )]
                    ()
                    Public
                    .false.
                    .false.
                ),
            tmp:
                (Module
                    (SymbolTable
                        5
                        {
                            A:
                                (StructType
                                    (SymbolTable
                                        6
                                        {
                                            x:
                                                (Variable
                                                    6
                                                    x
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Integer 4 [])
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    A
                                    []
                                    [x]
                                    Source
                                    Public
                                    .false.
                                    .false.
                                    ()
                                    ()
                                ),
                            hi:
                                (Function
                                    (SymbolTable
                                        7
                                        {
                                            
                                        })
                                    hi
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(Print
                                        ()
                                        [(StringConstant
                                            "hi"
                                            (Character 1 2 () [])
                                        )]
                                        ()
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                )
                        })
                    tmp
                    []
                    .false.
                    .false.
                ),
            tmp_pkg:
                (Module
                    (SymbolTable
                        3
                        {
                            A:
                                (ExternalSymbol
                                    3
                                    A
                                    5 A
                                    tmp
                                    []
                                    A
                                    Public
                                ),
                            hi:
                                (ExternalSymbol
                                    3
                                    hi
                                    5 hi
                                    tmp
                                    []
                                    hi
                                    Public
                                )
                        })
                    tmp_pkg
                    [tmp]
                    .false.
                    .false.
                )
        })
    [(SubroutineCall
        1 main
        ()
        []
        ()
    )]
)
ASR verify pass error: ASR verify: Struct::m_derived_type cannot point outside of its symbol table
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 1684
    return emit_asr(arg_file, lpython_pass_manager, runtime_library_dir,
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 194
    r = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics,
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 6704
    throw LCompilersException("Verify failed");
LCompilersException: Verify failed

@Shaikh-Ubaid
Copy link
Collaborator Author

This is currently a WIP (Work in progress), hence the CI tests might not pass.

@Shaikh-Ubaid Shaikh-Ubaid marked this pull request as ready for review April 20, 2023 07:39
@Shaikh-Ubaid
Copy link
Collaborator Author

I posted some issues that I came across while working on this PR - #1721, #1722, #1723, #1724, #1725.

There is also another issue about importing dataclass, which I am hoping I fixed in Support importing StructType and
ASR: Fix derived_type points outside symtab.

The initial commits perform some refactoring in the pass_array_by_data(). I refactored it while going through the pass to catch #1721. The refactoring is (mostly) done to break down large code into smaller functions, hopefully improving readability/maintainability.

@Shaikh-Ubaid
Copy link
Collaborator Author

This is ready. Please review and share feedback.

new_args.push_back(al, new_arg);
args_updated = true;
use_original_arg = false;
void check_and_update_args_for_pass_arr_by_data_passed_as_callback(const T& x) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for #1721?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a refactoring of code into a function. I named the function as check_and_update_args_for_pass_arr_by_data_passed_as_callback based on the operation that I thought the code was performing. I think it improves the code readibility. I think it is not towards any specific issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just renamed it to update_args_for_pass_arr_by_data_funcs_passed_as_callback().

@certik
Copy link
Contributor

certik commented Apr 20, 2023

Awesome. Thanks @Shaikh-Ubaid for this, that's exactly what we need. To use LPython to create a library and see what bugs we hit and report them as you did.

update_args_for_pass_arr_by_data_funcs_passed_as_callback()
@Shaikh-Ubaid
Copy link
Collaborator Author

This is ready.

@Shaikh-Ubaid Shaikh-Ubaid requested a review from certik April 20, 2023 14:37
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is fine. @czgdp1807 do you see any issues with the code changes?

@certik
Copy link
Contributor

certik commented Apr 20, 2023

I am going to merge it, as it fixes an important bug (importing struct). Thank you @Shaikh-Ubaid, great work!

@certik certik merged commit b28a440 into lcompilers:main Apr 20, 2023
@Shaikh-Ubaid Shaikh-Ubaid deleted the perceptron_pkg branch April 20, 2023 23:48
@Shaikh-Ubaid
Copy link
Collaborator Author

Thank you for the approval and merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants