Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Simplest example compilation error #24

Closed
aokravchenko opened this issue May 11, 2018 · 3 comments
Closed

Simplest example compilation error #24

aokravchenko opened this issue May 11, 2018 · 3 comments

Comments

@aokravchenko
Copy link

aokravchenko commented May 11, 2018

I tried to run in Playground first code example mentioned here:
https://github.com/tensorflow/swift/blob/master/Usage.md

import TensorFlow

let x = Tensor([[1, 2], [3, 4]])
print(x)

An got this error:

note: overloads for 'Tensor<_>' exist with these partially matching parameter lists: (Tensor<Bool>), (ShapedArray<Scalar>), (Tensor<OtherScalar>), (Scalar), ([Tensor<Scalar>]), ([Scalar]), (C)
let x = Tensor([[1, 2], [3, 4]])

XCode 9.3, MacOS 10.13.4
Toolchain swift-tensorflow-DEVELOPMENT-2018-05-10-a-osx.pkg

@lattner
Copy link
Contributor

lattner commented May 11, 2018

Hi @aokravchenko,

We don't allow Tensor<Int> in Swift for TensorFlow, because Int changes size depending on what platform you're on, and many GPUs don't support Int64 (which is the default size of Int on most modern platforms).

This error message is Swift unhelpfully telling you that it doesn't know what element type to use. You can tell it by using:

let x = Tensor<Float>([[1, 2], [3, 4]])

@dan-zheng
Copy link
Member

dan-zheng commented May 11, 2018

This problem occurred because the Tensor literal convertible behavior changed in this commit.
To be specific, Tensor is no longer conforms to ExpressibleBy(Boolean|Integer|Float)Literal, fixing some ambiguity issues.

Previously, Tensor([[1, 2], [3, 4]]) worked and resolved to Tensor<Double>([[1, 2], [3, 4]]).
Now, you must write one of the following:

// You can replace `Float` with others `AccelerableByTensorFlow` scalar types like
// `Int32` or `Double`.
let x = Tensor<Float>([[1, 2], [3, 4]])
let x: Tensor<Float> = [[1, 2,], [3, 4]]

You might find the new syntax slightly less convenient. However, the new design fixes some long standing issues regarding scalar literal conversion.

In addition to tensor-tensor binary operations (like t + t), Tensor also defines many scalar-tensor binary operations for convenience (like 1 + t or t + 1):

/// Adds the scalar to every scalar of the tensor and produces the sum.
@_inlineable @inline(__always)
public extension Tensor where Scalar : Numeric {
  static func + (lhs: Scalar, rhs: Tensor) -> Tensor { ... }
}

However, one complication is that Tensor used to conform to ExpressibleBy(Boolean|Integer|Float)Literal. This caused some type checking issues, for example sometimes a scalar-scalar operation would incorrect resolve to be a scalar-tensor op:

// This is a contrived example, the actual circumstances producing
// this bug were more complex.

let bool: Bool = true
// The line below could resolve to `if bool == Tensor(true)` in certain cases,
// which is extremely undesirable.
if bool == true {
  ...
}

Now, Tensor is no longer scalar literal convertible, eliminating ambiguity.
let x: Tensor<Float> = 1 no longer works. If you want to convert 1 to a tensor, you must explicit call the Tensor initializer via let x = Tensor<Float>(1).


You can find more information about literal convertible behavior changes in the commit description if you're interested. I'll update sample code everywhere to work with the new design.

@dan-zheng
Copy link
Member

I updated code examples to account for Tensor literal convertible behavior changes in fd250cb.
If you have questions, please share them on the mailing list!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants