diff --git a/.github/scripts/trtt_wrapper.py b/.github/scripts/trtt_wrapper.py index 9ec93452..429b03ad 100644 --- a/.github/scripts/trtt_wrapper.py +++ b/.github/scripts/trtt_wrapper.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 import sys + from ttrt import main if __name__ == "__main__": diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3f025cd4..9c55432b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,6 +4,11 @@ repos: hooks: - id: black language_version: python3 + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + args: ["--profile", "black"] - repo: https://github.com/pre-commit/mirrors-clang-format rev: v18.1.7 hooks: diff --git a/examples/custom_dm_matmul.py b/examples/custom_dm_matmul.py index ca09fc75..393726cc 100644 --- a/examples/custom_dm_matmul.py +++ b/examples/custom_dm_matmul.py @@ -1,9 +1,9 @@ # SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 +import torch from ttlang.ttl_api import * from ttlang.utils.correctness import assert_allclose -import torch @pykernel_gen( diff --git a/examples/metal_examples/multicore_matmul/metal/multicore_matmul.py b/examples/metal_examples/multicore_matmul/metal/multicore_matmul.py index c3bdb466..7e5e52a1 100644 --- a/examples/metal_examples/multicore_matmul/metal/multicore_matmul.py +++ b/examples/metal_examples/multicore_matmul/metal/multicore_matmul.py @@ -1,10 +1,9 @@ # SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 -import ttnn import pytest import torch - +import ttnn from ttlang.utils.correctness import assert_with_ulp diff --git a/examples/metal_examples/multicore_matmul/ttlang/multicore_matmul.py b/examples/metal_examples/multicore_matmul/ttlang/multicore_matmul.py index c91addf4..daf54f1e 100644 --- a/examples/metal_examples/multicore_matmul/ttlang/multicore_matmul.py +++ b/examples/metal_examples/multicore_matmul/ttlang/multicore_matmul.py @@ -2,14 +2,12 @@ # # SPDX-License-Identifier: Apache-2.0 # up to tt-lang spec, not intended to compile or run currently -import ttnn import pytest import torch - -from ttl import Program, make_circular_buffer_like, copy - -from ttlang.utils.correctness import assert_with_ulp +import ttnn +from ttl import Program, copy, make_circular_buffer_like from ttlang.utils.block_allocation import split_work_to_cores +from ttlang.utils.correctness import assert_with_ulp def get_number_of_cores(grid_range): diff --git a/examples/metal_examples/multicore_reuse_matmul/metal/multicore_reuse_matmul.py b/examples/metal_examples/multicore_reuse_matmul/metal/multicore_reuse_matmul.py index fc54efa3..d57b1293 100644 --- a/examples/metal_examples/multicore_reuse_matmul/metal/multicore_reuse_matmul.py +++ b/examples/metal_examples/multicore_reuse_matmul/metal/multicore_reuse_matmul.py @@ -1,14 +1,13 @@ # SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 -import ttnn -import pytest -import torch import matplotlib.pyplot as plt import numpy as np - -from ttlang.utils.correctness import assert_with_ulp +import pytest +import torch +import ttnn from ttlang.utils.block_allocation import get_large_matmul_params +from ttlang.utils.correctness import assert_with_ulp @pytest.mark.parametrize("M,K,N", [(640, 640, 640)]) diff --git a/examples/metal_examples/multicore_reuse_matmul/ttlang/multicore_reuse_matmul.py b/examples/metal_examples/multicore_reuse_matmul/ttlang/multicore_reuse_matmul.py index 3df6059b..7258d6cf 100644 --- a/examples/metal_examples/multicore_reuse_matmul/ttlang/multicore_reuse_matmul.py +++ b/examples/metal_examples/multicore_reuse_matmul/ttlang/multicore_reuse_matmul.py @@ -1,12 +1,11 @@ # SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 -import ttnn import pytest import torch - -from ttl import Program, make_circular_buffer_like, copy, core +import ttnn from metal_examples.utils import assert_with_ulp +from ttl import Program, copy, core, make_circular_buffer_like @ttl.kernel(grid=(13, 10)) diff --git a/examples/metal_examples/singlecore_matmul/metal/singlecore_matmul.py b/examples/metal_examples/singlecore_matmul/metal/singlecore_matmul.py index 57f4de58..e1d25492 100644 --- a/examples/metal_examples/singlecore_matmul/metal/singlecore_matmul.py +++ b/examples/metal_examples/singlecore_matmul/metal/singlecore_matmul.py @@ -1,10 +1,9 @@ # SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 -import ttnn import pytest import torch - +import ttnn from ttlang.utils.correctness import assert_with_ulp diff --git a/examples/metal_examples/singlecore_matmul/ttlang/singlecore_matmul.py b/examples/metal_examples/singlecore_matmul/ttlang/singlecore_matmul.py index 06afe0c9..95ebff41 100644 --- a/examples/metal_examples/singlecore_matmul/ttlang/singlecore_matmul.py +++ b/examples/metal_examples/singlecore_matmul/ttlang/singlecore_matmul.py @@ -4,12 +4,11 @@ # up to tt-lang spec, not intended to compile or run currently import sys from pathlib import Path -import ttnn + import pytest import torch - -from ttl import Program, make_circular_buffer_like, copy - +import ttnn +from ttl import Program, copy, make_circular_buffer_like from ttlang.utils.correctness import assert_with_ulp diff --git a/examples/sim/eltwise_add.py b/examples/sim/eltwise_add.py index 20d195b3..f566e064 100644 --- a/examples/sim/eltwise_add.py +++ b/examples/sim/eltwise_add.py @@ -2,8 +2,8 @@ # # SPDX-License-Identifier: Apache-2.0 # type: ignore -from typing import TYPE_CHECKING import math +from typing import TYPE_CHECKING from sim import ttl, ttnn from sim.testing import assert_pcc diff --git a/examples/sim/eltwise_pipe.py b/examples/sim/eltwise_pipe.py index 805ee180..8b0216ce 100644 --- a/examples/sim/eltwise_pipe.py +++ b/examples/sim/eltwise_pipe.py @@ -2,12 +2,12 @@ # # SPDX-License-Identifier: Apache-2.0 # type: ignore -from typing import TYPE_CHECKING import math +from typing import TYPE_CHECKING from sim import ttl, ttnn -from sim.typedefs import Pipe from sim.testing import assert_pcc +from sim.typedefs import Pipe if TYPE_CHECKING: from sim.pykernel_env import granularity diff --git a/examples/sim/eltwise_pipe_core3.py b/examples/sim/eltwise_pipe_core3.py index 5b776bfe..83e20cbb 100644 --- a/examples/sim/eltwise_pipe_core3.py +++ b/examples/sim/eltwise_pipe_core3.py @@ -2,12 +2,12 @@ # # SPDX-License-Identifier: Apache-2.0 # type: ignore -from typing import TYPE_CHECKING import math +from typing import TYPE_CHECKING from sim import ttl, ttnn -from sim.typedefs import Pipe from sim.testing import assert_pcc +from sim.typedefs import Pipe if TYPE_CHECKING: from sim.pykernel_env import granularity diff --git a/examples/test_accessor_creation.py b/examples/test_accessor_creation.py index 8a3811a9..a1ab634e 100644 --- a/examples/test_accessor_creation.py +++ b/examples/test_accessor_creation.py @@ -8,9 +8,9 @@ defined in the DSL are materialized as stream_layout ops immediately in the Python-generated IR, rather than being added later by a pass. """ +import torch from ttlang.ttl_api import * from ttlang.utils.correctness import assert_allclose -import torch @pykernel_gen( diff --git a/examples/test_simple_add.py b/examples/test_simple_add.py index f7da2335..2761def6 100644 --- a/examples/test_simple_add.py +++ b/examples/test_simple_add.py @@ -8,8 +8,8 @@ Tests the complete data path: host → L1 → compute → L1 → host """ -from ttlang.ttl_api import * import torch +from ttlang.ttl_api import * @pykernel_gen(grid=(1, 1), block_factors=[(1, 1), (1, 1), (1, 1)]) diff --git a/pyproject.toml b/pyproject.toml index d5a8e73f..6a81b0fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,3 +57,12 @@ reportUntypedFunctionDecorator = "error" reportPrivateUsage = "error" reportImportCycles = "error" reportUnnecessaryIsInstance = "warning" + +[tool.isort] +profile = "black" +line_length = 88 +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +use_parentheses = true +ensure_newline_before_comments = true diff --git a/python/pykernel/_src/base_ast.py b/python/pykernel/_src/base_ast.py index 6a06498c..cfb2cd6c 100644 --- a/python/pykernel/_src/base_ast.py +++ b/python/pykernel/_src/base_ast.py @@ -7,8 +7,8 @@ import ast import inspect +from ttmlir.dialects import emitc, func from ttmlir.ir import * -from ttmlir.dialects import func, emitc class PyKernelAstBase(ast.NodeVisitor): diff --git a/python/pykernel/_src/kernel_ast.py b/python/pykernel/_src/kernel_ast.py index ffedb44e..ca09ae0c 100644 --- a/python/pykernel/_src/kernel_ast.py +++ b/python/pykernel/_src/kernel_ast.py @@ -8,11 +8,11 @@ import ast import inspect +from ttmlir.dialects import arith, emitc, func, memref, scf from ttmlir.ir import * -from ttmlir.dialects import func, scf, arith, memref, emitc -from .kernel_types import ClassRegistry from .base_ast import PyKernelAstBase +from .kernel_types import ClassRegistry from .utils import _cast, _get_type_str diff --git a/python/pykernel/_src/utils.py b/python/pykernel/_src/utils.py index d90f949a..148b9182 100644 --- a/python/pykernel/_src/utils.py +++ b/python/pykernel/_src/utils.py @@ -5,9 +5,10 @@ # NOTE: This file was copied from tt-mlir/tools/pykernel/_src/utils.py # and cleaned up to remove unused code (_discover_dialect_ops). -import textwrap import inspect +import textwrap from typing import Callable + from ttmlir.dialects import arith from ttmlir.ir import * diff --git a/python/setup.py b/python/setup.py index 2b771899..e61b0404 100644 --- a/python/setup.py +++ b/python/setup.py @@ -9,11 +9,10 @@ import pathlib import shutil import subprocess - -from setuptools import setup, Extension -from setuptools.command.build_ext import build_ext from datetime import datetime +from setuptools import Extension, setup +from setuptools.command.build_ext import build_ext readme = None diff --git a/python/sim/__init__.py b/python/sim/__init__.py index b8a0e8b7..6db1d130 100644 --- a/python/sim/__init__.py +++ b/python/sim/__init__.py @@ -6,16 +6,15 @@ sim package: simulation components for TT-Lang including circular buffers, tensors, and copy operations. """ +from . import ttnnsim as ttnn from .cbapi import CBAPI, CBStats -from .typedefs import CoreIndex, Shape, Pipe -from .constants import TILE_SHAPE, MAX_CBS -from .copy import copy, CopyTransaction -from .program import Program -from .kernel import core +from .constants import MAX_CBS, TILE_SHAPE +from .copy import CopyTransaction, copy from .decorators import compute, datamovement -from .kernel import kernel -from .pipe import if_pipe_src, if_pipe_dst, core_in_pipe -from . import ttnnsim as ttnn +from .kernel import core, kernel +from .pipe import core_in_pipe, if_pipe_dst, if_pipe_src +from .program import Program +from .typedefs import CoreIndex, Pipe, Shape # Create ttl namespace object @@ -23,14 +22,14 @@ class _TTLNamespace: """TT-Lang namespace for DSL constructs.""" def __init__(self): - from .kernel import kernel, grid_size, core from .cb import make_circular_buffer_like + from .constants import TILE_SHAPE + from .copy import copy from .decorators import compute, datamovement + from .kernel import core, grid_size, kernel + from .pipe import core_in_pipe, if_pipe_dst, if_pipe_src from .program import Program - from .copy import copy - from .typedefs import Pipe, Size, Shape - from .constants import TILE_SHAPE - from .pipe import if_pipe_src, if_pipe_dst, core_in_pipe + from .typedefs import Pipe, Shape, Size self.kernel = kernel self.grid_size = grid_size diff --git a/python/sim/block.py b/python/sim/block.py index e36d91ea..6716d0f9 100644 --- a/python/sim/block.py +++ b/python/sim/block.py @@ -7,11 +7,13 @@ """ import operator as _op -from typing import List, Sequence, Any, Union, Callable -from .typedefs import Size, Index, Span +from typing import Any, Callable, List, Sequence, Union + +from pydantic import validate_call + from .cbstate import CBSlot from .ttnnsim import Tensor -from pydantic import validate_call +from .typedefs import Index, Size, Span # Notice that get_read_ptr and get_write_ptr return a C++ pointer which does not diff --git a/python/sim/cb.py b/python/sim/cb.py index ca62fc56..9055b2d7 100644 --- a/python/sim/cb.py +++ b/python/sim/cb.py @@ -10,16 +10,16 @@ operations. """ -from typing import Tuple, Optional, Union, List, Callable from types import TracebackType +from typing import Callable, List, Optional, Tuple, Union import torch -from .cbapi import CBAPI from .block import Block -from .typedefs import CBID, Size, Shape -from .ttnnsim import Tensor +from .cbapi import CBAPI from .constants import TILE_SHAPE +from .ttnnsim import Tensor +from .typedefs import CBID, Shape, Size class _BlockContextManager: diff --git a/python/sim/cbapi.py b/python/sim/cbapi.py index b7389c9f..0e79418e 100644 --- a/python/sim/cbapi.py +++ b/python/sim/cbapi.py @@ -7,13 +7,15 @@ """ import threading -from typing import List, Optional, Annotated, NamedTuple, Any -from pydantic import validate_call, Field -from .errors import CBContractError, CBTimeoutError -from .constants import MAX_CBS, CB_DEFAULT_TIMEOUT -from .typedefs import Size, CBID +from typing import Annotated, Any, List, NamedTuple, Optional + +from pydantic import Field, validate_call + from .block import Block from .cbstate import CBState +from .constants import CB_DEFAULT_TIMEOUT, MAX_CBS +from .errors import CBContractError, CBTimeoutError +from .typedefs import CBID, Size class CBStats(NamedTuple): diff --git a/python/sim/cbstate.py b/python/sim/cbstate.py index 7095162d..90736bca 100644 --- a/python/sim/cbstate.py +++ b/python/sim/cbstate.py @@ -8,9 +8,10 @@ from threading import Condition, RLock, Thread from typing import List, Optional -from .typedefs import Size, Index, Count, Span + from .errors import CBContractError, CBNotConfigured from .ttnnsim import Tensor +from .typedefs import Count, Index, Size, Span # Type alias for circular buffer slots CBSlot = Optional[Tensor] diff --git a/python/sim/constants.py b/python/sim/constants.py index e630d814..ee49ae8b 100644 --- a/python/sim/constants.py +++ b/python/sim/constants.py @@ -6,10 +6,11 @@ Constants for the cbsim module. """ from typing import Any, cast -from .typedefs import Shape, CBID -from pydantic.fields import FieldInfo from annotated_types import Lt # type that holds the 'lt' constraint +from pydantic.fields import FieldInfo + +from .typedefs import CBID, Shape def _extract_max_cbs_from_cbid() -> int: diff --git a/python/sim/copy.py b/python/sim/copy.py index 9438fa23..453fb3da 100644 --- a/python/sim/copy.py +++ b/python/sim/copy.py @@ -10,9 +10,9 @@ """ from .copyhandlers import ( - CopyTransferHandler, CopyEndpoint, CopyEndpointType, + CopyTransferHandler, handler_registry, ) diff --git a/python/sim/copyhandlers.py b/python/sim/copyhandlers.py index a05fa08f..4b944e3c 100644 --- a/python/sim/copyhandlers.py +++ b/python/sim/copyhandlers.py @@ -9,26 +9,26 @@ @register_copy_handler. """ +import threading +import time +from collections import deque from typing import ( + TYPE_CHECKING, Any, + Deque, Dict, + List, Protocol, Tuple, Type, - Deque, - List, - Union, TypedDict, - TYPE_CHECKING, + Union, ) -from collections import deque -import threading -import time -from .typedefs import Count, Shape -from .ttnnsim import Tensor + from .block import Block -from .constants import TILE_SHAPE, COPY_PIPE_TIMEOUT -from .typedefs import Pipe, Count +from .constants import COPY_PIPE_TIMEOUT, TILE_SHAPE +from .ttnnsim import Tensor +from .typedefs import Count, Pipe, Shape if TYPE_CHECKING: from .cb import ReserveContext, WaitContext diff --git a/python/sim/decorators.py b/python/sim/decorators.py index 0fd82b1a..3e30d49c 100644 --- a/python/sim/decorators.py +++ b/python/sim/decorators.py @@ -8,8 +8,8 @@ operations within the simulation framework. """ -from typing import Any, Callable, Dict from types import FunctionType +from typing import Any, Callable, Dict from .program import BindableTemplate, rebind_func_with_ctx diff --git a/python/sim/kernel.py b/python/sim/kernel.py index da76d1b2..54469d40 100644 --- a/python/sim/kernel.py +++ b/python/sim/kernel.py @@ -8,9 +8,10 @@ specified grid configurations and granularity settings. """ -from typing import Any, Callable, Union, Tuple, List, cast import inspect -from .typedefs import Shape, Size, CoreIndex, Index +from typing import Any, Callable, List, Tuple, Union, cast + +from .typedefs import CoreIndex, Index, Shape, Size def _get_from_frame(var_name: str, error_msg: str) -> Any: diff --git a/python/sim/pipe.py b/python/sim/pipe.py index 2dfeafec..f13a15b7 100644 --- a/python/sim/pipe.py +++ b/python/sim/pipe.py @@ -5,9 +5,10 @@ Pipe utilities for conditional execution based on pipe roles. """ -from typing import Callable, Union, List, Optional, Iterator, Generator, Any -from .typedefs import Pipe, CoreIndex +from typing import Any, Callable, Generator, Iterator, List, Optional, Union + from .kernel import core, flatten_core_index +from .typedefs import CoreIndex, Pipe # TODO: Core Ranges should probably be their own type diff --git a/python/sim/program.py b/python/sim/program.py index bec0d7b0..5a082e98 100644 --- a/python/sim/program.py +++ b/python/sim/program.py @@ -8,19 +8,18 @@ functions across multiple cores with proper context binding and error handling. """ -import types import copy -import traceback import inspect import textwrap -from typing import Any, Callable, Dict, List, Tuple, Protocol, Generator +import traceback +import types from types import CellType, FunctionType - +from typing import Any, Callable, Dict, Generator, List, Protocol, Tuple from .cb import CircularBuffer from .cbapi import CBAPI -from .xformyield import transform_wait_reserve_to_yield from .ttnnsim import Tensor +from .xformyield import transform_wait_reserve_to_yield # Protocol for templates that have a bind method diff --git a/python/sim/tensoraccessor.py b/python/sim/tensoraccessor.py index ce9932a9..4dc9b365 100644 --- a/python/sim/tensoraccessor.py +++ b/python/sim/tensoraccessor.py @@ -6,6 +6,7 @@ """ from typing import Tuple + import torch from .constants import TILE_SHAPE diff --git a/python/sim/torch_utils.py b/python/sim/torch_utils.py index 8d79bea3..a715db8f 100644 --- a/python/sim/torch_utils.py +++ b/python/sim/torch_utils.py @@ -47,8 +47,10 @@ assert tu.all_true(tensor > 0) """ +from typing import List, Union + import torch -from typing import Union, List + from .typedefs import Count, Shape diff --git a/python/sim/ttnnsim.py b/python/sim/ttnnsim.py index 73236a3e..2708b05d 100644 --- a/python/sim/ttnnsim.py +++ b/python/sim/ttnnsim.py @@ -18,12 +18,13 @@ from __future__ import annotations -from typing import Any, Optional, Tuple, Union, cast, List +from typing import Any, List, Optional, Tuple, Union, cast + import torch -from .tensoraccessor import TensorAccessor -from .typedefs import IndexType, Shape, Count from .constants import TILE_SHAPE +from .tensoraccessor import TensorAccessor +from .typedefs import Count, IndexType, Shape # Public constants (mirror TTL constants) TILE_SIZE: int = TILE_SHAPE[0] diff --git a/python/sim/typedefs.py b/python/sim/typedefs.py index c7cb91fd..1df5f8cd 100644 --- a/python/sim/typedefs.py +++ b/python/sim/typedefs.py @@ -6,10 +6,11 @@ Type aliases with Pydantic constraints for runtime validation. """ -from typing import Annotated, Tuple, NamedTuple, Union -from pydantic import Field -from enum import Enum, auto from dataclasses import dataclass +from enum import Enum, auto +from typing import Annotated, NamedTuple, Tuple, Union + +from pydantic import Field # TODO: Expand IndexType as needed, see relevant issue: diff --git a/python/ttlang/__init__.py b/python/ttlang/__init__.py index fd1424de..a9d22fc9 100644 --- a/python/ttlang/__init__.py +++ b/python/ttlang/__init__.py @@ -9,19 +9,19 @@ # Export TTL DSL API module (decorators) from ttlang import ttl +# Export generated elementwise operators (auto-generated from TTLElementwiseOps.def) +from ttlang._generated_elementwise import * # noqa: F401,F403 +from ttlang._generated_elementwise import __all__ as _elementwise_all +from ttlang.circular_buffer import make_circular_buffer_like + # Export TTL DSL API from ttlang.ttl_api import ( - Program, CircularBuffer, - TensorBlock, - Semaphore, CopyTransferHandler, + Program, + Semaphore, + TensorBlock, ) -from ttlang.circular_buffer import make_circular_buffer_like - -# Export generated elementwise operators (auto-generated from TTLElementwiseOps.def) -from ttlang._generated_elementwise import * # noqa: F401,F403 -from ttlang._generated_elementwise import __all__ as _elementwise_all __all__ = [ "ttl", diff --git a/python/ttlang/_mlir_libs/__init__.py b/python/ttlang/_mlir_libs/__init__.py index ce2dde56..a5d8a2d2 100644 --- a/python/ttlang/_mlir_libs/__init__.py +++ b/python/ttlang/_mlir_libs/__init__.py @@ -7,9 +7,6 @@ # shipping a duplicate binary. import sys -from ttmlir._mlir_libs import ( # type: ignore - _mlir, - get_dialect_registry, -) +from ttmlir._mlir_libs import _mlir, get_dialect_registry # type: ignore sys.modules[__name__ + "._mlir"] = _mlir diff --git a/python/ttlang/_src/ttl_ast.py b/python/ttlang/_src/ttl_ast.py index a77ae146..11e82707 100644 --- a/python/ttlang/_src/ttl_ast.py +++ b/python/ttlang/_src/ttl_ast.py @@ -4,22 +4,20 @@ import ast import inspect -from typing import List, Set from dataclasses import dataclass - -from ttmlir.ir import * -from ttmlir.dialects import ttcore, func, arith, ttkernel +from typing import List, Set from pykernel._src.kernel_ast import TTCompilerBase -from .tensor_registry import get_tensor_global_index, get_tensor_source +from ttmlir.dialects import arith, func, ttcore, ttkernel +from ttmlir.ir import * -from ..dialects import ttl -from ..dtype_utils import is_ttnn_tensor -from ..layouts import create_ttnn_layout, TTNNLayoutConfig -from ..dtype_utils import tensor_dtype_to_ttcore_datatype from ..constants import DEFAULT_TILE_SIZE -from ..ttl_utils import get_thread_type_string from ..diagnostics import TTLangCompileError +from ..dialects import ttl +from ..dtype_utils import is_ttnn_tensor, tensor_dtype_to_ttcore_datatype +from ..layouts import TTNNLayoutConfig, create_ttnn_layout +from ..ttl_utils import get_thread_type_string +from .tensor_registry import get_tensor_global_index, get_tensor_source def _make_file_loc(ctx, source_file: str, node, line_offset: int = 0) -> Location: diff --git a/python/ttlang/circular_buffer.py b/python/ttlang/circular_buffer.py index 981ca73e..ebcb11c1 100644 --- a/python/ttlang/circular_buffer.py +++ b/python/ttlang/circular_buffer.py @@ -4,12 +4,12 @@ """Circular buffer operations for inter-thread communication.""" -from typing import Tuple, Any +from typing import Any, Tuple from ttmlir.ir import * -from .dialects import ttl from ._src.ttl_ast import syntax +from .dialects import ttl # Module-level counter for CB index assignment in creation order _cb_index_counter = 0 diff --git a/python/ttlang/dialects/ttl.py b/python/ttlang/dialects/ttl.py index 059064d3..ff797d28 100644 --- a/python/ttlang/dialects/ttl.py +++ b/python/ttlang/dialects/ttl.py @@ -4,11 +4,12 @@ """TTL (TT-Lang) dialect Python bindings.""" -from ._ttl_ops_gen import * # noqa: F401,F403 -from ._ttl_enum_gen import * # noqa: F401,F403 +from ttmlir._mlir_libs import get_dialect_registry + from .._mlir_libs import _ttlang from .._mlir_libs._ttlang import ttl_ir as ir -from ttmlir._mlir_libs import get_dialect_registry +from ._ttl_enum_gen import * # noqa: F401,F403 +from ._ttl_ops_gen import * # noqa: F401,F403 def ensure_dialects_registered(ctx): diff --git a/python/ttlang/layouts.py b/python/ttlang/layouts.py index 46cb7e4a..c3d52bf0 100644 --- a/python/ttlang/layouts.py +++ b/python/ttlang/layouts.py @@ -4,8 +4,8 @@ """Layout creation utilities for tensor distribution across cores.""" -from typing import List from dataclasses import dataclass +from typing import List from ttmlir.dialects import ttcore, ttnn diff --git a/python/ttlang/operators.py b/python/ttlang/operators.py index 496a2851..0da2fd85 100644 --- a/python/ttlang/operators.py +++ b/python/ttlang/operators.py @@ -10,12 +10,11 @@ from ttmlir.ir import Type -from .dialects import ttl -from ._src.ttl_ast import syntax - # Re-export generated elementwise operations from ._generated_elementwise import * # noqa: F401,F403 from ._generated_elementwise import __all__ as _generated_all +from ._src.ttl_ast import syntax +from .dialects import ttl # Type aliases for common patterns CoreCoordinate = Tuple[int, int] diff --git a/python/ttlang/semaphore.py b/python/ttlang/semaphore.py index 89516e53..f28fa9d5 100644 --- a/python/ttlang/semaphore.py +++ b/python/ttlang/semaphore.py @@ -6,10 +6,10 @@ from typing import Optional, Tuple +from pykernel._src.utils import _asindex from ttmlir.dialects import d2m from ._src.ttl_ast import syntax -from pykernel._src.utils import _asindex @syntax("!d2m.semaphore") diff --git a/python/ttlang/ttl.py b/python/ttlang/ttl.py index 2faf1648..56c359c0 100644 --- a/python/ttlang/ttl.py +++ b/python/ttlang/ttl.py @@ -6,11 +6,8 @@ TTL DSL module providing decorator APIs (@ttl.kernel(), @ttl.compute(), @ttl.datamovement()) """ -from ttlang.ttl_api import ( - pykernel_gen as kernel, - compute, - datamovement, -) +from ttlang.ttl_api import compute, datamovement +from ttlang.ttl_api import pykernel_gen as kernel __all__ = [ "kernel", diff --git a/python/ttlang/ttl_api.py b/python/ttlang/ttl_api.py index 23ae880c..c4665fe5 100644 --- a/python/ttlang/ttl_api.py +++ b/python/ttlang/ttl_api.py @@ -7,51 +7,50 @@ from __future__ import annotations import ast -import inspect import functools +import inspect import os -from typing import List, Optional, Callable, Dict, Union +from typing import Callable, Dict, List, Optional, Union try: import ttnn except ModuleNotFoundError: ttnn = None -from ttmlir.ir import * -from ttmlir.passmanager import PassManager +import ttlang._mlir_libs._ttlang # Register tt-lang passes +from pykernel._src.utils import _cleanup_source_code from ttmlir.dialects import ttkernel +from ttmlir.ir import * from ttmlir.passes import ( - ttkernel_to_cpp_by_name, - get_ttkernel_names, get_ttkernel_arg_spec, + get_ttkernel_names, + ttkernel_to_cpp_by_name, ) +from ttmlir.passmanager import PassManager -import ttlang._mlir_libs._ttlang # Register tt-lang passes - -from .ttl_utils import get_thread_type_string - - -from pykernel._src.utils import _cleanup_source_code from ._src.tensor_registry import ( - register_tensor_name, - register_tensor_source, get_tensor_global_index, get_tensor_source, + register_tensor_name, + register_tensor_source, ) -from .diagnostics import find_variable_assignment - from ._src.ttl_ast import TTLGenericCompiler -from .diagnostics import format_mlir_error, format_python_error, TTLangCompileError - -from .operators import TensorBlock, CopyTransferHandler, copy from .circular_buffer import CircularBuffer -from .semaphore import Semaphore +from .constants import SUPPORTED_MEMORY_SPACES +from .diagnostics import ( + TTLangCompileError, + find_variable_assignment, + format_mlir_error, + format_python_error, +) from .dtype_utils import ( - torch_dtype_to_ttnn_datatype, - tile_bytes_from_dtype, is_ttnn_tensor, + tile_bytes_from_dtype, + torch_dtype_to_ttnn_datatype, ) -from .constants import SUPPORTED_MEMORY_SPACES +from .operators import CopyTransferHandler, TensorBlock, copy +from .semaphore import Semaphore +from .ttl_utils import get_thread_type_string class CompilerConfig: @@ -269,8 +268,8 @@ def _write_kernel_to_tmp( name: str, source: str, num_tensors: int = 0, tensor_indices: list = None ) -> str: """Write kernel source to /tmp and return the file path.""" - import re import hashlib + import re # TODO(XX): Fix TensorAccessorArgs CTA offsets. C++ emits placeholder 42+idx, # replace with actual offset = global_idx + num_tensors (CB indices occupy 0..num_tensors-1). diff --git a/python/ttlang/utils/__init__.py b/python/ttlang/utils/__init__.py index f8324c6e..735821bf 100644 --- a/python/ttlang/utils/__init__.py +++ b/python/ttlang/utils/__init__.py @@ -4,13 +4,8 @@ """Utility functions for tt-lang.""" -from .block_allocation import split_work_to_cores, get_large_matmul_params - -from .correctness import ( - assert_pcc, - assert_allclose, - assert_with_ulp, -) +from .block_allocation import split_work_to_cores +from .correctness import assert_allclose, assert_pcc, assert_with_ulp __all__ = [ # block_allocation diff --git a/python/ttlang/utils/block_allocation.py b/python/ttlang/utils/block_allocation.py index 72a01dc0..9e0415d1 100644 --- a/python/ttlang/utils/block_allocation.py +++ b/python/ttlang/utils/block_allocation.py @@ -1,11 +1,9 @@ # SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 -from typing import Tuple, List import itertools import math -import sympy -from collections import namedtuple +from typing import List, Tuple def remove_leading_ones(grid: Tuple[int, ...]) -> Tuple[int, ...]: diff --git a/python/ttlang/utils/correctness.py b/python/ttlang/utils/correctness.py index 5d539d38..f877d625 100644 --- a/python/ttlang/utils/correctness.py +++ b/python/ttlang/utils/correctness.py @@ -4,9 +4,10 @@ """Utilities for comparing tensor outputs in tests.""" -import torch import math +import torch + def assert_pcc(golden, actual, threshold=0.99): """ diff --git a/test/bindings/python/test_ttl_ttcore_integration.py b/test/bindings/python/test_ttl_ttcore_integration.py index d7697b8a..187b05af 100644 --- a/test/bindings/python/test_ttl_ttcore_integration.py +++ b/test/bindings/python/test_ttl_ttcore_integration.py @@ -5,9 +5,9 @@ # Pytest for the ttlang python bindings and the ability to use # ttmlir and ttlang bindings together. +from ttlang.dialects import ttl from ttmlir import ir as tmlir_ir from ttmlir.dialects import ttcore -from ttlang.dialects import ttl def test_ttl_and_ttcore_same_context(): diff --git a/test/bindings/python/ttl_autoregistration.py b/test/bindings/python/ttl_autoregistration.py index 746fc162..d848caf8 100644 --- a/test/bindings/python/ttl_autoregistration.py +++ b/test/bindings/python/ttl_autoregistration.py @@ -6,8 +6,8 @@ # Verify ttl site-init registers the dialect so SliceAttr works without # calling ensure_dialects_registered explicitly. -from ttmlir import ir as tmlir_ir from ttlang.dialects import ttl +from ttmlir import ir as tmlir_ir with tmlir_ir.Context() as ctx, tmlir_ir.Location.unknown(): ttl.ensure_dialects_registered(ctx) diff --git a/test/bindings/python/ttl_bindings.py b/test/bindings/python/ttl_bindings.py index 23155cc8..c15c2236 100644 --- a/test/bindings/python/ttl_bindings.py +++ b/test/bindings/python/ttl_bindings.py @@ -6,9 +6,9 @@ # Tests for TTL dialect Python bindings, using both ttmlir and ttlang modules -from ttmlir.ir import * -from ttmlir.dialects import ttcore from ttlang.dialects import ttl +from ttmlir.dialects import ttcore +from ttmlir.ir import * with Context() as ctx, Location.unknown(): ttl.ensure_dialects_registered(ctx) diff --git a/test/bindings/python/ttl_ttcore_ttnn_integration.py b/test/bindings/python/ttl_ttcore_ttnn_integration.py index adfd4d66..6c24ea1c 100644 --- a/test/bindings/python/ttl_ttcore_ttnn_integration.py +++ b/test/bindings/python/ttl_ttcore_ttnn_integration.py @@ -5,9 +5,9 @@ # # Ensure ttl, ttcore, and ttnn dialects all work in the shared tt-mlir context. +from ttlang.dialects import ttl from ttmlir import ir as tmlir_ir from ttmlir.dialects import ttcore, ttnn -from ttlang.dialects import ttl with tmlir_ir.Context() as ctx, tmlir_ir.Location.unknown(): ttl.ensure_dialects_registered(ctx) diff --git a/test/lit.cfg.py b/test/lit.cfg.py index 5c15cbd5..67494fa6 100644 --- a/test/lit.cfg.py +++ b/test/lit.cfg.py @@ -3,8 +3,8 @@ # SPDX-License-Identifier: Apache-2.0 import os -import sys import platform +import sys import lit.formats import lit.util diff --git a/test/python/debug_locations.py b/test/python/debug_locations.py index 96d51e4f..00be2b5c 100644 --- a/test/python/debug_locations.py +++ b/test/python/debug_locations.py @@ -18,9 +18,9 @@ os.environ["TTLANG_COMPILE_ONLY"] = "1" os.environ["TTLANG_DEBUG_LOCATIONS"] = "1" -from ttlang import ttl, make_circular_buffer_like -from ttlang.ttl_api import Program +from ttlang import make_circular_buffer_like, ttl from ttlang.operators import copy +from ttlang.ttl_api import Program try: import ttnn diff --git a/test/python/invalid/invalid_copy_no_wait.py b/test/python/invalid/invalid_copy_no_wait.py index 75fc2e83..f2cdd3fc 100644 --- a/test/python/invalid/invalid_copy_no_wait.py +++ b/test/python/invalid/invalid_copy_no_wait.py @@ -19,9 +19,9 @@ os.environ["TTLANG_COMPILE_ONLY"] = "1" -from ttlang import ttl, make_circular_buffer_like -from ttlang.ttl_api import Program +from ttlang import make_circular_buffer_like, ttl from ttlang.operators import copy +from ttlang.ttl_api import Program try: import ttnn diff --git a/test/python/simple_add_f32.py b/test/python/simple_add_f32.py index 5f0db3a4..8ce0a56a 100644 --- a/test/python/simple_add_f32.py +++ b/test/python/simple_add_f32.py @@ -16,9 +16,9 @@ os.environ["TTLANG_COMPILE_ONLY"] = "1" -from ttlang import ttl, make_circular_buffer_like -from ttlang.ttl_api import Program +from ttlang import make_circular_buffer_like, ttl from ttlang.operators import copy +from ttlang.ttl_api import Program try: import ttnn diff --git a/test/python/utils/test_block_allocation.py b/test/python/utils/test_block_allocation.py index d7763d83..22b81e07 100644 --- a/test/python/utils/test_block_allocation.py +++ b/test/python/utils/test_block_allocation.py @@ -8,8 +8,7 @@ get_large_matmul_params is compared with hard coded expected values """ import pytest - -from ttlang.utils.block_allocation import split_work_to_cores, get_large_matmul_params +from ttlang.utils.block_allocation import split_work_to_cores ttnn = pytest.importorskip("ttnn") diff --git a/test/sim/test_cb.py b/test/sim/test_cb.py index 96e75cde..a1479236 100644 --- a/test/sim/test_cb.py +++ b/test/sim/test_cb.py @@ -9,18 +9,19 @@ """ import pytest -from python.sim.cb import CircularBuffer -from python.sim.cbapi import CBAPI -from python.sim import ttnn, TILE_SHAPE, copy -from python.sim.errors import CBContractError from test_utils import ( + make_ones_tensor, make_ones_tile, - make_zeros_tile, make_rand_tensor, make_zeros_tensor, - make_ones_tensor, + make_zeros_tile, ) +from python.sim import TILE_SHAPE, copy, ttnn +from python.sim.cb import CircularBuffer +from python.sim.cbapi import CBAPI +from python.sim.errors import CBContractError + @pytest.fixture def api(): diff --git a/test/sim/test_cbapi.py b/test/sim/test_cbapi.py index 9a6916bf..0c5fdffa 100644 --- a/test/sim/test_cbapi.py +++ b/test/sim/test_cbapi.py @@ -2,17 +2,18 @@ # # SPDX-License-Identifier: Apache-2.0 -import pytest import threading import time from typing import List, Tuple +import pytest +from test_utils import make_full_tensor, tensors_exact_equal + +from python.sim.cb import CircularBuffer from python.sim.cbapi import CBAPI +from python.sim.cbstate import CBSlot from python.sim.errors import CBContractError, CBTimeoutError from python.sim.typedefs import CBID -from python.sim.cb import CircularBuffer -from python.sim.cbstate import CBSlot -from test_utils import make_full_tensor, tensors_exact_equal # Pytest fixtures to reduce redundant setup code diff --git a/test/sim/test_copy.py b/test/sim/test_copy.py index b32668fb..e308fcfe 100644 --- a/test/sim/test_copy.py +++ b/test/sim/test_copy.py @@ -8,20 +8,22 @@ including error handling and edge cases. """ -import pytest -from python.sim.copy import CopyTransaction, copy -from python.sim.block import Block, Span -from python.sim.cbstate import CBSlot from typing import List -from python.sim.typedefs import Pipe + +import pytest from test_utils import ( - make_ones_tile, - make_zeros_tile, make_full_tile, + make_ones_tile, make_rand_tensor, + make_zeros_tile, tensors_equal, ) +from python.sim.block import Block, Span +from python.sim.cbstate import CBSlot +from python.sim.copy import CopyTransaction, copy +from python.sim.typedefs import Pipe + class TestCopyTransaction: """Test CopyTransaction class functionality.""" diff --git a/test/sim/test_copyhandlers.py b/test/sim/test_copyhandlers.py index 3fabda5d..691f20e7 100644 --- a/test/sim/test_copyhandlers.py +++ b/test/sim/test_copyhandlers.py @@ -2,29 +2,31 @@ # # SPDX-License-Identifier: Apache-2.0 -import pytest from typing import List -from python.sim.copyhandlers import ( - handler_registry, - TensorToBlockHandler, - BlockToTensorHandler, - BlockToPipeHandler, - PipeToBlockHandler, -) -from python.sim.block import Block, Span -from python.sim.cbstate import CBSlot -from python.sim.typedefs import Pipe -from python.sim import ttnn -from python.sim.constants import TILE_SHAPE + +import pytest from test_utils import ( - make_ones_tile, - make_zeros_tile, + make_arange_tensor, make_full_tile, + make_ones_tile, make_rand_tensor, - make_arange_tensor, + make_zeros_tile, tensors_equal, ) +from python.sim import ttnn +from python.sim.block import Block, Span +from python.sim.cbstate import CBSlot +from python.sim.constants import TILE_SHAPE +from python.sim.copyhandlers import ( + BlockToPipeHandler, + BlockToTensorHandler, + PipeToBlockHandler, + TensorToBlockHandler, + handler_registry, +) +from python.sim.typedefs import Pipe + class TestHandlerRegistry: """Test the handler registry mechanism.""" diff --git a/test/sim/test_examples.py b/test/sim/test_examples.py index fa43d4d6..f80b817d 100644 --- a/test/sim/test_examples.py +++ b/test/sim/test_examples.py @@ -17,11 +17,12 @@ from eltwise_add import eltwise_add # type: ignore[import-not-found] from eltwise_pipe import eltwise_pipe # type: ignore[import-not-found] from eltwise_pipe_core3 import eltwise_pipe_core3 # type: ignore[import-not-found] -from singlecore_matmul import tt_lang_singlecore_matmul # type: ignore[import-not-found] from multicore_matmul import tt_lang_multicore_matmul # type: ignore[import-not-found] - from sim import ttnn from sim.testing import assert_pcc +from singlecore_matmul import ( + tt_lang_singlecore_matmul, # type: ignore[import-not-found] +) class TestExamples: diff --git a/test/sim/test_kernel.py b/test/sim/test_kernel.py index 0d7b3ded..226cc0a9 100644 --- a/test/sim/test_kernel.py +++ b/test/sim/test_kernel.py @@ -5,12 +5,14 @@ Tests for kernel.py module (kernel decorator, grid_size, etc.). """ -import pytest from typing import cast + +import pytest +from test_utils import make_zeros_tensor + from python.sim import ttl, ttnn from python.sim.kernel import flatten_core_index from python.sim.typedefs import Shape -from test_utils import make_zeros_tensor class TestGridSize: diff --git a/test/sim/test_program.py b/test/sim/test_program.py index f671a311..318e029f 100644 --- a/test/sim/test_program.py +++ b/test/sim/test_program.py @@ -11,23 +11,17 @@ - Multi-core execution """ +from typing import cast + import pytest import torch -from typing import cast -from python.sim import ( - ttl, - TILE_SHAPE, - copy, - ttnn, -) -from python.sim.program import ( - Program, - rebind_func_with_ctx, - _make_cell, # type: ignore[reportPrivateUsage] -) import torch.testing as tt_testing from test_utils import make_ones_tensor, make_zeros_tensor +from python.sim import TILE_SHAPE, copy, ttl, ttnn +from python.sim.program import _make_cell # type: ignore[reportPrivateUsage] +from python.sim.program import Program, rebind_func_with_ctx + class TestBasicExecution: """Test basic execution in cooperative mode.""" diff --git a/test/sim/test_tensor_accessor.py b/test/sim/test_tensor_accessor.py index 6507397c..1bb07b9d 100644 --- a/test/sim/test_tensor_accessor.py +++ b/test/sim/test_tensor_accessor.py @@ -7,9 +7,10 @@ import pytest import torch +from test_utils import all_equal, make_full, make_ones, make_randn, make_zeros + from python.sim.tensoraccessor import TensorAccessor from python.sim.typedefs import IndexType -from test_utils import make_randn, make_zeros, make_ones, make_full, all_equal class TestTensorAccessor: diff --git a/test/sim/test_ttnnsim.py b/test/sim/test_ttnnsim.py index 5d658e9f..35f26489 100644 --- a/test/sim/test_ttnnsim.py +++ b/test/sim/test_ttnnsim.py @@ -1,10 +1,10 @@ # SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 -import torch -import pytest from typing import Any +import pytest +import torch from sim import ttnn diff --git a/test/sim/test_utils.py b/test/sim/test_utils.py index f93f3e84..466c7822 100644 --- a/test/sim/test_utils.py +++ b/test/sim/test_utils.py @@ -5,8 +5,9 @@ """Common test utilities for simulator tests.""" import torch + from python.sim import ttnn -from python.sim.typedefs import Size, Shape +from python.sim.typedefs import Shape, Size # Constants TILE_SHAPE: Shape = (32, 32) diff --git a/test/sim/test_xformyield.py b/test/sim/test_xformyield.py index cacef5c1..f681d8e4 100644 --- a/test/sim/test_xformyield.py +++ b/test/sim/test_xformyield.py @@ -9,11 +9,12 @@ """ import ast + from python.sim.xformyield import ( WaitReserveToYieldTransformer, - YieldInserter, - YieldingFunctionMarker, YieldFromInserter, + YieldingFunctionMarker, + YieldInserter, transform_wait_reserve_to_yield, )