forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CINN] Add local var value for cinn ir::Tensor (PaddlePaddle#64459)
* cinn(backend): change slice's attribute from Int to Expr * add precision check * fix bug of cuda kernel name * fix bug * revert test_cinn_transform_symbolic --------- Co-authored-by: 6clc <chaoliu.lc@foxmail.com>
- Loading branch information
Showing
9 changed files
with
220 additions
and
63 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# 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 sys | ||
import unittest | ||
from os.path import dirname | ||
|
||
import numpy as np | ||
|
||
import paddle | ||
from paddle import nn | ||
from paddle.static import InputSpec | ||
|
||
sys.path.append(dirname(dirname(__file__))) | ||
|
||
import utils | ||
|
||
|
||
class CastLayer(nn.Layer): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, x): | ||
end = x.shape[1] - 1 | ||
return x[:, :end, :] | ||
|
||
|
||
class TestCast(unittest.TestCase): | ||
def setUp(self): | ||
paddle.seed(2022) | ||
self.prepare_data() | ||
|
||
def prepare_data(self): | ||
self.shape = [1024, 32, 1024, 17] | ||
self.x = paddle.randn(self.shape, dtype="float32") | ||
self.x.stop_gradient = True | ||
|
||
def check_jit_kernel_info(self, static_fn): | ||
utils.check_jit_kernel_number(static_fn, 1) | ||
utils.check_jit_kernel_structure(static_fn, {utils.JIT_KERNEL_NAME: 1}) | ||
|
||
def eval(self, use_cinn): | ||
net = CastLayer() | ||
input_spec = [ | ||
InputSpec(shape=[None, None, 1024, None], dtype='float32'), | ||
] | ||
net = utils.apply_to_static(net, use_cinn, input_spec) | ||
net.eval() | ||
out = net(self.x) | ||
if use_cinn: | ||
self.check_jit_kernel_info(net.forward) | ||
return out | ||
|
||
def test_eval(self): | ||
cinn_out = self.eval(use_cinn=True) | ||
dy_out = self.eval(use_cinn=False) | ||
np.testing.assert_allclose( | ||
cinn_out.numpy(), dy_out.numpy(), atol=1e-6, rtol=1e-6 | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |