• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daisytuner / docc / 28688078630

03 Jul 2026 11:40PM UTC coverage: 62.472% (+0.3%) from 62.147%
28688078630

Pull #832

github

web-flow
Merge 57652ca2e into 3726be1d9
Pull Request #832: activates numpy tests

88 of 98 new or added lines in 2 files covered. (89.8%)

25 existing lines in 2 files now uncovered.

39755 of 63637 relevant lines covered (62.47%)

977.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.74
/python/docc/python/python_program.py
1
import inspect
4✔
2
import shutil
4✔
3
import textwrap
4✔
4
import ast
4✔
5
import os
4✔
6
import getpass
4✔
7
import hashlib
4✔
8
import ml_dtypes
4✔
9
import numpy as np
4✔
10
from typing import Annotated, get_origin, get_args, Any, Optional
4✔
11

12
from docc.sdfg import (
4✔
13
    Scalar,
14
    PrimitiveType,
15
    Pointer,
16
    Structure,
17
    Array,
18
    Type,
19
    Tensor,
20
    StructuredSDFG,
21
    StructuredSDFGBuilder,
22
)
23
from docc.compiler.docc_program import DoccProgram
4✔
24
from docc.compiler.compiled_sdfg import CompiledSDFG
4✔
25
from docc.python.ast_parser import ASTParser
4✔
26
from docc.python.types import element_type_from_sdfg_type
4✔
27

28

29
def _compile_wrapper(self, output_folder=None):
4✔
30
    """Wrapper to allow StructuredSDFG.compile() to return a CompiledSDFG."""
31
    lib_path = self._compile(output_folder)
×
32
    return CompiledSDFG(lib_path, self)
×
33

34

35
# Monkey-patch StructuredSDFG to add compile method
36
StructuredSDFG.compile = _compile_wrapper
4✔
37

38

39
def _map_python_type(dtype):
4✔
40
    """Map Python/numpy types to SDFG types."""
41
    # If it is already a sdfg Type, return it
42
    if isinstance(dtype, Type):
4✔
43
        return dtype
×
44

45
    # Handle Annotated for Arrays
46
    if get_origin(dtype) is Annotated:
4✔
UNCOV
47
        args = get_args(dtype)
×
UNCOV
48
        base_type = args[0]
×
UNCOV
49
        metadata = args[1:]
×
50

UNCOV
51
        if base_type is np.ndarray:
×
52
            # Convention: Annotated[np.ndarray, shape, dtype]
UNCOV
53
            shape = metadata[0]
×
UNCOV
54
            elem_type = Scalar(PrimitiveType.Double)  # Default
×
55

UNCOV
56
            if len(metadata) > 1:
×
UNCOV
57
                possible_dtype = metadata[1]
×
UNCOV
58
                elem_type = _map_python_type(possible_dtype)
×
59

UNCOV
60
            return Pointer(elem_type)
×
61

62
    # Handle numpy.ndarray[Shape, DType]
63
    if get_origin(dtype) is np.ndarray:
4✔
64
        args = get_args(dtype)
×
65
        # args[0] is shape, args[1] is dtype
66
        if len(args) >= 2:
×
67
            elem_type = _map_python_type(args[1])
×
68
            return Pointer(elem_type)
×
69

70
    # Simple mapping for python types
71
    if dtype is float or dtype is np.float64:
4✔
72
        return Scalar(PrimitiveType.Double)
4✔
73
    elif dtype is np.float32:
4✔
74
        return Scalar(PrimitiveType.Float)
×
75
    elif dtype is bool or dtype is np.bool_:
4✔
76
        return Scalar(PrimitiveType.Bool)
4✔
77
    elif dtype is int or dtype is np.int64:
4✔
78
        return Scalar(PrimitiveType.Int64)
4✔
UNCOV
79
    elif dtype is np.int32:
×
UNCOV
80
        return Scalar(PrimitiveType.Int32)
×
81
    elif dtype is np.int16:
×
82
        return Scalar(PrimitiveType.Int16)
×
83
    elif dtype is np.int8:
×
84
        return Scalar(PrimitiveType.Int8)
×
85
    elif dtype is np.uint64:
×
86
        return Scalar(PrimitiveType.UInt64)
×
87
    elif dtype is np.uint32:
×
88
        return Scalar(PrimitiveType.UInt32)
×
89
    elif dtype is np.uint16:
×
90
        return Scalar(PrimitiveType.UInt16)
×
91
    elif dtype is np.uint8:
×
92
        return Scalar(PrimitiveType.UInt8)
×
93

94
    # Handle Python classes - map to Structure type
95
    if inspect.isclass(dtype):
×
96
        # Use the class name as the structure name
97
        return Pointer(Structure(dtype.__name__))
×
98

99
    return dtype
×
100

101

102
class PythonProgram(DoccProgram):
4✔
103

104
    def __init__(
4✔
105
        self,
106
        func,
107
        target: str = "none",
108
        category: str = "server",
109
        instrumentation_mode: Optional[str] = None,
110
        capture_args: Optional[bool] = None,
111
        remote_tuning: bool = False,
112
    ):
113
        super().__init__(
4✔
114
            name=func.__name__,
115
            target=target,
116
            category=category,
117
            instrumentation_mode=instrumentation_mode,
118
            capture_args=capture_args,
119
            remote_tuning=remote_tuning,
120
        )
121
        self.func = func
4✔
122
        self._last_structure_member_info = {}
4✔
123

124
    def __call__(self, *args: Any) -> Any:
4✔
125
        # JIT compile and run. CompiledSDFG validates the call mode (numpy /
126
        # cupy / torch) and rejects GPU arrays on non-device-resident artifacts.
127
        compiled = self.compile(*args)
4✔
128
        res = compiled(*args)
4✔
129

130
        # Handle return value conversion based on annotation
131
        sig = inspect.signature(self.func)
4✔
132
        ret_annotation = sig.return_annotation
4✔
133

134
        if ret_annotation is not inspect.Signature.empty:
4✔
135
            if get_origin(ret_annotation) is Annotated:
4✔
UNCOV
136
                type_args = get_args(ret_annotation)
×
UNCOV
137
                if len(type_args) >= 1 and type_args[0] is np.ndarray:
×
UNCOV
138
                    shape = None
×
UNCOV
139
                    if len(type_args) >= 2:
×
UNCOV
140
                        shape = type_args[1]
×
141

UNCOV
142
                    if shape is not None:
×
UNCOV
143
                        try:
×
UNCOV
144
                            return np.ctypeslib.as_array(res, shape=shape)
×
145
                        except Exception:
×
146
                            pass
×
147

148
        # Try to infer return shape from metadata
149
        if hasattr(compiled, "get_return_shape"):
4✔
150
            shape = compiled.get_return_shape(*args)
4✔
151
            if shape is not None:
4✔
152
                try:
×
153
                    return np.ctypeslib.as_array(res, shape=shape)
×
154
                except Exception:
×
155
                    pass
×
156

157
        return res
4✔
158

159
    def compile(
4✔
160
        self,
161
        *args: Any,
162
        output_folder: Optional[str] = None,
163
        instrumentation_mode: Optional[str] = None,
164
        capture_args: Optional[bool] = None,
165
        remote_tuning: Optional[bool] = None,
166
    ) -> CompiledSDFG:
167
        original_output_folder = output_folder
4✔
168

169
        # Resolve options
170
        instrumentation_mode, capture_args, remote_tuning = (
4✔
171
            self._resolve_compile_options(
172
                instrumentation_mode, capture_args, remote_tuning
173
            )
174
        )
175

176
        # 1. Analyze arguments and shapes
177
        arg_types = []
4✔
178
        shape_values = []  # List of unique shape values found
