-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Break TensorGroup into InputTensorGroup and OutputTensorGroup. #20188
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
@swift-ci please test tensorflow |
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.
Yay!
Could you also add a test that exercises this?
lib/IRGen/IRGenSIL.cpp
Outdated
auto conformance = tfModule->lookupConformance(canType, | ||
tensorGroupProto); | ||
auto conformance = | ||
tfModule->lookupConformance(canType, outputTensorGroupProto); | ||
assert(conformance && "out type does not conform to TensorGroup"); |
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.
s/TensorGroup/OutputTensorGroup/
@swift-ci please test tensorflow |
Updated with fixes. |
@swift-ci please test tensorflow |
lib/IRGen/IRGenSIL.cpp
Outdated
@@ -2060,7 +2063,7 @@ void IRGenSILFunction::visitGraphOperationInst(GraphOperationInst *i) { | |||
// TODO: We should also handle the following cases: |
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.
Ooo, you can remove this whole TODO now!
|
||
// Test packing dynamic arrays. | ||
TensorGroupTest.testAllBackends("input, array") { | ||
some_tf_op(n: 32); |
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.
No semicolon needed
let ret: TensorHandle<Float> = #tfop("Pack", | ||
[arr], T$dtype: Float.tensorFlowDataType, axis: 0) | ||
let t: Tensor<Float> = Tensor<Float>(shape: [Int32(n), 3], scalars: arr_exp) | ||
expectEqual(Tensor<Float>(handle: ret).array, t.array) |
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 convention is to have the expected value first.
Also you can simplify this by making the expected ShapedArray directly with ShapedArray(shape: scalars:)
instead of making a tensor and converting it to array.
arr_exp.insert(contentsOf: [Float(i), 2, 3], at: arr_exp.count) | ||
} | ||
|
||
let ret: TensorHandle<Float> = #tfop("Pack", |
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.
You should be able to do let ret: Tensor<Float>
to avoid having to construct the Tensor yourself. This may require my very latest change that I submitted just about an hour ago.
|
||
/// This protocol is divided from OutputTensorGroup in order for the number of | ||
/// tensors to be determined at runtime. For example, Array<Tensor<Float>> may | ||
/// have an unknown number of elements at runtime. |
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.
s/runtime/compile time/
Thanks for the updates! I re-reviewed it. Is swift-ci still not listening to you? Let me know if you want me to trigger it. |
Also, my conflicting PR is submitted now so expect a few conflicts. Let me know if you want help resolving them. Sorry :) |
610c44c
to
4252736
Compare
All good now. |
5871232
to
ee3dd73
Compare
@swift-ci please test tensorflow |
1 similar comment
@swift-ci please test tensorflow |
This allows array to conform to InputTensorGroup and participate as an input to a #tfop.
@swift-ci please test tensorflow |
1 similar comment
@swift-ci please test tensorflow |
@swift-ci please test tensorflow |
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.
Left some comments.
First, I'd like to see some justification on the naming choice. This is user-visible API, meaning that the user has to understand what "input" and "output" are. As far as I can tell, much of this change has been implementation-driven, leaking compiler-specific information to the user space. I believe there are better names, and potentially better solutions to this problem. Why such a hurry in landing this patch?
Second, public API changes are expected to go through a discussion with the owner of API design and Swift readability at a minimum.
ptr = ptr!.advanced(by: Int(elem._inputTensorHandleCount)) | ||
} | ||
} | ||
public var _inputTensorHandleCount : Int32 { get { |
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.
Please reformat this as the following:
public var _inputTensorHandleCount : Int32 {
get {
...
}
}
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.
Would it be even better to do it without the get
, like this?
public var _inputTensorHandleCount: Int32 {
return ...
}
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.
Yes it definitely would. I didn't notice there's no set
or there's no special attributes on get
.
ptr = ptr!.advanced(by: Int(elem._inputTensorHandleCount)) | ||
} | ||
} | ||
public var _inputTensorHandleCount : Int32 { get { |
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.
Please remove space before :
.
return [Scalar.tensorFlowDataType] | ||
} | ||
|
||
public var _inputTensorHandleCount : Int32 { |
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.
Please remove the space before :
.
return [Scalar.tensorFlowDataType] | ||
} | ||
|
||
public var _inputTensorHandleCount : Int32 { |
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.
Please remove the space before :
.
return [TensorDataType(TF_VARIANT)] | ||
} | ||
|
||
public var _inputTensorHandleCount : Int32 { |
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.
Please remove the space before :
.
return [TensorDataType(TF_RESOURCE)] | ||
} | ||
|
||
public var _inputTensorHandleCount : Int32 { |
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.
Please remove the space before :
.
return [Scalar.tensorFlowDataType] | ||
} | ||
|
||
public var _inputTensorHandleCount : Int32 { |
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.
Please remove the space before :
.
/// | ||
/// TODO: Add a derived conformance to TensorGroup so that users don't have | ||
/// to write the conformance themselves. | ||
public protocol TensorGroup : InputTensorGroup & OutputTensorGroup {} |
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.
This is not idiomatic. Please change this to a typealias instead.
public typealias TensorGroup = InoutTensorGroup & OutputTensorGroup
/// The types of the tensor stored properties in this type. | ||
static var _typeList: [TensorDataType] { get } | ||
|
||
/// This protocol is divided from OutputTensorGroup in order for the number of |
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.
This is not valid Swift API doc comment because it's implementation-specific, not something the user is expected to understand. If you wanted it to be seen the library implementer, please use double slashes.
/// tensor operation as an input list whose elements are the tensor fields of | ||
/// the type. When a TensorGroup is used as an output, it gets initialized | ||
/// with its tensor fields set to the tensor operation's tensor outputs. | ||
/// A protocol for types that can be used as tensor operation inputs. When a |
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.
"tensor operation inputs" in an implementation-specific comment. It does not have a meaning to the user.
This allows array to conform to InputTensorGroup and participate as an input to a #tfop.