diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Almaty b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Almaty new file mode 100644 index 0000000000000000000000000000000000000000..855abbd6e3ee338c3d46bee7b592f3d645d2c273 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Almaty differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Anadyr b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Anadyr new file mode 100644 index 0000000000000000000000000000000000000000..35c531c0709fabace97e52cacef74b4cfbbcd820 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Anadyr differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Brunei b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Brunei new file mode 100644 index 0000000000000000000000000000000000000000..098c6a0b0afc2c3ef3a6937104d270074ff2a9b6 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Brunei differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Chita b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Chita new file mode 100644 index 0000000000000000000000000000000000000000..75b3d7b3a6b3bf2b7459532fce1142912b897efd Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Chita differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Dili b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Dili new file mode 100644 index 0000000000000000000000000000000000000000..4614e4fc4353a7094ba5173a0bd09158ef49d9a9 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Dili differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Karachi b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Karachi new file mode 100644 index 0000000000000000000000000000000000000000..ba65c0e8d31cb1d59935878af29baac722f45936 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Karachi differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Omsk b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Omsk new file mode 100644 index 0000000000000000000000000000000000000000..16c5f3cfed75151d50ffc2dc483ac6279816a01f Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Omsk differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Taipei b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Taipei new file mode 100644 index 0000000000000000000000000000000000000000..24c43444b6751343d2915843d03e55753d4d7359 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Taipei differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Tel_Aviv b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Tel_Aviv new file mode 100644 index 0000000000000000000000000000000000000000..1ebd0664aa29c0abd722661f761031ec0304631c Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Tel_Aviv differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Thimbu b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Thimbu new file mode 100644 index 0000000000000000000000000000000000000000..95a9de9657f5b8b15aacb6e05c085c7e11bb8f55 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Thimbu differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Ust-Nera b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Ust-Nera new file mode 100644 index 0000000000000000000000000000000000000000..d05726aba9fd67bf230290b3e2d74b75e46ee214 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Ust-Nera differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Yakutsk b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Yakutsk new file mode 100644 index 0000000000000000000000000000000000000000..ae65a5f9b94d196b29b68ddec7862e098d829b59 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Yakutsk differ diff --git a/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Yerevan b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Yerevan new file mode 100644 index 0000000000000000000000000000000000000000..0d5f6853ac79c41fcb445a23062a27aca9e471f3 Binary files /dev/null and b/parrot/lib/python3.10/site-packages/pytz/zoneinfo/Asia/Yerevan differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/cache.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/cache.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..504241d1164e76e770cc62b1402aed6e12326af5 Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/cache.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/configuration.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/configuration.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..83b48c83f64f25d84a422f838115b961c89983ec Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/configuration.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/pyproject.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/pyproject.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8a5f58fbd67ee894cb32ecdfc7120ba3a0c84442 Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/pyproject.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/self_outdated_check.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/self_outdated_check.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6638163becfdeccc98fe7939d4c44db0b22bce71 Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/self_outdated_check.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/wheel_builder.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/wheel_builder.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e991a3c45066af7621a810aa50810162ab656c24 Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/__pycache__/wheel_builder.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/_jaraco_text.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/_jaraco_text.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8b0bdf76f6cd170ac61b90da74ca3c6757b18561 Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/_jaraco_text.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/datetime.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/datetime.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8ad025e4c84a619d1b6a4f5c8fdc75d241c289e6 Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/datetime.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/deprecation.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/deprecation.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bb4d684d1e694682555bf7364111337417fa463c Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/deprecation.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/misc.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/misc.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..41b3f6cbd94fd3ad2d8431072e7cd365330773e3 Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/misc.cpython-310.pyc differ diff --git a/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/retry.cpython-310.pyc b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/retry.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..fb84ee050c031977ee5371d609b38ed90fdc4f9f Binary files /dev/null and b/videollama2/lib/python3.10/site-packages/pip/_internal/utils/__pycache__/retry.cpython-310.pyc differ diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/copy_if.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/copy_if.h new file mode 100644 index 0000000000000000000000000000000000000000..c5f0ddc4a44246ff89ee40b054ab82005829268e --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/copy_if.h @@ -0,0 +1,30 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system inherits copy_if +#include diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/count.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/count.h new file mode 100644 index 0000000000000000000000000000000000000000..0968d886dc52f20a71c2cdb4d68a8a6d524dd053 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/count.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special version of this algorithm diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/generate.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/generate.h new file mode 100644 index 0000000000000000000000000000000000000000..0968d886dc52f20a71c2cdb4d68a8a6d524dd053 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/generate.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special version of this algorithm diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/merge.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/merge.h new file mode 100644 index 0000000000000000000000000000000000000000..e900f2c6ee1105031f4a200af57484439b2b201f --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/merge.h @@ -0,0 +1,30 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system inherits merge +#include diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/per_device_resource.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/per_device_resource.h new file mode 100644 index 0000000000000000000000000000000000000000..f02fcfcdc3ffb035a21d9189dfa72f235ff82146 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/per_device_resource.h @@ -0,0 +1,29 @@ +/* + * Copyright 2018 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special per device resource functions diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/sequence.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/sequence.h new file mode 100644 index 0000000000000000000000000000000000000000..0968d886dc52f20a71c2cdb4d68a8a6d524dd053 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/sequence.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special version of this algorithm diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/swap_ranges.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/swap_ranges.h new file mode 100644 index 0000000000000000000000000000000000000000..a0469f4e16a8bb2240142305fc9cc3003df10de7 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/swap_ranges.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// cpp has no special swap_ranges diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/tabulate.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/tabulate.h new file mode 100644 index 0000000000000000000000000000000000000000..0968d886dc52f20a71c2cdb4d68a8a6d524dd053 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/tabulate.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special version of this algorithm diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/unique_by_key.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/unique_by_key.h new file mode 100644 index 0000000000000000000000000000000000000000..5f4b9efc9ce710145ff5d3b3d3cbe01168077f47 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/unique_by_key.h @@ -0,0 +1,30 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system inherits unique_by_key +#include diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/vector.inl b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/vector.inl new file mode 100644 index 0000000000000000000000000000000000000000..0e3c2626ec0b784c34210883cd012abf8d58f70a --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cpp/detail/vector.inl @@ -0,0 +1,130 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header +#include + +#include + +THRUST_NAMESPACE_BEGIN +namespace system +{ +namespace cpp +{ + +template +vector::vector() + : super_t() +{} + +template +vector::vector(size_type n) + : super_t(n) +{} + +template +vector::vector(size_type n, const value_type& value) + : super_t(n, value) +{} + +template +vector::vector(const vector& x) + : super_t(x) +{} + +template +vector::vector(vector&& x) + : super_t(std::move(x)) +{} + +template +template +vector::vector(const thrust::detail::vector_base& x) + : super_t(x) +{} + +template +template +vector::vector(const std::vector& x) + : super_t(x) +{} + +template +template +vector::vector(InputIterator first, InputIterator last) + : super_t(first, last) +{} + +template +vector& vector::operator=(const vector& x) +{ + super_t::operator=(x); + return *this; +} + +template +vector& vector::operator=(vector&& x) +{ + super_t::operator=(std::move(x)); + return *this; +} + +template +vector::vector(std::initializer_list il) + : super_t(il) +{} + +template +vector::vector(std::initializer_list il, const Allocator& alloc) + : super_t(il, alloc) +{} + +template +vector& vector::operator=(std::initializer_list il) +{ + super_t::operator=(il); + return *this; +} + +template +template +vector& vector::operator=(const std::vector& x) +{ + super_t::operator=(x); + return *this; +} + +template +template +vector& vector::operator=(const thrust::detail::vector_base& x) +{ + super_t::operator=(x); + return *this; +} + +} // namespace cpp +} // namespace system +THRUST_NAMESPACE_END diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/adjacent_difference.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/adjacent_difference.h new file mode 100644 index 0000000000000000000000000000000000000000..7d74e0b3c963ea1ab14bc54d3afc8a8b22d5f163 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/adjacent_difference.h @@ -0,0 +1,224 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +# include + +THRUST_NAMESPACE_BEGIN + +template +_CCCL_HOST_DEVICE OutputIterator adjacent_difference( + const thrust::detail::execution_policy_base& exec, + InputIterator first, + InputIterator last, + OutputIterator result, + BinaryFunction binary_op); + +namespace cuda_cub +{ + +namespace __adjacent_difference +{ + +template +cudaError_t THRUST_RUNTIME_FUNCTION doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + InputIt first, + OutputIt result, + BinaryOp binary_op, + std::size_t num_items, + cudaStream_t stream) +{ + if (num_items == 0) + { + return cudaSuccess; + } + + constexpr bool may_alias = MayAlias; + constexpr bool read_left = true; + + using Dispatch32 = cub::DispatchAdjacentDifference; + using Dispatch64 = cub::DispatchAdjacentDifference; + + cudaError_t status; + THRUST_INDEX_TYPE_DISPATCH2( + status, + Dispatch32::Dispatch, + Dispatch64::Dispatch, + num_items, + (d_temp_storage, temp_storage_bytes, first, result, num_items_fixed, binary_op, stream)); + return status; +} + +template +cudaError_t THRUST_RUNTIME_FUNCTION doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + InputIt first, + OutputIt result, + BinaryOp binary_op, + std::size_t num_items, + cudaStream_t stream, + thrust::detail::integral_constant /* comparable */) +{ + constexpr bool may_alias = true; + return doit_step(d_temp_storage, temp_storage_bytes, first, result, binary_op, num_items, stream); +} + +template +cudaError_t THRUST_RUNTIME_FUNCTION doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + InputIt first, + OutputIt result, + BinaryOp binary_op, + std::size_t num_items, + cudaStream_t stream, + thrust::detail::integral_constant /* comparable */) +{ + // The documentation states that pointers might be equal but can't alias in + // any other way. That is, the distance should be equal to zero or exceed + // `num_items`. In the latter case, we use an optimized version. + if (first != result) + { + constexpr bool may_alias = false; + return doit_step(d_temp_storage, temp_storage_bytes, first, result, binary_op, num_items, stream); + } + + constexpr bool may_alias = true; + return doit_step(d_temp_storage, temp_storage_bytes, first, result, binary_op, num_items, stream); +} + +template +OutputIt THRUST_RUNTIME_FUNCTION +adjacent_difference(execution_policy& policy, InputIt first, InputIt last, OutputIt result, BinaryOp binary_op) +{ + const auto num_items = static_cast(thrust::distance(first, last)); + std::size_t storage_size = 0; + cudaStream_t stream = cuda_cub::stream(policy); + + using UnwrapInputIt = thrust::try_unwrap_contiguous_iterator_t; + using UnwrapOutputIt = thrust::try_unwrap_contiguous_iterator_t; + + using InputValueT = thrust::iterator_value_t; + using OutputValueT = thrust::iterator_value_t; + + constexpr bool can_compare_iterators = + ::cuda::std::is_pointer::value && ::cuda::std::is_pointer::value + && std::is_same::value; + + auto first_unwrap = thrust::try_unwrap_contiguous_iterator(first); + auto result_unwrap = thrust::try_unwrap_contiguous_iterator(result); + + thrust::detail::integral_constant comparable; + + cudaError_t status = + doit_step(nullptr, storage_size, first_unwrap, result_unwrap, binary_op, num_items, stream, comparable); + cuda_cub::throw_on_error(status, "adjacent_difference failed on 1st step"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, storage_size); + + status = doit_step( + static_cast(tmp.data().get()), + storage_size, + first_unwrap, + result_unwrap, + binary_op, + num_items, + stream, + comparable); + cuda_cub::throw_on_error(status, "adjacent_difference failed on 2nd step"); + + status = cuda_cub::synchronize_optional(policy); + cuda_cub::throw_on_error(status, "adjacent_difference failed to synchronize"); + + return result + num_items; +} + +} // namespace __adjacent_difference + +//------------------------- +// Thrust API entry points +//------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIt _CCCL_HOST_DEVICE +adjacent_difference(execution_policy& policy, InputIt first, InputIt last, OutputIt result, BinaryOp binary_op) +{ + THRUST_CDP_DISPATCH( + (result = __adjacent_difference::adjacent_difference(policy, first, last, result, binary_op);), + (result = thrust::adjacent_difference(cvt_to_seq(derived_cast(policy)), first, last, result, binary_op);)); + return result; +} + +template +OutputIt _CCCL_HOST_DEVICE +adjacent_difference(execution_policy& policy, InputIt first, InputIt last, OutputIt result) +{ + using input_type = typename iterator_traits::value_type; + return cuda_cub::adjacent_difference(policy, first, last, result, minus()); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +// +# include +# include +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/assign_value.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/assign_value.h new file mode 100644 index 0000000000000000000000000000000000000000..395dce9341f9d5739257238282f74af2c33cdd41 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/assign_value.h @@ -0,0 +1,97 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +inline _CCCL_HOST_DEVICE void +assign_value(thrust::cuda::execution_policy& exec, Pointer1 dst, Pointer2 src) +{ + // XXX war nvbugs/881631 + struct war_nvbugs_881631 + { + _CCCL_HOST inline static void + host_path(thrust::cuda::execution_policy& exec, Pointer1 dst, Pointer2 src) + { + cuda_cub::copy(exec, src, src + 1, dst); + } + + _CCCL_DEVICE inline static void + device_path(thrust::cuda::execution_policy&, Pointer1 dst, Pointer2 src) + { + *thrust::raw_pointer_cast(dst) = *thrust::raw_pointer_cast(src); + } + }; + + NV_IF_TARGET( + NV_IS_HOST, (war_nvbugs_881631::host_path(exec, dst, src);), (war_nvbugs_881631::device_path(exec, dst, src);)); + +} // end assign_value() + +template +inline _CCCL_HOST_DEVICE void assign_value(cross_system& systems, Pointer1 dst, Pointer2 src) +{ + // XXX war nvbugs/881631 + struct war_nvbugs_881631 + { + _CCCL_HOST inline static void host_path(cross_system& systems, Pointer1 dst, Pointer2 src) + { + // rotate the systems so that they are ordered the same as (src, dst) + // for the call to thrust::copy + cross_system rotated_systems = systems.rotate(); + cuda_cub::copy(rotated_systems, src, src + 1, dst); + } + + _CCCL_DEVICE inline static void device_path(cross_system&, Pointer1 dst, Pointer2 src) + { + // XXX forward the true cuda::execution_policy inside systems here + // instead of materializing a tag + thrust::cuda::tag cuda_tag; + thrust::cuda_cub::assign_value(cuda_tag, dst, src); + } + }; + + NV_IF_TARGET(NV_IS_HOST, + (war_nvbugs_881631::host_path(systems, dst, src);), + (war_nvbugs_881631::device_path(systems, dst, src);)); +} // end assign_value() + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/binary_search.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/binary_search.h new file mode 100644 index 0000000000000000000000000000000000000000..5193bbce77fca4d5527bc7c4703fc7c07897c6f2 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/binary_search.h @@ -0,0 +1,29 @@ +/* + * Copyright 2021 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special version of this algorithm diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/cdp_dispatch.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/cdp_dispatch.h new file mode 100644 index 0000000000000000000000000000000000000000..d17aa066838f42d1863f21e2bead3be80916b1db --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/cdp_dispatch.h @@ -0,0 +1,97 @@ +/* + * Copyright 2021-2022 NVIDIA Corporation + * + * 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. + */ + +/** + * \file + * Utilities for CUDA dynamic parallelism. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include + +#include + +#include + +/** + * \def THRUST_CDP_DISPATCH + * + * If CUDA Dynamic Parallelism / CUDA Nested Parallelism is available, always + * run the parallel implementation. Otherwise, run the parallel implementation + * when called from the host, and fallback to the sequential implementation on + * the device. + * + * `par_impl` and `seq_impl` are blocks of C++ statements enclosed in + * parentheses, similar to NV_IF_TARGET blocks: + * + * \code + * THRUST_CDP_DISPATCH((launch_parallel_kernel();), (run_serial_impl();)); + * \endcode + */ + +#if defined(CUB_DETAIL_CDPv1) + +// Special case for NVCC -- need to inform the device path about the kernels +// that are launched from the host path. +# if defined(__CUDACC__) && defined(__CUDA_ARCH__) + +// seq_impl only used on platforms that do not support device synchronization. +# define THRUST_CDP_DISPATCH(par_impl, seq_impl) \ + if (false) \ + { /* Without this, the device pass won't compile any kernels. */ \ + NV_IF_TARGET(NV_ANY_TARGET, par_impl); \ + } \ + NV_IF_TARGET(NV_PROVIDES_SM_90, seq_impl, par_impl) + +# else // NVCC device pass + +// seq_impl only used on platforms that do not support device synchronization. +# define THRUST_CDP_DISPATCH(par_impl, seq_impl) NV_IF_TARGET(NV_PROVIDES_SM_90, seq_impl, par_impl) + +# endif // NVCC device pass + +#else // CDPv1 unavailable. Always fallback to serial on device: + +// Special case for NVCC -- need to inform the device path about the kernels +// that are launched from the host path. +# if defined(__CUDACC__) && defined(__CUDA_ARCH__) + +// Device-side launch not supported, fallback to sequential in device code. +# define THRUST_CDP_DISPATCH(par_impl, seq_impl) \ + if (false) \ + { /* Without this, the device pass won't compile any kernels. */ \ + NV_IF_TARGET(NV_ANY_TARGET, par_impl); \ + } \ + NV_IF_TARGET(NV_IS_HOST, par_impl, seq_impl) + +# else // !(NVCC device pass): + +# define THRUST_CDP_DISPATCH(par_impl, seq_impl) NV_IF_TARGET(NV_IS_HOST, par_impl, seq_impl) + +# endif // NVCC device pass + +#endif // CDP version diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/copy.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/copy.h new file mode 100644 index 0000000000000000000000000000000000000000..857af1eceec33713dfae2b0cd4c44981677229b8 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/copy.h @@ -0,0 +1,129 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include + +#include +#include +#include +#include + +THRUST_NAMESPACE_BEGIN + +template +_CCCL_HOST_DEVICE OutputIt +copy(const thrust::detail::execution_policy_base& exec, InputIt first, InputIt last, OutputIt result); + +template +_CCCL_HOST_DEVICE OutputIt +copy_n(const thrust::detail::execution_policy_base& exec, InputIt first, Size n, OutputIt result); + +namespace cuda_cub +{ + +// D->D copy requires NVCC compiler +template +OutputIterator _CCCL_HOST_DEVICE +copy(execution_policy& system, InputIterator first, InputIterator last, OutputIterator result); + +template +OutputIterator _CCCL_HOST +copy(cross_system systems, InputIterator first, InputIterator last, OutputIterator result); + +template +OutputIterator _CCCL_HOST_DEVICE +copy_n(execution_policy& system, InputIterator first, Size n, OutputIterator result); + +template +OutputIterator _CCCL_HOST +copy_n(cross_system systems, InputIterator first, Size n, OutputIterator result); + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +#include +#include +#include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +#if _CCCL_HAS_CUDA_COMPILER +// D->D copy requires NVCC compiler + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIterator _CCCL_HOST_DEVICE +copy(execution_policy& system, InputIterator first, InputIterator last, OutputIterator result) +{ + THRUST_CDP_DISPATCH((result = __copy::device_to_device(system, first, last, result);), + (result = thrust::copy(cvt_to_seq(derived_cast(system)), first, last, result);)); + return result; +} // end copy() + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIterator _CCCL_HOST_DEVICE +copy_n(execution_policy& system, InputIterator first, Size n, OutputIterator result) +{ + THRUST_CDP_DISPATCH((result = __copy::device_to_device(system, first, thrust::next(first, n), result);), + (result = thrust::copy_n(cvt_to_seq(derived_cast(system)), first, n, result);)); + return result; +} // end copy_n() +#endif + +template +OutputIterator _CCCL_HOST +copy(cross_system systems, InputIterator first, InputIterator last, OutputIterator result) +{ + return __copy::cross_system_copy(systems, first, last, result); +} // end copy() + +template +OutputIterator _CCCL_HOST +copy_n(cross_system systems, InputIterator first, Size n, OutputIterator result) +{ + return __copy::cross_system_copy_n(systems, first, n, result); +} // end copy_n() + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +#include +#include diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/copy_if.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/copy_if.h new file mode 100644 index 0000000000000000000000000000000000000000..a0e5f6e254d0ef19fc7858acd46bf4f3cf7215eb --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/copy_if.h @@ -0,0 +1,280 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN +// XXX declare generic copy_if interface +// to avoid circulular dependency from thrust/copy.h +template +_CCCL_HOST_DEVICE OutputIterator copy_if( + const thrust::detail::execution_policy_base& exec, + InputIterator first, + InputIterator last, + OutputIterator result, + Predicate pred); + +template +_CCCL_HOST_DEVICE OutputIterator copy_if( + const thrust::detail::execution_policy_base& exec, + InputIterator1 first, + InputIterator1 last, + InputIterator2 stencil, + OutputIterator result, + Predicate pred); + +namespace cuda_cub +{ + +namespace detail +{ + +/** + * Enum class to indicate whether the memory of input and output iterators potentially alias one another. + */ +enum class InputMayAliasOutput +{ + no, + yes +}; + +template +struct DispatchCopyIf +{ + static cudaError_t THRUST_RUNTIME_FUNCTION dispatch( + execution_policy& policy, + void* d_temp_storage, + size_t& temp_storage_bytes, + InputIt first, + StencilIt stencil, + OutputIt& output, + Predicate predicate, + OffsetT num_items) + { + using num_selected_out_it_t = OffsetT*; + using equality_op_t = cub::NullType; + + cudaError_t status = cudaSuccess; + cudaStream_t stream = cuda_cub::stream(policy); + + std::size_t allocation_sizes[2] = {0, sizeof(OffsetT)}; + void* allocations[2] = {nullptr, nullptr}; + + // drop rejected items (i.e., this is not a partition, but a selection) + constexpr bool keep_rejects = false; + constexpr bool may_alias = (MayAlias == InputMayAliasOutput::yes); + + // Query algorithm memory requirements + status = cub::DispatchSelectIf< + InputIt, + StencilIt, + OutputIt, + num_selected_out_it_t, + Predicate, + equality_op_t, + OffsetT, + keep_rejects, + may_alias>::Dispatch(nullptr, + allocation_sizes[0], + first, + stencil, + output, + static_cast(nullptr), + predicate, + equality_op_t{}, + num_items, + stream); + CUDA_CUB_RET_IF_FAIL(status); + + status = cub::AliasTemporaries(d_temp_storage, temp_storage_bytes, allocations, allocation_sizes); + CUDA_CUB_RET_IF_FAIL(status); + + // Return if we're only querying temporary storage requirements + if (d_temp_storage == nullptr) + { + return status; + } + + // Return for empty problems + if (num_items == 0) + { + return status; + } + + // Memory allocation for the number of selected output items + OffsetT* d_num_selected_out = thrust::detail::aligned_reinterpret_cast(allocations[1]); + + // Run algorithm + status = cub::DispatchSelectIf< + InputIt, + StencilIt, + OutputIt, + num_selected_out_it_t, + Predicate, + equality_op_t, + OffsetT, + keep_rejects, + may_alias>::Dispatch(allocations[0], + allocation_sizes[0], + first, + stencil, + output, + d_num_selected_out, + predicate, + equality_op_t{}, + num_items, + stream); + CUDA_CUB_RET_IF_FAIL(status); + + // Get number of selected items + status = cuda_cub::synchronize(policy); + CUDA_CUB_RET_IF_FAIL(status); + OffsetT num_selected = get_value(policy, d_num_selected_out); + thrust::advance(output, num_selected); + return status; + } +}; + +template +THRUST_RUNTIME_FUNCTION OutputIt copy_if( + execution_policy& policy, + InputIt first, + InputIt last, + StencilIt stencil, + OutputIt output, + Predicate predicate) +{ + using size_type = typename iterator_traits::difference_type; + + size_type num_items = static_cast(thrust::distance(first, last)); + cudaError_t status = cudaSuccess; + size_t temp_storage_bytes = 0; + + // 64-bit offset-type dispatch + // Since https://github.com/NVIDIA/cccl/pull/2400, cub::DeviceSelect is using a streaming approach that splits up + // inputs larger than INT_MAX into partitions of up to `INT_MAX` items each, repeatedly invoking the respective + // algorithm. With that approach, we can always use i64 offset types for DispatchSelectIf, because there's only very + // limited performance upside for using i32 offset types. This avoids potentially duplicate kernel compilation. + using dispatch64_t = DispatchCopyIf; + + // Query temporary storage requirements + status = dispatch64_t::dispatch( + policy, nullptr, temp_storage_bytes, first, stencil, output, predicate, static_cast(num_items)); + cuda_cub::throw_on_error(status, "copy_if failed on 1st step"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, temp_storage_bytes); + void* temp_storage = static_cast(tmp.data().get()); + + // Run algorithm + status = dispatch64_t::dispatch( + policy, temp_storage, temp_storage_bytes, first, stencil, output, predicate, static_cast(num_items)); + cuda_cub::throw_on_error(status, "copy_if failed on 2nd step"); + + return output; +} + +} // namespace detail + +//------------------------- +// Thrust API entry points +//------------------------- +_CCCL_EXEC_CHECK_DISABLE +template +OutputIterator _CCCL_HOST_DEVICE copy_if( + execution_policy& policy, InputIterator first, InputIterator last, OutputIterator result, Predicate pred) +{ + THRUST_CDP_DISPATCH((return detail::copy_if( + policy, first, last, static_cast(nullptr), result, pred);), + (return thrust::copy_if(cvt_to_seq(derived_cast(policy)), first, last, result, pred);)); +} + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIterator _CCCL_HOST_DEVICE copy_if( + execution_policy& policy, + InputIterator first, + InputIterator last, + StencilIterator stencil, + OutputIterator result, + Predicate pred) +{ + THRUST_CDP_DISPATCH( + (return detail::copy_if(policy, first, last, stencil, result, pred);), + (return thrust::copy_if(cvt_to_seq(derived_cast(policy)), first, last, stencil, result, pred);)); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +# include +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/count.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/count.h new file mode 100644 index 0000000000000000000000000000000000000000..530682ba717d88af62195ad908332199ea97d22f --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/count.h @@ -0,0 +1,71 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include +# include +# include +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +typename iterator_traits::difference_type _CCCL_HOST_DEVICE +count_if(execution_policy& policy, InputIt first, InputIt last, UnaryPred unary_pred) +{ + using size_type = typename iterator_traits::difference_type; + using flag_iterator_t = transform_iterator; + + return cuda_cub::reduce_n( + policy, flag_iterator_t(first, unary_pred), thrust::distance(first, last), size_type(0), plus()); +} + +template +typename iterator_traits::difference_type _CCCL_HOST_DEVICE +count(execution_policy& policy, InputIt first, InputIt last, Value const& value) +{ + return cuda_cub::count_if(policy, first, last, thrust::detail::equal_to_value(value)); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/fill.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/fill.h new file mode 100644 index 0000000000000000000000000000000000000000..a31b48674d02e238daec0e8ee2719bc29387d593 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/fill.h @@ -0,0 +1,91 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include +# include +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +namespace __fill +{ + +// fill functor +template +struct functor +{ + Iterator it; + T value; + + THRUST_FUNCTION + functor(Iterator it, T value) + : it(it) + , value(value) + {} + + template + THRUST_DEVICE_FUNCTION void operator()(Size idx) + { + it[idx] = value; + } +}; // struct functor + +} // namespace __fill + +template +OutputIterator _CCCL_HOST_DEVICE +fill_n(execution_policy& policy, OutputIterator first, Size count, const T& value) +{ + cuda_cub::parallel_for(policy, __fill::functor(first, value), count); + + return first + count; +} // func fill_n + +template +void _CCCL_HOST_DEVICE +fill(execution_policy& policy, ForwardIterator first, ForwardIterator last, const T& value) +{ + cuda_cub::fill_n(policy, first, thrust::distance(first, last), value); +} // func fill + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/find.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/find.h new file mode 100644 index 0000000000000000000000000000000000000000..4b4e11c8ed9fdc107f0191a0c0ffa160d0acadc6 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/find.h @@ -0,0 +1,175 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include +# include +# include +# include +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +// XXX forward declare to circumvent circular dependency +template +InputIt _CCCL_HOST_DEVICE find_if(execution_policy& policy, InputIt first, InputIt last, Predicate predicate); + +template +InputIt _CCCL_HOST_DEVICE +find_if_not(execution_policy& policy, InputIt first, InputIt last, Predicate predicate); + +template +InputIt _CCCL_HOST_DEVICE find(execution_policy& policy, InputIt first, InputIt last, T const& value); + +}; // namespace cuda_cub +THRUST_NAMESPACE_END + +# include +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +namespace __find_if +{ + +template +struct functor +{ + THRUST_DEVICE_FUNCTION TupleType operator()(const TupleType& lhs, const TupleType& rhs) const + { + // select the smallest index among true results + if (thrust::get<0>(lhs) && thrust::get<0>(rhs)) + { + return TupleType(true, (thrust::min)(thrust::get<1>(lhs), thrust::get<1>(rhs))); + } + else if (thrust::get<0>(lhs)) + { + return lhs; + } + else + { + return rhs; + } + } +}; +} // namespace __find_if + +template +InputIt _CCCL_HOST_DEVICE +find_if_n(execution_policy& policy, InputIt first, Size num_items, Predicate predicate) +{ + using result_type = typename thrust::tuple; + + // empty sequence + if (num_items == 0) + { + return first; + } + + // this implementation breaks up the sequence into separate intervals + // in an attempt to early-out as soon as a value is found + // + // XXX compose find_if from a look-back prefix scan algorithm + // and abort kernel when the first element is found + + // TODO incorporate sizeof(InputType) into interval_threshold and round to multiple of 32 + const Size interval_threshold = 1 << 20; + const Size interval_size = (thrust::min)(interval_threshold, num_items); + + // force transform_iterator output to bool + using XfrmIterator = transform_iterator; + using IteratorTuple = thrust::tuple>; + using ZipIterator = thrust::zip_iterator; + + IteratorTuple iter_tuple = thrust::make_tuple(XfrmIterator(first, predicate), counting_iterator(0)); + + ZipIterator begin = thrust::make_zip_iterator(iter_tuple); + ZipIterator end = begin + num_items; + + for (ZipIterator interval_begin = begin; interval_begin < end; interval_begin += interval_size) + { + ZipIterator interval_end = interval_begin + interval_size; + if (end < interval_end) + { + interval_end = end; + } // end if + + result_type result = reduce( + policy, interval_begin, interval_end, result_type(false, interval_end - begin), __find_if::functor()); + + // see if we found something + if (thrust::get<0>(result)) + { + return first + thrust::get<1>(result); + } + } + + // nothing was found if we reach here... + return first + num_items; +} + +template +InputIt _CCCL_HOST_DEVICE find_if(execution_policy& policy, InputIt first, InputIt last, Predicate predicate) +{ + return cuda_cub::find_if_n(policy, first, thrust::distance(first, last), predicate); +} + +template +InputIt _CCCL_HOST_DEVICE +find_if_not(execution_policy& policy, InputIt first, InputIt last, Predicate predicate) +{ + return cuda_cub::find_if(policy, first, last, thrust::not_fn(predicate)); +} + +template +InputIt _CCCL_HOST_DEVICE find(execution_policy& policy, InputIt first, InputIt last, T const& value) +{ + using thrust::placeholders::_1; + + return cuda_cub::find_if(policy, first, last, _1 == value); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/for_each.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/for_each.h new file mode 100644 index 0000000000000000000000000000000000000000..d0a3a65032a214ac30b7fd69be616e8cb4dba560 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/for_each.h @@ -0,0 +1,83 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include + +# include +# include +# include +# include +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +// for_each_n +_CCCL_EXEC_CHECK_DISABLE +template +Input THRUST_FUNCTION for_each_n(execution_policy& policy, Input first, Size count, UnaryOp op) +{ + THRUST_CDP_DISPATCH( + (cudaStream_t stream = cuda_cub::stream(policy); + cudaError_t status = cub::DeviceFor::ForEachN(first, count, op, stream); + cuda_cub::throw_on_error(status, "parallel_for failed"); + status = cuda_cub::synchronize_optional(policy); + cuda_cub::throw_on_error(status, "parallel_for: failed to synchronize");), + (for (Size idx = 0; idx != count; ++idx) { op(raw_reference_cast(*(first + idx))); })); + + return first + count; +} + +// for_each +template +Input THRUST_FUNCTION for_each(execution_policy& policy, Input first, Input last, UnaryOp op) +{ + using size_type = typename iterator_traits::difference_type; + size_type count = static_cast(thrust::distance(first, last)); + + return THRUST_NS_QUALIFIER::cuda_cub::for_each_n(policy, first, count, op); +} +} // namespace cuda_cub + +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/future.inl b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/future.inl new file mode 100644 index 0000000000000000000000000000000000000000..a35bfb2530e6523b2d8cfa636ec1e5f0d616fd38 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/future.inl @@ -0,0 +1,1235 @@ +// Copyright (c) 2018 NVIDIA Corporation +// Author: Bryce Adelstein Lelbach +// +// Distributed under the Boost Software License v1.0 (boost.org/LICENSE_1_0.txt) + +// TODO: Split into more granular headers (move unique_stream/unique_marker to +// another header, etc). + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header +#include + +#if _CCCL_STD_VER >= 2014 + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include +# include + +# include + +_CCCL_SUPPRESS_DEPRECATED_PUSH +THRUST_NAMESPACE_BEGIN + +// Forward declaration. +struct CCCL_DEPRECATED new_stream_t; + +namespace system +{ +namespace cuda +{ +namespace detail +{ + +/////////////////////////////////////////////////////////////////////////////// + +struct nonowning_t final +{}; + +_CCCL_GLOBAL_CONSTANT nonowning_t nonowning{}; + +/////////////////////////////////////////////////////////////////////////////// + +struct marker_deleter final +{ + _CCCL_HOST void operator()(CUevent_st* e) const + { + if (nullptr != e) + { + thrust::cuda_cub::throw_on_error(cudaEventDestroy(e)); + } + } +}; + +/////////////////////////////////////////////////////////////////////////////// + +struct unique_marker final +{ + using native_handle_type = CUevent_st*; + +private: + std::unique_ptr handle_; + +public: + /// \brief Create a new stream and construct a handle to it. When the handle + /// is destroyed, the stream is destroyed. + _CCCL_HOST unique_marker() + : handle_(nullptr, marker_deleter()) + { + native_handle_type e; + thrust::cuda_cub::throw_on_error(cudaEventCreateWithFlags(&e, cudaEventDisableTiming)); + handle_.reset(e); + } + + _CCCL_EXEC_CHECK_DISABLE + unique_marker(unique_marker const&) = delete; + _CCCL_EXEC_CHECK_DISABLE + unique_marker(unique_marker&&) = default; + _CCCL_EXEC_CHECK_DISABLE + unique_marker& operator=(unique_marker const&) = delete; + _CCCL_EXEC_CHECK_DISABLE + unique_marker& operator=(unique_marker&&) = default; + + _CCCL_EXEC_CHECK_DISABLE + ~unique_marker() = default; + + _CCCL_HOST auto get() const THRUST_DECLTYPE_RETURNS(native_handle_type(handle_.get())); + _CCCL_HOST auto native_handle() const THRUST_DECLTYPE_RETURNS(native_handle_type(handle_.get())); + + _CCCL_HOST bool valid() const noexcept + { + return bool(handle_); + } + + _CCCL_HOST bool ready() const + { + cudaError_t const err = cudaEventQuery(handle_.get()); + + if (cudaErrorNotReady == err) + { + return false; + } + + // Throw on any other error. + thrust::cuda_cub::throw_on_error(err); + + return true; + } + + _CCCL_HOST void wait() const + { + thrust::cuda_cub::throw_on_error(cudaEventSynchronize(handle_.get())); + } + + _CCCL_HOST bool operator==(unique_marker const& other) const + { + return other.handle_ == handle_; + } + + _CCCL_HOST bool operator!=(unique_marker const& other) const + { + return !(other == *this); + } +}; + +/////////////////////////////////////////////////////////////////////////////// + +struct stream_deleter final +{ + _CCCL_HOST void operator()(CUstream_st* s) const + { + if (nullptr != s) + { + thrust::cuda_cub::throw_on_error(cudaStreamDestroy(s)); + } + } +}; + +struct stream_conditional_deleter final +{ +private: + bool cond_; + +public: + _CCCL_HOST constexpr stream_conditional_deleter() noexcept + : cond_(true) + {} + + _CCCL_HOST explicit constexpr stream_conditional_deleter(nonowning_t) noexcept + : cond_(false) + {} + + _CCCL_HOST void operator()(CUstream_st* s) const + { + if (cond_ && nullptr != s) + { + thrust::cuda_cub::throw_on_error(cudaStreamDestroy(s)); + } + } +}; + +/////////////////////////////////////////////////////////////////////////////// + +struct unique_stream final +{ + using native_handle_type = CUstream_st*; + +private: + std::unique_ptr handle_; + +public: + /// \brief Create a new stream and construct a handle to it. When the handle + /// is destroyed, the stream is destroyed. + _CCCL_HOST unique_stream() + : handle_(nullptr, stream_conditional_deleter()) + { + native_handle_type s; + thrust::cuda_cub::throw_on_error(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking)); + handle_.reset(s); + } + + /// \brief Construct a non-owning handle to an existing stream. When the + /// handle is destroyed, the stream is not destroyed. + _CCCL_HOST explicit unique_stream(nonowning_t, native_handle_type handle) + : handle_(handle, stream_conditional_deleter(nonowning)) + {} + + _CCCL_EXEC_CHECK_DISABLE + unique_stream(unique_stream const&) = delete; + + // GCC 10 complains if this is defaulted. See NVIDIA/thrust#1269. + _CCCL_EXEC_CHECK_DISABLE + _CCCL_HOST unique_stream(unique_stream&& o) noexcept + : handle_(std::move(o.handle_)) + {} + + _CCCL_EXEC_CHECK_DISABLE + unique_stream& operator=(unique_stream const&) = delete; + _CCCL_EXEC_CHECK_DISABLE + unique_stream& operator=(unique_stream&&) = default; + + _CCCL_EXEC_CHECK_DISABLE + ~unique_stream() = default; + + _CCCL_HOST auto get() const THRUST_DECLTYPE_RETURNS(native_handle_type(handle_.get())); + _CCCL_HOST auto native_handle() const THRUST_DECLTYPE_RETURNS(native_handle_type(handle_.get())); + + _CCCL_HOST bool valid() const noexcept + { + return bool(handle_); + } + + _CCCL_HOST bool ready() const + { + cudaError_t const err = cudaStreamQuery(handle_.get()); + + if (cudaErrorNotReady == err) + { + return false; + } + + // Throw on any other error. + thrust::cuda_cub::throw_on_error(err); + + return true; + } + + _CCCL_HOST void wait() const + { + thrust::cuda_cub::throw_on_error(cudaStreamSynchronize(handle_.get())); + } + + _CCCL_HOST void depend_on(unique_marker& e) + { + thrust::cuda_cub::throw_on_error(cudaStreamWaitEvent(handle_.get(), e.get(), 0)); + } + + _CCCL_HOST void depend_on(unique_stream& s) + { + if (s != *this) + { + unique_marker e; + s.record(e); + depend_on(e); + } + } + + _CCCL_HOST void record(unique_marker& e) + { + thrust::cuda_cub::throw_on_error(cudaEventRecord(e.get(), handle_.get())); + } + + _CCCL_HOST bool operator==(unique_stream const& other) const + { + return other.handle_ == handle_; + } + + _CCCL_HOST bool operator!=(unique_stream const& other) const + { + return !(other == *this); + } +}; + +/////////////////////////////////////////////////////////////////////////////// + +// Inheritance hierarchy of future/event shared state types. + +struct async_signal; + +template +struct async_keep_alives /* : virtual async_signal */; + +template +struct async_value /* : virtual async_signal */; + +template +struct async_addressable_value_with_keep_alives + /* : async_value, async_keep_alives */; + +/////////////////////////////////////////////////////////////////////////////// + +template +struct weak_promise; + +template > +struct unique_eager_future_promise_pair final +{ + unique_eager_future future; + weak_promise promise; +}; + +_CCCL_SUPPRESS_DEPRECATED_PUSH // for thrust::optional + + struct acquired_stream final +{ + unique_stream stream; + optional const acquired_from; + // `acquired_from` contains the index in the tuple of dependencies from which + // the stream was acquired. If `acquired_from` is empty, no stream could be + // acquired from a dependency, and then the stream was newly created. +}; + +// Precondition: `device` is the current CUDA device. +template +_CCCL_HOST optional try_acquire_stream(int device, std::unique_ptr&) noexcept; + +// Precondition: `device` is the current CUDA device. +_CCCL_HOST inline optional try_acquire_stream(int, unique_stream& stream) noexcept; + +// Precondition: `device` is the current CUDA device. +_CCCL_HOST inline optional try_acquire_stream(int device, ready_event&) noexcept; + +// Precondition: `device` is the current CUDA device. +template +_CCCL_HOST inline optional try_acquire_stream(int device, ready_future&) noexcept; + +// Precondition: `device` is the current CUDA device. +_CCCL_HOST inline optional try_acquire_stream(int device, unique_eager_event& parent) noexcept; + +// Precondition: `device` is the current CUDA device. +template +_CCCL_HOST optional try_acquire_stream(int device, unique_eager_future& parent) noexcept; + +_CCCL_SUPPRESS_DEPRECATED_POP + +template +_CCCL_HOST acquired_stream acquire_stream(int device, Dependencies&... deps) noexcept; + +template +_CCCL_HOST unique_eager_event make_dependent_event(std::tuple&& deps); + +template +_CCCL_HOST unique_eager_future_promise_pair +make_dependent_future(ComputeContent&& cc, std::tuple&& deps); + +/////////////////////////////////////////////////////////////////////////////// + +struct async_signal +{ +protected: + unique_stream stream_; + +public: + // Constructs an `async_signal` which uses `stream`. + _CCCL_HOST explicit async_signal(unique_stream&& stream) + : stream_(std::move(stream)) + {} + + _CCCL_HOST virtual ~async_signal() {} + + unique_stream& stream() noexcept + { + return stream_; + } + unique_stream const& stream() const noexcept + { + return stream_; + } +}; + +template +struct async_keep_alives> : virtual async_signal +{ + using keep_alives_type = std::tuple; + +protected: + keep_alives_type keep_alives_; + +public: + // Constructs an `async_keep_alives` which uses `stream`, and keeps the + // objects in the tuple `keep_alives` alive until the asynchronous signal is + // destroyed. + _CCCL_HOST explicit async_keep_alives(unique_stream&& stream, keep_alives_type&& keep_alives) + : async_signal(std::move(stream)) + , keep_alives_(std::move(keep_alives)) + {} + + _CCCL_HOST virtual ~async_keep_alives() {} +}; + +template +struct async_value : virtual async_signal +{ + using value_type = T; + using raw_const_pointer = value_type const*; + + // Constructs an `async_value` which uses `stream` and has no content. + _CCCL_HOST explicit async_value(unique_stream stream) + : async_signal(std::move(stream)) + {} + + _CCCL_HOST virtual ~async_value() {} + + _CCCL_HOST virtual bool valid_content() const noexcept + { + return false; + } + + _CCCL_HOST virtual value_type get() + { + throw thrust::event_error(event_errc::no_state); + } + + _CCCL_HOST virtual value_type extract() + { + throw thrust::event_error(event_errc::no_state); + } + +// For testing only. +# if defined(THRUST_ENABLE_FUTURE_RAW_DATA_MEMBER) + _CCCL_HOST virtual raw_const_pointer raw_data() const + { + return nullptr; + } +# endif +}; + +template +struct async_addressable_value_with_keep_alives> final + : async_value + , async_keep_alives> +{ + using value_type = typename async_value::value_type; + using raw_const_pointer = typename async_value::raw_const_pointer; + + using keep_alives_type = typename async_keep_alives>::keep_alives_type; + + using pointer = typename thrust::detail::pointer_traits::template rebind::other; + using const_pointer = typename thrust::detail::pointer_traits::template rebind::other; + +private: + pointer content_; + +public: + // Constructs an `async_addressable_value_with_keep_alives` which uses + // `stream`, keeps the objects in the tuple `keep_alives` alive until the + // asynchronous value is destroyed, and determines the location of its + // content by evaluating `compute_content(content_keep_alive)`. + // NOTE: The use of a callback idiom is necessary if the content is stored in + // place in the content keep alive object, in which case we need to get its + // address after its been moved into the new signal we're constructing. + // NOTE: NVCC has a bug that causes it to reorder our base class initializers + // in generated host code, which leads to -Wreorder warnings. + _CCCL_DIAG_PUSH + _CCCL_DIAG_SUPPRESS_CLANG("-Wreorder") + _CCCL_DIAG_SUPPRESS_GCC("-Wreorder") + _CCCL_DIAG_SUPPRESS_ICC(2407) + template + _CCCL_HOST explicit async_addressable_value_with_keep_alives( + unique_stream&& stream, keep_alives_type&& keep_alives, ComputeContent&& compute_content) + : async_signal(std::move(stream)) + , async_value(std::move(stream)) + , async_keep_alives(std::move(stream), std::move(keep_alives)) + { + content_ = THRUST_FWD(compute_content)(std::get<0>(this->keep_alives_)); + } + _CCCL_DIAG_POP + + _CCCL_HOST bool valid_content() const noexcept final override + { + return nullptr != content_; + } + + // Precondition: `true == valid_content()`. + _CCCL_HOST pointer data() + { + if (!valid_content()) + { + throw thrust::event_error(event_errc::no_content); + } + + return content_; + } + + // Precondition: `true == valid_content()`. + _CCCL_HOST const_pointer data() const + { + if (!valid_content()) + { + throw thrust::event_error(event_errc::no_content); + } + + return content_; + } + + // Blocks. + // Precondition: `true == valid_content()`. + _CCCL_HOST value_type get() final override + { + this->stream().wait(); + return *data(); + } + + // Blocks. + // Precondition: `true == valid_content()`. + _CCCL_HOST value_type extract() final override + { + this->stream().wait(); + return std::move(*data()); + } + +// For testing only. +# if defined(THRUST_ENABLE_FUTURE_RAW_DATA_MEMBER) + _CCCL_HOST raw_const_pointer raw_data() const final override + { + return raw_pointer_cast(content_); + } +# endif +}; + +/////////////////////////////////////////////////////////////////////////////// + +template +struct weak_promise final +{ + using value_type = typename async_value::value_type; + + using pointer = typename thrust::detail::pointer_traits::template rebind::other; + using const_pointer = typename thrust::detail::pointer_traits::template rebind::other; + +private: + int device_ = 0; + pointer content_; + + explicit weak_promise(int device_id, pointer content) + : device_(device_id) + , content_(std::move(content)) + {} + +public: + _CCCL_HOST_DEVICE weak_promise() + : device_(0) + , content_{} + {} + + _CCCL_EXEC_CHECK_DISABLE + weak_promise(weak_promise const&) = default; + _CCCL_EXEC_CHECK_DISABLE + weak_promise(weak_promise&&) = default; + _CCCL_EXEC_CHECK_DISABLE + weak_promise& operator=(weak_promise const&) = default; + _CCCL_EXEC_CHECK_DISABLE + weak_promise& operator=(weak_promise&&) = default; + + template + _CCCL_HOST_DEVICE void set_value(U&& value) && + { + *content_ = THRUST_FWD(value); + } + + template + friend _CCCL_HOST unique_eager_future_promise_pair + thrust::system::cuda::detail::make_dependent_future(ComputeContent&& cc, std::tuple&& deps); +}; + +/////////////////////////////////////////////////////////////////////////////// + +} // namespace detail + +struct CCCL_DEPRECATED ready_event final +{ + ready_event() = default; + + template + _CCCL_HOST_DEVICE explicit ready_event(ready_future) + {} + + _CCCL_HOST_DEVICE static constexpr bool valid_content() noexcept + { + return true; + } + + _CCCL_HOST_DEVICE static constexpr bool ready() noexcept + { + return true; + } +}; + +template +struct CCCL_DEPRECATED ready_future final +{ + using value_type = T; + using raw_const_pointer = T const*; + +private: + value_type value_; + +public: + _CCCL_HOST_DEVICE ready_future() + : value_{} + {} + + ready_future(ready_future&&) = default; + ready_future(ready_future const&) = default; + ready_future& operator=(ready_future&&) = default; + ready_future& operator=(ready_future const&) = default; + + template + _CCCL_HOST_DEVICE explicit ready_future(U&& u) + : value_(THRUST_FWD(u)) + {} + + _CCCL_HOST_DEVICE static constexpr bool valid_content() noexcept + { + return true; + } + + _CCCL_HOST_DEVICE static constexpr bool ready() noexcept + { + return true; + } + + _CCCL_HOST_DEVICE value_type get() const + { + return value_; + } + + _CCCL_NODISCARD _CCCL_HOST_DEVICE value_type extract() + { + return std::move(value_); + } + +# if defined(THRUST_ENABLE_FUTURE_RAW_DATA_MEMBER) + // For testing only. + _CCCL_HOST_DEVICE raw_const_pointer data() const + { + return addressof(value_); + } +# endif +}; + +struct CCCL_DEPRECATED unique_eager_event final +{ +protected: + int device_ = 0; + std::unique_ptr async_signal_; + + _CCCL_HOST explicit unique_eager_event(int device_id, std::unique_ptr async_signal) + : device_(device_id) + , async_signal_(std::move(async_signal)) + {} + +public: + _CCCL_HOST unique_eager_event() + : device_(0) + , async_signal_() + {} + + unique_eager_event(unique_eager_event&&) = default; + unique_eager_event(unique_eager_event const&) = delete; + unique_eager_event& operator=(unique_eager_event&&) = default; + unique_eager_event& operator=(unique_eager_event const&) = delete; + + // Any `unique_eager_future` can be explicitly converted to a + // `unique_eager_event`. + template + _CCCL_HOST explicit unique_eager_event(unique_eager_future&& other) + // NOTE: We upcast to `unique_ptr` here. + : device_(other.where()) + , async_signal_(other.async_signal_.release()) + {} + + _CCCL_HOST + // NOTE: We take `new_stream_t` by `const&` because it is incomplete here. + explicit unique_eager_event(new_stream_t const&) + : device_(0) + , async_signal_(new detail::async_signal(detail::unique_stream{})) + { + thrust::cuda_cub::throw_on_error(cudaGetDevice(&device_)); + } + + _CCCL_HOST virtual ~unique_eager_event() + { + // FIXME: If we could asynchronously handle destruction of keep alives, we + // could avoid doing this. + if (valid_stream()) + { + wait(); + } + } + + _CCCL_HOST bool valid_stream() const noexcept + { + return bool(async_signal_); + } + + _CCCL_HOST bool ready() const noexcept + { + if (valid_stream()) + { + return stream().ready(); + } + else + { + return false; + } + } + + // Precondition: `true == valid_stream()`. + _CCCL_HOST detail::unique_stream& stream() + { + if (!valid_stream()) + { + throw thrust::event_error(event_errc::no_state); + } + + return async_signal_->stream(); + } + detail::unique_stream const& stream() const + { + if (!valid_stream()) + { + throw thrust::event_error(event_errc::no_state); + } + + return async_signal_->stream(); + } + + _CCCL_HOST int where() const noexcept + { + return device_; + } + + // Precondition: `true == valid_stream()`. + _CCCL_HOST void wait() + { + stream().wait(); + } + + _CCCL_SUPPRESS_DEPRECATED_PUSH // for thrust::optional + friend _CCCL_HOST optional + thrust::system::cuda::detail::try_acquire_stream(int device_id, unique_eager_event& parent) noexcept; + _CCCL_SUPPRESS_DEPRECATED_POP + + template + friend _CCCL_HOST unique_eager_event + thrust::system::cuda::detail::make_dependent_event(std::tuple&& deps); +}; + +template +struct CCCL_DEPRECATED unique_eager_future final +{ + THRUST_STATIC_ASSERT_MSG((!std::is_same>::value), + "`thrust::event` should be used to express valueless futures"); + + using value_type = typename detail::async_value::value_type; + using raw_const_pointer = typename detail::async_value::raw_const_pointer; + +private: + int device_ = 0; + ::cuda::std::unique_ptr> async_signal_; + + _CCCL_HOST explicit unique_eager_future(int device_id, std::unique_ptr> async_signal) + : device_(device_id) + , async_signal_(async_signal.release()) + {} + +public: + _CCCL_HOST unique_eager_future() + : device_(0) + , async_signal_() + {} + + unique_eager_future(unique_eager_future&&) = default; + unique_eager_future(unique_eager_future const&) = delete; + unique_eager_future& operator=(unique_eager_future&&) = default; + unique_eager_future& operator=(unique_eager_future const&) = delete; + + _CCCL_HOST + // NOTE: We take `new_stream_t` by `const&` because it is incomplete here. + explicit unique_eager_future(new_stream_t const&) + : device_(0) + , async_signal_(new detail::async_value(detail::unique_stream{})) + { + thrust::cuda_cub::throw_on_error(cudaGetDevice(&device_)); + } + + _CCCL_HOST ~unique_eager_future() + { + // FIXME: If we could asynchronously handle destruction of keep alives, we + // could avoid doing this. + if (valid_stream()) + { + wait(); + } + } + + _CCCL_HOST bool valid_stream() const noexcept + { + return bool(async_signal_); + } + + _CCCL_HOST bool valid_content() const noexcept + { + if (!valid_stream()) + { + return false; + } + + // We might have been constructed with `new_stream_t`, in which case we'd + // have an async_value, but it doesn't have content. + return async_signal_->valid_content(); + } + + // Precondition: `true == valid_stream()`. + _CCCL_HOST bool ready() const noexcept + { + if (valid_stream()) + { + return stream().ready(); + } + else + { + return false; + } + } + + // Precondition: `true == valid_stream()`. + _CCCL_HOST detail::unique_stream& stream() + { + if (!valid_stream()) + { + throw thrust::event_error(event_errc::no_state); + } + + return async_signal_->stream(); + } + _CCCL_HOST detail::unique_stream const& stream() const + { + if (!valid_stream()) + { + throw thrust::event_error(event_errc::no_state); + } + + return async_signal_->stream(); + } + + _CCCL_HOST int where() const noexcept + { + return device_; + } + + // Blocks. + // Precondition: `true == valid_stream()`. + _CCCL_HOST void wait() + { + stream().wait(); + } + + // Blocks. + // Precondition: `true == valid_content()`. + _CCCL_HOST value_type get() + { + if (!valid_content()) + { + throw thrust::event_error(event_errc::no_content); + } + + return async_signal_->get(); + } + + // Blocks. + // Precondition: `true == valid_content()`. + _CCCL_NODISCARD _CCCL_HOST value_type extract() + { + if (!valid_content()) + { + throw thrust::event_error(event_errc::no_content); + } + + value_type tmp(async_signal_->extract()); + async_signal_.reset(); + return tmp; + } + +// For testing only. +# if defined(THRUST_ENABLE_FUTURE_RAW_DATA_MEMBER) + // Precondition: `true == valid_stream()`. + _CCCL_HOST raw_const_pointer raw_data() const + { + if (!valid_stream()) + { + throw thrust::event_error(event_errc::no_state); + } + + return async_signal_->raw_data(); + } +# endif + + _CCCL_SUPPRESS_DEPRECATED_PUSH // for thrust::optional + template + friend _CCCL_HOST optional + thrust::system::cuda::detail::try_acquire_stream(int device_id, unique_eager_future& parent) noexcept; + _CCCL_SUPPRESS_DEPRECATED_POP + + template + friend _CCCL_HOST detail::unique_eager_future_promise_pair + thrust::system::cuda::detail::make_dependent_future(ComputeContent&& cc, std::tuple&& deps); + + friend struct unique_eager_event; +}; + +/////////////////////////////////////////////////////////////////////////////// + +namespace detail +{ +_CCCL_SUPPRESS_DEPRECATED_PUSH // for thrust::optional + + template + _CCCL_HOST optional try_acquire_stream(int, std::unique_ptr&) noexcept +{ + // There's no stream to acquire! + return {}; +} + +_CCCL_HOST inline optional try_acquire_stream(int, unique_stream& stream) noexcept +{ + return {std::move(stream)}; +} + +_CCCL_HOST inline optional try_acquire_stream(int, ready_event&) noexcept +{ + // There's no stream to acquire! + return {}; +} + +template +_CCCL_HOST optional try_acquire_stream(int, ready_future&) noexcept +{ + // There's no stream to acquire! + return {}; +} + +_CCCL_HOST optional try_acquire_stream(int device_id, unique_eager_event& parent) noexcept +{ + // We have unique ownership, so we can always steal the stream if the future + // has one as long as they are on the same device as us. + if (parent.valid_stream()) + { + if (device_id == parent.device_) + { + return std::move(parent.async_signal_->stream()); + } + } + + return {}; +} + +template +_CCCL_HOST optional try_acquire_stream(int device_id, unique_eager_future& parent) noexcept +{ + // We have unique ownership, so we can always steal the stream if the future + // has one as long as they are on the same device as us. + if (parent.valid_stream()) + { + if (device_id == parent.device_) + { + return std::move(parent.async_signal_->stream()); + } + } + + return {}; +} + +_CCCL_SUPPRESS_DEPRECATED_POP + +/////////////////////////////////////////////////////////////////////////////// + +template +_CCCL_HOST acquired_stream acquire_stream_impl(int, std::tuple&, index_sequence<>) noexcept +{ + // We tried to take a stream from all of our dependencies and failed every + // time, so we need to make a new stream. + return {unique_stream{}, {}}; +} + +template +_CCCL_HOST acquired_stream +acquire_stream_impl(int device_id, std::tuple& deps, index_sequence) noexcept +{ + _CCCL_SUPPRESS_DEPRECATED_PUSH // for thrust::optional (MSVC warnings here) + auto tr = try_acquire_stream(device_id, std::get(deps)); + + if (tr) + { + return {std::move(*tr), {I0}}; + } + else + { + return acquire_stream_impl(device_id, deps, index_sequence{}); + } + _CCCL_SUPPRESS_DEPRECATED_POP +} + +template +_CCCL_HOST acquired_stream acquire_stream(int device_id, std::tuple& deps) noexcept +{ + return acquire_stream_impl(device_id, deps, make_index_sequence{}); +} + +/////////////////////////////////////////////////////////////////////////////// + +template +_CCCL_HOST void create_dependency(unique_stream&, std::unique_ptr&) noexcept +{} + +_CCCL_HOST inline void create_dependency(unique_stream&, ready_event&) noexcept {} + +template +_CCCL_HOST void create_dependency(unique_stream&, ready_future&) noexcept +{} + +_CCCL_HOST inline void create_dependency(unique_stream& child, unique_stream& parent) +{ + child.depend_on(parent); +} + +_CCCL_HOST inline void create_dependency(unique_stream& child, unique_eager_event& parent) +{ + child.depend_on(parent.stream()); +} + +template +_CCCL_HOST void create_dependency(unique_stream& child, unique_eager_future& parent) +{ + child.depend_on(parent.stream()); +} + +template +_CCCL_HOST void create_dependencies_impl(acquired_stream&, std::tuple&, index_sequence<>) +{} + +template +_CCCL_HOST void +create_dependencies_impl(acquired_stream& as, std::tuple& deps, index_sequence) +{ + // We only need to wait on the current dependency if we didn't steal our + // stream from it. + _CCCL_SUPPRESS_DEPRECATED_PUSH + if (!as.acquired_from || *as.acquired_from != I0) + { + create_dependency(as.stream, std::get(deps)); + } + _CCCL_SUPPRESS_DEPRECATED_POP + + create_dependencies_impl(as, deps, index_sequence{}); +} + +template +_CCCL_HOST void create_dependencies(acquired_stream& as, std::tuple& deps) +{ + create_dependencies_impl(as, deps, make_index_sequence{}); +} + +/////////////////////////////////////////////////////////////////////////////// + +// Metafunction that determine which `Dependencies` need to be kept alive. +// Returns the result as an `index_sequence` of indices into the parameter +// pack. +template +struct find_keep_alives_impl; +template +using find_keep_alives = + typename find_keep_alives_impl::value>>::type; + +template <> +struct find_keep_alives_impl, index_sequence<>> +{ + using type = index_sequence<>; +}; + +// User-provided stream. +template +struct find_keep_alives_impl, index_sequence> +{ + // Nothing to keep alive, skip this index. + using type = typename find_keep_alives_impl, index_sequence>::type; +}; + +template +struct find_keep_alives_impl, index_sequence> +{ + // Nothing to keep alive, skip this index. + using type = typename find_keep_alives_impl, index_sequence>::type; +}; + +template +struct find_keep_alives_impl, Dependencies...>, index_sequence> +{ + // Add this index to the list. + using type = integer_sequence_push_front< + std::size_t, + I0, + typename find_keep_alives_impl, index_sequence>::type>; +}; + +template +struct find_keep_alives_impl, index_sequence> +{ + // Add this index to the list. + using type = integer_sequence_push_front< + std::size_t, + I0, + typename find_keep_alives_impl, index_sequence>::type>; +}; + +template +struct find_keep_alives_impl, Dependencies...>, index_sequence> +{ + // Add this index to the list. + using type = integer_sequence_push_front< + std::size_t, + I0, + typename find_keep_alives_impl, index_sequence>::type>; +}; + +// Content storage. +template +struct find_keep_alives_impl, Dependencies...>, index_sequence> +{ + // Add this index to the list. + using type = integer_sequence_push_front< + std::size_t, + I0, + typename find_keep_alives_impl, index_sequence>::type>; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template +_CCCL_HOST unique_eager_event make_dependent_event(std::tuple&& deps) +{ + int device_id = 0; + thrust::cuda_cub::throw_on_error(cudaGetDevice(&device_id)); + + // First, either steal a stream from one of our children or make a new one. + auto as = acquire_stream(device_id, deps); + + // Then, make the stream we've acquired asynchronously wait on all of our + // dependencies, except the one we stole the stream from. + create_dependencies(as, deps); + + // Then, we determine which subset of dependencies need to be kept alive. + auto ka = tuple_subset(std::move(deps), find_keep_alives>{}); + + // Next, we create the asynchronous signal. + using async_signal_type = async_keep_alives; + + std::unique_ptr sig(new async_signal_type(std::move(as.stream), std::move(ka))); + + // Finally, we create the event object. + return unique_eager_event(device_id, std::move(sig)); +} + +/////////////////////////////////////////////////////////////////////////////// + +template +_CCCL_HOST unique_eager_future_promise_pair +make_dependent_future(ComputeContent&& cc, std::tuple&& deps) +{ + int device_id = 0; + thrust::cuda_cub::throw_on_error(cudaGetDevice(&device_id)); + + // First, either steal a stream from one of our children or make a new one. + auto as = acquire_stream(device_id, deps); + + // Then, make the stream we've acquired asynchronously wait on all of our + // dependencies, except the one we stole the stream from. + create_dependencies(as, deps); + + // Then, we determine which subset of dependencies need to be kept alive. + auto ka = tuple_subset(std::move(deps), find_keep_alives>{}); + + // Next, we create the asynchronous value. + using async_signal_type = async_addressable_value_with_keep_alives; + + std::unique_ptr sig(new async_signal_type(std::move(as.stream), std::move(ka), std::move(cc))); + + // Finally, we create the promise and future objects. + weak_promise child_prom(device_id, sig->data()); + unique_eager_future child_fut(device_id, std::move(sig)); + + return unique_eager_future_promise_pair{std::move(child_fut), std::move(child_prom)}; +} + +} // namespace detail + +/////////////////////////////////////////////////////////////////////////////// + +template +CCCL_DEPRECATED _CCCL_HOST unique_eager_event when_all(Events&&... evs) +// TODO: Constrain to events, futures, and maybe streams (currently allows keep +// alives). +{ + return detail::make_dependent_event(std::make_tuple(std::move(evs)...)); +} + +// ADL hook for transparent `.after` move support. +CCCL_DEPRECATED _CCCL_HOST inline auto capture_as_dependency(unique_eager_event& dependency) + THRUST_DECLTYPE_RETURNS(std::move(dependency)) + + // ADL hook for transparent `.after` move support. + template + CCCL_DEPRECATED _CCCL_HOST auto capture_as_dependency(unique_eager_future& dependency) + THRUST_DECLTYPE_RETURNS(std::move(dependency)) + +} // namespace cuda +} // namespace system + +_CCCL_SUPPRESS_DEPRECATED_POP +THRUST_NAMESPACE_END + +#endif // C++14 diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/gather.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/gather.h new file mode 100644 index 0000000000000000000000000000000000000000..8a7f31bbac89b8dfc2111799ecb48a10c9c3f7ee --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/gather.h @@ -0,0 +1,91 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +ResultIt _CCCL_HOST_DEVICE +gather(execution_policy& policy, MapIt map_first, MapIt map_last, ItemsIt items, ResultIt result) +{ + return cuda_cub::transform( + policy, + thrust::make_permutation_iterator(items, map_first), + thrust::make_permutation_iterator(items, map_last), + result, + ::cuda::std::__identity{}); +} + +template +ResultIt _CCCL_HOST_DEVICE gather_if( + execution_policy& policy, + MapIt map_first, + MapIt map_last, + StencilIt stencil, + ItemsIt items, + ResultIt result, + Predicate predicate) +{ + return cuda_cub::transform_if( + policy, + thrust::make_permutation_iterator(items, map_first), + thrust::make_permutation_iterator(items, map_last), + stencil, + result, + ::cuda::std::__identity{}, + predicate); +} + +template +ResultIt _CCCL_HOST_DEVICE gather_if( + execution_policy& policy, MapIt map_first, MapIt map_last, StencilIt stencil, ItemsIt items, ResultIt result) +{ + return cuda_cub::gather_if(policy, map_first, map_last, stencil, items, result, ::cuda::std::__identity{}); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/generate.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/generate.h new file mode 100644 index 0000000000000000000000000000000000000000..be891374ab7aaa157b6155378c793a23f62154f6 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/generate.h @@ -0,0 +1,87 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +// for_each functor +template +struct generate_f +{ + Generator generator; + + THRUST_FUNCTION + generate_f(Generator generator_) + : generator(generator_) + {} + + template + THRUST_DEVICE_FUNCTION void operator()(T const& value) + { + T& lvalue = const_cast(value); + lvalue = generator(); + } +}; + +// for_each_n +template +OutputIt _CCCL_HOST_DEVICE +generate_n(execution_policy& policy, OutputIt result, Size count, Generator generator) +{ + return cuda_cub::for_each_n(policy, result, count, generate_f(generator)); +} + +// for_each +template +void _CCCL_HOST_DEVICE generate(execution_policy& policy, OutputIt first, OutputIt last, Generator generator) +{ + cuda_cub::generate_n(policy, first, thrust::distance(first, last), generator); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/get_value.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/get_value.h new file mode 100644 index 0000000000000000000000000000000000000000..5d1bb7e4cb6eebf61da742038866c50ef0e69f5b --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/get_value.h @@ -0,0 +1,89 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +namespace +{ + +template +inline _CCCL_HOST_DEVICE typename thrust::iterator_value::type +get_value_msvc2005_war(execution_policy& exec, Pointer ptr) +{ + using result_type = typename thrust::iterator_value::type; + + // XXX war nvbugs/881631 + struct war_nvbugs_881631 + { + _CCCL_HOST inline static result_type host_path(execution_policy& exec, Pointer ptr) + { + // when called from host code, implement with assign_value + // note that this requires a type with default constructor + result_type result; + + thrust::host_system_tag host_tag; + cross_system systems(host_tag, exec); + assign_value(systems, &result, ptr); + + return result; + } + + _CCCL_DEVICE inline static result_type device_path(execution_policy&, Pointer ptr) + { + // when called from device code, just do simple deref + return *thrust::raw_pointer_cast(ptr); + } + }; + + NV_IF_TARGET( + NV_IS_HOST, (return war_nvbugs_881631::host_path(exec, ptr);), (return war_nvbugs_881631::device_path(exec, ptr);)) +} // end get_value_msvc2005_war() +} // namespace + +template +inline _CCCL_HOST_DEVICE typename thrust::iterator_value::type +get_value(execution_policy& exec, Pointer ptr) +{ + return get_value_msvc2005_war(exec, ptr); +} // end get_value() + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/iter_swap.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/iter_swap.h new file mode 100644 index 0000000000000000000000000000000000000000..2740177ca5ec508315e59ac15589d99e22d3d004 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/iter_swap.h @@ -0,0 +1,69 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include +# include + +# include + +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +inline _CCCL_HOST_DEVICE void iter_swap(thrust::cuda::execution_policy&, Pointer1 a, Pointer2 b) +{ + // XXX war nvbugs/881631 + struct war_nvbugs_881631 + { + _CCCL_HOST inline static void host_path(Pointer1 a, Pointer2 b) + { + thrust::swap_ranges(a, a + 1, b); + } + + _CCCL_DEVICE inline static void device_path(Pointer1 a, Pointer2 b) + { + using ::cuda::std::swap; + swap(*thrust::raw_pointer_cast(a), *thrust::raw_pointer_cast(b)); + } + }; + + NV_IF_TARGET(NV_IS_HOST, (war_nvbugs_881631::host_path(a, b);), (war_nvbugs_881631::device_path(a, b);)); + +} // end iter_swap() + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/logical.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/logical.h new file mode 100644 index 0000000000000000000000000000000000000000..0968d886dc52f20a71c2cdb4d68a8a6d524dd053 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/logical.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special version of this algorithm diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/make_unsigned_special.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/make_unsigned_special.h new file mode 100644 index 0000000000000000000000000000000000000000..7fe7e98c4f8485ba228c9d10f7347b5692e800a1 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/make_unsigned_special.h @@ -0,0 +1,61 @@ +/* + * Copyright 2019 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +namespace detail +{ + +template +struct make_unsigned_special; + +template <> +struct make_unsigned_special +{ + using type = unsigned int; +}; + +// this is special, because CUDA's atomicAdd doesn't have an overload +// for unsigned long, for some godforsaken reason +template <> +struct make_unsigned_special +{ + using type = unsigned long long; +}; + +template <> +struct make_unsigned_special +{ + using type = unsigned long long; +}; + +} // namespace detail +} // namespace cuda_cub +THRUST_NAMESPACE_END diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/malloc_and_free.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/malloc_and_free.h new file mode 100644 index 0000000000000000000000000000000000000000..a9b817fc3903a5166ce9f68f8123c1790f327ae8 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/malloc_and_free.h @@ -0,0 +1,121 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifdef THRUST_CACHING_DEVICE_MALLOC +# include +#endif + +#include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +#ifdef THRUST_CACHING_DEVICE_MALLOC +# define __CUB_CACHING_MALLOC +# ifndef __CUDA_ARCH__ +inline cub::CachingDeviceAllocator& get_allocator() +{ + static cub::CachingDeviceAllocator g_allocator(true); + return g_allocator; +} +# endif +#endif + +// note that malloc returns a raw pointer to avoid +// depending on the heavyweight thrust/system/cuda/memory.h header +template +_CCCL_HOST_DEVICE void* malloc(execution_policy&, std::size_t n) +{ + void* result = 0; + + // need to repeat a lot of code here because we can't use #if inside of the + // NV_IF_TARGET macro. + // The device path is the same either way, but the host allocations differ. +#ifdef __CUB_CACHING_MALLOC + NV_IF_TARGET( + NV_IS_HOST, + (cub::CachingDeviceAllocator& alloc = get_allocator(); cudaError_t status = alloc.DeviceAllocate(&result, n); + + if (status != cudaSuccess) { + cudaGetLastError(); // Clear global CUDA error state. + throw thrust::system::detail::bad_alloc(thrust::cuda_category().message(status).c_str()); + }), + ( // NV_IS_DEVICE + result = thrust::raw_pointer_cast(thrust::malloc(thrust::seq, n));)); +#else // not __CUB_CACHING_MALLOC + NV_IF_TARGET( + NV_IS_HOST, + (cudaError_t status = cudaMalloc(&result, n); + + if (status != cudaSuccess) { + cudaGetLastError(); // Clear global CUDA error state. + throw thrust::system::detail::bad_alloc(thrust::cuda_category().message(status).c_str()); + }), + ( // NV_IS_DEVICE + result = thrust::raw_pointer_cast(thrust::malloc(thrust::seq, n));)); +#endif + + return result; +} // end malloc() + +template +_CCCL_HOST_DEVICE void free(execution_policy&, Pointer ptr) +{ + // need to repeat a lot of code here because we can't use #if inside of the + // NV_IF_TARGET macro. + // The device path is the same either way, but the host deallocations differ. +#ifdef __CUB_CACHING_MALLOC + NV_IF_TARGET( + NV_IS_HOST, + (cub::CachingDeviceAllocator& alloc = get_allocator(); + cudaError_t status = alloc.DeviceFree(thrust::raw_pointer_cast(ptr)); + cuda_cub::throw_on_error(status, "device free failed");), + ( // NV_IS_DEVICE + thrust::free(thrust::seq, ptr);)); +#else // not __CUB_CACHING_MALLOC + NV_IF_TARGET(NV_IS_HOST, + (cudaError_t status = cudaFree(thrust::raw_pointer_cast(ptr)); + cuda_cub::throw_on_error(status, "device free failed");), + ( // NV_IS_DEVICE + thrust::free(thrust::seq, ptr);)); +#endif +} // end free() + +} // namespace cuda_cub +THRUST_NAMESPACE_END diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/memory.inl b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/memory.inl new file mode 100644 index 0000000000000000000000000000000000000000..b99cc5744c941561a2ce563cd94e0a3185b133e6 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/memory.inl @@ -0,0 +1,57 @@ +/* + * Copyright 2008-2018 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header +#include +#include + +#include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +_CCCL_HOST_DEVICE pointer malloc(std::size_t n) +{ + tag cuda_tag; + return pointer(thrust::cuda_cub::malloc(cuda_tag, n)); +} // end malloc() + +template +_CCCL_HOST_DEVICE pointer malloc(std::size_t n) +{ + pointer raw_ptr = thrust::cuda_cub::malloc(sizeof(T) * n); + return pointer(reinterpret_cast(raw_ptr.get())); +} // end malloc() + +_CCCL_HOST_DEVICE void free(pointer ptr) +{ + tag cuda_tag; + return thrust::cuda_cub::free(cuda_tag, ptr.get()); +} // end free() + +} // namespace cuda_cub +THRUST_NAMESPACE_END diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/merge.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/merge.h new file mode 100644 index 0000000000000000000000000000000000000000..0c39a15f669d7fad1bbd4f8fdd4359359359eaf1 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/merge.h @@ -0,0 +1,228 @@ +/****************************************************************************** + * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ +_CCCL_EXEC_CHECK_DISABLE +template > +ResultIt _CCCL_HOST_DEVICE +merge(execution_policy& policy, + KeysIt1 keys1_begin, + KeysIt1 keys1_end, + KeysIt2 keys2_begin, + KeysIt2 keys2_end, + ResultIt result_begin, + CompareOp compare_op = {}) +{ + THRUST_CDP_DISPATCH( + (using size_type = typename iterator_traits::difference_type; + const auto num_keys1 = static_cast(thrust::distance(keys1_begin, keys1_end)); + const auto num_keys2 = static_cast(thrust::distance(keys2_begin, keys2_end)); + const auto num_keys_out = num_keys1 + num_keys2; + if (num_keys_out == 0) { return result_begin; } + + using dispatch32_t = cub::detail::merge:: + dispatch_t; + using dispatch64_t = cub::detail::merge:: + dispatch_t; + + const auto stream = cuda_cub::stream(policy); + cudaError_t status; + size_t storage_size = 0; + THRUST_DOUBLE_INDEX_TYPE_DISPATCH2( + status, + dispatch32_t::dispatch, + dispatch64_t::dispatch, + num_keys1, + num_keys2, + (nullptr, + storage_size, + keys1_begin, + nullptr, + num_keys1_fixed, + keys2_begin, + nullptr, + num_keys2_fixed, + result_begin, + nullptr, + compare_op, + stream)); + throw_on_error(status, "merge: failed on 1st step"); + + thrust::detail::temporary_array temp_storage(policy, storage_size); + THRUST_DOUBLE_INDEX_TYPE_DISPATCH2( + status, + dispatch32_t::dispatch, + dispatch64_t::dispatch, + num_keys1, + num_keys2, + (temp_storage.data().get(), + storage_size, + keys1_begin, + nullptr, + num_keys1_fixed, + keys2_begin, + nullptr, + num_keys2_fixed, + result_begin, + nullptr, + compare_op, + stream)); + throw_on_error(status, "merge: failed on 2nd step"); + + status = cuda_cub::synchronize_optional(policy); + throw_on_error(status, "merge: failed to synchronize"); + + return result_begin + num_keys_out;), + (return thrust::merge( + cvt_to_seq(derived_cast(policy)), keys1_begin, keys1_end, keys2_begin, keys2_end, result_begin, compare_op); + + )); +} + +_CCCL_EXEC_CHECK_DISABLE +template > +pair _CCCL_HOST_DEVICE merge_by_key( + execution_policy& policy, + KeysIt1 keys1_begin, + KeysIt1 keys1_end, + KeysIt2 keys2_begin, + KeysIt2 keys2_end, + ItemsIt1 items1_begin, + ItemsIt2 items2_begin, + KeysOutputIt keys_out_begin, + ItemsOutputIt items_out_begin, + CompareOp compare_op = {}) +{ + THRUST_CDP_DISPATCH( + (using size_type = typename iterator_traits::difference_type; + + const auto num_keys1 = static_cast(thrust::distance(keys1_begin, keys1_end)); + const auto num_keys2 = static_cast(thrust::distance(keys2_begin, keys2_end)); + const auto num_keys_out = num_keys1 + num_keys2; + if (num_keys_out == 0) { return {keys_out_begin, items_out_begin}; } + + using dispatch32_t = cub::detail::merge:: + dispatch_t; + using dispatch64_t = cub::detail::merge:: + dispatch_t; + + const auto stream = cuda_cub::stream(policy); + cudaError_t status; + size_t storage_size = 0; + THRUST_DOUBLE_INDEX_TYPE_DISPATCH2( + status, + dispatch32_t::dispatch, + dispatch64_t::dispatch, + num_keys1, + num_keys2, + (nullptr, + storage_size, + keys1_begin, + items1_begin, + num_keys1_fixed, + keys2_begin, + items2_begin, + num_keys2_fixed, + keys_out_begin, + items_out_begin, + compare_op, + stream)); + throw_on_error(status, "merge: failed on 1st step"); + + thrust::detail::temporary_array temp_storage(policy, storage_size); + THRUST_DOUBLE_INDEX_TYPE_DISPATCH2( + status, + dispatch32_t::dispatch, + dispatch64_t::dispatch, + num_keys1, + num_keys2, + (temp_storage.data().get(), + storage_size, + keys1_begin, + items1_begin, + num_keys1_fixed, + keys2_begin, + items2_begin, + num_keys2_fixed, + keys_out_begin, + items_out_begin, + compare_op, + stream)); + throw_on_error(status, "merge: failed on 2nd step"); + + status = cuda_cub::synchronize_optional(policy); + throw_on_error(status, "merge: failed to synchronize"); + + return {keys_out_begin + num_keys_out, items_out_begin + num_keys_out};), + (return thrust::merge_by_key( + cvt_to_seq(derived_cast(policy)), + keys1_begin, + keys1_end, + keys2_begin, + keys2_end, + items1_begin, + items2_begin, + keys_out_begin, + items_out_begin, + compare_op);)); +} +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/par.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/par.h new file mode 100644 index 0000000000000000000000000000000000000000..05b2444dcb930be136bab3c7333341af2b1eb197 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/par.h @@ -0,0 +1,245 @@ +/****************************************************************************** + * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header +#include +#include +#include +#include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +struct execute_on_stream_base : execution_policy +{ +private: + cudaStream_t stream; + +public: + _CCCL_EXEC_CHECK_DISABLE + _CCCL_HOST_DEVICE execute_on_stream_base(cudaStream_t stream_ = default_stream()) + : stream(stream_) + {} + + THRUST_RUNTIME_FUNCTION Derived on(cudaStream_t const& s) const + { + Derived result = derived_cast(*this); + result.stream = s; + return result; + } + +private: + friend _CCCL_HOST_DEVICE cudaStream_t get_stream(const execute_on_stream_base& exec) + { + return exec.stream; + } +}; + +template +struct execute_on_stream_nosync_base : execution_policy +{ +private: + cudaStream_t stream; + +public: + _CCCL_HOST_DEVICE execute_on_stream_nosync_base(cudaStream_t stream_ = default_stream()) + : stream(stream_) + {} + + THRUST_RUNTIME_FUNCTION Derived on(cudaStream_t const& s) const + { + Derived result = derived_cast(*this); + result.stream = s; + return result; + } + +private: + friend _CCCL_HOST_DEVICE cudaStream_t get_stream(const execute_on_stream_nosync_base& exec) + { + return exec.stream; + } + + friend _CCCL_HOST_DEVICE bool must_perform_optional_stream_synchronization(const execute_on_stream_nosync_base&) + { + return false; + } +}; + +struct execute_on_stream : execute_on_stream_base +{ + using base_t = execute_on_stream_base; + + _CCCL_HOST_DEVICE execute_on_stream() + : base_t() {}; + _CCCL_HOST_DEVICE execute_on_stream(cudaStream_t stream) + : base_t(stream) {}; +}; + +struct execute_on_stream_nosync : execute_on_stream_nosync_base +{ + using base_t = execute_on_stream_nosync_base; + + _CCCL_HOST_DEVICE execute_on_stream_nosync() + : base_t() {}; + _CCCL_HOST_DEVICE execute_on_stream_nosync(cudaStream_t stream) + : base_t(stream) {}; +}; + +_CCCL_SUPPRESS_DEPRECATED_PUSH +struct par_t + : execution_policy + , thrust::detail::allocator_aware_execution_policy + , thrust::detail::dependencies_aware_execution_policy +{ + using base_t = execution_policy; + + _CCCL_HOST_DEVICE constexpr par_t() + : base_t() + {} + + using stream_attachment_type = execute_on_stream; + + THRUST_RUNTIME_FUNCTION stream_attachment_type on(cudaStream_t const& stream) const + { + return execute_on_stream(stream); + } +}; +_CCCL_SUPPRESS_DEPRECATED_POP + +_CCCL_SUPPRESS_DEPRECATED_PUSH +struct par_nosync_t + : execution_policy + , thrust::detail::allocator_aware_execution_policy + , thrust::detail::dependencies_aware_execution_policy +{ + using base_t = execution_policy; + + _CCCL_HOST_DEVICE constexpr par_nosync_t() + : base_t() + {} + + using stream_attachment_type = execute_on_stream_nosync; + + THRUST_RUNTIME_FUNCTION stream_attachment_type on(cudaStream_t const& stream) const + { + return execute_on_stream_nosync(stream); + } + +private: + // this function is defined to allow non-blocking calls on the default_stream() with thrust::cuda::par_nosync + // without explicitly using thrust::cuda::par_nosync.on(default_stream()) + friend _CCCL_HOST_DEVICE bool must_perform_optional_stream_synchronization(const par_nosync_t&) + { + return false; + } +}; +_CCCL_SUPPRESS_DEPRECATED_POP + +_CCCL_GLOBAL_CONSTANT par_t par; + +/*! \p thrust::cuda::par_nosync is a parallel execution policy targeting Thrust's CUDA device backend. + * Similar to \p thrust::cuda::par it allows execution of Thrust algorithms in a specific CUDA stream. + * + * \p thrust::cuda::par_nosync indicates that an algorithm is free to avoid any synchronization of the + * associated stream that is not strictly required for correctness. Additionally, algorithms may return + * before the corresponding kernels are completed, similar to asynchronous kernel launches via <<< >>> syntax. + * The user must take care to perform explicit synchronization if necessary. + * + * The following code snippet demonstrates how to use \p thrust::cuda::par_nosync : + * + * \code + * #include + * #include + * #include + * + * struct IncFunctor{ + * __host__ __device__ + * void operator()(std::size_t& x){ x = x + 1; }; + * }; + * + * int main(){ + * std::size_t N = 1000000; + * thrust::device_vector d_vec(N); + * + * cudaStream_t stream; + * cudaStreamCreate(&stream); + * auto nosync_policy = thrust::cuda::par_nosync.on(stream); + * + * thrust::for_each(nosync_policy, d_vec.begin(), d_vec.end(), IncFunctor{}); + * thrust::for_each(nosync_policy, d_vec.begin(), d_vec.end(), IncFunctor{}); + * thrust::for_each(nosync_policy, d_vec.begin(), d_vec.end(), IncFunctor{}); + * + * //for_each may return before completion. Could do other cpu work in the meantime + * // ... + * + * //Wait for the completion of all for_each kernels + * cudaStreamSynchronize(stream); + * + * std::size_t x = thrust::reduce(nosync_policy, d_vec.begin(), d_vec.end()); + * //Currently, this synchronization is not necessary. reduce will still perform + * //implicit synchronization to transfer the reduced value to the host to return it. + * cudaStreamSynchronize(stream); + * cudaStreamDestroy(stream); + * } + * \endcode + * + */ +_CCCL_GLOBAL_CONSTANT par_nosync_t par_nosync; +} // namespace cuda_cub + +namespace system +{ +namespace cuda +{ +using thrust::cuda_cub::par; +using thrust::cuda_cub::par_nosync; +namespace detail +{ +using thrust::cuda_cub::par_nosync_t; +using thrust::cuda_cub::par_t; +} // namespace detail +} // namespace cuda +} // namespace system + +namespace cuda +{ +using thrust::cuda_cub::par; +using thrust::cuda_cub::par_nosync; +} // namespace cuda + +THRUST_NAMESPACE_END diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/par_to_seq.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/par_to_seq.h new file mode 100644 index 0000000000000000000000000000000000000000..7ba29b14737b8d87f4f74e8df5caeb9c625f05c0 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/par_to_seq.h @@ -0,0 +1,95 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include +#include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +struct has_par : thrust::detail::true_type +{}; + +template <> +struct has_par<0> : thrust::detail::false_type +{}; + +template +struct cvt_to_seq_impl +{ + using seq_t = thrust::detail::seq_t; + + static seq_t _CCCL_HOST_DEVICE doit(Policy&) + { + return seq_t(); + } +}; // cvt_to_seq_impl + +#if 0 +template +struct cvt_to_seq_impl< + thrust::detail::execute_with_allocator > +{ + using Policy = thrust::detail::execute_with_allocator + ; + using seq_t = thrust::detail::execute_with_allocator< + Allocator, + thrust::system::detail::sequential::execution_policy> + ; + + + static seq_t _CCCL_HOST_DEVICE + doit(Policy& policy) + { + return seq_t(policy.m_alloc); + } +}; // specialization of struct cvt_to_seq_impl +#endif + +template +typename cvt_to_seq_impl::seq_t _CCCL_HOST_DEVICE cvt_to_seq(Policy& policy) +{ + return cvt_to_seq_impl::doit(policy); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/parallel_for.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/parallel_for.h new file mode 100644 index 0000000000000000000000000000000000000000..cb4e9aac3e8b53e3e00195333345177892a86813 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/parallel_for.h @@ -0,0 +1,81 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include + +# include +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +_CCCL_EXEC_CHECK_DISABLE +template +void _CCCL_HOST_DEVICE parallel_for(execution_policy& policy, F f, Size count) +{ + if (count == 0) + { + return; + } + + // clang-format off + THRUST_CDP_DISPATCH( + (cudaStream_t stream = cuda_cub::stream(policy); + cudaError_t status = cub::DeviceFor::Bulk(count, f, stream); + cuda_cub::throw_on_error(status, "parallel_for failed"); + status = cuda_cub::synchronize_optional(policy); + cuda_cub::throw_on_error(status, "parallel_for: failed to synchronize");), + // CDP sequential impl: + (for (Size idx = 0; idx != count; ++idx) + { + f(idx); + } + )); + // clang-format on +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/per_device_resource.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/per_device_resource.h new file mode 100644 index 0000000000000000000000000000000000000000..8cc91a4793a4433cb4dbaf14fc87f76c1cf8d056 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/per_device_resource.h @@ -0,0 +1,72 @@ +/****************************************************************************** + * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include + +# include +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +template +_CCCL_HOST MR* get_per_device_resource(execution_policy&) +{ + static std::mutex map_lock; + static std::unordered_map device_id_to_resource; + + int device_id; + thrust::cuda_cub::throw_on_error(cudaGetDevice(&device_id)); + + std::lock_guard lock{map_lock}; + return &device_id_to_resource[device_id]; +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END + +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reduce.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reduce.h new file mode 100644 index 0000000000000000000000000000000000000000..9b9401e2891971859edbd9a3580095ba47f4a826 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reduce.h @@ -0,0 +1,902 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN + +// forward declare generic reduce +// to circumvent circular dependency +template +T _CCCL_HOST_DEVICE +reduce(const thrust::detail::execution_policy_base& exec, + InputIterator first, + InputIterator last, + T init, + BinaryFunction binary_op); + +namespace cuda_cub +{ + +namespace __reduce +{ + +template +struct is_true : thrust::detail::false_type +{}; +template <> +struct is_true : thrust::detail::true_type +{}; + +template +struct PtxPolicy +{ + enum + { + BLOCK_THREADS = _BLOCK_THREADS, + ITEMS_PER_THREAD = _ITEMS_PER_THREAD, + VECTOR_LOAD_LENGTH = _VECTOR_LOAD_LENGTH, + ITEMS_PER_TILE = _BLOCK_THREADS * _ITEMS_PER_THREAD + }; + + static const cub::BlockReduceAlgorithm BLOCK_ALGORITHM = _BLOCK_ALGORITHM; + static const cub::CacheLoadModifier LOAD_MODIFIER = _LOAD_MODIFIER; + static const cub::GridMappingStrategy GRID_MAPPING = _GRID_MAPPING; +}; // struct PtxPolicy + +template +struct Tuning; + +template +struct Tuning +{ + enum + { + // Relative size of T type to a 4-byte word + SCALE_FACTOR_4B = (sizeof(T) + 3) / 4, + // Relative size of T type to a 1-byte word + SCALE_FACTOR_1B = sizeof(T), + }; + + using type = + PtxPolicy<256, + (((20 / SCALE_FACTOR_4B) > (1)) ? (20 / SCALE_FACTOR_4B) : (1)), + 2, + cub::BLOCK_REDUCE_WARP_REDUCTIONS, + cub::LOAD_DEFAULT, + cub::GRID_MAPPING_RAKE>; +}; // Tuning sm30 + +template +struct Tuning : Tuning +{ + // ReducePolicy1B (GTX Titan: 228.7 GB/s @ 192M 1B items) + using ReducePolicy1B = + PtxPolicy<128, + (((24 / Tuning::SCALE_FACTOR_1B) > (1)) ? (24 / Tuning::SCALE_FACTOR_1B) : (1)), + 4, + cub::BLOCK_REDUCE_WARP_REDUCTIONS, + cub::LOAD_LDG, + cub::GRID_MAPPING_DYNAMIC>; + + // ReducePolicy4B types (GTX Titan: 255.1 GB/s @ 48M 4B items) + using ReducePolicy4B = + PtxPolicy<256, + (((20 / Tuning::SCALE_FACTOR_4B) > (1)) ? (20 / Tuning::SCALE_FACTOR_4B) : (1)), + 4, + cub::BLOCK_REDUCE_WARP_REDUCTIONS, + cub::LOAD_LDG, + cub::GRID_MAPPING_DYNAMIC>; + + using type = ::cuda::std::conditional_t<(sizeof(T) < 4), ReducePolicy1B, ReducePolicy4B>; +}; // Tuning sm35 + +template +struct ReduceAgent +{ + using UnsignedSize = typename detail::make_unsigned_special::type; + + template + struct PtxPlan : Tuning::type + { + // we need this type definition to indicate "specialize_plan" metafunction + // that this PtxPlan may have specializations for different Arch + // via Tuning type. + // + using tuning = Tuning; + + using Vector = typename cub::CubVector; + using LoadIt = typename core::LoadIterator::type; + using BlockReduce = cub::BlockReduce; + + using VectorLoadIt = cub::CacheModifiedInputIterator; + + struct TempStorage + { + typename BlockReduce::TempStorage reduce; + // + Size dequeue_offset; + }; // struct TempStorage + + }; // struct PtxPlan + + // Reduction need additional information which is not covered in + // default core::AgentPlan. We thus inherit from core::AgentPlan + // and add additional member fields that are needed. + // Other algorithms, e.g. merge, may not need additional information, + // and may use AgentPlan directly, instead of defining their own Plan type. + // + struct Plan : core::AgentPlan + { + cub::GridMappingStrategy grid_mapping; + + THRUST_RUNTIME_FUNCTION Plan() {} + + template + THRUST_RUNTIME_FUNCTION Plan(P) + : core::AgentPlan(P()) + , grid_mapping(P::GRID_MAPPING) + {} + }; + + // this specialized PtxPlan for a device-compiled Arch + // ptx_plan type *must* only be used from device code + // Its use from host code will result in *undefined behaviour* + // + using ptx_plan = typename core::specialize_plan_msvc10_war::type::type; + + using TempStorage = typename ptx_plan::TempStorage; + using Vector = typename ptx_plan::Vector; + using LoadIt = typename ptx_plan::LoadIt; + using BlockReduce = typename ptx_plan::BlockReduce; + using VectorLoadIt = typename ptx_plan::VectorLoadIt; + + enum + { + ITEMS_PER_THREAD = ptx_plan::ITEMS_PER_THREAD, + BLOCK_THREADS = ptx_plan::BLOCK_THREADS, + ITEMS_PER_TILE = ptx_plan::ITEMS_PER_TILE, + VECTOR_LOAD_LENGTH = ptx_plan::VECTOR_LOAD_LENGTH, + + ATTEMPT_VECTORIZATION = (VECTOR_LOAD_LENGTH > 1) && (ITEMS_PER_THREAD % VECTOR_LOAD_LENGTH == 0) + && ::cuda::std::is_pointer::value + && ::cuda::std::is_arithmetic>::value + }; + + struct impl + { + //--------------------------------------------------------------------- + // Per thread data + //--------------------------------------------------------------------- + + TempStorage& storage; + InputIt input_it; + LoadIt load_it; + ReductionOp reduction_op; + + //--------------------------------------------------------------------- + // Constructor + //--------------------------------------------------------------------- + + THRUST_DEVICE_FUNCTION impl(TempStorage& storage_, InputIt input_it_, ReductionOp reduction_op_) + : storage(storage_) + , input_it(input_it_) + , load_it(core::make_load_iterator(ptx_plan(), input_it)) + , reduction_op(reduction_op_) + {} + + //--------------------------------------------------------------------- + // Utility + //--------------------------------------------------------------------- + + // Whether or not the input is aligned with the vector type + // (specialized for types we can vectorize) + // + template + static THRUST_DEVICE_FUNCTION bool is_aligned(Iterator d_in, thrust::detail::true_type /* can_vectorize */) + { + return (size_t(d_in) & (sizeof(Vector) - 1)) == 0; + } + + // Whether or not the input is aligned with the vector type + // (specialized for types we cannot vectorize) + // + template + static THRUST_DEVICE_FUNCTION bool is_aligned(Iterator, thrust::detail::false_type /* can_vectorize */) + { + return false; + } + + //--------------------------------------------------------------------- + // Tile processing + //--------------------------------------------------------------------- + + // Consume a full tile of input (non-vectorized) + // + template + THRUST_DEVICE_FUNCTION void consume_tile( + T& thread_aggregate, + Size block_offset, + int /*valid_items*/, + thrust::detail::true_type /* is_full_tile */, + thrust::detail::false_type /* can_vectorize */) + { + T items[ITEMS_PER_THREAD]; + + // Load items in striped fashion + cub::LoadDirectStriped(threadIdx.x, load_it + block_offset, items); + + // Reduce items within each thread stripe + thread_aggregate = (IS_FIRST_TILE) ? cub::ThreadReduce(items, reduction_op) + : cub::ThreadReduce(items, reduction_op, thread_aggregate); + } + + // Consume a full tile of input (vectorized) + // + template + THRUST_DEVICE_FUNCTION void consume_tile( + T& thread_aggregate, + Size block_offset, + int /*valid_items*/, + thrust::detail::true_type /* is_full_tile */, + thrust::detail::true_type /* can_vectorize */) + { + // Alias items as an array of VectorT and load it in striped fashion + enum + { + WORDS = ITEMS_PER_THREAD / VECTOR_LOAD_LENGTH + }; + + T items[ITEMS_PER_THREAD]; + + Vector* vec_items = reinterpret_cast(items); + + // Vector Input iterator wrapper type (for applying cache modifier) + T* d_in_unqualified = const_cast(input_it) + block_offset + (threadIdx.x * VECTOR_LOAD_LENGTH); + VectorLoadIt vec_load_it(reinterpret_cast(d_in_unqualified)); + +# pragma unroll + for (int i = 0; i < WORDS; ++i) + { + vec_items[i] = vec_load_it[BLOCK_THREADS * i]; + } + + // Reduce items within each thread stripe + thread_aggregate = (IS_FIRST_TILE) ? cub::ThreadReduce(items, reduction_op) + : cub::ThreadReduce(items, reduction_op, thread_aggregate); + } + + // Consume a partial tile of input + // + template + THRUST_DEVICE_FUNCTION void consume_tile( + T& thread_aggregate, + Size block_offset, + int valid_items, + thrust::detail::false_type /* is_full_tile */, + CAN_VECTORIZE) + { + // Partial tile + int thread_offset = threadIdx.x; + + // Read first item + if ((IS_FIRST_TILE) && (thread_offset < valid_items)) + { + thread_aggregate = load_it[block_offset + thread_offset]; + thread_offset += BLOCK_THREADS; + } + + // Continue reading items (block-striped) + while (thread_offset < valid_items) + { + thread_aggregate = + reduction_op(thread_aggregate, thrust::raw_reference_cast(load_it[block_offset + thread_offset])); + thread_offset += BLOCK_THREADS; + } + } + + //--------------------------------------------------------------- + // Consume a contiguous segment of tiles + //--------------------------------------------------------------------- + + // Reduce a contiguous segment of input tiles + // + template + THRUST_DEVICE_FUNCTION T consume_range_impl(Size block_offset, Size block_end, CAN_VECTORIZE can_vectorize) + { + T thread_aggregate; + + if (block_offset + ITEMS_PER_TILE > block_end) + { + // First tile isn't full (not all threads have valid items) + int valid_items = block_end - block_offset; + consume_tile(thread_aggregate, block_offset, valid_items, thrust::detail::false_type(), can_vectorize); + return BlockReduce(storage.reduce).Reduce(thread_aggregate, reduction_op, valid_items); + } + + // At least one full block + consume_tile(thread_aggregate, block_offset, ITEMS_PER_TILE, thrust::detail::true_type(), can_vectorize); + block_offset += ITEMS_PER_TILE; + + // Consume subsequent full tiles of input + while (block_offset + ITEMS_PER_TILE <= block_end) + { + consume_tile(thread_aggregate, block_offset, ITEMS_PER_TILE, thrust::detail::true_type(), can_vectorize); + block_offset += ITEMS_PER_TILE; + } + + // Consume a partially-full tile + if (block_offset < block_end) + { + int valid_items = block_end - block_offset; + consume_tile(thread_aggregate, block_offset, valid_items, thrust::detail::false_type(), can_vectorize); + } + + // Compute block-wide reduction (all threads have valid items) + return BlockReduce(storage.reduce).Reduce(thread_aggregate, reduction_op); + } + + // Reduce a contiguous segment of input tiles + // + THRUST_DEVICE_FUNCTION T consume_range(Size block_offset, Size block_end) + { + using attempt_vec = is_true; + using path_a = is_true; + using path_b = is_true; + + return is_aligned(input_it + block_offset, attempt_vec()) + ? consume_range_impl(block_offset, block_end, path_a()) + : consume_range_impl(block_offset, block_end, path_b()); + } + + // Reduce a contiguous segment of input tiles + // + THRUST_DEVICE_FUNCTION T consume_tiles( + Size /*num_items*/, + cub::GridEvenShare& even_share, + cub::GridQueue& /*queue*/, + thrust::detail::integral_constant /*is_rake*/) + { + using attempt_vec = is_true; + using path_a = is_true; + using path_b = is_true; + + // Initialize even-share descriptor for this thread block + even_share.template BlockInit(); + + return is_aligned(input_it, attempt_vec()) + ? consume_range_impl(even_share.block_offset, even_share.block_end, path_a()) + : consume_range_impl(even_share.block_offset, even_share.block_end, path_b()); + } + + //--------------------------------------------------------------------- + // Dynamically consume tiles + //--------------------------------------------------------------------- + + // Dequeue and reduce tiles of items as part of a inter-block reduction + // + template + THRUST_DEVICE_FUNCTION T + consume_tiles_impl(Size num_items, cub::GridQueue queue, CAN_VECTORIZE can_vectorize) + { + using core::sync_threadblock; + + // We give each thread block at least one tile of input. + T thread_aggregate; + Size block_offset = blockIdx.x * ITEMS_PER_TILE; + Size even_share_base = gridDim.x * ITEMS_PER_TILE; + + if (block_offset + ITEMS_PER_TILE > num_items) + { + // First tile isn't full (not all threads have valid items) + int valid_items = num_items - block_offset; + consume_tile(thread_aggregate, block_offset, valid_items, thrust::detail::false_type(), can_vectorize); + return BlockReduce(storage.reduce).Reduce(thread_aggregate, reduction_op, valid_items); + } + + // Consume first full tile of input + consume_tile(thread_aggregate, block_offset, ITEMS_PER_TILE, thrust::detail::true_type(), can_vectorize); + + if (num_items > even_share_base) + { + // Dequeue a tile of items + if (threadIdx.x == 0) + { + storage.dequeue_offset = queue.Drain(ITEMS_PER_TILE) + even_share_base; + } + + sync_threadblock(); + + // Grab tile offset and check if we're done with full tiles + block_offset = storage.dequeue_offset; + + // Consume more full tiles + while (block_offset + ITEMS_PER_TILE <= num_items) + { + consume_tile( + thread_aggregate, block_offset, ITEMS_PER_TILE, thrust::detail::true_type(), can_vectorize); + + sync_threadblock(); + + // Dequeue a tile of items + if (threadIdx.x == 0) + { + storage.dequeue_offset = queue.Drain(ITEMS_PER_TILE) + even_share_base; + } + + sync_threadblock(); + + // Grab tile offset and check if we're done with full tiles + block_offset = storage.dequeue_offset; + } + + // Consume partial tile + if (block_offset < num_items) + { + int valid_items = num_items - block_offset; + consume_tile(thread_aggregate, block_offset, valid_items, thrust::detail::false_type(), can_vectorize); + } + } + + // Compute block-wide reduction (all threads have valid items) + return BlockReduce(storage.reduce).Reduce(thread_aggregate, reduction_op); + } + + // Dequeue and reduce tiles of items as part of a inter-block reduction + // + THRUST_DEVICE_FUNCTION T consume_tiles( + Size num_items, + cub::GridEvenShare& /*even_share*/, + cub::GridQueue& queue, + thrust::detail::integral_constant) + { + using attempt_vec = is_true; + using path_a = is_true; + using path_b = is_true; + + return is_aligned(input_it, attempt_vec()) + ? consume_tiles_impl(num_items, queue, path_a()) + : consume_tiles_impl(num_items, queue, path_b()); + } + }; // struct impl + + //--------------------------------------------------------------------- + // Agent entry points + //--------------------------------------------------------------------- + + // single tile reduce entry point + // + THRUST_AGENT_ENTRY(InputIt input_it, OutputIt output_it, Size num_items, ReductionOp reduction_op, char* shmem) + { + TempStorage& storage = *reinterpret_cast(shmem); + + if (num_items == 0) + { + return; + } + + T block_aggregate = impl(storage, input_it, reduction_op).consume_range((Size) 0, num_items); + + if (threadIdx.x == 0) + { + *output_it = block_aggregate; + } + } + + // single tile reduce entry point + // + THRUST_AGENT_ENTRY(InputIt input_it, OutputIt output_it, Size num_items, ReductionOp reduction_op, T init, char* shmem) + { + TempStorage& storage = *reinterpret_cast(shmem); + + if (num_items == 0) + { + if (threadIdx.x == 0) + { + *output_it = init; + } + return; + } + + T block_aggregate = impl(storage, input_it, reduction_op).consume_range((Size) 0, num_items); + + if (threadIdx.x == 0) + { + *output_it = reduction_op(init, block_aggregate); + } + } + + THRUST_AGENT_ENTRY( + InputIt input_it, + OutputIt output_it, + Size num_items, + cub::GridEvenShare even_share, + cub::GridQueue queue, + ReductionOp reduction_op, + char* shmem) + { + TempStorage& storage = *reinterpret_cast(shmem); + + using grid_mapping = thrust::detail::integral_constant; + + T block_aggregate = + impl(storage, input_it, reduction_op).consume_tiles(num_items, even_share, queue, grid_mapping()); + + if (threadIdx.x == 0) + { + output_it[blockIdx.x] = block_aggregate; + } + } +}; // struct ReduceAgent + +template +struct DrainAgent +{ + using UnsignedSize = typename detail::make_unsigned_special::type; + + template + struct PtxPlan : PtxPolicy<1> + {}; + using ptx_plan = core::specialize_plan; + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY(cub::GridQueue grid_queue, Size num_items, char* /*shmem*/) + { + grid_queue.FillAndResetDrain(num_items); + } +}; // struct DrainAgent; + +template +cudaError_t THRUST_RUNTIME_FUNCTION doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + InputIt input_it, + Size num_items, + T init, + ReductionOp reduction_op, + OutputIt output_it, + cudaStream_t stream) +{ + using core::AgentLauncher; + using core::AgentPlan; + using core::cuda_optional; + using core::get_agent_plan; + + using UnsignedSize = typename detail::make_unsigned_special::type; + + if (num_items == 0) + { + return cudaErrorNotSupported; + } + + using reduce_agent = AgentLauncher>; + + typename reduce_agent::Plan reduce_plan = reduce_agent::get_plan(stream); + + cudaError_t status = cudaSuccess; + + if (num_items <= reduce_plan.items_per_tile) + { + size_t vshmem_size = core::vshmem_size(reduce_plan.shared_memory_size, 1); + + // small, single tile size + if (d_temp_storage == nullptr) + { + temp_storage_bytes = max(1, vshmem_size); + return status; + } + char* vshmem_ptr = vshmem_size > 0 ? (char*) d_temp_storage : nullptr; + + reduce_agent ra(reduce_plan, num_items, stream, vshmem_ptr, "reduce_agent: single_tile only"); + ra.launch(input_it, output_it, num_items, reduction_op, init); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + } + else + { + // regular size + cuda_optional sm_count = core::get_sm_count(); + CUDA_CUB_RET_IF_FAIL(sm_count.status()); + + // reduction will not use more cta counts than requested + cuda_optional max_blocks_per_sm = reduce_agent::template get_max_blocks_per_sm< + InputIt, + OutputIt, + Size, + cub::GridEvenShare, + cub::GridQueue, + ReductionOp>(reduce_plan); + CUDA_CUB_RET_IF_FAIL(max_blocks_per_sm.status()); + + int reduce_device_occupancy = (int) max_blocks_per_sm * sm_count; + + int sm_oversubscription = 5; + int max_blocks = reduce_device_occupancy * sm_oversubscription; + + cub::GridEvenShare even_share; + even_share.DispatchInit(static_cast(num_items), max_blocks, reduce_plan.items_per_tile); + + // we will launch at most "max_blocks" blocks in a grid + // so preallocate virtual shared memory storage for this if required + // + size_t vshmem_size = core::vshmem_size(reduce_plan.shared_memory_size, max_blocks); + + // Temporary storage allocation requirements + void* allocations[3] = {nullptr, nullptr, nullptr}; + size_t allocation_sizes[3] = { + max_blocks * sizeof(T), // bytes needed for privatized block reductions + cub::GridQueue::AllocationSize(), // bytes needed for grid queue descriptor0 + vshmem_size // size of virtualized shared memory storage + }; + status = cub::AliasTemporaries(d_temp_storage, temp_storage_bytes, allocations, allocation_sizes); + CUDA_CUB_RET_IF_FAIL(status); + if (d_temp_storage == nullptr) + { + return status; + } + + T* d_block_reductions = (T*) allocations[0]; + cub::GridQueue queue(allocations[1]); + char* vshmem_ptr = vshmem_size > 0 ? (char*) allocations[2] : nullptr; + + // Get grid size for device_reduce_sweep_kernel + int reduce_grid_size = 0; + if (reduce_plan.grid_mapping == cub::GRID_MAPPING_RAKE) + { + // Work is distributed evenly + reduce_grid_size = even_share.grid_size; + } + else if (reduce_plan.grid_mapping == cub::GRID_MAPPING_DYNAMIC) + { + // Work is distributed dynamically + size_t num_tiles = ::cuda::ceil_div(num_items, reduce_plan.items_per_tile); + + // if not enough to fill the device with threadblocks + // then fill the device with threadblocks + reduce_grid_size = static_cast((min) (num_tiles, static_cast(reduce_device_occupancy))); + + using drain_agent = AgentLauncher>; + AgentPlan drain_plan = drain_agent::get_plan(); + drain_plan.grid_size = 1; + drain_agent da(drain_plan, stream, "__reduce::drain_agent"); + da.launch(queue, num_items); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + } + else + { + CUDA_CUB_RET_IF_FAIL(cudaErrorNotSupported); + } + + reduce_plan.grid_size = reduce_grid_size; + reduce_agent ra(reduce_plan, stream, vshmem_ptr, "reduce_agent: regular size reduce"); + ra.launch(input_it, d_block_reductions, num_items, even_share, queue, reduction_op); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + + using reduce_agent_single = AgentLauncher>; + + reduce_plan.grid_size = 1; + reduce_agent_single ra1(reduce_plan, stream, vshmem_ptr, "reduce_agent: single tile reduce"); + + ra1.launch(d_block_reductions, output_it, reduce_grid_size, reduction_op, init); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + } + + return status; +} // func doit_step + +template +THRUST_RUNTIME_FUNCTION T +reduce(execution_policy& policy, InputIt first, Size num_items, T init, BinaryOp binary_op) +{ + if (num_items == 0) + { + return init; + } + + size_t temp_storage_bytes = 0; + cudaStream_t stream = cuda_cub::stream(policy); + + cudaError_t status; + status = doit_step(nullptr, temp_storage_bytes, first, num_items, init, binary_op, static_cast(nullptr), stream); + cuda_cub::throw_on_error(status, "reduce failed on 1st step"); + + size_t allocation_sizes[2] = {sizeof(T*), temp_storage_bytes}; + void* allocations[2] = {nullptr, nullptr}; + + size_t storage_size = 0; + status = core::alias_storage(nullptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "reduce failed on 1st alias_storage"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, storage_size); + void* ptr = static_cast(tmp.data().get()); + + status = core::alias_storage(ptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "reduce failed on 2nd alias_storage"); + + T* d_result = thrust::detail::aligned_reinterpret_cast(allocations[0]); + + status = doit_step(allocations[1], temp_storage_bytes, first, num_items, init, binary_op, d_result, stream); + cuda_cub::throw_on_error(status, "reduce failed on 2nd step"); + + status = cuda_cub::synchronize(policy); + cuda_cub::throw_on_error(status, "reduce failed to synchronize"); + + T result = cuda_cub::get_value(policy, d_result); + + return result; +} +} // namespace __reduce + +namespace detail +{ + +template +THRUST_RUNTIME_FUNCTION T +reduce_n_impl(execution_policy& policy, InputIt first, Size num_items, T init, BinaryOp binary_op) +{ + cudaStream_t stream = cuda_cub::stream(policy); + cudaError_t status; + + // Determine temporary device storage requirements. + + size_t tmp_size = 0; + + THRUST_INDEX_TYPE_DISPATCH( + status, + cub::DeviceReduce::Reduce, + num_items, + (nullptr, tmp_size, first, static_cast(nullptr), num_items_fixed, binary_op, init, stream)); + cuda_cub::throw_on_error(status, "after reduction step 1"); + + // Allocate temporary storage. + + thrust::detail::temporary_array tmp(policy, sizeof(T) + tmp_size); + + // Run reduction. + + // `tmp.begin()` yields a `normal_iterator`, which dereferences to a + // `reference`, which has an `operator&` that returns a `pointer`, which + // has a `.get` method that returns a raw pointer, which we can (finally) + // `static_cast` to `void*`. + // + // The array was dynamically allocated, so we assume that it's suitably + // aligned for any type of data. `malloc`/`cudaMalloc`/`new`/`std::allocator` + // make this guarantee. + T* ret_ptr = thrust::detail::aligned_reinterpret_cast(tmp.data().get()); + void* tmp_ptr = static_cast((tmp.data() + sizeof(T)).get()); + THRUST_INDEX_TYPE_DISPATCH( + status, + cub::DeviceReduce::Reduce, + num_items, + (tmp_ptr, tmp_size, first, ret_ptr, num_items_fixed, binary_op, init, stream)); + cuda_cub::throw_on_error(status, "after reduction step 2"); + + // Synchronize the stream and get the value. + + status = cuda_cub::synchronize(policy); + cuda_cub::throw_on_error(status, "reduce failed to synchronize"); + + // `tmp.begin()` yields a `normal_iterator`, which dereferences to a + // `reference`, which has an `operator&` that returns a `pointer`, which + // has a `.get` method that returns a raw pointer, which we can (finally) + // `static_cast` to `void*`. + // + // The array was dynamically allocated, so we assume that it's suitably + // aligned for any type of data. `malloc`/`cudaMalloc`/`new`/`std::allocator` + // make this guarantee. + return thrust::cuda_cub::get_value(policy, thrust::detail::aligned_reinterpret_cast(tmp.data().get())); +} + +} // namespace detail + +//------------------------- +// Thrust API entry points +//------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +_CCCL_HOST_DEVICE T +reduce_n(execution_policy& policy, InputIt first, Size num_items, T init, BinaryOp binary_op) +{ + THRUST_CDP_DISPATCH( + (init = thrust::cuda_cub::detail::reduce_n_impl(policy, first, num_items, init, binary_op);), + (init = thrust::reduce(cvt_to_seq(derived_cast(policy)), first, first + num_items, init, binary_op);)); + return init; +} + +template +_CCCL_HOST_DEVICE T reduce(execution_policy& policy, InputIt first, InputIt last, T init, BinaryOp binary_op) +{ + using size_type = typename iterator_traits::difference_type; + // FIXME: Check for RA iterator. + size_type num_items = static_cast(thrust::distance(first, last)); + return cuda_cub::reduce_n(policy, first, num_items, init, binary_op); +} + +template +_CCCL_HOST_DEVICE T reduce(execution_policy& policy, InputIt first, InputIt last, T init) +{ + return cuda_cub::reduce(policy, first, last, init, plus()); +} + +template +_CCCL_HOST_DEVICE typename iterator_traits::value_type +reduce(execution_policy& policy, InputIt first, InputIt last) +{ + using value_type = typename iterator_traits::value_type; + return cuda_cub::reduce(policy, first, last, value_type(0)); +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END + +# include +# include + +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reduce_by_key.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reduce_by_key.h new file mode 100644 index 0000000000000000000000000000000000000000..58aa33fd7e85572c490b660631d960ea562146be --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reduce_by_key.h @@ -0,0 +1,1048 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN + +template +_CCCL_HOST_DEVICE thrust::pair reduce_by_key( + const thrust::detail::execution_policy_base& exec, + InputIterator1 keys_first, + InputIterator1 keys_last, + InputIterator2 values_first, + OutputIterator1 keys_output, + OutputIterator2 values_output, + BinaryPredicate binary_pred); + +namespace cuda_cub +{ + +namespace __reduce_by_key +{ + +template +struct is_true : thrust::detail::false_type +{}; +template <> +struct is_true : thrust::detail::true_type +{}; + +namespace mpl = thrust::detail::mpl::math; + +template +struct PtxPolicy +{ + enum + { + BLOCK_THREADS = _BLOCK_THREADS, + ITEMS_PER_THREAD = _ITEMS_PER_THREAD, + ITEMS_PER_TILE = BLOCK_THREADS * ITEMS_PER_THREAD + }; + + static const cub::BlockLoadAlgorithm LOAD_ALGORITHM = _LOAD_ALGORITHM; + static const cub::CacheLoadModifier LOAD_MODIFIER = _LOAD_MODIFIER; + static const cub::BlockScanAlgorithm SCAN_ALGORITHM = _SCAN_ALGORITHM; +}; // struct PtxPolicy + +template +struct Tuning; + +template +struct Tuning +{ + enum + { + MAX_INPUT_BYTES = mpl::max::value, + COMBINED_INPUT_BYTES = sizeof(Key) + sizeof(Value), + + NOMINAL_4B_ITEMS_PER_THREAD = 6, + + ITEMS_PER_THREAD = + mpl::min(((NOMINAL_4B_ITEMS_PER_THREAD * 8) + COMBINED_INPUT_BYTES - 1) + / COMBINED_INPUT_BYTES)>::value>::value, + }; + + using type = + PtxPolicy<128, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_DEFAULT, cub::BLOCK_SCAN_WARP_SCANS>; +}; // Tuning sm30 + +template +struct Tuning : Tuning +{ + enum + { + MAX_INPUT_BYTES = mpl::max::value, + COMBINED_INPUT_BYTES = sizeof(Key) + sizeof(Value), + + NOMINAL_4B_ITEMS_PER_THREAD = 6, + + ITEMS_PER_THREAD = + (MAX_INPUT_BYTES <= 8) + ? 6 + : mpl::min< + int, + NOMINAL_4B_ITEMS_PER_THREAD, + mpl::max:: + value>::value, + }; + + using type = + PtxPolicy<128, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_LDG, cub::BLOCK_SCAN_WARP_SCANS>; +}; // Tuning sm35 + +template +struct Tuning : Tuning +{ + enum + { + MAX_INPUT_BYTES = mpl::max::value, + COMBINED_INPUT_BYTES = sizeof(Key) + sizeof(Value), + + NOMINAL_4B_ITEMS_PER_THREAD = 9, + + ITEMS_PER_THREAD = + (MAX_INPUT_BYTES <= 8) + ? 9 + : mpl::min< + int, + NOMINAL_4B_ITEMS_PER_THREAD, + mpl::max:: + value>::value, + }; + + using type = + PtxPolicy<256, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_LDG, cub::BLOCK_SCAN_WARP_SCANS>; +}; // Tuning sm52 + +template +struct ReduceByKeyAgent +{ + using key_type = typename iterator_traits::value_type; + using value_type = typename iterator_traits::value_type; + using size_type = Size; + + using size_value_pair_t = cub::KeyValuePair; + using key_value_pair_t = cub::KeyValuePair; + + using ScanTileState = cub::ReduceByKeyScanTileState; + using ReduceBySegmentOp = cub::ReduceBySegmentOp; + + template + struct PtxPlan : Tuning::type + { + using tuning = Tuning; + + using KeysLoadIt = typename core::LoadIterator::type; + using ValuesLoadIt = typename core::LoadIterator::type; + + using BlockLoadKeys = typename core::BlockLoad::type; + using BlockLoadValues = typename core::BlockLoad::type; + + using BlockDiscontinuityKeys = cub::BlockDiscontinuity; + + using TilePrefixCallback = + cub::TilePrefixCallbackOp; + using BlockScan = + cub::BlockScan; + + union TempStorage + { + struct ScanStorage + { + typename BlockScan::TempStorage scan; + typename TilePrefixCallback::TempStorage prefix; + typename BlockDiscontinuityKeys::TempStorage discontinuity; + } scan_storage; + + typename BlockLoadKeys::TempStorage load_keys; + typename BlockLoadValues::TempStorage load_values; + + core::uninitialized_array raw_exchange; + }; // union TempStorage + }; // struct PtxPlan + + using ptx_plan = typename core::specialize_plan_msvc10_war::type::type; + + using KeysLoadIt = typename ptx_plan::KeysLoadIt; + using ValuesLoadIt = typename ptx_plan::ValuesLoadIt; + using BlockLoadKeys = typename ptx_plan::BlockLoadKeys; + using BlockLoadValues = typename ptx_plan::BlockLoadValues; + using BlockDiscontinuityKeys = typename ptx_plan::BlockDiscontinuityKeys; + using TilePrefixCallback = typename ptx_plan::TilePrefixCallback; + using BlockScan = typename ptx_plan::BlockScan; + using TempStorage = typename ptx_plan::TempStorage; + + enum + { + BLOCK_THREADS = ptx_plan::BLOCK_THREADS, + ITEMS_PER_THREAD = ptx_plan::ITEMS_PER_THREAD, + ITEMS_PER_TILE = ptx_plan::ITEMS_PER_TILE, + TWO_PHASE_SCATTER = (ITEMS_PER_THREAD > 1), + + // Whether or not the scan operation has a zero-valued identity value + // (true if we're performing addition on a primitive type) + HAS_IDENTITY_ZERO = + ::cuda::std::is_same>::value && ::cuda::std::is_arithmetic::value + }; + + struct impl + { + //--------------------------------------------------------------------- + // Per-thread fields + //--------------------------------------------------------------------- + + TempStorage& storage; + KeysLoadIt keys_load_it; + ValuesLoadIt values_load_it; + KeysOutputIt keys_output_it; + ValuesOutputIt values_output_it; + NumRunsOutputIt num_runs_output_it; + cub::InequalityWrapper inequality_op; + ReduceBySegmentOp scan_op; + + //--------------------------------------------------------------------- + // Block scan utility methods + //--------------------------------------------------------------------- + + // Scan with identity (first tile) + // + THRUST_DEVICE_FUNCTION void scan_tile(size_value_pair_t (&scan_items)[ITEMS_PER_THREAD], + size_value_pair_t& tile_aggregate, + thrust::detail::true_type /* has_identity */) + { + size_value_pair_t identity; + identity.value = 0; + identity.key = 0; + BlockScan(storage.scan_storage.scan).ExclusiveScan(scan_items, scan_items, identity, scan_op, tile_aggregate); + } + + // Scan without identity (first tile). + // Without an identity, the first output item is undefined. + // + THRUST_DEVICE_FUNCTION void scan_tile(size_value_pair_t (&scan_items)[ITEMS_PER_THREAD], + size_value_pair_t& tile_aggregate, + thrust::detail::false_type /* has_identity */) + { + BlockScan(storage.scan_storage.scan).ExclusiveScan(scan_items, scan_items, scan_op, tile_aggregate); + } + + // Scan with identity (subsequent tile) + // + THRUST_DEVICE_FUNCTION void scan_tile( + size_value_pair_t (&scan_items)[ITEMS_PER_THREAD], + size_value_pair_t& tile_aggregate, + TilePrefixCallback& prefix_op, + thrust::detail::true_type /* has_identity */) + { + BlockScan(storage.scan_storage.scan).ExclusiveScan(scan_items, scan_items, scan_op, prefix_op); + tile_aggregate = prefix_op.GetBlockAggregate(); + } + + // Scan without identity (subsequent tile). + // Without an identity, the first output item is undefined. + THRUST_DEVICE_FUNCTION void scan_tile( + size_value_pair_t (&scan_items)[ITEMS_PER_THREAD], + size_value_pair_t& tile_aggregate, + TilePrefixCallback& prefix_op, + thrust::detail::false_type /* has_identity */) + { + BlockScan(storage.scan_storage.scan).ExclusiveScan(scan_items, scan_items, scan_op, prefix_op); + tile_aggregate = prefix_op.GetBlockAggregate(); + } + + //--------------------------------------------------------------------- + // Zip utility methods + //--------------------------------------------------------------------- + + template + THRUST_DEVICE_FUNCTION void zip_values_and_flags( + size_type num_remaining, + value_type (&values)[ITEMS_PER_THREAD], + size_type (&segment_flags)[ITEMS_PER_THREAD], + size_value_pair_t (&scan_items)[ITEMS_PER_THREAD]) + { + // Zip values and segment_flags +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + // Set segment_flags for first out-of-bounds item, zero for others + if (IS_LAST_TILE && Size(threadIdx.x * ITEMS_PER_THREAD) + ITEM == num_remaining) + { + segment_flags[ITEM] = 1; + } + + scan_items[ITEM].value = values[ITEM]; + scan_items[ITEM].key = segment_flags[ITEM]; + } + } + + THRUST_DEVICE_FUNCTION void zip_keys_and_values( + key_type (&keys)[ITEMS_PER_THREAD], + size_type (&segment_indices)[ITEMS_PER_THREAD], + size_value_pair_t (&scan_items)[ITEMS_PER_THREAD], + key_value_pair_t (&scatter_items)[ITEMS_PER_THREAD]) + { + // Zip values and segment_flags +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + scatter_items[ITEM].key = keys[ITEM]; + scatter_items[ITEM].value = scan_items[ITEM].value; + segment_indices[ITEM] = scan_items[ITEM].key; + } + } + + //--------------------------------------------------------------------- + // Scatter utility methods + //--------------------------------------------------------------------- + + // Directly scatter flagged items to output offsets + // (specialized for IS_SEGMENTED_REDUCTION_FIXUP == false) + THRUST_DEVICE_FUNCTION void scatter_direct( + key_value_pair_t (&scatter_items)[ITEMS_PER_THREAD], + size_type (&segment_flags)[ITEMS_PER_THREAD], + size_type (&segment_indices)[ITEMS_PER_THREAD]) + { + // Scatter flagged keys and values +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + if (segment_flags[ITEM]) + { + keys_output_it[segment_indices[ITEM]] = scatter_items[ITEM].key; + values_output_it[segment_indices[ITEM]] = scatter_items[ITEM].value; + } + } + } + + // 2-phase scatter flagged items to output offsets + // (specialized for IS_SEGMENTED_REDUCTION_FIXUP == false + // + // The exclusive scan causes each head flag to be paired with + // the previous value aggregate: + // * the scatter offsets must be decremented for value aggregates + // + THRUST_DEVICE_FUNCTION void scatter_two_phase( + key_value_pair_t (&scatter_items)[ITEMS_PER_THREAD], + size_type (&segment_flags)[ITEMS_PER_THREAD], + size_type (&segment_indices)[ITEMS_PER_THREAD], + size_type num_tile_segments, + size_type num_tile_segments_prefix) + { + using core::sync_threadblock; + + sync_threadblock(); + + // Compact and scatter keys +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + if (segment_flags[ITEM]) + { + int idx = static_cast(segment_indices[ITEM] - num_tile_segments_prefix); + storage.raw_exchange[idx] = scatter_items[ITEM]; + } + } + + sync_threadblock(); + + for (int item = threadIdx.x; item < num_tile_segments; item += BLOCK_THREADS) + { + size_type idx = num_tile_segments_prefix + item; + key_value_pair_t pair = storage.raw_exchange[item]; + keys_output_it[idx] = pair.key; + values_output_it[idx] = pair.value; + } + } + + // Scatter flagged items + // + THRUST_DEVICE_FUNCTION void scatter( + key_value_pair_t (&scatter_items)[ITEMS_PER_THREAD], + size_type (&segment_flags)[ITEMS_PER_THREAD], + size_type (&segment_indices)[ITEMS_PER_THREAD], + size_type num_tile_segments, + size_type num_tile_segments_prefix) + { + // Do a one-phase scatter if (a) two-phase is disabled or + // (b) the average number of selected items per thread is less than one + if (TWO_PHASE_SCATTER && (num_tile_segments > BLOCK_THREADS)) + { + scatter_two_phase(scatter_items, segment_flags, segment_indices, num_tile_segments, num_tile_segments_prefix); + } + else + { + scatter_direct(scatter_items, segment_flags, segment_indices); + } + } + + //--------------------------------------------------------------------- + // Finalization utility methods + //--------------------------------------------------------------------- + + // Finalize the carry-out from the last tile + // (specialized for IS_SEGMENTED_REDUCTION_FIXUP == false) + THRUST_DEVICE_FUNCTION void + finalize_last_tile(size_type num_segments, size_type num_remaining, key_type last_key, value_type last_value) + { + // Last thread will output final count and last item, if necessary + if (threadIdx.x == BLOCK_THREADS - 1) + { + // If the last tile is a whole tile, the inclusive prefix + // contains accumulated value reduction for the last segment + if (num_remaining == ITEMS_PER_TILE) + { + // Scatter key and value + keys_output_it[num_segments] = last_key; + values_output_it[num_segments] = last_value; + num_segments++; + } + + // Output the total number of items selected + *num_runs_output_it = num_segments; + } + } + + //--------------------------------------------------------------------- + // Cooperatively scan a device-wide sequence of tiles with other CTAs + //--------------------------------------------------------------------- + + // Process first tile of input (dynamic chained scan). + // Returns the running count of segments + // and aggregated values (including this tile) + // + template + THRUST_DEVICE_FUNCTION void consume_first_tile(Size num_remaining, Size tile_offset, ScanTileState& tile_state) + { + using core::sync_threadblock; + + key_type keys[ITEMS_PER_THREAD]; // Tile keys + key_type pred_keys[ITEMS_PER_THREAD]; // Tile keys shifted up (predecessor) + value_type values[ITEMS_PER_THREAD]; // Tile values + size_type segment_flags[ITEMS_PER_THREAD]; // Segment head flags + size_type segment_indices[ITEMS_PER_THREAD]; // Segment indices + size_value_pair_t scan_items[ITEMS_PER_THREAD]; // Zipped values and segment flags|indices + key_value_pair_t scatter_items[ITEMS_PER_THREAD]; // Zipped key value pairs for scattering + + // Load keys (last tile repeats final element) + if (IS_LAST_TILE) + { + // Fill last elements with the first element + // because collectives are not suffix guarded + BlockLoadKeys(storage.load_keys) + .Load(keys_load_it + tile_offset, keys, num_remaining, *(keys_load_it + tile_offset)); + } + else + { + BlockLoadKeys(storage.load_keys).Load(keys_load_it + tile_offset, keys); + } + + sync_threadblock(); + + // Load values (last tile repeats final element) + if (IS_LAST_TILE) + { + BlockLoadValues(storage.load_values) + .Load(values_load_it + tile_offset, values, num_remaining, *(values_load_it + tile_offset)); + } + else + { + BlockLoadValues(storage.load_values).Load(values_load_it + tile_offset, values); + } + + sync_threadblock(); + + // Set head segment_flags. + // First tile sets the first flag for the first item + BlockDiscontinuityKeys(storage.scan_storage.discontinuity) + .FlagHeads(segment_flags, keys, pred_keys, inequality_op); + + // Unset the flag for the first item in the first tile + // so we won't scatter it + // + if (threadIdx.x == 0) + { + segment_flags[0] = 0; + } + + // Zip values and segment_flags + zip_values_and_flags(num_remaining, values, segment_flags, scan_items); + + // Exclusive scan of values and segment_flags + size_value_pair_t tile_aggregate; + scan_tile(scan_items, tile_aggregate, is_true()); + + if (threadIdx.x == 0) + { + // Update tile status if this is not the last tile + if (!IS_LAST_TILE) + { + tile_state.SetInclusive(0, tile_aggregate); + } + + // Initialize the segment index for the first scan item if necessary + // (the exclusive prefix for the first item is garbage) + if (!HAS_IDENTITY_ZERO) + { + scan_items[0].key = 0; + } + } + + // Unzip values and segment indices + zip_keys_and_values(pred_keys, segment_indices, scan_items, scatter_items); + + // Scatter flagged items + scatter(scatter_items, segment_flags, segment_indices, tile_aggregate.key, 0); + + if (IS_LAST_TILE) + { + // Finalize the carry-out from the last tile + finalize_last_tile(tile_aggregate.key, num_remaining, keys[ITEMS_PER_THREAD - 1], tile_aggregate.value); + } + } + + // Process subsequent tile of input (dynamic chained scan). + // Returns the running count of segments + // and aggregated values (including this tile) + + template + THRUST_DEVICE_FUNCTION void + consume_subsequent_tile(Size num_remaining, int tile_idx, Size tile_offset, ScanTileState& tile_state) + { + using core::sync_threadblock; + + key_type keys[ITEMS_PER_THREAD]; // Tile keys + key_type pred_keys[ITEMS_PER_THREAD]; // Tile keys shifted up (predecessor) + value_type values[ITEMS_PER_THREAD]; // Tile values + size_type segment_flags[ITEMS_PER_THREAD]; // Segment head flags + size_type segment_indices[ITEMS_PER_THREAD]; // Segment indices + size_value_pair_t scan_items[ITEMS_PER_THREAD]; // Zipped values and segment flags|indices + key_value_pair_t scatter_items[ITEMS_PER_THREAD]; // Zipped key value pairs for scattering + + // Load keys (last tile repeats final element) + if (IS_LAST_TILE) + { + BlockLoadKeys(storage.load_keys) + .Load(keys_load_it + tile_offset, keys, num_remaining, *(keys_load_it + tile_offset)); + } + else + { + BlockLoadKeys(storage.load_keys).Load(keys_load_it + tile_offset, keys); + } + + key_type tile_pred_key = (threadIdx.x == 0) ? key_type(keys_load_it[tile_offset - 1]) : key_type(); + + sync_threadblock(); + + // Load values (last tile repeats final element) + if (IS_LAST_TILE) + { + BlockLoadValues(storage.load_values) + .Load(values_load_it + tile_offset, values, num_remaining, *(values_load_it + tile_offset)); + } + else + { + BlockLoadValues(storage.load_values).Load(values_load_it + tile_offset, values); + } + + sync_threadblock(); + + // Set head segment_flags + BlockDiscontinuityKeys(storage.scan_storage.discontinuity) + .FlagHeads(segment_flags, keys, pred_keys, inequality_op, tile_pred_key); + + // Zip values and segment_flags + zip_values_and_flags(num_remaining, values, segment_flags, scan_items); + + // Exclusive scan of values and segment_flags + size_value_pair_t tile_aggregate; + TilePrefixCallback prefix_op(tile_state, storage.scan_storage.prefix, scan_op, tile_idx); + scan_tile(scan_items, tile_aggregate, prefix_op, is_true()); + size_value_pair_t tile_inclusive_prefix = prefix_op.GetInclusivePrefix(); + + // Unzip values and segment indices + zip_keys_and_values(pred_keys, segment_indices, scan_items, scatter_items); + + // Scatter flagged items + scatter(scatter_items, segment_flags, segment_indices, tile_aggregate.key, prefix_op.GetExclusivePrefix().key); + + if (IS_LAST_TILE) + { + // Finalize the carry-out from the last tile + finalize_last_tile( + tile_inclusive_prefix.key, num_remaining, keys[ITEMS_PER_THREAD - 1], tile_inclusive_prefix.value); + } + } + template + THRUST_DEVICE_FUNCTION void + consume_tile(size_type num_remaining, int tile_idx, size_type tile_offset, ScanTileState& tile_state) + { + if (tile_idx == 0) + { + consume_first_tile(num_remaining, tile_offset, tile_state); + } + else + { + consume_subsequent_tile(num_remaining, tile_idx, tile_offset, tile_state); + } + } + + //--------------------------------------------------------------------- + // Constructor : consume_range + //--------------------------------------------------------------------- + + THRUST_DEVICE_FUNCTION impl( + TempStorage& storage_, + KeysInputIt keys_input_it_, + ValuesInputIt values_input_it_, + KeysOutputIt keys_output_it_, + ValuesOutputIt values_output_it_, + NumRunsOutputIt num_runs_output_it_, + EqualityOp equality_op_, + ReductionOp reduction_op_, + Size num_items, + int /*num_tiles*/, + ScanTileState& tile_state) + : storage(storage_) + , keys_load_it(core::make_load_iterator(ptx_plan(), keys_input_it_)) + , values_load_it(core::make_load_iterator(ptx_plan(), values_input_it_)) + , keys_output_it(keys_output_it_) + , values_output_it(values_output_it_) + , num_runs_output_it(num_runs_output_it_) + , inequality_op(equality_op_) + , scan_op(reduction_op_) + { + // Blocks are launched in increasing order, + // so just assign one tile per block + // + int tile_idx = blockIdx.x; + Size tile_offset = static_cast(tile_idx) * ITEMS_PER_TILE; + Size num_remaining = num_items - tile_offset; + + if (num_remaining > ITEMS_PER_TILE) + { + // Not the last tile (full) + consume_tile(num_remaining, tile_idx, tile_offset, tile_state); + } + else if (num_remaining > 0) + { + // The last tile (possibly partially-full) + consume_tile(num_remaining, tile_idx, tile_offset, tile_state); + } + } + }; // struct impl + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY( + KeysInputIt keys_input_it, + ValuesInputIt values_input_it, + KeysOutputIt keys_output_it, + ValuesOutputIt values_output_it, + NumRunsOutputIt num_runs_output_it, + ScanTileState tile_state, + EqualityOp equality_op, + ReductionOp reduction_op, + Size num_items, + int num_tiles, + char* shmem) + { + TempStorage& storage = *reinterpret_cast(shmem); + + impl(storage, + keys_input_it, + values_input_it, + keys_output_it, + values_output_it, + num_runs_output_it, + equality_op, + reduction_op, + num_items, + num_tiles, + tile_state); + } + +}; // struct ReduceByKeyAgent + +template +struct InitAgent +{ + template + struct PtxPlan : PtxPolicy<128> + {}; + using ptx_plan = core::specialize_plan; + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY(ScanTileState tile_state, Size num_tiles, NumSelectedIt num_selected_out, char* /*shmem*/) + { + tile_state.InitializeStatus(num_tiles); + if (blockIdx.x == 0 && threadIdx.x == 0) + { + *num_selected_out = 0; + } + } +}; // struct InitAgent + +template +THRUST_RUNTIME_FUNCTION cudaError_t doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + KeysInputIt keys_input_it, + ValuesInputIt values_input_it, + KeysOutputIt keys_output_it, + ValuesOutputIt values_output_it, + NumRunsOutputIt num_runs_output_it, + EqualityOp equality_op, + ReductionOp reduction_op, + Size num_items, + cudaStream_t stream) +{ + using core::AgentLauncher; + using core::AgentPlan; + + cudaError_t status = cudaSuccess; + if (num_items == 0) + { + return cudaErrorNotSupported; + } + + using reduce_by_key_agent = AgentLauncher< + ReduceByKeyAgent>; + + using ScanTileState = typename reduce_by_key_agent::ScanTileState; + using init_agent = AgentLauncher>; + + AgentPlan reduce_by_key_plan = reduce_by_key_agent::get_plan(stream); + AgentPlan init_plan = init_agent::get_plan(); + + // Number of input tiles + int tile_size = reduce_by_key_plan.items_per_tile; + Size num_tiles = ::cuda::ceil_div(num_items, tile_size); + + size_t vshmem_size = core::vshmem_size(reduce_by_key_plan.shared_memory_size, num_tiles); + + size_t allocation_sizes[2] = {9, vshmem_size}; + status = ScanTileState::AllocationSize(static_cast(num_tiles), allocation_sizes[0]); + CUDA_CUB_RET_IF_FAIL(status); + + void* allocations[2] = {nullptr, nullptr}; + status = cub::AliasTemporaries(d_temp_storage, temp_storage_bytes, allocations, allocation_sizes); + CUDA_CUB_RET_IF_FAIL(status); + + if (d_temp_storage == nullptr) + { + return status; + } + + ScanTileState tile_state; + status = tile_state.Init(static_cast(num_tiles), allocations[0], allocation_sizes[0]); + CUDA_CUB_RET_IF_FAIL(status); + + init_agent ia(init_plan, num_tiles, stream, "reduce_by_key::init_agent"); + ia.launch(tile_state, num_tiles, num_runs_output_it); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + + char* vshmem_ptr = vshmem_size > 0 ? (char*) allocations[1] : nullptr; + + reduce_by_key_agent rbka(reduce_by_key_plan, num_items, stream, vshmem_ptr, "reduce_by_keys::reduce_by_key_agent"); + rbka.launch( + keys_input_it, + values_input_it, + keys_output_it, + values_output_it, + num_runs_output_it, + tile_state, + equality_op, + reduction_op, + num_items, + num_tiles); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + return status; +} + +template +THRUST_RUNTIME_FUNCTION pair reduce_by_key_dispatch( + execution_policy& policy, + KeysInputIt keys_first, + Size num_items, + ValuesInputIt values_first, + KeysOutputIt keys_output, + ValuesOutputIt values_output, + EqualityOp equality_op, + ReductionOp reduction_op) +{ + size_t temp_storage_bytes = 0; + cudaStream_t stream = cuda_cub::stream(policy); + + if (num_items == 0) + { + return thrust::make_pair(keys_output, values_output); + } + + cudaError_t status; + status = doit_step( + nullptr, + temp_storage_bytes, + keys_first, + values_first, + keys_output, + values_output, + static_cast(nullptr), + equality_op, + reduction_op, + num_items, + stream); + cuda_cub::throw_on_error(status, "reduce_by_key failed on 1st step"); + + size_t allocation_sizes[2] = {sizeof(Size), temp_storage_bytes}; + void* allocations[2] = {nullptr, nullptr}; + + size_t storage_size = 0; + status = core::alias_storage(nullptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "reduce failed on 1st alias_storage"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, storage_size); + void* ptr = static_cast(tmp.data().get()); + + status = core::alias_storage(ptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "reduce failed on 2nd alias_storage"); + + Size* d_num_runs_out = thrust::detail::aligned_reinterpret_cast(allocations[0]); + + status = doit_step( + allocations[1], + temp_storage_bytes, + keys_first, + values_first, + keys_output, + values_output, + d_num_runs_out, + equality_op, + reduction_op, + num_items, + stream); + cuda_cub::throw_on_error(status, "reduce_by_key failed on 2nd step"); + + status = cuda_cub::synchronize(policy); + cuda_cub::throw_on_error(status, "reduce_by_key: failed to synchronize"); + + const auto num_runs_out = cuda_cub::get_value(policy, d_num_runs_out); + + return thrust::make_pair(keys_output + num_runs_out, values_output + num_runs_out); +} + +template +THRUST_RUNTIME_FUNCTION pair reduce_by_key( + execution_policy& policy, + KeysInputIt keys_first, + KeysInputIt keys_last, + ValuesInputIt values_first, + KeysOutputIt keys_output, + ValuesOutputIt values_output, + EqualityOp equality_op, + ReductionOp reduction_op) +{ + using size_type = typename iterator_traits::difference_type; + + size_type num_items = thrust::distance(keys_first, keys_last); + + pair result = thrust::make_pair(keys_output, values_output); + + if (num_items == 0) + { + return result; + } + + THRUST_INDEX_TYPE_DISPATCH( + result, + reduce_by_key_dispatch, + num_items, + (policy, keys_first, num_items_fixed, values_first, keys_output, values_output, equality_op, reduction_op)); + + return result; +} + +} // namespace __reduce_by_key + +//------------------------- +// Thrust API entry points +//------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +pair _CCCL_HOST_DEVICE reduce_by_key( + execution_policy& policy, + KeyInputIt keys_first, + KeyInputIt keys_last, + ValInputIt values_first, + KeyOutputIt keys_output, + ValOutputIt values_output, + BinaryPred binary_pred, + BinaryOp binary_op) +{ + auto ret = thrust::make_pair(keys_output, values_output); + THRUST_CDP_DISPATCH( + (ret = __reduce_by_key::reduce_by_key( + policy, keys_first, keys_last, values_first, keys_output, values_output, binary_pred, binary_op);), + (ret = thrust::reduce_by_key( + cvt_to_seq(derived_cast(policy)), + keys_first, + keys_last, + values_first, + keys_output, + values_output, + binary_pred, + binary_op);)); + return ret; +} + +template +pair _CCCL_HOST_DEVICE reduce_by_key( + execution_policy& policy, + KeyInputIt keys_first, + KeyInputIt keys_last, + ValInputIt values_first, + KeyOutputIt keys_output, + ValOutputIt values_output, + BinaryPred binary_pred) +{ + using value_type = typename thrust::detail::eval_if::value, + thrust::iterator_value, + thrust::iterator_value>::type; + return cuda_cub::reduce_by_key( + policy, keys_first, keys_last, values_first, keys_output, values_output, binary_pred, plus()); +} + +template +pair _CCCL_HOST_DEVICE reduce_by_key( + execution_policy& policy, + KeyInputIt keys_first, + KeyInputIt keys_last, + ValInputIt values_first, + KeyOutputIt keys_output, + ValOutputIt values_output) +{ + using KeyT = typename thrust::iterator_value::type; + return cuda_cub::reduce_by_key( + policy, keys_first, keys_last, values_first, keys_output, values_output, equal_to()); +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END + +# include +# include + +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/remove.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/remove.h new file mode 100644 index 0000000000000000000000000000000000000000..993408019419377ec4b230441640a72313079035 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/remove.h @@ -0,0 +1,107 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +// in-place + +_CCCL_EXEC_CHECK_DISABLE +template +InputIt _CCCL_HOST_DEVICE +remove_if(execution_policy& policy, InputIt first, InputIt last, StencilIt stencil, Predicate predicate) +{ + THRUST_CDP_DISPATCH((return cuda_cub::detail::copy_if( + policy, first, last, stencil, first, thrust::not_fn(predicate));), + (return thrust::remove_if(cvt_to_seq(derived_cast(policy)), first, last, stencil, predicate);)); +} + +_CCCL_EXEC_CHECK_DISABLE +template +InputIt _CCCL_HOST_DEVICE remove_if(execution_policy& policy, InputIt first, InputIt last, Predicate predicate) +{ + THRUST_CDP_DISPATCH( + (return cuda_cub::detail::copy_if( + policy, first, last, static_cast(nullptr), first, thrust::not_fn(predicate));), + (return thrust::remove_if(cvt_to_seq(derived_cast(policy)), first, last, predicate);)); +} + +template +InputIt _CCCL_HOST_DEVICE remove(execution_policy& policy, InputIt first, InputIt last, const T& value) +{ + using thrust::placeholders::_1; + + return cuda_cub::remove_if(policy, first, last, _1 == value); +} + +// copy + +template +OutputIt _CCCL_HOST_DEVICE remove_copy_if( + execution_policy& policy, + InputIt first, + InputIt last, + StencilIt stencil, + OutputIt result, + Predicate predicate) +{ + return cuda_cub::copy_if(policy, first, last, stencil, result, thrust::not_fn(predicate)); +} + +template +OutputIt _CCCL_HOST_DEVICE +remove_copy_if(execution_policy& policy, InputIt first, InputIt last, OutputIt result, Predicate predicate) +{ + return cuda_cub::copy_if(policy, first, last, result, thrust::not_fn(predicate)); +} + +template +OutputIt _CCCL_HOST_DEVICE +remove_copy(execution_policy& policy, InputIt first, InputIt last, OutputIt result, const T& value) +{ + thrust::detail::equal_to_value pred(value); + return cuda_cub::remove_copy_if(policy, first, last, result, pred); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reverse.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reverse.h new file mode 100644 index 0000000000000000000000000000000000000000..57cc16852fd4fd2785d43603619d6e64e0cecbc2 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/reverse.h @@ -0,0 +1,86 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +ResultIt _CCCL_HOST_DEVICE reverse_copy(execution_policy& policy, ItemsIt first, ItemsIt last, ResultIt result); + +template +void _CCCL_HOST_DEVICE reverse(execution_policy& policy, ItemsIt first, ItemsIt last); + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +# include +# include +# include +# include +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +template +ResultIt _CCCL_HOST_DEVICE reverse_copy(execution_policy& policy, ItemsIt first, ItemsIt last, ResultIt result) +{ + return cuda_cub::copy(policy, thrust::make_reverse_iterator(last), thrust::make_reverse_iterator(first), result); +} + +template +void _CCCL_HOST_DEVICE reverse(execution_policy& policy, ItemsIt first, ItemsIt last) +{ + using difference_type = typename thrust::iterator_difference::type; + + // find the midpoint of [first,last) + difference_type N = thrust::distance(first, last); + ItemsIt mid(first); + thrust::advance(mid, N / 2); + + cuda_cub::swap_ranges(policy, first, mid, thrust::make_reverse_iterator(last)); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/scan_by_key.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/scan_by_key.h new file mode 100644 index 0000000000000000000000000000000000000000..3ffc7898dcbb4447bb0991fd8a97d1449933c25e --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/scan_by_key.h @@ -0,0 +1,407 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ +namespace detail +{ + +_CCCL_EXEC_CHECK_DISABLE +template +_CCCL_HOST_DEVICE ValuesOutIt inclusive_scan_by_key_n( + thrust::cuda_cub::execution_policy& policy, + KeysInIt keys, + ValuesInIt values, + ValuesOutIt result, + SizeT num_items, + EqualityOpT equality_op, + ScanOpT scan_op) +{ + if (num_items == 0) + { + return result; + } + + // Convert to raw pointers if possible: + using KeysInUnwrapIt = thrust::try_unwrap_contiguous_iterator_t; + using ValuesInUnwrapIt = thrust::try_unwrap_contiguous_iterator_t; + using ValuesOutUnwrapIt = thrust::try_unwrap_contiguous_iterator_t; + using AccumT = typename thrust::iterator_traits::value_type; + + auto keys_unwrap = thrust::try_unwrap_contiguous_iterator(keys); + auto values_unwrap = thrust::try_unwrap_contiguous_iterator(values); + auto result_unwrap = thrust::try_unwrap_contiguous_iterator(result); + + using Dispatch32 = cub::DispatchScanByKey< + KeysInUnwrapIt, + ValuesInUnwrapIt, + ValuesOutUnwrapIt, + EqualityOpT, + ScanOpT, + cub::NullType, + std::uint32_t, + AccumT>; + using Dispatch64 = cub::DispatchScanByKey< + KeysInUnwrapIt, + ValuesInUnwrapIt, + ValuesOutUnwrapIt, + EqualityOpT, + ScanOpT, + cub::NullType, + std::uint64_t, + AccumT>; + + cudaStream_t stream = thrust::cuda_cub::stream(policy); + cudaError_t status{}; + + // Determine temporary storage requirements: + std::size_t tmp_size = 0; + { + THRUST_UNSIGNED_INDEX_TYPE_DISPATCH2( + status, + Dispatch32::Dispatch, + Dispatch64::Dispatch, + num_items, + (nullptr, + tmp_size, + keys_unwrap, + values_unwrap, + result_unwrap, + equality_op, + scan_op, + cub::NullType{}, + num_items_fixed, + stream)); + thrust::cuda_cub::throw_on_error( + status, + "after determining tmp storage " + "requirements for inclusive_scan_by_key"); + } + + // Run scan: + { + // Allocate temporary storage: + thrust::detail::temporary_array tmp{policy, tmp_size}; + + THRUST_UNSIGNED_INDEX_TYPE_DISPATCH2( + status, + Dispatch32::Dispatch, + Dispatch64::Dispatch, + num_items, + (tmp.data().get(), + tmp_size, + keys_unwrap, + values_unwrap, + result_unwrap, + equality_op, + scan_op, + cub::NullType{}, + num_items_fixed, + stream)); + + thrust::cuda_cub::throw_on_error(status, "after dispatching inclusive_scan_by_key kernel"); + + thrust::cuda_cub::throw_on_error( + thrust::cuda_cub::synchronize_optional(policy), "inclusive_scan_by_key failed to synchronize"); + } + + return result + num_items; +} + +_CCCL_EXEC_CHECK_DISABLE +template +_CCCL_HOST_DEVICE ValuesOutIt exclusive_scan_by_key_n( + thrust::cuda_cub::execution_policy& policy, + KeysInIt keys, + ValuesInIt values, + ValuesOutIt result, + SizeT num_items, + InitValueT init_value, + EqualityOpT equality_op, + ScanOpT scan_op) +{ + if (num_items == 0) + { + return result; + } + + // Convert to raw pointers if possible: + using KeysInUnwrapIt = thrust::try_unwrap_contiguous_iterator_t; + using ValuesInUnwrapIt = thrust::try_unwrap_contiguous_iterator_t; + using ValuesOutUnwrapIt = thrust::try_unwrap_contiguous_iterator_t; + + auto keys_unwrap = thrust::try_unwrap_contiguous_iterator(keys); + auto values_unwrap = thrust::try_unwrap_contiguous_iterator(values); + auto result_unwrap = thrust::try_unwrap_contiguous_iterator(result); + + using Dispatch32 = cub::DispatchScanByKey< + KeysInUnwrapIt, + ValuesInUnwrapIt, + ValuesOutUnwrapIt, + EqualityOpT, + ScanOpT, + InitValueT, + std::uint32_t, + InitValueT>; + using Dispatch64 = cub::DispatchScanByKey< + KeysInUnwrapIt, + ValuesInUnwrapIt, + ValuesOutUnwrapIt, + EqualityOpT, + ScanOpT, + InitValueT, + std::uint64_t, + InitValueT>; + + cudaStream_t stream = thrust::cuda_cub::stream(policy); + cudaError_t status{}; + + // Determine temporary storage requirements: + std::size_t tmp_size = 0; + { + THRUST_UNSIGNED_INDEX_TYPE_DISPATCH2( + status, + Dispatch32::Dispatch, + Dispatch64::Dispatch, + num_items, + (nullptr, + tmp_size, + keys_unwrap, + values_unwrap, + result_unwrap, + equality_op, + scan_op, + init_value, + num_items_fixed, + stream)); + thrust::cuda_cub::throw_on_error( + status, + "after determining tmp storage " + "requirements for exclusive_scan_by_key"); + } + + // Run scan: + { + // Allocate temporary storage: + thrust::detail::temporary_array tmp{policy, tmp_size}; + + THRUST_UNSIGNED_INDEX_TYPE_DISPATCH2( + status, + Dispatch32::Dispatch, + Dispatch64::Dispatch, + num_items, + (tmp.data().get(), + tmp_size, + keys_unwrap, + values_unwrap, + result_unwrap, + equality_op, + scan_op, + init_value, + num_items_fixed, + stream)); + + thrust::cuda_cub::throw_on_error(status, "after dispatching exclusive_scan_by_key kernel"); + + thrust::cuda_cub::throw_on_error( + thrust::cuda_cub::synchronize_optional(policy), "exclusive_scan_by_key failed to synchronize"); + } + + return result + num_items; +} + +} // namespace detail + +//------------------------- +// Thrust API entry points +//------------------------- + +//--------------------------- +// Inclusive scan +//--------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +ValOutputIt _CCCL_HOST_DEVICE inclusive_scan_by_key( + execution_policy& policy, + KeyInputIt key_first, + KeyInputIt key_last, + ValInputIt value_first, + ValOutputIt value_result, + BinaryPred binary_pred, + ScanOp scan_op) +{ + ValOutputIt ret = value_result; + THRUST_CDP_DISPATCH( + (ret = thrust::cuda_cub::detail::inclusive_scan_by_key_n( + policy, key_first, value_first, value_result, thrust::distance(key_first, key_last), binary_pred, scan_op);), + (ret = thrust::inclusive_scan_by_key( + cvt_to_seq(derived_cast(policy)), key_first, key_last, value_first, value_result, binary_pred, scan_op);)); + + return ret; +} + +template +ValOutputIt _CCCL_HOST_DEVICE inclusive_scan_by_key( + execution_policy& policy, + KeyInputIt key_first, + KeyInputIt key_last, + ValInputIt value_first, + ValOutputIt value_result, + BinaryPred binary_pred) +{ + return cuda_cub::inclusive_scan_by_key( + policy, key_first, key_last, value_first, value_result, binary_pred, thrust::plus<>()); +} + +template +ValOutputIt _CCCL_HOST_DEVICE inclusive_scan_by_key( + execution_policy& policy, + KeyInputIt key_first, + KeyInputIt key_last, + ValInputIt value_first, + ValOutputIt value_result) +{ + return cuda_cub::inclusive_scan_by_key(policy, key_first, key_last, value_first, value_result, thrust::equal_to<>()); +} + +//--------------------------- +// Exclusive scan +//--------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +ValOutputIt _CCCL_HOST_DEVICE exclusive_scan_by_key( + execution_policy& policy, + KeyInputIt key_first, + KeyInputIt key_last, + ValInputIt value_first, + ValOutputIt value_result, + Init init, + BinaryPred binary_pred, + ScanOp scan_op) +{ + ValOutputIt ret = value_result; + THRUST_CDP_DISPATCH( + (ret = thrust::cuda_cub::detail::exclusive_scan_by_key_n( + policy, key_first, value_first, value_result, thrust::distance(key_first, key_last), init, binary_pred, scan_op);), + (ret = thrust::exclusive_scan_by_key( + cvt_to_seq(derived_cast(policy)), key_first, key_last, value_first, value_result, init, binary_pred, scan_op);)); + return ret; +} + +template +ValOutputIt _CCCL_HOST_DEVICE exclusive_scan_by_key( + execution_policy& policy, + KeyInputIt key_first, + KeyInputIt key_last, + ValInputIt value_first, + ValOutputIt value_result, + Init init, + BinaryPred binary_pred) +{ + return cuda_cub::exclusive_scan_by_key( + policy, key_first, key_last, value_first, value_result, init, binary_pred, thrust::plus<>()); +} + +template +ValOutputIt _CCCL_HOST_DEVICE exclusive_scan_by_key( + execution_policy& policy, + KeyInputIt key_first, + KeyInputIt key_last, + ValInputIt value_first, + ValOutputIt value_result, + Init init) +{ + return cuda_cub::exclusive_scan_by_key( + policy, key_first, key_last, value_first, value_result, init, thrust::equal_to<>()); +} + +template +ValOutputIt _CCCL_HOST_DEVICE exclusive_scan_by_key( + execution_policy& policy, + KeyInputIt key_first, + KeyInputIt key_last, + ValInputIt value_first, + ValOutputIt value_result) +{ + using value_type = typename thrust::iterator_traits::value_type; + return cuda_cub::exclusive_scan_by_key(policy, key_first, key_last, value_first, value_result, value_type{}); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +# include + +#endif // NVCC diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/sequence.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/sequence.h new file mode 100644 index 0000000000000000000000000000000000000000..0968d886dc52f20a71c2cdb4d68a8a6d524dd053 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/sequence.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2013 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special version of this algorithm diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/set_operations.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/set_operations.h new file mode 100644 index 0000000000000000000000000000000000000000..5f0943774215e8b64946372633b4171748f86e70 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/set_operations.h @@ -0,0 +1,1756 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +namespace __set_operations +{ + +template +THRUST_DEVICE_FUNCTION void binary_search_iteration(It data, Size& begin, Size& end, T key, int shift, Comp comp) +{ + IntT scale = (1 << shift) - 1; + Size mid = (begin + scale * end) >> shift; + + T key2 = data[mid]; + bool pred = UpperBound ? !comp(key, key2) : comp(key2, key); + if (pred) + { + begin = mid + 1; + } + else + { + end = mid; + } +} + +template +THRUST_DEVICE_FUNCTION Size binary_search(It data, Size count, T key, Comp comp) +{ + Size begin = 0; + Size end = count; + while (begin < end) + { + binary_search_iteration(data, begin, end, key, 1, comp); + } + return begin; +} + +template +THRUST_DEVICE_FUNCTION Size biased_binary_search(It data, Size count, T key, IntT levels, Comp comp) +{ + Size begin = 0; + Size end = count; + + if (levels >= 4 && begin < end) + { + binary_search_iteration(data, begin, end, key, 9, comp); + } + if (levels >= 3 && begin < end) + { + binary_search_iteration(data, begin, end, key, 7, comp); + } + if (levels >= 2 && begin < end) + { + binary_search_iteration(data, begin, end, key, 5, comp); + } + if (levels >= 1 && begin < end) + { + binary_search_iteration(data, begin, end, key, 4, comp); + } + + while (begin < end) + { + binary_search_iteration(data, begin, end, key, 1, comp); + } + return begin; +} + +template +THRUST_DEVICE_FUNCTION Size merge_path(It1 a, Size aCount, It2 b, Size bCount, Size diag, Comp comp) +{ + using T = typename thrust::iterator_traits::value_type; + + Size begin = thrust::max(0, diag - bCount); + Size end = thrust::min(diag, aCount); + + while (begin < end) + { + Size mid = (begin + end) >> 1; + T aKey = a[mid]; + T bKey = b[diag - 1 - mid]; + bool pred = UpperBound ? comp(aKey, bKey) : !comp(bKey, aKey); + if (pred) + { + begin = mid + 1; + } + else + { + end = mid; + } + } + return begin; +} + +template +THRUST_DEVICE_FUNCTION pair +balanced_path(It1 keys1, It2 keys2, Size num_keys1, Size num_keys2, Size diag, Size2 levels, CompareOp compare_op) +{ + using T = typename iterator_traits::value_type; + + Size index1 = merge_path(keys1, num_keys1, keys2, num_keys2, diag, compare_op); + Size index2 = diag - index1; + + bool star = false; + if (index2 < num_keys2) + { + T x = keys2[index2]; + + // Search for the beginning of the duplicate run in both A and B. + Size start1 = biased_binary_search(keys1, index1, x, levels, compare_op); + Size start2 = biased_binary_search(keys2, index2, x, levels, compare_op); + + // The distance between x's merge path and its lower_bound is its rank. + // We add up the a and b ranks and evenly distribute them to + // get a stairstep path. + Size run1 = index1 - start1; + Size run2 = index2 - start2; + Size total_run = run1 + run2; + + // Attempt to advance b and regress a. + Size advance2 = max(total_run >> 1, total_run - run1); + Size end2 = min(num_keys2, start2 + advance2 + 1); + + Size run_end2 = index2 + binary_search(keys2 + index2, end2 - index2, x, compare_op); + run2 = run_end2 - start2; + + advance2 = min(advance2, run2); + Size advance1 = total_run - advance2; + + bool round_up = (advance1 == advance2 + 1) && (advance2 < run2); + if (round_up) + { + star = true; + } + + index1 = start1 + advance1; + } + return thrust::make_pair(index1, (diag - index1) + star); +} // func balanced_path + +template +struct PtxPolicy +{ + enum + { + BLOCK_THREADS = _BLOCK_THREADS, + ITEMS_PER_THREAD = _ITEMS_PER_THREAD, + ITEMS_PER_TILE = _BLOCK_THREADS * _ITEMS_PER_THREAD - 1 + }; + + static const cub::BlockLoadAlgorithm LOAD_ALGORITHM = _LOAD_ALGORITHM; + static const cub::CacheLoadModifier LOAD_MODIFIER = _LOAD_MODIFIER; + static const cub::BlockScanAlgorithm SCAN_ALGORITHM = _SCAN_ALGORITHM; +}; // PtxPolicy + +template +struct Tuning; + +namespace mpl = thrust::detail::mpl::math; + +template +struct Tuning +{ + enum + { + MAX_INPUT_BYTES = mpl::max::value, + COMBINED_INPUT_BYTES = sizeof(T), // + sizeof(Value), + NOMINAL_4B_ITEMS_PER_THREAD = 7, + ITEMS_PER_THREAD = + mpl::min(((NOMINAL_4B_ITEMS_PER_THREAD * 4) + COMBINED_INPUT_BYTES - 1) + / COMBINED_INPUT_BYTES)>::value>::value, + }; + + using type = + PtxPolicy<128, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_DEFAULT, cub::BLOCK_SCAN_WARP_SCANS>; +}; // tuning sm30 + +template +struct Tuning +{ + enum + { + MAX_INPUT_BYTES = mpl::max::value, + COMBINED_INPUT_BYTES = sizeof(T), // + sizeof(U), + NOMINAL_4B_ITEMS_PER_THREAD = 15, + ITEMS_PER_THREAD = + mpl::min(((NOMINAL_4B_ITEMS_PER_THREAD * 4) + COMBINED_INPUT_BYTES - 1) + / COMBINED_INPUT_BYTES)>::value>::value, + }; + + using type = + PtxPolicy<256, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_DEFAULT, cub::BLOCK_SCAN_WARP_SCANS>; +}; // tuning sm52 + +template +struct Tuning +{ + enum + { + MAX_INPUT_BYTES = mpl::max::value, + COMBINED_INPUT_BYTES = sizeof(T), // + sizeof(U), + NOMINAL_4B_ITEMS_PER_THREAD = 19, + ITEMS_PER_THREAD = + mpl::min(((NOMINAL_4B_ITEMS_PER_THREAD * 4) + COMBINED_INPUT_BYTES - 1) + / COMBINED_INPUT_BYTES)>::value>::value, + }; + + using type = + PtxPolicy<512, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_DEFAULT, cub::BLOCK_SCAN_WARP_SCANS>; +}; // tuning sm60 + +template +struct SetOpAgent +{ + using key1_type = typename iterator_traits::value_type; + using key2_type = typename iterator_traits::value_type; + using value1_type = typename iterator_traits::value_type; + using value2_type = typename iterator_traits::value_type; + + using key_type = key1_type; + using value_type = value1_type; + + using ScanTileState = cub::ScanTileState; + + template + struct PtxPlan : Tuning::type + { + using tuning = Tuning; + + using KeysLoadIt1 = typename core::LoadIterator::type; + using KeysLoadIt2 = typename core::LoadIterator::type; + using ValuesLoadIt1 = typename core::LoadIterator::type; + using ValuesLoadIt2 = typename core::LoadIterator::type; + + using BlockLoadKeys1 = typename core::BlockLoad::type; + using BlockLoadKeys2 = typename core::BlockLoad::type; + using BlockLoadValues1 = typename core::BlockLoad::type; + using BlockLoadValues2 = typename core::BlockLoad::type; + + using TilePrefixCallback = cub::TilePrefixCallbackOp, ScanTileState, Arch::ver>; + + using BlockScan = cub::BlockScan; + + // gather required temporary storage in a union + // + union TempStorage + { + struct ScanStorage + { + typename BlockScan::TempStorage scan; + typename TilePrefixCallback::TempStorage prefix; + } scan_storage; + + struct LoadStorage + { + core::uninitialized_array offset; + union + { + // FIXME These don't appear to be used anywhere? + typename BlockLoadKeys1::TempStorage load_keys1; + typename BlockLoadKeys2::TempStorage load_keys2; + typename BlockLoadValues1::TempStorage load_values1; + typename BlockLoadValues2::TempStorage load_values2; + + // Allocate extra shmem than truly necessary + // This will permit to avoid range checks in + // serial set operations, e.g. serial_set_difference + core::uninitialized_array keys_shared; + + core::uninitialized_array values_shared; + }; // anon union + } load_storage; // struct LoadStorage + }; // union TempStorage + }; // struct PtxPlan + + using ptx_plan = typename core::specialize_plan_msvc10_war::type::type; + + using KeysLoadIt1 = typename ptx_plan::KeysLoadIt1; + using KeysLoadIt2 = typename ptx_plan::KeysLoadIt2; + using ValuesLoadIt1 = typename ptx_plan::ValuesLoadIt1; + using ValuesLoadIt2 = typename ptx_plan::ValuesLoadIt2; + + using BlockLoadKeys1 = typename ptx_plan::BlockLoadKeys1; + using BlockLoadKeys2 = typename ptx_plan::BlockLoadKeys2; + using BlockLoadValues1 = typename ptx_plan::BlockLoadValues1; + using BlockLoadValues2 = typename ptx_plan::BlockLoadValues2; + + using TilePrefixCallback = typename ptx_plan::TilePrefixCallback; + using BlockScan = typename ptx_plan::BlockScan; + + using TempStorage = typename ptx_plan::TempStorage; + + enum + { + ITEMS_PER_THREAD = ptx_plan::ITEMS_PER_THREAD, + BLOCK_THREADS = ptx_plan::BLOCK_THREADS, + }; + + struct impl + { + //--------------------------------------------------------------------- + // Per-thread fields + //--------------------------------------------------------------------- + + TempStorage& storage; + ScanTileState& tile_state; + KeysLoadIt1 keys1_in; + KeysLoadIt2 keys2_in; + ValuesLoadIt1 values1_in; + ValuesLoadIt2 values2_in; + Size keys1_count; + Size keys2_count; + KeysOutputIt keys_out; + ValuesOutputIt values_out; + CompareOp compare_op; + SetOp set_op; + pair* partitions; + std::size_t* output_count; + + //--------------------------------------------------------------------- + // Utility functions + //--------------------------------------------------------------------- + + template + THRUST_DEVICE_FUNCTION void + gmem_to_reg(T (&output)[ITEMS_PER_THREAD], It1 input1, It2 input2, int count1, int count2) + { + if (IS_FULL_TILE) + { +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD - 1; ++ITEM) + { + int idx = BLOCK_THREADS * ITEM + threadIdx.x; + output[ITEM] = (idx < count1) ? static_cast(input1[idx]) : static_cast(input2[idx - count1]); + } + + // last ITEM might be a conditional load even for full tiles + // please check first before attempting to load. + int ITEM = ITEMS_PER_THREAD - 1; + int idx = BLOCK_THREADS * ITEM + threadIdx.x; + if (idx < count1 + count2) + { + output[ITEM] = (idx < count1) ? static_cast(input1[idx]) : static_cast(input2[idx - count1]); + } + } + else + { +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + int idx = BLOCK_THREADS * ITEM + threadIdx.x; + if (idx < count1 + count2) + { + output[ITEM] = (idx < count1) ? static_cast(input1[idx]) : static_cast(input2[idx - count1]); + } + } + } + } + + template + THRUST_DEVICE_FUNCTION void reg_to_shared(It output, T (&input)[ITEMS_PER_THREAD]) + { +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + int idx = BLOCK_THREADS * ITEM + threadIdx.x; + output[idx] = input[ITEM]; + } + } + + template + void THRUST_DEVICE_FUNCTION scatter( + OutputIt output, + T (&input)[ITEMS_PER_THREAD], + SharedIt shared, + int active_mask, + Size thread_output_prefix, + Size tile_output_prefix, + int tile_output_count) + { + using core::sync_threadblock; + + int local_scatter_idx = thread_output_prefix - tile_output_prefix; +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + if (active_mask & (1 << ITEM)) + { + shared[local_scatter_idx++] = input[ITEM]; + } + } + sync_threadblock(); + + for (int item = threadIdx.x; item < tile_output_count; item += BLOCK_THREADS) + { + output[tile_output_prefix + item] = shared[item]; + } + } + + int THRUST_DEVICE_FUNCTION serial_set_op( + key_type* keys, + int keys1_beg, + int keys2_beg, + int keys1_count, + int keys2_count, + key_type (&output)[ITEMS_PER_THREAD], + int (&indices)[ITEMS_PER_THREAD], + CompareOp compare_op, + SetOp set_op) + { + int active_mask = set_op(keys, keys1_beg, keys2_beg, keys1_count, keys2_count, output, indices, compare_op); + + return active_mask; + } + + //--------------------------------------------------------------------- + // Tile operations + //--------------------------------------------------------------------- + + template + void THRUST_DEVICE_FUNCTION consume_tile(Size tile_idx) + { + using core::sync_threadblock; + using core::uninitialized_array; + + pair partition_beg = partitions[tile_idx + 0]; + pair partition_end = partitions[tile_idx + 1]; + + Size keys1_beg = partition_beg.first; + Size keys1_end = partition_end.first; + Size keys2_beg = partition_beg.second; + Size keys2_end = partition_end.second; + + // number of keys per tile + // + int num_keys1 = static_cast(keys1_end - keys1_beg); + int num_keys2 = static_cast(keys2_end - keys2_beg); + + // load keys into shared memory for further processing + key_type keys_loc[ITEMS_PER_THREAD]; + + gmem_to_reg(keys_loc, keys1_in + keys1_beg, keys2_in + keys2_beg, num_keys1, num_keys2); + + reg_to_shared(&storage.load_storage.keys_shared[0], keys_loc); + + sync_threadblock(); + + int diag_loc = min(ITEMS_PER_THREAD * threadIdx.x, num_keys1 + num_keys2); + + pair partition_loc = balanced_path( + &storage.load_storage.keys_shared[0], + &storage.load_storage.keys_shared[num_keys1], + num_keys1, + num_keys2, + diag_loc, + 4, + compare_op); + + int keys1_beg_loc = partition_loc.first; + int keys2_beg_loc = partition_loc.second; + + // compute difference between next and current thread + // to obtain number of elements per thread + int value = threadIdx.x == 0 ? (num_keys1 << 16) | num_keys2 : (partition_loc.first << 16) | partition_loc.second; + + int dst = threadIdx.x == 0 ? BLOCK_THREADS - 1 : threadIdx.x - 1; + storage.load_storage.offset[dst] = value; + + core::sync_threadblock(); + + pair partition1_loc = thrust::make_pair( + storage.load_storage.offset[threadIdx.x] >> 16, storage.load_storage.offset[threadIdx.x] & 0xFFFF); + + int keys1_end_loc = partition1_loc.first; + int keys2_end_loc = partition1_loc.second; + + int num_keys1_loc = keys1_end_loc - keys1_beg_loc; + int num_keys2_loc = keys2_end_loc - keys2_beg_loc; + + // perform serial set operation + // + int indices[ITEMS_PER_THREAD]; + + int active_mask = serial_set_op( + &storage.load_storage.keys_shared[0], + keys1_beg_loc, + keys2_beg_loc + num_keys1, + num_keys1_loc, + num_keys2_loc, + keys_loc, + indices, + compare_op, + set_op); + sync_threadblock(); +# if 0 + if (ITEMS_PER_THREAD*threadIdx.x >= num_keys1 + num_keys2) + active_mask = 0; +# endif + + // look-back scan over thread_output_count + // to compute global thread_output_base and tile_otput_count; + Size tile_output_count = 0; + Size thread_output_prefix = 0; + Size tile_output_prefix = 0; + Size thread_output_count = static_cast(__popc(active_mask)); + + if (tile_idx == 0) // first tile + { + BlockScan(storage.scan_storage.scan).ExclusiveSum(thread_output_count, thread_output_prefix, tile_output_count); + if (threadIdx.x == 0) + { + // Update tile status if this is not the last tile + if (!IS_LAST_TILE) + { + tile_state.SetInclusive(0, tile_output_count); + } + } + } + else + { + TilePrefixCallback prefix_cb(tile_state, storage.scan_storage.prefix, ::cuda::std::plus<>{}, tile_idx); + + BlockScan(storage.scan_storage.scan).ExclusiveSum(thread_output_count, thread_output_prefix, prefix_cb); + tile_output_count = prefix_cb.GetBlockAggregate(); + tile_output_prefix = prefix_cb.GetExclusivePrefix(); + } + + sync_threadblock(); + + // scatter results + // + scatter(keys_out, + keys_loc, + &storage.load_storage.keys_shared[0], + active_mask, + thread_output_prefix, + tile_output_prefix, + tile_output_count); + + if (HAS_VALUES::value) + { + value_type values_loc[ITEMS_PER_THREAD]; + gmem_to_reg(values_loc, values1_in + keys1_beg, values2_in + keys2_beg, num_keys1, num_keys2); + + sync_threadblock(); + + reg_to_shared(&storage.load_storage.values_shared[0], values_loc); + + sync_threadblock(); + + // gather items from shared mem + // +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + if (active_mask & (1 << ITEM)) + { + values_loc[ITEM] = storage.load_storage.values_shared[indices[ITEM]]; + } + } + + sync_threadblock(); + + scatter(values_out, + values_loc, + &storage.load_storage.values_shared[0], + active_mask, + thread_output_prefix, + tile_output_prefix, + tile_output_count); + } + + if (IS_LAST_TILE && threadIdx.x == 0) + { + *output_count = tile_output_prefix + tile_output_count; + } + } + + //--------------------------------------------------------------------- + // Constructor + //--------------------------------------------------------------------- + + THRUST_DEVICE_FUNCTION + impl(TempStorage& storage_, + ScanTileState& tile_state_, + KeysIt1 keys1_, + KeysIt2 keys2_, + ValuesIt1 values1_, + ValuesIt2 values2_, + Size keys1_count_, + Size keys2_count_, + KeysOutputIt keys_out_, + ValuesOutputIt values_out_, + CompareOp compare_op_, + SetOp set_op_, + pair* partitions_, + std::size_t* output_count_) + : storage(storage_) + , tile_state(tile_state_) + , keys1_in(core::make_load_iterator(ptx_plan(), keys1_)) + , keys2_in(core::make_load_iterator(ptx_plan(), keys2_)) + , values1_in(core::make_load_iterator(ptx_plan(), values1_)) + , values2_in(core::make_load_iterator(ptx_plan(), values2_)) + , keys1_count(keys1_count_) + , keys2_count(keys2_count_) + , keys_out(keys_out_) + , values_out(values_out_) + , compare_op(compare_op_) + , set_op(set_op_) + , partitions(partitions_) + , output_count(output_count_) + { + int tile_idx = blockIdx.x; + int num_tiles = gridDim.x; + + if (tile_idx < num_tiles - 1) + { + consume_tile(tile_idx); + } + else + { + consume_tile(tile_idx); + } + } + }; // struct impl + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY( + KeysIt1 keys1, + KeysIt2 keys2, + ValuesIt1 values1, + ValuesIt2 values2, + Size keys1_count, + Size keys2_count, + KeysOutputIt keys_output, + ValuesOutputIt values_output, + CompareOp compare_op, + SetOp set_op, + pair* partitions, + std::size_t* output_count, + ScanTileState tile_state, + char* shmem) + { + TempStorage& storage = *reinterpret_cast(shmem); + + impl(storage, + tile_state, + keys1, + keys2, + values1, + values2, + keys1_count, + keys2_count, + keys_output, + values_output, + compare_op, + set_op, + partitions, + output_count); + } +}; // struct SetOpAgent + +template +struct PartitionAgent +{ + template + struct PtxPlan : PtxPolicy<256> + {}; + + using ptx_plan = core::specialize_plan; + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY( + KeysIt1 keys1, + KeysIt2 keys2, + Size keys1_count, + Size keys2_count, + Size num_partitions, + pair* partitions, + CompareOp compare_op, + int items_per_tile, + char* /*shmem*/) + { + Size partition_idx = blockDim.x * blockIdx.x + threadIdx.x; + if (partition_idx < num_partitions) + { + Size partition_at = min(partition_idx * items_per_tile, keys1_count + keys2_count); + pair diag = balanced_path(keys1, keys2, keys1_count, keys2_count, partition_at, 4ll, compare_op); + partitions[partition_idx] = diag; + } + } +}; // struct PartitionAgent + +template +struct InitAgent +{ + template + struct PtxPlan : PtxPolicy<128> + {}; + + using ptx_plan = core::specialize_plan; + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY(ScanTileState tile_state, Size num_tiles, char* /*shmem*/) + { + tile_state.InitializeStatus(num_tiles); + } +}; // struct InitAgent + +//--------------------------------------------------------------------- +// Serial set operations +//--------------------------------------------------------------------- + +// serial_set_intersection +// ----------------------- +// emit A if A and B are in range and equal. +struct serial_set_intersection +{ + // max_input_size <= 32 + template + int THRUST_DEVICE_FUNCTION operator()( + T* keys, + int keys1_beg, + int keys2_beg, + int keys1_count, + int keys2_count, + T (&output)[ITEMS_PER_THREAD], + int (&indices)[ITEMS_PER_THREAD], + CompareOp compare_op) + { + int active_mask = 0; + + int aBegin = keys1_beg; + int bBegin = keys2_beg; + int aEnd = keys1_beg + keys1_count; + int bEnd = keys2_beg + keys2_count; + + T aKey = keys[aBegin]; + T bKey = keys[bBegin]; + +# pragma unroll + for (int i = 0; i < ITEMS_PER_THREAD; ++i) + { + bool pA = compare_op(aKey, bKey); + bool pB = compare_op(bKey, aKey); + + // The outputs must come from A by definition of set intersection. + output[i] = aKey; + indices[i] = aBegin; + + if ((aBegin < aEnd) && (bBegin < bEnd) && pA == pB) + { + active_mask |= 1 << i; + } + + if (!pB) + { + aKey = keys[++aBegin]; + } + if (!pA) + { + bKey = keys[++bBegin]; + } + } + return active_mask; + } +}; // struct serial_set_intersection + +// serial_set_symmetric_difference +// --------------------- +// emit A if A < B and emit B if B < A. +struct serial_set_symmetric_difference +{ + // max_input_size <= 32 + template + int THRUST_DEVICE_FUNCTION operator()( + T* keys, + int keys1_beg, + int keys2_beg, + int keys1_count, + int keys2_count, + T (&output)[ITEMS_PER_THREAD], + int (&indices)[ITEMS_PER_THREAD], + CompareOp compare_op) + { + int active_mask = 0; + + int aBegin = keys1_beg; + int bBegin = keys2_beg; + int aEnd = keys1_beg + keys1_count; + int bEnd = keys2_beg + keys2_count; + int end = aEnd + bEnd; + + T aKey = keys[aBegin]; + T bKey = keys[bBegin]; + +# pragma unroll + for (int i = 0; i < ITEMS_PER_THREAD; ++i) + { + bool pB = aBegin >= aEnd; + bool pA = !pB && bBegin >= bEnd; + + if (!pA && !pB) + { + pA = compare_op(aKey, bKey); + pB = !pA && compare_op(bKey, aKey); + } + + // The outputs must come from A by definition of set difference. + output[i] = pA ? aKey : bKey; + indices[i] = pA ? aBegin : bBegin; + + if (aBegin + bBegin < end && pA != pB) + { + active_mask |= 1 << i; + } + + if (!pB) + { + aKey = keys[++aBegin]; + } + if (!pA) + { + bKey = keys[++bBegin]; + } + } + return active_mask; + } +}; // struct set_symmetric_difference + +// serial_set_difference +// --------------------- +// emit A if A < B +struct serial_set_difference +{ + // max_input_size <= 32 + template + int THRUST_DEVICE_FUNCTION operator()( + T* keys, + int keys1_beg, + int keys2_beg, + int keys1_count, + int keys2_count, + T (&output)[ITEMS_PER_THREAD], + int (&indices)[ITEMS_PER_THREAD], + CompareOp compare_op) + { + int active_mask = 0; + + int aBegin = keys1_beg; + int bBegin = keys2_beg; + int aEnd = keys1_beg + keys1_count; + int bEnd = keys2_beg + keys2_count; + int end = aEnd + bEnd; + + T aKey = keys[aBegin]; + T bKey = keys[bBegin]; + +# pragma unroll + for (int i = 0; i < ITEMS_PER_THREAD; ++i) + { + bool pB = aBegin >= aEnd; + bool pA = !pB && bBegin >= bEnd; + + if (!pA && !pB) + { + pA = compare_op(aKey, bKey); + pB = !pA && compare_op(bKey, aKey); + } + + // The outputs must come from A by definition of set difference. + output[i] = aKey; + indices[i] = aBegin; + + if (aBegin + bBegin < end && pA) + { + active_mask |= 1 << i; + } + + if (!pB) + { + aKey = keys[++aBegin]; + } + if (!pA) + { + bKey = keys[++bBegin]; + } + } + return active_mask; + } +}; // struct set_difference + +// serial_set_union +// ---------------- +// emit A if A <= B else emit B +struct serial_set_union +{ + // max_input_size <= 32 + template + int THRUST_DEVICE_FUNCTION operator()( + T* keys, + int keys1_beg, + int keys2_beg, + int keys1_count, + int keys2_count, + T (&output)[ITEMS_PER_THREAD], + int (&indices)[ITEMS_PER_THREAD], + CompareOp compare_op) + { + int active_mask = 0; + + int aBegin = keys1_beg; + int bBegin = keys2_beg; + int aEnd = keys1_beg + keys1_count; + int bEnd = keys2_beg + keys2_count; + int end = aEnd + bEnd; + + T aKey = keys[aBegin]; + T bKey = keys[bBegin]; + +# pragma unroll + for (int i = 0; i < ITEMS_PER_THREAD; ++i) + { + bool pB = aBegin >= aEnd; + bool pA = !pB && bBegin >= bEnd; + + if (!pA && !pB) + { + pA = compare_op(aKey, bKey); + pB = !pA && compare_op(bKey, aKey); + } + + // Output A in case of a tie, so check if b < a. + output[i] = pB ? bKey : aKey; + indices[i] = pB ? bBegin : aBegin; + + if (aBegin + bBegin < end) + { + active_mask |= 1 << i; + } + + if (!pB) + { + aKey = keys[++aBegin]; + } + if (!pA) + { + bKey = keys[++bBegin]; + } + } + return active_mask; + } +}; // struct set_union + +template +cudaError_t THRUST_RUNTIME_FUNCTION doit_step( + void* d_temp_storage, + size_t& temp_storage_size, + KeysIt1 keys1, + KeysIt2 keys2, + ValuesIt1 values1, + ValuesIt2 values2, + Size num_keys1, + Size num_keys2, + KeysOutputIt keys_output, + ValuesOutputIt values_output, + std::size_t* output_count, + CompareOp compare_op, + SetOp set_op, + cudaStream_t stream) +{ + Size keys_total = num_keys1 + num_keys2; + if (keys_total == 0) + { + return cudaErrorNotSupported; + } + + cudaError_t status = cudaSuccess; + + using core::AgentLauncher; + using core::AgentPlan; + + using set_op_agent = AgentLauncher< + SetOpAgent>; + + using partition_agent = AgentLauncher>; + + using ScanTileState = typename set_op_agent::ScanTileState; + using init_agent = AgentLauncher>; + + AgentPlan set_op_plan = set_op_agent::get_plan(stream); + AgentPlan init_plan = init_agent::get_plan(); + AgentPlan partition_plan = partition_agent::get_plan(); + + int tile_size = set_op_plan.items_per_tile; + Size num_tiles = (keys_total + tile_size - 1) / tile_size; + + size_t tile_agent_storage; + status = ScanTileState::AllocationSize(static_cast(num_tiles), tile_agent_storage); + CUDA_CUB_RET_IF_FAIL(status); + + size_t vshmem_storage = core::vshmem_size(set_op_plan.shared_memory_size, num_tiles); + size_t partition_agent_storage = (num_tiles + 1) * sizeof(Size) * 2; + + void* allocations[3] = {nullptr, nullptr, nullptr}; + size_t allocation_sizes[3] = {tile_agent_storage, partition_agent_storage, vshmem_storage}; + + status = core::alias_storage(d_temp_storage, temp_storage_size, allocations, allocation_sizes); + CUDA_CUB_RET_IF_FAIL(status); + + if (d_temp_storage == nullptr) + { + return status; + } + + ScanTileState tile_state; + status = tile_state.Init(static_cast(num_tiles), allocations[0], allocation_sizes[0]); + CUDA_CUB_RET_IF_FAIL(status); + + pair* partitions = (pair*) allocations[1]; + char* vshmem_ptr = vshmem_storage > 0 ? (char*) allocations[2] : nullptr; + + init_agent ia(init_plan, num_tiles, stream, "set_op::init_agent"); + ia.launch(tile_state, num_tiles); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + + partition_agent pa(partition_plan, num_tiles + 1, stream, "set_op::partition agent"); + pa.launch(keys1, keys2, num_keys1, num_keys2, num_tiles + 1, partitions, compare_op, tile_size); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + + set_op_agent sa(set_op_plan, keys_total, stream, vshmem_ptr, "set_op::set_op_agent"); + sa.launch( + keys1, + keys2, + values1, + values2, + num_keys1, + num_keys2, + keys_output, + values_output, + compare_op, + set_op, + partitions, + output_count, + tile_state); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + + return status; +} + +template +THRUST_RUNTIME_FUNCTION pair set_operations( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ValuesIt1 values1_first, + ValuesIt2 values2_first, + KeysOutputIt keys_output, + ValuesOutputIt values_output, + CompareOp compare_op, + SetOp set_op) +{ + using size_type = typename iterator_traits::difference_type; + + size_type num_keys1 = static_cast(thrust::distance(keys1_first, keys1_last)); + size_type num_keys2 = static_cast(thrust::distance(keys2_first, keys2_last)); + + if (num_keys1 + num_keys2 == 0) + { + return thrust::make_pair(keys_output, values_output); + } + + size_t temp_storage_bytes = 0; + cudaStream_t stream = cuda_cub::stream(policy); + + cudaError_t status; + THRUST_DOUBLE_INDEX_TYPE_DISPATCH( + status, + doit_step, + num_keys1, + num_keys2, + (nullptr, + temp_storage_bytes, + keys1_first, + keys2_first, + values1_first, + values2_first, + num_keys1_fixed, + num_keys2_fixed, + keys_output, + values_output, + static_cast(nullptr), + compare_op, + set_op, + stream)); + cuda_cub::throw_on_error(status, "set_operations failed on 1st step"); + + size_t allocation_sizes[2] = {sizeof(std::size_t), temp_storage_bytes}; + void* allocations[2] = {nullptr, nullptr}; + + size_t storage_size = 0; + + status = core::alias_storage(nullptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "set_operations failed on 1st alias_storage"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, storage_size); + void* ptr = static_cast(tmp.data().get()); + + status = core::alias_storage(ptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "set_operations failed on 2nd alias_storage"); + + std::size_t* d_output_count = thrust::detail::aligned_reinterpret_cast(allocations[0]); + + THRUST_DOUBLE_INDEX_TYPE_DISPATCH( + status, + doit_step, + num_keys1, + num_keys2, + (allocations[1], + temp_storage_bytes, + keys1_first, + keys2_first, + values1_first, + values2_first, + num_keys1_fixed, + num_keys2_fixed, + keys_output, + values_output, + d_output_count, + compare_op, + set_op, + stream)); + cuda_cub::throw_on_error(status, "set_operations failed on 2nd step"); + + status = cuda_cub::synchronize(policy); + cuda_cub::throw_on_error(status, "set_operations failed to synchronize"); + + std::size_t output_count = cuda_cub::get_value(policy, d_output_count); + + return thrust::make_pair(keys_output + output_count, values_output + output_count); +} +} // namespace __set_operations + +//------------------------- +// Thrust API entry points +//------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIt _CCCL_HOST_DEVICE set_difference( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result, + CompareOp compare) +{ + THRUST_CDP_DISPATCH( + (using items1_t = thrust::iterator_value_t; items1_t* null_ = nullptr; + auto tmp = __set_operations::set_operations( + policy, + items1_first, + items1_last, + items2_first, + items2_last, + null_, + null_, + result, + null_, + compare, + __set_operations::serial_set_difference()); + result = tmp.first;), + (result = thrust::set_difference( + cvt_to_seq(derived_cast(policy)), items1_first, items1_last, items2_first, items2_last, result, compare);)); + return result; +} + +template +OutputIt _CCCL_HOST_DEVICE set_difference( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_difference( + policy, items1_first, items1_last, items2_first, items2_last, result, less()); +} + +/*****************************/ + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIt _CCCL_HOST_DEVICE set_intersection( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result, + CompareOp compare) +{ + THRUST_CDP_DISPATCH( + (using items1_t = thrust::iterator_value_t; items1_t* null_ = nullptr; + auto tmp = __set_operations::set_operations( + policy, + items1_first, + items1_last, + items2_first, + items2_last, + null_, + null_, + result, + null_, + compare, + __set_operations::serial_set_intersection()); + result = tmp.first;), + (result = thrust::set_intersection( + cvt_to_seq(derived_cast(policy)), items1_first, items1_last, items2_first, items2_last, result, compare);)); + return result; +} + +template +OutputIt _CCCL_HOST_DEVICE set_intersection( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_intersection( + policy, items1_first, items1_last, items2_first, items2_last, result, less()); +} + +/*****************************/ + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIt _CCCL_HOST_DEVICE set_symmetric_difference( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result, + CompareOp compare) +{ + THRUST_CDP_DISPATCH( + (using items1_t = thrust::iterator_value_t; items1_t* null_ = nullptr; + auto tmp = __set_operations::set_operations( + policy, + items1_first, + items1_last, + items2_first, + items2_last, + null_, + null_, + result, + null_, + compare, + __set_operations::serial_set_symmetric_difference()); + result = tmp.first;), + (result = thrust::set_symmetric_difference( + cvt_to_seq(derived_cast(policy)), items1_first, items1_last, items2_first, items2_last, result, compare);)); + return result; +} + +template +OutputIt _CCCL_HOST_DEVICE set_symmetric_difference( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_symmetric_difference( + policy, items1_first, items1_last, items2_first, items2_last, result, less()); +} + +/*****************************/ + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIt _CCCL_HOST_DEVICE set_union( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result, + CompareOp compare) +{ + THRUST_CDP_DISPATCH( + (using items1_t = thrust::iterator_value_t; items1_t* null_ = nullptr; + auto tmp = __set_operations::set_operations( + policy, + items1_first, + items1_last, + items2_first, + items2_last, + null_, + null_, + result, + null_, + compare, + __set_operations::serial_set_union()); + result = tmp.first;), + (result = thrust::set_union( + cvt_to_seq(derived_cast(policy)), items1_first, items1_last, items2_first, items2_last, result, compare);)); + return result; +} + +template +OutputIt _CCCL_HOST_DEVICE set_union( + execution_policy& policy, + ItemsIt1 items1_first, + ItemsIt1 items1_last, + ItemsIt2 items2_first, + ItemsIt2 items2_last, + OutputIt result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_union(policy, items1_first, items1_last, items2_first, items2_last, result, less()); +} + +/*****************************/ +/*****************************/ +/***** *_by_key *****/ +/*****************************/ +/*****************************/ + +/*****************************/ + +_CCCL_EXEC_CHECK_DISABLE +template +pair _CCCL_HOST_DEVICE set_difference_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + ItemsIt2 items2_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result, + CompareOp compare_op) +{ + auto ret = thrust::make_pair(keys_result, items_result); + THRUST_CDP_DISPATCH( + (ret = __set_operations::set_operations( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + compare_op, + __set_operations::serial_set_difference());), + (ret = thrust::set_difference_by_key( + cvt_to_seq(derived_cast(policy)), + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + compare_op);)); + return ret; +} + +template +pair _CCCL_HOST_DEVICE set_difference_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + ItemsIt2 items2_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_difference_by_key( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + less()); +} + +/*****************************/ + +_CCCL_EXEC_CHECK_DISABLE +template +pair _CCCL_HOST_DEVICE set_intersection_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result, + CompareOp compare_op) +{ + auto ret = thrust::make_pair(keys_result, items_result); + THRUST_CDP_DISPATCH( + (ret = __set_operations::set_operations( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items1_first, + keys_result, + items_result, + compare_op, + __set_operations::serial_set_intersection());), + (ret = thrust::set_intersection_by_key( + cvt_to_seq(derived_cast(policy)), + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + keys_result, + items_result, + compare_op);)); + return ret; +} + +template +pair _CCCL_HOST_DEVICE set_intersection_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_intersection_by_key( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + keys_result, + items_result, + less()); +} + +/*****************************/ + +_CCCL_EXEC_CHECK_DISABLE +template +pair _CCCL_HOST_DEVICE set_symmetric_difference_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + ItemsIt2 items2_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result, + CompareOp compare_op) +{ + auto ret = thrust::make_pair(keys_result, items_result); + THRUST_CDP_DISPATCH( + (ret = __set_operations::set_operations( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + compare_op, + __set_operations::serial_set_symmetric_difference());), + (ret = thrust::set_symmetric_difference_by_key( + cvt_to_seq(derived_cast(policy)), + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + compare_op);)); + return ret; +} + +template +pair _CCCL_HOST_DEVICE set_symmetric_difference_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + ItemsIt2 items2_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_symmetric_difference_by_key( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + less()); +} + +/*****************************/ + +_CCCL_EXEC_CHECK_DISABLE +template +pair _CCCL_HOST_DEVICE set_union_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + ItemsIt2 items2_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result, + CompareOp compare_op) +{ + auto ret = thrust::make_pair(keys_result, items_result); + THRUST_CDP_DISPATCH( + (ret = __set_operations::set_operations( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + compare_op, + __set_operations::serial_set_union());), + (ret = thrust::set_union_by_key( + cvt_to_seq(derived_cast(policy)), + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + compare_op);)); + return ret; +} + +template +pair _CCCL_HOST_DEVICE set_union_by_key( + execution_policy& policy, + KeysIt1 keys1_first, + KeysIt1 keys1_last, + KeysIt2 keys2_first, + KeysIt2 keys2_last, + ItemsIt1 items1_first, + ItemsIt2 items2_first, + KeysOutputIt keys_result, + ItemsOutputIt items_result) +{ + using value_type = typename thrust::iterator_value::type; + return cuda_cub::set_union_by_key( + policy, + keys1_first, + keys1_last, + keys2_first, + keys2_last, + items1_first, + items2_first, + keys_result, + items_result, + less()); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/sort.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/sort.h new file mode 100644 index 0000000000000000000000000000000000000000..895119a0389a9921d046319797113fd84440e950 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/sort.h @@ -0,0 +1,477 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +# if defined(_CCCL_HAS_NVFP16) +# include +# endif // _CCCL_HAS_NVFP16 + +# if defined(_CCCL_HAS_NVBF16) +_CCCL_DIAG_PUSH +_CCCL_DIAG_SUPPRESS_CLANG("-Wunused-function") +# include +_CCCL_DIAG_POP +# endif // _CCCL_HAS_NVBF16 + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +namespace __merge_sort +{ + +template +THRUST_RUNTIME_FUNCTION cudaError_t doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + KeysIt keys, + ItemsIt, + Size keys_count, + CompareOp compare_op, + cudaStream_t stream, + thrust::detail::integral_constant /* sort_keys */) +{ + using ItemsInputIt = cub::NullType*; + ItemsInputIt items = nullptr; + + using DispatchMergeSortT = cub::DispatchMergeSort; + + return DispatchMergeSortT::Dispatch( + d_temp_storage, temp_storage_bytes, keys, items, keys, items, keys_count, compare_op, stream); +} + +template +THRUST_RUNTIME_FUNCTION cudaError_t doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + KeysIt keys, + ItemsIt items, + Size keys_count, + CompareOp compare_op, + cudaStream_t stream, + thrust::detail::integral_constant /* sort_items */) +{ + using DispatchMergeSortT = cub::DispatchMergeSort; + + return DispatchMergeSortT::Dispatch( + d_temp_storage, temp_storage_bytes, keys, items, keys, items, keys_count, compare_op, stream); +} + +template +THRUST_RUNTIME_FUNCTION cudaError_t doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + KeysIt keys, + ItemsIt items, + Size keys_count, + CompareOp compare_op, + cudaStream_t stream) +{ + if (keys_count == 0) + { + return cudaSuccess; + } + + thrust::detail::integral_constant sort_items{}; + + return doit_step(d_temp_storage, temp_storage_bytes, keys, items, keys_count, compare_op, stream, sort_items); +} + +template +THRUST_RUNTIME_FUNCTION void merge_sort( + execution_policy& policy, KeysIt keys_first, KeysIt keys_last, ItemsIt items_first, CompareOp compare_op) + +{ + using size_type = typename iterator_traits::difference_type; + + size_type count = static_cast(thrust::distance(keys_first, keys_last)); + + size_t storage_size = 0; + cudaStream_t stream = cuda_cub::stream(policy); + + cudaError_t status; + status = doit_step(nullptr, storage_size, keys_first, items_first, count, compare_op, stream); + cuda_cub::throw_on_error(status, "merge_sort: failed on 1st step"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, storage_size); + void* ptr = static_cast(tmp.data().get()); + + status = doit_step(ptr, storage_size, keys_first, items_first, count, compare_op, stream); + cuda_cub::throw_on_error(status, "merge_sort: failed on 2nd step"); + + status = cuda_cub::synchronize_optional(policy); + cuda_cub::throw_on_error(status, "merge_sort: failed to synchronize"); +} +} // namespace __merge_sort + +namespace __radix_sort +{ + +template +struct dispatch; + +// sort keys in ascending order +template +struct dispatch> +{ + template + THRUST_RUNTIME_FUNCTION static cudaError_t + doit(void* d_temp_storage, + size_t& temp_storage_bytes, + cub::DoubleBuffer& keys_buffer, + cub::DoubleBuffer& /*items_buffer*/, + Size count, + cudaStream_t stream) + { + return cub::DeviceRadixSort::SortKeys( + d_temp_storage, temp_storage_bytes, keys_buffer, count, 0, static_cast(sizeof(Key) * 8), stream); + } +}; // struct dispatch -- sort keys in ascending order; + +// sort keys in descending order +template +struct dispatch> +{ + template + THRUST_RUNTIME_FUNCTION static cudaError_t + doit(void* d_temp_storage, + size_t& temp_storage_bytes, + cub::DoubleBuffer& keys_buffer, + cub::DoubleBuffer& /*items_buffer*/, + Size count, + cudaStream_t stream) + { + return cub::DeviceRadixSort::SortKeysDescending( + d_temp_storage, temp_storage_bytes, keys_buffer, count, 0, static_cast(sizeof(Key) * 8), stream); + } +}; // struct dispatch -- sort keys in descending order; + +// sort pairs in ascending order +template +struct dispatch> +{ + template + THRUST_RUNTIME_FUNCTION static cudaError_t + doit(void* d_temp_storage, + size_t& temp_storage_bytes, + cub::DoubleBuffer& keys_buffer, + cub::DoubleBuffer& items_buffer, + Size count, + cudaStream_t stream) + { + return cub::DeviceRadixSort::SortPairs( + d_temp_storage, temp_storage_bytes, keys_buffer, items_buffer, count, 0, static_cast(sizeof(Key) * 8), stream); + } +}; // struct dispatch -- sort pairs in ascending order; + +// sort pairs in descending order +template +struct dispatch> +{ + template + THRUST_RUNTIME_FUNCTION static cudaError_t + doit(void* d_temp_storage, + size_t& temp_storage_bytes, + cub::DoubleBuffer& keys_buffer, + cub::DoubleBuffer& items_buffer, + Size count, + cudaStream_t stream) + { + return cub::DeviceRadixSort::SortPairsDescending( + d_temp_storage, temp_storage_bytes, keys_buffer, items_buffer, count, 0, static_cast(sizeof(Key) * 8), stream); + } +}; // struct dispatch -- sort pairs in descending order; + +template +struct dispatch> : dispatch> +{}; + +template +struct dispatch> : dispatch> +{}; + +template +THRUST_RUNTIME_FUNCTION void radix_sort(execution_policy& policy, Key* keys, Item* items, Size count, CompareOp) +{ + size_t temp_storage_bytes = 0; + cudaStream_t stream = cuda_cub::stream(policy); + + cub::DoubleBuffer keys_buffer(keys, nullptr); + cub::DoubleBuffer items_buffer(items, nullptr); + + Size keys_count = count; + Size items_count = SORT_ITEMS::value ? count : 0; + + cudaError_t status; + + status = + dispatch::doit(nullptr, temp_storage_bytes, keys_buffer, items_buffer, keys_count, stream); + cuda_cub::throw_on_error(status, "radix_sort: failed on 1st step"); + + size_t keys_temp_storage = core::align_to(sizeof(Key) * keys_count, 128); + size_t items_temp_storage = core::align_to(sizeof(Item) * items_count, 128); + + size_t storage_size = keys_temp_storage + items_temp_storage + temp_storage_bytes; + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, storage_size); + + keys_buffer.d_buffers[1] = thrust::detail::aligned_reinterpret_cast(tmp.data().get()); + items_buffer.d_buffers[1] = thrust::detail::aligned_reinterpret_cast(tmp.data().get() + keys_temp_storage); + void* ptr = static_cast(tmp.data().get() + keys_temp_storage + items_temp_storage); + + status = + dispatch::doit(ptr, temp_storage_bytes, keys_buffer, items_buffer, keys_count, stream); + cuda_cub::throw_on_error(status, "radix_sort: failed on 2nd step"); + + if (keys_buffer.selector != 0) + { + Key* temp_ptr = reinterpret_cast(keys_buffer.d_buffers[1]); + cuda_cub::copy_n(policy, temp_ptr, keys_count, keys); + } + _CCCL_IF_CONSTEXPR (SORT_ITEMS::value) + { + if (items_buffer.selector != 0) + { + Item* temp_ptr = reinterpret_cast(items_buffer.d_buffers[1]); + cuda_cub::copy_n(policy, temp_ptr, items_count, items); + } + } +} +} // namespace __radix_sort + +//--------------------------------------------------------------------- +// Smart sort picks at compile-time whether to dispatch radix or merge sort +//--------------------------------------------------------------------- + +namespace __smart_sort +{ + +// TODO(bgruber): we can drop thrust::less etc. when they truly alias to the ::cuda::std ones +template +using can_use_primitive_sort = ::cuda::std::integral_constant< + bool, + (::cuda::std::is_arithmetic::value +# if defined(_CCCL_HAS_NVFP16) && !defined(__CUDA_NO_HALF_OPERATORS__) && !defined(__CUDA_NO_HALF_CONVERSIONS__) + || ::cuda::std::is_same::value +# endif // defined(_CCCL_HAS_NVFP16) && !defined(__CUDA_NO_HALF_OPERATORS__) && !defined(__CUDA_NO_HALF_CONVERSIONS__) +# if defined(_CCCL_HAS_NVBF16) && !defined(__CUDA_NO_BFLOAT16_CONVERSIONS__) \ + && !defined(__CUDA_NO_BFLOAT16_OPERATORS__) + || ::cuda::std::is_same::value +# endif // defined(_CCCL_HAS_NVBF16) && !defined(__CUDA_NO_BFLOAT16_CONVERSIONS__) && + // !defined(__CUDA_NO_BFLOAT16_OPERATORS__) + ) + && (::cuda::std::is_same>::value + || ::cuda::std::is_same>::value + || ::cuda::std::is_same>::value + || ::cuda::std::is_same>::value + || ::cuda::std::is_same>::value + || ::cuda::std::is_same>::value + || ::cuda::std::is_same>::value + || ::cuda::std::is_same>::value)>; + +template < + class SORT_ITEMS, + class STABLE, + class Policy, + class KeysIt, + class ItemsIt, + class CompareOp, + ::cuda::std::enable_if_t::type, CompareOp>::value, int> = 0> +THRUST_RUNTIME_FUNCTION void +smart_sort(Policy& policy, KeysIt keys_first, KeysIt keys_last, ItemsIt items_first, CompareOp compare_op) +{ + __merge_sort::merge_sort(policy, keys_first, keys_last, items_first, compare_op); +} + +template < + class SORT_ITEMS, + class /*STABLE*/, + class Policy, + class KeysIt, + class ItemsIt, + class CompareOp, + ::cuda::std::enable_if_t::type, CompareOp>::value, int> = 0> +THRUST_RUNTIME_FUNCTION void smart_sort( + execution_policy& policy, KeysIt keys_first, KeysIt keys_last, ItemsIt items_first, CompareOp compare_op) +{ + // ensure sequences have trivial iterators + thrust::detail::trivial_sequence keys(policy, keys_first, keys_last); + + if (SORT_ITEMS::value) + { + thrust::detail::trivial_sequence values( + policy, items_first, items_first + (keys_last - keys_first)); + + __radix_sort::radix_sort( + policy, + thrust::raw_pointer_cast(&*keys.begin()), + thrust::raw_pointer_cast(&*values.begin()), + keys_last - keys_first, + compare_op); + + if (!is_contiguous_iterator::value) + { + cuda_cub::copy(policy, values.begin(), values.end(), items_first); + } + } + else + { + __radix_sort::radix_sort( + policy, + thrust::raw_pointer_cast(&*keys.begin()), + thrust::raw_pointer_cast(&*keys.begin()), + keys_last - keys_first, + compare_op); + } + + // copy results back, if necessary + if (!is_contiguous_iterator::value) + { + cuda_cub::copy(policy, keys.begin(), keys.end(), keys_first); + } + + cuda_cub::throw_on_error(cuda_cub::synchronize_optional(policy), "smart_sort: failed to synchronize"); +} +} // namespace __smart_sort + +//------------------------- +// Thrust API entry points +//------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +void _CCCL_HOST_DEVICE sort(execution_policy& policy, ItemsIt first, ItemsIt last, CompareOp compare_op) +{ + THRUST_CDP_DISPATCH((using item_t = thrust::iterator_value_t; item_t* null_ = nullptr; + __smart_sort::smart_sort( + policy, first, last, null_, compare_op);), + (thrust::sort(cvt_to_seq(derived_cast(policy)), first, last, compare_op);)); +} + +_CCCL_EXEC_CHECK_DISABLE +template +void _CCCL_HOST_DEVICE stable_sort(execution_policy& policy, ItemsIt first, ItemsIt last, CompareOp compare_op) +{ + THRUST_CDP_DISPATCH((using item_t = thrust::iterator_value_t; item_t* null_ = nullptr; + __smart_sort::smart_sort( + policy, first, last, null_, compare_op);), + (thrust::stable_sort(cvt_to_seq(derived_cast(policy)), first, last, compare_op);)); +} + +_CCCL_EXEC_CHECK_DISABLE +template +void _CCCL_HOST_DEVICE sort_by_key( + execution_policy& policy, KeysIt keys_first, KeysIt keys_last, ValuesIt values, CompareOp compare_op) +{ + THRUST_CDP_DISPATCH( + (__smart_sort::smart_sort( + policy, keys_first, keys_last, values, compare_op);), + (thrust::sort_by_key(cvt_to_seq(derived_cast(policy)), keys_first, keys_last, values, compare_op);)); +} + +_CCCL_EXEC_CHECK_DISABLE +template +void _CCCL_HOST_DEVICE stable_sort_by_key( + execution_policy& policy, KeysIt keys_first, KeysIt keys_last, ValuesIt values, CompareOp compare_op) +{ + THRUST_CDP_DISPATCH( + (__smart_sort::smart_sort( + policy, keys_first, keys_last, values, compare_op);), + (thrust::stable_sort_by_key(cvt_to_seq(derived_cast(policy)), keys_first, keys_last, values, compare_op);)); +} + +// API with default comparator + +template +void _CCCL_HOST_DEVICE sort(execution_policy& policy, ItemsIt first, ItemsIt last) +{ + using item_type = typename thrust::iterator_value::type; + cuda_cub::sort(policy, first, last, less()); +} + +template +void _CCCL_HOST_DEVICE stable_sort(execution_policy& policy, ItemsIt first, ItemsIt last) +{ + using item_type = typename thrust::iterator_value::type; + cuda_cub::stable_sort(policy, first, last, less()); +} + +template +void _CCCL_HOST_DEVICE +sort_by_key(execution_policy& policy, KeysIt keys_first, KeysIt keys_last, ValuesIt values) +{ + using key_type = typename thrust::iterator_value::type; + cuda_cub::sort_by_key(policy, keys_first, keys_last, values, less()); +} + +template +void _CCCL_HOST_DEVICE +stable_sort_by_key(execution_policy& policy, KeysIt keys_first, KeysIt keys_last, ValuesIt values) +{ + using key_type = typename thrust::iterator_value::type; + cuda_cub::stable_sort_by_key(policy, keys_first, keys_last, values, less()); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/swap_ranges.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/swap_ranges.h new file mode 100644 index 0000000000000000000000000000000000000000..73a73d63775f525cd1cc98c061d6dee819a511d5 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/swap_ranges.h @@ -0,0 +1,103 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include +# include +# include +# include +# include + +# include + +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +namespace __swap_ranges +{ + +template +struct swap_f +{ + ItemsIt1 items1; + ItemsIt2 items2; + + using value1_type = typename iterator_traits::value_type; + using value2_type = typename iterator_traits::value_type; + + THRUST_FUNCTION + swap_f(ItemsIt1 items1_, ItemsIt2 items2_) + : items1(items1_) + , items2(items2_) + {} + + template + void THRUST_DEVICE_FUNCTION operator()(Size idx) + { + // TODO(bgruber): this should probably use ::cuda::std::iter_swap(items1 + idx, items2 + idx); + value1_type item1 = items1[idx]; + value2_type item2 = items2[idx]; + using ::cuda::std::swap; + swap(item1, item2); + items1[idx] = item1; + items2[idx] = item2; + } +}; +} // namespace __swap_ranges + +template +ItemsIt2 _CCCL_HOST_DEVICE +swap_ranges(execution_policy& policy, ItemsIt1 first1, ItemsIt1 last1, ItemsIt2 first2) +{ + using size_type = typename iterator_traits::difference_type; + + size_type num_items = static_cast(thrust::distance(first1, last1)); + + cuda_cub::parallel_for(policy, __swap_ranges::swap_f(first1, first2), num_items); + + return first2 + num_items; +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/tabulate.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/tabulate.h new file mode 100644 index 0000000000000000000000000000000000000000..6caec2101205886d0be4611dc24f783432469e2c --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/tabulate.h @@ -0,0 +1,86 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include + +# include +# include +# include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +namespace __tabulate +{ + +template +struct functor +{ + Iterator items; + TabulateOp op; + + _CCCL_HOST_DEVICE functor(Iterator items_, TabulateOp op_) + : items(items_) + , op(op_) + {} + + void _CCCL_DEVICE operator()(Size idx) + { + items[idx] = op(idx); + } +}; // struct functor + +} // namespace __tabulate + +template +void _CCCL_HOST_DEVICE tabulate(execution_policy& policy, Iterator first, Iterator last, TabulateOp tabulate_op) +{ + using size_type = typename iterator_traits::difference_type; + + size_type count = thrust::distance(first, last); + + using functor_t = __tabulate::functor; + + cuda_cub::parallel_for(policy, functor_t(first, tabulate_op), count); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/temporary_buffer.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/temporary_buffer.h new file mode 100644 index 0000000000000000000000000000000000000000..52dbc142c5d17e9a0be953e3d84358d558f70695 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/temporary_buffer.h @@ -0,0 +1,29 @@ +/* + * Copyright 2008-2016 NVIDIA Corporation + * + * 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. + */ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +// this system has no special temporary buffer functions diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/terminate.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/terminate.h new file mode 100644 index 0000000000000000000000000000000000000000..7f4e8dc1a74b9233307ea5e5f4d4924e9fafb356 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/terminate.h @@ -0,0 +1,59 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ + +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include + +#include + +THRUST_NAMESPACE_BEGIN +namespace system +{ +namespace cuda +{ +namespace detail +{ +inline _CCCL_HOST_DEVICE void terminate_with_message(const char* message) +{ + printf("%s\n", message); + ::cuda::std::terminate(); +} +} // namespace detail +} // namespace cuda +} // namespace system +THRUST_NAMESPACE_END diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/transform_scan.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/transform_scan.h new file mode 100644 index 0000000000000000000000000000000000000000..2227249ab168ab38ddf757d4f861a8d89f44296a --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/transform_scan.h @@ -0,0 +1,121 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include +# include +# include +# include + +# include + +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +template +OutputIt _CCCL_HOST_DEVICE transform_inclusive_scan( + execution_policy& policy, + InputIt first, + InputIt last, + OutputIt result, + TransformOp transform_op, + ScanOp scan_op) +{ + // Use the transformed input iterator's value type per https://wg21.link/P0571 + using input_type = typename thrust::iterator_value::type; + using result_type = thrust::detail::invoke_result_t; + using value_type = ::cuda::std::remove_cvref_t; + + using size_type = typename iterator_traits::difference_type; + size_type num_items = static_cast(thrust::distance(first, last)); + using transformed_iterator_t = transform_iterator; + + return cuda_cub::inclusive_scan_n(policy, transformed_iterator_t(first, transform_op), num_items, result, scan_op); +} + +template +OutputIt _CCCL_HOST_DEVICE transform_inclusive_scan( + execution_policy& policy, + InputIt first, + InputIt last, + OutputIt result, + TransformOp transform_op, + InitialValueType init, + ScanOp scan_op) +{ + using input_type = typename thrust::iterator_value::type; + using result_type = thrust::detail::invoke_result_t; + using value_type = ::cuda::std::remove_cvref_t; + + using size_type = typename iterator_traits::difference_type; + size_type num_items = static_cast(thrust::distance(first, last)); + using transformed_iterator_t = transform_iterator; + + return cuda_cub::inclusive_scan_n( + policy, transformed_iterator_t(first, transform_op), num_items, result, init, scan_op); +} + +template +OutputIt _CCCL_HOST_DEVICE transform_exclusive_scan( + execution_policy& policy, + InputIt first, + InputIt last, + OutputIt result, + TransformOp transform_op, + InitialValueType init, + ScanOp scan_op) +{ + // Use the initial value type per https://wg21.link/P0571 + using result_type = ::cuda::std::remove_cvref_t; + + using size_type = typename iterator_traits::difference_type; + size_type num_items = static_cast(thrust::distance(first, last)); + using transformed_iterator_t = transform_iterator; + + return cuda_cub::exclusive_scan_n( + policy, transformed_iterator_t(first, transform_op), num_items, result, init, scan_op); +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/uninitialized_copy.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/uninitialized_copy.h new file mode 100644 index 0000000000000000000000000000000000000000..d24cf87e6edfeee796d8e22f2cbec52755201e6a --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/uninitialized_copy.h @@ -0,0 +1,108 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +namespace __uninitialized_copy +{ + +template +struct functor +{ + InputIt input; + OutputIt output; + + using InputType = typename iterator_traits::value_type; + using OutputType = typename iterator_traits::value_type; + + THRUST_FUNCTION + functor(InputIt input_, OutputIt output_) + : input(input_) + , output(output_) + {} + + template + void THRUST_DEVICE_FUNCTION operator()(Size idx) + { + InputType const& in = raw_reference_cast(input[idx]); + OutputType& out = raw_reference_cast(output[idx]); + +# if _CCCL_CUDA_COMPILER(CLANG) + // XXX unsafe, but clang is seemngly unable to call in-place new + out = in; +# else + ::new (static_cast(&out)) OutputType(in); +# endif + } +}; // struct functor + +} // namespace __uninitialized_copy + +template +OutputIt _CCCL_HOST_DEVICE +uninitialized_copy_n(execution_policy& policy, InputIt first, Size count, OutputIt result) +{ + using functor_t = __uninitialized_copy::functor; + + cuda_cub::parallel_for(policy, functor_t(first, result), count); + + return result + count; +} + +template +OutputIt _CCCL_HOST_DEVICE +uninitialized_copy(execution_policy& policy, InputIt first, InputIt last, OutputIt result) +{ + return cuda_cub::uninitialized_copy_n(policy, first, thrust::distance(first, last), result); +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/uninitialized_fill.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/uninitialized_fill.h new file mode 100644 index 0000000000000000000000000000000000000000..233f600e2bb6289da3c8f0a85e662b24d771794f --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/uninitialized_fill.h @@ -0,0 +1,105 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN + +namespace cuda_cub +{ + +namespace __uninitialized_fill +{ + +template +struct functor +{ + Iterator items; + T value; + + using value_type = typename iterator_traits::value_type; + + THRUST_FUNCTION + functor(Iterator items_, T const& value_) + : items(items_) + , value(value_) + {} + + template + void THRUST_DEVICE_FUNCTION operator()(Size idx) + { + value_type& out = raw_reference_cast(items[idx]); + +# if _CCCL_CUDA_COMPILER(CLANG) + // XXX unsafe. cuda-clang is seemingly unable to call ::new in device code + out = value; +# else + ::new (static_cast(&out)) value_type(value); +# endif + } +}; // struct functor + +} // namespace __uninitialized_fill + +template +Iterator _CCCL_HOST_DEVICE +uninitialized_fill_n(execution_policy& policy, Iterator first, Size count, T const& x) +{ + using functor_t = __uninitialized_fill::functor; + + cuda_cub::parallel_for(policy, functor_t(first, x), count); + + return first + count; +} + +template +void _CCCL_HOST_DEVICE uninitialized_fill(execution_policy& policy, Iterator first, Iterator last, T const& x) +{ + cuda_cub::uninitialized_fill_n(policy, first, thrust::distance(first, last), x); +} + +} // namespace cuda_cub + +THRUST_NAMESPACE_END +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/unique.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/unique.h new file mode 100644 index 0000000000000000000000000000000000000000..49d8e2668b6f4d45bcc3256c70bca0120d132bcd --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/unique.h @@ -0,0 +1,681 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN + +template +_CCCL_HOST_DEVICE ForwardIterator unique( + const thrust::detail::execution_policy_base& exec, + ForwardIterator first, + ForwardIterator last, + BinaryPredicate binary_pred); + +template +_CCCL_HOST_DEVICE OutputIterator unique_copy( + const thrust::detail::execution_policy_base& exec, + InputIterator first, + InputIterator last, + OutputIterator result, + BinaryPredicate binary_pred); + +template +_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type unique_count( + const thrust::detail::execution_policy_base& exec, + ForwardIterator first, + ForwardIterator last, + BinaryPredicate binary_pred); + +namespace cuda_cub +{ + +// XXX it should be possible to unify unique & unique_by_key into a single +// agent with various specializations, similar to what is done +// with partition +namespace __unique +{ + +template +struct PtxPolicy +{ + enum + { + BLOCK_THREADS = _BLOCK_THREADS, + ITEMS_PER_THREAD = _ITEMS_PER_THREAD, + ITEMS_PER_TILE = _BLOCK_THREADS * _ITEMS_PER_THREAD, + }; + static const cub::BlockLoadAlgorithm LOAD_ALGORITHM = _LOAD_ALGORITHM; + static const cub::CacheLoadModifier LOAD_MODIFIER = _LOAD_MODIFIER; + static const cub::BlockScanAlgorithm SCAN_ALGORITHM = _SCAN_ALGORITHM; +}; // struct PtxPolicy + +template +struct Tuning; + +namespace mpl = thrust::detail::mpl::math; + +template +struct items_per_thread +{ + enum + { + value = mpl::min(NOMINAL_4B_ITEMS_PER_THREAD * 4 / sizeof(T))>::value>::value + }; +}; + +template +struct Tuning +{ + const static int INPUT_SIZE = sizeof(T); + enum + { + NOMINAL_4B_ITEMS_PER_THREAD = 11, + // + ITEMS_PER_THREAD = items_per_thread::value + }; + + using type = + PtxPolicy<64, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_LDG, cub::BLOCK_SCAN_WARP_SCANS>; +}; // Tuning for sm52 + +template +struct Tuning +{ + const static int INPUT_SIZE = sizeof(T); + enum + { + NOMINAL_4B_ITEMS_PER_THREAD = 9, + // + ITEMS_PER_THREAD = items_per_thread::value + }; + + using type = + PtxPolicy<128, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_LDG, cub::BLOCK_SCAN_WARP_SCANS>; +}; // Tuning for sm35 + +template +struct Tuning +{ + const static int INPUT_SIZE = sizeof(T); + enum + { + NOMINAL_4B_ITEMS_PER_THREAD = 7, + // + ITEMS_PER_THREAD = items_per_thread::value + }; + + using type = + PtxPolicy<128, ITEMS_PER_THREAD, cub::BLOCK_LOAD_WARP_TRANSPOSE, cub::LOAD_DEFAULT, cub::BLOCK_SCAN_WARP_SCANS>; +}; // Tuning for sm30 + +template +struct UniqueAgent +{ + using item_type = typename iterator_traits::value_type; + + using ScanTileState = cub::ScanTileState; + + template + struct PtxPlan : Tuning::type + { + using tuning = Tuning; + + using ItemsLoadIt = typename core::LoadIterator::type; + + using BlockLoadItems = typename core::BlockLoad::type; + + using BlockDiscontinuityItems = cub::BlockDiscontinuity; + + using TilePrefixCallback = cub::TilePrefixCallbackOp, ScanTileState, Arch::ver>; + using BlockScan = cub::BlockScan; + + using shared_items_t = core::uninitialized_array; + + union TempStorage + { + struct ScanStorage + { + typename BlockScan::TempStorage scan; + typename TilePrefixCallback::TempStorage prefix; + typename BlockDiscontinuityItems::TempStorage discontinuity; + } scan_storage; + + typename BlockLoadItems::TempStorage load_items; + shared_items_t shared_items; + + }; // union TempStorage + }; // struct PtxPlan + + using ptx_plan = typename core::specialize_plan_msvc10_war::type::type; + + using ItemsLoadIt = typename ptx_plan::ItemsLoadIt; + using BlockLoadItems = typename ptx_plan::BlockLoadItems; + using BlockDiscontinuityItems = typename ptx_plan::BlockDiscontinuityItems; + using TilePrefixCallback = typename ptx_plan::TilePrefixCallback; + using BlockScan = typename ptx_plan::BlockScan; + using shared_items_t = typename ptx_plan::shared_items_t; + using TempStorage = typename ptx_plan::TempStorage; + + enum + { + BLOCK_THREADS = ptx_plan::BLOCK_THREADS, + ITEMS_PER_THREAD = ptx_plan::ITEMS_PER_THREAD, + ITEMS_PER_TILE = ptx_plan::ITEMS_PER_TILE + }; + + struct impl + { + //--------------------------------------------------------------------- + // Per-thread fields + //--------------------------------------------------------------------- + + TempStorage& temp_storage; + ScanTileState& tile_state; + ItemsLoadIt items_in; + ItemsOutputIt items_out; + cub::InequalityWrapper predicate; + Size num_items; + + //--------------------------------------------------------------------- + // Utility functions + //--------------------------------------------------------------------- + + THRUST_DEVICE_FUNCTION + shared_items_t& get_shared() + { + return temp_storage.shared_items; + } + + void THRUST_DEVICE_FUNCTION scatter( + item_type (&items)[ITEMS_PER_THREAD], + Size (&selection_flags)[ITEMS_PER_THREAD], + Size (&selection_indices)[ITEMS_PER_THREAD], + int /*num_tile_items*/, + int num_tile_selections, + Size num_selections_prefix, + Size /*num_selections*/) + { + using core::sync_threadblock; + +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + int local_scatter_offset = selection_indices[ITEM] - num_selections_prefix; + if (selection_flags[ITEM]) + { + get_shared()[local_scatter_offset] = items[ITEM]; + } + } + + sync_threadblock(); + + for (int item = threadIdx.x; item < num_tile_selections; item += BLOCK_THREADS) + { + items_out[num_selections_prefix + item] = get_shared()[item]; + } + + sync_threadblock(); + } + + //--------------------------------------------------------------------- + // Tile processing + //--------------------------------------------------------------------- + + template + Size THRUST_DEVICE_FUNCTION consume_tile_impl(int num_tile_items, int tile_idx, Size tile_base) + { + using core::sync_threadblock; + using core::uninitialized_array; + + item_type items_loc[ITEMS_PER_THREAD]; + Size selection_flags[ITEMS_PER_THREAD]; + Size selection_idx[ITEMS_PER_THREAD]; + + if (IS_LAST_TILE) + { + BlockLoadItems(temp_storage.load_items) + .Load(items_in + tile_base, items_loc, num_tile_items, *(items_in + tile_base)); + } + else + { + BlockLoadItems(temp_storage.load_items).Load(items_in + tile_base, items_loc); + } + + sync_threadblock(); + + if (IS_FIRST_TILE) + { + BlockDiscontinuityItems(temp_storage.scan_storage.discontinuity) + .FlagHeads(selection_flags, items_loc, predicate); + } + else + { + item_type tile_predecessor = items_in[tile_base - 1]; + BlockDiscontinuityItems(temp_storage.scan_storage.discontinuity) + .FlagHeads(selection_flags, items_loc, predicate, tile_predecessor); + } + +# pragma unroll + for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM) + { + // Set selection_flags for out-of-bounds items + if ((IS_LAST_TILE) && (Size(threadIdx.x * ITEMS_PER_THREAD) + ITEM >= num_tile_items)) + { + selection_flags[ITEM] = 1; + } + } + + sync_threadblock(); + + Size num_tile_selections = 0; + Size num_selections = 0; + Size num_selections_prefix = 0; + if (IS_FIRST_TILE) + { + BlockScan(temp_storage.scan_storage.scan).ExclusiveSum(selection_flags, selection_idx, num_tile_selections); + + if (threadIdx.x == 0) + { + // Update tile status if this is not the last tile + if (!IS_LAST_TILE) + { + tile_state.SetInclusive(0, num_tile_selections); + } + } + + // Do not count any out-of-bounds selections + if (IS_LAST_TILE) + { + int num_discount = ITEMS_PER_TILE - num_tile_items; + num_tile_selections -= num_discount; + } + num_selections = num_tile_selections; + } + else + { + TilePrefixCallback prefix_cb(tile_state, temp_storage.scan_storage.prefix, ::cuda::std::plus<>{}, tile_idx); + BlockScan(temp_storage.scan_storage.scan).ExclusiveSum(selection_flags, selection_idx, prefix_cb); + + num_selections = prefix_cb.GetInclusivePrefix(); + num_tile_selections = prefix_cb.GetBlockAggregate(); + num_selections_prefix = prefix_cb.GetExclusivePrefix(); + + if (IS_LAST_TILE) + { + int num_discount = ITEMS_PER_TILE - num_tile_items; + num_tile_selections -= num_discount; + num_selections -= num_discount; + } + } + + sync_threadblock(); + + scatter(items_loc, + selection_flags, + selection_idx, + num_tile_items, + num_tile_selections, + num_selections_prefix, + num_selections); + + return num_selections; + } + + template + Size THRUST_DEVICE_FUNCTION consume_tile(int num_tile_items, int tile_idx, Size tile_base) + { + if (tile_idx == 0) + { + return consume_tile_impl(num_tile_items, tile_idx, tile_base); + } + else + { + return consume_tile_impl(num_tile_items, tile_idx, tile_base); + } + } + + //--------------------------------------------------------------------- + // Constructor + //--------------------------------------------------------------------- + + THRUST_DEVICE_FUNCTION + impl(TempStorage& temp_storage_, + ScanTileState& tile_state_, + ItemsLoadIt items_in_, + ItemsOutputIt items_out_, + BinaryPred binary_pred_, + Size num_items_, + int num_tiles, + NumSelectedOutIt num_selected_out) + : temp_storage(temp_storage_) + , tile_state(tile_state_) + , items_in(items_in_) + , items_out(items_out_) + , predicate(binary_pred_) + , num_items(num_items_) + { + int tile_idx = blockIdx.x; + Size tile_base = tile_idx * ITEMS_PER_TILE; + + if (tile_idx < num_tiles - 1) + { + consume_tile(ITEMS_PER_TILE, tile_idx, tile_base); + } + else + { + int num_remaining = static_cast(num_items - tile_base); + Size num_selections = consume_tile(num_remaining, tile_idx, tile_base); + if (threadIdx.x == 0) + { + *num_selected_out = num_selections; + } + } + } + }; // struct impl + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY( + ItemsIt items_in, + ItemsOutputIt items_out, + BinaryPred binary_pred, + NumSelectedOutIt num_selected_out, + Size num_items, + ScanTileState tile_state, + int num_tiles, + char* shmem) + { + TempStorage& storage = *reinterpret_cast(shmem); + + impl(storage, + tile_state, + core::make_load_iterator(ptx_plan(), items_in), + items_out, + binary_pred, + num_items, + num_tiles, + num_selected_out); + } +}; // struct UniqueAgent + +template +struct InitAgent +{ + template + struct PtxPlan : PtxPolicy<128> + {}; + using ptx_plan = core::specialize_plan; + + //--------------------------------------------------------------------- + // Agent entry point + //--------------------------------------------------------------------- + + THRUST_AGENT_ENTRY(ScanTileState tile_state, Size num_tiles, NumSelectedIt num_selected_out, char* /*shmem*/) + { + tile_state.InitializeStatus(num_tiles); + if (blockIdx.x == 0 && threadIdx.x == 0) + { + *num_selected_out = 0; + } + } + +}; // struct InitAgent + +template +static cudaError_t THRUST_RUNTIME_FUNCTION doit_step( + void* d_temp_storage, + size_t& temp_storage_bytes, + ItemsInputIt items_in, + ItemsOutputIt items_out, + BinaryPred binary_pred, + NumSelectedOutIt num_selected_out, + Size num_items, + cudaStream_t stream) +{ + using core::AgentLauncher; + using core::AgentPlan; + using core::get_agent_plan; + + using unique_agent = AgentLauncher>; + + using ScanTileState = typename unique_agent::ScanTileState; + + using init_agent = AgentLauncher>; + + using core::get_plan; + typename get_plan::type init_plan = init_agent::get_plan(); + typename get_plan::type unique_plan = unique_agent::get_plan(stream); + + int tile_size = unique_plan.items_per_tile; + size_t num_tiles = ::cuda::ceil_div(num_items, tile_size); + + size_t vshmem_size = core::vshmem_size(unique_plan.shared_memory_size, num_tiles); + + cudaError_t status = cudaSuccess; + size_t allocation_sizes[2] = {0, vshmem_size}; + status = ScanTileState::AllocationSize(static_cast(num_tiles), allocation_sizes[0]); + CUDA_CUB_RET_IF_FAIL(status); + + void* allocations[2] = {nullptr, nullptr}; + // + status = cub::AliasTemporaries(d_temp_storage, temp_storage_bytes, allocations, allocation_sizes); + CUDA_CUB_RET_IF_FAIL(status); + + if (d_temp_storage == nullptr) + { + return status; + } + + ScanTileState tile_status; + status = tile_status.Init(static_cast(num_tiles), allocations[0], allocation_sizes[0]); + CUDA_CUB_RET_IF_FAIL(status); + + num_tiles = max(1, num_tiles); + init_agent ia(init_plan, num_tiles, stream, "unique_by_key::init_agent"); + ia.launch(tile_status, num_tiles, num_selected_out); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + + if (num_items == 0) + { + return status; + } + + char* vshmem_ptr = vshmem_size > 0 ? (char*) allocations[1] : nullptr; + + unique_agent ua(unique_plan, num_items, stream, vshmem_ptr, "unique_by_key::unique_agent"); + ua.launch(items_in, items_out, binary_pred, num_selected_out, num_items, tile_status, num_tiles); + CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); + return status; +} + +template +THRUST_RUNTIME_FUNCTION ItemsOutputIt unique( + execution_policy& policy, + ItemsInputIt items_first, + ItemsInputIt items_last, + ItemsOutputIt items_result, + BinaryPred binary_pred) +{ + // using size_type = typename iterator_traits::difference_type; + using size_type = int; + + size_type num_items = static_cast(thrust::distance(items_first, items_last)); + size_t temp_storage_bytes = 0; + cudaStream_t stream = cuda_cub::stream(policy); + + cudaError_t status; + status = doit_step( + nullptr, + temp_storage_bytes, + items_first, + items_result, + binary_pred, + static_cast(nullptr), + num_items, + stream); + cuda_cub::throw_on_error(status, "unique: failed on 1st step"); + + size_t allocation_sizes[2] = {sizeof(size_type), temp_storage_bytes}; + void* allocations[2] = {nullptr, nullptr}; + + size_t storage_size = 0; + status = core::alias_storage(nullptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "unique: failed on 1st step"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, storage_size); + void* ptr = static_cast(tmp.data().get()); + + status = core::alias_storage(ptr, storage_size, allocations, allocation_sizes); + cuda_cub::throw_on_error(status, "unique: failed on 2nd step"); + + size_type* d_num_selected_out = thrust::detail::aligned_reinterpret_cast(allocations[0]); + + status = doit_step( + allocations[1], temp_storage_bytes, items_first, items_result, binary_pred, d_num_selected_out, num_items, stream); + cuda_cub::throw_on_error(status, "unique: failed on 2nd step"); + + status = cuda_cub::synchronize(policy); + cuda_cub::throw_on_error(status, "unique: failed to synchronize"); + + size_type num_selected = get_value(policy, d_num_selected_out); + + return items_result + num_selected; +} +} // namespace __unique + +//------------------------- +// Thrust API entry points +//------------------------- + +_CCCL_EXEC_CHECK_DISABLE +template +OutputIt _CCCL_HOST_DEVICE +unique_copy(execution_policy& policy, InputIt first, InputIt last, OutputIt result, BinaryPred binary_pred) +{ + THRUST_CDP_DISPATCH( + (result = __unique::unique(policy, first, last, result, binary_pred);), + (result = thrust::unique_copy(cvt_to_seq(derived_cast(policy)), first, last, result, binary_pred);)); + return result; +} + +template +OutputIt _CCCL_HOST_DEVICE unique_copy(execution_policy& policy, InputIt first, InputIt last, OutputIt result) +{ + using input_type = typename iterator_traits::value_type; + return cuda_cub::unique_copy(policy, first, last, result, equal_to()); +} + +_CCCL_EXEC_CHECK_DISABLE +template +ForwardIt _CCCL_HOST_DEVICE +unique(execution_policy& policy, ForwardIt first, ForwardIt last, BinaryPred binary_pred) +{ + ForwardIt ret = first; + THRUST_CDP_DISPATCH((ret = cuda_cub::unique_copy(policy, first, last, first, binary_pred);), + (ret = thrust::unique(cvt_to_seq(derived_cast(policy)), first, last, binary_pred);)); + return ret; +} + +template +ForwardIt _CCCL_HOST_DEVICE unique(execution_policy& policy, ForwardIt first, ForwardIt last) +{ + using input_type = typename iterator_traits::value_type; + return cuda_cub::unique(policy, first, last, equal_to()); +} + +template +struct zip_adj_not_predicate +{ + template + bool _CCCL_HOST_DEVICE operator()(TupleType&& tuple) + { + return !binary_pred(thrust::get<0>(tuple), thrust::get<1>(tuple)); + } + + BinaryPred binary_pred; +}; + +_CCCL_EXEC_CHECK_DISABLE +template +typename thrust::iterator_traits::difference_type _CCCL_HOST_DEVICE +unique_count(execution_policy& policy, ForwardIt first, ForwardIt last, BinaryPred binary_pred) +{ + if (first == last) + { + return 0; + } + auto size = thrust::distance(first, last); + auto it = thrust::make_zip_iterator(thrust::make_tuple(first, thrust::next(first))); + return 1 + thrust::count_if(policy, it, thrust::next(it, size - 1), zip_adj_not_predicate{binary_pred}); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +// +# include +# include +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/unique_by_key.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/unique_by_key.h new file mode 100644 index 0000000000000000000000000000000000000000..5b88b9935b33a4ffd24142aca0c538c7abf120b0 --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/unique_by_key.h @@ -0,0 +1,312 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#if _CCCL_HAS_CUDA_COMPILER + +# include + +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include + +THRUST_NAMESPACE_BEGIN + +template +_CCCL_HOST_DEVICE thrust::pair unique_by_key( + const thrust::detail::execution_policy_base& exec, + ForwardIterator1 keys_first, + ForwardIterator1 keys_last, + ForwardIterator2 values_first); +template +_CCCL_HOST_DEVICE thrust::pair unique_by_key_copy( + const thrust::detail::execution_policy_base& exec, + InputIterator1 keys_first, + InputIterator1 keys_last, + InputIterator2 values_first, + OutputIterator1 keys_result, + OutputIterator2 values_result); + +namespace cuda_cub +{ + +namespace detail +{ + +template +struct DispatchUniqueByKey +{ + static cudaError_t THRUST_RUNTIME_FUNCTION dispatch( + execution_policy& policy, + void* d_temp_storage, + size_t& temp_storage_bytes, + KeyInputIt keys_in, + ValInputIt values_in, + KeyOutputIt keys_out, + ValOutputIt values_out, + OffsetT num_items, + BinaryPred binary_pred, + pair& result_end) + { + cudaError_t status = cudaSuccess; + cudaStream_t stream = cuda_cub::stream(policy); + size_t allocation_sizes[2] = {0, sizeof(OffsetT)}; + void* allocations[2] = {nullptr, nullptr}; + + // Query algorithm memory requirements + status = cub::DeviceSelect::UniqueByKey( + nullptr, + allocation_sizes[0], + keys_in, + values_in, + keys_out, + values_out, + static_cast(nullptr), + num_items, + binary_pred, + stream); + CUDA_CUB_RET_IF_FAIL(status); + + status = cub::AliasTemporaries(d_temp_storage, temp_storage_bytes, allocations, allocation_sizes); + CUDA_CUB_RET_IF_FAIL(status); + + // Return if we're only querying temporary storage requirements + if (d_temp_storage == nullptr) + { + return status; + } + + // Return for empty problems + if (num_items == 0) + { + result_end = thrust::make_pair(keys_out, values_out); + return status; + } + + // Memory allocation for the number of selected output items + OffsetT* d_num_selected_out = thrust::detail::aligned_reinterpret_cast(allocations[1]); + + // Run algorithm + status = cub::DeviceSelect::UniqueByKey( + allocations[0], + allocation_sizes[0], + keys_in, + values_in, + keys_out, + values_out, + d_num_selected_out, + num_items, + binary_pred, + stream); + CUDA_CUB_RET_IF_FAIL(status); + + // Get number of selected items + status = cuda_cub::synchronize(policy); + CUDA_CUB_RET_IF_FAIL(status); + OffsetT num_selected = get_value(policy, d_num_selected_out); + + result_end = thrust::make_pair(keys_out + num_selected, values_out + num_selected); + return status; + } +}; + +template +THRUST_RUNTIME_FUNCTION pair unique_by_key( + execution_policy& policy, + KeyInputIt keys_first, + KeyInputIt keys_last, + ValInputIt values_first, + KeyOutputIt keys_result, + ValOutputIt values_result, + BinaryPred binary_pred) +{ + using size_type = typename iterator_traits::difference_type; + + size_type num_items = static_cast(thrust::distance(keys_first, keys_last)); + pair result_end{}; + cudaError_t status = cudaSuccess; + size_t temp_storage_bytes = 0; + + // 32-bit offset-type dispatch + using dispatch32_t = + DispatchUniqueByKey; + + // 64-bit offset-type dispatch + using dispatch64_t = + DispatchUniqueByKey; + + // Query temporary storage requirements + THRUST_INDEX_TYPE_DISPATCH2( + status, + dispatch32_t::dispatch, + dispatch64_t::dispatch, + num_items, + (policy, + nullptr, + temp_storage_bytes, + keys_first, + values_first, + keys_result, + values_result, + num_items_fixed, + binary_pred, + result_end)); + cuda_cub::throw_on_error(status, "unique_by_key: failed on 1st step"); + + // Allocate temporary storage. + thrust::detail::temporary_array tmp(policy, temp_storage_bytes); + void* temp_storage = static_cast(tmp.data().get()); + + // Run algorithm + THRUST_INDEX_TYPE_DISPATCH2( + status, + dispatch32_t::dispatch, + dispatch64_t::dispatch, + num_items, + (policy, + temp_storage, + temp_storage_bytes, + keys_first, + values_first, + keys_result, + values_result, + num_items_fixed, + binary_pred, + result_end)); + cuda_cub::throw_on_error(status, "unique_by_key: failed on 2nd step"); + + return result_end; +} + +} // namespace detail + +//------------------------- +// Thrust API entry points +//------------------------- +_CCCL_EXEC_CHECK_DISABLE +template +pair _CCCL_HOST_DEVICE unique_by_key_copy( + execution_policy& policy, + KeyInputIt keys_first, + KeyInputIt keys_last, + ValInputIt values_first, + KeyOutputIt keys_result, + ValOutputIt values_result, + BinaryPred binary_pred) +{ + auto ret = thrust::make_pair(keys_result, values_result); + THRUST_CDP_DISPATCH( + (ret = detail::unique_by_key(policy, keys_first, keys_last, values_first, keys_result, values_result, binary_pred);), + (ret = thrust::unique_by_key_copy( + cvt_to_seq(derived_cast(policy)), keys_first, keys_last, values_first, keys_result, values_result, binary_pred);)); + return ret; +} + +template +pair _CCCL_HOST_DEVICE unique_by_key_copy( + execution_policy& policy, + KeyInputIt keys_first, + KeyInputIt keys_last, + ValInputIt values_first, + KeyOutputIt keys_result, + ValOutputIt values_result) +{ + using key_type = typename iterator_traits::value_type; + return cuda_cub::unique_by_key_copy( + policy, keys_first, keys_last, values_first, keys_result, values_result, equal_to()); +} + +template +pair _CCCL_HOST_DEVICE unique_by_key( + execution_policy& policy, + KeyInputIt keys_first, + KeyInputIt keys_last, + ValInputIt values_first, + BinaryPred binary_pred) +{ + auto ret = thrust::make_pair(keys_first, values_first); + THRUST_CDP_DISPATCH( + (ret = cuda_cub::unique_by_key_copy( + policy, keys_first, keys_last, values_first, keys_first, values_first, binary_pred);), + (ret = thrust::unique_by_key(cvt_to_seq(derived_cast(policy)), keys_first, keys_last, values_first, binary_pred);)); + return ret; +} + +template +pair _CCCL_HOST_DEVICE +unique_by_key(execution_policy& policy, KeyInputIt keys_first, KeyInputIt keys_last, ValInputIt values_first) +{ + using key_type = typename iterator_traits::value_type; + return cuda_cub::unique_by_key(policy, keys_first, keys_last, values_first, equal_to()); +} + +} // namespace cuda_cub +THRUST_NAMESPACE_END + +# include +# include + +#endif diff --git a/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/util.h b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/util.h new file mode 100644 index 0000000000000000000000000000000000000000..27f0e1af2885467c4ca7e6bb546eaae2da1ba96d --- /dev/null +++ b/vllm/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/thrust/thrust/system/cuda/detail/util.h @@ -0,0 +1,596 @@ +/****************************************************************************** + * Copyright (c) 2016, NVIDIA CORPORATION. All rights meserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the NVIDIA CORPORATION nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ******************************************************************************/ +#pragma once + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include + +THRUST_NAMESPACE_BEGIN +namespace cuda_cub +{ + +inline _CCCL_HOST_DEVICE cudaStream_t default_stream() +{ +#ifdef CUDA_API_PER_THREAD_DEFAULT_STREAM + return cudaStreamPerThread; +#else + return cudaStreamLegacy; +#endif +} + +// Fallback implementation of the customization point. +template +_CCCL_HOST_DEVICE cudaStream_t get_stream(execution_policy&) +{ + return default_stream(); +} + +// Entry point/interface. +template +_CCCL_HOST_DEVICE cudaStream_t stream(execution_policy& policy) +{ + return get_stream(derived_cast(policy)); +} + +// Fallback implementation of the customization point. +template +_CCCL_HOST_DEVICE bool must_perform_optional_stream_synchronization(execution_policy&) +{ + return true; +} + +// Entry point/interface. +template +_CCCL_HOST_DEVICE bool must_perform_optional_synchronization(execution_policy& policy) +{ + return must_perform_optional_stream_synchronization(derived_cast(policy)); +} + +// Fallback implementation of the customization point. +_CCCL_EXEC_CHECK_DISABLE +template +_CCCL_HOST_DEVICE cudaError_t synchronize_stream(execution_policy& policy) +{ + return cub::SyncStream(stream(policy)); +} + +// Entry point/interface. +template +_CCCL_HOST_DEVICE cudaError_t synchronize(Policy& policy) +{ + return synchronize_stream(derived_cast(policy)); +} + +// Fallback implementation of the customization point. +_CCCL_EXEC_CHECK_DISABLE +template +_CCCL_HOST_DEVICE cudaError_t synchronize_stream_optional(execution_policy& policy) +{ + cudaError_t result; + + if (must_perform_optional_synchronization(policy)) + { + result = synchronize_stream(policy); + } + else + { + result = cudaSuccess; + } + + return result; +} + +// Entry point/interface. +template +_CCCL_HOST_DEVICE cudaError_t synchronize_optional(Policy& policy) +{ + return synchronize_stream_optional(derived_cast(policy)); +} + +template +THRUST_HOST_FUNCTION cudaError_t trivial_copy_from_device(Type* dst, Type const* src, size_t count, cudaStream_t stream) +{ + cudaError status = cudaSuccess; + if (count == 0) + { + return status; + } + + status = ::cudaMemcpyAsync(dst, src, sizeof(Type) * count, cudaMemcpyDeviceToHost, stream); + cudaStreamSynchronize(stream); + return status; +} + +template +THRUST_HOST_FUNCTION cudaError_t trivial_copy_to_device(Type* dst, Type const* src, size_t count, cudaStream_t stream) +{ + cudaError status = cudaSuccess; + if (count == 0) + { + return status; + } + + status = ::cudaMemcpyAsync(dst, src, sizeof(Type) * count, cudaMemcpyHostToDevice, stream); + cudaStreamSynchronize(stream); + return status; +} + +template +THRUST_RUNTIME_FUNCTION cudaError_t +trivial_copy_device_to_device(Policy& policy, Type* dst, Type const* src, size_t count) +{ + cudaError_t status = cudaSuccess; + if (count == 0) + { + return status; + } + + cudaStream_t stream = cuda_cub::stream(policy); + // + status = ::cudaMemcpyAsync(dst, src, sizeof(Type) * count, cudaMemcpyDeviceToDevice, stream); + cuda_cub::synchronize_optional(policy); + return status; +} + +CCCL_DEPRECATED_BECAUSE("Use cuda::std::terminate() instead") inline void _CCCL_HOST_DEVICE terminate() +{ + NV_IF_TARGET(NV_IS_HOST, (std::terminate();), (asm("trap;");)); +} + +_CCCL_HOST_DEVICE inline void throw_on_error(cudaError_t status) +{ + // Clear the global CUDA error state which may have been set by the last + // call. Otherwise, errors may "leak" to unrelated kernel launches. +#ifdef THRUST_RDC_ENABLED + cudaGetLastError(); +#else + NV_IF_TARGET(NV_IS_HOST, (cudaGetLastError();)); +#endif + + if (cudaSuccess != status) + { + // Can't use #if inside NV_IF_TARGET, use a temp macro to hoist the device + // instructions out of the target logic. +#ifdef THRUST_RDC_ENABLED + +# define THRUST_TEMP_DEVICE_CODE \ + printf("Thrust CUDA backend error: %s: %s\n", cudaGetErrorName(status), cudaGetErrorString(status)) + +#else + +# define THRUST_TEMP_DEVICE_CODE printf("Thrust CUDA backend error: %d\n", static_cast(status)) + +#endif + + NV_IF_TARGET(NV_IS_HOST, + (throw thrust::system_error(status, thrust::cuda_category());), + (THRUST_TEMP_DEVICE_CODE; ::cuda::std::terminate();)); + +#undef THRUST_TEMP_DEVICE_CODE + } +} + +_CCCL_HOST_DEVICE inline void throw_on_error(cudaError_t status, char const* msg) +{ + // Clear the global CUDA error state which may have been set by the last + // call. Otherwise, errors may "leak" to unrelated kernel launches. +#ifdef THRUST_RDC_ENABLED + cudaGetLastError(); +#else + NV_IF_TARGET(NV_IS_HOST, (cudaGetLastError();)); +#endif + + if (cudaSuccess != status) + { + // Can't use #if inside NV_IF_TARGET, use a temp macro to hoist the device + // instructions out of the target logic. +#ifdef THRUST_RDC_ENABLED + +# define THRUST_TEMP_DEVICE_CODE \ + printf("Thrust CUDA backend error: %s: %s: %s\n", cudaGetErrorName(status), cudaGetErrorString(status), msg) + +#else + +# define THRUST_TEMP_DEVICE_CODE printf("Thrust CUDA backend error: %d: %s\n", static_cast(status), msg) + +#endif + + NV_IF_TARGET(NV_IS_HOST, + (throw thrust::system_error(status, thrust::cuda_category(), msg);), + (THRUST_TEMP_DEVICE_CODE; ::cuda::std::terminate();)); + +#undef THRUST_TEMP_DEVICE_CODE + } +} + +// deprecated [Since 2.8] +template +struct CCCL_DEPRECATED_BECAUSE("Use thrust::transform_iterator") transform_input_iterator_t +{ + _CCCL_SUPPRESS_DEPRECATED_PUSH + using self_t = transform_input_iterator_t; + _CCCL_SUPPRESS_DEPRECATED_POP + using difference_type = typename iterator_traits::difference_type; + using value_type = ValueType; + using pointer = void; + using reference = value_type; + using iterator_category = std::random_access_iterator_tag; + + InputIt input; + mutable UnaryOp op; + + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE transform_input_iterator_t(InputIt input, UnaryOp op) + : input(input) + , op(op) + {} + + transform_input_iterator_t(const self_t&) = default; + + // UnaryOp might not be copy assignable, such as when it is a lambda. Define + // an explicit copy assignment operator that doesn't try to assign it. + _CCCL_HOST_DEVICE self_t& operator=(const self_t& o) + { + input = o.input; + return *this; + } + + /// Postfix increment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator++(int) + { + self_t retval = *this; + ++input; + return retval; + } + + /// Prefix increment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator++() + { + ++input; + return *this; + } + + /// Indirection + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator*() const + { + typename thrust::iterator_value::type x = *input; + return op(x); + } + /// Indirection + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator*() + { + typename thrust::iterator_value::type x = *input; + return op(x); + } + + /// Addition + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator+(difference_type n) const + { + return self_t(input + n, op); + } + + /// Addition assignment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t& operator+=(difference_type n) + { + input += n; + return *this; + } + + /// Subtraction + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator-(difference_type n) const + { + return self_t(input - n, op); + } + + /// Subtraction assignment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t& operator-=(difference_type n) + { + input -= n; + return *this; + } + + /// Distance + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE difference_type operator-(self_t other) const + { + return input - other.input; + } + + /// Array subscript + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator[](difference_type n) const + { + return op(input[n]); + } + + /// Equal to + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE bool operator==(const self_t& rhs) const + { + return (input == rhs.input); + } + + /// Not equal to + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE bool operator!=(const self_t& rhs) const + { + return (input != rhs.input); + } +}; // struct transform_input_iterarot_t + +// deprecated [Since 2.8] +template +struct CCCL_DEPRECATED_BECAUSE("Use thrust::transform_iterator of a thrust::zip_iterator") + transform_pair_of_input_iterators_t +{ + _CCCL_SUPPRESS_DEPRECATED_PUSH + using self_t = transform_pair_of_input_iterators_t; + _CCCL_SUPPRESS_DEPRECATED_POP + using difference_type = typename iterator_traits::difference_type; + using value_type = ValueType; + using pointer = void; + using reference = value_type; + using iterator_category = std::random_access_iterator_tag; + + InputIt1 input1; + InputIt2 input2; + mutable BinaryOp op; + + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE + transform_pair_of_input_iterators_t(InputIt1 input1_, InputIt2 input2_, BinaryOp op_) + : input1(input1_) + , input2(input2_) + , op(op_) + {} + + transform_pair_of_input_iterators_t(const self_t&) = default; + + // BinaryOp might not be copy assignable, such as when it is a lambda. + // Define an explicit copy assignment operator that doesn't try to assign it. + _CCCL_HOST_DEVICE self_t& operator=(const self_t& o) + { + input1 = o.input1; + input2 = o.input2; + return *this; + } + + /// Postfix increment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator++(int) + { + self_t retval = *this; + ++input1; + ++input2; + return retval; + } + + /// Prefix increment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator++() + { + ++input1; + ++input2; + return *this; + } + + /// Indirection + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator*() const + { + return op(*input1, *input2); + } + /// Indirection + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator*() + { + return op(*input1, *input2); + } + + /// Addition + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator+(difference_type n) const + { + return self_t(input1 + n, input2 + n, op); + } + + /// Addition assignment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t& operator+=(difference_type n) + { + input1 += n; + input2 += n; + return *this; + } + + /// Subtraction + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator-(difference_type n) const + { + return self_t(input1 - n, input2 - n, op); + } + + /// Subtraction assignment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t& operator-=(difference_type n) + { + input1 -= n; + input2 -= n; + return *this; + } + + /// Distance + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE difference_type operator-(self_t other) const + { + return input1 - other.input1; + } + + /// Array subscript + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator[](difference_type n) const + { + return op(input1[n], input2[n]); + } + + /// Equal to + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE bool operator==(const self_t& rhs) const + { + return (input1 == rhs.input1) && (input2 == rhs.input2); + } + + /// Not equal to + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE bool operator!=(const self_t& rhs) const + { + return (input1 != rhs.input1) || (input2 != rhs.input2); + } + +}; // struct transform_pair_of_input_iterators_t + +// deprecated [Since 2.8] +struct CCCL_DEPRECATED_BECAUSE("Use cuda::std::identity") identity +{ + template + _CCCL_HOST_DEVICE T const& operator()(T const& t) const + { + return t; + } + + template + _CCCL_HOST_DEVICE T& operator()(T& t) const + { + return t; + } +}; + +// deprecated [Since 2.8] +template +struct CCCL_DEPRECATED_BECAUSE("Use thrust::counting_iterator") counting_iterator_t +{ + _CCCL_SUPPRESS_DEPRECATED_PUSH + using self_t = counting_iterator_t; + _CCCL_SUPPRESS_DEPRECATED_POP + using difference_type = T; + using value_type = T; + using pointer = void; + using reference = T; + using iterator_category = std::random_access_iterator_tag; + + T count; + + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE counting_iterator_t(T count_) + : count(count_) + {} + + /// Postfix increment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator++(int) + { + self_t retval = *this; + ++count; + return retval; + } + + /// Prefix increment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator++() + { + ++count; + return *this; + } + + /// Indirection + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator*() const + { + return count; + } + + /// Indirection + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator*() + { + return count; + } + + /// Addition + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator+(difference_type n) const + { + return self_t(count + n); + } + + /// Addition assignment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t& operator+=(difference_type n) + { + count += n; + return *this; + } + + /// Subtraction + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t operator-(difference_type n) const + { + return self_t(count - n); + } + + /// Subtraction assignment + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE self_t& operator-=(difference_type n) + { + count -= n; + return *this; + } + + /// Distance + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE difference_type operator-(self_t other) const + { + return count - other.count; + } + + /// Array subscript + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE reference operator[](difference_type n) const + { + return count + n; + } + + /// Equal to + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE bool operator==(const self_t& rhs) const + { + return (count == rhs.count); + } + + /// Not equal to + _CCCL_HOST_DEVICE _CCCL_FORCEINLINE bool operator!=(const self_t& rhs) const + { + return (count != rhs.count); + } + +}; // struct count_iterator_t + +} // namespace cuda_cub + +THRUST_NAMESPACE_END