4✔
179
        shape_sources = []  # List of (arg_idx, dim_idx) for each unique shape value
4✔
180

181
        # Mapping from (arg_idx, dim_idx) -> unique_shape_idx
182
        arg_shape_mapping = {}
4✔
183

184
        # First pass: collect scalar integer arguments and their values
185
        sig = inspect.signature(self.func)
4✔
186
        params = list(sig.parameters.items())
4✔
187
        scalar_int_params = {}  # Maps value -> parameter name (first one wins)
4✔
188
        for i, ((name, param), arg) in enumerate(zip(params, args)):
4✔
189
            if isinstance(arg, (int, np.integer)) and not isinstance(
4✔
190
                arg, (bool, np.bool_)
191
            ):
192
                val = int(arg)
4✔
193
                if val not in scalar_int_params:
4✔
194
                    scalar_int_params[val] = name
4✔
195

196
        for i, arg in enumerate(args):
4✔
197
            t = self._infer_type(arg)
4✔
198
            arg_types.append(t)
4✔
199

200
            if isinstance(arg, np.ndarray):
4✔
201
                for dim_idx, dim_val in enumerate(arg.shape):
4✔
202
                    # Check if we've seen this value
203
                    if dim_val in shape_values:
4✔
204
                        # Reuse
205
                        u_idx = shape_values.index(dim_val)
4✔
206
                    else:
207
                        # New
208
                        u_idx = len(shape_values)
4✔
209
                        shape_values.append(dim_val)
4✔
210
                        shape_sources.append((i, dim_idx))
4✔
211

212
                    arg_shape_mapping[(i, dim_idx)] = u_idx
4✔
213

214
        # 2. Signature - include scalar-shape equivalences for correct caching
215
        mapping_sig = sorted(arg_shape_mapping.items())
4✔
216
        type_sig = ", ".join(self._type_to_str(t) for t in arg_types)
4✔
217
        signature = f"{type_sig}|{mapping_sig}"
4✔
218

219
        if output_folder is None:
4✔
220
            source_path = inspect.getsourcefile(self.func)
4✔
221
            hash_input = f"{source_path}|{self.name}|{self.target}|{self.category}|{self.capture_args}|{self.instrumentation_mode}|{signature}".encode(
4✔
222
                "utf-8"
223
            )
224
            stable_id = hashlib.sha256(hash_input).hexdigest()[:16]
4✔
225
            filename = os.path.basename(inspect.getsourcefile(self.func))
4✔
226

227
            docc_tmp = os.environ.get("DOCC_TMP")
4✔
228
            if docc_tmp:
4✔
229
                output_folder = (
×
230
                    f"{docc_tmp}/{filename}-{self.name}-{self.target}-{stable_id}"
231
                )
232
            else:
233
                user = os.getenv("USER")
4✔
234
                if not user:
4✔
235
                    user = getpass.getuser()
4✔
236
                output_folder = f"/tmp/{user}/DOCC/{self.name}-{stable_id}"
4✔
237

238
        if original_output_folder is None and signature in self.cache:
4✔
239
            return self.cache[signature]
4✔
240

241
        # 3. Build SDFG
242
        if os.path.exists(output_folder):
4✔
243
            # Multiple python processes running the same code?
244
            shutil.rmtree(output_folder)
4✔
245
        sdfg, out_args, out_shapes, out_strides = self._build_sdfg(
4✔
246
            arg_types, args, arg_shape_mapping, shape_values
247
        )
248

249
        lib_path = self.sdfg_pipe(
4✔
250
            sdfg, output_folder, instrumentation_mode, capture_args, remote_tuning
251
        )
252

253
        # 4. Create CompiledSDFG
254
        compiled = CompiledSDFG(
4✔
255
            lib_path,
256
            sdfg,
257
            shape_sources,
258
            self._last_structure_member_info,
259
            out_args,
260
            out_shapes,
261
            out_strides,
262
            device_resident=self._device_resident,
263
            device_backend=self._device_backend,
264
            target=self.target,
265
        )
