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

Track the device name in LazyTensorOperation #262

Merged
merged 4 commits into from
Jun 19, 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: 2 additions & 0 deletions Sources/TensorFlow/Core/LazyTensorOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class LazyTensorOperation: TensorOperation {
let outputCount: Int
var inputs: [Input]
var attributes: [String: Attribute]
var deviceName: String?
var outputs: [TFETensorHandle]?
var id: String?

Expand Down Expand Up @@ -203,6 +204,7 @@ class LazyTensorOperation: TensorOperation {
self.name = name
self.inputs = []
self.attributes = [:]
self.deviceName = _ExecutionContext.global.currentDeviceName
self.outputCount = outputCount
self.outputs = nil
self.id = id
Expand Down
14 changes: 13 additions & 1 deletion Tests/TensorFlowTests/LazyTensorOperationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import XCTest
import CTensorFlow

final class LazyTensorOperationTests: XCTestCase {

func testNoInput() {
let placeholder = LazyTensorOperation(
_id: "V", name: "Placeholder", outputCount: 1)
Expand Down Expand Up @@ -204,6 +205,15 @@ final class LazyTensorOperationTests: XCTestCase {
XCTAssertEqual(op0.description, "%0 = Nop[fn: TFFunction(ExampleFunction)]()")
}

func testDeviceTracking() {
let op0 = LazyTensorOperation(_id: "0", name: "Nop", outputCount: 1)
XCTAssertEqual(op0.deviceName, nil)
withDevice(named: "/job:localhost/replica:0/task:0/device:CPU:0") {
let op1 = LazyTensorOperation(_id: "0", name: "Nop", outputCount: 1)
XCTAssertEqual(op1.deviceName ?? "", "/job:localhost/replica:0/task:0/device:CPU:0")
}
}

static var allTests = [
("testNoInput", testNoInput),
("testSingleInput", testSingleInput),
Expand All @@ -223,6 +233,8 @@ final class LazyTensorOperationTests: XCTestCase {
testOptionalTensorShapeArrayAttribute),
("testArrayAttributes", testArrayAttributes),
("testMultipleAttributes", testMultipleAttributes),
("testFunctionAttribute", testFunctionAttribute)
("testFunctionAttribute", testFunctionAttribute),
("testDeviceTracking", testDeviceTracking)

]
}