data_types.py 17.3 KB
Newer Older
1
import ctypes
2
import sympy as sp
3
import numpy as np
4

5
6
7
8
9
try:
    import llvmlite.ir as ir
except ImportError as e:
    ir = None
    _ir_importerror = e
10
from sympy.core.cache import cacheit
11

12
from pystencils.cache import memorycache
Martin Bauer's avatar
Martin Bauer committed
13
from pystencils.utils import all_equal
Martin Bauer's avatar
Martin Bauer committed
14
from sympy.logic.boolalg import Boolean
15

16

Martin Bauer's avatar
Martin Bauer committed
17
# noinspection PyPep8Naming
18
class cast_func(sp.Function):
19
    is_Atom = True
Martin Bauer's avatar
Martin Bauer committed
20

21
22
23
24
25
26
27
28
    def __new__(cls, *args, **kwargs):
        # to work in conditions of sp.Piecewise cast_func has to be of type Boolean as well
        # however, a cast_function should only be a boolean if its argument is a boolean, otherwise this leads
        # to problems when for example comparing cast_func's for equality
        #
        # lhs = bitwise_and(a, cast_func(1, 'int'))
        # rhs = cast_func(0, 'int')
        # print( sp.Ne(lhs, rhs) ) # would give true if all cast_funcs are booleans
Martin Bauer's avatar
Martin Bauer committed
29
        # -> thus a separate class boolean_cast_func is introduced
30
31
32
33
        if isinstance(args[0], Boolean):
            cls = boolean_cast_func
        return sp.Function.__new__(cls, *args, **kwargs)

34
35
36
37
38
39
    @property
    def canonical(self):
        if hasattr(self.args[0], 'canonical'):
            return self.args[0].canonical
        else:
            raise NotImplementedError()
40

41
42
43
44
    @property
    def is_commutative(self):
        return self.args[0].is_commutative

Martin Bauer's avatar
Martin Bauer committed
45
46
47
48
49
    @property
    def dtype(self):
        return self.args[1]


50
51
52
53
54
# noinspection PyPep8Naming
class boolean_cast_func(cast_func, Boolean):
    pass


Martin Bauer's avatar
Martin Bauer committed
55
56
57
58
# noinspection PyPep8Naming
class vector_memory_access(cast_func):
    nargs = (4,)

59

60
61
62
63
64
# noinspection PyPep8Naming
class reinterpret_cast_func(cast_func):
    pass


Martin Bauer's avatar
Martin Bauer committed
65
66
# noinspection PyPep8Naming
class pointer_arithmetic_func(sp.Function, Boolean):
67
68
69
70
71
72
73
74
    @property
    def canonical(self):
        if hasattr(self.args[0], 'canonical'):
            return self.args[0].canonical
        else:
            raise NotImplementedError()


75
class TypedSymbol(sp.Symbol):
Martin Bauer's avatar
Martin Bauer committed
76
77
    def __new__(cls, *args, **kwds):
        obj = TypedSymbol.__xnew_cached_(cls, *args, **kwds)
78
79
        return obj

80
    def __new_stage2__(cls, name, dtype):
81
        obj = super(TypedSymbol, cls).__xnew__(cls, name)
82
        try:
Martin Bauer's avatar
Martin Bauer committed
83
            obj._dtype = create_type(dtype)
84
85
86
        except TypeError:
            # on error keep the string
            obj._dtype = dtype
87
88
89
90
91
92
93
94
95
96
        return obj

    __xnew__ = staticmethod(__new_stage2__)
    __xnew_cached_ = staticmethod(cacheit(__new_stage2__))

    @property
    def dtype(self):
        return self._dtype

    def _hashable_content(self):
97
        return super()._hashable_content(), hash(self._dtype)
98
99

    def __getnewargs__(self):
100
101
102
        return self.name, self.dtype


Martin Bauer's avatar
Martin Bauer committed
103
def create_type(specification):
Martin Bauer's avatar
Martin Bauer committed
104
105
106
107
108
109
110
    """Creates a subclass of Type according to a string or an object of subclass Type.

    Args:
        specification: Type object, or a string

    Returns:
        Type object, or a new Type object parsed from the string
Jan Hoenig's avatar
Jan Hoenig committed
111
    """