266

267
        # Cache if using default output folder
268
        if original_output_folder is None:
4✔
269
            self.cache[signature] = compiled
4✔
270

271
        return compiled
4✔
272

273
    def to_sdfg(self, *args: Any) -> StructuredSDFG:
4✔
274
        arg_types = [self._infer_type(arg) for arg in args]
×
275

276
        # Build shape mapping
277
        shape_values = []
×
278
        shape_sources = []
×
279
        arg_shape_mapping = {}
×
280

281
        sig = inspect.signature(self.func)
×
282
        params = list(sig.parameters.items())
×
283
        scalar_int_params = {}
×
284
        for i, ((name, param), arg) in enumerate(zip(params, args)):
×
285
            if isinstance(arg, (int, np.integer)) and not isinstance(
×
286
                arg, (bool, np.bool_)
287
            ):
288
                val = int(arg)
×
289
                if val not in scalar_int_params:
×
290
                    scalar_int_params[val] = name
×
291

292
        for i, arg in enumerate(args):
×
293
            if isinstance(arg, np.ndarray):
×
294
                for dim_idx, dim_val in enumerate(arg.shape):
×
295
                    if dim_val in shape_values:
×
296
                        u_idx = shape_values.index(dim_val)
×
297
                    else:
298
                        u_idx = len(shape_values)
×
299
                        shape_values.append(dim_val)
×
300
                        shape_sources.append((i, dim_idx))
×
301
                    arg_shape_mapping[(i, dim_idx)] = u_idx
×
302

303
        sdfg, _, _, _ = self._build_sdfg(
×
304
            arg_types, args, arg_shape_mapping, shape_values
305
        )
306
        return sdfg
×
307

308
    def _convert_inputs(self, args: tuple) -> tuple:
4✔
309
        return args
×
310

311
    def _convert_outputs(self, result: Any, original_args: tuple) -> Any:
4✔
312
        return result
×
313

314
    def _get_signature(self, arg_types):
4✔
315
        return ", ".join(self._type_to_str(t) for t in arg_types)
×
316

317
    def _type_to_str(self, t):
4✔
318
        if isinstance(t, Scalar):
4✔
319
            return f"Scalar({t.primitive_type})"
4✔
320
        elif isinstance(t, Array):
4✔
321
            return f"Array({self._type_to_str(t.element_type)}, {t.num_elements})"
×
322
        elif isinstance(t, Pointer):
4✔
323
            return f"Pointer({self._type_to_str(t.pointee_type)})"
4✔
324
        elif isinstance(t, Structure):
4✔
325
            return f"Structure({t.name})"
4✔
326
        return str(t)
×
327

328
    def _infer_type(self, arg):
4✔
329
        if isinstance(arg, (float, np.float64)):
4✔
330
            return Scalar(PrimitiveType.Double)
4✔
331
        elif isinstance(arg, np.float32):
4✔
332
            return Scalar(PrimitiveType.Float)
4✔
333
        elif isinstance(arg, (bool, np.bool_)):
4✔
334
            return Scalar(PrimitiveType.Bool)
4✔
335
        elif isinstance(arg, (int, np.int64)):
4✔
336
            return Scalar(PrimitiveType.Int64)
4✔
337
        elif isinstance(arg, np.int32):
4✔
338
            return Scalar(PrimitiveType.Int32)
4✔
339
        elif isinstance(arg, np.int16):
4✔
340
            return Scalar(PrimitiveType.Int16)
×
341
        elif isinstance(arg, np.int8):
4✔
342
            return Scalar(PrimitiveType.Int8)
×
343
        elif isinstance(arg, np.uint64):
4✔
344
            return Scalar(PrimitiveType.UInt64)
×
345
        elif isinstance(arg, np.uint32):
4✔
346
            return Scalar(PrimitiveType.UInt32)
×
347
        elif isinstance(arg, np.uint16):
4✔
348
            return Scalar(PrimitiveType.UInt16)
×
349
        elif isinstance(arg, np.uint8):
4✔
350
            return Scalar(PrimitiveType.UInt8)
×
351
        elif isinstance(arg, np.ndarray):
4✔
352
            # Map dtype
353
            if arg.dtype == np.float64:
4✔
354
                elem_type = Scalar(PrimitiveType.Double)
4✔
355
            elif arg.dtype == np.float32:
4✔
356
                elem_type = Scalar(PrimitiveType.Float)
4✔
357
            elif arg.dtype == np.float16:
4✔
358
                elem_type = Scalar(PrimitiveType.Half)
×
359
            elif arg.dtype == ml_dtypes.bfloat16:
4✔
360
                elem_type = Scalar(PrimitiveType.BFloat)
4✔
361
            elif arg.dtype == np.bool_:
4✔
362
                elem_type = Scalar(PrimitiveType.Bool)
4✔
363
            elif arg.dtype == np.int64:
4✔
364
                elem_type = Scalar(PrimitiveType.Int64)
4✔
365
            elif arg.dtype == np.int32:
4✔
366
                elem_type = Scalar(PrimitiveType.Int32)
4✔
367
            elif arg.dtype == np.int16:
×
368
                elem_type = Scalar(PrimitiveType.Int16)
×
369
            elif arg.dtype == np.int8:
×
370
                elem_type = Scalar(PrimitiveType.Int8)
×
371
            elif arg.dtype == np.uint64:
×
372
                elem_type = Scalar(PrimitiveType.UInt64)
×
373
            elif arg.dtype == np.uint32:
×
374
                elem_type = Scalar(PrimitiveType.UInt32)
×
375
            elif arg.dtype == np.uint16:
×
376
                elem_type = Scalar(PrimitiveType.UInt16)
×
377
            elif arg.dtype == np.uint8:
×
378
                elem_type = Scalar(PrimitiveType.UInt8)
×
379
            else:
380
                raise ValueError(f"Unsupported numpy dtype: {arg.dtype}")
×
381

382
            return Pointer(elem_type)
4✔
383
        elif isinstance(arg, str):
4✔
384
            # Explicitly reject strings - they are not supported
385
            raise ValueError(f"Unsupported argument type: {type(arg)}")
4✔
386
        else:
387
            # Check if it's a class instance
388
            if hasattr(arg, "__class__") and not isinstance(arg, type):
4✔
389
                # It's an instance of a class, return pointer to Structure
390
                return Pointer(Structure(arg.__class__.__name__))
4✔
391
            raise ValueError(f"Unsupported argument type: {type(arg)}")
×
392

393
    def _build_sdfg(
4✔
394
        self,
395
        arg_types,
396
        args,
397
        arg_shape_mapping,
398
        shape_values,
399
    ):
400
        sig = inspect.signature(self.func)
4✔
401

402
        # Handle return type - always void for SDFG, output args used for returns
403
        return_type = Scalar(PrimitiveType.Void)
4✔
404
        infer_return_type = True
4✔
405

406
        # Parse return annotation to determine output arguments if possible
407
        explicit_returns = []
4✔
408
        if sig.return_annotation is not inspect.Signature.empty:
4✔
409
            infer_return_type = False
4✔
410

411
            # Helper to normalize annotation to list of types
412
            def normalize_annotation(ann):
4✔
413
                # Handle Tuple[type, ...]
414
                origin = get_origin(ann)
4✔
415
                if origin is tuple:
4✔
416
                    type_args = get_args(ann)
×
417
                    # Tuple[()] or Tuple w/o args
418
                    if not type_args:
×
419
                        return []
×
420
                    # Tuple[int, float]
