# 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. """ UT for air_topp_sampling kernel """ import paddle import unittest import numpy as np import fastdeploy.model_executor.ops.gpu class Test(unittest.TestCase): def setUp(self): """ Initialize. """ paddle.seed(2024) np.random.seed(42) print(paddle.device.cuda.get_device_properties()) print(paddle.__git_commit__) def test_air_topp_sampling(self): """ Check air_topp_sampling output with paddle.tensor.top_p_sampling. """ prop = paddle.device.cuda.get_device_properties() cc = prop.major * 10 + prop.minor if cc < 89: self.skipTest("air_topp_sampling only support sm89+") x = paddle.randn([1, 100]) x = paddle.nn.functional.softmax(x) x = paddle.cast(x, "float32") top_ps = paddle.to_tensor(np.random.uniform(0, 1, [1]).astype(np.float32)) out = fastdeploy.model_executor.ops.gpu.air_topp_sampling( x.cuda(), top_ps.cuda(), None, None, seed=0, k=1, mode="truncated" ) if __name__ == "__main__": unittest.main()