112
113
114
    if isinstance(specification, Type):
        return specification
    else:
Martin Bauer's avatar
Martin Bauer committed
115
116
117
        numpy_dtype = np.dtype(specification)
        if numpy_dtype.fields is None:
            return BasicType(numpy_dtype, const=False)
118
        else:
Martin Bauer's avatar
Martin Bauer committed
119
            return StructType(numpy_dtype, const=False)
120
121


122
@memorycache(maxsize=64)
Martin Bauer's avatar
Martin Bauer committed
123
def create_composite_type_from_string(specification):
Martin Bauer's avatar
Martin Bauer committed
124
125
126
127
128
129
130
    """Creates a new Type object from a c-like string specification.

    Args:
        specification: Specification string

    Returns:
        Type object
Jan Hoenig's avatar
Jan Hoenig committed
131
    """
132
133
134
135
136
137
138
    specification = specification.lower().split()
    parts = []
    current = []
    for s in specification:
        if s == '*':
            parts.append(current)
            current = [s]
139
        else:
140
141
142
            current.append(s)
    if len(current) > 0:
        parts.append(current)
Jan Hoenig's avatar
Jan Hoenig committed
143
        # Parse native part
Martin Bauer's avatar
Martin Bauer committed
144
    base_part = parts.pop(0)
145
    const = False
Martin Bauer's avatar
Martin Bauer committed
146
    if 'const' in base_part:
147
        const = True
Martin Bauer's avatar
Martin Bauer committed
148
149
150
151
        base_part.remove('const')
    assert len(base_part) == 1
    if base_part[0][-1] == "*":
        base_part[0] = base_part[0][:-1]
Jan Hoenig's avatar
Jan Hoenig committed
152
        parts.append('*')
Martin Bauer's avatar
Martin Bauer committed
153
    current_type = BasicType(np.dtype(base_part[0]), const)
154
155
156
157
158
159
160
161
162
163
164
    # Parse pointer parts
    for part in parts:
        restrict = False
        const = False
        if 'restrict' in part:
            restrict = True
            part.remove('restrict')
        if 'const' in part:
            const = True
            part.remove("const")
        assert len(part) == 1 and part[0] == '*'
Martin Bauer's avatar
Martin Bauer committed
165
166
        current_type = PointerType(current_type, const, restrict)
    return current_type
167
168


Martin Bauer's avatar
Martin Bauer committed
169
170
171
172
def get_base_type(data_type):
    while data_type.base_type is not None:
        data_type = data_type.base_type
    return data_type
173
174


Martin Bauer's avatar
Martin Bauer committed
175
def to_ctypes(data_type):
Jan Hoenig's avatar
Jan Hoenig committed
176
177
    """
    Transforms a given Type into ctypes
Martin Bauer's avatar
Martin Bauer committed
178
    :param data_type: Subclass of Type
Jan Hoenig's avatar
Jan Hoenig committed
179
180
    :return: ctypes type object
    """
Martin Bauer's avatar
Martin Bauer committed
181
182
183
    if isinstance(data_type, PointerType):
        return ctypes.POINTER(to_ctypes(data_type.base_type))
    elif isinstance(data_type, StructType):
184
        return ctypes.POINTER(ctypes.c_uint8)
185
    else:
Martin Bauer's avatar
Martin Bauer committed
186
        return to_ctypes.map[data_type.numpy_dtype]
187

Martin Bauer's avatar
Martin Bauer committed
188
189

to_ctypes.map = {
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    np.dtype(np.int8): ctypes.c_int8,
    np.dtype(np.int16): ctypes.c_int16,
    np.dtype(np.int32): ctypes.c_int32,
    np.dtype(np.int64): ctypes.c_int64,

    np.dtype(np.uint8): ctypes.c_uint8,
    np.dtype(np.uint16): ctypes.c_uint16,
    np.dtype(np.uint32): ctypes.c_uint32,
    np.dtype(np.uint64): ctypes.c_uint64,

    np.dtype(np.float32): ctypes.c_float,
    np.dtype(np.float64): ctypes.c_double,
}


