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

Try in Native Python #76

Merged
merged 2 commits into from
Dec 14, 2018
Merged
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
244 changes: 244 additions & 0 deletions notebooks/Native.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"from operator import mul\n",
"\n",
"from uarray.native.abstract import *\n",
"from uarray.native.moa import *\n",
"from uarray.native.helpers import *\n",
"from uarray.native.numpy import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def outer_then_index(a, b):\n",
" idx = AbstractArray.from_naturals(AbstractNaturals.from_value(5))\n",
" return index(idx, outer_product(a, b, mul, int))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"res = outer_then_index(iota(AbstractArray.from_value(10)), iota(AbstractArray.from_value(15)))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_numpy_array(res)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"a, b = np.arange(15), np.arange(15)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def outer_then_index_initial(a, b):\n",
" return np.multiply.outer(a, b)[5]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.01 µs ± 65.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit outer_then_index_initial(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def outer_then_index_np(a, b):\n",
" return to_numpy_array(outer_then_index(\n",
" AbstractArray.from_array(NumpyArray(a)),\n",
" AbstractArray.from_array(NumpyArray(b)),\n",
" ))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"413 µs ± 9.68 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit outer_then_index_np(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def outer_then_index_np_default(a, b):\n",
" return to_numpy_array(outer_then_index(\n",
" NumpyArray(a),\n",
" NumpyArray(b),\n",
" ))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"350 µs ± 14.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit outer_then_index_np_default(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"a, b = np.arange(10000), np.arange(10000)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"548 ms ± 3.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit outer_then_index_initial(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"258 ms ± 3.79 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit outer_then_index_np(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"777 ms ± 6.98 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit outer_then_index_np_default(a, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Empty file added uarray/native/__init__.py
Empty file.
72 changes: 72 additions & 0 deletions uarray/native/abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import typing
import dataclasses


from .typing import *
from .helpers import *

T = typing.TypeVar("T")


__all__ = ["AbstractNaturals", "AbstractArray"]


@dataclasses.dataclass
class AbstractNaturals(Naturals):
length: int
getitem: typing.Callable[[int], int]

def __len__(self) -> int:
return self.length

def __getitem__(self, index: int) -> int:
return self.getitem(index) # type: ignore

@classmethod
def empty(cls) -> "AbstractNaturals":
def getitem(x: int):
raise IndexError()

return cls(0, getitem)

@classmethod
def from_value(cls, x: int) -> "AbstractNaturals":
return cls(1, lambda _: x)

@classmethod
def from_array(cls, a: Array[int]) -> "AbstractNaturals":
assert is_vector(a)
return cls(u_shape(a)[0], lambda i: u_psi(a, cls.from_value(i)))

@classmethod
def from_sequence(cls, s: typing.Sequence[int]) -> "AbstractNaturals":
return cls(len(s), s.__getitem__)


@dataclasses.dataclass
class AbstractArray(Array[T]):
shape: Naturals
index_fn: Index[T]
mtype: typing.Type[T]

def __u_shape__(self) -> Naturals:
return self.shape

def __u_psi__(self, indices: Naturals) -> T:
return self.index_fn(indices) # type: ignore

def __u_mtype__(self) -> typing.Type[T]:
return self.mtype

@classmethod
def from_value(cls, x: T, mtype: typing.Type[T] = None) -> "AbstractArray[T]":
return AbstractArray(tuple(), lambda _: x, mtype or type(x))

@classmethod
def from_naturals(cls, xs: Naturals) -> "AbstractArray[int]":
return AbstractArray((len(xs),), lambda idxs: xs[idxs[0]], int)

@classmethod
def from_array(cls, a: Array[T]) -> "AbstractArray[T]":
return AbstractArray(u_shape(a), a.__u_psi__, a.__u_mtype__())

26 changes: 26 additions & 0 deletions uarray/native/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import typing

from .typing import *

__all__ = ["u_shape", "u_psi", "u_mtype", "is_scalar", "is_vector"]
T = typing.TypeVar("T")


def u_shape(a: Array[T]) -> Naturals:
return a.__u_shape__()


def u_psi(a: Array[T], indices: Naturals) -> T:
return a.__u_psi__(indices)


def u_mtype(a: Array[T]) -> typing.Type[T]:
return a.__u_mtype__()


def is_scalar(a: Array[T]):
return len(u_shape(a)) == 0


def is_vector(a: Array[T]):
return len(u_shape(a)) == 1
Loading