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

Commit 140fc41

Browse files
authored
Canonicalize the attribute list when printing a LazyTensorOperation (#223)
1 parent 036c471 commit 140fc41

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

Sources/TensorFlow/Core/LazyTensorOperation.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,26 +687,24 @@ extension LazyTensor: CustomStringConvertible {
687687

688688
extension LazyTensorOperation: CustomStringConvertible {
689689
public var description: String {
690-
let attrsDesc = attrs.map { (name, value) in "\(name): \(value)" }
691-
let inputsDesc = inputs.map { (input: Input) -> String in
690+
let attrsDesc = attrs.sorted(by: { $0.key < $1.key }).map { "\($0): \($1)" }
691+
let inputsDesc = inputs.map { input -> String in
692692
switch input {
693693
case Input.single(let lazyTensor):
694694
return "\(lazyTensor)"
695695
case Input.list(let lazyTensorList):
696-
do {
697-
let lazyTensors = lazyTensorList.map { "\($0)" }
698-
let lazyTensorsDesc = lazyTensors.joined(separator: ", ")
699-
return "[\(lazyTensorsDesc)]"
700-
}
696+
let lazyTensors = lazyTensorList.map { "\($0)" }
697+
let lazyTensorsDesc = lazyTensors.joined(separator: ", ")
698+
return "[\(lazyTensorsDesc)]"
701699
}
702700
}
703701
var desc = "\(outputName) = \(name)"
704-
if attrs.count > 0 {
702+
if !attrs.isEmpty {
705703
desc += "["
706704
desc += attrsDesc.joined(separator: ", ")
707705
desc += "]"
708706
}
709-
desc += "("
707+
desc += "("
710708
desc += inputsDesc.joined(separator: ", ")
711709
desc += ")"
712710
return desc

Tests/TensorFlowTests/LazyTensorOperationTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ final class LazyTensorOperationTests: XCTestCase {
166166
_id: "0", name: "Nop", outputCount: 1)
167167
op0.updateAttribute("b", true)
168168
op0.updateAttribute("s", "hello")
169-
XCTAssert(
170-
(op0.description == "%0 = Nop[b: true, s: \"hello\"]()") ||
171-
(op0.description == "%0 = Nop[s: \"hello\", b: true]()")
172-
)
169+
XCTAssertEqual(op0.description, "%0 = Nop[b: true, s: \"hello\"]()")
173170
}
174171

175172
func testFunctionAttribute() {

0 commit comments

Comments
 (0)