CodeSampleX

Exemplo

numba 0.67.0rc1: prange

Amostra verificada para pypi numba 0.67.0rc1: prange. O contrato rodou em python 3.12 · linux debian/x64 · docker e passou: prange fallback outside jit…

sha256:4259237a29e9c64e57ad7fb9228de66f8e816c0dc824ef693a393be0e0ada285

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 pip

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
python 3.12 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-slim@sha256:09f7da3bc104…
2026-09-02

Caso

HOW
Objetivo
verify numba.prange in pkg:pypi/numba@0.67.0rc1
Pacotes
Símbolos
  • numba.prange
Criado
2026-09-02T13:17:55Z

Contrato

  1. prange fallback outside jit execution matches standard range iteration with start, stop, and step arguments
  2. prange inside njit with parallel=True performs parallel reduction over array elements
  3. prange inside njit with parallel=True performs parallel array mutation and elementwise transformation
  4. prange supports start and stop slice boundary indexing in parallel execution
  5. prange supports nested multidimensional parallel loops for 2D array computation
  6. prange with step size other than 1 in parallel jit raises UnsupportedRewriteError

Arquivos

  • NOTES.md
  • PROMPT.md
  • csx.json
  • requirements.txt
  • spec.json
  • test/contract.py

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

NOTES.md
# Numba prange parallel loop API

This sample pins `numba` 0.67.0rc1 and verifies `numba.prange` for parallel iteration in JIT-compiled functions.

The contract verifies:
- `prange` outside JIT context behaves as a drop-in replacement for standard `range` with start, stop, and step attributes.
- `prange` inside `@njit(parallel=True)` enables parallel reduction across threads.
- `prange` inside `@njit(parallel=True)` enables independent parallel array element mutation.
- `prange` supports start and stop slice boundary indexing in parallel execution.
- `prange` supports nested multi-dimensional parallel loops for 2D array computation.
- `prange` with step size other than 1 in `@njit(parallel=True)` raises `UnsupportedRewriteError`.
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify numba.prange in pkg:pypi/numba@0.67.0rc1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/numba@0.67.0rc1
Demonstrate these symbols/APIs:
  - numba.prange

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:e091e9d46141a6e136c438aede7d9cbecbb11d6a8ccd7832582821f3d606e395","contract":["prange fallback outside jit execution matches standard range iteration with start, stop, and step arguments","prange inside njit with parallel=True performs parallel reduction over array elements","prange inside njit with parallel=True performs parallel array mutation and elementwise transformation","prange supports start and stop slice boundary indexing in parallel execution","prange supports nested multidimensional parallel loops for 2D array computation","prange with step size other than 1 in parallel jit raises UnsupportedRewriteError"],"goal":"verify numba.prange in pkg:pypi/numba@0.67.0rc1","kind":"HOW","packages":["pkg:pypi/numba@0.67.0rc1"],"schemaVersion":1,"symbols":["numba.prange"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"pypi","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"pip","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/numba@0.67.0rc1"],"schemaVersion":1,"subject":"pkg:pypi/numba@0.67.0rc1","symbols":["numba.prange"],"verifierAdapter":"python@1"}
requirements.txt
numba==0.67.0rc1
numpy==2.5.2
spec.json
{
  "schemaVersion": 1,
  "goal": "verify numba.prange in pkg:pypi/numba@0.67.0rc1",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/numba@0.67.0rc1"
  ],
  "symbols": [
    "numba.prange"
  ]
}
test/contract.py
import numpy as np
import numba
from numba import njit, prange
from numba.core.errors import UnsupportedRewriteError


def test_prange_outside_jit():
    r1 = prange(5)
    assert list(r1) == [0, 1, 2, 3, 4]
    assert r1.start == 0 and r1.stop == 5 and r1.step == 1

    r2 = prange(2, 7)
    assert list(r2) == [2, 3, 4, 5, 6]
    assert r2.start == 2 and r2.stop == 7 and r2.step == 1

    r3 = prange(1, 10, 2)
    assert list(r3) == [1, 3, 5, 7, 9]
    assert r3.start == 1 and r3.stop == 10 and r3.step == 2


def test_prange_parallel_reduction():
    @njit(parallel=True)
    def parallel_sum(arr):
        acc = 0.0
        for i in prange(len(arr)):
            acc += arr[i]
        return acc

    arr = np.linspace(1.0, 100.0, 200)
    res = parallel_sum(arr)
    expected = np.sum(arr)
    assert np.isclose(res, expected)


def test_prange_parallel_array_mutation():
    @njit(parallel=True)
    def parallel_scale(arr, factor):
        out = np.empty_like(arr)
        for i in prange(len(arr)):
            out[i] = arr[i] * factor
        return out

    arr = np.arange(50, dtype=np.float64)
    scaled = parallel_scale(arr, 3.5)
    expected = arr * 3.5
    assert np.array_equal(scaled, expected)


def test_prange_slice_bounds():
    @njit(parallel=True)
    def parallel_slice_sum(arr, start, stop):
        acc = 0.0
        for i in prange(start, stop):
            acc += arr[i]
        return acc

    arr = np.arange(100, dtype=np.float64)
    res = parallel_slice_sum(arr, 15, 65)
    expected = np.sum(arr[15:65])
    assert np.isclose(res, expected)


def test_prange_multidimensional_nested():
    @njit(parallel=True)
    def parallel_matrix_add(a, b):
        rows, cols = a.shape
        out = np.empty((rows, cols), dtype=a.dtype)
        for i in prange(rows):
            for j in prange(cols):
                out[i, j] = a[i, j] + b[i, j]
        return out

    a = np.ones((8, 8), dtype=np.float64)
    b = np.full((8, 8), 2.5, dtype=np.float64)
    out = parallel_matrix_add(a, b)
    assert np.array_equal(out, a + b)


def test_prange_unsupported_non_unit_step_raises():
    try:
        @njit(parallel=True)
        def bad_step(arr):
            acc = 0.0
            for i in prange(0, len(arr), 2):
                acc += arr[i]
            return acc

        arr = np.arange(10, dtype=np.float64)
        bad_step(arr)
        assert False, "Expected UnsupportedRewriteError for non-unit step in prange"
    except UnsupportedRewriteError as exc:
        assert "Only constant step size of 1 is supported for prange" in str(exc)


def main():
    test_prange_outside_jit()
    test_prange_parallel_reduction()
    test_prange_parallel_array_mutation()
    test_prange_slice_bounds()
    test_prange_multidimensional_nested()
    test_prange_unsupported_non_unit_step_raises()
    print("All contract assertions passed.")


if __name__ == "__main__":
    main()

Seeder de origem

anônimo