2020-05-02 01:38:52 +08:00
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
2020-05-09 08:36:07 +08:00
|
|
|
import numpy as np
|
2020-05-07 09:24:51 +08:00
|
|
|
from npcomp.dialect import Basicpy
|
2020-05-07 13:44:12 +08:00
|
|
|
from _npcomp.mlir import ir
|
2020-05-02 01:38:52 +08:00
|
|
|
|
|
|
|
__all__ = [
|
2020-05-09 07:04:58 +08:00
|
|
|
"load_builtin_module",
|
|
|
|
"DialectHelper",
|
2020-05-02 01:38:52 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-05-07 09:24:51 +08:00
|
|
|
class DialectHelper(Basicpy.DialectHelper):
|
2020-06-09 07:10:51 +08:00
|
|
|
r"""Dialect helper.
|
2020-06-29 09:42:08 +08:00
|
|
|
|
2020-05-02 09:44:06 +08:00
|
|
|
>>> c = ir.MLIRContext()
|
2020-06-09 07:10:51 +08:00
|
|
|
>>> h = DialectHelper(c, ir.OpBuilder(c))
|
2020-05-07 07:59:41 +08:00
|
|
|
|
2020-05-09 08:36:07 +08:00
|
|
|
DenseElementsAttrs:
|
2020-07-02 12:28:04 +08:00
|
|
|
>>> c.dense_elements_attr(np.asarray([1, 2, 3, 4], dtype=np.int32))
|
|
|
|
dense<[1, 2, 3, 4]> : tensor<4xsi32>
|
|
|
|
>>> c.dense_elements_attr(np.asarray([[1, 2], [3, 4]], dtype=np.int32))
|
|
|
|
dense<[[1, 2], [3, 4]]> : tensor<2x2xsi32>
|
2020-05-09 08:36:07 +08:00
|
|
|
>>> c.dense_elements_attr(np.asarray([[1., 2.], [3., 4.]]))
|
|
|
|
dense<[[1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00]]> : tensor<2x2xf64>
|
|
|
|
>>> c.dense_elements_attr(np.asarray([[1., 2.], [3., 4.]], dtype=np.float32))
|
|
|
|
dense<[[1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00]]> : tensor<2x2xf32>
|
|
|
|
|
2020-05-07 07:59:41 +08:00
|
|
|
Types:
|
2020-06-09 07:10:51 +08:00
|
|
|
>>> c = ir.MLIRContext()
|
|
|
|
>>> t = DialectHelper(c, ir.OpBuilder(c))
|
2020-05-07 07:59:41 +08:00
|
|
|
>>> t.numpy_any_dtype
|
2020-06-30 06:27:39 +08:00
|
|
|
!basicpy.UnknownType
|
2020-05-07 07:59:41 +08:00
|
|
|
>>> t.tensor_type(t.numpy_any_dtype, [1, 2, 3])
|
2020-06-30 06:27:39 +08:00
|
|
|
tensor<1x2x3x!basicpy.UnknownType>
|
2020-05-07 07:59:41 +08:00
|
|
|
>>> t.tensor_type(t.numpy_any_dtype)
|
2020-06-30 06:27:39 +08:00
|
|
|
tensor<*x!basicpy.UnknownType>
|
2020-05-07 07:59:41 +08:00
|
|
|
>>> t.tensor_type(t.numpy_any_dtype, [-1, 2])
|
2020-06-30 06:27:39 +08:00
|
|
|
tensor<?x2x!basicpy.UnknownType>
|
2020-05-07 07:59:41 +08:00
|
|
|
>>> t.tensor_type(t.f32_type)
|
|
|
|
tensor<*xf32>
|
|
|
|
>>> t.function_type([t.i32_type], [t.f32_type])
|
|
|
|
(i32) -> f32
|
2020-06-30 07:19:03 +08:00
|
|
|
>>> t.numpy_unknown_tensor_type
|
2020-06-30 06:27:39 +08:00
|
|
|
tensor<*x!basicpy.UnknownType>
|
2020-05-07 07:59:41 +08:00
|
|
|
|
2020-05-02 09:44:06 +08:00
|
|
|
"""
|
2020-06-14 05:53:54 +08:00
|
|
|
|
2020-06-09 07:10:51 +08:00
|
|
|
@property
|
|
|
|
def numpy_any_dtype(self):
|
2020-06-30 06:27:39 +08:00
|
|
|
return self.basicpy_UnknownType
|
|
|
|
|
|
|
|
@property
|
|
|
|
def numpy_unknown_tensor_type(self):
|
|
|
|
return self.tensor_type(self.basicpy_UnknownType)
|
2020-06-09 07:10:51 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unknown_array_type(self):
|
2020-06-30 06:27:39 +08:00
|
|
|
return self.numpy_NdArrayType(self.basicpy_UnknownType)
|
|
|
|
|
|
|
|
def numpy_builtin_ufunc_call_op(self, *args, qualified_name, result_type):
|
|
|
|
"""Creates a numpy.builtin_ufunc_call op."""
|
|
|
|
c = self.context
|
|
|
|
attrs = c.dictionary_attr({"qualified_name": c.string_attr(qualified_name)})
|
|
|
|
return self.op("numpy.builtin_ufunc_call", [result_type], args, attrs)
|
2020-05-09 07:04:58 +08:00
|
|
|
|
2020-06-09 07:10:51 +08:00
|
|
|
def numpy_narrow_op(self, result_type, operand):
|
|
|
|
"""Creates a numpy.narrow op."""
|
|
|
|
return self.op("numpy.narrow", [result_type], [operand])
|
2020-05-02 09:44:06 +08:00
|
|
|
|
2020-06-09 07:10:51 +08:00
|
|
|
def numpy_get_slice_op(self, result_type, array, *slice_elements):
|
|
|
|
return self.op("numpy.get_slice", [result_type],
|
|
|
|
[array] + list(slice_elements))
|
2020-05-03 10:52:21 +08:00
|
|
|
|
2020-05-02 09:44:06 +08:00
|
|
|
|
2020-05-02 01:38:52 +08:00
|
|
|
if __name__ == "__main__":
|
2020-06-09 07:10:51 +08:00
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|