Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add browser tests for CPU backend #7633

Merged
merged 7 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions tfjs-backend-cpu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test", "pkg_npm")
load("//tools:copy_to_dist.bzl", "copy_to_dist", "copy_ts_library_to_dist")
load("//tools:tfjs_bundle.bzl", "tfjs_bundle")
load("//tools:tfjs_web_test.bzl", "tfjs_web_test")

package(default_visibility = ["//visibility:public"])

Expand All @@ -29,6 +30,17 @@ nodejs_test(
tags = ["ci"],
)

tfjs_web_test(
name = "tfjs-backend-cpu_browser_test",
srcs = [
"//tfjs-backend-cpu/src:tfjs-backend-cpu_test_bundle",
],
args = [],
browsers = [],
headless = False,
presubmit_browsers = []
Copy link
Member

Choose a reason for hiding this comment

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

Nit: If this is empty, these tests won't run in presubmits. Is that intentional? Do the tests only work with headless = False?

Copy link
Collaborator Author

@Linchenn Linchenn Apr 28, 2023

Choose a reason for hiding this comment

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

Done. Thanks! Also removed core tests from this test target.

)

tfjs_bundle(
name = "tf-backend-cpu",
entry_point = "//tfjs-backend-cpu/src:index.ts",
Expand Down
43 changes: 41 additions & 2 deletions tfjs-backend-cpu/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.
# =============================================================================

load("//tools:defaults.bzl", "ts_library")
load("//tools:defaults.bzl", "esbuild", "ts_library")
load("//tools:enumerate_tests.bzl", "enumerate_tests")

package(default_visibility = ["//visibility:public"])

Expand All @@ -22,6 +23,23 @@ TEST_SRCS = [
"run_tests.ts",
]

filegroup(
name = "all_test_entrypoints",
srcs = glob(
["**/*_test.ts"],
exclude = [
"setup_test.ts",
],
),
)

# Generates the 'tests.ts' file that imports all test entrypoints.
enumerate_tests(
name = "tests",
srcs = [":all_test_entrypoints"],
root_path = "tfjs-backend-cpu/src",
)

ts_library(
name = "tfjs-backend-cpu_src_lib",
srcs = glob(
Expand All @@ -48,7 +66,9 @@ ts_library(
ts_library(
name = "tfjs-backend-cpu_test_lib",
testonly = True,
srcs = glob(TEST_SRCS),
srcs = glob(TEST_SRCS) + [
":tests",
],
module_name = "@tensorflow/tfjs-backend-cpu/dist",
deps = [
":tfjs-backend-cpu_lib",
Expand All @@ -60,3 +80,22 @@ ts_library(
"@npm//jasmine",
],
)

esbuild(
name = "tfjs-backend-cpu_test_bundle",
testonly = True,
entry_point = "setup_test.ts",
external = [
# webworker tests call 'require('@tensorflow/tfjs')', which
# is external to the test bundle.
# Note: This is not a bazel target. It's just a string.
"@tensorflow/tfjs",
"worker_threads",
"util",
],
sources_content = True,
deps = [
":tfjs-backend-cpu_lib",
":tfjs-backend-cpu_test_lib",
],
)
56 changes: 56 additions & 0 deletions tfjs-backend-cpu/src/setup_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license
* Copyright 2023 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import '@tensorflow/tfjs-backend-cpu';
// Register the backend.
import './index';
// tslint:disable-next-line: no-imports-from-dist
import '@tensorflow/tfjs-core/dist/public/chained_ops/register_all_chained_ops';
// tslint:disable-next-line: no-imports-from-dist
import '@tensorflow/tfjs-core/dist/register_all_gradients';

// tslint:disable-next-line: no-imports-from-dist
import {parseTestEnvFromKarmaFlags, setTestEnvs, setupTestFilters, TEST_ENVS, TestFilter} from '@tensorflow/tfjs-core/dist/jasmine_util';

setTestEnvs([{name: 'cpu', backendName: 'cpu', isDataSync: true}]);

const TEST_FILTERS: TestFilter[] = [];
const customInclude = (testName: string) => {
return true;
};
setupTestFilters(TEST_FILTERS, customInclude);

// Allow flags to override test envs
// tslint:disable-next-line:no-any
declare let __karma__: any;
if (typeof __karma__ !== 'undefined') {
const testEnv = parseTestEnvFromKarmaFlags(__karma__.config.args, TEST_ENVS);
if (testEnv != null) {
setTestEnvs([testEnv]);
}
}

// These use 'require' because they must not be hoisted above
// the preceding snippet that parses test environments.
// Import and run tests from core.
// tslint:disable-next-line:no-imports-from-dist
// tslint:disable-next-line:no-require-imports
require('@tensorflow/tfjs-core/dist/tests');
// Import and run tests from cpu.
// tslint:disable-next-line:no-imports-from-dist
// tslint:disable-next-line:no-require-imports
require('./tests');