Skip to content

Conversation

realdoug
Copy link

@realdoug realdoug commented Apr 3, 2019

This makes TensorShape conform to PythonConvertible per TF-76.

Couple small notes:

  • It looks like we can essentially delegate to the implementation in Array
  • I noticed that there were no tests for Array and Dictionary's existing PythonConvertible conformance so I added those as well.

@realdoug realdoug changed the title [TF-76][TF API] add TensorShape conformance to PythonConvertible [TF-76][Python Interop] add TensorShape conformance to PythonConvertible Apr 3, 2019
@realdoug realdoug changed the title [TF-76][Python Interop] add TensorShape conformance to PythonConvertible [TF-76][Python Interop] add PythonConvertible conformance to TensorShape Apr 3, 2019

extension TensorShape: PythonConvertible {
public var pythonObject: PythonObject {
return self.dimensions.pythonObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our local convention is to omit self. unless it's necessary.

}

public init?(_ pythonObject: PythonObject) {
self.init(Array<Int32>(pythonObject)!)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use short hand type names when possible for array and dictionary types.
  2. This is a failable initializer that allows you to return nil. The use of ! is unsafe.
Suggested change
self.init(Array<Int32>(pythonObject)!)
guard let array = [Int32](pythonObject) else {
return nil
}
self.init(array)

@rxwei
Copy link
Contributor

rxwei commented Apr 3, 2019

Have you tested something like np.reshape(x, [1, 1])?

@rxwei
Copy link
Contributor

rxwei commented Apr 3, 2019

Thanks for tackling this!

@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Apr 4, 2019
@realdoug
Copy link
Author

realdoug commented Apr 4, 2019

@rxwei in your example is x a TensorShape instance? If so, that works as expected 👍

Just made the above updates as well.

@rxwei
Copy link
Contributor

rxwei commented Apr 4, 2019

‘x’ should be a numpy array.

@rxwei
Copy link
Contributor

rxwei commented Apr 4, 2019

Could you add numpy reshape tests to https://github.com/apple/swift/blob/tensorflow/test/Python/numpy_conversion.swift?

expectEqual(five, Double(5).pythonObject)

expectEqual(intArray, Array([2, 3]).pythonObject)
expectEqual(dict, Dictionary<String, Int32>(["abc": 7]).pythonObject)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expectEqual(dict, Dictionary<String, Int32>(["abc": 7]).pythonObject)
expectEqual(dict, ["abc" : 7].pythonObject)

expectEqual(five, Float(5).pythonObject)
expectEqual(five, Double(5).pythonObject)

expectEqual(intArray, Array([2, 3]).pythonObject)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array literal [2, 3] already resolves to an Array.

Suggested change
expectEqual(intArray, Array([2, 3]).pythonObject)
expectEqual(intArray, [2, 3].pythonObject)


expectEqual([2, 3], Array(intArray))
expectEqual(TensorShape(2, 3), TensorShape(intArray))
expectEqual(["abc": 97], Dictionary<String, Int32>(dict))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Swift core repo we put a space before :. This is a bit different from tensorflow/swift-apis.

Suggested change
expectEqual(["abc": 97], Dictionary<String, Int32>(dict))
expectEqual(["abc" : 97], Dictionary<String, Int32>(dict))

let half: PythonObject = 0.5
let string: PythonObject = "abc"
let intArray: PythonObject = [2, 3]
let dict: PythonObject = ["abc": 97]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let dict: PythonObject = ["abc": 97]
let dict: PythonObject = ["abc" : 97]

let minusOne: PythonObject = -1
let five: PythonObject = 5
let intArray: PythonObject = [2, 3]
let dict: PythonObject = ["abc": 7]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let dict: PythonObject = ["abc": 7]
let dict: PythonObject = ["abc" : 7]

@realdoug
Copy link
Author

realdoug commented Apr 5, 2019

@rxwei ok, I updated the syntax in all places in this file for consistency, verified that np.reshape works on the 0.2 release as well as this branch and added some tests for it.

EDIT: added in two more in TensorFlowRuntime/numpy_conversions.swift as well.

}

let numpyArray1D = np.ones(28)
let reshaped3D = np.reshape(numpyArray1D, [2, 7, 2])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, i meant to ask you to add reshape tests to https://github.com/apple/swift/blob/4e84b9267991c2e88342c83e8698914e335a9e44/test/TensorFlowRuntime/numpy_conversion.swift, where TensorShape is available.

array)
}

let reshaped = np.reshape(numpyArrayInt32, [2, 3])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let reshaped = np.reshape(numpyArrayInt32, [2, 3])
let reshaped = np.reshape(numpyArrayInt32, [2, 3] as TensorShape)

This is not using TensorShape, which kind of misses the point. Could you make the shape be an explicit TensorShape?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ ok i see what you were saying now. How is this look? @rxwei

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

tensor.array)
}

let reshaped = np.reshape(numpyArrayInt32, [2, 3])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


import Python
import StdlibUnittest
import struct TensorFlow.TensorShape
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests should also move to TensorFlowRuntime/numpy_conversion.swift. Python tests should not depend on the TensorFlow module.

@rxwei
Copy link
Contributor

rxwei commented Apr 5, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 5, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 5, 2019

@swift-ci please clean test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 7, 2019

@swift-ci please test tensorflow

@realdoug
Copy link
Author

realdoug commented Apr 8, 2019

@rxwei I believe this is a real build failure. I was able to repro on a fresh Linux install after pasting my TensorShape extension onto tensorflow~HEAD. Looking into it but not sure f you have any gut thoughts based on the error message.

EDIT: crux of the issue is that my init(PythonObject) has higher precedence than the variadic init(Int32...) defined above it. Still working on the right fix.

@realdoug
Copy link
Author

realdoug commented Apr 9, 2019

@rxwei Can you rerun CI, please?

  1. I added logic to detect len() and handle the case where pythonObject is a single int.
  2. There is also a bug that I was able to reproduce without any python related code & changing TensorShape(1) to TensorShape([1]) steps around it for now.

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

10 similar comments
@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

Ugh... CI is dead.

@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

1 similar comment
@rxwei
Copy link
Contributor

rxwei commented Apr 9, 2019

@swift-ci please test tensorflow

@rxwei rxwei merged commit da317a5 into swiftlang:tensorflow Apr 9, 2019
//
//===----------------------------------------------------------------------===//

import Python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please do the following?

  • Rename stdlib/public/TensorFlow/NumpyConversion.swift to PythonConversion.swift.
    • Make appropriate changes to the file header so it's not NumPy-specific.
  • Move import Python and the PythonConvertible logic from this file to PythonConversion.swift.

That would be much appreciated! 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dan-zheng sure thing: #23891

@rxwei
Copy link
Contributor

rxwei commented Apr 15, 2019

Hi @realdoug, since this PR changed the semantics of TensorShape(0) to rely on Python, this may have broken some tests in other platforms. I'm going to revert this PR since we are approaching our 0.3 release. Let me know when you have a PR that splits PythonConvertible into two protocols!

rxwei added a commit that referenced this pull request Apr 15, 2019
rxwei pushed a commit to rxwei/swift that referenced this pull request May 11, 2019
…ape (swiftlang#23762)

* [TF-76] add TensorShape conformance to PythonConvertible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tensorflow This is for "tensorflow" branch PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants