Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Re-implement tensor shape/rank operators using TFE runtime APIs. #141

Merged
merged 1 commit into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/TensorFlow/Core/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public final class _ExecutionContext {
@usableFromInline let eagerContext: CTFEContext

/// The status for checking TensorFlow errors.
private let status: CTFStatus = TF_NewStatus()
@usableFromInline let status: CTFStatus = TF_NewStatus()

/// The mutex for preventing potential concurrent access.
private var mutex: pthread_mutex_t = pthread_mutex_t()
Expand Down
21 changes: 18 additions & 3 deletions Sources/TensorFlow/Core/Tensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import CTensorFlow

infix operator .==: ComparisonPrecedence
infix operator .!=: ComparisonPrecedence

Expand Down Expand Up @@ -50,7 +52,10 @@ public extension Tensor {
var rank: Int {
@_semantics("autodiff.nonvarying")
get {
return Int(rankTensor.scalar!)
let status = _ExecutionContext.global.status
let rank = TFE_TensorHandleNumDims(handle._cTensorHandle, status)
checkOk(status)
return Int(rank)
}
}

Expand All @@ -59,15 +64,25 @@ public extension Tensor {
var shape: TensorShape {
@_semantics("autodiff.nonvarying")
get {
return TensorShape(shapeTensor.scalars.map(Int.init))
let status = _ExecutionContext.global.status
let dims: [Int] = (0..<Int32(rank)).map { i in
let dim = TFE_TensorHandleDim(self.handle._cTensorHandle, i, status)
checkOk(status)
return Int(dim)
}
return TensorShape(dims)
}
}

/// The number of scalars in the `Tensor`.
@inlinable
var scalarCount: Int {
@_semantics("autodiff.nonvarying")
get {
return Int(scalarCountTensor.scalar!)
let status = _ExecutionContext.global.status
let size = TFE_TensorHandleNumElements(handle._cTensorHandle, status)
checkOk(status)
return Int(size)
}
}

Expand Down