421
                    if len(type_args) > 0 and type_args[-1] is not Ellipsis:
×
422
                        return [_map_python_type(t) for t in type_args]
×
423
                    # Tuple[int, ...] - not supported for fixed number of returns yet?
424
                    # For now assume fixed tuple
425
                    return [_map_python_type(t) for t in type_args]
×
426
                else:
427
                    return [_map_python_type(ann)]
4✔
428

429
            explicit_returns = normalize_annotation(sig.return_annotation)
4✔
430
            for rt in explicit_returns:
4✔
431
                if not isinstance(rt, Type):
4✔
432
                    # Fallback if map failed (e.g. invalid annotation)
433
                    infer_return_type = True
×
434
                    explicit_returns = []
×
435
                    break
×
436

437
        builder = StructuredSDFGBuilder(f"{self.name}_sdfg", return_type)
4✔
438

439
        # Add pre-defined return arguments if we know them
440
        if not infer_return_type:
4✔
441
            for i, dtype in enumerate(explicit_returns):
4✔
442
                # Scalar -> Pointer(Scalar)
443
                # Array -> Already Pointer(Scalar). Keep it.
444
                arg_type = dtype
4✔
445
                if isinstance(dtype, Scalar):
4✔
446
                    arg_type = Pointer(dtype)
4✔
447

448
                builder.add_container(f"_docc_ret_{i}", arg_type, is_argument=True)
4✔
449

450
        # Register structure types for any class arguments
451
        # Also track member name to index mapping for each structure
452
        structures_to_register = {}
4✔
453
        structure_member_info = {}  # Maps struct_name -> {member_name: (index, type)}
4✔
454
        for i, (arg, dtype) in enumerate(zip(args, arg_types)):
4✔
455
            if isinstance(dtype, Pointer) and dtype.has_pointee_type():
4✔
456
                pointee = dtype.pointee_type
4✔
457
                if isinstance(pointee, Structure):
4✔
458
                    struct_name = pointee.name
4✔
459
                    if struct_name not in structures_to_register:
4✔
460
                        # Get class from arg to introspect members
461
                        if hasattr(arg, "__dict__"):
4✔
462
                            # Use __dict__ to get only instance attributes
463
                            # Sort by name to ensure consistent ordering
464
                            # Note: This alphabetical ordering is used to define the
465
                            # structure layout and must match the order expected by
466
                            # the backend code generation
467
                            member_types = []
4✔
468
                            member_names = []
4✔
469
                            for attr_name, attr_value in sorted(arg.__dict__.items()):
4✔
470
                                if not attr_name.startswith("_"):
4✔
471
                                    # Infer member type from instance attribute
472
                                    # Check bool before int since bool is subclass of int
473
                                    member_type = None
4✔
474
                                    if isinstance(attr_value, bool):
4✔
475
                                        member_type = Scalar(PrimitiveType.Bool)
×
476
                                    elif isinstance(attr_value, (int, np.int64)):
4✔
477
                                        member_type = Scalar(PrimitiveType.Int64)
×
478
                                    elif isinstance(attr_value, (float, np.float64)):
4✔
479
                                        member_type = Scalar(PrimitiveType.Double)
4✔
480
                                    elif isinstance(attr_value, np.int32):
×
481
                                        member_type = Scalar(PrimitiveType.Int32)
×
482
                                    elif isinstance(attr_value, np.float32):
×
483
                                        member_type = Scalar(PrimitiveType.Float)
×
484
                                    # TODO: Consider using np.integer and np.floating abstract types
485
                                    # for more comprehensive numpy type coverage
486
                                    # TODO: Add support for nested structures and arrays
487

488
                                    if member_type is not None:
4✔
489
                                        member_types.append(member_type)
4✔
490
                                        member_names.append(attr_name)
4✔
491

492
                            if member_types:
4✔
493
                                structures_to_register[struct_name] = member_types
4✔
494
                                # Build member name to (index, type) mapping
