-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compile fixture to test integration ops with compile mode
- Loading branch information
1 parent
d37e4f8
commit f05b79f
Showing
34 changed files
with
302 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# | ||
# SPDX-FileCopyrightText: Copyright (c) 2024-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import pytest | ||
import inspect | ||
|
||
import tripy as tp | ||
|
||
|
||
@pytest.fixture(params=["compile", "eager"]) | ||
def execution_mode(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture | ||
def compile_fixture(execution_mode): | ||
def wrapper(func, *args, exclude_arg_names=None, **kwargs): | ||
def get_shape(x: tp.Tensor): | ||
x.eval() | ||
return tp.InputInfo(x.trace_tensor.shape, dtype=x.dtype) | ||
|
||
exclude_arg_names = set() if exclude_arg_names is None else set(exclude_arg_names) | ||
|
||
if execution_mode == "compile": | ||
# For args, we need to get their parameter names from function signature | ||
sig = inspect.signature(func) | ||
param_names = list(sig.parameters.keys()) | ||
|
||
compile_args = [] | ||
for i, arg in enumerate(args): | ||
if i < len(param_names) and param_names[i] in exclude_arg_names: | ||
compile_args.append(arg) | ||
elif isinstance(arg, tp.Tensor) and not isinstance(arg, tp.DimensionSize): | ||
compile_args.append(get_shape(arg)) | ||
else: | ||
compile_args.append(arg) | ||
compile_args = tuple(compile_args) | ||
|
||
compile_kwargs = dict( | ||
( | ||
k, | ||
( | ||
v | ||
if k in exclude_arg_names | ||
else (get_shape(v) if isinstance(v, tp.Tensor) and not isinstance(v, tp.DimensionSize) else v) | ||
), | ||
) | ||
for k, v in kwargs.items() | ||
) | ||
|
||
compiled_func = tp.compile(func, args=compile_args, kwargs=compile_kwargs) | ||
|
||
tensor_args = tuple( | ||
x | ||
for i, x in enumerate(args) | ||
if isinstance(x, tp.Tensor) | ||
and not isinstance(x, tp.DimensionSize) | ||
and (i >= len(param_names) or param_names[i] not in exclude_arg_names) | ||
) | ||
|
||
tensor_kwargs = { | ||
k: v | ||
for k, v in kwargs.items() | ||
if isinstance(v, tp.Tensor) and not isinstance(v, tp.DimensionSize) and k not in exclude_arg_names | ||
} | ||
|
||
return compiled_func(*tensor_args, **tensor_kwargs) | ||
|
||
elif execution_mode == "eager": | ||
return func(*args, **kwargs) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.