Skip to content

Commit

Permalink
fix nested array bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Jan 24, 2024
1 parent d323ec3 commit 5456514
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
35 changes: 19 additions & 16 deletions docs/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Here's a contrived rough benchmark to show the magnitude differences between Pyt

**Spoiler**: GPU-backed KlongPy is about 790x faster than naive Python and 36x faster than NumPy-backed KlongPy.

### Python
## Python

```python
def python_vec(number=100):
r = timeit.timeit(lambda: [2 * (1 + x) for x in range(10000000)], number=number)
return r/number
```

### KlongPy
## KlongPy

```python
# NumPy and CuPy (CuPy is enabled via USE_GPU=1 environment variable
Expand All @@ -24,7 +24,7 @@ def klong_vec(number=100):
return r/number
```

### NumPy (explicit usage)
## NumPy (explicit usage)

```python
def NumPy_vec(number=100):
Expand All @@ -36,19 +36,22 @@ def NumPy_vec(number=100):

### CPU (AMD Ryzen 9 7950x)

$ python3 tests/perf_vector.py
Python: 0.369111s
KlongPy USE_GPU=None: 0.017946s
Numpy: 0.017896s
Python / KlongPy => 20.568334
Numpy / KlongPy => 0.997245
```bash
$ python3 tests/perf_vector.py
Python: 0.369111s
KlongPy USE_GPU=None: 0.017946s
Numpy: 0.017896s
Python / KlongPy => 20.568334
Numpy / KlongPy => 0.997245
```

### GPU (Same CPU with NVIDIA GeForce RTX 3090)

$ USE_GPU=1 python3 tests/perf_vector.py
Python: 0.364893s
KlongPy USE_GPU=1: 0.000461s
NumPy: 0.017053s
Python / KlongPy => 790.678069
Numpy / KlongPy => 36.951443

```bash
$ USE_GPU=1 python3 tests/perf_vector.py
Python: 0.364893s
KlongPy USE_GPU=1: 0.000461s
NumPy: 0.017053s
Python / KlongPy => 790.678069
Numpy / KlongPy => 36.951443
```
10 changes: 2 additions & 8 deletions klongpy/dyads.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,8 @@ def eval_dyad_integer_divide(a, b):


def _arr_to_list(a):
if np.isarray(a):
if a.ndim == 1:
return a
elif a.shape[0] == 1:
return [a.flatten()] # TODO: test squeeze
elif a.shape[0] > 1:
return a
return [a] if not is_list(a) else a
""" Convert a to a list if it is not already one """
return a if is_list(a) else [a]# if not is_list(a) else a


def eval_dyad_join(a, b):
Expand Down
10 changes: 10 additions & 0 deletions tests/kgtests/language/test_nested_join.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
:" test that we can create new arrays with nested arrays and the result has the right shape "

data::{!3}
t("1,,{data()}'!2";1,,{data()}'!2;[1 [[0 1 2] [0 1 2]]])


a::!10
k::3
c::(,,,1#a),k
t("c::(,,,1#a),k";c::(,,,1#a),k;[[[[0]]] 3])
7 changes: 0 additions & 7 deletions tests/test_known_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ def test_table_access_with_at(self):
r = klong('T@"col1"')
self.assertTrue(kg_equal(r, data['col1']))

@unittest.skip
def test_join_nested_array(self):
klong = KlongInterpreter()
r = klong("a::!10;k::3;c::(,,,1#a),k")
# currently this flattens to [[0], 3]
self.assertTrue(kg_equal(r,[[[[0]]],3]))

@unittest.skip
def test_extra_spaces(self):
klong = KlongInterpreter()
Expand Down

0 comments on commit 5456514

Please sign in to comment.