Open
Conversation
Added BFloat16 case to PrintValue() and ToCSharpString() so that bfloat16 (and float16) tensors can be printed in all string styles.
Add test coverage for the bfloat16/float16 printing fix: - TestBFloat16ScalarToString: scalar tensor in jlstr, npstr, cstr formats - TestBFloat16TensorToString: 1-D and 2-D tensors in numpy, julia, csharp formats plus print() - TestFloat16TensorToString: 1-D tensors in csharp and numpy formats Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Implement a custom BFloat16 struct (2 bytes, binary-compatible with c10::BFloat16) with round-to-nearest-even conversion matching PyTorch. New features: - BFloat16 struct with arithmetic, comparison, and conversion operators - data<BFloat16>() and item<BFloat16>() for zero-copy tensor data access - torch.tensor(BFloat16[], ...) factory overloads for 1D-4D arrays - Scalar implicit conversion and ToBFloat16() extraction - ToBFloat16() tensor extension method - ToTensor<T> support for BFloat16 - as_tensor() BFloat16 overloads - frombuffer offset fix for BFloat16 and Float16 (2-byte types) Fixes dotnet#1469 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive support for the bfloat16 type to TorchSharp, fixing issue #1469 where bfloat16 tensors could not be printed. The implementation includes a new BFloat16 struct, tensor factory methods, conversion utilities, and extensive test coverage. Additionally, the PR fixes missing ToString support for Float16 types.
Changes:
- Introduced a new
BFloat16struct with IEEE 754-compliant conversion logic using round-to-nearest-even - Added tensor factory methods, Scalar conversions, and extension methods for BFloat16
- Fixed missing ToCSharpString and PrintValue handling for both BFloat16 and Float16 types
- Comprehensive test coverage including ToString, data accessors, round-trip conversions, and multidimensional tensor creation
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/TorchSharp/BFloat16.cs | New BFloat16 struct with conversion operators, arithmetic operators, and comparison operators |
| src/TorchSharp/Scalar.cs | Added BFloat16 implicit conversion and extension methods for ToScalar/ToBFloat16 |
| src/TorchSharp/Tensor/Tensor.cs | Updated ValidateType, ToCSharpString, PrintValue to handle BFloat16; added BFloat16 to type mapping; fixed Float16 ToString support |
| src/TorchSharp/Tensor/TensorExtensionMethods.cs | Added BFloat16 support to ToTensor generic methods and new ToBFloat16 extension |
| src/TorchSharp/Tensor/Factories/tensor_BFloat16.cs | Complete set of tensor factory methods for BFloat16 arrays and multidimensional arrays |
| src/TorchSharp/Tensor/Factories/as_tensor.cs | Added as_tensor overloads for BFloat16 arrays and IList |
| src/TorchSharp/Tensor/Factories/Tensor.Factories.cs | Added BFloat16 and Float16 to offset calculation in frombuffer |
| test/TorchSharpTest/TestTorchTensor.cs | Comprehensive tests for BFloat16 ToString, data access, round-trip conversion, tensor creation, struct operations, and Float16 ToString |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1469