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

daisytuner / docc / 26804877914

02 Jun 2026 07:22AM UTC coverage: 60.835% (-0.03%) from 60.869%
26804877914

Pull #725

github

web-flow
Merge ad1f0d80a into cd25c9278
Pull Request #725: Tensor node backport

599 of 1251 new or added lines in 50 files covered. (47.88%)

538 existing lines in 45 files now uncovered.

35099 of 57695 relevant lines covered (60.84%)

11081.73 hits per line

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

75.0
/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✔
47
        args = get_args(dtype)
4✔
48
        base_type = args[0]
4✔
49
        metadata = args[1:]
4✔
50

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

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

60
            return Pointer(elem_type)
4✔
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✔
79
    elif dtype is np.int32:
4✔
80
        return Scalar(PrimitiveType.Int32)
4✔
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
126
        compiled = self.compile(*args)
4✔
127
        res = compiled(*args)
4✔
128

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

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

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

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

156
        return res
4✔
157

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

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

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

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

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

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

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

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

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

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

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

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

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

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

252
        # 4. Create CompiledSDFG
253
        compiled = CompiledSDFG(
4✔
254
            lib_path,
255
            sdfg,
256
            shape_sources,
257
            self._last_structure_member_info,
258
            out_args,
259
            out_shapes,
260
            out_strides,
261
        )
262

263
        # Cache if using default output folder
264
        if original_output_folder is None:
4✔
265
            self.cache[signature] = compiled
4✔
266

267
        return compiled
4✔
268

269
    def to_sdfg(self, *args: Any) -> StructuredSDFG:
4✔
UNCOV
270
        arg_types = [self._infer_type(arg) for arg in args]
×
271

272
        # Build shape mapping
UNCOV
273
        shape_values = []
×
UNCOV
274
        shape_sources = []
×
UNCOV
275
        arg_shape_mapping = {}
×
276

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

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

299
        sdfg, _, _, _ = self._build_sdfg(
×
300
            arg_types, args, arg_shape_mapping, shape_values
301
        )
UNCOV
302
        return sdfg
×
303

304
    def _convert_inputs(self, args: tuple) -> tuple:
4✔
305
        return args
×
306

307
    def _convert_outputs(self, result: Any, original_args: tuple) -> Any:
4✔
308
        return result
×
309

310
    def _get_signature(self, arg_types):
4✔
311
        return ", ".join(self._type_to_str(t) for t in arg_types)
×
312

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

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

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

389
    def _build_sdfg(
4✔
390
        self,
391
        arg_types,
392
        args,
393
        arg_shape_mapping,
394
        shape_values,
395
    ):
396
        sig = inspect.signature(self.func)
4✔
397

398
        # Handle return type - always void for SDFG, output args used for returns
399
        return_type = Scalar(PrimitiveType.Void)
4✔
400
        infer_return_type = True
4✔
401

402
        # Parse return annotation to determine output arguments if possible
403
        explicit_returns = []
4✔
404
        if sig.return_annotation is not inspect.Signature.empty:
4✔
405
            infer_return_type = False
4✔
406

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

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

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

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

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

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

484
                                    if member_type is not None:
4✔
485
                                        member_types.append(member_type)
4✔
486
                                        member_names.append(attr_name)
4✔
487

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

498
        # Store structure_member_info for later use in CompiledSDFG
499
        self._last_structure_member_info = structure_member_info
4✔
500

501
        # Register all discovered structures with the builder
502
        for struct_name, member_types in structures_to_register.items():
4✔
503
            builder.add_structure(struct_name, member_types)
4✔
504

505
        # Register arguments
506
        params = list(sig.parameters.items())
4✔
507
        if len(params) != len(arg_types):
4✔
UNCOV
508
            raise ValueError(
×
509
                f"Argument count mismatch: expected {len(params)}, got {len(arg_types)}"
510
            )
511

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

517
            # Store layout information for arrays
518
            if isinstance(arg, np.ndarray):
4✔
519
                element_type = element_type_from_sdfg_type(dtype)
4✔
520

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

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

561
                offset = "0"
4✔
562
                tensor_table[name] = Tensor(element_type, shapes, strides, offset)
4✔
563

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

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

581
        # Create symbol table for parser
582
        container_table = {}
4✔
583
        for i, ((name, param), dtype, arg) in enumerate(zip(params, arg_types, args)):
4✔
584
            container_table[name] = dtype
4✔
585

586
        for i in range(len(shape_values)):
4✔
587
            if shape_values[i] != 1:
4✔
588
                container_table[f"_s{i}"] = Scalar(PrimitiveType.Int64)
4✔
589

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

597
        filename = inspect.getsourcefile(self.func)
4✔
598
        function_name = self.func.__name__
4✔
599

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

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

621
        # Emit hoisted allocations at function entry
622
        parser.memory_handler.emit_allocations()
4✔
623

624
        sdfg = builder.move()
4✔
625
        # Mark return arguments metadata
626
        out_args = []
4✔
627
        for name in sdfg.arguments:
4✔
628
            if name.startswith("_docc_ret_"):
4✔
629
                out_args.append(name)
4✔
630

631
        return (
4✔
632
            sdfg,
633
            out_args,
634
            parser.captured_return_shapes,
635
            parser.captured_return_strides,
636
        )
637

638

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

650
    Example:
651
        @native
652
        def my_function(x: np.ndarray) -> np.ndarray:
653
            return x * 2
654

655
        result = my_function(np.array([1.0, 2.0, 3.0]))
656
    """
657
    if func is None:
4✔
658
        return lambda f: PythonProgram(
4✔
659
            f,
660
            target=target,
661
            category=category,
662
            instrumentation_mode=instrumentation_mode,
663
            capture_args=capture_args,
664
            remote_tuning=remote_tuning,
665
        )
666
    return PythonProgram(
4✔
667
        func,
668
        target=target,
669
        category=category,
670
        instrumentation_mode=instrumentation_mode,
671
        capture_args=capture_args,
672
        remote_tuning=remote_tuning,
673
    )
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