205
def ctypes_from_llvm(data_type):
206
207
    if not ir:
        raise _ir_importerror
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    if isinstance(data_type, ir.PointerType):
        ctype = ctypes_from_llvm(data_type.pointee)
        if ctype is None:
            return ctypes.c_void_p
        else:
            return ctypes.POINTER(ctype)
    elif isinstance(data_type, ir.IntType):
        if data_type.width == 8:
            return ctypes.c_int8
        elif data_type.width == 16:
            return ctypes.c_int16
        elif data_type.width == 32:
            return ctypes.c_int32
        elif data_type.width == 64:
            return ctypes.c_int64
        else:
            raise ValueError("Int width %d is not supported" % data_type.width)
    elif isinstance(data_type, ir.FloatType):
        return ctypes.c_float
    elif isinstance(data_type, ir.DoubleType):
        return ctypes.c_double
    elif isinstance(data_type, ir.VoidType):
        return None  # Void type is not supported by ctypes
    else:
        raise NotImplementedError('Data type %s of %s is not supported yet' % (type(data_type), data_type))


def to_llvm_type(data_type):
    """
    Transforms a given type into ctypes
    :param data_type: Subclass of Type
    :return: llvmlite type object
    """
241
242
    if not ir:
        raise _ir_importerror
243
    if isinstance(data_type, PointerType):
Martin Bauer's avatar
Martin Bauer committed
244
        return to_llvm_type(data_type.base_type).as_pointer()
245
    else:
Martin Bauer's avatar
Martin Bauer committed
246
247
        return to_llvm_type.map[data_type.numpy_dtype]

248

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
if ir:
    to_llvm_type.map = {
        np.dtype(np.int8): ir.IntType(8),
        np.dtype(np.int16): ir.IntType(16),
        np.dtype(np.int32): ir.IntType(32),
        np.dtype(np.int64): ir.IntType(64),

        np.dtype(np.uint8): ir.IntType(8),
        np.dtype(np.uint16): ir.IntType(16),
        np.dtype(np.uint32): ir.IntType(32),
        np.dtype(np.uint64): ir.IntType(64),

        np.dtype(np.float32): ir.FloatType(),
        np.dtype(np.float64): ir.DoubleType(),
    }
264

265

Martin Bauer's avatar
Martin Bauer committed
266
267
268
def peel_off_type(dtype, type_to_peel_off):
    while type(dtype) is type_to_peel_off:
        dtype = dtype.base_type
269
270
271
    return dtype


Martin Bauer's avatar
Martin Bauer committed
272
def collate_types(types):
273
274
275
276
277
278
279
    """
    Takes a sequence of types and returns their "common type" e.g. (float, double, float) -> double
    Uses the collation rules from numpy.
    """

    # Pointer arithmetic case i.e. pointer + integer is allowed
    if any(type(t) is PointerType for t in types):
Martin Bauer's avatar
Martin Bauer committed
280
        pointer_type = None
281
282
        for t in types:
            if type(t) is PointerType:
Martin Bauer's avatar
Martin Bauer committed
283
                if pointer_type is not None:
284
                    raise ValueError("Cannot collate the combination of two pointer types")
Martin Bauer's avatar
Martin Bauer committed
285
                pointer_type = t
286
287
288
289
290
            elif type(t) is BasicType:
                if not (t.is_int() or t.is_uint()):
                    raise ValueError("Invalid pointer arithmetic")
            else:
                raise ValueError("Invalid pointer arithmetic")
Martin Bauer's avatar
Martin Bauer committed
291
        return pointer_type
292
293

    # peel of vector types, if at least one vector type occurred the result will also be the vector type
Martin Bauer's avatar
Martin Bauer committed
294
    vector_type = [t for t in types if type(t) is VectorType]
Martin Bauer's avatar
Martin Bauer committed
295
    if not all_equal(t.width for t in vector_type):
296
        raise ValueError("Collation failed because of vector types with different width")
Martin Bauer's avatar
Martin Bauer committed
297
    types = [peel_off_type(t, VectorType) for t in types]
298
299
300
301

    # now we should have a list of basic types - struct types are not yet supported
    assert all(type(t) is BasicType for t in types)

302
303
    if any(t.is_float() for t in types):
        types = tuple(t for t in types if t.is_float())
304
    # use numpy collation -> create type from numpy type -> and, put vector type around if necessary
Martin Bauer's avatar
Martin Bauer committed
305
306
307
308
    result_numpy_type = np.result_type(*(t.numpy_dtype for t in types))
    result = BasicType(result_numpy_type)
    if vector_type:
        result = VectorType(result, vector_type[0].width)
309
310
311
312
    return result


@memorycache(maxsize=2048)
Martin Bauer's avatar
Martin Bauer committed
313
def get_type_of_expression(expr):
314
    from pystencils.astnodes import ResolvedFieldAccess
315
316
    from pystencils.cpu.vectorization import vec_all, vec_any

317
318
    expr = sp.sympify(expr)
    if isinstance(expr, sp.Integer):
319
        return create_type("int")
320
    elif isinstance(expr, sp.Rational) or isinstance(expr, sp.Float):
Martin Bauer's avatar
Martin Bauer committed
321
        return create_type("double")
322
323
324
    elif isinstance(expr, ResolvedFieldAccess):
        return expr.field.dtype
    elif isinstance(expr, TypedSymbol):
325
        return expr.dtype
326
    elif isinstance(expr, sp.Symbol):
327
        raise ValueError("All symbols inside this expression have to be typed! ", str(expr))
Martin Bauer's avatar
Martin Bauer committed
328
    elif isinstance(expr, cast_func):
329
        return expr.args[1]
330
331
    elif isinstance(expr, vec_any) or isinstance(expr, vec_all):
        return create_type("bool")
332
    elif hasattr(expr, 'func') and expr.func == sp.Piecewise:
Martin Bauer's avatar
Martin Bauer committed
333
334
335
336
337
        collated_result_type = collate_types(tuple(get_type_of_expression(a[0]) for a in expr.args))
        collated_condition_type = collate_types(tuple(get_type_of_expression(a[1]) for a in expr.args))
        if type(collated_condition_type) is VectorType and type(collated_result_type) is not VectorType:
            collated_result_type = VectorType(collated_result_type, width=collated_condition_type.width)
        return collated_result_type
338
    elif isinstance(expr, sp.Indexed):
Martin Bauer's avatar
Martin Bauer committed
339
340
        typed_symbol = expr.base.label
        return typed_symbol.dtype.base_type
341
    elif isinstance(expr, sp.boolalg.Boolean) or isinstance(expr, sp.boolalg.BooleanFunction):
342
        # if any arg is of vector type return a vector boolean, else return a normal scalar boolean
Martin Bauer's avatar
Martin Bauer committed
343
344
345
346
        result = create_type("bool")
        vec_args = [get_type_of_expression(a) for a in expr.args if isinstance(get_type_of_expression(a), VectorType)]
        if vec_args:
            result = VectorType(result, width=vec_args[0].width)
347
        return result
348
349
    elif isinstance(expr, sp.Pow):
        return get_type_of_expression(expr.args[0])
350
    elif isinstance(expr, sp.Expr):
Martin Bauer's avatar
Martin Bauer committed
351
352
        types = tuple(get_type_of_expression(a) for a in expr.args)
        return collate_types(types)
353

354
    raise NotImplementedError("Could not determine type for", expr, type(expr))
355
356


357
class Type(sp.Basic):
Martin Bauer's avatar
Martin Bauer committed
358
359
    is_Atom = True

360
361
    def __new__(cls, *args, **kwargs):
        return sp.Basic.__new__(cls)
362

363
364
    def _sympystr(self, *args, **kwargs):
        return str(self)
365
366
367
368


class BasicType(Type):
    @staticmethod
Martin Bauer's avatar
Martin Bauer committed
369
    def numpy_name_to_c(name):
Jan Hoenig's avatar
Jan Hoenig committed
370
371
372
373
        if name == 'float64':
            return 'double'
        elif name == 'float32':
            return 'float'
374
375
376
377
        elif name.startswith('int'):
            width = int(name[len("int"):])
            return "int%d_t" % (width,)
        elif name.startswith('uint'):
378
            width = int(name[len("uint"):])
379
380
381
382
            return "uint%d_t" % (width,)
        elif name == 'bool':
            return 'bool'
        else:
Martin Bauer's avatar
Martin Bauer committed
383
            raise NotImplementedError("Can map numpy to C name for %s" % (name,))
384
385
386

    def __init__(self, dtype, const=False):
        self.const = const
387
        if isinstance(dtype, Type):
Martin Bauer's avatar
Martin Bauer committed
388
            self._dtype = dtype.numpy_dtype
389
390
        else:
            self._dtype = np.dtype(dtype)
391
392
393
394
        assert self._dtype.fields is None, "Tried to initialize NativeType with a structured type"
        assert self._dtype.hasobject is False
        assert self._dtype.subdtype is None

395
    def __getnewargs__(self):
Martin Bauer's avatar
Martin Bauer committed
396
        return self.numpy_dtype, self.const
397

398
    @property
Martin Bauer's avatar
Martin Bauer committed
399
    def base_type(self):
400
        return None
401

402
    @property
Martin Bauer's avatar
Martin Bauer committed
403
    def numpy_dtype(self):
404
405
        return self._dtype

406
    @property
Martin Bauer's avatar
Martin Bauer committed
407
    def item_size(self):
408
409
        return 1

410
    def is_int(self):
Martin Bauer's avatar
Martin Bauer committed
411
        return self.numpy_dtype in np.sctypes['int']
412
413

    def is_float(self):
Martin Bauer's avatar
Martin Bauer committed
414
        return self.numpy_dtype in np.sctypes['float']
415
416

    def is_uint(self):
Martin Bauer's avatar
Martin Bauer committed
417
        return self.numpy_dtype in np.sctypes['uint']
418

Martin Bauer's avatar
Martin Bauer committed
419
420
    def is_complex(self):
        return self.numpy_dtype in np.sctypes['complex']
421
422

    def is_other(self):
Martin Bauer's avatar
Martin Bauer committed
423
        return self.numpy_dtype in np.sctypes['others']
424

425
    @property
Martin Bauer's avatar
Martin Bauer committed
426
427
    def base_name(self):
        return BasicType.numpy_name_to_c(str(self._dtype))
428

Jan Hoenig's avatar
Jan Hoenig committed
429
    def __str__(self):
Martin Bauer's avatar
Martin Bauer committed
430
        result = BasicType.numpy_name_to_c(str(self._dtype))
431
432
433
        if self.const:
            result += " const"
        return result
Jan Hoenig's avatar
Jan Hoenig committed
434

435
436
437
    def __repr__(self):
        return str(self)

Jan Hoenig's avatar
Jan Hoenig committed
438
    def __eq__(self, other):
439
440
        if not isinstance(other, BasicType):
            return False
Jan Hoenig's avatar
Jan Hoenig committed
441
        else:
Martin Bauer's avatar
Martin Bauer committed
442
            return (self.numpy_dtype, self.const) == (other.numpy_dtype, other.const)
443
444
445
446
447

    def __hash__(self):
        return hash(str(self))


448
class VectorType(Type):
Martin Bauer's avatar
Martin Bauer committed
449
    instruction_set = None
450

Martin Bauer's avatar
Martin Bauer committed
451
452
    def __init__(self, base_type, width=4):
        self._base_type = base_type
453
454
455
        self.width = width

    @property
Martin Bauer's avatar
Martin Bauer committed
456
457
    def base_type(self):
        return self._base_type
458
459

    @property
Martin Bauer's avatar
Martin Bauer committed
460
461
    def item_size(self):
        return self.width * self.base_type.item_size
462
463
464
465
466

    def __eq__(self, other):
        if not isinstance(other, VectorType):
            return False
        else:
Martin Bauer's avatar
Martin Bauer committed
467
            return (self.base_type, self.width) == (other.base_type, other.width)
468
469

    def __str__(self):
Martin Bauer's avatar
Martin Bauer committed
470
        if self.instruction_set is None:
Martin Bauer's avatar
Martin Bauer committed
471
            return "%s[%d]" % (self.base_type, self.width)
472
        else:
Martin Bauer's avatar
Martin Bauer committed
473
            if self.base_type == create_type("int64"):
Martin Bauer's avatar
Martin Bauer committed
474
                return self.instruction_set['int']
Martin Bauer's avatar
Martin Bauer committed
475
            elif self.base_type == create_type("float64"):
Martin Bauer's avatar
Martin Bauer committed
476
                return self.instruction_set['double']
Martin Bauer's avatar
Martin Bauer committed
477
            elif self.base_type == create_type("float32"):
Martin Bauer's avatar
Martin Bauer committed
478
                return self.instruction_set['float']
Martin Bauer's avatar
Martin Bauer committed
479
            elif self.base_type == create_type("bool"):
Martin Bauer's avatar
Martin Bauer committed
480
                return self.instruction_set['bool']
481
482
483
484
            else:
                raise NotImplementedError()

    def __hash__(self):
Martin Bauer's avatar
Martin Bauer committed
485
        return hash((self.base_type, self.width))
486

Martin Bauer's avatar
Martin Bauer committed
487
488
489
    def __getnewargs__(self):
        return self._base_type, self.width

490

491
class PointerType(Type):
Martin Bauer's avatar
Martin Bauer committed
492
493
    def __init__(self, base_type, const=False, restrict=True):
        self._base_type = base_type
494
495
496
        self.const = const
        self.restrict = restrict

497
    def __getnewargs__(self):
Martin Bauer's avatar
Martin Bauer committed
498
        return self.base_type, self.const, self.restrict
499

500
501
502
503
504
    @property
    def alias(self):
        return not self.restrict

    @property
Martin Bauer's avatar
Martin Bauer committed
505
506
    def base_type(self):
        return self._base_type
507

508
    @property
Martin Bauer's avatar
Martin Bauer committed
509
510
    def item_size(self):
        return self.base_type.item_size
511

512
513
    def __eq__(self, other):
        if not isinstance(other, PointerType):
Jan Hoenig's avatar
Jan Hoenig committed
514
            return False
515
        else:
Martin Bauer's avatar
Martin Bauer committed
516
            return (self.base_type, self.const, self.restrict) == (other.base_type, other.const, other.restrict)
517

Jan Hoenig's avatar
Jan Hoenig committed
518
    def __str__(self):
519
520
521
522
523
524
        components = [str(self.base_type), '*']
        if self.restrict:
            components.append('RESTRICT')
        if self.const:
            components.append("const")
        return " ".join(components)
525

526
527
528
    def __repr__(self):
        return str(self)

529
    def __hash__(self):
Martin Bauer's avatar
Martin Bauer committed
530
        return hash((self._base_type, self.const, self.restrict))
531

Jan Hoenig's avatar
Jan Hoenig committed
532

533
class StructType:
Martin Bauer's avatar
Martin Bauer committed
534
    def __init__(self, numpy_type, const=False):
535
        self.const = const
Martin Bauer's avatar
Martin Bauer committed
536
        self._dtype = np.dtype(numpy_type)
Martin Bauer's avatar
Martin Bauer committed
537

538
    def __getnewargs__(self):
Martin Bauer's avatar
Martin Bauer committed
539
        return self.numpy_dtype, self.const
540

541
    @property
Martin Bauer's avatar
Martin Bauer committed
542
    def base_type(self):
543
544
545
        return None

    @property
Martin Bauer's avatar
Martin Bauer committed
546
    def numpy_dtype(self):
547
548
549
        return self._dtype

    @property
Martin Bauer's avatar
Martin Bauer committed
550
551
    def item_size(self):
        return self.numpy_dtype.itemsize
552

Martin Bauer's avatar
Martin Bauer committed
553
554
    def get_element_offset(self, element_name):
        return self.numpy_dtype.fields[element_name][1]
555

Martin Bauer's avatar
Martin Bauer committed
556
557
558
    def get_element_type(self, element_name):
        np_element_type = self.numpy_dtype.fields[element_name][0]
        return BasicType(np_element_type, self.const)
559

Martin Bauer's avatar
Martin Bauer committed
560
561
    def has_element(self, element_name):
        return element_name in self.numpy_dtype.fields
562

563
564
565
566
    def __eq__(self, other):
        if not isinstance(other, StructType):
            return False
        else:
Martin Bauer's avatar
Martin Bauer committed
567
            return (self.numpy_dtype, self.const) == (other.numpy_dtype, other.const)
568
569
570
571
572
573
574
575

    def __str__(self):
        # structs are handled byte-wise
        result = "uint8_t"
        if self.const:
            result += " const"
        return result

576
577
578
    def __repr__(self):
        return str(self)

579
    def __hash__(self):
Martin Bauer's avatar
Martin Bauer committed
580
        return hash((self.numpy_dtype, self.const))