Skip to content

[TF] Re-implement tensor shape/rank operators using TFE runtime APIs. #24949

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

Closed
wants to merge 1 commit into from
Closed
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 stdlib/public/TensorFlow/CompilerRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,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 stdlib/public/TensorFlow/Tensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,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 @@ -376,14 +379,26 @@ 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 {
return Int(scalarCountTensor.scalar!)
@_semantics("autodiff.nonvarying")
get {
let status = _ExecutionContext.global.status
let size = TFE_TensorHandleNumElements(handle._cTensorHandle, status)
checkOk(status)
return Int(size)
}
}
}

Expand Down