-
Notifications
You must be signed in to change notification settings - Fork 425
Add pytorch inspired DeviceTransform benchmark #9764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bernhardmgruber
wants to merge
14
commits into
NVIDIA:main
Choose a base branch
from
bernhardmgruber:pytorch_bench
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,222
−0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0cc3180
Add pytorch inspired benchmark
bernhardmgruber cc0b391
Drop compat math functions
bernhardmgruber 8774d98
nan
bernhardmgruber 52d3927
hardcode 64 bit offset and add const
bernhardmgruber ddc24aa
namesopace
bernhardmgruber ff737ec
Move bf16 to header
bernhardmgruber 84b09ac
Remove pow
bernhardmgruber 63f97a2
Document benchmark
bernhardmgruber 3cca58a
Document benchmark
bernhardmgruber 3275ba5
Replace more stuff by cuda
bernhardmgruber cdbb19f
Drop offset description
bernhardmgruber 0e5b684
Replace __bfloat16_as_ushort by reinterpret_cast
bernhardmgruber 6072088
Drop --expt-relaxed-constexpr
bernhardmgruber 704c500
Use round_to_nearest_even on host and below SM80
bernhardmgruber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
cub/benchmarks/bench/transform/applications/P1/bfloat16.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cuda_bf16.h> | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
|
|
||
| #include <nvbench/type_strings.cuh> | ||
|
|
||
| // ============================================================================ | ||
| // BFloat16 type — replicated from c10::BFloat16 | ||
| // (torch/headeronly/util/BFloat16.h) | ||
| // ============================================================================ | ||
|
|
||
| namespace bf16_detail | ||
| { | ||
| inline __host__ __device__ float f32_from_bits(uint16_t src) | ||
| { | ||
| float res = 0; | ||
| uint32_t tmp = src; | ||
| tmp <<= 16; | ||
| std::memcpy(&res, &tmp, sizeof(tmp)); | ||
| return res; | ||
| } | ||
|
|
||
| inline __host__ __device__ uint16_t round_to_nearest_even(float src) | ||
| { | ||
| if (std::isnan(src)) | ||
| { | ||
| return UINT16_C(0x7FC0); | ||
| } | ||
| else | ||
| { | ||
| uint32_t U32; | ||
| std::memcpy(&U32, &src, sizeof(U32)); | ||
| uint32_t rounding_bias = ((U32 >> 16) & 1) + UINT32_C(0x7FFF); | ||
| return static_cast<uint16_t>((U32 + rounding_bias) >> 16); | ||
| } | ||
| } | ||
| } // namespace bf16_detail | ||
|
|
||
| struct alignas(2) BFloat16 | ||
| { | ||
| uint16_t x; | ||
|
|
||
| BFloat16() = default; | ||
|
|
||
| struct from_bits_t | ||
| {}; | ||
| static constexpr __host__ __device__ from_bits_t from_bits() | ||
| { | ||
| return from_bits_t(); | ||
| } | ||
|
|
||
| constexpr __host__ __device__ BFloat16(unsigned short bits, from_bits_t) | ||
| : x(bits) | ||
| {} | ||
|
|
||
| /* implicit */ inline __host__ __device__ BFloat16(float value); | ||
| inline __host__ __device__ operator float() const; | ||
|
|
||
| inline __host__ __device__ BFloat16(const __nv_bfloat16& value); | ||
| explicit inline __host__ __device__ operator __nv_bfloat16() const; | ||
| }; | ||
|
|
||
| inline __host__ __device__ BFloat16::BFloat16(float value) | ||
| { | ||
| NV_IF_ELSE_TARGET(NV_PROVIDES_SM_80, | ||
| ({ | ||
| __nv_bfloat16 tmp = __float2bfloat16(value); | ||
| x = *reinterpret_cast<const unsigned short*>(&tmp); | ||
| }), | ||
| ({ x = bf16_detail::round_to_nearest_even(value); })); | ||
| } | ||
|
|
||
| inline __host__ __device__ BFloat16::operator float() const | ||
| { | ||
| return __bfloat162float(*reinterpret_cast<const __nv_bfloat16*>(&x)); | ||
| } | ||
|
|
||
| inline __host__ __device__ BFloat16::BFloat16(const __nv_bfloat16& value) | ||
| { | ||
| x = *reinterpret_cast<const unsigned short*>(&value); | ||
| } | ||
| inline __host__ __device__ BFloat16::operator __nv_bfloat16() const | ||
| { | ||
| return *reinterpret_cast<const __nv_bfloat16*>(&x); | ||
| } | ||
|
|
||
| // Arithmetic — BFloat16 x BFloat16 → BFloat16 | ||
| inline __host__ __device__ BFloat16 operator+(const BFloat16& a, const BFloat16& b) | ||
| { | ||
| return static_cast<float>(a) + static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator-(const BFloat16& a, const BFloat16& b) | ||
| { | ||
| return static_cast<float>(a) - static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator*(const BFloat16& a, const BFloat16& b) | ||
| { | ||
| return static_cast<float>(a) * static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator/(const BFloat16& a, const BFloat16& b) | ||
| { | ||
| return static_cast<float>(a) / static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator-(const BFloat16& a) | ||
| { | ||
| return -static_cast<float>(a); | ||
| } | ||
|
|
||
| // Compound assignment — BFloat16 | ||
| inline __host__ __device__ BFloat16& operator+=(BFloat16& a, const BFloat16& b) | ||
| { | ||
| a = a + b; | ||
| return a; | ||
| } | ||
| inline __host__ __device__ BFloat16& operator-=(BFloat16& a, const BFloat16& b) | ||
| { | ||
| a = a - b; | ||
| return a; | ||
| } | ||
| inline __host__ __device__ BFloat16& operator*=(BFloat16& a, const BFloat16& b) | ||
| { | ||
| a = a * b; | ||
| return a; | ||
| } | ||
| inline __host__ __device__ BFloat16& operator/=(BFloat16& a, const BFloat16& b) | ||
| { | ||
| a = a / b; | ||
| return a; | ||
| } | ||
|
|
||
| // Arithmetic — BFloat16 x float → float | ||
| inline __host__ __device__ float operator+(BFloat16 a, float b) | ||
| { | ||
| return static_cast<float>(a) + b; | ||
| } | ||
| inline __host__ __device__ float operator-(BFloat16 a, float b) | ||
| { | ||
| return static_cast<float>(a) - b; | ||
| } | ||
| inline __host__ __device__ float operator*(BFloat16 a, float b) | ||
| { | ||
| return static_cast<float>(a) * b; | ||
| } | ||
| inline __host__ __device__ float operator/(BFloat16 a, float b) | ||
| { | ||
| return static_cast<float>(a) / b; | ||
| } | ||
| inline __host__ __device__ float operator+(float a, BFloat16 b) | ||
| { | ||
| return a + static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ float operator-(float a, BFloat16 b) | ||
| { | ||
| return a - static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ float operator*(float a, BFloat16 b) | ||
| { | ||
| return a * static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ float operator/(float a, BFloat16 b) | ||
| { | ||
| return a / static_cast<float>(b); | ||
| } | ||
|
|
||
| // Compound assignment — float x BFloat16 → float | ||
| inline __host__ __device__ float& operator+=(float& a, const BFloat16& b) | ||
| { | ||
| return a += static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ float& operator-=(float& a, const BFloat16& b) | ||
| { | ||
| return a -= static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ float& operator*=(float& a, const BFloat16& b) | ||
| { | ||
| return a *= static_cast<float>(b); | ||
| } | ||
| inline __host__ __device__ float& operator/=(float& a, const BFloat16& b) | ||
| { | ||
| return a /= static_cast<float>(b); | ||
| } | ||
|
|
||
| // Arithmetic — BFloat16 x int → BFloat16 | ||
| inline __host__ __device__ BFloat16 operator+(BFloat16 a, int b) | ||
| { | ||
| return a + static_cast<BFloat16>(static_cast<float>(b)); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator-(BFloat16 a, int b) | ||
| { | ||
| return a - static_cast<BFloat16>(static_cast<float>(b)); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator*(BFloat16 a, int b) | ||
| { | ||
| return a * static_cast<BFloat16>(static_cast<float>(b)); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator/(BFloat16 a, int b) | ||
| { | ||
| return a / static_cast<BFloat16>(static_cast<float>(b)); | ||
| } | ||
| inline __host__ __device__ BFloat16 operator+(int a, BFloat16 b) | ||
| { | ||
| return static_cast<BFloat16>(static_cast<float>(a)) + b; | ||
| } | ||
| inline __host__ __device__ BFloat16 operator-(int a, BFloat16 b) | ||
| { | ||
| return static_cast<BFloat16>(static_cast<float>(a)) - b; | ||
| } | ||
| inline __host__ __device__ BFloat16 operator*(int a, BFloat16 b) | ||
| { | ||
| return static_cast<BFloat16>(static_cast<float>(a)) * b; | ||
| } | ||
| inline __host__ __device__ BFloat16 operator/(int a, BFloat16 b) | ||
| { | ||
| return static_cast<BFloat16>(static_cast<float>(a)) / b; | ||
| } | ||
|
|
||
| // Comparison — for std::min/std::max | ||
| inline __host__ __device__ bool operator>(BFloat16& lhs, BFloat16& rhs) | ||
| { | ||
| return float(lhs) > float(rhs); | ||
| } | ||
| inline __host__ __device__ bool operator<(BFloat16& lhs, BFloat16& rhs) | ||
| { | ||
| return float(lhs) < float(rhs); | ||
| } | ||
|
|
||
| // NVBench type registration | ||
| NVBENCH_DECLARE_TYPE_STRINGS(BFloat16, "bf16", "BFloat16"); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.