495
                                structure_member_info[struct_name] = {
4✔
496
                                    name: (idx, mtype)
497
                                    for idx, (name, mtype) in enumerate(
498
                                        zip(member_names, member_types)
499
                                    )
500
                                }
501

502
        # Store structure_member_info for later use in CompiledSDFG
503
        self._last_structure_member_info = structure_member_info
4✔
504

505
        # Register all discovered structures with the builder
506
        for struct_name, member_types in structures_to_register.items():
4✔
507
            builder.add_structure(struct_name, member_types)
4✔
508

509
        # Register arguments
510
        params = list(sig.parameters.items())
4✔
511
        if len(params) != len(arg_types):
4✔
512
            raise ValueError(
×
513
                f"Argument count mismatch: expected {len(params)}, got {len(arg_types)}"
514
            )
515

516
        # Add regular arguments
517
        tensor_table = {}
4✔
518
        for i, ((name, param), dtype, arg) in enumerate(zip(params, arg_types, args)):
4✔
519
            builder.add_container(name, dtype, is_argument=True)
4✔
520

521
            # Store layout information for arrays
522
            if isinstance(arg, np.ndarray):
4✔
523
                element_type = element_type_from_sdfg_type(dtype)
4✔
524

525
                shapes = []
4✔
526
                for dim_idx in range(arg.ndim):
4✔
527
                    dim_val = arg.shape[dim_idx]
4✔
528
                    if dim_val == 1:
4✔
529
                        # Always use literal "1" for size-1 dimensions to enable
530
                        # proper broadcasting detection
531
                        shapes.append("1")
4✔
532
                    else:
533
                        u_idx = arg_shape_mapping[(i, dim_idx)]
4✔
534
                        shapes.append(f"_s{u_idx}")
4✔
535

536
                strides = []
4✔
537
                if arg.flags["C_CONTIGUOUS"]:
4✔
538
                    # Row-major: stride[i] = product of shapes[i+1:]
539
                    for dim_idx in range(arg.ndim):
4✔
540
                        if dim_idx == arg.ndim - 1:
4✔
541
                            strides.append("1")
4✔
542
                        else:
543
                            suffix_shapes = shapes[dim_idx + 1 :]
4✔
544
                            if len(suffix_shapes) == 1:
4✔
545
                                strides.append(suffix_shapes[0])
4✔
546
                            else:
547
                                strides.append("(" + " * ".join(suffix_shapes) + ")")
4✔
548
                elif arg.flags["F_CONTIGUOUS"]:
4✔
549
                    # Column-major: stride[i] = product of shapes[:i]
550
                    for dim_idx in range(arg.ndim):
4✔
551
                        if dim_idx == 0:
4✔
552
                            strides.append("1")
4✔
553
                        else:
554
                            prefix_shapes = shapes[:dim_idx]
4✔
555
                            if len(prefix_shapes) == 1:
4✔
556
                                strides.append(prefix_shapes[0])
4✔
557
                            else:
558
                                strides.append("(" + " * ".join(prefix_shapes) + ")")
4✔
559
                else:
560
                    # Non-contiguous: use actual stride values
561
                    for dim_idx in range(arg.ndim):
4✔
562
                        stride_val = arg.strides[dim_idx] // arg.itemsize
4✔
563
                        strides.append(f"{stride_val}")
4✔
564

565
                offset = "0"
4✔
566
                tensor_table[name] = Tensor(element_type, shapes, strides, offset)
4✔
567

568
            elif isinstance(arg, np.generic):
4✔
569
                # NumPy scalar types (np.float64, np.int32, etc.) should be treated
570
                # as 0-d arrays for type promotion purposes - they trigger full
571
                # promotion, unlike Python literals which adapt to the array dtype
572
                element_type = element_type_from_sdfg_type(dtype)
4✔
573
                tensor_table[name] = Tensor(element_type, [], [], "0")
4✔
574

575
        # Add unified shape arguments only for shapes without scalar equivalents
576
        # and skip size-1 dimensions (they use literal "1" instead)
577
        for i in range(len(shape_values)):
4✔
578
            if shape_values[i] != 1:
4✔
579
                builder.add_container(
4✔
580
                    f"_s{i}", Scalar(PrimitiveType.Int64), is_argument=True
581
                )
582
                builder.add_assumption_lb(f"_s{i}", "1")  # Shapes must be positive
4✔
583
                builder.add_assumption_const(f"_s{i}", True)  # Shapes are constant
4✔
584

585
        # Create symbol table for parser
586
        container_table = {}
4✔
587
        for i, ((name, param), dtype, arg) in enumerate(zip(params, arg_types, args)):
4✔
588
            container_table[name] = dtype
4✔
589

590
        for i in range(len(shape_values)):
4✔
591
            if shape_values[i] != 1:
4✔
592
                container_table[f"_s{i}"] = Scalar(PrimitiveType.Int64)
4✔
593

594
        # Parse AST
595
        source_lines, start_line = inspect.getsourcelines(self.func)
4✔
596
        source = textwrap.dedent("".join(source_lines))
4✔
597
        tree = ast.parse(source)
4✔
598
        ast.increment_lineno(tree, start_line - 1)
4✔
599
        func_def = tree.body[0]
4✔
600

601
        filename = inspect.getsourcefile(self.func)
4✔
602
        function_name = self.func.__name__
4✔
603

604
        # Combine globals with closure variables (closure takes precedence)
605
        combined_globals = dict(self.func.__globals__)
4✔
606
        if self.func.__closure__ is not None and self.func.__code__.co_freevars:
4✔
607
            for name, cell in zip(
4✔
608
                self.func.__code__.co_freevars, self.func.__closure__
609
            ):
610
                combined_globals[name] = cell.cell_contents
4✔
611

612
        parser = ASTParser(
4✔
613
            builder,
614
            tensor_table,
615
            container_table,
616
            filename,
617
            function_name,
618
            infer_return_type=infer_return_type,
619
            globals_dict=combined_globals,
620
            structure_member_info=structure_member_info,
621
        )
622
        for node in func_def.body:
4✔
623
            parser.visit(node)
4✔
624

625
        # Emit hoisted allocations at function entry
626
        parser.memory_handler.emit_allocations()
4✔
627

628
        sdfg = builder.move()
4✔
629
        # Mark return arguments metadata
630
        out_args = []
4✔
631
        for name in sdfg.arguments:
4✔
632
            if name.startswith("_docc_ret_"):
4✔
633
                out_args.append(name)
4✔
634

635
        return (
4✔
636
            sdfg,
637
            out_args,
638
            parser.captured_return_shapes,
639
            parser.captured_return_strides,
640
        )
641

642

643
def native(
4✔
644
    func=None,
645
    *,
646
    target="none",
647
    category="server",
648
    instrumentation_mode=None,
649
    capture_args=None,
650
    remote_tuning=False,
651
):
652
    """Decorator to create a PythonProgram from a Python function.
653

654
    Example:
655
        @native
656
        def my_function(x: np.ndarray) -> np.ndarray:
657
            return x * 2
658

659
        result = my_function(np.array([1.0, 2.0, 3.0]))
660
    """
661
    if func is None:
4✔
662
        return lambda f: PythonProgram(
4✔
663
            f,
664
            target=target,
665
            category=category,
666
            instrumentation_mode=instrumentation_mode,
667
            capture_args=capture_args,
668
            remote_tuning=remote_tuning,
669
        )
670
    return PythonProgram(
4✔
671
        func,
672
        target=target,
673
        category=category,
674
        instrumentation_mode=instrumentation_mode,
675
        capture_args=capture_args,
676
        remote_tuning=remote_tuning,
677
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc