Skip to content

Implement Jupyter kernel #2756

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 67 additions & 24 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,36 @@ jobs:
with:
fetch-depth: 0

- name: Cache conda
uses: actions/cache@v3
env:
CACHE_NUMBER: 0
with:
path: ~/conda_pkgs_dir
key:
${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/environment.yml') }}

- uses: conda-incubator/setup-miniconda@v2
# - name: Cache conda
# uses: actions/cache@v3
# env:
# CACHE_NUMBER: 0
# with:
# path: ~/conda_pkgs_dir
# key:
# ${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/environment.yml') }}

- uses: mamba-org/setup-[email protected]
with:
miniconda-version: "latest"
auto-update-conda: true
environment-file: ci/environment.yml
python-version: ${{ matrix.python-version }}
use-only-tar-bz2: true
create-args: >-
python=${{ matrix.python-version }}

- name: Install Windows Conda Packages
if: contains(matrix.os, 'windows')
shell: bash -e -l {0}
run: conda install m2-bison=3.0.4 cmake=3.21.1
run: micromamba install m2-bison=3.0.4 m2-filesystem cmake=3.21.1

- name: Install Linux / macOS Conda Packages
if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos')
shell: bash -e -l {0}
run: conda install bison=3.4 nodejs=18
run: micromamba install bison=3.4 nodejs=18

- name: Conda info
shell: bash -e -l {0}
run: |
conda info
conda list
# - name: Conda info
# shell: bash -e -l {0}
# run: |
# conda info
# conda list

- name: Setup Platform (Linux)
if: contains(matrix.os, 'ubuntu')
Expand Down Expand Up @@ -87,9 +85,9 @@ jobs:
if: contains(matrix.os, 'windows')
shell: cmd
run: |
set CONDA_INSTALL_LOCN=C:\\Miniconda3
call %CONDA_INSTALL_LOCN%\Scripts\activate.bat
call conda activate test
set MAMBA_INSTALL_LOCN=C:\\Users\runneradmin\micromamba
call %MAMBA_INSTALL_LOCN%\Scripts\activate.bat
call micromamba activate test
set LFORTRAN_CMAKE_GENERATOR=Ninja
set WIN=1
set MACOS=0
Expand Down Expand Up @@ -497,6 +495,51 @@ jobs:
cd integration_tests
./run_tests.py -b cpython c_py

build_jupyter_kernel:
name: Build Jupyter Kernel
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: mamba-org/setup-micromamba@v1
with:
environment-file: ci/environment.yml
create-args: >-
jupyter
nlohmann_json
python=3.10
bison=3.4
xeus=5.1.0
xeus-zmq=3.0.0

- uses: hendrikmuhs/ccache-action@main
with:
variant: sccache
key: ${{ github.job }}-${{ matrix.os }}

- name: Build LPython with Kernel
shell: bash -e -l {0}
run: |
./build0.sh
export CXXFLAGS="-Werror"
cmake . -GNinja \
-DCMAKE_BUILD_TYPE=Debug \
-DWITH_LLVM=yes \
-DWITH_XEUS=yes \
-DCMAKE_PREFIX_PATH="$CONDA_PREFIX" \
-DCMAKE_INSTALL_PREFIX="$CONDA_PREFIX"

ninja install
ctest --output-on-failure
jupyter kernelspec list --json

- name: Test Kernel
shell: bash -e -l {0}
run: |
ctest --output-on-failure

upload_tarball:
name: Upload Tarball
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ src/libasr/wasm_visitor.h
src/libasr/pass/intrinsic_function_registry_util.h
src/libasr/config.h
share/jupyter/kernels/fortran/kernel.json
share/jupyter/kernels/lpython/kernel.json
src/runtime/*.o.empty.c
python_ast.py
python_ast.h
Expand Down
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,14 @@ endif()
# XEUS (Fortran kernel)
set(WITH_XEUS no CACHE BOOL "Build with XEUS support")
if (WITH_XEUS)
find_package(xeus 0.24.1 REQUIRED)
find_package(xeus 5.1.0 REQUIRED)
find_package(xeus-zmq 3.0.0 REQUIRED)
set(HAVE_LFORTRAN_XEUS yes)

# Generate kernel.json with correct paths
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/fortran/kernel.json.in"
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/fortran/kernel.json"
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/lpython/kernel.json.in"
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/lpython/kernel.json"
)

# Configuration and data directories for Jupyter and LFortran
Expand Down
3 changes: 2 additions & 1 deletion ci/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ dependencies:
- toml
- pytest
- jupyter
- xeus=1.0.1
- xeus=5.1.0
- xeus-zmq=3.0.0
- xtl
- nlohmann_json
- cppzmq
Copy link
Collaborator

Choose a reason for hiding this comment

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

Migration to xeus 5 and above allows us to drop the dependency on xtl and cppzmq for all downstream projects (mostly kernels) so you have remove them and also we should pin nlohmann_json to 3.11.3 wherever possible

Expand Down
67 changes: 67 additions & 0 deletions doc/src/developers_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "c86338ac-53ca-4115-8c5a-8bf8a5c7113e",
"metadata": {},
"outputs": [],
"source": [
"%%showast\n",
"def add(x: i32, y: i32) -> i32:\n",
" return x + y"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23834b08-2f3f-45e7-a1ce-21a9fd4e5117",
"metadata": {},
"outputs": [],
"source": [
"%%showasr\n",
"def add(x: i32, y: i32) -> i32:\n",
" return x + y"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec7426b4-e2e5-416c-bcae-9bb9c8926c9b",
"metadata": {},
"outputs": [],
"source": [
"%%showllvm\n",
"def sub(x: i32, y: i32) -> i32:\n",
" return add(x, -y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "716c56ef-8210-4daf-aa23-96b385801014",
"metadata": {},
"outputs": [],
"source": [
"%%showasm\n",
"def mul(x: i32, y: i32) -> i32:\n",
" return x * y"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "LPython",
"language": "python",
"name": "lpython"
},
"language_info": {
"file_extension": ".f90",
"mimetype": "text/x-python",
"name": "python",
"version": "2018"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
134 changes: 134 additions & 0 deletions examples/example_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "e87300c2-64ed-4636-8448-591f36faba29",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, LPython\n"
]
}
],
"source": [
"print(\"Hello, LPython\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dfcac851-7b49-4065-8c64-4a31658249f7",
"metadata": {},
"outputs": [],
"source": [
"def add(x: i32, y: i32) -> i32:\n",
" return x + y"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "09213386-84d5-4e7c-83ba-c3b027f765dd",
"metadata": {},
"outputs": [],
"source": [
"def sub(x: i32, y: i32) -> i32:\n",
" return x - y"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a4b49fd3-bf17-4287-9d5e-60f14ebc9a0f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add(2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d6f4961f-7f0c-45a6-9bf8-e549e97098b0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sub(2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "398fd4be-d7cc-4912-8aa1-880aa58b37ab",
"metadata": {},
"outputs": [],
"source": [
"@dataclass\n",
"class MyClass:\n",
" x: i32\n",
" y: f64\n",
" z: str"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "628f0b7d-09a6-49de-a0e6-2f6c664f2ba2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12 2.45000000000000000e+01 LPython\n"
]
}
],
"source": [
"x: MyClass = MyClass(12, 24.5, \"LPython\")\n",
"print(x)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "LPython",
"language": "python",
"name": "lpython"
},
"language_info": {
"file_extension": ".f90",
"mimetype": "text/x-python",
"name": "python",
"version": "2018"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
10 changes: 10 additions & 0 deletions share/jupyter/kernels/lpython/kernel.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"display_name": "LPython",
"argv": [
"@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@/lpython",
"kernel",
"-f",
"{connection_file}"
],
"language": "python"
}
11 changes: 9 additions & 2 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include <libasr/config.h>
#include <libasr/string_utils.h>
#include <libasr/lsp_interface.h>
#ifdef HAVE_LFORTRAN_XEUS
# include <lpython/python_kernel.h>
#endif
#include <lpython/utils.h>
#include <lpython/python_serialization.h>
#include <lpython/parser/tokenizer.h>
Expand Down Expand Up @@ -2012,8 +2015,12 @@ int main(int argc, char *argv[])
// }

if (kernel) {
std::cerr << "The kernel subcommand is not implemented yet for LPython." << std::endl;
return 1;
#ifdef HAVE_LFORTRAN_XEUS
return LCompilers::LPython::run_kernel(arg_kernel_f);
#else
std::cerr << "The kernel subcommand requires LFortran to be compiled with XEUS support. Recompile with `WITH_XEUS=yes`." << std::endl;
return 1;
#endif
}

// if (mod) {
Expand Down
Loading
Loading