Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Port over tensor_autodiff_runtime.swift tests. #235

Merged
merged 6 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 134 additions & 4 deletions Tests/TensorFlowTests/OperatorTests/BasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -443,19 +443,60 @@ final class BasicOperatorTests: XCTestCase {
XCTAssertEqual(result.shape, [1, 3, 1, 2, 1])
}

func testUnbroadcast1() {
func testUnbroadcastRank4ToRank2() {
let x = Tensor<Float>(repeating: 1, shape: [2, 3, 4, 5])
let y = Tensor<Float>(repeating: 1, shape: [4, 5])
let z = x.unbroadcasted(like: y)
XCTAssertEqual(z.array, ShapedArray<Float>(repeating: 6, shape: [4, 5]))
}

func testUnbroadcast2() {
func testUnbroadcastRank4ToRank3() {
let x = Tensor<Float>(repeating: 1, shape: [2, 3, 4, 5])
let y = Tensor<Float>(repeating: 1, shape: [3, 1, 5])
let z = x.unbroadcasted(like: y)
XCTAssertEqual(z.array, ShapedArray<Float>(repeating: 8, shape: [3, 1, 5]))
}

func testUnbroadcast3x3To1x3() {
func foo(tensor: Tensor<Float>, shape: Tensor<Int32>) -> Tensor<Float> {
tensor.unbroadcasted(toShape: shape)
}
var inputTensor: Tensor<Float>
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason for the initialization to be an a different line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can initialize it to the one needed for the first subtest.

var expected: Tensor<Float>
var pb: (Tensor<Float>) -> Tensor<Float>

// [3,3] -> [1,3]
let atTensor: Tensor<Float> = Tensor([
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]
)
pb = pullback(at: atTensor) { x in
foo(tensor: x, shape: Tensor([1, 3]))
}

// Same shape as parameter of pullback
inputTensor = Tensor([[1, 2, 3]])
expected = atTensor
XCTAssertEqual(expected, pb(inputTensor))
// Different shape than parameter of pullback
inputTensor = Tensor([2])
expected = Tensor([
[2, 2, 2],
[2, 2, 2],
[2, 2, 2]]
)
XCTAssertEqual(expected, pb(inputTensor))

// Same shape as tensor we are differentiating at
inputTensor = Tensor([
[8, 1, 3],
[8, 1, 3],
[8, 1, 3]]
)
expected = inputTensor
XCTAssertEqual(expected, pb(inputTensor))
}

func testSliceUpdate() {
var t1 = Tensor<Float>([[1, 2, 3], [4, 5, 6]])
Expand All @@ -482,6 +523,92 @@ final class BasicOperatorTests: XCTestCase {
target .= Tensor(repeating: 1, shape: [1, 3, 1])
XCTAssertEqual(target, Tensor(repeating: 1, shape: [2, 3, 4]))
}

func testBroadcast3x0To3x3() {
func foo(tensor: Tensor<Float>, shape: Tensor<Int32>) -> Tensor<Float> {
tensor.broadcasted(toShape: shape)
}
var inputTensor: Tensor<Float>
var expected: Tensor<Float>
var pb: (Tensor<Float>) -> Tensor<Float>

// [3,] -> [3,3]
pb = pullback(at: Tensor([99, 33, 55])) { x in
foo(tensor: x, shape: Tensor([3, 3]))
}

// Same shape as parameter of pullback
inputTensor = Tensor([
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]
)
expected = Tensor([3, 6, 9])
XCTAssertEqual(expected, pb(inputTensor))

// 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])
XCTAssertEqual(expected, pb(inputTensor))

// Same shape as tensor we are differentiating at
inputTensor = Tensor([1, 2, 3])
expected = Tensor([1, 2, 3])
XCTAssertEqual(expected, pb(inputTensor))

// Extremely padded shape as tensor we are differentiating at
inputTensor = Tensor([[[[[[1, 2, 3]]]]]])
expected = Tensor([1, 2, 3])
XCTAssertEqual(expected, pb(inputTensor))
}

func testBroadcast3x1To3x3() {
func foo(tensor: Tensor<Float>, shape: Tensor<Int32>) -> Tensor<Float> {
tensor.broadcasted(toShape: shape)
}
var inputTensor: Tensor<Float>
var expected: Tensor<Float>
var pb: (Tensor<Float>) -> Tensor<Float>

// [3,1] -> [3x3]
pb = pullback(at: Tensor([[99, 33, 55]])) { x in
foo(tensor: x, shape: Tensor([3, 3]))
}

// Same shape as parameter of pullback
inputTensor = Tensor([
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]
)
expected = Tensor([[3, 6, 9]])
XCTAssertEqual(expected, pb(inputTensor))

// 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]])
XCTAssertEqual(expected, pb(inputTensor))

// Same shape as tensor we are differentiating at
inputTensor = Tensor([[1, 2, 3]])
expected = Tensor([[1, 2, 3]])
XCTAssertEqual(expected, pb(inputTensor))

// Extremely padded shape of tensor we are differentiating at
inputTensor = Tensor([[[[[[1, 2, 3]]]]]])
expected = Tensor([[1, 2, 3]])
XCTAssertEqual(expected, pb(inputTensor))
}

static var allTests = [
("testGathering", testGathering),
Expand All @@ -507,9 +634,12 @@ final class BasicOperatorTests: XCTestCase {
("testFlatten0D", testFlatten0D),
("testReshapeToScalar", testReshapeToScalar),
("testReshapeTensor", testReshapeTensor),
("testUnbroadcast1", testUnbroadcast1),
("testUnbroadcast2", testUnbroadcast2),
("testUnbroadcastRank4ToRank2", testUnbroadcastRank4ToRank2),
("testUnbroadcastRank4ToRank3", testUnbroadcastRank4ToRank3),
("testUnbroadcast3x3To1x3", testUnbroadcast3x3To1x3),
("testSliceUpdate", testSliceUpdate),
("testBroadcast3x0To3x3", testBroadcast3x0To3x3),
("testBroadcast3x1To3x3", testBroadcast3x1To3x3),
("testBroadcastTensor", testBroadcastTensor)
]
}
Loading