This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Add tensor padding modes #514
Merged
Merged
Changes from 1 commit
Commits
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 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 |
|---|---|---|
|
|
@@ -768,34 +768,66 @@ extension Tensor where Scalar: TensorFlowFloatingPoint { | |
| // Padding | ||
| //===------------------------------------------------------------------------------------------===// | ||
|
|
||
|
|
||
| public extension Tensor where Scalar: Numeric { | ||
| /// Returns a padded tensor according to the specified padding sizes. | ||
| /// Padding modes: | ||
| /// * `constant` - pad with constant value. | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// * `reflect` - mirror values along padding dimensions, excluding the edge value. | ||
| /// * `symmetric` - mirror values along padding dimensions, including the edge value. | ||
| enum PaddingMode { | ||
| case constant(Scalar) | ||
| case reflect | ||
| case symmetric | ||
| } | ||
|
|
||
| /// Returns a tensor padded with constant according to the specified padding sizes. | ||
| @inlinable | ||
| @differentiable(wrt: self, vjp: _vjpPadded(forSizes:with:) where Scalar: TensorFlowFloatingPoint) | ||
| @differentiable(wrt: self where Scalar: TensorFlowFloatingPoint) | ||
| func padded(forSizes sizes: [(before: Int, after: Int)], with value: Scalar = 0) -> Tensor { | ||
| return self.padded(forSizes: sizes, with: .constant(value)) | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Returns a padded tensor according to the specified padding sizes and mode. | ||
| @inlinable | ||
| @differentiable(wrt: self, vjp: _vjpPaddedWithMode(forSizes:with:) where Scalar: TensorFlowFloatingPoint) | ||
| func padded(forSizes sizes: [(before: Int, after: Int)], with mode: PaddingMode) -> Tensor { | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let paddings = Tensor<Int32>( | ||
| shape: [sizes.count, 2], | ||
| scalars: sizes.flatMap { [Int32($0.before), Int32($0.after)] }) | ||
| return Raw.padV2(self, paddings: paddings, constantValues: Tensor(value)) | ||
| switch mode { | ||
| case .constant(let constantValue): | ||
| return Raw.padV2(self, paddings: paddings, constantValues: Tensor(constantValue)) | ||
| case .reflect: | ||
| return Raw.mirrorPad(self, paddings: paddings, mode: Raw.Mode5.reflect) | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case .symmetric: | ||
| return Raw.mirrorPad(self, paddings: paddings, mode: Raw.Mode5.symmetric) | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| internal extension Tensor where Scalar: TensorFlowFloatingPoint { | ||
| @inlinable | ||
| func _vjpPadded( | ||
| func _vjpPaddedWithMode( | ||
|
||
| forSizes sizes: [(before: Int, after: Int)], | ||
| with value: Scalar | ||
| with mode: PaddingMode | ||
| ) -> (Tensor, (Tensor) -> Tensor) { | ||
| let result = padded(forSizes: sizes, with: value) | ||
| let result = padded(forSizes: sizes, with: mode) | ||
| return (result, { [rank = rankTensor, shape = shapeTensor] v in | ||
| let paddings = Tensor<Int32>( | ||
| shape: [sizes.count, 2], | ||
| scalars: sizes.flatMap { [Int32($0.before), Int32($0.after)] }) | ||
| let padBefore = Raw.slice(paddings, | ||
| begin: Tensor<Int32>([0, 0]), | ||
| size: Tensor<Int32>(stacking: [rank, Tensor<Int32>(1)])) | ||
| let begin = padBefore.reshaped(to: [-1]) | ||
| return Raw.slice(v, begin: begin, size: shape) | ||
| switch mode { | ||
| case .constant(_): | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let padBefore = Raw.slice(paddings, | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| begin: Tensor<Int32>([0, 0]), | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| size: Tensor<Int32>(stacking: [rank, Tensor<Int32>(1)])) | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let begin = padBefore.reshaped(to: [-1]) | ||
| return Raw.slice(v, begin: begin, size: shape) | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case .reflect: | ||
| return Raw.mirrorPadGrad(v, paddings: paddings, mode: Raw.Mode5.reflect) | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case .symmetric: | ||
| return Raw.mirrorPadGrad(v, paddings: paddings, mode: Raw.Mode5.symmetric) | ||
vvmnnnkv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
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
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.