diff --git a/test/Sema/Inputs/struct_tensor_array_protocol_other_module.swift b/test/Sema/Inputs/struct_tensor_array_protocol_other_module.swift deleted file mode 100644 index b7415dd81653d..0000000000000 --- a/test/Sema/Inputs/struct_tensor_array_protocol_other_module.swift +++ /dev/null @@ -1,16 +0,0 @@ -// SWIFT_ENABLE_TENSORFLOW - -import TensorFlow - -// expected-note @+1 4 {{type declared here}} -struct OtherFileNonconforming { - var x: Tensor - var y: Tensor -} - -// expected-note @+1 4 {{type declared here}} -struct GenericOtherFileNonconforming { - var x: Tensor - var y: Tensor - var z: Tensor -} diff --git a/test/Sema/Inputs/struct_tensor_group_other_module.swift b/test/Sema/Inputs/struct_tensor_group_other_module.swift deleted file mode 100644 index 4f46ca4cb5fa2..0000000000000 --- a/test/Sema/Inputs/struct_tensor_group_other_module.swift +++ /dev/null @@ -1,16 +0,0 @@ -// SWIFT_ENABLE_TENSORFLOW - -import TensorFlow - -// expected-note @+1 2 {{type declared here}} -struct OtherFileNonconforming : TensorArrayProtocol { - var x: Tensor - var y: Tensor -} - -// expected-note @+1 2 {{type declared here}} -struct GenericOtherFileNonconforming : TensorArrayProtocol, Equatable { - var x: Tensor - var y: Tensor - var z: Tensor -} diff --git a/test/TensorFlow/Inputs/ExternalStructs.swift b/test/TensorFlow/Inputs/ExternalStructs.swift deleted file mode 100644 index f51b5762419f1..0000000000000 --- a/test/TensorFlow/Inputs/ExternalStructs.swift +++ /dev/null @@ -1,21 +0,0 @@ -import TensorFlow - -@_fixed_layout -public struct ExternalStructFixedLayout { - let x: Tensor -} - -extension ExternalStructFixedLayout : TensorGroup { - public func _unpackTensorHandles(into address: UnsafeMutablePointer?) { - fatalError("dummy conformance") - } - public static var _typeList: [TensorDataType] { - fatalError("dummy conformance") - } - public static var _unknownShapeList: [TensorShape?] { - fatalError("dummy conformance") - } - public init(_owning tensorHandles: UnsafePointer?) { - fatalError("dummy conformance") - } -} diff --git a/test/TensorFlow/PlaygroundsRuntime.swift b/test/TensorFlow/PlaygroundsRuntime.swift deleted file mode 100644 index 7da943fdaf3a2..0000000000000 --- a/test/TensorFlow/PlaygroundsRuntime.swift +++ /dev/null @@ -1,35 +0,0 @@ -// RUN: true - -// This file contains stubs for functions that the playground transform in -// lib/Sema/PlaygroundTransform.cpp generates calls into. - -@inline(never) -func __builtin_log(_ object : T, _ name : String, _ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? { - return nil -} - -@inline(never) -func __builtin_log_with_id(_ object : T, _ name : String, _ id : Int, _ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? { - return nil -} - -@inline(never) -func __builtin_log_scope_entry(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? { - return nil -} - -@inline(never) -func __builtin_log_scope_exit(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? { - return nil -} - -@inline(never) -func __builtin_postPrint(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? { - return nil -} - -@inline(never) -func __builtin_send_data(_ object:AnyObject?) { -} - - diff --git a/test/TensorFlow/sema_errors.swift b/test/TensorFlow/sema_errors.swift deleted file mode 100644 index fe30481591531..0000000000000 --- a/test/TensorFlow/sema_errors.swift +++ /dev/null @@ -1,52 +0,0 @@ -// RUN: %target-swift-frontend -O -emit-sil -verify %s - -// This file contains tests that produce errors in the semantic analysis pass -// or earlier. These must be split from other SIL tests because SIL passes are -// not run when there are type-checking errors. - -import TensorFlow - -public func testExpressibleByLiteral() { - let _: Tensor = [1, 2, 3, 4] // ok - let _: Tensor = [1, 2.0, 3, 4] // ok - let _: Tensor = [[[true, false, false, true]]] // ok - // expected-error @+1 4 {{generic struct '_TensorElementLiteral' requires that 'Float' conform to 'ExpressibleByBooleanLiteral'}} - let _: Tensor = [[[true, false, false, true]]] - let _: Tensor = Tensor([[[true, false, false, true]]]) // ok -} - -func testTensorFlowFunctionTypes() { - // expected-error@+2 {{convention 'tensorflow' not supported}} - // expected-error@+1 {{convention 'tensorflow' not supported}} - var tf_fn, tf_fn2 : @convention(tensorflow) () -> () - - tf_fn = tf_fn2 - tf_fn2 = tf_fn -} - -// These are testcases that show the next steps in "@convention(tensorflow)" support. - -// expected-error@+1 {{convention 'tensorflow' not supported}} -func takesTFFunc(fn : @convention(tensorflow) (Tensor) -> Tensor) { -} - -func testTFFunc() { - let one = Tensor(1) - - // It is fine to pass a closure as a tensorflow function. The parititioner will - // validate it and throw an error if it cannot be partitioned. - takesTFFunc { $0 + 1 } - - // This type checks even though it captures a value. The SILGen pass will - // check if the closures that are tensorflow functions do not have captures. - takesTFFunc { $0 + one } - // FIXME: Should eventually be supported. - // xpected-error @+2 {{convention 'tensorflow' not supported}} - // expected-error @+1 {{attribute can only be applied to types, not declarations}} - @convention(tensorflow) - func inner1(a : Tensor) -> Tensor { - return a+1 - } - takesTFFunc(fn: inner1) -} - diff --git a/test/TensorFlow/shape_lowering/shape_rank0.swift b/test/TensorFlow/shape_lowering/shape_rank0.swift deleted file mode 100644 index 87987a2d7a7af..0000000000000 --- a/test/TensorFlow/shape_lowering/shape_rank0.swift +++ /dev/null @@ -1,24 +0,0 @@ -// RUN: %target-swift-frontend -emit-sil -Xllvm -tf-dump-graph -O %s -// RUN: %target-swift-frontend -emit-sil -Xllvm -tf-dump-graph -O %s | %FileCheck %s - -// FIXME: Our graph lowering doesn't handle ops with explicit shape attrs. -// XFAIL: * - -// These tests are in separate files because functions appear in the GraphDef -// in nondeterministic order. - -import TensorFlow - -public func test() { - let _: TensorHandle = #tfop("Placeholder", - dtype$dtype: Float.tensorFlowDataType, - shape: TensorShape([])) -} - -// CHECK-LABEL: key: "shape" -// CHECK-NEXT: value { -// CHECK-NEXT: list { -// CHECK-NEXT: shape { -// CHECK-NEXT: } -// CHECK-NEXT: } -// CHECK-NEXT: } diff --git a/test/TensorFlow/shape_lowering/shape_unknownrank.swift b/test/TensorFlow/shape_lowering/shape_unknownrank.swift deleted file mode 100644 index 182bf7a506c9c..0000000000000 --- a/test/TensorFlow/shape_lowering/shape_unknownrank.swift +++ /dev/null @@ -1,25 +0,0 @@ -// RUN: %target-swift-frontend -emit-sil -Xllvm -tf-dump-graph -O %s -// RUN: %target-swift-frontend -emit-sil -Xllvm -tf-dump-graph -O %s | %FileCheck %s - -// FIXME: Our graph lowering doesn't handle ops with explicit shape attrs. -// XFAIL: * - -// These tests are in separate files because functions appear in the GraphDef -// in nondeterministic order. - -import TensorFlow - -public func test() { - let _: TensorHandle = #tfop("Placeholder", - dtype$dtype: Float.tensorFlowDataType, - shape: nil as TensorShape?) -} - -// CHECK-LABEL: key: "shape" -// CHECK-NEXT: value { -// CHECK-NEXT: list { -// CHECK-NEXT: shape { -// CHECK-NEXT: unknown_rank: true -// CHECK-NEXT: } -// CHECK-NEXT: } -// CHECK-NEXT: } diff --git a/test/TensorFlow/tensor_autodiff.swift b/test/TensorFlow/tensor_autodiff.swift deleted file mode 100644 index c819f512b4494..0000000000000 --- a/test/TensorFlow/tensor_autodiff.swift +++ /dev/null @@ -1,32 +0,0 @@ -// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s - -import TensorFlow - -public func matsquare(_ x: Tensor) -> Tensor { - return matmul(x, x) -} - -public func test1() { - _ = pullback(at: Tensor([[1, 1], [1, 1]]), in: matsquare) -} - -// CHECK: @{{.*}}matsquare{{.*}}__vjp_src_0_wrt_0 - -// SR-8709 -public func selfmin(_ x: Tensor) -> Tensor { - return min(x, x) -} - -public func test2() { - _ = pullback(at: Tensor(1), in: selfmin) -} - -// CHECK: @{{.*}}selfmin{{.*}}__vjp_src_0_wrt_0 - -// Test that print compiles fine. -// Addresses TF-102. -@differentiable -func printSideEffectsAreOk(value: Tensor) -> Tensor { - print(value) - return value -} diff --git a/test/TensorFlow/tfop_packing.swift b/test/TensorFlow/tfop_packing.swift deleted file mode 100644 index 6df93901f875a..0000000000000 --- a/test/TensorFlow/tfop_packing.swift +++ /dev/null @@ -1,152 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %target-swift-frontend %S/Inputs/ExternalStructs.swift -enable-resilience -emit-module -emit-module-path %t/ExternalStructs.swiftmodule - -import TensorFlow -import ExternalStructs - -@inlinable @inline(__always) -public func genericPackedResults1() -> T { - // expected-error @+1 {{op named 'SomeOp2' is not registered in TensorFlow}} - return #tfop("SomeOp2") -} - -struct ContainsOneTensor { - let x: Tensor -} -extension ContainsOneTensor : TensorGroup { - public func _unpackTensorHandles(into address: UnsafeMutablePointer?) { - fatalError("dummy conformance") - } - public static var _typeList: [TensorDataType] { - fatalError("dummy conformance") - } - public static var _unknownShapeList: [TensorShape?] { - fatalError("dummy conformance") - } - public init(_owning tensorHandles: UnsafePointer?) { - fatalError("dummy conformance") - } -} - -public func packResultsToAggregate_indirectResult1() { - let foo: ContainsOneTensor = genericPackedResults1() - _hostOp(foo) -} - -// CHECK-LABEL: --- TFDeabstraction Result: {{.*}}packResultsToAggregate_indirectResult1{{.*}} -// CHECK: [[B0:%.*]] = graph_op "SomeOp2"() -// CHECK: [[B1:%.*]] = struct $Tensor ([[B0]] : $TensorHandle) -// CHECK: [[B2:%.*]] = struct $ContainsOneTensor ([[B1]] : $Tensor) - -@inlinable @inline(__always) -public func genericPackedResults2() -> (T, Tensor) { - // expected-error @+1 {{op named 'SomeOp3' is not registered in TensorFlow}} - return #tfop("SomeOp3") -} - -public func packResultsToAggregate_indirectResult2() { - let foo: (ContainsOneTensor, Tensor) = genericPackedResults2() - _hostOp(foo) -} - -// CHECK-LABEL: --- TFDeabstraction Result: {{.*}}packResultsToAggregate_indirectResult2{{.*}} -// CHECK: ([[C0:%.*]], [[C1:%.*]]) = graph_op "SomeOp3"() -// CHECK: [[C2:%.*]] = struct $Tensor ([[C0]] : $TensorHandle) -// CHECK: [[C3:%.*]] = struct $ContainsOneTensor ([[C2]] : $Tensor) -// CHECK: [[C4:%.*]] = struct $Tensor ([[C1]] : $TensorHandle) -// CHECK: [[C5:%.*]] = tuple ([[C3]] : $ContainsOneTensor, [[C4]] : $Tensor) - -public func packResultsToAggregate_externalStructFixedLayout() { - // expected-error @+1 {{op named 'SomeOp4' is not registered in TensorFlow}} - let foo: ExternalStructFixedLayout = #tfop("SomeOp4") - _hostOp(foo) -} - -// CHECK-LABEL: --- TFDeabstraction Result: {{.*}}packResultsToAggregate_externalStructFixedLayout{{.*}} -// CHECK: [[C0:%.*]] = graph_op "SomeOp4"() -// CHECK: [[C1:%.*]] = struct $Tensor ([[C0]] : $TensorHandle) -// CHECK: [[C2:%.*]] = struct $ExternalStructFixedLayout ([[C1]] : $Tensor) - -struct ManyNestedTensors { - let x: Tensor - let y: (Tensor, (a: Tensor, b: Tensor)) -} -extension ManyNestedTensors : TensorGroup { - public func _unpackTensorHandles(into address: UnsafeMutablePointer?) { - fatalError("dummy conformance") - } - public static var _typeList: [TensorDataType] { - fatalError("dummy conformance") - } - public static var _unknownShapeList: [TensorShape?] { - fatalError("dummy conformance") - } - public init(_owning tensorHandles: UnsafePointer?) { - fatalError("dummy conformance") - } -} - -public func unpackAggregate_basic() { - let foo = ManyNestedTensors(x: Tensor(1), y: (Tensor(2), (a: Tensor(3), b: Tensor(false)))) - // expected-error @+1 {{op named 'SomeOp5' is not registered in TensorFlow}} - let _: Tensor = #tfop("SomeOp5", [foo]) -} - -// CHECK-LABEL: --- TFDeabstraction Result: {{.*}}unpackAggregate_basic{{.*}} -// CHECK: [[D_ManyNestedTensors_x:%.*]] = graph_op "Const"(){{.*}}f32 0x3F800000 /* 1 */{{.*}} -// CHECK: [[D_ManyNestedTensors_y_0:%.*]] = graph_op "Const"(){{.*}}f32 0x40000000 /* 2 */{{.*}} -// CHECK: [[D_ManyNestedTensors_y_1_a:%.*]] = graph_op "Const"(){{.*}}i32 3{{.*}} -// CHECK: [[D_ManyNestedTensors_y_1_b:%.*]] = graph_op "Const"(){{.*}}i1 0{{.*}} -// CHECK: graph_op "SomeOp5"({{\[}}[[D_ManyNestedTensors_x]] : $TensorHandle, [[D_ManyNestedTensors_y_0]] : $TensorHandle, [[D_ManyNestedTensors_y_1_a]] : $TensorHandle, [[D_ManyNestedTensors_y_1_b]] : $TensorHandle{{\]}}) - -@inlinable @inline(__always) -public func genericUnpackedAggregate(t: T) -> Tensor { - // expected-error @+1 {{op named 'SomeOp6' is not registered in TensorFlow}} - return #tfop("SomeOp6", [t]) -} - -public func unpackAggregate_generic() { - let foo = ManyNestedTensors(x: Tensor(1), y: (Tensor(2), (a: Tensor(3), b: Tensor(false)))) - let _: Tensor = genericUnpackedAggregate(t: foo) -} - -// CHECK-LABEL: --- TFDeabstraction Result: {{.*}}unpackAggregate_generic{{.*}} -// CHECK: [[E_ManyNestedTensors_x:%.*]] = graph_op "Const"(){{.*}}f32 0x3F800000 /* 1 */{{.*}} -// CHECK: [[E_ManyNestedTensors_y_0:%.*]] = graph_op "Const"(){{.*}}f32 0x40000000 /* 2 */{{.*}} -// CHECK: [[E_ManyNestedTensors_y_1_a:%.*]] = graph_op "Const"(){{.*}}i32 3{{.*}} -// CHECK: [[E_ManyNestedTensors_y_1_b:%.*]] = graph_op "Const"(){{.*}}i1 0{{.*}} -// CHECK: graph_op "SomeOp6"({{\[}}[[E_ManyNestedTensors_x]] : $TensorHandle, [[E_ManyNestedTensors_y_0]] : $TensorHandle, [[E_ManyNestedTensors_y_1_a]] : $TensorHandle, [[E_ManyNestedTensors_y_1_b]] : $TensorHandle{{\]}}) - -// Checks that unpacking reuses extraction instructions instead of making new -// ones for each element of the input list. -// TODO(SR-8680): After fixing tuples, make x a tuple, so that we also test tuple extract reuse. -public func unpackAggregate_reuse(x: Tensor) { - // expected-error @+1 {{op named 'SomeOp7' is not registered in TensorFlow}} - let _: Tensor = #tfop("SomeOp7", [x, x]) -} - -// CHECK-LABEL: --- TFDeabstraction Result: {{.*}}unpackAggregate_reuse{{.*}} -// CHECK: struct_extract -// CHECK: struct_extract -// CHECK: [[F_TensorHandle:%.*]] = struct_extract %0 : $Tensor, #Tensor.handle -// CHECK: graph_op "SomeOp7"({{\[}}[[F_TensorHandle]] : $TensorHandle, [[F_TensorHandle]] : $TensorHandle{{\]}}) - -// TODO(SR-8680): Make this testcase work. -// public func unpackAggregate_tuple() { -// // xpected-error @+1 {{op named 'SomeOp7' is not registered in TensorFlow}} -// let _: Tensor = #tfop("SomeOp7", [(Tensor(1), Tensor(2))]) -// } - -public func packResultsToAggregate_basic() { - // expected-error @+1 {{op named 'SomeOp' is not registered in TensorFlow}} - let (r0, r1): (ManyNestedTensors, Tensor) = #tfop("SomeOp") - let _ = Tensor(0) + r0.y.1.a - _hostOp(r1) -} -// CHECK-LABEL: --- TFPartition Accelerator Result: {{.*}}packResultsToAggregate_basic{{.*}} -// CHECK: ([[A0:%.*]], [[A1:%.*]], [[A2:%.*]], [[A3:%.*]], [[A4:%.*]]) = graph_op "SomeOp"() {__device: "/job:localhost/replica:0/task:0/device:CPU:0"} : $TensorHandle, $TensorHandle, $TensorHandle, $TensorHandle, $TensorHandle -// CHECK: graph_op "Add"({{.*}} : $TensorHandle, [[A2]] : $TensorHandle) {T$dtype: i32 3, __device: "/job:localhost/replica:0/task:0/device:CPU:0"} : $TensorHandle -// CHECK: [[PACKED:%.*]] = tuple ([[A0]] : $TensorHandle, [[A1]] : $TensorHandle, [[A2]] : $TensorHandle, [[A3]] : $TensorHandle, [[A4]] : $TensorHandle) -// CHECK: return [[PACKED]] : $(TensorHandle, TensorHandle, TensorHandle, TensorHandle, TensorHandle) - - diff --git a/test/TensorFlowRuntime/Inputs/ConflictingModule1.swift b/test/TensorFlowRuntime/Inputs/ConflictingModule1.swift deleted file mode 100644 index a5c3411a750e6..0000000000000 --- a/test/TensorFlowRuntime/Inputs/ConflictingModule1.swift +++ /dev/null @@ -1,11 +0,0 @@ -import TensorFlow - -public func conflictingFunction(_ n: Int) -> Tensor { - var i = 0 - var t = Tensor(0) - while i < n { - t += 1 - i += 1 - } - return t -} diff --git a/test/TensorFlowRuntime/Inputs/ConflictingModule2.swift b/test/TensorFlowRuntime/Inputs/ConflictingModule2.swift deleted file mode 100644 index 53e40eb3a6b1b..0000000000000 --- a/test/TensorFlowRuntime/Inputs/ConflictingModule2.swift +++ /dev/null @@ -1,11 +0,0 @@ -import TensorFlow - -public func conflictingFunction(_ n: Int) -> Tensor { - var i = 0 - var t = Tensor(0) - while i < n { - t += 2 - i += 1 - } - return t -} diff --git a/test/TensorFlowRuntime/Inputs/data.csv b/test/TensorFlowRuntime/Inputs/data.csv deleted file mode 100644 index ebb6763e11754..0000000000000 --- a/test/TensorFlowRuntime/Inputs/data.csv +++ /dev/null @@ -1,2 +0,0 @@ -1,2 -3,4 diff --git a/test/TensorFlowRuntime/codable.swift b/test/TensorFlowRuntime/codable.swift deleted file mode 100644 index ae0809704c5bb..0000000000000 --- a/test/TensorFlowRuntime/codable.swift +++ /dev/null @@ -1,54 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// REQUIRES: objc_interop - -import TensorFlow -import Foundation -import TensorFlowUnittest -import StdlibUnittest - -var CodableTests = TestSuite("TensorFlowCodable") - -// `testRoundTrip` adapted from test/stdlib/TestJSONEncoder.swift. -private func _testRoundTrip( - of value: T, expectedJSON json: Data? = nil -) where T : Codable, T : Equatable { - var payload: Data! = nil - do { - let encoder = JSONEncoder() - payload = try encoder.encode(value) - } catch { - expectUnreachable("Failed to encode \(T.self) to JSON: \(error)") - } - - if let expectedJSON = json { - expectEqual(expectedJSON, payload, "Produced JSON not identical to expected JSON.") - } - - do { - let decoder = JSONDecoder() - let decoded = try decoder.decode(T.self, from: payload) - expectEqual(value, decoded, "\(T.self) did not round-trip to an equal value.") - } catch { - expectUnreachable("Failed to decode \(T.self) from JSON: \(error)") - } -} - -CodableTests.testAllBackends("Tensor") { - let tensor = Tensor(shape: [2, 3], scalars: Array(1...6)) - let expectedJSON = "{\"shape\":[2,3],\"scalars\":[1,2,3,4,5,6]}".data(using: .utf8)! - _testRoundTrip(of: tensor, expectedJSON: expectedJSON) -} - -CodableTests.testAllBackends("ShapedArray") { - let array = ShapedArray(shape: [2, 3], scalars: Array(1...6)) - let expectedJSON = "{\"shape\":[2,3],\"scalars\":[1,2,3,4,5,6]}".data(using: .utf8)! - _testRoundTrip(of: array, expectedJSON: expectedJSON) -} - -CodableTests.testAllBackends("TensorShape") { - let shape: TensorShape = [2, 3, 4] - _testRoundTrip(of: shape) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/collective.swift b/test/TensorFlowRuntime/collective.swift deleted file mode 100644 index 5c53b099eafd1..0000000000000 --- a/test/TensorFlowRuntime/collective.swift +++ /dev/null @@ -1,42 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -// Tests on collective ops, as a building block for data/model parallel programs. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var CollectiveTests = TestSuite("Collective") - -CollectiveTests.testAllBackends("ConfigTest") { - // Run some tensor code to trigger runtime configuration. - print(Tensor(0.0) + Tensor(1.0)) - expectEqual(3, _RuntimeConfig.cpuDeviceCount) -} - -CollectiveTests.testAllBackends("SingletonGroup") { - let x = Tensor(1.0) - let t = Raw.collectiveReduce(x, groupSize: 1, groupKey: 1, instanceKey: 1, - mergeOp: .add, finalOp: .id, subdivOffsets: [0], - waitFor: []) - print(t) - expectEqualWithScalarTensor(1, t) -} - -CollectiveTests.testAllBackends("GroupWithSize2_threads") { - // Need 2 or more CPU devices for this test. - let x = Tensor(1.0) - - _runOnNDevices(2) { i in - withDevice(.cpu, UInt(i)) { - let t = Raw.collectiveReduce(x, groupSize: 2, groupKey: 3, instanceKey: 3, - mergeOp: .add, finalOp: .id, subdivOffsets: [0], waitFor: []) - - print(t) - expectEqualWithScalarTensor(2, t) - } - } -} - -runAllTests() diff --git a/test/TensorFlowRuntime/conflicting_module.swift b/test/TensorFlowRuntime/conflicting_module.swift deleted file mode 100644 index f35d9df115cc6..0000000000000 --- a/test/TensorFlowRuntime/conflicting_module.swift +++ /dev/null @@ -1,26 +0,0 @@ -// Compiles two modules with identically-named tensor functions, then calls -// those functions. This reproduces a former bug where the associated -// "whilecond" and "whilebody" functions get identical names, which causes a -// crash at runtime when adding the functions to the eager context. - -// RUN: %empty-directory(%t) -// RUN: %target-build-swift %S/Inputs/ConflictingModule1.swift -emit-module -emit-library -module-name ConflictingModule1 -o %t/%target-library-name(libConflictingModule1) -emit-module-path %t/ConflictingModule1.swiftmodule -// RUN: %target-build-swift %S/Inputs/ConflictingModule2.swift -emit-module -emit-library -module-name ConflictingModule2 -o %t/%target-library-name(libConflictingModule2) -emit-module-path %t/ConflictingModule2.swiftmodule -// RUN: %target-build-swift %s -I %t -L %t %t/%target-library-name(libConflictingModule1) %t/%target-library-name(libConflictingModule2) -Xlinker -rpath -Xlinker %t -o %t/a.out -// RUN: %target-run %t/a.out %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -import StdlibUnittest -import TensorFlowUnittest - -import ConflictingModule1 -import ConflictingModule2 - -var ConflictingModuleTests = TestSuite("ConflictingModule") - -ConflictingModuleTests.testAllBackends("call functions") { - expectEqual(5, ConflictingModule1.conflictingFunction(5).scalar!) - expectEqual(10, ConflictingModule2.conflictingFunction(5).scalar!) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/control_flow_1.swift b/test/TensorFlowRuntime/control_flow_1.swift deleted file mode 100644 index 13eab1e864e7d..0000000000000 --- a/test/TensorFlowRuntime/control_flow_1.swift +++ /dev/null @@ -1,217 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Control flow related tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var ControlFlowTests = TestSuite("ControlFlow") - -public enum Pet { - case bird, cat, dog, fish -} - -// Enumerated all cases. -@inline(never) -public func weighPet(_ pet: Pet, - _ expectedVal: Float) { - var weight = Tensor(1.0) - switch pet { - case .bird: - weight += 1.0 - case .cat: - weight += 5.0 - case .dog: - weight += 10.0 - case .fish: break // no tensor code here - } - expectNearlyEqualWithScalarTensor(expectedVal, weight) -} -// TODO: fix the disabled GPU tests below. -#if !CUDA -ControlFlowTests.testAllBackends("weighPet") { - weighPet(.bird, 2.0) - weighPet(.cat, 6.0) - weighPet(.dog, 11.0) - weighPet(.fish, 1.0) -} -#endif // CUDA - -@inline(never) -public func weighPetWithDefault(_ pet: Pet, - _ expectedVal: Float) { - var weight = Tensor(1.0) - switch pet { - case .cat: - weight += 5.0 - default: - weight += 3.0 - } - expectNearlyEqualWithScalarTensor(expectedVal, weight) -} -ControlFlowTests.testAllBackends("weighPetWithDefault") { - weighPetWithDefault(.cat, 6.0) - weighPetWithDefault(.bird, 4.0) - weighPetWithDefault(.dog, 4.0) - weighPetWithDefault(.fish, 4.0) -} - - -public enum EnumWithPayload { - case a(String) - case b(Float) - case c(Tensor, Tensor) - indirect case d(EnumWithPayload) -} - -@inline(never) -public func testEnumWithPayload(_ x: EnumWithPayload, _ expectedVal: Float) { - var val = Tensor(2.0) - switch x { - case .a(let x): - val += 1.0 - print(x) - case .b(let x): - print(x) - let tx = Tensor(x) - val += tx - case .c(let x, let y): - val *= x + y - print(x) - print(y) - case .d(let f): - val += 10.0 - print(f) - } - val += 0.0 - expectNearlyEqualWithScalarTensor(expectedVal, val) -} -#if !CUDA -ControlFlowTests.testAllBackends("testEnumWithPayload") { - testEnumWithPayload(.a("Hello"), 3.0) - testEnumWithPayload(.b(3.0), 5.0) - testEnumWithPayload(.c(Tensor(1.0), Tensor(2.0)), 6.0) - testEnumWithPayload(.d(.b(3.0)), 12.0) -} -#endif // CUDA - -@inline(never) -public func testCondBranch(_ a: Bool, - _ expectedVal: Float) { - var b = Tensor(2.0) - if a { - b += 1.0 - } - b -= 1.0 - expectNearlyEqualWithScalarTensor(expectedVal, b) -} -ControlFlowTests.testAllBackends("testCondBranch") { - testCondBranch(true, 2.0) - testCondBranch(false, 1.0) -} - - -@inline(never) -public func testSwitchEnum(_ a: Tensor?, - _ expectedVal: Float) { - var b = Tensor(2.0) - if let a = a { - b += a - } - b -= 1.0 - expectNearlyEqualWithScalarTensor(expectedVal, b) -} -ControlFlowTests.testAllBackends("testSwitchEnum") { - testSwitchEnum(Tensor(1.0), 2.0) - testSwitchEnum(nil, 1.0) -} - - -public protocol P {} -public struct S: P {} -public enum EnumAddr { - case A - case B(P) -} -@inline(never) -public func testSwitchEnumAddr(_ a: EnumAddr, - _ expectedVal: Float) { - var b = Tensor(2.0) - switch a { - case .A: - b += 1.0 - default: - break - } - b -= 1.0 - expectNearlyEqualWithScalarTensor(expectedVal, b) -} -ControlFlowTests.testAllBackends("testSwitchEnumAddr") { - testSwitchEnumAddr(EnumAddr.A, 2.0) - testSwitchEnumAddr(EnumAddr.B(S()), 1.0) -} - - -@inline(never) -public func testTryApply(_ a: Int, - _ expectedVal: Float) { - enum MyError : Error { - case E(String) - } - @inline(never) - func foo(_ x: Int) throws { - if x == 1 { - throw MyError.E("err") - } - } - - var b = Tensor(2.0) - do { - try foo(a) - b += 1.0 - } catch { } - b -= 1.0 - expectNearlyEqualWithScalarTensor(expectedVal, b) -} -ControlFlowTests.testAllBackends("testTryApply") { - testTryApply(0, 2.0) - testTryApply(1, 1.0) -} - - -class X {} -class Y {} -public func testCheckedCastBranch(_ a: AnyObject, - _ expectedVal: Float) { - var b = Tensor(2.0) - if a is X { - b += 1.0 - } - b -= 1.0 - expectNearlyEqualWithScalarTensor(expectedVal, b) -} -ControlFlowTests.testAllBackends("testCheckedCastBranch") { - testCheckedCastBranch(X(), 2.0) - testCheckedCastBranch(Y(), 1.0) -} - - -struct SS : P {} -@inline(never) -public func testCheckedCastAddrBranch(_ p: P, - _ expectedVal: Float) { - var b = Tensor(2.0) - if let _ = p as? S { - b += 1.0 - } - b -= 1.0 - expectNearlyEqualWithScalarTensor(expectedVal, b) -} -ControlFlowTests.testAllBackends("testCheckedCastAddrBranch") { - testCheckedCastAddrBranch(S(), 2.0) - testCheckedCastAddrBranch(SS(), 1.0) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/control_flow_objc.swift b/test/TensorFlowRuntime/control_flow_objc.swift deleted file mode 100644 index 36ff34b08373d..0000000000000 --- a/test/TensorFlowRuntime/control_flow_objc.swift +++ /dev/null @@ -1,34 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// REQUIRES: OS=macosx - -// Control flow related tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest -import Foundation - -var ControlFlowTests = TestSuite("ControlFlow") - -class X { - @objc func f() {} -} -class Y {} -@inline(never) -public func testDynamicMethodBranch(_ obj: AnyObject, - _ expectedVal: Float) { - var b = Tensor(2.0) - if let foo = obj.f { - foo() - b += 1.0 - } - b -= 1.0 - expectNearlyEqualWithScalarTensor(expectedVal, b) -} -ControlFlowTests.testAllBackends("testDynamicMethodBranch") { - testDynamicMethodBranch(X(), 2.0) - testDynamicMethodBranch(Y(), 1.0) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/dataset_1.swift b/test/TensorFlowRuntime/dataset_1.swift deleted file mode 100644 index 2cd9454ba6c5c..0000000000000 --- a/test/TensorFlowRuntime/dataset_1.swift +++ /dev/null @@ -1,48 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -// Dataset tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var DatasetTests = TestSuite("Dataset") - -#if !CUDA -struct SimpleOutput : TensorGroup { - let a: TensorHandle - let b: TensorHandle -} -DatasetTests.testAllBackends("MultiValue") { - let elements1: Tensor = [0, 1, 2] - let elements2: Tensor = [10, 11, 12] - let outputTypes = [Int32.tensorFlowDataType, Int32.tensorFlowDataType] - let outputShapes: [TensorShape?] = [nil, nil] - let dataset: VariantHandle = Raw.tensorSliceDataset( - components: [elements1, elements2], - outputShapes: outputShapes - ) - let iterator: ResourceHandle = Raw.iteratorV2(sharedName: "blah", - container: "earth", outputTypes: outputTypes, outputShapes: outputShapes - ) - Raw.makeIterator(dataset: dataset, iterator: iterator) - var next: SimpleOutput = Raw.iteratorGetNext( - iterator: iterator, outputShapes: outputShapes - ) - expectEqual(0, Tensor(handle: next.a).scalarized()) - expectEqual(10, Tensor(handle: next.b).scalarized()) - next = Raw.iteratorGetNext( - iterator: iterator, outputShapes: outputShapes - ) - expectEqual(1, Tensor(handle: next.a).scalarized()) - expectEqual(11, Tensor(handle: next.b).scalarized()) - next = Raw.iteratorGetNext( - iterator: iterator, outputShapes: outputShapes - ) - expectEqual(2, Tensor(handle: next.a).scalarized()) - expectEqual(12, Tensor(handle: next.b).scalarized()) -} -#endif - -runAllTests() diff --git a/test/TensorFlowRuntime/dataset_api.swift b/test/TensorFlowRuntime/dataset_api.swift deleted file mode 100644 index 2072a0581e417..0000000000000 --- a/test/TensorFlowRuntime/dataset_api.swift +++ /dev/null @@ -1,112 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Dataset API tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var DatasetAPITests = TestSuite("DatasetAPI") - -// TODO: fix the test suite for GPU. -// We have errors like: -// Not found: No registered 'ZipDataset' OpKernel for GPU devices compatible with node {{node op/_S4mainyycfU4_.tf_GPU.70.64_/device_GPU_0_7}} = ZipDataset[N=2, output_shapes=[, ], output_types=[DT_FLOAT, DT_FLOAT], _device="/device:GPU:0"](tf_recv_55, tf_recv_56) -#if !CUDA -DatasetAPITests.testAllBackends("SingleValueManualIterator") { - // [[1], [2], [3], [4], [5]] - let scalars = Tensor(rangeFrom: 0, to: 5, stride: 1) - .reshaped(to: [5, 1]) - let dataset = Dataset(elements: scalars) - var iterator = dataset.makeIterator() - var i: Int = 0 - while let item = iterator.next() { - expectEqual(scalars[i].array, item.array) - i += 1 - } -} - -DatasetAPITests.testAllBackends("DatasetIteration") { - // [[1], [2], [3], [4], [5]] - let scalars = Tensor(rangeFrom: 0, to: 5, stride: 1) - .reshaped(to: [5, 1]) - let dataset = Dataset(elements: scalars) - var i: Int = 0 - for item in dataset { - expectEqual(scalars[i].array, item.array) - i += 1 - } -} - -DatasetAPITests.testAllBackends("SingleValueTransformations") { - let scalars = Tensor(rangeFrom: 0, to: 5, stride: 1) - let dataset = Dataset(elements: scalars) - let shuffled = dataset.shuffled(sampleCount: 5, randomSeed: 42) - expectEqual([0, 4, 1, 3, 2], shuffled.map { $0.scalar! }) -} - -// TODO(TF-263): Make this test work in graph mode -#if !TF_NODYNAMIC_COMPILATION -DatasetAPITests.testAllBackends("SingleValueHOFs") { - let scalars = Tensor(rangeFrom: 0, to: 5, stride: 1) - let dataset = Dataset(elements: scalars) - let addedOne: Dataset = dataset.map { $0 + 1 } - expectEqual([1, 2, 3, 4, 5], addedOne.flatMap { $0.scalars }) - // Use '.==' in the following closure to avoid any conversions to - // host data types, which is not handled correctly in tracing. - let evens: Dataset = dataset.filter { Tensor($0 % 2) .== Tensor(0) } - expectEqual([0, 2, 4], evens.flatMap { $0.scalars }) -} - -DatasetAPITests.testAllBackends("ParallelMap") { - let scalars = Tensor(rangeFrom: 0, to: 5, stride: 1) - let dataset = Dataset(elements: scalars) - let addedOne: Dataset = dataset.map(parallelCallCount: 5) { $0 + 1 } - expectEqual([1, 2, 3, 4, 5], addedOne.flatMap { $0.scalars }) - // Use '.==' in the following closure to avoid any conversions to - // host data types, which is not handled correctly in tracing. - let evens: Dataset = dataset.filter { Tensor($0 % 2) .== Tensor(0) } - expectEqual([0, 2, 4], evens.flatMap { $0.scalars }) -} - -DatasetAPITests.testAllBackends("MapToDifferentType") { - let scalars = Tensor(rangeFrom: 0, to: 5, stride: 1) - let dataset = Dataset(elements: scalars) - let shuffled = dataset.shuffled(sampleCount: 5, randomSeed: 42) - expectEqual([0, 4, 1, 3, 2], shuffled.map { $0.scalar! }) - let evens = shuffled.map { Tensor($0 % 2) .== Tensor(0) } - expectEqual([true, true, false, false, true], evens.map { $0.scalar! }) -} - -#endif // !TF_NODYNAMIC_COMPILATION - -DatasetAPITests.testAllBackends("SingleValueBatched") { - let scalars = Tensor(rangeFrom: 0, to: 5, stride: 1) - let dataset = Dataset(elements: scalars) - let batched = dataset.batched(2) - - var iterator = batched.makeIterator() - expectEqual([0, 1], iterator.next()!.scalars) - expectEqual([2, 3], iterator.next()!.scalars) - expectEqual([4], iterator.next()!.scalars) -} - -// TODO(TF-263): Make this test work in graph mode. -#if !TF_NODYNAMIC_COMPILATION -DatasetAPITests.testAllBackends("DoubleValueDatasetIteration") { - let scalars1 = Tensor(rangeFrom: 0, to: 5, stride: 1) - let scalars2 = Tensor(rangeFrom: 5, to: 10, stride: 1) - let datasetLeft = Dataset(elements: scalars1) - let datasetRight = Dataset(elements: scalars2) - var i: Int = 0 - for pair in zip(datasetLeft, datasetRight) { - expectEqual(scalars1[i].array, pair.first.array) - expectEqual(scalars2[i].array, pair.second.array) - i += 1 - } -} -#endif // !TF_NODYNAMIC_COMPILATION - -#endif //!CUDA - -runAllTests() diff --git a/test/TensorFlowRuntime/dataset_global.swift b/test/TensorFlowRuntime/dataset_global.swift deleted file mode 100644 index 0c6d8cc57cba2..0000000000000 --- a/test/TensorFlowRuntime/dataset_global.swift +++ /dev/null @@ -1,46 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -// This file contains testing over a dataset as a global variable. This requires -// sends/recvs support for variant handles. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var DatasetGlobalTests = TestSuite("DatasetGlobal") - -// TODO: add GPU support -- in device partitioning, maybe sure we do not do tensor -// transfer for variant / resource ops. -// The current error is: -// Fatal error: No unary variant device copy function found for direction: 1 and Variant type_name: tensorflow::DatasetVariantWrapper -#if !CUDA - -DatasetGlobalTests.testAllBackends("DatasetAsGlobalVar") { - let scalars = Tensor([0, 1, 2]) - let dataset = Dataset(elements: scalars) - - var expectedVal: Float = 0.0 - for item in dataset { - print(item) - expectNearlyEqualWithScalarTensor(expectedVal, item) - expectedVal += 1.0 - } -} - -DatasetGlobalTests.testAllBackends("IteratorAsGlobalVar") { - let scalars = Tensor([0, 1, 2]) - let dataset = Dataset(elements: scalars) - var iterator = dataset.makeIterator() - - var expectedVal: Float = 0.0 - while let item = iterator.next() { - print(item) - expectNearlyEqualWithScalarTensor(expectedVal, item) - expectedVal += 1.0 - } -} - -#endif // !CUDA - -runAllTests() diff --git a/test/TensorFlowRuntime/device_placement.swift b/test/TensorFlowRuntime/device_placement.swift deleted file mode 100644 index b1fa250ea7519..0000000000000 --- a/test/TensorFlowRuntime/device_placement.swift +++ /dev/null @@ -1,22 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -// Test device placement API. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var DevicePlacementTests = TestSuite("DevicePlacement") - -DevicePlacementTests.testAllBackends("GroupWithSize2") { - // Need 2 or more CPU devices for this test. - let x = Tensor(1.0) - - withDefaultDevice() { - let t = Tensor(1.0) + Tensor(2.0) - expectEqualWithScalarTensor(3, t) - } -} - -runAllTests() diff --git a/test/TensorFlowRuntime/dynamic_attributes.swift b/test/TensorFlowRuntime/dynamic_attributes.swift deleted file mode 100644 index b9963fc7ffde0..0000000000000 --- a/test/TensorFlowRuntime/dynamic_attributes.swift +++ /dev/null @@ -1,363 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// REQUIRES: tensorflow - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var DynamicAttributeTests = TestSuite("DynamicAttribute") - -// ===== Dynamic Attribute Values ==== -// These global vars with @inline(never) loaders ensure that the compiler -// can't find the constant value of the attributes. - -var dtypeInt32 = Int32.tensorFlowDataType -@inline(never) -func loadDtypeInt32() -> TensorDataType { - return dtypeInt32 -} - -var dtypeDouble = Double.tensorFlowDataType -@inline(never) -func loadDtypeDouble() -> TensorDataType { - return dtypeDouble -} - -var stridesInt32 = [Int32(1), Int32(1), Int32(1), Int32(1)] -@inline(never) -func loadStridesInt32() -> [Int32] { - return stridesInt32 -} - -var trueBool = true -@inline(never) -func loadTrue() -> Bool { - return trueBool -} - -var falseBool = false -@inline(never) -func loadFalse() -> Bool { - return falseBool -} - -var int32_1: Int32 = 1 -@inline(never) -func loadInt32_1() -> Int32 { - return int32_1 -} - -var int32_2: Int32 = 2 -@inline(never) -func loadInt32_2() -> Int32 { - return int32_2 -} - -var int64_1: Int64 = 1 -@inline(never) -func loadInt64_1() -> Int64 { - return int64_1 -} - -var int64_2: Int64 = 2 -@inline(never) -func loadInt64_2() -> Int64 { - return int64_2 -} - -var double_1: Double = 1 -@inline(never) -func loadDouble_1() -> Double { - return double_1 -} - -var double_0point1: Double = 0.1 -@inline(never) -func loadDouble_0point1() -> Double { - return double_0point1 -} - -var boundariesDouble: [Double] = [0, 1, 2, 3] -@inline(never) -func loadBoundariesDouble() -> [Double] { - return boundariesDouble -} - -var tensorShapeArray = [TensorShape([1]), TensorShape([2])] -@inline(never) -func loadTensorShapeArray() -> [TensorShape] { - return tensorShapeArray -} - -var optionalTensorShapeArray = [TensorShape([1]), nil] -@inline(never) -func loadOptionalTensorShapeArray() -> [TensorShape?] { - return optionalTensorShapeArray -} - -var tensorDataTypeArray = [Int32.tensorFlowDataType, Int32.tensorFlowDataType] -@inline(never) -func loadTensorDataTypeArray() -> [TensorDataType] { - return tensorDataTypeArray -} - -var VALID = Raw.Padding2.valid -@inline(never) -func loadVALID() -> Raw.Padding2 { - return VALID -} - -var stringArrayAB = ["a", "b"] -@inline(never) -func loadStringArrayAB() -> [String] { - return stringArrayAB -} - -var stringArrayEmpty: [String] = [] -@inline(never) -func loadStringArrayEmpty() -> [String] { - return stringArrayEmpty -} - -var shape = TensorShape([1]) -@inline(never) -func loadShape() -> TensorShape { - return shape -} -@inline(never) -func loadOptionalShape() -> TensorShape? { - return shape -} - -var unknownShape: TensorShape? = nil -@inline(never) -func loadUnknownShape() -> TensorShape? { - return unknownShape -} - -// ==== Convolution Helper Values ==== -// Helper values for tests involving convolution ops. - -let convImage = Tensor([[ - [[1], [0], [0]], - [[0], [1], [0]], - [[0], [0], [0]] -]]) -let convFilter = Tensor([ - [[[1]], [[1]]], - [[[1]], [[1]]] -]) -let convExpectedResult = ShapedArray( - shape: [1, 2, 2, 1], - scalars: [2, 1, - 1, 1] -) - -// ==== Actual Tests ==== - -DynamicAttributeTests.testAllBackends("NormalAttribute Bool") { - let input = Tensor([[1, 2], [2, 1]]) - let reductionIndices = Tensor(0) - - let resultKeepDimsTrue = Raw.max(input, reductionIndices: reductionIndices, - keepDims: loadTrue()) - let expectedResultKeepDimsTrue = ShapedArray(shape: [1, 2], - scalars: [2, 2]) - expectEqual(expectedResultKeepDimsTrue, resultKeepDimsTrue.array) - - let resultKeepDimsFalse = Raw.max(input, reductionIndices: reductionIndices, - keepDims: loadFalse()) - let expectedResultKeepDimsFalse = ShapedArray(shape: [2], - scalars: [2, 2]) - expectEqual(expectedResultKeepDimsFalse, resultKeepDimsFalse.array) -} - -DynamicAttributeTests.testAllBackends("NormalAttribute Int64") { - let random1: Tensor = Raw.randomUniformInt(shape: Tensor([5]), - minval: Tensor(0), - maxval: Tensor(20), - seed: loadInt64_1(), - seed2: loadInt64_1()) - expectEqual([10, 18, 7, 2, 11], random1.scalars) - let random2: Tensor = Raw.randomUniformInt(shape: Tensor([5]), - minval: Tensor(50), - maxval: Tensor(100), - seed: loadInt64_2(), - seed2: loadInt64_2()) - expectEqual([70, 78, 90, 77, 78], random2.scalars) -} - -DynamicAttributeTests.testAllBackends("NormalAttribute Double") { - let x = Tensor(1) - let y = Tensor(1.5) - - let result1 = Raw.approximateEqual(x, y, tolerance: loadDouble_1()) - expectEqual(true, result1.scalar!) - - let result2 = Raw.approximateEqual(x, y, tolerance: loadDouble_0point1()) - expectEqual(false, result2.scalar!) -} - -DynamicAttributeTests.testAllBackends("NormalAttribute String") { - let result = Raw.conv2D( - convImage, - filter: convFilter, - strides: [1, 1, 1, 1] as [Int32], - padding: loadVALID(), - explicitPaddings: []) - expectPointwiseNearlyEqual(convExpectedResult, result.array) -} - -DynamicAttributeTests.testAllBackends("NormalAttribute Array") { - // There aren't any ops that take bool list attributes! -} - -DynamicAttributeTests.testAllBackends("NormalAttribute Array") { - let result = Raw.conv2D( - convImage, - filter: convFilter, - strides: loadStridesInt32(), - padding: .valid, - explicitPaddings: []) - expectPointwiseNearlyEqual(convExpectedResult, result.array) -} - -DynamicAttributeTests.testAllBackends("NormalAttribute Array") { - let input = Tensor([-1, 0.1, 4.3, 1.2]) - let result = Raw.bucketize(input, boundaries: loadBoundariesDouble()) - let expectedResult = ShapedArray([0, 1, 4, 2]) - expectEqual(expectedResult, result.array) -} - -/// Checks that `dataset` is a TensorSliceDataset of -/// Tensor([1, 2]) and Tensor([[1, 1], [2, 2]]) -func check(dataset: VariantHandle) { - let outputTypes = [Int32.tensorFlowDataType, Int32.tensorFlowDataType] - let outputShapes = [nil, nil] as [TensorShape?] - let iterator = Raw.iteratorV2( - sharedName: "blah", - container: "earth", - outputTypes: outputTypes, - outputShapes: outputShapes) - Raw.makeIterator(dataset: dataset, iterator: iterator) - - struct TensorTuple: TensorGroup { - let first: Tensor - let second: Tensor - } - - var next: TensorTuple = Raw.iteratorGetNext( - iterator: iterator, outputShapes: outputShapes) - expectEqual(ShapedArray([1]), next.first.array) - expectEqual(ShapedArray([1, 1]), next.second.array) - next = Raw.iteratorGetNext(iterator: iterator, outputShapes: outputShapes) - expectEqual(ShapedArray([2]), next.first.array) - expectEqual(ShapedArray([2, 2]), next.second.array) -} - -#if !CUDA -// TensorSliceDataset not available on GPU. -DynamicAttributeTests.testAllBackends("NormalAttribute Array") { - let elements1 = Tensor([[1], [2]]) - let elements2 = Tensor([[1, 1], [2, 2]]) - let dataset = Raw.tensorSliceDataset( - components: [elements1, elements2], - outputShapes: loadTensorShapeArray()) - check(dataset: dataset) -} - -DynamicAttributeTests.testAllBackends("NormalAttribute Array") { - let elements1 = Tensor([[1], [2]]) - let elements2 = Tensor([[1, 1], [2, 2]]) - let dataset = Raw.tensorSliceDataset( - components: [elements1, elements2], - outputShapes: loadOptionalTensorShapeArray()) - check(dataset: dataset) -} - -DynamicAttributeTests.testAllBackends("NormalAttribute Array") { - // "ParseSingleExample" is the easiest-to-test Op with a list(string) attr, - // so we use "ParseSingleExample" for this test. - - // Create a StringTensor containing the serialized bytes of an example - // with features {"a": [1.0], "b": [2.0], "c": [3.0]}. - let exampleBytesBase64 = StringTensor( - "Ci0KDQoBYRIIEgYKBAAAgD8KDQoBYhIIEgYKBAAAAEAKDQoBYxIIEgYKBAAAQEA=") - let exampleBytes = Raw.decodeBase64(exampleBytesBase64) - - struct Empty: TensorGroup {} - - // Call "ParseSingleExample" with the "dense_keys" list(string) attr. We - // only select 2 out of the 3 keys from the example, to verify that the - // "dense_keys" argument is actually having an effect. - let parsed: ( - sparseIndices: [Tensor], - sparseValues: Empty, - sparseShapes: [Tensor], - denseValues: [Tensor] - ) = Raw.parseSingleExample( - serialized: exampleBytes, - denseDefaults: [Tensor([0]), Tensor([0])], - numSparse: Int64(0), - sparseKeys: loadStringArrayEmpty(), - denseKeys: loadStringArrayAB(), - denseShapes: [TensorShape([1]), TensorShape([1])]) - - expectEqual(ShapedArray([1]), parsed.denseValues[0].array) - expectEqual(ShapedArray([2]), parsed.denseValues[1].array) -} -#endif // !CUDA - -DynamicAttributeTests.testAllBackends("TFDataTypeAttribute TensorDataType") { - let t1 = Tensor(-1) - let t1Result = Raw.abs(t1) - expectEqual(1, t1Result.scalar!) - - let t2 = Tensor(-2) - let t2Result = Raw.abs(t2) - expectEqual(2, t2Result.scalar!) -} - -#if !CUDA -// TensorSliceDataset not available on GPU. -DynamicAttributeTests.testAllBackends("TFDataTypeAttribute Array") { - let elements1 = Tensor([[1], [2]]) - let elements2 = Tensor([[1, 1], [2, 2]]) - let dataset = Raw.tensorSliceDataset( - components: [elements1, elements2], - outputShapes: [nil, nil]) - check(dataset: dataset) -} -#endif // !CUDA - -DynamicAttributeTests.testAllBackends("ShapeAttribute TensorShape") { - let t = Tensor([5.0]) - let result = Raw.ensureShape(t, shape: loadShape()) - expectEqual(t, result) -} - -DynamicAttributeTests.testAllBackends("ShapeAttribute TensorShape? non-nil") { - let t = Tensor([5.0]) - let result = Raw.ensureShape(t, shape: loadOptionalShape()) - expectEqual(t, result) -} - -DynamicAttributeTests.testAllBackends("ShapeAttribute TensorShape? nil") { - let t = Tensor([5.0]) - let result = Raw.ensureShape(t, shape: loadUnknownShape()) - expectEqual(t, result) -} - -// A tensor typed attribute with a scalar string value -DynamicAttributeTests.testAllBackends("StringTensorAttribute SR-9555") { - func foo() { - _ = StringTensor("string") - } - - withDevice(.cpu) { - foo() - } -} - -runAllTests() diff --git a/test/TensorFlowRuntime/dynamic_compilation.swift b/test/TensorFlowRuntime/dynamic_compilation.swift deleted file mode 100644 index 2369f7b0eda39..0000000000000 --- a/test/TensorFlowRuntime/dynamic_compilation.swift +++ /dev/null @@ -1,53 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -import CTensorFlow -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var DynamicCompilationTests = TestSuite("DynamicCompilation") - -DynamicCompilationTests.testAllBackends("ScalarNonConst") { - _RuntimeConfig.printsDebugLog = true - func scalarInitializer_CreateHostTensor(_ x: Float) { - let y = Tensor(x) - expectNearlyEqualWithScalarTensor(x, y) - } - scalarInitializer_CreateHostTensor(1.2) -} - -DynamicCompilationTests.testAllBackends("AddFloat") { - _RuntimeConfig.printsDebugLog = true - let x = Tensor(1.0) - let y = Tensor(2.0) - let z = x + y - expectNearlyEqualWithScalarTensor(3.0, z) -} - -DynamicCompilationTests.testAllBackends("AddInt64") { - _RuntimeConfig.printsDebugLog = true - let x = Tensor(1) - let y = Tensor(2) - let z = x + y - expectEqualWithScalarTensor(3, z) -} - -DynamicCompilationTests.testAllBackends("AddInt32") { - _RuntimeConfig.printsDebugLog = true - let x = Tensor(1) - let y = Tensor(2) - let z = x + y - expectEqualWithScalarTensor(3, z) -} - -DynamicCompilationTests.testAllBackends("2Adds") { - _RuntimeConfig.printsDebugLog = true - let x = Tensor(1.0) - let y = Tensor(2.0) - let z = x + y - let w = z + z - expectNearlyEqualWithScalarTensor(6.0, w) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/dynamic_compilation_tensor_group.swift b/test/TensorFlowRuntime/dynamic_compilation_tensor_group.swift deleted file mode 100644 index b153772296964..0000000000000 --- a/test/TensorFlowRuntime/dynamic_compilation_tensor_group.swift +++ /dev/null @@ -1,124 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var TensorGroupTest = TestSuite("TensorGroup") - -/// This function cannot be compiled in graph mode because it passes a value -/// of generic type T to a #tfop. -/// T is address-only because it is generic. -@inline(never) -func tensorSliceDataset(_ t: T) -> VariantHandle { - return Raw.tensorSliceDataset(components: t, outputShapes: T._unknownShapeList) -} - -/// This function cannot be compiled in graph mode because it executes a #tfop -/// with generic return type T. -/// T is address-only because it is generic. -@inline(never) -func first(_ dataset: VariantHandle) -> T { - let iterator: ResourceHandle = Raw.iteratorV2(sharedName: "blah", - container: "earth", - outputTypes: T._typeList, outputShapes: T._unknownShapeList) - Raw.makeIterator(dataset: dataset, iterator: iterator) - return Raw.iteratorGetNext(iterator: iterator, outputShapes: T._unknownShapeList) -} - -struct Example { - let x, y: Tensor -} - -extension Example : TensorGroup { - static let _typeList: [TensorDataType] = - [Float.tensorFlowDataType, Float.tensorFlowDataType] - static let _unknownShapeList: [TensorShape?] = [nil, nil] - func _unpackTensorHandles(into address: UnsafeMutablePointer?) { - address!.advanced(by: 0).initialize(to: x.handle._cTensorHandle) - address!.advanced(by: 1).initialize(to: y.handle._cTensorHandle) - } - init(_owning tensorHandles: UnsafePointer?) { - x = Tensor(handle: TensorHandle(_owning: tensorHandles!.advanced(by: 0).pointee)) - y = Tensor(handle: TensorHandle(_owning: tensorHandles!.advanced(by: 1).pointee)) - } -} - -struct EmptyExample : Equatable {} - -extension EmptyExample : TensorGroup { - static let _unknownShapeList: [TensorShape?] = [] - static let _typeList: [TensorDataType] = [] - func _unpackTensorHandles(into address: UnsafeMutablePointer?) {} - init(_owning tensorHandles: UnsafePointer?) {} -} - -#if !CUDA -// TensorSliceDataset is not available on GPU -TensorGroupTest.testAllBackends("dataset, address-only") { - let dataset = tensorSliceDataset(Example(x: Tensor([1, 2, 3]), y: Tensor([4, 5, 6]))) - let example: Example = first(dataset) - expectEqual(1, example.x.scalar!) - expectEqual(4, example.y.scalar!) -} -#endif // CUDA - -TensorGroupTest.testAllBackends("input, address-only") { - let example = Example(x: Tensor([1, 2]), y: Tensor([3, 4])) - let packed: Tensor = Raw.pack([example.x, example.y]) - expectEqual(ShapedArray(shape: [2, 2], scalars: [1, 2, 3, 4]), packed.array) -} - -TensorGroupTest.testAllBackends("output, address-only") { - let tensor = Tensor(shape: [2, 2], scalars: [1, 2, 3, 4]) - let example: [Tensor] = Raw.unpack(value: tensor, num: 2) - expectEqual(2, example.count) - expectEqual(ShapedArray(shape: [2], scalars: [1, 2]), example[0].array) - expectEqual(ShapedArray(shape: [2], scalars: [3, 4]), example[1].array) -} - -TensorGroupTest.testAllBackends("output to empty struct, address-only") { - let tensor = Tensor(shape: [0], scalars: []) - let example: [Tensor] = Raw.unpack(value: tensor, num: 0) - expectEqual(0, example.count) -} - -// When the input is a loadable type that conforms to TensorGroup, the -// graph_op receives the input as a value (rather than an address as in the -// address-only tests), so this tests a different code path that handles -// values. -TensorGroupTest.testAllBackends("input, loadable") { - let example = Example(x: Tensor([1, 2]), y: Tensor([3, 4])) - let packed: Tensor = Raw.pack([example.x, example.y]) - expectEqual(ShapedArray(shape: [2, 2], scalars: [1, 2, 3, 4]), - packed.array) -} - -TensorGroupTest.testAllBackends("output, loadable") { - let tensor = Tensor(shape: [2, 2], scalars: [1, 2, 3, 4]) - let example: [Tensor] = Raw.unpack(value: tensor, num: 2) - expectEqual(ShapedArray(shape: [2], scalars: [1, 2]), example[0].array) - expectEqual(ShapedArray(shape: [2], scalars: [3, 4]), example[1].array) -} - -@inline(never) -func some_tf_op(n : Int) { - var arr: [Tensor] = [] - var arr_exp: [Float] = [] - for i in 0..([Float(i), 2, 3])) - arr_exp.insert(contentsOf: [Float(i), 2, 3], at: arr_exp.count) - } - - let actual: Tensor = Raw.pack(arr) - let expected = ShapedArray(shape: [n, 3], scalars: arr_exp) - expectEqual(expected, actual.array) -} - -// Test packing dynamic arrays. -TensorGroupTest.testAllBackends("input, array") { - some_tf_op(n: 32) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/gpu_negative_tests.swift b/test/TensorFlowRuntime/gpu_negative_tests.swift deleted file mode 100644 index ed6372b66d69c..0000000000000 --- a/test/TensorFlowRuntime/gpu_negative_tests.swift +++ /dev/null @@ -1,31 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// XFAIL: * - -// This test suite contains expected failure tests for GPU execution. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var GPUNegativeTests = TestSuite("GPUNegative") - -// Make sure this test suite would fail when running on any device. -GPUNegativeTests.testAllBackends("dummy_failed_test") { - expectEqual(0, 1) -} - -// This test would fail when running under GPU, because Assert does not have a -// GPU kernel. -#if CUDA -public func noAssertOnGPU() { - let a = Tensor(1.0) - _hostOp(a) - #tfop("Assert", Tensor(true), [a]) as Void - expectEqualWithScalarTensor(1, a) -} -GPUNegativeTests.testAllBackends("noAssertOnGPU", noAssertOnGPU) -#endif // CUDA - -runAllTests() - diff --git a/test/TensorFlowRuntime/key_path_iterable.swift b/test/TensorFlowRuntime/key_path_iterable.swift deleted file mode 100644 index 852e2c8c4353c..0000000000000 --- a/test/TensorFlowRuntime/key_path_iterable.swift +++ /dev/null @@ -1,193 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %target-build-swift -swift-version 5 -g %s -o %t/a.out -// RUN: %target-codesign %t/a.out -// RUN: %target-run %t/a.out -// REQUIRES: executable_test -// -// `KeyPathIterable` tests. - -import StdlibUnittest - -var KeyPathIterableTests = TestSuite("KeyPathIterable") - -struct Simple : KeyPathIterable, Equatable { - var w, b: Float -} - -struct Mixed : KeyPathIterable, Equatable { - // Mutable. - var string: String - var float: Float - // Immutable. - let int: Int -} - -struct Nested : KeyPathIterable, Equatable { - // Immutable. - let simple: Simple - // Mutable. - var mixed: Mixed -} - -struct ComplexNested : KeyPathIterable, Equatable { - var float: Float - let simple: Simple - let array: [Simple] - var dictionary: [String : Simple] -} - -// TF-123: Test type with `@differentiable` function stored property. -struct Tensor: Differentiable { - var value: Scalar -} -struct TF_123 : KeyPathIterable { - let activation1: @differentiable (Float) -> Float - let activation2: @differentiable (Tensor) -> Tensor -} - -KeyPathIterableTests.test("Simple") { - var x = Simple(w: 1, b: 2) - expectEqual([\Simple.w, \Simple.b], x.allKeyPaths) - expectEqual([\Simple.w, \Simple.b], x.allKeyPaths(to: Float.self)) - expectEqual([\Simple.w, \Simple.b], x.allWritableKeyPaths(to: Float.self)) - expectEqual([\Simple.w, \Simple.b], x.recursivelyAllKeyPaths) - expectEqual([\Simple.w, \Simple.b], x.recursivelyAllKeyPaths(to: Float.self)) - expectEqual([\Simple.w, \Simple.b], x.recursivelyAllWritableKeyPaths(to: Float.self)) - expectEqual([], x.allKeyPaths(to: Int.self)) - expectEqual([], x.recursivelyAllKeyPaths(to: Double.self)) - - for kp in x.allWritableKeyPaths(to: Float.self) { - x[keyPath: kp] += x[keyPath: kp] - } - expectEqual(Simple(w: 2, b: 4), x) -} - -KeyPathIterableTests.test("Mixed") { - var x = Mixed(string: "hello", float: .pi, int: 0) - expectEqual([\Mixed.string, \Mixed.float, \Mixed.int], x.allKeyPaths) - expectEqual([\Mixed.string, \Mixed.float, \Mixed.int], x.recursivelyAllKeyPaths) - - expectEqual([\Mixed.string], x.allKeyPaths(to: String.self)) - expectEqual([\Mixed.string], x.allWritableKeyPaths(to: String.self)) - expectEqual([\Mixed.string], x.recursivelyAllKeyPaths(to: String.self)) - expectEqual([\Mixed.string], x.recursivelyAllWritableKeyPaths(to: String.self)) - - expectEqual([\Mixed.float], x.allKeyPaths(to: Float.self)) - expectEqual([\Mixed.float], x.allWritableKeyPaths(to: Float.self)) - expectEqual([\Mixed.float], x.recursivelyAllKeyPaths(to: Float.self)) - expectEqual([\Mixed.float], x.recursivelyAllWritableKeyPaths(to: Float.self)) - - expectEqual([\Mixed.int], x.allKeyPaths(to: Int.self)) - expectEqual([], x.allWritableKeyPaths(to: Int.self)) - expectEqual([\Mixed.int], x.recursivelyAllKeyPaths(to: Int.self)) - expectEqual([], x.recursivelyAllWritableKeyPaths(to: Int.self)) - - for kp in x.allWritableKeyPaths(to: String.self) { - x[keyPath: kp] = x[keyPath: kp] + " world" - } - expectEqual(Mixed(string: "hello world", float: .pi, int: 0), x) -} - -KeyPathIterableTests.test("SimpleNested") { - var x = Nested(simple: Simple(w: 1, b: 2), - mixed: Mixed(string: "foo", float: 3, int: 0)) - - expectEqual([\Nested.simple, \Nested.mixed], x.allKeyPaths) - expectEqual([\Nested.simple, \Nested.simple.w, \Nested.simple.b, - \Nested.mixed, \Nested.mixed.string, - \Nested.mixed.float, \Nested.mixed.int], - x.recursivelyAllKeyPaths) - - expectEqual([], x.allKeyPaths(to: Float.self)) - expectEqual([], x.allKeyPaths(to: Int.self)) - expectEqual([], x.allKeyPaths(to: String.self)) - - expectEqual([], x.allWritableKeyPaths(to: Float.self)) - expectEqual([], x.allWritableKeyPaths(to: Int.self)) - expectEqual([], x.allWritableKeyPaths(to: String.self)) - - expectEqual([\Nested.simple.w, \Nested.simple.b, \Nested.mixed.float], - x.recursivelyAllKeyPaths(to: Float.self)) - expectEqual([\Nested.mixed.int], x.recursivelyAllKeyPaths(to: Int.self)) - expectEqual([\Nested.mixed.string], x.recursivelyAllKeyPaths(to: String.self)) - - expectEqual([\Nested.mixed.float], x.recursivelyAllWritableKeyPaths(to: Float.self)) - expectEqual([], x.recursivelyAllWritableKeyPaths(to: Int.self)) - expectEqual([\Nested.mixed.string], x.recursivelyAllWritableKeyPaths(to: String.self)) - - expectEqual([], x.recursivelyAllKeyPaths(to: Double.self)) - - for kp in x.recursivelyAllWritableKeyPaths(to: Float.self) { - x[keyPath: kp] *= 100 - } - let expected = Nested(simple: Simple(w: 1, b: 2), - mixed: Mixed(string: "foo", float: 300, int: 0)) - expectEqual(expected, x) -} - -KeyPathIterableTests.test("ComplexNested") { - var x = ComplexNested(float: 1, simple: Simple(w: 3, b: 4), - array: [Simple(w: 5, b: 6), Simple(w: 7, b: 8)], - dictionary: ["foo" : Simple(w: 1, b: 2), - "bar" : Simple(w: 3, b: 4)]) - expectEqual([\ComplexNested.float, \ComplexNested.simple, - \ComplexNested.array, \ComplexNested.dictionary], - x.allKeyPaths) - expectEqual([\ComplexNested.float, - \ComplexNested.simple, - \ComplexNested.simple.w, - \ComplexNested.simple.b, - \ComplexNested.array, - \ComplexNested.array[0], - \ComplexNested.array[0].w, - \ComplexNested.array[0].b, - \ComplexNested.array[1], - \ComplexNested.array[1].w, - \ComplexNested.array[1].b, - \ComplexNested.dictionary, - \ComplexNested.dictionary["bar"]!, - \ComplexNested.dictionary["bar"]!.w, - \ComplexNested.dictionary["bar"]!.b, - \ComplexNested.dictionary["foo"]!, - \ComplexNested.dictionary["foo"]!.w, - \ComplexNested.dictionary["foo"]!.b], - x.recursivelyAllKeyPaths) - expectEqual([\ComplexNested.float, - \ComplexNested.simple.w, - \ComplexNested.simple.b, - \ComplexNested.array[0].w, - \ComplexNested.array[0].b, - \ComplexNested.array[1].w, - \ComplexNested.array[1].b, - \ComplexNested.dictionary["bar"]!.w, - \ComplexNested.dictionary["bar"]!.b, - \ComplexNested.dictionary["foo"]!.w, - \ComplexNested.dictionary["foo"]!.b], - x.recursivelyAllKeyPaths(to: Float.self)) - expectEqual([\ComplexNested.float, - \ComplexNested.dictionary["bar"]!.w, - \ComplexNested.dictionary["bar"]!.b, - \ComplexNested.dictionary["foo"]!.w, - \ComplexNested.dictionary["foo"]!.b], - x.recursivelyAllWritableKeyPaths(to: Float.self)) - - for kp in x.recursivelyAllWritableKeyPaths(to: Float.self) { - x[keyPath: kp] += 1 - } - let expected = ComplexNested(float: 2, simple: Simple(w: 3, b: 4), - array: [Simple(w: 5, b: 6), Simple(w: 7, b: 8)], - dictionary: ["foo" : Simple(w: 2, b: 3), - "bar" : Simple(w: 4, b: 5)]) - expectEqual(expected, x) -} - -// Verify that `Array.DifferentiableView.allKeyPaths` uses public property -// `base` instead of private property `_base`. -KeyPathIterableTests.test("Array.DifferentiableView") { - let view = [Float].DifferentiableView([1, 2, 3]) - let keyPaths1 = (0..<3).map { i in \Array.DifferentiableView.base[i] } - let keyPaths2 = view.allDifferentiableVariables.recursivelyAllWritableKeyPaths(to: Float.self) - expectEqual(keyPaths1, keyPaths2) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/lit.local.cfg b/test/TensorFlowRuntime/lit.local.cfg deleted file mode 100644 index 01ffc574508d3..0000000000000 --- a/test/TensorFlowRuntime/lit.local.cfg +++ /dev/null @@ -1 +0,0 @@ -config.environment['SWIFT_TENSORFLOW_CPU_DEVICE_COUNT'] = '3' diff --git a/test/TensorFlowRuntime/loops.swift b/test/TensorFlowRuntime/loops.swift deleted file mode 100644 index 5b12c9fb4e069..0000000000000 --- a/test/TensorFlowRuntime/loops.swift +++ /dev/null @@ -1,107 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Loop tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var LoopsTests = TestSuite("Loops") - -// Compute 1 << 10 == 1024 -LoopsTests.testAllBackends("simpleCounterLoop_a") { - let maxCount = 10 - // a cannot be an integer tensor due to a TensorFlow Eigen bug (b/77737504). - var a = Tensor(1) - var count = 1 - - while count < maxCount { - a += a - count += 1 - } - a += a - expectNearlyEqual(1024.0, a.scalar!) -} - -// FIXME: This test is failing on macOS for reasons that are not likely related -// to graph program extraction. See SR-8541. -#if !os(macOS) -LoopsTests.testAllBackends("simpleCounterLoop_ab") { - let maxCount = 100 - var a = Tensor(0) - let b = Tensor(1) - var count = 0 - - a -= b - while count < maxCount { - a += b - count += 1 - } - a -= b - expectEqual(98, a.scalar!) -} -#endif - -// TODO: fix the disabled GPU tests below. -#if !CUDA -LoopsTests.testAllBackends("SR8164") { - @inline(never) - func SR8164(count: Int32, expectedVal: Int32) { - // TODO(b/111815938): Fix this test for google internal CUDA setup. - #if !CUDA - var a = Tensor(count) - let b = Tensor(count) - if (count == 100) { - a += b - } else { - if (count <= 50) { - a -= b - } else { - a += b - } - } - a -= b - expectEqualWithScalarTensor(expectedVal, a) - #endif //!CUDA - } - - SR8164(count: 100, expectedVal: 100) - SR8164(count: 30, expectedVal: -30) - SR8164(count: 70, expectedVal: 70) -} -#endif // CUDA - -#if !CUDA -LoopsTests.testAllBackends("SR-8191") { - let t = Tensor(1.0) - var i = 0 - repeat { - let y = t + t - print(y) - i += 1 - } while i < 10 -} -#endif // CUDA - -// FIXME: Compiler bug (b/73607740) -// error: internal error generating TensorFlow graph: -// GraphGen cannot lower a 'send' to the host yet -#if false // Remove #if when fixed. -// This is derived from a TF Eager testcase. -TensorTests.testGPU("loopsAndConditions") { - var a = Tensor(6) - var count = Tensor(0) - while (a != 1).scalar! { - if (a % 2 == 0).scalar! { - a = a / 2 - } else { - a = 3 * a + 1 - } - count += 1 - } - expectEqual(8, count.scalar) -} -#endif - -runAllTests() diff --git a/test/TensorFlowRuntime/model_autodiff_runtime.swift b/test/TensorFlowRuntime/model_autodiff_runtime.swift deleted file mode 100644 index 196afb9b3f7ed..0000000000000 --- a/test/TensorFlowRuntime/model_autodiff_runtime.swift +++ /dev/null @@ -1,203 +0,0 @@ -// RUN: %target-run-simple-swift -// REQUIRES: executable_test -// -// Machine learning API AD runtime tests. -// -// NOTE: This file contains a mini high-level machine learning library. If you -// need to test other things from the `tensorflow/swift-apis` (aka. -// `TensorFlow`) library, please **copy** their definitions here instead of -// using those APIs directly. This helps avoid circular dependencies. - -import TensorFlow -import StdlibUnittest -import TensorFlowUnittest - -var ModelADTests = TestSuite("ModelAD") - -public protocol Layer: Differentiable & KeyPathIterable - where AllDifferentiableVariables : KeyPathIterable { - associatedtype Input: Differentiable - associatedtype Output: Differentiable - @differentiable(wrt: (self, input)) - func applied(to input: Input) -> Output -} - -@_fixed_layout -public struct Dense: Layer { - public var weight: Tensor - public var bias: Tensor - public typealias Activation = - @differentiable (Tensor) -> Tensor - @noDerivative public let activation: Activation - - // FIXME(SR-9716): Remove this once the bug is fixed or worked around. - public var allKeyPaths: [PartialKeyPath] { - return [\Dense.weight, \Dense.bias] - } - - @differentiable - public func applied(to input: Tensor) -> Tensor { - return activation(matmul(input, weight) + bias) - } -} - -public extension Dense where Scalar.RawSignificand: FixedWidthInteger { - init(inputSize: Int, outputSize: Int, activation: @escaping Activation) { - self.init(weight: Tensor(glorotUniform: [inputSize, outputSize]), - bias: Tensor(zeros: [outputSize]), - activation: activation) - } -} - -@_fixed_layout -public struct Conv2D: Layer { - public var filter: Tensor - public var bias: Tensor - public typealias Activation = @differentiable (Tensor) -> Tensor - @noDerivative public let activation: Activation - @noDerivative public let strides: (Int, Int) - @noDerivative public let padding: Padding - - @differentiable - public init( - filter: Tensor, - bias: Tensor, - activation: @escaping Activation, - strides: (Int, Int), - padding: Padding - ) { - self.filter = filter - self.bias = bias - self.activation = activation - self.strides = strides - self.padding = padding - } - - @differentiable - public func applied(to input: Tensor) -> Tensor { - return activation(input.convolved2D(withFilter: filter, - strides: (1, strides.0, strides.1, 1), - padding: padding) + bias) - } -} - -public protocol Optimizer { - associatedtype Model: Layer - associatedtype Scalar: FloatingPoint - var learningRate: Scalar { get } - mutating func update(_ variables: inout Model.AllDifferentiableVariables, - along vector: Model.TangentVector) -} - -public class RiemannSGD: Optimizer - where Model.TangentVector: VectorNumeric, - Model.TangentVector.Scalar == Scalar { - public var learningRate: Scalar - - public init( - learningRate: Scalar, - modelType: Model.Type = Model.self, - scalarType: Scalar.Type = Scalar.self - ) { - self.learningRate = learningRate - } - - public func update(_ model: inout Model.AllDifferentiableVariables, - along vector: Model.TangentVector) { - model = model.moved( - along: learningRate * (.zero - vector)) - } -} - -ModelADTests.testAllBackends("SimpleLayerAD") { - let ones = Tensor(ones: [2, 2]) - let dense = Dense(inputSize: 2, outputSize: 2, activation: { $0 }) - let grad = gradient(at: dense) { dense in - dense.applied(to: ones).sum() - } - expectEqual([[2, 2], [2, 2]], grad.weight) - expectEqual([2, 2], grad.bias) -} - -ModelADTests.testAllBackends("XORTraining") { - struct Classifier: Layer { - var l1, l2: Dense - init(hiddenSize: Int) { - l1 = Dense(inputSize: 2, outputSize: hiddenSize, activation: relu) - l2 = Dense(inputSize: hiddenSize, outputSize: 1, activation: relu) - } - @differentiable(wrt: (self, input)) - func applied(to input: Tensor) -> Tensor { - let h1 = l1.applied(to: input) - return l2.applied(to: h1) - } - } - var classifier = Classifier(hiddenSize: 4) - let optimizer = RiemannSGD(learningRate: 0.2) - let x: Tensor = [[0, 0], [0, 1], [1, 0], [1, 1]] - let y: Tensor = [0, 1, 1, 0] - for _ in 0..<1000 { - let š›model = classifier.gradient { classifier -> Tensor in - let Å· = classifier.applied(to: x) - return meanSquaredError(predicted: Å·, expected: y) - } - optimizer.update(&classifier.allDifferentiableVariables, along: š›model) - } - print(classifier.applied(to: [[0, 0], [0, 1], [1, 0], [1, 1]])) -} - -ModelADTests.testAllBackends("WithRespectToModel") { - struct Foo: Differentiable where Scalar: TensorFlowFloatingPoint { - var bar: Tensor - var baz: Tensor - - @differentiable(wrt: (self, input)) - func applied(to input: Tensor) -> Tensor { - return bar + input - } - } - let x = Tensor(0) - let model = Foo(bar: x, baz: x) - let d = gradient(at: model) { model in - model.applied(to: x) - } - expectEqual(Foo.AllDifferentiableVariables(bar: Tensor(1.0), - baz: Tensor(0.0)), d) -} - -ModelADTests.testAllBackends("TF437") { - struct TF437Layer: Layer { - typealias Input = Tensor - typealias Output = Tensor - - var weight: Tensor - var bias: Tensor - - init(inputSize: Int, outputSize: Int) { - weight = Tensor(randomNormal: [inputSize, outputSize]) - bias = Tensor(zeros: [outputSize]) - } - - @differentiable(wrt: (self, input)) - func applied(to input: Tensor) -> Tensor { - let output = matmul(input, weight) + bias - return output - } - } - - func tf437Step(_ model: inout Model, - inputs: Model.Input) -> () - where Model.AllDifferentiableVariables == Model.TangentVector, - Model.Output == Tensor { - _ = gradient(at: model) { model -> Model.Output in - let logits = model.applied(to: inputs) - return logits.mean() - } - } - - var model = TF437Layer(inputSize: 1, outputSize: 1) - let inputs = Tensor(zeros: [1, 1]) - tf437Step(&model, inputs: inputs) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/models.swift b/test/TensorFlowRuntime/models.swift deleted file mode 100644 index 877ba6f402ef2..0000000000000 --- a/test/TensorFlowRuntime/models.swift +++ /dev/null @@ -1,116 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Trivial model tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var ModelTests = TestSuite("Model") - -ModelTests.testAllBackends("StraightLineXORTraining") { - // Hyper-parameters - let iterationCount = 2000 - let learningRate: Float = 0.2 - var loss = Float.infinity - - // Parameters - var w1: Tensor = [[0.69414073, 0.017726839, 0.3128785, 0.74679214], - [0.80624646, 0.8905365, 0.7302696, 0.18774611]] - var w2: Tensor = [[0.38796782], [0.18304485], [0.8680929], [0.8904212]] - var b1 = Tensor(zeros: [1, 4]) - var b2 = Tensor(zeros: [1, 1]) - - // Training data - let x: Tensor = [[0, 0], [0, 1], [1, 0], [1, 1]] - let y: Tensor = [[0], [1], [1], [0]] - - for i in 0.. - - init() { - w1 = [[0.69414073, 0.017726839, 0.3128785, 0.74679214], - [0.80624646, 0.8905365, 0.7302696, 0.18774611]] - w2 = [[0.38796782], [0.18304485], [0.8680929], [0.8904212]] - b1 = Tensor(zeros: [1, 4]) - b2 = Tensor(zeros: [1, 1]) - } - - func prediction(for x: Tensor) -> Tensor { - let o1 = sigmoid(matmul(x, w1) + b1) - return sigmoid(matmul(o1, w2) + b2) - } - - func prediction(for x: Bool, _ y: Bool) -> Bool { - let input = Tensor(Tensor([x, y]).reshaped(to: [1, 2])) - let floatPred = prediction(for: input).scalarized() - return abs(floatPred - 1) < 0.1 - } - - func loss(of prediction: Tensor, - from exampleOutput: Tensor) -> Float { - return (prediction - exampleOutput).squared() - .mean(squeezingAxes: 0, 1).scalarized() - } - - mutating func train(inputBatch x: Tensor, - outputBatch y: Tensor, - iterationCount: Int, learningRate: Float) { - for i in 0..(numpy: numpyArrayEmpty)) { - expectEqual(ShapedArray(shape: [1, 0], scalars: []), array) - } - - let numpyArrayBool = np.array([[true, false], [false, true]]) - expectNil(ShapedArray(numpy: numpyArrayBool)) - expectNil(ShapedArray(numpy: numpyArrayBool)) - if let array = expectNotNil(ShapedArray(numpy: numpyArrayBool)) { - expectEqual(ShapedArray(shape: [2, 2], - scalars: [true, false, false, true]), - array) - } - - let numpyArrayFloat = np.ones([2, 3], dtype: np.float32) - expectNil(ShapedArray(numpy: numpyArrayFloat)) - expectNil(ShapedArray(numpy: numpyArrayFloat)) - if let array = expectNotNil(ShapedArray(numpy: numpyArrayFloat)) { - expectEqual(ShapedArray(repeating: 1, shape: [2, 3]), array) - } - - let numpyArrayInt32 = np.array([[[1, 2, 3], [4, 5, 6]]], dtype: np.int32) - expectNil(ShapedArray(numpy: numpyArrayInt32)) - expectNil(ShapedArray(numpy: numpyArrayInt32)) - if let array = expectNotNil(ShapedArray(numpy: numpyArrayInt32)) { - expectEqual(ShapedArray(shape: [1, 2, 3], scalars: [1, 2, 3, 4, 5, 6]), - array) - } - - let reshaped = np.reshape(numpyArrayInt32, [2, 3] as TensorShape) - if let array = expectNotNil(ShapedArray(numpy: reshaped)) { - expectEqual(ShapedArray(shape: [2, 3], scalars: [1, 2, 3, 4, 5, 6]), - array) - } - - let numpyArray1D = np.ones(28) - let reshaped3D = np.reshape(numpyArray1D, [2, 7, 2] as TensorShape) - expectEqual(reshaped3D.shape, Python.tuple([2, 7, 2])) - let reshaped2D = np.reshape(reshaped3D, [14, 2] as TensorShape) - expectEqual(reshaped2D.shape, Python.tuple([14, 2])) - - let numpyArrayStrided = np.array([[1, 2], [1, 2]], dtype: np.int32)[ - Python.slice(Python.None), 1] - // Assert that the array has a stride, so that we're certainly testing a - // strided array. - expectNotEqual(numpyArrayStrided.__array_interface__["strides"], Python.None) - if let array = expectNotNil( - ShapedArray(numpy: numpyArrayStrided)) { - expectEqual(ShapedArray(shape: [2], scalars: [2, 2]), array) - } -} - -PythonConversionTests.test("tensor-conversion") { - guard let np = numpyModule else { return } - - let numpyArrayEmpty = np.array([[]] as [[Float]], dtype: np.float32) - if let tensor = expectNotNil(Tensor(numpy: numpyArrayEmpty)) { - expectEqual(ShapedArray(shape: [1, 0], scalars: []), tensor.array) - } - - let numpyArrayBool = np.array([[true, false], [false, true]]) - expectNil(Tensor(numpy: numpyArrayBool)) - expectNil(Tensor(numpy: numpyArrayBool)) - if let tensor = expectNotNil(Tensor(numpy: numpyArrayBool)) { - expectEqual(ShapedArray(shape: [2, 2], - scalars: [true, false, false, true]), - tensor.array) - } - - let numpyArrayFloat = np.ones([2, 3], dtype: np.float32) - expectNil(Tensor(numpy: numpyArrayFloat)) - expectNil(Tensor(numpy: numpyArrayFloat)) - if let tensor = expectNotNil(Tensor(numpy: numpyArrayFloat)) { - expectEqual(ShapedArray(repeating: 1, shape: [2, 3]), tensor.array) - } - - let numpyArrayInt32 = np.array([[[1, 2, 3], [4, 5, 6]]], dtype: np.int32) - expectNil(Tensor(numpy: numpyArrayInt32)) - expectNil(Tensor(numpy: numpyArrayInt32)) - if let tensor = expectNotNil(Tensor(numpy: numpyArrayInt32)) { - expectEqual(ShapedArray(shape: [1, 2, 3], scalars: [1, 2, 3, 4, 5, 6]), - tensor.array) - } - - let reshaped = np.reshape(numpyArrayInt32, [2, 3] as TensorShape) - if let tensor = expectNotNil(Tensor(numpy: reshaped)) { - expectEqual(ShapedArray(shape: [2, 3], scalars: [1, 2, 3, 4, 5, 6]), - tensor.array) - } - - let numpyArrayStrided = np.array([[1, 2], [1, 2]], dtype: np.int32)[ - Python.slice(Python.None), 1] - // Assert that the array has a stride, so that we're certainly testing a - // strided array. - expectNotEqual(numpyArrayStrided.__array_interface__["strides"], Python.None) - if let tensor = expectNotNil(Tensor(numpy: numpyArrayStrided)) { - expectEqual(ShapedArray(shape: [2], scalars: [2, 2]), tensor.array) - } -} - -PythonConversionTests.test("tensor-round-trip") { - guard numpyModule != nil else { return } - guard ctypesModule != nil else { return } - - let t1 = Tensor(repeating: 3.0, shape: [1,2,3,4]) - expectEqual(t1, Tensor(numpy: t1.makeNumpyArray())!) - - let t2 = Tensor(shape: [2,3], scalars: [1, 2, 3, 4, 5, 6]) - expectEqual(t2, Tensor(numpy: t2.makeNumpyArray())!) - - let t3 = Tensor(repeating: 30, shape: [8,5,4]) - expectEqual(t3, Tensor(numpy: t3.makeNumpyArray())!) -} - -PythonConversionTests.test("tensor-shape") { - let pyArray = [2, 3].pythonObject - expectEqual(pyArray, TensorShape(2, 3).pythonObject) -} -#endif - -runAllTests() diff --git a/test/TensorFlowRuntime/raw_ops.swift b/test/TensorFlowRuntime/raw_ops.swift deleted file mode 100644 index f45003906f3b7..0000000000000 --- a/test/TensorFlowRuntime/raw_ops.swift +++ /dev/null @@ -1,85 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// TensorFlow Raw Ops API tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var RawOpsTests = TestSuite("RawOps") - -@inlinable @inline(__always) -public func testPointwiseBinaryOp( - tfOp: (Tensor, Tensor) -> Tensor, - swiftOp: (Float, Float) -> T) { - let lhsScalars: [Float] = [3, 1, 4, 1, 5, 9, 2, 7] - let rhsScalars: [Float] = [2, 7, 1, 8, 2, 8, 1, 7] - let shape: TensorShape = [2, 4] - let lhs = Tensor(shape: shape, scalars: lhsScalars) - let rhs = Tensor(shape: shape, scalars: rhsScalars) - - let tfResult = tfOp(lhs, rhs) - expectEqual(ShapedArray(shape: shape.dimensions, - scalars: zip(lhsScalars, rhsScalars).map(swiftOp)), - tfResult.array) -} - -// TODO(mazare): group all these tests in a single function once this does -// not cause an XLA compilation error anymore. -// TODO(marcrasi): Pass `Raw.xxxx` directly to `testPointwiseBinaryOp` rather -// than wrapping it in a closure, once IRGen can handle non-deabstracted -// functions. -RawOpsTests.testAllBackends("AddOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.add(x, y) }, swiftOp: { $0 + $1 }) -} - -RawOpsTests.testAllBackends("SubOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.sub(x, y) }, swiftOp: { $0 - $1 }) -} - -RawOpsTests.testAllBackends("MulOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.mul(x, y) }, swiftOp: { $0 * $1 }) -} - -RawOpsTests.testAllBackends("DivOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.div(x, y) }, swiftOp: { $0 / $1 }) -} - -RawOpsTests.testAllBackends("FloorDivOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.floorDiv(x, y) }, swiftOp: { ($0 / $1).rounded(.down) }) -} - -RawOpsTests.testAllBackends("MinimumOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.minimum(x, y) }, swiftOp: min) -} - -RawOpsTests.testAllBackends("MaximumOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.maximum(x, y) }, swiftOp: max) -} - -RawOpsTests.testAllBackends("MaximumOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.maximum(x, y) }, swiftOp: max) -} - -RawOpsTests.testAllBackends("EqualOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.equal(x, y) }, swiftOp: { $0 == $1 }) -} - -RawOpsTests.testAllBackends("LessOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.less(x, y) }, swiftOp: { $0 < $1 }) -} - -RawOpsTests.testAllBackends("LessEqualOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.lessEqual(x, y) }, swiftOp: { $0 <= $1 }) -} - -RawOpsTests.testAllBackends("GreaterOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.greater(x, y) }, swiftOp: { $0 > $1 }) -} - -RawOpsTests.testAllBackends("GreaterEqualOp") { - testPointwiseBinaryOp(tfOp: { (x: Tensor, y: Tensor) in Raw.greaterEqual(x, y) }, swiftOp: { $0 >= $1 }) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/retain_release_1.swift b/test/TensorFlowRuntime/retain_release_1.swift deleted file mode 100644 index 8cff82c687de2..0000000000000 --- a/test/TensorFlowRuntime/retain_release_1.swift +++ /dev/null @@ -1,18 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Retain/release tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var RetainReleaseTests = TestSuite("RetainRelease") - -RetainReleaseTests.testAllBackends("Basic") { - let t1 = Tensor(1.2) - let _ = t1 + t1 - let _ = t1.array -} - -runAllTests() diff --git a/test/TensorFlowRuntime/sese_loop_canonicalization.swift b/test/TensorFlowRuntime/sese_loop_canonicalization.swift deleted file mode 100644 index 9fd578f56a4d9..0000000000000 --- a/test/TensorFlowRuntime/sese_loop_canonicalization.swift +++ /dev/null @@ -1,155 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var ControlFlowTests = TestSuite("ControlFlow") - -// TODO: fix the disabled GPU tests below. -#if !CUDA -func powerOfTwo(_ N: Int32) -> Tensor{ - var a = Tensor(1.0) - for _ in 0.. Tensor { - var i: Int32 = 1 - var sum = Tensor(0) - let maxCount: Int32 = 100 - while i <= maxCount { - sum += i - if (i == breakIndex) { - break - } - i += 1 - } - return sum -} - -ControlFlowTests.testAllBackends("natSumWithBreak") { - expectEqualWithScalarTensor(3, natSumWithBreak(2)) - expectEqualWithScalarTensor(55, natSumWithBreak(10)) - expectEqualWithScalarTensor(5050, natSumWithBreak(-300)) - expectEqualWithScalarTensor(5050, natSumWithBreak(100)) - expectEqualWithScalarTensor(5050, natSumWithBreak(200)) -} - -#endif // !os(macOS) - -func sumOfProducts(_ M : Int32, _ N : Int32) -> Tensor { - // Effectively computes natSum(M)*natSum(N) - var sum = Tensor(0) - for i in 1...M { - for j in 1...N { - let prod = Tensor(Float(i * j)) - sum += prod - } - } - return sum -} - -ControlFlowTests.testAllBackends("sumOfProducts") { - expectNearlyEqualWithScalarTensor(18.0, sumOfProducts(2, 3)) - expectNearlyEqualWithScalarTensor(1980.0,sumOfProducts(10, 8)) - expectNearlyEqualWithScalarTensor(11550.0,sumOfProducts(20, 10)) -} - -// A contrived example to test labeled breaks -func sumOfProductsWithBound(_ M : Int32, _ N : Int32, _ Bound : Float) -> Tensor { - // Effectively computes natSum(M)*natSum(N) upto Bound - var sum = Tensor(0) - outer_loop: for i in 1...M { - for j in 1...N { - let prod = Tensor(Float(i * j)) - sum += prod - if (sum.scalarized() >= Bound) { - break outer_loop - } - } - } - return sum -} - -ControlFlowTests.testAllBackends("sumOfProductsWithBound") { - expectNearlyEqualWithScalarTensor(6, sumOfProductsWithBound(3, 3, 4)) - expectNearlyEqualWithScalarTensor(8, sumOfProductsWithBound(2, 3, 7)) - expectNearlyEqualWithScalarTensor(8, sumOfProductsWithBound(3, 3, 7)) - expectNearlyEqualWithScalarTensor(18, sumOfProductsWithBound(3, 3, 18)) - // Effectively no bound as natSum(3) * natSum(3) is 36. - expectNearlyEqualWithScalarTensor(36, sumOfProductsWithBound(3, 3, 100)) -} - -// Disable this test in macos for now -// https://bugs.swift.org/browse/SR-8986 -#if !os(macOS) - -func doWhileLoopWithBreak(_ breakIndex:Int32) -> Tensor { - var i: Int32 = 1 - var sum = Tensor(0) - let maxCount: Int32 = 100 - repeat { - sum += i - if i == breakIndex { - break - } - i += 1 - } while i <= maxCount - return sum -} - -ControlFlowTests.testAllBackends("doWhileLoopWithBreak") { - expectEqualWithScalarTensor(3, doWhileLoopWithBreak(2)) - expectEqualWithScalarTensor(55, doWhileLoopWithBreak(10)) - expectEqualWithScalarTensor(5050, doWhileLoopWithBreak(-300)) - expectEqualWithScalarTensor(5050, doWhileLoopWithBreak(100)) - expectEqualWithScalarTensor(5050, doWhileLoopWithBreak(200)) -} - -#endif // !os(macOS) - -func nestedDoWhileLoopWithBreak( - _ breakIndex:Int32, _ repetitions: Int32) -> Tensor { - var sum = Tensor(0) - for j in 1...repetitions { - var i: Int32 = 1 - let maxCount: Int32 = 100 - repeat { - sum += i - if i == breakIndex { - break - } - i += 1 - } while i <= maxCount - } - return sum -} - -ControlFlowTests.testAllBackends("nestedDoWhileLoopWithBreak") { - expectEqualWithScalarTensor(3, nestedDoWhileLoopWithBreak(2, 1)) - expectEqualWithScalarTensor(165, nestedDoWhileLoopWithBreak(10, 3)) - expectEqualWithScalarTensor(5050, nestedDoWhileLoopWithBreak(-300, 1)) - expectEqualWithScalarTensor(10100, nestedDoWhileLoopWithBreak(100, 2)) - expectEqualWithScalarTensor(5050, nestedDoWhileLoopWithBreak(200, 1)) -} - - - -#endif // CUDA - -runAllTests() diff --git a/test/TensorFlowRuntime/sese_regions.swift b/test/TensorFlowRuntime/sese_regions.swift deleted file mode 100644 index 4b0f8a464b23c..0000000000000 --- a/test/TensorFlowRuntime/sese_regions.swift +++ /dev/null @@ -1,143 +0,0 @@ -// This test has various test cases to check that SESE regions are computed correctly. -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var SESERegionTests = TestSuite("SESERegion") - -enum TestError : Error { - case NegativeOutOfBound - case PositiveOutOfBound -} - -public func testSharedRegion(_ count : Int32) -> Tensor { - var result = Tensor(0) - do { - if count > 0 { - if count < 100 { - result += 1 - } else { - result += 2 - throw TestError.PositiveOutOfBound - } - } else { - if count > -100 { - result += 3 - } else { - result += 4 - throw TestError.NegativeOutOfBound - } - } - } catch { - result += 5 - } - return result -} - -SESERegionTests.testAllBackends("testSharedRegion") { - expectEqualWithScalarTensor(1, testSharedRegion(99)) - expectEqualWithScalarTensor(7, testSharedRegion(101)) - expectEqualWithScalarTensor(3, testSharedRegion(-99)) - expectEqualWithScalarTensor(9, testSharedRegion(-101)) -} - -// CHECK-LABEL:--- XLA CFG Canonicalize: {{.*}}testSharedRegion{{.*}} -// CHECK:[sequence -// CHECK: {condition Header: {{bb[0-9]+}} -// CHECK: {condition Header: {{bb[0-9]+}} -// CHECK: block {{bb[0-9]+}} -// CHECK: [sequence -// CHECK: block {{bb[0-9]+}} -// CHECK: {shared -// CHECK: block [[FBLK:bb[0-9]+]] -// CHECK: }]} -// CHECK: {condition Header: {{bb[0-9]+}} -// CHECK: block {{bb[0-9]+}} -// CHECK: [sequence -// CHECK: block {{bb[0-9]+}} -// CHECK: {shared -// CHECK: block [[FBLK]] -// CHECK: }]}} -// CHECK: block {{bb[0-9]+}}] -// CHECK:--- XLA CFG Canonicalize end - -public func testSharedRegionWithLoop(_ count : Int32) -> Tensor { - var result = Tensor(0) - do { - if count > 0 { - if count < 100 { - result += 1 - } else { - result += 2 - throw TestError.PositiveOutOfBound - } - } else { - if count > -100 { - result += 3 - } else { - result += 4 - throw TestError.NegativeOutOfBound - } - } - } catch { - var i: Int32 = 0 - while i < 2 { - result += 5 - i += 1 - } - } - return result -} - -SESERegionTests.testAllBackends("testSharedRegionWithLoop") { - expectEqualWithScalarTensor(1, testSharedRegionWithLoop(99)) -#if !CUDA - // TODO fix. - expectEqualWithScalarTensor(12, testSharedRegionWithLoop(101)) -#endif // !CUDA - expectEqualWithScalarTensor(3, testSharedRegionWithLoop(-99)) -#if !CUDA - // TODO fix. - expectEqualWithScalarTensor(14, testSharedRegionWithLoop(-101)) -#endif // !CUDA -} - - -// CHECK-LABEL:--- XLA CFG Canonicalize: {{.*}}testSharedRegionWithLoop{{.*}} -// CHECK:[sequence -// CHECK: {condition Header: {{bb[0-9]+}} -// CHECK: {condition Header: {{bb[0-9]+}} -// CHECK: block {{bb[0-9]+}} -// CHECK: [sequence -// CHECK: block {{bb[0-9]+}} -// CHECK: {shared -// CHECK: [sequence -// CHECK: -// CHECK: block [[LATCH:bb[0-9]+]] -// CHECK: }]} -// CHECK: {condition Header: {{bb[0-9]+}} -// CHECK: block {{bb[0-9]+}} -// CHECK: [sequence -// CHECK: block {{bb[0-9]+}} -// CHECK: {shared -// CHECK: [sequence -// CHECK: -// CHECK: block [[LATCH]]] -// CHECK: }]}} -// CHECK: block {{bb[0-9]+}}] -// CHECK:--- XLA CFG Canonicalize end - -runAllTests() diff --git a/test/TensorFlowRuntime/shaped_array.swift b/test/TensorFlowRuntime/shaped_array.swift deleted file mode 100644 index 8c8228f18c1ae..0000000000000 --- a/test/TensorFlowRuntime/shaped_array.swift +++ /dev/null @@ -1,138 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// ShapedArray and ShapedArraySlice API tests. - -import TensorFlow -import StdlibUnittest - -var ShapedArrayTests = TestSuite("ShapedArrayTests") - -// TODO: add full Collection scalar test suite. -// Putting off for now since API is not finalized and writing all the tests is -// time consuming. - -ShapedArrayTests.test("AssociatedTypes") { - expectRandomAccessCollectionAssociatedTypes( - collectionType: ShapedArray.self, - iteratorType: ShapedArray.Iterator.self, - subSequenceType: ShapedArraySlice.self, - indexType: Int.self, - indicesType: Range.self - ) -} - -ShapedArrayTests.test("Initializers") { - let scalar = ShapedArray(shape: [], scalars: [1.0]) - expectEqual([], scalar.shape) - expectEqual([1.0], scalar.scalars) - - let x = ShapedArray(shape: [2, 3], scalars: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) - expectEqual([2, 3], x.shape) - expectEqual([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], x.scalars) - - let y: ShapedArraySlice = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] - expectEqual([2, 3], y.shape) - expectEqual([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], y.scalars) -} - -ShapedArrayTests.test("Indexing") { - let tensor = ShapedArray(shape: [3, 4, 5], scalars: Array(0..<60)) - - /// Test shapes. - expectEqual([4, 5], tensor[0].shape) - expectEqual([5], tensor[0][0].shape) - expectEqual([], tensor[0][0][0].shape) - expectEqual([2, 4, 5], tensor[0..<2].shape) - expectEqual([3, 5], tensor[0][0..<3].shape) - expectEqual([4, 5], tensor[0..<3][0].shape) - - /// Test indices. - expectEqual(0..<4, tensor[0].indices) - expectEqual(0..<5, tensor[0][0].indices) - expectEqual(0..<0, tensor[0][0][0].indices) - expectEqual(0..<2, tensor[0..<2].indices) - expectEqual(0..<3, tensor[0][0..<3].indices) - expectEqual(0..<4, tensor[0..<3][0].indices) - - /// Test element tensor scalars. - expectEqual(Array(0..<20), tensor[0].scalars) - expectEqual(Array(40..<60), tensor[2].scalars) - expectEqual(Array(0..<5), tensor[0][0].scalars) - expectEqual(Array(35..<40), tensor[1][3].scalars) - expectEqual([43], tensor[2][0][3].scalars) - expectEqual([37], tensor[1][3][2].scalars) - - /// Test subtensor scalars. - expectEqual(Array(20..<40), tensor[1..<2].scalars) - expectEqual(Array(20..<30), tensor[1][0..<2].scalars) - expectEqual(Array(45..<50), tensor[2][1..<2].scalars) - expectEqual(Array(3..<5), tensor[0][0][3..<5].scalars) - - // TODO: add more indexing tests, such as slice of slice -} - -ShapedArrayTests.test("ElementMutation") { - var tensor = ShapedArray(shape: [3, 4, 5], scalars: Array(0..<60)) - tensor[0] = ShapedArraySlice(repeating: 1, shape: [4, 5]) - expectEqual(Array(repeating: 1, count: 20) + Array(20..<60), tensor.scalars) - - tensor[0..<2] = ShapedArraySlice(shape: [2, 4, 5], scalars: Array(0..<40)) - expectEqual(Array(0..<60), tensor.scalars) - - tensor[0][1..<3] = ShapedArraySlice(shape: [2, 5], scalars: Array(0..<10)) - expectEqual(Array(0..<5) + Array(0..<10) + Array(15..<60), tensor.scalars) - - for scalarIndex in tensor[0][0].indices { - tensor[0][0][scalarIndex] = ShapedArraySlice(scalarIndex - 5) - } - expectEqual(Array(-5..<10) + Array(15..<60), tensor.scalars) - - tensor[0][1][1].scalar = 100 - expectEqual(100, tensor[0][1][1].scalar) -} - -ShapedArrayTests.test("ScalarMutation") { - var x = ShapedArray(shape: [2, 3], scalars: [0.0, 2.0, 3.0, 0.0, 0.0, 0.0]) - x.scalars[0] = 1.0 - expectEqual([1.0, 2.0, 3.0, 0.0, 0.0, 0.0], x.scalars) - x.scalars[3...] = ArraySlice([4.0, 5.0, 6.0]) - expectEqual([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], x.scalars) - - var y = ShapedArraySlice(shape: [2, 3], scalars: [0.0, 2.0, 3.0, 0.0, 0.0, 0.0]) - y.scalars[0] = 1.0 - expectEqual([1.0, 2.0, 3.0, 0.0, 0.0, 0.0], y.scalars) - y.scalars[3...] = ArraySlice([4.0, 5.0, 6.0]) - expectEqual([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], y.scalars) -} - -struct Foo : Equatable, Hashable { - var int: Int = 1337 - var float: Float = .pi -} - -ShapedArrayTests.test("Equatable") { - checkEquatable([ShapedArray(shape: [], scalars: [1.0])], oracle: { $0 == $1 }) - checkEquatable([ShapedArray(repeating: true, shape: [3, 4, 5])], oracle: { $0 == $1 }) - checkEquatable([ShapedArray(repeating: Foo(), shape: [3, 4, 5])], oracle: { $0 == $1 }) - checkEquatable([ShapedArray(shape: [2, 3], scalars: Array(0..<6))], oracle: { $0 == $1 }) - - checkEquatable([ShapedArraySlice(shape: [], scalars: [1.0])], oracle: { $0 == $1 }) - checkEquatable([ShapedArraySlice(repeating: true, shape: [3, 4, 5])], oracle: { $0 == $1 }) - checkEquatable([ShapedArraySlice(repeating: Foo(), shape: [3, 4, 5])], oracle: { $0 == $1 }) - checkEquatable([ShapedArraySlice(shape: [2, 3], scalars: Array(0..<6))], oracle: { $0 == $1 }) -} - -ShapedArrayTests.test("Hashable") { - checkHashable([ShapedArray(shape: [], scalars: [1.0])], equalityOracle: { $0 == $1 }) - checkHashable([ShapedArray(repeating: true, shape: [3, 4, 5])], equalityOracle: { $0 == $1 }) - checkHashable([ShapedArray(repeating: Foo(), shape: [3, 4, 5])], equalityOracle: { $0 == $1 }) - checkHashable([ShapedArray(shape: [2, 3], scalars: Array(0..<6))], equalityOracle: { $0 == $1 }) - - checkHashable([ShapedArraySlice(shape: [], scalars: [1.0])], equalityOracle: { $0 == $1 }) - checkHashable([ShapedArraySlice(repeating: true, shape: [3, 4, 5])], equalityOracle: { $0 == $1 }) - checkHashable([ShapedArraySlice(repeating: Foo(), shape: [3, 4, 5])], equalityOracle: { $0 == $1 }) - checkHashable([ShapedArraySlice(shape: [2, 3], scalars: Array(0..<6))], equalityOracle: { $0 == $1 }) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/string.swift b/test/TensorFlowRuntime/string.swift deleted file mode 100644 index 01cdf6f99ac9c..0000000000000 --- a/test/TensorFlowRuntime/string.swift +++ /dev/null @@ -1,35 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// String Tensor tests. - -import TensorFlow -import StdlibUnittest - -var StringTensorTests = TestSuite("String") - -StringTensorTests.test("StringComparison") { - let t1 = StringTensor("foo") - let result1 = t1.elementsEqual(t1) - expectEqual(ShapedArray(shape: [], scalars: [true]), result1.array) - - let t2 = StringTensor(["foo", "bar"]) - let result2 = t2.elementsEqual(t2) - expectEqual(ShapedArray(shape: [2], scalars: [true, true]), - result2.array) - - let t3 = StringTensor(["different", "bar"]) - let result3 = t2.elementsEqual(t3) - expectEqual(ShapedArray(shape: [2], scalars: [false, true]), - result3.array) -} - -StringTensorTests.test("StringTFOP") { - let encoded = StringTensor("aGVsbG8gd29ybGQ=") - let decoded: StringTensor = Raw.decodeBase64(encoded) - let expectedDecoded = StringTensor("hello world") - let comparison = expectedDecoded.elementsEqual(decoded) - expectEqual(ShapedArray(shape: [], scalars: [true]), comparison.array) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/string_description.swift b/test/TensorFlowRuntime/string_description.swift deleted file mode 100644 index 4d36b0c43fbf0..0000000000000 --- a/test/TensorFlowRuntime/string_description.swift +++ /dev/null @@ -1,257 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// `Tensor` string description tests. - -import TensorFlow -import StdlibUnittest - -// Note: Foundation is needed for `String.contains(_: String)`. -#if canImport(Foundation) -import Foundation -#endif - -var StringDescriptionTests = TestSuite("StringDescriptionTests") - -#if !CUDA -StringDescriptionTests.test("Empty") { - let empty = Tensor([] as [Float]) - expectEqual("[]", empty.description) -} - -StringDescriptionTests.test("Scalar") { - do { - let scalar = Tensor(1) - expectEqual("1", scalar.description) - } - - do { - let scalar = Tensor(-3.14) - expectEqual("-3.14", scalar.description) - } -} - -StringDescriptionTests.test("Vector") { - do { - let vector = Tensor(ones: [4]) - expectEqual("[1, 1, 1, 1]", vector.description) - } - - do { - var vector = Tensor([1, 2, 3, 4]) - expectEqual("[1.0, 2.0, 3.0, 4.0]", vector.description) - vector[1] = Tensor(-2) - expectEqual("[ 1.0, -2.0, 3.0, 4.0]", vector.description) - } - - // Test long vector (above 1000 scalar threshold). - do { - let vector = Tensor(repeating: 3, shape: [1001]) - expectEqual("[3.0, 3.0, 3.0, ..., 3.0, 3.0, 3.0]", vector.description) - } -} - -StringDescriptionTests.test("Matrix") { - do { - var matrix = Tensor(ones: [2, 2]) - expectEqual(""" - [[1, 1], - [1, 1]] - """, matrix.description) - // Increase max scalar length. - // Check that scalars are still aligned (properly left padded). - matrix[0][1] = Tensor(-1) - expectEqual(""" - [[ 1, -1], - [ 1, 1]] - """, matrix.description) - } - - do { - var matrix = Tensor(ones: [2, 2]) - expectEqual(""" - [[1.0, 1.0], - [1.0, 1.0]] - """, matrix.description) - // Increase max scalar length. - // Check that scalars are still aligned (properly left padded). - matrix[0][1] = Tensor(-1) - expectEqual(""" - [[ 1.0, -1.0], - [ 1.0, 1.0]] - """, matrix.description) - } -} - -StringDescriptionTests.test("HigherRankTensors") { - do { - let tensor = Tensor(ones: [1, 1, 1, 1]) - expectEqual("[[[[1]]]]", tensor.description) - } - - do { - let tensor = Tensor(ones: [3, 4, 5]) - expectEqual(""" - [[[1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1]], - - [[1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1]], - - [[1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1]]] - """, tensor.description) - } - - // Test large tensor (above 1000 scalar threshold). - do { - let tensor = Tensor(ones: [10, 10, 11]) - expectEqual(""" - [[[1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - ..., - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1]], - - [[1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - ..., - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1]], - - [[1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - ..., - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1]], - - ..., - - [[1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - ..., - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1]], - - [[1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - ..., - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1]], - - [[1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - ..., - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1], - [1, 1, 1, ..., 1, 1, 1]]] - """, tensor.description) - } -} - -// Test example random initialization (longer scalars). -// Hard-coded test tensors generated via `Tensor(randomNormal:)`. -StringDescriptionTests.test("RandomScalars") { - do { - var matrix = Tensor( - [[ 1.1621541, -0.39326498], - [ -1.5391855, -1.11794]] - ) - expectEqual(""" - [[ 1.1621541, -0.39326498], - [ -1.5391855, -1.11794]] - """, matrix.description) - // Use one short scalar. - // Check that scalars are still aligned (properly left padded). - matrix[0][1] = Tensor(-1) - expectEqual(""" - [[ 1.1621541, -1.0], - [-1.5391855, -1.11794]] - """, matrix.description) - } - - do { - var matrix = Tensor( - [[ 0.3374860907024739, 0.1805635511143933], - [ 0.1861132028235124, -0.08243178459215775]] - ) - expectEqual(""" - [[ 0.3374860907024739, 0.1805635511143933], - [ 0.1861132028235124, -0.08243178459215775]] - """, matrix.description) - // Use one short scalar. - // Check that scalars are still aligned (properly left padded). - matrix[0][1] = Tensor(-1) - expectEqual(""" - [[ 0.3374860907024739, -1.0], - [ 0.1861132028235124, -0.08243178459215775]] - """, matrix.description) - } -} - -StringDescriptionTests.test("DescriptionConfiguration") { - // Test `lineWidth` configuration. - let vector = Tensor(ones: [10]) - expectEqual("[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]", - vector.description) - expectEqual(""" - [1.0, 1.0, - 1.0, 1.0, - 1.0, 1.0, - 1.0, 1.0, - 1.0, 1.0] - """, vector.description(lineWidth: 6)) - - // Test `summarizing` configuration. - // NOTE: `String.contains(_ substring: String)` requires Foundation. -#if canImport(Foundation) - let longVector = Tensor(ones: [1001]) - expectTrue(longVector.description.contains("...")) - expectFalse(longVector.description(summarizing: false).contains("...")) -#endif // canImport(Foundation) - - // Test `edgeElementCount` configuration. - var tallMatrix = Tensor(ones: [50, 2]) - expectEqual(""" - [[1.0, 1.0], - [1.0, 1.0], - [1.0, 1.0], - ..., - [1.0, 1.0], - [1.0, 1.0], - [1.0, 1.0]] - """, - tallMatrix.description(summarizing: true)) - expectEqual(""" - [[1.0, 1.0], - ..., - [1.0, 1.0]] - """, - tallMatrix.description(edgeElementCount: 1, summarizing: true)) -} - -StringDescriptionTests.test("FullDescription") { - let vector = Tensor(ones: [2, 2, 2]) - expectEqual("[[[1.0, 1.0], [1.0, 1.0]], [[1.0, 1.0], [1.0, 1.0]]]", - vector.fullDescription) -} -#endif // !CUDA - -runAllTests() diff --git a/test/TensorFlowRuntime/tensor.swift b/test/TensorFlowRuntime/tensor.swift deleted file mode 100644 index 61e4460b522c4..0000000000000 --- a/test/TensorFlowRuntime/tensor.swift +++ /dev/null @@ -1,789 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Tensor API tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var TensorTests = TestSuite("Tensor") - -TensorTests.testAllBackends("Initializers") { - let scalar = Tensor(1) - let matrix: Tensor = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] - let broadcastScalar = Tensor(broadcasting: 10, rank: 3) - let some4d = Tensor(shape: [2, 1, 2, 1], - scalars: AnyRandomAccessCollection([2, 3, 4, 5])) - expectEqual(ShapedArray(shape: [2, 1, 2, 1], scalars: [2, 3, 4, 5]), - some4d.array) - expectEqual(ShapedArray(shape: [], scalars: [1]), scalar.array) - expectEqual(ShapedArray(shape: [2, 3], scalars: [1, 2, 3, 4, 5, 6]), - matrix.array) - expectEqual(ShapedArray(shape: [1, 1, 1], scalars: [10]), - broadcastScalar.array) -} - -TensorTests.testAllBackends("FactoryInitializers") { - let x = Tensor(ones: [1, 10]) - expectEqual(ShapedArray(repeating: 1, shape: [1, 10]), x.array) -} - -TensorTests.testAllBackends("NumericInitializers") { - let x = Tensor(oneHotAtIndices: [0, 2, -1, 1], depth: 3) - expectEqual(ShapedArray(shape: [4, 3], scalars: [1, 0, 0, - 0, 0, 1, - 0, 0, 0, - 0, 1, 0]), - x.array) -} - -TensorTests.testAllBackends("ScalarToTensorConversion") { - let tensor = Tensor(broadcasting: 42, rank: 4) - expectEqual([1, 1, 1, 1], tensor.shape) - expectEqual([42], tensor.scalars) -} - -TensorTests.testAllBackends("ArrayConversion") { - let array3D = ShapedArray(repeating: 1.0, shape: [2, 3, 4]) - let tensor3D = Tensor(array3D) - expectEqual(array3D, tensor3D.array) -} - -TensorTests.testAllBackends("DataTypeCast_NonTPU") { - let x = Tensor(ones: [5, 5]) - let ints = Tensor(x) - let floats = Tensor(x) - let i8s = Tensor(floats) - expectEqual(ShapedArray(repeating: 1, shape: [5, 5]), ints.array) - expectEqual(ShapedArray(repeating: 1, shape: [5, 5]), floats.array) - expectEqual(ShapedArray(repeating: 1, shape: [5, 5]), i8s.array) -} - -TensorTests.testAllBackends("DataTypeCast_TPU") { - let x = Tensor(ones: [5, 5]) - let ints = Tensor(x) - let floats = Tensor(x) - let u32s = Tensor(floats) - expectEqual(ShapedArray(repeating: 1, shape: [5, 5]), ints.array) - expectEqual(ShapedArray(repeating: 1, shape: [5, 5]), floats.array) - expectEqual(ShapedArray(repeating: 1, shape: [5, 5]), u32s.array) -} - -TensorTests.testAllBackends("BoolToNumericCast_NonTPU") { - let bools = Tensor(shape: [2, 2], scalars: [true, false, true, false]) - let ints = Tensor(bools) - let floats = Tensor(bools) - let i8s = Tensor(bools) - expectEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), ints.array) - expectEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), floats.array) - expectEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), i8s.array) -} - -TensorTests.testAllBackends("ElementIndexing") { - // NOTE: cannot test multiple `Tensor.shape` or `Tensor.scalars` directly - // until send and receive are implemented (without writing a bunch of mini - // tests). Instead, `Tensor.array` is called to make a ShapedArray host copy - // and the ShapedArray is tested. - let tensor3D = Tensor(shape: [3, 4, 5], - scalars: Array(stride(from: 0.0, to: 60, by: 1))) - let element2D = tensor3D[2] - let element1D = tensor3D[1][3] - let element0D = tensor3D[2][0][3] - - let array2D = element2D.array - let array1D = element1D.array - let array0D = element0D.array - - /// Test shapes - expectEqual([4, 5], array2D.shape) - expectEqual([5], array1D.shape) - expectEqual([], array0D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 40.0, to: 60, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 35.0, to: 40, by: 1)), array1D.scalars) - expectEqual([43], array0D.scalars) -} - -#if !CUDA -// TODO(https://bugs.swift.org/browse/TF-469): This test fails in GPU mode. -TensorTests.testAllBackends("ElementIndexingAssignment") { - // NOTE: cannot test multiple `Tensor.shape` or `Tensor.scalars` directly - // until send and receive are implemented (without writing a bunch of mini - // tests). Instead, `Tensor.array` is called to make a ShapedArray host copy - // and the ShapedArray is tested. - var tensor3D = Tensor(shape: [3, 4, 5], - scalars: Array(stride(from: 0.0, to: 60, by: 1))) - tensor3D[2] = Tensor(shape: [4, 5], - scalars: Array(stride(from: 20.0, to: 40, by: 1))) - let element2D = tensor3D[2] - let element1D = tensor3D[1][3] - let element0D = tensor3D[2][0][3] - - let array2D = element2D.array - let array1D = element1D.array - let array0D = element0D.array - - /// Test shapes - expectEqual([4, 5], array2D.shape) - expectEqual([5], array1D.shape) - expectEqual([], array0D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 20.0, to: 40, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 35.0, to: 40, by: 1)), array1D.scalars) - expectEqual([23], array0D.scalars) -} -#endif // !CUDA - -TensorTests.testAllBackends("NestedElementIndexing") { - // NOTE: This test could use a clearer name, along with other "indexing" - // tests. Note to update corresponding test names in other files - // (shaped_array.test) as well. - let tensor3D = Tensor(shape: [3, 4, 5], - scalars: Array(stride(from: 0.0, to: 60, by: 1))) - let element1D = tensor3D[1, 3] - let element0D = tensor3D[2, 0, 3] - - let array1D = element1D.array - let array0D = element0D.array - - /// Test shapes - expectEqual([5], array1D.shape) - expectEqual([], array0D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 35.0, to: 40, by: 1)), array1D.scalars) - expectEqual([43], array0D.scalars) -} - -TensorTests.testAllBackends("SliceIndexing") { - // NOTE: cannot test `Tensor.shape` or `Tensor.scalars` directly until send - // and receive are implemented (without writing a bunch of mini tests). - // Instead, `Tensor.array` is called to make a ShapedArray host copy and the - // ShapedArray is tested instead. - let tensor3D = Tensor(shape: [3, 4, 5], - scalars: Array(stride(from: 0.0, to: 60, by: 1))) - let slice3D = tensor3D[2...] - let slice2D = tensor3D[1][0..<2] - let slice1D = tensor3D[0][0][3..<5] - - let array3D = slice3D.array - let array2D = slice2D.array - let array1D = slice1D.array - - /// Test shapes - expectEqual([1, 4, 5], array3D.shape) - expectEqual([2, 5], array2D.shape) - expectEqual([2], array1D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 40.0, to: 60, by: 1)), array3D.scalars) - expectEqual(Array(stride(from: 20.0, to: 30, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 3.0, to: 5, by: 1)), array1D.scalars) -} - -TensorTests.testAllBackends("SliceUpdate") { - var t1 = Tensor([[1, 2, 3], [4, 5, 6]]) - t1[0] = Tensor(zeros: [3]) - expectEqual(ShapedArray(shape:[2, 3], scalars: [0, 0, 0, 4, 5, 6]), t1.array) - var t2 = t1 - t2[0][2] = Tensor(3) - expectEqual(ShapedArray(shape:[2, 3], scalars: [0, 0, 3, 4, 5, 6]), t2.array) - var t3 = Tensor([[true, true, true], [false, false, false]]) - t3[0][1] = Tensor(false) - expectEqual(ShapedArray(shape:[2, 3], - scalars: [true, false, true, false, false, false]), - t3.array) - var t4 = Tensor([[true, true, true], [false, false, false]]) - t4[0] = Tensor(repeating: false, shape: [3]) - expectEqual(ShapedArray(repeating: false, shape: [2, 3]), t4.array) -} - - -#if !CUDA -// TODO(https://bugs.swift.org/browse/TF-469): This test fails in GPU mode. -TensorTests.testAllBackends("SliceIndexingAssignment") { - // NOTE: cannot test `Tensor.shape` or `Tensor.scalars` directly until send - // and receive are implemented (without writing a bunch of mini tests). - // Instead, `Tensor.array` is called to make a ShapedArray host copy and the - // ShapedArray is tested instead. - var tensor3D = Tensor( - shape: [3, 4, 5], scalars: Array(stride(from: 0.0, to: 60, by: 1))) - tensor3D[2, 0..<5, 0..<6] = Tensor( - shape: [4, 5], scalars: Array(stride(from: 20.0, to: 40, by: 1))) - let slice3D = tensor3D[2...] - let slice2D = tensor3D[1][0..<2] - let slice1D = tensor3D[0][0][3..<5] - - let array3D = slice3D.array - let array2D = slice2D.array - let array1D = slice1D.array - - /// Test shapes - expectEqual([1, 4, 5], array3D.shape) - expectEqual([2, 5], array2D.shape) - expectEqual([2], array1D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 20.0, to: 40, by: 1)), array3D.scalars) - expectEqual(Array(stride(from: 20.0, to: 30, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 3.0, to: 5, by: 1)), array1D.scalars) -} -#endif // !CUDA - -#if !CUDA -// TODO(https://bugs.swift.org/browse/TF-469): This test fails in GPU mode. -TensorTests.testAllBackends("EllipsisIndexing") { - // NOTE: cannot test `Tensor.shape` or `Tensor.scalars` directly until send - // and receive are implemented (without writing a bunch of mini tests). - // Instead, `Tensor.array` is called to make a ShapedArray host copy and the - // ShapedArray is tested instead. - var tensor3D = Tensor( - shape: [3, 4, 5], scalars: Array(stride(from: 0.0, to: 60, by: 1))) - tensor3D[2, TensorRange.ellipsis] = Tensor( - shape: [4, 5], scalars: Array(stride(from: 20.0, to: 40, by: 1))) - let slice3D = tensor3D[2..., TensorRange.ellipsis] - let slice2D = tensor3D[1][0..<2] - let slice1D = tensor3D[0][0][3..<5] - - let array3D = slice3D.array - let array2D = slice2D.array - let array1D = slice1D.array - - /// Test shapes - expectEqual([1, 4, 5], array3D.shape) - expectEqual([2, 5], array2D.shape) - expectEqual([2], array1D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 20.0, to: 40, by: 1)), array3D.scalars) - expectEqual(Array(stride(from: 20.0, to: 30, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 3.0, to: 5, by: 1)), array1D.scalars) -} -#endif // !CUDA - -TensorTests.testAllBackends("NewAxisIndexing") { - // NOTE: cannot test `Tensor.shape` or `Tensor.scalars` directly until send - // and receive are implemented (without writing a bunch of mini tests). - // Instead, `Tensor.array` is called to make a ShapedArray host copy and the - // ShapedArray is tested instead. - let tensor3D = Tensor( - shape: [3, 4, 5], scalars: Array(stride(from: 0.0, to: 60, by: 1))) - let newAxis = TensorRange.newAxis - let ellipsis = TensorRange.ellipsis - let slice3D = tensor3D[2..., newAxis, ellipsis] - let slice2D = tensor3D[1, newAxis][0..<1, 0..<2] - let slice1D = tensor3D[0][newAxis, 0][0..<1, 3..<5, newAxis] - - let array3D = slice3D.array - let array2D = slice2D.array - let array1D = slice1D.array - - /// Test shapes - expectEqual([1, 1, 4, 5], array3D.shape) - expectEqual([1, 2, 5], array2D.shape) - expectEqual([1, 2, 1], array1D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 40.0, to: 60, by: 1)), array3D.scalars) - expectEqual(Array(stride(from: 20.0, to: 30, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 3.0, to: 5, by: 1)), array1D.scalars) -} - -TensorTests.testAllBackends("SqueezeAxisIndexing") { - // NOTE: cannot test `Tensor.shape` or `Tensor.scalars` directly until send - // and receive are implemented (without writing a bunch of mini tests). - // Instead, `Tensor.array` is called to make a ShapedArray host copy and the - // ShapedArray is tested instead. - let tensor3D = Tensor( - shape: [3, 4, 5], scalars: Array(stride(from: 0.0, to: 60, by: 1))) - let newAxis = TensorRange.newAxis - let ellipsis = TensorRange.ellipsis - let squeezeAxis = TensorRange.squeezeAxis - let slice3D = tensor3D[2..., newAxis, ellipsis][squeezeAxis, squeezeAxis] - let slice2D = tensor3D[1, newAxis][squeezeAxis, 0..<2] - let slice1D = tensor3D[0..<1, 0, 3..<5, newAxis][ - squeezeAxis, ellipsis, squeezeAxis] - - let array3D = slice3D.array - let array2D = slice2D.array - let array1D = slice1D.array - - /// Test shapes - expectEqual([4, 5], array3D.shape) - expectEqual([2, 5], array2D.shape) - expectEqual([2], array1D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 40.0, to: 60, by: 1)), array3D.scalars) - expectEqual(Array(stride(from: 20.0, to: 30, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 3.0, to: 5, by: 1)), array1D.scalars) -} - -TensorTests.testAllBackends("StridedSliceIndexing") { - // NOTE: cannot test `Tensor.shape` or `Tensor.scalars` directly until send - // and receive are implemented (without writing a bunch of mini tests). - // Instead, `Tensor.array` is called to make a ShapedArray host copy and the - // ShapedArray is tested instead. - let tensor3D = Tensor( - shape: [3, 4, 5], scalars: Array(stride(from: 0.0, to: 60, by: 1))) - let slice3D = tensor3D[2...] - let slice2D = tensor3D[1][0..<3..2] - let slice1D = tensor3D[0][0][1..<5..2] - - let array3D = slice3D.array - let array2D = slice2D.array - let array1D = slice1D.array - - /// Test shapes - expectEqual([1, 4, 5], array3D.shape) - expectEqual([2, 5], array2D.shape) - expectEqual([2], array1D.shape) - - /// Test scalars - expectEqual(Array(stride(from: 40.0, to: 60, by: 1)), array3D.scalars) - expectEqual( - Array(stride(from: 20.0, to: 25, by: 1)) + - Array(stride(from: 30.0, to: 35, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 1.0, to: 5, by: 2)), array1D.scalars) -} - -#if !CUDA -// TODO(https://bugs.swift.org/browse/TF-469): This test fails in GPU mode. -TensorTests.testAllBackends("StridedSliceIndexingAssignment") { - // NOTE: cannot test `Tensor.shape` or `Tensor.scalars` directly until send - // and receive are implemented (without writing a bunch of mini tests). - // Instead, `Tensor.array` is called to make a ShapedArray host copy and the - // ShapedArray is tested instead. - var tensor3D = Tensor( - shape: [3, 4, 5], scalars: Array(stride(from: 0.0, to: 60, by: 1))) - tensor3D[2, 0..<5..2, 0..<6] = Tensor( - shape: [2, 5], scalars: Array(stride(from: 20.0, to: 40, by: 2))) - let slice3D = tensor3D[2...] - let slice2D = tensor3D[1][0..<2] - let slice1D = tensor3D[0][0][3..<5] - - let array3D = slice3D.array - let array2D = slice2D.array - let array1D = slice1D.array - - /// Test shapes - expectEqual([1, 4, 5], array3D.shape) - expectEqual([2, 5], array2D.shape) - expectEqual([2], array1D.shape) - - /// Test scalars - expectEqual( - Array(stride(from: 20.0, to: 30, by: 2)) + - Array(stride(from: 45.0, to: 50, by: 1)) + - Array(stride(from: 30.0, to: 40, by: 2)) + - Array(stride(from: 55.0, to: 60, by: 1)), array3D.scalars) - expectEqual(Array(stride(from: 20.0, to: 30, by: 1)), array2D.scalars) - expectEqual(Array(stride(from: 3.0, to: 5, by: 1)), array1D.scalars) -} -#endif // !CUDA - -TensorTests.test("WholeTensorSlicing") { - let t: Tensor = [[[1, 1, 1], [2, 2, 2]], - [[3, 3, 3], [4, 4, 4]], - [[5, 5, 5], [6, 6, 6]]] - let slice2 = t.slice(lowerBounds: [1, 0, 0], upperBounds: [2, 1, 3]) - expectEqual(ShapedArray(shape: [1, 1, 3], scalars: [3, 3, 3]), - slice2.array) -} - -TensorTests.testAllBackends("AdvancedIndexing") { - // NOTE: cannot test multiple `Tensor.shape` or `Tensor.scalars` directly - // until send and receive are implemented (without writing a bunch of mini - // tests). Instead, `Tensor.array` is called to make a ShapedArray host copy - // and the ShapedArray is tested. - let tensor3D = Tensor(shape: [3, 4, 5], - scalars: Array(stride(from: 0.0, to: 60, by: 1))) - let element2D = tensor3D[1..<3, 0, 3...] - let array2D = element2D.array - - // Test shape - expectEqual([2, 2], array2D.shape) - - // Test scalars - expectEqual(Array([23.0, 24.0, 43.0, 44.0]), array2D.scalars) -} - -TensorTests.testAllBackends("Reduction") { - // 2 x 5 - let x = Tensor([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]) - expectEqual(Tensor(30), x.sum()) - expectEqual(Tensor(shape: [5], scalars: [2, 4, 6, 8, 10]), - x.sum(squeezingAxes: 0)) - expectEqual(Tensor(shape: [1, 5], scalars: [2, 4, 6, 8, 10]), - x.sum(alongAxes: 0)) - - expectEqual(Tensor(14400), x.product()) - expectEqual(Tensor(shape: [5], scalars: [1, 4, 9, 16, 25]), - x.product(squeezingAxes: 0)) - expectEqual(Tensor(shape: [1, 5], scalars: [1, 4, 9, 16, 25]), - x.product(alongAxes: 0)) - - expectEqual(Tensor(3), x.mean()) - expectEqual(Tensor(shape: [5], scalars: [1, 2, 3, 4, 5]), - x.mean(squeezingAxes: 0)) - expectEqual(Tensor(shape: [5], scalars: [1, 2, 3, 4, 5]), - x.mean(alongAxes: 0)) - expectEqual(Tensor(shape: [2], scalars: [3, 3]), - x.mean(squeezingAxes: 1)) - expectEqual(Tensor(shape: [1, 2], scalars: [3, 3]), - x.mean(alongAxes: 1)) - - expectEqual(Tensor(2), x.variance()) - expectEqual(Tensor(shape: [5], scalars: [0, 0, 0, 0, 0]), - x.variance(squeezingAxes: 0)) - expectEqual(Tensor(shape: [5], scalars: [0, 0, 0, 0, 0]), - x.variance(alongAxes: 0)) - expectEqual(Tensor(shape: [2], scalars: [2, 2]), - x.variance(squeezingAxes: 1)) - expectEqual(Tensor(shape: [1, 2], scalars: [2, 2]), - x.variance(alongAxes: 1)) -} - -TensorTests.testAllBackends("Concatenation") { - // 2 x 3 - let t1 = Tensor([[0, 1, 2], [3, 4, 5]]) - // 2 x 3 - let t2 = Tensor([[6, 7, 8], [9, 10, 11]]) - let concatenated = t1 ++ t2 - let concatenated0 = t1.concatenated(with: t2) - let concatenated1 = t1.concatenated(with: t2, alongAxis: 1) - expectEqual(ShapedArray(shape: [4, 3], scalars: Array(0..<12)), - concatenated.array) - expectEqual(ShapedArray(shape: [4, 3], scalars: Array(0..<12)), - concatenated0.array) - expectEqual(ShapedArray(shape: [2, 6], - scalars: [0, 1, 2, 6, 7, 8, 3, 4, 5, 9, 10, 11]), - concatenated1.array) -} - -TensorTests.test("EwiseComparison") { - let x = Tensor([0, 1, 2]) - let y = Tensor([2, 1, 3]) - expectEqual((x .< y).scalars, [true, false, true]) -} - -TensorTests.test("LexicographicalComparison") { - let x = Tensor([0, 1, 2, 3, 4]) - let y = Tensor([2, 3, 4, 5, 6]) - expectTrue(x < y) -} - -TensorTests.testAllBackends("ArgMax") { - // 2 x 3 - let x = Tensor([[0, 1, 2], [3, 4, 5]]) - let argmax0 = x.argmax(squeezingAxis: 0) - let argmax1 = x.argmax(squeezingAxis: 1) - let scalarsArgmax = x.argmax() - expectEqual(ShapedArray(shape: [3], scalars: [1, 1, 1]), argmax0.array) - expectEqual(ShapedArray(shape: [2], scalars: [2, 2]), argmax1.array) - expectEqual(ShapedArray(shape: [], scalars: [5]), scalarsArgmax.array) -} - -TensorTests.testAllBackends("CeilFloor") { - let x = Tensor([-1.3, -0.4, 0.5, 1.6]) - let xFloor = floor(x) - let xCeil = ceil(x) - expectEqual(ShapedArray(shape: [4], scalars: [-2, -1, 0, 1]), xFloor.array) - expectEqual(ShapedArray(shape: [4], scalars: [-1, 0, 1, 2]), xCeil.array) -} - -TensorTests.testAllBackends("SimpleMath") { - let x = Tensor([1.2, 1.2]) - let y = tanh(x) - let array = y.array - expectEqual([2], array.shape) - expectPointwiseNearlyEqual([0.833655, 0.833655], array.scalars, - byError: 0.0001) -} - -TensorTests.testAllBackends("StandardDeviation") { - expectEqual(Tensor(0), Tensor([1]).standardDeviation()) - expectEqual(Tensor(0.5), Tensor([0, 1]).standardDeviation(alongAxes: 0)) - expectEqual(Tensor(0.5), Tensor([0, 1]).standardDeviation()) - expectNearlyEqual( - 2.87228132, - Tensor(rangeFrom: 0, to: 10, stride: 1).standardDeviation().scalarized(), - byError: 0.001) - let matrix = Tensor(rangeFrom: 0, to: 10, stride: 1).reshaped(to: [2, 5]) - expectNearlyEqual(2.87228132, - matrix.standardDeviation().scalarized(), - byError: 0.001) - expectPointwiseNearlyEqual( - [1.4142, 1.4142], - matrix.standardDeviation(alongAxes: 1).array.scalars, - byError: 0.001) -} - -TensorTests.testAllBackends("3Adds") { - let a = Tensor([1]) - let b = Tensor([2]) - let c = Tensor([3]) - - let o = a + b + c - expectEqual([6], o.scalars) -} - -TensorTests.testAllBackends("MultiOpMath") { - let x = Tensor([1.2, 1.2]) - let y = Tensor([2.4, 2.4]) - let t1 = x + y - let t2 = t1 * t1 - let t3 = sqrt(t2) - - let array1 = t1.array - let array2 = t2.array - let array3 = t3.array - expectEqual([2], array1.shape) - expectEqual([2], array2.shape) - expectEqual([2], array3.shape) - expectPointwiseNearlyEqual([3.6, 3.6], array1.scalars) - expectPointwiseNearlyEqual([12.96, 12.96], array2.scalars) - expectPointwiseNearlyEqual([3.6, 3.6], array3.scalars) -} - -TensorTests.testAllBackends("XWPlusB") { - // Shape: 1 x 4 - let x = Tensor([[1.0, 2.0, 2.0, 1.0]]) - // Shape: 4 x 2 - let w = Tensor([[1.0, 0.0], [3.0, 0.0], [2.0, 3.0], [1.0, 0.0]]) - // Shape: 2 - let b = Tensor([0.5, 0.5]) - // Shape: 1 x 2 (broadcasted) - let result = matmul(x, w) + b - expectEqual([1, 2], result.shape) - expectEqual([12.5, 6.5], result.scalars) -} - -TensorTests.testAllBackends("Transpose") { - // 3 x 2 -> 2 x 3 - let xT = Tensor([[1, 2], [3, 4], [5, 6]]).transposed() - let xTArray = xT.array - expectEqual(2, xTArray.rank) - expectEqual([2, 3], xTArray.shape) - expectEqual([1, 3, 5, 2, 4, 6], xTArray.scalars) -} - -TensorTests.testAllBackends("SimpleCond") { - func selectValue(_ pred: Bool) -> Tensor { - let a = Tensor(0) - let b = Tensor(1) - if pred { - return a - } - return b - } - - expectEqual(0, selectValue(true).scalar) -} - -TensorTests.testAllBackends("TensorShapeDescription") { - expectEqual("[2, 2]", Tensor(ones: [2, 2]).shape.description) - expectEqual("[]", Tensor(1).shape.description) -} - -@inline(never) -func testXORInference() { - func xor(_ x: Float, _ y: Float) -> Float { - let x = Tensor([x, y]).reshaped(to: [1, 2]) - - // FIXME: If params are declared outside of `xor`, it would crash. - // 2 x 4 - let w1 = Tensor( - [[-1.83586664, -0.20809225, 0.47667537, 1.90780607], - [-1.83523219, -0.51167348, 0.15490439, 1.91018065]]) - // 1 x 4 - let b1 = Tensor( - [[2.54353216, 0.25132703, -0.16503136, -0.85754058]]) - // 4 x 1 - let w2 = Tensor( - [[3.04350065], [0.35590511], [-0.3252157], [3.49349223]]) - // 1 x 1 - let b2 = Tensor([[-0.74635993]]) - - let o1 = tanh(matmul(x, w1) + b1) - let y = tanh(matmul(o1, w2) + b2) - return y.array.scalars[0] // TODO: use better scalar getter - } - expectNearlyEqual(0.0, xor(0.0, 0.0), byError: 0.1) - expectNearlyEqual(1.0, xor(0.0, 1.0), byError: 0.1) - expectNearlyEqual(1.0, xor(1.0, 0.0), byError: 0.1) - expectNearlyEqual(0.0, xor(1.0, 1.0), byError: 0.1) -} -TensorTests.testAllBackends("XORInference", testXORInference) - -TensorTests.testAllBackends("MLPClassifierStruct") { - struct MLPClassifier { - // 2 x 4 - var w1 = Tensor([[1.0, 0.8, 0.4, 0.4], - [0.4, 0.3, 0.2, 0.1]]) - // 4 x 1 - var w2 = Tensor([[0.4], [0.4], [0.3], [0.9]]) - var b1 = Tensor(zeros: [1, 4]) - var b2 = Tensor(zeros: [1, 1]) - - func prediction(for x: Tensor) -> Tensor { - let o1 = tanh(matmul(x, w1) + b1) - return tanh(matmul(o1, w2) + b2) - } - } - let input = Tensor([[1, 0.5]]) - let classifier = MLPClassifier() - let prediction = classifier.prediction(for: input) - expectPointwiseNearlyEqual([0.816997], prediction.scalars) -} - -TensorTests.testAllBackends("ExpandingShape") { - // 2 x 3 -> 1 x 2 x 1 x 3 x 1 - let matrix = Tensor([[0, 1, 2], [3, 4, 5]]) - let reshaped = matrix.expandingShape(at: 0,2,4) - - expectEqual([1, 2, 1, 3, 1], reshaped.shape) - expectEqual(Array(0..<6), reshaped.scalars) - - // 1 x 2 x 1 x 3 x 1 -> 2 x 3 - let rereshaped = reshaped.squeezingShape(at: 0,2,4) - expectEqual([2, 3], rereshaped.shape) - expectEqual(Array(0..<6), rereshaped.scalars) -} - -TensorTests.testAllBackends("Reshape") { - // 2 x 3 -> 1 x 3 x 1 x 2 x 1 - let matrix = Tensor([[0, 1, 2], [3, 4, 5]]) - let reshaped = matrix.reshaped(to: [1, 3, 1, 2, 1]) - - expectEqual([1, 3, 1, 2, 1], reshaped.shape) - expectEqual(Array(0..<6), reshaped.scalars) -} - -TensorTests.testAllBackends("Flatten") { - // 2 x 3 -> 6 - let matrix = Tensor([[0, 1, 2], [3, 4, 5]]) - let flattened = matrix.flattened() - - expectEqual([6], flattened.shape) - expectEqual(Array(0..<6), flattened.scalars) -} - -TensorTests.testAllBackends("Flatten0D") { - let scalar = Tensor(5) - let flattened = scalar.flattened() - expectEqual([1], flattened.shape) - expectEqual([5], flattened.scalars) -} - -TensorTests.testAllBackends("ReshapeToScalar") { - // 1 x 1 -> scalar - let z = Tensor([[10]]).reshaped(to: []) - expectEqual([], z.shape) -} - -TensorTests.testAllBackends("ReshapeTensor") { - // 2 x 3 -> 1 x 3 x 1 x 2 x 1 - let x = Tensor(repeating: 0.0, shape: [2, 3]) - let y = Tensor(repeating: 0.0, shape: [1, 3, 1, 2, 1]) - let result = x.reshaped(like: y) - expectEqual([1, 3, 1, 2, 1], result.shape) -} - -TensorTests.testAllBackends("BroadcastedTensor") { - // 1 -> 2 x 3 x 4 - let one = Tensor(1) - var target = Tensor(repeating: 0.0, shape: [2, 3, 4]) - let broadcasted = one.broadcasted(like: target) - expectEqual(Tensor(repeating: 1, shape: [2, 3, 4]), broadcasted) - target .= Tensor(repeating: 1, shape: [1, 3, 1]) - expectEqual(Tensor(repeating: 1, shape: [2, 3, 4]), target) -} - -TensorTests.testAllBackends("Unbroadcasted1") { - let x = Tensor(repeating: 1, shape: [2, 3, 4, 5]) - let y = Tensor(repeating: 1, shape: [4, 5]) - let z = x.unbroadcasted(like: y) - expectEqual(ShapedArray(repeating: 6, shape: [4, 5]), - z.array) -} - -TensorTests.testAllBackends("Unbroadcasted2") { - let x = Tensor(repeating: 1, shape: [2, 3, 4, 5]) - let y = Tensor(repeating: 1, shape: [3, 1, 5]) - let z = x.unbroadcasted(like: y) - expectEqual(ShapedArray(repeating: 8, shape: [3, 1, 5]), - z.array) -} - -// TODO: Merge all rank/shape getter tests into one when we support code motion -// to avoid sends. - -@inline(never) -func testRankGetter() { - let tensor = Tensor(shape: [3, 4, 5], scalars: Array(0..<60)) - expectEqual(3, tensor.rank) -} -TensorTests.testAllBackends("RankGetter", testRankGetter) - -@inline(never) -func testRankGetter2() { - let vector = Tensor([1]) - expectEqual(1, vector.rank) -} -TensorTests.testAllBackends("RankGetter2", testRankGetter2) - -@inline(never) -func testRankGetter3() { - let matrix = Tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) - expectEqual(2, matrix.rank) -} -TensorTests.testAllBackends("RankGetter3", testRankGetter3) - -@inline(never) -func testRankGetter4() { - let ones = Tensor(ones: [1, 2, 2, 2, 2, 2, 1]) - expectEqual(7, ones.rank) -} -TensorTests.testAllBackends("RankGetter4", testRankGetter4) - -@inline(never) -func testShapeGetter() { - let tensor = Tensor(shape: [3, 4, 5], scalars: Array(0..<60)) - expectEqual([3, 4, 5], tensor.shape) -} -TensorTests.testAllBackends("ShapeGetter", testShapeGetter) - -@inline(never) -func testShapeGetter2() { - let vector = Tensor([1]) - expectEqual([1], vector.shape) -} -TensorTests.testAllBackends("ShapeGetter2", testShapeGetter2) - -@inline(never) -func testShapeGetter3() { - let matrix = Tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) - expectEqual([2, 3], matrix.shape) -} -TensorTests.testAllBackends("ShapeGetter3", testShapeGetter3) - -@inline(never) -func testShapeGetter4() { - let ones = Tensor(ones: [1, 2, 2, 2, 2, 2, 1]) - expectEqual([1, 2, 2, 2, 2, 2, 1], ones.shape) -} -TensorTests.testAllBackends("ShapeGetter4", testShapeGetter4) - -// For now it is sufficient to run remote tests with test cases in this file -// only. When creating new test files, consider simply calling runAllTests(). -#if CUDA -// RemoteSession does not work for GPU because partitioning logic gets confused -// with multiple devices. -runAllTests() -#else -runAllTestsWithRemoteSession() -#endif // CUDA diff --git a/test/TensorFlowRuntime/tensor_array_protocol.swift b/test/TensorFlowRuntime/tensor_array_protocol.swift deleted file mode 100644 index 64200dcabc524..0000000000000 --- a/test/TensorFlowRuntime/tensor_array_protocol.swift +++ /dev/null @@ -1,223 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %target-build-swift -swift-version 5 -g %s -o %t/a.out -// RUN: %target-codesign %t/a.out -// RUN: %target-run %t/a.out -// REQUIRES: executable_test -// -// `TensorArrayProtocol` tests. - -import TensorFlow -import StdlibUnittest - -var TensorArrayProtocolTests = TestSuite("TensorArrayProtocol") - -struct Empty : TensorGroup {} - -struct Simple : TensorGroup { - var w, b: Tensor -} - -struct Mixed : TensorGroup { - // Mutable. - var string: StringTensor - var float: Tensor - // Immutable. - let int: Tensor -} - -struct Nested : TensorGroup { - // Immutable. - let simple: Simple - // Mutable. - var mixed: Mixed -} - -struct Generic : TensorGroup { - var t: T - var u: U -} - -TensorArrayProtocolTests.test("EmptyTensorHandleCount") { - expectEqual(0, Empty()._tensorHandleCount) -} - -TensorArrayProtocolTests.test("SimpleTensorHandleCount") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - expectEqual(2, simple._tensorHandleCount) -} - -TensorArrayProtocolTests.test("SimpleUnpackTensorHandles") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let buffer = UnsafeMutablePointer.allocate(capacity: 2) - simple._unpackTensorHandles(into: buffer) - let expectedBuffer = UnsafeMutableBufferPointer.allocate(capacity: 2) - expectedBuffer.initialize(from: [w.handle._cTensorHandle, b.handle._cTensorHandle]) - expectEqual(expectedBuffer[0], buffer[0]) - expectEqual(expectedBuffer[1], buffer[1]) -} - -TensorArrayProtocolTests.test("MixedTensorHandleCount") { - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - expectEqual(3, mixed._tensorHandleCount) -} - -TensorArrayProtocolTests.test("MixedUnpackTensorHandles") { - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - let buffer = UnsafeMutablePointer.allocate(capacity: 3) - mixed._unpackTensorHandles(into: buffer) - let expectedBuffer = UnsafeMutableBufferPointer.allocate(capacity: 3) - expectedBuffer.initialize( - from: [string.handle._cTensorHandle, float.handle._cTensorHandle, - int.handle._cTensorHandle]) - expectEqual(expectedBuffer[0], buffer[0]) - expectEqual(expectedBuffer[1], buffer[1]) - expectEqual(expectedBuffer[2], buffer[2]) -} - -TensorArrayProtocolTests.test("NestedTensorHandleCount") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - let nested = Nested(simple: simple, mixed: mixed) - expectEqual(5, nested._tensorHandleCount) -} - -TensorArrayProtocolTests.test("NestedUnpackTensorHandles") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - let nested = Nested(simple: simple, mixed: mixed) - let buffer = UnsafeMutablePointer.allocate(capacity: 5) - nested._unpackTensorHandles(into: buffer) - let expectedBuffer = UnsafeMutableBufferPointer.allocate(capacity: 5) - expectedBuffer.initialize( - from: [w.handle._cTensorHandle, b.handle._cTensorHandle, - string.handle._cTensorHandle, float.handle._cTensorHandle, - int.handle._cTensorHandle]) - expectEqual(expectedBuffer[0], buffer[0]) - expectEqual(expectedBuffer[1], buffer[1]) - expectEqual(expectedBuffer[2], buffer[2]) - expectEqual(expectedBuffer[3], buffer[3]) - expectEqual(expectedBuffer[4], buffer[4]) -} - -TensorArrayProtocolTests.test("GenericTensorHandleCount") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - let generic = Generic(t: simple, u: mixed) - expectEqual(5, generic._tensorHandleCount) -} - -TensorArrayProtocolTests.test("GenericUnpackTensorHandles") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - let generic = Generic(t: simple, u: mixed) - let buffer = UnsafeMutablePointer.allocate(capacity: 5) - generic._unpackTensorHandles(into: buffer) - let expectedBuffer = UnsafeMutableBufferPointer.allocate(capacity: 5) - expectedBuffer.initialize( - from: [w.handle._cTensorHandle, b.handle._cTensorHandle, - string.handle._cTensorHandle, float.handle._cTensorHandle, - int.handle._cTensorHandle]) - expectEqual(expectedBuffer[0], buffer[0]) - expectEqual(expectedBuffer[1], buffer[1]) - expectEqual(expectedBuffer[2], buffer[2]) - expectEqual(expectedBuffer[3], buffer[3]) - expectEqual(expectedBuffer[4], buffer[4]) -} - -TensorArrayProtocolTests.test("NestedGenericTensorHandleCount") { - struct NestedGeneric { - func function() { - struct UltraNested : TensorArrayProtocol { - var a: Generic - var b: Generic - } - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - let genericSM = Generic(t: simple, u: mixed) - let genericMS = Generic(t: mixed, u: simple) - let nested = UltraNested(a: genericSM, b: genericMS) - expectEqual(10, nested._tensorHandleCount) - } - } - - NestedGeneric().function() -} - -TensorArrayProtocolTests.test("NestedGenericUnpackTensorHandles") { - struct NestedGeneric { - func function() { - struct UltraNested : TensorArrayProtocol { - var a: Generic - var b: Generic - } - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let string = StringTensor("Test") - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(string: string, float: float, int: int) - let genericSM = Generic(t: simple, u: mixed) - let genericMS = Generic(t: mixed, u: simple) - let nested = UltraNested(a: genericSM, b: genericMS) - let buffer = UnsafeMutablePointer.allocate(capacity: 10) - nested._unpackTensorHandles(into: buffer) - let expectedBuffer = UnsafeMutableBufferPointer.allocate(capacity: 10) - expectedBuffer.initialize( - from: [w.handle._cTensorHandle, b.handle._cTensorHandle, - string.handle._cTensorHandle, float.handle._cTensorHandle, - int.handle._cTensorHandle, string.handle._cTensorHandle, - float.handle._cTensorHandle, int.handle._cTensorHandle, - w.handle._cTensorHandle, b.handle._cTensorHandle]) - expectEqual(expectedBuffer[0], buffer[0]) - expectEqual(expectedBuffer[1], buffer[1]) - expectEqual(expectedBuffer[2], buffer[2]) - expectEqual(expectedBuffer[3], buffer[3]) - expectEqual(expectedBuffer[4], buffer[4]) - expectEqual(expectedBuffer[5], buffer[5]) - expectEqual(expectedBuffer[6], buffer[6]) - expectEqual(expectedBuffer[7], buffer[7]) - expectEqual(expectedBuffer[8], buffer[8]) - expectEqual(expectedBuffer[9], buffer[9]) - } - } - - NestedGeneric().function() -} - -runAllTests() diff --git a/test/TensorFlowRuntime/tensor_autodiff_indirect.swift b/test/TensorFlowRuntime/tensor_autodiff_indirect.swift deleted file mode 100644 index d4c142e17caba..0000000000000 --- a/test/TensorFlowRuntime/tensor_autodiff_indirect.swift +++ /dev/null @@ -1,205 +0,0 @@ -// RUN: %target-run-simple-swift -// REQUIRES: executable_test -// -// Tensor indirect passing AD runtime tests. - -import TensorFlow -import StdlibUnittest -import TensorFlowUnittest - -var TensorADTests = TestSuite("TensorIndirectAD") - -TensorADTests.testAllBackends("Generic") { - func indirect(_ x: Tensor) -> Tensor { - return (x + 3) * (x + 3) - } - expectEqual(Tensor(8), gradient(at: Tensor(1), in: indirect)) - expectEqual(Tensor(16), pullback(at: Tensor(1), in: indirect)(Tensor(2))) -} - -TensorADTests.testAllBackends("Concrete") { - @differentiable - func indirect(_ x: Tensor) -> Tensor { - return x * 1 * 1 * x - } - expectEqual(Tensor(12), pullback(at: Tensor(3), in: indirect)(Tensor(2))) - expectEqual(Tensor(18), pullback(at: Tensor(3), in: indirect)(Tensor(3))) -} - -extension Tensor where Scalar : Differentiable & FloatingPoint { - @differentiable(wrt: x, vjp: vjpFoo) - func foo(_ x: Scalar) -> Scalar { - return x - } - func vjpFoo(_ x: Scalar) -> (Scalar, (Scalar.TangentVector) -> Scalar.TangentVector) { - return (x, { v in v }) - } -} -TensorADTests.testAllBackends("GenericMethod") { - expectEqual(Tensor(0), pullback(at: Tensor(2), in: { $0.foo(2) })(2)) - expectEqual(2.0, pullback(at: 1, in: { Tensor(1).foo($0) })(2)) - expectEqual((Tensor(0), 1), pullback(at: Tensor(1), 1, in: { $0.foo($1) })(1)) -} - -// Protocol with differentiable function requirement. -protocol Addable : Differentiable & FloatingPoint { - @differentiable(wrt: (x, y)) - static func add(_ x: Self, _ y: Self) -> Self -} -extension Double : Addable { - @differentiable(wrt: (x, y)) - static func add(_ x: Double, _ y: Double) -> Double { - return x + y - } -} -TensorADTests.testAllBackends("ResultSelection") { - func indirect(_ x: T, _ y: T) -> (T, T) { - let first = T.add(x, x) - return (T.add(first, first), T.add(y, 2)) - } - expectEqual((4, 0), gradient(at: Double(3), 3, in: { x, y in indirect(x, y).0 })) - expectEqual((0, 1), gradient(at: Double(3), 3, in: { x, y in indirect(x, y).1 })) -} - -// Mini high-level ML library. -public protocol Layer: Differentiable { - associatedtype Input: Differentiable - associatedtype Output: Differentiable - @differentiable(wrt: (self, input)) - func applied(to input: Input) -> Output -} - -@_fixed_layout -public struct Dense: Layer { - public var weight: Tensor - public var bias: Tensor - public typealias Activation = @differentiable (Tensor) -> Tensor - @noDerivative public let activation: Activation - - @differentiable(wrt: (self, input)) - public func applied(to input: Tensor) -> Tensor { - return activation(matmul(input, weight) + bias) - } -} - -public extension Dense where Scalar.RawSignificand: FixedWidthInteger { - init(inputSize: Int, outputSize: Int, activation: @escaping Activation) { - self.init(weight: Tensor(glorotUniform: [inputSize, outputSize]), - bias: Tensor(zeros: [outputSize]), - activation: activation) - } -} - -TensorADTests.testAllBackends("GenericLayerMember") { - // Tests TF-203. - struct GenericLayerWrapper : Layer { - var layer: T - @differentiable(wrt: (self, input)) - func applied(to input: T.Input) -> T.Output { - return layer.applied(to: input) - } - } -} - -// Tests TF-235. -struct Sequential: Layer - where LHS.Output == RHS.Input -{ - let lhs: LHS - let rhs: RHS - - init(_ lhs: LHS, _ rhs: RHS) { - self.lhs = lhs - self.rhs = rhs - } - - @differentiable(wrt: (self, input)) - func applied(to input: LHS.Input) -> RHS.Output { - let intermediateValue = lhs.applied(to: input) - return rhs.applied(to: intermediateValue) - } -} - -// Forward function composition operator. Used in F#. -func >> (_ lhs: LHS, _ rhs: RHS) -> Sequential { - return Sequential(lhs, rhs) -} - -struct LayerTriple: Layer - where T.Output == U.Input, U.Output == V.Input -{ - let first: T - let second: U - let third: V - - init(_ first: T, _ second: U, _ third: V) { - self.first = first - self.second = second - self.third = third - } - - @differentiable(wrt: (self, input)) - func applied(to input: T.Input) -> V.Output { - let intermediate1 = first.applied(to: input) - let intermediate2 = second.applied(to: intermediate1) - return third.applied(to: intermediate2) - } -} - -// Tests TF-235 and TF-242. -TensorADTests.testAllBackends("GenericLayerMembers") { - func testFixedInput() { - let lhs = Dense(inputSize: 3, outputSize: 4, activation: relu) - let rhs = Dense(inputSize: 4, outputSize: 5, activation: sigmoid) - let combined = Sequential(lhs, rhs) - - let input = Tensor(ones: [2, 3]) - let seed = Tensor(ones: [input.shape[0], rhs.weight.shape[1]]) - let (š›lhs, š›rhs) = pullback(at: lhs, rhs) { lhs, rhs in - rhs.applied(to: lhs.applied(to: input)) - }(seed) - let š›combined = pullback(at: combined) { $0.applied(to: input) }(seed) - expectEqual(Sequential.TangentVector(lhs: š›lhs, rhs: š›rhs), š›combined) - } - testFixedInput() - - func testWrtInput(_ input: Tensor) { - let lhs = Dense(inputSize: 3, outputSize: 4, activation: relu) - let rhs = Dense(inputSize: 4, outputSize: 5, activation: sigmoid) - // Test "compose" operator. - let combined = lhs >> rhs - - let seed = Tensor(ones: [input.shape[0], rhs.weight.shape[1]]) - let (š›lhs, š›rhs) = pullback(at: lhs, rhs) { lhs, rhs in - rhs.applied(to: lhs.applied(to: input)) - }(seed) - let š›combined = pullback(at: combined) { $0.applied(to: input) }(seed) - expectEqual(Sequential.TangentVector(lhs: š›lhs, rhs: š›rhs), š›combined) - } - testWrtInput(Tensor(randomUniform: [2, 3])) -} - -// Tests TF-308. -TensorADTests.testAllBackends("GenericWrapperLayer") { - struct Wrapper: Layer - where Wrapped.Input == Tensor, Wrapped.Output == Tensor - { - var layer: Wrapped - - @differentiable - func applied(to input: Wrapped.Input) -> Wrapped.Output { - return layer.applied(to: input) - } - } - - let dense = Dense(inputSize: 2, outputSize: 3, activation: relu) - let wrapper = Wrapper(layer: dense) - let input = Tensor(ones: [2, 2]) - let seed = Tensor(ones: [input.shape[0], dense.weight.shape[1]]) - - let š›wrapper = pullback(at: wrapper) { $0.applied(to: input) }(seed) - let š›dense = pullback(at: dense) { $0.applied(to: input) }(seed) - expectEqual(Wrapper.TangentVector(layer: š›dense), š›wrapper) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/tensor_autodiff_indirect_crasher.swift b/test/TensorFlowRuntime/tensor_autodiff_indirect_crasher.swift deleted file mode 100644 index 5e10f55cc14c4..0000000000000 --- a/test/TensorFlowRuntime/tensor_autodiff_indirect_crasher.swift +++ /dev/null @@ -1,22 +0,0 @@ -// RUN: %target-run-simple-swift -// REQUIRES: executable_test -// -// FIXME(TF-326): Re-enable `-O` after deserialization failure fix. -// UNSUPPORTED: swift_test_mode_optimize -// -// Tensor indirect passing AD runtime tests. - -import TensorFlow -import StdlibUnittest -import TensorFlowUnittest - -var TensorADTests = TestSuite("TensorIndirectAD") - -TensorADTests.testAllBackends("TF-324") { - @differentiable(where T : TensorFlowFloatingPoint) - func TF_324(_ lhs: T, _ rhs: Tensor) -> Tensor where T : FloatingPoint { - return pow(Tensor(lhs), rhs) - } -} - -runAllTests() diff --git a/test/TensorFlowRuntime/tensor_autodiff_runtime.swift b/test/TensorFlowRuntime/tensor_autodiff_runtime.swift deleted file mode 100644 index c6d5c8c02581a..0000000000000 --- a/test/TensorFlowRuntime/tensor_autodiff_runtime.swift +++ /dev/null @@ -1,392 +0,0 @@ -// RUN: %target-run-simple-swift -// REQUIRES: executable_test -// -// Tensor AD runtime tests. - -import TensorFlow -import StdlibUnittest -import TensorFlowUnittest - -var TensorADTests = TestSuite("TensorAD") - -TensorADTests.testAllBackends("TestSimpleGrad") { - func square(_ x: Tensor) -> Tensor { - return x * x - } - expectEqual([0.2, 0.4, 0.6], gradient(at: [0.1, 0.2, 0.3], in: square)) - expectEqual([[20], [40]], gradient(at: [[10], [20]], in: square)) -} - -TensorADTests.testAllBackends("TestGenericGrad") { - func square(_ x: Tensor) -> Tensor { - return x * x - } - expectEqual([0.2, 0.4, 0.6], gradient(at: Tensor([0.1, 0.2, 0.3]), in: square)) -} - -TensorADTests.testAllBackends("TestScalarGenericGrad") { - // Tests TF-287. - func negate(_ x: Tensor) -> Tensor { - return 1 - x - } - expectEqual(Tensor(-1), gradient(at: Tensor([0.1, 0.2, 0.3]), in: negate)) -} - -TensorADTests.testAllBackends("TestScalarized") { - let grad = gradient(at: Tensor([3.0, 4.0])) { x in - logSoftmax(x).mean().scalarized() - } - expectEqual(Tensor([0.23105857, -0.2310586]), grad) -} - -TensorADTests.testAllBackends("+") { - let f = { (a: Tensor, b: Tensor) in a + b } - expectEqual((Tensor(1), Tensor(1)), gradient(at: Tensor(0), Tensor(0), in: f)) - expectEqual(([1], [1]), pullback(at: [1], [10], in: f)([1])) -} - -TensorADTests.testAllBackends("-") { - let f = { (a: Tensor, b: Tensor) in a - b } - expectEqual((Tensor(1), Tensor(-1)), gradient(at: Tensor(0), Tensor(0), in: f)) - expectEqual(([1], [-1]), pullback(at: [1], [10], in: f)([1])) -} - -TensorADTests.testAllBackends("*") { - let f = { (a: Tensor, b: Tensor) in a * b } - expectEqual(([0], [0]), gradient(at: [0], [0], in: f)) - expectEqual(([10], [1]), gradient(at: [1], [10], in: f)) -} - -TensorADTests.testAllBackends("/") { - let f = { (a: Tensor, b: Tensor) in a / b } - expectEqual(([0.1], [-0.01]), gradient(at: [1], [10], in: f)) -} - -TensorADTests.testAllBackends("matmul") { - let f = { (a: Tensor, b: Tensor) in matmul(a, b) } - let v = Tensor(ones: [1, 1]) - expectEqual(([[0]], [[0]]), pullback(at: [[0]], [[0]], in: f)(v)) - expectEqual(([[10]], [[1]]), pullback(at: [[1]], [[10]], in: f)(v)) -} - -TensorADTests.testAllBackends("•") { - let f = { (a: Tensor, b: Tensor) in a • b } - let v = Tensor(ones: [1, 1]) - expectEqual(([[0]], [[0]]), pullback(at: [[0]], [[0]], in: f)(v)) - expectEqual(([[10]], [[1]]), pullback(at: [[1]], [[10]], in: f)(v)) -} - -TensorADTests.testAllBackends("negate") { - let f = { (a: Tensor) in -a } - expectEqual([-1], gradient(at: [0], in: f)) - expectEqual([-1], gradient(at: [10], in: f)) -} - -TensorADTests.testAllBackends("Abs") { - let f = { (a: Tensor) in abs(a) } - expectEqual([1, -1, 0], gradient(at: [3.0, -3.0, 0], in: f)) -} - -TensorADTests.testAllBackends("sum") { - let input = Tensor(repeating: 42, shape: [2, 2]) - let sumPullbackScalar = pullback(at: input) { (a: Tensor) in a.sum() } - let sumPullbackSqueezingAxes = pullback(at: input) { (a: Tensor) in a.sum(squeezingAxes: 0, 1) } - let sumPullbackAlongAxes = pullback(at: input) { (a: Tensor) in a.sum(alongAxes: 0, 1) } - - let expected = Tensor(ones: [2, 2]) - expectEqual(expected, sumPullbackScalar(Tensor(1))) - expectEqual(expected, sumPullbackSqueezingAxes(Tensor(1))) - expectEqual(expected, sumPullbackAlongAxes(Tensor(1))) - expectEqual(expected * 3, sumPullbackScalar(Tensor(3))) - expectEqual(expected * 3, sumPullbackSqueezingAxes(Tensor(3))) - expectEqual(expected * 3, sumPullbackAlongAxes(Tensor(3))) -} - -TensorADTests.testAllBackends("mean") { - let meanGradScalar = gradient { (a: Tensor) in a.mean() } - let meanGradSqueezingAxes = gradient { (a: Tensor) in a.mean(squeezingAxes: 0, 1) } - let meanGradAlongAxes = gradient { (a: Tensor) in a.mean(alongAxes: 0, 1) } - - let input = Tensor(ones: [2, 2]) - let expected = Tensor(repeating: 0.25, shape: [2, 2]) - expectEqual(expected, meanGradScalar(input)) - expectEqual(expected, meanGradSqueezingAxes(input)) - expectEqual(expected, meanGradAlongAxes(input)) -} - -TensorADTests.testAllBackends("variance") { - let varianceGradScalar = gradient { (a: Tensor) in a.variance() } - let varianceGradSqueezingAxes = gradient { (a: Tensor) in a.variance(squeezingAxes: 0, 1) } - let varianceGradAlongAxes = gradient { (a: Tensor) in a.variance(alongAxes: 0, 1) } - - let input: Tensor = [[1, 2], [3, 4]] - let expected: Tensor = [[-0.75, -0.25], [0.25, 0.75]] - expectEqual(expected, varianceGradScalar(input)) - expectEqual(expected, varianceGradSqueezingAxes(input)) - expectEqual(expected, varianceGradAlongAxes(input)) -} - -TensorADTests.testAllBackends("expandingShape") { - let f1 = { (a: Tensor) in a.expandingShape(at: 0).squared() } - let f2 = { (a: Tensor) in a.squared().expandingShape(at: 0) } - expectEqual([6, 10], pullback(at: [3, 5], in: f1)([[1, 1]])) - expectEqual([6, 10], pullback(at: [3, 5], in: f2)([[1, 1]])) -} - -TensorADTests.testAllBackends("squeezingShape") { - let f1 = { (a: Tensor) in a.squeezingShape(at: 0).squared() } - let f2 = { (a: Tensor) in a.squared().squeezingShape(at: 0) } - expectEqual([[6, 10]], pullback(at: [[3, 5]], in: f1)([1, 1])) - expectEqual([[6, 10]], pullback(at: [[3, 5]], in: f2)([1, 1])) -} - -TensorADTests.testAllBackends("reshapedBackprop") { - let f1 = { (a: Tensor) in a.reshaped(toShape: Tensor([2, 1])).squared() } - let f2 = { (a: Tensor) in a.squared().reshaped(toShape: Tensor([2, 1])) } - expectEqual([[6, 10]], pullback(at: [[3, 5]], in: f1)([[1], [1]])) - expectEqual([[6, 10]], pullback(at: [[3, 5]], in: f2)([[1], [1]])) -} - -TensorADTests.testAllBackends("reshaped") { - let shapeTensor = Tensor([2, 2, 2]) - let input = Tensor(ones: [2, 4]) - let reshapedPullback = pullback(at: input) { (a: Tensor) in a.reshaped(toShape: shapeTensor) } - let reshaped = Tensor(ones: [2, 2, 2]) - expectEqual(input, reshapedPullback(reshaped)) -} - -TensorADTests.testAllBackends("concatenation (++)") { - let a1 = Tensor([1,2,3,4]) - let b1 = Tensor([5,6,7,8,9,10]) - - let a2 = Tensor([1,1,1,1]) - let b2 = Tensor([1,1,1,1,1,1]) - - let grads = gradient(at: a2, b2) { a, b in - return ((a1 * a) ++ (b1 * b)).sum() - } - - expectEqual(a1, grads.0) - expectEqual(b1, grads.1) -} - -TensorADTests.testAllBackends("concatenated") { - let a1 = Tensor([1,2,3,4]) - let b1 = Tensor([5,6,7,8,9,10]) - - let a2 = Tensor([1,1,1,1]) - let b2 = Tensor([1,1,1,1,1,1]) - - let grads = gradient(at: a2, b2) { a, b in - return (a1 * a).concatenated(with: b1 * b, alongAxis: -1).sum() - } - - expectEqual(a1, grads.0) - expectEqual(b1, grads.1) -} - -TensorADTests.testAllBackends("transposed") { - let input = Tensor(ones: [2, 3]) - let transposed = Tensor(ones: [3, 2]) - let transposedPullback = pullback(at: input) { (a: Tensor) in a.transposed() } - let transposedPermutationsPullback = pullback(at: input) { (a: Tensor) in a.transposed(withPermutations: [1, 0]) } - let transposedVariadicsPullback = pullback(at: input) { (a: Tensor) in a.transposed(withPermutations: 1, 0) } - - expectEqual(input, transposedPullback(transposed)) - expectEqual(input, transposedPermutationsPullback(transposed)) - expectEqual(input, transposedVariadicsPullback(transposed)) -} - -TensorADTests.testAllBackends("relu") { - let f = { (a: Tensor) in relu(a) } - expectEqual([1, 0, 0], gradient(at: [5, -5, 0], in: f)) -} - -TensorADTests.testAllBackends("softmax") { - let pb = pullback(at: Tensor(ones: [2, 2])) { (a: Tensor) in softmax(a) } - expectEqual([[0, 0], [0, 0]], pb([[1, 1], [1, 1]])) - expectEqual([[-0.25, 0.25], [0.75, -0.75]], pb([[1, 2], [4, 1]])) -} - -TensorADTests.testAllBackends("log_softmax") { - let pb = pullback(at: Tensor(ones: [3, 3])) { (a: Tensor) in logSoftmax(a) } - expectEqual(Tensor(repeating: 5.9604645e-08, shape: [3, 3]), pb(Tensor(ones: [3, 3]))) -} - -TensorADTests.testAllBackends("SR-9345: OwnedCheckpoints") { - @differentiable(vjp: vjpFoo) - func foo(_ x: Tensor) -> Tensor { - return Raw.identity(x) - } - func vjpFoo(_ x: Tensor) -> (Tensor, (Tensor) -> Tensor) { - return (foo(x), { v in v }) - } - func body(_ x: Tensor) -> Tensor { - return foo(foo(x)) - } - let pb = pullback(at: Tensor(Float(10)), in: body) - expectEqual(Tensor(1), pb(Tensor(1))) -} - -TensorADTests.testAllBackends("SR-9804: AD refcounting") { - func f(_ x: Tensor) -> Tensor { - return x - } - expectEqual(Tensor(1), gradient(at: Tensor(0), in: f)) -} - -let cube: (Tensor) -> Tensor = { $0 * $0 * $0 } -TensorADTests.testAllBackends("Differentiate global") { - expectEqual(Tensor(48), gradient(at: Tensor(4), in: cube)) -} - -TensorADTests.testAllBackends("Side effects") { - let foo: @differentiable (Tensor) -> Tensor = { x in - var a = x - a = a + x - a = a + x - return a + x - } - expectEqual(Tensor([8, 8]), pullback(at: Tensor(4), in: foo)([1, 1])) - - func bar(x: Tensor) -> Tensor { - var a = x - a = a * x - a = a * x - return a - } - expectEqual(Tensor(48), gradient(at: Tensor(4), in: bar)) -} - -TensorADTests.testAllBackends("broadcasted(toShape:)") { - func foo(tensor: Tensor, shape: Tensor) -> Tensor { - tensor.broadcasted(toShape: shape) - } - - var inputTensor: Tensor - var expected: Tensor - var pb: (Tensor) -> Tensor - - // [3,] -> [3,3] - pb = pullback(at: Tensor([99, 33, 55])) { x in - foo(tensor: x, shape: Tensor([3, 3])) - } - - // Test 1: same shape as parameter of pullback - inputTensor = Tensor([ - [1, 2, 3], - [1, 2, 3], - [1, 2, 3]] - ) - expected = Tensor([3, 6, 9]) - expectEqual(expected, pb(inputTensor)) - - // Test 2: different shape than parameter of pullback - inputTensor = Tensor([ - [1, 2, 3], - [1, 2, 3], - [1, 2, 3], - [1, 2, 3]] - ) - expected = Tensor([4, 8, 12]) - expectEqual(expected, pb(inputTensor)) - - // Test 3: same shape as tensor we are differentiating at - inputTensor = Tensor([1, 2, 3]) - expected = Tensor([1, 2, 3]) - expectEqual(expected, pb(inputTensor)) - - // Test 4: extremely padded shape as tensor we are differentiating at - inputTensor = Tensor([[[[[[1, 2, 3]]]]]]) - expected = Tensor([1, 2, 3]) - expectEqual(expected, pb(inputTensor)) - - // [3,1] -> [3x3] - pb = pullback(at: Tensor([[99, 33, 55]])) { x in - foo(tensor: x, shape: Tensor([3, 3])) - } - - // Test 5: same shape as parameter of pullback - inputTensor = Tensor([ - [1, 2, 3], - [1, 2, 3], - [1, 2, 3]] - ) - expected = Tensor([[3, 6, 9]]) - expectEqual(expected, pb(inputTensor)) - - // Test 6: different shape than parameter of pullback - inputTensor = Tensor([ - [1, 2, 3], - [1, 2, 3], - [1, 2, 3], - [1, 2, 3]] - ) - expected = Tensor([[4, 8, 12]]) - expectEqual(expected, pb(inputTensor)) - - // Test 7: same shape as tensor we are differentiating at - inputTensor = Tensor([[1, 2, 3]]) - expected = Tensor([[1, 2, 3]]) - expectEqual(expected, pb(inputTensor)) - - // Test 8: extremely padded shape of tensor we are differentiating at - inputTensor = Tensor([[[[[[1, 2, 3]]]]]]) - expected = Tensor([[1, 2, 3]]) - expectEqual(expected, pb(inputTensor)) -} - -TensorADTests.testAllBackends("unbroadcasted(toShape:") { - func foo(tensor: Tensor, shape: Tensor) -> Tensor { - tensor.unbroadcasted(toShape: shape) - } - - var inputTensor: Tensor - var expected: Tensor - var pb: (Tensor) -> Tensor - - // [3,3] -> [1,3] - let atTensor: Tensor = Tensor([ - [1, 2, 3], - [1, 2, 3], - [1, 2, 3]] - ) - pb = pullback(at: atTensor) { x in - foo(tensor: x, shape: Tensor([1, 3])) - } - - // Test 1: same shape as parameter of pullback - inputTensor = Tensor([[1, 2, 3]]) - expected = atTensor - expectEqual(expected, pb(inputTensor)) - - // Test 2: different shape than parameter of pullback - inputTensor = Tensor([2]) - expected = Tensor([ - [2, 2, 2], - [2, 2, 2], - [2, 2, 2]] - ) - expectEqual(expected, pb(inputTensor)) - - // Test 3: same shape as tensor we are differentiating at - inputTensor = Tensor([ - [8, 1, 3], - [8, 1, 3], - [8, 1, 3]] - ) - expected = inputTensor - expectEqual(expected, pb(inputTensor)) - - // TODO - // Test 4: extremely padded shape as tensor we are differentiating at - // inputTensor = Tensor([ - // [[8, 1, 3]], - // [[8, 1, 3]], - // [[8, 1, 3]]] - // ) - // expected = Tensor([1, 2, 3]) - // expectEqual(expected, pb(inputTensor)) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/tensor_debuglog.swift b/test/TensorFlowRuntime/tensor_debuglog.swift deleted file mode 100644 index 68cde03f810b5..0000000000000 --- a/test/TensorFlowRuntime/tensor_debuglog.swift +++ /dev/null @@ -1,29 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Tensor API tests, with debug logging enabled. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var TensorTests = TestSuite("TensorDebug") - -// Exercise debug logging, to make sure the extra code only executed under debug -// logging will not crash the binary or change its output. -TensorTests.testAllBackends("XWPlusB") { - _RuntimeConfig.printsDebugLog = true - - // Shape: 1 x 4 - let x = Tensor([[1.0, 2.0, 2.0, 1.0]]) - // Shape: 4 x 2 - let w = Tensor([[1.0, 0.0], [3.0, 0.0], [2.0, 3.0], [1.0, 0.0]]) - // Shape: 2 - let b = Tensor([0.5, 0.5]) - // Do xW+b! - let result = x • w + b - expectEqual([1, 2], result.shape) - expectEqual([12.5, 6.5], result.scalars) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/tensor_group.swift b/test/TensorFlowRuntime/tensor_group.swift deleted file mode 100644 index f80df287f19a2..0000000000000 --- a/test/TensorFlowRuntime/tensor_group.swift +++ /dev/null @@ -1,212 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %target-build-swift -swift-version 5 -g %s -o %t/a.out -// RUN: %target-codesign %t/a.out -// RUN: %target-run %t/a.out -// REQUIRES: executable_test -// -// `TensorGroup` tests. - -import CTensorFlow -import TensorFlow -import StdlibUnittest - -var TensorGroupTests = TestSuite("TensorGroup") - -extension TensorDataType : Equatable { - public static func == (lhs: TensorDataType, rhs: TensorDataType) -> Bool { - return Int(lhs._cDataType.rawValue) == Int(rhs._cDataType.rawValue) - } -} - -struct Empty : TensorGroup {} - -struct Simple : TensorGroup, Equatable { - var w, b: Tensor -} - -struct Mixed : TensorGroup, Equatable { - // Mutable. - var float: Tensor - // Immutable. - let int: Tensor -} - -struct Nested : TensorGroup, Equatable { - // Immutable. - let simple: Simple - // Mutable. - var mixed: Mixed -} - -struct Generic : TensorGroup, Equatable { - var t: T - var u: U -} - -TensorGroupTests.test("EmptyTypeList") { - expectEqual([], Empty._typeList) -} - -TensorGroupTests.test("SimpleTypeList") { - let float = Float.tensorFlowDataType - expectEqual([float, float], Simple._typeList) -} - -TensorGroupTests.test("SimpleInit") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - - let status = TF_NewStatus() - let wHandle = TFE_TensorHandleCopySharingTensor(w.handle._cTensorHandle, status)! - let bHandle = TFE_TensorHandleCopySharingTensor(b.handle._cTensorHandle, status)! - TF_DeleteStatus(status) - - let buffer = UnsafeMutableBufferPointer.allocate(capacity: 2) - buffer.initialize(from: [wHandle, bHandle]) - let expectedSimple = Simple(_owning: UnsafePointer(buffer.baseAddress)) - - expectEqual(expectedSimple, simple) -} - -TensorGroupTests.test("MixedTypeList") { - let float = Float.tensorFlowDataType - let int = Int32.tensorFlowDataType - expectEqual([float, int], Mixed._typeList) -} - -TensorGroupTests.test("MixedInit") { - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(float: float, int: int) - - let status = TF_NewStatus() - let floatHandle = TFE_TensorHandleCopySharingTensor(float.handle._cTensorHandle, status)! - let intHandle = TFE_TensorHandleCopySharingTensor(int.handle._cTensorHandle, status)! - TF_DeleteStatus(status) - - let buffer = UnsafeMutableBufferPointer.allocate(capacity: 2) - buffer.initialize(from: [floatHandle, intHandle]) - let expectedMixed = Mixed(_owning: UnsafePointer(buffer.baseAddress)) - - expectEqual(expectedMixed, mixed) -} - -TensorGroupTests.test("NestedTypeList") { - let float = Float.tensorFlowDataType - let int = Int32.tensorFlowDataType - expectEqual([float, float, float, int], Nested._typeList) -} - -TensorGroupTests.test("NestedInit") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(float: float, int: int) - let nested = Nested(simple: simple, mixed: mixed) - - let status = TF_NewStatus() - let wHandle = TFE_TensorHandleCopySharingTensor(w.handle._cTensorHandle, status)! - let bHandle = TFE_TensorHandleCopySharingTensor(b.handle._cTensorHandle, status)! - let floatHandle = TFE_TensorHandleCopySharingTensor(float.handle._cTensorHandle, status)! - let intHandle = TFE_TensorHandleCopySharingTensor(int.handle._cTensorHandle, status)! - TF_DeleteStatus(status) - - let buffer = UnsafeMutableBufferPointer.allocate(capacity: 4) - buffer.initialize(from: [wHandle, bHandle, floatHandle, intHandle]) - let expectedNested = Nested(_owning: UnsafePointer(buffer.baseAddress)) - - expectEqual(expectedNested, nested) -} - -TensorGroupTests.test("GenericTypeList") { - let float = Float.tensorFlowDataType - let int = Int32.tensorFlowDataType - expectEqual([float, float, float, int], Generic._typeList) -} - -TensorGroupTests.test("GenericInit") { - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(float: float, int: int) - let generic = Generic(t: simple, u: mixed) - - let status = TF_NewStatus() - let wHandle = TFE_TensorHandleCopySharingTensor(w.handle._cTensorHandle, status)! - let bHandle = TFE_TensorHandleCopySharingTensor(b.handle._cTensorHandle, status)! - let floatHandle = TFE_TensorHandleCopySharingTensor(float.handle._cTensorHandle, status)! - let intHandle = TFE_TensorHandleCopySharingTensor(int.handle._cTensorHandle, status)! - TF_DeleteStatus(status) - - let buffer = UnsafeMutableBufferPointer.allocate(capacity: 4) - buffer.initialize(from: [wHandle, bHandle, floatHandle, intHandle]) - let expectedGeneric = Generic(_owning: UnsafePointer(buffer.baseAddress)) - - expectEqual(expectedGeneric, generic) -} - -TensorGroupTests.test("NestedGenericTypeList") { - struct NestedGeneric { - func function() { - struct UltraNested : TensorGroup, Equatable { - var a: Generic - var b: Generic - } - let float = Float.tensorFlowDataType - let int = Int32.tensorFlowDataType - expectEqual([float, float, float, int, float, int, float, float], - UltraNested._typeList) - } - } - - NestedGeneric().function() -} - -TensorGroupTests.test("NestedGenericInit") { - struct NestedGeneric { - func function() { - struct UltraNested : TensorGroup, Equatable { - var a: Generic - var b: Generic - } - - let w = Tensor(0.1) - let b = Tensor(0.1) - let simple = Simple(w: w, b: b) - let float = Tensor(0.1) - let int = Tensor(1) - let mixed = Mixed(float: float, int: int) - let genericSM = Generic(t: simple, u: mixed) - let genericMS = Generic(t: mixed, u: simple) - let generic = UltraNested(a: genericSM, b: genericMS) - - let status = TF_NewStatus() - let wHandle1 = TFE_TensorHandleCopySharingTensor(w.handle._cTensorHandle, status)! - let wHandle2 = TFE_TensorHandleCopySharingTensor(w.handle._cTensorHandle, status)! - let bHandle1 = TFE_TensorHandleCopySharingTensor(b.handle._cTensorHandle, status)! - let bHandle2 = TFE_TensorHandleCopySharingTensor(b.handle._cTensorHandle, status)! - let floatHandle1 = TFE_TensorHandleCopySharingTensor(float.handle._cTensorHandle, status)! - let floatHandle2 = TFE_TensorHandleCopySharingTensor(float.handle._cTensorHandle, status)! - let intHandle1 = TFE_TensorHandleCopySharingTensor(int.handle._cTensorHandle, status)! - let intHandle2 = TFE_TensorHandleCopySharingTensor(int.handle._cTensorHandle, status)! - TF_DeleteStatus(status) - - let buffer = UnsafeMutableBufferPointer.allocate(capacity: 8) - buffer.initialize(from: [wHandle1, bHandle1, floatHandle1, intHandle1, - floatHandle2, intHandle2, wHandle2, bHandle2]) - let expectedGeneric = UltraNested( - _owning: UnsafePointer(buffer.baseAddress)) - - expectEqual(expectedGeneric, generic) - } - } - - NestedGeneric().function() -} - -runAllTests() diff --git a/test/TensorFlowRuntime/top_level_1.swift b/test/TensorFlowRuntime/top_level_1.swift deleted file mode 100644 index 1625f07f8c152..0000000000000 --- a/test/TensorFlowRuntime/top_level_1.swift +++ /dev/null @@ -1,22 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -// This tests various issues with top level code, ensuring deabstraction and -// other things work here. This is not intended to be a place to test device -// specifics, so we can keep it simple and just test CPU. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var TopLevelTests = TestSuite("TopLevel") - -#if !CUDA -TopLevelTests.testAllBackends("TopLevel") { - var x = Tensor([1,2,3])*2 - x = x + x - expectEqual(x.array, ShapedArray(shape: [3], scalars: [4, 8, 12])) -} -#endif - -runAllTests() diff --git a/test/TensorFlowRuntime/top_level_2.swift b/test/TensorFlowRuntime/top_level_2.swift deleted file mode 100644 index 76f59cf5b956e..0000000000000 --- a/test/TensorFlowRuntime/top_level_2.swift +++ /dev/null @@ -1,25 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -// This file contains stubs for functions that the playground transform in -// lib/Sema/PlaygroundTransform.cpp generates calls into. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var TopLevelTests = TestSuite("TopLevel") - -let g = Tensor(1.0) - -// This function is not inlined into main(). Confirm that it can read the global -// var properly and does not crash. -func SR8405() { - expectNearlyEqualWithScalarTensor(1.0, g) -} - -TopLevelTests.testAllBackends("TopLevel") { - SR8405() -} - -runAllTests() diff --git a/test/TensorFlowRuntime/tracer.swift b/test/TensorFlowRuntime/tracer.swift deleted file mode 100644 index 4985efe0e056b..0000000000000 --- a/test/TensorFlowRuntime/tracer.swift +++ /dev/null @@ -1,238 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Tracer tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var TracerTests = TestSuite("Tracer") - -typealias Data = Tensor - -typealias Result = Tensor - -extension Tensor : _TensorArrayProtocolEnhanced { - public func _makeInstance(owning inputs: C) -> Tensor - where C.Element == CTensorHandle { - assert(inputs.count == 1) - return Tensor(handle: TensorHandle(_owning: inputs.first!)) - } -} - -TracerTests.testAllBackends("Basic") { - func tracee(state: Tensor, data: Data) -> (Tensor, Result) { - return (state + data, data) - } - - // TODO: if we instead write Tensor(2.0), which defaults to Tensor, - // the resulting error message is hard to understand: - // error: cannot convert value of type '(Tensor, Data) -> - // (Tensor, Result)' (aka '(Tensor, Tensor) -> - // (Tensor, Tensor)') to expected argument type '(_, _) -> (_, - // _)' - _RuntimeConfig.printsDebugLog = true - let state = Tensor(2.0) - let data = Tensor(3.0) - let tracedFn = _graph(with: state, in: tracee) - let (newState, result) = tracedFn(state, data) - - print(newState) - expectNearlyEqualWithScalarTensor(5.0, newState) - - print(result) - expectNearlyEqualWithScalarTensor(3.0, result) - - // A second call to `tracedFn` with different input data. - let data2 = Tensor(1.0) - let (newState2, result2) = tracedFn(newState, data2) - - print(newState2) - expectNearlyEqualWithScalarTensor(6.0, newState2) - - print(result2) - expectNearlyEqualWithScalarTensor(1.0, result2) -} - -TracerTests.testAllBackends("AllDtypeSupport") { - func tracee(state: S, data: S) -> (S, S) { - return (state, data) - } - - func traceAndRun(_ v: T) -> (Tensor, Tensor) - where T: TensorFlowScalar & Numeric { - let state = Tensor(v) - let data = Tensor(v + 4) - let tracedFn = _graph(with: state, in: tracee) - return tracedFn(state, data) - } - - func traceAndCheckNearlyEqual(_ v: T) - where T: TensorFlowScalar & FloatingPoint & ExpressibleByFloatLiteral { - let (newState, newData) = traceAndRun(v) - expectNearlyEqualWithScalarTensor(v, newState) - expectNearlyEqualWithScalarTensor(v + 4, newData) - } - - func traceAndCheckEqual(_ v: T) - where T: TensorFlowScalar & Numeric & Comparable { - let (newState, newData) = traceAndRun(v) - expectEqualWithScalarTensor(v, newState) - expectEqualWithScalarTensor(v + 4, newData) - } - - // FloatingPoint - let f: Float = 1.0 - traceAndCheckNearlyEqual(f) - let d: Double = 2.0 - traceAndCheckNearlyEqual(d) - - // Int variants - let ui8: UInt8 = 1 - traceAndCheckEqual(ui8) - let i8: Int8 = 2 - traceAndCheckEqual(i8) - let ui16: UInt16 = 3 - traceAndCheckEqual(ui16) - let i16: Int16 = 4 - traceAndCheckEqual(i16) - let ui32: UInt32 = 5 - traceAndCheckEqual(ui32) - let i32: Int32 = 6 - traceAndCheckEqual(i32) - let ui64: UInt64 = 7 - traceAndCheckEqual(ui64) - let i64: Int64 = 8 - traceAndCheckEqual(i64) - - // Bool - let boolTF = _graph(with: Tensor(true), in: tracee) - let (bs, bd) = boolTF(Tensor(true), Tensor(false)) - expectTrue(bs.scalarized()) - expectFalse(bd.scalarized()) -} - -TracerTests.testAllBackends("TraceWithNoResult") { - func add(state: Tensor, data: Data) -> (Tensor) { - return state + data - } - let tracedAdd = _graph(with: Tensor(2.0), in: add) - let three = Tensor(3.0) - expectNearlyEqualWithScalarTensor(5.0, tracedAdd(Tensor(2.0), three)) - expectNearlyEqualWithScalarTensor(6.0, tracedAdd(Tensor(3.0), three)) - expectNearlyEqualWithScalarTensor(8.0, tracedAdd(Tensor(5.0), three)) -} - -TracerTests.testAllBackends("TracerWithInOut") { - func addOne(state: Tensor) -> (Tensor) { - return state + 1 - } - let addOneGraph = _graph(addOne) - expectEqual(addOneGraph(Tensor(5)), Tensor(6)) - expectEqual(addOneGraph(Tensor(0)), Tensor(1)) - expectEqual(addOneGraph(Tensor(-1)), Tensor(0)) -} - -TracerTests.testAllBackends("Basic_IntermediateTensors") { - func tracee(state: Tensor, data: Data) -> (Tensor, Result) { - // Create an intermediate tensor value, which the tracing infra needs to - // convert into a placeholder input into the generated trace graph function. - let tmp = Tensor(1.0) - return (tmp, tmp + data) - } - - let state = Tensor(2.0) - let data = Tensor(3.0) - let tracedFn = _graph(with: state, in: tracee) - let (newState, result) = tracedFn(state, data) - - print(newState) - expectNearlyEqualWithScalarTensor(1.0, newState) - - print(result) - expectNearlyEqualWithScalarTensor(4.0, result) -} - -TracerTests.testAllBackends("Advanced") { - typealias Model = [Tensor] - - typealias Optimizer = [Tensor] - - struct State : _TensorArrayProtocolEnhanced { - var model: Model = [Tensor(1.0), Tensor(2.0)] - var optimizer: Optimizer = [Tensor(1.0), Tensor(2.0)] - - public init() {} - - public init(_owning tensorHandles: UnsafePointer?, count: Int) { - self.model = [ - Tensor(_owning: tensorHandles), - Tensor(_owning: tensorHandles?.advanced(by: 1))] - self.optimizer = [ - Tensor(_owning: tensorHandles?.advanced(by: 2)), - Tensor(_owning: tensorHandles?.advanced(by: 3))] - } - - public func _unpackTensorHandles(into address: UnsafeMutablePointer?) { - print("Calling State._unpackTensorHandles().") - var ptr = address - model._unpackTensorHandles(into: ptr) - ptr = ptr!.advanced(by: Int(model._tensorHandleCount)) - optimizer._unpackTensorHandles(into: ptr) - } - - public var _tensorHandleCount: Int32 { - return model._tensorHandleCount + optimizer._tensorHandleCount - } - - public var _typeList: [TensorDataType] { - return model._typeList + optimizer._typeList - } - - func _makeInstance(owning inputs: C) -> State - where C.Element == CTensorHandle { - assert(inputs.count == 4) - var abstractState = State() - let index0 = inputs.startIndex - let index1 = inputs.index(after: index0) - abstractState.model = [Tensor(handle: TensorHandle(_owning: inputs[index0])), - Tensor(handle: TensorHandle(_owning: inputs[index1]))] - let index2 = inputs.index(after: index1) - let index3 = inputs.index(after: index2) - abstractState.optimizer = [Tensor(handle: TensorHandle(_owning: inputs[index2])), - Tensor(handle: TensorHandle(_owning: inputs[index3]))] - return abstractState - } - } - - func tracee(state: State, data: Data) -> (State, Result) { - print("Running tracee()") - var tmp = Tensor(0.0) - for i in 0..(3.0) - let tracedFn = _graph(with: state, in: tracee) - let (newState, result) = tracedFn(state, data) - - print(newState) // should be State(model: [3.0, 2.0], optimizer: [1.0, 2.0]) - expectNearlyEqualWithScalarTensor(3.0, newState.model[0]) - expectNearlyEqualWithScalarTensor(2.0, newState.model[1]) - expectNearlyEqualWithScalarTensor(1.0, newState.optimizer[0]) - expectNearlyEqualWithScalarTensor(2.0, newState.optimizer[1]) - - print(result) // should be 8.0 - expectNearlyEqualWithScalarTensor(8.0, result) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/tracer_tffunc.swift b/test/TensorFlowRuntime/tracer_tffunc.swift deleted file mode 100644 index 6c8c6626c375c..0000000000000 --- a/test/TensorFlowRuntime/tracer_tffunc.swift +++ /dev/null @@ -1,46 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test -// -// Tracer tests. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var TracerTests = TestSuite("TracerTFFunction") - -extension Tensor : _TensorArrayProtocolEnhanced { - public func _makeInstance(owning inputs: C) -> Tensor - where C.Element == CTensorHandle { - assert(inputs.count == 1) - return Tensor(handle: TensorHandle(_owning: inputs.first!)) - } -} - -struct State : TensorGroup { - let i: Tensor - let n: Tensor -} - -TracerTests.testAllBackends("SimpleTFFunction") { - func cond(s: State) -> Tensor { - return Tensor(s.i .< s.n) - } - - func body(s: State) -> State { - return State(i: s.i + 1, n: s.n) - } - - func runWhile(_ n: Int32) -> Tensor { - return Raw.while_( - State(i: Tensor(0), n: Tensor(n)), - cond: cond, - body: body, - outputShapes: [nil]).i - } - - expectEqualWithScalarTensor(10, runWhile(10)) - expectEqualWithScalarTensor(300, runWhile(300)) -} - -runAllTests() diff --git a/test/TensorFlowRuntime/with_device_1.swift b/test/TensorFlowRuntime/with_device_1.swift deleted file mode 100644 index 75654823b02e8..0000000000000 --- a/test/TensorFlowRuntime/with_device_1.swift +++ /dev/null @@ -1,38 +0,0 @@ -// RUN: %target-run-simple-swift %swift-tensorflow-test-run-extra-options -// REQUIRES: executable_test - -// Tests on collective ops, as a building block for data/model parallel programs. - -import TensorFlow -import TensorFlowUnittest -import StdlibUnittest - -var WithDeviceTests = TestSuite("WithDevice") - -WithDeviceTests.testAllBackends("ConfigTest") { - // Run some tensor code to trigger runtime configuration. - print(Tensor(0.0) + Tensor(1.0)) - expectEqual(3, _RuntimeConfig.cpuDeviceCount) -} - -WithDeviceTests.testAllBackends("Basic") { - func foo() { - let x = Tensor(1.0) - let y = x + x - expectEqualWithScalarTensor(2, y) - print(y) - } - withDevice(.cpu, 0) { - foo() - } - withDevice(.cpu, 1) { - foo() - } - #if CUDA - withDevice(.gpu, 0) { - foo() - } - #endif -} - -runAllTests()