chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
# Description:
# JavaScript/TypeScript code generation for TensorFlow.js
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load(
"//tensorflow:tensorflow.bzl",
"tf_cc_test",
)
visibility = [
"//tensorflow:internal",
]
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
default_visibility = visibility,
licenses = ["notice"],
)
cc_library(
name = "ts_op_gen",
srcs = [
"ops/ts_op_gen.cc",
],
hdrs = [
"ops/ts_op_gen.h",
],
visibility = ["//visibility:public"],
deps = [
"//tensorflow/core:framework",
"//tensorflow/core:lib",
"//tensorflow/core:lib_internal",
"//tensorflow/core:op_gen_lib",
"//tensorflow/core:protos_all_cc",
],
)
tf_cc_test(
name = "ts_op_gen_test",
srcs = [
"ops/ts_op_gen.cc",
"ops/ts_op_gen.h",
"ops/ts_op_gen_test.cc",
],
deps = [
"//tensorflow/core:framework",
"//tensorflow/core:lib",
"//tensorflow/core:lib_internal",
"//tensorflow/core:op_gen_lib",
"//tensorflow/core:protos_all_cc",
"//tensorflow/core:test",
"//tensorflow/core:test_main",
],
)
+293
View File
@@ -0,0 +1,293 @@
/* Copyright 2018 The TensorFlow Authors. 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.
==============================================================================*/
#include "tensorflow/js/ops/ts_op_gen.h"
#include <memory>
#include <unordered_map>
#include <vector>
#include "tensorflow/core/framework/api_def.pb.h"
#include "tensorflow/core/framework/op_def_util.h"
#include "tensorflow/core/lib/gtl/map_util.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/public/version.h"
namespace tensorflow {
namespace {
static bool IsListAttr(const OpDef_ArgDef& arg) {
return !arg.type_list_attr().empty() || !arg.number_attr().empty();
}
// Struct to hold a combo OpDef and ArgDef for a given Op argument:
struct ArgDefs {
ArgDefs(const OpDef::ArgDef& op_def_arg, const ApiDef::Arg& api_def_arg)
: op_def_arg(op_def_arg), api_def_arg(api_def_arg) {}
const OpDef::ArgDef& op_def_arg;
const ApiDef::Arg& api_def_arg;
};
// Struct to hold a combo OpDef::AttrDef and ApiDef::Attr for an Op.
struct OpAttrs {
OpAttrs(const OpDef::AttrDef& op_def_attr, const ApiDef::Attr& api_def_attr)
: op_def_attr(op_def_attr), api_def_attr(api_def_attr) {}
const OpDef::AttrDef& op_def_attr;
const ApiDef::Attr& api_def_attr;
};
// Helper class to generate TypeScript code for a given OpDef:
class GenTypeScriptOp {
public:
GenTypeScriptOp(const OpDef& op_def, const ApiDef& api_def);
~GenTypeScriptOp();
// Returns the generated code as a string:
std::string Code();
private:
void ProcessArgs();
void ProcessAttrs();
void AddAttrForArg(const std::string& attr, int arg_index);
std::string InputForAttr(const OpDef::AttrDef& op_def_attr);
void AddMethodSignature();
void AddOpAttrs();
void AddMethodReturnAndClose();
const OpDef& op_def_;
const ApiDef& api_def_;
// Placeholder string for all generated code:
std::string result_;
// Holds in-order vector of Op inputs:
std::vector<ArgDefs> input_op_args_;
// Holds in-order vector of Op attributes:
std::vector<OpAttrs> op_attrs_;
// Stores attributes-to-arguments by name:
typedef std::unordered_map<std::string, std::vector<int>> AttrArgIdxMap;
AttrArgIdxMap attr_arg_idx_map_;
// Holds number of outputs:
int num_outputs_;
};
GenTypeScriptOp::GenTypeScriptOp(const OpDef& op_def, const ApiDef& api_def)
: op_def_(op_def), api_def_(api_def), num_outputs_(0) {}
GenTypeScriptOp::~GenTypeScriptOp() = default;
std::string GenTypeScriptOp::Code() {
ProcessArgs();
ProcessAttrs();
// Generate exported function for Op:
AddMethodSignature();
AddOpAttrs();
AddMethodReturnAndClose();
absl::StrAppend(&result_, "\n");
return result_;
}
void GenTypeScriptOp::ProcessArgs() {
for (int i = 0; i < api_def_.arg_order_size(); i++) {
auto op_def_arg = FindInputArg(api_def_.arg_order(i), op_def_);
if (op_def_arg == nullptr) {
LOG(WARNING) << "Could not find OpDef::ArgDef for "
<< api_def_.arg_order(i);
continue;
}
auto api_def_arg = FindInputArg(api_def_.arg_order(i), api_def_);
if (api_def_arg == nullptr) {
LOG(WARNING) << "Could not find ApiDef::Arg for "
<< api_def_.arg_order(i);
continue;
}
// Map attr names to arg indexes:
if (!op_def_arg->type_attr().empty()) {
AddAttrForArg(op_def_arg->type_attr(), i);
} else if (!op_def_arg->type_list_attr().empty()) {
AddAttrForArg(op_def_arg->type_list_attr(), i);
}
if (!op_def_arg->number_attr().empty()) {
AddAttrForArg(op_def_arg->number_attr(), i);
}
input_op_args_.push_back(ArgDefs(*op_def_arg, *api_def_arg));
}
num_outputs_ = api_def_.out_arg_size();
}
void GenTypeScriptOp::ProcessAttrs() {
for (int i = 0; i < op_def_.attr_size(); i++) {
op_attrs_.push_back(OpAttrs(op_def_.attr(i), api_def_.attr(i)));
}
}
void GenTypeScriptOp::AddAttrForArg(const std::string& attr, int arg_index) {
// Keep track of attributes-to-arguments by name. These will be used for
// construction Op attributes that require information about the inputs.
auto iter = attr_arg_idx_map_.find(attr);
if (iter == attr_arg_idx_map_.end()) {
attr_arg_idx_map_.insert(AttrArgIdxMap::value_type(attr, {arg_index}));
} else {
iter->second.push_back(arg_index);
}
}
std::string GenTypeScriptOp::InputForAttr(const OpDef::AttrDef& op_def_attr) {
std::string inputs;
auto arg_list = attr_arg_idx_map_.find(op_def_attr.name());
if (arg_list != attr_arg_idx_map_.end()) {
for (auto iter = arg_list->second.begin(); iter != arg_list->second.end();
++iter) {
absl::StrAppend(&inputs, input_op_args_[*iter].op_def_arg.name());
}
}
return inputs;
}
void GenTypeScriptOp::AddMethodSignature() {
absl::StrAppend(&result_, "export function ", api_def_.endpoint(0).name(),
"(");
bool is_first = true;
for (auto& in_arg : input_op_args_) {
if (is_first) {
is_first = false;
} else {
absl::StrAppend(&result_, ", ");
}
auto op_def_arg = in_arg.op_def_arg;
absl::StrAppend(&result_, op_def_arg.name(), ": ");
if (IsListAttr(op_def_arg)) {
absl::StrAppend(&result_, "tfc.Tensor[]");
} else {
absl::StrAppend(&result_, "tfc.Tensor");
}
}
if (num_outputs_ == 1) {
absl::StrAppend(&result_, "): tfc.Tensor {\n");
} else {
absl::StrAppend(&result_, "): tfc.Tensor[] {\n");
}
}
void GenTypeScriptOp::AddOpAttrs() {
absl::StrAppend(&result_, " const opAttrs = [\n");
bool is_first = true;
for (auto& attr : op_attrs_) {
if (is_first) {
is_first = false;
} else {
absl::StrAppend(&result_, ",\n");
}
// Append 4 spaces to start:
absl::StrAppend(&result_, " ");
if (attr.op_def_attr.type() == "type") {
// Type OpAttributes can be generated from a helper function:
strings::StrAppend(&result_, "createTensorsTypeOpAttr('",
attr.op_def_attr.name(), "', ",
InputForAttr(attr.op_def_attr), ")");
} else if (attr.op_def_attr.type() == "int") {
absl::StrAppend(&result_, "{name: '", attr.op_def_attr.name(), "', ");
absl::StrAppend(&result_, "type: nodeBackend().binding.TF_ATTR_INT, ");
absl::StrAppend(&result_, "value: ", InputForAttr(attr.op_def_attr),
".length}");
}
}
absl::StrAppend(&result_, "\n ];\n");
}
void GenTypeScriptOp::AddMethodReturnAndClose() {
absl::StrAppend(&result_, " return null;\n}\n");
}
void WriteTSOp(const OpDef& op_def, const ApiDef& api_def, WritableFile* ts) {
GenTypeScriptOp ts_op(op_def, api_def);
TF_CHECK_OK(ts->Append(GenTypeScriptOp(op_def, api_def).Code()));
}
void StartFile(WritableFile* ts_file) {
const std::string header =
R"header(/**
* @license
* Copyright 2018 Google Inc. 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.
* =============================================================================
*/
// This file is MACHINE GENERATED! Do not edit
import * as tfc from '@tensorflow/tfjs-core';
import {createTensorsTypeOpAttr, nodeBackend} from './op_utils';
)header";
TF_CHECK_OK(ts_file->Append(header));
}
} // namespace
void WriteTSOps(const OpList& ops, const ApiDefMap& api_def_map,
const std::string& ts_filename) {
Env* env = Env::Default();
std::unique_ptr<WritableFile> ts_file = nullptr;
TF_CHECK_OK(env->NewWritableFile(ts_filename, &ts_file));
StartFile(ts_file.get());
for (const auto& op_def : ops.op()) {
// Skip deprecated ops
if (op_def.has_deprecation() &&
op_def.deprecation().version() <= TF_GRAPH_DEF_VERSION) {
continue;
}
const auto* api_def = api_def_map.GetApiDef(op_def.name());
if (api_def->visibility() == ApiDef::VISIBLE) {
WriteTSOp(op_def, *api_def, ts_file.get());
}
}
TF_CHECK_OK(ts_file->Close());
}
} // namespace tensorflow
+31
View File
@@ -0,0 +1,31 @@
/* Copyright 2018 The TensorFlow Authors. 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.
==============================================================================*/
#ifndef TENSORFLOW_JS_OPS_TS_OP_GEN_H_
#define TENSORFLOW_JS_OPS_TS_OP_GEN_H_
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/framework/op_gen_lib.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
// Generated code is written to the file ts_filename:
void WriteTSOps(const OpList& ops, const ApiDefMap& api_def_map,
const std::string& ts_filename);
} // namespace tensorflow
#endif // TENSORFLOW_JS_OPS_TS_OP_GEN_H_
+240
View File
@@ -0,0 +1,240 @@
/* Copyright 2018 The TensorFlow Authors. 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.
==============================================================================*/
#include "tensorflow/js/ops/ts_op_gen.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/framework/op_gen_lib.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/test.h"
namespace tensorflow {
namespace {
void ExpectContainsStr(absl::string_view s, absl::string_view expected) {
EXPECT_TRUE(absl::StrContains(s, expected))
<< "'" << s << "' does not contain '" << expected << "'";
}
void ExpectDoesNotContainStr(absl::string_view s, absl::string_view expected) {
EXPECT_FALSE(absl::StrContains(s, expected))
<< "'" << s << "' does not contain '" << expected << "'";
}
constexpr char kBaseOpDef[] = R"(
op {
name: "Foo"
input_arg {
name: "images"
type_attr: "T"
number_attr: "N"
description: "Images to process."
}
input_arg {
name: "dim"
description: "Description for dim."
type: DT_FLOAT
}
output_arg {
name: "output"
description: "Description for output."
type: DT_FLOAT
}
attr {
name: "T"
type: "type"
description: "Type for images"
allowed_values {
list {
type: DT_UINT8
type: DT_INT8
}
}
default_value {
i: 1
}
}
attr {
name: "N"
type: "int"
has_minimum: true
minimum: 1
}
summary: "Summary for op Foo."
description: "Description for op Foo."
}
)";
// Generate TypeScript code
void GenerateTsOpFileText(const std::string& op_def_str,
const std::string& api_def_str,
std::string* ts_file_text) {
Env* env = Env::Default();
OpList op_defs;
protobuf::TextFormat::ParseFromString(
op_def_str.empty() ? kBaseOpDef : op_def_str, &op_defs);
ApiDefMap api_def_map(op_defs);
if (!api_def_str.empty()) {
TF_ASSERT_OK(api_def_map.LoadApiDef(api_def_str));
}
const std::string& tmpdir = testing::TmpDir();
const auto ts_file_path = io::JoinPath(tmpdir, "test.ts");
WriteTSOps(op_defs, api_def_map, ts_file_path);
TF_ASSERT_OK(ReadFileToString(env, ts_file_path, ts_file_text));
}
TEST(TsOpGenTest, TestImports) {
std::string ts_file_text;
GenerateTsOpFileText("", "", &ts_file_text);
const std::string expected = R"(
import * as tfc from '@tensorflow/tfjs-core';
import {createTensorsTypeOpAttr, nodeBackend} from './op_utils';
)";
ExpectContainsStr(ts_file_text, expected);
}
TEST(TsOpGenTest, InputSingleAndList) {
const std::string api_def = R"pb(
op { graph_op_name: "Foo" arg_order: "dim" arg_order: "images" }
)pb";
std::string ts_file_text;
GenerateTsOpFileText("", api_def, &ts_file_text);
const std::string expected = R"(
export function Foo(dim: tfc.Tensor, images: tfc.Tensor[]): tfc.Tensor {
)";
ExpectContainsStr(ts_file_text, expected);
}
TEST(TsOpGenTest, TestVisibility) {
const std::string api_def = R"(
op {
graph_op_name: "Foo"
visibility: HIDDEN
}
)";
std::string ts_file_text;
GenerateTsOpFileText("", api_def, &ts_file_text);
const std::string expected = R"(
export function Foo(images: tfc.Tensor[], dim: tfc.Tensor): tfc.Tensor {
)";
ExpectDoesNotContainStr(ts_file_text, expected);
}
TEST(TsOpGenTest, SkipDeprecated) {
const std::string op_def = R"(
op {
name: "DeprecatedFoo"
input_arg {
name: "input"
type_attr: "T"
description: "Description for input."
}
output_arg {
name: "output"
description: "Description for output."
type: DT_FLOAT
}
attr {
name: "T"
type: "type"
description: "Type for input"
allowed_values {
list {
type: DT_FLOAT
}
}
}
deprecation {
explanation: "Deprecated."
}
}
)";
std::string ts_file_text;
GenerateTsOpFileText(op_def, "", &ts_file_text);
ExpectDoesNotContainStr(ts_file_text, "DeprecatedFoo");
}
TEST(TsOpGenTest, MultiOutput) {
const std::string op_def = R"(
op {
name: "MultiOutputFoo"
input_arg {
name: "input"
description: "Description for input."
type_attr: "T"
}
output_arg {
name: "output1"
description: "Description for output 1."
type: DT_FLOAT
}
output_arg {
name: "output2"
description: "Description for output 2."
type: DT_FLOAT
}
attr {
name: "T"
type: "type"
description: "Type for input"
allowed_values {
list {
type: DT_FLOAT
}
}
}
summary: "Summary for op MultiOutputFoo."
description: "Description for op MultiOutputFoo."
}
)";
std::string ts_file_text;
GenerateTsOpFileText(op_def, "", &ts_file_text);
const std::string expected = R"(
export function MultiOutputFoo(input: tfc.Tensor): tfc.Tensor[] {
)";
ExpectContainsStr(ts_file_text, expected);
}
TEST(TsOpGenTest, OpAttrs) {
std::string ts_file_text;
GenerateTsOpFileText("", "", &ts_file_text);
const std::string expectedFooAttrs = R"(
const opAttrs = [
createTensorsTypeOpAttr('T', images),
{name: 'N', type: nodeBackend().binding.TF_ATTR_INT, value: images.length}
];
)";
ExpectContainsStr(ts_file_text, expectedFooAttrs);
}
} // namespace
} // namespace tensorflow