-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[TF-76][Python Interop] add PythonConvertible conformance to TensorShape #23762
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
Conversation
|
||
extension TensorShape: PythonConvertible { | ||
public var pythonObject: PythonObject { | ||
return self.dimensions.pythonObject |
There was a problem hiding this comment.
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)!) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Use short hand type names when possible for array and dictionary types.
- This is a failable initializer that allows you to return
nil
. The use of!
is unsafe.
self.init(Array<Int32>(pythonObject)!) | |
guard let array = [Int32](pythonObject) else { | |
return nil | |
} | |
self.init(array) |
Have you tested something like |
Thanks for tackling this! |
@rxwei in your example is Just made the above updates as well. |
‘x’ should be a numpy array. |
Could you add numpy reshape tests to https://github.com/apple/swift/blob/tensorflow/test/Python/numpy_conversion.swift? |
test/Python/python_runtime.swift
Outdated
expectEqual(five, Double(5).pythonObject) | ||
|
||
expectEqual(intArray, Array([2, 3]).pythonObject) | ||
expectEqual(dict, Dictionary<String, Int32>(["abc": 7]).pythonObject) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectEqual(dict, Dictionary<String, Int32>(["abc": 7]).pythonObject) | |
expectEqual(dict, ["abc" : 7].pythonObject) |
test/Python/python_runtime.swift
Outdated
expectEqual(five, Float(5).pythonObject) | ||
expectEqual(five, Double(5).pythonObject) | ||
|
||
expectEqual(intArray, Array([2, 3]).pythonObject) |
There was a problem hiding this comment.
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
.
expectEqual(intArray, Array([2, 3]).pythonObject) | |
expectEqual(intArray, [2, 3].pythonObject) |
test/Python/python_runtime.swift
Outdated
|
||
expectEqual([2, 3], Array(intArray)) | ||
expectEqual(TensorShape(2, 3), TensorShape(intArray)) | ||
expectEqual(["abc": 97], Dictionary<String, Int32>(dict)) |
There was a problem hiding this comment.
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.
expectEqual(["abc": 97], Dictionary<String, Int32>(dict)) | |
expectEqual(["abc" : 97], Dictionary<String, Int32>(dict)) |
test/Python/python_runtime.swift
Outdated
let half: PythonObject = 0.5 | ||
let string: PythonObject = "abc" | ||
let intArray: PythonObject = [2, 3] | ||
let dict: PythonObject = ["abc": 97] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let dict: PythonObject = ["abc": 97] | |
let dict: PythonObject = ["abc" : 97] |
test/Python/python_runtime.swift
Outdated
let minusOne: PythonObject = -1 | ||
let five: PythonObject = 5 | ||
let intArray: PythonObject = [2, 3] | ||
let dict: PythonObject = ["abc": 7] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let dict: PythonObject = ["abc": 7] | |
let dict: PythonObject = ["abc" : 7] |
@rxwei ok, I updated the syntax in all places in this file for consistency, verified that EDIT: added in two more in TensorFlowRuntime/numpy_conversions.swift as well. |
test/Python/numpy_conversion.swift
Outdated
} | ||
|
||
let numpyArray1D = np.ones(28) | ||
let reshaped3D = np.reshape(numpyArray1D, [2, 7, 2]) |
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
test/Python/python_runtime.swift
Outdated
|
||
import Python | ||
import StdlibUnittest | ||
import struct TensorFlow.TensorShape |
There was a problem hiding this comment.
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.
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please clean test tensorflow |
@swift-ci please test tensorflow |
@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 |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
10 similar comments
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
Ugh... CI is dead. |
@swift-ci please test tensorflow |
1 similar comment
@swift-ci please test tensorflow |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Python |
There was a problem hiding this comment.
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
toPythonConversion.swift
.- Make appropriate changes to the file header so it's not NumPy-specific.
- Move
import Python
and thePythonConvertible
logic from this file toPythonConversion.swift
.
That would be much appreciated! 🙂
There was a problem hiding this comment.
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
Hi @realdoug, since this PR changed the semantics of |
…ape (swiftlang#23762) * [TF-76] add TensorShape conformance to PythonConvertible
This makes TensorShape conform to PythonConvertible per TF-76.
Couple small notes:
Array
Array
andDictionary
's existingPythonConvertible
conformance so I added those as well.