-
Notifications
You must be signed in to change notification settings - Fork 179
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
implementing fake_quantize_per_channel_affine #959
Draft
MovGP0
wants to merge
7
commits into
dotnet:main
Choose a base branch
from
MovGP0:main
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d9dece1
implementing fake_quantize_per_channel_affine
MovGP0 5aaa19f
cleaning AdjustGamma.cs
MovGP0 c2b3ce3
fixing array initializer
MovGP0 5f545f0
implementing fake_quantize_per_channel_affine
MovGP0 0489a2b
fixing unit test by implementing the example from the official docume…
MovGP0 60ccc5d
removing .vscode folder
MovGP0 8d3ee50
Merge branch 'main' into main
NiklasGustafsson 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information. | ||
#nullable enable | ||
using System; | ||
using static TorchSharp.PInvoke.LibTorchSharp; | ||
|
||
namespace TorchSharp | ||
{ | ||
public static partial class torch | ||
{ | ||
public partial class Tensor | ||
{ | ||
// https://pytorch.org/docs/stable/generated/torch.fake_quantize_per_channel_affine | ||
/// <summary> | ||
/// Returns a new tensor with the data in this fake quantized per channel using | ||
/// <paramref name="scale"/>, <paramref name="zero_point"/>, <paramref name="quant_min"/> and <paramref name="quant_max"/>, | ||
/// across the channel specified by <paramref name="axis"/>. | ||
/// </summary> | ||
/// <param name="scale">quantization scale, per channel (float32)</param> | ||
/// <param name="zero_point">quantization zero_point, per channel (torch.int32, torch.half, or torch.float32)</param> | ||
/// <param name="axis">channel axis</param> | ||
/// <param name="quant_min">lower bound of the quantized domain</param> | ||
/// <param name="quant_max">upper bound of the quantized domain</param> | ||
/// <returns>A newly fake_quantized per channel torch.float32 tensor</returns> | ||
public Tensor fake_quantize_per_channel_affine(Tensor scale, Tensor zero_point, long axis, long quant_min, long quant_max) | ||
{ | ||
var res = THSTensor_fake_quantize_per_channel_affine( | ||
Handle, scale.Handle, zero_point.handle, | ||
axis, quant_min, quant_max); | ||
|
||
if (res == IntPtr.Zero) | ||
CheckForErrors(); | ||
|
||
return new Tensor(res); | ||
} | ||
|
||
// see: aten/src/ATen/native/quantized/FakeQuantPerChannelAffine.cpp | ||
internal (Tensor res, Tensor mask) fake_quantize_per_channel_affine_cachemask(Tensor scale, Tensor zero_point, long axis, long quant_min, long quant_max) | ||
{ | ||
var res = THSTensor_fake_quantize_per_channel_affine_cachemask( | ||
Handle, scale.Handle, zero_point.handle, | ||
axis, quant_min, quant_max, out IntPtr mask); | ||
|
||
if (res == IntPtr.Zero || mask == IntPtr.Zero) | ||
CheckForErrors(); | ||
|
||
return (new Tensor(res), new Tensor(mask)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh? Is this just because the test is really just looking for an exception in the call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took this code from the official documentation. It's the minimal amount of testing.
Should test the result values, but would need to get rid of the random initializer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either use a Generator instance, or (I do this frequently) just create a tensor from the random values in the PyTorch doc and then the correct results should be right there in the documentation already...