• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/lite/delegates/gpu/metal/common.h"
17
18#import <XCTest/XCTest.h>
19
20#include <string>
21#include <tuple>
22#include <vector>
23
24using ::tflite::gpu::metal::GetBestSupportedMetalDevice;
25using ::tflite::gpu::metal::CreateComputeProgram;
26
27@interface CommonTest : XCTestCase
28
29@end
30
31@implementation CommonTest
32
33- (void)testComputeShaderCompilation {
34  const std::string code = R"(\
35#include <metal_stdlib>
36using namespace metal;
37kernel void FunctionName(device TYPE* const src_buffer[[buffer(0)]],
38                         device TYPE* const dst_buffer[[buffer(1)]],
39                         constant int2& size[[buffer(2)]],
40                         uint3 gid[[thread_position_in_grid]]) {
41  if (int(gid.x) >= size.x || int(gid.y) >= size.y) {
42    return;
43  }
44  const int linear_index = (gid.z * size.y + gid.y) * size.x + gid.x;
45  dst_buffer[linear_index] = src_buffer[linear_index];
46}
47)";
48
49  id<MTLDevice> device = GetBestSupportedMetalDevice();
50  XCTAssertNotNil(device, @"The Metal device must exists on real device");
51  id<MTLComputePipelineState> program;
52  absl::Status status;
53
54  status = CreateComputeProgram(device, code, "FunctionName", {{"TYPE", "float4"}}, &program);
55  XCTAssertTrue(status.ok(), @"%s", std::string(status.message()).c_str());
56  XCTAssertNotNil(program);
57
58  status = CreateComputeProgram(device, code, "FunctionName", {{"TYPE", "half4"}}, &program);
59  XCTAssertTrue(status.ok(), @"%s", std::string(status.message()).c_str());
60  XCTAssertNotNil(program);
61
62  // This compilation is intended to be incorrect
63  program = nil;
64  status = CreateComputeProgram(device, code, "FunctionName", {{"TYPE", "some_undefined_value"}},
65                                &program);
66  XCTAssertFalse(status.ok(), @"Shader contains an error that has not been detected");
67  XCTAssertNil(program);
68}
69
70@end
71